X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Fhashkit_functions.c;h=0f2c42a99d050cd3c20954cb6d77cf6058274b03;hb=349ca737f30ff0b6c3c71034f0930660663fa360;hp=1302011e76f48f14ad49f362bcf51c42f85bad09;hpb=dec0636927f5d0ae4cf06ad2710d022990419879;p=m6w6%2Flibmemcached diff --git a/tests/hashkit_functions.c b/tests/hashkit_functions.c index 1302011e..0f2c42a9 100644 --- a/tests/hashkit_functions.c +++ b/tests/hashkit_functions.c @@ -6,6 +6,8 @@ * the COPYING file in the parent directory for full text. */ +#include "config.h" + #include #include #include @@ -13,7 +15,7 @@ #include -#include "test.h" +#include #include "hash_results.h" @@ -29,32 +31,30 @@ struct hash_test_st bool _unused; }; -static test_return_t init_test(void *not_used __attribute__((unused))) +static test_return_t init_test(void *not_used) { hashkit_st hashk; hashkit_st *hashk_ptr; + (void)not_used; hashk_ptr= hashkit_create(&hashk); - test_truth(hashk_ptr); - test_truth(hashk_ptr == &hashk); - test_truth(hashkit_is_initialized(&hashk) == true); - test_truth(hashkit_is_allocated(hashk_ptr) == false); + test_true(hashk_ptr); + test_true(hashk_ptr == &hashk); + test_true(hashkit_is_allocated(hashk_ptr) == false); hashkit_free(hashk_ptr); - test_truth(hashkit_is_initialized(&hashk) == false); - return TEST_SUCCESS; } -static test_return_t allocation_test(void *not_used __attribute__((unused))) +static test_return_t allocation_test(void *not_used) { hashkit_st *hashk_ptr; + (void)not_used; hashk_ptr= hashkit_create(NULL); - test_truth(hashk_ptr); - test_truth(hashkit_is_allocated(hashk_ptr) == true); - test_truth(hashkit_is_initialized(hashk_ptr) == true); + test_true(hashk_ptr); + test_true(hashkit_is_allocated(hashk_ptr) == true); hashkit_free(hashk_ptr); return TEST_SUCCESS; @@ -66,15 +66,13 @@ static test_return_t clone_test(hashkit_st *hashk) assert(&global_hashk == hashk); // Second we test if hashk is even valid - test_truth(hashkit_is_initialized(hashk) == true); /* All null? */ { hashkit_st *hashk_ptr; hashk_ptr= hashkit_clone(NULL, NULL); - test_truth(hashk_ptr); - test_truth(hashkit_is_allocated(hashk_ptr) == true); - test_truth(hashkit_is_initialized(hashk_ptr) == true); + test_true(hashk_ptr); + test_true(hashkit_is_allocated(hashk_ptr)); hashkit_free(hashk_ptr); } @@ -84,23 +82,8 @@ static test_return_t clone_test(hashkit_st *hashk) hashk_ptr= hashkit_clone(NULL, hashk); - test_truth(hashk_ptr); - test_truth(hashkit_is_allocated(hashk_ptr) == true); - test_truth(hashkit_is_initialized(hashk_ptr) == true); - - test_truth(hashk_ptr->distribution == hashk->distribution); - test_truth(hashk_ptr->continuum_count == hashk->continuum_count); - test_truth(hashk_ptr->continuum_points_count == hashk->continuum_points_count); - test_truth(hashk_ptr->list_size == hashk->list_size); - test_truth(hashk_ptr->context_size == hashk->context_size); - test_truth(hashk_ptr->continuum == NULL); - test_truth(hashk_ptr->hash_fn == hashk->hash_fn); - test_truth(hashk_ptr->active_fn == hashk->active_fn); - test_truth(hashk_ptr->continuum_hash_fn == hashk->continuum_hash_fn); - test_truth(hashk_ptr->continuum_key_fn == hashk->continuum_key_fn); - test_truth(hashk_ptr->sort_fn == hashk->sort_fn); - test_truth(hashk_ptr->weight_fn == hashk->weight_fn); - test_truth(hashk_ptr->list == hashk->list); + test_true(hashk_ptr); + test_true(hashkit_is_allocated(hashk_ptr)); hashkit_free(hashk_ptr); } @@ -111,7 +94,9 @@ static test_return_t clone_test(hashkit_st *hashk) hashkit_st *hash_clone; hash_clone= hashkit_clone(&declared_clone, NULL); - test_truth(hash_clone); + test_true(hash_clone); + test_true(hash_clone == &declared_clone); + test_false(hashkit_is_allocated(hash_clone)); hashkit_free(hash_clone); } @@ -120,124 +105,149 @@ static test_return_t clone_test(hashkit_st *hashk) { hashkit_st declared_clone; hashkit_st *hash_clone; - memset(&declared_clone, 0 , sizeof(hashkit_st)); + hash_clone= hashkit_clone(&declared_clone, hashk); - test_truth(hash_clone); + test_true(hash_clone); + test_true(hash_clone == &declared_clone); + test_false(hashkit_is_allocated(hash_clone)); + hashkit_free(hash_clone); } return TEST_SUCCESS; } +static test_return_t one_at_a_time_run (hashkit_st *hashk) +{ + uint32_t x; + const char **ptr; + (void)hashk; -static test_return_t md5_run (hashkit_st *hashk __attribute__((unused))) + for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) + { + uint32_t hash_val; + + hash_val= libhashkit_one_at_a_time(*ptr, strlen(*ptr)); + test_true(one_at_a_time_values[x] == hash_val); + } + + return TEST_SUCCESS; +} + +static test_return_t md5_run (hashkit_st *hashk) { uint32_t x; const char **ptr; + (void)hashk; for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) { uint32_t hash_val; - hash_val= hashkit_md5(*ptr, strlen(*ptr)); - test_truth(md5_values[x] == hash_val); + hash_val= libhashkit_md5(*ptr, strlen(*ptr)); + test_true(md5_values[x] == hash_val); } return TEST_SUCCESS; } -static test_return_t crc_run (hashkit_st *hashk __attribute__((unused))) +static test_return_t crc_run (hashkit_st *hashk) { uint32_t x; const char **ptr; + (void)hashk; for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) { uint32_t hash_val; - hash_val= hashkit_crc32(*ptr, strlen(*ptr)); + hash_val= libhashkit_crc32(*ptr, strlen(*ptr)); assert(crc_values[x] == hash_val); } return TEST_SUCCESS; } -static test_return_t fnv1_64_run (hashkit_st *hashk __attribute__((unused))) +static test_return_t fnv1_64_run (hashkit_st *hashk) { uint32_t x; const char **ptr; + (void)hashk; for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) { uint32_t hash_val; - hash_val= hashkit_fnv1_64(*ptr, strlen(*ptr)); + hash_val= libhashkit_fnv1_64(*ptr, strlen(*ptr)); assert(fnv1_64_values[x] == hash_val); } return TEST_SUCCESS; } -static test_return_t fnv1a_64_run (hashkit_st *hashk __attribute__((unused))) +static test_return_t fnv1a_64_run (hashkit_st *hashk) { uint32_t x; const char **ptr; + (void)hashk; for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) { uint32_t hash_val; - hash_val= hashkit_fnv1a_64(*ptr, strlen(*ptr)); + hash_val= libhashkit_fnv1a_64(*ptr, strlen(*ptr)); assert(fnv1a_64_values[x] == hash_val); } return TEST_SUCCESS; } -static test_return_t fnv1_32_run (hashkit_st *hashk __attribute__((unused))) +static test_return_t fnv1_32_run (hashkit_st *hashk) { uint32_t x; const char **ptr; - + (void)hashk; for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) { uint32_t hash_val; - hash_val= hashkit_fnv1_32(*ptr, strlen(*ptr)); + hash_val= libhashkit_fnv1_32(*ptr, strlen(*ptr)); assert(fnv1_32_values[x] == hash_val); } return TEST_SUCCESS; } -static test_return_t fnv1a_32_run (hashkit_st *hashk __attribute__((unused))) +static test_return_t fnv1a_32_run (hashkit_st *hashk) { uint32_t x; const char **ptr; + (void)hashk; for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) { uint32_t hash_val; - hash_val= hashkit_fnv1a_32(*ptr, strlen(*ptr)); + hash_val= libhashkit_fnv1a_32(*ptr, strlen(*ptr)); assert(fnv1a_32_values[x] == hash_val); } return TEST_SUCCESS; } -static test_return_t hsieh_run (hashkit_st *hashk __attribute__((unused))) +static test_return_t hsieh_run (hashkit_st *hashk) { uint32_t x; const char **ptr; + (void)hashk; for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) { uint32_t hash_val; #ifdef HAVE_HSIEH_HASH - hash_val= hashkit_hsieh(*ptr, strlen(*ptr)); + hash_val= libhashkit_hsieh(*ptr, strlen(*ptr)); #else hash_val= 1; #endif @@ -247,8 +257,14 @@ static test_return_t hsieh_run (hashkit_st *hashk __attribute__((unused))) return TEST_SUCCESS; } -static test_return_t murmur_run (hashkit_st *hashk __attribute__((unused))) +static test_return_t murmur_run (hashkit_st *hashk) { + (void)hashk; + +#ifdef WORDS_BIGENDIAN + (void)murmur_values; + return TEST_SKIPPED; +#else uint32_t x; const char **ptr; @@ -256,24 +272,29 @@ static test_return_t murmur_run (hashkit_st *hashk __attribute__((unused))) { uint32_t hash_val; - hash_val= hashkit_murmur(*ptr, strlen(*ptr)); +#ifdef HAVE_MURMUR_HASH + hash_val= libhashkit_murmur(*ptr, strlen(*ptr)); +#else + hash_val= 1; +#endif assert(murmur_values[x] == hash_val); } return TEST_SUCCESS; +#endif } -static test_return_t jenkins_run (hashkit_st *hashk __attribute__((unused))) +static test_return_t jenkins_run (hashkit_st *hashk) { uint32_t x; const char **ptr; - + (void)hashk; for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) { uint32_t hash_val; - hash_val= hashkit_jenkins(*ptr, strlen(*ptr)); + hash_val= libhashkit_jenkins(*ptr, strlen(*ptr)); assert(jenkins_values[x] == hash_val); } @@ -294,7 +315,217 @@ test_st allocation[]= { {0, 0, 0} }; +static test_return_t hashkit_digest_test(hashkit_st *hashk) +{ + uint32_t value; + value= hashkit_digest(hashk, "a", sizeof("a")); + + return TEST_SUCCESS; +} + +static test_return_t hashkit_set_function_test(hashkit_st *hashk) +{ + for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++) + { + hashkit_return_t rc; + uint32_t x; + const char **ptr; + uint32_t *list; + + rc= hashkit_set_function(hashk, algo); + + /* Hsieh is disabled most of the time for patent issues */ +#ifndef HAVE_HSIEH_HASH + if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH) + continue; +#endif + +#ifndef HAVE_MURMUR_HASH + if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_MURMUR) + continue; +#endif + + if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_CUSTOM) + continue; + + test_true(rc == HASHKIT_SUCCESS); + + switch (algo) + { + case HASHKIT_HASH_DEFAULT: + list= one_at_a_time_values; + break; + case HASHKIT_HASH_MD5: + list= md5_values; + break; + case HASHKIT_HASH_CRC: + list= crc_values; + break; + case HASHKIT_HASH_FNV1_64: + list= fnv1_64_values; + break; + case HASHKIT_HASH_FNV1A_64: + list= fnv1a_64_values; + break; + case HASHKIT_HASH_FNV1_32: + list= fnv1_32_values; + break; + case HASHKIT_HASH_FNV1A_32: + list= fnv1a_32_values; + break; + case HASHKIT_HASH_HSIEH: + list= hsieh_values; + break; + case HASHKIT_HASH_MURMUR: + list= murmur_values; + break; + case HASHKIT_HASH_JENKINS: + list= jenkins_values; + break; + case HASHKIT_HASH_CUSTOM: + case HASHKIT_HASH_MAX: + default: + list= NULL; + break; + } + + // Now we make sure we did set the hash correctly. + if (list) + { + for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) + { + uint32_t hash_val; + + hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr)); + test_true(list[x] == hash_val); + } + } + else + { + return TEST_FAILURE; + } + } + + return TEST_SUCCESS; +} + +static uint32_t hash_test_function(const char *string, size_t string_length, void *context) +{ + (void)context; + return libhashkit_md5(string, string_length); +} + +static test_return_t hashkit_set_custom_function_test(hashkit_st *hashk) +{ + hashkit_return_t rc; + uint32_t x; + const char **ptr; + + + rc= hashkit_set_custom_function(hashk, hash_test_function, NULL); + test_true(rc == HASHKIT_SUCCESS); + + for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++) + { + uint32_t hash_val; + + hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr)); + test_true(md5_values[x] == hash_val); + } + + return TEST_SUCCESS; +} + +static test_return_t hashkit_set_distribution_function_test(hashkit_st *hashk) +{ + for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++) + { + hashkit_return_t rc; + + rc= hashkit_set_distribution_function(hashk, algo); + + /* Hsieh is disabled most of the time for patent issues */ + if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH) + continue; + + if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_CUSTOM) + continue; + + test_true(rc == HASHKIT_SUCCESS); + } + + return TEST_SUCCESS; +} + +static test_return_t hashkit_set_custom_distribution_function_test(hashkit_st *hashk) +{ + hashkit_return_t rc; + + rc= hashkit_set_custom_distribution_function(hashk, hash_test_function, NULL); + test_true(rc == HASHKIT_SUCCESS); + + return TEST_SUCCESS; +} + + +static test_return_t hashkit_get_function_test(hashkit_st *hashk) +{ + for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++) + { + hashkit_return_t rc; + + if (HASHKIT_HASH_CUSTOM || HASHKIT_HASH_HSIEH) + continue; + + rc= hashkit_set_function(hashk, algo); + test_true(rc == HASHKIT_SUCCESS); + + test_true(hashkit_get_function(hashk) == algo); + } + return TEST_SUCCESS; +} + +static test_return_t hashkit_compare_test(hashkit_st *hashk) +{ + hashkit_st *clone; + + clone= hashkit_clone(NULL, hashk); + + test_true(hashkit_compare(clone, hashk)); + hashkit_free(clone); + + return TEST_SUCCESS; +} + +test_st hashkit_st_functions[] ={ + {"hashkit_digest", 0, (test_callback_fn)hashkit_digest_test}, + {"hashkit_set_function", 0, (test_callback_fn)hashkit_set_function_test}, + {"hashkit_set_custom_function", 0, (test_callback_fn)hashkit_set_custom_function_test}, + {"hashkit_get_function", 0, (test_callback_fn)hashkit_get_function_test}, + {"hashkit_set_distribution_function", 0, (test_callback_fn)hashkit_set_distribution_function_test}, + {"hashkit_set_custom_distribution_function", 0, (test_callback_fn)hashkit_set_custom_distribution_function_test}, + {"hashkit_compare", 0, (test_callback_fn)hashkit_compare_test}, + {0, 0, 0} +}; + +static test_return_t libhashkit_digest_test(hashkit_st *hashk) +{ + uint32_t value; + + (void)hashk; + + value= libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT); + + return TEST_SUCCESS; +} + +test_st library_functions[] ={ + {"libhashkit_digest", 0, (test_callback_fn)libhashkit_digest_test}, + {0, 0, 0} +}; + test_st hash_tests[] ={ + {"one_at_a_time", 0, (test_callback_fn)one_at_a_time_run }, {"md5", 0, (test_callback_fn)md5_run }, {"crc", 0, (test_callback_fn)crc_run }, {"fnv1_64", 0, (test_callback_fn)fnv1_64_run }, @@ -319,27 +550,36 @@ test_st regression[]= { collection_st collection[] ={ {"allocation", 0, 0, allocation}, - {"regression", 0, 0, regression}, + {"hashkit_st_functions", 0, 0, hashkit_st_functions}, + {"library_functions", 0, 0, library_functions}, {"hashing", 0, 0, hash_tests}, + {"regression", 0, 0, regression}, {0, 0, 0, 0} }; /* Prototypes for functions we will pass to test framework */ -void *world_create(void); +void *world_create(test_return_t *error); test_return_t world_destroy(hashkit_st *hashk); -void *world_create(void) +void *world_create(test_return_t *error) { hashkit_st *hashk_ptr; hashk_ptr= hashkit_create(&global_hashk); - assert(hashk_ptr == &global_hashk); + if (hashk_ptr != &global_hashk) + { + *error= TEST_FAILURE; + return NULL; + } + + if (hashkit_is_allocated(hashk_ptr) == true) + { + *error= TEST_FAILURE; + return NULL; + } - // First we test if hashk is even valid - assert(hashkit_is_initialized(hashk_ptr) == true); - assert(hashkit_is_allocated(hashk_ptr) == false); - assert(hashk_ptr->continuum == NULL); + *error= TEST_SUCCESS; return hashk_ptr; } @@ -348,7 +588,6 @@ void *world_create(void) test_return_t world_destroy(hashkit_st *hashk) { // Did we get back what we expected? - assert(hashkit_is_initialized(hashk) == true); assert(hashkit_is_allocated(hashk) == false); hashkit_free(&global_hashk);