remove gcc for MacOS in conan.yml
[liba.git] / test / vec.h
blobeb99ca0d69c2188b99f9a20b7bb152b85cbc6c83
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_vec *ctx = a_vec_new(sizeof(a_u64));
22 for (a_u64 i = 0; i != 10; ++i)
24 a_u64 *obj = A_VEC_PUSH(a_u64, ctx);
25 if (obj) { *obj = i; }
27 a_vec_edit(ctx, sizeof(a_u32), A_NULL);
28 for (a_u32 i = 0; i != 20; ++i)
30 a_u32 *obj = A_VEC_PUSH(a_u32, ctx);
31 if (obj) { *obj = i; }
33 for (a_u32 i = 0; i != 10; ++i)
35 a_vec_pull(ctx);
39 a_u8 *end = A_VEC_END(a_u8, ctx);
40 a_u8 *top = A_VEC_TOP(a_u8, ctx);
41 TEST_BUG(a_vec_siz(ctx) == a_size_c(end - top));
44 a_vec_swap(ctx, 0, 0);
45 a_vec_swap(ctx, 0, ~0U);
46 a_vec_swap(ctx, ~0U, 0);
47 a_vec_swap(ctx, 4, 6);
48 a_vec_swap(ctx, 6, 4);
50 a_vec_forenum(i, ctx)
52 a_u32 *it = A_VEC_AT(a_u32, ctx, i);
53 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
54 if (it) { printf("%" PRIu32 " ", *it); }
56 putchar('\n');
57 a_vec_forenum_reverse(i, ctx)
59 a_u32 *it = A_VEC_AT(a_u32, ctx, i);
60 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
61 if (it) { printf("%" PRIu32 " ", *it); }
63 putchar('\n');
65 a_vec_foreach(a_u32, *, it, ctx)
67 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
68 TEST_BUG(sizeof(a_u32) == sizeof(*it));
69 printf("%" PRIu32 " ", *it);
71 putchar('\n');
72 a_vec_foreach_reverse(a_u32, *, it, ctx)
74 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
75 TEST_BUG(sizeof(a_u32) == sizeof(*it));
76 printf("%" PRIu32 " ", *it);
78 putchar('\n');
81 a_vec obj;
82 a_vec_copy(&obj, ctx, u32dup);
83 putchar('\n');
84 a_vec_dtor(ctx, A_NULL);
85 a_vec_move(ctx, &obj);
88 a_vec_die(ctx, dtor);
89 putchar('\n');
92 ctx = a_vec_new(sizeof(a_u32));
93 for (a_u32 i = 5; i != 10; ++i)
95 a_u32 *obj = A_VEC_INSERT(a_u32, ctx, i);
96 if (obj) { *obj = i; }
98 for (a_u32 i = 0; i != 5; ++i)
100 a_u32 *obj = A_VEC_INSERT(a_u32, ctx, i);
101 if (obj) { *obj = i; }
103 a_vec_foreach(a_u32, *, it, ctx)
105 printf("%" PRIu32 " ", *it);
107 putchar('\n');
108 for (a_u32 i = 0; i != 5; ++i)
110 a_u32 *obj = A_VEC_REMOVE(a_u32, ctx, i);
111 if (obj) { printf("%" PRIu32 " ", *obj); }
113 for (a_u32 i = 0; i != 5; ++i)
115 a_u32 *obj = A_VEC_REMOVE(a_u32, ctx, 0);
116 if (obj) { printf("%" PRIu32 " ", *obj); }
118 putchar('\n');
119 a_vec_die(ctx, A_NULL);
122 ctx = a_vec_new(sizeof(a_u32));
123 for (a_u32 i = 5; i != 10; ++i)
125 a_u32 *obj = A_VEC_PUSH_BACK(a_u32, ctx);
126 if (obj) { *obj = i; }
128 for (a_u32 i = 5; i != 10; ++i)
130 a_u32 *obj = A_VEC_PULL_BACK(a_u32, ctx);
131 if (obj) { printf("%" PRIu32 " ", *obj); }
133 for (a_u32 i = 0; i != 5; ++i)
135 a_u32 *obj = A_VEC_PUSH_FORE(a_u32, ctx);
136 if (obj) { *obj = i; }
138 for (a_u32 i = 0; i != 5; ++i)
140 a_u32 *obj = A_VEC_PULL_FORE(a_u32, ctx);
141 if (obj) { printf("%" PRIu32 " ", *obj); }
143 putchar('\n');
144 a_vec_die(ctx, A_NULL);
148 #include "a/str.h"
149 #include <time.h>
151 static int small(void const *lhs, void const *rhs)
153 int a = *a_int_(const *, lhs);
154 int b = *a_int_(const *, rhs);
155 return (a > b) - (a < b);
158 static int large(void const *lhs, void const *rhs)
160 int a = *a_int_(const *, lhs);
161 int b = *a_int_(const *, rhs);
162 return (a < b) - (a > b);
165 static int rand10(void)
167 return a_cast_s(int, rand() / a_cast_s(double, RAND_MAX) * 10); // NOLINT
170 static void test_sort(void)
172 unsigned int t = a_cast_s(unsigned int, time(A_NULL));
173 a_vec *ctx = a_vec_new(sizeof(int));
174 int x;
176 a_vec_make(ctx, 10, A_NULL);
178 x = -1;
179 srand(t);
180 a_vec_foreach(int, *, it, ctx)
182 *it = rand10();
183 printf("%i ", *it);
185 printf("-> ");
186 a_vec_sort(ctx, large);
187 a_vec_foreach(int, *, it, ctx)
189 if (x >= 0) { TEST_BUG(x >= *it); }
190 printf("%i ", *it);
191 x = *it;
193 putchar('\n');
195 x = -1;
196 srand(t);
197 a_vec_foreach(int, *, it, ctx)
199 *it = rand10();
200 printf("%i ", *it);
202 printf("-> ");
203 a_vec_sort(ctx, small);
204 a_vec_foreach(int, *, it, ctx)
206 if (x >= 0) { TEST_BUG(x <= *it); }
207 printf("%i ", *it);
208 x = *it;
210 putchar('\n');
212 x = -1;
213 srand(t);
214 a_vec_drop(ctx, A_NULL);
215 for (int i = 0; i != 10; ++i)
217 int *obj = A_VEC_PUSH_FORE(int, ctx);
218 if (obj)
220 *obj = rand10();
221 printf("%i ", *obj);
222 a_vec_sort_fore(ctx, small);
225 printf("-> ");
226 a_vec_foreach(int, *, it, ctx)
228 if (x >= 0) { TEST_BUG(x <= *it); }
229 printf("%i ", *it);
230 x = *it;
232 putchar('\n');
234 x = -1;
235 srand(t);
236 a_vec_drop(ctx, A_NULL);
237 for (int i = 0; i != 10; ++i)
239 int *obj = A_VEC_PUSH_BACK(int, ctx);
240 if (obj)
242 *obj = rand10();
243 printf("%i ", *obj);
244 a_vec_sort_back(ctx, small);
247 printf("-> ");
248 a_vec_foreach(int, *, it, ctx)
250 if (x >= 0) { TEST_BUG(x <= *it); }
251 printf("%i ", *it);
252 x = *it;
254 putchar('\n');
256 x = -1;
257 srand(t);
258 a_vec_drop(ctx, A_NULL);
259 for (int i = 0; i != 10; ++i)
261 int key = rand10();
262 int *obj = A_VEC_PUSH_SORT(int, ctx, &key, small);
263 if (obj)
265 printf("%i ", key);
266 *obj = key;
269 printf("-> ");
270 a_vec_foreach(int, *, it, ctx)
272 if (x >= 0) { TEST_BUG(x <= *it); }
273 printf("%i ", *it);
274 x = *it;
276 putchar('\n');
279 a_str *ok = a_str_new();
280 a_str *no = a_str_new();
281 a_str_puts(ok, "finding ");
282 a_str_puts(no, "missing ");
283 for (int i = 0; i != 10; ++i)
285 int *obj = A_VEC_SEARCH(int, ctx, &i, small);
286 if (obj)
288 a_str_putf(ok, "%i ", *obj);
290 else
292 a_str_putf(no, "%i ", i);
295 printf("%s\n%s\n", a_str_ptr(ok), a_str_ptr(no));
296 a_str_die(ok);
297 a_str_die(no);
300 a_vec_die(ctx, A_NULL);
303 int main(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
305 (void)argc;
306 (void)argv;
307 printf("%s\n", A_FUNC);
308 test();
309 test_sort();
310 return 0;