blob: b55be59b04c82253aeab358f348b2ebcc9c16d95 [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/flat_hash_set.h"
16
17#include <vector>
18
19#include "absl/container/internal/hash_generator_testing.h"
20#include "absl/container/internal/unordered_set_constructor_test.h"
21#include "absl/container/internal/unordered_set_lookup_test.h"
22#include "absl/container/internal/unordered_set_members_test.h"
23#include "absl/container/internal/unordered_set_modifiers_test.h"
24#include "absl/memory/memory.h"
25#include "absl/strings/string_view.h"
26
27namespace absl {
28namespace container_internal {
29namespace {
30
31using ::absl::container_internal::hash_internal::Enum;
32using ::absl::container_internal::hash_internal::EnumClass;
33using ::testing::Pointee;
34using ::testing::UnorderedElementsAre;
35using ::testing::UnorderedElementsAreArray;
36
37template <class T>
38using Set =
39 absl::flat_hash_set<T, StatefulTestingHash, StatefulTestingEqual, Alloc<T>>;
40
41using SetTypes =
42 ::testing::Types<Set<int>, Set<std::string>, Set<Enum>, Set<EnumClass>>;
43
44INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ConstructorTest, SetTypes);
45INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, LookupTest, SetTypes);
46INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, MembersTest, SetTypes);
47INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashSet, ModifiersTest, SetTypes);
48
49TEST(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
55TEST(FlatHashSet, BitfieldArgument) {
56 union {
57 int n : 1;
58 };
59 n = 0;
60 absl::flat_hash_set<int> s = {n};
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
72TEST(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 };
82 absl::flat_hash_set<std::unique_ptr<int>, Hash, Eq> set1, set2;
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