| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "mos/platform/platform.hpp" |
| 4 | #include "test_engine_impl.h" |
| 5 | |
| 6 | #include <mos/lib/structures/list.hpp> |
| 7 | #include <mos/misc/cmdline.hpp> |
| 8 | #include <mos/misc/panic.hpp> |
| 9 | #include <mos/misc/setup.hpp> |
| 10 | #include <mos/syslog/printk.hpp> |
| 11 | #include <mos_stdio.hpp> |
| 12 | #include <mos_stdlib.hpp> |
| 13 | #include <mos_string.hpp> |
| 14 | |
| 15 | s32 test_engine_n_warning_expected = 0; |
| 16 | |
| 17 | static void test_engine_warning_handler(const char *func, u32 line, const char *fmt, va_list args) |
| 18 | { |
| 19 | char message[MOS_PRINTK_BUFFER_SIZE]; |
| 20 | vsnprintf(buf: message, MOS_PRINTK_BUFFER_SIZE, format: fmt, args); |
| 21 | |
| 22 | if (test_engine_n_warning_expected == 0) |
| 23 | { |
| 24 | lprintk(loglevel: LogLevel::WARN, format: "\r\n" ); |
| 25 | lprintk(loglevel: LogLevel::WARN, format: "warning: %s" , message); |
| 26 | lprintk(loglevel: LogLevel::WARN, format: " in function: %s (line %u)" , func, line); |
| 27 | mos_panic("unexpected warning, test failed." ); |
| 28 | } |
| 29 | |
| 30 | test_engine_n_warning_expected--; |
| 31 | } |
| 32 | |
| 33 | static const char **test_engine_skip_prefix_list = NULL; |
| 34 | static bool mos_tests_halt_on_success = false; |
| 35 | |
| 36 | static bool mos_test_engine_setup_skip_prefix_list(mos::string_view arg) |
| 37 | { |
| 38 | // split the argument into a list of strings |
| 39 | int argc = 1; |
| 40 | for (int i = 0; arg[i]; i++) |
| 41 | if (arg[i] == ',') |
| 42 | argc++; |
| 43 | |
| 44 | test_engine_skip_prefix_list = kcalloc<const char *>(n_members: argc); |
| 45 | |
| 46 | int i = 0; |
| 47 | char *token = strtok(str: (char *) arg.data(), delim: "," ); |
| 48 | while (token) |
| 49 | { |
| 50 | test_engine_skip_prefix_list[i] = token; |
| 51 | token = strtok(NULL, delim: "," ); |
| 52 | i++; |
| 53 | } |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | MOS_SETUP("mos_tests_skip_prefix" , mos_test_engine_setup_skip_prefix_list); |
| 59 | |
| 60 | static bool mos_tests_setup_halt_on_success(mos::string_view arg) |
| 61 | { |
| 62 | mos_tests_halt_on_success = cmdline_string_truthiness(arg, default_value: true); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | MOS_SETUP("mos_tests_halt_on_success" , mos_tests_setup_halt_on_success); |
| 67 | |
| 68 | static bool mos_test_engine_should_skip(const char *test_name) |
| 69 | { |
| 70 | if (!test_engine_skip_prefix_list) |
| 71 | return false; |
| 72 | |
| 73 | for (int i = 0; test_engine_skip_prefix_list[i]; i++) |
| 74 | { |
| 75 | if (strncmp(str1: test_name, str2: test_engine_skip_prefix_list[i], n: strlen(str: test_engine_skip_prefix_list[i])) == 0) |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | static bool mos_test_engine_run_tests(mos::string_view arg) |
| 83 | { |
| 84 | MOS_UNUSED(arg); |
| 85 | kwarn_handler_set(handler: test_engine_warning_handler); |
| 86 | |
| 87 | mos_test_result_t result = { .n_total: 0 }; |
| 88 | |
| 89 | MOS_TEST_FOREACH_TEST_CASE(test_case) |
| 90 | { |
| 91 | bool should_skip = mos_test_engine_should_skip(test_name: test_case->test_name); |
| 92 | |
| 93 | if (should_skip) |
| 94 | continue; |
| 95 | |
| 96 | mos_test_result_t r = { .n_total: 0 }; |
| 97 | test_case->test_func(&r); |
| 98 | |
| 99 | result.n_total += r.n_total; |
| 100 | result.n_failed += r.n_failed; |
| 101 | result.n_skipped += r.n_skipped; |
| 102 | |
| 103 | if (result.n_failed > 0) |
| 104 | mos_panic("TEST FAILED." ); |
| 105 | } |
| 106 | |
| 107 | kwarn_handler_remove(); |
| 108 | |
| 109 | u32 passed = result.n_total - result.n_failed - result.n_skipped; |
| 110 | pr_emph("ALL %u TESTS PASSED: (%u succeed, %u failed, %u skipped)" , result.n_total, passed, result.n_failed, result.n_skipped); |
| 111 | |
| 112 | if (mos_tests_halt_on_success) |
| 113 | platform_halt_cpu(); |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | MOS_SETUP("mos_tests" , mos_test_engine_run_tests); |
| 119 | |