jmap: fix aliasing issues, one real testcase bug
[ccan] / ccan / jmap / test / run-uintidx-type.c
1 #include <ccan/tap/tap.h>
2 #include <ccan/jmap/jmap_type.h>
3 #include <ccan/jmap/jmap.c>
4
5 struct foo;
6
7 JMAP_DEFINE_UINTIDX_TYPE(struct foo, foo);
8
9 #define NUM 100
10
11 int main(int argc, char *argv[])
12 {
13         struct jmap_foo *map;
14         struct foo *foo[NUM], **foop;
15         unsigned int i;
16
17         plan_tests(37 + NUM*2 + 19);
18         for (i = 0; i < NUM; i++)
19                 foo[i] = malloc(20);
20
21         map = jmap_foo_new();
22         ok1(jmap_foo_error(map) == NULL);
23
24         ok1(jmap_foo_test(map, 0) == false);
25         ok1(jmap_foo_get(map, 0) == (struct foo *)NULL);
26         ok1(jmap_foo_popcount(map, 0, -1) == 0);
27         ok1(jmap_foo_first(map, 0) == 0);
28         ok1(jmap_foo_last(map, 0) == 0);
29         ok1(jmap_foo_del(map, 0) == false);
30
31         /* Set only works on existing cases. */
32         ok1(jmap_foo_set(map, 0, foo[0]) == false);
33         ok1(jmap_foo_add(map, 0, foo[1]) == true);
34         ok1(jmap_foo_get(map, 0) == foo[1]);
35         ok1(jmap_foo_set(map, 0, foo[0]) == true);
36         ok1(jmap_foo_get(map, 0) == foo[0]);
37
38         ok1(jmap_foo_test(map, 0) == true);
39         ok1(jmap_foo_popcount(map, 0, -1) == 1);
40         ok1(jmap_foo_first(map, -1) == 0);
41         ok1(jmap_foo_last(map, -1) == 0);
42         ok1(jmap_foo_next(map, 0, -1) == (size_t)-1);
43         ok1(jmap_foo_prev(map, 0, -1) == (size_t)-1);
44
45         ok1(jmap_foo_del(map, 0) == true);
46         ok1(jmap_foo_test(map, 0) == false);
47         ok1(jmap_foo_popcount(map, 0, -1) == 0);
48
49         for (i = 0; i < NUM; i++)
50                 jmap_foo_add(map, i, foo[i]);
51
52         ok1(jmap_foo_popcount(map, 0, -1) == NUM);
53         ok1(jmap_foo_popcount(map, 0, NUM-1) == NUM);
54         ok1(jmap_foo_popcount(map, 0, NUM/2-1) == NUM/2);
55         ok1(jmap_foo_popcount(map, NUM/2, NUM) == NUM - NUM/2);
56
57         ok1(jmap_foo_nth(map, 0, -1) == 0);
58         ok1(jmap_foo_nth(map, NUM-1, -1) == NUM-1);
59         ok1(jmap_foo_nth(map, NUM, -1) == (size_t)-1);
60         ok1(jmap_foo_first(map, -1) == 0);
61         ok1(jmap_foo_last(map, -1) == NUM-1);
62         ok1(jmap_foo_next(map, 0, -1) == 1);
63         ok1(jmap_foo_next(map, NUM-1, -1) == (size_t)-1);
64         ok1(jmap_foo_prev(map, 1, -1) == 0);
65         ok1(jmap_foo_prev(map, 0, -1) == (size_t)-1);
66
67         ok1(jmap_foo_get(map, 0) == foo[0]);
68         ok1(jmap_foo_get(map, NUM-1) == foo[NUM-1]);
69         ok1(jmap_foo_get(map, NUM) == NULL);
70
71         /* Reverse values in map. */
72         for (i = 0; i < NUM; i++) {
73                 foop = jmap_foo_getval(map, i);
74                 ok1(*foop == foo[i]);
75                 *foop = foo[NUM-1-i];
76                 jmap_foo_putval(map, &foop);
77         }
78         for (i = 0; i < NUM; i++)
79                 ok1(jmap_foo_get(map, i) == foo[NUM-1-i]);
80
81         foop = jmap_foo_nthval(map, 0, &i);
82         ok1(i == 0);
83         ok1(*foop == foo[NUM-1]);
84         jmap_foo_putval(map, &foop);
85         foop = jmap_foo_nthval(map, NUM-1, &i);
86         ok1(i == NUM-1);
87         ok1(*foop == foo[0]);
88         jmap_foo_putval(map, &foop);
89
90         foop = jmap_foo_firstval(map, &i);
91         ok1(i == 0);
92         ok1(*foop == foo[NUM-1]);
93         jmap_foo_putval(map, &foop);
94
95         foop = jmap_foo_nextval(map, &i);
96         ok1(i == 1);
97         ok1(*foop == foo[NUM-2]);
98         jmap_foo_putval(map, &foop);
99
100         foop = jmap_foo_prevval(map, &i);
101         ok1(i == 0);
102         ok1(*foop == foo[NUM-1]);
103         jmap_foo_putval(map, &foop);
104
105         foop = jmap_foo_prevval(map, &i);
106         ok1(foop == NULL);
107
108         foop = jmap_foo_lastval(map, &i);
109         ok1(i == NUM-1);
110         ok1(*foop == foo[0]);
111         jmap_foo_putval(map, &foop);
112
113         foop = jmap_foo_prevval(map, &i);
114         ok1(i == NUM-2);
115         ok1(*foop == foo[1]);
116         jmap_foo_putval(map, &foop);
117
118         foop = jmap_foo_nextval(map, &i);
119         ok1(i == NUM-1);
120         ok1(*foop == foo[0]);
121         jmap_foo_putval(map, &foop);
122
123         foop = jmap_foo_nextval(map, &i);
124         ok1(foop == NULL);
125
126         ok1(jmap_foo_error(map) == NULL);
127         jmap_foo_free(map);
128
129         return exit_status();
130 }