Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[sfrench/cifs-2.6.git] / tools / testing / selftests / kselftest.h
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
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         if (!(getenv("KSFT_TAP_LEVEL")))
61                 printf("TAP version 13\n");
62 }
63
64 static inline void ksft_print_cnts(void)
65 {
66         printf("Pass %d Fail %d Xfail %d Xpass %d Skip %d Error %d\n",
67                 ksft_cnt.ksft_pass, ksft_cnt.ksft_fail,
68                 ksft_cnt.ksft_xfail, ksft_cnt.ksft_xpass,
69                 ksft_cnt.ksft_xskip, ksft_cnt.ksft_error);
70         printf("1..%d\n", ksft_test_num());
71 }
72
73 static inline void ksft_print_msg(const char *msg, ...)
74 {
75         va_list args;
76
77         va_start(args, msg);
78         printf("# ");
79         vprintf(msg, args);
80         va_end(args);
81 }
82
83 static inline void ksft_test_result_pass(const char *msg, ...)
84 {
85         va_list args;
86
87         ksft_cnt.ksft_pass++;
88
89         va_start(args, msg);
90         printf("ok %d ", ksft_test_num());
91         vprintf(msg, args);
92         va_end(args);
93 }
94
95 static inline void ksft_test_result_fail(const char *msg, ...)
96 {
97         va_list args;
98
99         ksft_cnt.ksft_fail++;
100
101         va_start(args, msg);
102         printf("not ok %d ", ksft_test_num());
103         vprintf(msg, args);
104         va_end(args);
105 }
106
107 static inline void ksft_test_result_skip(const char *msg, ...)
108 {
109         va_list args;
110
111         ksft_cnt.ksft_xskip++;
112
113         va_start(args, msg);
114         printf("ok %d # skip ", ksft_test_num());
115         vprintf(msg, args);
116         va_end(args);
117 }
118
119 static inline void ksft_test_result_error(const char *msg, ...)
120 {
121         va_list args;
122
123         ksft_cnt.ksft_error++;
124
125         va_start(args, msg);
126         printf("not ok %d # error ", ksft_test_num());
127         vprintf(msg, args);
128         va_end(args);
129 }
130
131 static inline int ksft_exit_pass(void)
132 {
133         ksft_print_cnts();
134         exit(KSFT_PASS);
135 }
136
137 static inline int ksft_exit_fail(void)
138 {
139         printf("Bail out!\n");
140         ksft_print_cnts();
141         exit(KSFT_FAIL);
142 }
143
144 static inline int ksft_exit_fail_msg(const char *msg, ...)
145 {
146         va_list args;
147
148         va_start(args, msg);
149         printf("Bail out! ");
150         vprintf(msg, args);
151         va_end(args);
152
153         ksft_print_cnts();
154         exit(KSFT_FAIL);
155 }
156
157 static inline int ksft_exit_xfail(void)
158 {
159         ksft_print_cnts();
160         exit(KSFT_XFAIL);
161 }
162
163 static inline int ksft_exit_xpass(void)
164 {
165         ksft_print_cnts();
166         exit(KSFT_XPASS);
167 }
168
169 static inline int ksft_exit_skip(const char *msg, ...)
170 {
171         if (msg) {
172                 va_list args;
173
174                 va_start(args, msg);
175                 printf("1..%d # Skipped: ", ksft_test_num());
176                 vprintf(msg, args);
177                 va_end(args);
178         } else {
179                 ksft_print_cnts();
180         }
181         exit(KSFT_SKIP);
182 }
183
184 #endif /* __KSELFTEST_H */