src/cx/test.h

changeset 766
e59b76889f00
child 767
d31f4d4075dc
equal deleted inserted replaced
765:b5128bb44459 766:e59b76889f00
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_BEGIN
52 * // tests with CX_TEST_ASSERT() and/or
53 * // calls with CX_TEST_CALL_SUBROUTINE() here
54 * #CX_TEST_END
55 * // cleanup of memory here
56 * }
57 * </pre>
58 *
59 * @remark if a test fails, execution continues at the
60 * #CX_TEST_END macro! So make sure every necessary cleanup happens afterwards.
61 *
62 * @attention Do not call own functions within a test, that use
63 * CX_TEST_ASSERT() macros and are not defined by using CX_TEST_SUBROUTINE().
64 *
65 * @author Mike Becker
66 * @author Olaf Wintermann
67 *
68 */
69
70 #ifndef UCX_TEST_H
71 #define UCX_TEST_H
72
73 #include <stdlib.h>
74 #include <stdio.h>
75 #include <string.h>
76 #include <setjmp.h>
77
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81
82 #ifndef __FUNCTION__
83 /**
84 * Alias for the <code>__func__</code> preprocessor macro.
85 * Some compilers use <code>__func__</code> and others use __FUNCTION__.
86 * We use __FUNCTION__ so we define it for those compilers which use
87 * <code>__func__</code>.
88 */
89 #define __FUNCTION__ __func__
90 #endif
91
92 #ifndef UCX_COMMON_H
93 /**
94 * Function pointer compatible with fwrite-like functions.
95 */
96 typedef size_t (*cx_write_func)(
97 void const *,
98 size_t,
99 size_t,
100 void *
101 );
102 #endif // UCX_COMMON_H
103
104 /** Type for the CxTestSuite. */
105 typedef struct CxTestSuite CxTestSuite;
106
107 /** Pointer to a test function. */
108 typedef void(*CxTest)(CxTestSuite *, void *, cx_write_func);
109
110 /** Type for the internal list of test cases. */
111 typedef struct CxTestSet CxTestSet;
112
113 /** Structure for the internal list of test cases. */
114 struct CxTestSet {
115
116 /** Test case. */
117 CxTest test;
118
119 /** Pointer to the next list element. */
120 CxTestSet *next;
121 };
122
123 /**
124 * A test suite containing multiple test cases.
125 */
126 struct CxTestSuite {
127
128 /** The number of successful tests after the suite has been run. */
129 unsigned int success;
130
131 /** The number of failed tests after the suite has been run. */
132 unsigned int failure;
133
134 /** The optional name of this test suite. */
135 char const *name;
136
137 /**
138 * Internal list of test cases.
139 * Use cx_test_register() to add tests to this list.
140 */
141 CxTestSet *tests;
142 };
143
144 /**
145 * Creates a new test suite.
146 * @param name optional name of the suite
147 * @return a new test suite
148 */
149 static inline CxTestSuite* cx_test_suite_new(char const *name) {
150 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite));
151 if (suite != NULL) {
152 suite->name = name;
153 suite->success = 0;
154 suite->failure = 0;
155 suite->tests = NULL;
156 }
157
158 return suite;
159 }
160
161 /**
162 * Destroys a test suite.
163 * @param suite the test suite to destroy
164 */
165 static inline void cx_test_suite_free(CxTestSuite* suite) {
166 CxTestSet *l = suite->tests;
167 while (l != NULL) {
168 CxTestSet *e = l;
169 l = l->next;
170 free(e);
171 }
172 free(suite);
173 }
174
175 /**
176 * Registers a test function with the specified test suite.
177 *
178 * @param suite the suite, the test function shall be added to
179 * @param test the test function to register
180 * @return zero on success or non-zero on failure
181 */
182 static inline int cx_test_register(CxTestSuite* suite, CxTest test) {
183 CxTestSet *t = (CxTestSet*) malloc(sizeof(CxTestSet));
184 if (t) {
185 t->test = test;
186 t->next = NULL;
187 if (suite->tests == NULL) {
188 suite->tests = t;
189 } else {
190 CxTestSet *last = suite->tests;
191 while (last->next) {
192 last = last->next;
193 }
194 last->next = t;
195 }
196 return 0;
197 } else {
198 return 1;
199 }
200 }
201
202 /**
203 * Runs a test suite and writes the test log to the specified stream.
204 * @param suite the test suite to run
205 * @param out_target the target buffer or file to write the output to
206 * @param out_writer the write function writing to \p out_target
207 */
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",
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 * Marks the begin of a test.
256 * <b>Note:</b> Any CX_TEST_ASSERT() calls must be performed <b>after</b>
257 * #CX_TEST_BEGIN.
258 *
259 * @see #CX_TEST_END
260 */
261 #define CX_TEST_BEGIN _writefnc_("Running ", 1, 8, _output_);\
262 _writefnc_(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
263 _writefnc_("... ", 1, 4, _output_);\
264 jmp_buf _env_; \
265 if (!setjmp(_env_)) {
266
267 /**
268 * Checks a test assertion.
269 * If the assertion is correct, the test carries on. If the assertion is not
270 * correct, the specified message (terminated by a dot and a line break) is
271 * written to the test suites output stream.
272 * @param condition the condition to check
273 * @param message the message that shall be printed out on failure
274 */
275 #define CX_TEST_ASSERT(condition,message) if (!(condition)) { \
276 _writefnc_(message".\n", 1, 2+strlen(message), _output_); \
277 _suite_->failure++; \
278 longjmp(_env_, 1);\
279 }
280
281 /**
282 * Macro for a test subroutine function header.
283 *
284 * Use this to declare and/or define a subroutine that can be called by using
285 * CX_TEST_CALL_SUBROUTINE().
286 *
287 * @param name the name of the subroutine
288 * @param ... the parameter list
289 *
290 * @see CX_TEST_CALL_SUBROUTINE()
291 */
292 #define CX_TEST_SUBROUTINE(name,...) void name(CxTestSuite* _suite_,\
293 void *_output_, jmp_buf _env_, __VA_ARGS__)
294
295 /**
296 * Macro for calling a test subroutine.
297 *
298 * Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this
299 * macro.
300 *
301 * <b>Note:</b> You may <b>only</b> call subroutines within a #CX_TEST_BEGIN-
302 * #CX_TEST_END-block.
303 *
304 * @param name the name of the subroutine
305 * @param ... the argument list
306 *
307 * @see CX_TEST_SUBROUTINE()
308 */
309 #define CX_TEST_CALL_SUBROUTINE(name,...) \
310 name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__);
311
312 /**
313 * Marks the end of a test.
314 * <b>Note:</b> Any CX_TEST_ASSERT() calls must be performed <b>before</b>
315 * #CX_TEST_END.
316 *
317 * @see #CX_TEST_BEGIN
318 */
319 #define CX_TEST_END _writefnc_("success.\n", 1, 9, _output_); _suite_->success++;}
320
321 #ifdef __cplusplus
322 }
323 #endif
324
325 #endif /* UCX_TEST_H */
326

mercurial