Skip to content

Commit da4f4e8

Browse files
keith-zephyrnashif
authored andcommitted
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 <[email protected]>
1 parent 82a2a16 commit da4f4e8

File tree

3 files changed

+62
-57
lines changed

3 files changed

+62
-57
lines changed

subsys/testsuite/ztest/include/zephyr/ztest_assert.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <stdio.h>
1919
#include <string.h>
2020

21+
#include <zephyr/tc_util.h>
2122
#include <zephyr/ztest.h>
2223

2324
#ifdef __cplusplus
@@ -34,7 +35,8 @@ void ztest_skip_failed_assumption(void);
3435
static inline bool z_zassert_(bool cond, const char *file, int line)
3536
{
3637
if (cond == false) {
37-
PRINT("\n Assertion failed at %s:%d\n", ztest_relative_filename(file), line);
38+
PRINT_DATA("\n Assertion failed at %s:%d\n", ztest_relative_filename(file),
39+
line);
3840
ztest_test_fail();
3941
return false;
4042
}
@@ -47,7 +49,8 @@ static inline bool z_zassert_(bool cond, const char *file, int line)
4749
static inline bool z_zassume_(bool cond, const char *file, int line)
4850
{
4951
if (cond == false) {
50-
PRINT("\n Assumption failed at %s:%d\n", ztest_relative_filename(file), line);
52+
PRINT_DATA("\n Assumption failed at %s:%d\n", ztest_relative_filename(file),
53+
line);
5154
ztest_skip_failed_assumption();
5255
return false;
5356
}
@@ -60,7 +63,8 @@ static inline bool z_zassume_(bool cond, const char *file, int line)
6063
static inline bool z_zexpect_(bool cond, const char *file, int line)
6164
{
6265
if (cond == false) {
63-
PRINT("\n Expectation failed at %s:%d\n", ztest_relative_filename(file), line);
66+
PRINT_DATA("\n Expectation failed at %s:%d\n", ztest_relative_filename(file),
67+
line);
6468
ztest_test_expect_fail();
6569
return false;
6670
}
@@ -79,8 +83,8 @@ static inline bool z_zassert(bool cond, const char *default_msg, const char *fil
7983
va_list vargs;
8084

8185
va_start(vargs, msg);
82-
PRINT("\n Assertion failed at %s:%d: %s: %s\n", ztest_relative_filename(file),
83-
line, func, default_msg);
86+
PRINT_DATA("\n Assertion failed at %s:%d: %s: %s\n",
87+
ztest_relative_filename(file), line, func, default_msg);
8488
vprintk(msg, vargs);
8589
printk("\n");
8690
va_end(vargs);
@@ -89,8 +93,8 @@ static inline bool z_zassert(bool cond, const char *default_msg, const char *fil
8993
}
9094
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
9195
else {
92-
PRINT("\n Assertion succeeded at %s:%d (%s)\n", ztest_relative_filename(file),
93-
line, func);
96+
PRINT_DATA("\n Assertion succeeded at %s:%d (%s)\n",
97+
ztest_relative_filename(file), line, func);
9498
}
9599
#endif
96100
return true;
@@ -103,8 +107,8 @@ static inline bool z_zassume(bool cond, const char *default_msg, const char *fil
103107
va_list vargs;
104108

105109
va_start(vargs, msg);
106-
PRINT("\n Assumption failed at %s:%d: %s: %s\n", ztest_relative_filename(file),
107-
line, func, default_msg);
110+
PRINT_DATA("\n Assumption failed at %s:%d: %s: %s\n",
111+
ztest_relative_filename(file), line, func, default_msg);
108112
vprintk(msg, vargs);
109113
printk("\n");
110114
va_end(vargs);
@@ -113,8 +117,8 @@ static inline bool z_zassume(bool cond, const char *default_msg, const char *fil
113117
}
114118
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
115119
else {
116-
PRINT("\n Assumption succeeded at %s:%d (%s)\n", ztest_relative_filename(file),
117-
line, func);
120+
PRINT_DATA("\n Assumption succeeded at %s:%d (%s)\n",
121+
ztest_relative_filename(file), line, func);
118122
}
119123
#endif
120124
return true;
@@ -127,8 +131,8 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
127131
va_list vargs;
128132

129133
va_start(vargs, msg);
130-
PRINT("\n Expectation failed at %s:%d: %s: %s\n", ztest_relative_filename(file),
131-
line, func, default_msg);
134+
PRINT_DATA("\n Expectation failed at %s:%d: %s: %s\n",
135+
ztest_relative_filename(file), line, func, default_msg);
132136
vprintk(msg, vargs);
133137
printk("\n");
134138
va_end(vargs);
@@ -137,8 +141,8 @@ static inline bool z_zexpect(bool cond, const char *default_msg, const char *fil
137141
}
138142
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
139143
else {
140-
PRINT("\n Expectation succeeded at %s:%d (%s)\n", ztest_relative_filename(file),
141-
line, func);
144+
PRINT_DATA("\n Expectation succeeded at %s:%d (%s)\n",
145+
ztest_relative_filename(file), line, func);
142146
}
143147
#endif
144148
return true;

subsys/testsuite/ztest/src/ztest.c

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ static int cleanup_test(struct ztest_unit_test *test)
9292
#endif
9393

9494
if (!ret && mock_status == 1) {
95-
PRINT("Test %s failed: Unused mock parameter values\n", test->name);
95+
PRINT_DATA("Test %s failed: Unused mock parameter values\n", test->name);
9696
ret = TC_FAIL;
9797
} else if (!ret && mock_status == 2) {
98-
PRINT("Test %s failed: Unused mock return values\n", test->name);
98+
PRINT_DATA("Test %s failed: Unused mock return values\n", test->name);
9999
ret = TC_FAIL;
100100
} else {
101101
;
@@ -414,17 +414,17 @@ void ztest_test_fail(void)
414414
{
415415
switch (cur_phase) {
416416
case TEST_PHASE_SETUP:
417-
PRINT(" at %s function\n", get_friendly_phase_name(cur_phase));
417+
PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
418418
longjmp(test_suite_fail, 1);
419419
case TEST_PHASE_BEFORE:
420420
case TEST_PHASE_TEST:
421-
PRINT(" at %s function\n", get_friendly_phase_name(cur_phase));
421+
PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
422422
longjmp(test_fail, 1);
423423
case TEST_PHASE_AFTER:
424424
case TEST_PHASE_TEARDOWN:
425425
case TEST_PHASE_FRAMEWORK:
426-
PRINT(" ERROR: cannot fail in test phase '%s()', bailing\n",
427-
get_friendly_phase_name(cur_phase));
426+
PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
427+
get_friendly_phase_name(cur_phase));
428428
longjmp(stack_fail, 1);
429429
}
430430
}
@@ -435,8 +435,8 @@ void ztest_test_pass(void)
435435
if (cur_phase == TEST_PHASE_TEST) {
436436
longjmp(test_pass, 1);
437437
}
438-
PRINT(" ERROR: cannot pass in test phase '%s()', bailing\n",
439-
get_friendly_phase_name(cur_phase));
438+
PRINT_DATA(" ERROR: cannot pass in test phase '%s()', bailing\n",
439+
get_friendly_phase_name(cur_phase));
440440
longjmp(stack_fail, 1);
441441
}
442442
EXPORT_SYMBOL(ztest_test_pass);
@@ -449,8 +449,8 @@ void ztest_test_skip(void)
449449
case TEST_PHASE_TEST:
450450
longjmp(test_skip, 1);
451451
default:
452-
PRINT(" ERROR: cannot skip in test phase '%s()', bailing\n",
453-
get_friendly_phase_name(cur_phase));
452+
PRINT_DATA(" ERROR: cannot skip in test phase '%s()', bailing\n",
453+
get_friendly_phase_name(cur_phase));
454454
longjmp(stack_fail, 1);
455455
}
456456
}
@@ -462,17 +462,17 @@ void ztest_test_expect_fail(void)
462462

463463
switch (cur_phase) {
464464
case TEST_PHASE_SETUP:
465-
PRINT(" at %s function\n", get_friendly_phase_name(cur_phase));
465+
PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
466466
break;
467467
case TEST_PHASE_BEFORE:
468468
case TEST_PHASE_TEST:
469-
PRINT(" at %s function\n", get_friendly_phase_name(cur_phase));
469+
PRINT_DATA(" at %s function\n", get_friendly_phase_name(cur_phase));
470470
break;
471471
case TEST_PHASE_AFTER:
472472
case TEST_PHASE_TEARDOWN:
473473
case TEST_PHASE_FRAMEWORK:
474-
PRINT(" ERROR: cannot fail in test phase '%s()', bailing\n",
475-
get_friendly_phase_name(cur_phase));
474+
PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
475+
get_friendly_phase_name(cur_phase));
476476
longjmp(stack_fail, 1);
477477
}
478478
}
@@ -572,8 +572,8 @@ void ztest_test_fail(void)
572572
test_finalize();
573573
break;
574574
default:
575-
PRINT(" ERROR: cannot fail in test phase '%s()', bailing\n",
576-
get_friendly_phase_name(cur_phase));
575+
PRINT_DATA(" ERROR: cannot fail in test phase '%s()', bailing\n",
576+
get_friendly_phase_name(cur_phase));
577577
test_status = ZTEST_STATUS_CRITICAL_ERROR;
578578
break;
579579
}
@@ -588,8 +588,8 @@ void ztest_test_pass(void)
588588
test_finalize();
589589
break;
590590
default:
591-
PRINT(" ERROR: cannot pass in test phase '%s()', bailing\n",
592-
get_friendly_phase_name(cur_phase));
591+
PRINT_DATA(" ERROR: cannot pass in test phase '%s()', bailing\n",
592+
get_friendly_phase_name(cur_phase));
593593
test_status = ZTEST_STATUS_CRITICAL_ERROR;
594594
if (cur_phase == TEST_PHASE_BEFORE) {
595595
test_finalize();
@@ -610,8 +610,8 @@ void ztest_test_skip(void)
610610
test_finalize();
611611
break;
612612
default:
613-
PRINT(" ERROR: cannot skip in test phase '%s()', bailing\n",
614-
get_friendly_phase_name(cur_phase));
613+
PRINT_DATA(" ERROR: cannot skip in test phase '%s()', bailing\n",
614+
get_friendly_phase_name(cur_phase));
615615
test_status = ZTEST_STATUS_CRITICAL_ERROR;
616616
break;
617617
}
@@ -807,7 +807,7 @@ static int z_ztest_run_test_suite_ptr(struct ztest_suite_node *suite, bool shuff
807807

808808
#ifndef KERNEL
809809
if (setjmp(stack_fail)) {
810-
PRINT("TESTSUITE crashed.\n");
810+
PRINT_DATA("TESTSUITE crashed.\n");
811811
test_status = ZTEST_STATUS_CRITICAL_ERROR;
812812
end_report();
813813
exit(1);
@@ -1157,17 +1157,18 @@ void ztest_verify_all_test_suites_ran(void)
11571157
for (suite = _ztest_suite_node_list_start; suite < _ztest_suite_node_list_end;
11581158
++suite) {
11591159
if (suite->stats->run_count < 1) {
1160-
PRINT("ERROR: Test suite '%s' did not run.\n", suite->name);
1160+
PRINT_DATA("ERROR: Test suite '%s' did not run.\n", suite->name);
11611161
all_tests_run = false;
11621162
}
11631163
}
11641164

11651165
for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) {
11661166
suite = ztest_find_test_suite(test->test_suite_name);
11671167
if (suite == NULL) {
1168-
PRINT("ERROR: Test '%s' assigned to test suite '%s' which doesn't "
1169-
"exist\n",
1170-
test->name, test->test_suite_name);
1168+
PRINT_DATA("ERROR: Test '%s' assigned to test suite '%s' which "
1169+
"doesn't "
1170+
"exist\n",
1171+
test->name, test->test_suite_name);
11711172
all_tests_run = false;
11721173
}
11731174
}
@@ -1180,7 +1181,7 @@ void ztest_verify_all_test_suites_ran(void)
11801181
for (test = _ztest_unit_test_list_start; test < _ztest_unit_test_list_end; ++test) {
11811182
if (test->stats->fail_count + test->stats->pass_count + test->stats->skip_count !=
11821183
test->stats->run_count) {
1183-
PRINT("Bad stats for %s.%s\n", test->test_suite_name, test->name);
1184+
PRINT_DATA("Bad stats for %s.%s\n", test->test_suite_name, test->name);
11841185
test_status = 1;
11851186
}
11861187
}
@@ -1430,11 +1431,11 @@ int main(void)
14301431
}
14311432
state.boots += 1;
14321433
if (test_status == 0) {
1433-
PRINT("Reset board #%u to test again\n", state.boots);
1434+
PRINT_DATA("Reset board #%u to test again\n", state.boots);
14341435
k_msleep(10);
14351436
sys_reboot(SYS_REBOOT_COLD);
14361437
} else {
1437-
PRINT("Failed after %u attempts\n", state.boots);
1438+
PRINT_DATA("Failed after %u attempts\n", state.boots);
14381439
state.boots = 0;
14391440
}
14401441
}

