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
);
38 test_truth(hashk_ptr
);
39 test_truth(hashk_ptr
== &hashk
);
40 test_truth(hashkit_is_initialized(&hashk
) == true);
41 test_truth(hashkit_is_allocated(hashk_ptr
) == false);
43 hashkit_free(hashk_ptr
);
45 test_truth(hashkit_is_initialized(&hashk
) == false);
50 static test_return_t
allocation_test(void *not_used
__attribute__((unused
)))
52 hashkit_st
*hashk_ptr
;
54 hashk_ptr
= hashkit_create(NULL
);
55 test_truth(hashk_ptr
);
56 test_truth(hashkit_is_allocated(hashk_ptr
) == true);
57 test_truth(hashkit_is_initialized(hashk_ptr
) == true);
58 hashkit_free(hashk_ptr
);
63 static test_return_t
clone_test(hashkit_st
*hashk
)
65 // First we make sure that the testing system is giving us what we expect.
66 assert(&global_hashk
== hashk
);
68 // Second we test if hashk is even valid
69 test_truth(hashkit_is_initialized(hashk
) == true);
73 hashkit_st
*hashk_ptr
;
74 hashk_ptr
= hashkit_clone(NULL
, NULL
);
75 test_truth(hashk_ptr
);
76 test_truth(hashkit_is_allocated(hashk_ptr
) == true);
77 test_truth(hashkit_is_initialized(hashk_ptr
) == true);
78 hashkit_free(hashk_ptr
);
81 /* Can we init from null? */
83 hashkit_st
*hashk_ptr
;
85 hashk_ptr
= hashkit_clone(NULL
, hashk
);
87 test_truth(hashk_ptr
);
88 test_truth(hashkit_is_allocated(hashk_ptr
) == true);
89 test_truth(hashkit_is_initialized(hashk_ptr
) == true);
91 test_truth(hashk_ptr
->distribution
== hashk
->distribution
);
92 test_truth(hashk_ptr
->continuum_count
== hashk
->continuum_count
);
93 test_truth(hashk_ptr
->continuum_points_count
== hashk
->continuum_points_count
);
94 test_truth(hashk_ptr
->list_size
== hashk
->list_size
);
95 test_truth(hashk_ptr
->context_size
== hashk
->context_size
);
96 test_truth(hashk_ptr
->continuum
== NULL
);
97 test_truth(hashk_ptr
->hash_fn
== hashk
->hash_fn
);
98 test_truth(hashk_ptr
->active_fn
== hashk
->active_fn
);
99 test_truth(hashk_ptr
->continuum_hash_fn
== hashk
->continuum_hash_fn
);
100 test_truth(hashk_ptr
->continuum_key_fn
== hashk
->continuum_key_fn
);
101 test_truth(hashk_ptr
->sort_fn
== hashk
->sort_fn
);
102 test_truth(hashk_ptr
->weight_fn
== hashk
->weight_fn
);
103 test_truth(hashk_ptr
->list
== hashk
->list
);
105 hashkit_free(hashk_ptr
);
108 /* Can we init from struct? */
110 hashkit_st declared_clone
;
111 hashkit_st
*hash_clone
;
113 hash_clone
= hashkit_clone(&declared_clone
, NULL
);
114 test_truth(hash_clone
);
116 hashkit_free(hash_clone
);
119 /* Can we init from struct? */
121 hashkit_st declared_clone
;
122 hashkit_st
*hash_clone
;
123 memset(&declared_clone
, 0 , sizeof(hashkit_st
));
124 hash_clone
= hashkit_clone(&declared_clone
, hashk
);
125 test_truth(hash_clone
);
126 hashkit_free(hash_clone
);
133 static test_return_t
md5_run (hashkit_st
*hashk
__attribute__((unused
)))
138 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
142 hash_val
= hashkit_md5(*ptr
, strlen(*ptr
));
143 test_truth(md5_values
[x
] == hash_val
);
149 static test_return_t
crc_run (hashkit_st
*hashk
__attribute__((unused
)))
154 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
158 hash_val
= hashkit_crc32(*ptr
, strlen(*ptr
));
159 assert(crc_values
[x
] == hash_val
);
165 static test_return_t
fnv1_64_run (hashkit_st
*hashk
__attribute__((unused
)))
170 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
174 hash_val
= hashkit_fnv1_64(*ptr
, strlen(*ptr
));
175 assert(fnv1_64_values
[x
] == hash_val
);
181 static test_return_t
fnv1a_64_run (hashkit_st
*hashk
__attribute__((unused
)))
186 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
190 hash_val
= hashkit_fnv1a_64(*ptr
, strlen(*ptr
));
191 assert(fnv1a_64_values
[x
] == hash_val
);
197 static test_return_t
fnv1_32_run (hashkit_st
*hashk
__attribute__((unused
)))
203 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
207 hash_val
= hashkit_fnv1_32(*ptr
, strlen(*ptr
));
208 assert(fnv1_32_values
[x
] == hash_val
);
214 static test_return_t
fnv1a_32_run (hashkit_st
*hashk
__attribute__((unused
)))
219 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
223 hash_val
= hashkit_fnv1a_32(*ptr
, strlen(*ptr
));
224 assert(fnv1a_32_values
[x
] == hash_val
);
230 static test_return_t
hsieh_run (hashkit_st
*hashk
__attribute__((unused
)))
235 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
239 #ifdef HAVE_HSIEH_HASH
240 hash_val
= hashkit_hsieh(*ptr
, strlen(*ptr
));
244 assert(hsieh_values
[x
] == hash_val
);
250 static test_return_t
murmur_run (hashkit_st
*hashk
__attribute__((unused
)))
258 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
262 hash_val
= hashkit_murmur(*ptr
, strlen(*ptr
));
263 assert(murmur_values
[x
] == hash_val
);
270 static test_return_t
jenkins_run (hashkit_st
*hashk
__attribute__((unused
)))
276 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
280 hash_val
= hashkit_jenkins(*ptr
, strlen(*ptr
));
281 assert(jenkins_values
[x
] == hash_val
);
291 @brief now we list out the tests.
294 test_st allocation
[]= {
295 {"init", 0, (test_callback_fn
)init_test
},
296 {"create and free", 0, (test_callback_fn
)allocation_test
},
297 {"clone", 0, (test_callback_fn
)clone_test
},
301 test_st hash_tests
[] ={
302 {"md5", 0, (test_callback_fn
)md5_run
},
303 {"crc", 0, (test_callback_fn
)crc_run
},
304 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
305 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
306 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
307 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
308 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
309 {"murmur", 0, (test_callback_fn
)murmur_run
},
310 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
311 {0, 0, (test_callback_fn
)0}
315 * The following test suite is used to verify that we don't introduce
316 * regression bugs. If you want more information about the bug / test,
317 * you should look in the bug report at
318 * http://bugs.launchpad.net/libmemcached
320 test_st regression
[]= {
324 collection_st collection
[] ={
325 {"allocation", 0, 0, allocation
},
326 {"regression", 0, 0, regression
},
327 {"hashing", 0, 0, hash_tests
},
331 /* Prototypes for functions we will pass to test framework */
332 void *world_create(void);
333 test_return_t
world_destroy(hashkit_st
*hashk
);
335 void *world_create(void)
337 hashkit_st
*hashk_ptr
;
339 hashk_ptr
= hashkit_create(&global_hashk
);
341 assert(hashk_ptr
== &global_hashk
);
343 // First we test if hashk is even valid
344 assert(hashkit_is_initialized(hashk_ptr
) == true);
345 assert(hashkit_is_allocated(hashk_ptr
) == false);
346 assert(hashk_ptr
->continuum
== NULL
);
352 test_return_t
world_destroy(hashkit_st
*hashk
)
354 // Did we get back what we expected?
355 assert(hashkit_is_initialized(hashk
) == true);
356 assert(hashkit_is_allocated(hashk
) == false);
357 hashkit_free(&global_hashk
);
362 void get_world(world_st
*world
)
364 world
->collections
= collection
;
365 world
->create
= (test_callback_create_fn
)world_create
;
366 world
->destroy
= (test_callback_fn
)world_destroy
;