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