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