-
Notifications
You must be signed in to change notification settings - Fork 10
/
SlotMapTest02.cpp
104 lines (85 loc) · 2.89 KB
/
SlotMapTest02.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <array>
#include <gtest/gtest.h>
#include <slot_map.h>
#include <unordered_set>
// Note: You could skip slow tests by using "SlotMapTest --gtest_filter=-SlotMapTest.*_Slow"
TEST(SlotMapTest, IdCollisions_Slow)
{
static const int kNumberOfIds = 1024;
std::vector<dod::slot_map<int>::key> ids;
ids.resize(kNumberOfIds);
dod::slot_map<int> slotMap;
for (size_t i = 0; i < ids.size(); i++)
{
ids[i] = slotMap.emplace(int(113 + i));
}
for (size_t i = 0; i < ids.size(); i++)
{
EXPECT_TRUE(slotMap.has_key(ids[i]));
}
for (size_t i = 0; i < ids.size(); i++)
{
slotMap.erase(ids[i]);
}
std::unordered_set<dod::slot_map<int>::key> idsToTest;
for (size_t i = 0; i < ids.size(); i++)
{
idsToTest.emplace(ids[i]);
EXPECT_FALSE(slotMap.has_key(ids[i]));
}
EXPECT_EQ(idsToTest.size(), ids.size());
EXPECT_EQ(slotMap.size(), 0u);
#ifdef _DEBUG
static const int kNumberOfIterations = 300000;
#else
static const int kNumberOfIterations = 5000000;
#endif
static const int kNumberOfTempIds = 1024;
std::array<dod::slot_map<int>::key, kNumberOfTempIds> tempIds;
size_t numAllocationsChecked = 0;
for (size_t iter = 1; iter <= kNumberOfIterations; iter++)
{
if ((iter % 8839) == 0 || (iter + 1) == kNumberOfIterations || (iter == 1))
{
printf("Iteration: %d (%3.2f%%) \r", int(iter), 100.0f * double(iter) / double(kNumberOfIterations));
}
for (size_t j = 0; j < kNumberOfTempIds; j++)
{
numAllocationsChecked++;
auto id = slotMap.emplace(int(j + 7));
tempIds[j] = id;
// check for collisions
auto it = idsToTest.find(id);
ASSERT_EQ(it, idsToTest.end());
}
for (size_t j = 0; j < kNumberOfTempIds; j++)
{
EXPECT_TRUE(slotMap.has_key(tempIds[j]));
slotMap.erase(tempIds[j]);
}
}
printf("\n");
printf("Num allocations: %d\n", int(numAllocationsChecked));
slotMap.emplace(33);
EXPECT_EQ(slotMap.size(), 1u);
for (size_t i = 0; i < ids.size(); i++)
{
EXPECT_FALSE(slotMap.has_key(ids[i]));
}
for (size_t i = 0; i < ids.size(); i++)
{
EXPECT_EQ(slotMap.get(ids[i]), nullptr);
}
for (size_t i = 0; i < ids.size(); i++)
{
std::optional<int> r = slotMap.pop(ids[i]);
EXPECT_EQ(r.has_value(), false);
}
auto stats = slotMap.debug_stats();
printf("Pages: total: %d, active: %d, inactive: %d\n", int(stats.numPagesTotal), int(stats.numActivePages),
int(stats.numInactivePages));
printf("Items: total: %d, alive: %d, tombstone: %d, inactive: %d\n", int(stats.numItemsTotal), int(stats.numAliveItems),
int(stats.numTombstoneItems), int(stats.numInactiveItems));
slotMap.clear();
slotMap.reset();
}