rename a_float_push,roll to a_float_push_,roll_ and create new a_float_push,roll
[liba.git] / test / a.h
blobcf354d29f56b8e0470b1128d4bb3c1477e0602c1
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, 8, 9};
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');
119 a_float_push(array, A_LEN(array), -1);
121 for (a_size i = 0; i < A_LEN(array); ++i)
123 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
126 a_float cache[] = {-1, -2};
127 a_float_push_(array, A_LEN(array), cache, A_LEN(cache));
129 for (a_size i = 0; i < A_LEN(array); ++i)
131 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
134 a_float cache[] = {-1, -2, -3, -4};
135 a_float_push_(array, A_LEN(array), cache, A_LEN(cache));
137 for (a_size i = 0; i < A_LEN(array); ++i)
139 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
142 a_float cache[] = {-1, -2, -3, -4, -5, -6, -7, -8};
143 a_float_push_(array, A_LEN(array), cache, A_LEN(cache));
145 for (a_size i = 0; i < A_LEN(array); ++i)
147 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
150 a_float cache[] = {-0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
151 a_float_push_(array, A_LEN(array), cache, A_LEN(cache));
153 for (a_size i = 0; i < A_LEN(array); ++i)
155 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
157 (void)argc;
158 (void)argv;
161 static void test_roll(int argc, char *argv[])
163 a_float array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
164 a_float shift[16];
165 for (a_size i = 0; i < A_LEN(array); ++i)
167 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
169 a_float_roll(array, A_LEN(array));
170 for (a_size i = 0; i < A_LEN(array); ++i)
172 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
174 a_float_roll_(array, A_LEN(array), shift, 2);
175 for (a_size i = 0; i < A_LEN(array); ++i)
177 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
179 a_float_roll_(array, A_LEN(array), shift, 4);
180 for (a_size i = 0; i < A_LEN(array); ++i)
182 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
184 a_float_roll_(array, A_LEN(array), shift, 8);
185 for (a_size i = 0; i < A_LEN(array); ++i)
187 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
189 a_float_roll_(array, A_LEN(array), shift, 15);
190 for (a_size i = 0; i < A_LEN(array); ++i)
192 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
194 (void)argc;
195 (void)argv;
198 static void test_mean(int argc, char *argv[])
200 a_size const n = a_cast_s(a_size, argc);
201 a_float *p = a_new(a_float, A_NULL, n);
202 for (a_size i = 0; i < n; ++i)
204 char *endptr;
205 p[i] = strtonum(argv[i], &endptr);
208 debug("{");
209 for (a_size i = 0; i < n; ++i)
211 debug("%c" A_FLOAT_PRI("", "g"), i ? ',' : 0, p[i]);
213 debug("}:" A_FLOAT_PRI("", "g\n"), a_float_mean(p, n));
215 a_die(p);
218 static void test_sum(int argc, char *argv[])
220 a_size const n = a_cast_s(a_size, argc);
221 a_float *p = a_new(a_float, A_NULL, n);
222 for (a_size i = 0; i < n; ++i)
224 char *endptr;
225 p[i] = strtonum(argv[i], &endptr);
228 debug("{");
229 for (a_size i = 0; i < n; ++i)
231 debug("%c" A_FLOAT_PRI("", "g"), i ? ',' : 0, p[i]);
233 debug("}:" A_FLOAT_PRI("", "g,") A_FLOAT_PRI("", "g,") A_FLOAT_PRI("", "g\n"),
234 a_float_sum(p, n), a_float_sum1(p, n), a_float_sum2(p, n));
236 a_die(p);
239 static void test_hash_bkdr(int argc, char *argv[])
241 for (int idx = 1; idx < argc; ++idx)
243 debug("case 0x%08" PRIX32 ": // %s\n break;\n", a_hash_bkdr(argv[idx], 0), argv[idx]);
247 static void test_hash_sdbm(int argc, char *argv[])
249 for (int idx = 1; idx < argc; ++idx)
251 debug("case 0x%08" PRIX32 ": // %s\n break;\n", a_hash_sdbm(argv[idx], 0), argv[idx]);
255 int main(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
257 test_sq();
258 test_min();
259 test_max();
260 test_abs();
261 test_sgn();
262 test_sat();
263 if (argc < 2)
265 test_for(argc, argv);
266 test_swap(argc, argv);
267 test_push(argc, argv);
268 test_roll(argc, argv);
269 test_hash_bkdr(argc, argv);
270 test_hash_sdbm(argc, argv);
271 test_sum(argc - 1, argv + 1);
272 test_mean(argc - 1, argv + 1);
273 return 0;
275 switch (a_hash_bkdr(argv[1], 0))
277 case 0x001AEED5: // for
278 test_for(argc - 1, argv + 1);
279 break;
280 case 0x001E5957: // sum
281 test_sum(argc - 2, argv + 2);
282 break;
283 case 0x0F8837E3: // swap
284 test_swap(argc - 1, argv + 1);
285 break;
286 case 0x0F20D22E: // push
287 test_push(argc - 1, argv + 1);
288 break;
289 case 0x0F63D79D: // roll
290 test_roll(argc - 1, argv + 1);
291 break;
292 case 0x0EB5AF9D: // mean
293 test_mean(argc - 2, argv + 2);
294 break;
295 case 0x0E0928A2: // hash
296 case 0x57614C8C: // hash_bkdr
297 test_hash_bkdr(argc - 1, argv + 1);
298 break;
299 case 0x59A69D8D: // hash_sdbm
300 test_hash_sdbm(argc - 1, argv + 1);
301 break;
302 default:
303 debug("for\n");
304 debug("sum\n");
305 debug("swap\n");
306 debug("save\n");
307 debug("roll\n");
308 debug("mean\n");
309 debug("hash_bkdr\n");
310 debug("hash_sdbm\n");
312 return 0;