15 #include "absl/container/node_hash_map.h"
17 #include "absl/container/internal/tracked.h"
18 #include "absl/container/internal/unordered_map_constructor_test.h"
19 #include "absl/container/internal/unordered_map_lookup_test.h"
20 #include "absl/container/internal/unordered_map_members_test.h"
21 #include "absl/container/internal/unordered_map_modifiers_test.h"
25 namespace container_internal {
35 Alloc<std::pair<const int, int>>>,
38 Alloc<std::pair<const std::string, std::string>>>>;
47 TEST(NodeHashMap, Emplace) {
68 m.emplace(std::make_pair(
"a", t));
76 std::pair<std::string, Tracked<int>>
p(
"a", t);
83 const std::pair<std::string, Tracked<int>> cp(
"a", t);
90 std::pair<const std::string, Tracked<int>> pc(
"a", t);
97 const std::pair<const std::string, Tracked<int>> cpc(
"a", t);
104 m.emplace(std::piecewise_construct, std::forward_as_tuple(
"a"),
105 std::forward_as_tuple(t));
109 m.emplace(std::piecewise_construct, std::forward_as_tuple(
std::string(
"a")),
110 std::forward_as_tuple(t));
115 TEST(NodeHashMap, AssignRecursive) {
121 const Tree&
child =
root.children.emplace().first->second;
126 TEST(FlatHashMap, MoveOnlyKey) {
130 Key& operator=(
Key&&) =
default;
133 bool operator()(
const Key&,
const Key&)
const {
return true; }
136 size_t operator()(
const Key&)
const {
return 0; }
142 struct NonMovableKey {
143 explicit NonMovableKey(
int i) :
i(
i) {}
144 NonMovableKey(NonMovableKey&&) =
delete;
147 struct NonMovableKeyHash {
148 using is_transparent = void;
149 size_t operator()(
const NonMovableKey&
k)
const {
return k.i; }
150 size_t operator()(
int k)
const {
return k; }
152 struct NonMovableKeyEq {
153 using is_transparent = void;
154 bool operator()(
const NonMovableKey& a,
const NonMovableKey&
b)
const {
157 bool operator()(
const NonMovableKey& a,
int b)
const {
return a.i ==
b; }
160 TEST(NodeHashMap, MergeExtractInsert) {
173 auto Elem = [](
int key,
int value) {
189 auto node = set1.extract(7);
195 auto insert_result = set2.insert(
std::move(node));
199 EXPECT_EQ(insert_result.node.key().i, 7);
200 EXPECT_EQ(insert_result.node.mapped(), -7);
201 EXPECT_THAT(*insert_result.position, Elem(7, -70));
204 node = set1.extract(17);
212 insert_result = set2.insert(
std::move(node));
216 EXPECT_THAT(*insert_result.position, Elem(17, 23));
220 bool FirstIsEven(std::pair<const int, int> p) {
return p.first % 2 == 0; }
225 node_hash_map<int, int>
s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
231 node_hash_map<int, int>
s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
238 node_hash_map<int, int>
s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
240 [](std::pair<const int, int> kvp) {
241 return kvp.first % 2 == 1;
248 node_hash_map<int, int>
s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
254 node_hash_map<int, int>
s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
261 #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
262 TEST(NodeHashMap, NodeHandleMutableKeyAccess) {
263 node_hash_map<std::string, std::string>
map;
265 map[
"key1"] =
"mapped";
267 auto nh =
map.extract(
map.begin());