c148d3effd10528ac8cf427ef6bc4447a02df93d
[m6w6/libmemcached] / tests / hash_plus.cc
1 /*
2 C++ to libhashkit
3 */
4 #include <libtest/test.h>
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <libhashkit/hashkit.h>
11
12 #include "hash_results.h"
13
14 static test_return_t exists_test(void *obj)
15 {
16 Hashkit hashk;
17 (void)obj;
18 (void)hashk;
19
20 return TEST_SUCCESS;
21 }
22
23 static test_return_t new_test(void *obj)
24 {
25 Hashkit *hashk= new Hashkit;
26 (void)obj;
27
28 (void)hashk;
29
30 delete hashk;
31
32 return TEST_SUCCESS;
33 }
34
35 static test_return_t copy_test(void *obj)
36 {
37 Hashkit *hashk= new Hashkit;
38 Hashkit *copy(hashk);
39 (void)obj;
40
41 (void)copy;
42
43 delete hashk;
44
45 return TEST_SUCCESS;
46 }
47
48 static test_return_t assign_test(void *obj)
49 {
50 Hashkit hashk;
51 Hashkit copy;
52 (void)obj;
53
54 copy= hashk;
55
56 (void)copy;
57
58 return TEST_SUCCESS;
59 }
60
61 static test_return_t digest_test(void *obj)
62 {
63 Hashkit hashk;
64 uint32_t value;
65 (void)obj;
66
67 value= hashk.digest("Foo", sizeof("Foo"));
68
69 return TEST_SUCCESS;
70 }
71
72 static test_return_t set_function_test(void *obj)
73 {
74 Hashkit hashk;
75 hashkit_hash_algorithm_t algo_list[]= {
76 HASHKIT_HASH_DEFAULT,
77 HASHKIT_HASH_MD5,
78 HASHKIT_HASH_CRC,
79 HASHKIT_HASH_FNV1_64,
80 HASHKIT_HASH_FNV1A_64,
81 HASHKIT_HASH_FNV1_32,
82 HASHKIT_HASH_FNV1A_32,
83 HASHKIT_HASH_MURMUR,
84 HASHKIT_HASH_JENKINS,
85 HASHKIT_HASH_MAX
86 };
87 hashkit_hash_algorithm_t *algo;
88 (void)obj;
89
90
91 for (algo= algo_list; *algo != HASHKIT_HASH_MAX; algo++)
92 {
93 hashkit_return_t rc;
94 uint32_t x;
95 const char **ptr;
96 uint32_t *list;
97
98 rc= hashk.set_function(*algo);
99
100 test_true(rc == HASHKIT_SUCCESS);
101
102 switch (*algo)
103 {
104 case HASHKIT_HASH_DEFAULT:
105 list= one_at_a_time_values;
106 break;
107 case HASHKIT_HASH_MD5:
108 list= md5_values;
109 break;
110 case HASHKIT_HASH_CRC:
111 list= crc_values;
112 break;
113 case HASHKIT_HASH_FNV1_64:
114 list= fnv1_64_values;
115 break;
116 case HASHKIT_HASH_FNV1A_64:
117 list= fnv1a_64_values;
118 break;
119 case HASHKIT_HASH_FNV1_32:
120 list= fnv1_32_values;
121 break;
122 case HASHKIT_HASH_FNV1A_32:
123 list= fnv1a_32_values;
124 break;
125 case HASHKIT_HASH_HSIEH:
126 list= hsieh_values;
127 break;
128 case HASHKIT_HASH_MURMUR:
129 list= murmur_values;
130 break;
131 case HASHKIT_HASH_JENKINS:
132 list= jenkins_values;
133 break;
134 case HASHKIT_HASH_CUSTOM:
135 case HASHKIT_HASH_MAX:
136 default:
137 list= NULL;
138 test_fail("We ended up on a non-existent hash");
139 }
140
141 // Now we make sure we did set the hash correctly.
142 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
143 {
144 uint32_t hash_val;
145
146 hash_val= hashk.digest(*ptr, strlen(*ptr));
147 test_true(list[x] == hash_val);
148 }
149 }
150
151 return TEST_SUCCESS;
152 }
153
154 static test_return_t set_distribution_function_test(void *obj)
155 {
156 Hashkit hashk;
157 hashkit_return_t rc;
158 (void)obj;
159
160 rc= hashk.set_distribution_function(HASHKIT_HASH_CUSTOM);
161 test_true(rc == HASHKIT_FAILURE);
162
163 rc= hashk.set_distribution_function(HASHKIT_HASH_JENKINS);
164 test_true(rc == HASHKIT_SUCCESS);
165
166 return TEST_SUCCESS;
167 }
168
169 static test_return_t compare_function_test(void *obj)
170 {
171 Hashkit a, b;
172 (void)obj;
173
174 b= a;
175
176 test_true(a == b);
177
178 b.set_function(HASHKIT_HASH_MURMUR);
179
180 test_false(a == b);
181 test_true(b == b);
182 test_true(a == a);
183
184 return TEST_SUCCESS;
185 }
186
187 test_st basic[] ={
188 { "exists", 0, reinterpret_cast<test_callback_fn>(exists_test) },
189 { "new", 0, reinterpret_cast<test_callback_fn>(new_test) },
190 { "copy", 0, reinterpret_cast<test_callback_fn>(copy_test) },
191 { "assign", 0, reinterpret_cast<test_callback_fn>(assign_test) },
192 { "digest", 0, reinterpret_cast<test_callback_fn>(digest_test) },
193 { "set_function", 0, reinterpret_cast<test_callback_fn>(set_function_test) },
194 { "set_distribution_function", 0, reinterpret_cast<test_callback_fn>(set_distribution_function_test) },
195 { "compare", 0, reinterpret_cast<test_callback_fn>(compare_function_test) },
196 { 0, 0, 0}
197 };
198
199 collection_st collection[] ={
200 {"basic", 0, 0, basic},
201 {0, 0, 0, 0}
202 };
203
204 void get_world(world_st *world)
205 {
206 world->collections= collection;
207 }