]> git.ozlabs.org Git - ccan/blob - ccan/jmap/test/run-ptridx-int.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / jmap / test / run-ptridx-int.c
1 #include <ccan/tap/tap.h>
2 #include <ccan/jmap/jmap.c>
3
4 struct idx;
5
6 struct map {
7         JMAP_MEMBERS(struct idx *, int);
8 };
9
10 #define NUM 100
11
12 static int cmp_ptr(const void *a, const void *b)
13 {
14         return *(char **)a - *(char **)b;
15 }
16
17 int main(int argc, char *argv[])
18 {
19         struct map *map;
20         struct idx *idx[NUM+1], *index;
21         unsigned int i;
22         int *intp;
23
24         plan_tests(25 + NUM*2 + 6);
25         for (i = 0; i < NUM+1; i++)
26                 idx[i] = malloc(20);
27
28         qsort(idx, NUM, sizeof(idx[0]), cmp_ptr);
29
30         map = jmap_new(struct map);
31         ok1(jmap_error(map) == NULL);
32
33         ok1(jmap_test(map, idx[NUM]) == false);
34         ok1(jmap_get(map, idx[NUM]) == 0);
35         ok1(jmap_count(map) == 0);
36         ok1(jmap_first(map) == 0);
37         ok1(jmap_del(map, idx[0]) == false);
38
39         /* Set only works on existing cases. */
40         ok1(jmap_set(map, idx[0], 0) == false);
41         ok1(jmap_add(map, idx[0], 1) == true);
42         ok1(jmap_get(map, idx[0]) == 1);
43         ok1(jmap_set(map, idx[0], -1) == true);
44         ok1(jmap_get(map, idx[0]) == -1);
45
46         ok1(jmap_test(map, idx[0]) == true);
47         ok1(jmap_count(map) == 1);
48         ok1(jmap_first(map) == idx[0]);
49         ok1(jmap_next(map, idx[0]) == NULL);
50
51         ok1(jmap_del(map, idx[0]) == true);
52         ok1(jmap_test(map, idx[0]) == false);
53         ok1(jmap_count(map) == 0);
54
55         for (i = 0; i < NUM; i++)
56                 jmap_add(map, idx[i], i+1);
57
58         ok1(jmap_count(map) == NUM);
59
60         ok1(jmap_first(map) == idx[0]);
61         ok1(jmap_next(map, idx[0]) == idx[1]);
62         ok1(jmap_next(map, idx[NUM-1]) == NULL);
63
64         ok1(jmap_get(map, idx[0]) == 1);
65         ok1(jmap_get(map, idx[NUM-1]) == NUM);
66         ok1(jmap_get(map, (void *)((char *)idx[NUM-1] + 1)) == 0);
67
68         /* Reverse values in map. */
69         for (i = 0; i < NUM; i++) {
70                 intp = jmap_getval(map, idx[i]);
71                 ok1(*intp == i+1);
72                 *intp = NUM-i;
73                 jmap_putval(map, &intp);
74         }
75         for (i = 0; i < NUM; i++)
76                 ok1(jmap_get(map, idx[i]) == NUM-i);
77
78         intp = jmap_firstval(map, &index);
79         ok1(index == idx[0]);
80         ok1(*intp == NUM);
81         jmap_putval(map, &intp);
82
83         intp = jmap_nextval(map, &index);
84         ok1(index == idx[1]);
85         ok1(*intp == NUM-1);
86         jmap_putval(map, &intp);
87
88         index = idx[NUM-1];
89         intp = jmap_nextval(map, &index);
90         ok1(intp == NULL);
91
92         ok1(jmap_error(map) == NULL);
93         jmap_free(map);
94
95         for (i = 0; i < NUM+1; i++)
96                 free(idx[i]);
97
98         return exit_status();
99 }