1 /* Tests for hash table routines */
10 /* TEST 1: integers */
17 #define HASH_NODE struct node
18 #define HASH_PREFIX(x) test_##x
19 #define HASH_KEY_ATOMIC key
20 #define HASH_ATOMIC_TYPE int
22 #define HASH_GIVE_INIT_DATA
23 static inline void test_init_data(struct node *n)
25 n->data = n->key + 123;
28 #define HASH_WANT_FIND
29 //#define HASH_WANT_NEW
30 #define HASH_WANT_LOOKUP
31 //#define HASH_WANT_DELETE
32 #define HASH_WANT_REMOVE
34 #include "lib/hashtable.h"
36 static void test(void)
41 for (i=0; i<1024; i++)
43 struct node *n = test_lookup(i);
44 ASSERT(n->data == i+123);
46 for (i=1; i<1024; i+=2)
51 struct node *n = test_lookup(i);
55 for (i=0; i<1024; i++)
57 struct node *n = test_find(i);
58 if (!n != (i&1) || (n && n->data != i+123))
59 die("Inconsistency at i=%d", i);
65 // printf("%d\n", n->key);
74 /* TEST 2: external strings */
81 #define HASH_NODE struct node
82 #define HASH_PREFIX(x) test_##x
83 #define HASH_KEY_STRING key
85 #define HASH_AUTO_POOL 4096
87 #define HASH_WANT_FIND
90 #include "lib/hashtable.h"
92 static void test(void)
97 for (i=0; i<1024; i+=2)
100 sprintf(x, "abc%d", i);
101 test_new(stralloc(x));
103 for (i=0; i<1024; i++)
107 sprintf(x, "ABC%d", i);
110 die("Inconsistency at i=%d", i);
117 /* TEST 3: internal strings + pools */
119 #include "lib/pools.h"
121 static struct mempool *pool;
128 #define HASH_NODE struct node
129 #define HASH_PREFIX(x) test_##x
130 #define HASH_KEY_ENDSTRING key
132 #define HASH_WANT_FIND
133 #define HASH_WANT_NEW
135 #define HASH_USE_POOL pool
137 #include "lib/hashtable.h"
139 static void test(void)
143 pool = mp_new(16384);
145 for (i=0; i<1048576; i+=2)
148 sprintf(x, "abc%d", i);
151 for (i=0; i<1048576; i++)
155 sprintf(x, "abc%d", i);
158 die("Inconsistency at i=%d", i);
165 /* TEST 4: complex keys */
167 #include "lib/hashfunc.h"
175 #define HASH_NODE struct node
176 #define HASH_PREFIX(x) test_##x
177 #define HASH_KEY_COMPLEX(x) x host, x port
178 #define HASH_KEY_DECL char *host, int port
180 #define HASH_WANT_CLEANUP
181 #define HASH_WANT_FIND
182 #define HASH_WANT_NEW
183 #define HASH_WANT_LOOKUP
184 #define HASH_WANT_DELETE
185 #define HASH_WANT_REMOVE
187 #define HASH_GIVE_HASHFN
188 static uns test_hash(char *host, int port)
190 return hash_string_nocase(host) ^ hash_int(port);
194 static inline int test_eq(char *host1, int port1, char *host2, int port2)
196 return !strcasecmp(host1,host2) && port1 == port2;
199 #define HASH_GIVE_EXTRA_SIZE
200 static inline uns test_extra_size(char *host, int port UNUSED)
205 #define HASH_GIVE_INIT_KEY
206 static inline void test_init_key(struct node *n, char *host, int port)
208 strcpy(n->host, host);
212 #include "lib/hashtable.h"
214 static void test(void)
219 for (i=0; i<1024; i+=2)
222 sprintf(x, "abc%d", i);
225 for (i=0; i<1024; i++)
229 sprintf(x, "ABC%d", i);
230 n = test_find(x, i%10);
232 die("Inconsistency at i=%d", i);
236 log(L_INFO, "Cleaned up");