Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 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 | #include "absl/container/flat_hash_map.h" |
| 16 | |
| 17 | #include <memory> |
| 18 | |
| 19 | #include "absl/container/internal/hash_generator_testing.h" |
| 20 | #include "absl/container/internal/unordered_map_constructor_test.h" |
| 21 | #include "absl/container/internal/unordered_map_lookup_test.h" |
| 22 | #include "absl/container/internal/unordered_map_members_test.h" |
| 23 | #include "absl/container/internal/unordered_map_modifiers_test.h" |
| 24 | #include "absl/types/any.h" |
| 25 | |
| 26 | namespace absl { |
| 27 | namespace container_internal { |
| 28 | namespace { |
| 29 | using ::absl::container_internal::hash_internal::Enum; |
| 30 | using ::absl::container_internal::hash_internal::EnumClass; |
| 31 | using ::testing::_; |
| 32 | using ::testing::Pair; |
| 33 | using ::testing::UnorderedElementsAre; |
| 34 | |
| 35 | template <class K, class V> |
| 36 | using Map = flat_hash_map<K, V, StatefulTestingHash, StatefulTestingEqual, |
| 37 | Alloc<std::pair<const K, V>>>; |
| 38 | |
| 39 | static_assert(!std::is_standard_layout<NonStandardLayout>(), ""); |
| 40 | |
| 41 | using MapTypes = |
| 42 | ::testing::Types<Map<int, int>, Map<std::string, int>, |
| 43 | Map<Enum, std::string>, Map<EnumClass, int>, |
| 44 | Map<int, NonStandardLayout>, Map<NonStandardLayout, int>>; |
| 45 | |
| 46 | INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, ConstructorTest, MapTypes); |
| 47 | INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, LookupTest, MapTypes); |
| 48 | INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, MembersTest, MapTypes); |
| 49 | INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, ModifiersTest, MapTypes); |
| 50 | |
| 51 | using UniquePtrMapTypes = ::testing::Types<Map<int, std::unique_ptr<int>>>; |
| 52 | |
| 53 | INSTANTIATE_TYPED_TEST_SUITE_P(FlatHashMap, UniquePtrModifiersTest, |
| 54 | UniquePtrMapTypes); |
| 55 | |
| 56 | TEST(FlatHashMap, StandardLayout) { |
| 57 | struct Int { |
| 58 | explicit Int(size_t value) : value(value) {} |
| 59 | Int() : value(0) { ADD_FAILURE(); } |
| 60 | Int(const Int& other) : value(other.value) { ADD_FAILURE(); } |
| 61 | Int(Int&&) = default; |
| 62 | bool operator==(const Int& other) const { return value == other.value; } |
| 63 | size_t value; |
| 64 | }; |
| 65 | static_assert(std::is_standard_layout<Int>(), ""); |
| 66 | |
| 67 | struct Hash { |
| 68 | size_t operator()(const Int& obj) const { return obj.value; } |
| 69 | }; |
| 70 | |
| 71 | // Verify that neither the key nor the value get default-constructed or |
| 72 | // copy-constructed. |
| 73 | { |
| 74 | flat_hash_map<Int, Int, Hash> m; |
| 75 | m.try_emplace(Int(1), Int(2)); |
| 76 | m.try_emplace(Int(3), Int(4)); |
| 77 | m.erase(Int(1)); |
| 78 | m.rehash(2 * m.bucket_count()); |
| 79 | } |
| 80 | { |
| 81 | flat_hash_map<Int, Int, Hash> m; |
| 82 | m.try_emplace(Int(1), Int(2)); |
| 83 | m.try_emplace(Int(3), Int(4)); |
| 84 | m.erase(Int(1)); |
| 85 | m.clear(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // gcc becomes unhappy if this is inside the method, so pull it out here. |
| 90 | struct balast {}; |
| 91 | |
| 92 | TEST(FlatHashMap, IteratesMsan) { |
| 93 | // Because SwissTable randomizes on pointer addresses, we keep old tables |
| 94 | // around to ensure we don't reuse old memory. |
| 95 | std::vector<absl::flat_hash_map<int, balast>> garbage; |
| 96 | for (int i = 0; i < 100; ++i) { |
| 97 | absl::flat_hash_map<int, balast> t; |
| 98 | for (int j = 0; j < 100; ++j) { |
| 99 | t[j]; |
| 100 | for (const auto& p : t) EXPECT_THAT(p, Pair(_, _)); |
| 101 | } |
| 102 | garbage.push_back(std::move(t)); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Demonstration of the "Lazy Key" pattern. This uses heterogeneous insert to |
| 107 | // avoid creating expensive key elements when the item is already present in the |
| 108 | // map. |
| 109 | struct LazyInt { |
| 110 | explicit LazyInt(size_t value, int* tracker) |
| 111 | : value(value), tracker(tracker) {} |
| 112 | |
| 113 | explicit operator size_t() const { |
| 114 | ++*tracker; |
| 115 | return value; |
| 116 | } |
| 117 | |
| 118 | size_t value; |
| 119 | int* tracker; |
| 120 | }; |
| 121 | |
| 122 | struct Hash { |
| 123 | using is_transparent = void; |
| 124 | int* tracker; |
| 125 | size_t operator()(size_t obj) const { |
| 126 | ++*tracker; |
| 127 | return obj; |
| 128 | } |
| 129 | size_t operator()(const LazyInt& obj) const { |
| 130 | ++*tracker; |
| 131 | return obj.value; |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | struct Eq { |
| 136 | using is_transparent = void; |
| 137 | bool operator()(size_t lhs, size_t rhs) const { |
| 138 | return lhs == rhs; |
| 139 | } |
| 140 | bool operator()(size_t lhs, const LazyInt& rhs) const { |
| 141 | return lhs == rhs.value; |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | TEST(FlatHashMap, LazyKeyPattern) { |
| 146 | // hashes are only guaranteed in opt mode, we use assertions to track internal |
| 147 | // state that can cause extra calls to hash. |
| 148 | int conversions = 0; |
| 149 | int hashes = 0; |
| 150 | flat_hash_map<size_t, size_t, Hash, Eq> m(0, Hash{&hashes}); |
| 151 | m.reserve(3); |
| 152 | |
| 153 | m[LazyInt(1, &conversions)] = 1; |
| 154 | EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 1))); |
| 155 | EXPECT_EQ(conversions, 1); |
| 156 | #ifdef NDEBUG |
| 157 | EXPECT_EQ(hashes, 1); |
| 158 | #endif |
| 159 | |
| 160 | m[LazyInt(1, &conversions)] = 2; |
| 161 | EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2))); |
| 162 | EXPECT_EQ(conversions, 1); |
| 163 | #ifdef NDEBUG |
| 164 | EXPECT_EQ(hashes, 2); |
| 165 | #endif |
| 166 | |
| 167 | m.try_emplace(LazyInt(2, &conversions), 3); |
| 168 | EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2), Pair(2, 3))); |
| 169 | EXPECT_EQ(conversions, 2); |
| 170 | #ifdef NDEBUG |
| 171 | EXPECT_EQ(hashes, 3); |
| 172 | #endif |
| 173 | |
| 174 | m.try_emplace(LazyInt(2, &conversions), 4); |
| 175 | EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 2), Pair(2, 3))); |
| 176 | EXPECT_EQ(conversions, 2); |
| 177 | #ifdef NDEBUG |
| 178 | EXPECT_EQ(hashes, 4); |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | TEST(FlatHashMap, BitfieldArgument) { |
| 183 | union { |
| 184 | int n : 1; |
| 185 | }; |
| 186 | n = 0; |
| 187 | flat_hash_map<int, int> m; |
| 188 | m.erase(n); |
| 189 | m.count(n); |
| 190 | m.prefetch(n); |
| 191 | m.find(n); |
| 192 | m.contains(n); |
| 193 | m.equal_range(n); |
| 194 | m.insert_or_assign(n, n); |
| 195 | m.insert_or_assign(m.end(), n, n); |
| 196 | m.try_emplace(n); |
| 197 | m.try_emplace(m.end(), n); |
| 198 | m.at(n); |
| 199 | m[n]; |
| 200 | } |
| 201 | |
| 202 | TEST(FlatHashMap, MergeExtractInsert) { |
| 203 | // We can't test mutable keys, or non-copyable keys with flat_hash_map. |
| 204 | // Test that the nodes have the proper API. |
| 205 | absl::flat_hash_map<int, int> m = {{1, 7}, {2, 9}}; |
| 206 | auto node = m.extract(1); |
| 207 | EXPECT_TRUE(node); |
| 208 | EXPECT_EQ(node.key(), 1); |
| 209 | EXPECT_EQ(node.mapped(), 7); |
| 210 | EXPECT_THAT(m, UnorderedElementsAre(Pair(2, 9))); |
| 211 | |
| 212 | node.mapped() = 17; |
| 213 | m.insert(std::move(node)); |
| 214 | EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 17), Pair(2, 9))); |
| 215 | } |
| 216 | |
| 217 | #if (defined(ABSL_HAVE_STD_ANY) || !defined(_LIBCPP_VERSION)) && \ |
| 218 | !defined(__EMSCRIPTEN__) |
| 219 | TEST(FlatHashMap, Any) { |
| 220 | absl::flat_hash_map<int, absl::any> m; |
| 221 | m.emplace(1, 7); |
| 222 | auto it = m.find(1); |
| 223 | ASSERT_NE(it, m.end()); |
| 224 | EXPECT_EQ(7, absl::any_cast<int>(it->second)); |
| 225 | |
| 226 | m.emplace(std::piecewise_construct, std::make_tuple(2), std::make_tuple(8)); |
| 227 | it = m.find(2); |
| 228 | ASSERT_NE(it, m.end()); |
| 229 | EXPECT_EQ(8, absl::any_cast<int>(it->second)); |
| 230 | |
| 231 | m.emplace(std::piecewise_construct, std::make_tuple(3), |
| 232 | std::make_tuple(absl::any(9))); |
| 233 | it = m.find(3); |
| 234 | ASSERT_NE(it, m.end()); |
| 235 | EXPECT_EQ(9, absl::any_cast<int>(it->second)); |
| 236 | |
| 237 | struct H { |
| 238 | size_t operator()(const absl::any&) const { return 0; } |
| 239 | }; |
| 240 | struct E { |
| 241 | bool operator()(const absl::any&, const absl::any&) const { return true; } |
| 242 | }; |
| 243 | absl::flat_hash_map<absl::any, int, H, E> m2; |
| 244 | m2.emplace(1, 7); |
| 245 | auto it2 = m2.find(1); |
| 246 | ASSERT_NE(it2, m2.end()); |
| 247 | EXPECT_EQ(7, it2->second); |
| 248 | } |
| 249 | #endif // (defined(ABSL_HAVE_STD_ANY) || !defined(_LIBCPP_VERSION)) && |
| 250 | // !defined(__EMSCRIPTEN__) |
| 251 | |
| 252 | } // namespace |
| 253 | } // namespace container_internal |
| 254 | } // namespace absl |