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