test/ucxtest.h

changeset 213
cf7b86d79d35
parent 149
ea2448961441
equal deleted inserted replaced
212:16a114c84ff5 213:cf7b86d79d35
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_DO { 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 * } 54 * }
55 * // cleanup of memory here 55 * // cleanup of memory here
56 * } 56 * }
70 #include <stdlib.h> 70 #include <stdlib.h>
71 #include <stdio.h> 71 #include <stdio.h>
72 #include <string.h> 72 #include <string.h>
73 #include <setjmp.h> 73 #include <setjmp.h>
74 74
75 typedef size_t (*cx_write_func)(const void*, size_t, size_t, void*);
76
77 #ifndef __FUNCTION__ 75 #ifndef __FUNCTION__
78 /** 76 /**
79 * Alias for the <code>__func__</code> preprocessor macro. 77 * Alias for the <code>__func__</code> preprocessor macro.
80 * Some compilers use <code>__func__</code> and others use __FUNCTION__. 78 * Some compilers use <code>__func__</code> and others use __FUNCTION__.
81 * We use __FUNCTION__ so we define it for those compilers which use 79 * We use __FUNCTION__ so we define it for those compilers which use
84 #define __FUNCTION__ __func__ 82 #define __FUNCTION__ __func__
85 #endif 83 #endif
86 84
87 #if !defined(__clang__) && __GNUC__ > 3 85 #if !defined(__clang__) && __GNUC__ > 3
88 #pragma GCC diagnostic ignored "-Wclobbered" 86 #pragma GCC diagnostic ignored "-Wclobbered"
89 #pragma GCC diagnostic ignored "-Wunused-function"
90 #endif 87 #endif
91 88
92 /** Type for the CxTestSuite. */ 89 /** Type for the CxTestSuite. */
93 typedef struct CxTestSuite CxTestSuite; 90 typedef struct CxTestSuite CxTestSuite;
94 91
92 /** Output function compatible to cx_write_func */
93 typedef size_t (*cx_test_output_func)(const void*, size_t, size_t, void*);
94
95 /** Pointer to a test function. */ 95 /** Pointer to a test function. */
96 typedef void(*CxTest)(CxTestSuite *, void *, cx_write_func); 96 typedef void(*CxTest)(CxTestSuite *, void *, cx_test_output_func);
97 97
98 /** Type for the internal list of test cases. */ 98 /** Type for the internal list of test cases. */
99 typedef struct CxTestSet CxTestSet; 99 typedef struct CxTestSet CxTestSet;
100 100
101 /** Structure for the internal list of test cases. */ 101 /** Structure for the internal list of test cases. */
132 /** 132 /**
133 * Creates a new test suite. 133 * Creates a new test suite.
134 * @param name optional name of the suite 134 * @param name optional name of the suite
135 * @return a new test suite 135 * @return a new test suite
136 */ 136 */
137 static CxTestSuite* cx_test_suite_new(const char *name) { 137 static inline CxTestSuite* cx_test_suite_new(const char *name) {
138 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite)); 138 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite));
139 if (suite != NULL) { 139 if (suite != NULL) {
140 suite->name = name; 140 suite->name = name;
141 suite->success = 0; 141 suite->success = 0;
142 suite->failure = 0; 142 suite->failure = 0;
149 /** 149 /**
150 * Deallocates a test suite. 150 * Deallocates a test suite.
151 * 151 *
152 * @param suite the test suite to free 152 * @param suite the test suite to free
153 */ 153 */
154 static void cx_test_suite_free(CxTestSuite* suite) { 154 static inline void cx_test_suite_free(CxTestSuite* suite) {
155 if (suite == NULL) return; 155 if (suite == NULL) return;
156 CxTestSet *l = suite->tests; 156 CxTestSet *l = suite->tests;
157 while (l != NULL) { 157 while (l != NULL) {
158 CxTestSet *e = l; 158 CxTestSet *e = l;
159 l = l->next; 159 l = l->next;
168 * @param suite the suite the test function shall be added to 168 * @param suite the suite the test function shall be added to
169 * @param test the test function to register 169 * @param test the test function to register
170 * @retval zero success 170 * @retval zero success
171 * @retval non-zero failure 171 * @retval non-zero failure
172 */ 172 */
173 static int cx_test_register(CxTestSuite* suite, CxTest test) { 173 static inline int cx_test_register(CxTestSuite* suite, CxTest test) {
174 CxTestSet *t = (CxTestSet*) malloc(sizeof(CxTestSet)); 174 CxTestSet *t = (CxTestSet*) malloc(sizeof(CxTestSet));
175 if (t) { 175 if (t) {
176 t->test = test; 176 t->test = test;
177 t->next = NULL; 177 t->next = NULL;
178 if (suite->tests == NULL) { 178 if (suite->tests == NULL) {
194 * Runs a test suite and writes the test log to the specified stream. 194 * Runs a test suite and writes the test log to the specified stream.
195 * @param suite the test suite to run 195 * @param suite the test suite to run
196 * @param out_target the target buffer or file to write the output to 196 * @param out_target the target buffer or file to write the output to
197 * @param out_writer the write function writing to @p out_target 197 * @param out_writer the write function writing to @p out_target
198 */ 198 */
199 static void cx_test_run(CxTestSuite *suite, void *out_target, cx_write_func out_writer) { 199 static inline void cx_test_run(CxTestSuite *suite, void *out_target,
200 cx_test_output_func out_writer) {
200 if (suite->name == NULL) { 201 if (suite->name == NULL) {
201 out_writer("*** Test Suite ***\n", 1, 19, out_target); 202 out_writer("*** Test Suite ***\n", 1, 19, out_target);
202 } else { 203 } else {
203 out_writer("*** Test Suite : ", 1, 17, out_target); 204 out_writer("*** Test Suite : ", 1, 17, out_target);
204 out_writer(suite->name, 1, strlen(suite->name), out_target); 205 out_writer(suite->name, 1, strlen(suite->name), out_target);
222 /** 223 /**
223 * Runs a test suite and writes the test log to the specified FILE stream. 224 * Runs a test suite and writes the test log to the specified FILE stream.
224 * @param suite (@c CxTestSuite*) the test suite to run 225 * @param suite (@c CxTestSuite*) the test suite to run
225 * @param file (@c FILE*) the target file to write the output to 226 * @param file (@c FILE*) the target file to write the output to
226 */ 227 */
227 #define cx_test_run_f(suite, file) cx_test_run(suite, (void*)file, (cx_write_func)fwrite) 228 #define cx_test_run_f(suite, file) cx_test_run(suite, (void*)file, \
229 (cx_test_output_func) fwrite)
228 230
229 /** 231 /**
230 * Runs a test suite and writes the test log to stdout. 232 * Runs a test suite and writes the test log to stdout.
231 * @param suite (@c CxTestSuite*) the test suite to run 233 * @param suite (@c CxTestSuite*) the test suite to run
232 */ 234 */
237 * 239 *
238 * Use this macro to declare and/or define a #CxTest function. 240 * Use this macro to declare and/or define a #CxTest function.
239 * 241 *
240 * @param name the name of the test function 242 * @param name the name of the test function
241 */ 243 */
242 #define CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, cx_write_func _writefnc_) 244 #define CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, \
245 cx_test_output_func _writefnc_)
243 246
244 /** 247 /**
245 * Defines the scope of a test. 248 * Defines the scope of a test.
246 * 249 *
247 * @code 250 * @code
287 * If the assertion is correct, the test carries on. If the assertion is not 290 * If the assertion is correct, the test carries on. If the assertion is not
288 * correct, the specified message (terminated by a dot and a line break) is 291 * correct, the specified message (terminated by a dot and a line break) is
289 * written to the test suites output stream. 292 * written to the test suites output stream.
290 * @param condition (@c bool) the condition to check 293 * @param condition (@c bool) the condition to check
291 */ 294 */
292 #define CX_TEST_ASSERT(condition) CX_TEST_ASSERTM(condition, #condition " failed") 295 #define CX_TEST_ASSERT(condition) \
296 CX_TEST_ASSERTM(condition, #condition " failed")
293 297
294 /** 298 /**
295 * Macro for a test subroutine function header. 299 * Macro for a test subroutine function header.
296 * 300 *
297 * Use this to declare and/or define a subroutine that can be called by using 301 * Use this to declare and/or define a subroutine that can be called by using
301 * @param ... the parameter list 305 * @param ... the parameter list
302 * 306 *
303 * @see CX_TEST_CALL_SUBROUTINE() 307 * @see CX_TEST_CALL_SUBROUTINE()
304 */ 308 */
305 #define CX_TEST_SUBROUTINE(name,...) void name(CxTestSuite* _suite_,\ 309 #define CX_TEST_SUBROUTINE(name,...) void name(CxTestSuite* _suite_,\
306 void *_output_, cx_write_func _writefnc_, jmp_buf _env_, __VA_ARGS__) 310 void *_output_, cx_test_output_func _writefnc_, \
311 jmp_buf _env_, __VA_ARGS__)
307 312
308 /** 313 /**
309 * Macro for calling a test subroutine. 314 * Macro for calling a test subroutine.
310 * 315 *
311 * Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this 316 * Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this

mercurial