1.1 --- a/src/cx/test.h Wed Dec 20 16:46:14 2023 +0100 1.2 +++ b/src/cx/test.h Wed Dec 20 17:57:18 2023 +0100 1.3 @@ -48,16 +48,13 @@ 1.4 * 1.5 * CX_TEST(function_name) { 1.6 * // memory allocation and other stuff here 1.7 - * #CX_TEST_BEGIN 1.8 - * // tests with CX_TEST_ASSERT() and/or 1.9 - * // calls with CX_TEST_CALL_SUBROUTINE() here 1.10 - * #CX_TEST_END 1.11 + * #CX_TEST_DO { 1.12 + * // tests with CX_TEST_ASSERT() and/or 1.13 + * // calls with CX_TEST_CALL_SUBROUTINE() here 1.14 + * } 1.15 * // cleanup of memory here 1.16 * } 1.17 * </pre> 1.18 - * 1.19 - * @remark if a test fails, execution continues at the 1.20 - * #CX_TEST_END macro! So make sure every necessary cleanup happens afterwards. 1.21 * 1.22 * @attention Do not call own functions within a test, that use 1.23 * CX_TEST_ASSERT() macros and are not defined by using CX_TEST_SUBROUTINE(). 1.24 @@ -252,17 +249,18 @@ 1.25 #define CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, cx_write_func _writefnc_) 1.26 1.27 /** 1.28 - * Marks the begin of a test. 1.29 - * <b>Note:</b> Any CX_TEST_ASSERT() calls must be performed <b>after</b> 1.30 - * #CX_TEST_BEGIN. 1.31 - * 1.32 - * @see #CX_TEST_END 1.33 + * Defines the scope of a test. 1.34 + * @attention Any CX_TEST_ASSERT() calls must be performed in scope of 1.35 + * #CX_TEST_DO. 1.36 */ 1.37 -#define CX_TEST_BEGIN _writefnc_("Running ", 1, 8, _output_);\ 1.38 +#define CX_TEST_DO _writefnc_("Running ", 1, 8, _output_);\ 1.39 _writefnc_(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ 1.40 _writefnc_("... ", 1, 4, _output_);\ 1.41 - jmp_buf _env_; \ 1.42 - if (!setjmp(_env_)) { 1.43 + jmp_buf _env_;\ 1.44 + for (unsigned int _cx_test_loop_ = 0 ;\ 1.45 + _cx_test_loop_ == 0 && !setjmp(_env_);\ 1.46 + _writefnc_("success.\n", 1, 9, _output_),\ 1.47 + _suite_->success++, _cx_test_loop_++) 1.48 1.49 /** 1.50 * Checks a test assertion. 1.51 @@ -272,11 +270,22 @@ 1.52 * @param condition the condition to check 1.53 * @param message the message that shall be printed out on failure 1.54 */ 1.55 -#define CX_TEST_ASSERT(condition,message) if (!(condition)) { \ 1.56 - _writefnc_(message".\n", 1, 2+strlen(message), _output_); \ 1.57 +#define CX_TEST_ASSERTM(condition,message) if (!(condition)) { \ 1.58 + char const* _assert_msg_ = message; \ 1.59 + _writefnc_(_assert_msg_, 1, strlen(_assert_msg_), _output_); \ 1.60 + _writefnc_(".\n", 1, 2, _output_); \ 1.61 _suite_->failure++; \ 1.62 longjmp(_env_, 1);\ 1.63 - } 1.64 + } (void) 0 1.65 + 1.66 +/** 1.67 + * Checks a test assertion. 1.68 + * If the assertion is correct, the test carries on. If the assertion is not 1.69 + * correct, the specified message (terminated by a dot and a line break) is 1.70 + * written to the test suites output stream. 1.71 + * @param condition the condition to check 1.72 + */ 1.73 +#define CX_TEST_ASSERT(condition) CX_TEST_ASSERTM(condition, #condition " failed") 1.74 1.75 /** 1.76 * Macro for a test subroutine function header. 1.77 @@ -298,8 +307,7 @@ 1.78 * Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this 1.79 * macro. 1.80 * 1.81 - * <b>Note:</b> You may <b>only</b> call subroutines within a #CX_TEST_BEGIN- 1.82 - * #CX_TEST_END-block. 1.83 + * @remark You may <b>only</b> call subroutines within a #CX_TEST_DO block. 1.84 * 1.85 * @param name the name of the subroutine 1.86 * @param ... the argument list 1.87 @@ -309,15 +317,6 @@ 1.88 #define CX_TEST_CALL_SUBROUTINE(name,...) \ 1.89 name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__); 1.90 1.91 -/** 1.92 - * Marks the end of a test. 1.93 - * <b>Note:</b> Any CX_TEST_ASSERT() calls must be performed <b>before</b> 1.94 - * #CX_TEST_END. 1.95 - * 1.96 - * @see #CX_TEST_BEGIN 1.97 - */ 1.98 -#define CX_TEST_END _writefnc_("success.\n", 1, 9, _output_); _suite_->success++;} 1.99 - 1.100 #ifdef __cplusplus 1.101 } 1.102 #endif