Create userdata without user values in Lua 5.4
[liba.git] / test / vec.h
blob3d38160d6bc09ecc5a26736142fef2b878f79dae
1 #define MAIN_(x) A_CAST_2(x, _vec)
2 #include "test.h"
3 #include "a/vec.h"
5 static void dtor(void *ptr)
7 a_u32_t *obj = a_u32_c(*, ptr);
8 printf("%" PRIu32 " ", *obj);
11 static int u32dup(void *dst, void const *src)
13 *a_u32_c(*, dst) = *a_u32_c(const *, src);
14 printf("%" PRIu32 " ", *a_u32_c(const *, src));
15 return 0;
18 static void test(void)
20 a_vec_s *ctx = a_vec_new(sizeof(a_u64_t));
21 a_vec_foreach(a_u64_t, it, ctx);
22 a_vec_foreach_reverse(a_u64_t, it, ctx);
23 for (a_u64_t i = 0; i != 10; ++i)
25 a_u64_t *obj = a_vec_push(a_u64_t, ctx);
26 if (obj) { *obj = i; }
28 a_vec_edit(ctx, sizeof(a_u32_t), A_NULL);
29 for (a_u32_t i = 0; i != 20; ++i)
31 a_u32_t *obj = a_vec_push(a_u32_t, ctx);
32 if (obj) { *obj = i; }
34 for (a_u32_t i = 0; i != 10; ++i)
36 (void)(a_vec_pull(a_u32_t, ctx));
40 a_u8_t *end = a_vec_end(a_u8_t, ctx);
41 a_u8_t *top = a_vec_top(a_u8_t, 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_t *it = a_vec_at(a_u32_t, 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_t *it = a_vec_at(a_u32_t, 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_t, it, ctx)
68 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
69 TEST_BUG(sizeof(a_u32_t) == sizeof(*it));
70 printf("%" PRIu32 " ", *it);
72 putchar('\n');
73 a_vec_foreach_reverse(a_u32_t, it, ctx)
75 TEST_BUG(a_vec_siz(ctx) == sizeof(*it));
76 TEST_BUG(sizeof(a_u32_t) == sizeof(*it));
77 printf("%" PRIu32 " ", *it);
79 putchar('\n');
82 a_vec_s 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_t));
94 for (a_u32_t i = 5; i != 10; ++i)
96 a_u32_t *obj = a_vec_insert(a_u32_t, ctx, i);
97 if (obj) { *obj = i; }
99 for (a_u32_t i = 0; i != 5; ++i)
101 a_u32_t *obj = a_vec_insert(a_u32_t, ctx, i);
102 if (obj) { *obj = i; }
104 a_vec_foreach(a_u32_t, it, ctx)
106 printf("%" PRIu32 " ", *it);
108 putchar('\n');
109 for (a_u32_t i = 0; i != 5; ++i)
111 a_u32_t *obj = a_vec_remove(a_u32_t, ctx, i);
112 if (obj) { printf("%" PRIu32 " ", *obj); }
114 for (a_u32_t i = 0; i != 5; ++i)
116 a_u32_t *obj = a_vec_remove(a_u32_t, 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_t));
124 for (a_u32_t i = 5; i != 10; ++i)
126 a_u32_t *obj = a_vec_push_back(a_u32_t, ctx);
127 if (obj) { *obj = i; }
129 for (a_u32_t i = 5; i != 10; ++i)
131 a_u32_t *obj = a_vec_pull_back(a_u32_t, ctx);
132 if (obj) { printf("%" PRIu32 " ", *obj); }
134 for (a_u32_t i = 0; i != 5; ++i)
136 a_u32_t *obj = a_vec_push_fore(a_u32_t, ctx);
137 if (obj) { *obj = i; }
139 for (a_u32_t i = 0; i != 5; ++i)
141 a_u32_t *obj = a_vec_pull_fore(a_u32_t, 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_c(const *, lhs) - *a_int_c(const *, rhs);
157 static int cmpr(void const *lhs, void const *rhs)
159 return *a_int_c(const *, rhs) - *a_int_c(const *, lhs);
162 static void test_sort(void)
164 unsigned int t = a_cast_s(unsigned int, time(A_NULL));
165 a_vec_s *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_s *ok = a_str_new();
237 a_str_s *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;