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
[])
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
);
62 a_forenum(unsigned int, i
, n
)
64 p
[i
] = a_cast_s(int, i
);
69 a_foreach(int, *, it
, p
, n
) { debug("%i ", *it
); }
72 a_iterate(int, *, it
, p
, d
) { debug("%i ", *it
); }
75 a_forenum_reverse(unsigned int, i
, n
)
77 p
[i
] = a_cast_s(int, i
);
82 a_foreach_reverse(int, *, it
, p
, n
) { debug("%i ", *it
); }
85 a_iterate_reverse(int, *, it
, p
, d
) { debug("%i ", *it
); }
91 static void test_swap(int argc
, char *argv
[])
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
);
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');
164 static void test_roll(int argc
, char *argv
[])
166 a_float array
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
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');
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
)
213 p
[i
] = strtonum(argv
[i
], &endptr
);
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
));
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
)
233 p
[i
] = strtonum(argv
[i
], &endptr
);
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
));
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)
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);
283 switch (a_hash_bkdr(argv
[1], 0))
285 case 0x001AEED5: // for
286 test_for(argc
- 1, argv
+ 1);
288 case 0x001E5957: // sum
289 test_sum(argc
- 2, argv
+ 2);
291 case 0x0F8837E3: // swap
292 test_swap(argc
- 1, argv
+ 1);
294 case 0x0F20D22E: // push
295 test_push(argc
- 1, argv
+ 1);
297 case 0x0F63D79D: // roll
298 test_roll(argc
- 1, argv
+ 1);
300 case 0x0EB5AF9D: // mean
301 test_mean(argc
- 2, argv
+ 2);
303 case 0x0E0928A2: // hash
304 case 0x57614C8C: // hash_bkdr
305 test_hash_bkdr(argc
- 1, argv
+ 1);
307 case 0x59A69D8D: // hash_sdbm
308 test_hash_sdbm(argc
- 1, argv
+ 1);
317 debug("hash_bkdr\n");
318 debug("hash_sdbm\n");