-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathExcaliburHashTest07.cpp
More file actions
87 lines (76 loc) · 2.67 KB
/
ExcaliburHashTest07.cpp
File metadata and controls
87 lines (76 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "ExcaliburHash.h"
#include <chrono>
#include <gtest/gtest.h>
#include <thread>
using namespace Excalibur;
// Test case for the infinite loop bug when hash table contains only tombstones
TEST(ExcaliburHashInfiniteLoopTest, EmplaceIntoTableWithOnlyTombstones)
{
HashMap<int32_t, int32_t, 1> hashMap;
hashMap.reserve(16);
// Let's first understand the exact growth behavior by testing incrementally
EXPECT_EQ(hashMap.capacity(), 16); // Should start with minimum capacity
// "poison" hash_map - filling it up with tombstones, but always keep one element alive to prevent internal optimizations
hashMap.emplace(-1, -100);
for (int32_t i = 0; i < 256; ++i)
{
if (hashMap.getNumTombstones() == hashMap.capacity() - 1)
{
break;
}
hashMap.emplace(i, i * 10);
hashMap.erase(i);
}
// the hash map is now poisoned... no more free slots left
if (hashMap.getNumTombstones() == hashMap.capacity() - 1)
{
EXPECT_EQ(hashMap.size(), 1);
EXPECT_EQ(hashMap.capacity(), hashMap.getNumTombstones() + 1);
// Set up a timeout to catch infinite loops
std::atomic<bool> completed{false};
std::atomic<bool> timed_out{false};
// Run the potentially infinite operation in a separate thread
std::thread test_thread(
[&]()
{
try
{
// This should trigger the infinite loop bug!
auto result = hashMap.emplace(999, 9999);
EXPECT_TRUE(result.second); // Should be inserted
EXPECT_EQ(result.first.value(), 9999);
completed = true;
}
catch (...)
{
completed = true;
}
});
// Wait for a reasonable time - if it takes longer than 2 seconds,
// we likely have an infinite loop
auto start_time = std::chrono::steady_clock::now();
while (!completed && std::chrono::steady_clock::now() - start_time < std::chrono::seconds(2))
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
if (!completed)
{
timed_out = true;
test_thread.detach();
FAIL() << "Infinite loop detected! emplace() did not complete within 2 seconds. ";
}
else
{
test_thread.join();
if (!timed_out)
{
EXPECT_EQ(hashMap.size(), 1);
EXPECT_TRUE(hashMap.has(999));
}
}
}
else
{
SUCCEED() << "Can't 'poision' hash map";
}
}