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