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);
22 struct child c1, c2, c3, x1, *c, *n;
24 struct list_head list = LIST_HEAD_INIT(list);
26 struct list_head opaque_list = LIST_HEAD_INIT(opaque_list);
30 /* Test LIST_HEAD, LIST_HEAD_INIT, list_empty and check_list */
31 ok1(list_empty(&static_list));
32 ok1(list_check(&static_list, NULL));
33 ok1(list_empty(&list));
34 ok1(list_check(&list, NULL));
36 parent.num_children = 0;
37 list_head_init(&parent.children);
38 /* Test list_head_init */
39 ok1(list_empty(&parent.children));
40 ok1(list_check(&parent.children, NULL));
43 list_add(&parent.children, &c2.list);
44 /* Test list_add and !list_empty. */
45 ok1(!list_empty(&parent.children));
46 ok1(c2.list.next == &parent.children.n);
47 ok1(c2.list.prev == &parent.children.n);
48 ok1(parent.children.n.next == &c2.list);
49 ok1(parent.children.n.prev == &c2.list);
51 ok1(list_check(&parent.children, NULL));
54 list_add(&parent.children, &c1.list);
55 /* Test list_add and !list_empty. */
56 ok1(!list_empty(&parent.children));
57 ok1(c2.list.next == &parent.children.n);
58 ok1(c2.list.prev == &c1.list);
59 ok1(parent.children.n.next == &c1.list);
60 ok1(parent.children.n.prev == &c2.list);
61 ok1(c1.list.next == &c2.list);
62 ok1(c1.list.prev == &parent.children.n);
64 ok1(list_check(&parent.children, NULL));
67 list_add_tail(&parent.children, &c3.list);
68 /* Test list_add_tail and !list_empty. */
69 ok1(!list_empty(&parent.children));
70 ok1(parent.children.n.next == &c1.list);
71 ok1(parent.children.n.prev == &c3.list);
72 ok1(c1.list.next == &c2.list);
73 ok1(c1.list.prev == &parent.children.n);
74 ok1(c2.list.next == &c3.list);
75 ok1(c2.list.prev == &c1.list);
76 ok1(c3.list.next == &parent.children.n);
77 ok1(c3.list.prev == &c2.list);
79 ok1(list_check(&parent.children, NULL));
81 /* Test list_check_node */
82 ok1(list_check_node(&c1.list, NULL));
83 ok1(list_check_node(&c2.list, NULL));
84 ok1(list_check_node(&c3.list, NULL));
87 ok1(list_top(&parent.children, struct child, list) == &c1);
90 ok1(list_pop(&parent.children, struct child, list) == &c1);
91 ok1(list_top(&parent.children, struct child, list) == &c2);
92 list_add(&parent.children, &c1.list);
95 ok1(list_tail(&parent.children, struct child, list) == &c3);
97 /* Test list_for_each. */
99 list_for_each(&parent.children, c, list) {
116 /* Test list_for_each_rev. */
118 list_for_each_rev(&parent.children, c, list) {
135 /* Test list_for_each_safe, list_del and list_del_from. */
137 list_for_each_safe(&parent.children, c, n, list) {
145 list_del_from(&parent.children, &c->list);
149 list_del_from(&parent.children, &c->list);
153 /* prepare for list_for_each_rev_safe test */
154 list_add(&rev, &c->list);
156 ok1(list_check(&parent.children, NULL));
161 ok1(list_empty(&parent.children));
163 /* Test list_for_each_rev_safe, list_del and list_del_from. */
165 list_for_each_rev_safe(&rev, c, n, list) {
173 list_del_from(&rev, &c->list);
177 list_del_from(&rev, &c->list);
180 ok1(list_check(&rev, NULL));
185 ok1(list_empty(&rev));
187 /* Test list_node_init: safe to list_del after this. */
188 list_node_init(&c->list);
191 /* Test list_del_init */
192 list_add(&parent.children, &c->list);
193 ok1(!list_empty(&parent.children));
194 list_del_init(&c->list);
195 ok1(list_empty(&parent.children));
196 /* We can call this as many times as we like. */
197 list_del_init(&c->list);
198 list_del_init(&c->list);
200 /* Test list_for_each_off. */
201 list_add_tail(&opaque_list,
202 (struct list_node *)create_opaque_blob());
203 list_add_tail(&opaque_list,
204 (struct list_node *)create_opaque_blob());
205 list_add_tail(&opaque_list,
206 (struct list_node *)create_opaque_blob());
210 list_for_each_off(&opaque_list, q, 0) {
212 ok1(if_blobs_know_the_secret(q));
216 /* Test list_for_each_safe_off, list_del_off and list_del_from_off. */
218 list_for_each_safe_off(&opaque_list, q, nq, 0) {
221 ok1(if_blobs_know_the_secret(q));
223 destroy_opaque_blob(q);
226 ok1(if_blobs_know_the_secret(q));
227 list_del_from_off(&opaque_list, q, 0);
228 destroy_opaque_blob(q);
232 list_del_from_off(&opaque_list, q, 0);
233 destroy_opaque_blob(q);
236 ok1(list_check(&opaque_list, NULL));
241 ok1(list_empty(&opaque_list));
243 /* Test list_top/list_tail/list_pop on empty list. */
244 ok1(list_top(&parent.children, struct child, list) == NULL);
245 ok1(list_tail(&parent.children, struct child, list) == NULL);
246 ok1(list_pop(&parent.children, struct child, list) == NULL);
248 /* Test list_add_before and list_add_after */
249 list_add(&parent.children, &c1.list);
250 list_add_after(&parent.children, &c1.list, &c2.list);
251 ok1(list_check(&parent.children, "list_add_after"));
254 list_for_each(&parent.children, c, list) {
266 list_add_before(&parent.children, &c2.list, &c3.list);
267 ok1(list_check(&parent.children, "list_add_before"));
270 list_for_each(&parent.children, c, list) {
286 list_swap(&c3.list, &x1.list);
287 ok1(list_check(&parent.children, "list_swap"));
289 list_for_each(&parent.children, c, list) {
304 return exit_status();