try to remove stdint.h
[liba.git] / test / list.h
bloba77196deb1961dd71ca5d984c39d608551a3a170
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 *it;
12 A_LIST_FOREACH_NEXT(it, ctx)
14 if (it == it->next)
16 printf("\nwarning endless loop!\n");
17 break;
19 ++count;
22 else if (ctx != ctx->prev)
24 a_list *it;
25 A_LIST_FOREACH_PREV(it, ctx)
27 if (it == it->prev)
29 printf("\nwarning endless loop!\n");
30 break;
32 ++count;
35 return count;
38 typedef struct
40 a_list node;
41 a_cast data;
42 } data;
44 static void test_next(void)
46 int i;
47 a_list *list1, *list2;
48 a_list *it, *at, *next;
49 list1 = a_new(a_list, A_NULL, 1);
50 a_list_ctor(list1);
51 for (i = 0; i != 10; ++i)
53 data *node = a_new(data, A_NULL, 1);
54 node->data.i = i;
55 a_list_add_prev(list1, &node->node);
57 a_list_rot_prev(list1);
58 list2 = a_new(a_list, A_NULL, 1);
59 a_list_ctor(list2);
60 for (i = 10; i != 21; ++i)
62 data *node = a_new(data, A_NULL, 1);
63 node->data.i = i;
64 a_list_add_prev(list2, &node->node);
67 data *node = a_list_entry_prev(list2, data, node); /* NOLINT(performance-no-int-to-ptr) */
68 a_list_del_prev(list2);
69 a_die(node);
71 next = list2->next;
72 a_list_mov_prev(list1, list2);
73 A_LIST_FOREACH_NEXT(it, list1)
75 data *node = a_list_entry(it, data, node); /* NOLINT(performance-no-int-to-ptr) */
76 printf("%i ", node->data.i);
78 printf("%" PRIz "u", a_list_len(list1));
79 A_LIST_FORSAFE_NEXT(it, at, list1)
81 data *node = a_list_entry(it, data, node); /* NOLINT(performance-no-int-to-ptr) */
82 a_list_del_node(&node->node);
83 a_die(node);
85 if (list1->next == list1 && list2->next == next)
87 printf(" ok");
89 (void)putchar('\n');
90 a_die(list1);
91 a_die(list2);
94 static void test_prev(void)
96 int i;
97 a_list *list1, *list2;
98 a_list *it, *at, *prev;
99 list1 = a_new(a_list, A_NULL, 1);
100 a_list_ctor(list1);
101 for (i = 0; i != 10; ++i)
103 data *node = a_new(data, A_NULL, 1);
104 node->data.i = i;
105 a_list_add_next(list1, &node->node);
107 a_list_rot_next(list1);
108 list2 = a_new(a_list, A_NULL, 1);
109 a_list_ctor(list2);
110 for (i = 10; i != 21; ++i)
112 data *node = a_new(data, A_NULL, 1);
113 node->data.i = i;
114 a_list_add_next(list2, &node->node);
117 data *node = a_list_entry_next(list2, data, node); /* NOLINT(performance-no-int-to-ptr) */
118 a_list_del_next(list2);
119 a_die(node);
121 prev = list2->prev;
122 a_list_mov_next(list1, list2);
123 A_LIST_FOREACH_PREV(it, list1)
125 data *node = a_list_entry(it, data, node); /* NOLINT(performance-no-int-to-ptr) */
126 printf("%i ", node->data.i);
128 printf("%" PRIz "u", a_list_len(list1));
129 A_LIST_FORSAFE_PREV(it, at, list1)
131 data *node = a_list_entry(it, data, node); /* NOLINT(performance-no-int-to-ptr) */
132 a_list_del_node(&node->node);
133 a_die(node);
135 if (list1->prev == list1 && list2->prev == prev)
137 printf(" ok");
139 (void)putchar('\n');
140 a_die(list1);
141 a_die(list2);
144 static void test_func(void)
146 int i;
147 data *ctx;
148 a_list *it, *at;
149 a_list *list1, *list2;
150 list1 = a_new(a_list, A_NULL, 1);
151 a_list_ctor(list1);
152 for (i = 0; i != 10; ++i)
154 data *node = a_new(data, A_NULL, 1);
155 node->data.i = i;
156 a_list_add_prev(list1, &node->node);
158 list2 = a_new(a_list, A_NULL, 1);
159 a_list_ctor(list2);
160 for (i = 10; i != 20; ++i)
162 data *node = a_new(data, A_NULL, 1);
163 node->data.i = i;
164 a_list_add_prev(list2, &node->node);
166 ctx = a_new(data, A_NULL, 1);
167 ctx->data.i = -1;
169 a_list *ptr = list2->prev;
170 a_list_set_node(ptr, &ctx->node);
171 ctx = a_list_entry(ptr, data, node); /* NOLINT(performance-no-int-to-ptr) */
173 a_list_swap_node(list2->prev, list2->next);
174 A_LIST_FOREACH_NEXT(it, list1)
176 data *node = a_list_entry(it, data, node); /* NOLINT(performance-no-int-to-ptr) */
177 printf("%i ", node->data.i);
179 A_LIST_FOREACH_NEXT(it, list2)
181 data *node = a_list_entry(it, data, node); /* NOLINT(performance-no-int-to-ptr) */
182 printf("%i ", node->data.i);
184 printf("%" PRIz "u", a_list_len(list1) + a_list_len(list2));
185 A_LIST_FORSAFE_NEXT(it, at, list1)
187 data *node = a_list_entry(it, data, node); /* NOLINT(performance-no-int-to-ptr) */
188 a_list_del_node(&node->node);
189 a_die(node);
191 A_LIST_FORSAFE_NEXT(it, at, list2)
193 data *node = a_list_entry(it, data, node); /* NOLINT(performance-no-int-to-ptr) */
194 a_list_del_node(&node->node);
195 a_die(node);
197 if (list1->next == list1 && list2->next == list2)
199 printf(" ok");
201 (void)putchar('\n');
202 a_die(list1);
203 a_die(list2);
204 a_die(ctx);
207 static void test_null(void)
209 static a_list list1 = A_LIST_INIT(list1);
210 static a_list list2 = A_LIST_INIT(list2);
211 a_size len = a_list_len(&list1) + a_list_len(&list2);
213 a_list_add_next(&list1, &list1);
214 len = a_list_len(&list1) + a_list_len(&list2);
215 if (len != 0)
217 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
220 a_list_add_prev(&list1, &list1);
221 len = a_list_len(&list1) + a_list_len(&list2);
222 if (len != 0)
224 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
227 a_list_add_next(&list1, &list2);
228 len = a_list_len(&list1) + a_list_len(&list2);
229 if (len != 2)
231 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
234 a_list_add_prev(&list1, &list2);
235 len = a_list_len(&list1) + a_list_len(&list2);
236 if (len != 2)
238 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
241 a_list_del_node(&list1);
242 a_list_del_node(&list1);
243 a_list_del_next(&list1);
244 a_list_del_prev(&list1);
245 a_list_ctor(&list1);
246 len = a_list_len(&list1) + a_list_len(&list2);
247 if (len != 0)
249 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
252 a_list_rot_next(&list1);
253 a_list_rot_prev(&list1);
254 len = a_list_len(&list1) + a_list_len(&list2);
255 if (len != 0)
257 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
260 a_list_link(&list1, &list2);
261 a_list_loop(&list1, &list2);
262 len = a_list_len(&list1) + a_list_len(&list2);
263 if (len != 2)
265 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
268 a_list_rot_next(&list1);
269 a_list_rot_prev(&list1);
270 a_list_rot_prev(&list1);
271 len = a_list_len(&list1) + a_list_len(&list2);
272 if (len != 2)
274 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
277 a_list_swap_node(&list1, &list1);
278 a_list_swap_node(&list2, &list2);
279 len = a_list_len(&list1) + a_list_len(&list2);
280 if (len != 2)
282 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
285 a_list_swap_node(&list1, &list2);
286 len = a_list_len(&list1) + a_list_len(&list2);
287 if (len != 0)
289 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
292 a_list_swap_node(&list2, &list1);
293 len = a_list_len(&list1) + a_list_len(&list2);
294 if (len != 2)
296 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
299 a_list_set_node(&list1, &list1);
300 a_list_set_node(&list2, &list2);
301 len = a_list_len(&list1) + a_list_len(&list2);
302 if (len != 2)
304 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
307 a_list_set_node(&list2, &list1);
308 a_list_init(&list2);
309 len = a_list_len(&list1) + a_list_len(&list2);
310 if (len != 0)
312 printf("failure in %s %i %" PRIz "u\n", __FILE__, __LINE__, len);
316 int main(int argc, char *argv[]) /* NOLINT(misc-definitions-in-headers) */
318 puts(A_FUNC);
319 test_next();
320 test_prev();
321 test_func();
322 test_null();
323 (void)argc;
324 (void)argv;
325 return 0;