ucx/test.h

Fri, 09 Aug 2013 14:36:54 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 09 Aug 2013 14:36:54 +0200
changeset 135
a0aa1c15f46b
parent 134
4d320dc3a7af
child 138
7800811078b8
permissions
-rw-r--r--

documented mempool + some fixes

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

mercurial