1 #define MAIN_(x) A_CAST_2(x, _a)
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
[])
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
);
55 a_forenum(unsigned int, i
, n
)
57 p
[i
] = a_cast_s(int, i
);
62 a_foreach(int, it
, p
, n
) { debug("%i ", *it
); }
67 a_iterate(int, it
, p
, d
) { debug("%i ", *it
); }
71 a_forenum_reverse(unsigned int, i
, n
)
73 p
[i
] = a_cast_s(int, i
);
78 a_foreach_reverse(int, it
, p
, n
) { debug("%i ", *it
); }
83 a_iterate_reverse(int, it
, p
, d
) { debug("%i ", *it
); }
90 static void test_swap(int argc
, char *argv
[])
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
);
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');
161 static void test_roll(int argc
, char *argv
[])
163 a_float array
[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
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');
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)
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
);
231 switch (a_hash_bkdr(argv
[1], 0))
233 case 0x001AEED5: // for
234 test_for(argc
- 1, argv
+ 1);
236 case 0x0F8837E3: // swap
237 test_swap(argc
- 1, argv
+ 1);
239 case 0x0F20D22E: // push
240 test_push(argc
- 1, argv
+ 1);
242 case 0x0F63D79D: // roll
243 test_roll(argc
- 1, argv
+ 1);
245 case 0x0E0928A2: // hash
246 case 0x23CAC2CA: // hash_bkdr
247 test_hash_bkdr(argc
- 1, argv
+ 1);
249 case 0x59A69D8D: // hash_sdbm
250 test_hash_sdbm(argc
- 1, argv
+ 1);
257 debug("hash_bkdr\n");
258 debug("hash_sdbm\n");