]> git.ozlabs.org Git - ccan/blob - ccan/intmap/test/run.c
intmap: new module.
[ccan] / ccan / intmap / test / run.c
1 #include <ccan/intmap/intmap.h>
2 #include <ccan/intmap/intmap.c>
3 #include <ccan/tap/tap.h>
4
5 int main(void)
6 {
7         UINTMAP(char *) map;
8         const char val[] = "there";
9         const char none[] = "";
10
11         /* This is how many tests you plan to run */
12         plan_tests(28);
13
14         uintmap_init(&map);
15
16         ok1(!uintmap_get(&map, 1));
17         ok1(errno == ENOENT);
18         ok1(!uintmap_get(&map, 0));
19         ok1(errno == ENOENT);
20         ok1(!uintmap_del(&map, 1));
21         ok1(errno == ENOENT);
22         ok1(!uintmap_del(&map, 0));
23         ok1(errno == ENOENT);
24
25         ok1(uintmap_add(&map, 1, val));
26         ok1(uintmap_get(&map, 1) == val);
27         ok1(!uintmap_get(&map, 0));
28         ok1(errno == ENOENT);
29
30         /* Add a duplicate should fail. */
31         ok1(!uintmap_add(&map, 1, val));
32         ok1(errno == EEXIST);
33
34         /* Delete should succeed. */
35         ok1(uintmap_del(&map, 1) == val);
36         ok1(!uintmap_get(&map, 1));
37         ok1(errno == ENOENT);
38         ok1(!uintmap_get(&map, 0));
39         ok1(errno == ENOENT);
40
41         /* Both at once... */
42         ok1(uintmap_add(&map, 0, none));
43         ok1(uintmap_add(&map, 1, val));
44         ok1(uintmap_get(&map, 1) == val);
45         ok1(uintmap_get(&map, 0) == none);
46         ok1(!uintmap_del(&map, 2));
47         ok1(uintmap_del(&map, 0) == none);
48         ok1(uintmap_get(&map, 1) == val);
49         ok1(uintmap_del(&map, 1) == val);
50
51         ok1(uintmap_empty(&map));
52
53         /* This exits depending on whether all tests passed */
54         return exit_status();
55 }