]> git.ozlabs.org Git - ccan/blob - ccan/intmap/test/run.c
base64: fix for unsigned chars (e.g. ARM).
[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         uint64_t idx;
11
12         /* This is how many tests you plan to run */
13         plan_tests(40);
14
15         uintmap_init(&map);
16
17         ok1(!uintmap_get(&map, 1));
18         ok1(errno == ENOENT);
19         ok1(!uintmap_get(&map, 0));
20         ok1(errno == ENOENT);
21         ok1(!uintmap_del(&map, 1));
22         ok1(errno == ENOENT);
23         ok1(!uintmap_del(&map, 0));
24         ok1(errno == ENOENT);
25         ok1(!uintmap_first(&map, &idx));
26         ok1(errno == ENOENT);
27         ok1(!uintmap_last(&map, &idx));
28         ok1(errno == ENOENT);
29
30         ok1(uintmap_add(&map, 1, val));
31         ok1(uintmap_get(&map, 1) == val);
32         ok1(!uintmap_get(&map, 0));
33         ok1(errno == ENOENT);
34         ok1(uintmap_first(&map, &idx) == val);
35         ok1(idx == 1);
36         ok1(uintmap_last(&map, &idx) == val);
37         ok1(idx == 1);
38
39         /* Add a duplicate should fail. */
40         ok1(!uintmap_add(&map, 1, val));
41         ok1(errno == EEXIST);
42
43         /* Delete should succeed. */
44         ok1(uintmap_del(&map, 1) == val);
45         ok1(!uintmap_get(&map, 1));
46         ok1(errno == ENOENT);
47         ok1(!uintmap_get(&map, 0));
48         ok1(errno == ENOENT);
49
50         /* Both at once... */
51         ok1(uintmap_add(&map, 0, none));
52         ok1(uintmap_add(&map, 1, val));
53         ok1(uintmap_get(&map, 1) == val);
54         ok1(uintmap_get(&map, 0) == none);
55         ok1(uintmap_first(&map, &idx) == none);
56         ok1(idx == 0);
57         ok1(uintmap_last(&map, &idx) == val);
58         ok1(idx == 1);
59         ok1(!uintmap_del(&map, 2));
60         ok1(uintmap_del(&map, 0) == none);
61         ok1(uintmap_get(&map, 1) == val);
62         ok1(uintmap_del(&map, 1) == val);
63
64         ok1(uintmap_empty(&map));
65
66         /* This exits depending on whether all tests passed */
67         return exit_status();
68 }