1d548f4abeaa29c78d79a24757d67ac57d5927db
[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_digest_test(hashkit_st *hashk)
301 {
302 uint32_t value;
303 value= hashkit_digest(hashk, "a", sizeof("a"));
304
305 return TEST_SUCCESS;
306 }
307
308 static test_return_t hashkit_set_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_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 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_CUSTOM)
324 continue;
325
326 test_true(rc == HASHKIT_SUCCESS);
327
328 switch (algo)
329 {
330 case HASHKIT_HASH_DEFAULT:
331 list= one_at_a_time_values;
332 break;
333 case HASHKIT_HASH_MD5:
334 list= md5_values;
335 break;
336 case HASHKIT_HASH_CRC:
337 list= crc_values;
338 break;
339 case HASHKIT_HASH_FNV1_64:
340 list= fnv1_64_values;
341 break;
342 case HASHKIT_HASH_FNV1A_64:
343 list= fnv1a_64_values;
344 break;
345 case HASHKIT_HASH_FNV1_32:
346 list= fnv1_32_values;
347 break;
348 case HASHKIT_HASH_FNV1A_32:
349 list= fnv1a_32_values;
350 break;
351 case HASHKIT_HASH_HSIEH:
352 list= hsieh_values;
353 break;
354 case HASHKIT_HASH_MURMUR:
355 list= murmur_values;
356 break;
357 case HASHKIT_HASH_JENKINS:
358 list= jenkins_values;
359 break;
360 case HASHKIT_HASH_CUSTOM:
361 case HASHKIT_HASH_MAX:
362 default:
363 list= NULL;
364 test_fail("We ended up on a non-existent hash");
365 }
366
367 // Now we make sure we did set the hash correctly.
368 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
369 {
370 uint32_t hash_val;
371
372 hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr));
373 test_true(list[x] == hash_val);
374 }
375 }
376
377 return TEST_SUCCESS;
378 }
379
380 static uint32_t hash_test_function(const char *string, size_t string_length, void *context)
381 {
382 (void)context;
383 return libhashkit_md5(string, string_length);
384 }
385
386 static test_return_t hashkit_set_custom_function_test(hashkit_st *hashk)
387 {
388 hashkit_return_t rc;
389 uint32_t x;
390 const char **ptr;
391
392
393 rc= hashkit_set_custom_function(hashk, hash_test_function, NULL);
394 test_true(rc == HASHKIT_SUCCESS);
395
396 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
397 {
398 uint32_t hash_val;
399
400 hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr));
401 test_true(md5_values[x] == hash_val);
402 }
403
404 return TEST_SUCCESS;
405 }
406
407 static test_return_t hashkit_set_distribution_function_test(hashkit_st *hashk)
408 {
409 for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++)
410 {
411 hashkit_return_t rc;
412
413 rc= hashkit_set_distribution_function(hashk, algo);
414
415 /* Hsieh is disabled most of the time for patent issues */
416 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH)
417 continue;
418
419 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_CUSTOM)
420 continue;
421
422 test_true(rc == HASHKIT_SUCCESS);
423 }
424
425 return TEST_SUCCESS;
426 }
427
428 static test_return_t hashkit_set_custom_distribution_function_test(hashkit_st *hashk)
429 {
430 hashkit_return_t rc;
431
432 rc= hashkit_set_custom_distribution_function(hashk, hash_test_function, NULL);
433 test_true(rc == HASHKIT_SUCCESS);
434
435 return TEST_SUCCESS;
436 }
437
438
439 static test_return_t hashkit_get_function_test(hashkit_st *hashk)
440 {
441 for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++)
442 {
443 hashkit_return_t rc;
444
445 if (HASHKIT_HASH_CUSTOM || HASHKIT_HASH_HSIEH)
446 continue;
447
448 rc= hashkit_set_function(hashk, algo);
449 test_true(rc == HASHKIT_SUCCESS);
450
451 test_true(hashkit_get_function(hashk) == algo);
452 }
453 return TEST_SUCCESS;
454 }
455
456 static test_return_t hashkit_compare_test(hashkit_st *hashk)
457 {
458 hashkit_st *clone;
459
460 clone= hashkit_clone(NULL, hashk);
461
462 test_true(hashkit_compare(clone, hashk));
463
464 return TEST_SUCCESS;
465 }
466
467 test_st hashkit_st_functions[] ={
468 {"hashkit_digest", 0, (test_callback_fn)hashkit_digest_test},
469 {"hashkit_set_function", 0, (test_callback_fn)hashkit_set_function_test},
470 {"hashkit_set_custom_function", 0, (test_callback_fn)hashkit_set_custom_function_test},
471 {"hashkit_get_function", 0, (test_callback_fn)hashkit_get_function_test},
472 {"hashkit_set_distribution_function", 0, (test_callback_fn)hashkit_set_distribution_function_test},
473 {"hashkit_set_custom_distribution_function", 0, (test_callback_fn)hashkit_set_custom_distribution_function_test},
474 {"hashkit_compare", 0, (test_callback_fn)hashkit_compare_test},
475 {0, 0, 0}
476 };
477
478 static test_return_t libhashkit_digest_test(hashkit_st *hashk)
479 {
480 uint32_t value;
481
482 (void)hashk;
483
484 value= libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT);
485
486 return TEST_SUCCESS;
487 }
488
489 test_st library_functions[] ={
490 {"libhashkit_digest", 0, (test_callback_fn)libhashkit_digest_test},
491 {0, 0, 0}
492 };
493
494 test_st hash_tests[] ={
495 {"one_at_a_time", 0, (test_callback_fn)one_at_a_time_run },
496 {"md5", 0, (test_callback_fn)md5_run },
497 {"crc", 0, (test_callback_fn)crc_run },
498 {"fnv1_64", 0, (test_callback_fn)fnv1_64_run },
499 {"fnv1a_64", 0, (test_callback_fn)fnv1a_64_run },
500 {"fnv1_32", 0, (test_callback_fn)fnv1_32_run },
501 {"fnv1a_32", 0, (test_callback_fn)fnv1a_32_run },
502 {"hsieh", 0, (test_callback_fn)hsieh_run },
503 {"murmur", 0, (test_callback_fn)murmur_run },
504 {"jenkis", 0, (test_callback_fn)jenkins_run },
505 {0, 0, (test_callback_fn)0}
506 };
507
508 /*
509 * The following test suite is used to verify that we don't introduce
510 * regression bugs. If you want more information about the bug / test,
511 * you should look in the bug report at
512 * http://bugs.launchpad.net/libmemcached
513 */
514 test_st regression[]= {
515 {0, 0, 0}
516 };
517
518 collection_st collection[] ={
519 {"allocation", 0, 0, allocation},
520 {"hashkit_st_functions", 0, 0, hashkit_st_functions},
521 {"library_functions", 0, 0, library_functions},
522 {"hashing", 0, 0, hash_tests},
523 {"regression", 0, 0, regression},
524 {0, 0, 0, 0}
525 };
526
527 /* Prototypes for functions we will pass to test framework */
528 void *world_create(test_return_t *error);
529 test_return_t world_destroy(hashkit_st *hashk);
530
531 void *world_create(test_return_t *error)
532 {
533 hashkit_st *hashk_ptr;
534
535 hashk_ptr= hashkit_create(&global_hashk);
536
537 if (hashk_ptr != &global_hashk)
538 {
539 *error= TEST_FAILURE;
540 return NULL;
541 }
542
543 if (hashkit_is_allocated(hashk_ptr) == true)
544 {
545 *error= TEST_FAILURE;
546 return NULL;
547 }
548
549 *error= TEST_SUCCESS;
550
551 return hashk_ptr;
552 }
553
554
555 test_return_t world_destroy(hashkit_st *hashk)
556 {
557 // Did we get back what we expected?
558 assert(hashkit_is_allocated(hashk) == false);
559 hashkit_free(&global_hashk);
560
561 return TEST_SUCCESS;
562 }
563
564 void get_world(world_st *world)
565 {
566 world->collections= collection;
567 world->create= (test_callback_create_fn)world_create;
568 world->destroy= (test_callback_fn)world_destroy;
569 }