Adding back libhashkit.
[m6w6/libmemcached] / tests / hashkit_functions.c
1 /* libHashKit Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include <libhashkit/hashkit.h>
15
16 #include "test.h"
17
18 #include "hash_results.h"
19
20 static hashkit_st global_hashk;
21
22 /**
23 @brief hash_test_st is a structure we use in testing. It is currently empty.
24 */
25 typedef struct hash_test_st hash_test_st;
26
27 struct hash_test_st
28 {
29 bool _unused;
30 };
31
32 static test_return_t init_test(void *not_used __attribute__((unused)))
33 {
34 hashkit_st hashk;
35 hashkit_st *hashk_ptr;
36
37 hashk_ptr= hashkit_create(&hashk);
38 test_truth(hashk_ptr);
39 test_truth(hashk_ptr == &hashk);
40 test_truth(hashkit_is_initialized(&hashk) == true);
41 test_truth(hashkit_is_allocated(hashk_ptr) == false);
42
43 hashkit_free(hashk_ptr);
44
45 test_truth(hashkit_is_initialized(&hashk) == false);
46
47 return TEST_SUCCESS;
48 }
49
50 static test_return_t allocation_test(void *not_used __attribute__((unused)))
51 {
52 hashkit_st *hashk_ptr;
53
54 hashk_ptr= hashkit_create(NULL);
55 test_truth(hashk_ptr);
56 test_truth(hashkit_is_allocated(hashk_ptr) == true);
57 test_truth(hashkit_is_initialized(hashk_ptr) == true);
58 hashkit_free(hashk_ptr);
59
60 return TEST_SUCCESS;
61 }
62
63 static test_return_t clone_test(hashkit_st *hashk)
64 {
65 // First we make sure that the testing system is giving us what we expect.
66 assert(&global_hashk == hashk);
67
68 // Second we test if hashk is even valid
69 test_truth(hashkit_is_initialized(hashk) == true);
70
71 /* All null? */
72 {
73 hashkit_st *hashk_ptr;
74 hashk_ptr= hashkit_clone(NULL, NULL);
75 test_truth(hashk_ptr);
76 test_truth(hashkit_is_allocated(hashk_ptr) == true);
77 test_truth(hashkit_is_initialized(hashk_ptr) == true);
78 hashkit_free(hashk_ptr);
79 }
80
81 /* Can we init from null? */
82 {
83 hashkit_st *hashk_ptr;
84
85 hashk_ptr= hashkit_clone(NULL, hashk);
86
87 test_truth(hashk_ptr);
88 test_truth(hashkit_is_allocated(hashk_ptr) == true);
89 test_truth(hashkit_is_initialized(hashk_ptr) == true);
90
91 test_truth(hashk_ptr->distribution == hashk->distribution);
92 test_truth(hashk_ptr->continuum_count == hashk->continuum_count);
93 test_truth(hashk_ptr->continuum_points_count == hashk->continuum_points_count);
94 test_truth(hashk_ptr->list_size == hashk->list_size);
95 test_truth(hashk_ptr->context_size == hashk->context_size);
96 test_truth(hashk_ptr->continuum == NULL);
97 test_truth(hashk_ptr->hash_fn == hashk->hash_fn);
98 test_truth(hashk_ptr->active_fn == hashk->active_fn);
99 test_truth(hashk_ptr->continuum_hash_fn == hashk->continuum_hash_fn);
100 test_truth(hashk_ptr->continuum_key_fn == hashk->continuum_key_fn);
101 test_truth(hashk_ptr->sort_fn == hashk->sort_fn);
102 test_truth(hashk_ptr->weight_fn == hashk->weight_fn);
103 test_truth(hashk_ptr->list == hashk->list);
104
105 hashkit_free(hashk_ptr);
106 }
107
108 /* Can we init from struct? */
109 {
110 hashkit_st declared_clone;
111 hashkit_st *hash_clone;
112
113 hash_clone= hashkit_clone(&declared_clone, NULL);
114 test_truth(hash_clone);
115
116 hashkit_free(hash_clone);
117 }
118
119 /* Can we init from struct? */
120 {
121 hashkit_st declared_clone;
122 hashkit_st *hash_clone;
123 memset(&declared_clone, 0 , sizeof(hashkit_st));
124 hash_clone= hashkit_clone(&declared_clone, hashk);
125 test_truth(hash_clone);
126 hashkit_free(hash_clone);
127 }
128
129 return TEST_SUCCESS;
130 }
131
132
133 static test_return_t md5_run (hashkit_st *hashk __attribute__((unused)))
134 {
135 uint32_t x;
136 const char **ptr;
137
138 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
139 {
140 uint32_t hash_val;
141
142 hash_val= hashkit_md5(*ptr, strlen(*ptr));
143 test_truth(md5_values[x] == hash_val);
144 }
145
146 return TEST_SUCCESS;
147 }
148
149 static test_return_t crc_run (hashkit_st *hashk __attribute__((unused)))
150 {
151 uint32_t x;
152 const char **ptr;
153
154 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
155 {
156 uint32_t hash_val;
157
158 hash_val= hashkit_crc32(*ptr, strlen(*ptr));
159 assert(crc_values[x] == hash_val);
160 }
161
162 return TEST_SUCCESS;
163 }
164
165 static test_return_t fnv1_64_run (hashkit_st *hashk __attribute__((unused)))
166 {
167 uint32_t x;
168 const char **ptr;
169
170 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
171 {
172 uint32_t hash_val;
173
174 hash_val= hashkit_fnv1_64(*ptr, strlen(*ptr));
175 assert(fnv1_64_values[x] == hash_val);
176 }
177
178 return TEST_SUCCESS;
179 }
180
181 static test_return_t fnv1a_64_run (hashkit_st *hashk __attribute__((unused)))
182 {
183 uint32_t x;
184 const char **ptr;
185
186 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
187 {
188 uint32_t hash_val;
189
190 hash_val= hashkit_fnv1a_64(*ptr, strlen(*ptr));
191 assert(fnv1a_64_values[x] == hash_val);
192 }
193
194 return TEST_SUCCESS;
195 }
196
197 static test_return_t fnv1_32_run (hashkit_st *hashk __attribute__((unused)))
198 {
199 uint32_t x;
200 const char **ptr;
201
202
203 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
204 {
205 uint32_t hash_val;
206
207 hash_val= hashkit_fnv1_32(*ptr, strlen(*ptr));
208 assert(fnv1_32_values[x] == hash_val);
209 }
210
211 return TEST_SUCCESS;
212 }
213
214 static test_return_t fnv1a_32_run (hashkit_st *hashk __attribute__((unused)))
215 {
216 uint32_t x;
217 const char **ptr;
218
219 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
220 {
221 uint32_t hash_val;
222
223 hash_val= hashkit_fnv1a_32(*ptr, strlen(*ptr));
224 assert(fnv1a_32_values[x] == hash_val);
225 }
226
227 return TEST_SUCCESS;
228 }
229
230 static test_return_t hsieh_run (hashkit_st *hashk __attribute__((unused)))
231 {
232 uint32_t x;
233 const char **ptr;
234
235 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
236 {
237 uint32_t hash_val;
238
239 #ifdef HAVE_HSIEH_HASH
240 hash_val= hashkit_hsieh(*ptr, strlen(*ptr));
241 #else
242 hash_val= 1;
243 #endif
244 assert(hsieh_values[x] == hash_val);
245 }
246
247 return TEST_SUCCESS;
248 }
249
250 static test_return_t murmur_run (hashkit_st *hashk __attribute__((unused)))
251 {
252 uint32_t x;
253 const char **ptr;
254
255 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
256 {
257 uint32_t hash_val;
258
259 hash_val= hashkit_murmur(*ptr, strlen(*ptr));
260 assert(murmur_values[x] == hash_val);
261 }
262
263 return TEST_SUCCESS;
264 }
265
266 static test_return_t jenkins_run (hashkit_st *hashk __attribute__((unused)))
267 {
268 uint32_t x;
269 const char **ptr;
270
271
272 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
273 {
274 uint32_t hash_val;
275
276 hash_val= hashkit_jenkins(*ptr, strlen(*ptr));
277 assert(jenkins_values[x] == hash_val);
278 }
279
280 return TEST_SUCCESS;
281 }
282
283
284
285
286 /**
287 @brief now we list out the tests.
288 */
289
290 test_st allocation[]= {
291 {"init", 0, (test_callback_fn)init_test},
292 {"create and free", 0, (test_callback_fn)allocation_test},
293 {"clone", 0, (test_callback_fn)clone_test},
294 {0, 0, 0}
295 };
296
297 test_st hash_tests[] ={
298 {"md5", 0, (test_callback_fn)md5_run },
299 {"crc", 0, (test_callback_fn)crc_run },
300 {"fnv1_64", 0, (test_callback_fn)fnv1_64_run },
301 {"fnv1a_64", 0, (test_callback_fn)fnv1a_64_run },
302 {"fnv1_32", 0, (test_callback_fn)fnv1_32_run },
303 {"fnv1a_32", 0, (test_callback_fn)fnv1a_32_run },
304 {"hsieh", 0, (test_callback_fn)hsieh_run },
305 {"murmur", 0, (test_callback_fn)murmur_run },
306 {"jenkis", 0, (test_callback_fn)jenkins_run },
307 {0, 0, (test_callback_fn)0}
308 };
309
310 /*
311 * The following test suite is used to verify that we don't introduce
312 * regression bugs. If you want more information about the bug / test,
313 * you should look in the bug report at
314 * http://bugs.launchpad.net/libmemcached
315 */
316 test_st regression[]= {
317 {0, 0, 0}
318 };
319
320 collection_st collection[] ={
321 {"allocation", 0, 0, allocation},
322 {"regression", 0, 0, regression},
323 {"hashing", 0, 0, hash_tests},
324 {0, 0, 0, 0}
325 };
326
327 /* Prototypes for functions we will pass to test framework */
328 void *world_create(void);
329 test_return_t world_destroy(hashkit_st *hashk);
330
331 void *world_create(void)
332 {
333 hashkit_st *hashk_ptr;
334
335 hashk_ptr= hashkit_create(&global_hashk);
336
337 assert(hashk_ptr == &global_hashk);
338
339 // First we test if hashk is even valid
340 assert(hashkit_is_initialized(hashk_ptr) == true);
341 assert(hashkit_is_allocated(hashk_ptr) == false);
342 assert(hashk_ptr->continuum == NULL);
343
344 return hashk_ptr;
345 }
346
347
348 test_return_t world_destroy(hashkit_st *hashk)
349 {
350 // Did we get back what we expected?
351 assert(hashkit_is_initialized(hashk) == true);
352 assert(hashkit_is_allocated(hashk) == false);
353 hashkit_free(&global_hashk);
354
355 return TEST_SUCCESS;
356 }
357
358 void get_world(world_st *world)
359 {
360 world->collections= collection;
361 world->create= (test_callback_create_fn)world_create;
362 world->destroy= (test_callback_fn)world_destroy;
363 }