| 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 |
|
| 15 |
#ifndef PHMAP_PRIV_UNORDERED_SET_MODIFIERS_TEST_H_ |
| 16 |
#define PHMAP_PRIV_UNORDERED_SET_MODIFIERS_TEST_H_ |
| 17 |
|
| 18 |
#include "gmock/gmock.h" |
| 19 |
#include "gtest/gtest.h" |
| 20 |
#include "hash_generator_testing.h" |
| 21 |
#include "hash_policy_testing.h" |
| 22 |
|
| 23 |
namespace phmap { |
| 24 |
namespace priv { |
| 25 |
|
| 26 |
template <class UnordSet> |
| 27 |
class ModifiersTest : public ::testing::Test {}; |
| 28 |
|
| 29 |
TYPED_TEST_SUITE_P(ModifiersTest); |
| 30 |
|
| 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)); |
| 38 |
m.clear(); |
| 39 |
EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre()); |
| 40 |
EXPECT_TRUE(m.empty()); |
| 41 |
} |
| 42 |
|
| 43 |
TYPED_TEST_P(ModifiersTest, Insert) { |
| 44 |
using T = hash_internal::GeneratedType<TypeParam>; |
| 45 |
T val = hash_internal::Generator<T>()(); |
| 46 |
TypeParam m; |
| 47 |
auto p = m.insert(val); |
| 48 |
EXPECT_TRUE(p.second); |
| 49 |
EXPECT_EQ(val, *p.first); |
| 50 |
p = m.insert(val); |
| 51 |
EXPECT_FALSE(p.second); |
| 52 |
} |
| 53 |
|
| 54 |
TYPED_TEST_P(ModifiersTest, InsertHint) { |
| 55 |
using T = hash_internal::GeneratedType<TypeParam>; |
| 56 |
T val = hash_internal::Generator<T>()(); |
| 57 |
TypeParam m; |
| 58 |
auto it = m.insert(m.end(), val); |
| 59 |
EXPECT_TRUE(it != m.end()); |
| 60 |
EXPECT_EQ(val, *it); |
| 61 |
it = m.insert(it, val); |
| 62 |
EXPECT_TRUE(it != m.end()); |
| 63 |
EXPECT_EQ(val, *it); |
| 64 |
} |
| 65 |
|
| 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>()); |
| 71 |
TypeParam m; |
| 72 |
m.insert(values.begin(), values.end()); |
| 73 |
ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values)); |
| 74 |
} |
| 75 |
|
| 76 |
TYPED_TEST_P(ModifiersTest, Emplace) { |
| 77 |
using T = hash_internal::GeneratedType<TypeParam>; |
| 78 |
T val = hash_internal::Generator<T>()(); |
| 79 |
TypeParam m; |
| 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); |
| 85 |
p = m.emplace(val); |
| 86 |
EXPECT_FALSE(p.second); |
| 87 |
EXPECT_EQ(val, *p.first); |
| 88 |
} |
| 89 |
|
| 90 |
TYPED_TEST_P(ModifiersTest, EmplaceHint) { |
| 91 |
using T = hash_internal::GeneratedType<TypeParam>; |
| 92 |
T val = hash_internal::Generator<T>()(); |
| 93 |
TypeParam m; |
| 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); |
| 97 |
EXPECT_EQ(val, *it); |
| 98 |
it = m.emplace_hint(it, val); |
| 99 |
EXPECT_EQ(val, *it); |
| 100 |
} |
| 101 |
|
| 102 |
template <class V> |
| 103 |
using IfNotVoid = typename std::enable_if<!std::is_void<V>::value, V>::type; |
| 104 |
|
| 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. |
| 107 |
struct EraseFirst { |
| 108 |
template <class Map> |
| 109 |
auto operator()(Map* m, int) const |
| 110 |
-> IfNotVoid<decltype(m->erase(m->begin()))> { |
| 111 |
return m->erase(m->begin()); |
| 112 |
} |
| 113 |
template <class Map> |
| 114 |
typename Map::iterator operator()(Map* m, ...) const { |
| 115 |
auto it = m->begin(); |
| 116 |
m->erase(it++); |
| 117 |
return it; |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 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(), |
| 135 |
values2.end())); |
| 136 |
} |
| 137 |
|
| 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()); |
| 148 |
} |
| 149 |
|
| 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, |
| 160 |
values.end())); |
| 161 |
} |
| 162 |
|
| 163 |
TYPED_TEST_P(ModifiersTest, Swap) { |
| 164 |
using T = hash_internal::GeneratedType<TypeParam>; |
| 165 |
std::vector<T> v1; |
| 166 |
std::vector<T> v2; |
| 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)); |
| 173 |
m1.swap(m2); |
| 174 |
EXPECT_THAT(keys(m1), ::testing::UnorderedElementsAreArray(v2)); |
| 175 |
EXPECT_THAT(keys(m2), ::testing::UnorderedElementsAreArray(v1)); |
| 176 |
} |
| 177 |
|
| 178 |
// TODO(alkis): Write tests for extract. |
| 179 |
// TODO(alkis): Write tests for merge. |
| 180 |
|
| 181 |
REGISTER_TYPED_TEST_SUITE_P(ModifiersTest, Clear, Insert, InsertHint, |
| 182 |
InsertRange, Emplace, EmplaceHint, Erase, EraseRange, |
| 183 |
EraseKey, Swap); |
| 184 |
|
| 185 |
} // namespace priv |
| 186 |
} // namespace phmap |
| 187 |
|
| 188 |
#endif // PHMAP_PRIV_UNORDERED_SET_MODIFIERS_TEST_H_ |