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 
16 
21 
22 namespace absl {
23 namespace container_internal {
24 namespace {
27 using ::testing::Pointee;
28 using ::testing::UnorderedElementsAre;
29 
30 using SetTypes = ::testing::Types<
31  node_hash_set<int, StatefulTestingHash, StatefulTestingEqual, Alloc<int>>,
32  node_hash_set<std::string, StatefulTestingHash, StatefulTestingEqual,
33  Alloc<std::string>>,
34  node_hash_set<Enum, StatefulTestingHash, StatefulTestingEqual, Alloc<Enum>>,
35  node_hash_set<EnumClass, StatefulTestingHash, StatefulTestingEqual,
36  Alloc<EnumClass>>>;
37 
38 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ConstructorTest, SetTypes);
39 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, LookupTest, SetTypes);
40 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, MembersTest, SetTypes);
41 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ModifiersTest, SetTypes);
42 
43 TEST(NodeHashSet, MoveableNotCopyableCompiles) {
44  node_hash_set<std::unique_ptr<void*>> t;
45  node_hash_set<std::unique_ptr<void*>> u;
46  u = std::move(t);
47 }
48 
49 TEST(NodeHashSet, MergeExtractInsert) {
50  struct Hash {
51  size_t operator()(const std::unique_ptr<int>& p) const { return *p; }
52  };
53  struct Eq {
54  bool operator()(const std::unique_ptr<int>& a,
55  const std::unique_ptr<int>& b) const {
56  return *a == *b;
57  }
58  };
60  set1.insert(absl::make_unique<int>(7));
61  set1.insert(absl::make_unique<int>(17));
62 
63  set2.insert(absl::make_unique<int>(7));
64  set2.insert(absl::make_unique<int>(19));
65 
66  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17)));
67  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(19)));
68 
69  set1.merge(set2);
70 
71  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(7), Pointee(17), Pointee(19)));
72  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
73 
74  auto node = set1.extract(absl::make_unique<int>(7));
75  EXPECT_TRUE(node);
76  EXPECT_THAT(node.value(), Pointee(7));
77  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(17), Pointee(19)));
78 
79  auto insert_result = set2.insert(std::move(node));
80  EXPECT_FALSE(node);
81  EXPECT_FALSE(insert_result.inserted);
82  EXPECT_TRUE(insert_result.node);
83  EXPECT_THAT(insert_result.node.value(), Pointee(7));
84  EXPECT_EQ(**insert_result.position, 7);
85  EXPECT_NE(insert_result.position->get(), insert_result.node.value().get());
86  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7)));
87 
88  node = set1.extract(absl::make_unique<int>(17));
89  EXPECT_TRUE(node);
90  EXPECT_THAT(node.value(), Pointee(17));
91  EXPECT_THAT(set1, UnorderedElementsAre(Pointee(19)));
92 
93  node.value() = absl::make_unique<int>(23);
94 
95  insert_result = set2.insert(std::move(node));
96  EXPECT_FALSE(node);
97  EXPECT_TRUE(insert_result.inserted);
98  EXPECT_FALSE(insert_result.node);
99  EXPECT_EQ(**insert_result.position, 23);
100  EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
101 }
102 
103 } // namespace
104 } // namespace container_internal
105 } // namespace absl
EnumClass
Definition: hash_test.cc:90
TEST(NotificationTest, SanityTest)
Definition: algorithm.h:29
void merge(raw_hash_set< absl::container_internal::NodeHashSetPolicy< T >, H, E, Alloc > &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 Wed Jun 19 2019 19:19:57