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