xref: /linux/tools/testing/selftests/kselftest.h (revision 48e42f91c10482992b474cc0874c0e33d76cb509)
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 
16 /* define kselftest exit codes */
17 #define KSFT_PASS  0
18 #define KSFT_FAIL  1
19 #define KSFT_XFAIL 2
20 #define KSFT_XPASS 3
21 #define KSFT_SKIP  4
22 
23 /* counters */
24 struct ksft_count {
25 	unsigned int ksft_pass;
26 	unsigned int ksft_fail;
27 	unsigned int ksft_xfail;
28 	unsigned int ksft_xpass;
29 	unsigned int ksft_xskip;
30 };
31 
32 static struct ksft_count ksft_cnt;
33 
34 static inline int ksft_test_num(void)
35 {
36 	return ksft_cnt.ksft_pass + ksft_cnt.ksft_fail +
37 		ksft_cnt.ksft_xfail + ksft_cnt.ksft_xpass +
38 		ksft_cnt.ksft_xskip;
39 }
40 
41 static inline void ksft_inc_pass_cnt(void) { ksft_cnt.ksft_pass++; }
42 static inline void ksft_inc_fail_cnt(void) { ksft_cnt.ksft_fail++; }
43 static inline void ksft_inc_xfail_cnt(void) { ksft_cnt.ksft_xfail++; }
44 static inline void ksft_inc_xpass_cnt(void) { ksft_cnt.ksft_xpass++; }
45 static inline void ksft_inc_xskip_cnt(void) { ksft_cnt.ksft_xskip++; }
46 
47 static inline void ksft_print_header(void)
48 {
49 	printf("TAP version 13\n");
50 }
51 
52 static inline void ksft_print_cnts(void)
53 {
54 	printf("1..%d\n", ksft_test_num());
55 }
56 
57 static inline void ksft_test_result_pass(const char *msg)
58 {
59 	ksft_cnt.ksft_pass++;
60 	printf("ok %d %s\n", ksft_test_num(), msg);
61 }
62 
63 static inline void ksft_test_result_fail(const char *msg)
64 {
65 	ksft_cnt.ksft_fail++;
66 	printf("not ok %d %s\n", ksft_test_num(), msg);
67 }
68 
69 static inline void ksft_test_result_skip(const char *msg)
70 {
71 	ksft_cnt.ksft_xskip++;
72 	printf("ok %d # skip %s\n", ksft_test_num(), msg);
73 }
74 
75 static inline int ksft_exit_pass(void)
76 {
77 	ksft_print_cnts();
78 	exit(KSFT_PASS);
79 }
80 
81 static inline int ksft_exit_fail(void)
82 {
83 	printf("Bail out!\n");
84 	ksft_print_cnts();
85 	exit(KSFT_FAIL);
86 }
87 
88 static inline int ksft_exit_fail_msg(const char *msg)
89 {
90 	printf("Bail out! %s\n", msg);
91 	ksft_print_cnts();
92 	exit(KSFT_FAIL);
93 }
94 
95 static inline int ksft_exit_xfail(void)
96 {
97 	ksft_print_cnts();
98 	exit(KSFT_XFAIL);
99 }
100 
101 static inline int ksft_exit_xpass(void)
102 {
103 	ksft_print_cnts();
104 	exit(KSFT_XPASS);
105 }
106 
107 static inline int ksft_exit_skip(void)
108 {
109 	ksft_print_cnts();
110 	exit(KSFT_SKIP);
111 }
112 
113 #endif /* __KSELFTEST_H */
114