1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
13 // class Alloc = allocator<pair<const Key, T>>>
14 // class unordered_map
16 // void swap(unordered_map& __u);
18 #include <unordered_map>
22 #include "../../test_compare.h"
23 #include "../../test_hash.h"
24 #include "test_allocator.h"
25 #include "min_allocator.h"
30 typedef test_hash<std::hash<int> > Hash;
31 typedef test_compare<std::equal_to<int> > Compare;
32 typedef test_allocator<std::pair<const int, std::string> > Alloc;
33 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
34 typedef std::pair<int, std::string> P;
35 C c1(0, Hash(1), Compare(1), Alloc(1));
36 C c2(0, Hash(2), Compare(2), Alloc(2));
37 c2.max_load_factor(2);
40 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() == Alloc(1));
45 assert(std::distance(c1.begin(), c1.end()) == c1.size());
46 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
47 assert(c1.max_load_factor() == 2);
49 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() == Alloc(2));
54 assert(std::distance(c2.begin(), c2.end()) == c2.size());
55 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
56 assert(c2.max_load_factor() == 1);
59 typedef test_hash<std::hash<int> > Hash;
60 typedef test_compare<std::equal_to<int> > Compare;
61 typedef test_allocator<std::pair<const int, std::string> > Alloc;
62 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
63 typedef std::pair<int, std::string> P;
75 C c1(0, Hash(1), Compare(1), Alloc(1));
76 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
77 c2.max_load_factor(2);
80 assert(c1.bucket_count() >= 11);
81 assert(c1.size() == 8);
82 assert(c1.at(10) == "ten");
83 assert(c1.at(20) == "twenty");
84 assert(c1.at(30) == "thirty");
85 assert(c1.at(40) == "forty");
86 assert(c1.at(50) == "fifty");
87 assert(c1.at(60) == "sixty");
88 assert(c1.at(70) == "seventy");
89 assert(c1.at(80) == "eighty");
90 assert(c1.hash_function() == Hash(2));
91 assert(c1.key_eq() == Compare(2));
92 assert(c1.get_allocator() == Alloc(1));
93 assert(std::distance(c1.begin(), c1.end()) == c1.size());
94 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
95 assert(c1.max_load_factor() == 2);
97 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() == Alloc(2));
102 assert(std::distance(c2.begin(), c2.end()) == c2.size());
103 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
104 assert(c2.max_load_factor() == 1);
107 typedef test_hash<std::hash<int> > Hash;
108 typedef test_compare<std::equal_to<int> > Compare;
109 typedef test_allocator<std::pair<const int, std::string> > Alloc;
110 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
111 typedef std::pair<int, std::string> P;
121 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
122 C c2(0, Hash(2), Compare(2), Alloc(2));
123 c2.max_load_factor(2);
126 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() == Alloc(1));
131 assert(std::distance(c1.begin(), c1.end()) == c1.size());
132 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
133 assert(c1.max_load_factor() == 2);
135 assert(c2.bucket_count() >= 5);
136 assert(c2.size() == 4);
137 assert(c2.at(1) == "one");
138 assert(c2.at(2) == "two");
139 assert(c2.at(3) == "three");
140 assert(c2.at(4) == "four");
141 assert(c2.hash_function() == Hash(1));
142 assert(c2.key_eq() == Compare(1));
143 assert(c2.get_allocator() == Alloc(2));
144 assert(std::distance(c2.begin(), c2.end()) == c2.size());
145 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
146 assert(c2.max_load_factor() == 1);
149 typedef test_hash<std::hash<int> > Hash;
150 typedef test_compare<std::equal_to<int> > Compare;
151 typedef test_allocator<std::pair<const int, std::string> > Alloc;
152 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
153 typedef std::pair<int, std::string> P;
174 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
175 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
176 c2.max_load_factor(2);
179 assert(c1.bucket_count() >= 11);
180 assert(c1.size() == 8);
181 assert(c1.at(10) == "ten");
182 assert(c1.at(20) == "twenty");
183 assert(c1.at(30) == "thirty");
184 assert(c1.at(40) == "forty");
185 assert(c1.at(50) == "fifty");
186 assert(c1.at(60) == "sixty");
187 assert(c1.at(70) == "seventy");
188 assert(c1.at(80) == "eighty");
189 assert(c1.hash_function() == Hash(2));
190 assert(c1.key_eq() == Compare(2));
191 assert(c1.get_allocator() == Alloc(1));
192 assert(std::distance(c1.begin(), c1.end()) == c1.size());
193 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
194 assert(c1.max_load_factor() == 2);
196 assert(c2.bucket_count() >= 5);
197 assert(c2.size() == 4);
198 assert(c2.at(1) == "one");
199 assert(c2.at(2) == "two");
200 assert(c2.at(3) == "three");
201 assert(c2.at(4) == "four");
202 assert(c2.hash_function() == Hash(1));
203 assert(c2.key_eq() == Compare(1));
204 assert(c2.get_allocator() == Alloc(2));
205 assert(std::distance(c2.begin(), c2.end()) == c2.size());
206 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
207 assert(c2.max_load_factor() == 1);
211 typedef test_hash<std::hash<int> > Hash;
212 typedef test_compare<std::equal_to<int> > Compare;
213 typedef other_allocator<std::pair<const int, std::string> > Alloc;
214 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
215 typedef std::pair<int, std::string> P;
216 C c1(0, Hash(1), Compare(1), Alloc(1));
217 C c2(0, Hash(2), Compare(2), Alloc(2));
218 c2.max_load_factor(2);
221 assert(c1.bucket_count() == 0);
222 assert(c1.size() == 0);
223 assert(c1.hash_function() == Hash(2));
224 assert(c1.key_eq() == Compare(2));
225 assert(c1.get_allocator() == Alloc(2));
226 assert(std::distance(c1.begin(), c1.end()) == c1.size());
227 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
228 assert(c1.max_load_factor() == 2);
230 assert(c2.bucket_count() == 0);
231 assert(c2.size() == 0);
232 assert(c2.hash_function() == Hash(1));
233 assert(c2.key_eq() == Compare(1));
234 assert(c2.get_allocator() == Alloc(1));
235 assert(std::distance(c2.begin(), c2.end()) == c2.size());
236 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
237 assert(c2.max_load_factor() == 1);
240 typedef test_hash<std::hash<int> > Hash;
241 typedef test_compare<std::equal_to<int> > Compare;
242 typedef other_allocator<std::pair<const int, std::string> > Alloc;
243 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
244 typedef std::pair<int, std::string> P;
256 C c1(0, Hash(1), Compare(1), Alloc(1));
257 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
258 c2.max_load_factor(2);
261 assert(c1.bucket_count() >= 11);
262 assert(c1.size() == 8);
263 assert(c1.at(10) == "ten");
264 assert(c1.at(20) == "twenty");
265 assert(c1.at(30) == "thirty");
266 assert(c1.at(40) == "forty");
267 assert(c1.at(50) == "fifty");
268 assert(c1.at(60) == "sixty");
269 assert(c1.at(70) == "seventy");
270 assert(c1.at(80) == "eighty");
271 assert(c1.hash_function() == Hash(2));
272 assert(c1.key_eq() == Compare(2));
273 assert(c1.get_allocator() == Alloc(2));
274 assert(std::distance(c1.begin(), c1.end()) == c1.size());
275 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
276 assert(c1.max_load_factor() == 2);
278 assert(c2.bucket_count() == 0);
279 assert(c2.size() == 0);
280 assert(c2.hash_function() == Hash(1));
281 assert(c2.key_eq() == Compare(1));
282 assert(c2.get_allocator() == Alloc(1));
283 assert(std::distance(c2.begin(), c2.end()) == c2.size());
284 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
285 assert(c2.max_load_factor() == 1);
288 typedef test_hash<std::hash<int> > Hash;
289 typedef test_compare<std::equal_to<int> > Compare;
290 typedef other_allocator<std::pair<const int, std::string> > Alloc;
291 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
292 typedef std::pair<int, std::string> P;
302 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
303 C c2(0, Hash(2), Compare(2), Alloc(2));
304 c2.max_load_factor(2);
307 assert(c1.bucket_count() == 0);
308 assert(c1.size() == 0);
309 assert(c1.hash_function() == Hash(2));
310 assert(c1.key_eq() == Compare(2));
311 assert(c1.get_allocator() == Alloc(2));
312 assert(std::distance(c1.begin(), c1.end()) == c1.size());
313 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
314 assert(c1.max_load_factor() == 2);
316 assert(c2.bucket_count() >= 5);
317 assert(c2.size() == 4);
318 assert(c2.at(1) == "one");
319 assert(c2.at(2) == "two");
320 assert(c2.at(3) == "three");
321 assert(c2.at(4) == "four");
322 assert(c2.hash_function() == Hash(1));
323 assert(c2.key_eq() == Compare(1));
324 assert(c2.get_allocator() == Alloc(1));
325 assert(std::distance(c2.begin(), c2.end()) == c2.size());
326 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
327 assert(c2.max_load_factor() == 1);
330 typedef test_hash<std::hash<int> > Hash;
331 typedef test_compare<std::equal_to<int> > Compare;
332 typedef other_allocator<std::pair<const int, std::string> > Alloc;
333 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
334 typedef std::pair<int, std::string> P;
355 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc(1));
356 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc(2));
357 c2.max_load_factor(2);
360 assert(c1.bucket_count() >= 11);
361 assert(c1.size() == 8);
362 assert(c1.at(10) == "ten");
363 assert(c1.at(20) == "twenty");
364 assert(c1.at(30) == "thirty");
365 assert(c1.at(40) == "forty");
366 assert(c1.at(50) == "fifty");
367 assert(c1.at(60) == "sixty");
368 assert(c1.at(70) == "seventy");
369 assert(c1.at(80) == "eighty");
370 assert(c1.hash_function() == Hash(2));
371 assert(c1.key_eq() == Compare(2));
372 assert(c1.get_allocator() == Alloc(2));
373 assert(std::distance(c1.begin(), c1.end()) == c1.size());
374 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
375 assert(c1.max_load_factor() == 2);
377 assert(c2.bucket_count() >= 5);
378 assert(c2.size() == 4);
379 assert(c2.at(1) == "one");
380 assert(c2.at(2) == "two");
381 assert(c2.at(3) == "three");
382 assert(c2.at(4) == "four");
383 assert(c2.hash_function() == Hash(1));
384 assert(c2.key_eq() == Compare(1));
385 assert(c2.get_allocator() == Alloc(1));
386 assert(std::distance(c2.begin(), c2.end()) == c2.size());
387 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
388 assert(c2.max_load_factor() == 1);
390 #if __cplusplus >= 201103L
392 typedef test_hash<std::hash<int> > Hash;
393 typedef test_compare<std::equal_to<int> > Compare;
394 typedef min_allocator<std::pair<const int, std::string> > Alloc;
395 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
396 typedef std::pair<int, std::string> P;
397 C c1(0, Hash(1), Compare(1), Alloc());
398 C c2(0, Hash(2), Compare(2), Alloc());
399 c2.max_load_factor(2);
402 assert(c1.bucket_count() == 0);
403 assert(c1.size() == 0);
404 assert(c1.hash_function() == Hash(2));
405 assert(c1.key_eq() == Compare(2));
406 assert(c1.get_allocator() == Alloc());
407 assert(std::distance(c1.begin(), c1.end()) == c1.size());
408 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
409 assert(c1.max_load_factor() == 2);
411 assert(c2.bucket_count() == 0);
412 assert(c2.size() == 0);
413 assert(c2.hash_function() == Hash(1));
414 assert(c2.key_eq() == Compare(1));
415 assert(c2.get_allocator() == Alloc());
416 assert(std::distance(c2.begin(), c2.end()) == c2.size());
417 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
418 assert(c2.max_load_factor() == 1);
421 typedef test_hash<std::hash<int> > Hash;
422 typedef test_compare<std::equal_to<int> > Compare;
423 typedef min_allocator<std::pair<const int, std::string> > Alloc;
424 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
425 typedef std::pair<int, std::string> P;
437 C c1(0, Hash(1), Compare(1), Alloc());
438 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
439 c2.max_load_factor(2);
442 assert(c1.bucket_count() >= 11);
443 assert(c1.size() == 8);
444 assert(c1.at(10) == "ten");
445 assert(c1.at(20) == "twenty");
446 assert(c1.at(30) == "thirty");
447 assert(c1.at(40) == "forty");
448 assert(c1.at(50) == "fifty");
449 assert(c1.at(60) == "sixty");
450 assert(c1.at(70) == "seventy");
451 assert(c1.at(80) == "eighty");
452 assert(c1.hash_function() == Hash(2));
453 assert(c1.key_eq() == Compare(2));
454 assert(c1.get_allocator() == Alloc());
455 assert(std::distance(c1.begin(), c1.end()) == c1.size());
456 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
457 assert(c1.max_load_factor() == 2);
459 assert(c2.bucket_count() == 0);
460 assert(c2.size() == 0);
461 assert(c2.hash_function() == Hash(1));
462 assert(c2.key_eq() == Compare(1));
463 assert(c2.get_allocator() == Alloc());
464 assert(std::distance(c2.begin(), c2.end()) == c2.size());
465 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
466 assert(c2.max_load_factor() == 1);
469 typedef test_hash<std::hash<int> > Hash;
470 typedef test_compare<std::equal_to<int> > Compare;
471 typedef min_allocator<std::pair<const int, std::string> > Alloc;
472 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
473 typedef std::pair<int, std::string> P;
483 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
484 C c2(0, Hash(2), Compare(2), Alloc());
485 c2.max_load_factor(2);
488 assert(c1.bucket_count() == 0);
489 assert(c1.size() == 0);
490 assert(c1.hash_function() == Hash(2));
491 assert(c1.key_eq() == Compare(2));
492 assert(c1.get_allocator() == Alloc());
493 assert(std::distance(c1.begin(), c1.end()) == c1.size());
494 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
495 assert(c1.max_load_factor() == 2);
497 assert(c2.bucket_count() >= 5);
498 assert(c2.size() == 4);
499 assert(c2.at(1) == "one");
500 assert(c2.at(2) == "two");
501 assert(c2.at(3) == "three");
502 assert(c2.at(4) == "four");
503 assert(c2.hash_function() == Hash(1));
504 assert(c2.key_eq() == Compare(1));
505 assert(c2.get_allocator() == Alloc());
506 assert(std::distance(c2.begin(), c2.end()) == c2.size());
507 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
508 assert(c2.max_load_factor() == 1);
511 typedef test_hash<std::hash<int> > Hash;
512 typedef test_compare<std::equal_to<int> > Compare;
513 typedef min_allocator<std::pair<const int, std::string> > Alloc;
514 typedef std::unordered_map<int, std::string, Hash, Compare, Alloc> C;
515 typedef std::pair<int, std::string> P;
536 C c1(std::begin(a1), std::end(a1), 0, Hash(1), Compare(1), Alloc());
537 C c2(std::begin(a2), std::end(a2), 0, Hash(2), Compare(2), Alloc());
538 c2.max_load_factor(2);
541 assert(c1.bucket_count() >= 11);
542 assert(c1.size() == 8);
543 assert(c1.at(10) == "ten");
544 assert(c1.at(20) == "twenty");
545 assert(c1.at(30) == "thirty");
546 assert(c1.at(40) == "forty");
547 assert(c1.at(50) == "fifty");
548 assert(c1.at(60) == "sixty");
549 assert(c1.at(70) == "seventy");
550 assert(c1.at(80) == "eighty");
551 assert(c1.hash_function() == Hash(2));
552 assert(c1.key_eq() == Compare(2));
553 assert(c1.get_allocator() == Alloc());
554 assert(std::distance(c1.begin(), c1.end()) == c1.size());
555 assert(std::distance(c1.cbegin(), c1.cend()) == c1.size());
556 assert(c1.max_load_factor() == 2);
558 assert(c2.bucket_count() >= 5);
559 assert(c2.size() == 4);
560 assert(c2.at(1) == "one");
561 assert(c2.at(2) == "two");
562 assert(c2.at(3) == "three");
563 assert(c2.at(4) == "four");
564 assert(c2.hash_function() == Hash(1));
565 assert(c2.key_eq() == Compare(1));
566 assert(c2.get_allocator() == Alloc());
567 assert(std::distance(c2.begin(), c2.end()) == c2.size());
568 assert(std::distance(c2.cbegin(), c2.cend()) == c2.size());
569 assert(c2.max_load_factor() == 1);