src/cx/test.h

Wed, 17 Jan 2024 21:01:50 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 17 Jan 2024 21:01:50 +0100
changeset 814
5f9e07d3dd6c
parent 784
ba5faf85dec6
permissions
-rw-r--r--

fix using warning options with compilers which do not support them - fixes #363

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@767 51 * #CX_TEST_DO {
universe@767 52 * // tests with CX_TEST_ASSERT() and/or
universe@767 53 * // calls with CX_TEST_CALL_SUBROUTINE() here
universe@767 54 * }
universe@766 55 * // cleanup of memory here
universe@766 56 * }
universe@766 57 * </pre>
universe@766 58 *
universe@766 59 * @attention Do not call own functions within a test, that use
universe@766 60 * CX_TEST_ASSERT() macros and are not defined by using CX_TEST_SUBROUTINE().
universe@766 61 *
universe@766 62 * @author Mike Becker
universe@766 63 * @author Olaf Wintermann
universe@766 64 *
universe@766 65 */
universe@766 66
universe@766 67 #ifndef UCX_TEST_H
universe@766 68 #define UCX_TEST_H
universe@766 69
universe@766 70 #include <stdlib.h>
universe@766 71 #include <stdio.h>
universe@766 72 #include <string.h>
universe@766 73 #include <setjmp.h>
universe@766 74
universe@766 75 #ifdef __cplusplus
universe@766 76 extern "C" {
universe@766 77 #endif
universe@766 78
universe@766 79 #ifndef __FUNCTION__
universe@766 80 /**
universe@766 81 * Alias for the <code>__func__</code> preprocessor macro.
universe@766 82 * Some compilers use <code>__func__</code> and others use __FUNCTION__.
universe@766 83 * We use __FUNCTION__ so we define it for those compilers which use
universe@766 84 * <code>__func__</code>.
universe@766 85 */
universe@766 86 #define __FUNCTION__ __func__
universe@766 87 #endif
universe@766 88
universe@814 89 //
universe@814 90 #if !defined(__clang__) && __GNUC__ > 3
universe@814 91 #pragma GCC diagnostic ignored "-Wclobbered"
universe@814 92 #endif
universe@814 93
universe@766 94 #ifndef UCX_COMMON_H
universe@766 95 /**
universe@766 96 * Function pointer compatible with fwrite-like functions.
universe@766 97 */
universe@766 98 typedef size_t (*cx_write_func)(
universe@766 99 void const *,
universe@766 100 size_t,
universe@766 101 size_t,
universe@766 102 void *
universe@766 103 );
universe@766 104 #endif // UCX_COMMON_H
universe@766 105
universe@766 106 /** Type for the CxTestSuite. */
universe@766 107 typedef struct CxTestSuite CxTestSuite;
universe@766 108
universe@766 109 /** Pointer to a test function. */
universe@766 110 typedef void(*CxTest)(CxTestSuite *, void *, cx_write_func);
universe@766 111
universe@766 112 /** Type for the internal list of test cases. */
universe@766 113 typedef struct CxTestSet CxTestSet;
universe@766 114
universe@766 115 /** Structure for the internal list of test cases. */
universe@766 116 struct CxTestSet {
universe@766 117
universe@766 118 /** Test case. */
universe@766 119 CxTest test;
universe@766 120
universe@766 121 /** Pointer to the next list element. */
universe@766 122 CxTestSet *next;
universe@766 123 };
universe@766 124
universe@766 125 /**
universe@766 126 * A test suite containing multiple test cases.
universe@766 127 */
universe@766 128 struct CxTestSuite {
universe@766 129
universe@766 130 /** The number of successful tests after the suite has been run. */
universe@766 131 unsigned int success;
universe@766 132
universe@766 133 /** The number of failed tests after the suite has been run. */
universe@766 134 unsigned int failure;
universe@766 135
universe@766 136 /** The optional name of this test suite. */
universe@766 137 char const *name;
universe@766 138
universe@766 139 /**
universe@766 140 * Internal list of test cases.
universe@766 141 * Use cx_test_register() to add tests to this list.
universe@766 142 */
universe@766 143 CxTestSet *tests;
universe@766 144 };
universe@766 145
universe@766 146 /**
universe@766 147 * Creates a new test suite.
universe@766 148 * @param name optional name of the suite
universe@766 149 * @return a new test suite
universe@766 150 */
universe@766 151 static inline CxTestSuite* cx_test_suite_new(char const *name) {
universe@766 152 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite));
universe@766 153 if (suite != NULL) {
universe@766 154 suite->name = name;
universe@766 155 suite->success = 0;
universe@766 156 suite->failure = 0;
universe@766 157 suite->tests = NULL;
universe@766 158 }
universe@766 159
universe@766 160 return suite;
universe@766 161 }
universe@766 162
universe@766 163 /**
universe@766 164 * Destroys a test suite.
universe@766 165 * @param suite the test suite to destroy
universe@766 166 */
universe@766 167 static inline void cx_test_suite_free(CxTestSuite* suite) {
universe@766 168 CxTestSet *l = suite->tests;
universe@766 169 while (l != NULL) {
universe@766 170 CxTestSet *e = l;
universe@766 171 l = l->next;
universe@766 172 free(e);
universe@766 173 }
universe@766 174 free(suite);
universe@766 175 }
universe@766 176
universe@766 177 /**
universe@766 178 * Registers a test function with the specified test suite.
universe@766 179 *
universe@766 180 * @param suite the suite, the test function shall be added to
universe@766 181 * @param test the test function to register
universe@766 182 * @return zero on success or non-zero on failure
universe@766 183 */
universe@766 184 static inline int cx_test_register(CxTestSuite* suite, CxTest test) {
universe@766 185 CxTestSet *t = (CxTestSet*) malloc(sizeof(CxTestSet));
universe@766 186 if (t) {
universe@766 187 t->test = test;
universe@766 188 t->next = NULL;
universe@766 189 if (suite->tests == NULL) {
universe@766 190 suite->tests = t;
universe@766 191 } else {
universe@766 192 CxTestSet *last = suite->tests;
universe@766 193 while (last->next) {
universe@766 194 last = last->next;
universe@766 195 }
universe@766 196 last->next = t;
universe@766 197 }
universe@766 198 return 0;
universe@766 199 } else {
universe@766 200 return 1;
universe@766 201 }
universe@766 202 }
universe@766 203
universe@766 204 /**
universe@766 205 * Runs a test suite and writes the test log to the specified stream.
universe@766 206 * @param suite the test suite to run
universe@766 207 * @param out_target the target buffer or file to write the output to
universe@766 208 * @param out_writer the write function writing to \p out_target
universe@766 209 */
universe@766 210 static inline void cx_test_run(CxTestSuite *suite,
universe@766 211 void *out_target, cx_write_func out_writer) {
universe@766 212 if (suite->name == NULL) {
universe@766 213 out_writer("*** Test Suite ***\n", 1, 19, out_target);
universe@766 214 } else {
universe@766 215 out_writer("*** Test Suite : ", 1, 17, out_target);
universe@766 216 out_writer(suite->name, 1, strlen(suite->name), out_target);
universe@766 217 out_writer(" ***\n", 1, 5, out_target);
universe@766 218 }
universe@766 219 suite->success = 0;
universe@766 220 suite->failure = 0;
universe@766 221 for (CxTestSet *elem = suite->tests; elem; elem = elem->next) {
universe@766 222 elem->test(suite, out_target, out_writer);
universe@766 223 }
universe@766 224 out_writer("\nAll test completed.\n", 1, 21, out_target);
universe@766 225 char total[80];
universe@766 226 int len = snprintf(
universe@766 227 total, 80,
universe@768 228 " Total: %u\n Success: %u\n Failure: %u\n\n",
universe@766 229 suite->success + suite->failure, suite->success, suite->failure
universe@766 230 );
universe@766 231 out_writer(total, 1, len, out_target);
universe@766 232 }
universe@766 233
universe@766 234 /**
universe@766 235 * Runs a test suite and writes the test log to the specified FILE stream.
universe@766 236 * @param suite the test suite to run
universe@766 237 * @param file the target file to write the output to
universe@766 238 */
universe@766 239 #define cx_test_run_f(suite, file) cx_test_run(suite, (void*)file, (cx_write_func)fwrite)
universe@766 240
universe@766 241 /**
universe@766 242 * Runs a test suite and writes the test log to stdout.
universe@766 243 * @param suite the test suite to run
universe@766 244 */
universe@766 245 #define cx_test_run_stdout(suite) cx_test_run_f(suite, stdout)
universe@766 246
universe@766 247 /**
universe@766 248 * Macro for a #CxTest function header.
universe@766 249 *
universe@766 250 * Use this macro to declare and/or define a #CxTest function.
universe@766 251 *
universe@766 252 * @param name the name of the test function
universe@766 253 */
universe@766 254 #define CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, cx_write_func _writefnc_)
universe@766 255
universe@766 256 /**
universe@767 257 * Defines the scope of a test.
universe@767 258 * @attention Any CX_TEST_ASSERT() calls must be performed in scope of
universe@767 259 * #CX_TEST_DO.
universe@766 260 */
universe@767 261 #define CX_TEST_DO _writefnc_("Running ", 1, 8, _output_);\
universe@766 262 _writefnc_(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
universe@766 263 _writefnc_("... ", 1, 4, _output_);\
universe@767 264 jmp_buf _env_;\
universe@767 265 for (unsigned int _cx_test_loop_ = 0 ;\
universe@767 266 _cx_test_loop_ == 0 && !setjmp(_env_);\
universe@767 267 _writefnc_("success.\n", 1, 9, _output_),\
universe@767 268 _suite_->success++, _cx_test_loop_++)
universe@766 269
universe@766 270 /**
universe@766 271 * Checks a test assertion.
universe@766 272 * If the assertion is correct, the test carries on. If the assertion is not
universe@766 273 * correct, the specified message (terminated by a dot and a line break) is
universe@766 274 * written to the test suites output stream.
universe@766 275 * @param condition the condition to check
universe@766 276 * @param message the message that shall be printed out on failure
universe@766 277 */
universe@767 278 #define CX_TEST_ASSERTM(condition,message) if (!(condition)) { \
universe@767 279 char const* _assert_msg_ = message; \
universe@767 280 _writefnc_(_assert_msg_, 1, strlen(_assert_msg_), _output_); \
universe@767 281 _writefnc_(".\n", 1, 2, _output_); \
universe@766 282 _suite_->failure++; \
universe@766 283 longjmp(_env_, 1);\
universe@767 284 } (void) 0
universe@767 285
universe@767 286 /**
universe@767 287 * Checks a test assertion.
universe@767 288 * If the assertion is correct, the test carries on. If the assertion is not
universe@767 289 * correct, the specified message (terminated by a dot and a line break) is
universe@767 290 * written to the test suites output stream.
universe@767 291 * @param condition the condition to check
universe@767 292 */
universe@767 293 #define CX_TEST_ASSERT(condition) CX_TEST_ASSERTM(condition, #condition " failed")
universe@766 294
universe@766 295 /**
universe@766 296 * Macro for a test subroutine function header.
universe@766 297 *
universe@766 298 * Use this to declare and/or define a subroutine that can be called by using
universe@766 299 * CX_TEST_CALL_SUBROUTINE().
universe@766 300 *
universe@766 301 * @param name the name of the subroutine
universe@766 302 * @param ... the parameter list
universe@766 303 *
universe@766 304 * @see CX_TEST_CALL_SUBROUTINE()
universe@766 305 */
universe@766 306 #define CX_TEST_SUBROUTINE(name,...) void name(CxTestSuite* _suite_,\
universe@784 307 void *_output_, cx_write_func _writefnc_, jmp_buf _env_, __VA_ARGS__)
universe@766 308
universe@766 309 /**
universe@766 310 * Macro for calling a test subroutine.
universe@766 311 *
universe@766 312 * Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this
universe@766 313 * macro.
universe@766 314 *
universe@767 315 * @remark You may <b>only</b> call subroutines within a #CX_TEST_DO block.
universe@766 316 *
universe@766 317 * @param name the name of the subroutine
universe@766 318 * @param ... the argument list
universe@766 319 *
universe@766 320 * @see CX_TEST_SUBROUTINE()
universe@766 321 */
universe@766 322 #define CX_TEST_CALL_SUBROUTINE(name,...) \
universe@784 323 name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__)
universe@766 324
universe@766 325 #ifdef __cplusplus
universe@766 326 }
universe@766 327 #endif
universe@766 328
universe@766 329 #endif /* UCX_TEST_H */
universe@766 330

mercurial