Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
47#define TC_STR_HELPER(x) #x
48#define TC_STR(x) TC_STR_HELPER(x)
49#ifdef TC_RUNID
50#define TC_PRINT_RUNID PRINT_DATA("RunID: " TC_STR(TC_RUNID) "\n")
51#else
52#define TC_PRINT_RUNID do {} while (0)
53#endif
54
55#ifndef PRINT_LINE
56#define PRINT_LINE \
57 PRINT_DATA( \
58 "============================================================" \
59 "=======\n")
60#endif
61
62/* stack size and priority for test suite task */
63#define TASK_STACK_SIZE (1024 * 2)
64
65#define FMT_ERROR "%s - %s@%d. "
66
67#define TC_PASS 0
68#define TC_FAIL 1
69#define TC_SKIP 2
70#define TC_FLAKY 3
71
72#ifndef TC_PASS_STR
73#define TC_PASS_STR "PASS"
74#endif
75#ifndef TC_FAIL_STR
76#define TC_FAIL_STR "FAIL"
77#endif
78#ifndef TC_SKIP_STR
79#define TC_SKIP_STR "SKIP"
80#endif
81#ifndef TC_FLAKY_STR
82#define TC_FLAKY_STR "FLAKY"
83#endif
84
85static inline const char *TC_RESULT_TO_STR(int result)
86{
87 switch (result) {
88 case TC_PASS:
89 return TC_PASS_STR;
90 case TC_FAIL:
91 return TC_FAIL_STR;
92 case TC_SKIP:
93 return TC_SKIP_STR;
94 case TC_FLAKY:
95 return TC_FLAKY_STR;
96 default:
97 return "?";
98 }
99}
100
103
104static inline void get_start_time_cyc(void)
105{
107}
108
109static inline void get_test_duration_ms(void)
110{
111 uint32_t spend_cycle = k_cycle_get_32() - tc_start_time;
112
113 tc_spend_time = k_cyc_to_ms_ceil32(spend_cycle);
114}
115
116#ifndef TC_ERROR
117#define TC_ERROR(fmt, ...) \
118 do { \
119 PRINT_DATA(FMT_ERROR, "FAIL", __func__, __LINE__); \
120 PRINT_DATA(fmt, ##__VA_ARGS__); \
121 } while (0)
122#endif
123
124static inline void print_nothing(const char *fmt, ...)
125{
126 ARG_UNUSED(fmt);
127}
128
129#ifndef TC_PRINT
130/* Need to check for CONFIG_ZTEST_NEW_API since the TC_PRINT
131 * is also used by the old ztest.
132 */
133#ifdef CONFIG_ZTEST_NEW_API
134#if defined(CONFIG_ZTEST_VERBOSE_OUTPUT)
135#define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
136#else
137#define TC_PRINT(fmt, ...) print_nothing(fmt, ##__VA_ARGS__)
138#endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */
139#else
140#define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
141#endif /* CONFIG_ZTEST_NEW_API */
142#endif /* TC_PRINT */
143
144#ifndef TC_SUMMARY_PRINT
145#define TC_SUMMARY_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
146#endif
147
148#ifndef TC_START_PRINT
149#ifdef CONFIG_ZTEST_NEW_API
150#if defined(CONFIG_ZTEST_VERBOSE_OUTPUT)
151#define TC_START_PRINT(name) PRINT_DATA("START - %s\n", name);
152#else
153#define TC_START_PRINT(name) print_nothing(name)
154#endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */
155#else
156#define TC_START_PRINT(name) PRINT_DATA("START - %s\n", name);
157#endif /* CONFIG_ZTEST_NEW_API */
158#endif /* TC_START_PRINT */
159
160#ifndef TC_START
161#define TC_START(name) \
162 do { \
163 TC_START_PRINT(name); \
164 } while (0)
165#endif
166
167#ifndef TC_END
168#define TC_END(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
169#endif
170
171#ifndef TC_END_PRINT
172#ifdef CONFIG_ZTEST_NEW_API
173#if defined(CONFIG_ZTEST_VERBOSE_OUTPUT)
174#define TC_END_PRINT(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__); PRINT_LINE
175#else
176#define TC_END_PRINT(result, fmt, ...) print_nothing(fmt)
177#endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */
178#else
179#define TC_END_PRINT(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__); PRINT_LINE
180#endif /* CONFIG_ZTEST_NEW_API */
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 (0)
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 (0)
224#endif
225
226#if defined(CONFIG_ARCH_POSIX)
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 (0)
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:1816
#define k_cyc_to_ms_ceil32(t)
Convert hardware cycles to milliseconds.
Definition: time_units.h:943
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
#define TC_FAIL
Definition: tc_util.h:68
static void print_nothing(const char *fmt,...)
Definition: tc_util.h:124
static void get_start_time_cyc(void)
Definition: tc_util.h:104
#define TC_FLAKY_STR
Definition: tc_util.h:82
#define TC_SKIP_STR
Definition: tc_util.h:79
#define TC_PASS_STR
Definition: tc_util.h:73
static uint32_t tc_spend_time
Definition: tc_util.h:102
static const char * TC_RESULT_TO_STR(int result)
Definition: tc_util.h:85
static void get_test_duration_ms(void)
Definition: tc_util.h:109
#define TC_SKIP
Definition: tc_util.h:69
#define TC_FLAKY
Definition: tc_util.h:70
#define TC_FAIL_STR
Definition: tc_util.h:76
static uint32_t tc_start_time
Definition: tc_util.h:101
#define TC_PASS
Definition: tc_util.h:67