1 #include <ccan/tap/tap.h>
2 #include <ccan/jmap/jmap.c>
7 JMAP_MEMBERS(struct idx *, int);
12 static int cmp_ptr(const void *a, const void *b)
14 return *(char **)a - *(char **)b;
17 int main(int argc, char *argv[])
20 struct idx *idx[NUM+1], *index;
24 plan_tests(25 + NUM*2 + 6);
25 for (i = 0; i < NUM+1; i++)
28 qsort(idx, NUM, sizeof(idx[0]), cmp_ptr);
30 map = jmap_new(struct map);
31 ok1(jmap_error(map) == NULL);
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);
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);
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);
51 ok1(jmap_del(map, idx[0]) == true);
52 ok1(jmap_test(map, idx[0]) == false);
53 ok1(jmap_count(map) == 0);
55 for (i = 0; i < NUM; i++)
56 jmap_add(map, idx[i], i+1);
58 ok1(jmap_count(map) == NUM);
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);
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);
68 /* Reverse values in map. */
69 for (i = 0; i < NUM; i++) {
70 intp = jmap_getval(map, idx[i]);
73 jmap_putval(map, &intp);
75 for (i = 0; i < NUM; i++)
76 ok1(jmap_get(map, idx[i]) == NUM-i);
78 intp = jmap_firstval(map, &index);
81 jmap_putval(map, &intp);
83 intp = jmap_nextval(map, &index);
86 jmap_putval(map, &intp);
89 intp = jmap_nextval(map, &index);
92 ok1(jmap_error(map) == NULL);
95 for (i = 0; i < NUM+1; i++)