Fri, 09 Aug 2013 14:36:54 +0200
documented mempool + some fixes
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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 */
29 /**
30 * @file: test.h
31 *
32 * UCX Test Framework.
33 *
34 * Usage of this test framework:
35 *
36 * **** IN HEADER FILE: ****
37 *
38 * <pre>
39 * UCX_TEST(function_name)
40 * UCX_TEST_SUBROUTINE(subroutine_name, paramlist) // optional
41 * </pre>
42 *
43 * **** IN SOURCE FILE: ****
44 * <pre>
45 * UCX_TEST_SUBROUTINE(subroutine_name, paramlist) {
46 * // tests with UCX_TEST_ASSERT()
47 * }
48 *
49 * UCX_TEST(function_name) {
50 * // memory allocation and other stuff here
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
68 *
69 */
71 #ifndef UCX_TEST_H
72 #define UCX_TEST_H
74 #include "ucx.h"
75 #include <stdio.h>
76 #include <string.h>
77 #include <setjmp.h>
79 #ifdef __cplusplus
80 extern "C" {
81 #endif
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 */
90 #define __FUNCTION__ __func__
91 #endif
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*);
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. */
105 unsigned int success;
106 /** The number of failed tests after the suite has been run. */
107 unsigned int failure;
108 /**
109 * Internal list of test cases.
110 * Use ucx_test_register() to add tests to this list.
111 */
112 UcxTestList *tests;
113 };
115 /**
116 * Creates a new test suite.
117 * @return a new test suite
118 */
119 UcxTestSuite* ucx_test_suite_new();
120 /**
121 * Destroys a test suite.
122 * @param the test suite to destroy
123 */
124 void ucx_test_suite_free(UcxTestSuite* suite);
126 /**
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);
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_)
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 */
158 #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
159 fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
160 fwrite("... ", 1, 4, _output_);\
161 jmp_buf _env_; \
162 if (!setjmp(_env_)) {
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 */
172 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
173 fwrite(message".\n", 1, 2+strlen(message), _output_); \
174 _suite_->failure++; \
175 longjmp(_env_, 1);\
176 }
178 /**
179 * Macro for a test subroutine function header.
180 *
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__)
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__);
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 */
216 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
218 #ifdef __cplusplus
219 }
220 #endif
222 #endif /* UCX_TEST_H */