xref: /linux/tools/testing/selftests/kselftest.h (revision 44f57d78)
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 <stdlib.h>
14 #include <unistd.h>
15 #include <stdarg.h>
16 #include <stdio.h>
17 
18 /* define kselftest exit codes */
19 #define KSFT_PASS  0
20 #define KSFT_FAIL  1
21 #define KSFT_XFAIL 2
22 #define KSFT_XPASS 3
23 #define KSFT_SKIP  4
24 
25 /* counters */
26 struct ksft_count {
27 	unsigned int ksft_pass;
28 	unsigned int ksft_fail;
29 	unsigned int ksft_xfail;
30 	unsigned int ksft_xpass;
31 	unsigned int ksft_xskip;
32 	unsigned int ksft_error;
33 };
34 
35 static struct ksft_count ksft_cnt;
36 static unsigned int ksft_plan;
37 
38 static inline int ksft_test_num(void)
39 {
40 	return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
41 		ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
42 		ksft_cnt.ksft_xskip + ksft_cnt.ksft_error;
43 }
44 
45 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
46 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
47 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
48 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
49 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
50 static inline void ksft_inc_error_cnt(void) { ksft_cnt.ksft_error++; }
51 
52 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
53 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
54 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
55 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
56 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
57 static inline int ksft_get_error_cnt(void) { return ksft_cnt.ksft_error; }
58 
59 static inline void ksft_print_header(void)
60 {
61 	if (!(getenv("KSFT_TAP_LEVEL")))
62 		printf("TAP version 13\n");
63 }
64 
65 static inline void ksft_set_plan(unsigned int plan)
66 {
67 	ksft_plan = plan;
68 	printf("1..%d\n", ksft_plan);
69 }
70 
71 static inline void ksft_print_cnts(void)
72 {
73 	if (ksft_plan != ksft_test_num())
74 		printf("# Planned tests != run tests (%u != %u)\n",
75 			ksft_plan, ksft_test_num());
76 	printf("# Pass %d Fail %d Xfail %d Xpass %d Skip %d Error %d\n",
77 		ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
78 		ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
79 		ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
80 }
81 
82 static inline void ksft_print_msg(const char *msg, ...)
83 {
84 	va_list args;
85 
86 	va_start(args, msg);
87 	printf("# ");
88 	vprintf(msg, args);
89 	va_end(args);
90 }
91 
92 static inline void ksft_test_result_pass(const char *msg, ...)
93 {
94 	va_list args;
95 
96 	ksft_cnt.ksft_pass++;
97 
98 	va_start(args, msg);
99 	printf("ok %d ", ksft_test_num());
100 	vprintf(msg, args);
101 	va_end(args);
102 }
103 
104 static inline void ksft_test_result_fail(const char *msg, ...)
105 {
106 	va_list args;
107 
108 	ksft_cnt.ksft_fail++;
109 
110 	va_start(args, msg);
111 	printf("not ok %d ", ksft_test_num());
112 	vprintf(msg, args);
113 	va_end(args);
114 }
115 
116 static inline void ksft_test_result_skip(const char *msg, ...)
117 {
118 	va_list args;
119 
120 	ksft_cnt.ksft_xskip++;
121 
122 	va_start(args, msg);
123 	printf("not ok %d # SKIP ", ksft_test_num());
124 	vprintf(msg, args);
125 	va_end(args);
126 }
127 
128 static inline void ksft_test_result_error(const char *msg, ...)
129 {
130 	va_list args;
131 
132 	ksft_cnt.ksft_error++;
133 
134 	va_start(args, msg);
135 	printf("not ok %d # error ", ksft_test_num());
136 	vprintf(msg, args);
137 	va_end(args);
138 }
139 
140 static inline int ksft_exit_pass(void)
141 {
142 	ksft_print_cnts();
143 	exit(KSFT_PASS);
144 }
145 
146 static inline int ksft_exit_fail(void)
147 {
148 	printf("Bail out!\n");
149 	ksft_print_cnts();
150 	exit(KSFT_FAIL);
151 }
152 
153 static inline int ksft_exit_fail_msg(const char *msg, ...)
154 {
155 	va_list args;
156 
157 	va_start(args, msg);
158 	printf("Bail out! ");
159 	vprintf(msg, args);
160 	va_end(args);
161 
162 	ksft_print_cnts();
163 	exit(KSFT_FAIL);
164 }
165 
166 static inline int ksft_exit_xfail(void)
167 {
168 	ksft_print_cnts();
169 	exit(KSFT_XFAIL);
170 }
171 
172 static inline int ksft_exit_xpass(void)
173 {
174 	ksft_print_cnts();
175 	exit(KSFT_XPASS);
176 }
177 
178 static inline int ksft_exit_skip(const char *msg, ...)
179 {
180 	if (msg) {
181 		va_list args;
182 
183 		va_start(args, msg);
184 		printf("not ok %d # SKIP ", 1 + ksft_test_num());
185 		vprintf(msg, args);
186 		va_end(args);
187 	} else {
188 		ksft_print_cnts();
189 	}
190 	exit(KSFT_SKIP);
191 }
192 
193 #endif /* __KSELFTEST_H */
194