]> mj.ucw.cz Git - libucw.git/blob - lib/hash-test.c
Added bitsig_free().
[libucw.git] / lib / hash-test.c
1 /* Tests for hash table routines */
2
3 #include "lib/lib.h"
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #if 1
9
10 /* TEST 1: integers */
11
12 struct node {
13   int key;
14   int data;
15 };
16
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
21
22 #define HASH_GIVE_INIT_DATA
23 static inline void test_init_data(struct node *n)
24 {
25   n->data = n->key + 123;
26 }
27
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
33
34 #include "lib/hashtable.h"
35
36 static void test(void)
37 {
38   int i;
39
40   test_init();
41   for (i=0; i<1024; i++)
42     {
43       struct node *n = test_lookup(i);
44       ASSERT(n->data == i+123);
45     }
46   for (i=1; i<1024; i+=2)
47     {
48 #if 0
49       test_delete(i);
50 #else
51       struct node *n = test_lookup(i);
52       test_remove(n);
53 #endif
54     }
55   for (i=0; i<1024; i++)
56     {
57       struct node *n = test_find(i);
58       if (!n != (i&1) || (n && n->data != i+123))
59         die("Inconsistency at i=%d", i);
60     }
61   i=0;
62   HASH_FOR_ALL(test, n)
63     {
64       i += 1 + 0*n->key;
65       // printf("%d\n", n->key);
66     }
67   HASH_END_FOR;
68   ASSERT(i == 512);
69   log(L_INFO, "OK");
70 }
71
72 #elif 0
73
74 /* TEST 2: external strings */
75
76 struct node {
77   char *key;
78   int data;
79 };
80
81 #define HASH_NODE struct node
82 #define HASH_PREFIX(x) test_##x
83 #define HASH_KEY_STRING key
84 #define HASH_NOCASE
85
86 #define HASH_WANT_FIND
87 #define HASH_WANT_NEW
88
89 #include "lib/hashtable.h"
90
91 static void test(void)
92 {
93   int i;
94
95   test_init();
96   for (i=0; i<1024; i+=2)
97     {
98       char x[32];
99       sprintf(x, "abc%d", i);
100       test_new(stralloc(x));
101     }
102   for (i=0; i<1024; i++)
103     {
104       char x[32];
105       struct node *n;
106       sprintf(x, "ABC%d", i);
107       n = test_find(x);
108       if (!n != (i&1))
109         die("Inconsistency at i=%d", i);
110     }
111   log(L_INFO, "OK");
112 }
113
114 #elif 0
115
116 /* TEST 3: internal strings + pools */
117
118 #include "lib/pools.h"
119
120 static struct mempool *pool;
121
122 struct node {
123   int data;
124   char key[1];
125 };
126
127 #define HASH_NODE struct node
128 #define HASH_PREFIX(x) test_##x
129 #define HASH_KEY_ENDSTRING key
130
131 #define HASH_WANT_FIND
132 #define HASH_WANT_NEW
133
134 #define HASH_USE_POOL pool
135
136 #include "lib/hashtable.h"
137
138 static void test(void)
139 {
140   int i;
141
142   pool = mp_new(16384);
143   test_init();
144   for (i=0; i<1024; i+=2)
145     {
146       char x[32];
147       sprintf(x, "abc%d", i);
148       test_new(x);
149     }
150   for (i=0; i<1024; i++)
151     {
152       char x[32];
153       struct node *n;
154       sprintf(x, "abc%d", i);
155       n = test_find(x);
156       if (!n != (i&1))
157         die("Inconsistency at i=%d", i);
158     }
159   log(L_INFO, "OK");
160 }
161
162 #elif 1
163
164 /* TEST 4: complex keys */
165
166 #include "lib/hashfunc.h"
167
168 struct node {
169   int port;
170   int data;
171   char host[1];
172 };
173
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
178
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
185
186 #define HASH_GIVE_HASHFN
187 static uns test_hash(char *host, int port)
188 {
189   return hash_string_nocase(host) ^ hash_int(port);
190 }
191
192 #define HASH_GIVE_EQ
193 static inline int test_eq(char *host1, int port1, char *host2, int port2)
194 {
195   return !strcasecmp(host1,host2) && port1 == port2;
196 }
197
198 #define HASH_GIVE_EXTRA_SIZE
199 static inline uns test_extra_size(char *host, int port UNUSED)
200 {
201   return strlen(host);
202 }
203
204 #define HASH_GIVE_INIT_KEY
205 static inline void test_init_key(struct node *n, char *host, int port)
206 {
207   strcpy(n->host, host);
208   n->port = port;
209 }
210
211 #include "lib/hashtable.h"
212
213 static void test(void)
214 {
215   int i;
216
217   test_init();
218   for (i=0; i<1024; i+=2)
219     {
220       char x[32];
221       sprintf(x, "abc%d", i);
222       test_new(x, i%10);
223     }
224   for (i=0; i<1024; i++)
225     {
226       char x[32];
227       struct node *n;
228       sprintf(x, "ABC%d", i);
229       n = test_find(x, i%10);
230       if (!n != (i&1))
231         die("Inconsistency at i=%d", i);
232     }
233   log(L_INFO, "OK");
234   test_cleanup();
235   log(L_INFO, "Cleaned up");
236 }
237
238 #endif
239
240 int
241 main(void)
242 {
243   test();
244   return 0;
245 }