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