src/cx/test.h

Wed, 20 Dec 2023 17:57:18 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 20 Dec 2023 17:57:18 +0100
changeset 767
d31f4d4075dc
parent 766
e59b76889f00
child 768
0e1cf2cd500e
permissions
-rw-r--r--

migrate utils tests - relates to #342

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

mercurial