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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
* BIRD Library -- Hash Tests
*
* (c) 2015 CZ.NIC z.s.p.o.
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#undef LOCAL_DEBUG
#include "test/birdtest.h"
#include "test/bt-utils.h"
#include "lib/io-loop.h"
#include "lib/hash.h"
struct test_node {
struct test_node *next; /* Hash chain */
u32 key;
};
#define TEST_KEY(n) n->key
#define TEST_NEXT(n) n->next
#define TEST_EQ(n1,n2) n1 == n2
#define TEST_FN(n) (n) ^ u32_hash((n))
#define TEST_ORDER 13
#define TEST_PARAMS /TEST_ORDER, *2, 2, 2, TEST_ORDER, 20
#define TEST_REHASH test_rehash
HASH_DEFINE_REHASH_FN(TEST, struct test_node);
HASH(struct test_node) hash;
struct pool *my_pool;
#define MAX_NUM (1 << TEST_ORDER)
struct test_node nodes[MAX_NUM];
static void
print_rate_of_fulfilment(void)
{
int i;
int num_stacked_items = 0;
for (i = 0; i < MAX_NUM; i++)
if (!hash.data[i])
num_stacked_items++;
double percent_stacked_items = ((double)num_stacked_items/(double)MAX_NUM)*100.;
bt_debug("%d (%.2f %%) chained of %d hashes \n", num_stacked_items, percent_stacked_items, MAX_NUM);
}
#ifdef LOCAL_DEBUG
static void
dump_nodes(void)
{
int i;
for (i = 0; i < MAX_NUM; i++)
bt_debug("nodes[%3d] is at address %14p has .key %3d, .next %14p \n", i, &nodes[i], nodes[i].key, nodes[i].next);
}
#endif
static void
init_hash_(uint order)
{
my_pool = rp_new(&root_pool, &main_birdloop, "Test pool");
HASH_INIT(hash, my_pool, order);
int i;
for (i = 0; i < MAX_NUM; i++)
{
nodes[i].key = i;
nodes[i].next = NULL;
}
bt_debug("MAX_NUM %d \n", MAX_NUM);
}
static void
init_hash(void)
{
init_hash_(TEST_ORDER);
}
static void
validate_filled_hash(void)
{
int i;
struct test_node *node;
for (i = 0; i < MAX_NUM; i++)
{
node = HASH_FIND(hash, TEST, nodes[i].key);
bt_assert_msg(node->key == nodes[i].key, "Hash should be filled, to find (%p) the node[%d] (%p) with .key = %u, .next %p", node, i, &nodes[i], nodes[i].key, nodes[i].next);
}
print_rate_of_fulfilment();
}
static void
validate_empty_hash(void)
{
int i;
struct test_node *node;
for (i = 0; i < MAX_NUM; i++)
{
node = HASH_FIND(hash, TEST, nodes[i].key);
bt_assert_msg(node == NULL, "Hash should be empty, to find (%p) the node[%d] (%p) with .key %u, .next %p", node, i, &nodes[i], nodes[i].key, nodes[i].next);
}
}
static void
fill_hash(void)
{
int i;
struct test_node *node;
for (i = 0; i < MAX_NUM; i++)
{
nodes[i].key = i;
node = &nodes[i];
HASH_INSERT(hash, TEST, node);
}
}
static int
t_insert_find(void)
{
init_hash();
fill_hash();
validate_filled_hash();
return 1;
}
static int
t_insert_find_random(void)
{
init_hash();
int i;
struct test_node *node;
for (i = 0; i < MAX_NUM; i++)
{
nodes[i].key = bt_random();
node = &nodes[i];
HASH_INSERT(hash, TEST, node);
}
validate_filled_hash();
return 1;
}
static int
t_insert2_find(void)
{
init_hash_(1);
int i;
struct test_node *node;
for (i = 0; i < MAX_NUM; i++)
{
nodes[i].key = i;
node = &nodes[i];
HASH_INSERT2(hash, TEST, my_pool, node);
}
bt_assert_msg(hash.order != 1, "The hash should auto-resize from order 2^1. The order of the hash is 2^%u.", hash.order);
validate_filled_hash();
return 1;
}
static int
t_walk(void)
{
init_hash();
fill_hash();
uint i;
uint check[MAX_NUM];
for (i = 0; i < MAX_NUM; i++)
check[i] = 0;
HASH_WALK(hash, next, n)
{
check[n->key]++;
}
HASH_WALK_END;
for (i = 0; i < MAX_NUM; i++)
bt_assert(check[i] == 1);
return 1;
}
static int
t_walk_delsafe_delete(void)
{
init_hash();
fill_hash();
HASH_WALK_DELSAFE(hash, next, n)
{
HASH_DELETE(hash, TEST, n->key);
}
HASH_WALK_DELSAFE_END;
validate_empty_hash();
return 1;
}
static int
t_walk_delsafe_remove(void)
{
init_hash();
fill_hash();
HASH_WALK_DELSAFE(hash, next, n)
{
HASH_REMOVE(hash, TEST, n);
}
HASH_WALK_DELSAFE_END;
validate_empty_hash();
return 1;
}
static int
t_walk_delsafe_delete2(void)
{
init_hash();
fill_hash();
HASH_WALK_DELSAFE(hash, next, n)
{
HASH_DELETE2(hash, TEST, my_pool, n->key);
}
HASH_WALK_DELSAFE_END;
validate_empty_hash();
return 1;
}
static int
t_walk_delsafe_remove2(void)
{
init_hash();
fill_hash();
HASH_WALK_DELSAFE(hash, next, n)
{
HASH_REMOVE2(hash, TEST, my_pool, n);
}
HASH_WALK_DELSAFE_END;
validate_empty_hash();
return 1;
}
static int
t_walk_filter(void)
{
init_hash();
fill_hash();
uint i;
uint check[MAX_NUM];
for (i = 0; i < MAX_NUM; i++)
check[i] = 0;
HASH_WALK_FILTER(hash, next, n, m)
{
bt_assert(n == *m);
check[n->key]++;
}
HASH_WALK_FILTER_END;
for (i = 0; i < MAX_NUM; i++)
bt_assert(check[i] == 1);
return 1;
}
int
main(int argc, char *argv[])
{
bt_init(argc, argv);
bt_bird_init();
bt_test_suite(t_insert_find, "HASH_INSERT and HASH_FIND");
bt_test_suite(t_insert_find_random, "HASH_INSERT pseudo-random keys and HASH_FIND");
bt_test_suite(t_insert2_find, "HASH_INSERT2 and HASH_FIND. HASH_INSERT2 is HASH_INSERT and a smart auto-resize function");
bt_test_suite(t_walk, "HASH_WALK");
bt_test_suite(t_walk_delsafe_delete, "HASH_WALK_DELSAFE and HASH_DELETE");
bt_test_suite(t_walk_delsafe_delete2, "HASH_WALK_DELSAFE and HASH_DELETE2. HASH_DELETE2 is HASH_DELETE and smart auto-resize function");
bt_test_suite(t_walk_delsafe_remove, "HASH_WALK_DELSAFE and HASH_REMOVE");
bt_test_suite(t_walk_delsafe_remove2, "HASH_WALK_DELSAFE and HASH_REMOVE2. HASH_REMOVE2 is HASH_REMOVE and smart auto-resize function");
bt_test_suite(t_walk_filter, "HASH_WALK_FILTER");
return bt_exit_value();
}
|