6
6
* Copyright (c) 2014 Shuah Khan <[email protected] >
7
7
* Copyright (c) 2014 Samsung Electronics Co., Ltd.
8
8
*
9
+ * Using this API consists of first counting how many tests your code
10
+ * has to run, and then starting up the reporting:
11
+ *
12
+ * ksft_print_header();
13
+ * ksft_set_plan(total_number_of_tests);
14
+ *
15
+ * For each test, report any progress, debugging, etc with:
16
+ *
17
+ * ksft_print_msg(fmt, ...);
18
+ *
19
+ * and finally report the pass/fail/skip/xfail state of the test with one of:
20
+ *
21
+ * ksft_test_result(condition, fmt, ...);
22
+ * ksft_test_result_pass(fmt, ...);
23
+ * ksft_test_result_fail(fmt, ...);
24
+ * ksft_test_result_skip(fmt, ...);
25
+ * ksft_test_result_xfail(fmt, ...);
26
+ * ksft_test_result_error(fmt, ...);
27
+ *
28
+ * When all tests are finished, clean up and exit the program with one of:
29
+ *
30
+ * ksft_exit(condition);
31
+ * ksft_exit_pass();
32
+ * ksft_exit_fail();
33
+ *
34
+ * If the program wants to report details on why the entire program has
35
+ * failed, it can instead exit with a message (this is usually done when
36
+ * the program is aborting before finishing all tests):
37
+ *
38
+ * ksft_exit_fail_msg(fmt, ...);
39
+ *
9
40
*/
10
41
#ifndef __KSELFTEST_H
11
42
#define __KSELFTEST_H
@@ -74,7 +105,7 @@ static inline void ksft_print_cnts(void)
74
105
if (ksft_plan != ksft_test_num ())
75
106
printf ("# Planned tests != run tests (%u != %u)\n" ,
76
107
ksft_plan , ksft_test_num ());
77
- printf ("# Pass %d Fail %d Xfail %d Xpass %d Skip %d Error %d\n" ,
108
+ printf ("# Totals: pass: %d fail: %d xfail: %d xpass: %d skip: %d error: %d\n" ,
78
109
ksft_cnt .ksft_pass , ksft_cnt .ksft_fail ,
79
110
ksft_cnt .ksft_xfail , ksft_cnt .ksft_xpass ,
80
111
ksft_cnt .ksft_xskip , ksft_cnt .ksft_error );
@@ -120,6 +151,32 @@ static inline void ksft_test_result_fail(const char *msg, ...)
120
151
va_end (args );
121
152
}
122
153
154
+ /**
155
+ * ksft_test_result() - Report test success based on truth of condition
156
+ *
157
+ * @condition: if true, report test success, otherwise failure.
158
+ */
159
+ #define ksft_test_result (condition , fmt , ...) do { \
160
+ if (!!(condition)) \
161
+ ksft_test_result_pass(fmt, ##__VA_ARGS__);\
162
+ else \
163
+ ksft_test_result_fail(fmt, ##__VA_ARGS__);\
164
+ } while (0)
165
+
166
+ static inline void ksft_test_result_xfail (const char * msg , ...)
167
+ {
168
+ int saved_errno = errno ;
169
+ va_list args ;
170
+
171
+ ksft_cnt .ksft_xfail ++ ;
172
+
173
+ va_start (args , msg );
174
+ printf ("ok %d # XFAIL " , ksft_test_num ());
175
+ errno = saved_errno ;
176
+ vprintf (msg , args );
177
+ va_end (args );
178
+ }
179
+
123
180
static inline void ksft_test_result_skip (const char * msg , ...)
124
181
{
125
182
int saved_errno = errno ;
@@ -134,6 +191,7 @@ static inline void ksft_test_result_skip(const char *msg, ...)
134
191
va_end (args );
135
192
}
136
193
194
+ /* TODO: how does "error" differ from "fail" or "skip"? */
137
195
static inline void ksft_test_result_error (const char * msg , ...)
138
196
{
139
197
int saved_errno = errno ;
@@ -156,11 +214,22 @@ static inline int ksft_exit_pass(void)
156
214
157
215
static inline int ksft_exit_fail (void )
158
216
{
159
- printf ("Bail out!\n" );
160
217
ksft_print_cnts ();
161
218
exit (KSFT_FAIL );
162
219
}
163
220
221
+ /**
222
+ * ksft_exit() - Exit selftest based on truth of condition
223
+ *
224
+ * @condition: if true, exit self test with success, otherwise fail.
225
+ */
226
+ #define ksft_exit (condition ) do { \
227
+ if (!!(condition)) \
228
+ ksft_exit_pass(); \
229
+ else \
230
+ ksft_exit_fail(); \
231
+ } while (0)
232
+
164
233
static inline int ksft_exit_fail_msg (const char * msg , ...)
165
234
{
166
235
int saved_errno = errno ;
0 commit comments