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 #include <cfloat>
22
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 typedef std::pair<typename C::const_iterator, typename C::const_iterator> Eq;
30 Eq eq = c.equal_range(1);
31 assert(std::distance(eq.first, eq.second) == 2);
32 typename C::const_iterator i = eq.first;
33 assert(i->first == 1);
34 assert(i->second == "one");
35 ++i;
36 assert(i->first == 1);
37 assert(i->second == "four");
38 eq = c.equal_range(2);
39 assert(std::distance(eq.first, eq.second) == 2);
40 i = eq.first;
41 assert(i->first == 2);
42 assert(i->second == "two");
43 ++i;
44 assert(i->first == 2);
45 assert(i->second == "four");
46
47 eq = c.equal_range(3);
48 assert(std::distance(eq.first, eq.second) == 1);
49 i = eq.first;
50 assert(i->first == 3);
51 assert(i->second == "three");
52 eq = c.equal_range(4);
53 assert(std::distance(eq.first, eq.second) == 1);
54 i = eq.first;
55 assert(i->first == 4);
56 assert(i->second == "four");
57 assert(std::distance(c.begin(), c.end()) == c.size());
58 assert(std::distance(c.cbegin(), c.cend()) == c.size());
59 assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
60 }
61
main()62 int main()
63 {
64 {
65 typedef std::unordered_multimap<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() >= 7);
79 c.rehash(3);
80 assert(c.bucket_count() == 7);
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 #if __cplusplus >= 201103L
91 {
92 typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
93 min_allocator<std::pair<const int, std::string>>> C;
94 typedef std::pair<int, std::string> P;
95 P a[] =
96 {
97 P(1, "one"),
98 P(2, "two"),
99 P(3, "three"),
100 P(4, "four"),
101 P(1, "four"),
102 P(2, "four"),
103 };
104 C c(a, a + sizeof(a)/sizeof(a[0]));
105 test(c);
106 assert(c.bucket_count() >= 7);
107 c.rehash(3);
108 assert(c.bucket_count() == 7);
109 test(c);
110 c.max_load_factor(2);
111 c.rehash(3);
112 assert(c.bucket_count() == 3);
113 test(c);
114 c.rehash(31);
115 assert(c.bucket_count() == 31);
116 test(c);
117 }
118 #endif
119 }
120