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
86 #define HASH_WANT_FIND
89 #include "lib/hashtable.h"
91 static void test(void)
96 for (i=0; i<1024; i+=2)
99 sprintf(x, "abc%d", i);
100 test_new(stralloc(x));
102 for (i=0; i<1024; i++)
106 sprintf(x, "ABC%d", i);
109 die("Inconsistency at i=%d", i);
116 /* TEST 3: internal strings + pools */
118 #include "lib/pools.h"
120 static struct mempool *pool;
127 #define HASH_NODE struct node
128 #define HASH_PREFIX(x) test_##x
129 #define HASH_KEY_ENDSTRING key
131 #define HASH_WANT_FIND
132 #define HASH_WANT_NEW
134 #define HASH_USE_POOL pool
136 #include "lib/hashtable.h"
138 static void test(void)
142 pool = mp_new(16384);
144 for (i=0; i<1048576; i+=2)
147 sprintf(x, "abc%d", i);
150 for (i=0; i<1048576; i++)
154 sprintf(x, "abc%d", i);
157 die("Inconsistency at i=%d", i);
164 /* TEST 4: complex keys */
166 #include "lib/hashfunc.h"
174 #define HASH_NODE struct node
175 #define HASH_PREFIX(x) test_##x
176 #define HASH_KEY_COMPLEX(x) x host, x port
177 #define HASH_KEY_DECL char *host, int port
179 #define HASH_WANT_CLEANUP
180 #define HASH_WANT_FIND
181 #define HASH_WANT_NEW
182 #define HASH_WANT_LOOKUP
183 #define HASH_WANT_DELETE
184 #define HASH_WANT_REMOVE
186 #define HASH_GIVE_HASHFN
187 static uns test_hash(char *host, int port)
189 return hash_string_nocase(host) ^ hash_int(port);
193 static inline int test_eq(char *host1, int port1, char *host2, int port2)
195 return !strcasecmp(host1,host2) && port1 == port2;
198 #define HASH_GIVE_EXTRA_SIZE
199 static inline uns test_extra_size(char *host, int port UNUSED)
204 #define HASH_GIVE_INIT_KEY
205 static inline void test_init_key(struct node *n, char *host, int port)
207 strcpy(n->host, host);
211 #include "lib/hashtable.h"
213 static void test(void)
218 for (i=0; i<1024; i+=2)
221 sprintf(x, "abc%d", i);
224 for (i=0; i<1024; i++)
228 sprintf(x, "ABC%d", i);
229 n = test_find(x, i%10);
231 die("Inconsistency at i=%d", i);
235 log(L_INFO, "Cleaned up");