Add a small header to report test results using the Test Anything Protocol, which meson parses when the test declares the 'tap' protocol:
- TAP_START() prints the TAP version line. - tap_report() prints one numbered "ok" / "not ok" line, with a printf-style description. - TAP_CHECK() reports a condition, using its text as description. - tap_assert() is a non-fatal assert: it prints a '# FAIL' TAP diagnostic and counts the failure in tap_assert_failures, for tests grouping several asserts into a single reported result. - tap_done() prints the plan and returns the exit status for main(). The counters are static, so each test must be a single translation unit. Signed-off-by: Philippe Mathieu-Daudé <[email protected]> --- MAINTAINERS | 1 + tests/tcg/include/tap.h | 60 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/tcg/include/tap.h diff --git a/MAINTAINERS b/MAINTAINERS index 8884e4177e4f..761b2f5d4dc6 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4699,6 +4699,7 @@ Guest Test Compilation Support M: Alex Bennée <[email protected]> M: Pierrick Bouvier <[email protected]> S: Maintained +F: tests/tcg/include/tap.h F: tests/tcg/meson.build GitLab custom runners (HW sponsored by Linaro, OSUOSL and IBM) diff --git a/tests/tcg/include/tap.h b/tests/tcg/include/tap.h new file mode 100644 index 000000000000..ce7e990bed4b --- /dev/null +++ b/tests/tcg/include/tap.h @@ -0,0 +1,60 @@ +/* + * Minimal TAP (Test Anything Protocol) helpers for TCG tests. + * + * Select the protocol with 'protocol': 'tap' in the meson setup entry. + * Each test is a single translation unit: the counters are file-local. + * + * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef TESTS_TCG_TAP_H +#define TESTS_TCG_TAP_H + +#include <stdarg.h> +#include <stdio.h> + +static unsigned tap_tests, tap_failures; +static unsigned tap_assert_failures __attribute__((unused)); + +#define TAP_START() printf("TAP version 14\n") + +/* Report one result; the description is a printf-style format. */ +static inline __attribute__((format(printf, 2, 3))) +void tap_report(int ok, const char *fmt, ...) +{ + va_list ap; + + printf("%s %u - ", ok ? "ok" : "not ok", ++tap_tests); + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + printf("\n"); + tap_failures += !ok; +} + +/* Note: the stringified condition must not contain '#'. */ +#define TAP_CHECK(cond) tap_report(!!(cond), "%s", #cond) + +/* + * Check a condition without aborting: print a TAP diagnostic ('# ' line) + * and count the failure, to be consumed by the test reporting its result + * (see tap_assert_failures). + */ +#define tap_assert(cond) \ + do { \ + if (!(cond)) { \ + printf("# FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ + tap_assert_failures++; \ + } \ + } while (0) + +/* Print the plan and return the exit status for main(). */ +static inline int tap_done(void) +{ + printf("1..%u\n", tap_tests); + + return tap_failures ? 1 : 0; +} + +#endif -- 2.55.0
