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_generate_value_test(hashkit_st
*hashk
)
303 value
= hashkit_generate_value(hashk
, "a", sizeof("a"));
308 static test_return_t
hashkit_set_base_function_test(hashkit_st
*hashk
)
310 for (hashkit_hash_algorithm_t algo
= HASHKIT_HASH_DEFAULT
; algo
< HASHKIT_HASH_MAX
; algo
++)
317 rc
= hashkit_set_base_function(hashk
, algo
);
319 /* Hsieh is disabled most of the time for patent issues */
320 if (rc
== HASHKIT_FAILURE
&& algo
== HASHKIT_HASH_HSIEH
)
323 test_true(rc
== HASHKIT_SUCCESS
);
327 case HASHKIT_HASH_DEFAULT
:
328 list
= one_at_a_time_values
;
330 case HASHKIT_HASH_MD5
:
333 case HASHKIT_HASH_CRC
:
336 case HASHKIT_HASH_FNV1_64
:
337 list
= fnv1_64_values
;
339 case HASHKIT_HASH_FNV1A_64
:
340 list
= fnv1a_64_values
;
342 case HASHKIT_HASH_FNV1_32
:
343 list
= fnv1_32_values
;
345 case HASHKIT_HASH_FNV1A_32
:
346 list
= fnv1a_32_values
;
348 case HASHKIT_HASH_HSIEH
:
351 case HASHKIT_HASH_MURMUR
:
354 case HASHKIT_HASH_JENKINS
:
355 list
= jenkins_values
;
357 case HASHKIT_HASH_MAX
:
360 test_fail("We ended up on a non-existent hash");
363 // Now we make sure we did set the hash correctly.
364 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
368 hash_val
= hashkit_generate_value(hashk
, *ptr
, strlen(*ptr
));
369 test_true(list
[x
] == hash_val
);
376 static uint32_t hash_test_function(const char *string
, size_t string_length
, void *context
)
379 return libhashkit_md5(string
, string_length
);
382 static test_return_t
hashkit_set_base_function_custom_test(hashkit_st
*hashk
)
389 rc
= hashkit_set_base_function_custom(hashk
, hash_test_function
, NULL
);
390 test_true(rc
== HASHKIT_SUCCESS
);
392 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
396 hash_val
= hashkit_generate_value(hashk
, *ptr
, strlen(*ptr
));
397 test_true(md5_values
[x
] == hash_val
);
403 test_st hashkit_st_functions
[] ={
404 {"hashkit_generate_value", 0, (test_callback_fn
)hashkit_generate_value_test
},
405 {"hashkit_set_base_function", 0, (test_callback_fn
)hashkit_set_base_function_test
},
406 {"hashkit_set_base_function_custom", 0, (test_callback_fn
)hashkit_set_base_function_custom_test
},
410 static test_return_t
libhashkit_generate_value_test(hashkit_st
*hashk
)
416 value
= libhashkit_generate_value("a", sizeof("a"), HASHKIT_HASH_DEFAULT
);
421 test_st library_functions
[] ={
422 {"libhashkit_generate_value", 0, (test_callback_fn
)libhashkit_generate_value_test
},
426 test_st hash_tests
[] ={
427 {"one_at_a_time", 0, (test_callback_fn
)one_at_a_time_run
},
428 {"md5", 0, (test_callback_fn
)md5_run
},
429 {"crc", 0, (test_callback_fn
)crc_run
},
430 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
431 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
432 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
433 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
434 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
435 {"murmur", 0, (test_callback_fn
)murmur_run
},
436 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
437 {0, 0, (test_callback_fn
)0}
441 * The following test suite is used to verify that we don't introduce
442 * regression bugs. If you want more information about the bug / test,
443 * you should look in the bug report at
444 * http://bugs.launchpad.net/libmemcached
446 test_st regression
[]= {
450 collection_st collection
[] ={
451 {"allocation", 0, 0, allocation
},
452 {"hashkit_st_functions", 0, 0, hashkit_st_functions
},
453 {"library_functions", 0, 0, library_functions
},
454 {"hashing", 0, 0, hash_tests
},
455 {"regression", 0, 0, regression
},
459 /* Prototypes for functions we will pass to test framework */
460 void *world_create(test_return_t
*error
);
461 test_return_t
world_destroy(hashkit_st
*hashk
);
463 void *world_create(test_return_t
*error
)
465 hashkit_st
*hashk_ptr
;
467 hashk_ptr
= hashkit_create(&global_hashk
);
469 if (hashk_ptr
!= &global_hashk
)
471 *error
= TEST_FAILURE
;
475 if (hashkit_is_allocated(hashk_ptr
) == true)
477 *error
= TEST_FAILURE
;
481 *error
= TEST_SUCCESS
;
487 test_return_t
world_destroy(hashkit_st
*hashk
)
489 // Did we get back what we expected?
490 assert(hashkit_is_allocated(hashk
) == false);
491 hashkit_free(&global_hashk
);
496 void get_world(world_st
*world
)
498 world
->collections
= collection
;
499 world
->create
= (test_callback_create_fn
)world_create
;
500 world
->destroy
= (test_callback_fn
)world_destroy
;