ucx/test.h

changeset 33
9c219a62070d
parent 27
22644e2572bc
child 69
fb59270b1de3
equal deleted inserted replaced
32:c7af4ec56e19 33:9c219a62070d
1 /* 1 /*
2 * File: test.h 2 * File: test.h
3 * Author: Mike 3 * Author: Mike
4 * 4 *
5 * Created on 18. Februar 2012, 14:15 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 *
6 */ 31 */
7 32
8 #ifndef TEST_H 33 #ifndef TEST_H
9 #define TEST_H 34 #define TEST_H
10 35
11 #include <stdio.h> 36 #include <stdio.h>
12 #include <string.h> 37 #include <string.h>
38 #include <setjmp.h>
13 #include "list.h" 39 #include "list.h"
14 40
15 #ifdef __cplusplus 41 #ifdef __cplusplus
16 extern "C" { 42 extern "C" {
17 #endif 43 #endif
29 55
30 void ucx_test_register(UcxTestSuite*, UcxTest); 56 void ucx_test_register(UcxTestSuite*, UcxTest);
31 void ucx_test_run(UcxTestSuite*, FILE*); 57 void ucx_test_run(UcxTestSuite*, FILE*);
32 58
33 #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *); 59 #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *);
34 #define UCX_TEST_BEGIN(name) void name(UcxTestSuite* _suite_,FILE *_output_) {\ 60 #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_)
35 fwrite("Running "#name"... ", 1, 12+strlen(#name), _output_); 61
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_)) {
36 67
37 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ 68 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
38 fwrite(message".\n", 1, 2+strlen(message), _output_); \ 69 fwrite(message".\n", 1, 2+strlen(message), _output_); \
39 _suite_->failure++; \ 70 _suite_->failure++; \
40 return;\ 71 longjmp(_env_, 1);\
41 } 72 }
42 73
43 #define UCX_TEST_END } fwrite("success.\n", 1, 9, _output_); _suite_->success++; 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);
77
78 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
44 79
45 #ifdef __cplusplus 80 #ifdef __cplusplus
46 } 81 }
47 #endif 82 #endif
48 83

mercurial