| |
1 /* |
| |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| |
3 * |
| |
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright |
| |
10 * notice, this list of conditions and the following disclaimer. |
| |
11 * |
| |
12 * 2. Redistributions in binary form must reproduce the above copyright |
| |
13 * notice, this list of conditions and the following disclaimer in the |
| |
14 * documentation and/or other materials provided with the distribution. |
| |
15 * |
| |
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
26 * POSSIBILITY OF SUCH DAMAGE. |
| |
27 */ |
| |
28 |
| |
29 /** |
| |
30 * @file: test.h |
| |
31 * |
| |
32 * UCX Test Framework. |
| |
33 * |
| |
34 * Usage of this test framework: |
| |
35 * |
| |
36 * **** IN HEADER FILE: **** |
| |
37 * |
| |
38 * <pre> |
| |
39 * CX_TEST(function_name); |
| |
40 * CX_TEST_SUBROUTINE(subroutine_name, paramlist); // optional |
| |
41 * </pre> |
| |
42 * |
| |
43 * **** IN SOURCE FILE: **** |
| |
44 * <pre> |
| |
45 * CX_TEST_SUBROUTINE(subroutine_name, paramlist) { |
| |
46 * // tests with CX_TEST_ASSERT() |
| |
47 * } |
| |
48 * |
| |
49 * CX_TEST(function_name) { |
| |
50 * // memory allocation and other stuff here |
| |
51 * #CX_TEST_DO { |
| |
52 * // tests with CX_TEST_ASSERT() and/or |
| |
53 * // calls with CX_TEST_CALL_SUBROUTINE() here |
| |
54 * } |
| |
55 * // cleanup of memory here |
| |
56 * } |
| |
57 * </pre> |
| |
58 * |
| |
59 * @attention Do not call own functions within a test, that use |
| |
60 * CX_TEST_ASSERT() macros and are not defined by using CX_TEST_SUBROUTINE(). |
| |
61 * |
| |
62 * @author Mike Becker |
| |
63 * @author Olaf Wintermann |
| |
64 * |
| |
65 */ |
| |
66 |
| |
67 #ifndef UCX_TEST_H |
| |
68 #define UCX_TEST_H |
| |
69 |
| |
70 #include "common.h" |
| |
71 |
| |
72 #include <stdio.h> |
| |
73 #include <string.h> |
| |
74 #include <setjmp.h> |
| |
75 |
| |
76 #ifdef __cplusplus |
| |
77 extern "C" { |
| |
78 #endif |
| |
79 |
| |
80 #ifndef __FUNCTION__ |
| |
81 /** |
| |
82 * Alias for the <code>__func__</code> preprocessor macro. |
| |
83 * Some compilers use <code>__func__</code> and others use __FUNCTION__. |
| |
84 * We use __FUNCTION__ so we define it for those compilers which use |
| |
85 * <code>__func__</code>. |
| |
86 */ |
| |
87 #define __FUNCTION__ __func__ |
| |
88 #endif |
| |
89 |
| |
90 // |
| |
91 #if !defined(__clang__) && __GNUC__ > 3 |
| |
92 #pragma GCC diagnostic ignored "-Wclobbered" |
| |
93 #endif |
| |
94 |
| |
95 /** Type for the CxTestSuite. */ |
| |
96 typedef struct CxTestSuite CxTestSuite; |
| |
97 |
| |
98 /** Pointer to a test function. */ |
| |
99 cx_attr_nonnull |
| |
100 typedef void(*CxTest)(CxTestSuite *, void *, cx_write_func); |
| |
101 |
| |
102 /** Type for the internal list of test cases. */ |
| |
103 typedef struct CxTestSet CxTestSet; |
| |
104 |
| |
105 /** Structure for the internal list of test cases. */ |
| |
106 struct CxTestSet { |
| |
107 |
| |
108 /** Test case. */ |
| |
109 CxTest test; |
| |
110 |
| |
111 /** Pointer to the next list element. */ |
| |
112 CxTestSet *next; |
| |
113 }; |
| |
114 |
| |
115 /** |
| |
116 * A test suite containing multiple test cases. |
| |
117 */ |
| |
118 struct CxTestSuite { |
| |
119 |
| |
120 /** The number of successful tests after the suite has been run. */ |
| |
121 unsigned int success; |
| |
122 |
| |
123 /** The number of failed tests after the suite has been run. */ |
| |
124 unsigned int failure; |
| |
125 |
| |
126 /** The optional name of this test suite. */ |
| |
127 const char *name; |
| |
128 |
| |
129 /** |
| |
130 * Internal list of test cases. |
| |
131 * Use cx_test_register() to add tests to this list. |
| |
132 */ |
| |
133 CxTestSet *tests; |
| |
134 }; |
| |
135 |
| |
136 /** |
| |
137 * Creates a new test suite. |
| |
138 * @param name optional name of the suite |
| |
139 * @return a new test suite |
| |
140 */ |
| |
141 cx_attr_nonnull |
| |
142 cx_attr_nodiscard |
| |
143 cx_attr_cstr_arg(1) |
| |
144 cx_attr_malloc |
| |
145 static inline CxTestSuite* cx_test_suite_new(const char *name) { |
| |
146 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite)); |
| |
147 if (suite != NULL) { |
| |
148 suite->name = name; |
| |
149 suite->success = 0; |
| |
150 suite->failure = 0; |
| |
151 suite->tests = NULL; |
| |
152 } |
| |
153 |
| |
154 return suite; |
| |
155 } |
| |
156 |
| |
157 /** |
| |
158 * Deallocates a test suite. |
| |
159 * |
| |
160 * @param suite the test suite to free |
| |
161 */ |
| |
162 static inline void cx_test_suite_free(CxTestSuite* suite) { |
| |
163 if (suite == NULL) return; |
| |
164 CxTestSet *l = suite->tests; |
| |
165 while (l != NULL) { |
| |
166 CxTestSet *e = l; |
| |
167 l = l->next; |
| |
168 free(e); |
| |
169 } |
| |
170 free(suite); |
| |
171 } |
| |
172 |
| |
173 /** |
| |
174 * Registers a test function with the specified test suite. |
| |
175 * |
| |
176 * @param suite the suite, the test function shall be added to |
| |
177 * @param test the test function to register |
| |
178 * @return zero on success or non-zero on failure |
| |
179 */ |
| |
180 cx_attr_nonnull |
| |
181 static inline int cx_test_register(CxTestSuite* suite, CxTest test) { |
| |
182 CxTestSet *t = (CxTestSet*) malloc(sizeof(CxTestSet)); |
| |
183 if (t) { |
| |
184 t->test = test; |
| |
185 t->next = NULL; |
| |
186 if (suite->tests == NULL) { |
| |
187 suite->tests = t; |
| |
188 } else { |
| |
189 CxTestSet *last = suite->tests; |
| |
190 while (last->next) { |
| |
191 last = last->next; |
| |
192 } |
| |
193 last->next = t; |
| |
194 } |
| |
195 return 0; |
| |
196 } else { |
| |
197 return 1; |
| |
198 } |
| |
199 } |
| |
200 |
| |
201 /** |
| |
202 * Runs a test suite and writes the test log to the specified stream. |
| |
203 * @param suite the test suite to run |
| |
204 * @param out_target the target buffer or file to write the output to |
| |
205 * @param out_writer the write function writing to \p out_target |
| |
206 */ |
| |
207 cx_attr_nonnull |
| |
208 static inline void cx_test_run(CxTestSuite *suite, |
| |
209 void *out_target, cx_write_func out_writer) { |
| |
210 if (suite->name == NULL) { |
| |
211 out_writer("*** Test Suite ***\n", 1, 19, out_target); |
| |
212 } else { |
| |
213 out_writer("*** Test Suite : ", 1, 17, out_target); |
| |
214 out_writer(suite->name, 1, strlen(suite->name), out_target); |
| |
215 out_writer(" ***\n", 1, 5, out_target); |
| |
216 } |
| |
217 suite->success = 0; |
| |
218 suite->failure = 0; |
| |
219 for (CxTestSet *elem = suite->tests; elem; elem = elem->next) { |
| |
220 elem->test(suite, out_target, out_writer); |
| |
221 } |
| |
222 out_writer("\nAll test completed.\n", 1, 21, out_target); |
| |
223 char total[80]; |
| |
224 int len = snprintf( |
| |
225 total, 80, |
| |
226 " Total: %u\n Success: %u\n Failure: %u\n\n", |
| |
227 suite->success + suite->failure, suite->success, suite->failure |
| |
228 ); |
| |
229 out_writer(total, 1, len, out_target); |
| |
230 } |
| |
231 |
| |
232 /** |
| |
233 * Runs a test suite and writes the test log to the specified FILE stream. |
| |
234 * @param suite the test suite to run |
| |
235 * @param file the target file to write the output to |
| |
236 */ |
| |
237 #define cx_test_run_f(suite, file) cx_test_run(suite, (void*)file, (cx_write_func)fwrite) |
| |
238 |
| |
239 /** |
| |
240 * Runs a test suite and writes the test log to stdout. |
| |
241 * @param suite the test suite to run |
| |
242 */ |
| |
243 #define cx_test_run_stdout(suite) cx_test_run_f(suite, stdout) |
| |
244 |
| |
245 /** |
| |
246 * Macro for a #CxTest function header. |
| |
247 * |
| |
248 * Use this macro to declare and/or define a #CxTest function. |
| |
249 * |
| |
250 * @param name the name of the test function |
| |
251 */ |
| |
252 #define CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, cx_write_func _writefnc_) |
| |
253 |
| |
254 /** |
| |
255 * Defines the scope of a test. |
| |
256 * @attention Any CX_TEST_ASSERT() calls must be performed in scope of |
| |
257 * #CX_TEST_DO. |
| |
258 */ |
| |
259 #define CX_TEST_DO _writefnc_("Running ", 1, 8, _output_);\ |
| |
260 _writefnc_(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ |
| |
261 _writefnc_("... ", 1, 4, _output_);\ |
| |
262 jmp_buf _env_;\ |
| |
263 for (unsigned int _cx_test_loop_ = 0 ;\ |
| |
264 _cx_test_loop_ == 0 && !setjmp(_env_);\ |
| |
265 _writefnc_("success.\n", 1, 9, _output_),\ |
| |
266 _suite_->success++, _cx_test_loop_++) |
| |
267 |
| |
268 /** |
| |
269 * Checks a test assertion. |
| |
270 * If the assertion is correct, the test carries on. If the assertion is not |
| |
271 * correct, the specified message (terminated by a dot and a line break) is |
| |
272 * written to the test suites output stream. |
| |
273 * @param condition the condition to check |
| |
274 * @param message the message that shall be printed out on failure |
| |
275 */ |
| |
276 #define CX_TEST_ASSERTM(condition,message) if (!(condition)) { \ |
| |
277 const char *_assert_msg_ = message; \ |
| |
278 _writefnc_(_assert_msg_, 1, strlen(_assert_msg_), _output_); \ |
| |
279 _writefnc_(".\n", 1, 2, _output_); \ |
| |
280 _suite_->failure++; \ |
| |
281 longjmp(_env_, 1);\ |
| |
282 } (void) 0 |
| |
283 |
| |
284 /** |
| |
285 * Checks a test assertion. |
| |
286 * If the assertion is correct, the test carries on. If the assertion is not |
| |
287 * correct, the specified message (terminated by a dot and a line break) is |
| |
288 * written to the test suites output stream. |
| |
289 * @param condition the condition to check |
| |
290 */ |
| |
291 #define CX_TEST_ASSERT(condition) CX_TEST_ASSERTM(condition, #condition " failed") |
| |
292 |
| |
293 /** |
| |
294 * Macro for a test subroutine function header. |
| |
295 * |
| |
296 * Use this to declare and/or define a subroutine that can be called by using |
| |
297 * CX_TEST_CALL_SUBROUTINE(). |
| |
298 * |
| |
299 * @param name the name of the subroutine |
| |
300 * @param ... the parameter list |
| |
301 * |
| |
302 * @see CX_TEST_CALL_SUBROUTINE() |
| |
303 */ |
| |
304 #define CX_TEST_SUBROUTINE(name,...) void name(CxTestSuite* _suite_,\ |
| |
305 void *_output_, cx_write_func _writefnc_, jmp_buf _env_, __VA_ARGS__) |
| |
306 |
| |
307 /** |
| |
308 * Macro for calling a test subroutine. |
| |
309 * |
| |
310 * Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this |
| |
311 * macro. |
| |
312 * |
| |
313 * @remark You may <b>only</b> call subroutines within a #CX_TEST_DO block. |
| |
314 * |
| |
315 * @param name the name of the subroutine |
| |
316 * @param ... the argument list |
| |
317 * |
| |
318 * @see CX_TEST_SUBROUTINE() |
| |
319 */ |
| |
320 #define CX_TEST_CALL_SUBROUTINE(name,...) \ |
| |
321 name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__) |
| |
322 |
| |
323 #ifdef __cplusplus |
| |
324 } |
| |
325 #endif |
| |
326 |
| |
327 #endif /* UCX_TEST_H */ |
| |
328 |