1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "bs_tracing.h"
8 #include "bs_types.h"
9 #include "bstests.h"
10 
11 extern enum bst_result_t bst_result;
12 
13 /*
14  * @brief Mark the test as in progress
15  *
16  * Call this at the start of the test entry point.
17  *
18  * @param ... format-string and arguments to print to console
19  *
20  */
21 #define TEST_START(msg, ...)                                                                       \
22 	do {                                                                                       \
23 		bst_result = In_progress;                                                          \
24 		bs_trace_info_time(2, "Test start: " msg "\n", ##__VA_ARGS__);                     \
25 	} while (0)
26 
27 /*
28  * @brief Fail the test and exit
29  *
30  * Printf-like function that also terminates the device with an error code.
31  *
32  * @param ... format-string and arguments to print to console
33  *
34  */
35 #define TEST_FAIL(msg, ...)                                                                        \
36 	do {                                                                                       \
37 		bst_result = Failed;                                                               \
38 		bs_trace_error_time_line(msg "\n", ##__VA_ARGS__);                                 \
39 	} while (0)
40 
41 /*
42  * @brief Mark the currently-running test as "Passed"
43  *
44  * Mark the test as "passed". The execution continues after that point.
45  *
46  * @note Use this if you use backchannels (testlib/bsim/sync.h).
47  *
48  * After calling this, the executable will eventually return 0 when it exits.
49  * Unless `TEST_FAIL` is called (e.g. in a callback).
50  *
51  * @param ... format-string and arguments to print to console
52  *
53  */
54 #define TEST_PASS(msg, ...)                                                                        \
55 	do {                                                                                       \
56 		bst_result = Passed;                                                               \
57 		bs_trace_info_time(2, "Test end: " msg "\n", ##__VA_ARGS__);                       \
58 	} while (0)
59 
60 /*
61  * @brief Mark test case as passed and end execution
62  *
63  * Mark the role / test-case as "Passed" and return 0.
64  *
65  * @note DO NOT use this if you use backchannels (testlib/bsim/sync.h).
66  *
67  * @note This macro only ends execution for the current executable, not all
68  * executables in a simulation.
69  *
70  * @param ... format-string and arguments to print to console
71  *
72  */
73 #define TEST_PASS_AND_EXIT(msg, ...)                                                               \
74 	do {                                                                                       \
75 		bst_result = Passed;                                                               \
76 		bs_trace_info_time(2, "Test end: " msg "\n", ##__VA_ARGS__);                       \
77 		bs_trace_silent_exit(0);                                                           \
78 	} while (0)
79 
80 /*
81  * @brief Assert `expr` is true
82  *
83  * Assert that `expr` is true. If assertion is false, print a printf-like
84  * message to the console and fail the test. I.e. return non-zero.
85  *
86  * @note This is different than `sys/__assert.h`.
87  *
88  * @param message String to print to console
89  *
90  */
91 #define TEST_ASSERT(expr, ...)                                                                     \
92 	do {                                                                                       \
93 		if (!(expr)) {                                                                     \
94 			TEST_FAIL(__VA_ARGS__);                                                    \
95 		}                                                                                  \
96 	} while (0)
97 
98 /*
99  * @brief Print a value. Lower-level than `printk` or `LOG_xx`.
100  *
101  * Print a message to console.
102  *
103  * This can be safely called at any time in the execution of the device.
104  * Use it to print when the logging subsystem is not available, e.g. early
105  * startup or shutdown.
106  *
107  * @param ... format-string and arguments to print to console
108  *
109  */
110 #define TEST_PRINT(msg, ...)                                                                       \
111 		bs_trace_print(BS_TRACE_INFO, __FILE__, __LINE__, 0, BS_TRACE_AUTOTIME, 0,         \
112 			       msg "\n", ##__VA_ARGS__)
113