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
84int main(int, char**) {
85 const int expected_s[] = {1, 2, 3, INT_MAX};
86
87 {
88 const int arr[] = {1, 2, 1, INT_MAX, 3};
89 std::unordered_set s(std::begin(arr: arr), std::end(arr: arr));
90
91 ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int>);
92 assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
93 }
94
95 {
96 const int arr[] = {1, 2, 1, INT_MAX, 3};
97 std::unordered_set s(std::begin(arr: arr), std::end(arr: arr), 42);
98
99 ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int>);
100 assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
101 }
102
103 {
104 const int arr[] = {1, 2, 1, INT_MAX, 3};
105 std::unordered_set s(std::begin(arr: arr), std::end(arr: arr), 42, std::hash<long long>());
106
107 ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>>);
108 assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
109 }
110
111 {
112 const int arr[] = {1, 2, 1, INT_MAX, 3};
113 std::unordered_set s(std::begin(arr: arr), std::end(arr: arr), 42, std::hash<long long>(), test_allocator<int>(0, 40));
114
115 ASSERT_SAME_TYPE(
116 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(
181 {1, 2, 1, INT_MAX, 3}, 42, std::hash<long long>(), std::equal_to<>(), test_allocator<int>(0, 43));
182
183 ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<>, 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 const int arr[] = {1, 2, 1, INT_MAX, 3};
190 std::unordered_set s(std::begin(arr: arr), std::end(arr: arr), 42, test_allocator<int>(0, 44));
191
192 ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<int>, std::equal_to<int>, test_allocator<int>>);
193 assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
194 assert(s.get_allocator().get_id() == 44);
195 }
196
197 {
198 const int arr[] = {1, 2, 1, INT_MAX, 3};
199 std::unordered_set s(std::begin(arr: arr), std::end(arr: arr), 42, std::hash<long long>(), test_allocator<int>(0, 44));
200
201 ASSERT_SAME_TYPE(
202 decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<int>, test_allocator<int>>);
203 assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
204 assert(s.get_allocator().get_id() == 44);
205 }
206
207 {
208 std::unordered_set s({1, 2, 1, INT_MAX, 3}, 42, test_allocator<int>(0, 43));
209
210 ASSERT_SAME_TYPE(decltype(s), std::unordered_set<int, std::hash<int>, std::equal_to<int>, test_allocator<int>>);
211 assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
212 assert(s.get_allocator().get_id() == 43);
213 }
214
215 {
216 std::unordered_set s({1, 2, 1, INT_MAX, 3}, 42, std::hash<long long>(), test_allocator<int>(0, 42));
217
218 ASSERT_SAME_TYPE(
219 decltype(s), std::unordered_set<int, std::hash<long long>, std::equal_to<int>, test_allocator<int>>);
220 assert(std::is_permutation(s.begin(), s.end(), std::begin(expected_s), std::end(expected_s)));
221 assert(s.get_allocator().get_id() == 42);
222 }
223
224#if TEST_STD_VER >= 23
225 {
226 using Range = std::array<int, 0>;
227 using Pred = test_equal_to<int>;
228 using DefaultPred = std::equal_to<int>;
229 using Hash = test_hash<int>;
230 using DefaultHash = std::hash<int>;
231 using Alloc = test_allocator<int>;
232
233 { // (from_range, range)
234 std::unordered_set c(std::from_range, Range());
235 static_assert(std::is_same_v<decltype(c), std::unordered_set<int>>);
236 }
237
238 { // (from_range, range, n)
239 std::unordered_set c(std::from_range, Range(), std::size_t());
240 static_assert(std::is_same_v<decltype(c), std::unordered_set<int>>);
241 }
242
243 { // (from_range, range, n, hash)
244 std::unordered_set c(std::from_range, Range(), std::size_t(), Hash());
245 static_assert(std::is_same_v<decltype(c), std::unordered_set<int, Hash>>);
246 }
247
248 { // (from_range, range, n, hash, pred)
249 std::unordered_set c(std::from_range, Range(), std::size_t(), Hash(), Pred());
250 static_assert(std::is_same_v<decltype(c), std::unordered_set<int, Hash, Pred>>);
251 }
252
253 { // (from_range, range, n, hash, pred, alloc)
254 std::unordered_set c(std::from_range, Range(), std::size_t(), Hash(), Pred(), Alloc());
255 static_assert(std::is_same_v<decltype(c), std::unordered_set<int, Hash, Pred, Alloc>>);
256 }
257
258 { // (from_range, range, n, alloc)
259 std::unordered_set c(std::from_range, Range(), std::size_t(), Alloc());
260 static_assert(std::is_same_v<decltype(c), std::unordered_set<int, DefaultHash, DefaultPred, Alloc>>);
261 }
262
263 // TODO(LWG 2713): uncomment this test once the constructor is added.
264 { // (from_range, range, alloc)
265 //std::unordered_set c(std::from_range, Range(), Alloc());
266 //static_assert(std::is_same_v<decltype(c), std::unordered_set<int, DefaultHash, DefaultPred, Alloc>>);
267 }
268
269 { // (from_range, range, n, hash, alloc)
270 std::unordered_set c(std::from_range, Range(), std::size_t(), Hash(), Alloc());
271 static_assert(std::is_same_v<decltype(c), std::unordered_set<int, Hash, DefaultPred, Alloc>>);
272 }
273 }
274#endif
275
276 UnorderedContainerDeductionGuidesSfinaeAway<std::unordered_set, std::unordered_set<int>>();
277
278 return 0;
279}
280

source code of libcxx/test/std/containers/unord/unord.set/unord.set.cnstr/deduct.pass.cpp