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