1 /* Tests for hash table routines */
4 #include "lib/mempool.h"
16 #define HASH_NODE struct node1
17 #define HASH_PREFIX(x) test1_##x
18 #define HASH_KEY_ATOMIC key
19 #define HASH_ATOMIC_TYPE int
21 #define HASH_GIVE_INIT_DATA
22 static inline void test1_init_data(struct node1 *n)
24 n->data = n->key + 123;
27 #define HASH_WANT_FIND
28 #define HASH_WANT_LOOKUP
29 #define HASH_WANT_REMOVE
31 #include "lib/hashtable.h"
33 static void test1(void)
38 for (i=0; i<1024; i++)
40 struct node1 *n = test1_lookup(i);
41 ASSERT(n->data == i+123);
43 for (i=1; i<1024; i+=2)
45 struct node1 *n = test1_lookup(i);
48 for (i=0; i<1024; i++)
50 struct node1 *n = test1_find(i);
51 if (!n != (i&1) || (n && n->data != i+123))
52 die("Inconsistency at i=%d", i);
55 HASH_FOR_ALL(test1, n)
64 /* TEST 2: external strings */
71 #define HASH_NODE struct node2
72 #define HASH_PREFIX(x) test2_##x
73 #define HASH_KEY_STRING key
75 #define HASH_AUTO_POOL 4096
77 #define HASH_WANT_FIND
80 #include "lib/hashtable.h"
82 static void test2(void)
87 for (i=0; i<1024; i+=2)
90 sprintf(x, "abc%d", i);
91 test2_new(xstrdup(x));
93 for (i=0; i<1024; i++)
97 sprintf(x, "ABC%d", i);
100 die("Inconsistency at i=%d", i);
105 /* TEST 3: internal strings + pools */
107 static struct mempool *pool3;
114 #define HASH_NODE struct node3
115 #define HASH_PREFIX(x) test3_##x
116 #define HASH_KEY_ENDSTRING key
118 #define HASH_WANT_FIND
119 #define HASH_WANT_NEW
121 #define HASH_USE_POOL pool3
123 #include "lib/hashtable.h"
125 static void test3(void)
129 pool3 = mp_new(16384);
131 for (i=0; i<1048576; i+=2)
134 sprintf(x, "abc%d", i);
137 for (i=0; i<1048576; i++)
141 sprintf(x, "abc%d", i);
144 die("Inconsistency at i=%d", i);
149 /* TEST 4: complex keys */
151 #include "lib/hashfunc.h"
159 #define HASH_NODE struct node4
160 #define HASH_PREFIX(x) test4_##x
161 #define HASH_KEY_COMPLEX(x) x host, x port
162 #define HASH_KEY_DECL char *host, int port
164 #define HASH_WANT_CLEANUP
165 #define HASH_WANT_FIND
166 #define HASH_WANT_NEW
167 #define HASH_WANT_LOOKUP
168 #define HASH_WANT_DELETE
169 #define HASH_WANT_REMOVE
171 #define HASH_GIVE_HASHFN
172 static uns test4_hash(char *host, int port)
174 return hash_string_nocase(host) ^ hash_int(port);
178 static inline int test4_eq(char *host1, int port1, char *host2, int port2)
180 return !strcasecmp(host1,host2) && port1 == port2;
183 #define HASH_GIVE_EXTRA_SIZE
184 static inline uns test4_extra_size(char *host, int port UNUSED)
189 #define HASH_GIVE_INIT_KEY
190 static inline void test4_init_key(struct node4 *n, char *host, int port)
192 strcpy(n->host, host);
196 #include "lib/hashtable.h"
198 static void test4(void)
205 for (i=0; i<1024; i++)
208 sprintf(x, "abc%d", i);
209 n = test4_new(x, i%10);
212 for (i=0; i<1024; i++)
214 sprintf(x, "abc%d", i);
215 n = test4_lookup(x, i%10);
218 for (i=0; i<1024; i++)
221 sprintf(x, "aBc%d", i);
224 n = test4_find(x, i%10);
229 test4_delete(x, i%10);
231 for (i=0; i<1024; i++)
233 sprintf(x, "ABC%d", i);
234 n = test4_find(x, i%10);
235 if (!n != (i&1) || (n && n->data != i))
236 die("Inconsistency at i=%d", i);
242 /* TEST 5: integers again, but this time dynamically */
249 #define HASH_NODE struct node5
250 #define HASH_PREFIX(x) test5_##x
251 #define HASH_KEY_ATOMIC key
252 #define HASH_ATOMIC_TYPE int
253 #define HASH_TABLE_DYNAMIC
257 #define HASH_GIVE_INIT_DATA
258 static inline void test5_init_data(struct test5_table *table UNUSED, struct node5 *n)
260 n->data = n->key + 123;
263 #define HASH_WANT_FIND
264 #define HASH_WANT_NEW
265 #define HASH_WANT_DELETE
267 #include "lib/hashtable.h"
269 static void test5(void)
272 struct test5_table tab;
275 for (i=0; i<1024; i++)
277 struct node5 *n = test5_new(&tab, i);
278 ASSERT(n->data == i+123);
280 for (i=1; i<1024; i+=2)
281 test5_delete(&tab, i);
282 for (i=0; i<1024; i++)
284 struct node5 *n = test5_find(&tab, i);
285 if (!n != (i&1) || (n && n->data != i+123))
286 die("Inconsistency at i=%d", i);
289 HASH_FOR_ALL_DYNAMIC(test5, &tab, n)
297 main(int argc, char **argv)
303 for (int i=1; i<argc; i++)
304 m |= 1 << atol(argv[i]);