/
hash_table_test.cc
206 lines (162 loc) · 4.63 KB
/
hash_table_test.cc
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <signal.h>
#include <assert.h>
#include <algorithm>
#include <ep.hh>
#include <item.hh>
extern "C" {
static rel_time_t basic_current_time(void) {
return 0;
}
rel_time_t (*ep_current_time)() = basic_current_time;
}
class Counter : public HashTableVisitor {
public:
int count;
Counter() : count() {}
void visit(StoredValue *v) {
count += 1;
std::string key = v->getKey();
value_t val = v->getValue();
// Value has length beyond the value that's requested to be stored.
std::string subval = val->substr(0, val->length());
assert(key.compare(subval) == 0);
}
};
static int count(HashTable &h) {
Counter c;
h.visit(c);
return c.count;
}
static void store(HashTable &h, std::string &k) {
Item i(k, 0, 0, k.c_str(), k.length());
h.set(i);
}
static void storeMany(HashTable &h, std::vector<std::string> &keys) {
std::vector<std::string>::iterator it;
for (it = keys.begin(); it != keys.end(); it++) {
std::string key = *it;
store(h, key);
}
}
static void addMany(HashTable &h, std::vector<std::string> &keys, bool expect) {
std::vector<std::string>::iterator it;
for (it = keys.begin(); it != keys.end(); it++) {
std::string k = *it;
Item i(k, 0, 0, k.c_str(), k.length());
bool v = h.add(i);
assert(expect == v);
}
}
static std::vector<std::string> generateKeys(int num, int start=0) {
std::vector<std::string> rv;
for (int i = start; i < num; i++) {
char buf[64];
snprintf(buf, sizeof(buf), "key%d", i);
std::string key(buf);
rv.push_back(key);
}
return rv;
}
// ----------------------------------------------------------------------
// Actual tests below.
// ----------------------------------------------------------------------
static void testHashSize() {
HashTable h;
assert(count(h) == 0);
std::string k = "testkey";
store(h, k);
assert(count(h) == 1);
}
static void testHashSizeTwo() {
HashTable h;
assert(count(h) == 0);
std::vector<std::string> keys = generateKeys(5);
storeMany(h, keys);
assert(count(h) == 5);
h.clear();
assert(count(h) == 0);
}
static void testReverseDeletions() {
alarm(10);
HashTable h(5, 1);
assert(count(h) == 0);
const int nkeys = 10000;
std::vector<std::string> keys = generateKeys(nkeys);
storeMany(h, keys);
assert(count(h) == nkeys);
std::reverse(keys.begin(), keys.end());
std::vector<std::string>::iterator it;
for (it = keys.begin(); it != keys.end(); it++) {
std::string key = *it;
h.del(key);
}
assert(count(h) == 0);
}
static void testForwardDeletions() {
alarm(10);
HashTable h(5, 1);
assert(h.getSize() == 5);
assert(h.getNumLocks() == 1);
assert(count(h) == 0);
const int nkeys = 10000;
std::vector<std::string> keys = generateKeys(nkeys);
storeMany(h, keys);
assert(count(h) == nkeys);
std::vector<std::string>::iterator it;
for (it = keys.begin(); it != keys.end(); it++) {
std::string key = *it;
h.del(key);
}
assert(count(h) == 0);
}
static void testFind() {
HashTable h(5, 1);
const int nkeys = 5000;
std::vector<std::string> keys = generateKeys(nkeys);
storeMany(h, keys);
std::string missingKey = "aMissingKey";
assert(h.find(missingKey) == NULL);
std::vector<std::string>::iterator it;
for (it = keys.begin(); it != keys.end(); it++) {
std::string key = *it;
assert(h.find(key));
}
}
static void testAdd() {
HashTable h(5, 1);
const int nkeys = 5000;
std::vector<std::string> keys = generateKeys(nkeys);
addMany(h, keys, true);
std::string missingKey = "aMissingKey";
assert(h.find(missingKey) == NULL);
std::vector<std::string>::iterator it;
for (it = keys.begin(); it != keys.end(); it++) {
std::string key = *it;
assert(h.find(key));
}
addMany(h, keys, false);
for (it = keys.begin(); it != keys.end(); it++) {
std::string key = *it;
assert(h.find(key));
}
}
static void testDepthCounting() {
HashTable h(5, 1);
const int nkeys = 5000;
std::vector<std::string> keys = generateKeys(nkeys);
storeMany(h, keys);
HashTableDepthStatVisitor depthCounter;
h.visitDepth(depthCounter);
// std::cout << "Max depth: " << depthCounter.maxDepth << std::endl;
assert(depthCounter.max > 1000);
}
int main() {
testHashSize();
testHashSizeTwo();
testReverseDeletions();
testForwardDeletions();
testFind();
testAdd();
testDepthCounting();
exit(0);
}