2dad25fa95ed2838b5e9833ed0c800b386d958a1
[awesomized/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_true(hashk_ptr);
39 test_true(hashk_ptr == &hashk);
40 test_true(hashkit_is_allocated(hashk_ptr) == false);
41
42 hashkit_free(hashk_ptr);
43
44 return TEST_SUCCESS;
45 }
46
47 static test_return_t allocation_test(void *not_used __attribute__((unused)))
48 {
49 hashkit_st *hashk_ptr;
50
51 hashk_ptr= hashkit_create(NULL);
52 test_true(hashk_ptr);
53 test_true(hashkit_is_allocated(hashk_ptr) == true);
54 hashkit_free(hashk_ptr);
55
56 return TEST_SUCCESS;
57 }
58
59 static test_return_t clone_test(hashkit_st *hashk)
60 {
61 // First we make sure that the testing system is giving us what we expect.
62 assert(&global_hashk == hashk);
63
64 // Second we test if hashk is even valid
65
66 /* All null? */
67 {
68 hashkit_st *hashk_ptr;
69 hashk_ptr= hashkit_clone(NULL, NULL);
70 test_true(hashk_ptr);
71 test_true(hashkit_is_allocated(hashk_ptr));
72 hashkit_free(hashk_ptr);
73 }
74
75 /* Can we init from null? */
76 {
77 hashkit_st *hashk_ptr;
78
79 hashk_ptr= hashkit_clone(NULL, hashk);
80
81 test_true(hashk_ptr);
82 test_true(hashkit_is_allocated(hashk_ptr));
83
84 hashkit_free(hashk_ptr);
85 }
86
87 /* Can we init from struct? */
88 {
89 hashkit_st declared_clone;
90 hashkit_st *hash_clone;
91
92 hash_clone= hashkit_clone(&declared_clone, NULL);
93 test_true(hash_clone);
94 test_true(hash_clone == &declared_clone);
95 test_false(hashkit_is_allocated(hash_clone));
96
97 hashkit_free(hash_clone);
98 }
99
100 /* Can we init from struct? */
101 {
102 hashkit_st declared_clone;
103 hashkit_st *hash_clone;
104
105 hash_clone= hashkit_clone(&declared_clone, hashk);
106 test_true(hash_clone);
107 test_true(hash_clone == &declared_clone);
108 test_false(hashkit_is_allocated(hash_clone));
109
110 hashkit_free(hash_clone);
111 }
112
113 return TEST_SUCCESS;
114 }
115
116 static test_return_t one_at_a_time_run (hashkit_st *hashk __attribute__((unused)))
117 {
118 uint32_t x;
119 const char **ptr;
120
121 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
122 {
123 uint32_t hash_val;
124
125 hash_val= libhashkit_one_at_a_time(*ptr, strlen(*ptr));
126 test_true(one_at_a_time_values[x] == hash_val);
127 }
128
129 return TEST_SUCCESS;
130 }
131
132 static test_return_t md5_run (hashkit_st *hashk __attribute__((unused)))
133 {
134 uint32_t x;
135 const char **ptr;
136
137 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
138 {
139 uint32_t hash_val;
140
141 hash_val= libhashkit_md5(*ptr, strlen(*ptr));
142 test_true(md5_values[x] == hash_val);
143 }
144
145 return TEST_SUCCESS;
146 }
147
148 static test_return_t crc_run (hashkit_st *hashk __attribute__((unused)))
149 {
150 uint32_t x;
151 const char **ptr;
152
153 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
154 {
155 uint32_t hash_val;
156
157 hash_val= libhashkit_crc32(*ptr, strlen(*ptr));
158 assert(crc_values[x] == hash_val);
159 }
160
161 return TEST_SUCCESS;
162 }
163
164 static test_return_t fnv1_64_run (hashkit_st *hashk __attribute__((unused)))
165 {
166 uint32_t x;
167 const char **ptr;
168
169 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
170 {
171 uint32_t hash_val;
172
173 hash_val= libhashkit_fnv1_64(*ptr, strlen(*ptr));
174 assert(fnv1_64_values[x] == hash_val);
175 }
176
177 return TEST_SUCCESS;
178 }
179
180 static test_return_t fnv1a_64_run (hashkit_st *hashk __attribute__((unused)))
181 {
182 uint32_t x;
183 const char **ptr;
184
185 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
186 {
187 uint32_t hash_val;
188
189 hash_val= libhashkit_fnv1a_64(*ptr, strlen(*ptr));
190 assert(fnv1a_64_values[x] == hash_val);
191 }
192
193 return TEST_SUCCESS;
194 }
195
196 static test_return_t fnv1_32_run (hashkit_st *hashk __attribute__((unused)))
197 {
198 uint32_t x;
199 const char **ptr;
200
201
202 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
203 {
204 uint32_t hash_val;
205
206 hash_val= libhashkit_fnv1_32(*ptr, strlen(*ptr));
207 assert(fnv1_32_values[x] == hash_val);
208 }
209
210 return TEST_SUCCESS;
211 }
212
213 static test_return_t fnv1a_32_run (hashkit_st *hashk __attribute__((unused)))
214 {
215 uint32_t x;
216 const char **ptr;
217
218 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
219 {
220 uint32_t hash_val;
221
222 hash_val= libhashkit_fnv1a_32(*ptr, strlen(*ptr));
223 assert(fnv1a_32_values[x] == hash_val);
224 }
225
226 return TEST_SUCCESS;
227 }
228
229 static test_return_t hsieh_run (hashkit_st *hashk __attribute__((unused)))
230 {
231 uint32_t x;
232 const char **ptr;
233
234 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
235 {
236 uint32_t hash_val;
237
238 #ifdef HAVE_HSIEH_HASH
239 hash_val= libhashkit_hsieh(*ptr, strlen(*ptr));
240 #else
241 hash_val= 1;
242 #endif
243 assert(hsieh_values[x] == hash_val);
244 }
245
246 return TEST_SUCCESS;
247 }
248
249 static test_return_t murmur_run (hashkit_st *hashk __attribute__((unused)))
250 {
251 #ifdef __sparc
252 return TEST_SKIPPED;
253 #else
254 uint32_t x;
255 const char **ptr;
256
257 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
258 {
259 uint32_t hash_val;
260
261 hash_val= libhashkit_murmur(*ptr, strlen(*ptr));
262 assert(murmur_values[x] == hash_val);
263 }
264
265 return TEST_SUCCESS;
266 #endif
267 }
268
269 static test_return_t jenkins_run (hashkit_st *hashk __attribute__((unused)))
270 {
271 uint32_t x;
272 const char **ptr;
273
274
275 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
276 {
277 uint32_t hash_val;
278
279 hash_val= libhashkit_jenkins(*ptr, strlen(*ptr));
280 assert(jenkins_values[x] == hash_val);
281 }
282
283 return TEST_SUCCESS;
284 }
285
286
287
288
289 /**
290 @brief now we list out the tests.
291 */
292
293 test_st allocation[]= {
294 {"init", 0, (test_callback_fn)init_test},
295 {"create and free", 0, (test_callback_fn)allocation_test},
296 {"clone", 0, (test_callback_fn)clone_test},
297 {0, 0, 0}
298 };
299
300 static test_return_t hashkit_digest_test(hashkit_st *hashk)
301 {
302 uint32_t value;
303 value= hashkit_digest(hashk, "a", sizeof("a"));
304
305 return TEST_SUCCESS;
306 }
307
308 static test_return_t hashkit_set_function_test(hashkit_st *hashk)
309 {
310 for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++)
311 {
312 hashkit_return_t rc;
313 uint32_t x;
314 const char **ptr;
315 uint32_t *list;
316
317 rc= hashkit_set_function(hashk, algo);
318
319 /* Hsieh is disabled most of the time for patent issues */
320 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH)
321 continue;
322
323 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_CUSTOM)
324 continue;
325
326 test_true(rc == HASHKIT_SUCCESS);
327
328 switch (algo)
329 {
330 case HASHKIT_HASH_DEFAULT:
331 list= one_at_a_time_values;
332 break;
333 case HASHKIT_HASH_MD5:
334 list= md5_values;
335 break;
336 case HASHKIT_HASH_CRC:
337 list= crc_values;
338 break;
339 case HASHKIT_HASH_FNV1_64:
340 list= fnv1_64_values;
341 break;
342 case HASHKIT_HASH_FNV1A_64:
343 list= fnv1a_64_values;
344 break;
345 case HASHKIT_HASH_FNV1_32:
346 list= fnv1_32_values;
347 break;
348 case HASHKIT_HASH_FNV1A_32:
349 list= fnv1a_32_values;
350 break;
351 case HASHKIT_HASH_HSIEH:
352 list= hsieh_values;
353 break;
354 case HASHKIT_HASH_MURMUR:
355 list= murmur_values;
356 break;
357 case HASHKIT_HASH_JENKINS:
358 list= jenkins_values;
359 break;
360 case HASHKIT_HASH_CUSTOM:
361 case HASHKIT_HASH_MAX:
362 default:
363 list= NULL;
364 break;
365 }
366
367 // Now we make sure we did set the hash correctly.
368 if (list)
369 {
370 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
371 {
372 uint32_t hash_val;
373
374 hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr));
375 test_true(list[x] == hash_val);
376 }
377 }
378 else
379 {
380 return TEST_FAILURE;
381 }
382 }
383
384 return TEST_SUCCESS;
385 }
386
387 static uint32_t hash_test_function(const char *string, size_t string_length, void *context)
388 {
389 (void)context;
390 return libhashkit_md5(string, string_length);
391 }
392
393 static test_return_t hashkit_set_custom_function_test(hashkit_st *hashk)
394 {
395 hashkit_return_t rc;
396 uint32_t x;
397 const char **ptr;
398
399
400 rc= hashkit_set_custom_function(hashk, hash_test_function, NULL);
401 test_true(rc == HASHKIT_SUCCESS);
402
403 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
404 {
405 uint32_t hash_val;
406
407 hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr));
408 test_true(md5_values[x] == hash_val);
409 }
410
411 return TEST_SUCCESS;
412 }
413
414 static test_return_t hashkit_set_distribution_function_test(hashkit_st *hashk)
415 {
416 for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++)
417 {
418 hashkit_return_t rc;
419
420 rc= hashkit_set_distribution_function(hashk, algo);
421
422 /* Hsieh is disabled most of the time for patent issues */
423 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH)
424 continue;
425
426 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_CUSTOM)
427 continue;
428
429 test_true(rc == HASHKIT_SUCCESS);
430 }
431
432 return TEST_SUCCESS;
433 }
434
435 static test_return_t hashkit_set_custom_distribution_function_test(hashkit_st *hashk)
436 {
437 hashkit_return_t rc;
438
439 rc= hashkit_set_custom_distribution_function(hashk, hash_test_function, NULL);
440 test_true(rc == HASHKIT_SUCCESS);
441
442 return TEST_SUCCESS;
443 }
444
445
446 static test_return_t hashkit_get_function_test(hashkit_st *hashk)
447 {
448 for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++)
449 {
450 hashkit_return_t rc;
451
452 if (HASHKIT_HASH_CUSTOM || HASHKIT_HASH_HSIEH)
453 continue;
454
455 rc= hashkit_set_function(hashk, algo);
456 test_true(rc == HASHKIT_SUCCESS);
457
458 test_true(hashkit_get_function(hashk) == algo);
459 }
460 return TEST_SUCCESS;
461 }
462
463 static test_return_t hashkit_compare_test(hashkit_st *hashk)
464 {
465 hashkit_st *clone;
466
467 clone= hashkit_clone(NULL, hashk);
468
469 test_true(hashkit_compare(clone, hashk));
470 hashkit_free(clone);
471
472 return TEST_SUCCESS;
473 }
474
475 test_st hashkit_st_functions[] ={
476 {"hashkit_digest", 0, (test_callback_fn)hashkit_digest_test},
477 {"hashkit_set_function", 0, (test_callback_fn)hashkit_set_function_test},
478 {"hashkit_set_custom_function", 0, (test_callback_fn)hashkit_set_custom_function_test},
479 {"hashkit_get_function", 0, (test_callback_fn)hashkit_get_function_test},
480 {"hashkit_set_distribution_function", 0, (test_callback_fn)hashkit_set_distribution_function_test},
481 {"hashkit_set_custom_distribution_function", 0, (test_callback_fn)hashkit_set_custom_distribution_function_test},
482 {"hashkit_compare", 0, (test_callback_fn)hashkit_compare_test},
483 {0, 0, 0}
484 };
485
486 static test_return_t libhashkit_digest_test(hashkit_st *hashk)
487 {
488 uint32_t value;
489
490 (void)hashk;
491
492 value= libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT);
493
494 return TEST_SUCCESS;
495 }
496
497 test_st library_functions[] ={
498 {"libhashkit_digest", 0, (test_callback_fn)libhashkit_digest_test},
499 {0, 0, 0}
500 };
501
502 test_st hash_tests[] ={
503 {"one_at_a_time", 0, (test_callback_fn)one_at_a_time_run },
504 {"md5", 0, (test_callback_fn)md5_run },
505 {"crc", 0, (test_callback_fn)crc_run },
506 {"fnv1_64", 0, (test_callback_fn)fnv1_64_run },
507 {"fnv1a_64", 0, (test_callback_fn)fnv1a_64_run },
508 {"fnv1_32", 0, (test_callback_fn)fnv1_32_run },
509 {"fnv1a_32", 0, (test_callback_fn)fnv1a_32_run },
510 {"hsieh", 0, (test_callback_fn)hsieh_run },
511 {"murmur", 0, (test_callback_fn)murmur_run },
512 {"jenkis", 0, (test_callback_fn)jenkins_run },
513 {0, 0, (test_callback_fn)0}
514 };
515
516 /*
517 * The following test suite is used to verify that we don't introduce
518 * regression bugs. If you want more information about the bug / test,
519 * you should look in the bug report at
520 * http://bugs.launchpad.net/libmemcached
521 */
522 test_st regression[]= {
523 {0, 0, 0}
524 };
525
526 collection_st collection[] ={
527 {"allocation", 0, 0, allocation},
528 {"hashkit_st_functions", 0, 0, hashkit_st_functions},
529 {"library_functions", 0, 0, library_functions},
530 {"hashing", 0, 0, hash_tests},
531 {"regression", 0, 0, regression},
532 {0, 0, 0, 0}
533 };
534
535 /* Prototypes for functions we will pass to test framework */
536 void *world_create(test_return_t *error);
537 test_return_t world_destroy(hashkit_st *hashk);
538
539 void *world_create(test_return_t *error)
540 {
541 hashkit_st *hashk_ptr;
542
543 hashk_ptr= hashkit_create(&global_hashk);
544
545 if (hashk_ptr != &global_hashk)
546 {
547 *error= TEST_FAILURE;
548 return NULL;
549 }
550
551 if (hashkit_is_allocated(hashk_ptr) == true)
552 {
553 *error= TEST_FAILURE;
554 return NULL;
555 }
556
557 *error= TEST_SUCCESS;
558
559 return hashk_ptr;
560 }
561
562
563 test_return_t world_destroy(hashkit_st *hashk)
564 {
565 // Did we get back what we expected?
566 assert(hashkit_is_allocated(hashk) == false);
567 hashkit_free(&global_hashk);
568
569 return TEST_SUCCESS;
570 }
571
572 void get_world(world_st *world)
573 {
574 world->collections= collection;
575 world->create= (test_callback_create_fn)world_create;
576 world->destroy= (test_callback_fn)world_destroy;
577 }