try to compile with -std=c90 4/n
[liba.git] / test / vec.h
blobaf623d0dc20da80134e63638620a46d020caaa82
1 #define MAIN(x) vec##x
2 #include "test.h"
3 #include "a/vec.h"
5 static void dtor(void *ptr)
7 a_u32 *obj = a_u32_(*, ptr);
8 printf("%" PRIu32 " ", *obj);
11 static int u32dup(void *dst, void const *src)
13 *a_u32_(*, dst) = *a_u32_(const *, src);
14 printf("%" PRIu32 " ", *a_u32_(const *, src));
15 return 0;
18 static void test(void)
20 a_u32 i, *it, *at;
21 a_vec *ctx = a_vec_new(sizeof(a_u64));
23 for (i = 0; i != 10; ++i)
25 a_u64 *obj = A_VEC_PUSH(a_u64, ctx);
26 if (obj) { *obj = i; }
28 a_vec_edit(ctx, sizeof(a_u32), A_NULL);
29 for (i = 0; i != 20; ++i)
31 a_u32 *obj = A_VEC_PUSH(a_u32, ctx);
32 if (obj) { *obj = i; }
34 for (i = 0; i != 10; ++i)
36 a_vec_pull(ctx);
40 a_u8 *end = A_VEC_END(a_u8, ctx);
41 a_u8 *top = A_VEC_TOP(a_u8, ctx);
42 TEST_BUG(a_vec_siz(ctx) == a_size_c(end - top));
45 a_vec_swap(ctx, 0, 0);
46 a_vec_swap(ctx, 0, ~0U);
47 a_vec_swap(ctx, ~0U, 0);
48 a_vec_swap(ctx, 4, 6);
49 a_vec_swap(ctx, 6, 4);
51 A_VEC_FORENUM(a_u32, i, ctx)
53 it = A_VEC_AT(a_u32, ctx, i);
54 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
55 if (it) { printf("%" PRIu32 " ", *it); }
57 (void)putchar('\n');
58 A_VEC_FORENUM_REVERSE(a_u32, i, ctx)
60 it = A_VEC_AT(a_u32, ctx, i);
61 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
62 if (it) { printf("%" PRIu32 " ", *it); }
64 (void)putchar('\n');
66 A_VEC_FOREACH(a_u32 *, it, at, ctx)
68 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
69 TEST_BUG(sizeof(a_u32) == sizeof(*it));
70 printf("%" PRIu32 " ", *it);
72 (void)putchar('\n');
73 A_VEC_FOREACH_REVERSE(a_u32 *, it, at, ctx)
75 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
76 TEST_BUG(sizeof(a_u32) == sizeof(*it));
77 printf("%" PRIu32 " ", *it);
79 (void)putchar('\n');
82 a_vec obj;
83 a_vec_copy(&obj, ctx, u32dup);
84 (void)putchar('\n');
85 a_vec_dtor(ctx, A_NULL);
86 a_vec_move(ctx, &obj);
89 a_vec_die(ctx, dtor);
90 (void)putchar('\n');
93 ctx = a_vec_new(sizeof(a_u32));
94 for (i = 5; i != 10; ++i)
96 a_u32 *obj = A_VEC_INSERT(a_u32, ctx, i);
97 if (obj) { *obj = i; }
99 for (i = 0; i != 5; ++i)
101 a_u32 *obj = A_VEC_INSERT(a_u32, ctx, i);
102 if (obj) { *obj = i; }
104 A_VEC_FOREACH(a_u32 *, it, at, ctx)
106 printf("%" PRIu32 " ", *it);
108 (void)putchar('\n');
109 for (i = 0; i != 5; ++i)
111 a_u32 *obj = A_VEC_REMOVE(a_u32, ctx, i);
112 if (obj) { printf("%" PRIu32 " ", *obj); }
114 for (i = 0; i != 5; ++i)
116 a_u32 *obj = A_VEC_REMOVE(a_u32, ctx, 0);
117 if (obj) { printf("%" PRIu32 " ", *obj); }
119 (void)putchar('\n');
120 a_vec_die(ctx, A_NULL);
123 ctx = a_vec_new(sizeof(a_u32));
124 for (i = 5; i != 10; ++i)
126 a_u32 *obj = A_VEC_PUSH_BACK(a_u32, ctx);
127 if (obj) { *obj = i; }
129 for (i = 5; i != 10; ++i)
131 a_u32 *obj = A_VEC_PULL_BACK(a_u32, ctx);
132 if (obj) { printf("%" PRIu32 " ", *obj); }
134 for (i = 0; i != 5; ++i)
136 a_u32 *obj = A_VEC_PUSH_FORE(a_u32, ctx);
137 if (obj) { *obj = i; }
139 for (i = 0; i != 5; ++i)
141 a_u32 *obj = A_VEC_PULL_FORE(a_u32, ctx);
142 if (obj) { printf("%" PRIu32 " ", *obj); }
144 (void)putchar('\n');
145 a_vec_die(ctx, A_NULL);
149 #include "a/str.h"
150 #include <time.h>
152 static int small(void const *lhs, void const *rhs)
154 int a = *a_int_(const *, lhs);
155 int b = *a_int_(const *, rhs);
156 return (a > b) - (a < b);
159 static int large(void const *lhs, void const *rhs)
161 int a = *a_int_(const *, lhs);
162 int b = *a_int_(const *, rhs);
163 return (a < b) - (a > b);
166 static int rand10(void)
168 return a_cast_s(int, rand() / a_cast_s(double, RAND_MAX) * 10); /* NOLINT */
171 static void test_sort(void)
173 unsigned int t = a_cast_s(unsigned int, time(A_NULL));
174 a_vec *ctx = a_vec_new(sizeof(int));
175 int i, x, *it, *at;
177 a_vec_make(ctx, 10, A_NULL);
179 x = -1;
180 srand(t);
181 A_VEC_FOREACH(int *, it, at, ctx)
183 *it = rand10();
184 printf("%i ", *it);
186 printf("-> ");
187 a_vec_sort(ctx, large);
188 A_VEC_FOREACH(int *, it, at, ctx)
190 if (x >= 0) { TEST_BUG(x >= *it); }
191 printf("%i ", *it);
192 x = *it;
194 (void)putchar('\n');
196 x = -1;
197 srand(t);
198 A_VEC_FOREACH(int *, it, at, ctx)
200 *it = rand10();
201 printf("%i ", *it);
203 printf("-> ");
204 a_vec_sort(ctx, small);
205 A_VEC_FOREACH(int *, it, at, ctx)
207 if (x >= 0) { TEST_BUG(x <= *it); }
208 printf("%i ", *it);
209 x = *it;
211 (void)putchar('\n');
213 x = -1;
214 srand(t);
215 a_vec_drop(ctx, A_NULL);
216 for (i = 0; i != 10; ++i)
218 int *obj = A_VEC_PUSH_FORE(int, ctx);
219 if (obj)
221 *obj = rand10();
222 printf("%i ", *obj);
223 a_vec_sort_fore(ctx, small);
226 printf("-> ");
227 A_VEC_FOREACH(int *, it, at, ctx)
229 if (x >= 0) { TEST_BUG(x <= *it); }
230 printf("%i ", *it);
231 x = *it;
233 (void)putchar('\n');
235 x = -1;
236 srand(t);
237 a_vec_drop(ctx, A_NULL);
238 for (i = 0; i != 10; ++i)
240 int *obj = A_VEC_PUSH_BACK(int, ctx);
241 if (obj)
243 *obj = rand10();
244 printf("%i ", *obj);
245 a_vec_sort_back(ctx, small);
248 printf("-> ");
249 A_VEC_FOREACH(int *, it, at, ctx)
251 if (x >= 0) { TEST_BUG(x <= *it); }
252 printf("%i ", *it);
253 x = *it;
255 (void)putchar('\n');
257 x = -1;
258 srand(t);
259 a_vec_drop(ctx, A_NULL);
260 for (i = 0; i != 10; ++i)
262 int key = rand10();
263 int *obj = A_VEC_PUSH_SORT(int, ctx, &key, small);
264 if (obj)
266 printf("%i ", key);
267 *obj = key;
270 printf("-> ");
271 A_VEC_FOREACH(int *, it, at, ctx)
273 if (x >= 0) { TEST_BUG(x <= *it); }
274 printf("%i ", *it);
275 x = *it;
277 (void)putchar('\n');
280 a_str *ok = a_str_new();
281 a_str *no = a_str_new();
282 a_str_puts(ok, "finding ");
283 a_str_puts(no, "missing ");
284 for (i = 0; i != 10; ++i)
286 int *obj = A_VEC_SEARCH(int, ctx, &i, small);
287 if (obj)
289 a_str_putf(ok, "%i ", *obj);
291 else
293 a_str_putf(no, "%i ", i);
296 printf("%s\n%s\n", a_str_ptr(ok), a_str_ptr(no));
297 a_str_die(ok);
298 a_str_die(no);
301 a_vec_die(ctx, A_NULL);
304 int main(int argc, char *argv[]) /* NOLINT(misc-definitions-in-headers) */
306 puts(A_FUNC);
307 test();
308 test_sort();
309 (void)argc;
310 (void)argv;
311 return 0;