1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* libHashKit Functions Test
*
* Copyright (C) 2011 Data Differential, http://datadifferential.com/
* Copyright (C) 2006-2009 Brian Aker All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * The names of its contributors may not be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <mem_config.h>
#include <libtest/test.hpp>
using namespace libtest;
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <libhashkit-1.0/hashkit.h>
#include <libhashkit/is.h>
#include "tests/hash_results.h"
static hashkit_st global_hashk;
/**
@brief hash_test_st is a structure we use in testing. It is currently empty.
*/
typedef struct hash_test_st hash_test_st;
struct hash_test_st
{
bool _unused;
};
static test_return_t init_test(void *)
{
hashkit_st hashk;
hashkit_st *hashk_ptr;
hashk_ptr= hashkit_create(&hashk);
test_true(hashk_ptr);
test_true(hashk_ptr == &hashk);
test_false(hashkit_is_allocated(hashk_ptr));
hashkit_free(hashk_ptr);
return TEST_SUCCESS;
}
static test_return_t allocation_test(void *)
{
hashkit_st *hashk_ptr;
hashk_ptr= hashkit_create(NULL);
test_true(hashk_ptr);
test_true(hashkit_is_allocated(hashk_ptr));
hashkit_free(hashk_ptr);
return TEST_SUCCESS;
}
static test_return_t clone_test(hashkit_st *hashk)
{
// First we make sure that the testing system is giving us what we expect.
test_true(&global_hashk == hashk);
// Second we test if hashk is even valid
/* All null? */
{
hashkit_st *hashk_ptr;
hashk_ptr= hashkit_clone(NULL, NULL);
test_true(hashk_ptr);
test_true(hashkit_is_allocated(hashk_ptr));
hashkit_free(hashk_ptr);
}
/* Can we init from null? */
{
hashkit_st *hashk_ptr;
hashk_ptr= hashkit_clone(NULL, hashk);
test_true(hashk_ptr);
test_true(hashkit_is_allocated(hashk_ptr));
hashkit_free(hashk_ptr);
}
/* Can we init from struct? */
{
hashkit_st declared_clone;
hashkit_st *hash_clone;
hash_clone= hashkit_clone(&declared_clone, NULL);
test_true(hash_clone);
test_true(hash_clone == &declared_clone);
test_false(hashkit_is_allocated(hash_clone));
hashkit_free(hash_clone);
}
/* Can we init from struct? */
{
hashkit_st declared_clone;
hashkit_st *hash_clone;
hash_clone= hashkit_clone(&declared_clone, hashk);
test_true(hash_clone);
test_true(hash_clone == &declared_clone);
test_false(hashkit_is_allocated(hash_clone));
hashkit_free(hash_clone);
}
return TEST_SUCCESS;
}
static test_return_t one_at_a_time_run (hashkit_st *)
{
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(one_at_a_time_values[x],
libhashkit_one_at_a_time(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
}
static test_return_t md5_run (hashkit_st *)
{
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(md5_values[x],
libhashkit_md5(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
}
static test_return_t crc_run (hashkit_st *)
{
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(crc_values[x],
libhashkit_crc32(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
}
static test_return_t fnv1_64_run (hashkit_st *)
{
test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_FNV1_64));
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(fnv1_64_values[x],
libhashkit_fnv1_64(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
}
static test_return_t fnv1a_64_run (hashkit_st *)
{
test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_FNV1A_64));
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(fnv1a_64_values[x],
libhashkit_fnv1a_64(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
}
static test_return_t fnv1_32_run (hashkit_st *)
{
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(fnv1_32_values[x],
libhashkit_fnv1_32(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
}
static test_return_t fnv1a_32_run (hashkit_st *)
{
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(fnv1a_32_values[x],
libhashkit_fnv1a_32(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
}
static test_return_t hsieh_run (hashkit_st *)
{
test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_HSIEH));
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(hsieh_values[x],
libhashkit_hsieh(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
}
static test_return_t murmur3_TEST(hashkit_st *)
{
test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_MURMUR3));
#ifdef WORDS_BIGENDIAN
(void)murmur3_values;
return TEST_SKIPPED;
#else
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(murmur3_values[x],
libhashkit_murmur3(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
#endif
}
static test_return_t murmur_run (hashkit_st *)
{
test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_MURMUR));
#ifdef WORDS_BIGENDIAN
(void)murmur_values;
return TEST_SKIPPED;
#else
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(murmur_values[x],
libhashkit_murmur(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
#endif
}
static test_return_t jenkins_run (hashkit_st *)
{
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(jenkins_values[x],
libhashkit_jenkins(*ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
}
/**
@brief now we list out the tests.
*/
test_st allocation[]= {
{"init", 0, (test_callback_fn*)init_test},
{"create and free", 0, (test_callback_fn*)allocation_test},
{"clone", 0, (test_callback_fn*)clone_test},
{0, 0, 0}
};
static test_return_t hashkit_digest_test(hashkit_st *hashk)
{
test_true(hashkit_digest(hashk, "a", sizeof("a")));
return TEST_SUCCESS;
}
static test_return_t hashkit_set_function_test(hashkit_st *hashk)
{
for (int algo= int(HASHKIT_HASH_DEFAULT); algo < int(HASHKIT_HASH_MAX); algo++)
{
uint32_t x;
const char **ptr;
uint32_t *list;
test_skip(true, libhashkit_has_algorithm(static_cast<hashkit_hash_algorithm_t>(algo)));
hashkit_return_t rc= hashkit_set_function(hashk, static_cast<hashkit_hash_algorithm_t>(algo));
test_compare_got(HASHKIT_SUCCESS, rc, hashkit_strerror(NULL, rc));
switch (algo)
{
case HASHKIT_HASH_DEFAULT:
list= one_at_a_time_values;
break;
case HASHKIT_HASH_MD5:
list= md5_values;
break;
case HASHKIT_HASH_CRC:
list= crc_values;
break;
case HASHKIT_HASH_FNV1_64:
list= fnv1_64_values;
break;
case HASHKIT_HASH_FNV1A_64:
list= fnv1a_64_values;
break;
case HASHKIT_HASH_FNV1_32:
list= fnv1_32_values;
break;
case HASHKIT_HASH_FNV1A_32:
list= fnv1a_32_values;
break;
case HASHKIT_HASH_HSIEH:
list= hsieh_values;
break;
case HASHKIT_HASH_MURMUR:
list= murmur_values;
break;
case HASHKIT_HASH_JENKINS:
list= jenkins_values;
break;
case HASHKIT_HASH_CUSTOM:
case HASHKIT_HASH_MAX:
default:
list= NULL;
break;
}
// Now we make sure we did set the hash correctly.
if (list)
{
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(list[x],
hashkit_digest(hashk, *ptr, strlen(*ptr)));
}
}
else
{
return TEST_FAILURE;
}
}
return TEST_SUCCESS;
}
static uint32_t hash_test_function(const char *string, size_t string_length, void *)
{
return libhashkit_md5(string, string_length);
}
static test_return_t hashkit_set_custom_function_test(hashkit_st *hashk)
{
uint32_t x;
const char **ptr;
test_compare(HASHKIT_SUCCESS,
hashkit_set_custom_function(hashk, hash_test_function, NULL));
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
test_compare(md5_values[x],
hashkit_digest(hashk, *ptr, strlen(*ptr)));
}
return TEST_SUCCESS;
}
static test_return_t hashkit_set_distribution_function_test(hashkit_st *hashk)
{
for (int algo= int(HASHKIT_HASH_DEFAULT); algo < int(HASHKIT_HASH_MAX); algo++)
{
hashkit_return_t rc= hashkit_set_distribution_function(hashk, static_cast<hashkit_hash_algorithm_t>(algo));
/* Hsieh is disabled most of the time for patent issues */
if (rc == HASHKIT_INVALID_ARGUMENT)
continue;
test_compare(HASHKIT_SUCCESS, rc);
}
return TEST_SUCCESS;
}
static test_return_t hashkit_set_custom_distribution_function_test(hashkit_st *hashk)
{
test_compare(HASHKIT_SUCCESS,
hashkit_set_custom_distribution_function(hashk, hash_test_function, NULL));
return TEST_SUCCESS;
}
static test_return_t hashkit_get_function_test(hashkit_st *hashk)
{
for (int algo= int(HASHKIT_HASH_DEFAULT); algo < int(HASHKIT_HASH_MAX); algo++)
{
if (HASHKIT_HASH_CUSTOM)
{
continue;
}
test_skip(true, libhashkit_has_algorithm(static_cast<hashkit_hash_algorithm_t>(algo)));
test_compare(HASHKIT_SUCCESS,
hashkit_set_function(hashk, static_cast<hashkit_hash_algorithm_t>(algo)));
test_compare(hashkit_get_function(hashk), algo);
}
return TEST_SUCCESS;
}
static test_return_t hashkit_compare_test(hashkit_st *hashk)
{
hashkit_st *clone= hashkit_clone(NULL, hashk);
test_true(hashkit_compare(clone, hashk));
hashkit_free(clone);
return TEST_SUCCESS;
}
test_st hashkit_st_functions[] ={
{"hashkit_digest", 0, (test_callback_fn*)hashkit_digest_test},
{"hashkit_set_function", 0, (test_callback_fn*)hashkit_set_function_test},
{"hashkit_set_custom_function", 0, (test_callback_fn*)hashkit_set_custom_function_test},
{"hashkit_get_function", 0, (test_callback_fn*)hashkit_get_function_test},
{"hashkit_set_distribution_function", 0, (test_callback_fn*)hashkit_set_distribution_function_test},
{"hashkit_set_custom_distribution_function", 0, (test_callback_fn*)hashkit_set_custom_distribution_function_test},
{"hashkit_compare", 0, (test_callback_fn*)hashkit_compare_test},
{0, 0, 0}
};
static test_return_t libhashkit_digest_test(hashkit_st *)
{
test_true(libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT));
return TEST_SUCCESS;
}
test_st library_functions[] ={
{"libhashkit_digest", 0, (test_callback_fn*)libhashkit_digest_test},
{0, 0, 0}
};
test_st hash_tests[] ={
{"one_at_a_time", 0, (test_callback_fn*)one_at_a_time_run },
{"md5", 0, (test_callback_fn*)md5_run },
{"crc", 0, (test_callback_fn*)crc_run },
{"fnv1_64", 0, (test_callback_fn*)fnv1_64_run },
{"fnv1a_64", 0, (test_callback_fn*)fnv1a_64_run },
{"fnv1_32", 0, (test_callback_fn*)fnv1_32_run },
{"fnv1a_32", 0, (test_callback_fn*)fnv1a_32_run },
{"hsieh", 0, (test_callback_fn*)hsieh_run },
{"murmur", 0, (test_callback_fn*)murmur_run },
{"murmur3", 0, (test_callback_fn*)murmur3_TEST },
{"jenkis", 0, (test_callback_fn*)jenkins_run },
{0, 0, (test_callback_fn*)0}
};
/*
* The following test suite is used to verify that we don't introduce
* regression bugs. If you want more information about the bug / test,
* you should look in the bug report at
* http://bugs.launchpad.net/libmemcached
*/
test_st regression[]= {
{0, 0, 0}
};
collection_st collection[] ={
{"allocation", 0, 0, allocation},
{"hashkit_st_functions", 0, 0, hashkit_st_functions},
{"library_functions", 0, 0, library_functions},
{"hashing", 0, 0, hash_tests},
{"regression", 0, 0, regression},
{0, 0, 0, 0}
};
static void *world_create(libtest::server_startup_st&, test_return_t& error)
{
hashkit_st *hashk_ptr= hashkit_create(&global_hashk);
if (hashk_ptr != &global_hashk)
{
error= TEST_FAILURE;
return NULL;
}
if (hashkit_is_allocated(hashk_ptr) == true)
{
error= TEST_FAILURE;
return NULL;
}
return hashk_ptr;
}
static bool world_destroy(void *object)
{
hashkit_st *hashk= (hashkit_st *)object;
// Did we get back what we expected?
test_true(hashkit_is_allocated(hashk) == false);
hashkit_free(&global_hashk);
return TEST_SUCCESS;
}
void get_world(libtest::Framework* world)
{
world->collections(collection);
world->create(world_create);
world->destroy(world_destroy);
}
|