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_map>
11 
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
15 
16 // void rehash(size_type n);
17 
18 #include <unordered_map>
19 #include <string>
20 #include <cassert>
21 
22 #include "min_allocator.h"
23 
24 template <class C>
test(const C & c)25 void test(const C& c)
26 {
27     assert(c.size() == 4);
28     assert(c.at(1) == "one");
29     assert(c.at(2) == "two");
30     assert(c.at(3) == "three");
31     assert(c.at(4) == "four");
32 }
33 
main()34 int main()
35 {
36     {
37         typedef std::unordered_map<int, std::string> C;
38         typedef std::pair<int, std::string> P;
39         P a[] =
40         {
41             P(1, "one"),
42             P(2, "two"),
43             P(3, "three"),
44             P(4, "four"),
45             P(1, "four"),
46             P(2, "four"),
47         };
48         C c(a, a + sizeof(a)/sizeof(a[0]));
49         test(c);
50         assert(c.bucket_count() >= 5);
51         c.rehash(3);
52         assert(c.bucket_count() == 5);
53         test(c);
54         c.max_load_factor(2);
55         c.rehash(3);
56         assert(c.bucket_count() == 3);
57         test(c);
58         c.rehash(31);
59         assert(c.bucket_count() == 31);
60         test(c);
61     }
62 #if __cplusplus >= 201103L
63     {
64         typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
65                             min_allocator<std::pair<const int, std::string>>> C;
66         typedef std::pair<int, std::string> P;
67         P a[] =
68         {
69             P(1, "one"),
70             P(2, "two"),
71             P(3, "three"),
72             P(4, "four"),
73             P(1, "four"),
74             P(2, "four"),
75         };
76         C c(a, a + sizeof(a)/sizeof(a[0]));
77         test(c);
78         assert(c.bucket_count() >= 5);
79         c.rehash(3);
80         assert(c.bucket_count() == 5);
81         test(c);
82         c.max_load_factor(2);
83         c.rehash(3);
84         assert(c.bucket_count() == 3);
85         test(c);
86         c.rehash(31);
87         assert(c.bucket_count() == 31);
88         test(c);
89     }
90 #endif
91 }
92