chiark / gitweb /
57d87d984ac5d32fc8919cfa0a3a2c49500afc0e
[elogind.git] / src / shared / test-tables.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright © 2013 Zbigniew Jędrzejewski-Szmek
4 ***/
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 //#include <string.h>
9
10 typedef const char* (*lookup_t)(int);
11 typedef int (*reverse_t)(const char*);
12
13 static inline void _test_table(const char *name,
14                                lookup_t lookup,
15                                reverse_t reverse,
16                                int size,
17                                bool sparse) {
18         int i, boring = 0;
19
20         for (i = -1; i < size + 1; i++) {
21                 const char* val = lookup(i);
22                 int rev;
23
24                 if (val) {
25                         rev = reverse(val);
26                         boring = 0;
27                 } else {
28                         rev = reverse("--no-such--value----");
29                         boring += i >= 0;
30                 }
31
32                 if (boring < 1 || i == size)
33                         printf("%s: %d → %s → %d\n", name, i, val, rev);
34                 else if (boring == 1)
35                         printf("%*s  ...\n", (int) strlen(name), "");
36
37                 assert_se(!(i >= 0 && i < size ?
38                             sparse ? rev != i && rev != -1 : val == NULL || rev != i :
39                             val != NULL || rev != -1));
40         }
41 }
42
43 #define test_table(lower, upper) \
44         _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false)
45
46 #define test_table_sparse(lower, upper) \
47         _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true)