create regress_simple for Python
[liba.git] / test / que.h
blobc95b4a48f9c95fe8e0a35f73a465342bc6d848b1
1 #define MAIN(x) que##x
2 #include "test.h"
3 #include "a/que.h"
5 static void dtor(void *ptr)
7 a_u32 *u = a_u32_(*, ptr);
8 printf("%" PRIu32 " ", *u);
11 static void test(void)
13 a_que *ctx = a_que_new(sizeof(a_u64));
14 for (a_u64 i = 0; i != 10; ++i)
16 a_u64 *obj = A_QUE_PUSH_BACK(a_u64, ctx);
17 if (obj) { *obj = i; }
20 a_u64 *fore = A_QUE_FORE_(a_u64, ctx);
21 a_u64 *back = A_QUE_BACK_(a_u64, ctx);
22 printf("%" PRIu64 " %" PRIu64 " ", *fore, *back);
25 a_u64 *fore = A_QUE_FORE(a_u64, ctx);
26 a_u64 *back = A_QUE_BACK(a_u64, ctx);
27 if (fore && back)
29 printf("%" PRIu64 " %" PRIu64 " ", *fore, *back);
33 a_u64 *fore = A_QUE_AT(a_u64, ctx, 0);
34 a_u64 *back = A_QUE_AT(a_u64, ctx, ~0);
35 if (fore && back)
37 printf("%" PRIu64 " %" PRIu64 "\n", *fore, *back);
40 for (a_u64 i = 0; i != 5; ++i)
42 a_que_pull_fore(ctx);
44 a_que_edit(ctx, sizeof(a_u32) + 8, A_NULL);
45 for (a_u32 i = 5; i--;)
47 a_u32 *obj = A_QUE_PUSH_FORE(a_u32, ctx);
48 if (obj) { *obj = i; }
50 for (a_u32 i = 5; i != 10; ++i)
52 a_u32 *obj = A_QUE_PUSH_BACK(a_u32, ctx);
53 if (obj) { *obj = i; }
56 a_u32 *obj = A_QUE_INSERT(a_u32, ctx, 0);
57 if (obj) { *obj = A_U32_MAX; }
60 a_u32 *obj = A_QUE_INSERT(a_u32, ctx, A_SIZE_MAX);
61 if (obj) { *obj = A_U32_MAX; }
63 a_que_remove(ctx, 0);
64 a_que_remove(ctx, A_SIZE_MAX);
65 a_que_swap(ctx, 0, A_SIZE_MAX);
67 a_u32 *lhs = A_QUE_FORE_(a_u32, ctx);
68 a_u32 *rhs = A_QUE_BACK_(a_u32, ctx);
69 a_que_swap_(lhs, rhs);
71 a_que_foreach(a_u32, *, it, ctx)
73 TEST_BUG(a_que_siz(ctx) >= sizeof(*it));
74 printf("%" PRIu32 " ", *it);
76 putchar('\n');
77 a_que_foreach_reverse(a_u32, *, it, ctx)
79 TEST_BUG(a_que_siz(ctx) >= sizeof(*it));
80 printf("%" PRIu32 " ", *it);
82 putchar('\n');
83 for (a_u32 i = 0; i != 5; ++i)
85 a_que_push_back(ctx);
87 for (a_u32 i = 0; i != 5; ++i)
89 a_que_push_fore(ctx);
91 a_que_die(ctx, dtor);
92 putchar('\n');
95 #include <time.h>
97 static int small(void const *lhs, void const *rhs)
99 int a = *a_int_(const *, lhs);
100 int b = *a_int_(const *, rhs);
101 return (a > b) - (a < b);
104 static int large(void const *lhs, void const *rhs)
106 int a = *a_int_(const *, lhs);
107 int b = *a_int_(const *, rhs);
108 return (a < b) - (a > b);
111 static int rand10(void)
113 return a_cast_s(int, rand() / a_cast_s(double, RAND_MAX) * 10); // NOLINT
116 static void test_sort(void)
118 unsigned int t = a_cast_s(unsigned int, time(A_NULL));
119 a_que *ctx = a_que_new(sizeof(int));
120 int x;
122 x = -1;
123 srand(t);
124 a_que_drop(ctx, A_NULL);
125 for (int i = 0; i != 10; ++i)
127 int *obj = A_QUE_PUSH_FORE(int, ctx);
128 if (obj)
130 *obj = rand10();
131 printf("%i ", *obj);
132 a_que_sort_fore(ctx, small);
135 printf("-> ");
136 a_que_foreach(int, *, it, ctx)
138 if (x >= 0) { TEST_BUG(x <= *it); }
139 printf("%i ", *it);
140 x = *it;
143 x = -1;
144 srand(t);
145 printf("-> ");
146 a_que_drop(ctx, dtor);
147 putchar('\n');
148 for (int i = 0; i != 10; ++i)
150 int *obj = A_QUE_PUSH_BACK(int, ctx);
151 if (obj)
153 *obj = rand10();
154 printf("%i ", *obj);
155 a_que_sort_back(ctx, large);
158 printf("-> ");
159 a_que_foreach(int, *, it, ctx)
161 if (x >= 0) { TEST_BUG(x >= *it); }
162 printf("%i ", *it);
163 x = *it;
166 x = -1;
167 srand(t);
168 printf("-> ");
169 a_que_drop(ctx, dtor);
170 putchar('\n');
171 for (int i = 0; i != 10; ++i)
173 int key = rand10();
174 int *obj = A_QUE_PUSH_SORT(int, ctx, &key, small);
175 if (obj)
177 printf("%i ", key);
178 *obj = key;
181 printf("-> ");
182 a_que_foreach(int, *, it, ctx)
184 if (x >= 0) { TEST_BUG(x <= *it); }
185 printf("%i ", *it);
186 x = *it;
189 printf("-> ");
190 a_que_die(ctx, dtor);
191 putchar('\n');
194 int main(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
196 (void)argc;
197 (void)argv;
198 printf("%s\n", A_FUNC);
199 test();
200 test_sort();
201 return 0;