WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / kselftest.h
blob8d50483fe204a71d50fe3df82399b1da2635ecc6
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.
7 * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
8 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
10 * Using this API consists of first counting how many tests your code
11 * has to run, and then starting up the reporting:
13 * ksft_print_header();
14 * ksft_set_plan(total_number_of_tests);
16 * For each test, report any progress, debugging, etc with:
18 * ksft_print_msg(fmt, ...);
20 * and finally report the pass/fail/skip/xfail state of the test with one of:
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, ...);
29 * When all tests are finished, clean up and exit the program with one of:
31 * ksft_exit(condition);
32 * ksft_exit_pass();
33 * ksft_exit_fail();
35 * If the program wants to report details on why the entire program has
36 * failed, it can instead exit with a message (this is usually done when
37 * the program is aborting before finishing all tests):
39 * ksft_exit_fail_msg(fmt, ...);
42 #ifndef __KSELFTEST_H
43 #define __KSELFTEST_H
45 #include <errno.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <stdarg.h>
49 #include <stdio.h>
51 /* define kselftest exit codes */
52 #define KSFT_PASS 0
53 #define KSFT_FAIL 1
54 #define KSFT_XFAIL 2
55 #define KSFT_XPASS 3
56 #define KSFT_SKIP 4
58 /* counters */
59 struct ksft_count {
60 unsigned int ksft_pass;
61 unsigned int ksft_fail;
62 unsigned int ksft_xfail;
63 unsigned int ksft_xpass;
64 unsigned int ksft_xskip;
65 unsigned int ksft_error;
68 static struct ksft_count ksft_cnt;
69 static unsigned int ksft_plan;
71 static inline unsigned int ksft_test_num(void)
73 return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
74 ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
75 ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
78 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
79 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
80 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
81 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
82 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
83 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
85 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
86 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
87 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
88 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
89 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
90 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
92 static inline void ksft_print_header(void)
94 if (!(getenv("KSFT_TAP_LEVEL")))
95 printf("TAP version 13\n");
98 static inline void ksft_set_plan(unsigned int plan)
100 ksft_plan = plan;
101 printf("1..%d\n", ksft_plan);
104 static inline void ksft_print_cnts(void)
106 if (ksft_plan != ksft_test_num())
107 printf("# Planned tests != run tests (%u != %u)\n",
108 ksft_plan, ksft_test_num());
109 printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n",
110 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
111 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
112 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
115 static inline void ksft_print_msg(const char *msg, ...)
117 int saved_errno = errno;
118 va_list args;
120 va_start(args, msg);
121 printf("# ");
122 errno = saved_errno;
123 vprintf(msg, args);
124 va_end(args);
127 static inline void ksft_test_result_pass(const char *msg, ...)
129 int saved_errno = errno;
130 va_list args;
132 ksft_cnt.ksft_pass++;
134 va_start(args, msg);
135 printf("ok %d ", ksft_test_num());
136 errno = saved_errno;
137 vprintf(msg, args);
138 va_end(args);
141 static inline void ksft_test_result_fail(const char *msg, ...)
143 int saved_errno = errno;
144 va_list args;
146 ksft_cnt.ksft_fail++;
148 va_start(args, msg);
149 printf("not ok %d ", ksft_test_num());
150 errno = saved_errno;
151 vprintf(msg, args);
152 va_end(args);
156 * ksft_test_result() - Report test success based on truth of condition
158 * @condition: if true, report test success, otherwise failure.
160 #define ksft_test_result(condition, fmt, ...) do { \
161 if (!!(condition)) \
162 ksft_test_result_pass(fmt, ##__VA_ARGS__);\
163 else \
164 ksft_test_result_fail(fmt, ##__VA_ARGS__);\
165 } while (0)
167 static inline void ksft_test_result_xfail(const char *msg, ...)
169 int saved_errno = errno;
170 va_list args;
172 ksft_cnt.ksft_xfail++;
174 va_start(args, msg);
175 printf("ok %d # XFAIL ", ksft_test_num());
176 errno = saved_errno;
177 vprintf(msg, args);
178 va_end(args);
181 static inline void ksft_test_result_skip(const char *msg, ...)
183 int saved_errno = errno;
184 va_list args;
186 ksft_cnt.ksft_xskip++;
188 va_start(args, msg);
189 printf("ok %d # SKIP ", ksft_test_num());
190 errno = saved_errno;
191 vprintf(msg, args);
192 va_end(args);
195 /* TODO: how does "error" differ from "fail" or "skip"? */
196 static inline void ksft_test_result_error(const char *msg, ...)
198 int saved_errno = errno;
199 va_list args;
201 ksft_cnt.ksft_error++;
203 va_start(args, msg);
204 printf("not ok %d # error ", ksft_test_num());
205 errno = saved_errno;
206 vprintf(msg, args);
207 va_end(args);
210 static inline int ksft_exit_pass(void)
212 ksft_print_cnts();
213 exit(KSFT_PASS);
216 static inline int ksft_exit_fail(void)
218 ksft_print_cnts();
219 exit(KSFT_FAIL);
223 * ksft_exit() - Exit selftest based on truth of condition
225 * @condition: if true, exit self test with success, otherwise fail.
227 #define ksft_exit(condition) do { \
228 if (!!(condition)) \
229 ksft_exit_pass(); \
230 else \
231 ksft_exit_fail(); \
232 } while (0)
234 static inline int ksft_exit_fail_msg(const char *msg, ...)
236 int saved_errno = errno;
237 va_list args;
239 va_start(args, msg);
240 printf("Bail out! ");
241 errno = saved_errno;
242 vprintf(msg, args);
243 va_end(args);
245 ksft_print_cnts();
246 exit(KSFT_FAIL);
249 static inline int ksft_exit_xfail(void)
251 ksft_print_cnts();
252 exit(KSFT_XFAIL);
255 static inline int ksft_exit_xpass(void)
257 ksft_print_cnts();
258 exit(KSFT_XPASS);
261 static inline int ksft_exit_skip(const char *msg, ...)
263 int saved_errno = errno;
264 va_list args;
266 va_start(args, msg);
269 * FIXME: several tests misuse ksft_exit_skip so produce
270 * something sensible if some tests have already been run
271 * or a plan has been printed. Those tests should use
272 * ksft_test_result_skip or ksft_exit_fail_msg instead.
274 if (ksft_plan || ksft_test_num()) {
275 ksft_cnt.ksft_xskip++;
276 printf("ok %d # SKIP ", 1 + ksft_test_num());
277 } else {
278 printf("1..0 # SKIP ");
280 if (msg) {
281 errno = saved_errno;
282 vprintf(msg, args);
283 va_end(args);
285 if (ksft_test_num())
286 ksft_print_cnts();
287 exit(KSFT_SKIP);
290 #endif /* __KSELFTEST_H */