chiark / gitweb /
Prep v236 : Add missing SPDX-License-Identifier (6/9) src/shared
[elogind.git] / src / shared / test-tables.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
24 typedef const char* (*lookup_t)(int);
25 typedef int (*reverse_t)(const char*);
26
27 static inline void _test_table(const char *name,
28                                lookup_t lookup,
29                                reverse_t reverse,
30                                int size,
31                                bool sparse) {
32         int i, boring = 0;
33
34         for (i = -1; i < size + 1; i++) {
35                 const char* val = lookup(i);
36                 int rev;
37
38                 if (val) {
39                         rev = reverse(val);
40                         boring = 0;
41                 } else {
42                         rev = reverse("--no-such--value----");
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), "");
50
51                 assert_se(!(i >= 0 && i < size ?
52                             sparse ? rev != i && rev != -1 : val == NULL || rev != i :
53                             val != NULL || rev != -1));
54         }
55 }
56
57 #define test_table(lower, upper) \
58         _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false)
59
60 #define test_table_sparse(lower, upper) \
61         _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true)