release 0.1.13
[liba.git] / test / a.h
blob85d768cd8beefd2e0c20361da72b66f6300b9523
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(~0) > 0);
16 static void test_min(void)
18 A_BUILD_ASSERT(A_MIN(0, 1) == 0);
19 A_BUILD_ASSERT(A_MIN(1, 0) == 0);
22 static void test_max(void)
24 A_BUILD_ASSERT(A_MAX(0, 1) == 1);
25 A_BUILD_ASSERT(A_MAX(1, 0) == 1);
28 static void test_sgn(void)
30 A_BUILD_ASSERT(A_SGN(0) == 0);
31 A_BUILD_ASSERT(A_SGN(+10) == +1);
32 A_BUILD_ASSERT(A_SGN(-10) == -1);
35 static void test_sat(void)
37 A_BUILD_ASSERT(A_SAT(0, -10, +10) == 0);
38 A_BUILD_ASSERT(A_SAT(+100, -10, +10) <= +10);
39 A_BUILD_ASSERT(A_SAT(-100, -10, +10) >= -10);
42 static void test_for(int argc, char *argv[])
44 unsigned int n = 10;
45 if (argc > 1)
47 char *endptr = A_NULL;
48 unsigned long l = strtoul(argv[1], &endptr, 0);
49 if (l > n) { n = a_cast_s(unsigned int, l); }
52 int *p = a_new(int, A_NULL, n);
53 int *d = p + n;
54 A_ASSUME(p);
56 a_forenum(unsigned int, i, n)
58 p[i] = a_cast_s(int, i);
59 debug("%u ", i);
61 debug("\n");
63 a_foreach(int, it, p, n) { debug("%i ", *it); }
64 debug("\n");
66 a_iterate(int, it, p, d) { debug("%i ", *it); }
67 debug("\n");
69 a_forenum_reverse(unsigned int, i, n)
71 p[i] = a_cast_s(int, i);
72 debug("%u ", i);
74 debug("\n");
76 a_foreach_reverse(int, it, p, n) { debug("%i ", *it); }
77 debug("\n");
79 a_iterate_reverse(int, it, p, d) { debug("%i ", *it); }
80 debug("\n");
82 a_die(p);
85 static void test_swap(int argc, char *argv[])
87 a_u64 lhs = 0;
88 a_u64 rhs = A_U64_MAX;
89 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
90 a_swap(&lhs, &rhs, 1);
91 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
92 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
93 a_swap(&lhs, &rhs, 2);
94 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
95 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
96 a_swap(&lhs, &rhs, 4);
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, 8);
100 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
101 (void)argc;
102 (void)argv;
105 static void test_push(int argc, char *argv[])
107 a_float array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
108 for (a_size i = 0; i < A_LEN(array); ++i)
110 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
113 a_float cache[] = {-1};
114 a_float_push(array, A_LEN(array), cache, A_LEN(cache));
116 for (a_size i = 0; i < A_LEN(array); ++i)
118 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
121 a_float cache[] = {-1, -2};
122 a_float_push(array, A_LEN(array), cache, A_LEN(cache));
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, -3, -4};
130 a_float_push(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[] = {-1, -2, -3, -4, -5, -6, -7, -8};
138 a_float_push(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, -10};
146 a_float_push(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');
152 (void)argc;
153 (void)argv;
156 static void test_roll(int argc, char *argv[])
158 a_float array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
159 a_float shift[16];
160 for (a_size i = 0; i < A_LEN(array); ++i)
162 debug(A_FLOAT_PRI(, "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
164 a_float_roll(array, A_LEN(array), shift, 1);
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), shift, 2);
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, 4);
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, 8);
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, 15);
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 (void)argc;
190 (void)argv;
193 static void test_hash_bkdr(int argc, char *argv[])
195 for (int idx = 1; idx < argc; ++idx)
197 debug("case 0x%08" PRIX32 ": // %s\n break;\n", a_hash_bkdr(argv[idx], 0), argv[idx]);
201 static void test_hash_sdbm(int argc, char *argv[])
203 for (int idx = 1; idx < argc; ++idx)
205 debug("case 0x%08" PRIX32 ": // %s\n break;\n", a_hash_sdbm(argv[idx], 0), argv[idx]);
209 int main(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
211 test_sq();
212 test_min();
213 test_max();
214 test_abs();
215 test_sgn();
216 test_sat();
217 if (argc < 2)
219 test_for(argc, argv);
220 test_swap(argc, argv);
221 test_push(argc, argv);
222 test_roll(argc, argv);
223 test_hash_bkdr(argc, argv);
224 test_hash_sdbm(argc, argv);
225 return 0;
227 switch (a_hash_bkdr(argv[1], 0))
229 case 0x001AEED5: // for
230 test_for(argc - 1, argv + 1);
231 break;
232 case 0x0F8837E3: // swap
233 test_swap(argc - 1, argv + 1);
234 break;
235 case 0x0F20D22E: // push
236 test_push(argc - 1, argv + 1);
237 break;
238 case 0x0F63D79D: // roll
239 test_roll(argc - 1, argv + 1);
240 break;
241 case 0x0E0928A2: // hash
242 case 0x23CAC2CA: // hash_bkdr
243 test_hash_bkdr(argc - 1, argv + 1);
244 break;
245 case 0x59A69D8D: // hash_sdbm
246 test_hash_sdbm(argc - 1, argv + 1);
247 break;
248 default:
249 debug("for\n");
250 debug("swap\n");
251 debug("save\n");
252 debug("roll\n");
253 debug("hash_bkdr\n");
254 debug("hash_sdbm\n");
256 return 0;