1
#include <ccan/list/test/helper.c>
2
#include <ccan/list/list.h>
3
#include <ccan/tap/tap.h>
4
#include <ccan/list/list.c>
9
struct list_head children;
10
unsigned int num_children;
15
struct list_node list;
18
static LIST_HEAD(static_list);
20
int main(int argc, char *argv[])
23
struct child c1, c2, c3, *c, *n;
25
struct list_head list = LIST_HEAD_INIT(list);
27
struct list_head opaque_list = LIST_HEAD_INIT(opaque_list);
33
/* Test LIST_HEAD, LIST_HEAD_INIT, list_empty and check_list */
34
ok1(list_empty(&static_list));
35
ok1(list_check(&static_list, NULL));
36
ok1(list_empty(&list));
37
ok1(list_check(&list, NULL));
39
parent.num_children = 0;
40
list_head_init(&parent.children);
41
/* Test list_head_init */
42
ok1(list_empty(&parent.children));
43
ok1(list_check(&parent.children, NULL));
46
list_add(&parent.children, &c2.list);
47
/* Test list_add and !list_empty. */
48
ok1(!list_empty(&parent.children));
49
ok1(c2.list.next == &parent.children.n);
50
ok1(c2.list.prev == &parent.children.n);
51
ok1(parent.children.n.next == &c2.list);
52
ok1(parent.children.n.prev == &c2.list);
54
ok1(list_check(&parent.children, NULL));
57
list_add(&parent.children, &c1.list);
58
/* Test list_add and !list_empty. */
59
ok1(!list_empty(&parent.children));
60
ok1(c2.list.next == &parent.children.n);
61
ok1(c2.list.prev == &c1.list);
62
ok1(parent.children.n.next == &c1.list);
63
ok1(parent.children.n.prev == &c2.list);
64
ok1(c1.list.next == &c2.list);
65
ok1(c1.list.prev == &parent.children.n);
67
ok1(list_check(&parent.children, NULL));
70
list_add_tail(&parent.children, &c3.list);
71
/* Test list_add_tail and !list_empty. */
72
ok1(!list_empty(&parent.children));
73
ok1(parent.children.n.next == &c1.list);
74
ok1(parent.children.n.prev == &c3.list);
75
ok1(c1.list.next == &c2.list);
76
ok1(c1.list.prev == &parent.children.n);
77
ok1(c2.list.next == &c3.list);
78
ok1(c2.list.prev == &c1.list);
79
ok1(c3.list.next == &parent.children.n);
80
ok1(c3.list.prev == &c2.list);
82
ok1(list_check(&parent.children, NULL));
84
/* Test list_check_node */
85
ok1(list_check_node(&c1.list, NULL));
86
ok1(list_check_node(&c2.list, NULL));
87
ok1(list_check_node(&c3.list, NULL));
90
ok1(list_top(&parent.children, struct child, list) == &c1);
93
ok1(list_tail(&parent.children, struct child, list) == &c3);
95
/* Test list_for_each. */
97
list_for_each(&parent.children, c, list) {
114
/* Test list_for_each_rev. */
116
list_for_each_rev(&parent.children, c, list) {
133
/* Test list_for_each_safe, list_del and list_del_from. */
135
list_for_each_safe(&parent.children, c, n, list) {
143
list_del_from(&parent.children, &c->list);
147
list_del_from(&parent.children, &c->list);
150
ok1(list_check(&parent.children, NULL));
155
ok1(list_empty(&parent.children));
157
/* Test list_for_each_off. */
158
list_add_tail(&opaque_list,
159
(struct list_node *)create_opaque_blob());
160
list_add_tail(&opaque_list,
161
(struct list_node *)create_opaque_blob());
162
list_add_tail(&opaque_list,
163
(struct list_node *)create_opaque_blob());
167
list_for_each_off(&opaque_list, q, 0) {
169
ok1(if_blobs_know_the_secret(q));
173
/* Test list_for_each_safe_off, list_del_off and list_del_from_off. */
175
list_for_each_safe_off(&opaque_list, q, nq, 0) {
178
ok1(if_blobs_know_the_secret(q));
180
destroy_opaque_blob(q);
183
ok1(if_blobs_know_the_secret(q));
184
list_del_from_off(&opaque_list, q, 0);
185
destroy_opaque_blob(q);
189
list_del_from_off(&opaque_list, q, 0);
190
destroy_opaque_blob(q);
193
ok1(list_check(&opaque_list, NULL));
198
ok1(list_empty(&opaque_list));
200
/* Test list_top/list_tail on empty list. */
201
ok1(list_top(&parent.children, struct child, list) == NULL);
202
ok1(list_tail(&parent.children, struct child, list) == NULL);
203
return exit_status();