subsys/testsuite/ztest/src/ztest_mock.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static struct parameter *alloc_parameter(void)
3333

3434
param = calloc(1, sizeof(struct parameter));
3535
if (!param) {
36-
PRINT("Failed to allocate mock parameter\n");
36+
PRINT_DATA("Failed to allocate mock parameter\n");
3737
ztest_test_fail();
3838
}
3939

@@ -205,7 +205,7 @@ void z_ztest_check_expected_value(const char *fn, const char *name, uintptr_t va
205205

206206
param = find_and_delete_value(&parameter_list, fn, name);
207207
if (!param) {
208-
PRINT("Failed to find parameter %s for %s\n", name, fn);
208+
PRINT_DATA("Failed to find parameter %s for %s\n", name, fn);
209209
ztest_test_fail();
210210
}
211211

@@ -216,8 +216,8 @@ void z_ztest_check_expected_value(const char *fn, const char *name, uintptr_t va
216216
/* We need to cast these values since the toolchain doesn't
217217
* provide inttypes.h
218218
*/
219-
PRINT("%s:%s received wrong value: Got %lu, expected %lu\n", fn, name,
220-
(unsigned long)val, (unsigned long)expected);
219+
PRINT_DATA("%s:%s received wrong value: Got %lu, expected %lu\n", fn, name,
220+
(unsigned long)val, (unsigned long)expected);
221221
ztest_test_fail();
222222
}
223223
}
@@ -234,7 +234,7 @@ void z_ztest_check_expected_data(const char *fn, const char *name, void *data, u
234234

235235
param = find_and_delete_value(&parameter_list, fn, name);
236236
if (!param) {
237-
PRINT("Failed to find parameter %s for %s\n", name, fn);
237+
PRINT_DATA("Failed to find parameter %s for %s\n", name, fn);
238238
/* No return from this function but for coverity reasons
239239
* put a return after to avoid the warning of a null
240240
* dereference of param below.
@@ -247,14 +247,14 @@ void z_ztest_check_expected_data(const char *fn, const char *name, void *data, u
247247
free_parameter(param);
248248

249249
if (expected == NULL && data != NULL) {
250-
PRINT("%s:%s received data while expected null pointer\n", fn, name);
250+
PRINT_DATA("%s:%s received data while expected null pointer\n", fn, name);
251251
ztest_test_fail();
252252
} else if (data == NULL && expected != NULL) {
253-
PRINT("%s:%s received null pointer while expected data\n", fn, name);
253+
PRINT_DATA("%s:%s received null pointer while expected data\n", fn, name);
254254
ztest_test_fail();
255255
} else if (data != NULL) {
256256
if (memcmp(data, expected, length) != 0) {
257-
PRINT("%s:%s data provided don't match\n", fn, name);
257+
PRINT_DATA("%s:%s data provided don't match\n", fn, name);
258258
ztest_test_fail();
259259
}
260260
}
@@ -271,14 +271,14 @@ void z_ztest_copy_return_data(const char *fn, const char *name, void *data, uint
271271
void *return_data;
272272

273273
if (data == NULL) {
274-
PRINT("%s:%s received null pointer\n", fn, name);
274+
PRINT_DATA("%s:%s received null pointer\n", fn, name);
275275
ztest_test_fail();
276276
return;
277277
}
278278

279279
param = find_and_delete_value(&parameter_list, fn, name);
280280
if (!param) {
281-
PRINT("Failed to find parameter %s for %s\n", name, fn);
281+
PRINT_DATA("Failed to find parameter %s for %s\n", name, fn);
282282
memset(data, 0, length);
283283
ztest_test_fail();
284284
} else {
@@ -299,7 +299,7 @@ uintptr_t z_ztest_get_return_value(const char *fn)
299299
struct parameter *param = find_and_delete_value(&return_value_list, fn, "");
300300

301301
if (!param) {
302-
PRINT("Failed to find return value for function %s\n", fn);
302+
PRINT_DATA("Failed to find return value for function %s\n", fn);
303303
ztest_test_fail();
304304
}
305305

@@ -325,12 +325,12 @@ int z_cleanup_mock(void)
325325
int fail = 0;
326326

327327
if (parameter_list.next) {
328-
PRINT("Parameter not used by mock: %s:%s\n", parameter_list.next->fn,
329-
parameter_list.next->name);
328+
PRINT_DATA("Parameter not used by mock: %s:%s\n", parameter_list.next->fn,
329+
parameter_list.next->name);
330330
fail = 1;
331331
}
332332
if (return_value_list.next) {
333-
PRINT("Return value no used by mock: %s\n", return_value_list.next->fn);
333+
PRINT_DATA("Return value no used by mock: %s\n", return_value_list.next->fn);
334334
fail = 2;
335335
}
336336

0 commit comments

Comments
 (0)