s3: smbclient: Allinfo leaves the file handle open.
[Samba.git] / lib / ccan / list / test / run.c
bloba93234d6551ae4618afaa716389075ea4654f247
1 #include <ccan/list/list.h>
2 #include <ccan/tap/tap.h>
3 #include <ccan/list/list.c>
4 #include "helper.h"
6 struct parent {
7 const char *name;
8 struct ccan_list_head children;
9 unsigned int num_children;
12 struct child {
13 const char *name;
14 struct ccan_list_node list;
17 static CCAN_LIST_HEAD(static_list);
19 int main(int argc, char *argv[])
21 struct parent parent;
22 struct child c1, c2, c3, *c, *n;
23 unsigned int i;
24 struct ccan_list_head list = CCAN_LIST_HEAD_INIT(list);
25 opaque_t *q, *nq;
26 struct ccan_list_head opaque_list = CCAN_LIST_HEAD_INIT(opaque_list);
28 plan_tests(65);
29 /* Test CCAN_LIST_HEAD, CCAN_LIST_HEAD_INIT, ccan_list_empty and check_list */
30 ok1(ccan_list_empty(&static_list));
31 ok1(ccan_list_check(&static_list, NULL));
32 ok1(ccan_list_empty(&list));
33 ok1(ccan_list_check(&list, NULL));
35 parent.num_children = 0;
36 ccan_list_head_init(&parent.children);
37 /* Test ccan_list_head_init */
38 ok1(ccan_list_empty(&parent.children));
39 ok1(ccan_list_check(&parent.children, NULL));
41 c2.name = "c2";
42 ccan_list_add(&parent.children, &c2.list);
43 /* Test ccan_list_add and !ccan_list_empty. */
44 ok1(!ccan_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 ccan_list_check */
50 ok1(ccan_list_check(&parent.children, NULL));
52 c1.name = "c1";
53 ccan_list_add(&parent.children, &c1.list);
54 /* Test ccan_list_add and !ccan_list_empty. */
55 ok1(!ccan_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 ccan_list_check */
63 ok1(ccan_list_check(&parent.children, NULL));
65 c3.name = "c3";
66 ccan_list_add_tail(&parent.children, &c3.list);
67 /* Test ccan_list_add_tail and !ccan_list_empty. */
68 ok1(!ccan_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 ccan_list_check */
78 ok1(ccan_list_check(&parent.children, NULL));
80 /* Test ccan_list_check_node */
81 ok1(ccan_list_check_node(&c1.list, NULL));
82 ok1(ccan_list_check_node(&c2.list, NULL));
83 ok1(ccan_list_check_node(&c3.list, NULL));
85 /* Test ccan_list_top */
86 ok1(ccan_list_top(&parent.children, struct child, list) == &c1);
88 /* Test ccan_list_tail */
89 ok1(ccan_list_tail(&parent.children, struct child, list) == &c3);
91 /* Test ccan_list_for_each. */
92 i = 0;
93 ccan_list_for_each(&parent.children, c, list) {
94 switch (i++) {
95 case 0:
96 ok1(c == &c1);
97 break;
98 case 1:
99 ok1(c == &c2);
100 break;
101 case 2:
102 ok1(c == &c3);
103 break;
105 if (i > 2)
106 break;
108 ok1(i == 3);
110 /* Test ccan_list_for_each_rev. */
111 i = 0;
112 ccan_list_for_each_rev(&parent.children, c, list) {
113 switch (i++) {
114 case 0:
115 ok1(c == &c3);
116 break;
117 case 1:
118 ok1(c == &c2);
119 break;
120 case 2:
121 ok1(c == &c1);
122 break;
124 if (i > 2)
125 break;
127 ok1(i == 3);
129 /* Test ccan_list_for_each_safe, ccan_list_del and ccan_list_del_from. */
130 i = 0;
131 ccan_list_for_each_safe(&parent.children, c, n, list) {
132 switch (i++) {
133 case 0:
134 ok1(c == &c1);
135 ccan_list_del(&c->list);
136 break;
137 case 1:
138 ok1(c == &c2);
139 ccan_list_del_from(&parent.children, &c->list);
140 break;
141 case 2:
142 ok1(c == &c3);
143 ccan_list_del_from(&parent.children, &c->list);
144 break;
146 ok1(ccan_list_check(&parent.children, NULL));
147 if (i > 2)
148 break;
150 ok1(i == 3);
151 ok1(ccan_list_empty(&parent.children));
153 /* Test ccan_list_for_each_off. */
154 ccan_list_add_tail(&opaque_list,
155 (struct ccan_list_node *)create_opaque_blob());
156 ccan_list_add_tail(&opaque_list,
157 (struct ccan_list_node *)create_opaque_blob());
158 ccan_list_add_tail(&opaque_list,
159 (struct ccan_list_node *)create_opaque_blob());
161 i = 0;
163 ccan_list_for_each_off(&opaque_list, q, 0) {
164 i++;
165 ok1(if_blobs_know_the_secret(q));
167 ok1(i == 3);
169 /* Test ccan_list_for_each_safe_off, ccan_list_del_off and ccan_list_del_from_off. */
170 i = 0;
171 ccan_list_for_each_safe_off(&opaque_list, q, nq, 0) {
172 switch (i++) {
173 case 0:
174 ok1(if_blobs_know_the_secret(q));
175 ccan_list_del_off(q, 0);
176 destroy_opaque_blob(q);
177 break;
178 case 1:
179 ok1(if_blobs_know_the_secret(q));
180 ccan_list_del_from_off(&opaque_list, q, 0);
181 destroy_opaque_blob(q);
182 break;
183 case 2:
184 ok1(c == &c3);
185 ccan_list_del_from_off(&opaque_list, q, 0);
186 destroy_opaque_blob(q);
187 break;
189 ok1(ccan_list_check(&opaque_list, NULL));
190 if (i > 2)
191 break;
193 ok1(i == 3);
194 ok1(ccan_list_empty(&opaque_list));
196 /* Test ccan_list_top/ccan_list_tail on empty list. */
197 ok1(ccan_list_top(&parent.children, struct child, list) == NULL);
198 ok1(ccan_list_tail(&parent.children, struct child, list) == NULL);
199 return exit_status();