306744598e2461a5bd6eb28de70593c7b009088e
[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 #include "helper.h"
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, x1, *c, *n;
23         unsigned int i;
24         struct list_head list = LIST_HEAD_INIT(list);
25         opaque_t *q, *nq;
26         struct list_head opaque_list = LIST_HEAD_INIT(opaque_list);
27
28         plan_tests(84);
29         /* Test LIST_HEAD, LIST_HEAD_INIT, list_empty and check_list */
30         ok1(list_empty(&static_list));
31         ok1(list_check(&static_list, NULL));
32         ok1(list_empty(&list));
33         ok1(list_check(&list, NULL));
34
35         parent.num_children = 0;
36         list_head_init(&parent.children);
37         /* Test list_head_init */
38         ok1(list_empty(&parent.children));
39         ok1(list_check(&parent.children, NULL));
40
41         c2.name = "c2";
42         list_add(&parent.children, &c2.list);
43         /* Test list_add and !list_empty. */
44         ok1(!list_empty(&parent.children));
45         ok1(c2.list.next == &parent.children.n);
46         ok1(c2.list.prev == &parent.children.n);
47         ok1(parent.children.n.next == &c2.list);
48         ok1(parent.children.n.prev == &c2.list);
49         /* Test list_check */
50         ok1(list_check(&parent.children, NULL));
51
52         c1.name = "c1";
53         list_add(&parent.children, &c1.list);
54         /* Test list_add and !list_empty. */
55         ok1(!list_empty(&parent.children));
56         ok1(c2.list.next == &parent.children.n);
57         ok1(c2.list.prev == &c1.list);
58         ok1(parent.children.n.next == &c1.list);
59         ok1(parent.children.n.prev == &c2.list);
60         ok1(c1.list.next == &c2.list);
61         ok1(c1.list.prev == &parent.children.n);
62         /* Test list_check */
63         ok1(list_check(&parent.children, NULL));
64
65         c3.name = "c3";
66         list_add_tail(&parent.children, &c3.list);
67         /* Test list_add_tail and !list_empty. */
68         ok1(!list_empty(&parent.children));
69         ok1(parent.children.n.next == &c1.list);
70         ok1(parent.children.n.prev == &c3.list);
71         ok1(c1.list.next == &c2.list);
72         ok1(c1.list.prev == &parent.children.n);
73         ok1(c2.list.next == &c3.list);
74         ok1(c2.list.prev == &c1.list);
75         ok1(c3.list.next == &parent.children.n);
76         ok1(c3.list.prev == &c2.list);
77         /* Test list_check */
78         ok1(list_check(&parent.children, NULL));
79
80         /* Test list_check_node */
81         ok1(list_check_node(&c1.list, NULL));
82         ok1(list_check_node(&c2.list, NULL));
83         ok1(list_check_node(&c3.list, NULL));
84
85         /* Test list_top */
86         ok1(list_top(&parent.children, struct child, list) == &c1);
87
88         /* Test list_pop */
89         ok1(list_pop(&parent.children, struct child, list) == &c1);
90         ok1(list_top(&parent.children, struct child, list) == &c2);
91         list_add(&parent.children, &c1.list);
92
93         /* Test list_tail */
94         ok1(list_tail(&parent.children, struct child, list) == &c3);
95
96         /* Test list_for_each. */
97         i = 0;
98         list_for_each(&parent.children, c, 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                 if (i > 2)
111                         break;
112         }
113         ok1(i == 3);
114
115         /* Test list_for_each_rev. */
116         i = 0;
117         list_for_each_rev(&parent.children, c, list) {
118                 switch (i++) {
119                 case 0:
120                         ok1(c == &c3);
121                         break;
122                 case 1:
123                         ok1(c == &c2);
124                         break;
125                 case 2:
126                         ok1(c == &c1);
127                         break;
128                 }
129                 if (i > 2)
130                         break;
131         }
132         ok1(i == 3);
133
134         /* Test list_for_each_safe, list_del and list_del_from. */
135         i = 0;
136         list_for_each_safe(&parent.children, c, n, list) {
137                 switch (i++) {
138                 case 0:
139                         ok1(c == &c1);  
140                         list_del(&c->list);
141                         break;
142                 case 1:
143                         ok1(c == &c2);
144                         list_del_from(&parent.children, &c->list);
145                         break;
146                 case 2:
147                         ok1(c == &c3);
148                         list_del_from(&parent.children, &c->list);
149                         break;
150                 }
151                 ok1(list_check(&parent.children, NULL));
152                 if (i > 2)
153                         break;
154         }
155         ok1(i == 3);
156         ok1(list_empty(&parent.children));
157
158         /* Test list_node_init: safe to list_del after this. */
159         list_node_init(&c->list);
160         list_del(&c->list);
161
162         /* Test list_del_init */
163         list_add(&parent.children, &c->list);
164         ok1(!list_empty(&parent.children));
165         list_del_init(&c->list);
166         ok1(list_empty(&parent.children));
167         /* We can call this as many times as we like. */
168         list_del_init(&c->list);
169         list_del_init(&c->list);
170
171         /* Test list_for_each_off. */
172         list_add_tail(&opaque_list,
173                       (struct list_node *)create_opaque_blob());
174         list_add_tail(&opaque_list,
175                       (struct list_node *)create_opaque_blob());
176         list_add_tail(&opaque_list,
177                       (struct list_node *)create_opaque_blob());
178
179         i = 0;
180
181         list_for_each_off(&opaque_list, q, 0) {
182           i++;
183           ok1(if_blobs_know_the_secret(q));
184         }
185         ok1(i == 3);
186
187         /* Test list_for_each_safe_off, list_del_off and list_del_from_off. */
188         i = 0;
189         list_for_each_safe_off(&opaque_list, q, nq, 0) {
190                 switch (i++) {
191                 case 0:
192                         ok1(if_blobs_know_the_secret(q));
193                         list_del_off(q, 0);
194                         destroy_opaque_blob(q);
195                         break;
196                 case 1:
197                         ok1(if_blobs_know_the_secret(q));
198                         list_del_from_off(&opaque_list, q, 0);
199                         destroy_opaque_blob(q);
200                         break;
201                 case 2:
202                         ok1(c == &c3);
203                         list_del_from_off(&opaque_list, q, 0);
204                         destroy_opaque_blob(q);
205                         break;
206                 }
207                 ok1(list_check(&opaque_list, NULL));
208                 if (i > 2)
209                         break;
210         }
211         ok1(i == 3);
212         ok1(list_empty(&opaque_list));
213
214         /* Test list_top/list_tail/list_pop on empty list. */
215         ok1(list_top(&parent.children, struct child, list) == NULL);
216         ok1(list_tail(&parent.children, struct child, list) == NULL);
217         ok1(list_pop(&parent.children, struct child, list) == NULL);
218
219         /* Test list_add_before and list_add_after */
220         list_add(&parent.children, &c1.list);
221         list_add_after(&parent.children, &c1.list, &c2.list);
222         ok1(list_check(&parent.children, "list_add_after"));
223
224         i = 0;
225         list_for_each(&parent.children, c, list) {
226                 switch (i++) {
227                 case 0:
228                         ok1(c == &c1);
229                         break;
230                 case 1:
231                         ok1(c == &c2);
232                         break;
233                 }
234         }
235         ok1(i == 2);
236
237         list_add_before(&parent.children, &c2.list, &c3.list);
238         ok1(list_check(&parent.children, "list_add_before"));
239
240         i = 0;
241         list_for_each(&parent.children, c, list) {
242                 switch (i++) {
243                 case 0:
244                         ok1(c == &c1);
245                         break;
246                 case 1:
247                         ok1(c == &c3);
248                         break;
249                 case 2:
250                         ok1(c == &c2);
251                         break;
252                 }
253         }
254         ok1(i == 3);
255
256         /* test list_swap */
257         list_swap(&c3.list, &x1.list);
258         ok1(list_check(&parent.children, "list_swap"));
259         i = 0;
260         list_for_each(&parent.children, c, list) {
261                 switch (i++) {
262                 case 0:
263                         ok1(c == &c1);
264                         break;
265                 case 1:
266                         ok1(c == &x1);
267                         break;
268                 case 2:
269                         ok1(c == &c2);
270                         break;
271                 }
272         }
273         ok1(i == 3);
274
275         return exit_status();
276 }