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