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