cemu
载入中...
搜索中...
未找到
unittest.h
浏览该文件的文档.
1
52#ifndef UNIT_TEST_H
53#define UNIT_TEST_H
54
55#ifdef __cplusplus
56extern "C"
57{
58#endif
59
60#include <stdio.h>
61#include "color.h"
62
63/* Count of args */
64#define VA_NARG(args...) VA_NARG_(0, ##args, VA_RSEQ_N())
65#define VA_NARG_(args...) VA_ARG_N(args)
66#define VA_ARG_N(_0, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, N, ...) N
67#define VA_RSEQ_N() 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
68
69/* The first arg */
70#define VA_FIRST(args...) VA_FIRST_(args)
71#define VA_FIRST_(F, ...) F
72
73/* The first arg in string */
74#define VA_FIRST_STR(args...) VA_FIRST_STR_(args)
75#define VA_FIRST_STR_(F, ...) #F
76
77/* The rest args */
78#define VA_REST_(F, args...) args
79#define VA_REST(args...) VA_REST_(args)
80
81#ifdef __linux__
82#define UT_FAIL "✘"
83#define UT_PASS "✔"
84#else /* for others */
85#define UT_FAIL "FAIL"
86#define UT_PASS "PASS"
87#endif /* __linux__ */
88
90{
91 int n_test; /* number of tests */
92 int n_pass; /* number of tests passed */
93 int n_fail; /* number of tests failed */
94 int u_t;
95 int u_p;
96 int u_f;
97 int flag;
98 int quiet;
99};
100
101static struct unittest_t ut = {
102 .n_fail = 0,
103 .n_pass = 0,
104 .n_test = 0,
105 .u_f = 0,
106 .u_p = 0,
107 .u_t = 0,
108 .flag = 0,
109 .quiet = 0
110};
111
112#define ut_set_quiet(num) \
113 ut.quiet = num;
114
115#define ut_assert(test...) \
116 do \
117 { \
118 ut.u_t++; \
119 if (!(VA_FIRST(test))) \
120 { \
121 ut.u_f++; \
122 ut.flag = 1; \
123 if (ut.quiet <= 3) \
124 { \
125 printf("\n" \
126 "| - " _bred("FAIL %d") " " _black("%s:%d") ": '" _yellow("%s") "' ", \
127 ut.u_t, \
128 __FILE__, __LINE__, VA_FIRST_STR(test)); \
129 if (VA_NARG(test) == 1 || ut.quiet > 1) \
130 printf(_bred(UT_FAIL)); \
131 else \
132 printf(_bred(UT_FAIL) "\n" \
133 "| : " VA_REST(test)); \
134 } \
135 break; \
136 } \
137 else \
138 { \
139 ut.u_p++; \
140 if (ut.quiet <= 0) \
141 { \
142 printf("\n" \
143 "| - " _bgreen("PASS %d") " " _black("%s:%d") ": '" _yellow("%s") "' ", \
144 ut.u_t, \
145 __FILE__, __LINE__, VA_FIRST_STR(test)); \
146 if (VA_NARG(test) == 1 || ut.quiet > 1) \
147 printf(_bgreen(UT_PASS)); \
148 else \
149 printf(_bgreen(UT_PASS) "\n" \
150 "| : " VA_REST(test)); \
151 } \
152 break; \
153 } \
154 } while (0)
155
156#define ut_dec_test(testname) \
157 int testname##_test()
158
159#define ut_def_test(testname, ...) \
160 int testname##_test() \
161 { \
162 ut.flag = 0; \
163 ut.u_t = 0, ut.u_p = 0, ut.u_f = 0; \
164 __VA_ARGS__ \
165 if (ut.quiet < 3) \
166 { \
167 printf("\n" \
168 "|-" _bgreen(" %d " UT_PASS) " - ", \
169 ut.u_p); \
170 printf(_bred("%d " UT_FAIL), ut.u_f); \
171 printf(" - " _bold("%d all") " \n|", ut.u_t); \
172 } \
173 return ut.flag; \
174 }
175
176#define ut_run_test(testname) \
177 do \
178 { \
179 if (ut.n_test == 0 && ut.quiet < 5) \
180 { \
181 printf("\n" \
182 "|============== Unit tests ==============="); \
183 } \
184 ++ut.n_test; \
185 if (ut.quiet < 5) \
186 { \
187 printf("\n" \
188 "|> " _bold("%s") _black(" %s:%d "), \
189 #testname, __FILE__, __LINE__); \
190 } \
191 if (!testname##_test()) \
192 { \
193 ++ut.n_pass; \
194 if (ut.quiet > 2 && ut.quiet < 5) \
195 { \
196 printf(_bgreen(UT_PASS)); \
197 } \
198 } \
199 else \
200 { \
201 ++ut.n_fail; \
202 if (ut.quiet > 3 && ut.quiet < 5) \
203 { \
204 printf(_bred(UT_FAIL)); \
205 } \
206 } \
207 } while (0)
208
209#define ut_print_test() \
210 do \
211 { \
212 if (ut.quiet < 6) \
213 { \
214 printf("\n" \
215 "|============== Test Result =============="); \
216 printf("\n" \
217 "| " _bgreen("%d " UT_PASS) " and ", \
218 ut.n_pass); \
219 printf(_bred("%d " UT_FAIL), ut.n_fail); \
220 printf(" in " _bold("%d TEST(S)"), ut.n_test); \
221 if (ut.n_pass == ut.n_test) \
222 printf("\n" \
223 "|============" _green(" ALL TESTS PASSED ") "==========="); \
224 else \
225 printf("\n" \
226 "|===========" _red(" %d TEST(S) FAILED ") "============" \
227 "\n", \
228 ut.n_fail); \
229 } \
230 } while (0)
231
232#ifdef __cplusplus
233}
234#endif
235
236#endif /* UNIT_TEST_H */
ANSI颜色宏头文件
Definition unittest.h:90