src/cx/test.h

changeset 767
d31f4d4075dc
parent 766
e59b76889f00
child 768
0e1cf2cd500e
equal deleted inserted replaced
766:e59b76889f00 767:d31f4d4075dc
46 * // tests with CX_TEST_ASSERT() 46 * // tests with CX_TEST_ASSERT()
47 * } 47 * }
48 * 48 *
49 * CX_TEST(function_name) { 49 * CX_TEST(function_name) {
50 * // memory allocation and other stuff here 50 * // memory allocation and other stuff here
51 * #CX_TEST_BEGIN 51 * #CX_TEST_DO {
52 * // tests with CX_TEST_ASSERT() and/or 52 * // tests with CX_TEST_ASSERT() and/or
53 * // calls with CX_TEST_CALL_SUBROUTINE() here 53 * // calls with CX_TEST_CALL_SUBROUTINE() here
54 * #CX_TEST_END 54 * }
55 * // cleanup of memory here 55 * // cleanup of memory here
56 * } 56 * }
57 * </pre> 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 * 58 *
62 * @attention Do not call own functions within a test, that use 59 * @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(). 60 * CX_TEST_ASSERT() macros and are not defined by using CX_TEST_SUBROUTINE().
64 * 61 *
65 * @author Mike Becker 62 * @author Mike Becker
250 * @param name the name of the test function 247 * @param name the name of the test function
251 */ 248 */
252 #define CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, cx_write_func _writefnc_) 249 #define CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, cx_write_func _writefnc_)
253 250
254 /** 251 /**
255 * Marks the begin of a test. 252 * Defines the scope of a test.
256 * <b>Note:</b> Any CX_TEST_ASSERT() calls must be performed <b>after</b> 253 * @attention Any CX_TEST_ASSERT() calls must be performed in scope of
257 * #CX_TEST_BEGIN. 254 * #CX_TEST_DO.
258 * 255 */
259 * @see #CX_TEST_END 256 #define CX_TEST_DO _writefnc_("Running ", 1, 8, _output_);\
260 */
261 #define CX_TEST_BEGIN _writefnc_("Running ", 1, 8, _output_);\
262 _writefnc_(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ 257 _writefnc_(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
263 _writefnc_("... ", 1, 4, _output_);\ 258 _writefnc_("... ", 1, 4, _output_);\
264 jmp_buf _env_; \ 259 jmp_buf _env_;\
265 if (!setjmp(_env_)) { 260 for (unsigned int _cx_test_loop_ = 0 ;\
261 _cx_test_loop_ == 0 && !setjmp(_env_);\
262 _writefnc_("success.\n", 1, 9, _output_),\
263 _suite_->success++, _cx_test_loop_++)
266 264
267 /** 265 /**
268 * Checks a test assertion. 266 * Checks a test assertion.
269 * If the assertion is correct, the test carries on. If the assertion is not 267 * 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 268 * correct, the specified message (terminated by a dot and a line break) is
271 * written to the test suites output stream. 269 * written to the test suites output stream.
272 * @param condition the condition to check 270 * @param condition the condition to check
273 * @param message the message that shall be printed out on failure 271 * @param message the message that shall be printed out on failure
274 */ 272 */
275 #define CX_TEST_ASSERT(condition,message) if (!(condition)) { \ 273 #define CX_TEST_ASSERTM(condition,message) if (!(condition)) { \
276 _writefnc_(message".\n", 1, 2+strlen(message), _output_); \ 274 char const* _assert_msg_ = message; \
275 _writefnc_(_assert_msg_, 1, strlen(_assert_msg_), _output_); \
276 _writefnc_(".\n", 1, 2, _output_); \
277 _suite_->failure++; \ 277 _suite_->failure++; \
278 longjmp(_env_, 1);\ 278 longjmp(_env_, 1);\
279 } 279 } (void) 0
280
281 /**
282 * Checks a test assertion.
283 * If the assertion is correct, the test carries on. If the assertion is not
284 * correct, the specified message (terminated by a dot and a line break) is
285 * written to the test suites output stream.
286 * @param condition the condition to check
287 */
288 #define CX_TEST_ASSERT(condition) CX_TEST_ASSERTM(condition, #condition " failed")
280 289
281 /** 290 /**
282 * Macro for a test subroutine function header. 291 * Macro for a test subroutine function header.
283 * 292 *
284 * Use this to declare and/or define a subroutine that can be called by using 293 * Use this to declare and/or define a subroutine that can be called by using
296 * Macro for calling a test subroutine. 305 * Macro for calling a test subroutine.
297 * 306 *
298 * Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this 307 * Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this
299 * macro. 308 * macro.
300 * 309 *
301 * <b>Note:</b> You may <b>only</b> call subroutines within a #CX_TEST_BEGIN- 310 * @remark You may <b>only</b> call subroutines within a #CX_TEST_DO block.
302 * #CX_TEST_END-block.
303 * 311 *
304 * @param name the name of the subroutine 312 * @param name the name of the subroutine
305 * @param ... the argument list 313 * @param ... the argument list
306 * 314 *
307 * @see CX_TEST_SUBROUTINE() 315 * @see CX_TEST_SUBROUTINE()
308 */ 316 */
309 #define CX_TEST_CALL_SUBROUTINE(name,...) \ 317 #define CX_TEST_CALL_SUBROUTINE(name,...) \
310 name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__); 318 name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__);
311 319
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 320 #ifdef __cplusplus
322 } 321 }
323 #endif 322 #endif
324 323
325 #endif /* UCX_TEST_H */ 324 #endif /* UCX_TEST_H */

mercurial