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