Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
tc_util.h
Go to the documentation of this file.
1/* tc_utilities.h - testcase utilities header file */
2
3/*
4 * Copyright (c) 2012-2015 Wind River Systems, Inc.
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#ifndef ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_
10#define ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_
11
12#include <zephyr/kernel.h>
13
14#include <string.h>
15#ifdef CONFIG_SHELL
16#include <zephyr/shell/shell.h>
17#endif
18#include <zephyr/sys/printk.h>
19
20#if defined CONFIG_ZTEST_TC_UTIL_USER_OVERRIDE
21#include <tc_util_user_override.h>
22#endif
23
24#ifndef PRINT_DATA
25#define PRINT_DATA(fmt, ...) printk(fmt, ##__VA_ARGS__)
26#endif
27
28#if defined CONFIG_ARCH_POSIX
29#include <posix_board_if.h>
30#endif
31
60#define TC_STR_HELPER(x) #x
61#define TC_STR(x) TC_STR_HELPER(x)
62#ifdef TC_RUNID
63#define TC_PRINT_RUNID PRINT_DATA("RunID: " TC_STR(TC_RUNID) "\n")
64#define TC_PRINT_RUNID_START PRINT_DATA("RunID started: " TC_STR(TC_RUNID) "\n")
65#else
66#define TC_PRINT_RUNID do {} while (false)
67#define TC_PRINT_RUNID_START do {} while (false)
68#endif
69
70#ifndef PRINT_LINE
71#define PRINT_LINE \
72 PRINT_DATA( \
73 "============================================================" \
74 "=======\n")
75#endif
76
77/* stack size and priority for test suite task */
78#define TASK_STACK_SIZE (1024 * 2)
79
80#define FMT_ERROR "%s - %s@%d. "
81
82#define TC_PASS 0
83#define TC_FAIL 1
84#define TC_SKIP 2
85#define TC_FLAKY 3
86
87#ifndef TC_PASS_STR
88#define TC_PASS_STR "PASS"
89#endif
90#ifndef TC_FAIL_STR
91#define TC_FAIL_STR "FAIL"
92#endif
93#ifndef TC_SKIP_STR
94#define TC_SKIP_STR "SKIP"
95#endif
96#ifndef TC_FLAKY_STR
97#define TC_FLAKY_STR "FLAKY"
98#endif
99
100static inline const char *TC_RESULT_TO_STR(int result)
101{
102 switch (result) {
103 case TC_PASS:
104 return TC_PASS_STR;
105 case TC_FAIL:
106 return TC_FAIL_STR;
107 case TC_SKIP:
108 return TC_SKIP_STR;
109 case TC_FLAKY:
110 return TC_FLAKY_STR;
111 default:
112 return "?";
113 }
114}
115
118
119static inline void get_start_time_cyc(void)
120{
122}
123
124static inline void get_test_duration_ms(void)
125{
126 uint32_t spend_cycle = k_cycle_get_32() - tc_start_time;
127
128 tc_spend_time = k_cyc_to_ms_ceil32(spend_cycle);
129}
130
131#ifndef TC_ERROR
132#define TC_ERROR(fmt, ...) \
133 do { \
134 PRINT_DATA(FMT_ERROR, "FAIL", __func__, __LINE__); \
135 PRINT_DATA(fmt, ##__VA_ARGS__); \
136 } while (false)
137#endif
138
139static inline void print_nothing(const char *fmt, ...)
140{
141 ARG_UNUSED(fmt);
142}
143
144#ifndef TC_PRINT
145#if defined(CONFIG_ZTEST_VERBOSE_OUTPUT)
146#define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
147#else
148#define TC_PRINT(fmt, ...) print_nothing(fmt, ##__VA_ARGS__)
149#endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */
150#endif /* TC_PRINT */
151
152#ifndef TC_SUMMARY_PRINT
153#define TC_SUMMARY_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
154#endif
155
156#ifndef TC_START_PRINT
157#if defined(CONFIG_ZTEST_VERBOSE_OUTPUT)
158#define TC_START_PRINT(name) PRINT_DATA("START - %s\n", name);
159#else
160#define TC_START_PRINT(name) print_nothing(name)
161#endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */
162#endif /* TC_START_PRINT */
163
164#ifndef TC_START
165#define TC_START(name) \
166 do { \
167 TC_START_PRINT(name); \
168 } while (0)
169#endif
170
171#ifndef TC_END
172#define TC_END(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
173#endif
174
175#ifndef TC_END_PRINT
176#if defined(CONFIG_ZTEST_VERBOSE_OUTPUT)
177#define TC_END_PRINT(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__); PRINT_LINE
178#else
179#define TC_END_PRINT(result, fmt, ...) print_nothing(fmt)
180#endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */
181#endif /* TC_END_PRINT */
182
183/* prints result and the function name */
184#ifndef Z_TC_END_RESULT
185#define Z_TC_END_RESULT(result, func) \
186 do { \
187 TC_END_PRINT(result, " %s - %s in %u.%03u seconds\n", \
188 TC_RESULT_TO_STR(result), func, tc_spend_time/1000, \
189 tc_spend_time%1000); \
190 } while (0)
191#endif
192
193#ifndef TC_END_RESULT
194#define TC_END_RESULT(result) \
195 Z_TC_END_RESULT((result), __func__)
196#endif
197
198#ifndef TC_END_RESULT_CUSTOM
199#define TC_END_RESULT_CUSTOM(result, func) \
200 Z_TC_END_RESULT((result), func)
201#endif
202
203#ifndef TC_SUITE_PRINT
204#define TC_SUITE_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
205#endif
206
207#ifndef TC_SUITE_START
208#define TC_SUITE_START(name) \
209 do { \
210 TC_SUITE_PRINT("Running TESTSUITE %s\n", name); \
211 PRINT_LINE; \
212 } while (false)
213#endif
214
215#ifndef TC_SUITE_END
216#define TC_SUITE_END(name, result) \
217 do { \
218 if (result != TC_FAIL) { \
219 TC_SUITE_PRINT("TESTSUITE %s succeeded\n", name); \
220 } else { \
221 TC_SUITE_PRINT("TESTSUITE %s failed.\n", name); \
222 } \
223 } while (false)
224#endif
225
226#if defined(CONFIG_ARCH_POSIX) && !defined(CONFIG_ZTEST_SHELL)
228#define TC_END_POST(result) do { \
229 LOG_PANIC(); \
230 posix_exit(result); \
231} while (0)
232#else
233#define TC_END_POST(result)
234#endif /* CONFIG_ARCH_POSIX */
235
236#ifndef TC_END_REPORT
237#define TC_END_REPORT(result) \
238 do { \
239 PRINT_LINE; \
240 TC_PRINT_RUNID; \
241 TC_END(result, \
242 "PROJECT EXECUTION %s\n", \
243 (result) == TC_PASS ? "SUCCESSFUL" : "FAILED"); \
244 TC_END_POST(result); \
245 } while (false)
246#endif
247
248#if defined(CONFIG_SHELL)
249#define TC_CMD_DEFINE(name) \
250 static int cmd_##name(const struct shell *sh, size_t argc, \
251 char **argv) \
252 { \
253 TC_START(__func__); \
254 name(); \
255 TC_END_RESULT(TC_PASS); \
256 return 0; \
257 }
258#define TC_CMD_ITEM(name) cmd_##name
259#else
260#define TC_CMD_DEFINE(name) \
261 int cmd_##name(int argc, char *argv[]) \
262 { \
263 TC_START(__func__); \
264 name(); \
265 TC_END_RESULT(TC_PASS); \
266 return 0; \
267 }
268#define TC_CMD_ITEM(name) {STRINGIFY(name), cmd_##name, "none"}
269#endif
270
271#endif /* ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_ */
static uint32_t k_cycle_get_32(void)
Read the hardware clock.
Definition kernel.h:2288
#define k_cyc_to_ms_ceil32(t)
Convert hardware cycles to milliseconds.
Definition time_units.h:1302
Public kernel APIs.
Header file for the logger control functions.
Header file for the shell subsystem.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
#define TC_FAIL
Definition tc_util.h:83
static void print_nothing(const char *fmt,...)
Definition tc_util.h:139
static void get_start_time_cyc(void)
Definition tc_util.h:119
#define TC_FLAKY_STR
Definition tc_util.h:97
#define TC_SKIP_STR
Definition tc_util.h:94
#define TC_PASS_STR
Definition tc_util.h:88
static uint32_t tc_spend_time
Definition tc_util.h:117
static const char * TC_RESULT_TO_STR(int result)
Definition tc_util.h:100
static void get_test_duration_ms(void)
Definition tc_util.h:124
#define TC_SKIP
Definition tc_util.h:84
#define TC_FLAKY
Definition tc_util.h:85
#define TC_FAIL_STR
Definition tc_util.h:91
static uint32_t tc_start_time
Definition tc_util.h:116
#define TC_PASS
Definition tc_util.h:82