]> git.ozlabs.org Git - ccan/blob - ccan/strmap/test/run.c
strmap: set errno to ENOENT even if we return NULL.
[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(42);
18
19         strmap_init(&map);
20
21         ok1(!strmap_get(&map, str));
22         ok1(errno == ENOENT);
23         ok1(!strmap_get(&map, none));
24         ok1(errno == ENOENT);
25         ok1(!strmap_del(&map, str, NULL));
26         ok1(errno == ENOENT);
27         ok1(!strmap_del(&map, none, NULL));
28         ok1(errno == ENOENT);
29
30         ok1(strmap_add(&map, str, val));
31         ok1(strmap_get(&map, str) == val);
32         /* We compare the string, not the pointer. */
33         ok1(strmap_get(&map, dup) == val);
34         ok1(!strmap_get(&map, none));
35         ok1(errno == ENOENT);
36
37         /* Add a duplicate should fail. */
38         ok1(!strmap_add(&map, dup, val));
39         ok1(errno == EEXIST);
40         ok1(strmap_get(&map, dup) == val);
41
42         /* Delete should return original string. */
43         ok1(strmap_del(&map, dup, &v) == str);
44         ok1(v == val);
45         ok1(!strmap_get(&map, str));
46         ok1(errno == ENOENT);
47         ok1(!strmap_get(&map, none));
48         ok1(errno == ENOENT);
49
50         /* Try insert and delete of empty string. */
51         ok1(strmap_add(&map, none, none));
52         ok1(strmap_get(&map, none) == none);
53         ok1(!strmap_get(&map, str));
54         ok1(errno == ENOENT);
55
56         /* Delete should return original string. */
57         ok1(strmap_del(&map, "", &v) == none);
58         ok1(v == none);
59         ok1(!strmap_get(&map, str));
60         ok1(errno == ENOENT);
61         ok1(!strmap_get(&map, none));
62         ok1(errno == ENOENT);
63
64         /* Both at once... */
65         ok1(strmap_add(&map, none, none));
66         ok1(strmap_add(&map, str, val));
67         ok1(strmap_get(&map, str) == val);
68         ok1(strmap_get(&map, none) == none);
69         ok1(strmap_del(&map, "does not exist", NULL) == NULL);
70         ok1(strmap_del(&map, "", NULL) == none);
71         ok1(strmap_get(&map, str) == val);
72         ok1(strmap_del(&map, dup, &v) == str);
73         ok1(v == val);
74
75         ok1(strmap_empty(&map));
76         free(dup);
77
78         /* This exits depending on whether all tests passed */
79         return exit_status();
80 }