1 // Copyright 2018 The Abseil Authors.
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_
16 #define ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20 #include "absl/container/internal/hash_generator_testing.h"
21 #include "absl/container/internal/hash_policy_testing.h"
24 namespace container_internal {
26 template <class UnordSet>
27 class ModifiersTest : public ::testing::Test {};
29 TYPED_TEST_SUITE_P(ModifiersTest);
31 TYPED_TEST_P(ModifiersTest, Clear) {
32 using T = hash_internal::GeneratedType<TypeParam>;
33 std::vector<T> values;
34 std::generate_n(std::back_inserter(values), 10,
35 hash_internal::Generator<T>());
36 TypeParam m(values.begin(), values.end());
37 ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
39 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
40 EXPECT_TRUE(m.empty());
43 TYPED_TEST_P(ModifiersTest, Insert) {
44 using T = hash_internal::GeneratedType<TypeParam>;
45 T val = hash_internal::Generator<T>()();
47 auto p = m.insert(val);
48 EXPECT_TRUE(p.second);
49 EXPECT_EQ(val, *p.first);
51 EXPECT_FALSE(p.second);
54 TYPED_TEST_P(ModifiersTest, InsertHint) {
55 using T = hash_internal::GeneratedType<TypeParam>;
56 T val = hash_internal::Generator<T>()();
58 auto it = m.insert(m.end(), val);
59 EXPECT_TRUE(it != m.end());
61 it = m.insert(it, val);
62 EXPECT_TRUE(it != m.end());
66 TYPED_TEST_P(ModifiersTest, InsertRange) {
67 using T = hash_internal::GeneratedType<TypeParam>;
68 std::vector<T> values;
69 std::generate_n(std::back_inserter(values), 10,
70 hash_internal::Generator<T>());
72 m.insert(values.begin(), values.end());
73 ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
76 TYPED_TEST_P(ModifiersTest, Emplace) {
77 using T = hash_internal::GeneratedType<TypeParam>;
78 T val = hash_internal::Generator<T>()();
80 // TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
81 // with test traits/policy.
82 auto p = m.emplace(val);
83 EXPECT_TRUE(p.second);
84 EXPECT_EQ(val, *p.first);
86 EXPECT_FALSE(p.second);
87 EXPECT_EQ(val, *p.first);
90 TYPED_TEST_P(ModifiersTest, EmplaceHint) {
91 using T = hash_internal::GeneratedType<TypeParam>;
92 T val = hash_internal::Generator<T>()();
94 // TODO(alkis): We need a way to run emplace in a more meaningful way. Perhaps
95 // with test traits/policy.
96 auto it = m.emplace_hint(m.end(), val);
98 it = m.emplace_hint(it, val);
103 using IfNotVoid = typename std::enable_if<!std::is_void<V>::value, V>::type;
105 // In openmap we chose not to return the iterator from erase because that's
106 // more expensive. As such we adapt erase to return an iterator here.
109 auto operator()(Map* m, int) const
110 -> IfNotVoid<decltype(m->erase(m->begin()))> {
111 return m->erase(m->begin());
114 typename Map::iterator operator()(Map* m, ...) const {
115 auto it = m->begin();
121 TYPED_TEST_P(ModifiersTest, Erase) {
122 using T = hash_internal::GeneratedType<TypeParam>;
123 std::vector<T> values;
124 std::generate_n(std::back_inserter(values), 10,
125 hash_internal::Generator<T>());
126 TypeParam m(values.begin(), values.end());
127 ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
128 std::vector<T> values2;
129 for (const auto& val : values)
130 if (val != *m.begin()) values2.push_back(val);
131 auto it = EraseFirst()(&m, 0);
132 ASSERT_TRUE(it != m.end());
133 EXPECT_EQ(1, std::count(values2.begin(), values2.end(), *it));
134 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values2.begin(),
138 TYPED_TEST_P(ModifiersTest, EraseRange) {
139 using T = hash_internal::GeneratedType<TypeParam>;
140 std::vector<T> values;
141 std::generate_n(std::back_inserter(values), 10,
142 hash_internal::Generator<T>());
143 TypeParam m(values.begin(), values.end());
144 ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
145 auto it = m.erase(m.begin(), m.end());
146 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
147 EXPECT_TRUE(it == m.end());
150 TYPED_TEST_P(ModifiersTest, EraseKey) {
151 using T = hash_internal::GeneratedType<TypeParam>;
152 std::vector<T> values;
153 std::generate_n(std::back_inserter(values), 10,
154 hash_internal::Generator<T>());
155 TypeParam m(values.begin(), values.end());
156 ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
157 EXPECT_EQ(1, m.erase(values[0]));
158 EXPECT_EQ(0, std::count(m.begin(), m.end(), values[0]));
159 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values.begin() + 1,
163 TYPED_TEST_P(ModifiersTest, Swap) {
164 using T = hash_internal::GeneratedType<TypeParam>;
167 std::generate_n(std::back_inserter(v1), 5, hash_internal::Generator<T>());
168 std::generate_n(std::back_inserter(v2), 5, hash_internal::Generator<T>());
169 TypeParam m1(v1.begin(), v1.end());
170 TypeParam m2(v2.begin(), v2.end());
171 EXPECT_THAT(keys(m1), ::testing::UnorderedElementsAreArray(v1));
172 EXPECT_THAT(keys(m2), ::testing::UnorderedElementsAreArray(v2));
174 EXPECT_THAT(keys(m1), ::testing::UnorderedElementsAreArray(v2));
175 EXPECT_THAT(keys(m2), ::testing::UnorderedElementsAreArray(v1));
178 // TODO(alkis): Write tests for extract.
179 // TODO(alkis): Write tests for merge.
181 REGISTER_TYPED_TEST_CASE_P(ModifiersTest, Clear, Insert, InsertHint,
182 InsertRange, Emplace, EmplaceHint, Erase, EraseRange,
185 } // namespace container_internal
188 #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_