UNIXworkcode

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