Thu, 31 May 2012 12:51:22 +0200
major refactoring of test framework
1 /*
2 * File: test.h
3 * Author: Mike
4 *
5 * Created on 18. Februar 2012, 14:15
6 *
7 *
8 *
9 * Usage of this test framework:
10 *
11 * **** IN HEADER FILE: ****
12 *
13 * UCX_TEST_DECLARE(function_name)
14 *
15 * **** IN SOURCE FILE: ****
16 *
17 * UCX_TEST_IMPLEMENT(function_name) {
18 * <memory allocation and other stuff here>
19 * UCX_TEST_BEGIN
20 * <tests with UCX_TEST_ASSERT here>
21 * UCX_TEST_END
22 * <cleanup of memory here>
23 * }
24 *
25 * PLEASE NOTE: if a test fails, a longjump is performed
26 * back to the UCX_TEST_BEGIN macro!
27 *
28 * You may use multiple BEGIN-END blocks if you are aware of the
29 * longjmp behaviour.
30 *
31 */
33 #ifndef TEST_H
34 #define TEST_H
36 #include <stdio.h>
37 #include <string.h>
38 #include <setjmp.h>
39 #include "list.h"
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
45 typedef struct {
46 unsigned int success;
47 unsigned int failure;
48 UcxList *tests;
49 } UcxTestSuite;
51 typedef void(*UcxTest)(UcxTestSuite*,FILE*);
53 UcxTestSuite* ucx_test_suite_new();
54 void ucx_test_suite_free(UcxTestSuite*);
56 void ucx_test_register(UcxTestSuite*, UcxTest);
57 void ucx_test_run(UcxTestSuite*, FILE*);
59 #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *);
60 #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_)
62 #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
63 fwrite(__func__, 1, strlen(__func__), _output_);\
64 fwrite("... ", 1, 4, _output_);\
65 jmp_buf _env_; \
66 if (!setjmp(_env_)) {
68 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
69 fwrite(message".\n", 1, 2+strlen(message), _output_); \
70 _suite_->failure++; \
71 longjmp(_env_, 1);\
72 }
74 #define UCX_TEST_SUBROUTINE(name,data) void name(UcxTestSuite* _suite_,\
75 FILE *_output_, jmp_buf _env_, void* data)
76 #define UCX_TEST_CALL_SUBROUTINE(name,data) name(_suite_,_output_,_env_,data);
78 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
80 #ifdef __cplusplus
81 }
82 #endif
84 #endif /* TEST_H */