7 int ivals[] = {0,1,2,3,4,5,6,7,8,9};
9 unsigned long djb2hash(const void *x)
12 unsigned long hash = 5381;
16 while ( (c = *str++) )
21 int scmp(const void *a, const void *b, void *aux)
27 void myfree(void *a, void *aux)
35 hashmap *h = hm_new(0, djb2hash, scmp, NULL);
37 assert(hm_length(h) == 0);
38 assert(hm_is_empty(h));
40 assert(!hm_iter_next(&i));
42 assert(!hm_at(h, "zero"));
43 hm_insert(h, "zero", ivals);
44 assert(hm_length(h) == 1);
45 assert(*(int*)hm_at(h, "zero") == 0);
48 hm_insert(h, "zero", ivals+1);
49 assert(hm_length(h) == 1);
50 assert(*(int*)hm_at(h, "zero") == 1);
52 hm_insert(h, "zero", ivals);
53 assert(*(int*)hm_at(h, "zero") == 0);
55 hm_insert(h, "one", ivals+1);
56 assert(hm_length(h) == 2);
57 assert(*(int*)hm_at(h, "zero") == 0);
58 assert(*(int*)hm_at(h, "one") == 1);
59 assert(!hm_at(h, "flurgle"));
63 for (hm_iter_begin(h, &i); (p = hm_iter_next(&i)); n_keys++)
64 assert(strcmp((char*)p->k, "zero") == 0 ||
65 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, myfree, myfree, 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);