1 #define CCAN_LIST_DEBUG 1
2 #include <ccan/list/list.h>
3 #include <ccan/tap/tap.h>
4 #include <ccan/list/list.c>
8 struct list_head children;
9 unsigned int num_children;
14 struct list_node list;
17 static LIST_HEAD(static_list);
19 int main(int argc, char *argv[])
22 struct child c1, c2, c3, *c, *n;
26 /* Test LIST_HEAD, list_empty and check_list */
27 ok1(list_empty(&static_list));
28 ok1(list_check(&static_list, NULL));
30 parent.num_children = 0;
31 list_head_init(&parent.children);
32 /* Test list_head_init */
33 ok1(list_empty(&parent.children));
34 ok1(list_check(&parent.children, NULL));
37 list_add(&parent.children, &c2.list);
38 /* Test list_add and !list_empty. */
39 ok1(!list_empty(&parent.children));
40 ok1(c2.list.next == &parent.children.n);
41 ok1(c2.list.prev == &parent.children.n);
42 ok1(parent.children.n.next == &c2.list);
43 ok1(parent.children.n.prev == &c2.list);
45 ok1(list_check(&parent.children, NULL));
48 list_add(&parent.children, &c1.list);
49 /* Test list_add and !list_empty. */
50 ok1(!list_empty(&parent.children));
51 ok1(c2.list.next == &parent.children.n);
52 ok1(c2.list.prev == &c1.list);
53 ok1(parent.children.n.next == &c1.list);
54 ok1(parent.children.n.prev == &c2.list);
55 ok1(c1.list.next == &c2.list);
56 ok1(c1.list.prev == &parent.children.n);
58 ok1(list_check(&parent.children, NULL));
61 list_add_tail(&parent.children, &c3.list);
62 /* Test list_add_tail and !list_empty. */
63 ok1(!list_empty(&parent.children));
64 ok1(parent.children.n.next == &c1.list);
65 ok1(parent.children.n.prev == &c3.list);
66 ok1(c1.list.next == &c2.list);
67 ok1(c1.list.prev == &parent.children.n);
68 ok1(c2.list.next == &c3.list);
69 ok1(c2.list.prev == &c1.list);
70 ok1(c3.list.next == &parent.children.n);
71 ok1(c3.list.prev == &c2.list);
73 ok1(list_check(&parent.children, NULL));
75 /* Test list_check_node */
76 ok1(list_check_node(&c1.list, NULL));
77 ok1(list_check_node(&c2.list, NULL));
78 ok1(list_check_node(&c3.list, NULL));
81 ok1(list_top(&parent.children, struct child, list) == &c1);
84 ok1(list_tail(&parent.children, struct child, list) == &c3);
86 /* Test list_for_each. */
88 list_for_each(&parent.children, c, list) {
105 /* Test list_for_each_safe, list_del and list_del_from. */
107 list_for_each_safe(&parent.children, c, n, list) {
115 list_del_from(&parent.children, &c->list);
119 list_del_from(&parent.children, &c->list);
122 ok1(list_check(&parent.children, NULL));
127 ok1(list_empty(&parent.children));
129 /* Test list_top/list_tail on empty list. */
130 ok1(list_top(&parent.children, struct child, list) == NULL);
131 ok1(list_tail(&parent.children, struct child, list) == NULL);
132 return exit_status();