1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // <unordered_map> |
10 | |
11 | // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, |
12 | // class Alloc = allocator<pair<const Key, T>>> |
13 | // class unordered_multimap |
14 | |
15 | // void rehash(size_type n); |
16 | |
17 | #include <unordered_map> |
18 | #include <string> |
19 | #include <set> |
20 | #include <cassert> |
21 | #include <cfloat> |
22 | #include <cmath> |
23 | #include <cstddef> |
24 | |
25 | #include "test_macros.h" |
26 | #include "min_allocator.h" |
27 | |
28 | template <class C> |
29 | void rehash_postcondition(const C& c, std::size_t n) |
30 | { |
31 | assert(c.bucket_count() >= c.size() / c.max_load_factor() && c.bucket_count() >= n); |
32 | } |
33 | |
34 | template <class C> |
35 | void test(const C& c) |
36 | { |
37 | assert(c.size() == 6); |
38 | typedef std::pair<typename C::const_iterator, typename C::const_iterator> Eq; |
39 | Eq eq = c.equal_range(1); |
40 | assert(std::distance(eq.first, eq.second) == 2); |
41 | typename C::const_iterator i = eq.first; |
42 | { |
43 | std::set<std::string> s; |
44 | s.insert(x: "one" ); |
45 | s.insert(x: "four" ); |
46 | for ( int n = 0; n < 2; ++n ) |
47 | { |
48 | assert(i->first == 1); |
49 | assert(s.find(i->second) != s.end()); |
50 | s.erase(s.find(i->second)); |
51 | ++i; |
52 | } |
53 | } |
54 | eq = c.equal_range(2); |
55 | assert(std::distance(eq.first, eq.second) == 2); |
56 | i = eq.first; |
57 | { |
58 | std::set<std::string> s; |
59 | s.insert(x: "two" ); |
60 | s.insert(x: "four" ); |
61 | for ( int n = 0; n < 2; ++n ) |
62 | { |
63 | assert(i->first == 2); |
64 | assert(s.find(i->second) != s.end()); |
65 | s.erase(s.find(i->second)); |
66 | ++i; |
67 | } |
68 | } |
69 | eq = c.equal_range(3); |
70 | assert(std::distance(eq.first, eq.second) == 1); |
71 | i = eq.first; |
72 | assert(i->first == 3); |
73 | assert(i->second == "three" ); |
74 | eq = c.equal_range(4); |
75 | assert(std::distance(eq.first, eq.second) == 1); |
76 | i = eq.first; |
77 | assert(i->first == 4); |
78 | assert(i->second == "four" ); |
79 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
80 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
81 | assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
82 | } |
83 | |
84 | int main(int, char**) |
85 | { |
86 | { |
87 | typedef std::unordered_multimap<int, std::string> C; |
88 | typedef std::pair<int, std::string> P; |
89 | P a[] = |
90 | { |
91 | P(1, "one" ), |
92 | P(2, "two" ), |
93 | P(3, "three" ), |
94 | P(4, "four" ), |
95 | P(1, "four" ), |
96 | P(2, "four" ), |
97 | }; |
98 | C c(a, a + sizeof(a)/sizeof(a[0])); |
99 | test(c); |
100 | assert(c.bucket_count() >= 7); |
101 | c.rehash(n: 3); |
102 | rehash_postcondition(c, n: 3); |
103 | LIBCPP_ASSERT(c.bucket_count() == 7); |
104 | test(c); |
105 | c.max_load_factor(z: 2); |
106 | c.rehash(n: 3); |
107 | rehash_postcondition(c, n: 3); |
108 | LIBCPP_ASSERT(c.bucket_count() == 3); |
109 | test(c); |
110 | c.rehash(n: 31); |
111 | rehash_postcondition(c, n: 31); |
112 | LIBCPP_ASSERT(c.bucket_count() == 31); |
113 | test(c); |
114 | } |
115 | #if TEST_STD_VER >= 11 |
116 | { |
117 | typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>, |
118 | min_allocator<std::pair<const int, std::string>>> C; |
119 | typedef std::pair<int, std::string> P; |
120 | P a[] = |
121 | { |
122 | P(1, "one" ), |
123 | P(2, "two" ), |
124 | P(3, "three" ), |
125 | P(4, "four" ), |
126 | P(1, "four" ), |
127 | P(2, "four" ), |
128 | }; |
129 | C c(a, a + sizeof(a)/sizeof(a[0])); |
130 | test(c); |
131 | assert(c.bucket_count() >= 7); |
132 | c.rehash(3); |
133 | rehash_postcondition(c, 3); |
134 | LIBCPP_ASSERT(c.bucket_count() == 7); |
135 | test(c); |
136 | c.max_load_factor(2); |
137 | c.rehash(3); |
138 | rehash_postcondition(c, 3); |
139 | LIBCPP_ASSERT(c.bucket_count() == 3); |
140 | test(c); |
141 | c.rehash(31); |
142 | rehash_postcondition(c, 31); |
143 | LIBCPP_ASSERT(c.bucket_count() == 31); |
144 | test(c); |
145 | } |
146 | #endif |
147 | |
148 | return 0; |
149 | } |
150 | |