ucx/test.h

changeset 5
88625853ae74
parent 1
1bcaac272cdf
child 17
11dffb40cd91
equal deleted inserted replaced
4:ae5a98f0545c 5:88625853ae74
1 /* 1 /*
2 * File: test.h 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * Author: Mike 3 *
4 * 4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 * Created on 18. Februar 2012, 14:15 5 *
6 * 6 * Redistribution and use in source and binary forms, with or without
7 * 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
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
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file: test.h
31 *
32 * UCX Test Framework.
33 *
9 * Usage of this test framework: 34 * Usage of this test framework:
10 * 35 *
11 * **** IN HEADER FILE: **** 36 * **** IN HEADER FILE: ****
12 * 37 *
13 * UCX_TEST_DECLARE(function_name) 38 * <pre>
39 * UCX_TEST(function_name)
40 * UCX_TEST_SUBROUTINE(subroutine_name, paramlist) // optional
41 * </pre>
14 * 42 *
15 * **** IN SOURCE FILE: **** 43 * **** IN SOURCE FILE: ****
16 * 44 * <pre>
17 * UCX_TEST_IMPLEMENT(function_name) { 45 * UCX_TEST_SUBROUTINE(subroutine_name, paramlist) {
18 * <memory allocation and other stuff here> 46 * // tests with UCX_TEST_ASSERT()
19 * UCX_TEST_BEGIN
20 * <tests with UCX_TEST_ASSERT here>
21 * UCX_TEST_END
22 * <cleanup of memory here>
23 * } 47 * }
24 * 48 *
25 * PLEASE NOTE: if a test fails, a longjump is performed 49 * UCX_TEST(function_name) {
26 * back to the UCX_TEST_BEGIN macro! 50 * // memory allocation and other stuff here
27 * 51 * #UCX_TEST_BEGIN
28 * You may use multiple BEGIN-END blocks if you are aware of the 52 * // tests with UCX_TEST_ASSERT() and/or
29 * longjmp behaviour. 53 * // calls with UCX_TEST_CALL_SUBROUTINE() here
30 * 54 * #UCX_TEST_END
31 */ 55 * // cleanup of memory here
32 56 * }
33 #ifndef TEST_H 57 * </pre>
34 #define TEST_H 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
68 *
69 */
70
71 #ifndef UCX_TEST_H
72 #define UCX_TEST_H
35 73
36 #include "ucx.h" 74 #include "ucx.h"
37 #include <stdio.h> 75 #include <stdio.h>
38 #include <string.h> 76 #include <string.h>
39 #include <setjmp.h> 77 #include <setjmp.h>
40 #include "list.h"
41 78
42 #ifdef __cplusplus 79 #ifdef __cplusplus
43 extern "C" { 80 extern "C" {
44 #endif 81 #endif
45 82
46 #ifndef __FUNCTION__ 83 #ifndef __FUNCTION__
84 /**
85 * Alias for the <code>__func__</code> preprocessor macro.
86 * Some compilers use <code>__func__</code> and others use __FUNC__.
87 * We use __FUNC__ so we define it for those compilers which use
88 * <code>__func__</code>.
89 */
47 #define __FUNCTION__ __func__ 90 #define __FUNCTION__ __func__
48 #endif 91 #endif
49 92
50 typedef struct { 93 /** Type for the internal list of test cases. */
94 typedef struct UcxTestList UcxTestList;
95 /** Type for the UcxTestSuite. */
96 typedef struct UcxTestSuite UcxTestSuite;
97 /** Pointer to a test function. */
98 typedef void(*UcxTest)(UcxTestSuite*,FILE*);
99
100 /**
101 * A test suite containing multiple test cases.
102 */
103 struct UcxTestSuite {
104 /** The number of successful tests after the suite has been run. */
51 unsigned int success; 105 unsigned int success;
106 /** The number of failed tests after the suite has been run. */
52 unsigned int failure; 107 unsigned int failure;
53 UcxList *tests; 108 /**
54 } UcxTestSuite; 109 * Internal list of test cases.
55 110 * Use ucx_test_register() to add tests to this list.
56 typedef void(*UcxTest)(UcxTestSuite*,FILE*); 111 */
57 112 UcxTestList *tests;
113 };
114
115 /**
116 * Creates a new test suite.
117 * @return a new test suite
118 */
58 UcxTestSuite* ucx_test_suite_new(); 119 UcxTestSuite* ucx_test_suite_new();
59 void ucx_test_suite_free(UcxTestSuite*); 120 /**
60 121 * Destroys a test suite.
61 void ucx_test_register(UcxTestSuite*, UcxTest); 122 * @param the test suite to destroy
62 void ucx_test_run(UcxTestSuite*, FILE*); 123 */
63 124 void ucx_test_suite_free(UcxTestSuite* suite);
64 #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *) 125
65 #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_) 126 /**
66 127 * Registers a test function with the specified test suite.
128 *
129 * @param suite the suite, the test function shall be added to
130 * @param test the test function to register
131 * @return <code>EXIT_SUCCESS</code> on success or
132 * <code>EXIT_FAILURE</code> on failure
133 */
134 int ucx_test_register(UcxTestSuite* suite, UcxTest test);
135 /**
136 * Runs a test suite and writes the test log to the specified stream.
137 * @param suite the test suite to run
138 * @param outstream the stream the log shall be written to
139 */
140 void ucx_test_run(UcxTestSuite* suite, FILE* outstream);
141
142 /**
143 * Macro for a #UcxTest function header.
144 *
145 * Use this macro to declare and/or define an #UcxTest function.
146 *
147 * @param name the name of the test function
148 */
149 #define UCX_TEST(name) void name(UcxTestSuite* _suite_,FILE *_output_)
150
151 /**
152 * Marks the begin of a test.
153 * <b>Note:</b> Any UCX_TEST_ASSERT() calls must be performed <b>after</b>
154 * #UCX_TEST_BEGIN.
155 *
156 * @see #UCX_TEST_END
157 */
67 #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\ 158 #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
68 fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ 159 fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
69 fwrite("... ", 1, 4, _output_);\ 160 fwrite("... ", 1, 4, _output_);\
70 jmp_buf _env_; \ 161 jmp_buf _env_; \
71 if (!setjmp(_env_)) { 162 if (!setjmp(_env_)) {
72 163
164 /**
165 * Checks a test assertion.
166 * If the assertion is correct, the test carries on. If the assertion is not
167 * correct, the specified message (terminated by a dot and a line break) is
168 * written to the test suites output stream.
169 * @param condition the condition to check
170 * @param message the message that shall be printed out on failure
171 */
73 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ 172 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
74 fwrite(message".\n", 1, 2+strlen(message), _output_); \ 173 fwrite(message".\n", 1, 2+strlen(message), _output_); \
75 _suite_->failure++; \ 174 _suite_->failure++; \
76 longjmp(_env_, 1);\ 175 longjmp(_env_, 1);\
77 } 176 }
78 177
79 #define UCX_TEST_SUBROUTINE(name,data) void name(UcxTestSuite* _suite_,\ 178 /**
80 FILE *_output_, jmp_buf _env_, void* data) 179 * Macro for a test subroutine function header.
81 #define UCX_TEST_CALL_SUBROUTINE(name,data) name(_suite_,_output_,_env_,data); 180 *
82 181 * Use this to declare and/or define an subroutine that can be called by using
182 * UCX_TEST_CALL_SUBROUTINE().
183 *
184 * @param name the name of the subroutine
185 * @param ... the parameter list
186 *
187 * @see UCX_TEST_CALL_SUBROUTINE()
188 */
189 #define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\
190 FILE *_output_, jmp_buf _env_, __VA_ARGS__)
191
192 /**
193 * Macro for calling a test subroutine.
194 *
195 * Subroutines declared with UCX_TEST_SUBROUTINE() can be called by using this
196 * macro.
197 *
198 * <b>Note:</b> You may <b>only</b> call subroutines within a #UCX_TEST_BEGIN-
199 * #UCX_TEST_END-block.
200 *
201 * @param name the name of the subroutine
202 * @param ... the argument list
203 *
204 * @see UCX_TEST_SUBROUTINE()
205 */
206 #define UCX_TEST_CALL_SUBROUTINE(name,...) \
207 name(_suite_,_output_,_env_,__VA_ARGS__);
208
209 /**
210 * Marks the end of a test.
211 * <b>Note:</b> Any UCX_TEST_ASSERT() calls must be performed <b>before</b>
212 * #UCX_TEST_END.
213 *
214 * @see #UCX_TEST_BEGIN
215 */
83 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;} 216 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
84 217
85 #ifdef __cplusplus 218 #ifdef __cplusplus
86 } 219 }
87 #endif 220 #endif
88 221
89 #endif /* TEST_H */ 222 #endif /* UCX_TEST_H */
90 223

mercurial