41#include <gtest/gtest.h>
66 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
73 template <
class Table>
size_t busy_lists(
const Table & table)
75 const auto stats = table.
stats();
76 return table.
capacity() - (stats.lens.size() > 0 ? stats.lens(0) : 0);
88 std::vector<Bucket *> buckets;
89 for (
int k = 0;
k < 1000; ++
k)
91 buckets.push_back(
new Bucket(
k));
92 ASSERT_NE(table.
insert(buckets.back()),
nullptr) <<
"key " <<
k;
94 const size_t grown = table.
capacity();
99 for (
int k = 0;
k < 990; ++
k)
100 delete table.
remove(buckets[
k]);
101 refused = refused_allocations;
104 EXPECT_GT(refused, 0u);
106 EXPECT_EQ(table.
size(), 10u);
108 for (
int k = 990;
k < 1000; ++
k)
109 EXPECT_EQ(table.
search(
k), buckets[
k]) <<
"key " <<
k;
112 delete table.
remove(buckets[990]);
114 for (
int k = 991;
k < 1000; ++
k)
116 EXPECT_EQ(table.
search(
k), buckets[
k]) <<
"key " <<
k;
117 delete table.
remove(buckets[
k]);
132 std::vector<Bucket *> buckets;
133 for (
int k = 0;
k < 20; ++
k)
135 buckets.push_back(
new Bucket(
k));
136 ASSERT_NE(table.
insert(buckets.back()),
nullptr) <<
"key " <<
k;
139 const size_t before = allocations;
140 for (
int k = 0;
k < 10; ++
k)
141 delete table.
remove(buckets[
k]);
142 EXPECT_EQ(allocations, before);
145 for (
int k = 10;
k < 20; ++
k)
146 delete table.
remove(buckets[
k]);
157 0.85f, 0.9f,
true,
true);
159 std::vector<Bucket *> buckets;
160 for (
int k = 0;
k < 22; ++
k)
162 buckets.push_back(
new Bucket(
k));
163 ASSERT_NE(table.
insert(buckets.back()),
nullptr) <<
"key " <<
k;
166 const size_t before = allocations;
167 delete table.
remove(buckets[0]);
168 EXPECT_EQ(allocations, before);
171 delete table.
remove(buckets[1]);
174 for (
int k = 2;
k < 22; ++
k)
175 delete table.
remove(buckets[
k]);
182 if constexpr (
requires (
Table & t) { t.get_second_hash_fct(); })
183 return Table(11, Aleph::dft_hash_ptr_fct<long>,
198 TypeParam table = this->make();
199 for (
long k = 0;
k < 1000; ++
k)
200 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
201 const size_t grown = table.capacity();
203 size_t refused = 0, threw = 0;
206 for (
long k = 0;
k < 990; ++
k)
212 table.remove_ptr(table.search(
k));
218 refused = refused_allocations;
221 EXPECT_EQ(threw, 0u);
222 EXPECT_GT(refused, 0u);
223 EXPECT_EQ(table.capacity(), grown);
224 EXPECT_EQ(table.size(), 10u);
225 for (
long k = 0;
k < 1000; ++
k)
226 EXPECT_EQ(table.contains(
k),
k >= 990) <<
"key " <<
k;
229 EXPECT_LT(table.capacity(), grown);
230 for (
long k = 991;
k < 1000; ++
k)
231 EXPECT_TRUE(table.contains(
k)) <<
"key " <<
k;
236 size_t identity_hash(
const size_t &
k)
noexcept
256 constexpr size_t len = 8193;
260 std::vector<Bucket *> fillers;
261 for (
size_t t = 0; table.
capacity() <= len + 4097; ++t)
262 for (
size_t r = 0;
r < 4096
and table.
capacity() <= len + 4097; ++
r)
264 fillers.push_back(
new Bucket(
r + len * t));
265 ASSERT_NE(table.
insert(fillers.back()),
nullptr);
267 auto * special =
new Bucket(4096 + len);
268 ASSERT_NE(table.
insert(special),
nullptr);
269 const size_t expanded = table.
capacity();
270 ASSERT_GT(expanded, len + 4097);
272 size_t refused = 0, removed = 0;
275 for (; removed < fillers.size() - 100; ++removed)
276 delete table.
remove(fillers[removed]);
277 refused = refused_allocations;
280 EXPECT_GT(refused, 0u);
281 EXPECT_GE(table.
capacity(), len + 4097);
282 EXPECT_EQ(table.
size(), 101u);
283 EXPECT_EQ(table.
busy_slots(), busy_lists(table));
284 EXPECT_EQ(table.
search(4096 + len), special);
285 for (
size_t i = removed; i < fillers.size(); ++i)
286 EXPECT_EQ(table.
search(fillers[i]->get_key()), fillers[i]) << i;
289 delete table.
remove(fillers[removed++]);
290 EXPECT_LT(table.
capacity(), len + 4097);
291 EXPECT_EQ(table.
busy_slots(), busy_lists(table));
292 EXPECT_EQ(table.
search(4096 + len), special);
293 for (
size_t i = removed; i < fillers.size(); ++i)
294 EXPECT_EQ(table.
search(fillers[i]->get_key()), fillers[i]) << i;
303 static inline long alive = 0;
306 Live_Key() noexcept { ++alive; }
307 Live_Key(
const int v) noexcept :
value(v) { ++alive; }
308 Live_Key(
const Live_Key &
k) noexcept :
value(
k.value) { ++alive; }
309 Live_Key & operator = (
const Live_Key &)
noexcept =
default;
310 ~Live_Key() { --alive; }
317 return static_cast<size_t>(
k.value) * 40503u;
324 void expect_a_failed_first_insertion_to_leak_nothing(Set & moved_from,
const size_t failing)
326 const long alive = Live_Key::alive;
327 const size_t capacity = moved_from.capacity();
329 const Fail_At fail_at(failing);
330 EXPECT_THROW((
void) moved_from.insert(Live_Key(7)), std::bad_alloc);
332 EXPECT_EQ(Live_Key::alive, alive);
333 EXPECT_TRUE(moved_from.is_empty());
334 EXPECT_EQ(moved_from.capacity(), capacity);
335 EXPECT_FALSE(moved_from.contains(Live_Key(7)));
337 ASSERT_NE(moved_from.insert(Live_Key(7)),
nullptr);
338 EXPECT_EQ(Live_Key::alive, alive + 1);
339 EXPECT_TRUE(moved_from.contains(Live_Key(7)));
350 for (
int i = 0; i < 20; ++i)
351 ASSERT_NE(source.insert(Live_Key(i)),
nullptr);
353 ASSERT_EQ(source.capacity(), 0u);
357 expect_a_failed_first_insertion_to_leak_nothing(source, 2);
359 EXPECT_EQ(target.size(), 20u);
364 const long before = Live_Key::alive;
367 for (
int i = 0; i < 20; ++i)
368 source.insert(i, Live_Key(i));
370 ASSERT_EQ(source.capacity(), 0u);
372 const long alive = Live_Key::alive;
374 const Fail_At fail_at(2);
375 EXPECT_THROW((
void) source.insert(7, Live_Key(7)), std::bad_alloc);
377 EXPECT_EQ(Live_Key::alive, alive);
378 EXPECT_TRUE(source.is_empty());
379 EXPECT_EQ(source.search(7),
nullptr);
381 source.insert(7, Live_Key(70));
382 EXPECT_EQ(source.find(7).value, 70);
383 EXPECT_EQ(target.size(), 20u);
385 EXPECT_EQ(Live_Key::alive, before);
390 const long before = Live_Key::alive;
393 for (
int i = 0; i < 20; ++i)
394 ASSERT_NE(source.insert(i, Live_Key(i)),
nullptr);
396 ASSERT_EQ(source.search(3),
nullptr);
398 const long alive = Live_Key::alive;
400 const Fail_At fail_at(2);
401 EXPECT_THROW((
void) source.insert(7, Live_Key(7)), std::bad_alloc);
403 EXPECT_EQ(Live_Key::alive, alive);
405 const Fail_At fail_at(2);
406 EXPECT_THROW(source[8] = Live_Key(8), std::bad_alloc);
408 EXPECT_EQ(Live_Key::alive, alive);
409 EXPECT_EQ(source.search(7),
nullptr);
410 EXPECT_EQ(source.search(8),
nullptr);
412 ASSERT_NE(source.insert(7, Live_Key(70)),
nullptr);
413 source[8] = Live_Key(80);
414 ASSERT_NE(source.search(7),
nullptr);
415 EXPECT_EQ(source.search(7)->value, 70);
416 ASSERT_NE(source.search(8),
nullptr);
417 EXPECT_EQ(source.search(8)->value, 80);
418 ASSERT_NE(target.
search(3),
nullptr);
420 EXPECT_EQ(Live_Key::alive, before);
428 const long before = Live_Key::alive;
431 const long alive = Live_Key::alive;
435 for (
size_t failing = 2; failing < 6
and not threw; ++failing)
437 const Fail_At fail_at(failing);
440 (void) set.
insert(Live_Key(7));
442 catch (
const std::bad_alloc &)
448 ASSERT_TRUE(set.
contains(Live_Key(7)));
454 EXPECT_EQ(Live_Key::alive, alive);
456 EXPECT_FALSE(set.
contains(Live_Key(7)));
458 ASSERT_NE(set.
insert(Live_Key(7)),
nullptr);
459 EXPECT_TRUE(set.
contains(Live_Key(7)));
461 EXPECT_EQ(Live_Key::alive, before);
469 for (
int i = 0; i < 20; ++i)
470 ASSERT_NE(source.insert(Live_Key(i)),
nullptr);
472 ASSERT_EQ(source.capacity(), 0u);
474 const long alive = Live_Key::alive;
476 const Fail_At fail_at(2);
477 EXPECT_THROW((
void) source.search_or_insert(Live_Key(7)), std::bad_alloc);
479 EXPECT_EQ(Live_Key::alive, alive);
481 const Fail_At fail_at(2);
482 EXPECT_THROW((
void) source.contains_or_insert(Live_Key(7)), std::bad_alloc);
484 EXPECT_EQ(Live_Key::alive, alive);
485 EXPECT_TRUE(source.is_empty());
486 EXPECT_EQ(source.capacity(), 0u);
488 ASSERT_NE(source.search_or_insert(Live_Key(7)),
nullptr);
489 EXPECT_TRUE(source.contains(Live_Key(7)));
490 EXPECT_EQ(Live_Key::alive, alive + 1);
502 const size_t capacity = probe.capacity();
503 while (probe.capacity() == capacity)
504 ASSERT_NE(probe.
insert(Live_Key(growing++)),
nullptr);
506 ASSERT_GT(growing, 1);
508 const long before = Live_Key::alive;
511 for (
int i = 0; i < growing - 1; ++i)
512 ASSERT_NE(set.
insert(Live_Key(i)),
nullptr);
513 const size_t capacity = set.capacity();
514 const long alive = Live_Key::alive;
517 const Fail_At fail_at(2);
518 EXPECT_THROW((
void) set.
insert(Live_Key(growing - 1)), std::bad_alloc);
520 EXPECT_EQ(set.capacity(), capacity);
521 EXPECT_EQ(set.size(),
static_cast<size_t>(growing));
522 EXPECT_TRUE(set.
contains(Live_Key(growing - 1)));
523 EXPECT_EQ(Live_Key::alive, alive + 1);
524 for (
int i = 0; i < growing; ++i)
525 ASSERT_TRUE(set.
contains(Live_Key(i))) << i;
527 EXPECT_EQ(Live_Key::alive, before);
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
size_t size_t int32_t value
bool contains(const Key &key) const noexcept
Return true if the key exists in the table.
void remove(Key *key)
Removes a key from the hash table using its pointer.
Key * insert(const Key &key)
Inserts key into the hash set.
bool is_empty() const noexcept
Checks if the container is empty.
Dynamic hash table mapping keys to records with separate chaining.
Record * search(const Key &key)
Search for a key in the table.
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table.
Bucket * insert(Bucket *bucket)
Inserts bucket into the table and returns its address if the key is not already in the table; otherwi...
const size_t & get_num_busy_slots() const noexcept
Returns the number of occupied entries in the array.
const size_t & size() const noexcept
Returns the number of elements contained in the table.
const size_t & capacity() const noexcept
Returns the table capacity.
Bucket * search(const Key &key) const noexcept
Search in the table for a bucket with key.
constexpr bool is_empty() const noexcept
Generic linear hash table.
const size_t & capacity() const noexcept
Returns the table capacity.
const size_t & size() const noexcept
Returns the number of elements in the table.
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table, without freeing it.
BucketType< Key > Bucket
The bucket type.
const size_t & busy_slots() const noexcept
Returns the number of busy slots in the table.
Bucket * insert(Bucket *bucket)
Insert bucket in the table.
Bucket * search(const Key &key) const noexcept
Search for key in the linear hash table.
Open addressing hash table with double hashing collision resolution.
Stats stats() const
Computes statistics about chain length distribution.
TEST_F(HashTableWithoutMemory, ChainedRemovalKeepsTheCapacity)
TYPED_TEST(OpenTableWithoutMemory, RemovalsKeepTheCapacity)
TYPED_TEST_SUITE(OpenTableWithoutMemory, Open_Tables)
::testing::Types< OLhashTable< long >, ODhashTable< long > > Open_Tables
LinearHashTable< int > Table
Main namespace for Aleph-w library functions.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
const float hash_default_upper_alpha
size_t aleph_hash_value(const AdversarialTranspositionKey< StateKey > &key) noexcept
and
Check uniqueness with explicit hash + equality functors.
const float hash_default_lower_alpha
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Hash-based dynamic set (defined in tpl_dynSetHash.H).
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
Dynamic hash table mapping keys to records with separate chaining.
Dynamic set implementations based on hash tables.
Hash tables with collision resolution by separate chaining.
Linear hashing with chaining.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.