13a1da4198134d88cbeb56020c1ee9acd7cce24a
[ccan] / ccan / list / test / run.c
1 #include <ccan/list/list.h>
2 #include <ccan/tap/tap.h>
3 #include <ccan/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(47);
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_check_node */
75         ok1(list_check_node(&c1.list, NULL));
76         ok1(list_check_node(&c2.list, NULL));
77         ok1(list_check_node(&c3.list, NULL));
78
79         /* Test list_top */
80         ok1(list_top(&parent.children, struct child, list) == &c1);
81
82         /* Test list_tail */
83         ok1(list_tail(&parent.children, struct child, list) == &c3);
84
85         /* Test list_for_each. */
86         i = 0;
87         list_for_each(&parent.children, c, list) {
88                 switch (i++) {
89                 case 0:
90                         ok1(c == &c1);
91                         break;
92                 case 1:
93                         ok1(c == &c2);
94                         break;
95                 case 2:
96                         ok1(c == &c3);
97                         break;
98                 }
99                 if (i > 2)
100                         break;
101         }
102         ok1(i == 3);
103
104         /* Test list_for_each_safe, list_del and list_del_from. */
105         i = 0;
106         list_for_each_safe(&parent.children, c, n, list) {
107                 switch (i++) {
108                 case 0:
109                         ok1(c == &c1);  
110                         list_del(&c->list);
111                         break;
112                 case 1:
113                         ok1(c == &c2);
114                         list_del_from(&parent.children, &c->list);
115                         break;
116                 case 2:
117                         ok1(c == &c3);
118                         list_del_from(&parent.children, &c->list);
119                         break;
120                 }
121                 ok1(list_check(&parent.children, NULL));
122                 if (i > 2)
123                         break;
124         }
125         ok1(i == 3);
126         ok1(list_empty(&parent.children));
127
128         /* Test list_top/list_tail on empty list. */
129         ok1(list_top(&parent.children, struct child, list) == NULL);
130         ok1(list_tail(&parent.children, struct child, list) == NULL);
131         return exit_status();
132 }