Debian Codegraph @

tests/unordered_map_constructor_test.h

raw
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef PHMAP_PRIV_UNORDERED_MAP_CONSTRUCTOR_TEST_H_
16 #define PHMAP_PRIV_UNORDERED_MAP_CONSTRUCTOR_TEST_H_
17
18 #include <algorithm>
19 #include <vector>
20 #include <unordered_map>
21
22 #ifdef _MSC_VER
23 #pragma warning(push, 0)
24 #endif
25
26 #include "gmock/gmock.h"
27 #include "gtest/gtest.h"
28 #include "hash_generator_testing.h"
29 #include "hash_policy_testing.h"
30
31 #ifdef _MSC_VER
32 #pragma warning(pop)
33 #endif
34
35 namespace phmap {
36 namespace priv {
37
38 template <class UnordMap>
39 class ConstructorTest : public ::testing::Test {};
40
41 TYPED_TEST_SUITE_P(ConstructorTest);
42
43 TYPED_TEST_P(ConstructorTest, NoArgs) {
44 TypeParam m;
45 EXPECT_TRUE(m.empty());
46 EXPECT_THAT(m, ::testing::UnorderedElementsAre());
47 }
48
49 TYPED_TEST_P(ConstructorTest, BucketCount) {
50 TypeParam m(123);
51 EXPECT_TRUE(m.empty());
52 EXPECT_THAT(m, ::testing::UnorderedElementsAre());
53 EXPECT_GE(m.bucket_count(), 123);
54 }
55
56 TYPED_TEST_P(ConstructorTest, BucketCountHash) {
57 using H = typename TypeParam::hasher;
58 H hasher;
59 TypeParam m(123, hasher);
60 EXPECT_EQ(m.hash_function(), hasher);
61 EXPECT_TRUE(m.empty());
62 EXPECT_THAT(m, ::testing::UnorderedElementsAre());
63 EXPECT_GE(m.bucket_count(), 123);
64 }
65
66 TYPED_TEST_P(ConstructorTest, BucketCountHashEqual) {
67 using H = typename TypeParam::hasher;
68 using E = typename TypeParam::key_equal;
69 H hasher;
70 E equal;
71 TypeParam m(123, hasher, equal);
72 EXPECT_EQ(m.hash_function(), hasher);
73 EXPECT_EQ(m.key_eq(), equal);
74 EXPECT_TRUE(m.empty());
75 EXPECT_THAT(m, ::testing::UnorderedElementsAre());
76 EXPECT_GE(m.bucket_count(), 123);
77 }
78
79 TYPED_TEST_P(ConstructorTest, BucketCountHashEqualAlloc) {
80 using H = typename TypeParam::hasher;
81 using E = typename TypeParam::key_equal;
82 using A = typename TypeParam::allocator_type;
83 H hasher;
84 E equal;
85 A alloc(0);
86 TypeParam m(123, hasher, equal, alloc);
87 EXPECT_EQ(m.hash_function(), hasher);
88 EXPECT_EQ(m.key_eq(), equal);
89 EXPECT_EQ(m.get_allocator(), alloc);
90 EXPECT_TRUE(m.empty());
91 EXPECT_THAT(m, ::testing::UnorderedElementsAre());
92 EXPECT_GE(m.bucket_count(), 123);
93 }
94
95 template <typename T>
96 struct is_std_unordered_map : std::false_type {};
97
98 template <typename... T>
99 struct is_std_unordered_map<std::unordered_map<T...>> : std::true_type {};
100
101 #if defined(UNORDERED_MAP_CXX14) || defined(UNORDERED_MAP_CXX17)
102 using has_cxx14_std_apis = std::true_type;
103 #else
104 using has_cxx14_std_apis = std::false_type;
105 #endif
106
107 template <typename T>
108 using expect_cxx14_apis =
109 phmap::disjunction<phmap::negation<is_std_unordered_map<T>>,
110 has_cxx14_std_apis>;
111
112 template <typename TypeParam>
113 void BucketCountAllocTest(std::false_type) {}
114
115 template <typename TypeParam>
116 void BucketCountAllocTest(std::true_type) {
117 using A = typename TypeParam::allocator_type;
118 A alloc(0);
119 TypeParam m(123, alloc);
120 EXPECT_EQ(m.get_allocator(), alloc);
121 EXPECT_TRUE(m.empty());
122 EXPECT_THAT(m, ::testing::UnorderedElementsAre());
123 EXPECT_GE(m.bucket_count(), 123);
124 }
125
126 TYPED_TEST_P(ConstructorTest, BucketCountAlloc) {
127 BucketCountAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
128 }
129
130 template <typename TypeParam>
131 void BucketCountHashAllocTest(std::false_type) {}
132
133 template <typename TypeParam>
134 void BucketCountHashAllocTest(std::true_type) {
135 using H = typename TypeParam::hasher;
136 using A = typename TypeParam::allocator_type;
137 H hasher;
138 A alloc(0);
139 TypeParam m(123, hasher, alloc);
140 EXPECT_EQ(m.hash_function(), hasher);
141 EXPECT_EQ(m.get_allocator(), alloc);
142 EXPECT_TRUE(m.empty());
143 EXPECT_THAT(m, ::testing::UnorderedElementsAre());
144 EXPECT_GE(m.bucket_count(), 123);
145 }
146
147 TYPED_TEST_P(ConstructorTest, BucketCountHashAlloc) {
148 BucketCountHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
149 }
150
151 #if PHMAP_UNORDERED_SUPPORTS_ALLOC_CTORS
152 using has_alloc_std_constructors = std::true_type;
153 #else
154 using has_alloc_std_constructors = std::false_type;
155 #endif
156
157 template <typename T>
158 using expect_alloc_constructors =
159 phmap::disjunction<phmap::negation<is_std_unordered_map<T>>,
160 has_alloc_std_constructors>;
161
162 template <typename TypeParam>
163 void AllocTest(std::false_type) {}
164
165 template <typename TypeParam>
166 void AllocTest(std::true_type) {
167 using A = typename TypeParam::allocator_type;
168 A alloc(0);
169 TypeParam m(alloc);
170 EXPECT_EQ(m.get_allocator(), alloc);
171 EXPECT_TRUE(m.empty());
172 EXPECT_THAT(m, ::testing::UnorderedElementsAre());
173 }
174
175 TYPED_TEST_P(ConstructorTest, Alloc) {
176 AllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
177 }
178
179 TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashEqualAlloc) {
180 using T = hash_internal::GeneratedType<TypeParam>;
181 using H = typename TypeParam::hasher;
182 using E = typename TypeParam::key_equal;
183 using A = typename TypeParam::allocator_type;
184 H hasher;
185 E equal;
186 A alloc(0);
187 std::vector<T> values;
188 std::generate_n(std::back_inserter(values), 10,
189 hash_internal::Generator<T>());
190 TypeParam m(values.begin(), values.end(), 123, hasher, equal, alloc);
191 EXPECT_EQ(m.hash_function(), hasher);
192 EXPECT_EQ(m.key_eq(), equal);
193 EXPECT_EQ(m.get_allocator(), alloc);
194 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
195 EXPECT_GE(m.bucket_count(), 123);
196 }
197
198 template <typename TypeParam>
199 void InputIteratorBucketAllocTest(std::false_type) {}
200
201 template <typename TypeParam>
202 void InputIteratorBucketAllocTest(std::true_type) {
203 using T = hash_internal::GeneratedType<TypeParam>;
204 using A = typename TypeParam::allocator_type;
205 A alloc(0);
206 std::vector<T> values;
207 std::generate_n(std::back_inserter(values), 10,
208 hash_internal::Generator<T>());
209 TypeParam m(values.begin(), values.end(), 123, alloc);
210 EXPECT_EQ(m.get_allocator(), alloc);
211 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
212 EXPECT_GE(m.bucket_count(), 123);
213 }
214
215 TYPED_TEST_P(ConstructorTest, InputIteratorBucketAlloc) {
216 InputIteratorBucketAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
217 }
218
219 template <typename TypeParam>
220 void InputIteratorBucketHashAllocTest(std::false_type) {}
221
222 template <typename TypeParam>
223 void InputIteratorBucketHashAllocTest(std::true_type) {
224 using T = hash_internal::GeneratedType<TypeParam>;
225 using H = typename TypeParam::hasher;
226 using A = typename TypeParam::allocator_type;
227 H hasher;
228 A alloc(0);
229 std::vector<T> values;
230 std::generate_n(std::back_inserter(values), 10,
231 hash_internal::Generator<T>());
232 TypeParam m(values.begin(), values.end(), 123, hasher, alloc);
233 EXPECT_EQ(m.hash_function(), hasher);
234 EXPECT_EQ(m.get_allocator(), alloc);
235 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
236 EXPECT_GE(m.bucket_count(), 123);
237 }
238
239 TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashAlloc) {
240 InputIteratorBucketHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
241 }
242
243 TYPED_TEST_P(ConstructorTest, CopyConstructor) {
244 using T = hash_internal::GeneratedType<TypeParam>;
245 using H = typename TypeParam::hasher;
246 using E = typename TypeParam::key_equal;
247 using A = typename TypeParam::allocator_type;
248 H hasher;
249 E equal;
250 A alloc(0);
251 TypeParam m(123, hasher, equal, alloc);
252 for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
253 TypeParam n(m);
254 EXPECT_EQ(m.hash_function(), n.hash_function());
255 EXPECT_EQ(m.key_eq(), n.key_eq());
256 EXPECT_EQ(m.get_allocator(), n.get_allocator());
257 EXPECT_EQ(m, n);
258 }
259
260 template <typename TypeParam>
261 void CopyConstructorAllocTest(std::false_type) {}
262
263 template <typename TypeParam>
264 void CopyConstructorAllocTest(std::true_type) {
265 using T = hash_internal::GeneratedType<TypeParam>;
266 using H = typename TypeParam::hasher;
267 using E = typename TypeParam::key_equal;
268 using A = typename TypeParam::allocator_type;
269 H hasher;
270 E equal;
271 A alloc(0);
272 TypeParam m(123, hasher, equal, alloc);
273 for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
274 TypeParam n(m, A(11));
275 EXPECT_EQ(m.hash_function(), n.hash_function());
276 EXPECT_EQ(m.key_eq(), n.key_eq());
277 EXPECT_NE(m.get_allocator(), n.get_allocator());
278 EXPECT_EQ(m, n);
279 }
280
281 TYPED_TEST_P(ConstructorTest, CopyConstructorAlloc) {
282 CopyConstructorAllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
283 }
284
285 // TODO(alkis): Test non-propagating allocators on copy constructors.
286
287 TYPED_TEST_P(ConstructorTest, MoveConstructor) {
288 using T = hash_internal::GeneratedType<TypeParam>;
289 using H = typename TypeParam::hasher;
290 using E = typename TypeParam::key_equal;
291 using A = typename TypeParam::allocator_type;
292 H hasher;
293 E equal;
294 A alloc(0);
295 TypeParam m(123, hasher, equal, alloc);
296 for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
297 TypeParam t(m);
298 TypeParam n(std::move(t));
299 EXPECT_EQ(m.hash_function(), n.hash_function());
300 EXPECT_EQ(m.key_eq(), n.key_eq());
301 EXPECT_EQ(m.get_allocator(), n.get_allocator());
302 EXPECT_EQ(m, n);
303 }
304
305 template <typename TypeParam>
306 void MoveConstructorAllocTest(std::false_type) {}
307
308 template <typename TypeParam>
309 void MoveConstructorAllocTest(std::true_type) {
310 using T = hash_internal::GeneratedType<TypeParam>;
311 using H = typename TypeParam::hasher;
312 using E = typename TypeParam::key_equal;
313 using A = typename TypeParam::allocator_type;
314 H hasher;
315 E equal;
316 A alloc(0);
317 TypeParam m(123, hasher, equal, alloc);
318 for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
319 TypeParam t(m);
320 TypeParam n(std::move(t), A(1));
321 EXPECT_EQ(m.hash_function(), n.hash_function());
322 EXPECT_EQ(m.key_eq(), n.key_eq());
323 EXPECT_NE(m.get_allocator(), n.get_allocator());
324 EXPECT_EQ(m, n);
325 }
326
327 TYPED_TEST_P(ConstructorTest, MoveConstructorAlloc) {
328 MoveConstructorAllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
329 }
330
331 // TODO(alkis): Test non-propagating allocators on move constructors.
332
333 TYPED_TEST_P(ConstructorTest, InitializerListBucketHashEqualAlloc) {
334 using T = hash_internal::GeneratedType<TypeParam>;
335 hash_internal::Generator<T> gen;
336 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
337 using H = typename TypeParam::hasher;
338 using E = typename TypeParam::key_equal;
339 using A = typename TypeParam::allocator_type;
340 H hasher;
341 E equal;
342 A alloc(0);
343 TypeParam m(values, 123, hasher, equal, alloc);
344 EXPECT_EQ(m.hash_function(), hasher);
345 EXPECT_EQ(m.key_eq(), equal);
346 EXPECT_EQ(m.get_allocator(), alloc);
347 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
348 EXPECT_GE(m.bucket_count(), 123);
349 }
350
351 template <typename TypeParam>
352 void InitializerListBucketAllocTest(std::false_type) {}
353
354 template <typename TypeParam>
355 void InitializerListBucketAllocTest(std::true_type) {
356 using T = hash_internal::GeneratedType<TypeParam>;
357 using A = typename TypeParam::allocator_type;
358 hash_internal::Generator<T> gen;
359 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
360 A alloc(0);
361 TypeParam m(values, 123, alloc);
362 EXPECT_EQ(m.get_allocator(), alloc);
363 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
364 EXPECT_GE(m.bucket_count(), 123);
365 }
366
367 TYPED_TEST_P(ConstructorTest, InitializerListBucketAlloc) {
368 InitializerListBucketAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
369 }
370
371 template <typename TypeParam>
372 void InitializerListBucketHashAllocTest(std::false_type) {}
373
374 template <typename TypeParam>
375 void InitializerListBucketHashAllocTest(std::true_type) {
376 using T = hash_internal::GeneratedType<TypeParam>;
377 using H = typename TypeParam::hasher;
378 using A = typename TypeParam::allocator_type;
379 H hasher;
380 A alloc(0);
381 hash_internal::Generator<T> gen;
382 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
383 TypeParam m(values, 123, hasher, alloc);
384 EXPECT_EQ(m.hash_function(), hasher);
385 EXPECT_EQ(m.get_allocator(), alloc);
386 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
387 EXPECT_GE(m.bucket_count(), 123);
388 }
389
390 TYPED_TEST_P(ConstructorTest, InitializerListBucketHashAlloc) {
391 InitializerListBucketHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
392 }
393
394 TYPED_TEST_P(ConstructorTest, Assignment) {
395 using T = hash_internal::GeneratedType<TypeParam>;
396 using H = typename TypeParam::hasher;
397 using E = typename TypeParam::key_equal;
398 using A = typename TypeParam::allocator_type;
399 H hasher;
400 E equal;
401 A alloc(0);
402 hash_internal::Generator<T> gen;
403 TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
404 TypeParam n;
405 n = m;
406 EXPECT_EQ(m.hash_function(), n.hash_function());
407 EXPECT_EQ(m.key_eq(), n.key_eq());
408 EXPECT_EQ(m, n);
409 }
410
411 // TODO(alkis): Test [non-]propagating allocators on move/copy assignments
412 // (it depends on traits).
413
414 TYPED_TEST_P(ConstructorTest, MoveAssignment) {
415 using T = hash_internal::GeneratedType<TypeParam>;
416 using H = typename TypeParam::hasher;
417 using E = typename TypeParam::key_equal;
418 using A = typename TypeParam::allocator_type;
419 H hasher;
420 E equal;
421 A alloc(0);
422 hash_internal::Generator<T> gen;
423 TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
424 TypeParam t(m);
425 TypeParam n;
426 n = std::move(t);
427 EXPECT_EQ(m.hash_function(), n.hash_function());
428 EXPECT_EQ(m.key_eq(), n.key_eq());
429 EXPECT_EQ(m, n);
430 }
431
432 TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerList) {
433 using T = hash_internal::GeneratedType<TypeParam>;
434 hash_internal::Generator<T> gen;
435 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
436 TypeParam m;
437 m = values;
438 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
439 }
440
441 TYPED_TEST_P(ConstructorTest, AssignmentOverwritesExisting) {
442 using T = hash_internal::GeneratedType<TypeParam>;
443 hash_internal::Generator<T> gen;
444 TypeParam m({gen(), gen(), gen()});
445 TypeParam n({gen()});
446 n = m;
447 EXPECT_EQ(m, n);
448 }
449
450 TYPED_TEST_P(ConstructorTest, MoveAssignmentOverwritesExisting) {
451 using T = hash_internal::GeneratedType<TypeParam>;
452 hash_internal::Generator<T> gen;
453 TypeParam m({gen(), gen(), gen()});
454 TypeParam t(m);
455 TypeParam n({gen()});
456 n = std::move(t);
457 EXPECT_EQ(m, n);
458 }
459
460 TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerListOverwritesExisting) {
461 using T = hash_internal::GeneratedType<TypeParam>;
462 hash_internal::Generator<T> gen;
463 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
464 TypeParam m;
465 m = values;
466 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
467 }
468
469 TYPED_TEST_P(ConstructorTest, AssignmentOnSelf) {
470 using T = hash_internal::GeneratedType<TypeParam>;
471 hash_internal::Generator<T> gen;
472 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
473 TypeParam m(values);
474 m = *&m; // Avoid -Wself-assign
475 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
476 }
477
478 // We cannot test self move as standard states that it leaves standard
479 // containers in unspecified state (and in practice in causes memory-leak
480 // according to heap-checker!).
481
482 REGISTER_TYPED_TEST_SUITE_P(
483 ConstructorTest, NoArgs, BucketCount, BucketCountHash, BucketCountHashEqual,
484 BucketCountHashEqualAlloc, BucketCountAlloc, BucketCountHashAlloc, Alloc,
485 InputIteratorBucketHashEqualAlloc, InputIteratorBucketAlloc,
486 InputIteratorBucketHashAlloc, CopyConstructor, CopyConstructorAlloc,
487 MoveConstructor, MoveConstructorAlloc, InitializerListBucketHashEqualAlloc,
488 InitializerListBucketAlloc, InitializerListBucketHashAlloc, Assignment,
489 MoveAssignment, AssignmentFromInitializerList, AssignmentOverwritesExisting,
490 MoveAssignmentOverwritesExisting,
491 AssignmentFromInitializerListOverwritesExisting, AssignmentOnSelf);
492
493 } // namespace priv
494 } // namespace phmap
495
496 #endif // PHMAP_PRIV_UNORDERED_MAP_CONSTRUCTOR_TEST_H_