1 /* SPDX-License-Identifier: LGPL-2.1-or-later */ 2 #pragma once 3 4 #include <errno.h> 5 #include <inttypes.h> 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <string.h> 9 10 #include "string-util.h" 11 12 #define _test_table(name, lookup, reverse, size, sparse) \ 13 for (int64_t _i = -EINVAL, _boring = 0; _i < size + 1; _i++) { \ 14 const char* _val; \ 15 int64_t _rev; \ 16 \ 17 _val = lookup(_i); \ 18 if (_val) { \ 19 _rev = reverse(_val); \ 20 _boring = 0; \ 21 } else { \ 22 _rev = reverse("--no-such--value----"); \ 23 _boring += _i >= 0; \ 24 } \ 25 if (_boring == 0 || _i == size) \ 26 printf("%s: %" PRIi64 " → %s → %" PRIi64 "\n", name, _i, strnull(_val), _rev); \ 27 else if (_boring == 1) \ 28 printf("%*s ...\n", (int) strlen(name), ""); \ 29 \ 30 if (_i >= 0 && _i < size) { \ 31 if (sparse) \ 32 assert_se(_rev == _i || _rev == -EINVAL); \ 33 else \ 34 assert_se(_val && _rev == _i); \ 35 } else \ 36 assert_se(!_val && _rev == -EINVAL); \ 37 } 38 39 #define test_table(lower, upper) \ 40 _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, false) 41 42 #define test_table_sparse(lower, upper) \ 43 _test_table(STRINGIFY(lower), lower##_to_string, lower##_from_string, _##upper##_MAX, true) 44