| 1 |
/* Copyright (c) 2013-2018 Evan Nemerson <[email protected]> |
| 2 |
* |
| 3 |
* Permission is hereby granted, free of charge, to any person |
| 4 |
* obtaining a copy of this software and associated documentation |
| 5 |
* files (the "Software"), to deal in the Software without |
| 6 |
* restriction, including without limitation the rights to use, copy, |
| 7 |
* modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
* of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be |
| 12 |
* included in all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 18 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 19 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
/*** Configuration ***/ |
| 25 |
|
| 26 |
/* This is just where the output from the test goes. It's really just |
| 27 |
* meant to let you choose stdout or stderr, but if anyone really want |
| 28 |
* to direct it to a file let me know, it would be fairly easy to |
| 29 |
* support. */ |
| 30 |
#if !defined(MUNIT_OUTPUT_FILE) |
| 31 |
# define MUNIT_OUTPUT_FILE stdout |
| 32 |
#endif |
| 33 |
|
| 34 |
/* This is a bit more useful; it tells µnit how to format the seconds in |
| 35 |
* timed tests. If your tests run for longer you might want to reduce |
| 36 |
* it, and if your computer is really fast and your tests are tiny you |
| 37 |
* can increase it. */ |
| 38 |
#if !defined(MUNIT_TEST_TIME_FORMAT) |
| 39 |
# define MUNIT_TEST_TIME_FORMAT "0.8f" |
| 40 |
#endif |
| 41 |
|
| 42 |
/* If you have long test names you might want to consider bumping |
| 43 |
* this. The result information takes 43 characters. */ |
| 44 |
#if !defined(MUNIT_TEST_NAME_LEN) |
| 45 |
# define MUNIT_TEST_NAME_LEN 37 |
| 46 |
#endif |
| 47 |
|
| 48 |
/* If you don't like the timing information, you can disable it by |
| 49 |
* defining MUNIT_DISABLE_TIMING. */ |
| 50 |
#if !defined(MUNIT_DISABLE_TIMING) |
| 51 |
# define MUNIT_ENABLE_TIMING |
| 52 |
#endif |
| 53 |
|
| 54 |
/*** End configuration ***/ |
| 55 |
|
| 56 |
#if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE < 200809L) |
| 57 |
# undef _POSIX_C_SOURCE |
| 58 |
#endif |
| 59 |
#if !defined(_POSIX_C_SOURCE) |
| 60 |
# define _POSIX_C_SOURCE 200809L |
| 61 |
#endif |
| 62 |
|
| 63 |
/* Solaris freaks out if you try to use a POSIX or SUS standard without |
| 64 |
* the "right" C standard. */ |
| 65 |
#if defined(_XOPEN_SOURCE) |
| 66 |
# undef _XOPEN_SOURCE |
| 67 |
#endif |
| 68 |
|
| 69 |
#if defined(__STDC_VERSION__) |
| 70 |
# if __STDC_VERSION__ >= 201112L |
| 71 |
# define _XOPEN_SOURCE 700 |
| 72 |
# elif __STDC_VERSION__ >= 199901L |
| 73 |
# define _XOPEN_SOURCE 600 |
| 74 |
# endif |
| 75 |
#endif |
| 76 |
|
| 77 |
/* Because, according to Microsoft, POSIX is deprecated. You've got |
| 78 |
* to appreciate the chutzpah. */ |
| 79 |
#if defined(_MSC_VER) && !defined(_CRT_NONSTDC_NO_DEPRECATE) |
| 80 |
# define _CRT_NONSTDC_NO_DEPRECATE |
| 81 |
#endif |
| 82 |
|
| 83 |
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) |
| 84 |
# include <stdbool.h> |
| 85 |
#elif defined(_WIN32) |
| 86 |
/* https://msdn.microsoft.com/en-us/library/tf4dy80a.aspx */ |
| 87 |
#endif |
| 88 |
|
| 89 |
#include <limits.h> |
| 90 |
#include <time.h> |
| 91 |
#include <errno.h> |
| 92 |
#include <string.h> |
| 93 |
#include <stdlib.h> |
| 94 |
#include <stdio.h> |
| 95 |
#include <stdarg.h> |
| 96 |
#include <setjmp.h> |
| 97 |
|
| 98 |
#if !defined(MUNIT_NO_NL_LANGINFO) && !defined(_WIN32) |
| 99 |
#define MUNIT_NL_LANGINFO |
| 100 |
#include <locale.h> |
| 101 |
#include <langinfo.h> |
| 102 |
#include <strings.h> |
| 103 |
#endif |
| 104 |
|
| 105 |
#if !defined(_WIN32) |
| 106 |
# include <unistd.h> |
| 107 |
# include <sys/types.h> |
| 108 |
# include <sys/wait.h> |
| 109 |
#else |
| 110 |
# include <windows.h> |
| 111 |
# include <io.h> |
| 112 |
# include <fcntl.h> |
| 113 |
# if !defined(STDERR_FILENO) |
| 114 |
# define STDERR_FILENO _fileno(stderr) |
| 115 |
# endif |
| 116 |
#endif |
| 117 |
|
| 118 |
#include "munit.h" |
| 119 |
|
| 120 |
#define MUNIT_STRINGIFY(x) #x |
| 121 |
#define MUNIT_XSTRINGIFY(x) MUNIT_STRINGIFY(x) |
| 122 |
|
| 123 |
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__) |
| 124 |
# define MUNIT_THREAD_LOCAL __thread |
| 125 |
#elif (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201102L)) || defined(_Thread_local) |
| 126 |
# define MUNIT_THREAD_LOCAL _Thread_local |
| 127 |
#elif defined(_WIN32) |
| 128 |
# define MUNIT_THREAD_LOCAL __declspec(thread) |
| 129 |
#endif |
| 130 |
|
| 131 |
/* MSVC 12.0 will emit a warning at /W4 for code like 'do { ... } |
| 132 |
* while (0)', or 'do { ... } while (true)'. I'm pretty sure nobody |
| 133 |
* at Microsoft compiles with /W4. */ |
| 134 |
#if defined(_MSC_VER) && (_MSC_VER <= 1800) |
| 135 |
#pragma warning(disable: 4127) |
| 136 |
#endif |
| 137 |
|
| 138 |
#if defined(_WIN32) || defined(__EMSCRIPTEN__) |
| 139 |
# define MUNIT_NO_FORK |
| 140 |
#endif |
| 141 |
|
| 142 |
#if defined(__EMSCRIPTEN__) |
| 143 |
# define MUNIT_NO_BUFFER |
| 144 |
#endif |
| 145 |
|
| 146 |
/*** Logging ***/ |
| 147 |
|
| 148 |
static MunitLogLevel munit_log_level_visible = MUNIT_LOG_INFO; |
| 149 |
static MunitLogLevel munit_log_level_fatal = MUNIT_LOG_ERROR; |
| 150 |
|
| 151 |
#if defined(MUNIT_THREAD_LOCAL) |
| 152 |
static MUNIT_THREAD_LOCAL bool munit_error_jmp_buf_valid = false; |
| 153 |
static MUNIT_THREAD_LOCAL jmp_buf munit_error_jmp_buf; |
| 154 |
#endif |
| 155 |
|
| 156 |
/* At certain warning levels, mingw will trigger warnings about |
| 157 |
* suggesting the format attribute, which we've explicity *not* set |
| 158 |
* because it will then choke on our attempts to use the MS-specific |
| 159 |
* I64 modifier for size_t (which we have to use since MSVC doesn't |
| 160 |
* support the C99 z modifier). */ |
| 161 |
|
| 162 |
#if defined(__MINGW32__) || defined(__MINGW64__) |
| 163 |
# pragma GCC diagnostic push |
| 164 |
# pragma GCC diagnostic ignored "-Wsuggest-attribute=format" |
| 165 |
#endif |
| 166 |
|
| 167 |
MUNIT_PRINTF(5,0) |
| 168 |
static void |
| 169 |
munit_logf_exv(MunitLogLevel level, FILE* fp, const char* filename, int line, const char* format, va_list ap) { |
| 170 |
if (level < munit_log_level_visible) |
| 171 |
return; |
| 172 |
|
| 173 |
switch (level) { |
| 174 |
case MUNIT_LOG_DEBUG: |
| 175 |
fputs("Debug", fp); |
| 176 |
break; |
| 177 |
case MUNIT_LOG_INFO: |
| 178 |
fputs("Info", fp); |
| 179 |
break; |
| 180 |
case MUNIT_LOG_WARNING: |
| 181 |
fputs("Warning", fp); |
| 182 |
break; |
| 183 |
case MUNIT_LOG_ERROR: |
| 184 |
fputs("Error", fp); |
| 185 |
break; |
| 186 |
default: |
| 187 |
munit_logf_ex(MUNIT_LOG_ERROR, filename, line, "Invalid log level (%d)", level); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
fputs(": ", fp); |
| 192 |
if (filename != NULL) |
| 193 |
fprintf(fp, "%s:%d: ", filename, line); |
| 194 |
vfprintf(fp, format, ap); |
| 195 |
fputc('\n', fp); |
| 196 |
} |
| 197 |
|
| 198 |
MUNIT_PRINTF(3,4) |
| 199 |
static void |
| 200 |
munit_logf_internal(MunitLogLevel level, FILE* fp, const char* format, ...) { |
| 201 |
va_list ap; |
| 202 |
|
| 203 |
va_start(ap, format); |
| 204 |
munit_logf_exv(level, fp, NULL, 0, format, ap); |
| 205 |
va_end(ap); |
| 206 |
} |
| 207 |
|
| 208 |
static void |
| 209 |
munit_log_internal(MunitLogLevel level, FILE* fp, const char* message) { |
| 210 |
munit_logf_internal(level, fp, "%s", message); |
| 211 |
} |
| 212 |
|
| 213 |
void |
| 214 |
munit_logf_ex(MunitLogLevel level, const char* filename, int line, const char* format, ...) { |
| 215 |
va_list ap; |
| 216 |
|
| 217 |
va_start(ap, format); |
| 218 |
munit_logf_exv(level, stderr, filename, line, format, ap); |
| 219 |
va_end(ap); |
| 220 |
|
| 221 |
if (level >= munit_log_level_fatal) { |
| 222 |
#if defined(MUNIT_THREAD_LOCAL) |
| 223 |
if (munit_error_jmp_buf_valid) |
| 224 |
longjmp(munit_error_jmp_buf, 1); |
| 225 |
#endif |
| 226 |
abort(); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
void |
| 231 |
munit_errorf_ex(const char* filename, int line, const char* format, ...) { |
| 232 |
va_list ap; |
| 233 |
|
| 234 |
va_start(ap, format); |
| 235 |
munit_logf_exv(MUNIT_LOG_ERROR, stderr, filename, line, format, ap); |
| 236 |
va_end(ap); |
| 237 |
|
| 238 |
#if defined(MUNIT_THREAD_LOCAL) |
| 239 |
if (munit_error_jmp_buf_valid) |
| 240 |
longjmp(munit_error_jmp_buf, 1); |
| 241 |
#endif |
| 242 |
abort(); |
| 243 |
} |
| 244 |
|
| 245 |
#if defined(__MINGW32__) || defined(__MINGW64__) |
| 246 |
#pragma GCC diagnostic pop |
| 247 |
#endif |
| 248 |
|
| 249 |
#if !defined(MUNIT_STRERROR_LEN) |
| 250 |
# define MUNIT_STRERROR_LEN 80 |
| 251 |
#endif |
| 252 |
|
| 253 |
static void |
| 254 |
munit_log_errno(MunitLogLevel level, FILE* fp, const char* msg) { |
| 255 |
#if defined(MUNIT_NO_STRERROR_R) || (defined(__MINGW32__) && !defined(MINGW_HAS_SECURE_API)) |
| 256 |
munit_logf_internal(level, fp, "%s: %s (%d)", msg, strerror(errno), errno); |
| 257 |
#else |
| 258 |
char munit_error_str[MUNIT_STRERROR_LEN]; |
| 259 |
munit_error_str[0] = '\0'; |
| 260 |
|
| 261 |
#if !defined(_WIN32) |
| 262 |
strerror_r(errno, munit_error_str, MUNIT_STRERROR_LEN); |
| 263 |
#else |
| 264 |
strerror_s(munit_error_str, MUNIT_STRERROR_LEN, errno); |
| 265 |
#endif |
| 266 |
|
| 267 |
munit_logf_internal(level, fp, "%s: %s (%d)", msg, munit_error_str, errno); |
| 268 |
#endif |
| 269 |
} |
| 270 |
|
| 271 |
/*** Memory allocation ***/ |
| 272 |
|
| 273 |
void* |
| 274 |
munit_malloc_ex(const char* filename, int line, size_t size) { |
| 275 |
void* ptr; |
| 276 |
|
| 277 |
if (size == 0) |
| 278 |
return NULL; |
| 279 |
|
| 280 |
ptr = calloc(1, size); |
| 281 |
if (MUNIT_UNLIKELY(ptr == NULL)) { |
| 282 |
munit_logf_ex(MUNIT_LOG_ERROR, filename, line, "Failed to allocate %" MUNIT_SIZE_MODIFIER "u bytes.", size); |
| 283 |
} |
| 284 |
|
| 285 |
return ptr; |
| 286 |
} |
| 287 |
|
| 288 |
/*** Timer code ***/ |
| 289 |
|
| 290 |
#if defined(MUNIT_ENABLE_TIMING) |
| 291 |
|
| 292 |
#define psnip_uint64_t munit_uint64_t |
| 293 |
#define psnip_uint32_t munit_uint32_t |
| 294 |
|
| 295 |
/* Code copied from portable-snippets |
| 296 |
* <https://github.com/nemequ/portable-snippets/>. If you need to |
| 297 |
* change something, please do it there so we can keep the code in |
| 298 |
* sync. */ |
| 299 |
|
| 300 |
/* Clocks (v1) |
| 301 |
* Portable Snippets - https://gitub.com/nemequ/portable-snippets |
| 302 |
* Created by Evan Nemerson <[email protected]> |
| 303 |
* |
| 304 |
* To the extent possible under law, the authors have waived all |
| 305 |
* copyright and related or neighboring rights to this code. For |
| 306 |
* details, see the Creative Commons Zero 1.0 Universal license at |
| 307 |
* https://creativecommons.org/publicdomain/zero/1.0/ |
| 308 |
*/ |
| 309 |
|
| 310 |
#if !defined(PSNIP_CLOCK_H) |
| 311 |
#define PSNIP_CLOCK_H |
| 312 |
|
| 313 |
#if !defined(psnip_uint64_t) |
| 314 |
# include "../exact-int/exact-int.h" |
| 315 |
#endif |
| 316 |
|
| 317 |
#if !defined(PSNIP_CLOCK_STATIC_INLINE) |
| 318 |
# if defined(__GNUC__) |
| 319 |
# define PSNIP_CLOCK__COMPILER_ATTRIBUTES __attribute__((__unused__)) |
| 320 |
# else |
| 321 |
# define PSNIP_CLOCK__COMPILER_ATTRIBUTES |
| 322 |
# endif |
| 323 |
|
| 324 |
# define PSNIP_CLOCK__FUNCTION PSNIP_CLOCK__COMPILER_ATTRIBUTES static |
| 325 |
#endif |
| 326 |
|
| 327 |
enum PsnipClockType { |
| 328 |
/* This clock provides the current time, in units since 1970-01-01 |
| 329 |
* 00:00:00 UTC not including leap seconds. In other words, UNIX |
| 330 |
* time. Keep in mind that this clock doesn't account for leap |
| 331 |
* seconds, and can go backwards (think NTP adjustments). */ |
| 332 |
PSNIP_CLOCK_TYPE_WALL = 1, |
| 333 |
/* The CPU time is a clock which increases only when the current |
| 334 |
* process is active (i.e., it doesn't increment while blocking on |
| 335 |
* I/O). */ |
| 336 |
PSNIP_CLOCK_TYPE_CPU = 2, |
| 337 |
/* Monotonic time is always running (unlike CPU time), but it only |
| 338 |
ever moves forward unless you reboot the system. Things like NTP |
| 339 |
adjustments have no effect on this clock. */ |
| 340 |
PSNIP_CLOCK_TYPE_MONOTONIC = 3 |
| 341 |
}; |
| 342 |
|
| 343 |
struct PsnipClockTimespec { |
| 344 |
psnip_uint64_t seconds; |
| 345 |
psnip_uint64_t nanoseconds; |
| 346 |
}; |
| 347 |
|
| 348 |
/* Methods we support: */ |
| 349 |
|
| 350 |
#define PSNIP_CLOCK_METHOD_CLOCK_GETTIME 1 |
| 351 |
#define PSNIP_CLOCK_METHOD_TIME 2 |
| 352 |
#define PSNIP_CLOCK_METHOD_GETTIMEOFDAY 3 |
| 353 |
#define PSNIP_CLOCK_METHOD_QUERYPERFORMANCECOUNTER 4 |
| 354 |
#define PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME 5 |
| 355 |
#define PSNIP_CLOCK_METHOD_CLOCK 6 |
| 356 |
#define PSNIP_CLOCK_METHOD_GETPROCESSTIMES 7 |
| 357 |
#define PSNIP_CLOCK_METHOD_GETRUSAGE 8 |
| 358 |
#define PSNIP_CLOCK_METHOD_GETSYSTEMTIMEPRECISEASFILETIME 9 |
| 359 |
#define PSNIP_CLOCK_METHOD_GETTICKCOUNT64 10 |
| 360 |
|
| 361 |
#include <assert.h> |
| 362 |
|
| 363 |
#if defined(HEDLEY_UNREACHABLE) |
| 364 |
# define PSNIP_CLOCK_UNREACHABLE() HEDLEY_UNREACHABLE() |
| 365 |
#else |
| 366 |
# define PSNIP_CLOCK_UNREACHABLE() assert(0) |
| 367 |
#endif |
| 368 |
|
| 369 |
/* Choose an implementation */ |
| 370 |
|
| 371 |
/* #undef PSNIP_CLOCK_WALL_METHOD */ |
| 372 |
/* #undef PSNIP_CLOCK_CPU_METHOD */ |
| 373 |
/* #undef PSNIP_CLOCK_MONOTONIC_METHOD */ |
| 374 |
|
| 375 |
/* We want to be able to detect the libc implementation, so we include |
| 376 |
<limits.h> (<features.h> isn't available everywhere). */ |
| 377 |
|
| 378 |
#if defined(__unix__) || defined(__unix) || defined(__linux__) |
| 379 |
# include <limits.h> |
| 380 |
# include <unistd.h> |
| 381 |
#endif |
| 382 |
|
| 383 |
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) |
| 384 |
/* These are known to work without librt. If you know of others |
| 385 |
* please let us know so we can add them. */ |
| 386 |
# if \ |
| 387 |
(defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17))) || \ |
| 388 |
(defined(__FreeBSD__)) |
| 389 |
# define PSNIP_CLOCK_HAVE_CLOCK_GETTIME |
| 390 |
# elif !defined(PSNIP_CLOCK_NO_LIBRT) |
| 391 |
# define PSNIP_CLOCK_HAVE_CLOCK_GETTIME |
| 392 |
# endif |
| 393 |
#endif |
| 394 |
|
| 395 |
#if defined(_WIN32) |
| 396 |
# if !defined(PSNIP_CLOCK_CPU_METHOD) |
| 397 |
# define PSNIP_CLOCK_CPU_METHOD PSNIP_CLOCK_METHOD_GETPROCESSTIMES |
| 398 |
# endif |
| 399 |
# if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) |
| 400 |
# define PSNIP_CLOCK_MONOTONIC_METHOD PSNIP_CLOCK_METHOD_QUERYPERFORMANCECOUNTER |
| 401 |
# endif |
| 402 |
#endif |
| 403 |
|
| 404 |
#if defined(__MACH__) && !defined(__gnu_hurd__) |
| 405 |
# if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) |
| 406 |
# define PSNIP_CLOCK_MONOTONIC_METHOD PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME |
| 407 |
# endif |
| 408 |
#endif |
| 409 |
|
| 410 |
#if defined(PSNIP_CLOCK_HAVE_CLOCK_GETTIME) |
| 411 |
# include <time.h> |
| 412 |
# if !defined(PSNIP_CLOCK_WALL_METHOD) |
| 413 |
# if defined(CLOCK_REALTIME_PRECISE) |
| 414 |
# define PSNIP_CLOCK_WALL_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 415 |
# define PSNIP_CLOCK_CLOCK_GETTIME_WALL CLOCK_REALTIME_PRECISE |
| 416 |
# elif !defined(__sun) |
| 417 |
# define PSNIP_CLOCK_WALL_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 418 |
# define PSNIP_CLOCK_CLOCK_GETTIME_WALL CLOCK_REALTIME |
| 419 |
# endif |
| 420 |
# endif |
| 421 |
# if !defined(PSNIP_CLOCK_CPU_METHOD) |
| 422 |
# if defined(_POSIX_CPUTIME) || defined(CLOCK_PROCESS_CPUTIME_ID) |
| 423 |
# define PSNIP_CLOCK_CPU_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 424 |
# define PSNIP_CLOCK_CLOCK_GETTIME_CPU CLOCK_PROCESS_CPUTIME_ID |
| 425 |
# elif defined(CLOCK_VIRTUAL) |
| 426 |
# define PSNIP_CLOCK_CPU_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 427 |
# define PSNIP_CLOCK_CLOCK_GETTIME_CPU CLOCK_VIRTUAL |
| 428 |
# endif |
| 429 |
# endif |
| 430 |
# if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) |
| 431 |
# if defined(CLOCK_MONOTONIC_RAW) |
| 432 |
# define PSNIP_CLOCK_MONOTONIC_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 433 |
# define PSNIP_CLOCK_CLOCK_GETTIME_MONOTONIC CLOCK_MONOTONIC |
| 434 |
# elif defined(CLOCK_MONOTONIC_PRECISE) |
| 435 |
# define PSNIP_CLOCK_MONOTONIC_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 436 |
# define PSNIP_CLOCK_CLOCK_GETTIME_MONOTONIC CLOCK_MONOTONIC_PRECISE |
| 437 |
# elif defined(_POSIX_MONOTONIC_CLOCK) || defined(CLOCK_MONOTONIC) |
| 438 |
# define PSNIP_CLOCK_MONOTONIC_METHOD PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 439 |
# define PSNIP_CLOCK_CLOCK_GETTIME_MONOTONIC CLOCK_MONOTONIC |
| 440 |
# endif |
| 441 |
# endif |
| 442 |
#endif |
| 443 |
|
| 444 |
#if defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L) |
| 445 |
# if !defined(PSNIP_CLOCK_WALL_METHOD) |
| 446 |
# define PSNIP_CLOCK_WALL_METHOD PSNIP_CLOCK_METHOD_GETTIMEOFDAY |
| 447 |
# endif |
| 448 |
#endif |
| 449 |
|
| 450 |
#if !defined(PSNIP_CLOCK_WALL_METHOD) |
| 451 |
# define PSNIP_CLOCK_WALL_METHOD PSNIP_CLOCK_METHOD_TIME |
| 452 |
#endif |
| 453 |
|
| 454 |
#if !defined(PSNIP_CLOCK_CPU_METHOD) |
| 455 |
# define PSNIP_CLOCK_CPU_METHOD PSNIP_CLOCK_METHOD_CLOCK |
| 456 |
#endif |
| 457 |
|
| 458 |
/* Primarily here for testing. */ |
| 459 |
#if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) && defined(PSNIP_CLOCK_REQUIRE_MONOTONIC) |
| 460 |
# error No monotonic clock found. |
| 461 |
#endif |
| 462 |
|
| 463 |
/* Implementations */ |
| 464 |
|
| 465 |
#if \ |
| 466 |
(defined(PSNIP_CLOCK_CPU_METHOD) && (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) || \ |
| 467 |
(defined(PSNIP_CLOCK_WALL_METHOD) && (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) || \ |
| 468 |
(defined(PSNIP_CLOCK_MONOTONIC_METHOD) && (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) || \ |
| 469 |
(defined(PSNIP_CLOCK_CPU_METHOD) && (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK)) || \ |
| 470 |
(defined(PSNIP_CLOCK_WALL_METHOD) && (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_CLOCK)) || \ |
| 471 |
(defined(PSNIP_CLOCK_MONOTONIC_METHOD) && (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_CLOCK)) || \ |
| 472 |
(defined(PSNIP_CLOCK_CPU_METHOD) && (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_TIME)) || \ |
| 473 |
(defined(PSNIP_CLOCK_WALL_METHOD) && (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_TIME)) || \ |
| 474 |
(defined(PSNIP_CLOCK_MONOTONIC_METHOD) && (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_TIME)) |
| 475 |
# include <time.h> |
| 476 |
#endif |
| 477 |
|
| 478 |
#if \ |
| 479 |
(defined(PSNIP_CLOCK_CPU_METHOD) && (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETTIMEOFDAY)) || \ |
| 480 |
(defined(PSNIP_CLOCK_WALL_METHOD) && (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETTIMEOFDAY)) || \ |
| 481 |
(defined(PSNIP_CLOCK_MONOTONIC_METHOD) && (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETTIMEOFDAY)) |
| 482 |
# include <sys/time.h> |
| 483 |
#endif |
| 484 |
|
| 485 |
#if \ |
| 486 |
(defined(PSNIP_CLOCK_CPU_METHOD) && (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETPROCESSTIMES)) || \ |
| 487 |
(defined(PSNIP_CLOCK_WALL_METHOD) && (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETPROCESSTIMES)) || \ |
| 488 |
(defined(PSNIP_CLOCK_MONOTONIC_METHOD) && (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETPROCESSTIMES)) || \ |
| 489 |
(defined(PSNIP_CLOCK_CPU_METHOD) && (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETTICKCOUNT64)) || \ |
| 490 |
(defined(PSNIP_CLOCK_WALL_METHOD) && (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETTICKCOUNT64)) || \ |
| 491 |
(defined(PSNIP_CLOCK_MONOTONIC_METHOD) && (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETTICKCOUNT64)) |
| 492 |
# include <windows.h> |
| 493 |
#endif |
| 494 |
|
| 495 |
#if \ |
| 496 |
(defined(PSNIP_CLOCK_CPU_METHOD) && (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE)) || \ |
| 497 |
(defined(PSNIP_CLOCK_WALL_METHOD) && (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE)) || \ |
| 498 |
(defined(PSNIP_CLOCK_MONOTONIC_METHOD) && (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE)) |
| 499 |
# include <sys/time.h> |
| 500 |
# include <sys/resource.h> |
| 501 |
#endif |
| 502 |
|
| 503 |
#if \ |
| 504 |
(defined(PSNIP_CLOCK_CPU_METHOD) && (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME)) || \ |
| 505 |
(defined(PSNIP_CLOCK_WALL_METHOD) && (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME)) || \ |
| 506 |
(defined(PSNIP_CLOCK_MONOTONIC_METHOD) && (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME)) |
| 507 |
# include <CoreServices/CoreServices.h> |
| 508 |
# include <mach/mach.h> |
| 509 |
# include <mach/mach_time.h> |
| 510 |
#endif |
| 511 |
|
| 512 |
/*** Implementations ***/ |
| 513 |
|
| 514 |
#define PSNIP_CLOCK_NSEC_PER_SEC ((psnip_uint32_t) (1000000000ULL)) |
| 515 |
|
| 516 |
#if \ |
| 517 |
(defined(PSNIP_CLOCK_CPU_METHOD) && (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) || \ |
| 518 |
(defined(PSNIP_CLOCK_WALL_METHOD) && (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) || \ |
| 519 |
(defined(PSNIP_CLOCK_MONOTONIC_METHOD) && (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME)) |
| 520 |
PSNIP_CLOCK__FUNCTION psnip_uint32_t |
| 521 |
psnip_clock__clock_getres (clockid_t clk_id) { |
| 522 |
struct timespec res; |
| 523 |
int r; |
| 524 |
|
| 525 |
r = clock_getres(clk_id, &res); |
| 526 |
if (r != 0) |
| 527 |
return 0; |
| 528 |
|
| 529 |
return (psnip_uint32_t) (PSNIP_CLOCK_NSEC_PER_SEC / res.tv_nsec); |
| 530 |
} |
| 531 |
|
| 532 |
PSNIP_CLOCK__FUNCTION int |
| 533 |
psnip_clock__clock_gettime (clockid_t clk_id, struct PsnipClockTimespec* res) { |
| 534 |
struct timespec ts; |
| 535 |
|
| 536 |
if (clock_gettime(clk_id, &ts) != 0) |
| 537 |
return -10; |
| 538 |
|
| 539 |
res->seconds = (psnip_uint64_t) (ts.tv_sec); |
| 540 |
res->nanoseconds = (psnip_uint64_t) (ts.tv_nsec); |
| 541 |
|
| 542 |
return 0; |
| 543 |
} |
| 544 |
#endif |
| 545 |
|
| 546 |
PSNIP_CLOCK__FUNCTION psnip_uint32_t |
| 547 |
psnip_clock_wall_get_precision (void) { |
| 548 |
#if !defined(PSNIP_CLOCK_WALL_METHOD) |
| 549 |
return 0; |
| 550 |
#elif defined(PSNIP_CLOCK_WALL_METHOD) && PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 551 |
return psnip_clock__clock_getres(PSNIP_CLOCK_CLOCK_GETTIME_WALL); |
| 552 |
#elif defined(PSNIP_CLOCK_WALL_METHOD) && PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETTIMEOFDAY |
| 553 |
return 1000000; |
| 554 |
#elif defined(PSNIP_CLOCK_WALL_METHOD) && PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_TIME |
| 555 |
return 1; |
| 556 |
#else |
| 557 |
return 0; |
| 558 |
#endif |
| 559 |
} |
| 560 |
|
| 561 |
PSNIP_CLOCK__FUNCTION int |
| 562 |
psnip_clock_wall_get_time (struct PsnipClockTimespec* res) { |
| 563 |
(void) res; |
| 564 |
|
| 565 |
#if !defined(PSNIP_CLOCK_WALL_METHOD) |
| 566 |
return -2; |
| 567 |
#elif defined(PSNIP_CLOCK_WALL_METHOD) && PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 568 |
return psnip_clock__clock_gettime(PSNIP_CLOCK_CLOCK_GETTIME_WALL, res); |
| 569 |
#elif defined(PSNIP_CLOCK_WALL_METHOD) && PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_TIME |
| 570 |
res->seconds = time(NULL); |
| 571 |
res->nanoseconds = 0; |
| 572 |
#elif defined(PSNIP_CLOCK_WALL_METHOD) && PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETTIMEOFDAY |
| 573 |
struct timeval tv; |
| 574 |
|
| 575 |
if (gettimeofday(&tv, NULL) != 0) |
| 576 |
return -6; |
| 577 |
|
| 578 |
res->seconds = tv.tv_sec; |
| 579 |
res->nanoseconds = tv.tv_usec * 1000; |
| 580 |
#else |
| 581 |
return -2; |
| 582 |
#endif |
| 583 |
|
| 584 |
return 0; |
| 585 |
} |
| 586 |
|
| 587 |
PSNIP_CLOCK__FUNCTION psnip_uint32_t |
| 588 |
psnip_clock_cpu_get_precision (void) { |
| 589 |
#if !defined(PSNIP_CLOCK_CPU_METHOD) |
| 590 |
return 0; |
| 591 |
#elif defined(PSNIP_CLOCK_CPU_METHOD) && PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 592 |
return psnip_clock__clock_getres(PSNIP_CLOCK_CLOCK_GETTIME_CPU); |
| 593 |
#elif defined(PSNIP_CLOCK_CPU_METHOD) && PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK |
| 594 |
return CLOCKS_PER_SEC; |
| 595 |
#elif defined(PSNIP_CLOCK_CPU_METHOD) && PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETPROCESSTIMES |
| 596 |
return PSNIP_CLOCK_NSEC_PER_SEC / 100; |
| 597 |
#else |
| 598 |
return 0; |
| 599 |
#endif |
| 600 |
} |
| 601 |
|
| 602 |
PSNIP_CLOCK__FUNCTION int |
| 603 |
psnip_clock_cpu_get_time (struct PsnipClockTimespec* res) { |
| 604 |
#if !defined(PSNIP_CLOCK_CPU_METHOD) |
| 605 |
(void) res; |
| 606 |
return -2; |
| 607 |
#elif defined(PSNIP_CLOCK_CPU_METHOD) && PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 608 |
return psnip_clock__clock_gettime(PSNIP_CLOCK_CLOCK_GETTIME_CPU, res); |
| 609 |
#elif defined(PSNIP_CLOCK_CPU_METHOD) && PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_CLOCK |
| 610 |
clock_t t = clock(); |
| 611 |
if (t == ((clock_t) -1)) |
| 612 |
return -5; |
| 613 |
res->seconds = t / CLOCKS_PER_SEC; |
| 614 |
res->nanoseconds = (t % CLOCKS_PER_SEC) * (PSNIP_CLOCK_NSEC_PER_SEC / CLOCKS_PER_SEC); |
| 615 |
#elif defined(PSNIP_CLOCK_CPU_METHOD) && PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETPROCESSTIMES |
| 616 |
FILETIME CreationTime, ExitTime, KernelTime, UserTime; |
| 617 |
LARGE_INTEGER date, adjust; |
| 618 |
|
| 619 |
if (!GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime, &KernelTime, &UserTime)) |
| 620 |
return -7; |
| 621 |
|
| 622 |
/* http://www.frenk.com/2009/12/convert-filetime-to-unix-timestamp/ */ |
| 623 |
date.HighPart = UserTime.dwHighDateTime; |
| 624 |
date.LowPart = UserTime.dwLowDateTime; |
| 625 |
adjust.QuadPart = 11644473600000 * 10000; |
| 626 |
date.QuadPart -= adjust.QuadPart; |
| 627 |
|
| 628 |
res->seconds = date.QuadPart / 10000000; |
| 629 |
res->nanoseconds = (date.QuadPart % 10000000) * (PSNIP_CLOCK_NSEC_PER_SEC / 100); |
| 630 |
#elif PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE |
| 631 |
struct rusage usage; |
| 632 |
if (getrusage(RUSAGE_SELF, &usage) != 0) |
| 633 |
return -8; |
| 634 |
|
| 635 |
res->seconds = usage.ru_utime.tv_sec; |
| 636 |
res->nanoseconds = tv.tv_usec * 1000; |
| 637 |
#else |
| 638 |
(void) res; |
| 639 |
return -2; |
| 640 |
#endif |
| 641 |
|
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
PSNIP_CLOCK__FUNCTION psnip_uint32_t |
| 646 |
psnip_clock_monotonic_get_precision (void) { |
| 647 |
#if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) |
| 648 |
return 0; |
| 649 |
#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 650 |
return psnip_clock__clock_getres(PSNIP_CLOCK_CLOCK_GETTIME_MONOTONIC); |
| 651 |
#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME |
| 652 |
static mach_timebase_info_data_t tbi = { 0, }; |
| 653 |
if (tbi.denom == 0) |
| 654 |
mach_timebase_info(&tbi); |
| 655 |
return (psnip_uint32_t) (tbi.numer / tbi.denom); |
| 656 |
#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETTICKCOUNT64 |
| 657 |
return 1000; |
| 658 |
#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_QUERYPERFORMANCECOUNTER |
| 659 |
LARGE_INTEGER Frequency; |
| 660 |
QueryPerformanceFrequency(&Frequency); |
| 661 |
return (psnip_uint32_t) ((Frequency.QuadPart > PSNIP_CLOCK_NSEC_PER_SEC) ? PSNIP_CLOCK_NSEC_PER_SEC : Frequency.QuadPart); |
| 662 |
#else |
| 663 |
return 0; |
| 664 |
#endif |
| 665 |
} |
| 666 |
|
| 667 |
PSNIP_CLOCK__FUNCTION int |
| 668 |
psnip_clock_monotonic_get_time (struct PsnipClockTimespec* res) { |
| 669 |
#if !defined(PSNIP_CLOCK_MONOTONIC_METHOD) |
| 670 |
(void) res; |
| 671 |
return -2; |
| 672 |
#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_CLOCK_GETTIME |
| 673 |
return psnip_clock__clock_gettime(PSNIP_CLOCK_CLOCK_GETTIME_MONOTONIC, res); |
| 674 |
#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_MACH_ABSOLUTE_TIME |
| 675 |
psnip_uint64_t nsec = mach_absolute_time(); |
| 676 |
static mach_timebase_info_data_t tbi = { 0, }; |
| 677 |
if (tbi.denom == 0) |
| 678 |
mach_timebase_info(&tbi); |
| 679 |
nsec *= ((psnip_uint64_t) tbi.numer) / ((psnip_uint64_t) tbi.denom); |
| 680 |
res->seconds = nsec / PSNIP_CLOCK_NSEC_PER_SEC; |
| 681 |
res->nanoseconds = nsec % PSNIP_CLOCK_NSEC_PER_SEC; |
| 682 |
#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_QUERYPERFORMANCECOUNTER |
| 683 |
LARGE_INTEGER t, f; |
| 684 |
if (QueryPerformanceCounter(&t) == 0) |
| 685 |
return -12; |
| 686 |
|
| 687 |
QueryPerformanceFrequency(&f); |
| 688 |
res->seconds = t.QuadPart / f.QuadPart; |
| 689 |
res->nanoseconds = t.QuadPart % f.QuadPart; |
| 690 |
if (f.QuadPart > PSNIP_CLOCK_NSEC_PER_SEC) |
| 691 |
res->nanoseconds /= f.QuadPart / PSNIP_CLOCK_NSEC_PER_SEC; |
| 692 |
else |
| 693 |
res->nanoseconds *= PSNIP_CLOCK_NSEC_PER_SEC / f.QuadPart; |
| 694 |
#elif defined(PSNIP_CLOCK_MONOTONIC_METHOD) && PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETTICKCOUNT64 |
| 695 |
const ULONGLONG msec = GetTickCount64(); |
| 696 |
res->seconds = msec / 1000; |
| 697 |
res->nanoseconds = sec % 1000; |
| 698 |
#else |
| 699 |
return -2; |
| 700 |
#endif |
| 701 |
|
| 702 |
return 0; |
| 703 |
} |
| 704 |
|
| 705 |
/* Returns the number of ticks per second for the specified clock. |
| 706 |
* For example, a clock with millisecond precision would return 1000, |
| 707 |
* and a clock with 1 second (such as the time() function) would |
| 708 |
* return 1. |
| 709 |
* |
| 710 |
* If the requested clock isn't available, it will return 0. |
| 711 |
* Hopefully this will be rare, but if it happens to you please let us |
| 712 |
* know so we can work on finding a way to support your system. |
| 713 |
* |
| 714 |
* Note that different clocks on the same system often have a |
| 715 |
* different precisions. |
| 716 |
*/ |
| 717 |
PSNIP_CLOCK__FUNCTION psnip_uint32_t |
| 718 |
psnip_clock_get_precision (enum PsnipClockType clock_type) { |
| 719 |
switch (clock_type) { |
| 720 |
case PSNIP_CLOCK_TYPE_MONOTONIC: |
| 721 |
return psnip_clock_monotonic_get_precision (); |
| 722 |
case PSNIP_CLOCK_TYPE_CPU: |
| 723 |
return psnip_clock_cpu_get_precision (); |
| 724 |
case PSNIP_CLOCK_TYPE_WALL: |
| 725 |
return psnip_clock_wall_get_precision (); |
| 726 |
} |
| 727 |
|
| 728 |
PSNIP_CLOCK_UNREACHABLE(); |
| 729 |
return 0; |
| 730 |
} |
| 731 |
|
| 732 |
/* Set the provided timespec to the requested time. Returns 0 on |
| 733 |
* success, or a negative value on failure. */ |
| 734 |
PSNIP_CLOCK__FUNCTION int |
| 735 |
psnip_clock_get_time (enum PsnipClockType clock_type, struct PsnipClockTimespec* res) { |
| 736 |
assert(res != NULL); |
| 737 |
|
| 738 |
switch (clock_type) { |
| 739 |
case PSNIP_CLOCK_TYPE_MONOTONIC: |
| 740 |
return psnip_clock_monotonic_get_time (res); |
| 741 |
case PSNIP_CLOCK_TYPE_CPU: |
| 742 |
return psnip_clock_cpu_get_time (res); |
| 743 |
case PSNIP_CLOCK_TYPE_WALL: |
| 744 |
return psnip_clock_wall_get_time (res); |
| 745 |
} |
| 746 |
|
| 747 |
return -1; |
| 748 |
} |
| 749 |
|
| 750 |
#endif /* !defined(PSNIP_CLOCK_H) */ |
| 751 |
|
| 752 |
static psnip_uint64_t |
| 753 |
munit_clock_get_elapsed(struct PsnipClockTimespec* start, struct PsnipClockTimespec* end) { |
| 754 |
psnip_uint64_t r = (end->seconds - start->seconds) * PSNIP_CLOCK_NSEC_PER_SEC; |
| 755 |
if (end->nanoseconds < start->nanoseconds) { |
| 756 |
r -= (start->nanoseconds - end->nanoseconds); |
| 757 |
} else { |
| 758 |
r += (end->nanoseconds - start->nanoseconds); |
| 759 |
} |
| 760 |
return r; |
| 761 |
} |
| 762 |
|
| 763 |
#else |
| 764 |
# include <time.h> |
| 765 |
#endif /* defined(MUNIT_ENABLE_TIMING) */ |
| 766 |
|
| 767 |
/*** PRNG stuff ***/ |
| 768 |
|
| 769 |
/* This is (unless I screwed up, which is entirely possible) the |
| 770 |
* version of PCG with 32-bit state. It was chosen because it has a |
| 771 |
* small enough state that we should reliably be able to use CAS |
| 772 |
* instead of requiring a lock for thread-safety. |
| 773 |
* |
| 774 |
* If I did screw up, I probably will not bother changing it unless |
| 775 |
* there is a significant bias. It's really not important this be |
| 776 |
* particularly strong, as long as it is fairly random it's much more |
| 777 |
* important that it be reproducible, so bug reports have a better |
| 778 |
* chance of being reproducible. */ |
| 779 |
|
| 780 |
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__) && !defined(__EMSCRIPTEN__) && (!defined(__GNUC_MINOR__) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 8)) |
| 781 |
# define HAVE_STDATOMIC |
| 782 |
#elif defined(__clang__) |
| 783 |
# if __has_extension(c_atomic) |
| 784 |
# define HAVE_CLANG_ATOMICS |
| 785 |
# endif |
| 786 |
#endif |
| 787 |
|
| 788 |
/* Workaround for http://llvm.org/bugs/show_bug.cgi?id=26911 */ |
| 789 |
#if defined(__clang__) && defined(_WIN32) |
| 790 |
# undef HAVE_STDATOMIC |
| 791 |
# if defined(__c2__) |
| 792 |
# undef HAVE_CLANG_ATOMICS |
| 793 |
# endif |
| 794 |
#endif |
| 795 |
|
| 796 |
#if defined(_OPENMP) |
| 797 |
# define ATOMIC_UINT32_T uint32_t |
| 798 |
# define ATOMIC_UINT32_INIT(x) (x) |
| 799 |
#elif defined(HAVE_STDATOMIC) |
| 800 |
# include <stdatomic.h> |
| 801 |
# define ATOMIC_UINT32_T _Atomic uint32_t |
| 802 |
# define ATOMIC_UINT32_INIT(x) ATOMIC_VAR_INIT(x) |
| 803 |
#elif defined(HAVE_CLANG_ATOMICS) |
| 804 |
# define ATOMIC_UINT32_T _Atomic uint32_t |
| 805 |
# define ATOMIC_UINT32_INIT(x) (x) |
| 806 |
#elif defined(_WIN32) |
| 807 |
# define ATOMIC_UINT32_T volatile LONG |
| 808 |
# define ATOMIC_UINT32_INIT(x) (x) |
| 809 |
#else |
| 810 |
# define ATOMIC_UINT32_T volatile uint32_t |
| 811 |
# define ATOMIC_UINT32_INIT(x) (x) |
| 812 |
#endif |
| 813 |
|
| 814 |
static ATOMIC_UINT32_T munit_rand_state = ATOMIC_UINT32_INIT(42); |
| 815 |
|
| 816 |
#if defined(_OPENMP) |
| 817 |
static inline void |
| 818 |
munit_atomic_store(ATOMIC_UINT32_T* dest, ATOMIC_UINT32_T value) { |
| 819 |
#pragma omp critical (munit_atomics) |
| 820 |
*dest = value; |
| 821 |
} |
| 822 |
|
| 823 |
static inline uint32_t |
| 824 |
munit_atomic_load(ATOMIC_UINT32_T* src) { |
| 825 |
int ret; |
| 826 |
#pragma omp critical (munit_atomics) |
| 827 |
ret = *src; |
| 828 |
return ret; |
| 829 |
} |
| 830 |
|
| 831 |
static inline uint32_t |
| 832 |
munit_atomic_cas(ATOMIC_UINT32_T* dest, ATOMIC_UINT32_T* expected, ATOMIC_UINT32_T desired) { |
| 833 |
bool ret; |
| 834 |
|
| 835 |
#pragma omp critical (munit_atomics) |
| 836 |
{ |
| 837 |
if (*dest == *expected) { |
| 838 |
*dest = desired; |
| 839 |
ret = true; |
| 840 |
} else { |
| 841 |
ret = false; |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
return ret; |
| 846 |
} |
| 847 |
#elif defined(HAVE_STDATOMIC) |
| 848 |
# define munit_atomic_store(dest, value) atomic_store(dest, value) |
| 849 |
# define munit_atomic_load(src) atomic_load(src) |
| 850 |
# define munit_atomic_cas(dest, expected, value) atomic_compare_exchange_weak(dest, expected, value) |
| 851 |
#elif defined(HAVE_CLANG_ATOMICS) |
| 852 |
# define munit_atomic_store(dest, value) __c11_atomic_store(dest, value, __ATOMIC_SEQ_CST) |
| 853 |
# define munit_atomic_load(src) __c11_atomic_load(src, __ATOMIC_SEQ_CST) |
| 854 |
# define munit_atomic_cas(dest, expected, value) __c11_atomic_compare_exchange_weak(dest, expected, value, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) |
| 855 |
#elif defined(__GNUC__) && (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) |
| 856 |
# define munit_atomic_store(dest, value) __atomic_store_n(dest, value, __ATOMIC_SEQ_CST) |
| 857 |
# define munit_atomic_load(src) __atomic_load_n(src, __ATOMIC_SEQ_CST) |
| 858 |
# define munit_atomic_cas(dest, expected, value) __atomic_compare_exchange_n(dest, expected, value, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) |
| 859 |
#elif defined(__GNUC__) && (__GNUC__ >= 4) |
| 860 |
# define munit_atomic_store(dest,value) do { *(dest) = (value); } while (0) |
| 861 |
# define munit_atomic_load(src) (*(src)) |
| 862 |
# define munit_atomic_cas(dest, expected, value) __sync_bool_compare_and_swap(dest, *expected, value) |
| 863 |
#elif defined(_WIN32) /* Untested */ |
| 864 |
# define munit_atomic_store(dest,value) do { *(dest) = (value); } while (0) |
| 865 |
# define munit_atomic_load(src) (*(src)) |
| 866 |
# define munit_atomic_cas(dest, expected, value) InterlockedCompareExchange((dest), (value), *(expected)) |
| 867 |
#else |
| 868 |
# warning No atomic implementation, PRNG will not be thread-safe |
| 869 |
# define munit_atomic_store(dest, value) do { *(dest) = (value); } while (0) |
| 870 |
# define munit_atomic_load(src) (*(src)) |
| 871 |
static inline bool |
| 872 |
munit_atomic_cas(ATOMIC_UINT32_T* dest, ATOMIC_UINT32_T* expected, ATOMIC_UINT32_T desired) { |
| 873 |
if (*dest == *expected) { |
| 874 |
*dest = desired; |
| 875 |
return true; |
| 876 |
} else { |
| 877 |
return false; |
| 878 |
} |
| 879 |
} |
| 880 |
#endif |
| 881 |
|
| 882 |
#define MUNIT_PRNG_MULTIPLIER (747796405U) |
| 883 |
#define MUNIT_PRNG_INCREMENT (1729U) |
| 884 |
|
| 885 |
static munit_uint32_t |
| 886 |
munit_rand_next_state(munit_uint32_t state) { |
| 887 |
return state * MUNIT_PRNG_MULTIPLIER + MUNIT_PRNG_INCREMENT; |
| 888 |
} |
| 889 |
|
| 890 |
static munit_uint32_t |
| 891 |
munit_rand_from_state(munit_uint32_t state) { |
| 892 |
munit_uint32_t res = ((state >> ((state >> 28) + 4)) ^ state) * (277803737U); |
| 893 |
res ^= res >> 22; |
| 894 |
return res; |
| 895 |
} |
| 896 |
|
| 897 |
void |
| 898 |
munit_rand_seed(munit_uint32_t seed) { |
| 899 |
munit_uint32_t state = munit_rand_next_state(seed + MUNIT_PRNG_INCREMENT); |
| 900 |
munit_atomic_store(&munit_rand_state, state); |
| 901 |
} |
| 902 |
|
| 903 |
static munit_uint32_t |
| 904 |
munit_rand_generate_seed(void) { |
| 905 |
munit_uint32_t seed, state; |
| 906 |
#if defined(MUNIT_ENABLE_TIMING) |
| 907 |
struct PsnipClockTimespec wc = { 0, 0 }; |
| 908 |
|
| 909 |
psnip_clock_get_time(PSNIP_CLOCK_TYPE_WALL, &wc); |
| 910 |
seed = (munit_uint32_t) wc.nanoseconds; |
| 911 |
#else |
| 912 |
seed = (munit_uint32_t) time(NULL); |
| 913 |
#endif |
| 914 |
|
| 915 |
state = munit_rand_next_state(seed + MUNIT_PRNG_INCREMENT); |
| 916 |
return munit_rand_from_state(state); |
| 917 |
} |
| 918 |
|
| 919 |
static munit_uint32_t |
| 920 |
munit_rand_state_uint32(munit_uint32_t* state) { |
| 921 |
const munit_uint32_t old = *state; |
| 922 |
*state = munit_rand_next_state(old); |
| 923 |
return munit_rand_from_state(old); |
| 924 |
} |
| 925 |
|
| 926 |
munit_uint32_t |
| 927 |
munit_rand_uint32(void) { |
| 928 |
munit_uint32_t old, state; |
| 929 |
|
| 930 |
do { |
| 931 |
old = munit_atomic_load(&munit_rand_state); |
| 932 |
state = munit_rand_next_state(old); |
| 933 |
} while (!munit_atomic_cas(&munit_rand_state, &old, state)); |
| 934 |
|
| 935 |
return munit_rand_from_state(old); |
| 936 |
} |
| 937 |
|
| 938 |
static void |
| 939 |
munit_rand_state_memory(munit_uint32_t* state, size_t size, munit_uint8_t data[MUNIT_ARRAY_PARAM(size)]) { |
| 940 |
size_t members_remaining = size / sizeof(munit_uint32_t); |
| 941 |
size_t bytes_remaining = size % sizeof(munit_uint32_t); |
| 942 |
munit_uint8_t* b = data; |
| 943 |
munit_uint32_t rv; |
| 944 |
while (members_remaining-- > 0) { |
| 945 |
rv = munit_rand_state_uint32(state); |
| 946 |
memcpy(b, &rv, sizeof(munit_uint32_t)); |
| 947 |
b += sizeof(munit_uint32_t); |
| 948 |
} |
| 949 |
if (bytes_remaining != 0) { |
| 950 |
rv = munit_rand_state_uint32(state); |
| 951 |
memcpy(b, &rv, bytes_remaining); |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
void |
| 956 |
munit_rand_memory(size_t size, munit_uint8_t data[MUNIT_ARRAY_PARAM(size)]) { |
| 957 |
munit_uint32_t old, state; |
| 958 |
|
| 959 |
do { |
| 960 |
state = old = munit_atomic_load(&munit_rand_state); |
| 961 |
munit_rand_state_memory(&state, size, data); |
| 962 |
} while (!munit_atomic_cas(&munit_rand_state, &old, state)); |
| 963 |
} |
| 964 |
|
| 965 |
static munit_uint32_t |
| 966 |
munit_rand_state_at_most(munit_uint32_t* state, munit_uint32_t salt, munit_uint32_t max) { |
| 967 |
/* We want (UINT32_MAX + 1) % max, which in unsigned arithmetic is the same |
| 968 |
* as (UINT32_MAX + 1 - max) % max = -max % max. We compute -max using not |
| 969 |
* to avoid compiler warnings. |
| 970 |
*/ |
| 971 |
const munit_uint32_t min = (~max + 1U) % max; |
| 972 |
munit_uint32_t x; |
| 973 |
|
| 974 |
if (max == (~((munit_uint32_t) 0U))) |
| 975 |
return munit_rand_state_uint32(state) ^ salt; |
| 976 |
|
| 977 |
max++; |
| 978 |
|
| 979 |
do { |
| 980 |
x = munit_rand_state_uint32(state) ^ salt; |
| 981 |
} while (x < min); |
| 982 |
|
| 983 |
return x % max; |
| 984 |
} |
| 985 |
|
| 986 |
static munit_uint32_t |
| 987 |
munit_rand_at_most(munit_uint32_t salt, munit_uint32_t max) { |
| 988 |
munit_uint32_t old, state; |
| 989 |
munit_uint32_t retval; |
| 990 |
|
| 991 |
do { |
| 992 |
state = old = munit_atomic_load(&munit_rand_state); |
| 993 |
retval = munit_rand_state_at_most(&state, salt, max); |
| 994 |
} while (!munit_atomic_cas(&munit_rand_state, &old, state)); |
| 995 |
|
| 996 |
return retval; |
| 997 |
} |
| 998 |
|
| 999 |
int |
| 1000 |
munit_rand_int_range(int min, int max) { |
| 1001 |
munit_uint64_t range = (munit_uint64_t) max - (munit_uint64_t) min; |
| 1002 |
|
| 1003 |
if (min > max) |
| 1004 |
return munit_rand_int_range(max, min); |
| 1005 |
|
| 1006 |
if (range > (~((munit_uint32_t) 0U))) |
| 1007 |
range = (~((munit_uint32_t) 0U)); |
| 1008 |
|
| 1009 |
return min + munit_rand_at_most(0, (munit_uint32_t) range); |
| 1010 |
} |
| 1011 |
|
| 1012 |
double |
| 1013 |
munit_rand_double(void) { |
| 1014 |
munit_uint32_t old, state; |
| 1015 |
double retval = 0.0; |
| 1016 |
|
| 1017 |
do { |
| 1018 |
state = old = munit_atomic_load(&munit_rand_state); |
| 1019 |
|
| 1020 |
/* See http://mumble.net/~campbell/tmp/random_real.c for how to do |
| 1021 |
* this right. Patches welcome if you feel that this is too |
| 1022 |
* biased. */ |
| 1023 |
retval = munit_rand_state_uint32(&state) / ((~((munit_uint32_t) 0U)) + 1.0); |
| 1024 |
} while (!munit_atomic_cas(&munit_rand_state, &old, state)); |
| 1025 |
|
| 1026 |
return retval; |
| 1027 |
} |
| 1028 |
|
| 1029 |
/*** Test suite handling ***/ |
| 1030 |
|
| 1031 |
typedef struct { |
| 1032 |
unsigned int successful; |
| 1033 |
unsigned int skipped; |
| 1034 |
unsigned int failed; |
| 1035 |
unsigned int errored; |
| 1036 |
#if defined(MUNIT_ENABLE_TIMING) |
| 1037 |
munit_uint64_t cpu_clock; |
| 1038 |
munit_uint64_t wall_clock; |
| 1039 |
#endif |
| 1040 |
} MunitReport; |
| 1041 |
|
| 1042 |
typedef struct { |
| 1043 |
const char* prefix; |
| 1044 |
const MunitSuite* suite; |
| 1045 |
const char** tests; |
| 1046 |
munit_uint32_t seed; |
| 1047 |
unsigned int iterations; |
| 1048 |
MunitParameter* parameters; |
| 1049 |
bool single_parameter_mode; |
| 1050 |
void* user_data; |
| 1051 |
MunitReport report; |
| 1052 |
bool colorize; |
| 1053 |
bool fork; |
| 1054 |
bool show_stderr; |
| 1055 |
bool fatal_failures; |
| 1056 |
} MunitTestRunner; |
| 1057 |
|
| 1058 |
const char* |
| 1059 |
munit_parameters_get(const MunitParameter params[], const char* key) { |
| 1060 |
const MunitParameter* param; |
| 1061 |
|
| 1062 |
for (param = params ; param != NULL && param->name != NULL ; param++) |
| 1063 |
if (strcmp(param->name, key) == 0) |
| 1064 |
return param->value; |
| 1065 |
return NULL; |
| 1066 |
} |
| 1067 |
|
| 1068 |
#if defined(MUNIT_ENABLE_TIMING) |
| 1069 |
static void |
| 1070 |
munit_print_time(FILE* fp, munit_uint64_t nanoseconds) { |
| 1071 |
fprintf(fp, "%" MUNIT_TEST_TIME_FORMAT, ((double) nanoseconds) / ((double) PSNIP_CLOCK_NSEC_PER_SEC)); |
| 1072 |
} |
| 1073 |
#endif |
| 1074 |
|
| 1075 |
/* Add a paramter to an array of parameters. */ |
| 1076 |
static MunitResult |
| 1077 |
munit_parameters_add(size_t* params_size, MunitParameter* params[MUNIT_ARRAY_PARAM(*params_size)], char* name, char* value) { |
| 1078 |
*params = realloc(*params, sizeof(MunitParameter) * (*params_size + 2)); |
| 1079 |
if (*params == NULL) |
| 1080 |
return MUNIT_ERROR; |
| 1081 |
|
| 1082 |
(*params)[*params_size].name = name; |
| 1083 |
(*params)[*params_size].value = value; |
| 1084 |
(*params_size)++; |
| 1085 |
(*params)[*params_size].name = NULL; |
| 1086 |
(*params)[*params_size].value = NULL; |
| 1087 |
|
| 1088 |
return MUNIT_OK; |
| 1089 |
} |
| 1090 |
|
| 1091 |
/* Concatenate two strings, but just return one of the components |
| 1092 |
* unaltered if the other is NULL or "". */ |
| 1093 |
static char* |
| 1094 |
munit_maybe_concat(size_t* len, char* prefix, char* suffix) { |
| 1095 |
char* res; |
| 1096 |
size_t res_l; |
| 1097 |
const size_t prefix_l = prefix != NULL ? strlen(prefix) : 0; |
| 1098 |
const size_t suffix_l = suffix != NULL ? strlen(suffix) : 0; |
| 1099 |
if (prefix_l == 0 && suffix_l == 0) { |
| 1100 |
res = NULL; |
| 1101 |
res_l = 0; |
| 1102 |
} else if (prefix_l == 0 && suffix_l != 0) { |
| 1103 |
res = suffix; |
| 1104 |
res_l = suffix_l; |
| 1105 |
} else if (prefix_l != 0 && suffix_l == 0) { |
| 1106 |
res = prefix; |
| 1107 |
res_l = prefix_l; |
| 1108 |
} else { |
| 1109 |
res_l = prefix_l + suffix_l; |
| 1110 |
res = malloc(res_l + 1); |
| 1111 |
memcpy(res, prefix, prefix_l); |
| 1112 |
memcpy(res + prefix_l, suffix, suffix_l); |
| 1113 |
res[res_l] = 0; |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (len != NULL) |
| 1117 |
*len = res_l; |
| 1118 |
|
| 1119 |
return res; |
| 1120 |
} |
| 1121 |
|
| 1122 |
/* Possbily free a string returned by munit_maybe_concat. */ |
| 1123 |
static void |
| 1124 |
munit_maybe_free_concat(char* s, const char* prefix, const char* suffix) { |
| 1125 |
if (prefix != s && suffix != s) |
| 1126 |
free(s); |
| 1127 |
} |
| 1128 |
|
| 1129 |
/* Cheap string hash function, just used to salt the PRNG. */ |
| 1130 |
static munit_uint32_t |
| 1131 |
munit_str_hash(const char* name) { |
| 1132 |
const char *p; |
| 1133 |
munit_uint32_t h = 5381U; |
| 1134 |
|
| 1135 |
for (p = name; *p != '\0'; p++) |
| 1136 |
h = (h << 5) + h + *p; |
| 1137 |
|
| 1138 |
return h; |
| 1139 |
} |
| 1140 |
|
| 1141 |
static void |
| 1142 |
munit_splice(int from, int to) { |
| 1143 |
munit_uint8_t buf[1024]; |
| 1144 |
#if !defined(_WIN32) |
| 1145 |
ssize_t len; |
| 1146 |
ssize_t bytes_written; |
| 1147 |
ssize_t write_res; |
| 1148 |
#else |
| 1149 |
int len; |
| 1150 |
int bytes_written; |
| 1151 |
int write_res; |
| 1152 |
#endif |
| 1153 |
do { |
| 1154 |
len = read(from, buf, sizeof(buf)); |
| 1155 |
if (len > 0) { |
| 1156 |
bytes_written = 0; |
| 1157 |
do { |
| 1158 |
write_res = write(to, buf + bytes_written, len - bytes_written); |
| 1159 |
if (write_res < 0) |
| 1160 |
break; |
| 1161 |
bytes_written += write_res; |
| 1162 |
} while (bytes_written < len); |
| 1163 |
} |
| 1164 |
else |
| 1165 |
break; |
| 1166 |
} while (true); |
| 1167 |
} |
| 1168 |
|
| 1169 |
/* This is the part that should be handled in the child process */ |
| 1170 |
static MunitResult |
| 1171 |
munit_test_runner_exec(MunitTestRunner* runner, const MunitTest* test, const MunitParameter params[], MunitReport* report) { |
| 1172 |
unsigned int iterations = runner->iterations; |
| 1173 |
MunitResult result = MUNIT_FAIL; |
| 1174 |
#if defined(MUNIT_ENABLE_TIMING) |
| 1175 |
struct PsnipClockTimespec wall_clock_begin = { 0, 0 }, wall_clock_end = { 0, 0 }; |
| 1176 |
struct PsnipClockTimespec cpu_clock_begin = { 0, 0 }, cpu_clock_end = { 0, 0 }; |
| 1177 |
#endif |
| 1178 |
unsigned int i = 0; |
| 1179 |
|
| 1180 |
if ((test->options & MUNIT_TEST_OPTION_SINGLE_ITERATION) == MUNIT_TEST_OPTION_SINGLE_ITERATION) |
| 1181 |
iterations = 1; |
| 1182 |
else if (iterations == 0) |
| 1183 |
iterations = runner->suite->iterations; |
| 1184 |
|
| 1185 |
munit_rand_seed(runner->seed); |
| 1186 |
|
| 1187 |
do { |
| 1188 |
void* data = (test->setup == NULL) ? runner->user_data : test->setup(params, runner->user_data); |
| 1189 |
|
| 1190 |
#if defined(MUNIT_ENABLE_TIMING) |
| 1191 |
psnip_clock_get_time(PSNIP_CLOCK_TYPE_WALL, &wall_clock_begin); |
| 1192 |
psnip_clock_get_time(PSNIP_CLOCK_TYPE_CPU, &cpu_clock_begin); |
| 1193 |
#endif |
| 1194 |
|
| 1195 |
result = test->test(params, data); |
| 1196 |
|
| 1197 |
#if defined(MUNIT_ENABLE_TIMING) |
| 1198 |
psnip_clock_get_time(PSNIP_CLOCK_TYPE_WALL, &wall_clock_end); |
| 1199 |
psnip_clock_get_time(PSNIP_CLOCK_TYPE_CPU, &cpu_clock_end); |
| 1200 |
#endif |
| 1201 |
|
| 1202 |
if (test->tear_down != NULL) |
| 1203 |
test->tear_down(data); |
| 1204 |
|
| 1205 |
if (MUNIT_LIKELY(result == MUNIT_OK)) { |
| 1206 |
report->successful++; |
| 1207 |
#if defined(MUNIT_ENABLE_TIMING) |
| 1208 |
report->wall_clock += munit_clock_get_elapsed(&wall_clock_begin, &wall_clock_end); |
| 1209 |
report->cpu_clock += munit_clock_get_elapsed(&cpu_clock_begin, &cpu_clock_end); |
| 1210 |
#endif |
| 1211 |
} else { |
| 1212 |
switch ((int) result) { |
| 1213 |
case MUNIT_SKIP: |
| 1214 |
report->skipped++; |
| 1215 |
break; |
| 1216 |
case MUNIT_FAIL: |
| 1217 |
report->failed++; |
| 1218 |
break; |
| 1219 |
case MUNIT_ERROR: |
| 1220 |
report->errored++; |
| 1221 |
break; |
| 1222 |
default: |
| 1223 |
break; |
| 1224 |
} |
| 1225 |
break; |
| 1226 |
} |
| 1227 |
} while (++i < iterations); |
| 1228 |
|
| 1229 |
return result; |
| 1230 |
} |
| 1231 |
|
| 1232 |
#if defined(MUNIT_EMOTICON) |
| 1233 |
# define MUNIT_RESULT_STRING_OK ":)" |
| 1234 |
# define MUNIT_RESULT_STRING_SKIP ":|" |
| 1235 |
# define MUNIT_RESULT_STRING_FAIL ":(" |
| 1236 |
# define MUNIT_RESULT_STRING_ERROR ":o" |
| 1237 |
# define MUNIT_RESULT_STRING_TODO ":/" |
| 1238 |
#else |
| 1239 |
# define MUNIT_RESULT_STRING_OK "OK " |
| 1240 |
# define MUNIT_RESULT_STRING_SKIP "SKIP " |
| 1241 |
# define MUNIT_RESULT_STRING_FAIL "FAIL " |
| 1242 |
# define MUNIT_RESULT_STRING_ERROR "ERROR" |
| 1243 |
# define MUNIT_RESULT_STRING_TODO "TODO " |
| 1244 |
#endif |
| 1245 |
|
| 1246 |
static void |
| 1247 |
munit_test_runner_print_color(const MunitTestRunner* runner, const char* string, char color) { |
| 1248 |
if (runner->colorize) |
| 1249 |
fprintf(MUNIT_OUTPUT_FILE, "\x1b[3%cm%s\x1b[39m", color, string); |
| 1250 |
else |
| 1251 |
fputs(string, MUNIT_OUTPUT_FILE); |
| 1252 |
} |
| 1253 |
|
| 1254 |
#if !defined(MUNIT_NO_BUFFER) |
| 1255 |
static int |
| 1256 |
munit_replace_stderr(FILE* stderr_buf) { |
| 1257 |
if (stderr_buf != NULL) { |
| 1258 |
const int orig_stderr = dup(STDERR_FILENO); |
| 1259 |
|
| 1260 |
int errfd = fileno(stderr_buf); |
| 1261 |
if (MUNIT_UNLIKELY(errfd == -1)) { |
| 1262 |
exit(EXIT_FAILURE); |
| 1263 |
} |
| 1264 |
|
| 1265 |
dup2(errfd, STDERR_FILENO); |
| 1266 |
|
| 1267 |
return orig_stderr; |
| 1268 |
} |
| 1269 |
|
| 1270 |
return -1; |
| 1271 |
} |
| 1272 |
|
| 1273 |
static void |
| 1274 |
munit_restore_stderr(int orig_stderr) { |
| 1275 |
if (orig_stderr != -1) { |
| 1276 |
dup2(orig_stderr, STDERR_FILENO); |
| 1277 |
close(orig_stderr); |
| 1278 |
} |
| 1279 |
} |
| 1280 |
#endif /* !defined(MUNIT_NO_BUFFER) */ |
| 1281 |
|
| 1282 |
/* Run a test with the specified parameters. */ |
| 1283 |
static void |
| 1284 |
munit_test_runner_run_test_with_params(MunitTestRunner* runner, const MunitTest* test, const MunitParameter params[]) { |
| 1285 |
MunitResult result = MUNIT_OK; |
| 1286 |
MunitReport report = { |
| 1287 |
0, 0, 0, 0, |
| 1288 |
#if defined(MUNIT_ENABLE_TIMING) |
| 1289 |
0, 0 |
| 1290 |
#endif |
| 1291 |
}; |
| 1292 |
unsigned int output_l; |
| 1293 |
bool first; |
| 1294 |
const MunitParameter* param; |
| 1295 |
FILE* stderr_buf; |
| 1296 |
#if !defined(MUNIT_NO_FORK) |
| 1297 |
int pipefd[2]; |
| 1298 |
pid_t fork_pid; |
| 1299 |
ssize_t bytes_written = 0; |
| 1300 |
ssize_t write_res; |
| 1301 |
ssize_t bytes_read = 0; |
| 1302 |
ssize_t read_res; |
| 1303 |
int status = 0; |
| 1304 |
pid_t changed_pid; |
| 1305 |
#endif |
| 1306 |
|
| 1307 |
if (params != NULL) { |
| 1308 |
output_l = 2; |
| 1309 |
fputs(" ", MUNIT_OUTPUT_FILE); |
| 1310 |
first = true; |
| 1311 |
for (param = params ; param != NULL && param->name != NULL ; param++) { |
| 1312 |
if (!first) { |
| 1313 |
fputs(", ", MUNIT_OUTPUT_FILE); |
| 1314 |
output_l += 2; |
| 1315 |
} else { |
| 1316 |
first = false; |
| 1317 |
} |
| 1318 |
|
| 1319 |
output_l += fprintf(MUNIT_OUTPUT_FILE, "%s=%s", param->name, param->value); |
| 1320 |
} |
| 1321 |
while (output_l++ < MUNIT_TEST_NAME_LEN) { |
| 1322 |
fputc(' ', MUNIT_OUTPUT_FILE); |
| 1323 |
} |
| 1324 |
} |
| 1325 |
|
| 1326 |
fflush(MUNIT_OUTPUT_FILE); |
| 1327 |
|
| 1328 |
stderr_buf = NULL; |
| 1329 |
#if !defined(_WIN32) || defined(__MINGW32__) |
| 1330 |
stderr_buf = tmpfile(); |
| 1331 |
#else |
| 1332 |
tmpfile_s(&stderr_buf); |
| 1333 |
#endif |
| 1334 |
if (stderr_buf == NULL) { |
| 1335 |
munit_log_errno(MUNIT_LOG_ERROR, stderr, "unable to create buffer for stderr"); |
| 1336 |
result = MUNIT_ERROR; |
| 1337 |
goto print_result; |
| 1338 |
} |
| 1339 |
|
| 1340 |
#if !defined(MUNIT_NO_FORK) |
| 1341 |
if (runner->fork) { |
| 1342 |
pipefd[0] = -1; |
| 1343 |
pipefd[1] = -1; |
| 1344 |
if (pipe(pipefd) != 0) { |
| 1345 |
munit_log_errno(MUNIT_LOG_ERROR, stderr, "unable to create pipe"); |
| 1346 |
result = MUNIT_ERROR; |
| 1347 |
goto print_result; |
| 1348 |
} |
| 1349 |
|
| 1350 |
fork_pid = fork(); |
| 1351 |
if (fork_pid == 0) { |
| 1352 |
#if !defined(MUNIT_NO_BUFFER) |
| 1353 |
int orig_stderr; |
| 1354 |
#endif |
| 1355 |
|
| 1356 |
close(pipefd[0]); |
| 1357 |
|
| 1358 |
#if !defined(MUNIT_NO_BUFFER) |
| 1359 |
orig_stderr = munit_replace_stderr(stderr_buf); |
| 1360 |
#endif |
| 1361 |
munit_test_runner_exec(runner, test, params, &report); |
| 1362 |
|
| 1363 |
#if !defined(MUNIT_NO_BUFFER) |
| 1364 |
/* Note that we don't restore stderr. This is so we can buffer |
| 1365 |
* things written to stderr later on (such as by |
| 1366 |
* asan/tsan/ubsan, valgrind, etc.) */ |
| 1367 |
close(orig_stderr); |
| 1368 |
#endif |
| 1369 |
|
| 1370 |
do { |
| 1371 |
write_res = write(pipefd[1], ((munit_uint8_t*) (&report)) + bytes_written, sizeof(report) - bytes_written); |
| 1372 |
if (write_res < 0) { |
| 1373 |
if (stderr_buf != NULL) { |
| 1374 |
munit_log_errno(MUNIT_LOG_ERROR, stderr, "unable to write to pipe"); |
| 1375 |
} |
| 1376 |
exit(EXIT_FAILURE); |
| 1377 |
} |
| 1378 |
bytes_written += write_res; |
| 1379 |
} while ((size_t) bytes_written < sizeof(report)); |
| 1380 |
|
| 1381 |
if (stderr_buf != NULL) |
| 1382 |
fclose(stderr_buf); |
| 1383 |
close(pipefd[1]); |
| 1384 |
|
| 1385 |
exit(EXIT_SUCCESS); |
| 1386 |
} else if (fork_pid == -1) { |
| 1387 |
close(pipefd[0]); |
| 1388 |
close(pipefd[1]); |
| 1389 |
if (stderr_buf != NULL) { |
| 1390 |
munit_log_errno(MUNIT_LOG_ERROR, stderr, "unable to fork"); |
| 1391 |
} |
| 1392 |
report.errored++; |
| 1393 |
result = MUNIT_ERROR; |
| 1394 |
} else { |
| 1395 |
close(pipefd[1]); |
| 1396 |
do { |
| 1397 |
read_res = read(pipefd[0], ((munit_uint8_t*) (&report)) + bytes_read, sizeof(report) - bytes_read); |
| 1398 |
if (read_res < 1) |
| 1399 |
break; |
| 1400 |
bytes_read += read_res; |
| 1401 |
} while (bytes_read < (ssize_t) sizeof(report)); |
| 1402 |
|
| 1403 |
changed_pid = waitpid(fork_pid, &status, 0); |
| 1404 |
|
| 1405 |
if (MUNIT_LIKELY(changed_pid == fork_pid) && MUNIT_LIKELY(WIFEXITED(status))) { |
| 1406 |
if (bytes_read != sizeof(report)) { |
| 1407 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr_buf, "child exited unexpectedly with status %d", WEXITSTATUS(status)); |
| 1408 |
report.errored++; |
| 1409 |
} else if (WEXITSTATUS(status) != EXIT_SUCCESS) { |
| 1410 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr_buf, "child exited with status %d", WEXITSTATUS(status)); |
| 1411 |
report.errored++; |
| 1412 |
} |
| 1413 |
} else { |
| 1414 |
if (WIFSIGNALED(status)) { |
| 1415 |
#if defined(_XOPEN_VERSION) && (_XOPEN_VERSION >= 700) |
| 1416 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr_buf, "child killed by signal %d (%s)", WTERMSIG(status), strsignal(WTERMSIG(status))); |
| 1417 |
#else |
| 1418 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr_buf, "child killed by signal %d", WTERMSIG(status)); |
| 1419 |
#endif |
| 1420 |
} else if (WIFSTOPPED(status)) { |
| 1421 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr_buf, "child stopped by signal %d", WSTOPSIG(status)); |
| 1422 |
} |
| 1423 |
report.errored++; |
| 1424 |
} |
| 1425 |
|
| 1426 |
close(pipefd[0]); |
| 1427 |
waitpid(fork_pid, NULL, 0); |
| 1428 |
} |
| 1429 |
} else |
| 1430 |
#endif |
| 1431 |
{ |
| 1432 |
#if !defined(MUNIT_NO_BUFFER) |
| 1433 |
const volatile int orig_stderr = munit_replace_stderr(stderr_buf); |
| 1434 |
#endif |
| 1435 |
|
| 1436 |
#if defined(MUNIT_THREAD_LOCAL) |
| 1437 |
if (MUNIT_UNLIKELY(setjmp(munit_error_jmp_buf) != 0)) { |
| 1438 |
result = MUNIT_FAIL; |
| 1439 |
report.failed++; |
| 1440 |
} else { |
| 1441 |
munit_error_jmp_buf_valid = true; |
| 1442 |
result = munit_test_runner_exec(runner, test, params, &report); |
| 1443 |
} |
| 1444 |
#else |
| 1445 |
result = munit_test_runner_exec(runner, test, params, &report); |
| 1446 |
#endif |
| 1447 |
|
| 1448 |
#if !defined(MUNIT_NO_BUFFER) |
| 1449 |
munit_restore_stderr(orig_stderr); |
| 1450 |
#endif |
| 1451 |
|
| 1452 |
/* Here just so that the label is used on Windows and we don't get |
| 1453 |
* a warning */ |
| 1454 |
goto print_result; |
| 1455 |
} |
| 1456 |
|
| 1457 |
print_result: |
| 1458 |
|
| 1459 |
fputs("[ ", MUNIT_OUTPUT_FILE); |
| 1460 |
if ((test->options & MUNIT_TEST_OPTION_TODO) == MUNIT_TEST_OPTION_TODO) { |
| 1461 |
if (report.failed != 0 || report.errored != 0 || report.skipped != 0) { |
| 1462 |
munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_TODO, '3'); |
| 1463 |
result = MUNIT_OK; |
| 1464 |
} else { |
| 1465 |
munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_ERROR, '1'); |
| 1466 |
if (MUNIT_LIKELY(stderr_buf != NULL)) |
| 1467 |
munit_log_internal(MUNIT_LOG_ERROR, stderr_buf, "Test marked TODO, but was successful."); |
| 1468 |
runner->report.failed++; |
| 1469 |
result = MUNIT_ERROR; |
| 1470 |
} |
| 1471 |
} else if (report.failed > 0) { |
| 1472 |
munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_FAIL, '1'); |
| 1473 |
runner->report.failed++; |
| 1474 |
result = MUNIT_FAIL; |
| 1475 |
} else if (report.errored > 0) { |
| 1476 |
munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_ERROR, '1'); |
| 1477 |
runner->report.errored++; |
| 1478 |
result = MUNIT_ERROR; |
| 1479 |
} else if (report.skipped > 0) { |
| 1480 |
munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_SKIP, '3'); |
| 1481 |
runner->report.skipped++; |
| 1482 |
result = MUNIT_SKIP; |
| 1483 |
} else if (report.successful > 1) { |
| 1484 |
munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_OK, '2'); |
| 1485 |
#if defined(MUNIT_ENABLE_TIMING) |
| 1486 |
fputs(" ] [ ", MUNIT_OUTPUT_FILE); |
| 1487 |
munit_print_time(MUNIT_OUTPUT_FILE, report.wall_clock / report.successful); |
| 1488 |
fputs(" / ", MUNIT_OUTPUT_FILE); |
| 1489 |
munit_print_time(MUNIT_OUTPUT_FILE, report.cpu_clock / report.successful); |
| 1490 |
fprintf(MUNIT_OUTPUT_FILE, " CPU ]\n %-" MUNIT_XSTRINGIFY(MUNIT_TEST_NAME_LEN) "s Total: [ ", ""); |
| 1491 |
munit_print_time(MUNIT_OUTPUT_FILE, report.wall_clock); |
| 1492 |
fputs(" / ", MUNIT_OUTPUT_FILE); |
| 1493 |
munit_print_time(MUNIT_OUTPUT_FILE, report.cpu_clock); |
| 1494 |
fputs(" CPU", MUNIT_OUTPUT_FILE); |
| 1495 |
#endif |
| 1496 |
runner->report.successful++; |
| 1497 |
result = MUNIT_OK; |
| 1498 |
} else if (report.successful > 0) { |
| 1499 |
munit_test_runner_print_color(runner, MUNIT_RESULT_STRING_OK, '2'); |
| 1500 |
#if defined(MUNIT_ENABLE_TIMING) |
| 1501 |
fputs(" ] [ ", MUNIT_OUTPUT_FILE); |
| 1502 |
munit_print_time(MUNIT_OUTPUT_FILE, report.wall_clock); |
| 1503 |
fputs(" / ", MUNIT_OUTPUT_FILE); |
| 1504 |
munit_print_time(MUNIT_OUTPUT_FILE, report.cpu_clock); |
| 1505 |
fputs(" CPU", MUNIT_OUTPUT_FILE); |
| 1506 |
#endif |
| 1507 |
runner->report.successful++; |
| 1508 |
result = MUNIT_OK; |
| 1509 |
} |
| 1510 |
fputs(" ]\n", MUNIT_OUTPUT_FILE); |
| 1511 |
|
| 1512 |
if (stderr_buf != NULL) { |
| 1513 |
if (result == MUNIT_FAIL || result == MUNIT_ERROR || runner->show_stderr) { |
| 1514 |
fflush(MUNIT_OUTPUT_FILE); |
| 1515 |
|
| 1516 |
rewind(stderr_buf); |
| 1517 |
munit_splice(fileno(stderr_buf), STDERR_FILENO); |
| 1518 |
|
| 1519 |
fflush(stderr); |
| 1520 |
} |
| 1521 |
|
| 1522 |
fclose(stderr_buf); |
| 1523 |
} |
| 1524 |
} |
| 1525 |
|
| 1526 |
static void |
| 1527 |
munit_test_runner_run_test_wild(MunitTestRunner* runner, |
| 1528 |
const MunitTest* test, |
| 1529 |
const char* test_name, |
| 1530 |
MunitParameter* params, |
| 1531 |
MunitParameter* p) { |
| 1532 |
const MunitParameterEnum* pe; |
| 1533 |
char** values; |
| 1534 |
MunitParameter* next; |
| 1535 |
|
| 1536 |
for (pe = test->parameters ; pe != NULL && pe->name != NULL ; pe++) { |
| 1537 |
if (p->name == pe->name) |
| 1538 |
break; |
| 1539 |
} |
| 1540 |
|
| 1541 |
if (pe == NULL) |
| 1542 |
return; |
| 1543 |
|
| 1544 |
for (values = pe->values ; *values != NULL ; values++) { |
| 1545 |
next = p + 1; |
| 1546 |
p->value = *values; |
| 1547 |
if (next->name == NULL) { |
| 1548 |
munit_test_runner_run_test_with_params(runner, test, params); |
| 1549 |
} else { |
| 1550 |
munit_test_runner_run_test_wild(runner, test, test_name, params, next); |
| 1551 |
} |
| 1552 |
if (runner->fatal_failures && (runner->report.failed != 0 || runner->report.errored != 0)) |
| 1553 |
break; |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
/* Run a single test, with every combination of parameters |
| 1558 |
* requested. */ |
| 1559 |
static void |
| 1560 |
munit_test_runner_run_test(MunitTestRunner* runner, |
| 1561 |
const MunitTest* test, |
| 1562 |
const char* prefix) { |
| 1563 |
char* test_name = munit_maybe_concat(NULL, (char*) prefix, (char*) test->name); |
| 1564 |
/* The array of parameters to pass to |
| 1565 |
* munit_test_runner_run_test_with_params */ |
| 1566 |
MunitParameter* params = NULL; |
| 1567 |
size_t params_l = 0; |
| 1568 |
/* Wildcard parameters are parameters which have possible values |
| 1569 |
* specified in the test, but no specific value was passed to the |
| 1570 |
* CLI. That means we want to run the test once for every |
| 1571 |
* possible combination of parameter values or, if --single was |
| 1572 |
* passed to the CLI, a single time with a random set of |
| 1573 |
* parameters. */ |
| 1574 |
MunitParameter* wild_params = NULL; |
| 1575 |
size_t wild_params_l = 0; |
| 1576 |
const MunitParameterEnum* pe; |
| 1577 |
const MunitParameter* cli_p; |
| 1578 |
bool filled; |
| 1579 |
unsigned int possible; |
| 1580 |
char** vals; |
| 1581 |
size_t first_wild; |
| 1582 |
const MunitParameter* wp; |
| 1583 |
int pidx; |
| 1584 |
|
| 1585 |
munit_rand_seed(runner->seed); |
| 1586 |
|
| 1587 |
fprintf(MUNIT_OUTPUT_FILE, "%-" MUNIT_XSTRINGIFY(MUNIT_TEST_NAME_LEN) "s", test_name); |
| 1588 |
|
| 1589 |
if (test->parameters == NULL) { |
| 1590 |
/* No parameters. Simple, nice. */ |
| 1591 |
munit_test_runner_run_test_with_params(runner, test, NULL); |
| 1592 |
} else { |
| 1593 |
fputc('\n', MUNIT_OUTPUT_FILE); |
| 1594 |
|
| 1595 |
for (pe = test->parameters ; pe != NULL && pe->name != NULL ; pe++) { |
| 1596 |
/* Did we received a value for this parameter from the CLI? */ |
| 1597 |
filled = false; |
| 1598 |
for (cli_p = runner->parameters ; cli_p != NULL && cli_p->name != NULL ; cli_p++) { |
| 1599 |
if (strcmp(cli_p->name, pe->name) == 0) { |
| 1600 |
if (MUNIT_UNLIKELY(munit_parameters_add(¶ms_l, ¶ms, pe->name, cli_p->value) != MUNIT_OK)) |
| 1601 |
goto cleanup; |
| 1602 |
filled = true; |
| 1603 |
break; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
if (filled) |
| 1607 |
continue; |
| 1608 |
|
| 1609 |
/* Nothing from CLI, is the enum NULL/empty? We're not a |
| 1610 |
* fuzzer… */ |
| 1611 |
if (pe->values == NULL || pe->values[0] == NULL) |
| 1612 |
continue; |
| 1613 |
|
| 1614 |
/* If --single was passed to the CLI, choose a value from the |
| 1615 |
* list of possibilities randomly. */ |
| 1616 |
if (runner->single_parameter_mode) { |
| 1617 |
possible = 0; |
| 1618 |
for (vals = pe->values ; *vals != NULL ; vals++) |
| 1619 |
possible++; |
| 1620 |
/* We want the tests to be reproducible, even if you're only |
| 1621 |
* running a single test, but we don't want every test with |
| 1622 |
* the same number of parameters to choose the same parameter |
| 1623 |
* number, so use the test name as a primitive salt. */ |
| 1624 |
pidx = munit_rand_at_most(munit_str_hash(test_name), possible - 1); |
| 1625 |
if (MUNIT_UNLIKELY(munit_parameters_add(¶ms_l, ¶ms, pe->name, pe->values[pidx]) != MUNIT_OK)) |
| 1626 |
goto cleanup; |
| 1627 |
} else { |
| 1628 |
/* We want to try every permutation. Put in a placeholder |
| 1629 |
* entry, we'll iterate through them later. */ |
| 1630 |
if (MUNIT_UNLIKELY(munit_parameters_add(&wild_params_l, &wild_params, pe->name, NULL) != MUNIT_OK)) |
| 1631 |
goto cleanup; |
| 1632 |
} |
| 1633 |
} |
| 1634 |
|
| 1635 |
if (wild_params_l != 0) { |
| 1636 |
first_wild = params_l; |
| 1637 |
for (wp = wild_params ; wp != NULL && wp->name != NULL ; wp++) { |
| 1638 |
for (pe = test->parameters ; pe != NULL && pe->name != NULL && pe->values != NULL ; pe++) { |
| 1639 |
if (strcmp(wp->name, pe->name) == 0) { |
| 1640 |
if (MUNIT_UNLIKELY(munit_parameters_add(¶ms_l, ¶ms, pe->name, pe->values[0]) != MUNIT_OK)) |
| 1641 |
goto cleanup; |
| 1642 |
} |
| 1643 |
} |
| 1644 |
} |
| 1645 |
|
| 1646 |
munit_test_runner_run_test_wild(runner, test, test_name, params, params + first_wild); |
| 1647 |
} else { |
| 1648 |
munit_test_runner_run_test_with_params(runner, test, params); |
| 1649 |
} |
| 1650 |
|
| 1651 |
cleanup: |
| 1652 |
free(params); |
| 1653 |
free(wild_params); |
| 1654 |
} |
| 1655 |
|
| 1656 |
munit_maybe_free_concat(test_name, prefix, test->name); |
| 1657 |
} |
| 1658 |
|
| 1659 |
/* Recurse through the suite and run all the tests. If a list of |
| 1660 |
* tests to run was provied on the command line, run only those |
| 1661 |
* tests. */ |
| 1662 |
static void |
| 1663 |
munit_test_runner_run_suite(MunitTestRunner* runner, |
| 1664 |
const MunitSuite* suite, |
| 1665 |
const char* prefix) { |
| 1666 |
size_t pre_l; |
| 1667 |
char* pre = munit_maybe_concat(&pre_l, (char*) prefix, (char*) suite->prefix); |
| 1668 |
const MunitTest* test; |
| 1669 |
const char** test_name; |
| 1670 |
const MunitSuite* child_suite; |
| 1671 |
|
| 1672 |
/* Run the tests. */ |
| 1673 |
for (test = suite->tests ; test != NULL && test->test != NULL ; test++) { |
| 1674 |
if (runner->tests != NULL) { /* Specific tests were requested on the CLI */ |
| 1675 |
for (test_name = runner->tests ; test_name != NULL && *test_name != NULL ; test_name++) { |
| 1676 |
if ((pre_l == 0 || strncmp(pre, *test_name, pre_l) == 0) && |
| 1677 |
strncmp(test->name, *test_name + pre_l, strlen(*test_name + pre_l)) == 0) { |
| 1678 |
munit_test_runner_run_test(runner, test, pre); |
| 1679 |
if (runner->fatal_failures && (runner->report.failed != 0 || runner->report.errored != 0)) |
| 1680 |
goto cleanup; |
| 1681 |
} |
| 1682 |
} |
| 1683 |
} else { /* Run all tests */ |
| 1684 |
munit_test_runner_run_test(runner, test, pre); |
| 1685 |
} |
| 1686 |
} |
| 1687 |
|
| 1688 |
if (runner->fatal_failures && (runner->report.failed != 0 || runner->report.errored != 0)) |
| 1689 |
goto cleanup; |
| 1690 |
|
| 1691 |
/* Run any child suites. */ |
| 1692 |
for (child_suite = suite->suites ; child_suite != NULL && child_suite->prefix != NULL ; child_suite++) { |
| 1693 |
munit_test_runner_run_suite(runner, child_suite, pre); |
| 1694 |
} |
| 1695 |
|
| 1696 |
cleanup: |
| 1697 |
|
| 1698 |
munit_maybe_free_concat(pre, prefix, suite->prefix); |
| 1699 |
} |
| 1700 |
|
| 1701 |
static void |
| 1702 |
munit_test_runner_run(MunitTestRunner* runner) { |
| 1703 |
munit_test_runner_run_suite(runner, runner->suite, NULL); |
| 1704 |
} |
| 1705 |
|
| 1706 |
static void |
| 1707 |
munit_print_help(int argc, char* const argv[MUNIT_ARRAY_PARAM(argc)], void* user_data, const MunitArgument arguments[]) { |
| 1708 |
const MunitArgument* arg; |
| 1709 |
(void) argc; |
| 1710 |
|
| 1711 |
printf("USAGE: %s [OPTIONS...] [TEST...]\n\n", argv[0]); |
| 1712 |
puts(" --seed SEED\n" |
| 1713 |
" Value used to seed the PRNG. Must be a 32-bit integer in decimal\n" |
| 1714 |
" notation with no separators (commas, decimals, spaces, etc.), or\n" |
| 1715 |
" hexidecimal prefixed by \"0x\".\n" |
| 1716 |
" --iterations N\n" |
| 1717 |
" Run each test N times. 0 means the default number.\n" |
| 1718 |
" --param name value\n" |
| 1719 |
" A parameter key/value pair which will be passed to any test with\n" |
| 1720 |
" takes a parameter of that name. If not provided, the test will be\n" |
| 1721 |
" run once for each possible parameter value.\n" |
| 1722 |
" --list Write a list of all available tests.\n" |
| 1723 |
" --list-params\n" |
| 1724 |
" Write a list of all available tests and their possible parameters.\n" |
| 1725 |
" --single Run each parameterized test in a single configuration instead of\n" |
| 1726 |
" every possible combination\n" |
| 1727 |
" --log-visible debug|info|warning|error\n" |
| 1728 |
" --log-fatal debug|info|warning|error\n" |
| 1729 |
" Set the level at which messages of different severities are visible,\n" |
| 1730 |
" or cause the test to terminate.\n" |
| 1731 |
#if !defined(MUNIT_NO_FORK) |
| 1732 |
" --no-fork Do not execute tests in a child process. If this option is supplied\n" |
| 1733 |
" and a test crashes (including by failing an assertion), no further\n" |
| 1734 |
" tests will be performed.\n" |
| 1735 |
#endif |
| 1736 |
" --fatal-failures\n" |
| 1737 |
" Stop executing tests as soon as a failure is found.\n" |
| 1738 |
" --show-stderr\n" |
| 1739 |
" Show data written to stderr by the tests, even if the test succeeds.\n" |
| 1740 |
" --color auto|always|never\n" |
| 1741 |
" Colorize (or don't) the output.\n" |
| 1742 |
/* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */ |
| 1743 |
" --help Print this help message and exit.\n"); |
| 1744 |
#if defined(MUNIT_NL_LANGINFO) |
| 1745 |
setlocale(LC_ALL, ""); |
| 1746 |
fputs((strcasecmp("UTF-8", nl_langinfo(CODESET)) == 0) ? "µnit" : "munit", stdout); |
| 1747 |
#else |
| 1748 |
puts("munit"); |
| 1749 |
#endif |
| 1750 |
printf(" %d.%d.%d\n" |
| 1751 |
"Full documentation at: https://nemequ.github.io/munit/\n", |
| 1752 |
(MUNIT_CURRENT_VERSION >> 16) & 0xff, |
| 1753 |
(MUNIT_CURRENT_VERSION >> 8) & 0xff, |
| 1754 |
(MUNIT_CURRENT_VERSION >> 0) & 0xff); |
| 1755 |
for (arg = arguments ; arg != NULL && arg->name != NULL ; arg++) |
| 1756 |
arg->write_help(arg, user_data); |
| 1757 |
} |
| 1758 |
|
| 1759 |
static const MunitArgument* |
| 1760 |
munit_arguments_find(const MunitArgument arguments[], const char* name) { |
| 1761 |
const MunitArgument* arg; |
| 1762 |
|
| 1763 |
for (arg = arguments ; arg != NULL && arg->name != NULL ; arg++) |
| 1764 |
if (strcmp(arg->name, name) == 0) |
| 1765 |
return arg; |
| 1766 |
|
| 1767 |
return NULL; |
| 1768 |
} |
| 1769 |
|
| 1770 |
static void |
| 1771 |
munit_suite_list_tests(const MunitSuite* suite, bool show_params, const char* prefix) { |
| 1772 |
size_t pre_l; |
| 1773 |
char* pre = munit_maybe_concat(&pre_l, (char*) prefix, (char*) suite->prefix); |
| 1774 |
const MunitTest* test; |
| 1775 |
const MunitParameterEnum* params; |
| 1776 |
bool first; |
| 1777 |
char** val; |
| 1778 |
const MunitSuite* child_suite; |
| 1779 |
|
| 1780 |
for (test = suite->tests ; |
| 1781 |
test != NULL && test->name != NULL ; |
| 1782 |
test++) { |
| 1783 |
if (pre != NULL) |
| 1784 |
fputs(pre, stdout); |
| 1785 |
puts(test->name); |
| 1786 |
|
| 1787 |
if (show_params) { |
| 1788 |
for (params = test->parameters ; |
| 1789 |
params != NULL && params->name != NULL ; |
| 1790 |
params++) { |
| 1791 |
fprintf(stdout, " - %s: ", params->name); |
| 1792 |
if (params->values == NULL) { |
| 1793 |
puts("Any"); |
| 1794 |
} else { |
| 1795 |
first = true; |
| 1796 |
for (val = params->values ; |
| 1797 |
*val != NULL ; |
| 1798 |
val++ ) { |
| 1799 |
if(!first) { |
| 1800 |
fputs(", ", stdout); |
| 1801 |
} else { |
| 1802 |
first = false; |
| 1803 |
} |
| 1804 |
fputs(*val, stdout); |
| 1805 |
} |
| 1806 |
putc('\n', stdout); |
| 1807 |
} |
| 1808 |
} |
| 1809 |
} |
| 1810 |
} |
| 1811 |
|
| 1812 |
for (child_suite = suite->suites ; child_suite != NULL && child_suite->prefix != NULL ; child_suite++) { |
| 1813 |
munit_suite_list_tests(child_suite, show_params, pre); |
| 1814 |
} |
| 1815 |
|
| 1816 |
munit_maybe_free_concat(pre, prefix, suite->prefix); |
| 1817 |
} |
| 1818 |
|
| 1819 |
static bool |
| 1820 |
munit_stream_supports_ansi(FILE *stream) { |
| 1821 |
#if !defined(_WIN32) |
| 1822 |
return isatty(fileno(stream)); |
| 1823 |
#else |
| 1824 |
|
| 1825 |
#if !defined(__MINGW32__) |
| 1826 |
size_t ansicon_size = 0; |
| 1827 |
#endif |
| 1828 |
|
| 1829 |
if (isatty(fileno(stream))) { |
| 1830 |
#if !defined(__MINGW32__) |
| 1831 |
getenv_s(&ansicon_size, NULL, 0, "ANSICON"); |
| 1832 |
return ansicon_size != 0; |
| 1833 |
#else |
| 1834 |
return getenv("ANSICON") != NULL; |
| 1835 |
#endif |
| 1836 |
} |
| 1837 |
return false; |
| 1838 |
#endif |
| 1839 |
} |
| 1840 |
|
| 1841 |
int |
| 1842 |
munit_suite_main_custom(const MunitSuite* suite, void* user_data, |
| 1843 |
int argc, char* const argv[MUNIT_ARRAY_PARAM(argc)], |
| 1844 |
const MunitArgument arguments[]) { |
| 1845 |
int result = EXIT_FAILURE; |
| 1846 |
MunitTestRunner runner; |
| 1847 |
size_t parameters_size = 0; |
| 1848 |
size_t tests_size = 0; |
| 1849 |
int arg; |
| 1850 |
|
| 1851 |
char* envptr; |
| 1852 |
unsigned long ts; |
| 1853 |
char* endptr; |
| 1854 |
unsigned long long iterations; |
| 1855 |
MunitLogLevel level; |
| 1856 |
const MunitArgument* argument; |
| 1857 |
const char** runner_tests; |
| 1858 |
unsigned int tests_run; |
| 1859 |
unsigned int tests_total; |
| 1860 |
|
| 1861 |
runner.prefix = NULL; |
| 1862 |
runner.suite = NULL; |
| 1863 |
runner.tests = NULL; |
| 1864 |
runner.seed = 0; |
| 1865 |
runner.iterations = 0; |
| 1866 |
runner.parameters = NULL; |
| 1867 |
runner.single_parameter_mode = false; |
| 1868 |
runner.user_data = NULL; |
| 1869 |
|
| 1870 |
runner.report.successful = 0; |
| 1871 |
runner.report.skipped = 0; |
| 1872 |
runner.report.failed = 0; |
| 1873 |
runner.report.errored = 0; |
| 1874 |
#if defined(MUNIT_ENABLE_TIMING) |
| 1875 |
runner.report.cpu_clock = 0; |
| 1876 |
runner.report.wall_clock = 0; |
| 1877 |
#endif |
| 1878 |
|
| 1879 |
runner.colorize = false; |
| 1880 |
#if !defined(_WIN32) |
| 1881 |
runner.fork = true; |
| 1882 |
#else |
| 1883 |
runner.fork = false; |
| 1884 |
#endif |
| 1885 |
runner.show_stderr = false; |
| 1886 |
runner.fatal_failures = false; |
| 1887 |
runner.suite = suite; |
| 1888 |
runner.user_data = user_data; |
| 1889 |
runner.seed = munit_rand_generate_seed(); |
| 1890 |
runner.colorize = munit_stream_supports_ansi(MUNIT_OUTPUT_FILE); |
| 1891 |
|
| 1892 |
for (arg = 1 ; arg < argc ; arg++) { |
| 1893 |
if (strncmp("--", argv[arg], 2) == 0) { |
| 1894 |
if (strcmp("seed", argv[arg] + 2) == 0) { |
| 1895 |
if (arg + 1 >= argc) { |
| 1896 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr, "%s requires an argument", argv[arg]); |
| 1897 |
goto cleanup; |
| 1898 |
} |
| 1899 |
|
| 1900 |
envptr = argv[arg + 1]; |
| 1901 |
ts = strtoul(argv[arg + 1], &envptr, 0); |
| 1902 |
if (*envptr != '\0' || ts > (~((munit_uint32_t) 0U))) { |
| 1903 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr, "invalid value ('%s') passed to %s", argv[arg + 1], argv[arg]); |
| 1904 |
goto cleanup; |
| 1905 |
} |
| 1906 |
runner.seed = (munit_uint32_t) ts; |
| 1907 |
|
| 1908 |
arg++; |
| 1909 |
} else if (strcmp("iterations", argv[arg] + 2) == 0) { |
| 1910 |
if (arg + 1 >= argc) { |
| 1911 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr, "%s requires an argument", argv[arg]); |
| 1912 |
goto cleanup; |
| 1913 |
} |
| 1914 |
|
| 1915 |
endptr = argv[arg + 1]; |
| 1916 |
iterations = strtoul(argv[arg + 1], &endptr, 0); |
| 1917 |
if (*endptr != '\0' || iterations > UINT_MAX) { |
| 1918 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr, "invalid value ('%s') passed to %s", argv[arg + 1], argv[arg]); |
| 1919 |
goto cleanup; |
| 1920 |
} |
| 1921 |
|
| 1922 |
runner.iterations = (unsigned int) iterations; |
| 1923 |
|
| 1924 |
arg++; |
| 1925 |
} else if (strcmp("param", argv[arg] + 2) == 0) { |
| 1926 |
if (arg + 2 >= argc) { |
| 1927 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr, "%s requires two arguments", argv[arg]); |
| 1928 |
goto cleanup; |
| 1929 |
} |
| 1930 |
|
| 1931 |
runner.parameters = realloc(runner.parameters, sizeof(MunitParameter) * (parameters_size + 2)); |
| 1932 |
if (runner.parameters == NULL) { |
| 1933 |
munit_log_internal(MUNIT_LOG_ERROR, stderr, "failed to allocate memory"); |
| 1934 |
goto cleanup; |
| 1935 |
} |
| 1936 |
runner.parameters[parameters_size].name = (char*) argv[arg + 1]; |
| 1937 |
runner.parameters[parameters_size].value = (char*) argv[arg + 2]; |
| 1938 |
parameters_size++; |
| 1939 |
runner.parameters[parameters_size].name = NULL; |
| 1940 |
runner.parameters[parameters_size].value = NULL; |
| 1941 |
arg += 2; |
| 1942 |
} else if (strcmp("color", argv[arg] + 2) == 0) { |
| 1943 |
if (arg + 1 >= argc) { |
| 1944 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr, "%s requires an argument", argv[arg]); |
| 1945 |
goto cleanup; |
| 1946 |
} |
| 1947 |
|
| 1948 |
if (strcmp(argv[arg + 1], "always") == 0) |
| 1949 |
runner.colorize = true; |
| 1950 |
else if (strcmp(argv[arg + 1], "never") == 0) |
| 1951 |
runner.colorize = false; |
| 1952 |
else if (strcmp(argv[arg + 1], "auto") == 0) |
| 1953 |
runner.colorize = munit_stream_supports_ansi(MUNIT_OUTPUT_FILE); |
| 1954 |
else { |
| 1955 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr, "invalid value ('%s') passed to %s", argv[arg + 1], argv[arg]); |
| 1956 |
goto cleanup; |
| 1957 |
} |
| 1958 |
|
| 1959 |
arg++; |
| 1960 |
} else if (strcmp("help", argv[arg] + 2) == 0) { |
| 1961 |
munit_print_help(argc, argv, user_data, arguments); |
| 1962 |
result = EXIT_SUCCESS; |
| 1963 |
goto cleanup; |
| 1964 |
} else if (strcmp("single", argv[arg] + 2) == 0) { |
| 1965 |
runner.single_parameter_mode = true; |
| 1966 |
} else if (strcmp("show-stderr", argv[arg] + 2) == 0) { |
| 1967 |
runner.show_stderr = true; |
| 1968 |
#if !defined(_WIN32) |
| 1969 |
} else if (strcmp("no-fork", argv[arg] + 2) == 0) { |
| 1970 |
runner.fork = false; |
| 1971 |
#endif |
| 1972 |
} else if (strcmp("fatal-failures", argv[arg] + 2) == 0) { |
| 1973 |
runner.fatal_failures = true; |
| 1974 |
} else if (strcmp("log-visible", argv[arg] + 2) == 0 || |
| 1975 |
strcmp("log-fatal", argv[arg] + 2) == 0) { |
| 1976 |
if (arg + 1 >= argc) { |
| 1977 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr, "%s requires an argument", argv[arg]); |
| 1978 |
goto cleanup; |
| 1979 |
} |
| 1980 |
|
| 1981 |
if (strcmp(argv[arg + 1], "debug") == 0) |
| 1982 |
level = MUNIT_LOG_DEBUG; |
| 1983 |
else if (strcmp(argv[arg + 1], "info") == 0) |
| 1984 |
level = MUNIT_LOG_INFO; |
| 1985 |
else if (strcmp(argv[arg + 1], "warning") == 0) |
| 1986 |
level = MUNIT_LOG_WARNING; |
| 1987 |
else if (strcmp(argv[arg + 1], "error") == 0) |
| 1988 |
level = MUNIT_LOG_ERROR; |
| 1989 |
else { |
| 1990 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr, "invalid value ('%s') passed to %s", argv[arg + 1], argv[arg]); |
| 1991 |
goto cleanup; |
| 1992 |
} |
| 1993 |
|
| 1994 |
if (strcmp("log-visible", argv[arg] + 2) == 0) |
| 1995 |
munit_log_level_visible = level; |
| 1996 |
else |
| 1997 |
munit_log_level_fatal = level; |
| 1998 |
|
| 1999 |
arg++; |
| 2000 |
} else if (strcmp("list", argv[arg] + 2) == 0) { |
| 2001 |
munit_suite_list_tests(suite, false, NULL); |
| 2002 |
result = EXIT_SUCCESS; |
| 2003 |
goto cleanup; |
| 2004 |
} else if (strcmp("list-params", argv[arg] + 2) == 0) { |
| 2005 |
munit_suite_list_tests(suite, true, NULL); |
| 2006 |
result = EXIT_SUCCESS; |
| 2007 |
goto cleanup; |
| 2008 |
} else { |
| 2009 |
argument = munit_arguments_find(arguments, argv[arg] + 2); |
| 2010 |
if (argument == NULL) { |
| 2011 |
munit_logf_internal(MUNIT_LOG_ERROR, stderr, "unknown argument ('%s')", argv[arg]); |
| 2012 |
goto cleanup; |
| 2013 |
} |
| 2014 |
|
| 2015 |
if (!argument->parse_argument(suite, user_data, &arg, argc, argv)) |
| 2016 |
goto cleanup; |
| 2017 |
} |
| 2018 |
} else { |
| 2019 |
runner_tests = realloc((void*) runner.tests, sizeof(char*) * (tests_size + 2)); |
| 2020 |
if (runner_tests == NULL) { |
| 2021 |
munit_log_internal(MUNIT_LOG_ERROR, stderr, "failed to allocate memory"); |
| 2022 |
goto cleanup; |
| 2023 |
} |
| 2024 |
runner.tests = runner_tests; |
| 2025 |
runner.tests[tests_size++] = argv[arg]; |
| 2026 |
runner.tests[tests_size] = NULL; |
| 2027 |
} |
| 2028 |
} |
| 2029 |
|
| 2030 |
fflush(stderr); |
| 2031 |
fprintf(MUNIT_OUTPUT_FILE, "Running test suite with seed 0x%08" PRIx32 "...\n", runner.seed); |
| 2032 |
|
| 2033 |
munit_test_runner_run(&runner); |
| 2034 |
|
| 2035 |
tests_run = runner.report.successful + runner.report.failed + runner.report.errored; |
| 2036 |
tests_total = tests_run + runner.report.skipped; |
| 2037 |
if (tests_run == 0) { |
| 2038 |
fprintf(stderr, "No tests run, %d (100%%) skipped.\n", runner.report.skipped); |
| 2039 |
} else { |
| 2040 |
fprintf(MUNIT_OUTPUT_FILE, "%d of %d (%0.0f%%) tests successful, %d (%0.0f%%) test skipped.\n", |
| 2041 |
runner.report.successful, tests_run, |
| 2042 |
(((double) runner.report.successful) / ((double) tests_run)) * 100.0, |
| 2043 |
runner.report.skipped, |
| 2044 |
(((double) runner.report.skipped) / ((double) tests_total)) * 100.0); |
| 2045 |
} |
| 2046 |
|
| 2047 |
if (runner.report.failed == 0 && runner.report.errored == 0) { |
| 2048 |
result = EXIT_SUCCESS; |
| 2049 |
} |
| 2050 |
|
| 2051 |
cleanup: |
| 2052 |
free(runner.parameters); |
| 2053 |
free((void*) runner.tests); |
| 2054 |
|
| 2055 |
return result; |
| 2056 |
} |
| 2057 |
|
| 2058 |
int |
| 2059 |
munit_suite_main(const MunitSuite* suite, void* user_data, |
| 2060 |
int argc, char* const argv[MUNIT_ARRAY_PARAM(argc)]) { |
| 2061 |
return munit_suite_main_custom(suite, user_data, argc, argv, NULL); |
| 2062 |
} |