-
Notifications
You must be signed in to change notification settings - Fork 1
/
robinhoodhashtest.cpp
118 lines (98 loc) · 3.54 KB
/
robinhoodhashtest.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <set>
#include "robinhoodhash.h"
#define TS 33
uint32_t hashtable[TS] = {0};
#define mytable_setvalue(index, key, val) hashtable[index] = val;
#define mytable_setnil(index) hashtable[index] = 0;
#define mytable_nilvalue 0
#define mytable_getvalue(index) hashtable[index]
#define mytable_getkey(index) hashtable[index]
#define mytable_keysequal(key1, key2) key1 == key2
#define mytable_isnil(index) (hashtable[index]==0)
#define mytable_n_elem TS
#define mytable_getbucket(key) (size_t)((key-1)%(TS-1) + 1)
#define mytable_overflow printf("\nX HT full!\n");
#define mytable_removefailed(key) //printf("\nX Remove failed %u!\n", key);
#define mytable_swap(index1, index2) { \
uint32_t tmp = hashtable[index1]; \
hashtable[index1] = hashtable[index2]; \
hashtable[index2] = tmp; \
}
std::set<uint32_t> theset;
int main() {
//srand((unsigned)time(0));
srand(23);
int timetocheck = 0;
for(;;) {
int op = (unsigned)rand() % 10;
uint32_t val = (unsigned)rand() % (TS*5) + 1;
//printf("size=%d\n", theset.size());
if (op == 0) {
if (theset.size() >= TS-1) continue;
uint32_t assgn = 0xFFFF;
ROBINHOOD_HASH_GET(mytable, val, assgn);
if (assgn != (theset.count(val) ? val : 0)) {
printf("\nX Before insert element: %u\n", assgn);
}
// set
printf(".");
ROBINHOOD_HASH_SET(mytable, val, val);
ROBINHOOD_HASH_GET(mytable, val, assgn);
if (assgn != val) {
printf("\nX After insert element: %u instead of %u\n", assgn, val);
}
theset.insert(val);
} else
if (op >= 1) {
// del
//if (theset.count(val) == 0) continue;
uint32_t assgn = 0xFFFF;
ROBINHOOD_HASH_GET(mytable, val, assgn);
if (assgn != (theset.count(val) ? val : 0)) {
printf("\nX Before removal element: %u instead of %u\n", assgn, val);
}
if (theset.count(val)) printf("-");
ROBINHOOD_HASH_DEL(mytable, val);
theset.erase(val);
ROBINHOOD_HASH_GET(mytable, val, assgn);
if (assgn != 0) {
printf("\nX After removal element: %u\n", assgn);
}
}
//_ROBINHOOD_HASH_DEBUGPRINT;
if (timetocheck<=0) {
// check entire table for consistency
for(uint32_t i : theset) {
uint32_t assgn = 44444;
ROBINHOOD_HASH_GET(mytable, i, assgn);
if (assgn == i) {
// OK
} else
if (assgn == 0) {
printf("\nX Missing element: %u\n", i);
} else {
printf("\nX Error assgn=%u instead of %u\n", assgn, i);
}
}
for(uint32_t i = 1; i<TS; ++i) {
if (theset.count(i) > 0) continue;
uint32_t assgn;
ROBINHOOD_HASH_GET(mytable, i, assgn);
if (assgn == 0) {
// OK
} else {
printf("\nX Extra element: %u %u\n", i, assgn);
}
}
timetocheck = 5000;
} else {
timetocheck -= 1;
}
}
return 0;
}