chiark / gitweb /
59c2c06ec2b20a75ab21c31f840d3b49c6c0b595
[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
8 #include <stdio.h>
9 #include <stdlib.h>
10 //#include <string.h>
11
12 typedef const char* (*lookup_t)(int);
13 typedef int (*reverse_t)(const char*);
14
15 static inline void _test_table(const char *name,
16                                lookup_t lookup,
17                                reverse_t reverse,
18                                int size,
19                                bool sparse) {
20         int i, boring = 0;
21
22         for (i = -1; i < size + 1; i++) {
23                 const char* val = lookup(i);
24                 int rev;
25
26                 if (val) {
27                         rev = reverse(val);
28                         boring = 0;
29                 } else {
30                         rev = reverse("--no-such--value----");
31                         boring += i >= 0;
32                 }
33
34                 if (boring < 1 || i == size)
35                         printf("%s: %d → %s → %d\n", name, i, val, rev);
36                 else if (boring == 1)
37                         printf("%*s  ...\n", (int) strlen(name), "");
38
39                 assert_se(!(i >= 0 && i < size ?
40                             sparse ? rev != i && rev != -1 : val == NULL || rev != i :
41                             val != NULL || rev != -1));
42         }
43 }
44
45 #define test_table(lower, upper) \
46         _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false)
47
48 #define test_table_sparse(lower, upper) \
49         _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true)