flat_hash_set_test.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include "absl/container/flat_hash_set.h"
  15. #include <vector>
  16. #include "absl/base/internal/raw_logging.h"
  17. #include "absl/container/internal/hash_generator_testing.h"
  18. #include "absl/container/internal/unordered_set_constructor_test.h"
  19. #include "absl/container/internal/unordered_set_lookup_test.h"
  20. #include "absl/container/internal/unordered_set_members_test.h"
  21. #include "absl/container/internal/unordered_set_modifiers_test.h"
  22. #include "absl/memory/memory.h"
  23. #include "absl/strings/string_view.h"
  24. namespace absl {
  25. ABSL_NAMESPACE_BEGIN
  26. namespace container_internal {
  27. namespace {
  28. using ::absl::container_internal::hash_internal::Enum;
  29. using ::absl::container_internal::hash_internal::EnumClass;
  30. using ::testing::IsEmpty;
  31. using ::testing::Pointee;
  32. using ::testing::UnorderedElementsAre;
  33. using ::testing::UnorderedElementsAreArray;
  34. // Check that absl::flat_hash_set works in a global constructor.
  35. struct BeforeMain {
  36. BeforeMain() {
  37. absl::flat_hash_set<int> x;
  38. x.insert(1);
  39. ABSL_RAW_CHECK(!x.contains(0), "x should not contain 0");
  40. ABSL_RAW_CHECK(x.contains(1), "x should contain 1");
  41. }
  42. };
  43. const BeforeMain before_main;
  44. template <class T>
  45. using Set =
  46. absl::flat_hash_set<T, StatefulTestingHash, StatefulTestingEqual, Alloc<T>>;
  47. using SetTypes =
  48. ::testing::Types<Set<int>, Set<std::string>, Set<Enum>, Set<EnumClass>>;
  49. INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ConstructorTest, SetTypes);
  50. INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, LookupTest, SetTypes);
  51. INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, MembersTest, SetTypes);
  52. INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ModifiersTest, SetTypes);
  53. TEST(FlatHashSet, EmplaceString) {
  54. std::vector<std::string> v = {"a", "b"};
  55. absl::flat_hash_set<absl::string_view> hs(v.begin(), v.end());
  56. EXPECT_THAT(hs, UnorderedElementsAreArray(v));
  57. }
  58. TEST(FlatHashSet, BitfieldArgument) {
  59. union {
  60. int n : 1;
  61. };
  62. n = 0;
  63. absl::flat_hash_set<int> s = {n};
  64. s.insert(n);
  65. s.insert(s.end(), n);
  66. s.insert({n});
  67. s.erase(n);
  68. s.count(n);
  69. s.prefetch(n);
  70. s.find(n);
  71. s.contains(n);
  72. s.equal_range(n);
  73. }
  74. TEST(FlatHashSet, MergeExtractInsert) {
  75. struct Hash {
  76. size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
  77. };
  78. struct Eq {
  79. bool operator()(const std::unique_ptr<int>& a,
  80. const std::unique_ptr<int>& b) const {
  81. return *a == *b;
  82. }
  83. };
  84. absl::flat_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
  85. set1.insert(absl::make_unique<int>(7));
  86. set1.insert(absl::make_unique<int>(17));
  87. set2.insert(absl::make_unique<int>(7));
  88. set2.insert(absl::make_unique<int>(19));
  89. EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17)));
  90. EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19)));
  91. set1.merge(set2);
  92. EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19)));
  93. EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
  94. auto node = set1.extract(absl::make_unique<int>(7));
  95. EXPECT_TRUE(node);
  96. EXPECT_THAT(node.value(), Pointee(7));
  97. EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19)));
  98. auto insert_result = set2.insert(std::move(node));
  99. EXPECT_FALSE(node);
  100. EXPECT_FALSE(insert_result.inserted);
  101. EXPECT_TRUE(insert_result.node);
  102. EXPECT_THAT(insert_result.node.value(), Pointee(7));
  103. EXPECT_EQ(**insert_result.position, 7);
  104. EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
  105. EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
  106. node = set1.extract(absl::make_unique<int>(17));
  107. EXPECT_TRUE(node);
  108. EXPECT_THAT(node.value(), Pointee(17));
  109. EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19)));
  110. node.value() = absl::make_unique<int>(23);
  111. insert_result = set2.insert(std::move(node));
  112. EXPECT_FALSE(node);
  113. EXPECT_TRUE(insert_result.inserted);
  114. EXPECT_FALSE(insert_result.node);
  115. EXPECT_EQ(**insert_result.position, 23);
  116. EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
  117. }
  118. bool IsEven(int k) { return k % 2 == 0; }
  119. TEST(FlatHashSet, EraseIf) {
  120. // Erase all elements.
  121. {
  122. flat_hash_set<int> s = {1, 2, 3, 4, 5};
  123. erase_if(s, [](int) { return true; });
  124. EXPECT_THAT(s, IsEmpty());
  125. }
  126. // Erase no elements.
  127. {
  128. flat_hash_set<int> s = {1, 2, 3, 4, 5};
  129. erase_if(s, [](int) { return false; });
  130. EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
  131. }
  132. // Erase specific elements.
  133. {
  134. flat_hash_set<int> s = {1, 2, 3, 4, 5};
  135. erase_if(s, [](int k) { return k % 2 == 1; });
  136. EXPECT_THAT(s, UnorderedElementsAre(2, 4));
  137. }
  138. // Predicate is function reference.
  139. {
  140. flat_hash_set<int> s = {1, 2, 3, 4, 5};
  141. erase_if(s, IsEven);
  142. EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
  143. }
  144. // Predicate is function pointer.
  145. {
  146. flat_hash_set<int> s = {1, 2, 3, 4, 5};
  147. erase_if(s, &IsEven);
  148. EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
  149. }
  150. }
  151. } // namespace
  152. } // namespace container_internal
  153. ABSL_NAMESPACE_END
  154. } // namespace absl