blob: 0ea76e7c78ec6531bd0929a6915a455266c37a2a [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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
22namespace absl {
23namespace container_internal {
24namespace {
25using ::absl::container_internal::hash_internal::Enum;
26using ::absl::container_internal::hash_internal::EnumClass;
27using ::testing::Pointee;
28using ::testing::UnorderedElementsAre;
29
30using 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
38INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ConstructorTest, SetTypes);
39INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, LookupTest, SetTypes);
40INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, MembersTest, SetTypes);
41INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashSet, ModifiersTest, SetTypes);
42
43TEST(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
49TEST(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 };
59 absl::node_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
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