unordered_map_lookup_test.h
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #ifndef ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_
00016 #define ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_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 LookupTest : public ::testing::Test {};
00028 
00029 TYPED_TEST_SUITE_P(LookupTest);
00030 
00031 TYPED_TEST_P(LookupTest, At) {
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   for (const auto& p : values) {
00038     const auto& val = m.at(p.first);
00039     EXPECT_EQ(p.second, val) << ::testing::PrintToString(p.first);
00040   }
00041 }
00042 
00043 TYPED_TEST_P(LookupTest, OperatorBracket) {
00044   using T = hash_internal::GeneratedType<TypeParam>;
00045   using V = typename TypeParam::mapped_type;
00046   std::vector<T> values;
00047   std::generate_n(std::back_inserter(values), 10,
00048                   hash_internal::Generator<T>());
00049   TypeParam m;
00050   for (const auto& p : values) {
00051     auto& val = m[p.first];
00052     EXPECT_EQ(V(), val) << ::testing::PrintToString(p.first);
00053     val = p.second;
00054   }
00055   for (const auto& p : values)
00056     EXPECT_EQ(p.second, m[p.first]) << ::testing::PrintToString(p.first);
00057 }
00058 
00059 TYPED_TEST_P(LookupTest, Count) {
00060   using T = hash_internal::GeneratedType<TypeParam>;
00061   std::vector<T> values;
00062   std::generate_n(std::back_inserter(values), 10,
00063                   hash_internal::Generator<T>());
00064   TypeParam m;
00065   for (const auto& p : values)
00066     EXPECT_EQ(0, m.count(p.first)) << ::testing::PrintToString(p.first);
00067   m.insert(values.begin(), values.end());
00068   for (const auto& p : values)
00069     EXPECT_EQ(1, m.count(p.first)) << ::testing::PrintToString(p.first);
00070 }
00071 
00072 TYPED_TEST_P(LookupTest, Find) {
00073   using std::get;
00074   using T = hash_internal::GeneratedType<TypeParam>;
00075   std::vector<T> values;
00076   std::generate_n(std::back_inserter(values), 10,
00077                   hash_internal::Generator<T>());
00078   TypeParam m;
00079   for (const auto& p : values)
00080     EXPECT_TRUE(m.end() == m.find(p.first))
00081         << ::testing::PrintToString(p.first);
00082   m.insert(values.begin(), values.end());
00083   for (const auto& p : values) {
00084     auto it = m.find(p.first);
00085     EXPECT_TRUE(m.end() != it) << ::testing::PrintToString(p.first);
00086     EXPECT_EQ(p.second, get<1>(*it)) << ::testing::PrintToString(p.first);
00087   }
00088 }
00089 
00090 TYPED_TEST_P(LookupTest, EqualRange) {
00091   using std::get;
00092   using T = hash_internal::GeneratedType<TypeParam>;
00093   std::vector<T> values;
00094   std::generate_n(std::back_inserter(values), 10,
00095                   hash_internal::Generator<T>());
00096   TypeParam m;
00097   for (const auto& p : values) {
00098     auto r = m.equal_range(p.first);
00099     ASSERT_EQ(0, std::distance(r.first, r.second));
00100   }
00101   m.insert(values.begin(), values.end());
00102   for (const auto& p : values) {
00103     auto r = m.equal_range(p.first);
00104     ASSERT_EQ(1, std::distance(r.first, r.second));
00105     EXPECT_EQ(p.second, get<1>(*r.first)) << ::testing::PrintToString(p.first);
00106   }
00107 }
00108 
00109 REGISTER_TYPED_TEST_CASE_P(LookupTest, At, OperatorBracket, Count, Find,
00110                            EqualRange);
00111 
00112 }  // namespace container_internal
00113 }  // namespace absl
00114 
00115 #endif  // ABSL_CONTAINER_INTERNAL_UNORDERED_MAP_LOOKUP_TEST_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:16