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