Systemd/src/shared/test-tables.h
Lennart Poettering dccca82b1a log: minimize includes in log.h
log.h really should only include the bare minimum of other headers, as
it is really pulled into pretty much everything else and already in
itself one of the most basic pieces of code we have.

Let's hence drop inclusion of:

1. sd-id128.h because it's entirely unneeded in current log.h
2. errno.h, dito.
3. sys/signalfd.h which we can replace by a simple struct forward
   declaration
4. process-util.h which was needed for getpid_cached() which we now hide
   in a funciton log_emergency_level() instead, which nicely abstracts
   the details away.
5. sys/socket.h which was needed for struct iovec, but a simple struct
   forward declaration suffices for that too.

Ultimately this actually makes our source tree larger (since users of
the functionality above must now include it themselves, log.h won't do
that for them), but I think it helps to untangle our web of includes a
tiny bit.

(Background: I'd like to isolate the generic bits of src/basic/ enough
so that we can do a git submodule import into casync for it)
2018-01-11 14:44:31 +01:00

63 lines
2.2 KiB
C

/* SPDX-License-Identifier: LGPL-2.1+ */
/***
This file is part of systemd
Copyright 2013 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef const char* (*lookup_t)(int);
typedef int (*reverse_t)(const char*);
static inline void _test_table(const char *name,
lookup_t lookup,
reverse_t reverse,
int size,
bool sparse) {
int i, boring = 0;
for (i = -1; i < size + 1; i++) {
const char* val = lookup(i);
int rev;
if (val) {
rev = reverse(val);
boring = 0;
} else {
rev = reverse("--no-such--value----");
boring += i >= 0;
}
if (boring < 1 || i == size)
printf("%s: %d → %s → %d\n", name, i, val, rev);
else if (boring == 1)
printf("%*s ...\n", (int) strlen(name), "");
assert_se(!(i >= 0 && i < size ?
sparse ? rev != i && rev != -1 : val == NULL || rev != i :
val != NULL || rev != -1));
}
}
#define test_table(lower, upper) \
_test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false)
#define test_table_sparse(lower, upper) \
_test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true)