diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:49:52 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:49:52 +0000 |
commit | 55944e5e40b1be2afc4855d8d2baf4b73d1876b5 (patch) | |
tree | 33f869f55a1b149e9b7c2b7e201867ca5dd52992 /src/shared/test-tables.h | |
parent | Initial commit. (diff) | |
download | systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.tar.xz systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.zip |
Adding upstream version 255.4.upstream/255.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/shared/test-tables.h')
-rw-r--r-- | src/shared/test-tables.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/shared/test-tables.h b/src/shared/test-tables.h new file mode 100644 index 0000000..3f20318 --- /dev/null +++ b/src/shared/test-tables.h @@ -0,0 +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> + +#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) \ + _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) |