]> git.ozlabs.org Git - ccan/blob - ccan/list/test/run.c
tdb2: make internal coalesce() function return length coalesced.
[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         struct list_head list = LIST_HEAD_INIT(list);
24
25         plan_tests(49);
26         /* Test LIST_HEAD, LIST_HEAD_INIT, list_empty and check_list */
27         ok1(list_empty(&static_list));
28         ok1(list_check(&static_list, NULL));
29         ok1(list_empty(&list));
30         ok1(list_check(&list, NULL));
31
32         parent.num_children = 0;
33         list_head_init(&parent.children);
34         /* Test list_head_init */
35         ok1(list_empty(&parent.children));
36         ok1(list_check(&parent.children, NULL));
37
38         c2.name = "c2";
39         list_add(&parent.children, &c2.list);
40         /* Test list_add and !list_empty. */
41         ok1(!list_empty(&parent.children));
42         ok1(c2.list.next == &parent.children.n);
43         ok1(c2.list.prev == &parent.children.n);
44         ok1(parent.children.n.next == &c2.list);
45         ok1(parent.children.n.prev == &c2.list);
46         /* Test list_check */
47         ok1(list_check(&parent.children, NULL));
48
49         c1.name = "c1";
50         list_add(&parent.children, &c1.list);
51         /* Test list_add and !list_empty. */
52         ok1(!list_empty(&parent.children));
53         ok1(c2.list.next == &parent.children.n);
54         ok1(c2.list.prev == &c1.list);
55         ok1(parent.children.n.next == &c1.list);
56         ok1(parent.children.n.prev == &c2.list);
57         ok1(c1.list.next == &c2.list);
58         ok1(c1.list.prev == &parent.children.n);
59         /* Test list_check */
60         ok1(list_check(&parent.children, NULL));
61
62         c3.name = "c3";
63         list_add_tail(&parent.children, &c3.list);
64         /* Test list_add_tail and !list_empty. */
65         ok1(!list_empty(&parent.children));
66         ok1(parent.children.n.next == &c1.list);
67         ok1(parent.children.n.prev == &c3.list);
68         ok1(c1.list.next == &c2.list);
69         ok1(c1.list.prev == &parent.children.n);
70         ok1(c2.list.next == &c3.list);
71         ok1(c2.list.prev == &c1.list);
72         ok1(c3.list.next == &parent.children.n);
73         ok1(c3.list.prev == &c2.list);
74         /* Test list_check */
75         ok1(list_check(&parent.children, NULL));
76
77         /* Test list_check_node */
78         ok1(list_check_node(&c1.list, NULL));
79         ok1(list_check_node(&c2.list, NULL));
80         ok1(list_check_node(&c3.list, NULL));
81
82         /* Test list_top */
83         ok1(list_top(&parent.children, struct child, list) == &c1);
84
85         /* Test list_tail */
86         ok1(list_tail(&parent.children, struct child, list) == &c3);
87
88         /* Test list_for_each. */
89         i = 0;
90         list_for_each(&parent.children, c, list) {
91                 switch (i++) {
92                 case 0:
93                         ok1(c == &c1);
94                         break;
95                 case 1:
96                         ok1(c == &c2);
97                         break;
98                 case 2:
99                         ok1(c == &c3);
100                         break;
101                 }
102                 if (i > 2)
103                         break;
104         }
105         ok1(i == 3);
106
107         /* Test list_for_each_safe, list_del and list_del_from. */
108         i = 0;
109         list_for_each_safe(&parent.children, c, n, list) {
110                 switch (i++) {
111                 case 0:
112                         ok1(c == &c1);  
113                         list_del(&c->list);
114                         break;
115                 case 1:
116                         ok1(c == &c2);
117                         list_del_from(&parent.children, &c->list);
118                         break;
119                 case 2:
120                         ok1(c == &c3);
121                         list_del_from(&parent.children, &c->list);
122                         break;
123                 }
124                 ok1(list_check(&parent.children, NULL));
125                 if (i > 2)
126                         break;
127         }
128         ok1(i == 3);
129         ok1(list_empty(&parent.children));
130
131         /* Test list_top/list_tail on empty list. */
132         ok1(list_top(&parent.children, struct child, list) == NULL);
133         ok1(list_tail(&parent.children, struct child, list) == NULL);
134         return exit_status();
135 }