change gcc to clang in docs.yaml
[liba.git] / test / a.h
blob2791e4a6cb028d9f929f4cda3dcc05627bdfffaa
1 #define MAIN_(x) A_CAST_2(x, _a)
2 #include "test.h"
3 #include <stdlib.h>
5 static void test_sq(void)
7 A_ASSERT_BUILD(A_SQ(+2) == 4);
8 A_ASSERT_BUILD(A_SQ(-2) == 4);
11 static void test_abs(void)
13 A_ASSERT_BUILD(A_ABS(~0) > 0);
16 static void test_min(void)
18 A_ASSERT_BUILD(A_MIN(0, 1) == 0);
19 A_ASSERT_BUILD(A_MIN(1, 0) == 0);
22 static void test_max(void)
24 A_ASSERT_BUILD(A_MAX(0, 1) == 1);
25 A_ASSERT_BUILD(A_MAX(1, 0) == 1);
28 static void test_sgn(void)
30 A_ASSERT_BUILD(A_SGN(0) == 0);
31 A_ASSERT_BUILD(A_SGN(+10) == +1);
32 A_ASSERT_BUILD(A_SGN(-10) == -1);
35 static void test_sat(void)
37 A_ASSERT_BUILD(A_SAT(0, -10, +10) == 0);
38 A_ASSERT_BUILD(A_SAT(+100, -10, +10) <= +10);
39 A_ASSERT_BUILD(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;
55 a_forenum(unsigned int, i, n)
57 p[i] = a_cast_s(int, i);
58 debug("%u ", i);
60 debug("\n");
62 a_foreach(int, it, p, n) { debug("%i ", *it); }
63 debug("\n");
65 if (p && d)
67 a_iterate(int, it, p, d) { debug("%i ", *it); }
68 debug("\n");
71 a_forenum_reverse(unsigned int, i, n)
73 p[i] = a_cast_s(int, i);
74 debug("%u ", i);
76 debug("\n");
78 a_foreach_reverse(int, it, p, n) { debug("%i ", *it); }
79 debug("\n");
81 if (p && d)
83 a_iterate_reverse(int, it, p, d) { debug("%i ", *it); }
84 debug("\n");
87 a_die(p);
90 static void test_swap(int argc, char *argv[])
92 a_u64 lhs = 0;
93 a_u64 rhs = A_U64_MAX;
94 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
95 a_swap(&lhs, &rhs, 1);
96 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
97 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
98 a_swap(&lhs, &rhs, 2);
99 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
100 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
101 a_swap(&lhs, &rhs, 4);
102 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
103 debug("0x%016" PRIX64 " 0x%016" PRIX64 " -> ", lhs, rhs);
104 a_swap(&lhs, &rhs, 8);
105 debug("0x%016" PRIX64 " 0x%016" PRIX64 " \n", lhs, rhs);
106 (void)argc;
107 (void)argv;
110 static void test_push(int argc, char *argv[])
112 a_float array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
113 for (a_size i = 0; i < A_LEN(array); ++i)
115 debug(A_FLOAT_PRI("+", "g") "%c", array[i], i + 1 < A_LEN(array) ? ' ' : '\n');
118 a_float cache[] = {-1};
119 a_float_push(array, A_LEN(array), cache, A_LEN(cache));
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), shift, 1);
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_hash_bkdr(int argc, char *argv[])
200 for (int idx = 1; idx < argc; ++idx)
202 debug("case 0x%08" PRIX32 ": // %s\n break;\n", a_hash_bkdr(argv[idx], 0), argv[idx]);
206 static void test_hash_sdbm(int argc, char *argv[])
208 for (int idx = 1; idx < argc; ++idx)
210 debug("case 0x%08" PRIX32 ": // %s\n break;\n", a_hash_sdbm(argv[idx], 0), argv[idx]);
214 int MAIN(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
216 test_sq();
217 test_min();
218 test_max();
219 test_abs();
220 test_sgn();
221 test_sat();
222 if (argc < 2)
224 test_for(argc, argv);
225 test_swap(argc, argv);
226 test_push(argc, argv);
227 test_roll(argc, argv);
228 test_hash_bkdr(argc, argv);
229 return 0;
231 switch (a_hash_bkdr(argv[1], 0))
233 case 0x001AEED5: // for
234 test_for(argc - 1, argv + 1);
235 break;
236 case 0x0F8837E3: // swap
237 test_swap(argc - 1, argv + 1);
238 break;
239 case 0x0F20D22E: // push
240 test_push(argc - 1, argv + 1);
241 break;
242 case 0x0F63D79D: // roll
243 test_roll(argc - 1, argv + 1);
244 break;
245 case 0x0E0928A2: // hash
246 case 0x23CAC2CA: // hash_bkdr
247 test_hash_bkdr(argc - 1, argv + 1);
248 break;
249 case 0x59A69D8D: // hash_sdbm
250 test_hash_sdbm(argc - 1, argv + 1);
251 break;
252 default:
253 debug("for\n");
254 debug("swap\n");
255 debug("save\n");
256 debug("roll\n");
257 debug("hash_bkdr\n");
258 debug("hash_sdbm\n");
260 return 0;