]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/test-tables.h
log: minimize includes in log.h
[thirdparty/systemd.git] / src / shared / test-tables.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a8b409db
ZJS
2/***
3 This file is part of systemd
4
5 Copyright 2013 Zbigniew Jędrzejewski-Szmek
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
21#include <stdio.h>
22#include <stdlib.h>
dccca82b 23#include <string.h>
a8b409db
ZJS
24
25typedef const char* (*lookup_t)(int);
26typedef int (*reverse_t)(const char*);
27
28static inline void _test_table(const char *name,
29 lookup_t lookup,
30 reverse_t reverse,
daabe549
ZJS
31 int size,
32 bool sparse) {
f3367a64 33 int i, boring = 0;
a8b409db 34
daabe549 35 for (i = -1; i < size + 1; i++) {
a8b409db 36 const char* val = lookup(i);
bf502e63 37 int rev;
a8b409db 38
f3367a64 39 if (val) {
a8b409db 40 rev = reverse(val);
f3367a64
ZJS
41 boring = 0;
42 } else {
bf502e63 43 rev = reverse("--no-such--value----");
f3367a64
ZJS
44 boring += i >= 0;
45 }
46
47 if (boring < 1 || i == size)
48 printf("%s: %d → %s → %d\n", name, i, val, rev);
49 else if (boring == 1)
50 printf("%*s ...\n", (int) strlen(name), "");
a8b409db 51
288c0991
LP
52 assert_se(!(i >= 0 && i < size ?
53 sparse ? rev != i && rev != -1 : val == NULL || rev != i :
54 val != NULL || rev != -1));
a8b409db
ZJS
55 }
56}
57
58#define test_table(lower, upper) \
daabe549 59 _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false)
86bbe5bf
ZJS
60
61#define test_table_sparse(lower, upper) \
62 _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true)