abseil-cpp/absl/container/flat_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/flat_hash_set.h"
16 
17 #include <vector>
18 
19 #include "absl/base/internal/raw_logging.h"
20 #include "absl/container/internal/hash_generator_testing.h"
21 #include "absl/container/internal/unordered_set_constructor_test.h"
22 #include "absl/container/internal/unordered_set_lookup_test.h"
23 #include "absl/container/internal/unordered_set_members_test.h"
24 #include "absl/container/internal/unordered_set_modifiers_test.h"
25 #include "absl/memory/memory.h"
26 #include "absl/strings/string_view.h"
27 
28 namespace absl {
30 namespace container_internal {
31 namespace {
32 
39 
40 // Check that absl::flat_hash_set works in a global constructor.
41 struct BeforeMain {
42  BeforeMain() {
44  x.insert(1);
45  ABSL_RAW_CHECK(!x.contains(0), "x should not contain 0");
46  ABSL_RAW_CHECK(x.contains(1), "x should contain 1");
47  }
48 };
49 const BeforeMain before_main;
50 
51 template <class T>
52 using Set =
54 
55 using SetTypes =
56  ::testing::Types<Set<int>, Set<std::string>, Set<Enum>, Set<EnumClass>>;
57 
58 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ConstructorTest, SetTypes);
59 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, LookupTest, SetTypes);
60 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, MembersTest, SetTypes);
61 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ModifiersTest, SetTypes);
62 
63 TEST(FlatHashSet, EmplaceString) {
64  std::vector<std::string> v = {"a", "b"};
65  absl::flat_hash_set<absl::string_view> hs(v.begin(), v.end());
67 }
68 
69 TEST(FlatHashSet, BitfieldArgument) {
70  union {
71  int n : 1;
72  };
73  n = 0;
75  s.insert(n);
76  s.insert(s.end(), n);
77  s.insert({n});
78  s.erase(n);
79  s.count(n);
80  s.prefetch(n);
81  s.find(n);
82  s.contains(n);
83  s.equal_range(n);
84 }
85 
86 TEST(FlatHashSet, MergeExtractInsert) {
87  struct Hash {
88  size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
89  };
90  struct Eq {
91  bool operator()(const std::unique_ptr<int>& a,
92  const std::unique_ptr<int>& b) const {
93  return *a == *b;
94  }
95  };
97  set1.insert(absl::make_unique<int>(7));
98  set1.insert(absl::make_unique<int>(17));
99 
100  set2.insert(absl::make_unique<int>(7));
101  set2.insert(absl::make_unique<int>(19));
102 
105 
106  set1.merge(set2);
107 
110 
111  auto node = set1.extract(absl::make_unique<int>(7));
112  EXPECT_TRUE(node);
113  EXPECT_THAT(node.value(), Pointee(7));
115 
116  auto insert_result = set2.insert(std::move(node));
117  EXPECT_FALSE(node);
118  EXPECT_FALSE(insert_result.inserted);
119  EXPECT_TRUE(insert_result.node);
120  EXPECT_THAT(insert_result.node.value(), Pointee(7));
121  EXPECT_EQ(**insert_result.position, 7);
122  EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
124 
125  node = set1.extract(absl::make_unique<int>(17));
126  EXPECT_TRUE(node);
127  EXPECT_THAT(node.value(), Pointee(17));
129 
130  node.value() = absl::make_unique<int>(23);
131 
132  insert_result = set2.insert(std::move(node));
133  EXPECT_FALSE(node);
134  EXPECT_TRUE(insert_result.inserted);
135  EXPECT_FALSE(insert_result.node);
136  EXPECT_EQ(**insert_result.position, 23);
138 }
139 
140 bool IsEven(int k) { return k % 2 == 0; }
141 
143  // Erase all elements.
144  {
145  flat_hash_set<int> s = {1, 2, 3, 4, 5};
146  EXPECT_EQ(erase_if(s, [](int) { return true; }), 5);
147  EXPECT_THAT(s, IsEmpty());
148  }
149  // Erase no elements.
150  {
151  flat_hash_set<int> s = {1, 2, 3, 4, 5};
152  EXPECT_EQ(erase_if(s, [](int) { return false; }), 0);
153  EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
154  }
155  // Erase specific elements.
156  {
157  flat_hash_set<int> s = {1, 2, 3, 4, 5};
158  EXPECT_EQ(erase_if(s, [](int k) { return k % 2 == 1; }), 3);
160  }
161  // Predicate is function reference.
162  {
163  flat_hash_set<int> s = {1, 2, 3, 4, 5};
164  EXPECT_EQ(erase_if(s, IsEven), 2);
165  EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
166  }
167  // Predicate is function pointer.
168  {
169  flat_hash_set<int> s = {1, 2, 3, 4, 5};
170  EXPECT_EQ(erase_if(s, &IsEven), 2);
171  EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
172  }
173 }
174 
175 } // namespace
176 } // namespace container_internal
178 } // namespace absl
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
ABSL_RAW_CHECK
#define ABSL_RAW_CHECK(condition, message)
Definition: abseil-cpp/absl/base/internal/raw_logging.h:59
absl::container_internal::raw_hash_set< absl::container_internal::FlatHashSetPolicy< 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)
absl::container_internal::raw_hash_set< absl::container_internal::FlatHashSetPolicy< 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
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
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
testing::Eq
internal::EqMatcher< T > Eq(T x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8561
absl::flat_hash_set
Definition: abseil-cpp/absl/container/flat_hash_set.h:105
FlatHashSet
absl::flat_hash_set< T > FlatHashSet(size_t count)
Definition: abseil-cpp/absl/hash/hash_benchmark.cc:141
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
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
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::container_internal::IsEmpty
bool IsEmpty(ctrl_t c)
Definition: abseil-cpp/absl/container/internal/raw_hash_set.h:489
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
testing::UnorderedElementsAreArray
internal::UnorderedElementsAreArrayMatcher< typename ::std::iterator_traits< Iter >::value_type > UnorderedElementsAreArray(Iter first, Iter last)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8507
absl::container_internal::raw_hash_set< absl::container_internal::FlatHashSetPolicy< 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::FlatHashSetPolicy< 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:58:24