00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MODIFIERS_TEST_H_
00016 #define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MODIFIERS_TEST_H_
00017
00018 #include "gmock/gmock.h"
00019 #include "gtest/gtest.h"
00020 #include "absl/container/internal/hash_generator_testing.h"
00021 #include "absl/container/internal/hash_policy_testing.h"
00022
00023 namespace absl {
00024 namespace container_internal {
00025
00026 template <class UnordMap>
00027 class ModifiersTest : public ::testing::Test {};
00028
00029 TYPED_TEST_SUITE_P(ModifiersTest);
00030
00031 TYPED_TEST_P(ModifiersTest, Clear) {
00032 using T = hash_internal::GeneratedType<TypeParam>;
00033 std::vector<T> values;
00034 std::generate_n(std::back_inserter(values), 10,
00035 hash_internal::Generator<T>());
00036 TypeParam m(values.begin(), values.end());
00037 ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
00038 m.clear();
00039 EXPECT_THAT(items(m), ::testing::UnorderedElementsAre());
00040 EXPECT_TRUE(m.empty());
00041 }
00042
00043 TYPED_TEST_P(ModifiersTest, Insert) {
00044 using T = hash_internal::GeneratedType<TypeParam>;
00045 using V = typename TypeParam::mapped_type;
00046 T val = hash_internal::Generator<T>()();
00047 TypeParam m;
00048 auto p = m.insert(val);
00049 EXPECT_TRUE(p.second);
00050 EXPECT_EQ(val, *p.first);
00051 T val2 = {val.first, hash_internal::Generator<V>()()};
00052 p = m.insert(val2);
00053 EXPECT_FALSE(p.second);
00054 EXPECT_EQ(val, *p.first);
00055 }
00056
00057 TYPED_TEST_P(ModifiersTest, InsertHint) {
00058 using T = hash_internal::GeneratedType<TypeParam>;
00059 using V = typename TypeParam::mapped_type;
00060 T val = hash_internal::Generator<T>()();
00061 TypeParam m;
00062 auto it = m.insert(m.end(), val);
00063 EXPECT_TRUE(it != m.end());
00064 EXPECT_EQ(val, *it);
00065 T val2 = {val.first, hash_internal::Generator<V>()()};
00066 it = m.insert(it, val2);
00067 EXPECT_TRUE(it != m.end());
00068 EXPECT_EQ(val, *it);
00069 }
00070
00071 TYPED_TEST_P(ModifiersTest, InsertRange) {
00072 using T = hash_internal::GeneratedType<TypeParam>;
00073 std::vector<T> values;
00074 std::generate_n(std::back_inserter(values), 10,
00075 hash_internal::Generator<T>());
00076 TypeParam m;
00077 m.insert(values.begin(), values.end());
00078 ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
00079 }
00080
00081 TYPED_TEST_P(ModifiersTest, InsertOrAssign) {
00082 #ifdef UNORDERED_MAP_CXX17
00083 using std::get;
00084 using K = typename TypeParam::key_type;
00085 using V = typename TypeParam::mapped_type;
00086 K k = hash_internal::Generator<K>()();
00087 V val = hash_internal::Generator<V>()();
00088 TypeParam m;
00089 auto p = m.insert_or_assign(k, val);
00090 EXPECT_TRUE(p.second);
00091 EXPECT_EQ(k, get<0>(*p.first));
00092 EXPECT_EQ(val, get<1>(*p.first));
00093 V val2 = hash_internal::Generator<V>()();
00094 p = m.insert_or_assign(k, val2);
00095 EXPECT_FALSE(p.second);
00096 EXPECT_EQ(k, get<0>(*p.first));
00097 EXPECT_EQ(val2, get<1>(*p.first));
00098 #endif
00099 }
00100
00101 TYPED_TEST_P(ModifiersTest, InsertOrAssignHint) {
00102 #ifdef UNORDERED_MAP_CXX17
00103 using std::get;
00104 using K = typename TypeParam::key_type;
00105 using V = typename TypeParam::mapped_type;
00106 K k = hash_internal::Generator<K>()();
00107 V val = hash_internal::Generator<V>()();
00108 TypeParam m;
00109 auto it = m.insert_or_assign(m.end(), k, val);
00110 EXPECT_TRUE(it != m.end());
00111 EXPECT_EQ(k, get<0>(*it));
00112 EXPECT_EQ(val, get<1>(*it));
00113 V val2 = hash_internal::Generator<V>()();
00114 it = m.insert_or_assign(it, k, val2);
00115 EXPECT_EQ(k, get<0>(*it));
00116 EXPECT_EQ(val2, get<1>(*it));
00117 #endif
00118 }
00119
00120 TYPED_TEST_P(ModifiersTest, Emplace) {
00121 using T = hash_internal::GeneratedType<TypeParam>;
00122 using V = typename TypeParam::mapped_type;
00123 T val = hash_internal::Generator<T>()();
00124 TypeParam m;
00125
00126
00127 auto p = m.emplace(val);
00128 EXPECT_TRUE(p.second);
00129 EXPECT_EQ(val, *p.first);
00130 T val2 = {val.first, hash_internal::Generator<V>()()};
00131 p = m.emplace(val2);
00132 EXPECT_FALSE(p.second);
00133 EXPECT_EQ(val, *p.first);
00134 }
00135
00136 TYPED_TEST_P(ModifiersTest, EmplaceHint) {
00137 using T = hash_internal::GeneratedType<TypeParam>;
00138 using V = typename TypeParam::mapped_type;
00139 T val = hash_internal::Generator<T>()();
00140 TypeParam m;
00141
00142
00143 auto it = m.emplace_hint(m.end(), val);
00144 EXPECT_EQ(val, *it);
00145 T val2 = {val.first, hash_internal::Generator<V>()()};
00146 it = m.emplace_hint(it, val2);
00147 EXPECT_EQ(val, *it);
00148 }
00149
00150 TYPED_TEST_P(ModifiersTest, TryEmplace) {
00151 #ifdef UNORDERED_MAP_CXX17
00152 using T = hash_internal::GeneratedType<TypeParam>;
00153 using V = typename TypeParam::mapped_type;
00154 T val = hash_internal::Generator<T>()();
00155 TypeParam m;
00156
00157
00158 auto p = m.try_emplace(val.first, val.second);
00159 EXPECT_TRUE(p.second);
00160 EXPECT_EQ(val, *p.first);
00161 T val2 = {val.first, hash_internal::Generator<V>()()};
00162 p = m.try_emplace(val2.first, val2.second);
00163 EXPECT_FALSE(p.second);
00164 EXPECT_EQ(val, *p.first);
00165 #endif
00166 }
00167
00168 TYPED_TEST_P(ModifiersTest, TryEmplaceHint) {
00169 #ifdef UNORDERED_MAP_CXX17
00170 using T = hash_internal::GeneratedType<TypeParam>;
00171 using V = typename TypeParam::mapped_type;
00172 T val = hash_internal::Generator<T>()();
00173 TypeParam m;
00174
00175
00176 auto it = m.try_emplace(m.end(), val.first, val.second);
00177 EXPECT_EQ(val, *it);
00178 T val2 = {val.first, hash_internal::Generator<V>()()};
00179 it = m.try_emplace(it, val2.first, val2.second);
00180 EXPECT_EQ(val, *it);
00181 #endif
00182 }
00183
00184 template <class V>
00185 using IfNotVoid = typename std::enable_if<!std::is_void<V>::value, V>::type;
00186
00187
00188
00189 struct EraseFirst {
00190 template <class Map>
00191 auto operator()(Map* m, int) const
00192 -> IfNotVoid<decltype(m->erase(m->begin()))> {
00193 return m->erase(m->begin());
00194 }
00195 template <class Map>
00196 typename Map::iterator operator()(Map* m, ...) const {
00197 auto it = m->begin();
00198 m->erase(it++);
00199 return it;
00200 }
00201 };
00202
00203 TYPED_TEST_P(ModifiersTest, Erase) {
00204 using T = hash_internal::GeneratedType<TypeParam>;
00205 using std::get;
00206 std::vector<T> values;
00207 std::generate_n(std::back_inserter(values), 10,
00208 hash_internal::Generator<T>());
00209 TypeParam m(values.begin(), values.end());
00210 ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
00211 auto& first = *m.begin();
00212 std::vector<T> values2;
00213 for (const auto& val : values)
00214 if (get<0>(val) != get<0>(first)) values2.push_back(val);
00215 auto it = EraseFirst()(&m, 0);
00216 ASSERT_TRUE(it != m.end());
00217 EXPECT_EQ(1, std::count(values2.begin(), values2.end(), *it));
00218 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values2.begin(),
00219 values2.end()));
00220 }
00221
00222 TYPED_TEST_P(ModifiersTest, EraseRange) {
00223 using T = hash_internal::GeneratedType<TypeParam>;
00224 std::vector<T> values;
00225 std::generate_n(std::back_inserter(values), 10,
00226 hash_internal::Generator<T>());
00227 TypeParam m(values.begin(), values.end());
00228 ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
00229 auto it = m.erase(m.begin(), m.end());
00230 EXPECT_THAT(items(m), ::testing::UnorderedElementsAre());
00231 EXPECT_TRUE(it == m.end());
00232 }
00233
00234 TYPED_TEST_P(ModifiersTest, EraseKey) {
00235 using T = hash_internal::GeneratedType<TypeParam>;
00236 std::vector<T> values;
00237 std::generate_n(std::back_inserter(values), 10,
00238 hash_internal::Generator<T>());
00239 TypeParam m(values.begin(), values.end());
00240 ASSERT_THAT(items(m), ::testing::UnorderedElementsAreArray(values));
00241 EXPECT_EQ(1, m.erase(values[0].first));
00242 EXPECT_EQ(0, std::count(m.begin(), m.end(), values[0]));
00243 EXPECT_THAT(items(m), ::testing::UnorderedElementsAreArray(values.begin() + 1,
00244 values.end()));
00245 }
00246
00247 TYPED_TEST_P(ModifiersTest, Swap) {
00248 using T = hash_internal::GeneratedType<TypeParam>;
00249 std::vector<T> v1;
00250 std::vector<T> v2;
00251 std::generate_n(std::back_inserter(v1), 5, hash_internal::Generator<T>());
00252 std::generate_n(std::back_inserter(v2), 5, hash_internal::Generator<T>());
00253 TypeParam m1(v1.begin(), v1.end());
00254 TypeParam m2(v2.begin(), v2.end());
00255 EXPECT_THAT(items(m1), ::testing::UnorderedElementsAreArray(v1));
00256 EXPECT_THAT(items(m2), ::testing::UnorderedElementsAreArray(v2));
00257 m1.swap(m2);
00258 EXPECT_THAT(items(m1), ::testing::UnorderedElementsAreArray(v2));
00259 EXPECT_THAT(items(m2), ::testing::UnorderedElementsAreArray(v1));
00260 }
00261
00262
00263
00264
00265 REGISTER_TYPED_TEST_CASE_P(ModifiersTest, Clear, Insert, InsertHint,
00266 InsertRange, InsertOrAssign, InsertOrAssignHint,
00267 Emplace, EmplaceHint, TryEmplace, TryEmplaceHint,
00268 Erase, EraseRange, EraseKey, Swap);
00269
00270 }
00271 }
00272
00273 #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_MODIFIERS_TEST_H_