66b9630db514610ac9d78c76212492e39b6465ca
[ccan] / list / test / run.c
1 #include "list/list.h"
2 #include "tap/tap.h"
3 #include "list/list.c"
4
5 struct parent {
6         const char *name;
7         struct list_head children;
8         unsigned int num_children;
9 };
10
11 struct child {
12         const char *name;
13         struct list_node list;
14 };
15
16 static LIST_HEAD(static_list);
17
18 int main(int argc, char *argv[])
19 {
20         struct parent parent;
21         struct child c1, c2, c3, *c, *n;
22         unsigned int i;
23
24         plan_tests(41);
25         /* Test LIST_HEAD, list_empty and check_list */
26         ok1(list_empty(&static_list));
27         ok1(list_check(&static_list, NULL));
28
29         parent.num_children = 0;
30         list_head_init(&parent.children);
31         /* Test list_head_init */
32         ok1(list_empty(&parent.children));
33         ok1(list_check(&parent.children, NULL));
34
35         c2.name = "c2";
36         list_add(&parent.children, &c2.list);
37         /* Test list_add and !list_empty. */
38         ok1(!list_empty(&parent.children));
39         ok1(c2.list.next == &parent.children.n);
40         ok1(c2.list.prev == &parent.children.n);
41         ok1(parent.children.n.next == &c2.list);
42         ok1(parent.children.n.prev == &c2.list);
43         /* Test list_check */
44         ok1(list_check(&parent.children, NULL));
45
46         c1.name = "c1";
47         list_add(&parent.children, &c1.list);
48         /* Test list_add and !list_empty. */
49         ok1(!list_empty(&parent.children));
50         ok1(c2.list.next == &parent.children.n);
51         ok1(c2.list.prev == &c1.list);
52         ok1(parent.children.n.next == &c1.list);
53         ok1(parent.children.n.prev == &c2.list);
54         ok1(c1.list.next == &c2.list);
55         ok1(c1.list.prev == &parent.children.n);
56         /* Test list_check */
57         ok1(list_check(&parent.children, NULL));
58
59         c3.name = "c3";
60         list_add_tail(&parent.children, &c3.list);
61         /* Test list_add_tail and !list_empty. */
62         ok1(!list_empty(&parent.children));
63         ok1(parent.children.n.next == &c1.list);
64         ok1(parent.children.n.prev == &c3.list);
65         ok1(c1.list.next == &c2.list);
66         ok1(c1.list.prev == &parent.children.n);
67         ok1(c2.list.next == &c3.list);
68         ok1(c2.list.prev == &c1.list);
69         ok1(c3.list.next == &parent.children.n);
70         ok1(c3.list.prev == &c2.list);
71         /* Test list_check */
72         ok1(list_check(&parent.children, NULL));
73
74         /* Test list_top */
75         ok1(list_top(&parent.children, struct child, list) == &c1);
76
77         /* Test list_for_each. */
78         i = 0;
79         list_for_each(&parent.children, c, list) {
80                 switch (i++) {
81                 case 0:
82                         ok1(c == &c1);
83                         break;
84                 case 1:
85                         ok1(c == &c2);
86                         break;
87                 case 2:
88                         ok1(c == &c3);
89                         break;
90                 }
91                 if (i > 2)
92                         break;
93         }
94         ok1(i == 3);
95
96         /* Test list_for_each_safe and list_del. */
97         i = 0;
98         list_for_each_safe(&parent.children, c, n, list) {
99                 switch (i++) {
100                 case 0:
101                         ok1(c == &c1);
102                         break;
103                 case 1:
104                         ok1(c == &c2);
105                         break;
106                 case 2:
107                         ok1(c == &c3);
108                         break;
109                 }
110                 list_del(&c->list);
111                 ok1(list_check(&parent.children, NULL));
112                 if (i > 2)
113                         break;
114         }
115         ok1(i == 3);
116         ok1(list_empty(&parent.children));
117         return exit_status();
118 }