ucx/test.h

changeset 134
4d320dc3a7af
parent 128
b79b1ce438dd
child 135
a0aa1c15f46b
equal deleted inserted replaced
133:0a70e0d36949 134:4d320dc3a7af
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 /* 29 /**
30 * 30 * @file: test.h
31 *
32 * UCX Test Framework.
33 *
31 * Usage of this test framework: 34 * Usage of this test framework:
32 * 35 *
33 * **** IN HEADER FILE: **** 36 * **** IN HEADER FILE: ****
34 * 37 *
35 * UCX_TEST_DECLARE(function_name) 38 * <pre>
39 * UCX_TEST(function_name)
40 * UCX_TEST_SUBROUTINE(subroutine_name, paramlist) // optional
41 * </pre>
36 * 42 *
37 * **** IN SOURCE FILE: **** 43 * **** IN SOURCE FILE: ****
38 * 44 * <pre>
39 * UCX_TEST_IMPLEMENT(function_name) { 45 * UCX_TEST_SUBROUTINE(subroutine_name, paramlist) {
40 * <memory allocation and other stuff here> 46 * // tests with UCX_TEST_ASSERT()
41 * UCX_TEST_BEGIN
42 * <tests with UCX_TEST_ASSERT here>
43 * UCX_TEST_END
44 * <cleanup of memory here>
45 * } 47 * }
46 * 48 *
47 * PLEASE NOTE: if a test fails, a longjump is performed 49 * UCX_TEST(function_name) {
48 * back to the UCX_TEST_BEGIN macro! 50 * // memory allocation and other stuff here
49 * 51 * #UCX_TEST_BEGIN
52 * // tests with UCX_TEST_ASSERT() and/or
53 * // calls with UCX_TEST_CALL_SUBROUTINE() here
54 * #UCX_TEST_END
55 * // cleanup of memory here
56 * }
57 * </pre>
58 *
59 * <b>Note:</b> if a test fails, a longjump is performed
60 * back to the #UCX_TEST_BEGIN macro!
61 *
62 * <b>Attention:</b> Do not call own functions within a test, that use
63 * UCX_TEST_ASSERT() macros and are not defined by using UCX_TEST_SUBROUTINE().
64 *
65 *
66 * @author Mike Becker
67 * @author Olaf Wintermann
50 * 68 *
51 */ 69 */
52 70
53 #ifndef UCX_TEST_H 71 #ifndef UCX_TEST_H
54 #define UCX_TEST_H 72 #define UCX_TEST_H
61 #ifdef __cplusplus 79 #ifdef __cplusplus
62 extern "C" { 80 extern "C" {
63 #endif 81 #endif
64 82
65 #ifndef __FUNCTION__ 83 #ifndef __FUNCTION__
84 /**
85 * Alias for the __func__ preprocessor macro.
86 * Some compilers use __func__ and others use __FUNC__. We use __FUNC__ so we
87 * define it for those compilers which use __func__.
88 */
66 #define __FUNCTION__ __func__ 89 #define __FUNCTION__ __func__
67 #endif 90 #endif
68 91
92 /** Type for the internal list of test cases. */
69 typedef struct UcxTestList UcxTestList; 93 typedef struct UcxTestList UcxTestList;
94 /** Type for the UcxTestSuite. */
70 typedef struct UcxTestSuite UcxTestSuite; 95 typedef struct UcxTestSuite UcxTestSuite;
96 /** Pointer to a test function. */
71 typedef void(*UcxTest)(UcxTestSuite*,FILE*); 97 typedef void(*UcxTest)(UcxTestSuite*,FILE*);
72 98
73 struct UcxTestList{ 99 /**
74 UcxTest test; 100 * A test suite containing multiple test cases.
75 UcxTestList *next; 101 */
76 };
77
78 struct UcxTestSuite { 102 struct UcxTestSuite {
103 /** The number of successful tests after the suite has been run. */
79 unsigned int success; 104 unsigned int success;
105 /** The number of failed tests after the suite has been run. */
80 unsigned int failure; 106 unsigned int failure;
107 /**
108 * Internal list of test cases.
109 * Use ucx_test_register() to add tests to this list.
110 */
81 UcxTestList *tests; 111 UcxTestList *tests;
82 }; 112 };
83 113
114 /**
115 * Creates a new test suite.
116 * @return a new test suite
117 */
84 UcxTestSuite* ucx_test_suite_new(); 118 UcxTestSuite* ucx_test_suite_new();
85 void ucx_test_suite_free(UcxTestSuite*); 119 /**
86 120 * Destroys a test suite.
87 int ucx_test_register(UcxTestSuite*, UcxTest); 121 * @param the test suite to destroy
88 void ucx_test_run(UcxTestSuite*, FILE*); 122 */
89 123 void ucx_test_suite_free(UcxTestSuite* suite);
90 #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *) 124
91 #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_) 125 /**
92 126 * Registers a test function with the specified test suite.
127 *
128 * @param suite the suite, the test function shall be added to
129 * @param test the test function to register
130 * @return EXIT_SUCCESS on success or EXIT_FAILURE on failure
131 */
132 int ucx_test_register(UcxTestSuite* suite, UcxTest test);
133 /**
134 * Runs a test suite and writes the test log to the specified stream.
135 * @param suite the test suite to run
136 * @param outstream the stream the log shall be written to
137 */
138 void ucx_test_run(UcxTestSuite* suite, FILE* outstream);
139
140 /**
141 * Macro for a #UcxTest function header.
142 *
143 * Use this macro to declare and/or define an #UcxTest function.
144 *
145 * @param name the name of the test function
146 */
147 #define UCX_TEST(name) void name(UcxTestSuite* _suite_,FILE *_output_)
148
149 /**
150 * Marks the begin of a test.
151 * <b>Note:</b> Any UCX_TEST_ASSERT() calls must be performed <b>after</b>
152 * #UCX_TEST_BEGIN.
153 *
154 * @see #UCX_TEST_END
155 */
93 #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\ 156 #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
94 fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ 157 fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
95 fwrite("... ", 1, 4, _output_);\ 158 fwrite("... ", 1, 4, _output_);\
96 jmp_buf _env_; \ 159 jmp_buf _env_; \
97 if (!setjmp(_env_)) { 160 if (!setjmp(_env_)) {
98 161
162 /**
163 * Checks a test assertion.
164 * If the assertion is correct, the test carries on. If the assertion is not
165 * correct, the specified message (terminated by a dot and a line break) is
166 * written to the test suites output stream.
167 * @param condition the condition to check
168 * @param message the message that shall be printed out on failure
169 */
99 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ 170 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
100 fwrite(message".\n", 1, 2+strlen(message), _output_); \ 171 fwrite(message".\n", 1, 2+strlen(message), _output_); \
101 _suite_->failure++; \ 172 _suite_->failure++; \
102 longjmp(_env_, 1);\ 173 longjmp(_env_, 1);\
103 } 174 }
104 175
176 /**
177 * Macro for a test subroutine function header.
178 *
179 * Use this to declare and/or define an subroutine that can be called by using
180 * UCX_TEST_CALL_SUBROUTINE().
181 *
182 * @param name the name of the subroutine
183 * @param ... the parameter list
184 *
185 * @see UCX_TEST_CALL_SUBROUTINE()
186 */
105 #define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\ 187 #define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\
106 FILE *_output_, jmp_buf _env_, __VA_ARGS__) 188 FILE *_output_, jmp_buf _env_, __VA_ARGS__)
189
190 /**
191 * Macro for calling a test subroutine.
192 *
193 * Subroutines declared with UCX_TEST_SUBROUTINE() can be called by using this
194 * macro.
195 *
196 * <b>Note:</b> You may <b>only</b> call subroutines within a #UCX_TEST_BEGIN-
197 * #UCX_TEST_END-block.
198 *
199 * @param name the name of the subroutine
200 * @param ... the argument list
201 *
202 * @see UCX_TEST_SUBROUTINE()
203 */
107 #define UCX_TEST_CALL_SUBROUTINE(name,...) \ 204 #define UCX_TEST_CALL_SUBROUTINE(name,...) \
108 name(_suite_,_output_,_env_,__VA_ARGS__); 205 name(_suite_,_output_,_env_,__VA_ARGS__);
109 206
207 /**
208 * Marks the end of a test.
209 * <b>Note:</b> Any UCX_TEST_ASSERT() calls must be performed <b>before</b>
210 * #UCX_TEST_END.
211 *
212 * @see #UCX_TEST_BEGIN
213 */
110 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;} 214 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
111 215
112 #ifdef __cplusplus 216 #ifdef __cplusplus
113 } 217 }
114 #endif 218 #endif

mercurial