aa4e82f07fc060dccd8315598cdeeef01f03f8b0
[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_initialized(&hashk) == true);
41 test_true(hashkit_is_allocated(hashk_ptr) == false);
42
43 hashkit_free(hashk_ptr);
44
45 test_true(hashkit_is_initialized(&hashk) == false);
46
47 return TEST_SUCCESS;
48 }
49
50 static test_return_t allocation_test(void *not_used __attribute__((unused)))
51 {
52 hashkit_st *hashk_ptr;
53
54 hashk_ptr= hashkit_create(NULL);
55 test_true(hashk_ptr);
56 test_true(hashkit_is_allocated(hashk_ptr) == true);
57 test_true(hashkit_is_initialized(hashk_ptr) == true);
58 hashkit_free(hashk_ptr);
59
60 return TEST_SUCCESS;
61 }
62
63 static test_return_t clone_test(hashkit_st *hashk)
64 {
65 // First we make sure that the testing system is giving us what we expect.
66 assert(&global_hashk == hashk);
67
68 // Second we test if hashk is even valid
69 test_true(hashkit_is_initialized(hashk) == true);
70
71 /* All null? */
72 {
73 hashkit_st *hashk_ptr;
74 hashk_ptr= hashkit_clone(NULL, NULL);
75 test_true(hashk_ptr);
76 test_true(hashkit_is_allocated(hashk_ptr) == true);
77 test_true(hashkit_is_initialized(hashk_ptr) == true);
78 hashkit_free(hashk_ptr);
79 }
80
81 /* Can we init from null? */
82 {
83 hashkit_st *hashk_ptr;
84
85 hashk_ptr= hashkit_clone(NULL, hashk);
86
87 test_true(hashk_ptr);
88 test_true(hashkit_is_allocated(hashk_ptr) == true);
89 test_true(hashkit_is_initialized(hashk_ptr) == true);
90
91 test_true(hashk_ptr->distribution == hashk->distribution);
92 test_true(hashk_ptr->continuum_count == hashk->continuum_count);
93 test_true(hashk_ptr->continuum_points_count == hashk->continuum_points_count);
94 test_true(hashk_ptr->list_size == hashk->list_size);
95 test_true(hashk_ptr->context_size == hashk->context_size);
96 test_true(hashk_ptr->continuum == NULL);
97 test_true(hashk_ptr->hash_fn == hashk->hash_fn);
98 test_true(hashk_ptr->active_fn == hashk->active_fn);
99 test_true(hashk_ptr->continuum_hash_fn == hashk->continuum_hash_fn);
100 test_true(hashk_ptr->continuum_key_fn == hashk->continuum_key_fn);
101 test_true(hashk_ptr->sort_fn == hashk->sort_fn);
102 test_true(hashk_ptr->weight_fn == hashk->weight_fn);
103 test_true(hashk_ptr->list == hashk->list);
104
105 hashkit_free(hashk_ptr);
106 }
107
108 /* Can we init from struct? */
109 {
110 hashkit_st declared_clone;
111 hashkit_st *hash_clone;
112
113 hash_clone= hashkit_clone(&declared_clone, NULL);
114 test_true(hash_clone);
115
116 hashkit_free(hash_clone);
117 }
118
119 /* Can we init from struct? */
120 {
121 hashkit_st declared_clone;
122 hashkit_st *hash_clone;
123 memset(&declared_clone, 0 , sizeof(hashkit_st));
124 hash_clone= hashkit_clone(&declared_clone, hashk);
125 test_true(hash_clone);
126 hashkit_free(hash_clone);
127 }
128
129 return TEST_SUCCESS;
130 }
131
132
133 static test_return_t md5_run (hashkit_st *hashk __attribute__((unused)))
134 {
135 uint32_t x;
136 const char **ptr;
137
138 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
139 {
140 uint32_t hash_val;
141
142 hash_val= hashkit_md5(*ptr, strlen(*ptr));
143 test_true(md5_values[x] == hash_val);
144 }
145
146 return TEST_SUCCESS;
147 }
148
149 static test_return_t crc_run (hashkit_st *hashk __attribute__((unused)))
150 {
151 uint32_t x;
152 const char **ptr;
153
154 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
155 {
156 uint32_t hash_val;
157
158 hash_val= hashkit_crc32(*ptr, strlen(*ptr));
159 assert(crc_values[x] == hash_val);
160 }
161
162 return TEST_SUCCESS;
163 }
164
165 static test_return_t fnv1_64_run (hashkit_st *hashk __attribute__((unused)))
166 {
167 uint32_t x;
168 const char **ptr;
169
170 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
171 {
172 uint32_t hash_val;
173
174 hash_val= hashkit_fnv1_64(*ptr, strlen(*ptr));
175 assert(fnv1_64_values[x] == hash_val);
176 }
177
178 return TEST_SUCCESS;
179 }
180
181 static test_return_t fnv1a_64_run (hashkit_st *hashk __attribute__((unused)))
182 {
183 uint32_t x;
184 const char **ptr;
185
186 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
187 {
188 uint32_t hash_val;
189
190 hash_val= hashkit_fnv1a_64(*ptr, strlen(*ptr));
191 assert(fnv1a_64_values[x] == hash_val);
192 }
193
194 return TEST_SUCCESS;
195 }
196
197 static test_return_t fnv1_32_run (hashkit_st *hashk __attribute__((unused)))
198 {
199 uint32_t x;
200 const char **ptr;
201
202
203 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
204 {
205 uint32_t hash_val;
206
207 hash_val= hashkit_fnv1_32(*ptr, strlen(*ptr));
208 assert(fnv1_32_values[x] == hash_val);
209 }
210
211 return TEST_SUCCESS;
212 }
213
214 static test_return_t fnv1a_32_run (hashkit_st *hashk __attribute__((unused)))
215 {
216 uint32_t x;
217 const char **ptr;
218
219 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
220 {
221 uint32_t hash_val;
222
223 hash_val= hashkit_fnv1a_32(*ptr, strlen(*ptr));
224 assert(fnv1a_32_values[x] == hash_val);
225 }
226
227 return TEST_SUCCESS;
228 }
229
230 static test_return_t hsieh_run (hashkit_st *hashk __attribute__((unused)))
231 {
232 uint32_t x;
233 const char **ptr;
234
235 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
236 {
237 uint32_t hash_val;
238
239 #ifdef HAVE_HSIEH_HASH
240 hash_val= hashkit_hsieh(*ptr, strlen(*ptr));
241 #else
242 hash_val= 1;
243 #endif
244 assert(hsieh_values[x] == hash_val);
245 }
246
247 return TEST_SUCCESS;
248 }
249
250 static test_return_t murmur_run (hashkit_st *hashk __attribute__((unused)))
251 {
252 #ifdef __sparc
253 return TEST_SKIPPED;
254 #else
255 uint32_t x;
256 const char **ptr;
257
258 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
259 {
260 uint32_t hash_val;
261
262 hash_val= hashkit_murmur(*ptr, strlen(*ptr));
263 assert(murmur_values[x] == hash_val);
264 }
265
266 return TEST_SUCCESS;
267 #endif
268 }
269
270 static test_return_t jenkins_run (hashkit_st *hashk __attribute__((unused)))
271 {
272 uint32_t x;
273 const char **ptr;
274
275
276 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
277 {
278 uint32_t hash_val;
279
280 hash_val= hashkit_jenkins(*ptr, strlen(*ptr));
281 assert(jenkins_values[x] == hash_val);
282 }
283
284 return TEST_SUCCESS;
285 }
286
287
288
289
290 /**
291 @brief now we list out the tests.
292 */
293
294 test_st allocation[]= {
295 {"init", 0, (test_callback_fn)init_test},
296 {"create and free", 0, (test_callback_fn)allocation_test},
297 {"clone", 0, (test_callback_fn)clone_test},
298 {0, 0, 0}
299 };
300
301 test_st hash_tests[] ={
302 {"md5", 0, (test_callback_fn)md5_run },
303 {"crc", 0, (test_callback_fn)crc_run },
304 {"fnv1_64", 0, (test_callback_fn)fnv1_64_run },
305 {"fnv1a_64", 0, (test_callback_fn)fnv1a_64_run },
306 {"fnv1_32", 0, (test_callback_fn)fnv1_32_run },
307 {"fnv1a_32", 0, (test_callback_fn)fnv1a_32_run },
308 {"hsieh", 0, (test_callback_fn)hsieh_run },
309 {"murmur", 0, (test_callback_fn)murmur_run },
310 {"jenkis", 0, (test_callback_fn)jenkins_run },
311 {0, 0, (test_callback_fn)0}
312 };
313
314 /*
315 * The following test suite is used to verify that we don't introduce
316 * regression bugs. If you want more information about the bug / test,
317 * you should look in the bug report at
318 * http://bugs.launchpad.net/libmemcached
319 */
320 test_st regression[]= {
321 {0, 0, 0}
322 };
323
324 collection_st collection[] ={
325 {"allocation", 0, 0, allocation},
326 {"regression", 0, 0, regression},
327 {"hashing", 0, 0, hash_tests},
328 {0, 0, 0, 0}
329 };
330
331 /* Prototypes for functions we will pass to test framework */
332 void *world_create(test_return_t *error);
333 test_return_t world_destroy(hashkit_st *hashk);
334
335 void *world_create(test_return_t *error)
336 {
337 hashkit_st *hashk_ptr;
338
339 hashk_ptr= hashkit_create(&global_hashk);
340
341 if (hashk_ptr != &global_hashk)
342 {
343 *error= TEST_FAILURE;
344 return NULL;
345 }
346
347 // First we test if hashk is even valid
348 if (hashkit_is_initialized(hashk_ptr) == false)
349 {
350 *error= TEST_FAILURE;
351 return NULL;
352 }
353
354 if (hashkit_is_allocated(hashk_ptr) == true)
355 {
356 *error= TEST_FAILURE;
357 return NULL;
358 }
359
360 if (hashk_ptr->continuum != NULL)
361 {
362 *error= TEST_FAILURE;
363 return NULL;
364 }
365
366 *error= TEST_SUCCESS;
367
368 return hashk_ptr;
369 }
370
371
372 test_return_t world_destroy(hashkit_st *hashk)
373 {
374 // Did we get back what we expected?
375 assert(hashkit_is_initialized(hashk) == true);
376 assert(hashkit_is_allocated(hashk) == false);
377 hashkit_free(&global_hashk);
378
379 return TEST_SUCCESS;
380 }
381
382 void get_world(world_st *world)
383 {
384 world->collections= collection;
385 world->create= (test_callback_create_fn)world_create;
386 world->destroy= (test_callback_fn)world_destroy;
387 }