list: add list_next and list_prev helpers.
[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(int argc, char *argv[])
18 {
19         struct parent parent;
20         struct child c1, c2, c3;
21
22         plan_tests(12);
23         parent.num_children = 0;
24         list_head_init(&parent.children);
25
26         c1.name = "c1";
27         list_add(&parent.children, &c1.list);
28
29         ok1(list_next(&parent.children, &c1, list) == NULL);
30         ok1(list_prev(&parent.children, &c1, list) == NULL);
31
32         c2.name = "c2";
33         list_add_tail(&parent.children, &c2.list);
34
35         ok1(list_next(&parent.children, &c1, list) == &c2);
36         ok1(list_prev(&parent.children, &c1, list) == NULL);
37         ok1(list_next(&parent.children, &c2, list) == NULL);
38         ok1(list_prev(&parent.children, &c2, list) == &c1);
39
40         c3.name = "c3";
41         list_add_tail(&parent.children, &c3.list);
42
43         ok1(list_next(&parent.children, &c1, list) == &c2);
44         ok1(list_prev(&parent.children, &c1, list) == NULL);
45         ok1(list_next(&parent.children, &c2, list) == &c3);
46         ok1(list_prev(&parent.children, &c2, list) == &c1);
47         ok1(list_next(&parent.children, &c3, list) == NULL);
48         ok1(list_prev(&parent.children, &c3, list) == &c2);
49         return exit_status();
50 }