1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <unordered_set>
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: libcpp-no-deduction-guides
12 // UNSUPPORTED: apple-clang-9.1
13 
14 // template<class InputIterator,
15 //        class Hash = hash<iter-value-type<InputIterator>>,
16 //        class Pred = equal_to<iter-value-type<InputIterator>>,
17 //        class Allocator = allocator<iter-value-type<InputIterator>>>
18 // unordered_set(InputIterator, InputIterator, typename see below::size_type = see below,
19 //               Hash = Hash(), Pred = Pred(), Allocator = Allocator())
20 //   -> unordered_set<iter-value-type<InputIterator>,
21 //                    Hash, Pred, Allocator>;
22 //
23 // template<class T, class Hash = hash<T>,
24 //        class Pred = equal_to<T>, class Allocator = allocator<T>>
25 // unordered_set(initializer_list<T>, typename see below::size_type = see below,
26 //               Hash = Hash(), Pred = Pred(), Allocator = Allocator())
27 //   -> unordered_set<T, Hash, Pred, Allocator>;
28 //
29 // template<class InputIterator, class Allocator>
30 // unordered_set(InputIterator, InputIterator, typename see below::size_type, Allocator)
31 //   -> unordered_set<iter-value-type<InputIterator>,
32 //                    hash<iter-value-type<InputIterator>>,
33 //                    equal_to<iter-value-type<InputIterator>>,
34 //                    Allocator>;
35 //
36 // template<class InputIterator, class Hash, class Allocator>
37 // unordered_set(InputIterator, InputIterator, typename see below::size_type,
38 //               Hash, Allocator)
39 //   -> unordered_set<iter-value-type<InputIterator>, Hash,
40 //                    equal_to<iter-value-type<InputIterator>>,
41 //                    Allocator>;
42 //
43 // template<class T, class Allocator>
44 // unordered_set(initializer_list<T>, typename see below::size_type, Allocator)
45 //   -> unordered_set<T, hash<T>, equal_to<T>, Allocator>;
46 //
47 // template<class T, class Hash, class Allocator>
48 // unordered_set(initializer_list<T>, typename see below::size_type, Hash, Allocator)
49 //   -> unordered_set<T, Hash, equal_to<T>, Allocator>;
50 
51 #include <algorithm> // is_permutation
52 #include <cassert>
53 #include <climits> // INT_MAX
54 #include <type_traits>
55 #include <unordered_set>
56 
57 #include "test_allocator.h"
58 
main(int,char **)59 int main(int, char**)
60 {
61     const int expected_s[] = {1, 2, 3, INT_MAX};
62 
63     {
64     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
65     std::unordered_set s(std::begin(arr), std::end(arr));
66 
67     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int>);
68     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
69     }
70 
71     {
72     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
73     std::unordered_set s(std::begin(arr), std::end(arr), 42);
74 
75     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int>);
76     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
77     }
78 
79     {
80     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
81     std::unordered_set s(std::begin(arr), std::end(arr), 42, std::hash<long long>());
82 
83     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>>);
84     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
85     }
86 
87     {
88     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
89     std::unordered_set s(std::begin(arr), std::end(arr), 42, std::hash<long long>(), test_allocator<int>(0, 40));
90 
91     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<int>, test_allocator<int>>);
92     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
93     assert(s.get_allocator().get_id() == 40);
94     }
95 
96     {
97     std::unordered_set<int, std::hash<long long>, std::equal_to<>, test_allocator<int>> source;
98     std::unordered_set s(source);
99     ASSERT_SAME_TYPE(decltype(s), decltype(source));
100     assert(s.size() == 0);
101     }
102 
103     {
104     std::unordered_set<int, std::hash<long long>, std::equal_to<>, test_allocator<int>> source;
105     std::unordered_set s{source};  // braces instead of parens
106     ASSERT_SAME_TYPE(decltype(s), decltype(source));
107     assert(s.size() == 0);
108     }
109 
110     {
111     std::unordered_set<int, std::hash<long long>, std::equal_to<>, test_allocator<int>> source;
112     std::unordered_set s(source, test_allocator<int>(0, 41));
113     ASSERT_SAME_TYPE(decltype(s), decltype(source));
114     assert(s.size() == 0);
115     assert(s.get_allocator().get_id() == 41);
116     }
117 
118     {
119     std::unordered_set<int, std::hash<long long>, std::equal_to<>, test_allocator<int>> source;
120     std::unordered_set s{source, test_allocator<int>(0, 42)};  // braces instead of parens
121     ASSERT_SAME_TYPE(decltype(s), decltype(source));
122     assert(s.size() == 0);
123     assert(s.get_allocator().get_id() == 42);
124     }
125 
126     {
127     std::unordered_set s{ 1, 2, 1, INT_MAX, 3 };
128 
129     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int>);
130     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
131     }
132 
133     {
134     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42);
135 
136     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int>);
137     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
138     }
139 
140     {
141     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42, std::hash<long long>());
142 
143     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>>);
144     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
145     }
146 
147     {
148     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42, std::hash<long long>(), std::equal_to<>());
149 
150     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<>>);
151     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
152     }
153 
154     {
155     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42, std::hash<long long>(), std::equal_to<>(), test_allocator<int>(0, 43));
156 
157     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<>, test_allocator<int>>);
158     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
159     assert(s.get_allocator().get_id() == 43);
160     }
161 
162     {
163     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
164     std::unordered_set s(std::begin(arr), std::end(arr), 42, test_allocator<int>(0, 44));
165 
166     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<int>, std::equal_to<int>, test_allocator<int>>);
167     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
168     assert(s.get_allocator().get_id() == 44);
169     }
170 
171     {
172     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
173     std::unordered_set s(std::begin(arr), std::end(arr), 42, std::hash<long long>(), test_allocator<int>(0, 44));
174 
175     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<int>, test_allocator<int>>);
176     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
177     assert(s.get_allocator().get_id() == 44);
178     }
179 
180     {
181     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42, test_allocator<int>(0, 43));
182 
183     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<int>, std::equal_to<int>, test_allocator<int>>);
184     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
185     assert(s.get_allocator().get_id() == 43);
186     }
187 
188     {
189     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42, std::hash<long long>(), test_allocator<int>(0, 42));
190 
191     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<int>, test_allocator<int>>);
192     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
193     assert(s.get_allocator().get_id() == 42);
194     }
195 
196     return 0;
197 }
198