d97b40c8cf57d8401ab4952548692b1b2c2f8a3a
[awesomized/libmemcached] / tests / hashkit_functions.c
1 /* libHashKit Functions Test
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include <libhashkit/hashkit.h>
15
16 #include "test.h"
17
18 #include "hash_results.h"
19
20 static hashkit_st global_hashk;
21
22 /**
23 @brief hash_test_st is a structure we use in testing. It is currently empty.
24 */
25 typedef struct hash_test_st hash_test_st;
26
27 struct hash_test_st
28 {
29 bool _unused;
30 };
31
32 static test_return_t init_test(void *not_used __attribute__((unused)))
33 {
34 hashkit_st hashk;
35 hashkit_st *hashk_ptr;
36
37 hashk_ptr= hashkit_create(&hashk);
38 test_true(hashk_ptr);
39 test_true(hashk_ptr == &hashk);
40 test_true(hashkit_is_allocated(hashk_ptr) == false);
41
42 hashkit_free(hashk_ptr);
43
44 return TEST_SUCCESS;
45 }
46
47 static test_return_t allocation_test(void *not_used __attribute__((unused)))
48 {
49 hashkit_st *hashk_ptr;
50
51 hashk_ptr= hashkit_create(NULL);
52 test_true(hashk_ptr);
53 test_true(hashkit_is_allocated(hashk_ptr) == true);
54 hashkit_free(hashk_ptr);
55
56 return TEST_SUCCESS;
57 }
58
59 static test_return_t clone_test(hashkit_st *hashk)
60 {
61 // First we make sure that the testing system is giving us what we expect.
62 assert(&global_hashk == hashk);
63
64 // Second we test if hashk is even valid
65
66 /* All null? */
67 {
68 hashkit_st *hashk_ptr;
69 hashk_ptr= hashkit_clone(NULL, NULL);
70 test_true(hashk_ptr);
71 test_true(hashkit_is_allocated(hashk_ptr));
72 hashkit_free(hashk_ptr);
73 }
74
75 /* Can we init from null? */
76 {
77 hashkit_st *hashk_ptr;
78
79 hashk_ptr= hashkit_clone(NULL, hashk);
80
81 test_true(hashk_ptr);
82 test_true(hashkit_is_allocated(hashk_ptr));
83
84 hashkit_free(hashk_ptr);
85 }
86
87 /* Can we init from struct? */
88 {
89 hashkit_st declared_clone;
90 hashkit_st *hash_clone;
91
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));
96
97 hashkit_free(hash_clone);
98 }
99
100 /* Can we init from struct? */
101 {
102 hashkit_st declared_clone;
103 hashkit_st *hash_clone;
104
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));
109
110 hashkit_free(hash_clone);
111 }
112
113 return TEST_SUCCESS;
114 }
115
116 static test_return_t one_at_a_time_run (hashkit_st *hashk __attribute__((unused)))
117 {
118 uint32_t x;
119 const char **ptr;
120
121 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
122 {
123 uint32_t hash_val;
124
125 hash_val= libhashkit_one_at_a_time(*ptr, strlen(*ptr));
126 test_true(one_at_a_time_values[x] == hash_val);
127 }
128
129 return TEST_SUCCESS;
130 }
131
132 static test_return_t md5_run (hashkit_st *hashk __attribute__((unused)))
133 {
134 uint32_t x;
135 const char **ptr;
136
137 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
138 {
139 uint32_t hash_val;
140
141 hash_val= libhashkit_md5(*ptr, strlen(*ptr));
142 test_true(md5_values[x] == hash_val);
143 }
144
145 return TEST_SUCCESS;
146 }
147
148 static test_return_t crc_run (hashkit_st *hashk __attribute__((unused)))
149 {
150 uint32_t x;
151 const char **ptr;
152
153 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
154 {
155 uint32_t hash_val;
156
157 hash_val= libhashkit_crc32(*ptr, strlen(*ptr));
158 assert(crc_values[x] == hash_val);
159 }
160
161 return TEST_SUCCESS;
162 }
163
164 static test_return_t fnv1_64_run (hashkit_st *hashk __attribute__((unused)))
165 {
166 uint32_t x;
167 const char **ptr;
168
169 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
170 {
171 uint32_t hash_val;
172
173 hash_val= libhashkit_fnv1_64(*ptr, strlen(*ptr));
174 assert(fnv1_64_values[x] == hash_val);
175 }
176
177 return TEST_SUCCESS;
178 }
179
180 static test_return_t fnv1a_64_run (hashkit_st *hashk __attribute__((unused)))
181 {
182 uint32_t x;
183 const char **ptr;
184
185 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
186 {
187 uint32_t hash_val;
188
189 hash_val= libhashkit_fnv1a_64(*ptr, strlen(*ptr));
190 assert(fnv1a_64_values[x] == hash_val);
191 }
192
193 return TEST_SUCCESS;
194 }
195
196 static test_return_t fnv1_32_run (hashkit_st *hashk __attribute__((unused)))
197 {
198 uint32_t x;
199 const char **ptr;
200
201
202 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
203 {
204 uint32_t hash_val;
205
206 hash_val= libhashkit_fnv1_32(*ptr, strlen(*ptr));
207 assert(fnv1_32_values[x] == hash_val);
208 }
209
210 return TEST_SUCCESS;
211 }
212
213 static test_return_t fnv1a_32_run (hashkit_st *hashk __attribute__((unused)))
214 {
215 uint32_t x;
216 const char **ptr;
217
218 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
219 {
220 uint32_t hash_val;
221
222 hash_val= libhashkit_fnv1a_32(*ptr, strlen(*ptr));
223 assert(fnv1a_32_values[x] == hash_val);
224 }
225
226 return TEST_SUCCESS;
227 }
228
229 static test_return_t hsieh_run (hashkit_st *hashk __attribute__((unused)))
230 {
231 uint32_t x;
232 const char **ptr;
233
234 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
235 {
236 uint32_t hash_val;
237
238 #ifdef HAVE_HSIEH_HASH
239 hash_val= libhashkit_hsieh(*ptr, strlen(*ptr));
240 #else
241 hash_val= 1;
242 #endif
243 assert(hsieh_values[x] == hash_val);
244 }
245
246 return TEST_SUCCESS;
247 }
248
249 static test_return_t murmur_run (hashkit_st *hashk __attribute__((unused)))
250 {
251 #ifdef __sparc
252 return TEST_SKIPPED;
253 #else
254 uint32_t x;
255 const char **ptr;
256
257 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
258 {
259 uint32_t hash_val;
260
261 hash_val= libhashkit_murmur(*ptr, strlen(*ptr));
262 assert(murmur_values[x] == hash_val);
263 }
264
265 return TEST_SUCCESS;
266 #endif
267 }
268
269 static test_return_t jenkins_run (hashkit_st *hashk __attribute__((unused)))
270 {
271 uint32_t x;
272 const char **ptr;
273
274
275 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
276 {
277 uint32_t hash_val;
278
279 hash_val= libhashkit_jenkins(*ptr, strlen(*ptr));
280 assert(jenkins_values[x] == hash_val);
281 }
282
283 return TEST_SUCCESS;
284 }
285
286
287
288
289 /**
290 @brief now we list out the tests.
291 */
292
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},
297 {0, 0, 0}
298 };
299
300 static test_return_t hashkit_generate_value_test(hashkit_st *hashk)
301 {
302 uint32_t value;
303 value= hashkit_generate_value(hashk, "a", sizeof("a"));
304
305 return TEST_SUCCESS;
306 }
307
308 static test_return_t hashkit_set_base_function_test(hashkit_st *hashk)
309 {
310 for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++)
311 {
312 hashkit_return_t rc;
313 uint32_t x;
314 const char **ptr;
315 uint32_t *list;
316
317 rc= hashkit_set_base_function(hashk, algo);
318
319 /* Hsieh is disabled most of the time for patent issues */
320 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH)
321 continue;
322
323 test_true(rc == HASHKIT_SUCCESS);
324
325 switch (algo)
326 {
327 case HASHKIT_HASH_DEFAULT:
328 list= one_at_a_time_values;
329 break;
330 case HASHKIT_HASH_MD5:
331 list= md5_values;
332 break;
333 case HASHKIT_HASH_CRC:
334 list= crc_values;
335 break;
336 case HASHKIT_HASH_FNV1_64:
337 list= fnv1_64_values;
338 break;
339 case HASHKIT_HASH_FNV1A_64:
340 list= fnv1a_64_values;
341 break;
342 case HASHKIT_HASH_FNV1_32:
343 list= fnv1_32_values;
344 break;
345 case HASHKIT_HASH_FNV1A_32:
346 list= fnv1a_32_values;
347 break;
348 case HASHKIT_HASH_HSIEH:
349 list= hsieh_values;
350 break;
351 case HASHKIT_HASH_MURMUR:
352 list= murmur_values;
353 break;
354 case HASHKIT_HASH_JENKINS:
355 list= jenkins_values;
356 break;
357 case HASHKIT_HASH_MAX:
358 default:
359 list= NULL;
360 test_fail("We ended up on a non-existent hash");
361 }
362
363 // Now we make sure we did set the hash correctly.
364 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
365 {
366 uint32_t hash_val;
367
368 hash_val= hashkit_generate_value(hashk, *ptr, strlen(*ptr));
369 test_true(list[x] == hash_val);
370 }
371 }
372
373 return TEST_SUCCESS;
374 }
375
376 static uint32_t hash_test_function(const char *string, size_t string_length, void *context)
377 {
378 (void)context;
379 return libhashkit_md5(string, string_length);
380 }
381
382 static test_return_t hashkit_set_base_function_custom_test(hashkit_st *hashk)
383 {
384 hashkit_return_t rc;
385 uint32_t x;
386 const char **ptr;
387
388
389 rc= hashkit_set_base_function_custom(hashk, hash_test_function, NULL);
390 test_true(rc == HASHKIT_SUCCESS);
391
392 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
393 {
394 uint32_t hash_val;
395
396 hash_val= hashkit_generate_value(hashk, *ptr, strlen(*ptr));
397 test_true(md5_values[x] == hash_val);
398 }
399
400 return TEST_SUCCESS;
401 }
402
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},
407 {0, 0, 0}
408 };
409
410 static test_return_t libhashkit_generate_value_test(hashkit_st *hashk)
411 {
412 uint32_t value;
413
414 (void)hashk;
415
416 value= libhashkit_generate_value("a", sizeof("a"), HASHKIT_HASH_DEFAULT);
417
418 return TEST_SUCCESS;
419 }
420
421 test_st library_functions[] ={
422 {"libhashkit_generate_value", 0, (test_callback_fn)libhashkit_generate_value_test},
423 {0, 0, 0}
424 };
425
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}
438 };
439
440 /*
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
445 */
446 test_st regression[]= {
447 {0, 0, 0}
448 };
449
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},
456 {0, 0, 0, 0}
457 };
458
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);
462
463 void *world_create(test_return_t *error)
464 {
465 hashkit_st *hashk_ptr;
466
467 hashk_ptr= hashkit_create(&global_hashk);
468
469 if (hashk_ptr != &global_hashk)
470 {
471 *error= TEST_FAILURE;
472 return NULL;
473 }
474
475 if (hashkit_is_allocated(hashk_ptr) == true)
476 {
477 *error= TEST_FAILURE;
478 return NULL;
479 }
480
481 *error= TEST_SUCCESS;
482
483 return hashk_ptr;
484 }
485
486
487 test_return_t world_destroy(hashkit_st *hashk)
488 {
489 // Did we get back what we expected?
490 assert(hashkit_is_allocated(hashk) == false);
491 hashkit_free(&global_hashk);
492
493 return TEST_SUCCESS;
494 }
495
496 void get_world(world_st *world)
497 {
498 world->collections= collection;
499 world->create= (test_callback_create_fn)world_create;
500 world->destroy= (test_callback_fn)world_destroy;
501 }