00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_
00016 #define ABSL_CONTAINER_INTERNAL_UNORDERED_SET_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 UnordSet>
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(keys(m), ::testing::UnorderedElementsAreArray(values));
00038 m.clear();
00039 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
00040 EXPECT_TRUE(m.empty());
00041 }
00042
00043 TYPED_TEST_P(ModifiersTest, Insert) {
00044 using T = hash_internal::GeneratedType<TypeParam>;
00045 T val = hash_internal::Generator<T>()();
00046 TypeParam m;
00047 auto p = m.insert(val);
00048 EXPECT_TRUE(p.second);
00049 EXPECT_EQ(val, *p.first);
00050 p = m.insert(val);
00051 EXPECT_FALSE(p.second);
00052 }
00053
00054 TYPED_TEST_P(ModifiersTest, InsertHint) {
00055 using T = hash_internal::GeneratedType<TypeParam>;
00056 T val = hash_internal::Generator<T>()();
00057 TypeParam m;
00058 auto it = m.insert(m.end(), val);
00059 EXPECT_TRUE(it != m.end());
00060 EXPECT_EQ(val, *it);
00061 it = m.insert(it, val);
00062 EXPECT_TRUE(it != m.end());
00063 EXPECT_EQ(val, *it);
00064 }
00065
00066 TYPED_TEST_P(ModifiersTest, InsertRange) {
00067 using T = hash_internal::GeneratedType<TypeParam>;
00068 std::vector<T> values;
00069 std::generate_n(std::back_inserter(values), 10,
00070 hash_internal::Generator<T>());
00071 TypeParam m;
00072 m.insert(values.begin(), values.end());
00073 ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
00074 }
00075
00076 TYPED_TEST_P(ModifiersTest, Emplace) {
00077 using T = hash_internal::GeneratedType<TypeParam>;
00078 T val = hash_internal::Generator<T>()();
00079 TypeParam m;
00080
00081
00082 auto p = m.emplace(val);
00083 EXPECT_TRUE(p.second);
00084 EXPECT_EQ(val, *p.first);
00085 p = m.emplace(val);
00086 EXPECT_FALSE(p.second);
00087 EXPECT_EQ(val, *p.first);
00088 }
00089
00090 TYPED_TEST_P(ModifiersTest, EmplaceHint) {
00091 using T = hash_internal::GeneratedType<TypeParam>;
00092 T val = hash_internal::Generator<T>()();
00093 TypeParam m;
00094
00095
00096 auto it = m.emplace_hint(m.end(), val);
00097 EXPECT_EQ(val, *it);
00098 it = m.emplace_hint(it, val);
00099 EXPECT_EQ(val, *it);
00100 }
00101
00102 template <class V>
00103 using IfNotVoid = typename std::enable_if<!std::is_void<V>::value, V>::type;
00104
00105
00106
00107 struct EraseFirst {
00108 template <class Map>
00109 auto operator()(Map* m, int) const
00110 -> IfNotVoid<decltype(m->erase(m->begin()))> {
00111 return m->erase(m->begin());
00112 }
00113 template <class Map>
00114 typename Map::iterator operator()(Map* m, ...) const {
00115 auto it = m->begin();
00116 m->erase(it++);
00117 return it;
00118 }
00119 };
00120
00121 TYPED_TEST_P(ModifiersTest, Erase) {
00122 using T = hash_internal::GeneratedType<TypeParam>;
00123 std::vector<T> values;
00124 std::generate_n(std::back_inserter(values), 10,
00125 hash_internal::Generator<T>());
00126 TypeParam m(values.begin(), values.end());
00127 ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
00128 std::vector<T> values2;
00129 for (const auto& val : values)
00130 if (val != *m.begin()) values2.push_back(val);
00131 auto it = EraseFirst()(&m, 0);
00132 ASSERT_TRUE(it != m.end());
00133 EXPECT_EQ(1, std::count(values2.begin(), values2.end(), *it));
00134 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values2.begin(),
00135 values2.end()));
00136 }
00137
00138 TYPED_TEST_P(ModifiersTest, EraseRange) {
00139 using T = hash_internal::GeneratedType<TypeParam>;
00140 std::vector<T> values;
00141 std::generate_n(std::back_inserter(values), 10,
00142 hash_internal::Generator<T>());
00143 TypeParam m(values.begin(), values.end());
00144 ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
00145 auto it = m.erase(m.begin(), m.end());
00146 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAre());
00147 EXPECT_TRUE(it == m.end());
00148 }
00149
00150 TYPED_TEST_P(ModifiersTest, EraseKey) {
00151 using T = hash_internal::GeneratedType<TypeParam>;
00152 std::vector<T> values;
00153 std::generate_n(std::back_inserter(values), 10,
00154 hash_internal::Generator<T>());
00155 TypeParam m(values.begin(), values.end());
00156 ASSERT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values));
00157 EXPECT_EQ(1, m.erase(values[0]));
00158 EXPECT_EQ(0, std::count(m.begin(), m.end(), values[0]));
00159 EXPECT_THAT(keys(m), ::testing::UnorderedElementsAreArray(values.begin() + 1,
00160 values.end()));
00161 }
00162
00163 TYPED_TEST_P(ModifiersTest, Swap) {
00164 using T = hash_internal::GeneratedType<TypeParam>;
00165 std::vector<T> v1;
00166 std::vector<T> v2;
00167 std::generate_n(std::back_inserter(v1), 5, hash_internal::Generator<T>());
00168 std::generate_n(std::back_inserter(v2), 5, hash_internal::Generator<T>());
00169 TypeParam m1(v1.begin(), v1.end());
00170 TypeParam m2(v2.begin(), v2.end());
00171 EXPECT_THAT(keys(m1), ::testing::UnorderedElementsAreArray(v1));
00172 EXPECT_THAT(keys(m2), ::testing::UnorderedElementsAreArray(v2));
00173 m1.swap(m2);
00174 EXPECT_THAT(keys(m1), ::testing::UnorderedElementsAreArray(v2));
00175 EXPECT_THAT(keys(m2), ::testing::UnorderedElementsAreArray(v1));
00176 }
00177
00178
00179
00180
00181 REGISTER_TYPED_TEST_CASE_P(ModifiersTest, Clear, Insert, InsertHint,
00182 InsertRange, Emplace, EmplaceHint, Erase, EraseRange,
00183 EraseKey, Swap);
00184
00185 }
00186 }
00187
00188 #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_MODIFIERS_TEST_H_