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