unordered_set_constructor_test.h
Go to the documentation of this file.
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 ABSL_CONTAINER_INTERNAL_UNORDERED_SET_CONSTRUCTOR_TEST_H_
16 #define ABSL_CONTAINER_INTERNAL_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"
26 #include "absl/meta/type_traits.h"
27 
28 namespace absl {
29 namespace container_internal {
30 
31 template <class UnordMap>
32 class ConstructorTest : public ::testing::Test {};
33 
34 TYPED_TEST_SUITE_P(ConstructorTest);
35 
36 TYPED_TEST_P(ConstructorTest, NoArgs) {
37  TypeParam m;
38  EXPECT_TRUE(m.empty());
39  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
40 }
41 
42 TYPED_TEST_P(ConstructorTest, BucketCount) {
43  TypeParam m(123);
44  EXPECT_TRUE(m.empty());
45  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
46  EXPECT_GE(m.bucket_count(), 123);
47 }
48 
49 TYPED_TEST_P(ConstructorTest, BucketCountHash) {
50  using H = typename TypeParam::hasher;
51  H hasher;
52  TypeParam m(123, hasher);
53  EXPECT_EQ(m.hash_function(), hasher);
54  EXPECT_TRUE(m.empty());
55  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
56  EXPECT_GE(m.bucket_count(), 123);
57 }
58 
59 TYPED_TEST_P(ConstructorTest, BucketCountHashEqual) {
60  using H = typename TypeParam::hasher;
61  using E = typename TypeParam::key_equal;
62  H hasher;
63  E equal;
64  TypeParam m(123, hasher, equal);
65  EXPECT_EQ(m.hash_function(), hasher);
66  EXPECT_EQ(m.key_eq(), equal);
67  EXPECT_TRUE(m.empty());
68  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
69  EXPECT_GE(m.bucket_count(), 123);
70 }
71 
72 TYPED_TEST_P(ConstructorTest, BucketCountHashEqualAlloc) {
73  using H = typename TypeParam::hasher;
74  using E = typename TypeParam::key_equal;
75  using A = typename TypeParam::allocator_type;
76  H hasher;
77  E equal;
78  A alloc(0);
79  TypeParam m(123, hasher, equal, alloc);
80  EXPECT_EQ(m.hash_function(), hasher);
81  EXPECT_EQ(m.key_eq(), equal);
82  EXPECT_EQ(m.get_allocator(), alloc);
83  EXPECT_TRUE(m.empty());
84  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
85  EXPECT_GE(m.bucket_count(), 123);
86 
87  const auto& cm = m;
88  EXPECT_EQ(cm.hash_function(), hasher);
89  EXPECT_EQ(cm.key_eq(), equal);
90  EXPECT_EQ(cm.get_allocator(), alloc);
91  EXPECT_TRUE(cm.empty());
92  EXPECT_THAT(keys(cm), ::testing::UnorderedElementsAre());
93  EXPECT_GE(cm.bucket_count(), 123);
94 }
95 
96 template <typename T>
97 struct is_std_unordered_set : std::false_type {};
98 
99 template <typename... T>
100 struct is_std_unordered_set<std::unordered_set<T...>> : std::true_type {};
101 
102 #if defined(UNORDERED_SET_CXX14) || defined(UNORDERED_SET_CXX17)
103 using has_cxx14_std_apis = std::true_type;
104 #else
105 using has_cxx14_std_apis = std::false_type;
106 #endif
107 
108 template <typename T>
109 using expect_cxx14_apis =
112 
113 template <typename TypeParam>
114 void BucketCountAllocTest(std::false_type) {}
115 
116 template <typename TypeParam>
117 void BucketCountAllocTest(std::true_type) {
118  using A = typename TypeParam::allocator_type;
119  A alloc(0);
120  TypeParam m(123, alloc);
121  EXPECT_EQ(m.get_allocator(), alloc);
122  EXPECT_TRUE(m.empty());
123  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
124  EXPECT_GE(m.bucket_count(), 123);
125 }
126 
127 TYPED_TEST_P(ConstructorTest, BucketCountAlloc) {
128  BucketCountAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
129 }
130 
131 template <typename TypeParam>
132 void BucketCountHashAllocTest(std::false_type) {}
133 
134 template <typename TypeParam>
135 void BucketCountHashAllocTest(std::true_type) {
136  using H = typename TypeParam::hasher;
137  using A = typename TypeParam::allocator_type;
138  H hasher;
139  A alloc(0);
140  TypeParam m(123, hasher, alloc);
141  EXPECT_EQ(m.hash_function(), hasher);
142  EXPECT_EQ(m.get_allocator(), alloc);
143  EXPECT_TRUE(m.empty());
144  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
145  EXPECT_GE(m.bucket_count(), 123);
146 }
147 
148 TYPED_TEST_P(ConstructorTest, BucketCountHashAlloc) {
149  BucketCountHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
150 }
151 
152 #if ABSL_UNORDERED_SUPPORTS_ALLOC_CTORS
153 using has_alloc_std_constructors = std::true_type;
154 #else
155 using has_alloc_std_constructors = std::false_type;
156 #endif
157 
158 template <typename T>
162 
163 template <typename TypeParam>
164 void AllocTest(std::false_type) {}
165 
166 template <typename TypeParam>
167 void AllocTest(std::true_type) {
168  using A = typename TypeParam::allocator_type;
169  A alloc(0);
170  TypeParam m(alloc);
171  EXPECT_EQ(m.get_allocator(), alloc);
172  EXPECT_TRUE(m.empty());
173  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
174 }
175 
177  AllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
178 }
179 
180 TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashEqualAlloc) {
182  using H = typename TypeParam::hasher;
183  using E = typename TypeParam::key_equal;
184  using A = typename TypeParam::allocator_type;
185  H hasher;
186  E equal;
187  A alloc(0);
188  std::vector<T> values;
189  for (size_t i = 0; i != 10; ++i)
190  values.push_back(hash_internal::Generator<T>()());
191  TypeParam m(values.begin(), values.end(), 123, hasher, equal, alloc);
192  EXPECT_EQ(m.hash_function(), hasher);
193  EXPECT_EQ(m.key_eq(), equal);
194  EXPECT_EQ(m.get_allocator(), alloc);
195  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
196  EXPECT_GE(m.bucket_count(), 123);
197 }
198 
199 template <typename TypeParam>
200 void InputIteratorBucketAllocTest(std::false_type) {}
201 
202 template <typename TypeParam>
203 void InputIteratorBucketAllocTest(std::true_type) {
205  using A = typename TypeParam::allocator_type;
206  A alloc(0);
207  std::vector<T> values;
208  for (size_t i = 0; i != 10; ++i)
209  values.push_back(hash_internal::Generator<T>()());
210  TypeParam m(values.begin(), values.end(), 123, alloc);
211  EXPECT_EQ(m.get_allocator(), alloc);
212  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
213  EXPECT_GE(m.bucket_count(), 123);
214 }
215 
216 TYPED_TEST_P(ConstructorTest, InputIteratorBucketAlloc) {
217  InputIteratorBucketAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
218 }
219 
220 template <typename TypeParam>
221 void InputIteratorBucketHashAllocTest(std::false_type) {}
222 
223 template <typename TypeParam>
224 void InputIteratorBucketHashAllocTest(std::true_type) {
226  using H = typename TypeParam::hasher;
227  using A = typename TypeParam::allocator_type;
228  H hasher;
229  A alloc(0);
230  std::vector<T> values;
231  for (size_t i = 0; i != 10; ++i)
232  values.push_back(hash_internal::Generator<T>()());
233  TypeParam m(values.begin(), values.end(), 123, hasher, alloc);
234  EXPECT_EQ(m.hash_function(), hasher);
235  EXPECT_EQ(m.get_allocator(), alloc);
236  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
237  EXPECT_GE(m.bucket_count(), 123);
238 }
239 
240 TYPED_TEST_P(ConstructorTest, InputIteratorBucketHashAlloc) {
241  InputIteratorBucketHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
242 }
243 
244 TYPED_TEST_P(ConstructorTest, CopyConstructor) {
246  using H = typename TypeParam::hasher;
247  using E = typename TypeParam::key_equal;
248  using A = typename TypeParam::allocator_type;
249  H hasher;
250  E equal;
251  A alloc(0);
252  TypeParam m(123, hasher, equal, alloc);
253  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
254  TypeParam n(m);
255  EXPECT_EQ(m.hash_function(), n.hash_function());
256  EXPECT_EQ(m.key_eq(), n.key_eq());
257  EXPECT_EQ(m.get_allocator(), n.get_allocator());
258  EXPECT_EQ(m, n);
259  EXPECT_NE(TypeParam(0, hasher, equal, alloc), n);
260 }
261 
262 template <typename TypeParam>
263 void CopyConstructorAllocTest(std::false_type) {}
264 
265 template <typename TypeParam>
266 void CopyConstructorAllocTest(std::true_type) {
268  using H = typename TypeParam::hasher;
269  using E = typename TypeParam::key_equal;
270  using A = typename TypeParam::allocator_type;
271  H hasher;
272  E equal;
273  A alloc(0);
274  TypeParam m(123, hasher, equal, alloc);
275  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
276  TypeParam n(m, A(11));
277  EXPECT_EQ(m.hash_function(), n.hash_function());
278  EXPECT_EQ(m.key_eq(), n.key_eq());
279  EXPECT_NE(m.get_allocator(), n.get_allocator());
280  EXPECT_EQ(m, n);
281 }
282 
283 TYPED_TEST_P(ConstructorTest, CopyConstructorAlloc) {
284  CopyConstructorAllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
285 }
286 
287 // TODO(alkis): Test non-propagating allocators on copy constructors.
288 
289 TYPED_TEST_P(ConstructorTest, MoveConstructor) {
291  using H = typename TypeParam::hasher;
292  using E = typename TypeParam::key_equal;
293  using A = typename TypeParam::allocator_type;
294  H hasher;
295  E equal;
296  A alloc(0);
297  TypeParam m(123, hasher, equal, alloc);
298  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
299  TypeParam t(m);
300  TypeParam n(std::move(t));
301  EXPECT_EQ(m.hash_function(), n.hash_function());
302  EXPECT_EQ(m.key_eq(), n.key_eq());
303  EXPECT_EQ(m.get_allocator(), n.get_allocator());
304  EXPECT_EQ(m, n);
305 }
306 
307 template <typename TypeParam>
308 void MoveConstructorAllocTest(std::false_type) {}
309 
310 template <typename TypeParam>
311 void MoveConstructorAllocTest(std::true_type) {
313  using H = typename TypeParam::hasher;
314  using E = typename TypeParam::key_equal;
315  using A = typename TypeParam::allocator_type;
316  H hasher;
317  E equal;
318  A alloc(0);
319  TypeParam m(123, hasher, equal, alloc);
320  for (size_t i = 0; i != 10; ++i) m.insert(hash_internal::Generator<T>()());
321  TypeParam t(m);
322  TypeParam n(std::move(t), A(1));
323  EXPECT_EQ(m.hash_function(), n.hash_function());
324  EXPECT_EQ(m.key_eq(), n.key_eq());
325  EXPECT_NE(m.get_allocator(), n.get_allocator());
326  EXPECT_EQ(m, n);
327 }
328 
329 TYPED_TEST_P(ConstructorTest, MoveConstructorAlloc) {
330  MoveConstructorAllocTest<TypeParam>(expect_alloc_constructors<TypeParam>());
331 }
332 
333 // TODO(alkis): Test non-propagating allocators on move constructors.
334 
335 TYPED_TEST_P(ConstructorTest, InitializerListBucketHashEqualAlloc) {
338  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
339  using H = typename TypeParam::hasher;
340  using E = typename TypeParam::key_equal;
341  using A = typename TypeParam::allocator_type;
342  H hasher;
343  E equal;
344  A alloc(0);
345  TypeParam m(values, 123, hasher, equal, alloc);
346  EXPECT_EQ(m.hash_function(), hasher);
347  EXPECT_EQ(m.key_eq(), equal);
348  EXPECT_EQ(m.get_allocator(), alloc);
349  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
350  EXPECT_GE(m.bucket_count(), 123);
351 }
352 
353 template <typename TypeParam>
354 void InitializerListBucketAllocTest(std::false_type) {}
355 
356 template <typename TypeParam>
357 void InitializerListBucketAllocTest(std::true_type) {
359  using A = typename TypeParam::allocator_type;
361  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
362  A alloc(0);
363  TypeParam m(values, 123, alloc);
364  EXPECT_EQ(m.get_allocator(), alloc);
365  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
366  EXPECT_GE(m.bucket_count(), 123);
367 }
368 
369 TYPED_TEST_P(ConstructorTest, InitializerListBucketAlloc) {
370  InitializerListBucketAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
371 }
372 
373 template <typename TypeParam>
374 void InitializerListBucketHashAllocTest(std::false_type) {}
375 
376 template <typename TypeParam>
377 void InitializerListBucketHashAllocTest(std::true_type) {
379  using H = typename TypeParam::hasher;
380  using A = typename TypeParam::allocator_type;
381  H hasher;
382  A alloc(0);
384  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
385  TypeParam m(values, 123, hasher, alloc);
386  EXPECT_EQ(m.hash_function(), hasher);
387  EXPECT_EQ(m.get_allocator(), alloc);
388  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
389  EXPECT_GE(m.bucket_count(), 123);
390 }
391 
392 TYPED_TEST_P(ConstructorTest, InitializerListBucketHashAlloc) {
393  InitializerListBucketHashAllocTest<TypeParam>(expect_cxx14_apis<TypeParam>());
394 }
395 
396 TYPED_TEST_P(ConstructorTest, CopyAssignment) {
398  using H = typename TypeParam::hasher;
399  using E = typename TypeParam::key_equal;
400  using A = typename TypeParam::allocator_type;
401  H hasher;
402  E equal;
403  A alloc(0);
405  TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
406  TypeParam n;
407  n = m;
408  EXPECT_EQ(m.hash_function(), n.hash_function());
409  EXPECT_EQ(m.key_eq(), n.key_eq());
410  EXPECT_EQ(m, n);
411 }
412 
413 // TODO(alkis): Test [non-]propagating allocators on move/copy assignments
414 // (it depends on traits).
415 
416 TYPED_TEST_P(ConstructorTest, MoveAssignment) {
418  using H = typename TypeParam::hasher;
419  using E = typename TypeParam::key_equal;
420  using A = typename TypeParam::allocator_type;
421  H hasher;
422  E equal;
423  A alloc(0);
425  TypeParam m({gen(), gen(), gen()}, 123, hasher, equal, alloc);
426  TypeParam t(m);
427  TypeParam n;
428  n = std::move(t);
429  EXPECT_EQ(m.hash_function(), n.hash_function());
430  EXPECT_EQ(m.key_eq(), n.key_eq());
431  EXPECT_EQ(m, n);
432 }
433 
434 TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerList) {
437  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
438  TypeParam m;
439  m = values;
440  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
441 }
442 
443 TYPED_TEST_P(ConstructorTest, AssignmentOverwritesExisting) {
446  TypeParam m({gen(), gen(), gen()});
447  TypeParam n({gen()});
448  n = m;
449  EXPECT_EQ(m, n);
450 }
451 
452 TYPED_TEST_P(ConstructorTest, MoveAssignmentOverwritesExisting) {
455  TypeParam m({gen(), gen(), gen()});
456  TypeParam t(m);
457  TypeParam n({gen()});
458  n = std::move(t);
459  EXPECT_EQ(m, n);
460 }
461 
462 TYPED_TEST_P(ConstructorTest, AssignmentFromInitializerListOverwritesExisting) {
465  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
466  TypeParam m;
467  m = values;
468  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
469 }
470 
471 TYPED_TEST_P(ConstructorTest, AssignmentOnSelf) {
474  std::initializer_list<T> values = {gen(), gen(), gen(), gen(), gen()};
475  TypeParam m(values);
476  m = *&m; // Avoid -Wself-assign.
477  EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
478 }
479 
481  ConstructorTest, NoArgs, BucketCount, BucketCountHash, BucketCountHashEqual,
482  BucketCountHashEqualAlloc, BucketCountAlloc, BucketCountHashAlloc, Alloc,
483  InputIteratorBucketHashEqualAlloc, InputIteratorBucketAlloc,
484  InputIteratorBucketHashAlloc, CopyConstructor, CopyConstructorAlloc,
485  MoveConstructor, MoveConstructorAlloc, InitializerListBucketHashEqualAlloc,
486  InitializerListBucketAlloc, InitializerListBucketHashAlloc, CopyAssignment,
487  MoveAssignment, AssignmentFromInitializerList, AssignmentOverwritesExisting,
488  MoveAssignmentOverwritesExisting,
489  AssignmentFromInitializerListOverwritesExisting, AssignmentOnSelf);
490 
491 } // namespace container_internal
492 } // namespace absl
493 
494 #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_CONSTRUCTOR_TEST_H_
void InitializerListBucketHashAllocTest(std::false_type)
auto keys(const Set &s) -> std::vector< typename std::decay< typename Set::key_type >::type >
void MoveConstructorAllocTest(std::false_type)
void InputIteratorBucketHashAllocTest(std::false_type)
TYPED_TEST_SUITE_P(ConstructorTest)
void InitializerListBucketAllocTest(std::false_type)
Definition: algorithm.h:29
hash_default_hash< T > hasher
bool equal(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2, Pred &&pred)
Definition: algorithm.h:99
void InputIteratorBucketAllocTest(std::false_type)
TYPED_TEST_P(ConstructorTest, NoArgs)
decltype(std::declval< const Generator< typename std::conditional< generator_internal::IsMap< Container >::value, typename Container::value_type, typename Container::key_type >::type > & >()()) GeneratedType
std::allocator< int > alloc
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: utility.h:219
void CopyConstructorAllocTest(std::false_type)
REGISTER_TYPED_TEST_CASE_P(ConstructorTest, NoArgs, BucketCount, BucketCountHash, BucketCountHashEqual, BucketCountHashEqualAlloc, BucketCountAlloc, BucketCountHashAlloc, Alloc, InputIteratorBucketHashEqualAlloc, InputIteratorBucketAlloc, InputIteratorBucketHashAlloc, CopyConstructor, CopyConstructorAlloc, MoveConstructor, MoveConstructorAlloc, InitializerListBucketHashEqualAlloc, InitializerListBucketAlloc, InitializerListBucketHashAlloc, Assignment, MoveAssignment, AssignmentFromInitializerList, AssignmentOverwritesExisting, MoveAssignmentOverwritesExisting, AssignmentFromInitializerListOverwritesExisting, AssignmentOnSelf)
void BucketCountHashAllocTest(std::false_type)


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:37