]> begriffs open source - libderp/blob - test/t_hashmap.c
Use sed rather than m4 for rhel portability
[libderp] / test / t_hashmap.c
1 #include "derp/hashmap.h"
2
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 int ivals[] = {0,1,2,3,4,5,6,7,8,9};
8
9 unsigned long djb2hash(const void *x)
10 {
11         const char *str = x;
12         unsigned long hash = 5381;
13         int c;
14
15         if (str)
16                 while ( (c = *str++) )
17                         hash = hash * 33 + c;
18         return hash;
19 }
20
21 int scmp(const void *a, const void *b, void *aux)
22 {
23         (void)aux;
24         return strcmp(a, b);
25 }
26
27 void myfree(void *a, void *aux)
28 {
29         (void)aux;
30         free(a);
31 }
32
33 int main(void)
34 {
35         hashmap *h = hm_new(0, djb2hash, scmp, NULL);
36         hm_iter *i;
37         assert(hm_length(h) == 0);
38         assert(hm_is_empty(h));
39         i = hm_iter_begin(h);
40         assert(!hm_iter_next(i));
41         hm_iter_free(i);
42
43         assert(!hm_at(h, "zero"));
44         hm_insert(h, "zero", ivals);
45         assert(hm_length(h) == 1);
46         assert(*(int*)hm_at(h, "zero") == 0);
47
48         /* change it */
49         hm_insert(h, "zero", ivals+1);
50         assert(hm_length(h) == 1);
51         assert(*(int*)hm_at(h, "zero") == 1);
52         /* set it back */
53         hm_insert(h, "zero", ivals);
54         assert(*(int*)hm_at(h, "zero") == 0);
55
56         hm_insert(h, "one", ivals+1);
57         assert(hm_length(h) == 2);
58         assert(*(int*)hm_at(h, "zero") == 0);
59         assert(*(int*)hm_at(h, "one") == 1);
60         assert(!hm_at(h, "flurgle"));
61
62         struct map_pair *p;
63         int n_keys = 0;
64         for (i = hm_iter_begin(h); (p = hm_iter_next(i)); n_keys++)
65                 assert(strcmp((char*)p->k, "zero") == 0 ||
66                        strcmp((char*)p->k, "one") == 0);
67         hm_iter_free(i);
68         assert(n_keys == 2);
69
70         hm_remove(h, "one");
71         assert(!hm_at(h, "one"));
72
73         hm_clear(h);
74         assert(hm_length(h) == 0);
75         assert(!hm_at(h, "zero"));
76
77         /* test for memory leak */
78         hm_dtor(h, myfree, myfree, NULL);
79         char *key = malloc(5);
80         int  *val1 = malloc(sizeof *val1),
81              *val2 = malloc(sizeof *val2);
82         strcpy(key, "life");
83         *val1 = 42;
84         *val2 = 13;
85         hm_insert(h, key, val1);
86         hm_insert(h, key, val2);
87
88         hm_free(h);
89
90         /* test iterator when hashmap has only one bucket */
91         hashmap *h1 = hm_new(1, djb2hash, scmp, NULL);
92         assert(hm_is_empty(h1));
93         hm_insert(h1, "zero", ivals);
94         hm_insert(h1, "one", ivals+1);
95         assert(hm_length(h1) == 2);
96         for (n_keys = 0, i = hm_iter_begin(h1); (p = hm_iter_next(i)); n_keys++)
97                 ;
98         assert(n_keys == 2);
99         hm_iter_free(i);
100         hm_free(h1);
101
102         return 0;
103 }