xref: /linux/tools/testing/selftests/kselftest.h (revision 5b1b9c5851348c941ec0705c7c0baa5bef637ceb)
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 };
32 
33 static struct ksft_count ksft_cnt;
34 
35 static inline int ksft_test_num(void)
36 {
37 	return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
38 		ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
39 		ksft_cnt.ksft_xskip;
40 }
41 
42 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
43 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
44 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
45 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
46 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
47 
48 static inline int ksft_get_pass_cnt(void) { return ksft_cnt.ksft_pass; }
49 static inline int ksft_get_fail_cnt(void) { return ksft_cnt.ksft_fail; }
50 static inline int ksft_get_xfail_cnt(void) { return ksft_cnt.ksft_xfail; }
51 static inline int ksft_get_xpass_cnt(void) { return ksft_cnt.ksft_xpass; }
52 static inline int ksft_get_xskip_cnt(void) { return ksft_cnt.ksft_xskip; }
53 
54 static inline void ksft_print_header(void)
55 {
56 	printf("TAP version 13\n");
57 }
58 
59 static inline void ksft_print_cnts(void)
60 {
61 	printf("Pass %d Fail %d Xfail %d Xpass %d Skip %d\n",
62 		ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
63 		ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
64 		ksft_cnt.ksft_xskip);
65 	printf("1..%d\n", ksft_test_num());
66 }
67 
68 static inline void ksft_print_msg(const char *msg, ...)
69 {
70 	va_list args;
71 
72 	va_start(args, msg);
73 	printf("# ");
74 	vprintf(msg, args);
75 	va_end(args);
76 }
77 
78 static inline void ksft_test_result_pass(const char *msg, ...)
79 {
80 	va_list args;
81 
82 	ksft_cnt.ksft_pass++;
83 
84 	va_start(args, msg);
85 	printf("ok %d ", ksft_test_num());
86 	vprintf(msg, args);
87 	va_end(args);
88 }
89 
90 static inline void ksft_test_result_fail(const char *msg, ...)
91 {
92 	va_list args;
93 
94 	ksft_cnt.ksft_fail++;
95 
96 	va_start(args, msg);
97 	printf("not ok %d ", ksft_test_num());
98 	vprintf(msg, args);
99 	va_end(args);
100 }
101 
102 static inline void ksft_test_result_skip(const char *msg, ...)
103 {
104 	va_list args;
105 
106 	ksft_cnt.ksft_xskip++;
107 
108 	va_start(args, msg);
109 	printf("ok %d # skip ", ksft_test_num());
110 	vprintf(msg, args);
111 	va_end(args);
112 }
113 
114 static inline int ksft_exit_pass(void)
115 {
116 	ksft_print_cnts();
117 	exit(KSFT_PASS);
118 }
119 
120 static inline int ksft_exit_fail(void)
121 {
122 	printf("Bail out!\n");
123 	ksft_print_cnts();
124 	exit(KSFT_FAIL);
125 }
126 
127 static inline int ksft_exit_fail_msg(const char *msg, ...)
128 {
129 	va_list args;
130 
131 	va_start(args, msg);
132 	printf("Bail out! ");
133 	vprintf(msg, args);
134 	va_end(args);
135 
136 	ksft_print_cnts();
137 	exit(KSFT_FAIL);
138 }
139 
140 static inline int ksft_exit_xfail(void)
141 {
142 	ksft_print_cnts();
143 	exit(KSFT_XFAIL);
144 }
145 
146 static inline int ksft_exit_xpass(void)
147 {
148 	ksft_print_cnts();
149 	exit(KSFT_XPASS);
150 }
151 
152 static inline int ksft_exit_skip(const char *msg, ...)
153 {
154 	if (msg) {
155 		va_list args;
156 
157 		va_start(args, msg);
158 		printf("1..%d # Skipped: ", ksft_test_num());
159 		vprintf(msg, args);
160 		va_end(args);
161 	} else {
162 		ksft_print_cnts();
163 	}
164 	exit(KSFT_SKIP);
165 }
166 
167 #endif /* __KSELFTEST_H */
168