Skip to content

Commit e5e95d5

Browse files
committed
Improve HashMap
1 parent 2751984 commit e5e95d5

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

test/hashmap/hashmap_reserve.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,96 @@ GTEST_TEST(hashmap, reserve_non_empty_to_more_size_allocate_only)
114114
hud_assert_true(result);
115115
}
116116

117+
// Constant
118+
{
119+
constexpr auto result = test();
120+
hud_assert_true(result);
121+
}
122+
}
123+
124+
GTEST_TEST(hashmap, reserve_less_than_max_count_but_more_than_count_do_nothing)
125+
{
126+
using AllocatorType = hud_test::allocator_watcher<1>;
127+
using HashMapType = hud::hashmap<i32, i64, hud::hashmap_default_hasher, hud::hashmap_default_key_equal<i32>, AllocatorType>;
128+
129+
const auto test = []()
130+
{
131+
HashMapType map;
132+
bool reserve_ok = map.count() == 0;
133+
reserve_ok &= map.max_count() == 0;
134+
reserve_ok &= map.slack() == 0;
135+
reserve_ok &= map.allocator().allocation_count() == 0;
136+
reserve_ok &= map.allocator().free_count() == 0;
137+
map.reserve(4);
138+
map.add(1, 11);
139+
map.add(2, 22);
140+
reserve_ok &= map.count() == 2;
141+
reserve_ok &= map.max_count() >= 4;
142+
reserve_ok &= map.slack() >= 0;
143+
reserve_ok &= (map.allocator().allocation_count() == (hud::is_constant_evaluated() ? 2 : 1));
144+
reserve_ok &= map.allocator().free_count() == 0;
145+
146+
map.reserve(3);
147+
reserve_ok &= map.count() == 2;
148+
reserve_ok &= map.max_count() >= 4;
149+
reserve_ok &= map.slack() >= 0;
150+
reserve_ok &= (map.allocator().allocation_count() == (hud::is_constant_evaluated() ? 2 : 1));
151+
reserve_ok &= map.allocator().free_count() == 0;
152+
153+
return reserve_ok;
154+
};
155+
156+
// Non constant
157+
{
158+
const auto result = test();
159+
hud_assert_true(result);
160+
}
161+
162+
// Constant
163+
{
164+
constexpr auto result = test();
165+
hud_assert_true(result);
166+
}
167+
}
168+
169+
GTEST_TEST(hashmap, reserve_less_than_count_but_more_than_count_do_nothing)
170+
{
171+
using AllocatorType = hud_test::allocator_watcher<1>;
172+
using HashMapType = hud::hashmap<i32, i64, hud::hashmap_default_hasher, hud::hashmap_default_key_equal<i32>, AllocatorType>;
173+
174+
const auto test = []()
175+
{
176+
HashMapType map;
177+
bool reserve_ok = map.count() == 0;
178+
reserve_ok &= map.max_count() == 0;
179+
reserve_ok &= map.slack() == 0;
180+
reserve_ok &= map.allocator().allocation_count() == 0;
181+
reserve_ok &= map.allocator().free_count() == 0;
182+
map.reserve(4);
183+
map.add(1, 11);
184+
map.add(2, 22);
185+
reserve_ok &= map.count() == 2;
186+
reserve_ok &= map.max_count() >= 4;
187+
reserve_ok &= map.slack() >= 0;
188+
reserve_ok &= (map.allocator().allocation_count() == (hud::is_constant_evaluated() ? 2 : 1));
189+
reserve_ok &= map.allocator().free_count() == 0;
190+
191+
map.reserve(1);
192+
reserve_ok &= map.count() == 2;
193+
reserve_ok &= map.max_count() >= 4;
194+
reserve_ok &= map.slack() >= 0;
195+
reserve_ok &= (map.allocator().allocation_count() == (hud::is_constant_evaluated() ? 2 : 1));
196+
reserve_ok &= map.allocator().free_count() == 0;
197+
198+
return reserve_ok;
199+
};
200+
201+
// Non constant
202+
{
203+
const auto result = test();
204+
hud_assert_true(result);
205+
}
206+
117207
// Constant
118208
{
119209
constexpr auto result = test();

0 commit comments

Comments
 (0)