xref: /llvm-project/libcxx/test/std/containers/unord/unord.set/unord.set.cnstr/deduct.pass.cpp (revision ef70fe4d264d20abdd8476605650479a96a62071)
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 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // <unordered_set>
12 
13 // template<class InputIterator,
14 //        class Hash = hash<iter-value-type<InputIterator>>,
15 //        class Pred = equal_to<iter-value-type<InputIterator>>,
16 //        class Allocator = allocator<iter-value-type<InputIterator>>>
17 // unordered_set(InputIterator, InputIterator, typename see below::size_type = see below,
18 //               Hash = Hash(), Pred = Pred(), Allocator = Allocator())
19 //   -> unordered_set<iter-value-type<InputIterator>,
20 //                    Hash, Pred, Allocator>;
21 //
22 // template<class T, class Hash = hash<T>,
23 //        class Pred = equal_to<T>, class Allocator = allocator<T>>
24 // unordered_set(initializer_list<T>, typename see below::size_type = see below,
25 //               Hash = Hash(), Pred = Pred(), Allocator = Allocator())
26 //   -> unordered_set<T, Hash, Pred, Allocator>;
27 //
28 // template<class InputIterator, class Allocator>
29 // unordered_set(InputIterator, InputIterator, typename see below::size_type, Allocator)
30 //   -> unordered_set<iter-value-type<InputIterator>,
31 //                    hash<iter-value-type<InputIterator>>,
32 //                    equal_to<iter-value-type<InputIterator>>,
33 //                    Allocator>;
34 //
35 // template<class InputIterator, class Hash, class Allocator>
36 // unordered_set(InputIterator, InputIterator, typename see below::size_type,
37 //               Hash, Allocator)
38 //   -> unordered_set<iter-value-type<InputIterator>, Hash,
39 //                    equal_to<iter-value-type<InputIterator>>,
40 //                    Allocator>;
41 //
42 // template<class T, class Allocator>
43 // unordered_set(initializer_list<T>, typename see below::size_type, Allocator)
44 //   -> unordered_set<T, hash<T>, equal_to<T>, Allocator>;
45 //
46 // template<class T, class Hash, class Allocator>
47 // unordered_set(initializer_list<T>, typename see below::size_type, Hash, Allocator)
48 //   -> unordered_set<T, Hash, equal_to<T>, Allocator>;
49 //
50 // template<ranges::input_range R,
51 //          class Hash = hash<ranges::range_value_t<R>>,
52 //          class Pred = equal_to<ranges::range_value_t<R>>,
53 //          class Allocator = allocator<ranges::range_value_t<R>>>
54 //   unordered_set(from_range_t, R&&, typename see below::size_type = see below, Hash = Hash(), Pred = Pred(), Allocator = Allocator())
55 //     -> unordered_set<ranges::range_value_t<R>, Hash, Pred, Allocator>; // C++23
56 //
57 // template<ranges::input_range R, class Allocator>
58 //   unordered_set(from_range_t, R&&, typename see below::size_type, Allocator)
59 //     -> unordered_set<ranges::range_value_t<R>, hash<ranges::range_value_t<R>>,
60 //                       equal_to<ranges::range_value_t<R>>, Allocator>; // C++23
61 //
62 // template<ranges::input_range R, class Allocator>
63 //   unordered_set(from_range_t, R&&, Allocator)
64 //     -> unordered_set<ranges::range_value_t<R>, hash<ranges::range_value_t<R>>,
65 //                       equal_to<ranges::range_value_t<R>>, Allocator>; // C++23
66 //
67 // template<ranges::input_range R, class Hash, class Allocator>
68 //   unordered_set(from_range_t, R&&, typename see below::size_type, Hash, Allocator)
69 //     -> unordered_set<ranges::range_value_t<R>, Hash,
70 //                       equal_to<ranges::range_value_t<R>>, Allocator>; // C++23
71 
72 #include <algorithm> // is_permutation
73 #include <array>
74 #include <cassert>
75 #include <climits> // INT_MAX
76 #include <type_traits>
77 #include <unordered_set>
78 
79 #include "../../../test_compare.h"
80 #include "../../../test_hash.h"
81 #include "deduction_guides_sfinae_checks.h"
82 #include "test_allocator.h"
83 
main(int,char **)84 int main(int, char**)
85 {
86     const int expected_s[] = {1, 2, 3, INT_MAX};
87 
88     {
89     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
90     std::unordered_set s(std::begin(arr), std::end(arr));
91 
92     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int>);
93     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
94     }
95 
96     {
97     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
98     std::unordered_set s(std::begin(arr), std::end(arr), 42);
99 
100     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int>);
101     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
102     }
103 
104     {
105     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
106     std::unordered_set s(std::begin(arr), std::end(arr), 42, std::hash<long long>());
107 
108     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>>);
109     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
110     }
111 
112     {
113     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
114     std::unordered_set s(std::begin(arr), std::end(arr), 42, std::hash<long long>(), test_allocator<int>(0, 40));
115 
116     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<int>, test_allocator<int>>);
117     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
118     assert(s.get_allocator().get_id() == 40);
119     }
120 
121     {
122     std::unordered_set<int, std::hash<long long>, std::equal_to<>, test_allocator<int>> source;
123     std::unordered_set s(source);
124     ASSERT_SAME_TYPE(decltype(s), decltype(source));
125     assert(s.size() == 0);
126     }
127 
128     {
129     std::unordered_set<int, std::hash<long long>, std::equal_to<>, test_allocator<int>> source;
130     std::unordered_set s{source};  // braces instead of parens
131     ASSERT_SAME_TYPE(decltype(s), decltype(source));
132     assert(s.size() == 0);
133     }
134 
135     {
136     std::unordered_set<int, std::hash<long long>, std::equal_to<>, test_allocator<int>> source;
137     std::unordered_set s(source, test_allocator<int>(0, 41));
138     ASSERT_SAME_TYPE(decltype(s), decltype(source));
139     assert(s.size() == 0);
140     assert(s.get_allocator().get_id() == 41);
141     }
142 
143     {
144     std::unordered_set<int, std::hash<long long>, std::equal_to<>, test_allocator<int>> source;
145     std::unordered_set s{source, test_allocator<int>(0, 42)};  // braces instead of parens
146     ASSERT_SAME_TYPE(decltype(s), decltype(source));
147     assert(s.size() == 0);
148     assert(s.get_allocator().get_id() == 42);
149     }
150 
151     {
152     std::unordered_set s{ 1, 2, 1, INT_MAX, 3 };
153 
154     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int>);
155     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
156     }
157 
158     {
159     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42);
160 
161     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int>);
162     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
163     }
164 
165     {
166     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42, std::hash<long long>());
167 
168     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>>);
169     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
170     }
171 
172     {
173     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42, std::hash<long long>(), std::equal_to<>());
174 
175     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<>>);
176     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
177     }
178 
179     {
180     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42, std::hash<long long>(), std::equal_to<>(), test_allocator<int>(0, 43));
181 
182     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<>, test_allocator<int>>);
183     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
184     assert(s.get_allocator().get_id() == 43);
185     }
186 
187     {
188     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
189     std::unordered_set s(std::begin(arr), std::end(arr), 42, test_allocator<int>(0, 44));
190 
191     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<int>, 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() == 44);
194     }
195 
196     {
197     const int arr[] = { 1, 2, 1, INT_MAX, 3 };
198     std::unordered_set s(std::begin(arr), std::end(arr), 42, std::hash<long long>(), test_allocator<int>(0, 44));
199 
200     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<int>, test_allocator<int>>);
201     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
202     assert(s.get_allocator().get_id() == 44);
203     }
204 
205     {
206     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42, test_allocator<int>(0, 43));
207 
208     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<int>, std::equal_to<int>, test_allocator<int>>);
209     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
210     assert(s.get_allocator().get_id() == 43);
211     }
212 
213     {
214     std::unordered_set s({ 1, 2, 1, INT_MAX, 3 }, 42, std::hash<long long>(), test_allocator<int>(0, 42));
215 
216     ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<int>, test_allocator<int>>);
217     assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
218     assert(s.get_allocator().get_id() == 42);
219     }
220 
221 #if TEST_STD_VER >= 23
222     {
223       using Range = std::array<int, 0>;
224       using Pred = test_equal_to<int>;
225       using DefaultPred = std::equal_to<int>;
226       using Hash = test_hash<int>;
227       using DefaultHash = std::hash<int>;
228       using Alloc = test_allocator<int>;
229 
230       { // (from_range, range)
231         std::unordered_set c(std::from_range, Range());
232         static_assert(std::is_same_v<decltype(c), std::unordered_set<int>>);
233       }
234 
235       { // (from_range, range, n)
236         std::unordered_set c(std::from_range, Range(), std::size_t());
237         static_assert(std::is_same_v<decltype(c), std::unordered_set<int>>);
238       }
239 
240       { // (from_range, range, n, hash)
241         std::unordered_set c(std::from_range, Range(), std::size_t(), Hash());
242         static_assert(std::is_same_v<decltype(c), std::unordered_set<int, Hash>>);
243       }
244 
245       { // (from_range, range, n, hash, pred)
246         std::unordered_set c(std::from_range, Range(), std::size_t(), Hash(), Pred());
247         static_assert(std::is_same_v<decltype(c), std::unordered_set<int, Hash, Pred>>);
248       }
249 
250       { // (from_range, range, n, hash, pred, alloc)
251         std::unordered_set c(std::from_range, Range(), std::size_t(), Hash(), Pred(), Alloc());
252         static_assert(std::is_same_v<decltype(c), std::unordered_set<int, Hash, Pred, Alloc>>);
253       }
254 
255       { // (from_range, range, n, alloc)
256         std::unordered_set c(std::from_range, Range(), std::size_t(), Alloc());
257         static_assert(std::is_same_v<decltype(c), std::unordered_set<int, DefaultHash, DefaultPred, Alloc>>);
258       }
259 
260       // TODO(LWG 2713): uncomment this test once the constructor is added.
261       { // (from_range, range, alloc)
262         //std::unordered_set c(std::from_range, Range(), Alloc());
263         //static_assert(std::is_same_v<decltype(c), std::unordered_set<int, DefaultHash, DefaultPred, Alloc>>);
264       }
265 
266       { // (from_range, range, n, hash, alloc)
267         std::unordered_set c(std::from_range, Range(), std::size_t(), Hash(), Alloc());
268         static_assert(std::is_same_v<decltype(c), std::unordered_set<int, Hash, DefaultPred, Alloc>>);
269       }
270     }
271 #endif
272 
273     UnorderedContainerDeductionGuidesSfinaeAway<std::unordered_set, std::unordered_set<int>>();
274 
275     return 0;
276 }
277