d34c660e6582b96b3eedd9443684c22dd012ef0b
[ccan] / ccan / list / test / run-with-debug.c
1 #define CCAN_LIST_DEBUG 1
2 #include <ccan/list/list.h>
3 #include <ccan/tap/tap.h>
4 #include <ccan/list/list.c>
5
6 struct parent {
7         const char *name;
8         struct list_head children;
9         unsigned int num_children;
10 };
11
12 struct child {
13         const char *name;
14         struct list_node list;
15 };
16
17 static LIST_HEAD(static_list);
18
19 int main(int argc, char *argv[])
20 {
21         struct parent parent;
22         struct child c1, c2, c3, *c, *n;
23         unsigned int i;
24
25         plan_tests(47);
26         /* Test LIST_HEAD, list_empty and check_list */
27         ok1(list_empty(&static_list));
28         ok1(list_check(&static_list, NULL));
29
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));
35
36         c2.name = "c2";
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);
44         /* Test list_check */
45         ok1(list_check(&parent.children, NULL));
46
47         c1.name = "c1";
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);
57         /* Test list_check */
58         ok1(list_check(&parent.children, NULL));
59
60         c3.name = "c3";
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);
72         /* Test list_check */
73         ok1(list_check(&parent.children, NULL));
74
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));
79
80         /* Test list_top */
81         ok1(list_top(&parent.children, struct child, list) == &c1);
82
83         /* Test list_tail */
84         ok1(list_tail(&parent.children, struct child, list) == &c3);
85
86         /* Test list_for_each. */
87         i = 0;
88         list_for_each(&parent.children, c, list) {
89                 switch (i++) {
90                 case 0:
91                         ok1(c == &c1);
92                         break;
93                 case 1:
94                         ok1(c == &c2);
95                         break;
96                 case 2:
97                         ok1(c == &c3);
98                         break;
99                 }
100                 if (i > 2)
101                         break;
102         }
103         ok1(i == 3);
104
105         /* Test list_for_each_safe and list_del. */
106         i = 0;
107         list_for_each_safe(&parent.children, c, n, list) {
108                 switch (i++) {
109                 case 0:
110                         ok1(c == &c1);
111                         break;
112                 case 1:
113                         ok1(c == &c2);
114                         break;
115                 case 2:
116                         ok1(c == &c3);
117                         break;
118                 }
119                 list_del(&c->list);
120                 ok1(list_check(&parent.children, NULL));
121                 if (i > 2)
122                         break;
123         }
124         ok1(i == 3);
125         ok1(list_empty(&parent.children));
126
127         /* Test list_top/list_tail on empty list. */
128         ok1(list_top(&parent.children, struct child, list) == NULL);
129         ok1(list_tail(&parent.children, struct child, list) == NULL);
130         return exit_status();
131 }