abseil-cpp/absl/container/node_hash_set_test.cc
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 #include "absl/container/node_hash_set.h"
16 
17 #include "absl/container/internal/unordered_set_constructor_test.h"
18 #include "absl/container/internal/unordered_set_lookup_test.h"
19 #include "absl/container/internal/unordered_set_members_test.h"
20 #include "absl/container/internal/unordered_set_modifiers_test.h"
21 
22 namespace absl {
24 namespace container_internal {
25 namespace {
31 
32 using SetTypes = ::testing::Types<
33  node_hash_set<int, StatefulTestingHash, StatefulTestingEqual, Alloc<int>>,
34  node_hash_set<std::string, StatefulTestingHash, StatefulTestingEqual,
35  Alloc<std::string>>,
36  node_hash_set<Enum, StatefulTestingHash, StatefulTestingEqual, Alloc<Enum>>,
37  node_hash_set<EnumClass, StatefulTestingHash, StatefulTestingEqual,
38  Alloc<EnumClass>>>;
39 
40 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ConstructorTest, SetTypes);
41 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, LookupTest, SetTypes);
42 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, MembersTest, SetTypes);
43 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ModifiersTest, SetTypes);
44 
45 TEST(NodeHashSet, MoveableNotCopyableCompiles) {
46  node_hash_set<std::unique_ptr<void*>> t;
47  node_hash_set<std::unique_ptr<void*>> u;
48  u = std::move(t);
49 }
50 
51 TEST(NodeHashSet, MergeExtractInsert) {
52  struct Hash {
53  size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
54  };
55  struct Eq {
56  bool operator()(const std::unique_ptr<int>& a,
57  const std::unique_ptr<int>& b) const {
58  return *a == *b;
59  }
60  };
62  set1.insert(absl::make_unique<int>(7));
63  set1.insert(absl::make_unique<int>(17));
64 
65  set2.insert(absl::make_unique<int>(7));
66  set2.insert(absl::make_unique<int>(19));
67 
70 
71  set1.merge(set2);
72 
75 
76  auto node = set1.extract(absl::make_unique<int>(7));
77  EXPECT_TRUE(node);
78  EXPECT_THAT(node.value(), Pointee(7));
80 
81  auto insert_result = set2.insert(std::move(node));
82  EXPECT_FALSE(node);
83  EXPECT_FALSE(insert_result.inserted);
84  EXPECT_TRUE(insert_result.node);
85  EXPECT_THAT(insert_result.node.value(), Pointee(7));
86  EXPECT_EQ(**insert_result.position, 7);
87  EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
89 
90  node = set1.extract(absl::make_unique<int>(17));
91  EXPECT_TRUE(node);
92  EXPECT_THAT(node.value(), Pointee(17));
94 
95  node.value() = absl::make_unique<int>(23);
96 
97  insert_result = set2.insert(std::move(node));
98  EXPECT_FALSE(node);
99  EXPECT_TRUE(insert_result.inserted);
100  EXPECT_FALSE(insert_result.node);
101  EXPECT_EQ(**insert_result.position, 23);
103 }
104 
105 bool IsEven(int k) { return k % 2 == 0; }
106 
107 TEST(NodeHashSet, EraseIf) {
108  // Erase all elements.
109  {
110  node_hash_set<int> s = {1, 2, 3, 4, 5};
111  EXPECT_EQ(erase_if(s, [](int) { return true; }), 5);
112  EXPECT_THAT(s, IsEmpty());
113  }
114  // Erase no elements.
115  {
116  node_hash_set<int> s = {1, 2, 3, 4, 5};
117  EXPECT_EQ(erase_if(s, [](int) { return false; }), 0);
118  EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
119  }
120  // Erase specific elements.
121  {
122  node_hash_set<int> s = {1, 2, 3, 4, 5};
123  EXPECT_EQ(erase_if(s, [](int k) { return k % 2 == 1; }), 3);
125  }
126  // Predicate is function reference.
127  {
128  node_hash_set<int> s = {1, 2, 3, 4, 5};
129  EXPECT_EQ(erase_if(s, IsEven), 2);
130  EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
131  }
132  // Predicate is function pointer.
133  {
134  node_hash_set<int> s = {1, 2, 3, 4, 5};
135  EXPECT_EQ(erase_if(s, &IsEven), 2);
136  EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
137  }
138 }
139 
140 } // namespace
141 } // namespace container_internal
143 } // namespace absl
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
absl::container_internal::raw_hash_set< absl::container_internal::NodeHashSetPolicy< T >, absl::container_internal::hash_default_hash< T >, absl::container_internal::hash_default_eq< T >, std::allocator< T > >::extract
node_type extract(const_iterator position)
Definition: abseil-cpp/absl/container/internal/raw_hash_set.h:1633
Hash
Hash
Definition: abseil-cpp/absl/container/internal/hash_function_defaults_test.cc:339
testing::Pointee
internal::PointeeMatcher< InnerMatcher > Pointee(const InnerMatcher &inner_matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8691
EXPECT_THAT
#define EXPECT_THAT(value, matcher)
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::container_internal::raw_hash_set< absl::container_internal::NodeHashSetPolicy< T >, absl::container_internal::hash_default_hash< T >, absl::container_internal::hash_default_eq< T >, std::allocator< T > >::insert
std::pair< iterator, bool > insert(T &&value)
Definition: abseil-cpp/absl/container/internal/raw_hash_set.h:1382
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
absl::TEST
TEST(NotificationTest, SanityTest)
Definition: abseil-cpp/absl/synchronization/notification_test.cc:126
absl::FormatConversionChar::s
@ s
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
xds_manager.p
p
Definition: xds_manager.py:60
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::container_internal::hash_internal::EnumClass
EnumClass
Definition: abseil-cpp/absl/container/internal/hash_generator_testing.h:58
setup.k
k
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
Enum
struct Enum Enum
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:642
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
testing::internal::ProxyTypeList
Definition: googletest/googletest/include/gtest/internal/gtest-type-util.h:155
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
testing::Eq
internal::EqMatcher< T > Eq(T x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8561
INSTANTIATE_TYPED_TEST_SUITE_P
#define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types,...)
Definition: googletest/googletest/include/gtest/gtest-typed-test.h:306
absl::erase_if
btree_map< K, V, C, A >::size_type erase_if(btree_map< K, V, C, A > &map, Pred pred)
Definition: abseil-cpp/absl/container/btree_map.h:483
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
absl::container_internal::EraseIf
raw_hash_set< P, H, E, A >::size_type EraseIf(Predicate &pred, raw_hash_set< P, H, E, A > *c)
Definition: abseil-cpp/absl/container/internal/raw_hash_set.h:2287
absl::node_hash_set
Definition: abseil-cpp/absl/container/node_hash_set.h:100
absl::container_internal::IsEmpty
bool IsEmpty(ctrl_t c)
Definition: abseil-cpp/absl/container/internal/raw_hash_set.h:489
absl::str_format_internal::LengthMod::t
@ t
testing::UnorderedElementsAre
internal::UnorderedElementsAreMatcher< ::testing::tuple<> > UnorderedElementsAre()
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:13255
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::container_internal::raw_hash_set< absl::container_internal::NodeHashSetPolicy< T >, absl::container_internal::hash_default_hash< T >, absl::container_internal::hash_default_eq< T >, std::allocator< T > >::merge
void merge(raw_hash_set< absl::container_internal::NodeHashSetPolicy< T >, H, E, std::allocator< T > > &src)
Definition: abseil-cpp/absl/container/internal/raw_hash_set.h:1615
absl::Hash
absl::hash_internal::Hash< T > Hash
Definition: abseil-cpp/absl/hash/hash.h:247


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:32