ztest: Replace PRINT with PRINT_DATA

The testsuite subsystem provides the macro PRINT_DATA() which can be
overridden using CONFIG_ZTEST_TC_UTIL_USER_OVERRIDE. Switch all calls of
PRINT() to PRINT_DATA withing the testsuite subsystem.

Signed-off-by: Keith Short <keithshort@google.com>
This commit is contained in:
Keith Short 2024-08-02 14:58:55 -06:00 committed by Anas Nashif
commit da4f4e8748
3 changed files with 62 additions and 57 deletions

View file

@ -18,6 +18,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <zephyr/tc_util.h>
#include <zephyr/ztest.h> #include <zephyr/ztest.h>
#ifdef __cplusplus #ifdef __cplusplus
@ -34,7 +35,8 @@ void ztest_skip_failed_assumption(void);
static inline bool z_zassert_(bool cond, const char *file, int line) static inline bool z_zassert_(bool cond, const char *file, int line)
{ {
if (cond == false) { if (cond == false) {
PRINT("\n Assertion failed at %s:%d\n", ztest_relative_filename(file), line); PRINT_DATA("\n Assertion failed at %s:%d\n", ztest_relative_filename(file),
line);
ztest_test_fail(); ztest_test_fail();
return false; return false;
} }
@ -47,7 +49,8 @@ static inline bool z_zassert_(bool cond, const char *file, int line)
static inline bool z_zassume_(bool cond, const char *file, int line) static inline bool z_zassume_(bool cond, const char *file, int line)
{ {
if (cond == false) { if (cond == false) {
PRINT("\n Assumption failed at %s:%d\n", ztest_relative_filename(file), line); PRINT_DATA("\n Assumption failed at %s:%d\n", ztest_relative_filename(file),
line);
ztest_skip_failed_assumption(); ztest_skip_failed_assumption();
return false; return false;
} }
@ -60,7 +63,8 @@ static inline bool z_zassume_(bool cond, const char *file, int line)
static inline bool z_zexpect_(bool cond, const char *file, int line) static inline bool z_zexpect_(bool cond, const char *file, int line)
{ {
if (cond == false) { if (cond == false) {
PRINT("\n Expectation failed at %s:%d\n", ztest_relative_filename(file), line); PRINT_DATA("\n Expectation failed at %s:%d\n", ztest_relative_filename(file),
line);
ztest_test_expect_fail(); ztest_test_expect_fail();
return false; return false;
} }
@ -79,8 +83,8 @@ static inline bool z_zassert(bool cond, const char *default_msg, const char *fil
va_list vargs; va_list vargs;
va_start(vargs, msg); va_start(vargs, msg);
PRINT("\n Assertion failed at %s:%d: %s: %s\n", ztest_relative_filename(file), PRINT_DATA("\n Assertion failed at %s:%d: %s: %s\n",
line, func, default_msg); ztest_relative_filename(file), line, func, default_msg);
vprintk(msg, vargs); vprintk(msg, vargs);
printk("\n"); printk("\n");
va_end(vargs); va_end(vargs);
@ -89,8 +93,8 @@ static inline bool z_zassert(bool cond, const char *default_msg, const char *fil
} }
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2 #if CONFIG_ZTEST_ASSERT_VERBOSE == 2
else { else {
PRINT("\n Assertion succeeded at %s:%d (%s)\n", ztest_relative_filename(file), PRINT_DATA("\n Assertion succeeded at %s:%d (%s)\n",
line, func); ztest_relative_filename(file), line, func);
} }
#endif #endif
return true; return true;
@ -103,8 +107,8 @@ static inline bool z_zassume(bool cond, const char *default_msg, const char *fil
va_list vargs; va_list vargs;
va_start(vargs, msg); va_start(vargs, msg);
PRINT("\n Assumption failed at %s:%d: %s: %s\n", ztest_relative_filename(file), PRINT_DATA("\n Assumption failed at %s:%d: %s: %s\n",
line, func, default_msg); ztest_relative_filename(file), line, func, default_msg);
vprintk(msg, vargs); vprintk(msg, vargs);
printk("\n"); printk("\n");
va_end(vargs); va_end(vargs);
@ -113,8 +117,8 @@ static inline bool z_zassume(bool cond, const char *default_msg, const char *fil
} }
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2 #if CONFIG_ZTEST_ASSERT_VERBOSE == 2
else { else {
PRINT("\n Assumption succeeded at %s:%d (%s)\n", ztest_relative_filename(file), PRINT_DATA("\n Assumption succeeded at %s:%d (%s)\n",
line, func); ztest_relative_filename(file), line, func);
} }
#endif #endif
return true; return true;
@ -127,8 +131,8 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
va_list vargs; va_list vargs;
va_start(vargs, msg); va_start(vargs, msg);
PRINT("\n Expectation failed at %s:%d: %s: %s\n", ztest_relative_filename(file), PRINT_DATA("\n Expectation failed at %s:%d: %s: %s\n",
line, func, default_msg); ztest_relative_filename(file), line, func, default_msg);
vprintk(msg, vargs); vprintk(msg, vargs);
printk("\n"); printk("\n");
va_end(vargs); va_end(vargs);
@ -137,8 +141,8 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
} }
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2 #if CONFIG_ZTEST_ASSERT_VERBOSE == 2
else { else {
PRINT("\n Expectation succeeded at %s:%d (%s)\n", ztest_relative_filename(file), PRINT_DATA("\n Expectation succeeded at %s:%d (%s)\n",
line, func); ztest_relative_filename(file), line, func);
} }
#endif #endif
return true; return true;

View file

@ -92,10 +92,10 @@ static int cleanup_test(struct ztest_unit_test *test)
#endif #endif
if (!ret && mock_status == 1) { if (!ret && mock_status == 1) {
PRINT("Test %s failed: Unused mock parameter values\n", test->name); PRINT_DATA("Test %s failed: Unused mock parameter values\n", test->name);
ret = TC_FAIL; ret = TC_FAIL;
} else if (!ret && mock_status == 2) { } else if (!ret && mock_status == 2) {
PRINT("Test %s failed: Unused mock return values\n", test->name); PRINT_DATA("Test %s failed: Unused mock return values\n", test->name);
ret = TC_FAIL; ret = TC_FAIL;
} else { } else {
; ;
@ -414,17 +414,17 @@ void ztest_test_fail(void)
{ {
switch (cur_phase) { switch (cur_phase) {
case TEST_PHASE_SETUP: case TEST_PHASE_SETUP:
PRINT(" at %s function\n", get_friendly_phase_name(cur_phase)); PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
longjmp(test_suite_fail, 1); longjmp(test_suite_fail, 1);
case TEST_PHASE_BEFORE: case TEST_PHASE_BEFORE:
case TEST_PHASE_TEST: case TEST_PHASE_TEST:
PRINT(" at %s function\n", get_friendly_phase_name(cur_phase)); PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
longjmp(test_fail, 1); longjmp(test_fail, 1);
case TEST_PHASE_AFTER: case TEST_PHASE_AFTER:
case TEST_PHASE_TEARDOWN: case TEST_PHASE_TEARDOWN:
case TEST_PHASE_FRAMEWORK: case TEST_PHASE_FRAMEWORK:
PRINT(" ERROR: cannot fail in test phase '%s()', bailing\n", PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
get_friendly_phase_name(cur_phase)); get_friendly_phase_name(cur_phase));
longjmp(stack_fail, 1); longjmp(stack_fail, 1);
} }
} }
@ -435,8 +435,8 @@ void ztest_test_pass(void)
if (cur_phase == TEST_PHASE_TEST) { if (cur_phase == TEST_PHASE_TEST) {
longjmp(test_pass, 1); longjmp(test_pass, 1);
} }
PRINT(" ERROR: cannot pass in test phase '%s()', bailing\n", PRINT_DATA(" ERROR: cannot pass in test phase '%s()', bailing\n",
get_friendly_phase_name(cur_phase)); get_friendly_phase_name(cur_phase));
longjmp(stack_fail, 1); longjmp(stack_fail, 1);
} }
EXPORT_SYMBOL(ztest_test_pass); EXPORT_SYMBOL(ztest_test_pass);
@ -449,8 +449,8 @@ void ztest_test_skip(void)
case TEST_PHASE_TEST: case TEST_PHASE_TEST:
longjmp(test_skip, 1); longjmp(test_skip, 1);
default: default:
PRINT(" ERROR: cannot skip in test phase '%s()', bailing\n", PRINT_DATA(" ERROR: cannot skip in test phase '%s()', bailing\n",
get_friendly_phase_name(cur_phase)); get_friendly_phase_name(cur_phase));
longjmp(stack_fail, 1); longjmp(stack_fail, 1);
} }
} }
@ -462,17 +462,17 @@ void ztest_test_expect_fail(void)
switch (cur_phase) { switch (cur_phase) {
case TEST_PHASE_SETUP: case TEST_PHASE_SETUP:
PRINT(" at %s function\n", get_friendly_phase_name(cur_phase)); PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
break; break;
case TEST_PHASE_BEFORE: case TEST_PHASE_BEFORE:
case TEST_PHASE_TEST: case TEST_PHASE_TEST:
PRINT(" at %s function\n", get_friendly_phase_name(cur_phase)); PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
break; break;
case TEST_PHASE_AFTER: case TEST_PHASE_AFTER:
case TEST_PHASE_TEARDOWN: case TEST_PHASE_TEARDOWN:
case TEST_PHASE_FRAMEWORK: case TEST_PHASE_FRAMEWORK:
PRINT(" ERROR: cannot fail in test phase '%s()', bailing\n", PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
get_friendly_phase_name(cur_phase)); get_friendly_phase_name(cur_phase));
longjmp(stack_fail, 1); longjmp(stack_fail, 1);
} }
} }
@ -572,8 +572,8 @@ void ztest_test_fail(void)
test_finalize(); test_finalize();
break; break;
default: default:
PRINT(" ERROR: cannot fail in test phase '%s()', bailing\n", PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
get_friendly_phase_name(cur_phase)); get_friendly_phase_name(cur_phase));
test_status = ZTEST_STATUS_CRITICAL_ERROR; test_status = ZTEST_STATUS_CRITICAL_ERROR;
break; break;
} }
@ -588,8 +588,8 @@ void ztest_test_pass(void)
test_finalize(); test_finalize();
break; break;
default: default:
PRINT(" ERROR: cannot pass in test phase '%s()', bailing\n", PRINT_DATA(" ERROR: cannot pass in test phase '%s()', bailing\n",
get_friendly_phase_name(cur_phase)); get_friendly_phase_name(cur_phase));
test_status = ZTEST_STATUS_CRITICAL_ERROR; test_status = ZTEST_STATUS_CRITICAL_ERROR;
if (cur_phase == TEST_PHASE_BEFORE) { if (cur_phase == TEST_PHASE_BEFORE) {
test_finalize(); test_finalize();
@ -610,8 +610,8 @@ void ztest_test_skip(void)
test_finalize(); test_finalize();
break; break;
default: default:
PRINT(" ERROR: cannot skip in test phase '%s()', bailing\n", PRINT_DATA(" ERROR: cannot skip in test phase '%s()', bailing\n",
get_friendly_phase_name(cur_phase)); get_friendly_phase_name(cur_phase));
test_status = ZTEST_STATUS_CRITICAL_ERROR; test_status = ZTEST_STATUS_CRITICAL_ERROR;
break; break;
} }
@ -807,7 +807,7 @@ static int z_ztest_run_test_suite_ptr(struct ztest_suite_node *suite, bool shuff
#ifndef KERNEL #ifndef KERNEL
if (setjmp(stack_fail)) { if (setjmp(stack_fail)) {
PRINT("TESTSUITE crashed.\n"); PRINT_DATA("TESTSUITE crashed.\n");
test_status = ZTEST_STATUS_CRITICAL_ERROR; test_status = ZTEST_STATUS_CRITICAL_ERROR;
end_report(); end_report();
exit(1); exit(1);
@ -1157,7 +1157,7 @@ void ztest_verify_all_test_suites_ran(void)
for (suite = _ztest_suite_node_list_start; suite < _ztest_suite_node_list_end; for (suite = _ztest_suite_node_list_start; suite < _ztest_suite_node_list_end;
++suite) { ++suite) {
if (suite->stats->run_count < 1) { if (suite->stats->run_count < 1) {
PRINT("ERROR: Test suite '%s' did not run.\n", suite->name); PRINT_DATA("ERROR: Test suite '%s' did not run.\n", suite->name);
all_tests_run = false; all_tests_run = false;
} }
} }
@ -1165,9 +1165,10 @@ void ztest_verify_all_test_suites_ran(void)
for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) { for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) {
suite = ztest_find_test_suite(test->test_suite_name); suite = ztest_find_test_suite(test->test_suite_name);
if (suite == NULL) { if (suite == NULL) {
PRINT("ERROR: Test '%s' assigned to test suite '%s' which doesn't " PRINT_DATA("ERROR: Test '%s' assigned to test suite '%s' which "
"exist\n", "doesn't "
test->name, test->test_suite_name); "exist\n",
test->name, test->test_suite_name);
all_tests_run = false; all_tests_run = false;
} }
} }
@ -1180,7 +1181,7 @@ void ztest_verify_all_test_suites_ran(void)
for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) { for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) {
if (test->stats->fail_count + test->stats->pass_count + test->stats->skip_count != if (test->stats->fail_count + test->stats->pass_count + test->stats->skip_count !=
test->stats->run_count) { test->stats->run_count) {
PRINT("Bad stats for %s.%s\n", test->test_suite_name, test->name); PRINT_DATA("Bad stats for %s.%s\n", test->test_suite_name, test->name);
test_status = 1; test_status = 1;
} }
} }
@ -1430,11 +1431,11 @@ int main(void)
} }
state.boots += 1; state.boots += 1;
if (test_status == 0) { if (test_status == 0) {
PRINT("Reset board #%u to test again\n", state.boots); PRINT_DATA("Reset board #%u to test again\n", state.boots);
k_msleep(10); k_msleep(10);
sys_reboot(SYS_REBOOT_COLD); sys_reboot(SYS_REBOOT_COLD);
} else { } else {
PRINT("Failed after %u attempts\n", state.boots); PRINT_DATA("Failed after %u attempts\n", state.boots);
state.boots = 0; state.boots = 0;
} }
} }

