Debian Codegraph @

tests/unordered_set_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_SET_CONSTRUCTOR_TEST_H_
16 #define PHMAP_PRIV_UNORDERED_SET_CONSTRUCTOR_TEST_H_
17
18 #include <algorithm>
19 #include <unordered_set>
20 #include <vector>
21
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "hash_generator_testing.h"
25 #include "hash_policy_testing.h"
26
27 namespace phmap {
28 namespace priv {
29
30 template <class UnordMap>
31 class ConstructorTest : public ::testing::Test {};
32
33 TYPED_TEST_SUITE_P(ConstructorTest);
34
35 TYPED_TEST_P(ConstructorTest, NoArgs) {
36 TypeParam m;
37 EXPECT_TRUE(m.empty());
38 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
39 }
40
41 TYPED_TEST_P(ConstructorTest, BucketCount) {
42 TypeParam m(123);
43 EXPECT_TRUE(m.empty());
44 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
45 EXPECT_GE(m.bucket_count(), 123);
46 }
47
48 TYPED_TEST_P(ConstructorTest, BucketCountHash) {
49 using H = typename TypeParam::hasher;
50 H hasher;
51 TypeParam m(123, hasher);
52 EXPECT_EQ(m.hash_function(), hasher);
53 EXPECT_TRUE(m.empty());
54 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
55 EXPECT_GE(m.bucket_count(), 123);
56 }
57
58 TYPED_TEST_P(ConstructorTest, BucketCountHashEqual) {
59 using H = typename TypeParam::hasher;
60 using E = typename TypeParam::key_equal;
61 H hasher;
62 E equal;
63 TypeParam m(123, hasher, equal);
64 EXPECT_EQ(m.hash_function(), hasher);
65 EXPECT_EQ(m.key_eq(), equal);
66 EXPECT_TRUE(m.empty());
67 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
68 EXPECT_GE(m.bucket_count(), 123);
69 }
70
71 TYPED_TEST_P(ConstructorTest, BucketCountHashEqualAlloc) {
72 using H = typename TypeParam::hasher;
73 using E = typename TypeParam::key_equal;
74 using A = typename TypeParam::allocator_type;
75 H hasher;
76 E equal;
77 A alloc(0);
78 TypeParam m(123, hasher, equal, alloc);
79 EXPECT_EQ(m.hash_function(), hasher);
80 EXPECT_EQ(m.key_eq(), equal);
81 EXPECT_EQ(m.get_allocator(), alloc);
82 EXPECT_TRUE(m.empty());
83 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
84 EXPECT_GE(m.bucket_count(), 123);
85
86 const auto& cm = m;
87 EXPECT_EQ(cm.hash_function(), hasher);
88 EXPECT_EQ(cm.key_eq(), equal);
89 EXPECT_EQ(cm.get_allocator(), alloc);
90 EXPECT_TRUE(cm.empty());
91 EXPECT_THAT(keys(cm), ::testing::UnorderedElementsAre());
92 EXPECT_GE(cm.bucket_count(), 123);
93 }
94
95 template <typename T>
96 struct is_std_unordered_set : std::false_type {};
97
98 template <typename... T>
99 struct is_std_unordered_set<std::unordered_set<T...>> : std::true_type {};
100
101 #if defined(UNORDERED_SET_CXX14) || defined(UNORDERED_SET_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_set<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(keys(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(keys(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_set<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(keys(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 for (size_t i = 0; i != 10; ++i)
189 values.push_back(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(keys(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 for (size_t i = 0; i != 10; ++i)
208 values.push_back(hash_internal::Generator<T>()());
209 TypeParam m(values.begin(), values.end(), 123, alloc);
210 EXPECT_EQ(m.get_allocator(), alloc);
211 EXPECT_THAT(keys(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 for (size_t i = 0; i != 10; ++i)
231 values.push_back(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(keys(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 EXPECT_NE(TypeParam(0, hasher, equal, alloc), n);
259 }
260
261 template <typename TypeParam>
262 void CopyConstructorAllocTest(std::false_type) {}
263
264 template <typename TypeParam>
265 void CopyConstructorAllocTest(std::true_type) {
266 using T = hash_internal::GeneratedType<TypeParam>;
267 using H = typename TypeParam::hasher;
268 using E = typename TypeParam::key_equal;
269 using A = typename TypeParam::allocator_type;
270 H hasher;
271 E equal;
272 A alloc(0);
273 TypeParam m(123, hasher, equal, alloc);
274 for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
275 TypeParam n(m, A(11));
276 EXPECT_EQ(m.hash_function(), n.hash_function());
277 EXPECT_EQ(m.key_eq(), n.key_eq());
278 EXPECT_NE(m.get_allocator(), n.get_allocator());
279 EXPECT_EQ(m, n);
280 }
281
282 TYPED_TEST_P(ConstructorTest, CopyConstructorAlloc) {
283 CopyConstructorAllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
284 }
285
286 // TODO(alkis): Test non-propagating allocators on copy constructors.
287
288 TYPED_TEST_P(ConstructorTest, MoveConstructor) {
289 using T = hash_internal::GeneratedType<TypeParam>;
290 using H = typename TypeParam::hasher;
291 using E = typename TypeParam::key_equal;
292 using A = typename TypeParam::allocator_type;
293 H hasher;
294 E equal;
295 A alloc(0);
296 TypeParam m(123, hasher, equal, alloc);
297 for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
298 TypeParam t(m);
299 TypeParam n(std::move(t));
300 EXPECT_EQ(m.hash_function(), n.hash_function());
301 EXPECT_EQ(m.key_eq(), n.key_eq());
302 EXPECT_EQ(m.get_allocator(), n.get_allocator());
303 EXPECT_EQ(m, n);
304 }
305
306 template <typename TypeParam>
307 void MoveConstructorAllocTest(std::false_type) {}
308
309 template <typename TypeParam>
310 void MoveConstructorAllocTest(std::true_type) {
311 using T = hash_internal::GeneratedType<TypeParam>;
312 using H = typename TypeParam::hasher;
313 using E = typename TypeParam::key_equal;
314 using A = typename TypeParam::allocator_type;
315 H hasher;
316 E equal;
317 A alloc(0);
318 TypeParam m(123, hasher, equal, alloc);
319 for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
320 TypeParam t(m);
321 TypeParam n(std::move(t), A(1));
322 EXPECT_EQ(m.hash_function(), n.hash_function());
323 EXPECT_EQ(m.key_eq(), n.key_eq());
324 EXPECT_NE(m.get_allocator(), n.get_allocator());
325 EXPECT_EQ(m, n);
326 }
327
328 TYPED_TEST_P(ConstructorTest, MoveConstructorAlloc) {
329 MoveConstructorAllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
330 }
331
332 // TODO(alkis): Test non-propagating allocators on move constructors.
333
334 TYPED_TEST_P(ConstructorTest, InitializerListBucketHashEqualAlloc) {
335 using T = hash_internal::GeneratedType<TypeParam>;
336 hash_internal::Generator<T> gen;
337 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
338 using H = typename TypeParam::hasher;
339 using E = typename TypeParam::key_equal;
340 using A = typename TypeParam::allocator_type;
341 H hasher;
342 E equal;
343 A alloc(0);
344 TypeParam m(values, 123, hasher, equal, alloc);
345 EXPECT_EQ(m.hash_function(), hasher);
346 EXPECT_EQ(m.key_eq(), equal);
347 EXPECT_EQ(m.get_allocator(), alloc);
348 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
349 EXPECT_GE(m.bucket_count(), 123);
350 }
351
352 template <typename TypeParam>
353 void InitializerListBucketAllocTest(std::false_type) {}
354
355 template <typename TypeParam>
356 void InitializerListBucketAllocTest(std::true_type) {
357 using T = hash_internal::GeneratedType<TypeParam>;
358 using A = typename TypeParam::allocator_type;
359 hash_internal::Generator<T> gen;
360 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
361 A alloc(0);
362 TypeParam m(values, 123, alloc);
363 EXPECT_EQ(m.get_allocator(), alloc);
364 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
365 EXPECT_GE(m.bucket_count(), 123);
366 }
367
368 TYPED_TEST_P(ConstructorTest, InitializerListBucketAlloc) {
369 InitializerListBucketAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
370 }
371
372 template <typename TypeParam>
373 void InitializerListBucketHashAllocTest(std::false_type) {}
374
375 template <typename TypeParam>
376 void InitializerListBucketHashAllocTest(std::true_type) {
377 using T = hash_internal::GeneratedType<TypeParam>;
378 using H = typename TypeParam::hasher;
379 using A = typename TypeParam::allocator_type;
380 H hasher;
381 A alloc(0);
382 hash_internal::Generator<T> gen;
383 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
384 TypeParam m(values, 123, hasher, alloc);
385 EXPECT_EQ(m.hash_function(), hasher);
386 EXPECT_EQ(m.get_allocator(), alloc);
387 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
388 EXPECT_GE(m.bucket_count(), 123);
389 }
390
391 TYPED_TEST_P(ConstructorTest, InitializerListBucketHashAlloc) {
392 InitializerListBucketHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
393 }
394
395 TYPED_TEST_P(ConstructorTest, CopyAssignment) {
396 using T = hash_internal::GeneratedType<TypeParam>;
397 using H = typename TypeParam::hasher;
398 using E = typename TypeParam::key_equal;
399 using A = typename TypeParam::allocator_type;
400 H hasher;
401 E equal;
402 A alloc(0);
403 hash_internal::Generator<T> gen;
404 TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
405 TypeParam n;
406 n = m;
407 EXPECT_EQ(m.hash_function(), n.hash_function());
408 EXPECT_EQ(m.key_eq(), n.key_eq());
409 EXPECT_EQ(m, n);
410 }
411
412 // TODO(alkis): Test [non-]propagating allocators on move/copy assignments
413 // (it depends on traits).
414
415 TYPED_TEST_P(ConstructorTest, MoveAssignment) {
416 using T = hash_internal::GeneratedType<TypeParam>;
417 using H = typename TypeParam::hasher;
418 using E = typename TypeParam::key_equal;
419 using A = typename TypeParam::allocator_type;
420 H hasher;
421 E equal;
422 A alloc(0);
423 hash_internal::Generator<T> gen;
424 TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
425 TypeParam t(m);
426 TypeParam n;
427 n = std::move(t);
428 EXPECT_EQ(m.hash_function(), n.hash_function());
429 EXPECT_EQ(m.key_eq(), n.key_eq());
430 EXPECT_EQ(m, n);
431 }
432
433 TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerList) {
434 using T = hash_internal::GeneratedType<TypeParam>;
435 hash_internal::Generator<T> gen;
436 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
437 TypeParam m;
438 m = values;
439 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
440 }
441
442 TYPED_TEST_P(ConstructorTest, AssignmentOverwritesExisting) {
443 using T = hash_internal::GeneratedType<TypeParam>;
444 hash_internal::Generator<T> gen;
445 TypeParam m({gen(), gen(), gen()});
446 TypeParam n({gen()});
447 n = m;
448 EXPECT_EQ(m, n);
449 }
450
451 TYPED_TEST_P(ConstructorTest, MoveAssignmentOverwritesExisting) {
452 using T = hash_internal::GeneratedType<TypeParam>;
453 hash_internal::Generator<T> gen;
454 TypeParam m({gen(), gen(), gen()});
455 TypeParam t(m);
456 TypeParam n({gen()});
457 n = std::move(t);
458 EXPECT_EQ(m, n);
459 }
460
461 TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerListOverwritesExisting) {
462 using T = hash_internal::GeneratedType<TypeParam>;
463 hash_internal::Generator<T> gen;
464 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
465 TypeParam m;
466 m = values;
467 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
468 }
469
470 TYPED_TEST_P(ConstructorTest, AssignmentOnSelf) {
471 using T = hash_internal::GeneratedType<TypeParam>;
472 hash_internal::Generator<T> gen;
473 std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
474 TypeParam m(values);
475 m = *&m; // Avoid -Wself-assign.
476 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
477 }
478
479 REGISTER_TYPED_TEST_SUITE_P(
480 ConstructorTest, NoArgs, BucketCount, BucketCountHash, BucketCountHashEqual,
481 BucketCountHashEqualAlloc, BucketCountAlloc, BucketCountHashAlloc, Alloc,
482 InputIteratorBucketHashEqualAlloc, InputIteratorBucketAlloc,
483 InputIteratorBucketHashAlloc, CopyConstructor, CopyConstructorAlloc,
484 MoveConstructor, MoveConstructorAlloc, InitializerListBucketHashEqualAlloc,
485 InitializerListBucketAlloc, InitializerListBucketHashAlloc, CopyAssignment,
486 MoveAssignment, AssignmentFromInitializerList, AssignmentOverwritesExisting,
487 MoveAssignmentOverwritesExisting,
488 AssignmentFromInitializerListOverwritesExisting, AssignmentOnSelf);
489
490 } // namespace priv
491 } // namespace phmap
492
493 #endif // PHMAP_PRIV_UNORDERED_SET_CONSTRUCTOR_TEST_H_