]> mj.ucw.cz Git - libucw.git/blob - lib/hash-test.c
CF_USAGE_TAB can be used to insert more tabs to the default help message.
[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 0
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 1
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 #define HASH_AUTO_POOL 4096
86
87 #define HASH_WANT_FIND
88 #define HASH_WANT_NEW
89
90 #include "lib/hashtable.h"
91
92 static void test(void)
93 {
94   int i;
95
96   test_init();
97   for (i=0; i<1024; i+=2)
98     {
99       char x[32];
100       sprintf(x, "abc%d", i);
101       test_new(stralloc(x));
102     }
103   for (i=0; i<1024; i++)
104     {
105       char x[32];
106       struct node *n;
107       sprintf(x, "ABC%d", i);
108       n = test_find(x);
109       if (!n != (i&1))
110         die("Inconsistency at i=%d", i);
111     }
112   log(L_INFO, "OK");
113 }
114
115 #elif 1
116
117 /* TEST 3: internal strings + pools */
118
119 #include "lib/pools.h"
120
121 static struct mempool *pool;
122
123 struct node {
124   int data;
125   char key[1];
126 };
127
128 #define HASH_NODE struct node
129 #define HASH_PREFIX(x) test_##x
130 #define HASH_KEY_ENDSTRING key
131
132 #define HASH_WANT_FIND
133 #define HASH_WANT_NEW
134
135 #define HASH_USE_POOL pool
136
137 #include "lib/hashtable.h"
138
139 static void test(void)
140 {
141   int i;
142
143   pool = mp_new(16384);
144   test_init();
145   for (i=0; i<1048576; i+=2)
146     {
147       char x[32];
148       sprintf(x, "abc%d", i);
149       test_new(x);
150     }
151   for (i=0; i<1048576; i++)
152     {
153       char x[32];
154       struct node *n;
155       sprintf(x, "abc%d", i);
156       n = test_find(x);
157       if (!n != (i&1))
158         die("Inconsistency at i=%d", i);
159     }
160   log(L_INFO, "OK");
161 }
162
163 #elif 1
164
165 /* TEST 4: complex keys */
166
167 #include "lib/hashfunc.h"
168
169 struct node {
170   int port;
171   int data;
172   char host[1];
173 };
174
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
179
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
186
187 #define HASH_GIVE_HASHFN
188 static uns test_hash(char *host, int port)
189 {
190   return hash_string_nocase(host) ^ hash_int(port);
191 }
192
193 #define HASH_GIVE_EQ
194 static inline int test_eq(char *host1, int port1, char *host2, int port2)
195 {
196   return !strcasecmp(host1,host2) && port1 == port2;
197 }
198
199 #define HASH_GIVE_EXTRA_SIZE
200 static inline uns test_extra_size(char *host, int port UNUSED)
201 {
202   return strlen(host);
203 }
204
205 #define HASH_GIVE_INIT_KEY
206 static inline void test_init_key(struct node *n, char *host, int port)
207 {
208   strcpy(n->host, host);
209   n->port = port;
210 }
211
212 #include "lib/hashtable.h"
213
214 static void test(void)
215 {
216   int i;
217
218   test_init();
219   for (i=0; i<1024; i+=2)
220     {
221       char x[32];
222       sprintf(x, "abc%d", i);
223       test_new(x, i%10);
224     }
225   for (i=0; i<1024; i++)
226     {
227       char x[32];
228       struct node *n;
229       sprintf(x, "ABC%d", i);
230       n = test_find(x, i%10);
231       if (!n != (i&1))
232         die("Inconsistency at i=%d", i);
233     }
234   log(L_INFO, "OK");
235   test_cleanup();
236   log(L_INFO, "Cleaned up");
237 }
238
239 #endif
240
241 int
242 main(void)
243 {
244   test();
245   return 0;
246 }