]> git.ozlabs.org Git - ccan/blob - ccan/jset/test/run-pointer-type.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / jset / test / run-pointer-type.c
1 #include <ccan/tap/tap.h>
2 #include <ccan/jset/jset.c>
3
4 struct foo;
5
6 struct jset_foo {
7         JSET_MEMBERS(struct foo *);
8 };
9
10 static int cmp_ptr(const void *a, const void *b)
11 {
12         return *(char **)a - *(char **)b;
13 }
14
15 #define NUM 100
16
17 int main(int argc, char *argv[])
18 {
19         struct jset_foo *set;
20         struct foo *foo[NUM];
21         unsigned int i;
22
23         plan_tests(20);
24         for (i = 0; i < NUM; i++)
25                 foo[i] = malloc(20);
26
27         set = jset_new(struct jset_foo);
28         ok1(jset_error(set) == NULL);
29
30         ok1(jset_set(set, foo[0]) == true);
31         ok1(jset_set(set, foo[0]) == false);
32         ok1(jset_clear(set, foo[0]) == true);
33         ok1(jset_clear(set, foo[0]) == false);
34         ok1(jset_count(set) == 0);
35         ok1(jset_nth(set, 0, NULL) == (struct foo *)NULL);
36         ok1(jset_first(set) == (struct foo *)NULL);
37         ok1(jset_last(set) == (struct foo *)NULL);
38
39         for (i = 0; i < NUM; i++)
40                 jset_set(set, foo[i]);
41
42         qsort(foo, NUM, sizeof(foo[0]), cmp_ptr);
43
44         ok1(jset_count(set) == NUM);
45         ok1(jset_nth(set, 0, NULL) == foo[0]);
46         ok1(jset_nth(set, NUM-1, NULL) == foo[NUM-1]);
47         ok1(jset_nth(set, NUM, NULL) == (struct foo *)NULL);
48         ok1(jset_first(set) == foo[0]);
49         ok1(jset_last(set) == foo[NUM-1]);
50         ok1(jset_next(set, foo[0]) == foo[1]);
51         ok1(jset_next(set, foo[NUM-1]) == (struct foo *)NULL);
52         ok1(jset_prev(set, foo[1]) == foo[0]);
53         ok1(jset_prev(set, foo[0]) == (struct foo *)NULL);
54         ok1(jset_error(set) == NULL);
55         jset_free(set);
56
57         for (i = 0; i < NUM; i++)
58                 free(foo[i]);
59
60         return exit_status();
61 }