]> begriffs open source - libderp/blob - test/t_hashmap.c
Hore hashmap tests
[libderp] / test / t_hashmap.c
1 #include "hashmap.h"
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <stdio.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         uint_fast32_t 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 int main(void)
28 {
29         hashmap *h = hm_new(0, djb2hash, scmp, NULL, NULL, NULL);
30         assert(hm_length(h) == 0);
31         assert(hm_is_empty(h));
32
33         assert(!hm_at(h, "zero"));
34         hm_insert(h, "zero", ivals);
35         assert(hm_length(h) == 1);
36         assert(hm_at(h, "zero"));
37         assert(*(int*)hm_at(h, "zero") == 0);
38
39         hm_insert(h, "one", ivals+1);
40         assert(hm_length(h) == 2);
41         assert(hm_at(h, "one"));
42         assert(*(int*)hm_at(h, "zero") == 0);
43         assert(*(int*)hm_at(h, "one") == 1);
44         assert(!hm_at(h, "flurgle"));
45
46         hm_remove(h, "one");
47         assert(!hm_at(h, "one"));
48
49         hm_clear(h);
50         assert(hm_length(h) == 0);
51         assert(!hm_at(h, "zero"));
52
53         hm_free(h);
54
55         return 0;
56 }