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.
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:
32 * ksft_exit(condition);
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):
40 * ksft_exit_fail_msg(fmt, ...);
53 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
57 * gcc cpuid.h provides __cpuid_count() since v4.4.
58 * Clang/LLVM cpuid.h provides __cpuid_count() since v3.4.0.
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().
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))
71 /* define kselftest exit codes */
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;
88 static struct ksft_count ksft_cnt;
89 static unsigned int ksft_plan;
91 static inline unsigned int ksft_test_num(void)
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;
98 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
99 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
100 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
101 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
102 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
103 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
105 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
106 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
107 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
108 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
109 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
110 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
112 static inline void ksft_print_header(void)
114 if (!(getenv("KSFT_TAP_LEVEL")))
115 printf("TAP version 13\n");
118 static inline void ksft_set_plan(unsigned int plan)
121 printf("1..%d\n", ksft_plan);
124 static inline void ksft_print_cnts(void)
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);
135 static inline void ksft_print_msg(const char *msg, ...)
137 int saved_errno = errno;
147 static inline void ksft_test_result_pass(const char *msg, ...)
149 int saved_errno = errno;
152 ksft_cnt.ksft_pass++;
155 printf("ok %d ", ksft_test_num());
161 static inline void ksft_test_result_fail(const char *msg, ...)
163 int saved_errno = errno;
166 ksft_cnt.ksft_fail++;
169 printf("not ok %d ", ksft_test_num());
176 * ksft_test_result() - Report test success based on truth of condition
178 * @condition: if true, report test success, otherwise failure.
180 #define ksft_test_result(condition, fmt, ...) do { \
182 ksft_test_result_pass(fmt, ##__VA_ARGS__);\
184 ksft_test_result_fail(fmt, ##__VA_ARGS__);\
187 static inline void ksft_test_result_xfail(const char *msg, ...)
189 int saved_errno = errno;
192 ksft_cnt.ksft_xfail++;
195 printf("ok %d # XFAIL ", ksft_test_num());
201 static inline void ksft_test_result_skip(const char *msg, ...)
203 int saved_errno = errno;
206 ksft_cnt.ksft_xskip++;
209 printf("ok %d # SKIP ", ksft_test_num());
215 /* TODO: how does "error" differ from "fail" or "skip"? */
216 static inline void ksft_test_result_error(const char *msg, ...)
218 int saved_errno = errno;
221 ksft_cnt.ksft_error++;
224 printf("not ok %d # error ", ksft_test_num());
230 static inline int ksft_exit_pass(void)
236 static inline int ksft_exit_fail(void)
243 * ksft_exit() - Exit selftest based on truth of condition
245 * @condition: if true, exit self test with success, otherwise fail.
247 #define ksft_exit(condition) do { \
255 * ksft_finished() - Exit selftest with success if all tests passed
257 #define ksft_finished() \
258 ksft_exit(ksft_plan == \
259 ksft_cnt.ksft_pass + \
260 ksft_cnt.ksft_xfail + \
263 static inline int ksft_exit_fail_msg(const char *msg, ...)
265 int saved_errno = errno;
269 printf("Bail out! ");
278 static inline int ksft_exit_xfail(void)
284 static inline int ksft_exit_xpass(void)
290 static inline int ksft_exit_skip(const char *msg, ...)
292 int saved_errno = errno;
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.
303 if (ksft_plan || ksft_test_num()) {
304 ksft_cnt.ksft_xskip++;
305 printf("ok %d # SKIP ", 1 + ksft_test_num());
307 printf("1..0 # SKIP ");
319 #endif /* __KSELFTEST_H */