forked from urbit/vere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtable_tests.c
246 lines (198 loc) · 4.54 KB
/
hashtable_tests.c
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/// @file
#include "noun.h"
// defined in noun/hashtable.c
c3_w _ch_skip_slot(c3_w mug_w, c3_w lef_w);
/* _setup(): prepare for tests.
*/
static void
_setup(void)
{
u3m_init(1 << 26);
u3m_pave(c3y);
}
/* _test_bit_manipulation():
*/
static c3_i
_test_bit_manipulation()
{
c3_i ret_i = 1;
if ( sizeof(u3_noun) != sizeof(u3h_slot) ) {
fprintf(stderr, "bit manipulation: wrong size\r\n");
ret_i = 0;
}
u3h_slot a = 0;
if (u3h_slot_is_null(a) != c3y) {
fprintf(stderr, "bit manipulation: nullity\r\n");
ret_i = 0;
}
a = u3h_noun_be_warm(a);
if (u3h_slot_is_warm(a) != c3y) {
fprintf(stderr, "bit manipulation: warmth\r\n");
ret_i = 0;
}
if (u3h_slot_is_null(a) != c3n) {
fprintf(stderr, "bit manipulation: nullity 2\r\n");
ret_i = 0;
}
a = u3h_noun_be_cold(a);
if (u3h_slot_is_warm(a) != c3n) {
fprintf(stderr, "bit manipulation: coldness\r\n");
ret_i = 0;
}
return ret_i;
}
/* _test_no_cache(): test a hashtable without caching.
*/
static c3_i
_test_no_cache(void)
{
c3_i ret_i = 1;
c3_w max_w = 1000;
c3_w i_w;
u3p(u3h_root) har_p = u3h_new();
for ( i_w = 0; i_w < max_w; i_w++ ) {
u3h_put(har_p, i_w, i_w + max_w);
}
for ( i_w = 0; i_w < max_w; i_w++ ) {
if ( (i_w + max_w) != u3h_get(har_p, i_w) ) {
fprintf(stderr, "bit test_no_cache: get failed\r\n");
ret_i = 0;
}
}
u3h_free(har_p);
return ret_i;
}
/* _test_skip_slot():
*/
static c3_i
_test_skip_slot(void)
{
c3_i ret_i = 1;
// root table
{
c3_w mug_w = 0x17 << 25;
c3_w res_w = _ch_skip_slot(mug_w, 25);
if ( (0x18 << 25) != res_w ) {
fprintf(stderr, "bit skip_slot (a): failed\r\n");
ret_i = 0;
}
}
{
c3_w mug_w = 63 << 25; // 6 bits, all ones
c3_w res_w = _ch_skip_slot(mug_w, 25);
if ( 0 != res_w ) {
fprintf(stderr, "bit skip_slot (b): failed\r\n");
ret_i = 0;
}
}
// child nodes
{
c3_w mug_w = 17 << 20;
c3_w res_w = _ch_skip_slot(mug_w, 20);
if ( (18 << 20) != res_w ) {
fprintf(stderr, "bit skip_slot (c): failed\r\n");
ret_i = 0;
}
}
{
c3_w mug_w = 31 << 20; // 5 bits, all ones
c3_w res_w = _ch_skip_slot(mug_w, 20);
u3_assert((1 << 25) == res_w);
if ( (1 << 25) != res_w ) {
fprintf(stderr, "bit skip_slot (d): failed\r\n");
ret_i = 0;
}
}
return ret_i;
}
/* _test_cache_trimming(): ensure a caching hashtable removes stale items.
*/
static c3_i
_test_cache_trimming(void)
{
c3_i ret_i = 1;
c3_w max_w = 2000000; // big number
//c3_w max_w = 348000; // caused a leak before
c3_w i_w, fil_w = max_w / 10;
u3p(u3h_root) har_p = u3h_new_cache(fil_w);
u3h_root* har_u = u3to(u3h_root, har_p);
for ( i_w = 0; i_w < max_w; i_w++ ) {
u3_noun cel = u3nc(i_w, i_w);
u3h_put(har_p, cel, cel);
}
{
// last thing we put in is still there
c3_w las_w = max_w - 1;
u3_noun key = u3nc(las_w, las_w);
u3_noun val = u3h_get(har_p, key);
u3z(key);
if ( las_w != u3t(val) ) {
fprintf(stderr, "cache_trimming (a): fail\r\n");
ret_i = 0;
}
if ( fil_w != har_u->use_w ) {
fprintf(stderr, "cache_trimming (b): fail %d != %d\r\n",
fil_w, har_u->use_w );
ret_i = 0;
}
u3z(val);
}
u3h_free(har_p);
return ret_i;
}
/* _test_cache_replace_value():
*/
static c3_i
_test_cache_replace_value(void)
{
c3_i ret_i = 1;
c3_w max_w = 100;
c3_w i_w;
u3p(u3h_root) har_p = u3h_new_cache(max_w);
u3h_root* har_u = u3to(u3h_root, har_p);
for ( i_w = 0; i_w < max_w; i_w++ ) {
u3h_put(har_p, i_w, i_w + max_w);
}
for ( i_w = 0; i_w < max_w; i_w++ ) {
u3h_put(har_p, i_w, i_w + max_w + 1);
}
if ( (2 * max_w) != u3h_get(har_p, max_w - 1) ) {
fprintf(stderr, "cache_replace (a): fail\r\n");
ret_i = 0;
}
if ( max_w != har_u->use_w ) {
fprintf(stderr, "cache_replace (b): fail\r\n");
fprintf(stderr, "cache_replace (b): fail %d %d\r\n",
max_w, har_u->use_w );
ret_i = 0;
}
u3h_free(har_p);
return ret_i;
}
static c3_i
_test_hashtable(void)
{
c3_i ret_i = 1;
ret_i &= _test_bit_manipulation();
ret_i &= _test_no_cache();
ret_i &= _test_skip_slot();
ret_i &= _test_cache_trimming();
ret_i &= _test_cache_replace_value();
return ret_i;
}
/* main(): run all test cases.
*/
int
main(int argc, char* argv[])
{
_setup();
if ( !_test_hashtable() ) {
fprintf(stderr, "test_hashtable: failed\r\n");
exit(1);
}
// GC
//
u3m_grab(u3_none);
fprintf(stderr, "test_hashtable: ok\r\n");
return 0;
}