View file

@ -33,7 +33,7 @@ static struct parameter *alloc_parameter(void)
param = calloc(1, sizeof(struct parameter)); param = calloc(1, sizeof(struct parameter));
if (!param) { if (!param) {
PRINT("Failed to allocate mock parameter\n"); PRINT_DATA("Failed to allocate mock parameter\n");
ztest_test_fail(); ztest_test_fail();
} }
@ -205,7 +205,7 @@ void z_ztest_check_expected_value(const char *fn, const char *name, uintptr_t va
param = find_and_delete_value(&parameter_list, fn, name); param = find_and_delete_value(&parameter_list, fn, name);
if (!param) { if (!param) {
PRINT("Failed to find parameter %s for %s\n", name, fn); PRINT_DATA("Failed to find parameter %s for %s\n", name, fn);
ztest_test_fail(); ztest_test_fail();
} }
@ -216,8 +216,8 @@ void z_ztest_check_expected_value(const char *fn, const char *name, uintptr_t va
/* We need to cast these values since the toolchain doesn't /* We need to cast these values since the toolchain doesn't
* provide inttypes.h * provide inttypes.h
*/ */
PRINT("%s:%s received wrong value: Got %lu, expected %lu\n", fn, name, PRINT_DATA("%s:%s received wrong value: Got %lu, expected %lu\n", fn, name,
(unsigned long)val, (unsigned long)expected); (unsigned long)val, (unsigned long)expected);
ztest_test_fail(); ztest_test_fail();
} }
} }
@ -234,7 +234,7 @@ void z_ztest_check_expected_data(const char *fn, const char *name, void *data, u
param = find_and_delete_value(&parameter_list, fn, name); param = find_and_delete_value(&parameter_list, fn, name);
if (!param) { if (!param) {
PRINT("Failed to find parameter %s for %s\n", name, fn); PRINT_DATA("Failed to find parameter %s for %s\n", name, fn);
/* No return from this function but for coverity reasons /* No return from this function but for coverity reasons
* put a return after to avoid the warning of a null * put a return after to avoid the warning of a null
* dereference of param below. * dereference of param below.
@ -247,14 +247,14 @@ void z_ztest_check_expected_data(const char *fn, const char *name, void *data, u
free_parameter(param); free_parameter(param);
if (expected == NULL && data != NULL) { if (expected == NULL && data != NULL) {
PRINT("%s:%s received data while expected null pointer\n", fn, name); PRINT_DATA("%s:%s received data while expected null pointer\n", fn, name);
ztest_test_fail(); ztest_test_fail();
} else if (data == NULL && expected != NULL) { } else if (data == NULL && expected != NULL) {
PRINT("%s:%s received null pointer while expected data\n", fn, name); PRINT_DATA("%s:%s received null pointer while expected data\n", fn, name);
ztest_test_fail(); ztest_test_fail();
} else if (data != NULL) { } else if (data != NULL) {
if (memcmp(data, expected, length) != 0) { if (memcmp(data, expected, length) != 0) {
PRINT("%s:%s data provided don't match\n", fn, name); PRINT_DATA("%s:%s data provided don't match\n", fn, name);
ztest_test_fail(); ztest_test_fail();
} }
} }
@ -271,14 +271,14 @@ void z_ztest_copy_return_data(const char *fn, const char *name, void *data, uint
void *return_data; void *return_data;
if (data == NULL) { if (data == NULL) {
PRINT("%s:%s received null pointer\n", fn, name); PRINT_DATA("%s:%s received null pointer\n", fn, name);
ztest_test_fail(); ztest_test_fail();
return; return;
} }
param = find_and_delete_value(&parameter_list, fn, name); param = find_and_delete_value(&parameter_list, fn, name);
if (!param) { if (!param) {
PRINT("Failed to find parameter %s for %s\n", name, fn); PRINT_DATA("Failed to find parameter %s for %s\n", name, fn);
memset(data, 0, length); memset(data, 0, length);
ztest_test_fail(); ztest_test_fail();
} else { } else {
@ -299,7 +299,7 @@ uintptr_t z_ztest_get_return_value(const char *fn)
struct parameter *param = find_and_delete_value(&return_value_list, fn, ""); struct parameter *param = find_and_delete_value(&return_value_list, fn, "");
if (!param) { if (!param) {
PRINT("Failed to find return value for function %s\n", fn); PRINT_DATA("Failed to find return value for function %s\n", fn);
ztest_test_fail(); ztest_test_fail();
} }
@ -325,12 +325,12 @@ int z_cleanup_mock(void)
int fail = 0; int fail = 0;
if (parameter_list.next) { if (parameter_list.next) {
PRINT("Parameter not used by mock: %s:%s\n", parameter_list.next->fn, PRINT_DATA("Parameter not used by mock: %s:%s\n", parameter_list.next->fn,
parameter_list.next->name); parameter_list.next->name);
fail = 1; fail = 1;
} }
if (return_value_list.next) { if (return_value_list.next) {
PRINT("Return value no used by mock: %s\n", return_value_list.next->fn); PRINT_DATA("Return value no used by mock: %s\n", return_value_list.next->fn);
fail = 2; fail = 2;
} }