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