change sizeof(a_cast) to sizeof(void*)
[liba.git] / test / vec.h
blob489061d77391283ca7ecd16fb3ffa07df28f1403
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));
21 a_vec_foreach(a_u64, it, ctx);
22 a_vec_foreach_reverse(a_u64, it, ctx);
23 for (a_u64 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 (a_u32 i = 0; i != 20; ++i)
31 a_u32 *obj = a_vec_push(a_u32, ctx);
32 if (obj) { *obj = i; }
34 for (a_u32 i = 0; i != 10; ++i)
36 (void)(a_vec_pull(a_u32, 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(i, ctx)
53 a_u32 *it = a_vec_at(a_u32, ctx, i);
54 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
55 if (it) { printf("%" PRIu32 " ", *it); }
57 putchar('\n');
58 a_vec_forenum_reverse(i, ctx)
60 a_u32 *it = a_vec_at(a_u32, ctx, i);
61 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
62 if (it) { printf("%" PRIu32 " ", *it); }
64 putchar('\n');
66 a_vec_foreach(a_u32, it, ctx)
68 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
69 TEST_BUG(sizeof(a_u32) == sizeof(*it));
70 printf("%" PRIu32 " ", *it);
72 putchar('\n');
73 a_vec_foreach_reverse(a_u32, it, ctx)
75 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
76 TEST_BUG(sizeof(a_u32) == sizeof(*it));
77 printf("%" PRIu32 " ", *it);
79 putchar('\n');
82 a_vec obj;
83 a_vec_copy(&obj, ctx, u32dup);
84 putchar('\n');
85 a_vec_dtor(ctx, A_NULL);
86 a_vec_move(ctx, &obj);
89 a_vec_die(ctx, dtor);
90 putchar('\n');
93 ctx = a_vec_new(sizeof(a_u32));
94 for (a_u32 i = 5; i != 10; ++i)
96 a_u32 *obj = a_vec_insert(a_u32, ctx, i);
97 if (obj) { *obj = i; }
99 for (a_u32 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, ctx)
106 printf("%" PRIu32 " ", *it);
108 putchar('\n');
109 for (a_u32 i = 0; i != 5; ++i)
111 a_u32 *obj = a_vec_remove(a_u32, ctx, i);
112 if (obj) { printf("%" PRIu32 " ", *obj); }
114 for (a_u32 i = 0; i != 5; ++i)
116 a_u32 *obj = a_vec_remove(a_u32, ctx, 0);
117 if (obj) { printf("%" PRIu32 " ", *obj); }
119 putchar('\n');
120 a_vec_die(ctx, A_NULL);
123 ctx = a_vec_new(sizeof(a_u32));
124 for (a_u32 i = 5; i != 10; ++i)
126 a_u32 *obj = a_vec_push_back(a_u32, ctx);
127 if (obj) { *obj = i; }
129 for (a_u32 i = 5; i != 10; ++i)
131 a_u32 *obj = a_vec_pull_back(a_u32, ctx);
132 if (obj) { printf("%" PRIu32 " ", *obj); }
134 for (a_u32 i = 0; i != 5; ++i)
136 a_u32 *obj = a_vec_push_fore(a_u32, ctx);
137 if (obj) { *obj = i; }
139 for (a_u32 i = 0; i != 5; ++i)
141 a_u32 *obj = a_vec_pull_fore(a_u32, ctx);
142 if (obj) { printf("%" PRIu32 " ", *obj); }
144 putchar('\n');
145 a_vec_die(ctx, A_NULL);
149 #include "a/str.h"
150 #include <time.h>
152 static int cmp(void const *lhs, void const *rhs)
154 return *a_int_(const *, lhs) - *a_int_(const *, rhs);
157 static int cmpr(void const *lhs, void const *rhs)
159 return *a_int_(const *, rhs) - *a_int_(const *, lhs);
162 static void test_sort(void)
164 unsigned int t = a_cast_s(unsigned int, time(A_NULL));
165 a_vec *ctx = a_vec_new(sizeof(int));
167 a_vec_make(ctx, 10, A_NULL);
169 srand(t);
170 a_vec_foreach(int, it, ctx)
172 *it = rand() % 10; // NOLINT
173 printf("%i ", *it);
175 printf("-> ");
176 a_vec_sort(ctx, cmpr);
177 a_vec_foreach(int, it, ctx)
179 printf("%i ", *it);
181 putchar('\n');
183 srand(t);
184 a_vec_foreach(int, it, ctx)
186 *it = rand() % 10; // NOLINT
187 printf("%i ", *it);
189 printf("-> ");
190 a_vec_sort(ctx, cmp);
191 a_vec_foreach(int, it, ctx)
193 printf("%i ", *it);
195 putchar('\n');
197 srand(t);
198 a_vec_drop(ctx, A_NULL);
199 for (int i = 0; i != 10; ++i)
201 int *obj = a_vec_push_fore(int, ctx);
202 if (obj)
204 *obj = rand() % 10; // NOLINT
205 printf("%i ", *obj);
206 a_vec_sort_fore(ctx, cmp);
209 printf("-> ");
210 a_vec_foreach(int, it, ctx)
212 printf("%i ", *it);
214 putchar('\n');
216 srand(t);
217 a_vec_drop(ctx, A_NULL);
218 for (int i = 0; i != 10; ++i)
220 int *obj = a_vec_push_back(int, ctx);
221 if (obj)
223 *obj = rand() % 10; // NOLINT
224 printf("%i ", *obj);
225 a_vec_sort_back(ctx, cmp);
228 printf("-> ");
229 a_vec_foreach(int, it, ctx)
231 printf("%i ", *it);
233 putchar('\n');
236 a_str *ok = a_str_new();
237 a_str *no = a_str_new();
238 a_str_puts(ok, "finding ");
239 a_str_puts(no, "missing ");
240 for (int i = 0; i != 10; ++i)
242 int *obj = a_vec_search(int, ctx, &i, cmp);
243 if (obj)
245 a_str_putf(ok, "%i ", *obj);
247 else
249 a_str_putf(no, "%i ", i);
252 printf("%s\n%s\n", a_str_ptr(ok), a_str_ptr(no));
253 a_str_die(ok);
254 a_str_die(no);
257 a_vec_die(ctx, A_NULL);
260 int main(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
262 (void)(argc);
263 (void)(argv);
264 printf("%s\n", A_FUNC);
265 test();
266 test_sort();
267 return 0;