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++) )
30 derp_use_alloc_funcs(GC_malloc, GC_realloc, GC_free);
33 hashmap *h = hm_new(0, djb2hash, derp_strcmp, NULL);
35 assert(hm_length(h) == 0);
36 assert(hm_is_empty(h));
38 assert(!hm_iter_next(i));
41 assert(!hm_at(h, "zero"));
42 hm_insert(h, "zero", ivals);
43 assert(hm_length(h) == 1);
44 assert(*(int*)hm_at(h, "zero") == 0);
47 hm_insert(h, "zero", ivals+1);
48 assert(hm_length(h) == 1);
49 assert(*(int*)hm_at(h, "zero") == 1);
51 hm_insert(h, "zero", ivals);
52 assert(*(int*)hm_at(h, "zero") == 0);
54 hm_insert(h, "one", ivals+1);
55 assert(hm_length(h) == 2);
56 assert(*(int*)hm_at(h, "zero") == 0);
57 assert(*(int*)hm_at(h, "one") == 1);
58 assert(!hm_at(h, "flurgle"));
62 for (i = hm_iter_begin(h); (p = hm_iter_next(i)); n_keys++)
63 assert(strcmp((char*)p->k, "zero") == 0 ||
64 strcmp((char*)p->k, "one") == 0);
69 assert(!hm_at(h, "one"));
72 assert(hm_length(h) == 0);
73 assert(!hm_at(h, "zero"));
75 /* test for memory leak */
76 hm_dtor(h, derp_free, derp_free, NULL);
77 char *key = malloc(5);
78 int *val1 = malloc(sizeof *val1),
79 *val2 = malloc(sizeof *val2);
83 hm_insert(h, key, val1);
84 hm_insert(h, key, val2);
88 /* test iterator when hashmap has only one bucket */
89 hashmap *h1 = hm_new(1, djb2hash, derp_strcmp, NULL);
90 assert(hm_is_empty(h1));
91 hm_insert(h1, "zero", ivals);
92 hm_insert(h1, "one", ivals+1);
93 assert(hm_length(h1) == 2);
94 for (n_keys = 0, i = hm_iter_begin(h1); (p = hm_iter_next(i)); n_keys++)