release 0.1.15
[liba.git] / test / list.h
blob3c73dd7fca99673563051c3a4e9e4e9c13f5b8f0
1 #define MAIN(x) list##x
2 #include "test.h"
3 #include "a/list.h"
5 static a_size a_list_len(a_list const *ctx)
7 a_size count = 0;
8 if (!ctx) {}
9 else if (ctx != ctx->next)
11 a_list_foreach_next(it, ctx)
13 if (it == it->next)
15 printf("\nwarning endless loop!\n");
16 break;
18 ++count;
21 else if (ctx != ctx->prev)
23 a_list_foreach_prev(it, ctx)
25 if (it == it->prev)
27 printf("\nwarning endless loop!\n");
28 break;
30 ++count;
33 return count;
36 typedef struct
38 a_list node;
39 a_cast data;
40 } data;
42 static void test_next(void)
44 a_list *list1 = a_new(a_list, A_NULL, 1);
45 a_list_ctor(list1);
46 for (int i = 0; i != 10; ++i)
48 data *node = a_new(data, A_NULL, 1);
49 node->data.i = i;
50 a_list_add_prev(list1, &node->node);
52 a_list_rot_prev(list1);
53 a_list *list2 = a_new(a_list, A_NULL, 1);
54 a_list_ctor(list2);
55 for (int i = 10; i != 21; ++i)
57 data *node = a_new(data, A_NULL, 1);
58 node->data.i = i;
59 a_list_add_prev(list2, &node->node);
62 data *node = a_list_entry_prev(list2, data, node); // NOLINT(performance-no-int-to-ptr)
63 a_list_del_prev(list2);
64 a_die(node);
66 a_list *next = list2->next;
67 a_list_mov_prev(list1, list2);
68 a_list_foreach_next(it, list1)
70 data *node = a_list_entry(it, data, node); // NOLINT(performance-no-int-to-ptr)
71 printf("%i ", node->data.i);
73 printf("%" PRIz "u", a_list_len(list1));
74 a_list_forsafe_next(it, pre, list1)
76 data *node = a_list_entry(it, data, node); // NOLINT(performance-no-int-to-ptr)
77 a_list_del_node(&node->node);
78 a_die(node);
80 if (list1->next == list1 && list2->next == next)
82 printf(" ok");
84 putchar('\n');
85 a_die(list1);
86 a_die(list2);
89 static void test_prev(void)
91 a_list *list1 = a_new(a_list, A_NULL, 1);
92 a_list_ctor(list1);
93 for (int i = 0; i != 10; ++i)
95 data *node = a_new(data, A_NULL, 1);
96 node->data.i = i;
97 a_list_add_next(list1, &node->node);
99 a_list_rot_next(list1);
100 a_list *list2 = a_new(a_list, A_NULL, 1);
101 a_list_ctor(list2);
102 for (int i = 10; i != 21; ++i)
104 data *node = a_new(data, A_NULL, 1);
105 node->data.i = i;
106 a_list_add_next(list2, &node->node);
109 data *node = a_list_entry_next(list2, data, node); // NOLINT(performance-no-int-to-ptr)
110 a_list_del_next(list2);
111 a_die(node);
113 a_list *prev = list2->prev;
114 a_list_mov_next(list1, list2);
115 a_list_foreach_prev(it, list1)
117 data *node = a_list_entry(it, data, node); // NOLINT(performance-no-int-to-ptr)
118 printf("%i ", node->data.i);
120 printf("%" PRIz "u", a_list_len(list1));
121 a_list_forsafe_prev(it, pre, list1)
123 data *node = a_list_entry(it, data, node); // NOLINT(performance-no-int-to-ptr)
124 a_list_del_node(&node->node);
125 a_die(node);
127 if (list1->prev == list1 && list2->prev == prev)
129 printf(" ok");
131 putchar('\n');
132 a_die(list1);
133 a_die(list2);
136 static void test_func(void)
138 a_list *list1 = a_new(a_list, A_NULL, 1);
139 a_list_ctor(list1);
140 for (int i = 0; i != 10; ++i)
142 data *node = a_new(data, A_NULL, 1);
143 node->data.i = i;
144 a_list_add_prev(list1, &node->node);
146 a_list *list2 = a_new(a_list, A_NULL, 1);
147 a_list_ctor(list2);
148 for (int i = 10; i != 20; ++i)
150 data *node = a_new(data, A_NULL, 1);
151 node->data.i = i;
152 a_list_add_prev(list2, &node->node);
154 data *ctx = a_new(data, A_NULL, 1);
155 ctx->data.i = -1;
157 a_list *ptr = list2->prev;
158 a_list_set_node(ptr, &ctx->node);
159 ctx = a_list_entry(ptr, data, node); // NOLINT(performance-no-int-to-ptr)
161 a_list_swap_node(list2->prev, list2->next);
162 a_list_foreach_next(it, list1)
164 data *node = a_list_entry(it, data, node); // NOLINT(performance-no-int-to-ptr)
165 printf("%i ", node->data.i);
167 a_list_foreach_next(it, list2)
169 data *node = a_list_entry(it, data, node); // NOLINT(performance-no-int-to-ptr)
170 printf("%i ", node->data.i);
172 printf("%" PRIz "u", a_list_len(list1) + a_list_len(list2));
173 a_list_forsafe_next(it, at, list1)
175 data *node = a_list_entry(it, data, node); // NOLINT(performance-no-int-to-ptr)
176 a_list_del_node(&node->node);
177 a_die(node);
179 a_list_forsafe_next(it, at, list2)
181 data *node = a_list_entry(it, data, node); // NOLINT(performance-no-int-to-ptr)
182 a_list_del_node(&node->node);
183 a_die(node);
185 if (list1->next == list1 && list2->next == list2)
187 printf(" ok");
189 putchar('\n');
190 a_die(list1);
191 a_die(list2);
192 a_die(ctx);
195 static void test_null(void)
197 static a_list list1 = A_LIST_INIT(list1);
198 static a_list list2 = A_LIST_INIT(list2);
199 a_size len = a_list_len(&list1) + a_list_len(&list2);
201 a_list_add_next(&list1, &list1);
202 len = a_list_len(&list1) + a_list_len(&list2);
203 if (len != 0)
205 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
208 a_list_add_prev(&list1, &list1);
209 len = a_list_len(&list1) + a_list_len(&list2);
210 if (len != 0)
212 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
215 a_list_add_next(&list1, &list2);
216 len = a_list_len(&list1) + a_list_len(&list2);
217 if (len != 2)
219 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
222 a_list_add_prev(&list1, &list2);
223 len = a_list_len(&list1) + a_list_len(&list2);
224 if (len != 2)
226 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
229 a_list_del_node(&list1);
230 a_list_del_node(&list1);
231 a_list_del_next(&list1);
232 a_list_del_prev(&list1);
233 a_list_ctor(&list1);
234 len = a_list_len(&list1) + a_list_len(&list2);
235 if (len != 0)
237 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
240 a_list_rot_next(&list1);
241 a_list_rot_prev(&list1);
242 len = a_list_len(&list1) + a_list_len(&list2);
243 if (len != 0)
245 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
248 a_list_link(&list1, &list2);
249 a_list_loop(&list1, &list2);
250 len = a_list_len(&list1) + a_list_len(&list2);
251 if (len != 2)
253 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
256 a_list_rot_next(&list1);
257 a_list_rot_prev(&list1);
258 a_list_rot_prev(&list1);
259 len = a_list_len(&list1) + a_list_len(&list2);
260 if (len != 2)
262 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
265 a_list_swap_node(&list1, &list1);
266 a_list_swap_node(&list2, &list2);
267 len = a_list_len(&list1) + a_list_len(&list2);
268 if (len != 2)
270 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
273 a_list_swap_node(&list1, &list2);
274 len = a_list_len(&list1) + a_list_len(&list2);
275 if (len != 0)
277 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
280 a_list_swap_node(&list2, &list1);
281 len = a_list_len(&list1) + a_list_len(&list2);
282 if (len != 2)
284 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
287 a_list_set_node(&list1, &list1);
288 a_list_set_node(&list2, &list2);
289 len = a_list_len(&list1) + a_list_len(&list2);
290 if (len != 2)
292 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
295 a_list_set_node(&list2, &list1);
296 a_list_init(&list2);
297 len = a_list_len(&list1) + a_list_len(&list2);
298 if (len != 0)
300 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
304 int main(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
306 (void)argc;
307 (void)argv;
308 printf("%s\n", A_FUNC);
309 test_next();
310 test_prev();
311 test_func();
312 test_null();
313 return 0;