]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/test-tables.h
Add SPDX license identifiers to source files under the LGPL
[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>
23
24typedef const char* (*lookup_t)(int);
25typedef int (*reverse_t)(const char*);
26
27static inline void _test_table(const char *name,
28 lookup_t lookup,
29 reverse_t reverse,
daabe549
ZJS
30 int size,
31 bool sparse) {
f3367a64 32 int i, boring = 0;
a8b409db 33
daabe549 34 for (i = -1; i < size + 1; i++) {
a8b409db 35 const char* val = lookup(i);
bf502e63 36 int rev;
a8b409db 37
f3367a64 38 if (val) {
a8b409db 39 rev = reverse(val);
f3367a64
ZJS
40 boring = 0;
41 } else {
bf502e63 42 rev = reverse("--no-such--value----");
f3367a64
ZJS
43 boring += i >= 0;
44 }
45
46 if (boring < 1 || i == size)
47 printf("%s: %d → %s → %d\n", name, i, val, rev);
48 else if (boring == 1)
49 printf("%*s ...\n", (int) strlen(name), "");
a8b409db 50
288c0991
LP
51 assert_se(!(i >= 0 && i < size ?
52 sparse ? rev != i && rev != -1 : val == NULL || rev != i :
53 val != NULL || rev != -1));
a8b409db
ZJS
54 }
55}
56
57#define test_table(lower, upper) \
daabe549 58 _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false)
86bbe5bf
ZJS
59
60#define test_table_sparse(lower, upper) \
61 _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true)