1 #define CCAN_LIST_DEBUG 1
2 #include <ccan/tlist/tlist.h>
3 #include <ccan/tap/tap.h>
5 TLIST_TYPE(children, struct child);
9 struct tlist_children children;
10 unsigned int num_children;
15 struct list_node list;
18 int main(int argc, char *argv[])
21 struct child c1, c2, c3, *c, *n;
23 struct tlist_children tlist = TLIST_INIT(tlist);
26 /* Test TLIST_INIT, and tlist_empty */
27 ok1(tlist_empty(&tlist));
28 ok1(tlist_check(&tlist, NULL));
30 parent.num_children = 0;
31 tlist_init(&parent.children);
33 ok1(tlist_empty(&parent.children));
34 ok1(tlist_check(&parent.children, NULL));
37 tlist_add(&parent.children, &c2, list);
38 /* Test tlist_add and !tlist_empty. */
39 ok1(!tlist_empty(&parent.children));
40 ok1(c2.list.next == &parent.children.raw.n);
41 ok1(c2.list.prev == &parent.children.raw.n);
42 ok1(parent.children.raw.n.next == &c2.list);
43 ok1(parent.children.raw.n.prev == &c2.list);
44 /* Test tlist_check */
45 ok1(tlist_check(&parent.children, NULL));
48 tlist_add(&parent.children, &c1, list);
49 /* Test list_add and !list_empty. */
50 ok1(!tlist_empty(&parent.children));
51 ok1(c2.list.next == &parent.children.raw.n);
52 ok1(c2.list.prev == &c1.list);
53 ok1(parent.children.raw.n.next == &c1.list);
54 ok1(parent.children.raw.n.prev == &c2.list);
55 ok1(c1.list.next == &c2.list);
56 ok1(c1.list.prev == &parent.children.raw.n);
57 /* Test tlist_check */
58 ok1(tlist_check(&parent.children, NULL));
61 tlist_add_tail(&parent.children, &c3, list);
62 /* Test list_add_tail and !list_empty. */
63 ok1(!tlist_empty(&parent.children));
64 ok1(parent.children.raw.n.next == &c1.list);
65 ok1(parent.children.raw.n.prev == &c3.list);
66 ok1(c1.list.next == &c2.list);
67 ok1(c1.list.prev == &parent.children.raw.n);
68 ok1(c2.list.next == &c3.list);
69 ok1(c2.list.prev == &c1.list);
70 ok1(c3.list.next == &parent.children.raw.n);
71 ok1(c3.list.prev == &c2.list);
72 /* Test tlist_check */
73 ok1(tlist_check(&parent.children, NULL));
76 ok1(tlist_top(&parent.children, list) == &c1);
79 ok1(tlist_tail(&parent.children, list) == &c3);
81 /* Test tlist_for_each. */
83 tlist_for_each(&parent.children, c, list) {
100 /* Test tlist_for_each_rev. */
102 tlist_for_each_rev(&parent.children, c, list) {
119 /* Test tlist_for_each_safe, tlist_del and tlist_del_from. */
121 tlist_for_each_safe(&parent.children, c, n, list) {
129 tlist_del_from(&parent.children, c, list);
133 tlist_del_from(&parent.children, c, list);
136 ok1(tlist_check(&parent.children, NULL));
141 ok1(tlist_empty(&parent.children));
143 /* Test list_top/list_tail on empty list. */
144 ok1(tlist_top(&parent.children, list) == (struct child *)NULL);
145 ok1(tlist_tail(&parent.children, list) == (struct child *)NULL);
146 return exit_status();