Fri, 09 Aug 2013 11:32:10 +0200
documented test.h and removed duplicated implement/declare macros for UCX_TEST
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@134 | 85 | * Alias for the __func__ preprocessor macro. |
universe@134 | 86 | * Some compilers use __func__ and others use __FUNC__. We use __FUNC__ so we |
universe@134 | 87 | * define it for those compilers which use __func__. |
universe@134 | 88 | */ |
universe@69 | 89 | #define __FUNCTION__ __func__ |
universe@69 | 90 | #endif |
universe@69 | 91 | |
universe@134 | 92 | /** Type for the internal list of test cases. */ |
universe@83 | 93 | typedef struct UcxTestList UcxTestList; |
universe@134 | 94 | /** Type for the UcxTestSuite. */ |
universe@83 | 95 | typedef struct UcxTestSuite UcxTestSuite; |
universe@134 | 96 | /** Pointer to a test function. */ |
universe@83 | 97 | typedef void(*UcxTest)(UcxTestSuite*,FILE*); |
universe@83 | 98 | |
universe@134 | 99 | /** |
universe@134 | 100 | * A test suite containing multiple test cases. |
universe@134 | 101 | */ |
universe@83 | 102 | struct UcxTestSuite { |
universe@134 | 103 | /** The number of successful tests after the suite has been run. */ |
universe@26 | 104 | unsigned int success; |
universe@134 | 105 | /** The number of failed tests after the suite has been run. */ |
universe@26 | 106 | unsigned int failure; |
universe@134 | 107 | /** |
universe@134 | 108 | * Internal list of test cases. |
universe@134 | 109 | * Use ucx_test_register() to add tests to this list. |
universe@134 | 110 | */ |
universe@83 | 111 | UcxTestList *tests; |
universe@83 | 112 | }; |
universe@26 | 113 | |
universe@134 | 114 | /** |
universe@134 | 115 | * Creates a new test suite. |
universe@134 | 116 | * @return a new test suite |
universe@134 | 117 | */ |
universe@26 | 118 | UcxTestSuite* ucx_test_suite_new(); |
universe@134 | 119 | /** |
universe@134 | 120 | * Destroys a test suite. |
universe@134 | 121 | * @param the test suite to destroy |
universe@134 | 122 | */ |
universe@134 | 123 | void ucx_test_suite_free(UcxTestSuite* suite); |
universe@26 | 124 | |
universe@134 | 125 | /** |
universe@134 | 126 | * Registers a test function with the specified test suite. |
universe@134 | 127 | * |
universe@134 | 128 | * @param suite the suite, the test function shall be added to |
universe@134 | 129 | * @param test the test function to register |
universe@134 | 130 | * @return EXIT_SUCCESS on success or EXIT_FAILURE on failure |
universe@134 | 131 | */ |
universe@134 | 132 | int ucx_test_register(UcxTestSuite* suite, UcxTest test); |
universe@134 | 133 | /** |
universe@134 | 134 | * Runs a test suite and writes the test log to the specified stream. |
universe@134 | 135 | * @param suite the test suite to run |
universe@134 | 136 | * @param outstream the stream the log shall be written to |
universe@134 | 137 | */ |
universe@134 | 138 | void ucx_test_run(UcxTestSuite* suite, FILE* outstream); |
universe@26 | 139 | |
universe@134 | 140 | /** |
universe@134 | 141 | * Macro for a #UcxTest function header. |
universe@134 | 142 | * |
universe@134 | 143 | * Use this macro to declare and/or define an #UcxTest function. |
universe@134 | 144 | * |
universe@134 | 145 | * @param name the name of the test function |
universe@134 | 146 | */ |
universe@134 | 147 | #define UCX_TEST(name) void name(UcxTestSuite* _suite_,FILE *_output_) |
universe@33 | 148 | |
universe@134 | 149 | /** |
universe@134 | 150 | * Marks the begin of a test. |
universe@134 | 151 | * <b>Note:</b> Any UCX_TEST_ASSERT() calls must be performed <b>after</b> |
universe@134 | 152 | * #UCX_TEST_BEGIN. |
universe@134 | 153 | * |
universe@134 | 154 | * @see #UCX_TEST_END |
universe@134 | 155 | */ |
universe@33 | 156 | #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\ |
universe@69 | 157 | fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ |
universe@33 | 158 | fwrite("... ", 1, 4, _output_);\ |
universe@33 | 159 | jmp_buf _env_; \ |
universe@33 | 160 | if (!setjmp(_env_)) { |
universe@26 | 161 | |
universe@134 | 162 | /** |
universe@134 | 163 | * Checks a test assertion. |
universe@134 | 164 | * If the assertion is correct, the test carries on. If the assertion is not |
universe@134 | 165 | * correct, the specified message (terminated by a dot and a line break) is |
universe@134 | 166 | * written to the test suites output stream. |
universe@134 | 167 | * @param condition the condition to check |
universe@134 | 168 | * @param message the message that shall be printed out on failure |
universe@134 | 169 | */ |
universe@26 | 170 | #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ |
universe@26 | 171 | fwrite(message".\n", 1, 2+strlen(message), _output_); \ |
universe@26 | 172 | _suite_->failure++; \ |
universe@33 | 173 | longjmp(_env_, 1);\ |
universe@26 | 174 | } |
universe@26 | 175 | |
universe@134 | 176 | /** |
universe@134 | 177 | * Macro for a test subroutine function header. |
universe@134 | 178 | * |
universe@134 | 179 | * Use this to declare and/or define an subroutine that can be called by using |
universe@134 | 180 | * UCX_TEST_CALL_SUBROUTINE(). |
universe@134 | 181 | * |
universe@134 | 182 | * @param name the name of the subroutine |
universe@134 | 183 | * @param ... the parameter list |
universe@134 | 184 | * |
universe@134 | 185 | * @see UCX_TEST_CALL_SUBROUTINE() |
universe@134 | 186 | */ |
universe@88 | 187 | #define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\ |
universe@88 | 188 | FILE *_output_, jmp_buf _env_, __VA_ARGS__) |
universe@134 | 189 | |
universe@134 | 190 | /** |
universe@134 | 191 | * Macro for calling a test subroutine. |
universe@134 | 192 | * |
universe@134 | 193 | * Subroutines declared with UCX_TEST_SUBROUTINE() can be called by using this |
universe@134 | 194 | * macro. |
universe@134 | 195 | * |
universe@134 | 196 | * <b>Note:</b> You may <b>only</b> call subroutines within a #UCX_TEST_BEGIN- |
universe@134 | 197 | * #UCX_TEST_END-block. |
universe@134 | 198 | * |
universe@134 | 199 | * @param name the name of the subroutine |
universe@134 | 200 | * @param ... the argument list |
universe@134 | 201 | * |
universe@134 | 202 | * @see UCX_TEST_SUBROUTINE() |
universe@134 | 203 | */ |
universe@88 | 204 | #define UCX_TEST_CALL_SUBROUTINE(name,...) \ |
universe@88 | 205 | name(_suite_,_output_,_env_,__VA_ARGS__); |
universe@33 | 206 | |
universe@134 | 207 | /** |
universe@134 | 208 | * Marks the end of a test. |
universe@134 | 209 | * <b>Note:</b> Any UCX_TEST_ASSERT() calls must be performed <b>before</b> |
universe@134 | 210 | * #UCX_TEST_END. |
universe@134 | 211 | * |
universe@134 | 212 | * @see #UCX_TEST_BEGIN |
universe@134 | 213 | */ |
universe@33 | 214 | #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;} |
universe@26 | 215 | |
universe@26 | 216 | #ifdef __cplusplus |
universe@26 | 217 | } |
universe@26 | 218 | #endif |
universe@26 | 219 | |
olaf@120 | 220 | #endif /* UCX_TEST_H */ |
universe@26 | 221 |