root / xseg / xtypes / xhash_test.c @ 5be18103
History | View | Annotate | Download (7.8 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2012 GRNET S.A. All rights reserved.
|
3 |
*
|
4 |
* Redistribution and use in source and binary forms, with or
|
5 |
* without modification, are permitted provided that the following
|
6 |
* conditions are met:
|
7 |
*
|
8 |
* 1. Redistributions of source code must retain the above
|
9 |
* copyright notice, this list of conditions and the following
|
10 |
* disclaimer.
|
11 |
* 2. Redistributions in binary form must reproduce the above
|
12 |
* copyright notice, this list of conditions and the following
|
13 |
* disclaimer in the documentation and/or other materials
|
14 |
* provided with the distribution.
|
15 |
*
|
16 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
* POSSIBILITY OF SUCH DAMAGE.
|
28 |
*
|
29 |
* The views and conclusions contained in the software and
|
30 |
* documentation are those of the authors and should not be
|
31 |
* interpreted as representing official policies, either expressed
|
32 |
* or implied, of GRNET S.A.
|
33 |
*/
|
34 |
|
35 |
#include <stdio.h> |
36 |
#include <stdlib.h> |
37 |
|
38 |
#include "xhash.h" |
39 |
|
40 |
#define LOOPS 1000000 |
41 |
#define STRINGLEN 63 |
42 |
|
43 |
xhash_t *my_resize(xhash_t *h, xhashidx sizeshift) |
44 |
{ |
45 |
ssize_t bytes = xhash_get_alloc_size(sizeshift); |
46 |
xhash_t *new = malloc(bytes); |
47 |
if (!new) {
|
48 |
perror("malloc");
|
49 |
exit(1);
|
50 |
} |
51 |
xhash_resize(h, sizeshift, 0, new);
|
52 |
free(h); |
53 |
return new;
|
54 |
} |
55 |
|
56 |
void make_chunk(char *string, xhashidx id) |
57 |
{ |
58 |
xhashidx i; |
59 |
for (i = 0; i < STRINGLEN; i++) { |
60 |
string[i] = 'a' + !(!(id & (1 << i))); |
61 |
} |
62 |
string[STRINGLEN] = 0;
|
63 |
|
64 |
} |
65 |
|
66 |
int chekck_chunk(char *string, xhashidx s) |
67 |
{ |
68 |
xhashidx i; |
69 |
for (i = 0; i < STRINGLEN; i++) { |
70 |
if (string[i] != s + i)
|
71 |
return -1; |
72 |
} |
73 |
if (string[STRINGLEN] != 0) |
74 |
return -1; |
75 |
|
76 |
return 0; |
77 |
} |
78 |
|
79 |
int test_string(xhashidx loops)
|
80 |
{ |
81 |
xhashidx i, v; |
82 |
struct xhash *h;
|
83 |
int rr;
|
84 |
char **string = malloc(sizeof(char *) * loops); |
85 |
if (!string) {
|
86 |
perror("malloc");
|
87 |
exit(1);
|
88 |
} |
89 |
for (i = 0; i < loops; i++) { |
90 |
string[i] = malloc(sizeof(char) * STRINGLEN+1); |
91 |
if (!string[i]) {
|
92 |
perror("malloc");
|
93 |
exit(1);
|
94 |
} |
95 |
make_chunk(string[i], i); |
96 |
} |
97 |
|
98 |
h = malloc(xhash_get_alloc_size(2));
|
99 |
if (!h){
|
100 |
perror("malloc");
|
101 |
exit(1);
|
102 |
} |
103 |
xhash_init(h, 2, 0, STRING); |
104 |
for (i = 10; i < loops; i++) { |
105 |
int ret;
|
106 |
xhashidx r; |
107 |
//printf("insert(%lx, %lx)\n", i, -i);
|
108 |
rr = xhash_insert(h, string[i], i); |
109 |
if (rr == -XHASH_ERESIZE){
|
110 |
h = my_resize(h, xhash_grow_size_shift(h)); |
111 |
rr = xhash_insert(h, string[i], i); |
112 |
if (rr != 0) |
113 |
printf("resize string insert error in %lx: %lx != %lx\n", i, r, i);
|
114 |
} |
115 |
ret = xhash_lookup(h, string[i], &r); |
116 |
if (ret || (r != i)) {
|
117 |
printf("string insert error in %lx (ret: %d): returned val %lx, expected val %lx\n ", i, ret, r, i);
|
118 |
} |
119 |
//printf(" ->got(%lx, %lx)\n", i, r);
|
120 |
} |
121 |
for (i = 10; i < loops; i++) { |
122 |
int ret = xhash_lookup(h, string[i], &v);
|
123 |
//printf(" ->got(%lu, %lu)\n", i, v);
|
124 |
if (ret || (i != v)) {
|
125 |
printf("string error in %lu: %lu != %lu\n", i, i, v);
|
126 |
getchar(); |
127 |
} |
128 |
} |
129 |
for (i = 10; i < loops; i++) { |
130 |
int ret;
|
131 |
xhashidx r; |
132 |
//printf("insert(%lx, %lx)\n", i, -i);
|
133 |
rr = xhash_delete(h, string[i]); |
134 |
if (rr == -XHASH_ERESIZE){
|
135 |
h = my_resize(h, xhash_shrink_size_shift(h)); |
136 |
rr = xhash_delete(h, string[i]); |
137 |
if (rr != 0) |
138 |
printf("resize string delele error in %lx: %lx != %lx\n", i, r, i);
|
139 |
} |
140 |
ret = xhash_lookup(h, string[i], &r); |
141 |
if (!ret) {
|
142 |
printf("string delete error in %lx: %lx != %lx\n", i, r, i);
|
143 |
} |
144 |
//printf(" ->got(%lx, %lx)\n", i, r);
|
145 |
} |
146 |
free(h); |
147 |
|
148 |
return 0; |
149 |
} |
150 |
|
151 |
int test_string2()
|
152 |
{ |
153 |
xhashidx i, v; |
154 |
struct xhash *h;
|
155 |
int rr;
|
156 |
char *string[4]; |
157 |
string[0] = "b79111bca0cfd1fa9ff0f435357567bcb016cd4949e55484a9b49b129fc5f757"; |
158 |
string[1] = "bf508f5b-7bc2-4963-9fd3-5e06ffe1b50e.ext.disk0"; |
159 |
string[2] = "e5856732-30c4-4f2b-b888-897b9079eddc.ext.disk0"; |
160 |
string[3] = "11b7e801-ea1a-44ab-9d25-ed675da29747.ext.disk0"; |
161 |
|
162 |
h = malloc(xhash_get_alloc_size(3));
|
163 |
if (!h){
|
164 |
perror("malloc");
|
165 |
exit(1);
|
166 |
} |
167 |
xhash_init(h, 3, 0, STRING); |
168 |
for (i = 0; i < 4; i++) { |
169 |
int ret;
|
170 |
xhashidx r; |
171 |
rr = xhash_insert(h, (xhashidx) string[i], (xhashidx) i); |
172 |
if (rr == -XHASH_ERESIZE){
|
173 |
h = my_resize(h, xhash_grow_size_shift(h)); |
174 |
rr = xhash_insert(h, string[i], (xhashidx) i); |
175 |
if (rr != 0) |
176 |
printf("resize string insert error in %lx: %lx != %lx\n", i, r, i);
|
177 |
} |
178 |
ret = xhash_lookup(h, (xhashidx)string[i], (xhashidx *) &r); |
179 |
if (ret || (r != i)) {
|
180 |
printf("string insert error in %lx (ret: %d): returned val %lx, expected val %lx\n ", i, ret, r, i);
|
181 |
} |
182 |
} |
183 |
for (i = 0; i < 4; i++) { |
184 |
int ret = xhash_lookup(h, (xhashidx)string[i], (xhashidx *) &v);
|
185 |
//printf(" ->got(%lu, %lu)\n", i, v);
|
186 |
if (ret || (i != v)) {
|
187 |
printf("string error in %lu: %lu != %lu\n", i, i, v);
|
188 |
getchar(); |
189 |
} |
190 |
} |
191 |
for (i = 00; i < 4; i++) { |
192 |
int ret;
|
193 |
xhashidx r; |
194 |
//printf("insert(%lx, %lx)\n", i, -i);
|
195 |
rr = xhash_delete(h, (xhashidx)string[i]); |
196 |
if (rr == -XHASH_ERESIZE){
|
197 |
h = my_resize(h, xhash_shrink_size_shift(h)); |
198 |
rr = xhash_delete(h, (xhashidx)string[i]); |
199 |
if (rr != 0) |
200 |
printf("resize string delele error in %lx: %lx != %lx\n", i, r, i);
|
201 |
} |
202 |
ret = xhash_lookup(h, (xhashidx) string[i], (xhashidx *) &r); |
203 |
if (!ret) {
|
204 |
printf("string delete error in %lx: %lx != %lx\n", i, r, i);
|
205 |
} |
206 |
//printf(" ->got(%lx, %lx)\n", i, r);
|
207 |
} |
208 |
free(h); |
209 |
|
210 |
return 0; |
211 |
} |
212 |
|
213 |
//TODO add test for limit
|
214 |
int main(int argc, char **argv) { |
215 |
xhashidx loops, i, v; |
216 |
struct xhash *h;
|
217 |
int rr;
|
218 |
|
219 |
if (argc > 1) { |
220 |
loops = atoi(argv[1]);
|
221 |
} else {
|
222 |
loops = LOOPS; |
223 |
} |
224 |
|
225 |
h = malloc(xhash_get_alloc_size(2));
|
226 |
if (!h){
|
227 |
perror("malloc");
|
228 |
exit(1);
|
229 |
} |
230 |
xhash_init(h, 2, 0, INTEGER); |
231 |
for (i = 10; i < loops; i++) { |
232 |
int ret;
|
233 |
xhashidx r; |
234 |
//printf("insert(%lx, %lx)\n", i, -i);
|
235 |
rr = xhash_insert(h, i, -i); |
236 |
if (rr == -XHASH_ERESIZE){
|
237 |
h = my_resize(h, xhash_grow_size_shift(h)); |
238 |
rr = xhash_insert(h, i, -i); |
239 |
if (rr != 0) |
240 |
printf("resize insert error in %lx: %lx != %lx\n", i, r, -i);
|
241 |
} |
242 |
ret = xhash_lookup(h, i, &r); |
243 |
if (ret || (r != -i)) {
|
244 |
printf("insert error in %lx: %lx != %lx\n", i, r, -i);
|
245 |
} |
246 |
//printf(" ->got(%lx, %lx)\n", i, r);
|
247 |
} |
248 |
for (i = 10; i < loops; i++) { |
249 |
int ret = xhash_lookup(h, i, &v);
|
250 |
//printf(" ->got(%lu, %lu)\n", i, v);
|
251 |
if (ret || (i != -v)) {
|
252 |
printf("error in %lu: %lu != %lu\n", i, i, -v);
|
253 |
getchar(); |
254 |
} |
255 |
} |
256 |
for (i = 10; i < loops; i++) { |
257 |
int ret;
|
258 |
xhashidx r; |
259 |
//printf("insert(%lx, %lx)\n", i, -i);
|
260 |
rr = xhash_delete(h, i); |
261 |
if (rr == -XHASH_ERESIZE){
|
262 |
h = my_resize(h, xhash_shrink_size_shift(h)); |
263 |
rr = xhash_delete(h, i); |
264 |
if (rr != 0) |
265 |
printf("resize delele error in %lx: %lx != %lx\n", i, r, -i);
|
266 |
} |
267 |
ret = xhash_lookup(h, i, &r); |
268 |
if (!ret) {
|
269 |
printf("delete error in %lx: %lx != %lx\n", i, r, -i);
|
270 |
} |
271 |
//printf(" ->got(%lx, %lx)\n", i, r);
|
272 |
} |
273 |
free(h); |
274 |
test_string(loops); |
275 |
test_string2(); |
276 |
printf("test completed successfully\n");
|
277 |
return 0; |
278 |
} |