compiler: RETURNS_NONNULL macro
[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         STRMAP(char *) map;
8         const char str[] = "hello";
9         const char val[] = "there";
10         const char none[] = "";
11         char *dup = strdup(str);
12         char *v;
13
14         /* This is how many tests you plan to run */
15         plan_tests(42);
16
17         strmap_init(&map);
18
19         ok1(!strmap_get(&map, str));
20         ok1(errno == ENOENT);
21         ok1(!strmap_get(&map, none));
22         ok1(errno == ENOENT);
23         ok1(!strmap_del(&map, str, NULL));
24         ok1(errno == ENOENT);
25         ok1(!strmap_del(&map, none, NULL));
26         ok1(errno == ENOENT);
27
28         ok1(strmap_add(&map, str, val));
29         ok1(strmap_get(&map, str) == val);
30         /* We compare the string, not the pointer. */
31         ok1(strmap_get(&map, dup) == val);
32         ok1(!strmap_get(&map, none));
33         ok1(errno == ENOENT);
34
35         /* Add a duplicate should fail. */
36         ok1(!strmap_add(&map, dup, val));
37         ok1(errno == EEXIST);
38         ok1(strmap_get(&map, dup) == val);
39
40         /* Delete should return original string. */
41         ok1(strmap_del(&map, dup, &v) == str);
42         ok1(v == val);
43         ok1(!strmap_get(&map, str));
44         ok1(errno == ENOENT);
45         ok1(!strmap_get(&map, none));
46         ok1(errno == ENOENT);
47
48         /* Try insert and delete of empty string. */
49         ok1(strmap_add(&map, none, none));
50         ok1(strmap_get(&map, none) == none);
51         ok1(!strmap_get(&map, str));
52         ok1(errno == ENOENT);
53
54         /* Delete should return original string. */
55         ok1(strmap_del(&map, "", &v) == none);
56         ok1(v == none);
57         ok1(!strmap_get(&map, str));
58         ok1(errno == ENOENT);
59         ok1(!strmap_get(&map, none));
60         ok1(errno == ENOENT);
61
62         /* Both at once... */
63         ok1(strmap_add(&map, none, none));
64         ok1(strmap_add(&map, str, val));
65         ok1(strmap_get(&map, str) == val);
66         ok1(strmap_get(&map, none) == none);
67         ok1(strmap_del(&map, "does not exist", NULL) == NULL);
68         ok1(strmap_del(&map, "", NULL) == none);
69         ok1(strmap_get(&map, str) == val);
70         ok1(strmap_del(&map, dup, &v) == str);
71         ok1(v == val);
72
73         ok1(strmap_empty(&map));
74         free(dup);
75
76         /* This exits depending on whether all tests passed */
77         return exit_status();
78 }