treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / testing / selftests / kselftest.h
blob0ac49d91a26023c3eb10804f4cf77fc004b4d080
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * kselftest.h: kselftest framework return codes to include from
4 * selftests.
6 * Copyright (c) 2014 Shuah Khan <shuahkh@osg.samsung.com>
7 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
9 */
10 #ifndef __KSELFTEST_H
11 #define __KSELFTEST_H
13 #include <errno.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdarg.h>
17 #include <stdio.h>
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
26 /* counters */
27 struct 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;
36 static struct ksft_count ksft_cnt;
37 static unsigned int ksft_plan;
39 static inline int ksft_test_num(void)
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;
46 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
47 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
48 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
49 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
50 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
51 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
53 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
54 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
55 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
56 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
57 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
58 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
60 static inline void ksft_print_header(void)
62 if (!(getenv("KSFT_TAP_LEVEL")))
63 printf("TAP version 13\n");
66 static inline void ksft_set_plan(unsigned int plan)
68 ksft_plan = plan;
69 printf("1..%d\n", ksft_plan);
72 static inline void ksft_print_cnts(void)
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);
83 static inline void ksft_print_msg(const char *msg, ...)
85 int saved_errno = errno;
86 va_list args;
88 va_start(args, msg);
89 printf("# ");
90 errno = saved_errno;
91 vprintf(msg, args);
92 va_end(args);
95 static inline void ksft_test_result_pass(const char *msg, ...)
97 int saved_errno = errno;
98 va_list args;
100 ksft_cnt.ksft_pass++;
102 va_start(args, msg);
103 printf("ok %d ", ksft_test_num());
104 errno = saved_errno;
105 vprintf(msg, args);
106 va_end(args);
109 static inline void ksft_test_result_fail(const char *msg, ...)
111 int saved_errno = errno;
112 va_list args;
114 ksft_cnt.ksft_fail++;
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);
123 static inline void ksft_test_result_skip(const char *msg, ...)
125 int saved_errno = errno;
126 va_list args;
128 ksft_cnt.ksft_xskip++;
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);
137 static inline void ksft_test_result_error(const char *msg, ...)
139 int saved_errno = errno;
140 va_list args;
142 ksft_cnt.ksft_error++;
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);
151 static inline int ksft_exit_pass(void)
153 ksft_print_cnts();
154 exit(KSFT_PASS);
157 static inline int ksft_exit_fail(void)
159 printf("Bail out!\n");
160 ksft_print_cnts();
161 exit(KSFT_FAIL);
164 static inline int ksft_exit_fail_msg(const char *msg, ...)
166 int saved_errno = errno;
167 va_list args;
169 va_start(args, msg);
170 printf("Bail out! ");
171 errno = saved_errno;
172 vprintf(msg, args);
173 va_end(args);
175 ksft_print_cnts();
176 exit(KSFT_FAIL);
179 static inline int ksft_exit_xfail(void)
181 ksft_print_cnts();
182 exit(KSFT_XFAIL);
185 static inline int ksft_exit_xpass(void)
187 ksft_print_cnts();
188 exit(KSFT_XPASS);
191 static inline int ksft_exit_skip(const char *msg, ...)
193 if (msg) {
194 int saved_errno = errno;
195 va_list args;
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();
205 exit(KSFT_SKIP);
208 #endif /* __KSELFTEST_H */