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