diff options
author | Lennart Poettering <lennart@poettering.net> | 2021-02-11 16:02:26 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2021-02-16 20:35:07 +0100 |
commit | d385d54638da5c0246ce90b3b1d54d2cfe6e6765 (patch) | |
tree | be4f7c64ec7c6faee91a7e7c6a5b3c69a5ed768c /src/shared/test-tables.h | |
parent | Merge pull request #18636 from poettering/resolved-different-server (diff) | |
download | systemd-d385d54638da5c0246ce90b3b1d54d2cfe6e6765.tar.xz systemd-d385d54638da5c0246ce90b3b1d54d2cfe6e6765.zip |
test-tables: make sure we can test tables of either int or int64_t base type
To support both types, we unfortunately need to go down the macro rabbit
hole a bit more. But it works.
Diffstat (limited to 'src/shared/test-tables.h')
-rw-r--r-- | src/shared/test-tables.h | 69 |
1 files changed, 32 insertions, 37 deletions
diff --git a/src/shared/test-tables.h b/src/shared/test-tables.h index 90210b9b59..3f20318db1 100644 --- a/src/shared/test-tables.h +++ b/src/shared/test-tables.h @@ -1,48 +1,43 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once +#include <errno.h> +#include <inttypes.h> #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, strnull(val), rev); - else if (boring == 1) - printf("%*s ...\n", (int) strlen(name), ""); - - if (i >= 0 && i < size) { - if (sparse) - assert_se(rev == i || rev == -EINVAL); - else - assert_se(val != NULL && rev == i); - } else - assert_se(val == NULL && rev == -EINVAL); +#include "string-util.h" + +#define _test_table(name, lookup, reverse, size, sparse) \ + for (int64_t _i = -EINVAL, _boring = 0; _i < size + 1; _i++) { \ + const char* _val; \ + int64_t _rev; \ + \ + _val = lookup(_i); \ + if (_val) { \ + _rev = reverse(_val); \ + _boring = 0; \ + } else { \ + _rev = reverse("--no-such--value----"); \ + _boring += _i >= 0; \ + } \ + if (_boring == 0 || _i == size) \ + printf("%s: %" PRIi64 " → %s → %" PRIi64 "\n", name, _i, strnull(_val), _rev); \ + else if (_boring == 1) \ + printf("%*s ...\n", (int) strlen(name), ""); \ + \ + if (_i >= 0 && _i < size) { \ + if (sparse) \ + assert_se(_rev == _i || _rev == -EINVAL); \ + else \ + assert_se(_val && _rev == _i); \ + } else \ + assert_se(!_val && _rev == -EINVAL); \ } -} -#define test_table(lower, upper) \ +#define test_table(lower, upper) \ _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false) -#define test_table_sparse(lower, upper) \ +#define test_table_sparse(lower, upper) \ _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true) |