src/ucx/test.h

Sat, 28 Oct 2017 15:43:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 28 Oct 2017 15:43:51 +0200
changeset 259
2f5dea574a75
parent 251
fae240d633fc
child 297
ba760f2195c3
permissions
-rw-r--r--

modules documentation

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

mercurial