1 /* SPDX-License-Identifier: GPL-2.0 */
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);
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, ...);
52 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
56 * gcc cpuid.h provides __cpuid_count() since v4.4.
57 * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
59 * Provide local define for tests needing __cpuid_count() because
60 * selftests need to work in older environments that do not yet
61 * have __cpuid_count().
64 #define __cpuid_count(level, count, a, b, c, d) \
65 __asm__ __volatile__ ("cpuid\n\t" \
66 : "=a" (a), "=b" (b), "=c" (c), "=d" (d) \
67 : "0" (level), "2" (count))
70 /* define kselftest exit codes */
79 unsigned int ksft_pass;
80 unsigned int ksft_fail;
81 unsigned int ksft_xfail;
82 unsigned int ksft_xpass;
83 unsigned int ksft_xskip;
84 unsigned int ksft_error;
87 static struct ksft_count ksft_cnt;
88 static unsigned int ksft_plan;
90 static inline unsigned int ksft_test_num(void)
92 return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
93 ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
94 ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
97 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
98 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
99 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
100 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
101 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
102 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
104 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
105 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
106 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
107 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
108 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
109 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
111 static inline void ksft_print_header(void)
113 if (!(getenv("KSFT_TAP_LEVEL")))
114 printf("TAP version 13\n");
117 static inline void ksft_set_plan(unsigned int plan)
120 printf("1..%d\n", ksft_plan);
123 static inline void ksft_print_cnts(void)
125 if (ksft_plan != ksft_test_num())
126 printf("# Planned tests != run tests (%u != %u)\n",
127 ksft_plan, ksft_test_num());
128 printf("# Totals: pass:%d fail:%d xfail:%d xpass:%d skip:%d error:%d\n",
129 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
130 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
131 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
134 static inline void ksft_print_msg(const char *msg, ...)
136 int saved_errno = errno;
146 static inline void ksft_test_result_pass(const char *msg, ...)
148 int saved_errno = errno;
151 ksft_cnt.ksft_pass++;
154 printf("ok %d ", ksft_test_num());
160 static inline void ksft_test_result_fail(const char *msg, ...)
162 int saved_errno = errno;
165 ksft_cnt.ksft_fail++;
168 printf("not ok %d ", ksft_test_num());
175 * ksft_test_result() - Report test success based on truth of condition
177 * @condition: if true, report test success, otherwise failure.
179 #define ksft_test_result(condition, fmt, ...) do { \
181 ksft_test_result_pass(fmt, ##__VA_ARGS__);\
183 ksft_test_result_fail(fmt, ##__VA_ARGS__);\
186 static inline void ksft_test_result_xfail(const char *msg, ...)
188 int saved_errno = errno;
191 ksft_cnt.ksft_xfail++;
194 printf("ok %d # XFAIL ", ksft_test_num());
200 static inline void ksft_test_result_skip(const char *msg, ...)
202 int saved_errno = errno;
205 ksft_cnt.ksft_xskip++;
208 printf("ok %d # SKIP ", ksft_test_num());
214 /* TODO: how does "error" differ from "fail" or "skip"? */
215 static inline void ksft_test_result_error(const char *msg, ...)
217 int saved_errno = errno;
220 ksft_cnt.ksft_error++;
223 printf("not ok %d # error ", ksft_test_num());
229 static inline int ksft_exit_pass(void)
235 static inline int ksft_exit_fail(void)
242 * ksft_exit() - Exit selftest based on truth of condition
244 * @condition: if true, exit self test with success, otherwise fail.
246 #define ksft_exit(condition) do { \
253 static inline int ksft_exit_fail_msg(const char *msg, ...)
255 int saved_errno = errno;
259 printf("Bail out! ");
268 static inline int ksft_exit_xfail(void)
274 static inline int ksft_exit_xpass(void)
280 static inline int ksft_exit_skip(const char *msg, ...)
282 int saved_errno = errno;
288 * FIXME: several tests misuse ksft_exit_skip so produce
289 * something sensible if some tests have already been run
290 * or a plan has been printed. Those tests should use
291 * ksft_test_result_skip or ksft_exit_fail_msg instead.
293 if (ksft_plan || ksft_test_num()) {
294 ksft_cnt.ksft_xskip++;
295 printf("ok %d # SKIP ", 1 + ksft_test_num());
297 printf("1..0 # SKIP ");
309 #endif /* __KSELFTEST_H */