release 0.1.13
[liba.git] / test / vec.h
blob9fd0dcc0c4a6e05e0ffc3700ece4811bfc42b432
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 cmp(void const *lhs, void const *rhs)
153 return *a_int_(const *, lhs) - *a_int_(const *, rhs);
156 static int cmpr(void const *lhs, void const *rhs)
158 return *a_int_(const *, rhs) - *a_int_(const *, lhs);
161 static void test_sort(void)
163 unsigned int t = a_cast_s(unsigned int, time(A_NULL));
164 a_vec *ctx = a_vec_new(sizeof(int));
166 a_vec_make(ctx, 10, A_NULL);
168 srand(t);
169 a_vec_foreach(int, it, ctx)
171 *it = rand() % 10; // NOLINT
172 printf("%i ", *it);
174 printf("-> ");
175 a_vec_sort(ctx, cmpr);
176 a_vec_foreach(int, it, ctx)
178 printf("%i ", *it);
180 putchar('\n');
182 srand(t);
183 a_vec_foreach(int, it, ctx)
185 *it = rand() % 10; // NOLINT
186 printf("%i ", *it);
188 printf("-> ");
189 a_vec_sort(ctx, cmp);
190 a_vec_foreach(int, it, ctx)
192 printf("%i ", *it);
194 putchar('\n');
196 srand(t);
197 a_vec_drop(ctx, A_NULL);
198 for (int i = 0; i != 10; ++i)
200 int *obj = A_VEC_PUSH_FORE(int, ctx);
201 if (obj)
203 *obj = rand() % 10; // NOLINT
204 printf("%i ", *obj);
205 a_vec_sort_fore(ctx, cmp);
208 printf("-> ");
209 a_vec_foreach(int, it, ctx)
211 printf("%i ", *it);
213 putchar('\n');
215 srand(t);
216 a_vec_drop(ctx, A_NULL);
217 for (int i = 0; i != 10; ++i)
219 int *obj = A_VEC_PUSH_BACK(int, ctx);
220 if (obj)
222 *obj = rand() % 10; // NOLINT
223 printf("%i ", *obj);
224 a_vec_sort_back(ctx, cmp);
227 printf("-> ");
228 a_vec_foreach(int, it, ctx)
230 printf("%i ", *it);
232 putchar('\n');
235 a_str *ok = a_str_new();
236 a_str *no = a_str_new();
237 a_str_puts(ok, "finding ");
238 a_str_puts(no, "missing ");
239 for (int i = 0; i != 10; ++i)
241 int *obj = A_VEC_SEARCH(int, ctx, &i, cmp);
242 if (obj)
244 a_str_putf(ok, "%i ", *obj);
246 else
248 a_str_putf(no, "%i ", i);
251 printf("%s\n%s\n", a_str_ptr(ok), a_str_ptr(no));
252 a_str_die(ok);
253 a_str_die(no);
256 a_vec_die(ctx, A_NULL);
259 int main(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
261 (void)argc;
262 (void)argv;
263 printf("%s\n", A_FUNC);
264 test();
265 test_sort();
266 return 0;