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