| 1 | /* Copyright 2011-2023 Bas van den Berg |
| 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #ifndef CTEST_H |
| 17 | #define CTEST_H |
| 18 | |
| 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
| 23 | #ifdef __GNUC__ |
| 24 | #define CTEST_IMPL_FORMAT_PRINTF(a, b) __attribute__ ((format(printf, a, b))) |
| 25 | #else |
| 26 | #define CTEST_IMPL_FORMAT_PRINTF(a, b) |
| 27 | #endif |
| 28 | |
| 29 | #include <inttypes.h> /* intmax_t, uintmax_t, PRI* */ |
| 30 | #include <stdbool.h> /* bool, true, false */ |
| 31 | #include <stddef.h> /* size_t */ |
| 32 | |
| 33 | typedef void (*ctest_nullary_run_func)(void); |
| 34 | typedef void (*ctest_unary_run_func)(void*); |
| 35 | typedef void (*ctest_setup_func)(void*); |
| 36 | typedef void (*ctest_teardown_func)(void*); |
| 37 | |
| 38 | union ctest_run_func_union { |
| 39 | ctest_nullary_run_func nullary; |
| 40 | ctest_unary_run_func unary; |
| 41 | }; |
| 42 | |
| 43 | #define CTEST_IMPL_PRAGMA(x) _Pragma (#x) |
| 44 | |
| 45 | #if defined(__GNUC__) |
| 46 | #if defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 47 | /* the GCC argument will work for both gcc and clang */ |
| 48 | #define CTEST_IMPL_DIAG_PUSH_IGNORED(w) \ |
| 49 | CTEST_IMPL_PRAGMA(GCC diagnostic push) \ |
| 50 | CTEST_IMPL_PRAGMA(GCC diagnostic ignored "-W" #w) |
| 51 | #define CTEST_IMPL_DIAG_POP() \ |
| 52 | CTEST_IMPL_PRAGMA(GCC diagnostic pop) |
| 53 | #else |
| 54 | /* the push/pop functionality wasn't in gcc until 4.6, fallback to "ignored" */ |
| 55 | #define CTEST_IMPL_DIAG_PUSH_IGNORED(w) \ |
| 56 | CTEST_IMPL_PRAGMA(GCC diagnostic ignored "-W" #w) |
| 57 | #define CTEST_IMPL_DIAG_POP() |
| 58 | #endif |
| 59 | #else |
| 60 | /* leave them out entirely for non-GNUC compilers */ |
| 61 | #define CTEST_IMPL_DIAG_PUSH_IGNORED(w) |
| 62 | #define CTEST_IMPL_DIAG_POP() |
| 63 | #endif |
| 64 | |
| 65 | struct ctest { |
| 66 | const char* ssname; // suite name |
| 67 | const char* ttname; // test name |
| 68 | union ctest_run_func_union run; |
| 69 | |
| 70 | void* data; |
| 71 | ctest_setup_func* setup; |
| 72 | ctest_teardown_func* teardown; |
| 73 | |
| 74 | int skip; |
| 75 | |
| 76 | unsigned int magic; |
| 77 | }; |
| 78 | |
| 79 | #define CTEST_IMPL_NAME(name) ctest_##name |
| 80 | #define CTEST_IMPL_FNAME(sname, tname) CTEST_IMPL_NAME(sname##_##tname##_run) |
| 81 | #define CTEST_IMPL_TNAME(sname, tname) CTEST_IMPL_NAME(sname##_##tname) |
| 82 | #define CTEST_IMPL_DATA_SNAME(sname) CTEST_IMPL_NAME(sname##_data) |
| 83 | #define CTEST_IMPL_DATA_TNAME(sname, tname) CTEST_IMPL_NAME(sname##_##tname##_data) |
| 84 | #define CTEST_IMPL_SETUP_FNAME(sname) CTEST_IMPL_NAME(sname##_setup) |
| 85 | #define CTEST_IMPL_SETUP_FPNAME(sname) CTEST_IMPL_NAME(sname##_setup_ptr) |
| 86 | #define CTEST_IMPL_SETUP_TPNAME(sname, tname) CTEST_IMPL_NAME(sname##_##tname##_setup_ptr) |
| 87 | #define CTEST_IMPL_TEARDOWN_FNAME(sname) CTEST_IMPL_NAME(sname##_teardown) |
| 88 | #define CTEST_IMPL_TEARDOWN_FPNAME(sname) CTEST_IMPL_NAME(sname##_teardown_ptr) |
| 89 | #define CTEST_IMPL_TEARDOWN_TPNAME(sname, tname) CTEST_IMPL_NAME(sname##_##tname##_teardown_ptr) |
| 90 | |
| 91 | #define CTEST_IMPL_MAGIC (0xdeadbeef) |
| 92 | #ifdef __APPLE__ |
| 93 | #define CTEST_IMPL_SECTION __attribute__ ((used, section ("__DATA, .ctest"), aligned(1))) |
| 94 | #else |
| 95 | #define CTEST_IMPL_SECTION __attribute__ ((used, section (".ctest"), aligned(1))) |
| 96 | #endif |
| 97 | |
| 98 | #define CTEST_IMPL_STRUCT(sname, tname, tskip, tdata, tsetup, tteardown) \ |
| 99 | static struct ctest CTEST_IMPL_TNAME(sname, tname) CTEST_IMPL_SECTION = { \ |
| 100 | #sname, \ |
| 101 | #tname, \ |
| 102 | { (ctest_nullary_run_func) CTEST_IMPL_FNAME(sname, tname) }, \ |
| 103 | tdata, \ |
| 104 | (ctest_setup_func*) tsetup, \ |
| 105 | (ctest_teardown_func*) tteardown, \ |
| 106 | tskip, \ |
| 107 | CTEST_IMPL_MAGIC, \ |
| 108 | } |
| 109 | |
| 110 | #ifdef __cplusplus |
| 111 | |
| 112 | #define CTEST_SETUP(sname) \ |
| 113 | template <> void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* data) |
| 114 | |
| 115 | #define CTEST_TEARDOWN(sname) \ |
| 116 | template <> void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* data) |
| 117 | |
| 118 | #define CTEST_DATA(sname) \ |
| 119 | template <typename T> void CTEST_IMPL_SETUP_FNAME(sname)(T* data) { } \ |
| 120 | template <typename T> void CTEST_IMPL_TEARDOWN_FNAME(sname)(T* data) { } \ |
| 121 | struct CTEST_IMPL_DATA_SNAME(sname) |
| 122 | |
| 123 | #define CTEST_IMPL_CTEST(sname, tname, tskip) \ |
| 124 | static void CTEST_IMPL_FNAME(sname, tname)(void); \ |
| 125 | CTEST_IMPL_STRUCT(sname, tname, tskip, NULL, NULL, NULL); \ |
| 126 | static void CTEST_IMPL_FNAME(sname, tname)(void) |
| 127 | |
| 128 | #define CTEST_IMPL_CTEST2(sname, tname, tskip) \ |
| 129 | static struct CTEST_IMPL_DATA_SNAME(sname) CTEST_IMPL_DATA_TNAME(sname, tname); \ |
| 130 | static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* data); \ |
| 131 | static void (*CTEST_IMPL_SETUP_TPNAME(sname, tname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_SETUP_FNAME(sname)<struct CTEST_IMPL_DATA_SNAME(sname)>; \ |
| 132 | static void (*CTEST_IMPL_TEARDOWN_TPNAME(sname, tname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_TEARDOWN_FNAME(sname)<struct CTEST_IMPL_DATA_SNAME(sname)>; \ |
| 133 | CTEST_IMPL_STRUCT(sname, tname, tskip, &CTEST_IMPL_DATA_TNAME(sname, tname), &CTEST_IMPL_SETUP_TPNAME(sname, tname), &CTEST_IMPL_TEARDOWN_TPNAME(sname, tname)); \ |
| 134 | static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* data) |
| 135 | |
| 136 | #else |
| 137 | |
| 138 | #define CTEST_SETUP(sname) \ |
| 139 | static void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* data); \ |
| 140 | static void (*CTEST_IMPL_SETUP_FPNAME(sname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_SETUP_FNAME(sname); \ |
| 141 | static void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* data) |
| 142 | |
| 143 | #define CTEST_TEARDOWN(sname) \ |
| 144 | static void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* data); \ |
| 145 | static void (*CTEST_IMPL_TEARDOWN_FPNAME(sname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_TEARDOWN_FNAME(sname); \ |
| 146 | static void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* data) |
| 147 | |
| 148 | #define CTEST_DATA(sname) \ |
| 149 | struct CTEST_IMPL_DATA_SNAME(sname); \ |
| 150 | static void (*CTEST_IMPL_SETUP_FPNAME(sname))(struct CTEST_IMPL_DATA_SNAME(sname)*); \ |
| 151 | static void (*CTEST_IMPL_TEARDOWN_FPNAME(sname))(struct CTEST_IMPL_DATA_SNAME(sname)*); \ |
| 152 | struct CTEST_IMPL_DATA_SNAME(sname) |
| 153 | |
| 154 | #define CTEST_IMPL_CTEST(sname, tname, tskip) \ |
| 155 | static void CTEST_IMPL_FNAME(sname, tname)(void); \ |
| 156 | CTEST_IMPL_STRUCT(sname, tname, tskip, NULL, NULL, NULL); \ |
| 157 | static void CTEST_IMPL_FNAME(sname, tname)(void) |
| 158 | |
| 159 | #define CTEST_IMPL_CTEST2(sname, tname, tskip) \ |
| 160 | static struct CTEST_IMPL_DATA_SNAME(sname) CTEST_IMPL_DATA_TNAME(sname, tname); \ |
| 161 | static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* data); \ |
| 162 | CTEST_IMPL_STRUCT(sname, tname, tskip, &CTEST_IMPL_DATA_TNAME(sname, tname), &CTEST_IMPL_SETUP_FPNAME(sname), &CTEST_IMPL_TEARDOWN_FPNAME(sname)); \ |
| 163 | static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* data) |
| 164 | |
| 165 | #endif |
| 166 | |
| 167 | void CTEST_LOG(const char* fmt, ...) CTEST_IMPL_FORMAT_PRINTF(1, 2); |
| 168 | void CTEST_ERR(const char* fmt, ...) CTEST_IMPL_FORMAT_PRINTF(1, 2); // doesn't return |
| 169 | |
| 170 | #define CTEST(sname, tname) CTEST_IMPL_CTEST(sname, tname, 0) |
| 171 | #define CTEST_SKIP(sname, tname) CTEST_IMPL_CTEST(sname, tname, 1) |
| 172 | |
| 173 | #define CTEST2(sname, tname) CTEST_IMPL_CTEST2(sname, tname, 0) |
| 174 | #define CTEST2_SKIP(sname, tname) CTEST_IMPL_CTEST2(sname, tname, 1) |
| 175 | |
| 176 | |
| 177 | void assert_str(const char* cmp, const char* exp, const char* real, const char* caller, int line); |
| 178 | #define ASSERT_STR(exp, real) assert_str("==", exp, real, __FILE__, __LINE__) |
| 179 | #define ASSERT_NOT_STR(exp, real) assert_str("!=", exp, real, __FILE__, __LINE__) |
| 180 | #define ASSERT_STRSTR(str, substr) assert_str("=~", str, substr, __FILE__, __LINE__) |
| 181 | #define ASSERT_NOT_STRSTR(str, substr) assert_str("!~", str, substr, __FILE__, __LINE__) |
| 182 | |
| 183 | void assert_wstr(const char* cmp, const wchar_t *exp, const wchar_t *real, const char* caller, int line); |
| 184 | #define ASSERT_WSTR(exp, real) assert_wstr("==", exp, real, __FILE__, __LINE__) |
| 185 | #define ASSERT_NOT_WSTR(exp, real) assert_wstr("!=", exp, real, __FILE__, __LINE__) |
| 186 | #define ASSERT_WSTRSTR(str, substr) assert_wstr("=~", str, substr, __FILE__, __LINE__) |
| 187 | #define ASSERT_NOT_WSTRSTR(str, substr) assert_wstr("!~", str, substr, __FILE__, __LINE__) |
| 188 | |
| 189 | void assert_data(const unsigned char* exp, size_t expsize, |
| 190 | const unsigned char* real, size_t realsize, |
| 191 | const char* caller, int line); |
| 192 | #define ASSERT_DATA(exp, expsize, real, realsize) \ |
| 193 | assert_data(exp, expsize, real, realsize, __FILE__, __LINE__) |
| 194 | |
| 195 | #define CTEST_FLT_EPSILON 1e-5 |
| 196 | #define CTEST_DBL_EPSILON 1e-12 |
| 197 | |
| 198 | void assert_compare(const char* cmp, intmax_t exp, intmax_t real, const char* caller, int line); |
| 199 | #define ASSERT_EQUAL(exp, real) assert_compare("==", exp, real, __FILE__, __LINE__) |
| 200 | #define ASSERT_NOT_EQUAL(exp, real) assert_compare("!=", exp, real, __FILE__, __LINE__) |
| 201 | |
| 202 | #define ASSERT_LT(v1, v2) assert_compare("<", v1, v2, __FILE__, __LINE__) |
| 203 | #define ASSERT_LE(v1, v2) assert_compare("<=", v1, v2, __FILE__, __LINE__) |
| 204 | #define ASSERT_GT(v1, v2) assert_compare(">", v1, v2, __FILE__, __LINE__) |
| 205 | #define ASSERT_GE(v1, v2) assert_compare(">=", v1, v2, __FILE__, __LINE__) |
| 206 | |
| 207 | void assert_compare_u(const char* cmp, uintmax_t exp, uintmax_t real, const char* caller, int line); |
| 208 | #define ASSERT_EQUAL_U(exp, real) assert_compare_u("==", exp, real, __FILE__, __LINE__) |
| 209 | #define ASSERT_NOT_EQUAL_U(exp, real) assert_compare_u("!=", exp, real, __FILE__, __LINE__) |
| 210 | |
| 211 | void assert_interval(intmax_t exp1, intmax_t exp2, intmax_t real, const char* caller, int line); |
| 212 | #define ASSERT_INTERVAL(exp1, exp2, real) assert_interval(exp1, exp2, real, __FILE__, __LINE__) |
| 213 | |
| 214 | void assert_null(void* real, const char* caller, int line); |
| 215 | #define ASSERT_NULL(real) assert_null((void*)real, __FILE__, __LINE__) |
| 216 | |
| 217 | void assert_not_null(const void* real, const char* caller, int line); |
| 218 | #define ASSERT_NOT_NULL(real) assert_not_null(real, __FILE__, __LINE__) |
| 219 | |
| 220 | void assert_true(int real, const char* caller, int line); |
| 221 | #define ASSERT_TRUE(real) assert_true(real, __FILE__, __LINE__) |
| 222 | |
| 223 | void assert_false(int real, const char* caller, int line); |
| 224 | #define ASSERT_FALSE(real) assert_false(real, __FILE__, __LINE__) |
| 225 | |
| 226 | void assert_fail(const char* caller, int line); |
| 227 | #define ASSERT_FAIL() assert_fail(__FILE__, __LINE__) |
| 228 | |
| 229 | void assert_dbl_compare(const char* cmp, double exp, double real, double tol, const char* caller, int line); |
| 230 | #define ASSERT_DBL_NEAR(exp, real) assert_dbl_compare("==", exp, real, -CTEST_DBL_EPSILON, __FILE__, __LINE__) |
| 231 | #define ASSERT_DBL_NEAR_TOL(exp, real, tol) assert_dbl_compare("==", exp, real, tol, __FILE__, __LINE__) |
| 232 | #define ASSERT_DBL_FAR(exp, real) assert_dbl_compare("!=", exp, real, -CTEST_DBL_EPSILON, __FILE__, __LINE__) |
| 233 | #define ASSERT_DBL_FAR_TOL(exp, real, tol) assert_dbl_compare("!=", exp, real, tol, __FILE__, __LINE__) |
| 234 | |
| 235 | #define ASSERT_FLT_NEAR(v1, v2) assert_dbl_compare("==", v1, v2, -CTEST_FLT_EPSILON, __FILE__, __LINE__) |
| 236 | #define ASSERT_FLT_FAR(v1, v2) assert_dbl_compare("!=", v1, v2, -CTEST_FLT_EPSILON, __FILE__, __LINE__) |
| 237 | #define ASSERT_DBL_LT(v1, v2) assert_dbl_compare("<", v1, v2, 0.0, __FILE__, __LINE__) |
| 238 | #define ASSERT_DBL_GT(v1, v2) assert_dbl_compare(">", v1, v2, 0.0, __FILE__, __LINE__) |
| 239 | |
| 240 | #ifdef CTEST_MAIN |
| 241 | |
| 242 | #include <setjmp.h> |
| 243 | #include <stdarg.h> |
| 244 | #include <stdio.h> |
| 245 | #include <string.h> |
| 246 | #include <time.h> |
| 247 | #if !defined(_WIN32) || defined(__GNUC__) |
| 248 | #include <unistd.h> |
| 249 | #elif defined(_WIN32) |
| 250 | #include <io.h> |
| 251 | #endif |
| 252 | #include <stdint.h> |
| 253 | #include <stdlib.h> |
| 254 | #include <wchar.h> |
| 255 | |
| 256 | static size_t ctest_errorsize; |
| 257 | static char* ctest_errormsg; |
| 258 | #define MSG_SIZE 4096 |
| 259 | static char ctest_errorbuffer[MSG_SIZE]; |
| 260 | static jmp_buf ctest_err; |
| 261 | static int color_output = 1; |
| 262 | static const char* suite_name; |
| 263 | |
| 264 | typedef int (*ctest_filter_func)(struct ctest*); |
| 265 | |
| 266 | #define ANSI_BLACK "\033[0;30m" |
| 267 | #define ANSI_RED "\033[0;31m" |
| 268 | #define ANSI_GREEN "\033[0;32m" |
| 269 | #define ANSI_YELLOW "\033[0;33m" |
| 270 | #define ANSI_BLUE "\033[0;34m" |
| 271 | #define ANSI_MAGENTA "\033[0;35m" |
| 272 | #define ANSI_CYAN "\033[0;36m" |
| 273 | #define ANSI_GREY "\033[0;37m" |
| 274 | #define ANSI_DARKGREY "\033[01;30m" |
| 275 | #define ANSI_BRED "\033[01;31m" |
| 276 | #define ANSI_BGREEN "\033[01;32m" |
| 277 | #define ANSI_BYELLOW "\033[01;33m" |
| 278 | #define ANSI_BBLUE "\033[01;34m" |
| 279 | #define ANSI_BMAGENTA "\033[01;35m" |
| 280 | #define ANSI_BCYAN "\033[01;36m" |
| 281 | #define ANSI_WHITE "\033[01;37m" |
| 282 | #define ANSI_NORMAL "\033[0m" |
| 283 | |
| 284 | CTEST(suite, test) { } |
| 285 | |
| 286 | static void vprint_errormsg(const char* const fmt, va_list ap) CTEST_IMPL_FORMAT_PRINTF(1, 0); |
| 287 | static void print_errormsg(const char* const fmt, ...) CTEST_IMPL_FORMAT_PRINTF(1, 2); |
| 288 | |
| 289 | static void vprint_errormsg(const char* const fmt, va_list ap) { |
| 290 | // (v)snprintf returns the number that would have been written |
| 291 | const int ret = vsnprintf(ctest_errormsg, ctest_errorsize, fmt, ap); |
| 292 | if (ret < 0) { |
| 293 | ctest_errormsg[0] = 0x00; |
| 294 | } else { |
| 295 | const size_t size = (size_t) ret; |
| 296 | const size_t s = (ctest_errorsize <= size ? size -ctest_errorsize : size); |
| 297 | // ctest_errorsize may overflow at this point |
| 298 | ctest_errorsize -= s; |
| 299 | ctest_errormsg += s; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static void print_errormsg(const char* const fmt, ...) { |
| 304 | va_list argp; |
| 305 | va_start(argp, fmt); |
| 306 | vprint_errormsg(fmt, argp); |
| 307 | va_end(argp); |
| 308 | } |
| 309 | |
| 310 | static void msg_start(const char* color, const char* title) { |
| 311 | if (color_output) { |
| 312 | print_errormsg("%s" , color); |
| 313 | } |
| 314 | print_errormsg(" %s: " , title); |
| 315 | } |
| 316 | |
| 317 | static void msg_end(void) { |
| 318 | if (color_output) { |
| 319 | print_errormsg(ANSI_NORMAL); |
| 320 | } |
| 321 | print_errormsg("\n" ); |
| 322 | } |
| 323 | |
| 324 | void CTEST_LOG(const char* fmt, ...) |
| 325 | { |
| 326 | va_list argp; |
| 327 | msg_start(ANSI_BLUE, "LOG" ); |
| 328 | |
| 329 | va_start(argp, fmt); |
| 330 | vprint_errormsg(fmt, argp); |
| 331 | va_end(argp); |
| 332 | |
| 333 | msg_end(); |
| 334 | } |
| 335 | |
| 336 | CTEST_IMPL_DIAG_PUSH_IGNORED(missing-noreturn) |
| 337 | |
| 338 | void CTEST_ERR(const char* fmt, ...) |
| 339 | { |
| 340 | va_list argp; |
| 341 | msg_start(ANSI_YELLOW, "ERR" ); |
| 342 | |
| 343 | va_start(argp, fmt); |
| 344 | vprint_errormsg(fmt, argp); |
| 345 | va_end(argp); |
| 346 | |
| 347 | msg_end(); |
| 348 | longjmp(ctest_err, 1); |
| 349 | } |
| 350 | |
| 351 | CTEST_IMPL_DIAG_POP() |
| 352 | |
| 353 | void assert_str(const char* cmp, const char* exp, const char* real, const char* caller, int line) { |
| 354 | if ((!exp ^ !real) || (exp && ( |
| 355 | (cmp[1] == '=' && ((cmp[0] == '=') ^ (strcmp(exp, real) == 0))) || |
| 356 | (cmp[1] == '~' && ((cmp[0] == '=') ^ (strstr(exp, real) != NULL))) |
| 357 | ))) { |
| 358 | CTEST_ERR("%s:%d assertion failed, '%s' %s '%s'" , caller, line, exp, cmp, real); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void assert_wstr(const char* cmp, const wchar_t *exp, const wchar_t *real, const char* caller, int line) { |
| 363 | if ((!exp ^ !real) || (exp && ( |
| 364 | (cmp[1] == '=' && ((cmp[0] == '=') ^ (wcscmp(exp, real) == 0))) || |
| 365 | (cmp[1] == '~' && ((cmp[0] == '=') ^ (wcsstr(exp, real) != NULL))) |
| 366 | ))) { |
| 367 | CTEST_ERR("%s:%d assertion failed, '%ls' %s '%ls'" , caller, line, exp, cmp, real); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | void assert_data(const unsigned char* exp, size_t expsize, |
| 372 | const unsigned char* real, size_t realsize, |
| 373 | const char* caller, int line) { |
| 374 | size_t i; |
| 375 | if (expsize != realsize) { |
| 376 | CTEST_ERR("%s:%d expected %" PRIuMAX " bytes, got %" PRIuMAX, caller, line, (uintmax_t) expsize, (uintmax_t) realsize); |
| 377 | } |
| 378 | for (i=0; i<expsize; i++) { |
| 379 | if (exp[i] != real[i]) { |
| 380 | CTEST_ERR("%s:%d expected 0x%02x at offset %" PRIuMAX " got 0x%02x" , |
| 381 | caller, line, exp[i], (uintmax_t) i, real[i]); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static bool get_compare_result(const char* cmp, int c3, bool eq) { |
| 387 | if (cmp[0] == '<') |
| 388 | return c3 < 0 || ((cmp[1] == '=') & eq); |
| 389 | if (cmp[0] == '>') |
| 390 | return c3 > 0 || ((cmp[1] == '=') & eq); |
| 391 | return (cmp[0] == '=') == eq; |
| 392 | } |
| 393 | |
| 394 | void assert_compare(const char* cmp, intmax_t exp, intmax_t real, const char* caller, int line) { |
| 395 | int c3 = (real < exp) - (exp < real); |
| 396 | |
| 397 | if (!get_compare_result(cmp, c3, c3 == 0)) { |
| 398 | CTEST_ERR("%s:%d assertion failed, %" PRIdMAX " %s %" PRIdMAX "" , caller, line, exp, cmp, real); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void assert_compare_u(const char* cmp, uintmax_t exp, uintmax_t real, const char* caller, int line) { |
| 403 | int c3 = (real < exp) - (exp < real); |
| 404 | |
| 405 | if (!get_compare_result(cmp, c3, c3 == 0)) { |
| 406 | CTEST_ERR("%s:%d assertion failed, %" PRIuMAX " %s %" PRIuMAX, caller, line, exp, cmp, real); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void assert_interval(intmax_t exp1, intmax_t exp2, intmax_t real, const char* caller, int line) { |
| 411 | if (real < exp1 || real > exp2) { |
| 412 | CTEST_ERR("%s:%d expected %" PRIdMAX "-%" PRIdMAX ", got %" PRIdMAX, caller, line, exp1, exp2, real); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | static bool approximately_equal(double a, double b, double epsilon) { |
| 417 | double d = a - b; |
| 418 | if (d < 0) d = -d; |
| 419 | if (a < 0) a = -a; |
| 420 | if (b < 0) b = -b; |
| 421 | return d <= (a > b ? a : b)*epsilon; /* D.Knuth */ |
| 422 | } |
| 423 | |
| 424 | /* tol < 0 means it is an epsilon, else absolute error */ |
| 425 | void assert_dbl_compare(const char* cmp, double exp, double real, double tol, const char* caller, int line) { |
| 426 | double diff = exp - real; |
| 427 | double absdiff = diff < 0 ? -diff : diff; |
| 428 | int c3 = (real < exp) - (exp < real); |
| 429 | bool eq = tol < 0 ? approximately_equal(exp, real, -tol) : absdiff <= tol; |
| 430 | |
| 431 | if (!get_compare_result(cmp, c3, eq)) { |
| 432 | const char* tolstr = "tol" ; |
| 433 | if (tol < 0) { |
| 434 | tolstr = "eps" ; |
| 435 | tol = -tol; |
| 436 | } |
| 437 | CTEST_ERR("%s:%d assertion failed, %.8g %s %.8g (diff %.4g, %s %.4g)" , caller, line, exp, cmp, real, diff, tolstr, tol); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | void assert_null(void* real, const char* caller, int line) { |
| 442 | if ((real) != NULL) { |
| 443 | CTEST_ERR("%s:%d should be NULL" , caller, line); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void assert_not_null(const void* real, const char* caller, int line) { |
| 448 | if (real == NULL) { |
| 449 | CTEST_ERR("%s:%d should not be NULL" , caller, line); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void assert_true(int real, const char* caller, int line) { |
| 454 | if ((real) == 0) { |
| 455 | CTEST_ERR("%s:%d should be true" , caller, line); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void assert_false(int real, const char* caller, int line) { |
| 460 | if ((real) != 0) { |
| 461 | CTEST_ERR("%s:%d should be false" , caller, line); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | void assert_fail(const char* caller, int line) { |
| 466 | CTEST_ERR("%s:%d shouldn't come here" , caller, line); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | static int suite_all(struct ctest* t) { |
| 471 | (void) t; // fix unused parameter warning |
| 472 | return 1; |
| 473 | } |
| 474 | |
| 475 | static int suite_filter(struct ctest* t) { |
| 476 | return strncmp(suite_name, t->ssname, strlen(suite_name)) == 0; |
| 477 | } |
| 478 | |
| 479 | static void color_print(const char* color, const char* text) { |
| 480 | if (color_output) |
| 481 | printf("%s%s" ANSI_NORMAL "\n" , color, text); |
| 482 | else |
| 483 | printf("%s\n" , text); |
| 484 | } |
| 485 | |
| 486 | #ifdef CTEST_SEGFAULT |
| 487 | #include <signal.h> |
| 488 | static void sighandler(int signum) |
| 489 | { |
| 490 | const char msg_color[] = ANSI_BRED "[SIGSEGV: Segmentation fault]" ANSI_NORMAL "\n" ; |
| 491 | const char msg_nocolor[] = "[SIGSEGV: Segmentation fault]\n" ; |
| 492 | |
| 493 | const char* msg = color_output ? msg_color : msg_nocolor; |
| 494 | write(STDOUT_FILENO, msg, (unsigned int)strlen(msg)); |
| 495 | |
| 496 | /* "Unregister" the signal handler and send the signal back to the process |
| 497 | * so it can terminate as expected */ |
| 498 | signal(signum, SIG_DFL); |
| 499 | #if !defined(_WIN32) || defined(__CYGWIN__) |
| 500 | kill(getpid(), signum); |
| 501 | #endif |
| 502 | } |
| 503 | #endif |
| 504 | |
| 505 | int ctest_main(int argc, const char *argv[]); |
| 506 | |
| 507 | __attribute__((no_sanitize_address)) int ctest_main(int argc, const char *argv[]) |
| 508 | { |
| 509 | static int total = 0; |
| 510 | static int num_ok = 0; |
| 511 | static int num_fail = 0; |
| 512 | static int num_skip = 0; |
| 513 | static int idx = 1; |
| 514 | static ctest_filter_func filter = suite_all; |
| 515 | |
| 516 | #ifdef CTEST_SEGFAULT |
| 517 | signal(SIGSEGV, sighandler); |
| 518 | #endif |
| 519 | |
| 520 | if (argc == 2) { |
| 521 | suite_name = argv[1]; |
| 522 | filter = suite_filter; |
| 523 | } |
| 524 | #ifdef CTEST_NO_COLORS |
| 525 | color_output = 0; |
| 526 | #else |
| 527 | color_output = isatty(1); |
| 528 | #endif |
| 529 | clock_t t1 = clock(); |
| 530 | |
| 531 | struct ctest* ctest_begin = &CTEST_IMPL_TNAME(suite, test); |
| 532 | struct ctest* ctest_end = &CTEST_IMPL_TNAME(suite, test); |
| 533 | // find begin and end of section by comparing magics |
| 534 | while (1) { |
| 535 | struct ctest* t = ctest_begin-1; |
| 536 | if (t->magic != CTEST_IMPL_MAGIC) break; |
| 537 | ctest_begin--; |
| 538 | } |
| 539 | while (1) { |
| 540 | struct ctest* t = ctest_end+1; |
| 541 | if (t->magic != CTEST_IMPL_MAGIC) break; |
| 542 | ctest_end++; |
| 543 | } |
| 544 | ctest_end++; // end after last one |
| 545 | |
| 546 | static struct ctest* test; |
| 547 | for (test = ctest_begin; test != ctest_end; test++) { |
| 548 | if (test == &CTEST_IMPL_TNAME(suite, test)) continue; |
| 549 | if (filter(test)) total++; |
| 550 | } |
| 551 | |
| 552 | for (test = ctest_begin; test != ctest_end; test++) { |
| 553 | if (test == &CTEST_IMPL_TNAME(suite, test)) continue; |
| 554 | if (filter(test)) { |
| 555 | ctest_errorbuffer[0] = 0; |
| 556 | ctest_errorsize = MSG_SIZE-1; |
| 557 | ctest_errormsg = ctest_errorbuffer; |
| 558 | printf("TEST %d/%d %s:%s \n" , idx, total, test->ssname, test->ttname); |
| 559 | fflush(stdout); |
| 560 | if (test->skip) { |
| 561 | color_print(ANSI_BYELLOW, "[SKIPPED]" ); |
| 562 | num_skip++; |
| 563 | } else { |
| 564 | int result = setjmp(ctest_err); |
| 565 | if (result == 0) { |
| 566 | if (test->setup && *test->setup) (*test->setup)(test->data); |
| 567 | if (test->data) |
| 568 | test->run.unary(test->data); |
| 569 | else |
| 570 | test->run.nullary(); |
| 571 | if (test->teardown && *test->teardown) (*test->teardown)(test->data); |
| 572 | // if we got here it's ok |
| 573 | #ifdef CTEST_COLOR_OK |
| 574 | color_print(ANSI_BGREEN, "[OK]" ); |
| 575 | #else |
| 576 | printf("[OK]\n" ); |
| 577 | #endif |
| 578 | num_ok++; |
| 579 | } else { |
| 580 | color_print(ANSI_BRED, "[FAIL]" ); |
| 581 | num_fail++; |
| 582 | } |
| 583 | if (ctest_errorsize != MSG_SIZE-1) printf("%s" , ctest_errorbuffer); |
| 584 | } |
| 585 | idx++; |
| 586 | } |
| 587 | } |
| 588 | clock_t t2 = clock(); |
| 589 | |
| 590 | const char* color = (num_fail) ? ANSI_BRED : ANSI_GREEN; |
| 591 | char results[80]; |
| 592 | snprintf(results, sizeof(results), "RESULTS: %d tests (%d ok, %d failed, %d skipped) ran in %.1f ms" , |
| 593 | total, num_ok, num_fail, num_skip, (double)(t2 - t1)*1000.0/CLOCKS_PER_SEC); |
| 594 | color_print(color, results); |
| 595 | return num_fail; |
| 596 | } |
| 597 | |
| 598 | #endif |
| 599 | |
| 600 | #ifdef __cplusplus |
| 601 | } |
| 602 | #endif |
| 603 | |
| 604 | #endif |
| 605 | |
| 606 | |