File indexing completed on 2025-01-30 09:31:40
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_
0016 #define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_
0017
0018 #include "gmock/gmock.h"
0019 #include "gtest/gtest.h"
0020 #include "absl/container/internal/hash_generator_testing.h"
0021 #include "absl/container/internal/hash_policy_testing.h"
0022
0023 namespace absl {
0024 ABSL_NAMESPACE_BEGIN
0025 namespace container_internal {
0026
0027 template <class UnordMap>
0028 class LookupTest : public ::testing::Test {};
0029
0030 TYPED_TEST_SUITE_P(LookupTest);
0031
0032 TYPED_TEST_P(LookupTest, At) {
0033 using T = hash_internal::GeneratedType<TypeParam>;
0034 std::vector<T> values;
0035 std::generate_n(std::back_inserter(values), 10,
0036 hash_internal::Generator<T>());
0037 TypeParam m(values.begin(), values.end());
0038 for (const auto& p : values) {
0039 const auto& val = m.at(p.first);
0040 EXPECT_EQ(p.second, val) << ::testing::PrintToString(p.first);
0041 }
0042 }
0043
0044 TYPED_TEST_P(LookupTest, OperatorBracket) {
0045 using T = hash_internal::GeneratedType<TypeParam>;
0046 using V = typename TypeParam::mapped_type;
0047 std::vector<T> values;
0048 std::generate_n(std::back_inserter(values), 10,
0049 hash_internal::Generator<T>());
0050 TypeParam m;
0051 for (const auto& p : values) {
0052 auto& val = m[p.first];
0053 EXPECT_EQ(V(), val) << ::testing::PrintToString(p.first);
0054 val = p.second;
0055 }
0056 for (const auto& p : values)
0057 EXPECT_EQ(p.second, m[p.first]) << ::testing::PrintToString(p.first);
0058 }
0059
0060 TYPED_TEST_P(LookupTest, Count) {
0061 using T = hash_internal::GeneratedType<TypeParam>;
0062 std::vector<T> values;
0063 std::generate_n(std::back_inserter(values), 10,
0064 hash_internal::Generator<T>());
0065 TypeParam m;
0066 for (const auto& p : values)
0067 EXPECT_EQ(0, m.count(p.first)) << ::testing::PrintToString(p.first);
0068 m.insert(values.begin(), values.end());
0069 for (const auto& p : values)
0070 EXPECT_EQ(1, m.count(p.first)) << ::testing::PrintToString(p.first);
0071 }
0072
0073 TYPED_TEST_P(LookupTest, Find) {
0074 using std::get;
0075 using T = hash_internal::GeneratedType<TypeParam>;
0076 std::vector<T> values;
0077 std::generate_n(std::back_inserter(values), 10,
0078 hash_internal::Generator<T>());
0079 TypeParam m;
0080 for (const auto& p : values)
0081 EXPECT_TRUE(m.end() == m.find(p.first))
0082 << ::testing::PrintToString(p.first);
0083 m.insert(values.begin(), values.end());
0084 for (const auto& p : values) {
0085 auto it = m.find(p.first);
0086 EXPECT_TRUE(m.end() != it) << ::testing::PrintToString(p.first);
0087 EXPECT_EQ(p.second, get<1>(*it)) << ::testing::PrintToString(p.first);
0088 }
0089 }
0090
0091 TYPED_TEST_P(LookupTest, EqualRange) {
0092 using std::get;
0093 using T = hash_internal::GeneratedType<TypeParam>;
0094 std::vector<T> values;
0095 std::generate_n(std::back_inserter(values), 10,
0096 hash_internal::Generator<T>());
0097 TypeParam m;
0098 for (const auto& p : values) {
0099 auto r = m.equal_range(p.first);
0100 ASSERT_EQ(0, std::distance(r.first, r.second));
0101 }
0102 m.insert(values.begin(), values.end());
0103 for (const auto& p : values) {
0104 auto r = m.equal_range(p.first);
0105 ASSERT_EQ(1, std::distance(r.first, r.second));
0106 EXPECT_EQ(p.second, get<1>(*r.first)) << ::testing::PrintToString(p.first);
0107 }
0108 }
0109
0110 REGISTER_TYPED_TEST_SUITE_P(LookupTest, At, OperatorBracket, Count, Find,
0111 EqualRange);
0112
0113 }
0114 ABSL_NAMESPACE_END
0115 }
0116
0117 #endif