1 /* libHashKit Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
16 #include <libhashkit/hashkit.h>
20 #include "hash_results.h"
22 static hashkit_st global_hashk
;
25 @brief hash_test_st is a structure we use in testing. It is currently empty.
27 typedef struct hash_test_st hash_test_st
;
34 static test_return_t
init_test(void *not_used
)
37 hashkit_st
*hashk_ptr
;
40 hashk_ptr
= hashkit_create(&hashk
);
42 test_true(hashk_ptr
== &hashk
);
43 test_true(hashkit_is_allocated(hashk_ptr
) == false);
45 hashkit_free(hashk_ptr
);
50 static test_return_t
allocation_test(void *not_used
)
52 hashkit_st
*hashk_ptr
;
55 hashk_ptr
= hashkit_create(NULL
);
57 test_true(hashkit_is_allocated(hashk_ptr
) == true);
58 hashkit_free(hashk_ptr
);
63 static test_return_t
clone_test(hashkit_st
*hashk
)
65 // First we make sure that the testing system is giving us what we expect.
66 assert(&global_hashk
== hashk
);
68 // Second we test if hashk is even valid
72 hashkit_st
*hashk_ptr
;
73 hashk_ptr
= hashkit_clone(NULL
, NULL
);
75 test_true(hashkit_is_allocated(hashk_ptr
));
76 hashkit_free(hashk_ptr
);
79 /* Can we init from null? */
81 hashkit_st
*hashk_ptr
;
83 hashk_ptr
= hashkit_clone(NULL
, hashk
);
86 test_true(hashkit_is_allocated(hashk_ptr
));
88 hashkit_free(hashk_ptr
);
91 /* Can we init from struct? */
93 hashkit_st declared_clone
;
94 hashkit_st
*hash_clone
;
96 hash_clone
= hashkit_clone(&declared_clone
, NULL
);
97 test_true(hash_clone
);
98 test_true(hash_clone
== &declared_clone
);
99 test_false(hashkit_is_allocated(hash_clone
));
101 hashkit_free(hash_clone
);
104 /* Can we init from struct? */
106 hashkit_st declared_clone
;
107 hashkit_st
*hash_clone
;
109 hash_clone
= hashkit_clone(&declared_clone
, hashk
);
110 test_true(hash_clone
);
111 test_true(hash_clone
== &declared_clone
);
112 test_false(hashkit_is_allocated(hash_clone
));
114 hashkit_free(hash_clone
);
120 static test_return_t
one_at_a_time_run (hashkit_st
*hashk
)
126 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
130 hash_val
= libhashkit_one_at_a_time(*ptr
, strlen(*ptr
));
131 test_true(one_at_a_time_values
[x
] == hash_val
);
137 static test_return_t
md5_run (hashkit_st
*hashk
)
143 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
147 hash_val
= libhashkit_md5(*ptr
, strlen(*ptr
));
148 test_true(md5_values
[x
] == hash_val
);
154 static test_return_t
crc_run (hashkit_st
*hashk
)
160 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
164 hash_val
= libhashkit_crc32(*ptr
, strlen(*ptr
));
165 assert(crc_values
[x
] == hash_val
);
171 static test_return_t
fnv1_64_run (hashkit_st
*hashk
)
177 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
181 hash_val
= libhashkit_fnv1_64(*ptr
, strlen(*ptr
));
182 assert(fnv1_64_values
[x
] == hash_val
);
188 static test_return_t
fnv1a_64_run (hashkit_st
*hashk
)
194 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
198 hash_val
= libhashkit_fnv1a_64(*ptr
, strlen(*ptr
));
199 assert(fnv1a_64_values
[x
] == hash_val
);
205 static test_return_t
fnv1_32_run (hashkit_st
*hashk
)
211 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
215 hash_val
= libhashkit_fnv1_32(*ptr
, strlen(*ptr
));
216 assert(fnv1_32_values
[x
] == hash_val
);
222 static test_return_t
fnv1a_32_run (hashkit_st
*hashk
)
228 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
232 hash_val
= libhashkit_fnv1a_32(*ptr
, strlen(*ptr
));
233 assert(fnv1a_32_values
[x
] == hash_val
);
239 static test_return_t
hsieh_run (hashkit_st
*hashk
)
245 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
249 #ifdef HAVE_HSIEH_HASH
250 hash_val
= libhashkit_hsieh(*ptr
, strlen(*ptr
));
254 assert(hsieh_values
[x
] == hash_val
);
260 static test_return_t
murmur_run (hashkit_st
*hashk
)
264 #ifdef WORDS_BIGENDIAN
270 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
274 hash_val
= libhashkit_murmur(*ptr
, strlen(*ptr
));
275 assert(murmur_values
[x
] == hash_val
);
282 static test_return_t
jenkins_run (hashkit_st
*hashk
)
288 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
292 hash_val
= libhashkit_jenkins(*ptr
, strlen(*ptr
));
293 assert(jenkins_values
[x
] == hash_val
);
303 @brief now we list out the tests.
306 test_st allocation
[]= {
307 {"init", 0, (test_callback_fn
)init_test
},
308 {"create and free", 0, (test_callback_fn
)allocation_test
},
309 {"clone", 0, (test_callback_fn
)clone_test
},
313 static test_return_t
hashkit_digest_test(hashkit_st
*hashk
)
316 value
= hashkit_digest(hashk
, "a", sizeof("a"));
321 static test_return_t
hashkit_set_function_test(hashkit_st
*hashk
)
323 for (hashkit_hash_algorithm_t algo
= HASHKIT_HASH_DEFAULT
; algo
< HASHKIT_HASH_MAX
; algo
++)
330 rc
= hashkit_set_function(hashk
, algo
);
332 /* Hsieh is disabled most of the time for patent issues */
333 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_HSIEH
)
336 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_CUSTOM
)
339 test_true(rc
== HASHKIT_SUCCESS
);
343 case HASHKIT_HASH_DEFAULT
:
344 list
= one_at_a_time_values
;
346 case HASHKIT_HASH_MD5
:
349 case HASHKIT_HASH_CRC
:
352 case HASHKIT_HASH_FNV1_64
:
353 list
= fnv1_64_values
;
355 case HASHKIT_HASH_FNV1A_64
:
356 list
= fnv1a_64_values
;
358 case HASHKIT_HASH_FNV1_32
:
359 list
= fnv1_32_values
;
361 case HASHKIT_HASH_FNV1A_32
:
362 list
= fnv1a_32_values
;
364 case HASHKIT_HASH_HSIEH
:
367 case HASHKIT_HASH_MURMUR
:
370 case HASHKIT_HASH_JENKINS
:
371 list
= jenkins_values
;
373 case HASHKIT_HASH_CUSTOM
:
374 case HASHKIT_HASH_MAX
:
380 // Now we make sure we did set the hash correctly.
383 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
387 hash_val
= hashkit_digest(hashk
, *ptr
, strlen(*ptr
));
388 test_true(list
[x
] == hash_val
);
400 static uint32_t hash_test_function(const char *string
, size_t string_length
, void *context
)
403 return libhashkit_md5(string
, string_length
);
406 static test_return_t
hashkit_set_custom_function_test(hashkit_st
*hashk
)
413 rc
= hashkit_set_custom_function(hashk
, hash_test_function
, NULL
);
414 test_true(rc
== HASHKIT_SUCCESS
);
416 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
420 hash_val
= hashkit_digest(hashk
, *ptr
, strlen(*ptr
));
421 test_true(md5_values
[x
] == hash_val
);
427 static test_return_t
hashkit_set_distribution_function_test(hashkit_st
*hashk
)
429 for (hashkit_hash_algorithm_t algo
= HASHKIT_HASH_DEFAULT
; algo
< HASHKIT_HASH_MAX
; algo
++)
433 rc
= hashkit_set_distribution_function(hashk
, algo
);
435 /* Hsieh is disabled most of the time for patent issues */
436 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_HSIEH
)
439 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_CUSTOM
)
442 test_true(rc
== HASHKIT_SUCCESS
);
448 static test_return_t
hashkit_set_custom_distribution_function_test(hashkit_st
*hashk
)
452 rc
= hashkit_set_custom_distribution_function(hashk
, hash_test_function
, NULL
);
453 test_true(rc
== HASHKIT_SUCCESS
);
459 static test_return_t
hashkit_get_function_test(hashkit_st
*hashk
)
461 for (hashkit_hash_algorithm_t algo
= HASHKIT_HASH_DEFAULT
; algo
< HASHKIT_HASH_MAX
; algo
++)
465 if (HASHKIT_HASH_CUSTOM
|| HASHKIT_HASH_HSIEH
)
468 rc
= hashkit_set_function(hashk
, algo
);
469 test_true(rc
== HASHKIT_SUCCESS
);
471 test_true(hashkit_get_function(hashk
) == algo
);
476 static test_return_t
hashkit_compare_test(hashkit_st
*hashk
)
480 clone
= hashkit_clone(NULL
, hashk
);
482 test_true(hashkit_compare(clone
, hashk
));
488 test_st hashkit_st_functions
[] ={
489 {"hashkit_digest", 0, (test_callback_fn
)hashkit_digest_test
},
490 {"hashkit_set_function", 0, (test_callback_fn
)hashkit_set_function_test
},
491 {"hashkit_set_custom_function", 0, (test_callback_fn
)hashkit_set_custom_function_test
},
492 {"hashkit_get_function", 0, (test_callback_fn
)hashkit_get_function_test
},
493 {"hashkit_set_distribution_function", 0, (test_callback_fn
)hashkit_set_distribution_function_test
},
494 {"hashkit_set_custom_distribution_function", 0, (test_callback_fn
)hashkit_set_custom_distribution_function_test
},
495 {"hashkit_compare", 0, (test_callback_fn
)hashkit_compare_test
},
499 static test_return_t
libhashkit_digest_test(hashkit_st
*hashk
)
505 value
= libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT
);
510 test_st library_functions
[] ={
511 {"libhashkit_digest", 0, (test_callback_fn
)libhashkit_digest_test
},
515 test_st hash_tests
[] ={
516 {"one_at_a_time", 0, (test_callback_fn
)one_at_a_time_run
},
517 {"md5", 0, (test_callback_fn
)md5_run
},
518 {"crc", 0, (test_callback_fn
)crc_run
},
519 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
520 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
521 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
522 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
523 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
524 {"murmur", 0, (test_callback_fn
)murmur_run
},
525 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
526 {0, 0, (test_callback_fn
)0}
530 * The following test suite is used to verify that we don't introduce
531 * regression bugs. If you want more information about the bug / test,
532 * you should look in the bug report at
533 * http://bugs.launchpad.net/libmemcached
535 test_st regression
[]= {
539 collection_st collection
[] ={
540 {"allocation", 0, 0, allocation
},
541 {"hashkit_st_functions", 0, 0, hashkit_st_functions
},
542 {"library_functions", 0, 0, library_functions
},
543 {"hashing", 0, 0, hash_tests
},
544 {"regression", 0, 0, regression
},
548 /* Prototypes for functions we will pass to test framework */
549 void *world_create(test_return_t
*error
);
550 test_return_t
world_destroy(hashkit_st
*hashk
);
552 void *world_create(test_return_t
*error
)
554 hashkit_st
*hashk_ptr
;
556 hashk_ptr
= hashkit_create(&global_hashk
);
558 if (hashk_ptr
!= &global_hashk
)
560 *error
= TEST_FAILURE
;
564 if (hashkit_is_allocated(hashk_ptr
) == true)
566 *error
= TEST_FAILURE
;
570 *error
= TEST_SUCCESS
;
576 test_return_t
world_destroy(hashkit_st
*hashk
)
578 // Did we get back what we expected?
579 assert(hashkit_is_allocated(hashk
) == false);
580 hashkit_free(&global_hashk
);
585 void get_world(world_st
*world
)
587 world
->collections
= collection
;
588 world
->create
= (test_callback_create_fn
)world_create
;
589 world
->destroy
= (test_callback_fn
)world_destroy
;