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 | // template <class InputIterator> |
16 | // unordered_multimap(InputIterator first, InputIterator last); |
17 | |
18 | #include <unordered_map> |
19 | #include <string> |
20 | #include <set> |
21 | #include <cassert> |
22 | #include <cfloat> |
23 | #include <cmath> |
24 | #include <cstddef> |
25 | |
26 | #include "test_macros.h" |
27 | #include "test_iterators.h" |
28 | #include "../../../check_consecutive.h" |
29 | #include "../../../NotConstructible.h" |
30 | #include "../../../test_compare.h" |
31 | #include "../../../test_hash.h" |
32 | #include "test_allocator.h" |
33 | #include "min_allocator.h" |
34 | |
35 | int main(int, char**) |
36 | { |
37 | { |
38 | typedef std::unordered_multimap<int, std::string, |
39 | test_hash<int>, |
40 | test_equal_to<int>, |
41 | test_allocator<std::pair<const int, std::string> > |
42 | > C; |
43 | typedef std::pair<int, std::string> P; |
44 | P a[] = |
45 | { |
46 | P(1, "one" ), |
47 | P(2, "two" ), |
48 | P(3, "three" ), |
49 | P(4, "four" ), |
50 | P(1, "four" ), |
51 | P(2, "four" ), |
52 | }; |
53 | C c(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a)/sizeof(a[0]))); |
54 | assert(c.bucket_count() >= 7); |
55 | assert(c.size() == 6); |
56 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
57 | Eq eq = c.equal_range(1); |
58 | assert(std::distance(eq.first, eq.second) == 2); |
59 | std::multiset<std::string> s; |
60 | s.insert(x: "one" ); |
61 | s.insert(x: "four" ); |
62 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
63 | eq = c.equal_range(2); |
64 | assert(std::distance(eq.first, eq.second) == 2); |
65 | s.insert(x: "two" ); |
66 | s.insert(x: "four" ); |
67 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
68 | |
69 | eq = c.equal_range(3); |
70 | assert(std::distance(eq.first, eq.second) == 1); |
71 | C::const_iterator 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(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
82 | assert(c.max_load_factor() == 1); |
83 | assert(c.hash_function() == test_hash<int>()); |
84 | assert(c.key_eq() == test_equal_to<int>()); |
85 | assert((c.get_allocator() == test_allocator<std::pair<const int, std::string> >())); |
86 | } |
87 | #if TEST_STD_VER >= 11 |
88 | { |
89 | typedef std::unordered_multimap<int, std::string, |
90 | test_hash<int>, |
91 | test_equal_to<int>, |
92 | min_allocator<std::pair<const int, std::string> > |
93 | > 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(cpp17_input_iterator<P*>(a), cpp17_input_iterator<P*>(a + sizeof(a)/sizeof(a[0]))); |
105 | assert(c.bucket_count() >= 7); |
106 | assert(c.size() == 6); |
107 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
108 | Eq eq = c.equal_range(1); |
109 | assert(std::distance(eq.first, eq.second) == 2); |
110 | std::multiset<std::string> s; |
111 | s.insert("one" ); |
112 | s.insert("four" ); |
113 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
114 | eq = c.equal_range(2); |
115 | assert(std::distance(eq.first, eq.second) == 2); |
116 | s.insert("two" ); |
117 | s.insert("four" ); |
118 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
119 | |
120 | eq = c.equal_range(3); |
121 | assert(std::distance(eq.first, eq.second) == 1); |
122 | C::const_iterator i = eq.first; |
123 | assert(i->first == 3); |
124 | assert(i->second == "three" ); |
125 | eq = c.equal_range(4); |
126 | assert(std::distance(eq.first, eq.second) == 1); |
127 | i = eq.first; |
128 | assert(i->first == 4); |
129 | assert(i->second == "four" ); |
130 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
131 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
132 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
133 | assert(c.max_load_factor() == 1); |
134 | assert(c.hash_function() == test_hash<int>()); |
135 | assert(c.key_eq() == test_equal_to<int>()); |
136 | assert((c.get_allocator() == min_allocator<std::pair<const int, std::string> >())); |
137 | } |
138 | #if TEST_STD_VER > 11 |
139 | { |
140 | typedef std::pair<int, std::string> P; |
141 | typedef test_allocator<std::pair<const int, std::string>> A; |
142 | typedef test_hash<int> HF; |
143 | typedef test_equal_to<int> Comp; |
144 | typedef std::unordered_multimap<int, std::string, HF, Comp, A> C; |
145 | |
146 | P arr[] = |
147 | { |
148 | P(1, "one" ), |
149 | P(2, "two" ), |
150 | P(3, "three" ), |
151 | P(4, "four" ), |
152 | P(1, "four" ), |
153 | P(2, "four" ), |
154 | }; |
155 | A a(42); |
156 | C c(cpp17_input_iterator<P*>(arr), cpp17_input_iterator<P*>(arr + sizeof(arr)/sizeof(arr[0])), 14, a); |
157 | assert(c.bucket_count() >= 14); |
158 | assert(c.size() == 6); |
159 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
160 | Eq eq = c.equal_range(1); |
161 | assert(std::distance(eq.first, eq.second) == 2); |
162 | std::multiset<std::string> s; |
163 | s.insert("one" ); |
164 | s.insert("four" ); |
165 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
166 | eq = c.equal_range(2); |
167 | assert(std::distance(eq.first, eq.second) == 2); |
168 | s.insert("two" ); |
169 | s.insert("four" ); |
170 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
171 | |
172 | eq = c.equal_range(3); |
173 | assert(std::distance(eq.first, eq.second) == 1); |
174 | C::const_iterator i = eq.first; |
175 | assert(i->first == 3); |
176 | assert(i->second == "three" ); |
177 | eq = c.equal_range(4); |
178 | assert(std::distance(eq.first, eq.second) == 1); |
179 | i = eq.first; |
180 | assert(i->first == 4); |
181 | assert(i->second == "four" ); |
182 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
183 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
184 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
185 | assert(c.max_load_factor() == 1); |
186 | assert(c.hash_function() == HF()); |
187 | assert(c.key_eq() == Comp()); |
188 | assert(c.get_allocator() == a); |
189 | assert(!(c.get_allocator() == A())); |
190 | } |
191 | { |
192 | typedef std::pair<int, std::string> P; |
193 | typedef test_allocator<std::pair<const int, std::string>> A; |
194 | typedef test_hash<int> HF; |
195 | typedef test_equal_to<int> Comp; |
196 | typedef std::unordered_multimap<int, std::string, HF, Comp, A> C; |
197 | |
198 | P arr[] = |
199 | { |
200 | P(1, "one" ), |
201 | P(2, "two" ), |
202 | P(3, "three" ), |
203 | P(4, "four" ), |
204 | P(1, "four" ), |
205 | P(2, "four" ), |
206 | }; |
207 | A a(42); |
208 | HF hf (43); |
209 | C c(cpp17_input_iterator<P*>(arr), cpp17_input_iterator<P*>(arr + sizeof(arr)/sizeof(arr[0])), 12, hf, a ); |
210 | assert(c.bucket_count() >= 12); |
211 | assert(c.size() == 6); |
212 | typedef std::pair<C::const_iterator, C::const_iterator> Eq; |
213 | Eq eq = c.equal_range(1); |
214 | assert(std::distance(eq.first, eq.second) == 2); |
215 | std::multiset<std::string> s; |
216 | s.insert("one" ); |
217 | s.insert("four" ); |
218 | CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s); |
219 | eq = c.equal_range(2); |
220 | assert(std::distance(eq.first, eq.second) == 2); |
221 | s.insert("two" ); |
222 | s.insert("four" ); |
223 | CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s); |
224 | |
225 | eq = c.equal_range(3); |
226 | assert(std::distance(eq.first, eq.second) == 1); |
227 | C::const_iterator i = eq.first; |
228 | assert(i->first == 3); |
229 | assert(i->second == "three" ); |
230 | eq = c.equal_range(4); |
231 | assert(std::distance(eq.first, eq.second) == 1); |
232 | i = eq.first; |
233 | assert(i->first == 4); |
234 | assert(i->second == "four" ); |
235 | assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size()); |
236 | assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size()); |
237 | assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); |
238 | assert(c.max_load_factor() == 1); |
239 | assert(c.hash_function() == hf); |
240 | assert(!(c.hash_function() == HF())); |
241 | assert(c.key_eq() == Comp()); |
242 | assert(c.get_allocator() == a); |
243 | assert(!(c.get_allocator() == A())); |
244 | } |
245 | #endif |
246 | #endif |
247 | |
248 | return 0; |
249 | } |
250 | |