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
)))
255 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
259 hash_val
= hashkit_murmur(*ptr
, strlen(*ptr
));
260 assert(murmur_values
[x
] == hash_val
);
266 static test_return_t
jenkins_run (hashkit_st
*hashk
__attribute__((unused
)))
272 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
276 hash_val
= hashkit_jenkins(*ptr
, strlen(*ptr
));
277 assert(jenkins_values
[x
] == hash_val
);
287 @brief now we list out the tests.
290 test_st allocation
[]= {
291 {"init", 0, (test_callback_fn
)init_test
},
292 {"create and free", 0, (test_callback_fn
)allocation_test
},
293 {"clone", 0, (test_callback_fn
)clone_test
},
297 test_st hash_tests
[] ={
298 {"md5", 0, (test_callback_fn
)md5_run
},
299 {"crc", 0, (test_callback_fn
)crc_run
},
300 {"fnv1_64", 0, (test_callback_fn
)fnv1_64_run
},
301 {"fnv1a_64", 0, (test_callback_fn
)fnv1a_64_run
},
302 {"fnv1_32", 0, (test_callback_fn
)fnv1_32_run
},
303 {"fnv1a_32", 0, (test_callback_fn
)fnv1a_32_run
},
304 {"hsieh", 0, (test_callback_fn
)hsieh_run
},
305 {"murmur", 0, (test_callback_fn
)murmur_run
},
306 {"jenkis", 0, (test_callback_fn
)jenkins_run
},
307 {0, 0, (test_callback_fn
)0}
311 * The following test suite is used to verify that we don't introduce
312 * regression bugs. If you want more information about the bug / test,
313 * you should look in the bug report at
314 * http://bugs.launchpad.net/libmemcached
316 test_st regression
[]= {
320 collection_st collection
[] ={
321 {"allocation", 0, 0, allocation
},
322 {"regression", 0, 0, regression
},
323 {"hashing", 0, 0, hash_tests
},
327 /* Prototypes for functions we will pass to test framework */
328 void *world_create(void);
329 test_return_t
world_destroy(hashkit_st
*hashk
);
331 void *world_create(void)
333 hashkit_st
*hashk_ptr
;
335 hashk_ptr
= hashkit_create(&global_hashk
);
337 assert(hashk_ptr
== &global_hashk
);
339 // First we test if hashk is even valid
340 assert(hashkit_is_initialized(hashk_ptr
) == true);
341 assert(hashkit_is_allocated(hashk_ptr
) == false);
342 assert(hashk_ptr
->continuum
== NULL
);
348 test_return_t
world_destroy(hashkit_st
*hashk
)
350 // Did we get back what we expected?
351 assert(hashkit_is_initialized(hashk
) == true);
352 assert(hashkit_is_allocated(hashk
) == false);
353 hashkit_free(&global_hashk
);
358 void get_world(world_st
*world
)
360 world
->collections
= collection
;
361 world
->create
= (test_callback_create_fn
)world_create
;
362 world
->destroy
= (test_callback_fn
)world_destroy
;