compiler: RETURNS_NONNULL macro
[ccan] / ccan / jmap / test / run-uintidx-type.c
1 #include <ccan/tap/tap.h>
2 #include <ccan/jmap/jmap.c>
3
4 struct foo;
5
6 struct jmap_foo {
7         JMAP_MEMBERS(unsigned long, struct foo *);
8 };
9
10 #define NUM 100
11
12 int main(int argc, char *argv[])
13 {
14         struct jmap_foo *map;
15         struct foo *foo[NUM], **foop;
16         unsigned long i;
17
18         plan_tests(40 + NUM*2 + 19);
19         for (i = 0; i < NUM; i++)
20                 foo[i] = malloc(20);
21
22         map = jmap_new(struct jmap_foo);
23         ok1(jmap_error(map) == NULL);
24
25         ok1(jmap_test(map, 0) == false);
26         ok1(jmap_get(map, 0) == (struct foo *)NULL);
27         ok1(jmap_popcount(map, 0, -1) == 0);
28         ok1(jmap_first(map) == 0);
29         ok1(jmap_last(map) == 0);
30         ok1(jmap_del(map, 0) == false);
31
32         /* Set only works on existing cases. */
33         ok1(jmap_set(map, 1, foo[0]) == false);
34         ok1(jmap_add(map, 1, foo[1]) == true);
35         ok1(jmap_get(map, 1) == foo[1]);
36         ok1(jmap_set(map, 1, foo[0]) == true);
37         ok1(jmap_get(map, 1) == foo[0]);
38
39         ok1(jmap_test(map, 1) == true);
40         ok1(jmap_popcount(map, 0, -1) == 1);
41         ok1(jmap_first(map) == 1);
42         ok1(jmap_last(map) == 1);
43         ok1(jmap_next(map, 0) == 1);
44         ok1(jmap_next(map, 1) == 0);
45         ok1(jmap_prev(map, 2) == 1);
46         ok1(jmap_prev(map, 1) == 0);
47
48         ok1(jmap_del(map, 1) == true);
49         ok1(jmap_test(map, 1) == false);
50         ok1(jmap_popcount(map, 0, -1) == 0);
51
52         for (i = 0; i < NUM; i++)
53                 jmap_add(map, i+1, foo[i]);
54
55         ok1(jmap_count(map) == NUM);
56         ok1(jmap_popcount(map, 0, -1) == NUM);
57         ok1(jmap_popcount(map, 1, NUM) == NUM);
58         ok1(jmap_popcount(map, 1, NUM/2) == NUM/2);
59         ok1(jmap_popcount(map, NUM/2+1, NUM) == NUM - NUM/2);
60
61         ok1(jmap_nth(map, 0, -1) == 1);
62         ok1(jmap_nth(map, NUM-1, -1) == NUM);
63         ok1(jmap_nth(map, NUM, -1) == (size_t)-1);
64         ok1(jmap_first(map) == 1);
65         ok1(jmap_last(map) == NUM);
66         ok1(jmap_next(map, 1) == 2);
67         ok1(jmap_next(map, NUM) == 0);
68         ok1(jmap_prev(map, 2) == 1);
69         ok1(jmap_prev(map, 1) == 0);
70
71         ok1(jmap_get(map, 1) == foo[0]);
72         ok1(jmap_get(map, NUM) == foo[NUM-1]);
73         ok1(jmap_get(map, NUM+1) == NULL);
74
75         /* Reverse values in map. */
76         for (i = 0; i < NUM; i++) {
77                 foop = jmap_getval(map, i+1);
78                 ok1(*foop == foo[i]);
79                 *foop = foo[NUM-1-i];
80                 jmap_putval(map, &foop);
81         }
82         for (i = 0; i < NUM; i++)
83                 ok1(jmap_get(map, i+1) == foo[NUM-1-i]);
84
85         foop = jmap_nthval(map, 0, &i);
86         ok1(i == 1);
87         ok1(*foop == foo[NUM-1]);
88         jmap_putval(map, &foop);
89         foop = jmap_nthval(map, NUM-1, &i);
90         ok1(i == NUM);
91         ok1(*foop == foo[0]);
92         jmap_putval(map, &foop);
93
94         foop = jmap_firstval(map, &i);
95         ok1(i == 1);
96         ok1(*foop == foo[NUM-1]);
97         jmap_putval(map, &foop);
98
99         foop = jmap_nextval(map, &i);
100         ok1(i == 2);
101         ok1(*foop == foo[NUM-2]);
102         jmap_putval(map, &foop);
103
104         foop = jmap_prevval(map, &i);
105         ok1(i == 1);
106         ok1(*foop == foo[NUM-1]);
107         jmap_putval(map, &foop);
108
109         foop = jmap_prevval(map, &i);
110         ok1(foop == NULL);
111
112         foop = jmap_lastval(map, &i);
113         ok1(i == NUM);
114         ok1(*foop == foo[0]);
115         jmap_putval(map, &foop);
116
117         foop = jmap_prevval(map, &i);
118         ok1(i == NUM-1);
119         ok1(*foop == foo[1]);
120         jmap_putval(map, &foop);
121
122         foop = jmap_nextval(map, &i);
123         ok1(i == NUM);
124         ok1(*foop == foo[0]);
125         jmap_putval(map, &foop);
126
127         foop = jmap_nextval(map, &i);
128         ok1(foop == NULL);
129
130         ok1(jmap_error(map) == NULL);
131         jmap_free(map);
132
133         for (i = 0; i < NUM; i++)
134                 free(foo[i]);
135
136         return exit_status();
137 }