5 #include "derp/common.h"
6 #include "derp/hashmap.h"
9 #include <gc/leak_detector.h>
12 int ivals[] = {0,1,2,3,4,5,6,7,8,9};
14 unsigned long djb2hash(const void *x)
17 unsigned long hash = 5381;
21 while ( (c = *str++) )
31 GC_debug_malloc_replacement,
32 GC_debug_realloc_replacement, GC_debug_free);
35 hashmap *h = hm_new(0, djb2hash, derp_strcmp, NULL);
37 assert(hm_length(h) == 0);
38 assert(hm_is_empty(h));
40 assert(!hm_iter_next(i));
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);
49 hm_insert(h, "zero", ivals+1);
50 assert(hm_length(h) == 1);
51 assert(*(int*)hm_at(h, "zero") == 1);
53 hm_insert(h, "zero", ivals);
54 assert(*(int*)hm_at(h, "zero") == 0);
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"));
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);
71 assert(!hm_at(h, "one"));
74 assert(hm_length(h) == 0);
75 assert(!hm_at(h, "zero"));
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);
85 hm_insert(h, key, val1);
86 hm_insert(h, key, val2);
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++)