release 0.1.15
[liba.git] / test / a.h
blob74414bc608d2058de61e4c9c754a6939b894cd5c
1 #define MAIN(x) a##x
2 #include "test.h"
3 #include <stdlib.h>
5 static void test_sq(void)
7 A_BUILD_ASSERT(A_SQ(+2) == 4);
8 A_BUILD_ASSERT(A_SQ(-2) == 4);
11 static void test_abs(void)
13 A_BUILD_ASSERT(A_ABS(+1) > 0);
14 A_BUILD_ASSERT(A_ABS(-1) > 0);
15 A_BUILD_ASSERT(A_ABS_(+1, -1) > 0);
16 A_BUILD_ASSERT(A_ABS_(-1, +1) > 0);
19 static void test_min(void)
21 A_BUILD_ASSERT(A_MIN(0, 1) == 0);
22 A_BUILD_ASSERT(A_MIN(1, 0) == 0);
25 static void test_max(void)
27 A_BUILD_ASSERT(A_MAX(0, 1) == 1);
28 A_BUILD_ASSERT(A_MAX(1, 0) == 1);
31 static void test_sgn(void)
33 A_BUILD_ASSERT(A_SGN(0) == 0);
34 A_BUILD_ASSERT(A_SGN(+10) == +1);
35 A_BUILD_ASSERT(A_SGN(-10) == -1);
36 A_BUILD_ASSERT(A_SGN_(0, 0) == 0);
37 A_BUILD_ASSERT(A_SGN_(+1, -1) == +1);
38 A_BUILD_ASSERT(A_SGN_(-1, +1) == -1);
41 static void test_sat(void)
43 A_BUILD_ASSERT(A_SAT(0, -10, +10) == 0);
44 A_BUILD_ASSERT(A_SAT(+100, -10, +10) <= +10);
45 A_BUILD_ASSERT(A_SAT(-100, -10, +10) >= -10);
48 static void test_for(int argc, char *argv[])
50 unsigned int n = 10;
51 if (argc > 1)
53 char *endptr = A_NULL;
54 unsigned long l = strtoul(argv[1], &endptr, 0);
55 if (l > n) { n = a_cast_s(unsigned int, l); }
58 int *p = a_new(int, A_NULL, n);
59 int *d = p + n;
60 A_ASSUME(p);
62 a_forenum(unsigned int, i, n)
64 p[i] = a_cast_s(int, i);
65 debug("%u ", i);
67 debug("\n");
69 a_foreach(int, *, it, p, n) { debug("%i ", *it); }
70 debug("\n");
72 a_iterate(int, *, it, p, d) { debug("%i ", *it); }
73 debug("\n");
75 a_forenum_reverse(unsigned int, i, n)
77 p[i] = a_cast_s(int, i);
78 debug("%u ", i);
80 debug("\n");
82 a_foreach_reverse(int, *, it, p, n) { debug("%i ", *it); }
83 debug("\n");
85 a_iterate_reverse(int, *, it, p, d) { debug("%i ", *it); }
86 debug("\n");
88 a_die(p);
91 static void test_swap(int argc, char *argv[])
93 a_u64 lhs = 0;
94 a_u64 rhs = A_U64_MAX;
95 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
96 a_swap(&lhs, &rhs, 1);
97 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
98 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
99 a_swap(&lhs, &rhs, 2);
100 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
101 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
102 a_swap(&lhs, &rhs, 4);
103 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
104 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
105 a_swap(&lhs, &rhs, 8);
106 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
107 (void)argc;
108 (void)argv;
111 static void test_push(int argc, char *argv[])
113 a_float array[] = {0, 1, 2, 3, 4, 5, 6, 7};
114 for (a_size i = 0; i < A_LEN(array); ++i)
116 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
118 a_float_push_fore(array, A_LEN(array), -1);
119 for (a_size i = 0; i < A_LEN(array); ++i)
121 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
123 a_float_push_back(array, A_LEN(array), -1);
124 for (a_size i = 0; i < A_LEN(array); ++i)
126 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
129 a_float cache[] = {-1, -2};
130 a_float_push_fore_(array, A_LEN(array), cache, A_LEN(cache));
132 for (a_size i = 0; i < A_LEN(array); ++i)
134 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
137 a_float cache[] = {-2, -1};
138 a_float_push_back_(array, A_LEN(array), cache, A_LEN(cache));
140 for (a_size i = 0; i < A_LEN(array); ++i)
142 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
145 a_float cache[] = {-0, -1, -2, -3, -4, -5, -6, -7, -8, -9};
146 a_float_push_fore_(array, A_LEN(array), cache, A_LEN(cache));
148 for (a_size i = 0; i < A_LEN(array); ++i)
150 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
153 a_float cache[] = {-9, -8, -7, -6, -5, -4, -3, -2, -1, -0};
154 a_float_push_fore_(array, A_LEN(array), cache, A_LEN(cache));
156 for (a_size i = 0; i < A_LEN(array); ++i)
158 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
160 (void)argc;
161 (void)argv;
164 static void test_roll(int argc, char *argv[])
166 a_float array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
167 a_float shift[16];
168 for (a_size i = 0; i < A_LEN(array); ++i)
170 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
172 a_float_roll_fore(array, A_LEN(array));
173 for (a_size i = 0; i < A_LEN(array); ++i)
175 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
177 a_float_roll_back(array, A_LEN(array));
178 for (a_size i = 0; i < A_LEN(array); ++i)
180 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
182 a_float_roll_fore_(array, A_LEN(array), shift, 2);
183 for (a_size i = 0; i < A_LEN(array); ++i)
185 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
187 a_float_roll_back_(array, A_LEN(array), shift, 2);
188 for (a_size i = 0; i < A_LEN(array); ++i)
190 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
192 a_float_roll_fore_(array, A_LEN(array), shift, 15);
193 for (a_size i = 0; i < A_LEN(array); ++i)
195 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
197 a_float_roll_back_(array, A_LEN(array), shift, 15);
198 for (a_size i = 0; i < A_LEN(array); ++i)
200 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
202 (void)argc;
203 (void)argv;
206 static void test_mean(int argc, char *argv[])
208 a_size const n = a_cast_s(a_size, argc);
209 a_float *p = a_new(a_float, A_NULL, n);
210 for (a_size i = 0; i < n; ++i)
212 char *endptr;
213 p[i] = strtonum(argv[i], &endptr);
216 debug("{");
217 for (a_size i = 0; i < n; ++i)
219 debug("%c" A_FLOAT_PRI("", "g"), i ? ',' : 0, p[i]);
221 debug("}:" A_FLOAT_PRI("", "g\n"), a_float_mean(p, n));
223 a_die(p);
226 static void test_sum(int argc, char *argv[])
228 a_size const n = a_cast_s(a_size, argc);
229 a_float *p = a_new(a_float, A_NULL, n);
230 for (a_size i = 0; i < n; ++i)
232 char *endptr;
233 p[i] = strtonum(argv[i], &endptr);
236 debug("{");
237 for (a_size i = 0; i < n; ++i)
239 debug("%c" A_FLOAT_PRI("", "g"), i ? ',' : 0, p[i]);
241 debug("}:" A_FLOAT_PRI("", "g,") A_FLOAT_PRI("", "g,") A_FLOAT_PRI("", "g\n"),
242 a_float_sum(p, n), a_float_sum1(p, n), a_float_sum2(p, n));
244 a_die(p);
247 static void test_hash_bkdr(int argc, char *argv[])
249 for (int idx = 1; idx < argc; ++idx)
251 debug("case 0x%08" PRIX32 ": // %s\n break;\n", a_hash_bkdr(argv[idx], 0), argv[idx]);
255 static void test_hash_sdbm(int argc, char *argv[])
257 for (int idx = 1; idx < argc; ++idx)
259 debug("case 0x%08" PRIX32 ": // %s\n break;\n", a_hash_sdbm(argv[idx], 0), argv[idx]);
263 int main(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
265 test_sq();
266 test_min();
267 test_max();
268 test_abs();
269 test_sgn();
270 test_sat();
271 if (argc < 2)
273 test_for(argc, argv);
274 test_swap(argc, argv);
275 test_push(argc, argv);
276 test_roll(argc, argv);
277 test_hash_bkdr(argc, argv);
278 test_hash_sdbm(argc, argv);
279 test_sum(argc - 1, argv + 1);
280 test_mean(argc - 1, argv + 1);
281 return 0;
283 switch (a_hash_bkdr(argv[1], 0))
285 case 0x001AEED5: // for
286 test_for(argc - 1, argv + 1);
287 break;
288 case 0x001E5957: // sum
289 test_sum(argc - 2, argv + 2);
290 break;
291 case 0x0F8837E3: // swap
292 test_swap(argc - 1, argv + 1);
293 break;
294 case 0x0F20D22E: // push
295 test_push(argc - 1, argv + 1);
296 break;
297 case 0x0F63D79D: // roll
298 test_roll(argc - 1, argv + 1);
299 break;
300 case 0x0EB5AF9D: // mean
301 test_mean(argc - 2, argv + 2);
302 break;
303 case 0x0E0928A2: // hash
304 case 0x57614C8C: // hash_bkdr
305 test_hash_bkdr(argc - 1, argv + 1);
306 break;
307 case 0x59A69D8D: // hash_sdbm
308 test_hash_sdbm(argc - 1, argv + 1);
309 break;
310 default:
311 debug("for\n");
312 debug("sum\n");
313 debug("swap\n");
314 debug("save\n");
315 debug("roll\n");
316 debug("mean\n");
317 debug("hash_bkdr\n");
318 debug("hash_sdbm\n");
320 return 0;