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 
16 
17 #include <vector>
18 
24 #include "absl/memory/memory.h"
26 
27 namespace absl {
28 namespace container_internal {
29 namespace {
30 
33 using ::testing::Pointee;
34 using ::testing::UnorderedElementsAre;
35 using ::testing::UnorderedElementsAreArray;
36 
37 template <class T>
38 using Set =
40 
41 using SetTypes =
42  ::testing::Types<Set<int>, Set<std::string>, Set<Enum>, Set<EnumClass>>;
43 
44 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ConstructorTest, SetTypes);
45 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, LookupTest, SetTypes);
46 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, MembersTest, SetTypes);
47 INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ModifiersTest, SetTypes);
48 
49 TEST(FlatHashSet, EmplaceString) {
50  std::vector<std::string> v = {"a", "b"};
51  absl::flat_hash_set<absl::string_view> hs(v.begin(), v.end());
52  EXPECT_THAT(hs, UnorderedElementsAreArray(v));
53 }
54 
55 TEST(FlatHashSet, BitfieldArgument) {
56  union {
57  int n : 1;
58  };
59  n = 0;
61  s.insert(n);
62  s.insert(s.end(), n);
63  s.insert({n});
64  s.erase(n);
65  s.count(n);
66  s.prefetch(n);
67  s.find(n);
68  s.contains(n);
69  s.equal_range(n);
70 }
71 
72 TEST(FlatHashSet, MergeExtractInsert) {
73  struct Hash {
74  size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
75  };
76  struct Eq {
77  bool operator()(const std::unique_ptr<int>& a,
78  const std::unique_ptr<int>& b) const {
79  return *a == *b;
80  }
81  };
83  set1.insert(absl::make_unique<int>(7));
84  set1.insert(absl::make_unique<int>(17));
85 
86  set2.insert(absl::make_unique<int>(7));
87  set2.insert(absl::make_unique<int>(19));
88 
89  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17)));
90  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19)));
91 
92  set1.merge(set2);
93 
94  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19)));
95  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
96 
97  auto node = set1.extract(absl::make_unique<int>(7));
98  EXPECT_TRUE(node);
99  EXPECT_THAT(node.value(), Pointee(7));
100  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19)));
101 
102  auto insert_result = set2.insert(std::move(node));
103  EXPECT_FALSE(node);
104  EXPECT_FALSE(insert_result.inserted);
105  EXPECT_TRUE(insert_result.node);
106  EXPECT_THAT(insert_result.node.value(), Pointee(7));
107  EXPECT_EQ(**insert_result.position, 7);
108  EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
109  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
110 
111  node = set1.extract(absl::make_unique<int>(17));
112  EXPECT_TRUE(node);
113  EXPECT_THAT(node.value(), Pointee(17));
114  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19)));
115 
116  node.value() = absl::make_unique<int>(23);
117 
118  insert_result = set2.insert(std::move(node));
119  EXPECT_FALSE(node);
120  EXPECT_TRUE(insert_result.inserted);
121  EXPECT_FALSE(insert_result.node);
122  EXPECT_EQ(**insert_result.position, 23);
123  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
124 }
125 
126 } // namespace
127 } // namespace container_internal
128 } // namespace absl
int v
Definition: variant_test.cc:81
EnumClass
Definition: hash_test.cc:90
TEST(NotificationTest, SanityTest)
Definition: algorithm.h:29
void merge(raw_hash_set< absl::container_internal::FlatHashSetPolicy< T >, H, E, Allocator > &src)
uint64_t b
Definition: layout_test.cc:50
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: utility.h:219
absl::hash_internal::Hash< T > Hash
Definition: hash.h:213


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