make xhash request for less allocated space
[archipelago] / xseg / xtypes / xhash_test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "xhash.h"
5
6 #define LOOPS 1000000
7
8 xhash_t *my_resize(xhash_t *h, ul_t sizeshift)
9 {
10         ssize_t bytes = xhash_get_alloc_size(sizeshift);
11         xhash_t *new = malloc(bytes);
12         if (!new) {
13                 perror("malloc");
14                 exit(1);
15         }
16         xhash_resize(h, sizeshift, new);
17         free(h);
18         return new;
19 }
20
21
22 int main(int argc, char **argv) {
23     ul_t loops, i, v;
24     struct xhash *h;
25     int rr;
26
27     if (argc > 1) {
28         loops = atoi(argv[1]);
29     } else {
30         loops = LOOPS;
31     }
32
33     h = malloc(xhash_get_alloc_size(2));
34     if (!h){
35         perror("malloc");
36         exit(1);
37     }
38     xhash_init(h, 2);
39     for (i = 10; i < loops; i++) {
40         int ret;
41         ul_t r;
42         //printf("insert(%lx, %lx)\n", i, -i);
43         rr = xhash_insert(h, i, -i);
44         if (rr == -XHASH_ERESIZE){
45                 h = my_resize(h, grow_size_shift(h));
46                 rr = xhash_insert(h, i, -i);
47                 if (rr != 0)
48                         printf("resize insert error in %lx: %lx != %lx\n", i, r, -i);
49         }
50         ret = xhash_lookup(h, i, &r);
51         if (ret || (r != -i)) {
52             printf("insert error in %lx: %lx != %lx\n", i, r, -i);
53         }
54         //printf(" ->got(%lx, %lx)\n", i, r);
55     }
56     for (i = 10; i < loops; i++) {
57         int ret = xhash_lookup(h, i, &v);
58         //printf(" ->got(%lu, %lu)\n", i, v);
59         if (ret || (i != -v)) {
60             printf("error in %lu: %lu != %lu\n", i, i, -v);
61             getchar();
62         }
63     }
64     for (i = 10; i < loops; i++) {
65         int ret;
66         ul_t r;
67         //printf("insert(%lx, %lx)\n", i, -i);
68         rr = xhash_delete(h, i);
69         if (rr == -XHASH_ERESIZE){
70                 h = my_resize(h, shrink_size_shift(h));
71                 rr = xhash_delete(h, i);
72                 if (rr != 0)
73                         printf("resize delele error in %lx: %lx != %lx\n", i, r, -i);
74         }
75         ret = xhash_lookup(h, i, &r);
76         if (!ret) {
77             printf("delete error in %lx: %lx != %lx\n", i, r, -i);
78         }
79         //printf(" ->got(%lx, %lx)\n", i, r);
80     }
81     free(h);
82
83     printf("test completed successfully\n");
84     return 0;
85 }