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 unsigned int num_children;
10 struct tlist_children 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 == &tcon_unwrap(&parent.children)->n);
41 ok1(c2.list.prev == &tcon_unwrap(&parent.children)->n);
42 ok1(tcon_unwrap(&parent.children)->n.next == &c2.list);
43 ok1(tcon_unwrap(&parent.children)->n.prev == &c2.list);
44 ok1(tlist_next(&parent.children, &c2, list) == NULL);
45 ok1(tlist_prev(&parent.children, &c2, list) == NULL);
46 /* Test tlist_check */
47 ok1(tlist_check(&parent.children, NULL));
50 tlist_add(&parent.children, &c1, list);
51 /* Test list_add and !list_empty. */
52 ok1(!tlist_empty(&parent.children));
53 ok1(c2.list.next == &tcon_unwrap(&parent.children)->n);
54 ok1(c2.list.prev == &c1.list);
55 ok1(tcon_unwrap(&parent.children)->n.next == &c1.list);
56 ok1(tcon_unwrap(&parent.children)->n.prev == &c2.list);
57 ok1(c1.list.next == &c2.list);
58 ok1(c1.list.prev == &tcon_unwrap(&parent.children)->n);
59 ok1(tlist_next(&parent.children, &c1, list) == &c2);
60 ok1(tlist_next(&parent.children, &c2, list) == NULL);
61 ok1(tlist_prev(&parent.children, &c2, list) == &c1);
62 ok1(tlist_prev(&parent.children, &c1, list) == NULL);
63 /* Test tlist_check */
64 ok1(tlist_check(&parent.children, NULL));
67 tlist_add_tail(&parent.children, &c3, list);
68 /* Test list_add_tail and !list_empty. */
69 ok1(!tlist_empty(&parent.children));
70 ok1(tcon_unwrap(&parent.children)->n.next == &c1.list);
71 ok1(tcon_unwrap(&parent.children)->n.prev == &c3.list);
72 ok1(c1.list.next == &c2.list);
73 ok1(c1.list.prev == &tcon_unwrap(&parent.children)->n);
74 ok1(c2.list.next == &c3.list);
75 ok1(c2.list.prev == &c1.list);
76 ok1(c3.list.next == &tcon_unwrap(&parent.children)->n);
77 ok1(c3.list.prev == &c2.list);
78 ok1(tlist_next(&parent.children, &c1, list) == &c2);
79 ok1(tlist_next(&parent.children, &c2, list) == &c3);
80 ok1(tlist_next(&parent.children, &c3, list) == NULL);
81 ok1(tlist_prev(&parent.children, &c3, list) == &c2);
82 ok1(tlist_prev(&parent.children, &c2, list) == &c1);
83 ok1(tlist_prev(&parent.children, &c1, list) == NULL);
84 /* Test tlist_check */
85 ok1(tlist_check(&parent.children, NULL));
88 ok1(tlist_top(&parent.children, list) == &c1);
91 ok1(tlist_tail(&parent.children, list) == &c3);
93 /* Test tlist_for_each. */
95 tlist_for_each(&parent.children, c, list) {
112 /* Test tlist_for_each_rev. */
114 tlist_for_each_rev(&parent.children, c, list) {
131 /* Test tlist_for_each_safe, tlist_del and tlist_del_from. */
133 tlist_for_each_safe(&parent.children, c, n, list) {
141 tlist_del_from(&parent.children, c, list);
145 tlist_del_from(&parent.children, c, list);
148 ok1(tlist_check(&parent.children, NULL));
153 ok1(tlist_empty(&parent.children));
155 /* Test list_top/list_tail on empty list. */
156 ok1(tlist_top(&parent.children, list) == (struct child *)NULL);
157 ok1(tlist_tail(&parent.children, list) == (struct child *)NULL);
158 return exit_status();