compiler: RETURNS_NONNULL macro
[ccan] / ccan / list / test / run-list_prev-list_next.c
1 #include <ccan/list/list.h>
2 #include <ccan/tap/tap.h>
3 #include <ccan/list/list.c>
4 #include "helper.h"
5
6 struct parent {
7         const char *name;
8         unsigned int num_children;
9         struct list_head children;
10 };
11
12 struct child {
13         const char *name;
14         struct list_node list;
15 };
16
17 int main(void)
18 {
19         struct parent parent;
20         struct child c1, c2, c3;
21         const struct parent *p;
22         const struct child *c;
23
24         plan_tests(20);
25         parent.num_children = 0;
26         list_head_init(&parent.children);
27
28         c1.name = "c1";
29         list_add(&parent.children, &c1.list);
30
31         ok1(list_next(&parent.children, &c1, list) == NULL);
32         ok1(list_prev(&parent.children, &c1, list) == NULL);
33
34         c2.name = "c2";
35         list_add_tail(&parent.children, &c2.list);
36
37         ok1(list_next(&parent.children, &c1, list) == &c2);
38         ok1(list_prev(&parent.children, &c1, list) == NULL);
39         ok1(list_next(&parent.children, &c2, list) == NULL);
40         ok1(list_prev(&parent.children, &c2, list) == &c1);
41
42         c3.name = "c3";
43         list_add_tail(&parent.children, &c3.list);
44
45         ok1(list_next(&parent.children, &c1, list) == &c2);
46         ok1(list_prev(&parent.children, &c1, list) == NULL);
47         ok1(list_next(&parent.children, &c2, list) == &c3);
48         ok1(list_prev(&parent.children, &c2, list) == &c1);
49         ok1(list_next(&parent.children, &c3, list) == NULL);
50         ok1(list_prev(&parent.children, &c3, list) == &c2);
51
52         /* Const variants */
53         p = &parent;
54         c = &c2;
55         ok1(list_next(&p->children, &c1, list) == &c2);
56         ok1(list_prev(&p->children, &c1, list) == NULL);
57         ok1(list_next(&p->children, c, list) == &c3);
58         ok1(list_prev(&p->children, c, list) == &c1);
59         ok1(list_next(&parent.children, c, list) == &c3);
60         ok1(list_prev(&parent.children, c, list) == &c1);
61         ok1(list_next(&p->children, &c3, list) == NULL);
62         ok1(list_prev(&p->children, &c3, list) == &c2);
63
64         return exit_status();
65 }