Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * kselftest.h: low-level kselftest framework to include from
4 * selftest programs. When possible, please use
5 * kselftest_harness.h instead.
6 *
7 * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
8 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
9 *
10 * Using this API consists of first counting how many tests your code
11 * has to run, and then starting up the reporting:
12 *
13 * ksft_print_header();
14 * ksft_set_plan(total_number_of_tests);
15 *
16 * For each test, report any progress, debugging, etc with:
17 *
18 * ksft_print_msg(fmt, ...);
19 *
20 * and finally report the pass/fail/skip/xfail state of the test with one of:
21 *
22 * ksft_test_result(condition, fmt, ...);
23 * ksft_test_result_pass(fmt, ...);
24 * ksft_test_result_fail(fmt, ...);
25 * ksft_test_result_skip(fmt, ...);
26 * ksft_test_result_xfail(fmt, ...);
27 * ksft_test_result_error(fmt, ...);
28 *
29 * When all tests are finished, clean up and exit the program with one of:
30 *
31 * ksft_finished();
32 * ksft_exit(condition);
33 * ksft_exit_pass();
34 * ksft_exit_fail();
35 *
36 * If the program wants to report details on why the entire program has
37 * failed, it can instead exit with a message (this is usually done when
38 * the program is aborting before finishing all tests):
39 *
40 * ksft_exit_fail_msg(fmt, ...);
41 *
42 */
43#ifndef __KSELFTEST_H
44#define __KSELFTEST_H
45
46#include <errno.h>
47#include <stdlib.h>
48#include <unistd.h>
49#include <stdarg.h>
50#include <stdio.h>
51
52#ifndef ARRAY_SIZE
53#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
54#endif
55
56/*
57 * gcc cpuid.h provides __cpuid_count() since v4.4.
58 * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
59 *
60 * Provide local define for tests needing __cpuid_count() because
61 * selftests need to work in older environments that do not yet
62 * have __cpuid_count().
63 */
64#ifndef __cpuid_count
65#define __cpuid_count(level, count, a, b, c, d) \
66 __asm__ __volatile__ ("cpuid\n\t" \
67 : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
68 : "0" (level), "2" (count))
69#endif
70
71/* define kselftest exit codes */
72#define KSFT_PASS 0
73#define KSFT_FAIL 1
74#define KSFT_XFAIL 2
75#define KSFT_XPASS 3
76#define KSFT_SKIP 4
77
78/* counters */
79struct ksft_count {
80 unsigned int ksft_pass;
81 unsigned int ksft_fail;
82 unsigned int ksft_xfail;
83 unsigned int ksft_xpass;
84 unsigned int ksft_xskip;
85 unsigned int ksft_error;
86};
87
88static struct ksft_count ksft_cnt;
89static unsigned int ksft_plan;
90
91static inline unsigned int ksft_test_num(void)
92{
93 return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
94 ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
95 ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
96}
97
98static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
99static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
100static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
101static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
102static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
103static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
104
105static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
106static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
107static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
108static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
109static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
110static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
111
112static inline void ksft_print_header(void)
113{
114 if (!(getenv("KSFT_TAP_LEVEL")))
115 printf("TAP version 13\n");
116}
117
118static inline void ksft_set_plan(unsigned int plan)
119{
120 ksft_plan = plan;
121 printf("1..%d\n", ksft_plan);
122}
123
124static inline void ksft_print_cnts(void)
125{
126 if (ksft_plan != ksft_test_num())
127 printf("# Planned tests != run tests (%u != %u)\n",
128 ksft_plan, ksft_test_num());
129 printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n",
130 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
131 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
132 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
133}
134
135static inline void ksft_print_msg(const char *msg, ...)
136{
137 int saved_errno = errno;
138 va_list args;
139
140 va_start(args, msg);
141 printf("# ");
142 errno = saved_errno;
143 vprintf(msg, args);
144 va_end(args);
145}
146
147static inline void ksft_test_result_pass(const char *msg, ...)
148{
149 int saved_errno = errno;
150 va_list args;
151
152 ksft_cnt.ksft_pass++;
153
154 va_start(args, msg);
155 printf("ok %d ", ksft_test_num());
156 errno = saved_errno;
157 vprintf(msg, args);
158 va_end(args);
159}
160
161static inline void ksft_test_result_fail(const char *msg, ...)
162{
163 int saved_errno = errno;
164 va_list args;
165
166 ksft_cnt.ksft_fail++;
167
168 va_start(args, msg);
169 printf("not ok %d ", ksft_test_num());
170 errno = saved_errno;
171 vprintf(msg, args);
172 va_end(args);
173}
174
175/**
176 * ksft_test_result() - Report test success based on truth of condition
177 *
178 * @condition: if true, report test success, otherwise failure.
179 */
180#define ksft_test_result(condition, fmt, ...) do { \
181 if (!!(condition)) \
182 ksft_test_result_pass(fmt, ##__VA_ARGS__);\
183 else \
184 ksft_test_result_fail(fmt, ##__VA_ARGS__);\
185 } while (0)
186
187static inline void ksft_test_result_xfail(const char *msg, ...)
188{
189 int saved_errno = errno;
190 va_list args;
191
192 ksft_cnt.ksft_xfail++;
193
194 va_start(args, msg);
195 printf("ok %d # XFAIL ", ksft_test_num());
196 errno = saved_errno;
197 vprintf(msg, args);
198 va_end(args);
199}
200
201static inline void ksft_test_result_skip(const char *msg, ...)
202{
203 int saved_errno = errno;
204 va_list args;
205
206 ksft_cnt.ksft_xskip++;
207
208 va_start(args, msg);
209 printf("ok %d # SKIP ", ksft_test_num());
210 errno = saved_errno;
211 vprintf(msg, args);
212 va_end(args);
213}
214
215/* TODO: how does "error" differ from "fail" or "skip"? */
216static inline void ksft_test_result_error(const char *msg, ...)
217{
218 int saved_errno = errno;
219 va_list args;
220
221 ksft_cnt.ksft_error++;
222
223 va_start(args, msg);
224 printf("not ok %d # error ", ksft_test_num());
225 errno = saved_errno;
226 vprintf(msg, args);
227 va_end(args);
228}
229
230static inline int ksft_exit_pass(void)
231{
232 ksft_print_cnts();
233 exit(KSFT_PASS);
234}
235
236static inline int ksft_exit_fail(void)
237{
238 ksft_print_cnts();
239 exit(KSFT_FAIL);
240}
241
242/**
243 * ksft_exit() - Exit selftest based on truth of condition
244 *
245 * @condition: if true, exit self test with success, otherwise fail.
246 */
247#define ksft_exit(condition) do { \
248 if (!!(condition)) \
249 ksft_exit_pass(); \
250 else \
251 ksft_exit_fail(); \
252 } while (0)
253
254/**
255 * ksft_finished() - Exit selftest with success if all tests passed
256 */
257#define ksft_finished() \
258 ksft_exit(ksft_plan == \
259 ksft_cnt.ksft_pass + \
260 ksft_cnt.ksft_xfail + \
261 ksft_cnt.ksft_xskip)
262
263static inline int ksft_exit_fail_msg(const char *msg, ...)
264{
265 int saved_errno = errno;
266 va_list args;
267
268 va_start(args, msg);
269 printf("Bail out! ");
270 errno = saved_errno;
271 vprintf(msg, args);
272 va_end(args);
273
274 ksft_print_cnts();
275 exit(KSFT_FAIL);
276}
277
278static inline int ksft_exit_xfail(void)
279{
280 ksft_print_cnts();
281 exit(KSFT_XFAIL);
282}
283
284static inline int ksft_exit_xpass(void)
285{
286 ksft_print_cnts();
287 exit(KSFT_XPASS);
288}
289
290static inline int ksft_exit_skip(const char *msg, ...)
291{
292 int saved_errno = errno;
293 va_list args;
294
295 va_start(args, msg);
296
297 /*
298 * FIXME: several tests misuse ksft_exit_skip so produce
299 * something sensible if some tests have already been run
300 * or a plan has been printed. Those tests should use
301 * ksft_test_result_skip or ksft_exit_fail_msg instead.
302 */
303 if (ksft_plan || ksft_test_num()) {
304 ksft_cnt.ksft_xskip++;
305 printf("ok %d # SKIP ", 1 + ksft_test_num());
306 } else {
307 printf("1..0 # SKIP ");
308 }
309 if (msg) {
310 errno = saved_errno;
311 vprintf(msg, args);
312 va_end(args);
313 }
314 if (ksft_test_num())
315 ksft_print_cnts();
316 exit(KSFT_SKIP);
317}
318
319#endif /* __KSELFTEST_H */
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * kselftest.h: kselftest framework return codes to include from
4 * selftests.
5 *
6 * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
7 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
8 *
9 */
10#ifndef __KSELFTEST_H
11#define __KSELFTEST_H
12
13#include <errno.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <stdarg.h>
17#include <stdio.h>
18
19/* define kselftest exit codes */
20#define KSFT_PASS 0
21#define KSFT_FAIL 1
22#define KSFT_XFAIL 2
23#define KSFT_XPASS 3
24#define KSFT_SKIP 4
25
26/* counters */
27struct ksft_count {
28 unsigned int ksft_pass;
29 unsigned int ksft_fail;
30 unsigned int ksft_xfail;
31 unsigned int ksft_xpass;
32 unsigned int ksft_xskip;
33 unsigned int ksft_error;
34};
35
36static struct ksft_count ksft_cnt;
37static unsigned int ksft_plan;
38
39static inline int ksft_test_num(void)
40{
41 return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
42 ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
43 ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
44}
45
46static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
47static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
48static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
49static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
50static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
51static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
52
53static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
54static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
55static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
56static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
57static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
58static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
59
60static inline void ksft_print_header(void)
61{
62 if (!(getenv("KSFT_TAP_LEVEL")))
63 printf("TAP version 13\n");
64}
65
66static inline void ksft_set_plan(unsigned int plan)
67{
68 ksft_plan = plan;
69 printf("1..%d\n", ksft_plan);
70}
71
72static inline void ksft_print_cnts(void)
73{
74 if (ksft_plan != ksft_test_num())
75 printf("# Planned tests != run tests (%u != %u)\n",
76 ksft_plan, ksft_test_num());
77 printf("# Pass %d Fail %d Xfail %d Xpass %d Skip %d Error %d\n",
78 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
79 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
80 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
81}
82
83static inline void ksft_print_msg(const char *msg, ...)
84{
85 int saved_errno = errno;
86 va_list args;
87
88 va_start(args, msg);
89 printf("# ");
90 errno = saved_errno;
91 vprintf(msg, args);
92 va_end(args);
93}
94
95static inline void ksft_test_result_pass(const char *msg, ...)
96{
97 int saved_errno = errno;
98 va_list args;
99
100 ksft_cnt.ksft_pass++;
101
102 va_start(args, msg);
103 printf("ok %d ", ksft_test_num());
104 errno = saved_errno;
105 vprintf(msg, args);
106 va_end(args);
107}
108
109static inline void ksft_test_result_fail(const char *msg, ...)
110{
111 int saved_errno = errno;
112 va_list args;
113
114 ksft_cnt.ksft_fail++;
115
116 va_start(args, msg);
117 printf("not ok %d ", ksft_test_num());
118 errno = saved_errno;
119 vprintf(msg, args);
120 va_end(args);
121}
122
123static inline void ksft_test_result_skip(const char *msg, ...)
124{
125 int saved_errno = errno;
126 va_list args;
127
128 ksft_cnt.ksft_xskip++;
129
130 va_start(args, msg);
131 printf("not ok %d # SKIP ", ksft_test_num());
132 errno = saved_errno;
133 vprintf(msg, args);
134 va_end(args);
135}
136
137static inline void ksft_test_result_error(const char *msg, ...)
138{
139 int saved_errno = errno;
140 va_list args;
141
142 ksft_cnt.ksft_error++;
143
144 va_start(args, msg);
145 printf("not ok %d # error ", ksft_test_num());
146 errno = saved_errno;
147 vprintf(msg, args);
148 va_end(args);
149}
150
151static inline int ksft_exit_pass(void)
152{
153 ksft_print_cnts();
154 exit(KSFT_PASS);
155}
156
157static inline int ksft_exit_fail(void)
158{
159 printf("Bail out!\n");
160 ksft_print_cnts();
161 exit(KSFT_FAIL);
162}
163
164static inline int ksft_exit_fail_msg(const char *msg, ...)
165{
166 int saved_errno = errno;
167 va_list args;
168
169 va_start(args, msg);
170 printf("Bail out! ");
171 errno = saved_errno;
172 vprintf(msg, args);
173 va_end(args);
174
175 ksft_print_cnts();
176 exit(KSFT_FAIL);
177}
178
179static inline int ksft_exit_xfail(void)
180{
181 ksft_print_cnts();
182 exit(KSFT_XFAIL);
183}
184
185static inline int ksft_exit_xpass(void)
186{
187 ksft_print_cnts();
188 exit(KSFT_XPASS);
189}
190
191static inline int ksft_exit_skip(const char *msg, ...)
192{
193 if (msg) {
194 int saved_errno = errno;
195 va_list args;
196
197 va_start(args, msg);
198 printf("not ok %d # SKIP ", 1 + ksft_test_num());
199 errno = saved_errno;
200 vprintf(msg, args);
201 va_end(args);
202 } else {
203 ksft_print_cnts();
204 }
205 exit(KSFT_SKIP);
206}
207
208#endif /* __KSELFTEST_H */