node_hash_set_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/node_hash_set.h"
  15. #include "absl/container/internal/unordered_set_constructor_test.h"
  16. #include "absl/container/internal/unordered_set_lookup_test.h"
  17. #include "absl/container/internal/unordered_set_members_test.h"
  18. #include "absl/container/internal/unordered_set_modifiers_test.h"
  19. namespace absl {
  20. ABSL_NAMESPACE_BEGIN
  21. namespace container_internal {
  22. namespace {
  23. using ::absl::container_internal::hash_internal::Enum;
  24. using ::absl::container_internal::hash_internal::EnumClass;
  25. using ::testing::IsEmpty;
  26. using ::testing::Pointee;
  27. using ::testing::UnorderedElementsAre;
  28. using SetTypes = ::testing::Types<
  29. node_hash_set<int, StatefulTestingHash, StatefulTestingEqual, Alloc<int>>,
  30. node_hash_set<std::string, StatefulTestingHash, StatefulTestingEqual,
  31. Alloc<std::string>>,
  32. node_hash_set<Enum, StatefulTestingHash, StatefulTestingEqual, Alloc<Enum>>,
  33. node_hash_set<EnumClass, StatefulTestingHash, StatefulTestingEqual,
  34. Alloc<EnumClass>>>;
  35. INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ConstructorTest, SetTypes);
  36. INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, LookupTest, SetTypes);
  37. INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, MembersTest, SetTypes);
  38. INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ModifiersTest, SetTypes);
  39. TEST(NodeHashSet, MoveableNotCopyableCompiles) {
  40. node_hash_set<std::unique_ptr<void*>> t;
  41. node_hash_set<std::unique_ptr<void*>> u;
  42. u = std::move(t);
  43. }
  44. TEST(NodeHashSet, MergeExtractInsert) {
  45. struct Hash {
  46. size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
  47. };
  48. struct Eq {
  49. bool operator()(const std::unique_ptr<int>& a,
  50. const std::unique_ptr<int>& b) const {
  51. return *a == *b;
  52. }
  53. };
  54. absl::node_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
  55. set1.insert(absl::make_unique<int>(7));
  56. set1.insert(absl::make_unique<int>(17));
  57. set2.insert(absl::make_unique<int>(7));
  58. set2.insert(absl::make_unique<int>(19));
  59. EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17)));
  60. EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19)));
  61. set1.merge(set2);
  62. EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19)));
  63. EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
  64. auto node = set1.extract(absl::make_unique<int>(7));
  65. EXPECT_TRUE(node);
  66. EXPECT_THAT(node.value(), Pointee(7));
  67. EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19)));
  68. auto insert_result = set2.insert(std::move(node));
  69. EXPECT_FALSE(node);
  70. EXPECT_FALSE(insert_result.inserted);
  71. EXPECT_TRUE(insert_result.node);
  72. EXPECT_THAT(insert_result.node.value(), Pointee(7));
  73. EXPECT_EQ(**insert_result.position, 7);
  74. EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
  75. EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
  76. node = set1.extract(absl::make_unique<int>(17));
  77. EXPECT_TRUE(node);
  78. EXPECT_THAT(node.value(), Pointee(17));
  79. EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19)));
  80. node.value() = absl::make_unique<int>(23);
  81. insert_result = set2.insert(std::move(node));
  82. EXPECT_FALSE(node);
  83. EXPECT_TRUE(insert_result.inserted);
  84. EXPECT_FALSE(insert_result.node);
  85. EXPECT_EQ(**insert_result.position, 23);
  86. EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
  87. }
  88. bool IsEven(int k) { return k % 2 == 0; }
  89. TEST(NodeHashSet, EraseIf) {
  90. // Erase all elements.
  91. {
  92. node_hash_set<int> s = {1, 2, 3, 4, 5};
  93. erase_if(s, [](int) { return true; });
  94. EXPECT_THAT(s, IsEmpty());
  95. }
  96. // Erase no elements.
  97. {
  98. node_hash_set<int> s = {1, 2, 3, 4, 5};
  99. erase_if(s, [](int) { return false; });
  100. EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
  101. }
  102. // Erase specific elements.
  103. {
  104. node_hash_set<int> s = {1, 2, 3, 4, 5};
  105. erase_if(s, [](int k) { return k % 2 == 1; });
  106. EXPECT_THAT(s, UnorderedElementsAre(2, 4));
  107. }
  108. // Predicate is function reference.
  109. {
  110. node_hash_set<int> s = {1, 2, 3, 4, 5};
  111. erase_if(s, IsEven);
  112. EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
  113. }
  114. // Predicate is function pointer.
  115. {
  116. node_hash_set<int> s = {1, 2, 3, 4, 5};
  117. erase_if(s, &IsEven);
  118. EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
  119. }
  120. }
  121. } // namespace
  122. } // namespace container_internal
  123. ABSL_NAMESPACE_END
  124. } // namespace absl