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.
14 #include <libhashkit/hashkit.h>
18 #include "hash_results.h"
20 static hashkit_st global_hashk
;
23 @brief hash_test_st is a structure we use in testing. It is currently empty.
25 typedef struct hash_test_st hash_test_st
;
32 static test_return_t
init_test(void *not_used
__attribute__((unused
)))
35 hashkit_st
*hashk_ptr
;
37 hashk_ptr
= hashkit_create(&hashk
);
39 test_true(hashk_ptr
== &hashk
);
40 test_true(hashkit_is_allocated(hashk_ptr
) == false);
42 hashkit_free(hashk_ptr
);
47 static test_return_t
allocation_test(void *not_used
__attribute__((unused
)))
49 hashkit_st
*hashk_ptr
;
51 hashk_ptr
= hashkit_create(NULL
);
53 test_true(hashkit_is_allocated(hashk_ptr
) == true);
54 hashkit_free(hashk_ptr
);
59 static test_return_t
clone_test(hashkit_st
*hashk
)
61 // First we make sure that the testing system is giving us what we expect.
62 assert(&global_hashk
== hashk
);
64 // Second we test if hashk is even valid
68 hashkit_st
*hashk_ptr
;
69 hashk_ptr
= hashkit_clone(NULL
, NULL
);
71 test_true(hashkit_is_allocated(hashk_ptr
));
72 hashkit_free(hashk_ptr
);
75 /* Can we init from null? */
77 hashkit_st
*hashk_ptr
;
79 hashk_ptr
= hashkit_clone(NULL
, hashk
);
82 test_true(hashkit_is_allocated(hashk_ptr
));
84 hashkit_free(hashk_ptr
);
87 /* Can we init from struct? */
89 hashkit_st declared_clone
;
90 hashkit_st
*hash_clone
;
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
));
97 hashkit_free(hash_clone
);
100 /* Can we init from struct? */
102 hashkit_st declared_clone
;
103 hashkit_st
*hash_clone
;
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
));
110 hashkit_free(hash_clone
);
116 static test_return_t
one_at_a_time_run (hashkit_st
*hashk
__attribute__((unused
)))
121 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
125 hash_val
= libhashkit_one_at_a_time(*ptr
, strlen(*ptr
));
126 test_true(one_at_a_time_values
[x
] == hash_val
);
132 static test_return_t
md5_run (hashkit_st
*hashk
__attribute__((unused
)))
137 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
141 hash_val
= libhashkit_md5(*ptr
, strlen(*ptr
));
142 test_true(md5_values
[x
] == hash_val
);
148 static test_return_t
crc_run (hashkit_st
*hashk
__attribute__((unused
)))
153 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
157 hash_val
= libhashkit_crc32(*ptr
, strlen(*ptr
));
158 assert(crc_values
[x
] == hash_val
);
164 static test_return_t
fnv1_64_run (hashkit_st
*hashk
__attribute__((unused
)))
169 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
173 hash_val
= libhashkit_fnv1_64(*ptr
, strlen(*ptr
));
174 assert(fnv1_64_values
[x
] == hash_val
);
180 static test_return_t
fnv1a_64_run (hashkit_st
*hashk
__attribute__((unused
)))
185 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
189 hash_val
= libhashkit_fnv1a_64(*ptr
, strlen(*ptr
));
190 assert(fnv1a_64_values
[x
] == hash_val
);
196 static test_return_t
fnv1_32_run (hashkit_st
*hashk
__attribute__((unused
)))
202 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
206 hash_val
= libhashkit_fnv1_32(*ptr
, strlen(*ptr
));
207 assert(fnv1_32_values
[x
] == hash_val
);
213 static test_return_t
fnv1a_32_run (hashkit_st
*hashk
__attribute__((unused
)))
218 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
222 hash_val
= libhashkit_fnv1a_32(*ptr
, strlen(*ptr
));
223 assert(fnv1a_32_values
[x
] == hash_val
);
229 static test_return_t
hsieh_run (hashkit_st
*hashk
__attribute__((unused
)))
234 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
238 #ifdef HAVE_HSIEH_HASH
239 hash_val
= libhashkit_hsieh(*ptr
, strlen(*ptr
));
243 assert(hsieh_values
[x
] == hash_val
);
249 static test_return_t
murmur_run (hashkit_st
*hashk
__attribute__((unused
)))
257 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
261 hash_val
= libhashkit_murmur(*ptr
, strlen(*ptr
));
262 assert(murmur_values
[x
] == hash_val
);
269 static test_return_t
jenkins_run (hashkit_st
*hashk
__attribute__((unused
)))
275 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
279 hash_val
= libhashkit_jenkins(*ptr
, strlen(*ptr
));
280 assert(jenkins_values
[x
] == hash_val
);
290 @brief now we list out the tests.
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
},
300 static test_return_t
hashkit_digest_test(hashkit_st
*hashk
)
303 value
= hashkit_digest(hashk
, "a", sizeof("a"));
308 static test_return_t
hashkit_set_function_test(hashkit_st
*hashk
)
310 for (hashkit_hash_algorithm_t algo
= HASHKIT_HASH_DEFAULT
; algo
< HASHKIT_HASH_MAX
; algo
++)
317 rc
= hashkit_set_function(hashk
, algo
);
319 /* Hsieh is disabled most of the time for patent issues */
320 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_HSIEH
)
323 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_CUSTOM
)
326 test_true(rc
== HASHKIT_SUCCESS
);
330 case HASHKIT_HASH_DEFAULT
:
331 list
= one_at_a_time_values
;
333 case HASHKIT_HASH_MD5
:
336 case HASHKIT_HASH_CRC
:
339 case HASHKIT_HASH_FNV1_64
:
340 list
= fnv1_64_values
;
342 case HASHKIT_HASH_FNV1A_64
:
343 list
= fnv1a_64_values
;
345 case HASHKIT_HASH_FNV1_32
:
346 list
= fnv1_32_values
;
348 case HASHKIT_HASH_FNV1A_32
:
349 list
= fnv1a_32_values
;
351 case HASHKIT_HASH_HSIEH
:
354 case HASHKIT_HASH_MURMUR
:
357 case HASHKIT_HASH_JENKINS
:
358 list
= jenkins_values
;
360 case HASHKIT_HASH_CUSTOM
:
361 case HASHKIT_HASH_MAX
:
364 test_fail("We ended up on a non-existent hash");
367 // Now we make sure we did set the hash correctly.
368 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
372 hash_val
= hashkit_digest(hashk
, *ptr
, strlen(*ptr
));
373 test_true(list
[x
] == hash_val
);
380 static uint32_t hash_test_function(const char *string
, size_t string_length
, void *context
)
383 return libhashkit_md5(string
, string_length
);
386 static test_return_t
hashkit_set_custom_function_test(hashkit_st
*hashk
)
393 rc
= hashkit_set_custom_function(hashk
, hash_test_function
, NULL
);
394 test_true(rc
== HASHKIT_SUCCESS
);
396 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
400 hash_val
= hashkit_digest(hashk
, *ptr
, strlen(*ptr
));
401 test_true(md5_values
[x
] == hash_val
);
407 static test_return_t
hashkit_set_distribution_function_test(hashkit_st
*hashk
)
409 for (hashkit_hash_algorithm_t algo
= HASHKIT_HASH_DEFAULT
; algo
< HASHKIT_HASH_MAX
; algo
++)
413 rc
= hashkit_set_distribution_function(hashk
, algo
);
415 /* Hsieh is disabled most of the time for patent issues */
416 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_HSIEH
)
419 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_CUSTOM
)
422 test_true(rc
== HASHKIT_SUCCESS
);
428 static test_return_t
hashkit_set_custom_distribution_function_test(hashkit_st
*hashk
)
432 rc
= hashkit_set_custom_distribution_function(hashk
, hash_test_function
, NULL
);
433 test_true(rc
== HASHKIT_SUCCESS
);
439 static test_return_t
hashkit_get_function_test(hashkit_st
*hashk
)
441 for (hashkit_hash_algorithm_t algo
= HASHKIT_HASH_DEFAULT
; algo
< HASHKIT_HASH_MAX
; algo
++)
445 if (HASHKIT_HASH_CUSTOM
|| HASHKIT_HASH_HSIEH
)
448 rc
= hashkit_set_function(hashk
, algo
);
449 test_true(rc
== HASHKIT_SUCCESS
);
451 test_true(hashkit_get_function(hashk
) == algo
);
456 static test_return_t
hashkit_compare_test(hashkit_st
*hashk
)
460 clone
= hashkit_clone(NULL
, hashk
);
462 test_true(hashkit_compare(clone
, hashk
));
467 test_st hashkit_st_functions
[] ={
468 {"hashkit_digest", 0, (test_callback_fn
)hashkit_digest_test
},
469 {"hashkit_set_function", 0, (test_callback_fn
)hashkit_set_function_test
},
470 {"hashkit_set_custom_function", 0, (test_callback_fn
)hashkit_set_custom_function_test
},
471 {"hashkit_get_function", 0, (test_callback_fn
)hashkit_get_function_test
},
472 {"hashkit_set_distribution_function", 0, (test_callback_fn
)hashkit_set_distribution_function_test
},
473 {"hashkit_set_custom_distribution_function", 0, (test_callback_fn
)hashkit_set_custom_distribution_function_test
},
474 {"hashkit_compare", 0, (test_callback_fn
)hashkit_compare_test
},
478 static test_return_t
libhashkit_digest_test(hashkit_st
*hashk
)
484 value
= libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT
);
489 test_st library_functions
[] ={
490 {"libhashkit_digest", 0, (test_callback_fn
)libhashkit_digest_test
},
494 test_st hash_tests
[] ={
495 {"one_at_a_time", 0, (test_callback_fn
)one_at_a_time_run
},
496 {"md5", 0, (test_callback_fn
)md5_run
},
497 {"crc", 0, (test_callback_fn
)crc_run
},
498 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
499 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
500 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
501 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
502 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
503 {"murmur", 0, (test_callback_fn
)murmur_run
},
504 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
505 {0, 0, (test_callback_fn
)0}
509 * The following test suite is used to verify that we don't introduce
510 * regression bugs. If you want more information about the bug / test,
511 * you should look in the bug report at
512 * http://bugs.launchpad.net/libmemcached
514 test_st regression
[]= {
518 collection_st collection
[] ={
519 {"allocation", 0, 0, allocation
},
520 {"hashkit_st_functions", 0, 0, hashkit_st_functions
},
521 {"library_functions", 0, 0, library_functions
},
522 {"hashing", 0, 0, hash_tests
},
523 {"regression", 0, 0, regression
},
527 /* Prototypes for functions we will pass to test framework */
528 void *world_create(test_return_t
*error
);
529 test_return_t
world_destroy(hashkit_st
*hashk
);
531 void *world_create(test_return_t
*error
)
533 hashkit_st
*hashk_ptr
;
535 hashk_ptr
= hashkit_create(&global_hashk
);
537 if (hashk_ptr
!= &global_hashk
)
539 *error
= TEST_FAILURE
;
543 if (hashkit_is_allocated(hashk_ptr
) == true)
545 *error
= TEST_FAILURE
;
549 *error
= TEST_SUCCESS
;
555 test_return_t
world_destroy(hashkit_st
*hashk
)
557 // Did we get back what we expected?
558 assert(hashkit_is_allocated(hashk
) == false);
559 hashkit_free(&global_hashk
);
564 void get_world(world_st
*world
)
566 world
->collections
= collection
;
567 world
->create
= (test_callback_create_fn
)world_create
;
568 world
->destroy
= (test_callback_fn
)world_destroy
;