src/cx/test.h

changeset 1710
d53be93acc10
parent 1675
36c0fb2b60b2
equal deleted inserted replaced
1709:560a60fcf6ee 1710:d53be93acc10
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 * }
65 */ 65 */
66 66
67 #ifndef UCX_TEST_H 67 #ifndef UCX_TEST_H
68 #define UCX_TEST_H 68 #define UCX_TEST_H
69 69
70 #include "common.h" 70 #include <stdlib.h>
71
72 #include <stdio.h> 71 #include <stdio.h>
73 #include <string.h> 72 #include <string.h>
74 #include <setjmp.h> 73 #include <setjmp.h>
75 74
76 #ifndef __FUNCTION__ 75 #ifndef __FUNCTION__
88 #endif 87 #endif
89 88
90 /** Type for the CxTestSuite. */ 89 /** Type for the CxTestSuite. */
91 typedef struct CxTestSuite CxTestSuite; 90 typedef struct CxTestSuite CxTestSuite;
92 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
93 /** Pointer to a test function. */ 95 /** Pointer to a test function. */
94 typedef void(*CxTest)(CxTestSuite *, void *, cx_write_func); 96 typedef void(*CxTest)(CxTestSuite *, void *, cx_test_output_func);
95 97
96 /** Type for the internal list of test cases. */ 98 /** Type for the internal list of test cases. */
97 typedef struct CxTestSet CxTestSet; 99 typedef struct CxTestSet CxTestSet;
98 100
99 /** Structure for the internal list of test cases. */ 101 /** Structure for the internal list of test cases. */
130 /** 132 /**
131 * Creates a new test suite. 133 * Creates a new test suite.
132 * @param name optional name of the suite 134 * @param name optional name of the suite
133 * @return a new test suite 135 * @return a new test suite
134 */ 136 */
135 CX_NONNULL CX_NODISCARD CX_CSTR_ARG(1) CX_MALLOC
136 static inline CxTestSuite* cx_test_suite_new(const char *name) { 137 static inline CxTestSuite* cx_test_suite_new(const char *name) {
137 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite)); 138 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite));
138 if (suite != NULL) { 139 if (suite != NULL) {
139 suite->name = name; 140 suite->name = name;
140 suite->success = 0; 141 suite->success = 0;
148 /** 149 /**
149 * Deallocates a test suite. 150 * Deallocates a test suite.
150 * 151 *
151 * @param suite the test suite to free 152 * @param suite the test suite to free
152 */ 153 */
153 CX_INLINE void cx_test_suite_free(CxTestSuite* suite) { 154 static inline void cx_test_suite_free(CxTestSuite* suite) {
154 if (suite == NULL) return; 155 if (suite == NULL) return;
155 CxTestSet *l = suite->tests; 156 CxTestSet *l = suite->tests;
156 while (l != NULL) { 157 while (l != NULL) {
157 CxTestSet *e = l; 158 CxTestSet *e = l;
158 l = l->next; 159 l = l->next;
167 * @param suite the suite the test function shall be added to 168 * @param suite the suite the test function shall be added to
168 * @param test the test function to register 169 * @param test the test function to register
169 * @retval zero success 170 * @retval zero success
170 * @retval non-zero failure 171 * @retval non-zero failure
171 */ 172 */
172 CX_NONNULL 173 static inline int cx_test_register(CxTestSuite* suite, CxTest test) {
173 CX_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 CX_NONNULL 199 static inline void cx_test_run(CxTestSuite *suite, void *out_target,
200 CX_INLINE void cx_test_run(CxTestSuite *suite, void *out_target, cx_write_func out_writer) { 200 cx_test_output_func out_writer) {
201 if (suite->name == NULL) { 201 if (suite->name == NULL) {
202 out_writer("*** Test Suite ***\n", 1, 19, out_target); 202 out_writer("*** Test Suite ***\n", 1, 19, out_target);
203 } else { 203 } else {
204 out_writer("*** Test Suite : ", 1, 17, out_target); 204 out_writer("*** Test Suite : ", 1, 17, out_target);
205 out_writer(suite->name, 1, strlen(suite->name), out_target); 205 out_writer(suite->name, 1, strlen(suite->name), out_target);
223 /** 223 /**
224 * 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.
225 * @param suite (@c CxTestSuite*) the test suite to run 225 * @param suite (@c CxTestSuite*) the test suite to run
226 * @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
227 */ 227 */
228 #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)
229 230
230 /** 231 /**
231 * Runs a test suite and writes the test log to stdout. 232 * Runs a test suite and writes the test log to stdout.
232 * @param suite (@c CxTestSuite*) the test suite to run 233 * @param suite (@c CxTestSuite*) the test suite to run
233 */ 234 */
238 * 239 *
239 * Use this macro to declare and/or define a #CxTest function. 240 * Use this macro to declare and/or define a #CxTest function.
240 * 241 *
241 * @param name the name of the test function 242 * @param name the name of the test function
242 */ 243 */
243 #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_)
244 246
245 /** 247 /**
246 * Defines the scope of a test. 248 * Defines the scope of a test.
247 * 249 *
248 * @code 250 * @code
288 * 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
289 * 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
290 * written to the test suites output stream. 292 * written to the test suites output stream.
291 * @param condition (@c bool) the condition to check 293 * @param condition (@c bool) the condition to check
292 */ 294 */
293 #define CX_TEST_ASSERT(condition) CX_TEST_ASSERTM(condition, #condition " failed") 295 #define CX_TEST_ASSERT(condition) \
296 CX_TEST_ASSERTM(condition, #condition " failed")
294 297
295 /** 298 /**
296 * Macro for a test subroutine function header. 299 * Macro for a test subroutine function header.
297 * 300 *
298 * 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
302 * @param ... the parameter list 305 * @param ... the parameter list
303 * 306 *
304 * @see CX_TEST_CALL_SUBROUTINE() 307 * @see CX_TEST_CALL_SUBROUTINE()
305 */ 308 */
306 #define CX_TEST_SUBROUTINE(name,...) void name(CxTestSuite* _suite_,\ 309 #define CX_TEST_SUBROUTINE(name,...) void name(CxTestSuite* _suite_,\
307 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__)
308 312
309 /** 313 /**
310 * Macro for calling a test subroutine. 314 * Macro for calling a test subroutine.
311 * 315 *
312 * 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