Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/libmemcached] / tests / hash_plus.cc
1 /*
2 C++ to libhashkit
3 */
4
5 #include <mem_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_MURMUR3:
134 #ifdef WORDS_BIGENDIAN
135 continue;
136 #endif
137 list= murmur3_values;
138 break;
139 case HASHKIT_HASH_MURMUR:
140 #ifdef WORDS_BIGENDIAN
141 continue;
142 #endif
143 list= murmur_values;
144 break;
145
146 case HASHKIT_HASH_JENKINS:
147 list= jenkins_values;
148 break;
149
150 case HASHKIT_HASH_CUSTOM:
151 case HASHKIT_HASH_MAX:
152 default:
153 list= NULL;
154 test_fail("We ended up on a non-existent hash");
155 }
156
157 // Now we make sure we did set the hash correctly.
158 uint32_t x;
159 const char **ptr;
160 for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
161 {
162 uint32_t hash_val;
163
164 hash_val= hashk.digest(*ptr, strlen(*ptr));
165 char buffer[1024];
166 snprintf(buffer, sizeof(buffer), "%lu %lus %s", (unsigned long)list[x], (unsigned long)hash_val, libhashkit_string_hash(*algo));
167 test_compare(list[x], hash_val);
168 }
169 }
170
171 return TEST_SUCCESS;
172 }
173
174 static test_return_t set_distribution_function_test(void *)
175 {
176 Hashkit hashk;
177 hashkit_return_t rc;
178
179 rc= hashk.set_distribution_function(HASHKIT_HASH_CUSTOM);
180 test_true(rc == HASHKIT_FAILURE or rc == HASHKIT_INVALID_ARGUMENT);
181
182 test_compare(HASHKIT_SUCCESS,
183 hashk.set_distribution_function(HASHKIT_HASH_JENKINS));
184
185 return TEST_SUCCESS;
186 }
187
188 static test_return_t compare_function_test(void *)
189 {
190 Hashkit a, b;
191
192 b= a;
193
194 test_true(a == b);
195
196 b.set_function(HASHKIT_HASH_MURMUR);
197
198 test_false(a == b);
199 test_true(b == b);
200 test_true(a == a);
201
202 return TEST_SUCCESS;
203 }
204
205 test_st basic[] ={
206 { "exists", 0, reinterpret_cast<test_callback_fn*>(exists_test) },
207 { "new", 0, reinterpret_cast<test_callback_fn*>(new_test) },
208 { "copy", 0, reinterpret_cast<test_callback_fn*>(copy_test) },
209 { "assign", 0, reinterpret_cast<test_callback_fn*>(assign_test) },
210 { "digest", 0, reinterpret_cast<test_callback_fn*>(digest_test) },
211 { "set_function", 0, reinterpret_cast<test_callback_fn*>(set_function_test) },
212 { "set_distribution_function", 0, reinterpret_cast<test_callback_fn*>(set_distribution_function_test) },
213 { "compare", 0, reinterpret_cast<test_callback_fn*>(compare_function_test) },
214 { 0, 0, 0}
215 };
216
217 collection_st collection[] ={
218 {"basic", 0, 0, basic},
219 {0, 0, 0, 0}
220 };
221
222 void get_world(libtest::Framework* world)
223 {
224 world->collections(collection);
225 }