strmap: new module for ordered map of strings using a critbit tree.
[ccan] / ccan / strmap / test / run.c
1 #include <ccan/strmap/strmap.h>
2 #include <ccan/strmap/strmap.c>
3 #include <ccan/tap/tap.h>
4
5 int main(void)
6 {
7         struct strmap_charp {
8                 STRMAP_MEMBERS(char *);
9         } map;
10         const char str[] = "hello";
11         const char val[] = "there";
12         const char none[] = "";
13         char *dup = strdup(str);
14         char *v;
15
16         /* This is how many tests you plan to run */
17         plan_tests(31);
18
19         strmap_init(&map);
20
21         ok1(!strmap_get(&map, str));
22         ok1(!strmap_get(&map, none));
23         ok1(!strmap_del(&map, str, NULL));
24         ok1(!strmap_del(&map, none, NULL));
25
26         ok1(strmap_add(&map, str, val));
27         ok1(strmap_get(&map, str) == val);
28         /* We compare the string, not the pointer. */
29         ok1(strmap_get(&map, dup) == val);
30         ok1(!strmap_get(&map, none));
31
32         /* Add a duplicate should fail. */
33         ok1(!strmap_add(&map, dup, val));
34         ok1(strmap_get(&map, dup) == val);
35
36         /* Delete should return original string. */
37         ok1(strmap_del(&map, dup, &v) == str);
38         ok1(v == val);
39         ok1(!strmap_get(&map, str));
40         ok1(!strmap_get(&map, none));
41
42         /* Try insert and delete of empty string. */
43         ok1(strmap_add(&map, none, none));
44         ok1(strmap_get(&map, none) == none);
45         ok1(!strmap_get(&map, str));
46
47         /* Delete should return original string. */
48         ok1(strmap_del(&map, "", &v) == none);
49         ok1(v == none);
50         ok1(!strmap_get(&map, str));
51         ok1(!strmap_get(&map, none));
52
53         /* Both at once... */
54         ok1(strmap_add(&map, none, none));
55         ok1(strmap_add(&map, str, val));
56         ok1(strmap_get(&map, str) == val);
57         ok1(strmap_get(&map, none) == none);
58         ok1(strmap_del(&map, "does not exist", NULL) == NULL);
59         ok1(strmap_del(&map, "", NULL) == none);
60         ok1(strmap_get(&map, str) == val);
61         ok1(strmap_del(&map, dup, &v) == str);
62         ok1(v == val);
63
64         ok1(strmap_empty(&map));
65         free(dup);
66
67         /* This exits depending on whether all tests passed */
68         return exit_status();
69 }