1 #include <ccan/list/list.h>
2 #include <ccan/tap/tap.h>
3 #include <ccan/list/list.c>
7 struct list_head children;
8 unsigned int num_children;
13 struct list_node list;
16 static LIST_HEAD(static_list);
18 int main(int argc, char *argv[])
21 struct child c1, c2, c3, *c, *n;
23 struct list_head list = LIST_HEAD_INIT(list);
26 /* Test LIST_HEAD, LIST_HEAD_INIT, list_empty and check_list */
27 ok1(list_empty(&static_list));
28 ok1(list_check(&static_list, NULL));
29 ok1(list_empty(&list));
30 ok1(list_check(&list, NULL));
32 parent.num_children = 0;
33 list_head_init(&parent.children);
34 /* Test list_head_init */
35 ok1(list_empty(&parent.children));
36 ok1(list_check(&parent.children, NULL));
39 list_add(&parent.children, &c2.list);
40 /* Test list_add and !list_empty. */
41 ok1(!list_empty(&parent.children));
42 ok1(c2.list.next == &parent.children.n);
43 ok1(c2.list.prev == &parent.children.n);
44 ok1(parent.children.n.next == &c2.list);
45 ok1(parent.children.n.prev == &c2.list);
47 ok1(list_check(&parent.children, NULL));
50 list_add(&parent.children, &c1.list);
51 /* Test list_add and !list_empty. */
52 ok1(!list_empty(&parent.children));
53 ok1(c2.list.next == &parent.children.n);
54 ok1(c2.list.prev == &c1.list);
55 ok1(parent.children.n.next == &c1.list);
56 ok1(parent.children.n.prev == &c2.list);
57 ok1(c1.list.next == &c2.list);
58 ok1(c1.list.prev == &parent.children.n);
60 ok1(list_check(&parent.children, NULL));
63 list_add_tail(&parent.children, &c3.list);
64 /* Test list_add_tail and !list_empty. */
65 ok1(!list_empty(&parent.children));
66 ok1(parent.children.n.next == &c1.list);
67 ok1(parent.children.n.prev == &c3.list);
68 ok1(c1.list.next == &c2.list);
69 ok1(c1.list.prev == &parent.children.n);
70 ok1(c2.list.next == &c3.list);
71 ok1(c2.list.prev == &c1.list);
72 ok1(c3.list.next == &parent.children.n);
73 ok1(c3.list.prev == &c2.list);
75 ok1(list_check(&parent.children, NULL));
77 /* Test list_check_node */
78 ok1(list_check_node(&c1.list, NULL));
79 ok1(list_check_node(&c2.list, NULL));
80 ok1(list_check_node(&c3.list, NULL));
83 ok1(list_top(&parent.children, struct child, list) == &c1);
86 ok1(list_tail(&parent.children, struct child, list) == &c3);
88 /* Test list_for_each. */
90 list_for_each(&parent.children, c, list) {
107 /* Test list_for_each_safe, list_del and list_del_from. */
109 list_for_each_safe(&parent.children, c, n, list) {
117 list_del_from(&parent.children, &c->list);
121 list_del_from(&parent.children, &c->list);
124 ok1(list_check(&parent.children, NULL));
129 ok1(list_empty(&parent.children));
131 /* Test list_top/list_tail on empty list. */
132 ok1(list_top(&parent.children, struct child, list) == NULL);
133 ok1(list_tail(&parent.children, struct child, list) == NULL);
134 return exit_status();