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
= hashkit_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
= hashkit_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
= hashkit_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
= hashkit_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
= hashkit_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
= hashkit_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
= hashkit_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
= hashkit_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
= hashkit_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
= hashkit_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 test_return_t
hashkit_set_base_function_custom_test(hashkit_st
*hashk
)
383 rc
= hashkit_set_base_function_custom(hashk
, hashkit_md5
, NULL
);
384 test_true(rc
== HASHKIT_SUCCESS
);
386 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
390 hash_val
= hashkit_generate_value(hashk
, *ptr
, strlen(*ptr
));
391 test_true(md5_values
[x
] == hash_val
);
397 test_st hashkit_st_functions
[] ={
398 {"hashkit_generate_value", 0, (test_callback_fn
)hashkit_generate_value_test
},
399 {"hashkit_set_base_function", 0, (test_callback_fn
)hashkit_set_base_function_test
},
400 {"hashkit_set_base_function_custom", 0, (test_callback_fn
)hashkit_set_base_function_custom_test
},
404 static test_return_t
libhashkit_generate_value_test(hashkit_st
*hashk
)
410 value
= libhashkit_generate_value("a", sizeof("a"), HASHKIT_HASH_DEFAULT
);
415 test_st library_functions
[] ={
416 {"libhashkit_generate_value", 0, (test_callback_fn
)libhashkit_generate_value_test
},
420 test_st hash_tests
[] ={
421 {"one_at_a_time", 0, (test_callback_fn
)one_at_a_time_run
},
422 {"md5", 0, (test_callback_fn
)md5_run
},
423 {"crc", 0, (test_callback_fn
)crc_run
},
424 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
425 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
426 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
427 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
428 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
429 {"murmur", 0, (test_callback_fn
)murmur_run
},
430 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
431 {0, 0, (test_callback_fn
)0}
435 * The following test suite is used to verify that we don't introduce
436 * regression bugs. If you want more information about the bug / test,
437 * you should look in the bug report at
438 * http://bugs.launchpad.net/libmemcached
440 test_st regression
[]= {
444 collection_st collection
[] ={
445 {"allocation", 0, 0, allocation
},
446 {"hashkit_st_functions", 0, 0, hashkit_st_functions
},
447 {"library_functions", 0, 0, library_functions
},
448 {"hashing", 0, 0, hash_tests
},
449 {"regression", 0, 0, regression
},
453 /* Prototypes for functions we will pass to test framework */
454 void *world_create(test_return_t
*error
);
455 test_return_t
world_destroy(hashkit_st
*hashk
);
457 void *world_create(test_return_t
*error
)
459 hashkit_st
*hashk_ptr
;
461 hashk_ptr
= hashkit_create(&global_hashk
);
463 if (hashk_ptr
!= &global_hashk
)
465 *error
= TEST_FAILURE
;
469 if (hashkit_is_allocated(hashk_ptr
) == true)
471 *error
= TEST_FAILURE
;
475 *error
= TEST_SUCCESS
;
481 test_return_t
world_destroy(hashkit_st
*hashk
)
483 // Did we get back what we expected?
484 assert(hashkit_is_allocated(hashk
) == false);
485 hashkit_free(&global_hashk
);
490 void get_world(world_st
*world
)
492 world
->collections
= collection
;
493 world
->create
= (test_callback_create_fn
)world_create
;
494 world
->destroy
= (test_callback_fn
)world_destroy
;