-
Notifications
You must be signed in to change notification settings - Fork 632
/
tc_util.h
256 lines (220 loc) · 6.24 KB
/
tc_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* tc_utilities.h - testcase utilities header file */
/*
* Copyright (c) 2012-2015 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_
#define ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_
#include <zephyr/kernel.h>
#include <string.h>
#ifdef CONFIG_SHELL
#include <zephyr/shell/shell.h>
#endif
#include <zephyr/sys/printk.h>
#if defined CONFIG_ZTEST_TC_UTIL_USER_OVERRIDE
#include <tc_util_user_override.h>
#endif
#ifndef PRINT_DATA
#define PRINT_DATA(fmt, ...) printk(fmt, ##__VA_ARGS__)
#endif
#if defined CONFIG_ARCH_POSIX
#include "posix_board_if.h"
#endif
/**
* @def TC_PRINT_RUNID
* @brief Report a Run ID
*
* When the CPP symbol \c TC_RUNID is defined (for example, from the
* compile environment), print the defined string ``RunID:
* <TC_RUNID>`` when called (TC_END_REPORT() will also call it).
*
* This is used mainly when automating the execution and running of
* multiple test cases, to verify that the expected image is being
* executed (as sometimes the targets fail to flash or reset
* properly).
*
* TC_RUNID is any string, that will be converted to a string literal.
*/
#define TC_STR_HELPER(x) #x
#define TC_STR(x) TC_STR_HELPER(x)
#ifdef TC_RUNID
#define TC_PRINT_RUNID PRINT_DATA("RunID: " TC_STR(TC_RUNID) "\n")
#else
#define TC_PRINT_RUNID do {} while (0)
#endif
#ifndef PRINT_LINE
#define PRINT_LINE \
PRINT_DATA( \
"============================================================" \
"=======\n")
#endif
/* stack size and priority for test suite task */
#define TASK_STACK_SIZE (1024 * 2)
#define FMT_ERROR "%s - %s@%d. "
#define TC_PASS 0
#define TC_FAIL 1
#define TC_SKIP 2
#define TC_FLAKY 3
#ifndef TC_PASS_STR
#define TC_PASS_STR "PASS"
#endif
#ifndef TC_FAIL_STR
#define TC_FAIL_STR "FAIL"
#endif
#ifndef TC_SKIP_STR
#define TC_SKIP_STR "SKIP"
#endif
#ifndef TC_FLAKY_STR
#define TC_FLAKY_STR "FLAKY"
#endif
static inline const char *TC_RESULT_TO_STR(int result)
{
switch (result) {
case TC_PASS:
return TC_PASS_STR;
case TC_FAIL:
return TC_FAIL_STR;
case TC_SKIP:
return TC_SKIP_STR;
case TC_FLAKY:
return TC_FLAKY_STR;
default:
return "?";
}
}
static uint32_t tc_start_time;
static uint32_t tc_spend_time;
static inline void get_start_time_cyc(void)
{
tc_start_time = k_cycle_get_32();
}
static inline void get_test_duration_ms(void)
{
uint32_t spend_cycle = k_cycle_get_32() - tc_start_time;
tc_spend_time = k_cyc_to_ms_ceil32(spend_cycle);
}
#ifndef TC_ERROR
#define TC_ERROR(fmt, ...) \
do { \
PRINT_DATA(FMT_ERROR, "FAIL", __func__, __LINE__); \
PRINT_DATA(fmt, ##__VA_ARGS__); \
} while (0)
#endif
static inline void print_nothing(const char *fmt, ...)
{
ARG_UNUSED(fmt);
}
#ifndef TC_PRINT
#if defined(CONFIG_ZTEST_VERBOSE_OUTPUT)
#define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
#else
#define TC_PRINT(fmt, ...) print_nothing(fmt, ##__VA_ARGS__)
#endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */
#endif /* TC_PRINT */
#ifndef TC_SUMMARY_PRINT
#define TC_SUMMARY_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
#endif
#ifndef TC_START_PRINT
#if defined(CONFIG_ZTEST_VERBOSE_OUTPUT)
#define TC_START_PRINT(name) PRINT_DATA("START - %s\n", name);
#else
#define TC_START_PRINT(name) print_nothing(name)
#endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */
#endif /* TC_START_PRINT */
#ifndef TC_START
#define TC_START(name) \
do { \
TC_START_PRINT(name); \
} while (0)
#endif
#ifndef TC_END
#define TC_END(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
#endif
#ifndef TC_END_PRINT
#if defined(CONFIG_ZTEST_VERBOSE_OUTPUT)
#define TC_END_PRINT(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__); PRINT_LINE
#else
#define TC_END_PRINT(result, fmt, ...) print_nothing(fmt)
#endif /* CONFIG_ZTEST_VERBOSE_OUTPUT */
#endif /* TC_END_PRINT */
/* prints result and the function name */
#ifndef Z_TC_END_RESULT
#define Z_TC_END_RESULT(result, func) \
do { \
TC_END_PRINT(result, " %s - %s in %u.%03u seconds\n", \
TC_RESULT_TO_STR(result), func, tc_spend_time/1000, \
tc_spend_time%1000); \
} while (0)
#endif
#ifndef TC_END_RESULT
#define TC_END_RESULT(result) \
Z_TC_END_RESULT((result), __func__)
#endif
#ifndef TC_END_RESULT_CUSTOM
#define TC_END_RESULT_CUSTOM(result, func) \
Z_TC_END_RESULT((result), func)
#endif
#ifndef TC_SUITE_PRINT
#define TC_SUITE_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
#endif
#ifndef TC_SUITE_START
#define TC_SUITE_START(name) \
do { \
TC_SUITE_PRINT("Running TESTSUITE %s\n", name); \
PRINT_LINE; \
} while (0)
#endif
#ifndef TC_SUITE_END
#define TC_SUITE_END(name, result) \
do { \
if (result != TC_FAIL) { \
TC_SUITE_PRINT("TESTSUITE %s succeeded\n", name); \
} else { \
TC_SUITE_PRINT("TESTSUITE %s failed.\n", name); \
} \
} while (0)
#endif
#if defined(CONFIG_ARCH_POSIX)
#include <zephyr/logging/log_ctrl.h>
#define TC_END_POST(result) do { \
LOG_PANIC(); \
posix_exit(result); \
} while (0)
#else
#define TC_END_POST(result)
#endif /* CONFIG_ARCH_POSIX */
#ifndef TC_END_REPORT
#define TC_END_REPORT(result) \
do { \
PRINT_LINE; \
TC_PRINT_RUNID; \
TC_END(result, \
"PROJECT EXECUTION %s\n", \
(result) == TC_PASS ? "SUCCESSFUL" : "FAILED"); \
TC_END_POST(result); \
} while (0)
#endif
#if defined(CONFIG_SHELL)
#define TC_CMD_DEFINE(name) \
static int cmd_##name(const struct shell *sh, size_t argc, \
char **argv) \
{ \
TC_START(__func__); \
name(); \
TC_END_RESULT(TC_PASS); \
return 0; \
}
#define TC_CMD_ITEM(name) cmd_##name
#else
#define TC_CMD_DEFINE(name) \
int cmd_##name(int argc, char *argv[]) \
{ \
TC_START(__func__); \
name(); \
TC_END_RESULT(TC_PASS); \
return 0; \
}
#define TC_CMD_ITEM(name) {STRINGIFY(name), cmd_##name, "none"}
#endif
#endif /* ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_ */