1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12 // class Alloc = allocator<Value>>
13 // class unordered_set
15 // void swap(unordered_set& u);
17 #include <unordered_set>
21 #include "test_macros.h"
22 #include "../../test_compare.h"
23 #include "../../test_hash.h"
24 #include "test_allocator.h"
25 #include "min_allocator.h"
30 typedef test_hash
<int> Hash
;
31 typedef test_equal_to
<int> Compare
;
32 typedef test_allocator
<int> Alloc
;
33 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
34 C
c1(0, Hash(1), Compare(1), Alloc(1, 1));
35 C
c2(0, Hash(2), Compare(2), Alloc(1, 2));
36 c2
.max_load_factor(2);
39 LIBCPP_ASSERT(c1
.bucket_count() == 0);
40 assert(c1
.size() == 0);
41 assert(c1
.hash_function() == Hash(2));
42 assert(c1
.key_eq() == Compare(2));
43 assert(c1
.get_allocator().get_id() == 1);
44 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
45 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
46 assert(c1
.max_load_factor() == 2);
48 LIBCPP_ASSERT(c2
.bucket_count() == 0);
49 assert(c2
.size() == 0);
50 assert(c2
.hash_function() == Hash(1));
51 assert(c2
.key_eq() == Compare(1));
52 assert(c2
.get_allocator().get_id() == 2);
53 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
54 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
55 assert(c2
.max_load_factor() == 1);
58 typedef test_hash
<int> Hash
;
59 typedef test_equal_to
<int> Compare
;
60 typedef test_allocator
<int> Alloc
;
61 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
74 C
c1(0, Hash(1), Compare(1), Alloc(1, 1));
75 C
c2(std::begin(a2
), std::end(a2
), 0, Hash(2), Compare(2), Alloc(1, 2));
76 c2
.max_load_factor(2);
79 assert(c1
.bucket_count() >= 8);
80 assert(c1
.size() == 8);
81 assert(*c1
.find(10) == 10);
82 assert(*c1
.find(20) == 20);
83 assert(*c1
.find(30) == 30);
84 assert(*c1
.find(40) == 40);
85 assert(*c1
.find(50) == 50);
86 assert(*c1
.find(60) == 60);
87 assert(*c1
.find(70) == 70);
88 assert(*c1
.find(80) == 80);
89 assert(c1
.hash_function() == Hash(2));
90 assert(c1
.key_eq() == Compare(2));
91 assert(c1
.get_allocator().get_id() == 1);
92 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
93 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
94 assert(c1
.max_load_factor() == 2);
96 LIBCPP_ASSERT(c2
.bucket_count() == 0);
97 assert(c2
.size() == 0);
98 assert(c2
.hash_function() == Hash(1));
99 assert(c2
.key_eq() == Compare(1));
100 assert(c2
.get_allocator().get_id() == 2);
101 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
102 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
103 assert(c2
.max_load_factor() == 1);
106 typedef test_hash
<int> Hash
;
107 typedef test_equal_to
<int> Compare
;
108 typedef test_allocator
<int> Alloc
;
109 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
120 C
c1(std::begin(a1
), std::end(a1
), 0, Hash(1), Compare(1), Alloc(1, 1));
121 C
c2(0, Hash(2), Compare(2), Alloc(1, 2));
122 c2
.max_load_factor(2);
125 LIBCPP_ASSERT(c1
.bucket_count() == 0);
126 assert(c1
.size() == 0);
127 assert(c1
.hash_function() == Hash(2));
128 assert(c1
.key_eq() == Compare(2));
129 assert(c1
.get_allocator().get_id() == 1);
130 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
131 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
132 assert(c1
.max_load_factor() == 2);
134 assert(c2
.bucket_count() >= 4);
135 assert(c2
.size() == 4);
136 assert(c2
.count(1) == 1);
137 assert(c2
.count(2) == 1);
138 assert(c2
.count(3) == 1);
139 assert(c2
.count(4) == 1);
140 assert(c2
.hash_function() == Hash(1));
141 assert(c2
.key_eq() == Compare(1));
142 assert(c2
.get_allocator().get_id() == 2);
143 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
144 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
145 assert(c2
.max_load_factor() == 1);
148 typedef test_hash
<int> Hash
;
149 typedef test_equal_to
<int> Compare
;
150 typedef test_allocator
<int> Alloc
;
151 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
173 C
c1(std::begin(a1
), std::end(a1
), 0, Hash(1), Compare(1), Alloc(1, 1));
174 C
c2(std::begin(a2
), std::end(a2
), 0, Hash(2), Compare(2), Alloc(1, 2));
175 c2
.max_load_factor(2);
178 assert(c1
.bucket_count() >= 8);
179 assert(c1
.size() == 8);
180 assert(*c1
.find(10) == 10);
181 assert(*c1
.find(20) == 20);
182 assert(*c1
.find(30) == 30);
183 assert(*c1
.find(40) == 40);
184 assert(*c1
.find(50) == 50);
185 assert(*c1
.find(60) == 60);
186 assert(*c1
.find(70) == 70);
187 assert(*c1
.find(80) == 80);
188 assert(c1
.hash_function() == Hash(2));
189 assert(c1
.key_eq() == Compare(2));
190 assert(c1
.get_allocator().get_id() == 1);
191 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
192 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
193 assert(c1
.max_load_factor() == 2);
195 assert(c2
.bucket_count() >= 4);
196 assert(c2
.size() == 4);
197 assert(c2
.count(1) == 1);
198 assert(c2
.count(2) == 1);
199 assert(c2
.count(3) == 1);
200 assert(c2
.count(4) == 1);
201 assert(c2
.hash_function() == Hash(1));
202 assert(c2
.key_eq() == Compare(1));
203 assert(c2
.get_allocator().get_id() == 2);
204 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
205 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
206 assert(c2
.max_load_factor() == 1);
210 typedef test_hash
<int> Hash
;
211 typedef test_equal_to
<int> Compare
;
212 typedef other_allocator
<int> Alloc
;
213 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
214 C
c1(0, Hash(1), Compare(1), Alloc(1));
215 C
c2(0, Hash(2), Compare(2), Alloc(2));
216 c2
.max_load_factor(2);
219 LIBCPP_ASSERT(c1
.bucket_count() == 0);
220 assert(c1
.size() == 0);
221 assert(c1
.hash_function() == Hash(2));
222 assert(c1
.key_eq() == Compare(2));
223 assert(c1
.get_allocator() == Alloc(2));
224 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
225 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
226 assert(c1
.max_load_factor() == 2);
228 LIBCPP_ASSERT(c2
.bucket_count() == 0);
229 assert(c2
.size() == 0);
230 assert(c2
.hash_function() == Hash(1));
231 assert(c2
.key_eq() == Compare(1));
232 assert(c2
.get_allocator() == Alloc(1));
233 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
234 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
235 assert(c2
.max_load_factor() == 1);
238 typedef test_hash
<int> Hash
;
239 typedef test_equal_to
<int> Compare
;
240 typedef other_allocator
<int> Alloc
;
241 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
254 C
c1(0, Hash(1), Compare(1), Alloc(1));
255 C
c2(std::begin(a2
), std::end(a2
), 0, Hash(2), Compare(2), Alloc(2));
256 c2
.max_load_factor(2);
259 assert(c1
.bucket_count() >= 8);
260 assert(c1
.size() == 8);
261 assert(*c1
.find(10) == 10);
262 assert(*c1
.find(20) == 20);
263 assert(*c1
.find(30) == 30);
264 assert(*c1
.find(40) == 40);
265 assert(*c1
.find(50) == 50);
266 assert(*c1
.find(60) == 60);
267 assert(*c1
.find(70) == 70);
268 assert(*c1
.find(80) == 80);
269 assert(c1
.hash_function() == Hash(2));
270 assert(c1
.key_eq() == Compare(2));
271 assert(c1
.get_allocator() == Alloc(2));
272 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
273 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
274 assert(c1
.max_load_factor() == 2);
276 LIBCPP_ASSERT(c2
.bucket_count() == 0);
277 assert(c2
.size() == 0);
278 assert(c2
.hash_function() == Hash(1));
279 assert(c2
.key_eq() == Compare(1));
280 assert(c2
.get_allocator() == Alloc(1));
281 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
282 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
283 assert(c2
.max_load_factor() == 1);
286 typedef test_hash
<int> Hash
;
287 typedef test_equal_to
<int> Compare
;
288 typedef other_allocator
<int> Alloc
;
289 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
300 C
c1(std::begin(a1
), std::end(a1
), 0, Hash(1), Compare(1), Alloc(1));
301 C
c2(0, Hash(2), Compare(2), Alloc(2));
302 c2
.max_load_factor(2);
305 LIBCPP_ASSERT(c1
.bucket_count() == 0);
306 assert(c1
.size() == 0);
307 assert(c1
.hash_function() == Hash(2));
308 assert(c1
.key_eq() == Compare(2));
309 assert(c1
.get_allocator() == Alloc(2));
310 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
311 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
312 assert(c1
.max_load_factor() == 2);
314 assert(c2
.bucket_count() >= 4);
315 assert(c2
.size() == 4);
316 assert(c2
.count(1) == 1);
317 assert(c2
.count(2) == 1);
318 assert(c2
.count(3) == 1);
319 assert(c2
.count(4) == 1);
320 assert(c2
.hash_function() == Hash(1));
321 assert(c2
.key_eq() == Compare(1));
322 assert(c2
.get_allocator() == Alloc(1));
323 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
324 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
325 assert(c2
.max_load_factor() == 1);
328 typedef test_hash
<int> Hash
;
329 typedef test_equal_to
<int> Compare
;
330 typedef other_allocator
<int> Alloc
;
331 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
353 C
c1(std::begin(a1
), std::end(a1
), 0, Hash(1), Compare(1), Alloc(1));
354 C
c2(std::begin(a2
), std::end(a2
), 0, Hash(2), Compare(2), Alloc(2));
355 c2
.max_load_factor(2);
358 assert(c1
.bucket_count() >= 8);
359 assert(c1
.size() == 8);
360 assert(*c1
.find(10) == 10);
361 assert(*c1
.find(20) == 20);
362 assert(*c1
.find(30) == 30);
363 assert(*c1
.find(40) == 40);
364 assert(*c1
.find(50) == 50);
365 assert(*c1
.find(60) == 60);
366 assert(*c1
.find(70) == 70);
367 assert(*c1
.find(80) == 80);
368 assert(c1
.hash_function() == Hash(2));
369 assert(c1
.key_eq() == Compare(2));
370 assert(c1
.get_allocator() == Alloc(2));
371 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
372 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
373 assert(c1
.max_load_factor() == 2);
375 assert(c2
.bucket_count() >= 4);
376 assert(c2
.size() == 4);
377 assert(c2
.count(1) == 1);
378 assert(c2
.count(2) == 1);
379 assert(c2
.count(3) == 1);
380 assert(c2
.count(4) == 1);
381 assert(c2
.hash_function() == Hash(1));
382 assert(c2
.key_eq() == Compare(1));
383 assert(c2
.get_allocator() == Alloc(1));
384 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
385 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
386 assert(c2
.max_load_factor() == 1);
388 #if TEST_STD_VER >= 11
390 typedef test_hash
<int> Hash
;
391 typedef test_equal_to
<int> Compare
;
392 typedef min_allocator
<int> Alloc
;
393 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
394 C
c1(0, Hash(1), Compare(1), Alloc());
395 C
c2(0, Hash(2), Compare(2), Alloc());
396 c2
.max_load_factor(2);
399 LIBCPP_ASSERT(c1
.bucket_count() == 0);
400 assert(c1
.size() == 0);
401 assert(c1
.hash_function() == Hash(2));
402 assert(c1
.key_eq() == Compare(2));
403 assert(c1
.get_allocator() == Alloc());
404 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
405 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
406 assert(c1
.max_load_factor() == 2);
408 LIBCPP_ASSERT(c2
.bucket_count() == 0);
409 assert(c2
.size() == 0);
410 assert(c2
.hash_function() == Hash(1));
411 assert(c2
.key_eq() == Compare(1));
412 assert(c2
.get_allocator() == Alloc());
413 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
414 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
415 assert(c2
.max_load_factor() == 1);
418 typedef test_hash
<int> Hash
;
419 typedef test_equal_to
<int> Compare
;
420 typedef min_allocator
<int> Alloc
;
421 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
434 C
c1(0, Hash(1), Compare(1), Alloc());
435 C
c2(std::begin(a2
), std::end(a2
), 0, Hash(2), Compare(2), Alloc());
436 c2
.max_load_factor(2);
439 assert(c1
.bucket_count() >= 8);
440 assert(c1
.size() == 8);
441 assert(*c1
.find(10) == 10);
442 assert(*c1
.find(20) == 20);
443 assert(*c1
.find(30) == 30);
444 assert(*c1
.find(40) == 40);
445 assert(*c1
.find(50) == 50);
446 assert(*c1
.find(60) == 60);
447 assert(*c1
.find(70) == 70);
448 assert(*c1
.find(80) == 80);
449 assert(c1
.hash_function() == Hash(2));
450 assert(c1
.key_eq() == Compare(2));
451 assert(c1
.get_allocator() == Alloc());
452 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
453 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
454 assert(c1
.max_load_factor() == 2);
456 LIBCPP_ASSERT(c2
.bucket_count() == 0);
457 assert(c2
.size() == 0);
458 assert(c2
.hash_function() == Hash(1));
459 assert(c2
.key_eq() == Compare(1));
460 assert(c2
.get_allocator() == Alloc());
461 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
462 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
463 assert(c2
.max_load_factor() == 1);
466 typedef test_hash
<int> Hash
;
467 typedef test_equal_to
<int> Compare
;
468 typedef min_allocator
<int> Alloc
;
469 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
480 C
c1(std::begin(a1
), std::end(a1
), 0, Hash(1), Compare(1), Alloc());
481 C
c2(0, Hash(2), Compare(2), Alloc());
482 c2
.max_load_factor(2);
485 LIBCPP_ASSERT(c1
.bucket_count() == 0);
486 assert(c1
.size() == 0);
487 assert(c1
.hash_function() == Hash(2));
488 assert(c1
.key_eq() == Compare(2));
489 assert(c1
.get_allocator() == Alloc());
490 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
491 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
492 assert(c1
.max_load_factor() == 2);
494 assert(c2
.bucket_count() >= 4);
495 assert(c2
.size() == 4);
496 assert(c2
.count(1) == 1);
497 assert(c2
.count(2) == 1);
498 assert(c2
.count(3) == 1);
499 assert(c2
.count(4) == 1);
500 assert(c2
.hash_function() == Hash(1));
501 assert(c2
.key_eq() == Compare(1));
502 assert(c2
.get_allocator() == Alloc());
503 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
504 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
505 assert(c2
.max_load_factor() == 1);
508 typedef test_hash
<int> Hash
;
509 typedef test_equal_to
<int> Compare
;
510 typedef min_allocator
<int> Alloc
;
511 typedef std::unordered_set
<int, Hash
, Compare
, Alloc
> C
;
533 C
c1(std::begin(a1
), std::end(a1
), 0, Hash(1), Compare(1), Alloc());
534 C
c2(std::begin(a2
), std::end(a2
), 0, Hash(2), Compare(2), Alloc());
535 c2
.max_load_factor(2);
538 assert(c1
.bucket_count() >= 8);
539 assert(c1
.size() == 8);
540 assert(*c1
.find(10) == 10);
541 assert(*c1
.find(20) == 20);
542 assert(*c1
.find(30) == 30);
543 assert(*c1
.find(40) == 40);
544 assert(*c1
.find(50) == 50);
545 assert(*c1
.find(60) == 60);
546 assert(*c1
.find(70) == 70);
547 assert(*c1
.find(80) == 80);
548 assert(c1
.hash_function() == Hash(2));
549 assert(c1
.key_eq() == Compare(2));
550 assert(c1
.get_allocator() == Alloc());
551 assert(static_cast<std::size_t>(std::distance(c1
.begin(), c1
.end())) == c1
.size());
552 assert(static_cast<std::size_t>(std::distance(c1
.cbegin(), c1
.cend())) == c1
.size());
553 assert(c1
.max_load_factor() == 2);
555 assert(c2
.bucket_count() >= 4);
556 assert(c2
.size() == 4);
557 assert(c2
.count(1) == 1);
558 assert(c2
.count(2) == 1);
559 assert(c2
.count(3) == 1);
560 assert(c2
.count(4) == 1);
561 assert(c2
.hash_function() == Hash(1));
562 assert(c2
.key_eq() == Compare(1));
563 assert(c2
.get_allocator() == Alloc());
564 assert(static_cast<std::size_t>(std::distance(c2
.begin(), c2
.end())) == c2
.size());
565 assert(static_cast<std::size_t>(std::distance(c2
.cbegin(), c2
.cend())) == c2
.size());
566 assert(c2
.max_load_factor() == 1);