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
:
367 // Now we make sure we did set the hash correctly.
370 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
374 hash_val
= hashkit_digest(hashk
, *ptr
, strlen(*ptr
));
375 test_true(list
[x
] == hash_val
);
387 static uint32_t hash_test_function(const char *string
, size_t string_length
, void *context
)
390 return libhashkit_md5(string
, string_length
);
393 static test_return_t
hashkit_set_custom_function_test(hashkit_st
*hashk
)
400 rc
= hashkit_set_custom_function(hashk
, hash_test_function
, NULL
);
401 test_true(rc
== HASHKIT_SUCCESS
);
403 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
407 hash_val
= hashkit_digest(hashk
, *ptr
, strlen(*ptr
));
408 test_true(md5_values
[x
] == hash_val
);
414 static test_return_t
hashkit_set_distribution_function_test(hashkit_st
*hashk
)
416 for (hashkit_hash_algorithm_t algo
= HASHKIT_HASH_DEFAULT
; algo
< HASHKIT_HASH_MAX
; algo
++)
420 rc
= hashkit_set_distribution_function(hashk
, algo
);
422 /* Hsieh is disabled most of the time for patent issues */
423 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_HSIEH
)
426 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_CUSTOM
)
429 test_true(rc
== HASHKIT_SUCCESS
);
435 static test_return_t
hashkit_set_custom_distribution_function_test(hashkit_st
*hashk
)
439 rc
= hashkit_set_custom_distribution_function(hashk
, hash_test_function
, NULL
);
440 test_true(rc
== HASHKIT_SUCCESS
);
446 static test_return_t
hashkit_get_function_test(hashkit_st
*hashk
)
448 for (hashkit_hash_algorithm_t algo
= HASHKIT_HASH_DEFAULT
; algo
< HASHKIT_HASH_MAX
; algo
++)
452 if (HASHKIT_HASH_CUSTOM
|| HASHKIT_HASH_HSIEH
)
455 rc
= hashkit_set_function(hashk
, algo
);
456 test_true(rc
== HASHKIT_SUCCESS
);
458 test_true(hashkit_get_function(hashk
) == algo
);
463 static test_return_t
hashkit_compare_test(hashkit_st
*hashk
)
467 clone
= hashkit_clone(NULL
, hashk
);
469 test_true(hashkit_compare(clone
, hashk
));
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
},
486 static test_return_t
libhashkit_digest_test(hashkit_st
*hashk
)
492 value
= libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT
);
497 test_st library_functions
[] ={
498 {"libhashkit_digest", 0, (test_callback_fn
)libhashkit_digest_test
},
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}
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
522 test_st regression
[]= {
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
},
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
);
539 void *world_create(test_return_t
*error
)
541 hashkit_st
*hashk_ptr
;
543 hashk_ptr
= hashkit_create(&global_hashk
);
545 if (hashk_ptr
!= &global_hashk
)
547 *error
= TEST_FAILURE
;
551 if (hashkit_is_allocated(hashk_ptr
) == true)
553 *error
= TEST_FAILURE
;
557 *error
= TEST_SUCCESS
;
563 test_return_t
world_destroy(hashkit_st
*hashk
)
565 // Did we get back what we expected?
566 assert(hashkit_is_allocated(hashk
) == false);
567 hashkit_free(&global_hashk
);
572 void get_world(world_st
*world
)
574 world
->collections
= collection
;
575 world
->create
= (test_callback_create_fn
)world_create
;
576 world
->destroy
= (test_callback_fn
)world_destroy
;