unordered_set_lookup_test.h (3155B)
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_SET_LOOKUP_TEST_H_ 16 #define ABSL_CONTAINER_INTERNAL_UNORDERED_SET_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 UnordSet> 28 class LookupTest : public ::testing::Test {}; 29 30 TYPED_TEST_SUITE_P(LookupTest); 31 32 TYPED_TEST_P(LookupTest, Count) { 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; 38 for (const auto& v : values) 39 EXPECT_EQ(0, m.count(v)) << ::testing::PrintToString(v); 40 m.insert(values.begin(), values.end()); 41 for (const auto& v : values) 42 EXPECT_EQ(1, m.count(v)) << ::testing::PrintToString(v); 43 } 44 45 TYPED_TEST_P(LookupTest, Find) { 46 using T = hash_internal::GeneratedType<TypeParam>; 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& v : values) 52 EXPECT_TRUE(m.end() == m.find(v)) << ::testing::PrintToString(v); 53 m.insert(values.begin(), values.end()); 54 for (const auto& v : values) { 55 typename TypeParam::iterator it = m.find(v); 56 static_assert(std::is_same<const typename TypeParam::value_type&, 57 decltype(*it)>::value, 58 ""); 59 static_assert(std::is_same<const typename TypeParam::value_type*, 60 decltype(it.operator->())>::value, 61 ""); 62 EXPECT_TRUE(m.end() != it) << ::testing::PrintToString(v); 63 EXPECT_EQ(v, *it) << ::testing::PrintToString(v); 64 } 65 } 66 67 TYPED_TEST_P(LookupTest, EqualRange) { 68 using T = hash_internal::GeneratedType<TypeParam>; 69 std::vector<T> values; 70 std::generate_n(std::back_inserter(values), 10, 71 hash_internal::Generator<T>()); 72 TypeParam m; 73 for (const auto& v : values) { 74 auto r = m.equal_range(v); 75 ASSERT_EQ(0, std::distance(r.first, r.second)); 76 } 77 m.insert(values.begin(), values.end()); 78 for (const auto& v : values) { 79 auto r = m.equal_range(v); 80 ASSERT_EQ(1, std::distance(r.first, r.second)); 81 EXPECT_EQ(v, *r.first); 82 } 83 } 84 85 REGISTER_TYPED_TEST_SUITE_P(LookupTest, Count, Find, EqualRange); 86 87 } // namespace container_internal 88 ABSL_NAMESPACE_END 89 } // namespace absl 90 91 #endif // ABSL_CONTAINER_INTERNAL_UNORDERED_SET_LOOKUP_TEST_H_