1 #include <ccan/list/list.h>
2 #include <ccan/tap/tap.h>
3 #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;
24 struct list_head list = LIST_HEAD_INIT(list);
26 struct list_head opaque_list = LIST_HEAD_INIT(opaque_list);
29 /* Test LIST_HEAD, LIST_HEAD_INIT, list_empty and check_list */
30 ok1(list_empty(&static_list));
31 ok1(list_check(&static_list, NULL));
32 ok1(list_empty(&list));
33 ok1(list_check(&list, NULL));
35 parent.num_children = 0;
36 list_head_init(&parent.children);
37 /* Test list_head_init */
38 ok1(list_empty(&parent.children));
39 ok1(list_check(&parent.children, NULL));
42 list_add(&parent.children, &c2.list);
43 /* Test list_add and !list_empty. */
44 ok1(!list_empty(&parent.children));
45 ok1(c2.list.next == &parent.children.n);
46 ok1(c2.list.prev == &parent.children.n);
47 ok1(parent.children.n.next == &c2.list);
48 ok1(parent.children.n.prev == &c2.list);
50 ok1(list_check(&parent.children, NULL));
53 list_add(&parent.children, &c1.list);
54 /* Test list_add and !list_empty. */
55 ok1(!list_empty(&parent.children));
56 ok1(c2.list.next == &parent.children.n);
57 ok1(c2.list.prev == &c1.list);
58 ok1(parent.children.n.next == &c1.list);
59 ok1(parent.children.n.prev == &c2.list);
60 ok1(c1.list.next == &c2.list);
61 ok1(c1.list.prev == &parent.children.n);
63 ok1(list_check(&parent.children, NULL));
66 list_add_tail(&parent.children, &c3.list);
67 /* Test list_add_tail and !list_empty. */
68 ok1(!list_empty(&parent.children));
69 ok1(parent.children.n.next == &c1.list);
70 ok1(parent.children.n.prev == &c3.list);
71 ok1(c1.list.next == &c2.list);
72 ok1(c1.list.prev == &parent.children.n);
73 ok1(c2.list.next == &c3.list);
74 ok1(c2.list.prev == &c1.list);
75 ok1(c3.list.next == &parent.children.n);
76 ok1(c3.list.prev == &c2.list);
78 ok1(list_check(&parent.children, NULL));
80 /* Test list_check_node */
81 ok1(list_check_node(&c1.list, NULL));
82 ok1(list_check_node(&c2.list, NULL));
83 ok1(list_check_node(&c3.list, NULL));
86 ok1(list_top(&parent.children, struct child, list) == &c1);
89 ok1(list_pop(&parent.children, struct child, list) == &c1);
90 ok1(list_top(&parent.children, struct child, list) == &c2);
91 list_add(&parent.children, &c1.list);
94 ok1(list_tail(&parent.children, struct child, list) == &c3);
96 /* Test list_for_each. */
98 list_for_each(&parent.children, c, list) {
115 /* Test list_for_each_rev. */
117 list_for_each_rev(&parent.children, c, list) {
134 /* Test list_for_each_safe, list_del and list_del_from. */
136 list_for_each_safe(&parent.children, c, n, list) {
144 list_del_from(&parent.children, &c->list);
148 list_del_from(&parent.children, &c->list);
151 ok1(list_check(&parent.children, NULL));
156 ok1(list_empty(&parent.children));
158 /* Test list_for_each_off. */
159 list_add_tail(&opaque_list,
160 (struct list_node *)create_opaque_blob());
161 list_add_tail(&opaque_list,
162 (struct list_node *)create_opaque_blob());
163 list_add_tail(&opaque_list,
164 (struct list_node *)create_opaque_blob());
168 list_for_each_off(&opaque_list, q, 0) {
170 ok1(if_blobs_know_the_secret(q));
174 /* Test list_for_each_safe_off, list_del_off and list_del_from_off. */
176 list_for_each_safe_off(&opaque_list, q, nq, 0) {
179 ok1(if_blobs_know_the_secret(q));
181 destroy_opaque_blob(q);
184 ok1(if_blobs_know_the_secret(q));
185 list_del_from_off(&opaque_list, q, 0);
186 destroy_opaque_blob(q);
190 list_del_from_off(&opaque_list, q, 0);
191 destroy_opaque_blob(q);
194 ok1(list_check(&opaque_list, NULL));
199 ok1(list_empty(&opaque_list));
201 /* Test list_top/list_tail/list_pop on empty list. */
202 ok1(list_top(&parent.children, struct child, list) == NULL);
203 ok1(list_tail(&parent.children, struct child, list) == NULL);
204 ok1(list_pop(&parent.children, struct child, list) == NULL);
205 return exit_status();