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