1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 * libHashKit Functions Test
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <libtest/test.hpp>
41 using namespace libtest
;
48 #include <libhashkit-1.0/hashkit.h>
49 #include <libhashkit/is.h>
51 #include "tests/hash_results.h"
53 static hashkit_st global_hashk
;
56 @brief hash_test_st is a structure we use in testing. It is currently empty.
58 typedef struct hash_test_st hash_test_st
;
65 static test_return_t
init_test(void *)
68 hashkit_st
*hashk_ptr
;
70 hashk_ptr
= hashkit_create(&hashk
);
72 test_true(hashk_ptr
== &hashk
);
73 test_false(hashkit_is_allocated(hashk_ptr
));
75 hashkit_free(hashk_ptr
);
80 static test_return_t
allocation_test(void *)
82 hashkit_st
*hashk_ptr
;
84 hashk_ptr
= hashkit_create(NULL
);
86 test_true(hashkit_is_allocated(hashk_ptr
));
87 hashkit_free(hashk_ptr
);
92 static test_return_t
clone_test(hashkit_st
*hashk
)
94 // First we make sure that the testing system is giving us what we expect.
95 test_true(&global_hashk
== hashk
);
97 // Second we test if hashk is even valid
101 hashkit_st
*hashk_ptr
;
102 hashk_ptr
= hashkit_clone(NULL
, NULL
);
103 test_true(hashk_ptr
);
104 test_true(hashkit_is_allocated(hashk_ptr
));
105 hashkit_free(hashk_ptr
);
108 /* Can we init from null? */
110 hashkit_st
*hashk_ptr
;
112 hashk_ptr
= hashkit_clone(NULL
, hashk
);
114 test_true(hashk_ptr
);
115 test_true(hashkit_is_allocated(hashk_ptr
));
117 hashkit_free(hashk_ptr
);
120 /* Can we init from struct? */
122 hashkit_st declared_clone
;
123 hashkit_st
*hash_clone
;
125 hash_clone
= hashkit_clone(&declared_clone
, NULL
);
126 test_true(hash_clone
);
127 test_true(hash_clone
== &declared_clone
);
128 test_false(hashkit_is_allocated(hash_clone
));
130 hashkit_free(hash_clone
);
133 /* Can we init from struct? */
135 hashkit_st declared_clone
;
136 hashkit_st
*hash_clone
;
138 hash_clone
= hashkit_clone(&declared_clone
, hashk
);
139 test_true(hash_clone
);
140 test_true(hash_clone
== &declared_clone
);
141 test_false(hashkit_is_allocated(hash_clone
));
143 hashkit_free(hash_clone
);
149 static test_return_t
one_at_a_time_run (hashkit_st
*)
154 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
156 test_compare(one_at_a_time_values
[x
],
157 libhashkit_one_at_a_time(*ptr
, strlen(*ptr
)));
163 static test_return_t
md5_run (hashkit_st
*)
168 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
170 test_compare(md5_values
[x
],
171 libhashkit_md5(*ptr
, strlen(*ptr
)));
177 static test_return_t
crc_run (hashkit_st
*)
182 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
184 test_compare(crc_values
[x
],
185 libhashkit_crc32(*ptr
, strlen(*ptr
)));
191 static test_return_t
fnv1_64_run (hashkit_st
*)
193 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64
));
198 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
200 test_compare(fnv1_64_values
[x
],
201 libhashkit_fnv1_64(*ptr
, strlen(*ptr
)));
207 static test_return_t
fnv1a_64_run (hashkit_st
*)
209 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_FNV1A_64
));
213 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
215 test_compare(fnv1a_64_values
[x
],
216 libhashkit_fnv1a_64(*ptr
, strlen(*ptr
)));
222 static test_return_t
fnv1_32_run (hashkit_st
*)
227 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
229 test_compare(fnv1_32_values
[x
],
230 libhashkit_fnv1_32(*ptr
, strlen(*ptr
)));
236 static test_return_t
fnv1a_32_run (hashkit_st
*)
241 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
243 test_compare(fnv1a_32_values
[x
],
244 libhashkit_fnv1a_32(*ptr
, strlen(*ptr
)));
250 static test_return_t
hsieh_run (hashkit_st
*)
252 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_HSIEH
));
257 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
259 test_compare(hsieh_values
[x
],
260 libhashkit_hsieh(*ptr
, strlen(*ptr
)));
266 static test_return_t
murmur_run (hashkit_st
*)
268 test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_MURMUR
));
270 #ifdef WORDS_BIGENDIAN
277 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
279 test_compare(murmur_values
[x
],
280 libhashkit_murmur(*ptr
, strlen(*ptr
)));
287 static test_return_t
jenkins_run (hashkit_st
*)
292 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
294 test_compare(jenkins_values
[x
],
295 libhashkit_jenkins(*ptr
, strlen(*ptr
)));
305 @brief now we list out the tests.
308 test_st allocation
[]= {
309 {"init", 0, (test_callback_fn
*)init_test
},
310 {"create and free", 0, (test_callback_fn
*)allocation_test
},
311 {"clone", 0, (test_callback_fn
*)clone_test
},
315 static test_return_t
hashkit_digest_test(hashkit_st
*hashk
)
317 test_true(hashkit_digest(hashk
, "a", sizeof("a")));
322 static test_return_t
hashkit_set_function_test(hashkit_st
*hashk
)
324 for (int algo
= int(HASHKIT_HASH_DEFAULT
); algo
< int(HASHKIT_HASH_MAX
); algo
++)
330 test_skip(true, libhashkit_has_algorithm(static_cast<hashkit_hash_algorithm_t
>(algo
)));
332 hashkit_return_t rc
= hashkit_set_function(hashk
, static_cast<hashkit_hash_algorithm_t
>(algo
));
334 test_compare_got(HASHKIT_SUCCESS
, rc
, hashkit_strerror(NULL
, rc
));
338 case HASHKIT_HASH_DEFAULT
:
339 list
= one_at_a_time_values
;
342 case HASHKIT_HASH_MD5
:
346 case HASHKIT_HASH_CRC
:
350 case HASHKIT_HASH_FNV1_64
:
351 list
= fnv1_64_values
;
354 case HASHKIT_HASH_FNV1A_64
:
355 list
= fnv1a_64_values
;
358 case HASHKIT_HASH_FNV1_32
:
359 list
= fnv1_32_values
;
362 case HASHKIT_HASH_FNV1A_32
:
363 list
= fnv1a_32_values
;
366 case HASHKIT_HASH_HSIEH
:
370 case HASHKIT_HASH_MURMUR
:
374 case HASHKIT_HASH_JENKINS
:
375 list
= jenkins_values
;
378 case HASHKIT_HASH_CUSTOM
:
379 case HASHKIT_HASH_MAX
:
385 // Now we make sure we did set the hash correctly.
388 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
390 test_compare(list
[x
],
391 hashkit_digest(hashk
, *ptr
, strlen(*ptr
)));
403 static uint32_t hash_test_function(const char *string
, size_t string_length
, void *)
405 return libhashkit_md5(string
, string_length
);
408 static test_return_t
hashkit_set_custom_function_test(hashkit_st
*hashk
)
414 test_compare(HASHKIT_SUCCESS
,
415 hashkit_set_custom_function(hashk
, hash_test_function
, NULL
));
417 for (ptr
= list_to_hash
, x
= 0; *ptr
; ptr
++, x
++)
419 test_compare(md5_values
[x
],
420 hashkit_digest(hashk
, *ptr
, strlen(*ptr
)));
426 static test_return_t
hashkit_set_distribution_function_test(hashkit_st
*hashk
)
428 for (int algo
= int(HASHKIT_HASH_DEFAULT
); algo
< int(HASHKIT_HASH_MAX
); algo
++)
430 hashkit_return_t rc
= hashkit_set_distribution_function(hashk
, static_cast<hashkit_hash_algorithm_t
>(algo
));
432 /* Hsieh is disabled most of the time for patent issues */
433 if (rc
== HASHKIT_INVALID_ARGUMENT
)
436 test_compare(HASHKIT_SUCCESS
, rc
);
442 static test_return_t
hashkit_set_custom_distribution_function_test(hashkit_st
*hashk
)
444 test_compare(HASHKIT_SUCCESS
,
445 hashkit_set_custom_distribution_function(hashk
, hash_test_function
, NULL
));
451 static test_return_t
hashkit_get_function_test(hashkit_st
*hashk
)
453 for (int algo
= int(HASHKIT_HASH_DEFAULT
); algo
< int(HASHKIT_HASH_MAX
); algo
++)
456 if (HASHKIT_HASH_CUSTOM
)
460 test_skip(true, libhashkit_has_algorithm(static_cast<hashkit_hash_algorithm_t
>(algo
)));
462 test_compare(HASHKIT_SUCCESS
,
463 hashkit_set_function(hashk
, static_cast<hashkit_hash_algorithm_t
>(algo
)));
465 test_compare(hashkit_get_function(hashk
), algo
);
470 static test_return_t
hashkit_compare_test(hashkit_st
*hashk
)
472 hashkit_st
*clone
= hashkit_clone(NULL
, hashk
);
474 test_true(hashkit_compare(clone
, hashk
));
480 test_st hashkit_st_functions
[] ={
481 {"hashkit_digest", 0, (test_callback_fn
*)hashkit_digest_test
},
482 {"hashkit_set_function", 0, (test_callback_fn
*)hashkit_set_function_test
},
483 {"hashkit_set_custom_function", 0, (test_callback_fn
*)hashkit_set_custom_function_test
},
484 {"hashkit_get_function", 0, (test_callback_fn
*)hashkit_get_function_test
},
485 {"hashkit_set_distribution_function", 0, (test_callback_fn
*)hashkit_set_distribution_function_test
},
486 {"hashkit_set_custom_distribution_function", 0, (test_callback_fn
*)hashkit_set_custom_distribution_function_test
},
487 {"hashkit_compare", 0, (test_callback_fn
*)hashkit_compare_test
},
491 static test_return_t
libhashkit_digest_test(hashkit_st
*)
493 test_true(libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT
));
498 test_st library_functions
[] ={
499 {"libhashkit_digest", 0, (test_callback_fn
*)libhashkit_digest_test
},
503 test_st hash_tests
[] ={
504 {"one_at_a_time", 0, (test_callback_fn
*)one_at_a_time_run
},
505 {"md5", 0, (test_callback_fn
*)md5_run
},
506 {"crc", 0, (test_callback_fn
*)crc_run
},
507 {"fnv1_64", 0, (test_callback_fn
*)fnv1_64_run
},
508 {"fnv1a_64", 0, (test_callback_fn
*)fnv1a_64_run
},
509 {"fnv1_32", 0, (test_callback_fn
*)fnv1_32_run
},
510 {"fnv1a_32", 0, (test_callback_fn
*)fnv1a_32_run
},
511 {"hsieh", 0, (test_callback_fn
*)hsieh_run
},
512 {"murmur", 0, (test_callback_fn
*)murmur_run
},
513 {"jenkis", 0, (test_callback_fn
*)jenkins_run
},
514 {0, 0, (test_callback_fn
*)0}
518 * The following test suite is used to verify that we don't introduce
519 * regression bugs. If you want more information about the bug / test,
520 * you should look in the bug report at
521 * http://bugs.launchpad.net/libmemcached
523 test_st regression
[]= {
527 collection_st collection
[] ={
528 {"allocation", 0, 0, allocation
},
529 {"hashkit_st_functions", 0, 0, hashkit_st_functions
},
530 {"library_functions", 0, 0, library_functions
},
531 {"hashing", 0, 0, hash_tests
},
532 {"regression", 0, 0, regression
},
536 static void *world_create(libtest::server_startup_st
&, test_return_t
& error
)
538 hashkit_st
*hashk_ptr
= hashkit_create(&global_hashk
);
540 if (hashk_ptr
!= &global_hashk
)
546 if (hashkit_is_allocated(hashk_ptr
) == true)
556 static bool world_destroy(void *object
)
558 hashkit_st
*hashk
= (hashkit_st
*)object
;
559 // Did we get back what we expected?
560 test_true(hashkit_is_allocated(hashk
) == false);
561 hashkit_free(&global_hashk
);
566 void get_world(Framework
*world
)
568 world
->collections
= collection
;
569 world
->_create
= world_create
;
570 world
->_destroy
= world_destroy
;