ucx/test.h

changeset 1
1bcaac272cdf
child 5
88625853ae74
equal deleted inserted replaced
0:0f94d369bb02 1:1bcaac272cdf
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 */
32
33 #ifndef TEST_H
34 #define TEST_H
35
36 #include "ucx.h"
37 #include <stdio.h>
38 #include <string.h>
39 #include <setjmp.h>
40 #include "list.h"
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 #ifndef __FUNCTION__
47 #define __FUNCTION__ __func__
48 #endif
49
50 typedef struct {
51 unsigned int success;
52 unsigned int failure;
53 UcxList *tests;
54 } UcxTestSuite;
55
56 typedef void(*UcxTest)(UcxTestSuite*,FILE*);
57
58 UcxTestSuite* ucx_test_suite_new();
59 void ucx_test_suite_free(UcxTestSuite*);
60
61 void ucx_test_register(UcxTestSuite*, UcxTest);
62 void ucx_test_run(UcxTestSuite*, FILE*);
63
64 #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *)
65 #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_)
66
67 #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
68 fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
69 fwrite("... ", 1, 4, _output_);\
70 jmp_buf _env_; \
71 if (!setjmp(_env_)) {
72
73 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
74 fwrite(message".\n", 1, 2+strlen(message), _output_); \
75 _suite_->failure++; \
76 longjmp(_env_, 1);\
77 }
78
79 #define UCX_TEST_SUBROUTINE(name,data) void name(UcxTestSuite* _suite_,\
80 FILE *_output_, jmp_buf _env_, void* data)
81 #define UCX_TEST_CALL_SUBROUTINE(name,data) name(_suite_,_output_,_env_,data);
82
83 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
84
85 #ifdef __cplusplus
86 }
87 #endif
88
89 #endif /* TEST_H */
90

mercurial