1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <unordered_set>
11
12 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
13 // class Alloc = allocator<Value>>
14 // class unordered_set
15
16 // void swap(unordered_set& u);
17
18 #include <unordered_set>
19 #include <cassert>
20 #include <cstddef>
21
22 #include "test_macros.h"
23 #include "../../test_compare.h"
24 #include "../../test_hash.h"
25 #include "test_allocator.h"
26 #include "min_allocator.h"
27
main()28 int main()
29 {
30 {
31 typedef test_hash<std::hash<int> > Hash;
32 typedef test_compare<std::equal_to<int> > Compare;
33 typedef test_allocator<int> Alloc;
34 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
35 C c1(0, Hash(1), Compare(1), Alloc(1, 1));
36 C c2(0, Hash(2), Compare(2), Alloc(1, 2));
37 c2.max_load_factor(2);
38 c1.swap(c2);
39
40 LIBCPP_ASSERT(c1.bucket_count() == 0);
41 assert(c1.size() == 0);
42 assert(c1.hash_function() == Hash(2));
43 assert(c1.key_eq() == Compare(2));
44 assert(c1.get_allocator().get_id() == 1);
45 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
46 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
47 assert(c1.max_load_factor() == 2);
48
49 LIBCPP_ASSERT(c2.bucket_count() == 0);
50 assert(c2.size() == 0);
51 assert(c2.hash_function() == Hash(1));
52 assert(c2.key_eq() == Compare(1));
53 assert(c2.get_allocator().get_id() == 2);
54 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
55 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
56 assert(c2.max_load_factor() == 1);
57 }
58 {
59 typedef test_hash<std::hash<int> > Hash;
60 typedef test_compare<std::equal_to<int> > Compare;
61 typedef test_allocator<int> Alloc;
62 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
63 typedef int P;
64 P a2[] =
65 {
66 P(10),
67 P(20),
68 P(30),
69 P(40),
70 P(50),
71 P(60),
72 P(70),
73 P(80)
74 };
75 C c1(0, Hash(1), Compare(1), Alloc(1, 1));
76 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(1, 2));
77 c2.max_load_factor(2);
78 c1.swap(c2);
79
80 assert(c1.bucket_count() >= 8);
81 assert(c1.size() == 8);
82 assert(*c1.find(10) == 10);
83 assert(*c1.find(20) == 20);
84 assert(*c1.find(30) == 30);
85 assert(*c1.find(40) == 40);
86 assert(*c1.find(50) == 50);
87 assert(*c1.find(60) == 60);
88 assert(*c1.find(70) == 70);
89 assert(*c1.find(80) == 80);
90 assert(c1.hash_function() == Hash(2));
91 assert(c1.key_eq() == Compare(2));
92 assert(c1.get_allocator().get_id() == 1);
93 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
94 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
95 assert(c1.max_load_factor() == 2);
96
97 LIBCPP_ASSERT(c2.bucket_count() == 0);
98 assert(c2.size() == 0);
99 assert(c2.hash_function() == Hash(1));
100 assert(c2.key_eq() == Compare(1));
101 assert(c2.get_allocator().get_id() == 2);
102 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
103 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
104 assert(c2.max_load_factor() == 1);
105 }
106 {
107 typedef test_hash<std::hash<int> > Hash;
108 typedef test_compare<std::equal_to<int> > Compare;
109 typedef test_allocator<int> Alloc;
110 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
111 typedef int P;
112 P a1[] =
113 {
114 P(1),
115 P(2),
116 P(3),
117 P(4),
118 P(1),
119 P(2)
120 };
121 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1, 1));
122 C c2(0, Hash(2), Compare(2), Alloc(1, 2));
123 c2.max_load_factor(2);
124 c1.swap(c2);
125
126 LIBCPP_ASSERT(c1.bucket_count() == 0);
127 assert(c1.size() == 0);
128 assert(c1.hash_function() == Hash(2));
129 assert(c1.key_eq() == Compare(2));
130 assert(c1.get_allocator().get_id() == 1);
131 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
132 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
133 assert(c1.max_load_factor() == 2);
134
135 assert(c2.bucket_count() >= 4);
136 assert(c2.size() == 4);
137 assert(c2.count(1) == 1);
138 assert(c2.count(2) == 1);
139 assert(c2.count(3) == 1);
140 assert(c2.count(4) == 1);
141 assert(c2.hash_function() == Hash(1));
142 assert(c2.key_eq() == Compare(1));
143 assert(c2.get_allocator().get_id() == 2);
144 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
145 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
146 assert(c2.max_load_factor() == 1);
147 }
148 {
149 typedef test_hash<std::hash<int> > Hash;
150 typedef test_compare<std::equal_to<int> > Compare;
151 typedef test_allocator<int> Alloc;
152 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
153 typedef int P;
154 P a1[] =
155 {
156 P(1),
157 P(2),
158 P(3),
159 P(4),
160 P(1),
161 P(2)
162 };
163 P a2[] =
164 {
165 P(10),
166 P(20),
167 P(30),
168 P(40),
169 P(50),
170 P(60),
171 P(70),
172 P(80)
173 };
174 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1, 1));
175 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(1, 2));
176 c2.max_load_factor(2);
177 c1.swap(c2);
178
179 assert(c1.bucket_count() >= 8);
180 assert(c1.size() == 8);
181 assert(*c1.find(10) == 10);
182 assert(*c1.find(20) == 20);
183 assert(*c1.find(30) == 30);
184 assert(*c1.find(40) == 40);
185 assert(*c1.find(50) == 50);
186 assert(*c1.find(60) == 60);
187 assert(*c1.find(70) == 70);
188 assert(*c1.find(80) == 80);
189 assert(c1.hash_function() == Hash(2));
190 assert(c1.key_eq() == Compare(2));
191 assert(c1.get_allocator().get_id() == 1);
192 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
193 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
194 assert(c1.max_load_factor() == 2);
195
196 assert(c2.bucket_count() >= 4);
197 assert(c2.size() == 4);
198 assert(c2.count(1) == 1);
199 assert(c2.count(2) == 1);
200 assert(c2.count(3) == 1);
201 assert(c2.count(4) == 1);
202 assert(c2.hash_function() == Hash(1));
203 assert(c2.key_eq() == Compare(1));
204 assert(c2.get_allocator().get_id() == 2);
205 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
206 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
207 assert(c2.max_load_factor() == 1);
208 }
209
210 {
211 typedef test_hash<std::hash<int> > Hash;
212 typedef test_compare<std::equal_to<int> > Compare;
213 typedef other_allocator<int> Alloc;
214 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
215 C c1(0, Hash(1), Compare(1), Alloc(1));
216 C c2(0, Hash(2), Compare(2), Alloc(2));
217 c2.max_load_factor(2);
218 c1.swap(c2);
219
220 LIBCPP_ASSERT(c1.bucket_count() == 0);
221 assert(c1.size() == 0);
222 assert(c1.hash_function() == Hash(2));
223 assert(c1.key_eq() == Compare(2));
224 assert(c1.get_allocator() == Alloc(2));
225 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
226 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
227 assert(c1.max_load_factor() == 2);
228
229 LIBCPP_ASSERT(c2.bucket_count() == 0);
230 assert(c2.size() == 0);
231 assert(c2.hash_function() == Hash(1));
232 assert(c2.key_eq() == Compare(1));
233 assert(c2.get_allocator() == Alloc(1));
234 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
235 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
236 assert(c2.max_load_factor() == 1);
237 }
238 {
239 typedef test_hash<std::hash<int> > Hash;
240 typedef test_compare<std::equal_to<int> > Compare;
241 typedef other_allocator<int> Alloc;
242 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
243 typedef int P;
244 P a2[] =
245 {
246 P(10),
247 P(20),
248 P(30),
249 P(40),
250 P(50),
251 P(60),
252 P(70),
253 P(80)
254 };
255 C c1(0, Hash(1), Compare(1), Alloc(1));
256 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
257 c2.max_load_factor(2);
258 c1.swap(c2);
259
260 assert(c1.bucket_count() >= 8);
261 assert(c1.size() == 8);
262 assert(*c1.find(10) == 10);
263 assert(*c1.find(20) == 20);
264 assert(*c1.find(30) == 30);
265 assert(*c1.find(40) == 40);
266 assert(*c1.find(50) == 50);
267 assert(*c1.find(60) == 60);
268 assert(*c1.find(70) == 70);
269 assert(*c1.find(80) == 80);
270 assert(c1.hash_function() == Hash(2));
271 assert(c1.key_eq() == Compare(2));
272 assert(c1.get_allocator() == Alloc(2));
273 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
274 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
275 assert(c1.max_load_factor() == 2);
276
277 LIBCPP_ASSERT(c2.bucket_count() == 0);
278 assert(c2.size() == 0);
279 assert(c2.hash_function() == Hash(1));
280 assert(c2.key_eq() == Compare(1));
281 assert(c2.get_allocator() == Alloc(1));
282 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
283 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
284 assert(c2.max_load_factor() == 1);
285 }
286 {
287 typedef test_hash<std::hash<int> > Hash;
288 typedef test_compare<std::equal_to<int> > Compare;
289 typedef other_allocator<int> Alloc;
290 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
291 typedef int P;
292 P a1[] =
293 {
294 P(1),
295 P(2),
296 P(3),
297 P(4),
298 P(1),
299 P(2)
300 };
301 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
302 C c2(0, Hash(2), Compare(2), Alloc(2));
303 c2.max_load_factor(2);
304 c1.swap(c2);
305
306 LIBCPP_ASSERT(c1.bucket_count() == 0);
307 assert(c1.size() == 0);
308 assert(c1.hash_function() == Hash(2));
309 assert(c1.key_eq() == Compare(2));
310 assert(c1.get_allocator() == Alloc(2));
311 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
312 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
313 assert(c1.max_load_factor() == 2);
314
315 assert(c2.bucket_count() >= 4);
316 assert(c2.size() == 4);
317 assert(c2.count(1) == 1);
318 assert(c2.count(2) == 1);
319 assert(c2.count(3) == 1);
320 assert(c2.count(4) == 1);
321 assert(c2.hash_function() == Hash(1));
322 assert(c2.key_eq() == Compare(1));
323 assert(c2.get_allocator() == Alloc(1));
324 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
325 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
326 assert(c2.max_load_factor() == 1);
327 }
328 {
329 typedef test_hash<std::hash<int> > Hash;
330 typedef test_compare<std::equal_to<int> > Compare;
331 typedef other_allocator<int> Alloc;
332 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
333 typedef int P;
334 P a1[] =
335 {
336 P(1),
337 P(2),
338 P(3),
339 P(4),
340 P(1),
341 P(2)
342 };
343 P a2[] =
344 {
345 P(10),
346 P(20),
347 P(30),
348 P(40),
349 P(50),
350 P(60),
351 P(70),
352 P(80)
353 };
354 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
355 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
356 c2.max_load_factor(2);
357 c1.swap(c2);
358
359 assert(c1.bucket_count() >= 8);
360 assert(c1.size() == 8);
361 assert(*c1.find(10) == 10);
362 assert(*c1.find(20) == 20);
363 assert(*c1.find(30) == 30);
364 assert(*c1.find(40) == 40);
365 assert(*c1.find(50) == 50);
366 assert(*c1.find(60) == 60);
367 assert(*c1.find(70) == 70);
368 assert(*c1.find(80) == 80);
369 assert(c1.hash_function() == Hash(2));
370 assert(c1.key_eq() == Compare(2));
371 assert(c1.get_allocator() == Alloc(2));
372 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
373 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
374 assert(c1.max_load_factor() == 2);
375
376 assert(c2.bucket_count() >= 4);
377 assert(c2.size() == 4);
378 assert(c2.count(1) == 1);
379 assert(c2.count(2) == 1);
380 assert(c2.count(3) == 1);
381 assert(c2.count(4) == 1);
382 assert(c2.hash_function() == Hash(1));
383 assert(c2.key_eq() == Compare(1));
384 assert(c2.get_allocator() == Alloc(1));
385 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
386 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
387 assert(c2.max_load_factor() == 1);
388 }
389 #if TEST_STD_VER >= 11
390 {
391 typedef test_hash<std::hash<int> > Hash;
392 typedef test_compare<std::equal_to<int> > Compare;
393 typedef min_allocator<int> Alloc;
394 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
395 C c1(0, Hash(1), Compare(1), Alloc());
396 C c2(0, Hash(2), Compare(2), Alloc());
397 c2.max_load_factor(2);
398 c1.swap(c2);
399
400 LIBCPP_ASSERT(c1.bucket_count() == 0);
401 assert(c1.size() == 0);
402 assert(c1.hash_function() == Hash(2));
403 assert(c1.key_eq() == Compare(2));
404 assert(c1.get_allocator() == Alloc());
405 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
406 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
407 assert(c1.max_load_factor() == 2);
408
409 LIBCPP_ASSERT(c2.bucket_count() == 0);
410 assert(c2.size() == 0);
411 assert(c2.hash_function() == Hash(1));
412 assert(c2.key_eq() == Compare(1));
413 assert(c2.get_allocator() == Alloc());
414 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
415 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
416 assert(c2.max_load_factor() == 1);
417 }
418 {
419 typedef test_hash<std::hash<int> > Hash;
420 typedef test_compare<std::equal_to<int> > Compare;
421 typedef min_allocator<int> Alloc;
422 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
423 typedef int P;
424 P a2[] =
425 {
426 P(10),
427 P(20),
428 P(30),
429 P(40),
430 P(50),
431 P(60),
432 P(70),
433 P(80)
434 };
435 C c1(0, Hash(1), Compare(1), Alloc());
436 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
437 c2.max_load_factor(2);
438 c1.swap(c2);
439
440 assert(c1.bucket_count() >= 8);
441 assert(c1.size() == 8);
442 assert(*c1.find(10) == 10);
443 assert(*c1.find(20) == 20);
444 assert(*c1.find(30) == 30);
445 assert(*c1.find(40) == 40);
446 assert(*c1.find(50) == 50);
447 assert(*c1.find(60) == 60);
448 assert(*c1.find(70) == 70);
449 assert(*c1.find(80) == 80);
450 assert(c1.hash_function() == Hash(2));
451 assert(c1.key_eq() == Compare(2));
452 assert(c1.get_allocator() == Alloc());
453 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
454 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
455 assert(c1.max_load_factor() == 2);
456
457 LIBCPP_ASSERT(c2.bucket_count() == 0);
458 assert(c2.size() == 0);
459 assert(c2.hash_function() == Hash(1));
460 assert(c2.key_eq() == Compare(1));
461 assert(c2.get_allocator() == Alloc());
462 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
463 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
464 assert(c2.max_load_factor() == 1);
465 }
466 {
467 typedef test_hash<std::hash<int> > Hash;
468 typedef test_compare<std::equal_to<int> > Compare;
469 typedef min_allocator<int> Alloc;
470 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
471 typedef int P;
472 P a1[] =
473 {
474 P(1),
475 P(2),
476 P(3),
477 P(4),
478 P(1),
479 P(2)
480 };
481 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
482 C c2(0, Hash(2), Compare(2), Alloc());
483 c2.max_load_factor(2);
484 c1.swap(c2);
485
486 LIBCPP_ASSERT(c1.bucket_count() == 0);
487 assert(c1.size() == 0);
488 assert(c1.hash_function() == Hash(2));
489 assert(c1.key_eq() == Compare(2));
490 assert(c1.get_allocator() == Alloc());
491 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
492 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
493 assert(c1.max_load_factor() == 2);
494
495 assert(c2.bucket_count() >= 4);
496 assert(c2.size() == 4);
497 assert(c2.count(1) == 1);
498 assert(c2.count(2) == 1);
499 assert(c2.count(3) == 1);
500 assert(c2.count(4) == 1);
501 assert(c2.hash_function() == Hash(1));
502 assert(c2.key_eq() == Compare(1));
503 assert(c2.get_allocator() == Alloc());
504 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
505 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
506 assert(c2.max_load_factor() == 1);
507 }
508 {
509 typedef test_hash<std::hash<int> > Hash;
510 typedef test_compare<std::equal_to<int> > Compare;
511 typedef min_allocator<int> Alloc;
512 typedef std::unordered_set<int, Hash, Compare, Alloc> C;
513 typedef int P;
514 P a1[] =
515 {
516 P(1),
517 P(2),
518 P(3),
519 P(4),
520 P(1),
521 P(2)
522 };
523 P a2[] =
524 {
525 P(10),
526 P(20),
527 P(30),
528 P(40),
529 P(50),
530 P(60),
531 P(70),
532 P(80)
533 };
534 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
535 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
536 c2.max_load_factor(2);
537 c1.swap(c2);
538
539 assert(c1.bucket_count() >= 8);
540 assert(c1.size() == 8);
541 assert(*c1.find(10) == 10);
542 assert(*c1.find(20) == 20);
543 assert(*c1.find(30) == 30);
544 assert(*c1.find(40) == 40);
545 assert(*c1.find(50) == 50);
546 assert(*c1.find(60) == 60);
547 assert(*c1.find(70) == 70);
548 assert(*c1.find(80) == 80);
549 assert(c1.hash_function() == Hash(2));
550 assert(c1.key_eq() == Compare(2));
551 assert(c1.get_allocator() == Alloc());
552 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
553 assert(static_cast<std::size_t>(std::distance(c1.cbegin(), c1.cend())) == c1.size());
554 assert(c1.max_load_factor() == 2);
555
556 assert(c2.bucket_count() >= 4);
557 assert(c2.size() == 4);
558 assert(c2.count(1) == 1);
559 assert(c2.count(2) == 1);
560 assert(c2.count(3) == 1);
561 assert(c2.count(4) == 1);
562 assert(c2.hash_function() == Hash(1));
563 assert(c2.key_eq() == Compare(1));
564 assert(c2.get_allocator() == Alloc());
565 assert(static_cast<std::size_t>(std::distance(c2.begin(), c2.end())) == c2.size());
566 assert(static_cast<std::size_t>(std::distance(c2.cbegin(), c2.cend())) == c2.size());
567 assert(c2.max_load_factor() == 1);
568 }
569 #endif
570 }
571