^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * kselftest_harness.h: simple C unit test helper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * See documentation in Documentation/dev-tools/kselftest.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * API inspired by code.google.com/p/googletest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * DOC: example
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * #include "../kselftest_harness.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * TEST(standalone_test) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * do_some_stuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * EXPECT_GT(10, stuff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * stuff_state_t state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * enumerate_stuff_state(&state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * TH_LOG("expectation failed with state: %s", state.msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * more_stuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * ASSERT_NE(some_stuff, NULL) TH_LOG("how did it happen?!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * last_stuff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * EXPECT_EQ(0, last_stuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * FIXTURE(my_fixture) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * mytype_t *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * int awesomeness_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * FIXTURE_SETUP(my_fixture) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * self->data = mytype_new();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * ASSERT_NE(NULL, self->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * FIXTURE_TEARDOWN(my_fixture) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * mytype_free(self->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * TEST_F(my_fixture, data_is_good) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * EXPECT_EQ(1, is_my_data_good(self->data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * TEST_HARNESS_MAIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #ifndef __KSELFTEST_HARNESS_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define __KSELFTEST_HARNESS_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #ifndef _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <asm/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include "kselftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define TEST_TIMEOUT_DEFAULT 30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* Utilities exposed to the test definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #ifndef TH_LOG_STREAM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) # define TH_LOG_STREAM stderr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #ifndef TH_LOG_ENABLED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) # define TH_LOG_ENABLED 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * TH_LOG(fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * @fmt: format string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @...: optional arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * TH_LOG(format, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * Optional debug logging function available for use in tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * Logging may be enabled or disabled by defining TH_LOG_ENABLED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * E.g., #define TH_LOG_ENABLED 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * If no definition is provided, logging is enabled by default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * If there is no way to print an error message for the process running the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * test (e.g. not allowed to write to stderr), it is still possible to get the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * ASSERT_* number for which the test failed. This behavior can be enabled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * writing `_metadata->no_print = true;` before the check sequence that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * unable to print. When an error occur, instead of printing an error message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * and calling `abort(3)`, the test process call `_exit(2)` with the assert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * number as argument, which is then printed by the parent process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define TH_LOG(fmt, ...) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (TH_LOG_ENABLED) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) __TH_LOG(fmt, ##__VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* Unconditional logger for internal use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #define __TH_LOG(fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * SKIP(statement, fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * @statement: statement to run after reporting SKIP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * @fmt: format string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @...: optional arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * This forces a "pass" after reporting why something is being skipped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * and runs "statement", which is usually "return" or "goto skip".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #define SKIP(statement, fmt, ...) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) snprintf(_metadata->results->reason, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) sizeof(_metadata->results->reason), fmt, ##__VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (TH_LOG_ENABLED) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) fprintf(TH_LOG_STREAM, "# SKIP %s\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) _metadata->results->reason); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) _metadata->passed = 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) _metadata->skip = 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) _metadata->trigger = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) statement; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * TEST(test_name) - Defines the test function and creates the registration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * stub
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * @test_name: test name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * TEST(name) { implementation }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Defines a test by name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Names must be unique and tests must not be run in parallel. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * implementation containing block is a function and scoping should be treated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * as such. Returning early may be performed with a bare "return;" statement.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #define TEST(test_name) __TEST_IMPL(test_name, -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * TEST_SIGNAL(test_name, signal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * @test_name: test name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @signal: signal number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * TEST_SIGNAL(name, signal) { implementation }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * Defines a test by name and the expected term signal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * Names must be unique and tests must not be run in parallel. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * implementation containing block is a function and scoping should be treated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * as such. Returning early may be performed with a bare "return;" statement.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #define __TEST_IMPL(test_name, _signal) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static void test_name(struct __test_metadata *_metadata); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static inline void wrapper_##test_name( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct __test_metadata *_metadata, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct __fixture_variant_metadata *variant) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) test_name(_metadata); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static struct __test_metadata _##test_name##_object = \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) { .name = #test_name, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .fn = &wrapper_##test_name, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .fixture = &_fixture_global, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .termsig = _signal, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .timeout = TEST_TIMEOUT_DEFAULT, }; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static void __attribute__((constructor)) _register_##test_name(void) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) __register_test(&_##test_name##_object); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static void test_name( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct __test_metadata __attribute__((unused)) *_metadata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * FIXTURE_DATA(datatype_name) - Wraps the struct name so we have one less
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * argument to pass around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * @datatype_name: datatype name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * FIXTURE_DATA(datatype_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * Almost always, you want just FIXTURE() instead (see below).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * This call may be used when the type of the fixture data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * is needed. In general, this should not be needed unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * the *self* is being passed to a helper directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * FIXTURE(fixture_name) - Called once per fixture to setup the data and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * @fixture_name: fixture name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * FIXTURE(fixture_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * type property1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * Defines the data provided to TEST_F()-defined tests as *self*. It should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) #define FIXTURE(fixture_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) FIXTURE_VARIANT(fixture_name); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static struct __fixture_metadata _##fixture_name##_fixture_object = \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) { .name = #fixture_name, }; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static void __attribute__((constructor)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) _register_##fixture_name##_data(void) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) __register_fixture(&_##fixture_name##_fixture_object); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) FIXTURE_DATA(fixture_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * FIXTURE_SETUP(fixture_name) - Prepares the setup function for the fixture.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * @fixture_name: fixture name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * FIXTURE_SETUP(fixture_name) { implementation }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * Populates the required "setup" function for a fixture. An instance of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * ASSERT_* are valid for use in this context and will prempt the execution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * of any dependent fixture tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * A bare "return;" statement may be used to return early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #define FIXTURE_SETUP(fixture_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) void fixture_name##_setup( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) struct __test_metadata __attribute__((unused)) *_metadata, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) const FIXTURE_VARIANT(fixture_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) __attribute__((unused)) *variant)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * FIXTURE_TEARDOWN(fixture_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * @fixture_name: fixture name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * FIXTURE_TEARDOWN(fixture_name) { implementation }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * Populates the required "teardown" function for a fixture. An instance of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * implementation to clean up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * A bare "return;" statement may be used to return early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) #define FIXTURE_TEARDOWN(fixture_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) void fixture_name##_teardown( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct __test_metadata __attribute__((unused)) *_metadata, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) FIXTURE_DATA(fixture_name) __attribute__((unused)) *self)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * FIXTURE_VARIANT(fixture_name) - Optionally called once per fixture
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * to declare fixture variant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * @fixture_name: fixture name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * FIXTURE_VARIANT(fixture_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * type property1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * Defines type of constant parameters provided to FIXTURE_SETUP() and TEST_F()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * as *variant*. Variants allow the same tests to be run with different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * arguments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * FIXTURE_VARIANT_ADD(fixture_name, variant_name) - Called once per fixture
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * variant to setup and register the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * @fixture_name: fixture name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * @variant_name: name of the parameter set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * FIXTURE_VARIANT_ADD(fixture_name, variant_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * .property1 = val1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * TEST_F() as *variant*. Tests of each fixture will be run once for each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * variant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) extern FIXTURE_VARIANT(fixture_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) _##fixture_name##_##variant_name##_variant; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static struct __fixture_variant_metadata \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) _##fixture_name##_##variant_name##_object = \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) { .name = #variant_name, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) .data = &_##fixture_name##_##variant_name##_variant}; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static void __attribute__((constructor)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) _register_##fixture_name##_##variant_name(void) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) __register_fixture_variant(&_##fixture_name##_fixture_object, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) &_##fixture_name##_##variant_name##_object); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) FIXTURE_VARIANT(fixture_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) _##fixture_name##_##variant_name##_variant =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * TEST_F(fixture_name, test_name) - Emits test registration and helpers for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * fixture-based test cases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * @fixture_name: fixture name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * @test_name: test name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * TEST_F(fixture, name) { implementation }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * Defines a test that depends on a fixture (e.g., is part of a test case).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * Very similar to TEST() except that *self* is the setup instance of fixture's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * datatype exposed for use by the implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * Warning: use of ASSERT_* here will skip TEARDOWN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /* TODO(wad) register fixtures on dedicated test lists. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) #define TEST_F(fixture_name, test_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) #define TEST_F_SIGNAL(fixture_name, test_name, signal) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) __TEST_F_IMPL(fixture_name, test_name, -1, timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static void fixture_name##_##test_name( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct __test_metadata *_metadata, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) FIXTURE_DATA(fixture_name) *self, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) const FIXTURE_VARIANT(fixture_name) *variant); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static inline void wrapper_##fixture_name##_##test_name( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) struct __test_metadata *_metadata, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct __fixture_variant_metadata *variant) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /* fixture data is alloced, setup, and torn down per call. */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) FIXTURE_DATA(fixture_name) self; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) fixture_name##_setup(_metadata, &self, variant->data); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) /* Let setup failure terminate early. */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (!_metadata->passed) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) fixture_name##_##test_name(_metadata, &self, variant->data); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) fixture_name##_teardown(_metadata, &self); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static struct __test_metadata \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) _##fixture_name##_##test_name##_object = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) .name = #test_name, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) .fn = &wrapper_##fixture_name##_##test_name, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) .fixture = &_##fixture_name##_fixture_object, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) .termsig = signal, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) .timeout = tmout, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) static void __attribute__((constructor)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) _register_##fixture_name##_##test_name(void) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) __register_test(&_##fixture_name##_##test_name##_object); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static void fixture_name##_##test_name( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) struct __test_metadata __attribute__((unused)) *_metadata, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) const FIXTURE_VARIANT(fixture_name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) __attribute__((unused)) *variant)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * .. code-block:: c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * TEST_HARNESS_MAIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * Use once to append a main() to the test file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) #define TEST_HARNESS_MAIN \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static void __attribute__((constructor)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) __constructor_order_last(void) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (!__constructor_order) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int main(int argc, char **argv) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return test_harness_run(argc, argv); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * DOC: operators
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * Operators for use in TEST() and TEST_F().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * ASSERT_* calls will stop test execution immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * EXPECT_* calls will emit a failure warning, note it, and continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * ASSERT_EQ()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * ASSERT_EQ(expected, measured): expected == measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) #define ASSERT_EQ(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) __EXPECT(expected, #expected, seen, #seen, ==, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * ASSERT_NE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * ASSERT_NE(expected, measured): expected != measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) #define ASSERT_NE(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) __EXPECT(expected, #expected, seen, #seen, !=, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * ASSERT_LT()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * ASSERT_LT(expected, measured): expected < measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) #define ASSERT_LT(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) __EXPECT(expected, #expected, seen, #seen, <, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * ASSERT_LE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * ASSERT_LE(expected, measured): expected <= measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) #define ASSERT_LE(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) __EXPECT(expected, #expected, seen, #seen, <=, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * ASSERT_GT()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * ASSERT_GT(expected, measured): expected > measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) #define ASSERT_GT(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) __EXPECT(expected, #expected, seen, #seen, >, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * ASSERT_GE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * ASSERT_GE(expected, measured): expected >= measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) #define ASSERT_GE(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) __EXPECT(expected, #expected, seen, #seen, >=, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * ASSERT_NULL()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * ASSERT_NULL(measured): NULL == measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) #define ASSERT_NULL(seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) __EXPECT(NULL, "NULL", seen, #seen, ==, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * ASSERT_TRUE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * ASSERT_TRUE(measured): measured != 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) #define ASSERT_TRUE(seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) __EXPECT(0, "0", seen, #seen, !=, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * ASSERT_FALSE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) * ASSERT_FALSE(measured): measured == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) #define ASSERT_FALSE(seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) __EXPECT(0, "0", seen, #seen, ==, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * ASSERT_STREQ()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) #define ASSERT_STREQ(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) __EXPECT_STR(expected, seen, ==, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * ASSERT_STRNE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) #define ASSERT_STRNE(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) __EXPECT_STR(expected, seen, !=, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * EXPECT_EQ()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * EXPECT_EQ(expected, measured): expected == measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) #define EXPECT_EQ(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) __EXPECT(expected, #expected, seen, #seen, ==, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * EXPECT_NE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * EXPECT_NE(expected, measured): expected != measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) #define EXPECT_NE(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) __EXPECT(expected, #expected, seen, #seen, !=, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * EXPECT_LT()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * EXPECT_LT(expected, measured): expected < measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) #define EXPECT_LT(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) __EXPECT(expected, #expected, seen, #seen, <, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * EXPECT_LE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * EXPECT_LE(expected, measured): expected <= measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) #define EXPECT_LE(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) __EXPECT(expected, #expected, seen, #seen, <=, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) * EXPECT_GT()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * EXPECT_GT(expected, measured): expected > measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) #define EXPECT_GT(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) __EXPECT(expected, #expected, seen, #seen, >, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * EXPECT_GE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * EXPECT_GE(expected, measured): expected >= measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) #define EXPECT_GE(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) __EXPECT(expected, #expected, seen, #seen, >=, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * EXPECT_NULL()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * EXPECT_NULL(measured): NULL == measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) #define EXPECT_NULL(seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) __EXPECT(NULL, "NULL", seen, #seen, ==, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) * EXPECT_TRUE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * EXPECT_TRUE(measured): 0 != measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) #define EXPECT_TRUE(seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) __EXPECT(0, "0", seen, #seen, !=, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) * EXPECT_FALSE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * EXPECT_FALSE(measured): 0 == measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) #define EXPECT_FALSE(seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) __EXPECT(0, "0", seen, #seen, ==, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * EXPECT_STREQ()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) #define EXPECT_STREQ(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) __EXPECT_STR(expected, seen, ==, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * EXPECT_STRNE()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * @expected: expected value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * @seen: measured value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) #define EXPECT_STRNE(expected, seen) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) __EXPECT_STR(expected, seen, !=, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * not thread-safe, but it should be fine in most sane test scenarios.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * Using __bail(), which optionally abort()s, is the easiest way to early
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * return while still providing an optional block to the API consumer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) #define OPTIONAL_HANDLER(_assert) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) for (; _metadata->trigger; _metadata->trigger = \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) __bail(_assert, _metadata->no_print, _metadata->step))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) #define __INC_STEP(_metadata) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) /* Keep "step" below 255 (which is used for "SKIP" reporting). */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (_metadata->passed && _metadata->step < 253) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) _metadata->step++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) #define is_signed_type(var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) /* Avoid multiple evaluation of the cases */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) __typeof__(_expected) __exp = (_expected); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) __typeof__(_seen) __seen = (_seen); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (_assert) __INC_STEP(_metadata); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (!(__exp _t __seen)) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) /* Report with actual signedness to avoid weird output. */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) case 0: { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) unsigned long long __exp_print = (uintptr_t)__exp; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) unsigned long long __seen_print = (uintptr_t)__seen; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) __TH_LOG("Expected %s (%llu) %s %s (%llu)", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) _expected_str, __exp_print, #_t, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) _seen_str, __seen_print); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) break; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) case 1: { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) unsigned long long __exp_print = (uintptr_t)__exp; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) long long __seen_print = (intptr_t)__seen; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) __TH_LOG("Expected %s (%llu) %s %s (%lld)", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) _expected_str, __exp_print, #_t, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) _seen_str, __seen_print); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) break; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) case 2: { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) long long __exp_print = (intptr_t)__exp; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) unsigned long long __seen_print = (uintptr_t)__seen; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) __TH_LOG("Expected %s (%lld) %s %s (%llu)", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) _expected_str, __exp_print, #_t, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) _seen_str, __seen_print); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) break; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) case 3: { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) long long __exp_print = (intptr_t)__exp; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) long long __seen_print = (intptr_t)__seen; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) __TH_LOG("Expected %s (%lld) %s %s (%lld)", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) _expected_str, __exp_print, #_t, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) _seen_str, __seen_print); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) break; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) _metadata->passed = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) /* Ensure the optional handler is triggered */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) _metadata->trigger = 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) } while (0); OPTIONAL_HANDLER(_assert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) const char *__exp = (_expected); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) const char *__seen = (_seen); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) if (_assert) __INC_STEP(_metadata); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (!(strcmp(__exp, __seen) _t 0)) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) _metadata->passed = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) _metadata->trigger = 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) } while (0); OPTIONAL_HANDLER(_assert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) /* List helpers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) #define __LIST_APPEND(head, item) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) /* Circular linked list where only prev is circular. */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (head == NULL) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) head = item; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) item->next = NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) item->prev = item; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) return; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) item->next = NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) item->prev = head->prev; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) item->prev->next = item; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) head->prev = item; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) } else { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) item->next = head; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) item->next->prev = item; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) item->prev = item; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) head = item; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) struct __test_results {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) char reason[1024]; /* Reason for test result */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) struct __test_metadata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) struct __fixture_variant_metadata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) /* Contains all the information about a fixture. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) struct __fixture_metadata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) struct __test_metadata *tests;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) struct __fixture_variant_metadata *variant;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) struct __fixture_metadata *prev, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) } _fixture_global __attribute__((unused)) = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) .name = "global",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) .prev = &_fixture_global,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) static struct __fixture_metadata *__fixture_list = &_fixture_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) static int __constructor_order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) #define _CONSTRUCTOR_ORDER_FORWARD 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) #define _CONSTRUCTOR_ORDER_BACKWARD -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) static inline void __register_fixture(struct __fixture_metadata *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) __LIST_APPEND(__fixture_list, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct __fixture_variant_metadata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) const void *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) struct __fixture_variant_metadata *prev, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) __register_fixture_variant(struct __fixture_metadata *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) struct __fixture_variant_metadata *variant)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) __LIST_APPEND(f->variant, variant);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) /* Contains all the information for test execution and status checking. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) struct __test_metadata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) void (*fn)(struct __test_metadata *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) struct __fixture_variant_metadata *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) pid_t pid; /* pid of test when being run */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) struct __fixture_metadata *fixture;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) int termsig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) int passed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) int skip; /* did SKIP get used? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) int trigger; /* extra handler after the evaluation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) int timeout; /* seconds to wait for test timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) bool timed_out; /* did this test timeout instead of exiting? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) __u8 step;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) bool no_print; /* manual trigger when TH_LOG_STREAM is not available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) struct __test_results *results;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) struct __test_metadata *prev, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * Since constructors are called in reverse order, reverse the test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * list so tests are run in source declaration order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * However, it seems not all toolchains do this correctly, so use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * __constructor_order to detect which direction is called first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * and adjust list building logic to get things running in the right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) * direction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) static inline void __register_test(struct __test_metadata *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) __LIST_APPEND(t->fixture->tests, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) static inline int __bail(int for_realz, bool no_print, __u8 step)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (for_realz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (no_print)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) _exit(step);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) abort();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) struct __test_metadata *__active_test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) struct __test_metadata *t = __active_test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) /* Sanity check handler execution environment. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) if (!t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) fprintf(TH_LOG_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) "# no active test in SIGALRM handler!?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) abort();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) if (sig != SIGALRM || sig != info->si_signo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) fprintf(TH_LOG_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) "# %s: SIGALRM handler caught signal %d!?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) t->name, sig != SIGALRM ? sig : info->si_signo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) abort();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) t->timed_out = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) // signal process group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) kill(-(t->pid), SIGKILL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) void __wait_for_test(struct __test_metadata *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) struct sigaction action = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) .sa_sigaction = __timeout_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) .sa_flags = SA_SIGINFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) struct sigaction saved_action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) if (sigaction(SIGALRM, &action, &saved_action)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) t->passed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) fprintf(TH_LOG_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) "# %s: unable to install SIGALRM handler\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) t->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) __active_test = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) t->timed_out = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) alarm(t->timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) waitpid(t->pid, &status, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) alarm(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) if (sigaction(SIGALRM, &saved_action, NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) t->passed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) fprintf(TH_LOG_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) "# %s: unable to uninstall SIGALRM handler\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) t->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) __active_test = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) if (t->timed_out) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) t->passed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) fprintf(TH_LOG_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) "# %s: Test terminated by timeout\n", t->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) } else if (WIFEXITED(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) if (t->termsig != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) t->passed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) fprintf(TH_LOG_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) "# %s: Test exited normally instead of by signal (code: %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) t->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) WEXITSTATUS(status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) switch (WEXITSTATUS(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) /* Success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) t->passed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) /* SKIP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) case 255:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) t->passed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) t->skip = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) /* Other failure, assume step report. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) t->passed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) fprintf(TH_LOG_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) "# %s: Test failed at step #%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) t->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) WEXITSTATUS(status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) } else if (WIFSIGNALED(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) t->passed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (WTERMSIG(status) == SIGABRT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) fprintf(TH_LOG_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) "# %s: Test terminated by assertion\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) t->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) } else if (WTERMSIG(status) == t->termsig) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) t->passed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) fprintf(TH_LOG_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) "# %s: Test terminated unexpectedly by signal %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) t->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) WTERMSIG(status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) fprintf(TH_LOG_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) "# %s: Test ended in some other way [%u]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) t->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) void __run_test(struct __fixture_metadata *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) struct __fixture_variant_metadata *variant,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) struct __test_metadata *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) /* reset test struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) t->passed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) t->skip = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) t->trigger = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) t->step = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) t->no_print = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) memset(t->results->reason, 0, sizeof(t->results->reason));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) ksft_print_msg(" RUN %s%s%s.%s ...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) f->name, variant->name[0] ? "." : "", variant->name, t->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) /* Make sure output buffers are flushed before fork */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) fflush(stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) t->pid = fork();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) if (t->pid < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) t->passed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) } else if (t->pid == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) setpgrp();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) t->fn(t, variant);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) if (t->skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) _exit(255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) /* Pass is exit 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) if (t->passed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) _exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) /* Something else happened, report the step. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) _exit(t->step);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) __wait_for_test(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) ksft_print_msg(" %4s %s%s%s.%s\n", t->passed ? "OK" : "FAIL",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) f->name, variant->name[0] ? "." : "", variant->name, t->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if (t->skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) ksft_test_result_skip("%s\n", t->results->reason[0] ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) t->results->reason : "unknown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) ksft_test_result(t->passed, "%s%s%s.%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) f->name, variant->name[0] ? "." : "", variant->name, t->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) static int test_harness_run(int __attribute__((unused)) argc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) char __attribute__((unused)) **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) struct __fixture_variant_metadata no_variant = { .name = "", };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) struct __fixture_variant_metadata *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) struct __fixture_metadata *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) struct __test_results *results;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) struct __test_metadata *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) unsigned int case_count = 0, test_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) unsigned int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) unsigned int pass_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) for (f = __fixture_list; f; f = f->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) for (v = f->variant ?: &no_variant; v; v = v->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) case_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) for (t = f->tests; t; t = t->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) test_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) MAP_SHARED | MAP_ANONYMOUS, -1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) ksft_print_header();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) ksft_set_plan(test_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) ksft_print_msg("Starting %u tests from %u test cases.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) test_count, case_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) for (f = __fixture_list; f; f = f->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) for (v = f->variant ?: &no_variant; v; v = v->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) for (t = f->tests; t; t = t->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) t->results = results;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) __run_test(f, v, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) t->results = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) if (t->passed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) pass_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) munmap(results, sizeof(*results));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) pass_count, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) ksft_exit(ret == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) /* unreachable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) return KSFT_FAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) static void __attribute__((constructor)) __constructor_order_first(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) if (!__constructor_order)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) __constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) #endif /* __KSELFTEST_HARNESS_H */