Fixed autoconf macros for cross compile
[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 "config.h"
10
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <libhashkit/hashkit.h>
17
18 #include "test.h"
19
20 #include "hash_results.h"
21
22 static hashkit_st global_hashk;
23
24 /**
25 @brief hash_test_st is a structure we use in testing. It is currently empty.
26 */
27 typedef struct hash_test_st hash_test_st;
28
29 struct hash_test_st
30 {
31 bool _unused;
32 };
33
34 static test_return_t init_test(void *not_used __attribute__((unused)))
35 {
36 hashkit_st hashk;
37 hashkit_st *hashk_ptr;
38
39 hashk_ptr= hashkit_create(&hashk);
40 test_true(hashk_ptr);
41 test_true(hashk_ptr == &hashk);
42 test_true(hashkit_is_allocated(hashk_ptr) == false);
43
44 hashkit_free(hashk_ptr);
45
46 return TEST_SUCCESS;
47 }
48
49 static test_return_t allocation_test(void *not_used __attribute__((unused)))
50 {
51 hashkit_st *hashk_ptr;
52
53 hashk_ptr= hashkit_create(NULL);
54 test_true(hashk_ptr);
55 test_true(hashkit_is_allocated(hashk_ptr) == true);
56 hashkit_free(hashk_ptr);
57
58 return TEST_SUCCESS;
59 }
60
61 static test_return_t clone_test(hashkit_st *hashk)
62 {
63 // First we make sure that the testing system is giving us what we expect.
64 assert(&global_hashk == hashk);
65
66 // Second we test if hashk is even valid
67
68 /* All null? */
69 {
70 hashkit_st *hashk_ptr;
71 hashk_ptr= hashkit_clone(NULL, NULL);
72 test_true(hashk_ptr);
73 test_true(hashkit_is_allocated(hashk_ptr));
74 hashkit_free(hashk_ptr);
75 }
76
77 /* Can we init from null? */
78 {
79 hashkit_st *hashk_ptr;
80
81 hashk_ptr= hashkit_clone(NULL, hashk);
82
83 test_true(hashk_ptr);
84 test_true(hashkit_is_allocated(hashk_ptr));
85
86 hashkit_free(hashk_ptr);
87 }
88
89 /* Can we init from struct? */
90 {
91 hashkit_st declared_clone;
92 hashkit_st *hash_clone;
93
94 hash_clone= hashkit_clone(&declared_clone, NULL);
95 test_true(hash_clone);
96 test_true(hash_clone == &declared_clone);
97 test_false(hashkit_is_allocated(hash_clone));
98
99 hashkit_free(hash_clone);
100 }
101
102 /* Can we init from struct? */
103 {
104 hashkit_st declared_clone;
105 hashkit_st *hash_clone;
106
107 hash_clone= hashkit_clone(&declared_clone, hashk);
108 test_true(hash_clone);
109 test_true(hash_clone == &declared_clone);
110 test_false(hashkit_is_allocated(hash_clone));
111
112 hashkit_free(hash_clone);
113 }
114
115 return TEST_SUCCESS;
116 }
117
118 static test_return_t one_at_a_time_run (hashkit_st *hashk __attribute__((unused)))
119 {
120 uint32_t x;
121 const char **ptr;
122
123 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
124 {
125 uint32_t hash_val;
126
127 hash_val= libhashkit_one_at_a_time(*ptr, strlen(*ptr));
128 test_true(one_at_a_time_values[x] == hash_val);
129 }
130
131 return TEST_SUCCESS;
132 }
133
134 static test_return_t md5_run (hashkit_st *hashk __attribute__((unused)))
135 {
136 uint32_t x;
137 const char **ptr;
138
139 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
140 {
141 uint32_t hash_val;
142
143 hash_val= libhashkit_md5(*ptr, strlen(*ptr));
144 test_true(md5_values[x] == hash_val);
145 }
146
147 return TEST_SUCCESS;
148 }
149
150 static test_return_t crc_run (hashkit_st *hashk __attribute__((unused)))
151 {
152 uint32_t x;
153 const char **ptr;
154
155 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
156 {
157 uint32_t hash_val;
158
159 hash_val= libhashkit_crc32(*ptr, strlen(*ptr));
160 assert(crc_values[x] == hash_val);
161 }
162
163 return TEST_SUCCESS;
164 }
165
166 static test_return_t fnv1_64_run (hashkit_st *hashk __attribute__((unused)))
167 {
168 uint32_t x;
169 const char **ptr;
170
171 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
172 {
173 uint32_t hash_val;
174
175 hash_val= libhashkit_fnv1_64(*ptr, strlen(*ptr));
176 assert(fnv1_64_values[x] == hash_val);
177 }
178
179 return TEST_SUCCESS;
180 }
181
182 static test_return_t fnv1a_64_run (hashkit_st *hashk __attribute__((unused)))
183 {
184 uint32_t x;
185 const char **ptr;
186
187 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
188 {
189 uint32_t hash_val;
190
191 hash_val= libhashkit_fnv1a_64(*ptr, strlen(*ptr));
192 assert(fnv1a_64_values[x] == hash_val);
193 }
194
195 return TEST_SUCCESS;
196 }
197
198 static test_return_t fnv1_32_run (hashkit_st *hashk __attribute__((unused)))
199 {
200 uint32_t x;
201 const char **ptr;
202
203
204 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
205 {
206 uint32_t hash_val;
207
208 hash_val= libhashkit_fnv1_32(*ptr, strlen(*ptr));
209 assert(fnv1_32_values[x] == hash_val);
210 }
211
212 return TEST_SUCCESS;
213 }
214
215 static test_return_t fnv1a_32_run (hashkit_st *hashk __attribute__((unused)))
216 {
217 uint32_t x;
218 const char **ptr;
219
220 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
221 {
222 uint32_t hash_val;
223
224 hash_val= libhashkit_fnv1a_32(*ptr, strlen(*ptr));
225 assert(fnv1a_32_values[x] == hash_val);
226 }
227
228 return TEST_SUCCESS;
229 }
230
231 static test_return_t hsieh_run (hashkit_st *hashk __attribute__((unused)))
232 {
233 uint32_t x;
234 const char **ptr;
235
236 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
237 {
238 uint32_t hash_val;
239
240 #ifdef HAVE_HSIEH_HASH
241 hash_val= libhashkit_hsieh(*ptr, strlen(*ptr));
242 #else
243 hash_val= 1;
244 #endif
245 assert(hsieh_values[x] == hash_val);
246 }
247
248 return TEST_SUCCESS;
249 }
250
251 static test_return_t murmur_run (hashkit_st *hashk __attribute__((unused)))
252 {
253 #ifdef WORDS_BIGENDIAN
254 return TEST_SKIPPED;
255 #else
256 uint32_t x;
257 const char **ptr;
258
259 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
260 {
261 uint32_t hash_val;
262
263 hash_val= libhashkit_murmur(*ptr, strlen(*ptr));
264 assert(murmur_values[x] == hash_val);
265 }
266
267 return TEST_SUCCESS;
268 #endif
269 }
270
271 static test_return_t jenkins_run (hashkit_st *hashk __attribute__((unused)))
272 {
273 uint32_t x;
274 const char **ptr;
275
276
277 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
278 {
279 uint32_t hash_val;
280
281 hash_val= libhashkit_jenkins(*ptr, strlen(*ptr));
282 assert(jenkins_values[x] == hash_val);
283 }
284
285 return TEST_SUCCESS;
286 }
287
288
289
290
291 /**
292 @brief now we list out the tests.
293 */
294
295 test_st allocation[]= {
296 {"init", 0, (test_callback_fn)init_test},
297 {"create and free", 0, (test_callback_fn)allocation_test},
298 {"clone", 0, (test_callback_fn)clone_test},
299 {0, 0, 0}
300 };
301
302 static test_return_t hashkit_digest_test(hashkit_st *hashk)
303 {
304 uint32_t value;
305 value= hashkit_digest(hashk, "a", sizeof("a"));
306
307 return TEST_SUCCESS;
308 }
309
310 static test_return_t hashkit_set_function_test(hashkit_st *hashk)
311 {
312 for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++)
313 {
314 hashkit_return_t rc;
315 uint32_t x;
316 const char **ptr;
317 uint32_t *list;
318
319 rc= hashkit_set_function(hashk, algo);
320
321 /* Hsieh is disabled most of the time for patent issues */
322 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH)
323 continue;
324
325 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_CUSTOM)
326 continue;
327
328 test_true(rc == HASHKIT_SUCCESS);
329
330 switch (algo)
331 {
332 case HASHKIT_HASH_DEFAULT:
333 list= one_at_a_time_values;
334 break;
335 case HASHKIT_HASH_MD5:
336 list= md5_values;
337 break;
338 case HASHKIT_HASH_CRC:
339 list= crc_values;
340 break;
341 case HASHKIT_HASH_FNV1_64:
342 list= fnv1_64_values;
343 break;
344 case HASHKIT_HASH_FNV1A_64:
345 list= fnv1a_64_values;
346 break;
347 case HASHKIT_HASH_FNV1_32:
348 list= fnv1_32_values;
349 break;
350 case HASHKIT_HASH_FNV1A_32:
351 list= fnv1a_32_values;
352 break;
353 case HASHKIT_HASH_HSIEH:
354 list= hsieh_values;
355 break;
356 case HASHKIT_HASH_MURMUR:
357 list= murmur_values;
358 break;
359 case HASHKIT_HASH_JENKINS:
360 list= jenkins_values;
361 break;
362 case HASHKIT_HASH_CUSTOM:
363 case HASHKIT_HASH_MAX:
364 default:
365 list= NULL;
366 break;
367 }
368
369 // Now we make sure we did set the hash correctly.
370 if (list)
371 {
372 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
373 {
374 uint32_t hash_val;
375
376 hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr));
377 test_true(list[x] == hash_val);
378 }
379 }
380 else
381 {
382 return TEST_FAILURE;
383 }
384 }
385
386 return TEST_SUCCESS;
387 }
388
389 static uint32_t hash_test_function(const char *string, size_t string_length, void *context)
390 {
391 (void)context;
392 return libhashkit_md5(string, string_length);
393 }
394
395 static test_return_t hashkit_set_custom_function_test(hashkit_st *hashk)
396 {
397 hashkit_return_t rc;
398 uint32_t x;
399 const char **ptr;
400
401
402 rc= hashkit_set_custom_function(hashk, hash_test_function, NULL);
403 test_true(rc == HASHKIT_SUCCESS);
404
405 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
406 {
407 uint32_t hash_val;
408
409 hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr));
410 test_true(md5_values[x] == hash_val);
411 }
412
413 return TEST_SUCCESS;
414 }
415
416 static test_return_t hashkit_set_distribution_function_test(hashkit_st *hashk)
417 {
418 for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++)
419 {
420 hashkit_return_t rc;
421
422 rc= hashkit_set_distribution_function(hashk, algo);
423
424 /* Hsieh is disabled most of the time for patent issues */
425 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH)
426 continue;
427
428 if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_CUSTOM)
429 continue;
430
431 test_true(rc == HASHKIT_SUCCESS);
432 }
433
434 return TEST_SUCCESS;
435 }
436
437 static test_return_t hashkit_set_custom_distribution_function_test(hashkit_st *hashk)
438 {
439 hashkit_return_t rc;
440
441 rc= hashkit_set_custom_distribution_function(hashk, hash_test_function, NULL);
442 test_true(rc == HASHKIT_SUCCESS);
443
444 return TEST_SUCCESS;
445 }
446
447
448 static test_return_t hashkit_get_function_test(hashkit_st *hashk)
449 {
450 for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++)
451 {
452 hashkit_return_t rc;
453
454 if (HASHKIT_HASH_CUSTOM || HASHKIT_HASH_HSIEH)
455 continue;
456
457 rc= hashkit_set_function(hashk, algo);
458 test_true(rc == HASHKIT_SUCCESS);
459
460 test_true(hashkit_get_function(hashk) == algo);
461 }
462 return TEST_SUCCESS;
463 }
464
465 static test_return_t hashkit_compare_test(hashkit_st *hashk)
466 {
467 hashkit_st *clone;
468
469 clone= hashkit_clone(NULL, hashk);
470
471 test_true(hashkit_compare(clone, hashk));
472 hashkit_free(clone);
473
474 return TEST_SUCCESS;
475 }
476
477 test_st hashkit_st_functions[] ={
478 {"hashkit_digest", 0, (test_callback_fn)hashkit_digest_test},
479 {"hashkit_set_function", 0, (test_callback_fn)hashkit_set_function_test},
480 {"hashkit_set_custom_function", 0, (test_callback_fn)hashkit_set_custom_function_test},
481 {"hashkit_get_function", 0, (test_callback_fn)hashkit_get_function_test},
482 {"hashkit_set_distribution_function", 0, (test_callback_fn)hashkit_set_distribution_function_test},
483 {"hashkit_set_custom_distribution_function", 0, (test_callback_fn)hashkit_set_custom_distribution_function_test},
484 {"hashkit_compare", 0, (test_callback_fn)hashkit_compare_test},
485 {0, 0, 0}
486 };
487
488 static test_return_t libhashkit_digest_test(hashkit_st *hashk)
489 {
490 uint32_t value;
491
492 (void)hashk;
493
494 value= libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT);
495
496 return TEST_SUCCESS;
497 }
498
499 test_st library_functions[] ={
500 {"libhashkit_digest", 0, (test_callback_fn)libhashkit_digest_test},
501 {0, 0, 0}
502 };
503
504 test_st hash_tests[] ={
505 {"one_at_a_time", 0, (test_callback_fn)one_at_a_time_run },
506 {"md5", 0, (test_callback_fn)md5_run },
507 {"crc", 0, (test_callback_fn)crc_run },
508 {"fnv1_64", 0, (test_callback_fn)fnv1_64_run },
509 {"fnv1a_64", 0, (test_callback_fn)fnv1a_64_run },
510 {"fnv1_32", 0, (test_callback_fn)fnv1_32_run },
511 {"fnv1a_32", 0, (test_callback_fn)fnv1a_32_run },
512 {"hsieh", 0, (test_callback_fn)hsieh_run },
513 {"murmur", 0, (test_callback_fn)murmur_run },
514 {"jenkis", 0, (test_callback_fn)jenkins_run },
515 {0, 0, (test_callback_fn)0}
516 };
517
518 /*
519 * The following test suite is used to verify that we don't introduce
520 * regression bugs. If you want more information about the bug / test,
521 * you should look in the bug report at
522 * http://bugs.launchpad.net/libmemcached
523 */
524 test_st regression[]= {
525 {0, 0, 0}
526 };
527
528 collection_st collection[] ={
529 {"allocation", 0, 0, allocation},
530 {"hashkit_st_functions", 0, 0, hashkit_st_functions},
531 {"library_functions", 0, 0, library_functions},
532 {"hashing", 0, 0, hash_tests},
533 {"regression", 0, 0, regression},
534 {0, 0, 0, 0}
535 };
536
537 /* Prototypes for functions we will pass to test framework */
538 void *world_create(test_return_t *error);
539 test_return_t world_destroy(hashkit_st *hashk);
540
541 void *world_create(test_return_t *error)
542 {
543 hashkit_st *hashk_ptr;
544
545 hashk_ptr= hashkit_create(&global_hashk);
546
547 if (hashk_ptr != &global_hashk)
548 {
549 *error= TEST_FAILURE;
550 return NULL;
551 }
552
553 if (hashkit_is_allocated(hashk_ptr) == true)
554 {
555 *error= TEST_FAILURE;
556 return NULL;
557 }
558
559 *error= TEST_SUCCESS;
560
561 return hashk_ptr;
562 }
563
564
565 test_return_t world_destroy(hashkit_st *hashk)
566 {
567 // Did we get back what we expected?
568 assert(hashkit_is_allocated(hashk) == false);
569 hashkit_free(&global_hashk);
570
571 return TEST_SUCCESS;
572 }
573
574 void get_world(world_st *world)
575 {
576 world->collections= collection;
577 world->create= (test_callback_create_fn)world_create;
578 world->destroy= (test_callback_fn)world_destroy;
579 }