Tests: Test non-trivial re-hashing in HashTable

This caused a system-wide crash because of a previous bug relating to
non-trivial types in HashTable. Therefore, check that such types
actually work under various workloads.
This commit is contained in:
kleines Filmröllchen 2022-03-08 00:43:21 +01:00 committed by Andreas Kling
parent 49d29c8298
commit 8dc24d0256
Notes: sideshowbarker 2024-07-17 16:26:08 +09:00

View file

@ -7,6 +7,7 @@
#include <LibTest/TestCase.h>
#include <AK/HashTable.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/String.h>
TEST_CASE(construct)
@ -235,6 +236,26 @@ TEST_CASE(capacity_leak)
EXPECT(table.capacity() < 100u);
}
TEST_CASE(non_trivial_type_table)
{
HashTable<NonnullOwnPtr<int>> table;
table.set(make<int>(3));
table.set(make<int>(11));
for (int i = 0; i < 1'000; ++i) {
table.set(make<int>(-i));
}
for (int i = 0; i < 10'000; ++i) {
table.set(make<int>(i));
table.remove(make<int>(i));
}
EXPECT_EQ(table.remove_all_matching([&](auto&) { return true; }), true);
EXPECT(table.is_empty());
EXPECT_EQ(table.remove_all_matching([&](auto&) { return true; }), false);
}
// Inserting and removing a bunch of elements will "thrash" the table, leading to a lot of "deleted" markers.
BENCHMARK_CASE(benchmark_thrashing)
{