24 namespace container_internal {
27 using ::testing::Field;
28 using ::testing::Pair;
29 using ::testing::UnorderedElementsAre;
31 using MapTypes = ::testing::Types<
33 Alloc<std::pair<const int, int>>>,
36 Alloc<std::pair<const std::string, std::string>>>>;
38 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, ConstructorTest, MapTypes);
39 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, LookupTest, MapTypes);
40 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, MembersTest, MapTypes);
41 INSTANTIATE_TYPED_TEST_SUITE_P(NodeHashMap, ModifiersTest, MapTypes);
45 TEST(NodeHashMap, Emplace) {
49 ASSERT_EQ(0, t.num_moves());
50 ASSERT_EQ(1, t.num_copies());
52 m.emplace(std::string(
"a"), t);
53 ASSERT_EQ(0, t.num_moves());
54 ASSERT_EQ(1, t.num_copies());
58 ASSERT_EQ(0, t.num_moves());
59 ASSERT_EQ(1, t.num_copies());
61 const std::string ca(
"a");
63 ASSERT_EQ(0, t.num_moves());
64 ASSERT_EQ(1, t.num_copies());
66 m.emplace(std::make_pair(
"a", t));
67 ASSERT_EQ(0, t.num_moves());
68 ASSERT_EQ(2, t.num_copies());
70 m.emplace(std::make_pair(std::string(
"a"), t));
71 ASSERT_EQ(0, t.num_moves());
72 ASSERT_EQ(3, t.num_copies());
74 std::pair<std::string, Tracked<int>> p(
"a", t);
75 ASSERT_EQ(0, t.num_moves());
76 ASSERT_EQ(4, t.num_copies());
78 ASSERT_EQ(0, t.num_moves());
79 ASSERT_EQ(4, t.num_copies());
81 const std::pair<std::string, Tracked<int>> cp(
"a", t);
82 ASSERT_EQ(0, t.num_moves());
83 ASSERT_EQ(5, t.num_copies());
85 ASSERT_EQ(0, t.num_moves());
86 ASSERT_EQ(5, t.num_copies());
88 std::pair<const std::string, Tracked<int>> pc(
"a", t);
89 ASSERT_EQ(0, t.num_moves());
90 ASSERT_EQ(6, t.num_copies());
92 ASSERT_EQ(0, t.num_moves());
93 ASSERT_EQ(6, t.num_copies());
95 const std::pair<const std::string, Tracked<int>> cpc(
"a", t);
96 ASSERT_EQ(0, t.num_moves());
97 ASSERT_EQ(7, t.num_copies());
99 ASSERT_EQ(0, t.num_moves());
100 ASSERT_EQ(7, t.num_copies());
102 m.emplace(std::piecewise_construct, std::forward_as_tuple(
"a"),
103 std::forward_as_tuple(t));
104 ASSERT_EQ(0, t.num_moves());
105 ASSERT_EQ(7, t.num_copies());
107 m.emplace(std::piecewise_construct, std::forward_as_tuple(std::string(
"a")),
108 std::forward_as_tuple(t));
109 ASSERT_EQ(0, t.num_moves());
110 ASSERT_EQ(7, t.num_copies());
113 TEST(NodeHashMap, AssignRecursive) {
119 const Tree& child = root.children.emplace().first->second;
124 TEST(FlatHashMap, MoveOnlyKey) {
127 Key(Key&&) =
default;
128 Key& operator=(Key&&) =
default;
131 bool operator()(
const Key&,
const Key&)
const {
return true; }
134 size_t operator()(
const Key&)
const {
return 0; }
140 struct NonMovableKey {
141 explicit NonMovableKey(
int i) : i(i) {}
142 NonMovableKey(NonMovableKey&&) =
delete;
145 struct NonMovableKeyHash {
146 using is_transparent = void;
147 size_t operator()(
const NonMovableKey& k)
const {
return k.i; }
148 size_t operator()(
int k)
const {
return k; }
150 struct NonMovableKeyEq {
151 using is_transparent = void;
152 bool operator()(
const NonMovableKey&
a,
const NonMovableKey&
b)
const {
155 bool operator()(
const NonMovableKey& a,
int b)
const {
return a.i ==
b; }
158 TEST(NodeHashMap, MergeExtractInsert) {
161 set1.
emplace(std::piecewise_construct, std::make_tuple(7),
162 std::make_tuple(-7));
163 set1.
emplace(std::piecewise_construct, std::make_tuple(17),
164 std::make_tuple(-17));
166 set2.
emplace(std::piecewise_construct, std::make_tuple(7),
167 std::make_tuple(-70));
168 set2.
emplace(std::piecewise_construct, std::make_tuple(19),
169 std::make_tuple(-190));
171 auto Elem = [](
int key,
int value) {
175 EXPECT_THAT(set1, UnorderedElementsAre(Elem(7, -7), Elem(17, -17)));
176 EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70), Elem(19, -190)));
184 UnorderedElementsAre(Elem(7, -7), Elem(17, -17), Elem(19, -190)));
185 EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70)));
189 EXPECT_EQ(node.key().i, 7);
190 EXPECT_EQ(node.mapped(), -7);
191 EXPECT_THAT(set1, UnorderedElementsAre(Elem(17, -17), Elem(19, -190)));
195 EXPECT_FALSE(insert_result.inserted);
196 EXPECT_TRUE(insert_result.node);
197 EXPECT_EQ(insert_result.node.key().i, 7);
198 EXPECT_EQ(insert_result.node.mapped(), -7);
199 EXPECT_THAT(*insert_result.position, Elem(7, -70));
200 EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70)));
204 EXPECT_EQ(node.key().i, 17);
205 EXPECT_EQ(node.mapped(), -17);
206 EXPECT_THAT(set1, UnorderedElementsAre(Elem(19, -190)));
212 EXPECT_TRUE(insert_result.inserted);
213 EXPECT_FALSE(insert_result.node);
214 EXPECT_THAT(*insert_result.position, Elem(17, 23));
215 EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70), Elem(17, 23)));
std::pair< iterator, bool > emplace(Args &&... args)
TEST(NotificationTest, SanityTest)
node_type extract(const_iterator position)
void merge(raw_hash_set< absl::container_internal::NodeHashMapPolicy< Key, Value >, H, E, Alloc > &src)
std::pair< iterator, bool > insert(T &&value)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept