unordered_map_lookup_test.h (3986B)
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 ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_ 16 #define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_ 17 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" 22 23 namespace absl { 24 ABSL_NAMESPACE_BEGIN 25 namespace container_internal { 26 27 template <class UnordMap> 28 class LookupTest : public ::testing::Test {}; 29 30 TYPED_TEST_SUITE_P(LookupTest); 31 32 TYPED_TEST_P(LookupTest, At) { 33 using T = hash_internal::GeneratedType<TypeParam>; 34 std::vector<T> values; 35 std::generate_n(std::back_inserter(values), 10, 36 hash_internal::Generator<T>()); 37 TypeParam m(values.begin(), values.end()); 38 for (const auto& p : values) { 39 const auto& val = m.at(p.first); 40 EXPECT_EQ(p.second, val) << ::testing::PrintToString(p.first); 41 } 42 } 43 44 TYPED_TEST_P(LookupTest, OperatorBracket) { 45 using T = hash_internal::GeneratedType<TypeParam>; 46 using V = typename TypeParam::mapped_type; 47 std::vector<T> values; 48 std::generate_n(std::back_inserter(values), 10, 49 hash_internal::Generator<T>()); 50 TypeParam m; 51 for (const auto& p : values) { 52 auto& val = m[p.first]; 53 EXPECT_EQ(V(), val) << ::testing::PrintToString(p.first); 54 val = p.second; 55 } 56 for (const auto& p : values) 57 EXPECT_EQ(p.second, m[p.first]) << ::testing::PrintToString(p.first); 58 } 59 60 TYPED_TEST_P(LookupTest, Count) { 61 using T = hash_internal::GeneratedType<TypeParam>; 62 std::vector<T> values; 63 std::generate_n(std::back_inserter(values), 10, 64 hash_internal::Generator<T>()); 65 TypeParam m; 66 for (const auto& p : values) 67 EXPECT_EQ(0, m.count(p.first)) << ::testing::PrintToString(p.first); 68 m.insert(values.begin(), values.end()); 69 for (const auto& p : values) 70 EXPECT_EQ(1, m.count(p.first)) << ::testing::PrintToString(p.first); 71 } 72 73 TYPED_TEST_P(LookupTest, Find) { 74 using std::get; 75 using T = hash_internal::GeneratedType<TypeParam>; 76 std::vector<T> values; 77 std::generate_n(std::back_inserter(values), 10, 78 hash_internal::Generator<T>()); 79 TypeParam m; 80 for (const auto& p : values) 81 EXPECT_TRUE(m.end() == m.find(p.first)) 82 << ::testing::PrintToString(p.first); 83 m.insert(values.begin(), values.end()); 84 for (const auto& p : values) { 85 auto it = m.find(p.first); 86 EXPECT_TRUE(m.end() != it) << ::testing::PrintToString(p.first); 87 EXPECT_EQ(p.second, get<1>(*it)) << ::testing::PrintToString(p.first); 88 } 89 } 90 91 TYPED_TEST_P(LookupTest, EqualRange) { 92 using std::get; 93 using T = hash_internal::GeneratedType<TypeParam>; 94 std::vector<T> values; 95 std::generate_n(std::back_inserter(values), 10, 96 hash_internal::Generator<T>()); 97 TypeParam m; 98 for (const auto& p : values) { 99 auto r = m.equal_range(p.first); 100 ASSERT_EQ(0, std::distance(r.first, r.second)); 101 } 102 m.insert(values.begin(), values.end()); 103 for (const auto& p : values) { 104 auto r = m.equal_range(p.first); 105 ASSERT_EQ(1, std::distance(r.first, r.second)); 106 EXPECT_EQ(p.second, get<1>(*r.first)) << ::testing::PrintToString(p.first); 107 } 108 } 109 110 REGISTER_TYPED_TEST_SUITE_P(LookupTest, At, OperatorBracket, Count, Find, 111 EqualRange); 112 113 } // namespace container_internal 114 ABSL_NAMESPACE_END 115 } // namespace absl 116 117 #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_