15 #include "absl/container/flat_hash_map.h"
19 #include "absl/base/internal/raw_logging.h"
20 #include "absl/container/internal/hash_generator_testing.h"
21 #include "absl/container/internal/unordered_map_constructor_test.h"
22 #include "absl/container/internal/unordered_map_lookup_test.h"
23 #include "absl/container/internal/unordered_map_members_test.h"
24 #include "absl/container/internal/unordered_map_modifiers_test.h"
25 #include "absl/types/any.h"
29 namespace container_internal {
49 const BeforeMain before_main;
51 template <
class K,
class V>
52 using Map = flat_hash_map<K, V, StatefulTestingHash, StatefulTestingEqual,
53 Alloc<std::pair<const K, V>>>;
55 static_assert(!std::is_standard_layout<NonStandardLayout>(),
"");
72 TEST(FlatHashMap, StandardLayout) {
81 static_assert(std::is_standard_layout<Int>(),
"");
84 size_t operator()(
const Int&
obj)
const {
return obj.value; }
90 flat_hash_map<Int, Int, Hash>
m;
94 m.rehash(2 *
m.bucket_count());
97 flat_hash_map<Int, Int, Hash>
m;
108 TEST(FlatHashMap, IteratesMsan) {
111 std::vector<absl::flat_hash_map<int, balast>> garbage;
112 for (
int i = 0;
i < 100; ++
i) {
114 for (
int j = 0;
j < 100; ++
j) {
129 explicit operator size_t()
const {
139 using is_transparent = void;
141 size_t operator()(
size_t obj)
const {
145 size_t operator()(
const LazyInt&
obj)
const {
152 using is_transparent = void;
153 bool operator()(
size_t lhs,
size_t rhs)
const {
156 bool operator()(
size_t lhs,
const LazyInt& rhs)
const {
157 return lhs == rhs.value;
161 TEST(FlatHashMap, LazyKeyPattern) {
166 flat_hash_map<size_t, size_t, Hash, Eq>
m(0,
Hash{&hashes});
169 m[LazyInt(1, &conversions)] = 1;
176 m[LazyInt(1, &conversions)] = 2;
183 m.try_emplace(LazyInt(2, &conversions), 3);
190 m.try_emplace(LazyInt(2, &conversions), 4);
198 TEST(FlatHashMap, BitfieldArgument) {
203 flat_hash_map<int, int>
m;
210 m.insert_or_assign(n, n);
211 m.insert_or_assign(
m.end(), n, n);
213 m.try_emplace(
m.end(), n);
218 TEST(FlatHashMap, MergeExtractInsert) {
222 auto node =
m.extract(1);
233 bool FirstIsEven(std::pair<const int, int> p) {
return p.first % 2 == 0; }
238 flat_hash_map<int, int>
s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
244 flat_hash_map<int, int>
s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
251 flat_hash_map<int, int>
s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
253 [](std::pair<const int, int> kvp) {
254 return kvp.first % 2 == 1;
261 flat_hash_map<int, int>
s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
267 flat_hash_map<int, int>
s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
274 #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
275 TEST(FlatHashMap, NodeHandleMutableKeyAccess) {
276 flat_hash_map<std::string, std::string>
map;
278 map[
"key1"] =
"mapped";
280 auto nh =
map.extract(
map.begin());
288 TEST(FlatHashMap, Reserve) {
291 for (
size_t trial = 0; trial < 20; ++trial) {
292 for (
size_t initial = 3; initial < 100; ++initial) {
295 flat_hash_map<size_t, size_t>
map;
296 for (
size_t i = 0;
i < initial; ++
i) {
301 map.reserve(
map.size() + 2);
306 map[initial + 1] =
a2;
308 size_t& a2new =
map[2];