src/cx/test.h

Wed, 20 Dec 2023 16:46:14 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 20 Dec 2023 16:46:14 +0100
changeset 766
e59b76889f00
child 767
d31f4d4075dc
permissions
-rw-r--r--

bring back UCX test - fixes #341

universe@766 1 /*
universe@766 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@766 3 *
universe@766 4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
universe@766 5 *
universe@766 6 * Redistribution and use in source and binary forms, with or without
universe@766 7 * modification, are permitted provided that the following conditions are met:
universe@766 8 *
universe@766 9 * 1. Redistributions of source code must retain the above copyright
universe@766 10 * notice, this list of conditions and the following disclaimer.
universe@766 11 *
universe@766 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@766 13 * notice, this list of conditions and the following disclaimer in the
universe@766 14 * documentation and/or other materials provided with the distribution.
universe@766 15 *
universe@766 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@766 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@766 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@766 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@766 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@766 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@766 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@766 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@766 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@766 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@766 26 * POSSIBILITY OF SUCH DAMAGE.
universe@766 27 */
universe@766 28
universe@766 29 /**
universe@766 30 * @file: test.h
universe@766 31 *
universe@766 32 * UCX Test Framework.
universe@766 33 *
universe@766 34 * Usage of this test framework:
universe@766 35 *
universe@766 36 * **** IN HEADER FILE: ****
universe@766 37 *
universe@766 38 * <pre>
universe@766 39 * CX_TEST(function_name);
universe@766 40 * CX_TEST_SUBROUTINE(subroutine_name, paramlist); // optional
universe@766 41 * </pre>
universe@766 42 *
universe@766 43 * **** IN SOURCE FILE: ****
universe@766 44 * <pre>
universe@766 45 * CX_TEST_SUBROUTINE(subroutine_name, paramlist) {
universe@766 46 * // tests with CX_TEST_ASSERT()
universe@766 47 * }
universe@766 48 *
universe@766 49 * CX_TEST(function_name) {
universe@766 50 * // memory allocation and other stuff here
universe@766 51 * #CX_TEST_BEGIN
universe@766 52 * // tests with CX_TEST_ASSERT() and/or
universe@766 53 * // calls with CX_TEST_CALL_SUBROUTINE() here
universe@766 54 * #CX_TEST_END
universe@766 55 * // cleanup of memory here
universe@766 56 * }
universe@766 57 * </pre>
universe@766 58 *
universe@766 59 * @remark if a test fails, execution continues at the
universe@766 60 * #CX_TEST_END macro! So make sure every necessary cleanup happens afterwards.
universe@766 61 *
universe@766 62 * @attention Do not call own functions within a test, that use
universe@766 63 * CX_TEST_ASSERT() macros and are not defined by using CX_TEST_SUBROUTINE().
universe@766 64 *
universe@766 65 * @author Mike Becker
universe@766 66 * @author Olaf Wintermann
universe@766 67 *
universe@766 68 */
universe@766 69
universe@766 70 #ifndef UCX_TEST_H
universe@766 71 #define UCX_TEST_H
universe@766 72
universe@766 73 #include <stdlib.h>
universe@766 74 #include <stdio.h>
universe@766 75 #include <string.h>
universe@766 76 #include <setjmp.h>
universe@766 77
universe@766 78 #ifdef __cplusplus
universe@766 79 extern "C" {
universe@766 80 #endif
universe@766 81
universe@766 82 #ifndef __FUNCTION__
universe@766 83 /**
universe@766 84 * Alias for the <code>__func__</code> preprocessor macro.
universe@766 85 * Some compilers use <code>__func__</code> and others use __FUNCTION__.
universe@766 86 * We use __FUNCTION__ so we define it for those compilers which use
universe@766 87 * <code>__func__</code>.
universe@766 88 */
universe@766 89 #define __FUNCTION__ __func__
universe@766 90 #endif
universe@766 91
universe@766 92 #ifndef UCX_COMMON_H
universe@766 93 /**
universe@766 94 * Function pointer compatible with fwrite-like functions.
universe@766 95 */
universe@766 96 typedef size_t (*cx_write_func)(
universe@766 97 void const *,
universe@766 98 size_t,
universe@766 99 size_t,
universe@766 100 void *
universe@766 101 );
universe@766 102 #endif // UCX_COMMON_H
universe@766 103
universe@766 104 /** Type for the CxTestSuite. */
universe@766 105 typedef struct CxTestSuite CxTestSuite;
universe@766 106
universe@766 107 /** Pointer to a test function. */
universe@766 108 typedef void(*CxTest)(CxTestSuite *, void *, cx_write_func);
universe@766 109
universe@766 110 /** Type for the internal list of test cases. */
universe@766 111 typedef struct CxTestSet CxTestSet;
universe@766 112
universe@766 113 /** Structure for the internal list of test cases. */
universe@766 114 struct CxTestSet {
universe@766 115
universe@766 116 /** Test case. */
universe@766 117 CxTest test;
universe@766 118
universe@766 119 /** Pointer to the next list element. */
universe@766 120 CxTestSet *next;
universe@766 121 };
universe@766 122
universe@766 123 /**
universe@766 124 * A test suite containing multiple test cases.
universe@766 125 */
universe@766 126 struct CxTestSuite {
universe@766 127
universe@766 128 /** The number of successful tests after the suite has been run. */
universe@766 129 unsigned int success;
universe@766 130
universe@766 131 /** The number of failed tests after the suite has been run. */
universe@766 132 unsigned int failure;
universe@766 133
universe@766 134 /** The optional name of this test suite. */
universe@766 135 char const *name;
universe@766 136
universe@766 137 /**
universe@766 138 * Internal list of test cases.
universe@766 139 * Use cx_test_register() to add tests to this list.
universe@766 140 */
universe@766 141 CxTestSet *tests;
universe@766 142 };
universe@766 143
universe@766 144 /**
universe@766 145 * Creates a new test suite.
universe@766 146 * @param name optional name of the suite
universe@766 147 * @return a new test suite
universe@766 148 */
universe@766 149 static inline CxTestSuite* cx_test_suite_new(char const *name) {
universe@766 150 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite));
universe@766 151 if (suite != NULL) {
universe@766 152 suite->name = name;
universe@766 153 suite->success = 0;
universe@766 154 suite->failure = 0;
universe@766 155 suite->tests = NULL;
universe@766 156 }
universe@766 157
universe@766 158 return suite;
universe@766 159 }
universe@766 160
universe@766 161 /**
universe@766 162 * Destroys a test suite.
universe@766 163 * @param suite the test suite to destroy
universe@766 164 */
universe@766 165 static inline void cx_test_suite_free(CxTestSuite* suite) {
universe@766 166 CxTestSet *l = suite->tests;
universe@766 167 while (l != NULL) {
universe@766 168 CxTestSet *e = l;
universe@766 169 l = l->next;
universe@766 170 free(e);
universe@766 171 }
universe@766 172 free(suite);
universe@766 173 }
universe@766 174
universe@766 175 /**
universe@766 176 * Registers a test function with the specified test suite.
universe@766 177 *
universe@766 178 * @param suite the suite, the test function shall be added to
universe@766 179 * @param test the test function to register
universe@766 180 * @return zero on success or non-zero on failure
universe@766 181 */
universe@766 182 static inline int cx_test_register(CxTestSuite* suite, CxTest test) {
universe@766 183 CxTestSet *t = (CxTestSet*) malloc(sizeof(CxTestSet));
universe@766 184 if (t) {
universe@766 185 t->test = test;
universe@766 186 t->next = NULL;
universe@766 187 if (suite->tests == NULL) {
universe@766 188 suite->tests = t;
universe@766 189 } else {
universe@766 190 CxTestSet *last = suite->tests;
universe@766 191 while (last->next) {
universe@766 192 last = last->next;
universe@766 193 }
universe@766 194 last->next = t;
universe@766 195 }
universe@766 196 return 0;
universe@766 197 } else {
universe@766 198 return 1;
universe@766 199 }
universe@766 200 }
universe@766 201
universe@766 202 /**
universe@766 203 * Runs a test suite and writes the test log to the specified stream.
universe@766 204 * @param suite the test suite to run
universe@766 205 * @param out_target the target buffer or file to write the output to
universe@766 206 * @param out_writer the write function writing to \p out_target
universe@766 207 */
universe@766 208 static inline void cx_test_run(CxTestSuite *suite,
universe@766 209 void *out_target, cx_write_func out_writer) {
universe@766 210 if (suite->name == NULL) {
universe@766 211 out_writer("*** Test Suite ***\n", 1, 19, out_target);
universe@766 212 } else {
universe@766 213 out_writer("*** Test Suite : ", 1, 17, out_target);
universe@766 214 out_writer(suite->name, 1, strlen(suite->name), out_target);
universe@766 215 out_writer(" ***\n", 1, 5, out_target);
universe@766 216 }
universe@766 217 suite->success = 0;
universe@766 218 suite->failure = 0;
universe@766 219 for (CxTestSet *elem = suite->tests; elem; elem = elem->next) {
universe@766 220 elem->test(suite, out_target, out_writer);
universe@766 221 }
universe@766 222 out_writer("\nAll test completed.\n", 1, 21, out_target);
universe@766 223 char total[80];
universe@766 224 int len = snprintf(
universe@766 225 total, 80,
universe@766 226 " Total: %u\n Success: %u\n Failure: %u\n",
universe@766 227 suite->success + suite->failure, suite->success, suite->failure
universe@766 228 );
universe@766 229 out_writer(total, 1, len, out_target);
universe@766 230 }
universe@766 231
universe@766 232 /**
universe@766 233 * Runs a test suite and writes the test log to the specified FILE stream.
universe@766 234 * @param suite the test suite to run
universe@766 235 * @param file the target file to write the output to
universe@766 236 */
universe@766 237 #define cx_test_run_f(suite, file) cx_test_run(suite, (void*)file, (cx_write_func)fwrite)
universe@766 238
universe@766 239 /**
universe@766 240 * Runs a test suite and writes the test log to stdout.
universe@766 241 * @param suite the test suite to run
universe@766 242 */
universe@766 243 #define cx_test_run_stdout(suite) cx_test_run_f(suite, stdout)
universe@766 244
universe@766 245 /**
universe@766 246 * Macro for a #CxTest function header.
universe@766 247 *
universe@766 248 * Use this macro to declare and/or define a #CxTest function.
universe@766 249 *
universe@766 250 * @param name the name of the test function
universe@766 251 */
universe@766 252 #define CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, cx_write_func _writefnc_)
universe@766 253
universe@766 254 /**
universe@766 255 * Marks the begin of a test.
universe@766 256 * <b>Note:</b> Any CX_TEST_ASSERT() calls must be performed <b>after</b>
universe@766 257 * #CX_TEST_BEGIN.
universe@766 258 *
universe@766 259 * @see #CX_TEST_END
universe@766 260 */
universe@766 261 #define CX_TEST_BEGIN _writefnc_("Running ", 1, 8, _output_);\
universe@766 262 _writefnc_(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
universe@766 263 _writefnc_("... ", 1, 4, _output_);\
universe@766 264 jmp_buf _env_; \
universe@766 265 if (!setjmp(_env_)) {
universe@766 266
universe@766 267 /**
universe@766 268 * Checks a test assertion.
universe@766 269 * If the assertion is correct, the test carries on. If the assertion is not
universe@766 270 * correct, the specified message (terminated by a dot and a line break) is
universe@766 271 * written to the test suites output stream.
universe@766 272 * @param condition the condition to check
universe@766 273 * @param message the message that shall be printed out on failure
universe@766 274 */
universe@766 275 #define CX_TEST_ASSERT(condition,message) if (!(condition)) { \
universe@766 276 _writefnc_(message".\n", 1, 2+strlen(message), _output_); \
universe@766 277 _suite_->failure++; \
universe@766 278 longjmp(_env_, 1);\
universe@766 279 }
universe@766 280
universe@766 281 /**
universe@766 282 * Macro for a test subroutine function header.
universe@766 283 *
universe@766 284 * Use this to declare and/or define a subroutine that can be called by using
universe@766 285 * CX_TEST_CALL_SUBROUTINE().
universe@766 286 *
universe@766 287 * @param name the name of the subroutine
universe@766 288 * @param ... the parameter list
universe@766 289 *
universe@766 290 * @see CX_TEST_CALL_SUBROUTINE()
universe@766 291 */
universe@766 292 #define CX_TEST_SUBROUTINE(name,...) void name(CxTestSuite* _suite_,\
universe@766 293 void *_output_, jmp_buf _env_, __VA_ARGS__)
universe@766 294
universe@766 295 /**
universe@766 296 * Macro for calling a test subroutine.
universe@766 297 *
universe@766 298 * Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this
universe@766 299 * macro.
universe@766 300 *
universe@766 301 * <b>Note:</b> You may <b>only</b> call subroutines within a #CX_TEST_BEGIN-
universe@766 302 * #CX_TEST_END-block.
universe@766 303 *
universe@766 304 * @param name the name of the subroutine
universe@766 305 * @param ... the argument list
universe@766 306 *
universe@766 307 * @see CX_TEST_SUBROUTINE()
universe@766 308 */
universe@766 309 #define CX_TEST_CALL_SUBROUTINE(name,...) \
universe@766 310 name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__);
universe@766 311
universe@766 312 /**
universe@766 313 * Marks the end of a test.
universe@766 314 * <b>Note:</b> Any CX_TEST_ASSERT() calls must be performed <b>before</b>
universe@766 315 * #CX_TEST_END.
universe@766 316 *
universe@766 317 * @see #CX_TEST_BEGIN
universe@766 318 */
universe@766 319 #define CX_TEST_END _writefnc_("success.\n", 1, 9, _output_); _suite_->success++;}
universe@766 320
universe@766 321 #ifdef __cplusplus
universe@766 322 }
universe@766 323 #endif
universe@766 324
universe@766 325 #endif /* UCX_TEST_H */
universe@766 326

mercurial