47# include <gtest/gtest.h>
48# include <gtest/gtest-spi.h>
67 constexpr int Keys = 300;
69 size_t new_hash(
const int &
k)
noexcept
71 return static_cast<size_t>(
k) * 40503u + 17;
74 size_t new_pair_hash(
const std::pair<int, int> & p)
noexcept
76 return new_hash(p.first);
83 template <
class Container,
class Insert,
class Set,
class Holds>
84 size_t fail_every_allocation(Insert insert, Set set, Holds holds)
87 for (
int k = 0;
k < Keys; ++
k)
91 for (
size_t n = 1; n < 1000; ++n)
93 SCOPED_TRACE(
"the allocation " + std::to_string(n) +
" fails");
96 const Fail_At failing(n);
101 catch (
const std::bad_alloc &)
107 for (
int k = 0;
k < Keys; ++
k)
110 ADD_FAILURE() <<
"key " <<
k <<
" is no longer found";
117 if (refused_allocations > 0)
118 ADD_FAILURE() <<
"an allocation failed, but no std::bad_alloc reached the caller";
124 ADD_FAILURE() <<
"set_hash_fct() made more than 1000 allocations";
130 int *
volatile allocated =
nullptr;
132 void allocate_and_free()
134 allocated =
new int[4];
142 TEST(HashFunctionAllocation, AnAllocationThatTheOperationCatchesFailsTheTest)
145 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
156 catch (
const std::bad_alloc &)
161 [] (
auto & c,
int k) {
return c.search(
k) !=
nullptr; }),
162 "no std::bad_alloc reached the caller");
165 TEST(HashFunctionAllocation, AChainedTableKeepsItsFunction)
168 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
170 const size_t failures = fail_every_allocation<LhashTable<int>>(
172 [] (
auto & c) { c.set_hash_fct(new_hash); },
173 [] (
auto & c,
int k) {
return c.search(
k) !=
nullptr; });
174 EXPECT_GE(failures, 1u);
177 TEST(HashFunctionAllocation, AnOpenTableKeepsItsFunctions)
180 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
182 auto insert = [] (
auto & c,
int k) { (void) c.insert(
k); };
183 auto holds = [] (
auto & c,
int k) {
return c.search(
k) !=
nullptr; };
185 insert, [] (
auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
187 insert, [] (
auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
189 insert, [] (
auto & c) { c.set_second_hash_fct(new_hash); }, holds), 1u);
192 TEST(HashFunctionAllocation, AMapKeepsItsFunction)
195 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
197 auto insert = [] (
auto & c,
int k) { (void) c.insert(
k, 10 *
k); };
198 auto holds = [] (
auto & c,
int k)
200 const auto * p = c.search(
k);
201 return p !=
nullptr and p->second == 10 *
k;
205 EXPECT_GE(fail_every_allocation<Map>(
206 insert, [] (
auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
207 EXPECT_GE(fail_every_allocation<Map>(
208 insert, [] (
auto & c) { c.set_hash_fct(new_pair_hash); }, holds), 1u);
209 EXPECT_GE(fail_every_allocation<Open_Map>(
210 insert, [] (
auto & c) { c.set_hash_fct(new_pair_hash); }, holds), 1u);
218 TEST(HashFunctionAllocation, AChainedTableAboveItsLoadKeepsItsKeys)
221 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
224 auto filled = [] (
auto & c,
int k,
auto add)
226 c.set_with_resize(
false);
228 c.set_with_resize(
true);
230 auto insert = [&filled] (
auto & c,
int k)
234 auto holds = [] (
auto & c,
int k) {
return c.search(
k) !=
nullptr; };
236 EXPECT_GE(fail_every_allocation<Table>(
237 insert, [] (
auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
238 EXPECT_GE(fail_every_allocation<Table>(
239 insert, [] (
auto & c) { (void) c.resize(53); }, holds), 1u);
241 EXPECT_GE(fail_every_allocation<Table>(
242 insert, [&
next] (
auto & c)
246 if (c.insert(bucket) ==
nullptr)
250 auto set_insert = [&filled] (
auto & c,
int k)
252 filled(c,
k, [] (
auto & s,
int key) { (void) s.insert(key); });
255 set_insert, [] (
auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
257 auto map_insert = [&filled] (
auto & c,
int k)
259 filled(c,
k, [] (
auto &
m,
int key) { (void)
m.
insert(key, 10 * key); });
262 EXPECT_GE(fail_every_allocation<Map>(
263 map_insert, [] (
auto & c) { c.set_hash_fct(new_hash); }, holds), 1u);
269 TEST(HashFunctionAllocation, SetHashFctAfterAFailedGrowthKeepsItsKeys)
272 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
275 const size_t len = table.capacity();
277 for (
bool failed =
false; not failed; ++
keys)
280 const bool grows = (
keys + 1.0) / len >= table.get_upper_alpha();
281 const Fail_At failing(grows ? 1 : 1000000);
284 ASSERT_NE(table.insert(bucket),
nullptr);
286 catch (
const std::bad_alloc &)
290 ASSERT_EQ(table.capacity(), len);
292 ASSERT_GE(table.current_alpha(), table.get_upper_alpha());
295 for (
size_t n = 1; n < 100; ++n)
297 SCOPED_TRACE(
"the allocation " + std::to_string(n) +
" fails");
300 const Fail_At failing(n);
303 table.set_hash_fct(new_hash);
305 catch (
const std::bad_alloc &)
310 EXPECT_EQ(table.size(),
static_cast<size_t>(
keys));
312 ASSERT_NE(table.search(
k),
nullptr) <<
"key " <<
k;
317 EXPECT_EQ(failures, 1u) <<
"set_hash_fct() made more than the allocation of its lists";
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
Self-adjusting dynamic hash table.
Generic linear hash table.
Open addressing hash table with double hashing collision resolution.
Open addressing hash table with linear probing collision resolution.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
double add(double a, double b)
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
Open addressing hash map using linear probing.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
Hash tables with collision resolution by separate chaining.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.