]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a8b409db ZJS |
2 | |
3 | #include <stdio.h> | |
4 | #include <stdlib.h> | |
dccca82b | 5 | #include <string.h> |
a8b409db ZJS |
6 | |
7 | typedef const char* (*lookup_t)(int); | |
8 | typedef int (*reverse_t)(const char*); | |
9 | ||
10 | static inline void _test_table(const char *name, | |
11 | lookup_t lookup, | |
12 | reverse_t reverse, | |
daabe549 ZJS |
13 | int size, |
14 | bool sparse) { | |
f3367a64 | 15 | int i, boring = 0; |
a8b409db | 16 | |
daabe549 | 17 | for (i = -1; i < size + 1; i++) { |
a8b409db | 18 | const char* val = lookup(i); |
bf502e63 | 19 | int rev; |
a8b409db | 20 | |
f3367a64 | 21 | if (val) { |
a8b409db | 22 | rev = reverse(val); |
f3367a64 ZJS |
23 | boring = 0; |
24 | } else { | |
bf502e63 | 25 | rev = reverse("--no-such--value----"); |
f3367a64 ZJS |
26 | boring += i >= 0; |
27 | } | |
28 | ||
29 | if (boring < 1 || i == size) | |
30 | printf("%s: %d → %s → %d\n", name, i, val, rev); | |
31 | else if (boring == 1) | |
32 | printf("%*s ...\n", (int) strlen(name), ""); | |
a8b409db | 33 | |
288c0991 LP |
34 | assert_se(!(i >= 0 && i < size ? |
35 | sparse ? rev != i && rev != -1 : val == NULL || rev != i : | |
36 | val != NULL || rev != -1)); | |
a8b409db ZJS |
37 | } |
38 | } | |
39 | ||
40 | #define test_table(lower, upper) \ | |
daabe549 | 41 | _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false) |
86bbe5bf ZJS |
42 | |
43 | #define test_table_sparse(lower, upper) \ | |
44 | _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true) |