]> begriffs open source - libderp/blob - test/t_hashmap.c
WIP: begriffs style .so installation
[libderp] / test / t_hashmap.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "derp/common.h"
6 #include "derp/hashmap.h"
7
8 #ifdef HAVE_BOEHM_GC
9 #include <gc/leak_detector.h>
10 #endif
11
12 int ivals[] = {0,1,2,3,4,5,6,7,8,9};
13
14 unsigned long djb2hash(const void *x)
15 {
16         const char *str = x;
17         unsigned long hash = 5381;
18         int c;
19
20         if (str)
21                 while ( (c = *str++) )
22                         hash = hash * 33 + c;
23         return hash;
24 }
25
26 int main(void)
27 {
28 #ifdef HAVE_BOEHM_GC
29         GC_set_find_leak(1);
30         derp_use_alloc_funcs(
31                 GC_debug_malloc_replacement,
32                 GC_debug_realloc_replacement, GC_debug_free);
33 #endif
34
35         hashmap *h = hm_new(0, djb2hash, derp_strcmp, 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, derp_free, derp_free, 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, derp_strcmp, 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 #ifdef HAVE_BOEHM_GC
103         CHECK_LEAKS();
104 #endif
105         return 0;
106 }