1 // 2 // doctest.h - the lightest feature-rich C++ single-header testing framework for unit tests and TDD 3 // 4 // Copyright (c) 2016-2021 Viktor Kirilov 5 // 6 // Distributed under the MIT Software License 7 // See accompanying file LICENSE.txt or copy at 8 // https://opensource.org/licenses/MIT 9 // 10 // The documentation can be found at the library's page: 11 // https://github.com/onqtam/doctest/blob/master/doc/markdown/readme.md 12 // 13 // ================================================================================================= 14 // ================================================================================================= 15 // ================================================================================================= 16 // 17 // The library is heavily influenced by Catch - https://github.com/catchorg/Catch2 18 // which uses the Boost Software License - Version 1.0 19 // see here - https://github.com/catchorg/Catch2/blob/master/LICENSE.txt 20 // 21 // The concept of subcases (sections in Catch) and expression decomposition are from there. 22 // Some parts of the code are taken directly: 23 // - stringification - the detection of "ostream& operator<<(ostream&, const T&)" and StringMaker<> 24 // - the Approx() helper class for floating point comparison 25 // - colors in the console 26 // - breaking into a debugger 27 // - signal / SEH handling 28 // - timer 29 // - XmlWriter class - thanks to Phil Nash for allowing the direct reuse (AKA copy/paste) 30 // 31 // The expression decomposing templates are taken from lest - https://github.com/martinmoene/lest 32 // which uses the Boost Software License - Version 1.0 33 // see here - https://github.com/martinmoene/lest/blob/master/LICENSE.txt 34 // 35 // ================================================================================================= 36 // ================================================================================================= 37 // ================================================================================================= 38 39 #ifndef DOCTEST_LIBRARY_INCLUDED 40 #define DOCTEST_LIBRARY_INCLUDED 41 42 // ================================================================================================= 43 // == VERSION ====================================================================================== 44 // ================================================================================================= 45 46 #define DOCTEST_VERSION_MAJOR 2 47 #define DOCTEST_VERSION_MINOR 4 48 #define DOCTEST_VERSION_PATCH 7 49 #define DOCTEST_VERSION_STR "2.4.7" 50 51 #define DOCTEST_VERSION \ 52 (DOCTEST_VERSION_MAJOR * 10000 + DOCTEST_VERSION_MINOR * 100 + DOCTEST_VERSION_PATCH) 53 54 // ================================================================================================= 55 // == COMPILER VERSION ============================================================================= 56 // ================================================================================================= 57 58 // ideas for the version stuff are taken from here: https://github.com/cxxstuff/cxx_detect 59 60 #define DOCTEST_COMPILER(MAJOR, MINOR, PATCH) ((MAJOR)*10000000 + (MINOR)*100000 + (PATCH)) 61 62 // GCC/Clang and GCC/MSVC are mutually exclusive, but Clang/MSVC are not because of clang-cl... 63 #if defined(_MSC_VER) && defined(_MSC_FULL_VER) 64 #if _MSC_VER == _MSC_FULL_VER / 10000 65 #define DOCTEST_MSVC DOCTEST_COMPILER(_MSC_VER / 100, _MSC_VER % 100, _MSC_FULL_VER % 10000) 66 #else // MSVC 67 #define DOCTEST_MSVC \ 68 DOCTEST_COMPILER(_MSC_VER / 100, (_MSC_FULL_VER / 100000) % 100, _MSC_FULL_VER % 100000) 69 #endif // MSVC 70 #endif // MSVC 71 #if defined(__clang__) && defined(__clang_minor__) 72 #define DOCTEST_CLANG DOCTEST_COMPILER(__clang_major__, __clang_minor__, __clang_patchlevel__) 73 #elif defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) && \ 74 !defined(__INTEL_COMPILER) 75 #define DOCTEST_GCC DOCTEST_COMPILER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) 76 #endif // GCC 77 78 #ifndef DOCTEST_MSVC 79 #define DOCTEST_MSVC 0 80 #endif // DOCTEST_MSVC 81 #ifndef DOCTEST_CLANG 82 #define DOCTEST_CLANG 0 83 #endif // DOCTEST_CLANG 84 #ifndef DOCTEST_GCC 85 #define DOCTEST_GCC 0 86 #endif // DOCTEST_GCC 87 88 // ================================================================================================= 89 // == COMPILER WARNINGS HELPERS ==================================================================== 90 // ================================================================================================= 91 92 #if DOCTEST_CLANG 93 #define DOCTEST_PRAGMA_TO_STR(x) _Pragma(#x) 94 #define DOCTEST_CLANG_SUPPRESS_WARNING_PUSH _Pragma("clang diagnostic push") 95 #define DOCTEST_CLANG_SUPPRESS_WARNING(w) DOCTEST_PRAGMA_TO_STR(clang diagnostic ignored w) 96 #define DOCTEST_CLANG_SUPPRESS_WARNING_POP _Pragma("clang diagnostic pop") 97 #define DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH(w) \ 98 DOCTEST_CLANG_SUPPRESS_WARNING_PUSH DOCTEST_CLANG_SUPPRESS_WARNING(w) 99 #else // DOCTEST_CLANG 100 #define DOCTEST_CLANG_SUPPRESS_WARNING_PUSH 101 #define DOCTEST_CLANG_SUPPRESS_WARNING(w) 102 #define DOCTEST_CLANG_SUPPRESS_WARNING_POP 103 #define DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH(w) 104 #endif // DOCTEST_CLANG 105 106 #if DOCTEST_GCC 107 #define DOCTEST_PRAGMA_TO_STR(x) _Pragma(#x) 108 #define DOCTEST_GCC_SUPPRESS_WARNING_PUSH _Pragma("GCC diagnostic push") 109 #define DOCTEST_GCC_SUPPRESS_WARNING(w) DOCTEST_PRAGMA_TO_STR(GCC diagnostic ignored w) 110 #define DOCTEST_GCC_SUPPRESS_WARNING_POP _Pragma("GCC diagnostic pop") 111 #define DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH(w) \ 112 DOCTEST_GCC_SUPPRESS_WARNING_PUSH DOCTEST_GCC_SUPPRESS_WARNING(w) 113 #else // DOCTEST_GCC 114 #define DOCTEST_GCC_SUPPRESS_WARNING_PUSH 115 #define DOCTEST_GCC_SUPPRESS_WARNING(w) 116 #define DOCTEST_GCC_SUPPRESS_WARNING_POP 117 #define DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH(w) 118 #endif // DOCTEST_GCC 119 120 #if DOCTEST_MSVC 121 #define DOCTEST_MSVC_SUPPRESS_WARNING_PUSH __pragma(warning(push)) 122 #define DOCTEST_MSVC_SUPPRESS_WARNING(w) __pragma(warning(disable : w)) 123 #define DOCTEST_MSVC_SUPPRESS_WARNING_POP __pragma(warning(pop)) 124 #define DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(w) \ 125 DOCTEST_MSVC_SUPPRESS_WARNING_PUSH DOCTEST_MSVC_SUPPRESS_WARNING(w) 126 #else // DOCTEST_MSVC 127 #define DOCTEST_MSVC_SUPPRESS_WARNING_PUSH 128 #define DOCTEST_MSVC_SUPPRESS_WARNING(w) 129 #define DOCTEST_MSVC_SUPPRESS_WARNING_POP 130 #define DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(w) 131 #endif // DOCTEST_MSVC 132 133 // ================================================================================================= 134 // == COMPILER WARNINGS ============================================================================ 135 // ================================================================================================= 136 137 DOCTEST_CLANG_SUPPRESS_WARNING_PUSH 138 DOCTEST_CLANG_SUPPRESS_WARNING("-Wunknown-pragmas") 139 DOCTEST_CLANG_SUPPRESS_WARNING("-Wnon-virtual-dtor") 140 DOCTEST_CLANG_SUPPRESS_WARNING("-Wweak-vtables") 141 DOCTEST_CLANG_SUPPRESS_WARNING("-Wpadded") 142 DOCTEST_CLANG_SUPPRESS_WARNING("-Wdeprecated") 143 DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-prototypes") 144 DOCTEST_CLANG_SUPPRESS_WARNING("-Wunused-local-typedef") 145 DOCTEST_CLANG_SUPPRESS_WARNING("-Wc++98-compat") 146 DOCTEST_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") 147 148 DOCTEST_GCC_SUPPRESS_WARNING_PUSH 149 DOCTEST_GCC_SUPPRESS_WARNING("-Wunknown-pragmas") 150 DOCTEST_GCC_SUPPRESS_WARNING("-Wpragmas") 151 DOCTEST_GCC_SUPPRESS_WARNING("-Weffc++") 152 DOCTEST_GCC_SUPPRESS_WARNING("-Wstrict-overflow") 153 DOCTEST_GCC_SUPPRESS_WARNING("-Wstrict-aliasing") 154 DOCTEST_GCC_SUPPRESS_WARNING("-Wctor-dtor-privacy") 155 DOCTEST_GCC_SUPPRESS_WARNING("-Wmissing-declarations") 156 DOCTEST_GCC_SUPPRESS_WARNING("-Wnon-virtual-dtor") 157 DOCTEST_GCC_SUPPRESS_WARNING("-Wunused-local-typedefs") 158 DOCTEST_GCC_SUPPRESS_WARNING("-Wuseless-cast") 159 DOCTEST_GCC_SUPPRESS_WARNING("-Wnoexcept") 160 DOCTEST_GCC_SUPPRESS_WARNING("-Wsign-promo") 161 162 DOCTEST_MSVC_SUPPRESS_WARNING_PUSH 163 DOCTEST_MSVC_SUPPRESS_WARNING(4616) // invalid compiler warning 164 DOCTEST_MSVC_SUPPRESS_WARNING(4619) // invalid compiler warning 165 DOCTEST_MSVC_SUPPRESS_WARNING(4996) // The compiler encountered a deprecated declaration 166 DOCTEST_MSVC_SUPPRESS_WARNING(4706) // assignment within conditional expression 167 DOCTEST_MSVC_SUPPRESS_WARNING(4512) // 'class' : assignment operator could not be generated 168 DOCTEST_MSVC_SUPPRESS_WARNING(4127) // conditional expression is constant 169 DOCTEST_MSVC_SUPPRESS_WARNING(4820) // padding 170 DOCTEST_MSVC_SUPPRESS_WARNING(4625) // copy constructor was implicitly defined as deleted 171 DOCTEST_MSVC_SUPPRESS_WARNING(4626) // assignment operator was implicitly defined as deleted 172 DOCTEST_MSVC_SUPPRESS_WARNING(5027) // move assignment operator was implicitly defined as deleted 173 DOCTEST_MSVC_SUPPRESS_WARNING(5026) // move constructor was implicitly defined as deleted 174 DOCTEST_MSVC_SUPPRESS_WARNING(4623) // default constructor was implicitly defined as deleted 175 DOCTEST_MSVC_SUPPRESS_WARNING(4640) // construction of local static object is not thread-safe 176 DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation for memory load 177 // static analysis 178 DOCTEST_MSVC_SUPPRESS_WARNING(26439) // This kind of function may not throw. Declare it 'noexcept' 179 DOCTEST_MSVC_SUPPRESS_WARNING(26495) // Always initialize a member variable 180 DOCTEST_MSVC_SUPPRESS_WARNING(26451) // Arithmetic overflow ... 181 DOCTEST_MSVC_SUPPRESS_WARNING(26444) // Avoid unnamed objects with custom construction and dtr... 182 DOCTEST_MSVC_SUPPRESS_WARNING(26812) // Prefer 'enum class' over 'enum' 183 184 // 4548 - expression before comma has no effect; expected expression with side - effect 185 // 4265 - class has virtual functions, but destructor is not virtual 186 // 4986 - exception specification does not match previous declaration 187 // 4350 - behavior change: 'member1' called instead of 'member2' 188 // 4668 - 'x' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif' 189 // 4365 - conversion from 'int' to 'unsigned long', signed/unsigned mismatch 190 // 4774 - format string expected in argument 'x' is not a string literal 191 // 4820 - padding in structs 192 193 // only 4 should be disabled globally: 194 // - 4514 # unreferenced inline function has been removed 195 // - 4571 # SEH related 196 // - 4710 # function not inlined 197 // - 4711 # function 'x' selected for automatic inline expansion 198 199 #define DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN \ 200 DOCTEST_MSVC_SUPPRESS_WARNING_PUSH \ 201 DOCTEST_MSVC_SUPPRESS_WARNING(4548) \ 202 DOCTEST_MSVC_SUPPRESS_WARNING(4265) \ 203 DOCTEST_MSVC_SUPPRESS_WARNING(4986) \ 204 DOCTEST_MSVC_SUPPRESS_WARNING(4350) \ 205 DOCTEST_MSVC_SUPPRESS_WARNING(4668) \ 206 DOCTEST_MSVC_SUPPRESS_WARNING(4365) \ 207 DOCTEST_MSVC_SUPPRESS_WARNING(4774) \ 208 DOCTEST_MSVC_SUPPRESS_WARNING(4820) \ 209 DOCTEST_MSVC_SUPPRESS_WARNING(4625) \ 210 DOCTEST_MSVC_SUPPRESS_WARNING(4626) \ 211 DOCTEST_MSVC_SUPPRESS_WARNING(5027) \ 212 DOCTEST_MSVC_SUPPRESS_WARNING(5026) \ 213 DOCTEST_MSVC_SUPPRESS_WARNING(4623) \ 214 DOCTEST_MSVC_SUPPRESS_WARNING(5039) \ 215 DOCTEST_MSVC_SUPPRESS_WARNING(5045) \ 216 DOCTEST_MSVC_SUPPRESS_WARNING(5105) 217 218 #define DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END DOCTEST_MSVC_SUPPRESS_WARNING_POP 219 220 // ================================================================================================= 221 // == FEATURE DETECTION ============================================================================ 222 // ================================================================================================= 223 224 // general compiler feature support table: https://en.cppreference.com/w/cpp/compiler_support 225 // MSVC C++11 feature support table: https://msdn.microsoft.com/en-us/library/hh567368.aspx 226 // GCC C++11 feature support table: https://gcc.gnu.org/projects/cxx-status.html 227 // MSVC version table: 228 // https://en.wikipedia.org/wiki/Microsoft_Visual_C%2B%2B#Internal_version_numbering 229 // MSVC++ 14.2 (16) _MSC_VER == 1920 (Visual Studio 2019) 230 // MSVC++ 14.1 (15) _MSC_VER == 1910 (Visual Studio 2017) 231 // MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015) 232 // MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013) 233 // MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012) 234 // MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010) 235 // MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008) 236 // MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005) 237 238 #if DOCTEST_MSVC && !defined(DOCTEST_CONFIG_WINDOWS_SEH) 239 #define DOCTEST_CONFIG_WINDOWS_SEH 240 #endif // MSVC 241 #if defined(DOCTEST_CONFIG_NO_WINDOWS_SEH) && defined(DOCTEST_CONFIG_WINDOWS_SEH) 242 #undef DOCTEST_CONFIG_WINDOWS_SEH 243 #endif // DOCTEST_CONFIG_NO_WINDOWS_SEH 244 245 #if !defined(_WIN32) && !defined(__QNX__) && !defined(DOCTEST_CONFIG_POSIX_SIGNALS) && \ 246 !defined(__EMSCRIPTEN__) 247 #define DOCTEST_CONFIG_POSIX_SIGNALS 248 #endif // _WIN32 249 #if defined(DOCTEST_CONFIG_NO_POSIX_SIGNALS) && defined(DOCTEST_CONFIG_POSIX_SIGNALS) 250 #undef DOCTEST_CONFIG_POSIX_SIGNALS 251 #endif // DOCTEST_CONFIG_NO_POSIX_SIGNALS 252 253 #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS 254 #if !defined(__cpp_exceptions) && !defined(__EXCEPTIONS) && !defined(_CPPUNWIND) 255 #define DOCTEST_CONFIG_NO_EXCEPTIONS 256 #endif // no exceptions 257 #endif // DOCTEST_CONFIG_NO_EXCEPTIONS 258 259 #ifdef DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS 260 #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS 261 #define DOCTEST_CONFIG_NO_EXCEPTIONS 262 #endif // DOCTEST_CONFIG_NO_EXCEPTIONS 263 #endif // DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS 264 265 #if defined(DOCTEST_CONFIG_NO_EXCEPTIONS) && !defined(DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS) 266 #define DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS 267 #endif // DOCTEST_CONFIG_NO_EXCEPTIONS && !DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS 268 269 #if defined(DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN) && !defined(DOCTEST_CONFIG_IMPLEMENT) 270 #define DOCTEST_CONFIG_IMPLEMENT 271 #endif // DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN 272 273 #if defined(_WIN32) || defined(__CYGWIN__) 274 #if DOCTEST_MSVC 275 #define DOCTEST_SYMBOL_EXPORT __declspec(dllexport) 276 #define DOCTEST_SYMBOL_IMPORT __declspec(dllimport) 277 #else // MSVC 278 #define DOCTEST_SYMBOL_EXPORT __attribute__((dllexport)) 279 #define DOCTEST_SYMBOL_IMPORT __attribute__((dllimport)) 280 #endif // MSVC 281 #else // _WIN32 282 #define DOCTEST_SYMBOL_EXPORT __attribute__((visibility("default"))) 283 #define DOCTEST_SYMBOL_IMPORT 284 #endif // _WIN32 285 286 #ifdef DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL 287 #ifdef DOCTEST_CONFIG_IMPLEMENT 288 #define DOCTEST_INTERFACE DOCTEST_SYMBOL_EXPORT 289 #else // DOCTEST_CONFIG_IMPLEMENT 290 #define DOCTEST_INTERFACE DOCTEST_SYMBOL_IMPORT 291 #endif // DOCTEST_CONFIG_IMPLEMENT 292 #else // DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL 293 #define DOCTEST_INTERFACE 294 #endif // DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL 295 296 #define DOCTEST_EMPTY 297 298 #if DOCTEST_MSVC 299 #define DOCTEST_NOINLINE __declspec(noinline) 300 #define DOCTEST_UNUSED 301 #define DOCTEST_ALIGNMENT(x) 302 #elif DOCTEST_CLANG && DOCTEST_CLANG < DOCTEST_COMPILER(3, 5, 0) 303 #define DOCTEST_NOINLINE 304 #define DOCTEST_UNUSED 305 #define DOCTEST_ALIGNMENT(x) 306 #else 307 #define DOCTEST_NOINLINE __attribute__((noinline)) 308 #define DOCTEST_UNUSED __attribute__((unused)) 309 #define DOCTEST_ALIGNMENT(x) __attribute__((aligned(x))) 310 #endif 311 312 #ifndef DOCTEST_NORETURN 313 #if DOCTEST_MSVC && (DOCTEST_MSVC < DOCTEST_COMPILER(19, 0, 0)) 314 #define DOCTEST_NORETURN 315 #else // DOCTEST_MSVC 316 #define DOCTEST_NORETURN [[noreturn]] 317 #endif // DOCTEST_MSVC 318 #endif // DOCTEST_NORETURN 319 320 #ifndef DOCTEST_NOEXCEPT 321 #if DOCTEST_MSVC && (DOCTEST_MSVC < DOCTEST_COMPILER(19, 0, 0)) 322 #define DOCTEST_NOEXCEPT 323 #else // DOCTEST_MSVC 324 #define DOCTEST_NOEXCEPT noexcept 325 #endif // DOCTEST_MSVC 326 #endif // DOCTEST_NOEXCEPT 327 328 #ifndef DOCTEST_CONSTEXPR 329 #if DOCTEST_MSVC && (DOCTEST_MSVC < DOCTEST_COMPILER(19, 0, 0)) 330 #define DOCTEST_CONSTEXPR const 331 #else // DOCTEST_MSVC 332 #define DOCTEST_CONSTEXPR constexpr 333 #endif // DOCTEST_MSVC 334 #endif // DOCTEST_CONSTEXPR 335 336 // ================================================================================================= 337 // == FEATURE DETECTION END ======================================================================== 338 // ================================================================================================= 339 340 // internal macros for string concatenation and anonymous variable name generation 341 #define DOCTEST_CAT_IMPL(s1, s2) s1##s2 342 #define DOCTEST_CAT(s1, s2) DOCTEST_CAT_IMPL(s1, s2) 343 #ifdef __COUNTER__ // not standard and may be missing for some compilers 344 #define DOCTEST_ANONYMOUS(x) DOCTEST_CAT(x, __COUNTER__) 345 #else // __COUNTER__ 346 #define DOCTEST_ANONYMOUS(x) DOCTEST_CAT(x, __LINE__) 347 #endif // __COUNTER__ 348 349 #define DOCTEST_TOSTR(x) #x 350 351 #ifndef DOCTEST_CONFIG_ASSERTION_PARAMETERS_BY_VALUE 352 #define DOCTEST_REF_WRAP(x) x& 353 #else // DOCTEST_CONFIG_ASSERTION_PARAMETERS_BY_VALUE 354 #define DOCTEST_REF_WRAP(x) x 355 #endif // DOCTEST_CONFIG_ASSERTION_PARAMETERS_BY_VALUE 356 357 // not using __APPLE__ because... this is how Catch does it 358 #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED 359 #define DOCTEST_PLATFORM_MAC 360 #elif defined(__IPHONE_OS_VERSION_MIN_REQUIRED) 361 #define DOCTEST_PLATFORM_IPHONE 362 #elif defined(_WIN32) 363 #define DOCTEST_PLATFORM_WINDOWS 364 #else // DOCTEST_PLATFORM 365 #define DOCTEST_PLATFORM_LINUX 366 #endif // DOCTEST_PLATFORM 367 368 #define DOCTEST_GLOBAL_NO_WARNINGS(var) \ 369 DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wglobal-constructors") \ 370 DOCTEST_CLANG_SUPPRESS_WARNING("-Wunused-variable") \ 371 static const int var DOCTEST_UNUSED // NOLINT(fuchsia-statically-constructed-objects,cert-err58-cpp) 372 #define DOCTEST_GLOBAL_NO_WARNINGS_END() DOCTEST_CLANG_SUPPRESS_WARNING_POP 373 374 #ifndef DOCTEST_BREAK_INTO_DEBUGGER 375 // should probably take a look at https://github.com/scottt/debugbreak 376 #ifdef DOCTEST_PLATFORM_LINUX 377 #if defined(__GNUC__) && (defined(__i386) || defined(__x86_64)) 378 // Break at the location of the failing check if possible 379 #define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) // NOLINT (hicpp-no-assembler) 380 #else 381 #include <signal.h> 382 #define DOCTEST_BREAK_INTO_DEBUGGER() raise(SIGTRAP) 383 #endif 384 #elif defined(DOCTEST_PLATFORM_MAC) 385 #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(__i386) 386 #define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) // NOLINT (hicpp-no-assembler) 387 #else 388 #define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("brk #0"); // NOLINT (hicpp-no-assembler) 389 #endif 390 #elif DOCTEST_MSVC 391 #define DOCTEST_BREAK_INTO_DEBUGGER() __debugbreak() 392 #elif defined(__MINGW32__) 393 DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wredundant-decls") 394 extern "C" __declspec(dllimport) void __stdcall DebugBreak(); 395 DOCTEST_GCC_SUPPRESS_WARNING_POP 396 #define DOCTEST_BREAK_INTO_DEBUGGER() ::DebugBreak() 397 #else // linux 398 #define DOCTEST_BREAK_INTO_DEBUGGER() (static_cast<void>(0)) 399 #endif // linux 400 #endif // DOCTEST_BREAK_INTO_DEBUGGER 401 402 // this is kept here for backwards compatibility since the config option was changed 403 #ifdef DOCTEST_CONFIG_USE_IOSFWD 404 #define DOCTEST_CONFIG_USE_STD_HEADERS 405 #endif // DOCTEST_CONFIG_USE_IOSFWD 406 407 #ifdef DOCTEST_CONFIG_USE_STD_HEADERS 408 #ifndef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 409 #define DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 410 #endif // DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 411 #include <iosfwd> 412 #include <cstddef> 413 #include <ostream> 414 #else // DOCTEST_CONFIG_USE_STD_HEADERS 415 416 #if DOCTEST_CLANG 417 // to detect if libc++ is being used with clang (the _LIBCPP_VERSION identifier) 418 #include <ciso646> 419 #endif // clang 420 421 #ifdef _LIBCPP_VERSION 422 #define DOCTEST_STD_NAMESPACE_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD 423 #define DOCTEST_STD_NAMESPACE_END _LIBCPP_END_NAMESPACE_STD 424 #else // _LIBCPP_VERSION 425 #define DOCTEST_STD_NAMESPACE_BEGIN namespace std { 426 #define DOCTEST_STD_NAMESPACE_END } 427 #endif // _LIBCPP_VERSION 428 429 // Forward declaring 'X' in namespace std is not permitted by the C++ Standard. 430 DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4643) 431 432 DOCTEST_STD_NAMESPACE_BEGIN // NOLINT (cert-dcl58-cpp) 433 typedef decltype(nullptr) nullptr_t; 434 template <class charT> 435 struct char_traits; 436 template <> 437 struct char_traits<char>; 438 template <class charT, class traits> 439 class basic_ostream; 440 typedef basic_ostream<char, char_traits<char>> ostream; 441 template <class... Types> 442 class tuple; 443 #if DOCTEST_MSVC >= DOCTEST_COMPILER(19, 20, 0) 444 DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wreserved-identifier") 445 // see this issue on why this is needed: https://github.com/onqtam/doctest/issues/183 446 template <class _Ty> 447 class allocator; 448 template <class _Elem, class _Traits, class _Alloc> 449 class basic_string; 450 using string = basic_string<char, char_traits<char>, allocator<char>>; 451 DOCTEST_CLANG_SUPPRESS_WARNING_POP 452 #endif // VS 2019 453 DOCTEST_STD_NAMESPACE_END 454 455 DOCTEST_MSVC_SUPPRESS_WARNING_POP 456 457 #endif // DOCTEST_CONFIG_USE_STD_HEADERS 458 459 #ifdef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 460 #include <type_traits> 461 #endif // DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 462 463 namespace doctest { 464 465 DOCTEST_INTERFACE extern bool is_running_in_test; 466 467 // A 24 byte string class (can be as small as 17 for x64 and 13 for x86) that can hold strings with length 468 // of up to 23 chars on the stack before going on the heap - the last byte of the buffer is used for: 469 // - "is small" bit - the highest bit - if "0" then it is small - otherwise its "1" (128) 470 // - if small - capacity left before going on the heap - using the lowest 5 bits 471 // - if small - 2 bits are left unused - the second and third highest ones 472 // - if small - acts as a null terminator if strlen() is 23 (24 including the null terminator) 473 // and the "is small" bit remains "0" ("as well as the capacity left") so its OK 474 // Idea taken from this lecture about the string implementation of facebook/folly - fbstring 475 // https://www.youtube.com/watch?v=kPR8h4-qZdk 476 // TODO: 477 // - optimizations - like not deleting memory unnecessarily in operator= and etc. 478 // - resize/reserve/clear 479 // - substr 480 // - replace 481 // - back/front 482 // - iterator stuff 483 // - find & friends 484 // - push_back/pop_back 485 // - assign/insert/erase 486 // - relational operators as free functions - taking const char* as one of the params 487 class DOCTEST_INTERFACE String 488 { 489 static const unsigned len = 24; //!OCLINT avoid private static members 490 static const unsigned last = len - 1; //!OCLINT avoid private static members 491 492 struct view // len should be more than sizeof(view) - because of the final byte for flags 493 { 494 char* ptr; 495 unsigned size; 496 unsigned capacity; 497 }; 498 499 union 500 { 501 char buf[len]; 502 view data; 503 }; 504 isOnStack()505 bool isOnStack() const { return (buf[last] & 128) == 0; } 506 void setOnHeap(); 507 void setLast(unsigned in = last); 508 509 void copy(const String& other); 510 511 public: 512 String(); 513 ~String(); 514 515 // cppcheck-suppress noExplicitConstructor 516 String(const char* in); 517 String(const char* in, unsigned in_size); 518 519 String(const String& other); 520 String& operator=(const String& other); 521 522 String& operator+=(const String& other); 523 524 String(String&& other); 525 String& operator=(String&& other); 526 527 char operator[](unsigned i) const; 528 char& operator[](unsigned i); 529 530 // the only functions I'm willing to leave in the interface - available for inlining c_str()531 const char* c_str() const { return const_cast<String*>(this)->c_str(); } // NOLINT c_str()532 char* c_str() { 533 if(isOnStack()) 534 return reinterpret_cast<char*>(buf); 535 return data.ptr; 536 } 537 538 unsigned size() const; 539 unsigned capacity() const; 540 541 int compare(const char* other, bool no_case = false) const; 542 int compare(const String& other, bool no_case = false) const; 543 }; 544 545 DOCTEST_INTERFACE String operator+(const String& lhs, const String& rhs); 546 547 DOCTEST_INTERFACE bool operator==(const String& lhs, const String& rhs); 548 DOCTEST_INTERFACE bool operator!=(const String& lhs, const String& rhs); 549 DOCTEST_INTERFACE bool operator<(const String& lhs, const String& rhs); 550 DOCTEST_INTERFACE bool operator>(const String& lhs, const String& rhs); 551 DOCTEST_INTERFACE bool operator<=(const String& lhs, const String& rhs); 552 DOCTEST_INTERFACE bool operator>=(const String& lhs, const String& rhs); 553 554 DOCTEST_INTERFACE std::ostream& operator<<(std::ostream& s, const String& in); 555 556 namespace Color { 557 enum Enum 558 { 559 None = 0, 560 White, 561 Red, 562 Green, 563 Blue, 564 Cyan, 565 Yellow, 566 Grey, 567 568 Bright = 0x10, 569 570 BrightRed = Bright | Red, 571 BrightGreen = Bright | Green, 572 LightGrey = Bright | Grey, 573 BrightWhite = Bright | White 574 }; 575 576 DOCTEST_INTERFACE std::ostream& operator<<(std::ostream& s, Color::Enum code); 577 } // namespace Color 578 579 namespace assertType { 580 enum Enum 581 { 582 // macro traits 583 584 is_warn = 1, 585 is_check = 2 * is_warn, 586 is_require = 2 * is_check, 587 588 is_normal = 2 * is_require, 589 is_throws = 2 * is_normal, 590 is_throws_as = 2 * is_throws, 591 is_throws_with = 2 * is_throws_as, 592 is_nothrow = 2 * is_throws_with, 593 594 is_false = 2 * is_nothrow, 595 is_unary = 2 * is_false, // not checked anywhere - used just to distinguish the types 596 597 is_eq = 2 * is_unary, 598 is_ne = 2 * is_eq, 599 600 is_lt = 2 * is_ne, 601 is_gt = 2 * is_lt, 602 603 is_ge = 2 * is_gt, 604 is_le = 2 * is_ge, 605 606 // macro types 607 608 DT_WARN = is_normal | is_warn, 609 DT_CHECK = is_normal | is_check, 610 DT_REQUIRE = is_normal | is_require, 611 612 DT_WARN_FALSE = is_normal | is_false | is_warn, 613 DT_CHECK_FALSE = is_normal | is_false | is_check, 614 DT_REQUIRE_FALSE = is_normal | is_false | is_require, 615 616 DT_WARN_THROWS = is_throws | is_warn, 617 DT_CHECK_THROWS = is_throws | is_check, 618 DT_REQUIRE_THROWS = is_throws | is_require, 619 620 DT_WARN_THROWS_AS = is_throws_as | is_warn, 621 DT_CHECK_THROWS_AS = is_throws_as | is_check, 622 DT_REQUIRE_THROWS_AS = is_throws_as | is_require, 623 624 DT_WARN_THROWS_WITH = is_throws_with | is_warn, 625 DT_CHECK_THROWS_WITH = is_throws_with | is_check, 626 DT_REQUIRE_THROWS_WITH = is_throws_with | is_require, 627 628 DT_WARN_THROWS_WITH_AS = is_throws_with | is_throws_as | is_warn, 629 DT_CHECK_THROWS_WITH_AS = is_throws_with | is_throws_as | is_check, 630 DT_REQUIRE_THROWS_WITH_AS = is_throws_with | is_throws_as | is_require, 631 632 DT_WARN_NOTHROW = is_nothrow | is_warn, 633 DT_CHECK_NOTHROW = is_nothrow | is_check, 634 DT_REQUIRE_NOTHROW = is_nothrow | is_require, 635 636 DT_WARN_EQ = is_normal | is_eq | is_warn, 637 DT_CHECK_EQ = is_normal | is_eq | is_check, 638 DT_REQUIRE_EQ = is_normal | is_eq | is_require, 639 640 DT_WARN_NE = is_normal | is_ne | is_warn, 641 DT_CHECK_NE = is_normal | is_ne | is_check, 642 DT_REQUIRE_NE = is_normal | is_ne | is_require, 643 644 DT_WARN_GT = is_normal | is_gt | is_warn, 645 DT_CHECK_GT = is_normal | is_gt | is_check, 646 DT_REQUIRE_GT = is_normal | is_gt | is_require, 647 648 DT_WARN_LT = is_normal | is_lt | is_warn, 649 DT_CHECK_LT = is_normal | is_lt | is_check, 650 DT_REQUIRE_LT = is_normal | is_lt | is_require, 651 652 DT_WARN_GE = is_normal | is_ge | is_warn, 653 DT_CHECK_GE = is_normal | is_ge | is_check, 654 DT_REQUIRE_GE = is_normal | is_ge | is_require, 655 656 DT_WARN_LE = is_normal | is_le | is_warn, 657 DT_CHECK_LE = is_normal | is_le | is_check, 658 DT_REQUIRE_LE = is_normal | is_le | is_require, 659 660 DT_WARN_UNARY = is_normal | is_unary | is_warn, 661 DT_CHECK_UNARY = is_normal | is_unary | is_check, 662 DT_REQUIRE_UNARY = is_normal | is_unary | is_require, 663 664 DT_WARN_UNARY_FALSE = is_normal | is_false | is_unary | is_warn, 665 DT_CHECK_UNARY_FALSE = is_normal | is_false | is_unary | is_check, 666 DT_REQUIRE_UNARY_FALSE = is_normal | is_false | is_unary | is_require, 667 }; 668 } // namespace assertType 669 670 DOCTEST_INTERFACE const char* assertString(assertType::Enum at); 671 DOCTEST_INTERFACE const char* failureString(assertType::Enum at); 672 DOCTEST_INTERFACE const char* skipPathFromFilename(const char* file); 673 674 struct DOCTEST_INTERFACE TestCaseData 675 { 676 String m_file; // the file in which the test was registered (using String - see #350) 677 unsigned m_line; // the line where the test was registered 678 const char* m_name; // name of the test case 679 const char* m_test_suite; // the test suite in which the test was added 680 const char* m_description; 681 bool m_skip; 682 bool m_no_breaks; 683 bool m_no_output; 684 bool m_may_fail; 685 bool m_should_fail; 686 int m_expected_failures; 687 double m_timeout; 688 }; 689 690 struct DOCTEST_INTERFACE AssertData 691 { 692 // common - for all asserts 693 const TestCaseData* m_test_case; 694 assertType::Enum m_at; 695 const char* m_file; 696 int m_line; 697 const char* m_expr; 698 bool m_failed; 699 700 // exception-related - for all asserts 701 bool m_threw; 702 String m_exception; 703 704 // for normal asserts 705 String m_decomp; 706 707 // for specific exception-related asserts 708 bool m_threw_as; 709 const char* m_exception_type; 710 const char* m_exception_string; 711 }; 712 713 struct DOCTEST_INTERFACE MessageData 714 { 715 String m_string; 716 const char* m_file; 717 int m_line; 718 assertType::Enum m_severity; 719 }; 720 721 struct DOCTEST_INTERFACE SubcaseSignature 722 { 723 String m_name; 724 const char* m_file; 725 int m_line; 726 727 bool operator<(const SubcaseSignature& other) const; 728 }; 729 730 struct DOCTEST_INTERFACE IContextScope 731 { 732 IContextScope(); 733 virtual ~IContextScope(); 734 virtual void stringify(std::ostream*) const = 0; 735 }; 736 737 namespace detail { 738 struct DOCTEST_INTERFACE TestCase; 739 } // namespace detail 740 741 struct ContextOptions //!OCLINT too many fields 742 { 743 std::ostream* cout = nullptr; // stdout stream 744 String binary_name; // the test binary name 745 746 const detail::TestCase* currentTest = nullptr; 747 748 // == parameters from the command line 749 String out; // output filename 750 String order_by; // how tests should be ordered 751 unsigned rand_seed; // the seed for rand ordering 752 753 unsigned first; // the first (matching) test to be executed 754 unsigned last; // the last (matching) test to be executed 755 756 int abort_after; // stop tests after this many failed assertions 757 int subcase_filter_levels; // apply the subcase filters for the first N levels 758 759 bool success; // include successful assertions in output 760 bool case_sensitive; // if filtering should be case sensitive 761 bool exit; // if the program should be exited after the tests are ran/whatever 762 bool duration; // print the time duration of each test case 763 bool minimal; // minimal console output (only test failures) 764 bool quiet; // no console output 765 bool no_throw; // to skip exceptions-related assertion macros 766 bool no_exitcode; // if the framework should return 0 as the exitcode 767 bool no_run; // to not run the tests at all (can be done with an "*" exclude) 768 bool no_intro; // to not print the intro of the framework 769 bool no_version; // to not print the version of the framework 770 bool no_colors; // if output to the console should be colorized 771 bool force_colors; // forces the use of colors even when a tty cannot be detected 772 bool no_breaks; // to not break into the debugger 773 bool no_skip; // don't skip test cases which are marked to be skipped 774 bool gnu_file_line; // if line numbers should be surrounded with :x: and not (x): 775 bool no_path_in_filenames; // if the path to files should be removed from the output 776 bool no_line_numbers; // if source code line numbers should be omitted from the output 777 bool no_debug_output; // no output in the debug console when a debugger is attached 778 bool no_skipped_summary; // don't print "skipped" in the summary !!! UNDOCUMENTED !!! 779 bool no_time_in_output; // omit any time/timestamps from output !!! UNDOCUMENTED !!! 780 781 bool help; // to print the help 782 bool version; // to print the version 783 bool count; // if only the count of matching tests is to be retrieved 784 bool list_test_cases; // to list all tests matching the filters 785 bool list_test_suites; // to list all suites matching the filters 786 bool list_reporters; // lists all registered reporters 787 }; 788 789 namespace detail { 790 template <bool CONDITION, typename TYPE = void> 791 struct enable_if 792 {}; 793 794 template <typename TYPE> 795 struct enable_if<true, TYPE> 796 { typedef TYPE type; }; 797 798 // clang-format off 799 template<class T> struct remove_reference { typedef T type; }; 800 template<class T> struct remove_reference<T&> { typedef T type; }; 801 template<class T> struct remove_reference<T&&> { typedef T type; }; 802 803 template<typename T, typename U = T&&> U declval(int); 804 805 template<typename T> T declval(long); 806 807 template<typename T> auto declval() DOCTEST_NOEXCEPT -> decltype(declval<T>(0)) ; 808 809 template<class T> struct is_lvalue_reference { const static bool value=false; }; 810 template<class T> struct is_lvalue_reference<T&> { const static bool value=true; }; 811 812 template<class T> struct is_rvalue_reference { const static bool value=false; }; 813 template<class T> struct is_rvalue_reference<T&&> { const static bool value=true; }; 814 815 template <class T> 816 inline T&& forward(typename remove_reference<T>::type& t) DOCTEST_NOEXCEPT 817 { 818 return static_cast<T&&>(t); 819 } 820 821 template <class T> 822 inline T&& forward(typename remove_reference<T>::type&& t) DOCTEST_NOEXCEPT 823 { 824 static_assert(!is_lvalue_reference<T>::value, 825 "Can not forward an rvalue as an lvalue."); 826 return static_cast<T&&>(t); 827 } 828 829 template<class T> struct remove_const { typedef T type; }; 830 template<class T> struct remove_const<const T> { typedef T type; }; 831 #ifdef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 832 template<class T> struct is_enum : public std::is_enum<T> {}; 833 template<class T> struct underlying_type : public std::underlying_type<T> {}; 834 #else 835 // Use compiler intrinsics 836 template<class T> struct is_enum { DOCTEST_CONSTEXPR static bool value = __is_enum(T); }; 837 template<class T> struct underlying_type { typedef __underlying_type(T) type; }; 838 #endif 839 // clang-format on 840 841 template <typename T> 842 struct deferred_false 843 // cppcheck-suppress unusedStructMember 844 { static const bool value = false; }; 845 846 namespace has_insertion_operator_impl { 847 std::ostream &os(); 848 template<class T> 849 DOCTEST_REF_WRAP(T) val(); 850 851 template<class, class = void> 852 struct check { 853 static DOCTEST_CONSTEXPR bool value = false; 854 }; 855 856 template<class T> 857 struct check<T, decltype(os() << val<T>(), void())> { 858 static DOCTEST_CONSTEXPR bool value = true; 859 }; 860 } // namespace has_insertion_operator_impl 861 862 template<class T> 863 using has_insertion_operator = has_insertion_operator_impl::check<const T>; 864 865 DOCTEST_INTERFACE void my_memcpy(void* dest, const void* src, unsigned num); 866 867 DOCTEST_INTERFACE std::ostream* getTlsOss(bool reset=true); // returns a thread-local ostringstream 868 DOCTEST_INTERFACE String getTlsOssResult(); 869 870 871 template <bool C> 872 struct StringMakerBase 873 { 874 template <typename T> 875 static String convert(const DOCTEST_REF_WRAP(T)) { 876 return "{?}"; 877 } 878 }; 879 880 // Vector<int> and various type other than pointer or array. 881 template<typename T> 882 struct filldata 883 { 884 static void fill(const T &in) { 885 *getTlsOss() << in; 886 } 887 }; 888 889 /* This method can be chained */ 890 template<typename T,unsigned long N> 891 void fillstream(const T (&in)[N] ) { 892 for(unsigned long i = 0; i < N; i++) { 893 *getTlsOss(false) << in[i]; 894 } 895 } 896 897 template<typename T,unsigned long N> 898 struct filldata<T[N]> 899 { 900 static void fill(const T (&in)[N]) { 901 fillstream(in); 902 *getTlsOss(false)<<""; 903 } 904 }; 905 906 template<typename T> 907 void filloss(const T& in){ 908 filldata<T>::fill(in); 909 } 910 911 template<typename T,unsigned long N> 912 void filloss(const T (&in)[N]) { 913 // T[N], T(&)[N], T(&&)[N] have same behaviour. 914 // Hence remove reference. 915 filldata<typename remove_reference <decltype(in)>::type >::fill(in); 916 } 917 918 template <> 919 struct StringMakerBase<true> 920 { 921 template <typename T> 922 static String convert(const DOCTEST_REF_WRAP(T) in) { 923 /* When parameter "in" is a null terminated const char* it works. 924 * When parameter "in" is a T arr[N] without '\0' we can fill the 925 * stringstream with N objects (T=char).If in is char pointer * 926 * without '\0' , it would cause segfault 927 * stepping over unaccessible memory. 928 */ 929 930 filloss(in); 931 return getTlsOssResult(); 932 } 933 }; 934 935 DOCTEST_INTERFACE String rawMemoryToString(const void* object, unsigned size); 936 937 template <typename T> 938 String rawMemoryToString(const DOCTEST_REF_WRAP(T) object) { 939 return rawMemoryToString(&object, sizeof(object)); 940 } 941 942 template <typename T> 943 const char* type_to_string() { 944 return "<>"; 945 } 946 } // namespace detail 947 948 template <typename T> 949 struct StringMaker : public detail::StringMakerBase<detail::has_insertion_operator<T>::value> 950 {}; 951 952 template <typename T> 953 struct StringMaker<T*> 954 { 955 template <typename U> 956 static String convert(U* p) { 957 if(p) 958 return detail::rawMemoryToString(p); 959 return "NULL"; 960 } 961 }; 962 963 template <typename R, typename C> 964 struct StringMaker<R C::*> 965 { 966 static String convert(R C::*p) { 967 if(p) 968 return detail::rawMemoryToString(p); 969 return "NULL"; 970 } 971 }; 972 973 template <typename T, typename detail::enable_if<!detail::is_enum<T>::value, bool>::type = true> 974 String toString(const DOCTEST_REF_WRAP(T) value) { 975 return StringMaker<T>::convert(value); 976 } 977 978 #ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 979 DOCTEST_INTERFACE String toString(char* in); 980 DOCTEST_INTERFACE String toString(const char* in); 981 #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 982 DOCTEST_INTERFACE String toString(bool in); 983 DOCTEST_INTERFACE String toString(float in); 984 DOCTEST_INTERFACE String toString(double in); 985 DOCTEST_INTERFACE String toString(double long in); 986 987 DOCTEST_INTERFACE String toString(char in); 988 DOCTEST_INTERFACE String toString(char signed in); 989 DOCTEST_INTERFACE String toString(char unsigned in); 990 DOCTEST_INTERFACE String toString(int short in); 991 DOCTEST_INTERFACE String toString(int short unsigned in); 992 DOCTEST_INTERFACE String toString(int in); 993 DOCTEST_INTERFACE String toString(int unsigned in); 994 DOCTEST_INTERFACE String toString(int long in); 995 DOCTEST_INTERFACE String toString(int long unsigned in); 996 DOCTEST_INTERFACE String toString(int long long in); 997 DOCTEST_INTERFACE String toString(int long long unsigned in); 998 DOCTEST_INTERFACE String toString(std::nullptr_t in); 999 1000 template <typename T, typename detail::enable_if<detail::is_enum<T>::value, bool>::type = true> 1001 String toString(const DOCTEST_REF_WRAP(T) value) { 1002 typedef typename detail::underlying_type<T>::type UT; 1003 return toString(static_cast<UT>(value)); 1004 } 1005 1006 #if DOCTEST_MSVC >= DOCTEST_COMPILER(19, 20, 0) 1007 // see this issue on why this is needed: https://github.com/onqtam/doctest/issues/183 1008 DOCTEST_INTERFACE String toString(const std::string& in); 1009 #endif // VS 2019 1010 1011 class DOCTEST_INTERFACE Approx 1012 { 1013 public: 1014 explicit Approx(double value); 1015 1016 Approx operator()(double value) const; 1017 1018 #ifdef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 1019 template <typename T> 1020 explicit Approx(const T& value, 1021 typename detail::enable_if<std::is_constructible<double, T>::value>::type* = 1022 static_cast<T*>(nullptr)) { 1023 *this = Approx(static_cast<double>(value)); 1024 } 1025 #endif // DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 1026 1027 Approx& epsilon(double newEpsilon); 1028 1029 #ifdef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 1030 template <typename T> 1031 typename detail::enable_if<std::is_constructible<double, T>::value, Approx&>::type epsilon( 1032 const T& newEpsilon) { 1033 m_epsilon = static_cast<double>(newEpsilon); 1034 return *this; 1035 } 1036 #endif // DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 1037 1038 Approx& scale(double newScale); 1039 1040 #ifdef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 1041 template <typename T> 1042 typename detail::enable_if<std::is_constructible<double, T>::value, Approx&>::type scale( 1043 const T& newScale) { 1044 m_scale = static_cast<double>(newScale); 1045 return *this; 1046 } 1047 #endif // DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 1048 1049 // clang-format off 1050 DOCTEST_INTERFACE friend bool operator==(double lhs, const Approx & rhs); 1051 DOCTEST_INTERFACE friend bool operator==(const Approx & lhs, double rhs); 1052 DOCTEST_INTERFACE friend bool operator!=(double lhs, const Approx & rhs); 1053 DOCTEST_INTERFACE friend bool operator!=(const Approx & lhs, double rhs); 1054 DOCTEST_INTERFACE friend bool operator<=(double lhs, const Approx & rhs); 1055 DOCTEST_INTERFACE friend bool operator<=(const Approx & lhs, double rhs); 1056 DOCTEST_INTERFACE friend bool operator>=(double lhs, const Approx & rhs); 1057 DOCTEST_INTERFACE friend bool operator>=(const Approx & lhs, double rhs); 1058 DOCTEST_INTERFACE friend bool operator< (double lhs, const Approx & rhs); 1059 DOCTEST_INTERFACE friend bool operator< (const Approx & lhs, double rhs); 1060 DOCTEST_INTERFACE friend bool operator> (double lhs, const Approx & rhs); 1061 DOCTEST_INTERFACE friend bool operator> (const Approx & lhs, double rhs); 1062 1063 DOCTEST_INTERFACE friend String toString(const Approx& in); 1064 1065 #ifdef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 1066 #define DOCTEST_APPROX_PREFIX \ 1067 template <typename T> friend typename detail::enable_if<std::is_constructible<double, T>::value, bool>::type 1068 1069 DOCTEST_APPROX_PREFIX operator==(const T& lhs, const Approx& rhs) { return operator==(double(lhs), rhs); } 1070 DOCTEST_APPROX_PREFIX operator==(const Approx& lhs, const T& rhs) { return operator==(rhs, lhs); } 1071 DOCTEST_APPROX_PREFIX operator!=(const T& lhs, const Approx& rhs) { return !operator==(lhs, rhs); } 1072 DOCTEST_APPROX_PREFIX operator!=(const Approx& lhs, const T& rhs) { return !operator==(rhs, lhs); } 1073 DOCTEST_APPROX_PREFIX operator<=(const T& lhs, const Approx& rhs) { return double(lhs) < rhs.m_value || lhs == rhs; } 1074 DOCTEST_APPROX_PREFIX operator<=(const Approx& lhs, const T& rhs) { return lhs.m_value < double(rhs) || lhs == rhs; } 1075 DOCTEST_APPROX_PREFIX operator>=(const T& lhs, const Approx& rhs) { return double(lhs) > rhs.m_value || lhs == rhs; } 1076 DOCTEST_APPROX_PREFIX operator>=(const Approx& lhs, const T& rhs) { return lhs.m_value > double(rhs) || lhs == rhs; } 1077 DOCTEST_APPROX_PREFIX operator< (const T& lhs, const Approx& rhs) { return double(lhs) < rhs.m_value && lhs != rhs; } 1078 DOCTEST_APPROX_PREFIX operator< (const Approx& lhs, const T& rhs) { return lhs.m_value < double(rhs) && lhs != rhs; } 1079 DOCTEST_APPROX_PREFIX operator> (const T& lhs, const Approx& rhs) { return double(lhs) > rhs.m_value && lhs != rhs; } 1080 DOCTEST_APPROX_PREFIX operator> (const Approx& lhs, const T& rhs) { return lhs.m_value > double(rhs) && lhs != rhs; } 1081 #undef DOCTEST_APPROX_PREFIX 1082 #endif // DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS 1083 1084 // clang-format on 1085 1086 private: 1087 double m_epsilon; 1088 double m_scale; 1089 double m_value; 1090 }; 1091 1092 DOCTEST_INTERFACE String toString(const Approx& in); 1093 1094 DOCTEST_INTERFACE const ContextOptions* getContextOptions(); 1095 1096 #if !defined(DOCTEST_CONFIG_DISABLE) 1097 1098 namespace detail { 1099 // clang-format off 1100 #ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 1101 template<class T> struct decay_array { typedef T type; }; 1102 template<class T, unsigned N> struct decay_array<T[N]> { typedef T* type; }; 1103 template<class T> struct decay_array<T[]> { typedef T* type; }; 1104 1105 template<class T> struct not_char_pointer { enum { value = 1 }; }; 1106 template<> struct not_char_pointer<char*> { enum { value = 0 }; }; 1107 template<> struct not_char_pointer<const char*> { enum { value = 0 }; }; 1108 1109 template<class T> struct can_use_op : public not_char_pointer<typename decay_array<T>::type> {}; 1110 #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 1111 // clang-format on 1112 1113 struct DOCTEST_INTERFACE TestFailureException 1114 { 1115 }; 1116 1117 DOCTEST_INTERFACE bool checkIfShouldThrow(assertType::Enum at); 1118 1119 #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS 1120 DOCTEST_NORETURN 1121 #endif // DOCTEST_CONFIG_NO_EXCEPTIONS 1122 DOCTEST_INTERFACE void throwException(); 1123 1124 struct DOCTEST_INTERFACE Subcase 1125 { 1126 SubcaseSignature m_signature; 1127 bool m_entered = false; 1128 1129 Subcase(const String& name, const char* file, int line); 1130 ~Subcase(); 1131 1132 operator bool() const; 1133 }; 1134 1135 template <typename L, typename R> 1136 String stringifyBinaryExpr(const DOCTEST_REF_WRAP(L) lhs, const char* op, 1137 const DOCTEST_REF_WRAP(R) rhs) { 1138 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) 1139 return toString(lhs) + op + toString(rhs); 1140 } 1141 1142 #if DOCTEST_CLANG && DOCTEST_CLANG < DOCTEST_COMPILER(3, 6, 0) 1143 DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wunused-comparison") 1144 #endif 1145 1146 // This will check if there is any way it could find a operator like member or friend and uses it. 1147 // If not it doesn't find the operator or if the operator at global scope is defined after 1148 // this template, the template won't be instantiated due to SFINAE. Once the template is not 1149 // instantiated it can look for global operator using normal conversions. 1150 #define SFINAE_OP(ret,op) decltype((void)(doctest::detail::declval<L>() op doctest::detail::declval<R>()),ret{}) 1151 1152 #define DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(op, op_str, op_macro) \ 1153 template <typename R> \ 1154 DOCTEST_NOINLINE SFINAE_OP(Result,op) operator op(const R&& rhs) { \ 1155 bool res = op_macro(doctest::detail::forward<const L>(lhs), doctest::detail::forward<const R>(rhs)); \ 1156 if(m_at & assertType::is_false) \ 1157 res = !res; \ 1158 if(!res || doctest::getContextOptions()->success) \ 1159 return Result(res, stringifyBinaryExpr(lhs, op_str, rhs)); \ 1160 return Result(res); \ 1161 } \ 1162 template <typename R ,typename enable_if< !doctest::detail::is_rvalue_reference<R>::value , void >::type* = nullptr> \ 1163 DOCTEST_NOINLINE SFINAE_OP(Result,op) operator op(const R& rhs) { \ 1164 bool res = op_macro(doctest::detail::forward<const L>(lhs), doctest::detail::forward<const R>(rhs)); \ 1165 if(m_at & assertType::is_false) \ 1166 res = !res; \ 1167 if(!res || doctest::getContextOptions()->success) \ 1168 return Result(res, stringifyBinaryExpr(lhs, op_str, rhs)); \ 1169 return Result(res); \ 1170 } 1171 1172 1173 // more checks could be added - like in Catch: 1174 // https://github.com/catchorg/Catch2/pull/1480/files 1175 // https://github.com/catchorg/Catch2/pull/1481/files 1176 #define DOCTEST_FORBIT_EXPRESSION(rt, op) \ 1177 template <typename R> \ 1178 rt& operator op(const R&) { \ 1179 static_assert(deferred_false<R>::value, \ 1180 "Expression Too Complex Please Rewrite As Binary Comparison!"); \ 1181 return *this; \ 1182 } 1183 1184 struct DOCTEST_INTERFACE Result 1185 { 1186 bool m_passed; 1187 String m_decomp; 1188 1189 Result() = default; 1190 Result(bool passed, const String& decomposition = String()); 1191 1192 // forbidding some expressions based on this table: https://en.cppreference.com/w/cpp/language/operator_precedence 1193 DOCTEST_FORBIT_EXPRESSION(Result, &) 1194 DOCTEST_FORBIT_EXPRESSION(Result, ^) 1195 DOCTEST_FORBIT_EXPRESSION(Result, |) 1196 DOCTEST_FORBIT_EXPRESSION(Result, &&) 1197 DOCTEST_FORBIT_EXPRESSION(Result, ||) 1198 DOCTEST_FORBIT_EXPRESSION(Result, ==) 1199 DOCTEST_FORBIT_EXPRESSION(Result, !=) 1200 DOCTEST_FORBIT_EXPRESSION(Result, <) 1201 DOCTEST_FORBIT_EXPRESSION(Result, >) 1202 DOCTEST_FORBIT_EXPRESSION(Result, <=) 1203 DOCTEST_FORBIT_EXPRESSION(Result, >=) 1204 DOCTEST_FORBIT_EXPRESSION(Result, =) 1205 DOCTEST_FORBIT_EXPRESSION(Result, +=) 1206 DOCTEST_FORBIT_EXPRESSION(Result, -=) 1207 DOCTEST_FORBIT_EXPRESSION(Result, *=) 1208 DOCTEST_FORBIT_EXPRESSION(Result, /=) 1209 DOCTEST_FORBIT_EXPRESSION(Result, %=) 1210 DOCTEST_FORBIT_EXPRESSION(Result, <<=) 1211 DOCTEST_FORBIT_EXPRESSION(Result, >>=) 1212 DOCTEST_FORBIT_EXPRESSION(Result, &=) 1213 DOCTEST_FORBIT_EXPRESSION(Result, ^=) 1214 DOCTEST_FORBIT_EXPRESSION(Result, |=) 1215 }; 1216 1217 #ifndef DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION 1218 1219 DOCTEST_CLANG_SUPPRESS_WARNING_PUSH 1220 DOCTEST_CLANG_SUPPRESS_WARNING("-Wsign-conversion") 1221 DOCTEST_CLANG_SUPPRESS_WARNING("-Wsign-compare") 1222 //DOCTEST_CLANG_SUPPRESS_WARNING("-Wdouble-promotion") 1223 //DOCTEST_CLANG_SUPPRESS_WARNING("-Wconversion") 1224 //DOCTEST_CLANG_SUPPRESS_WARNING("-Wfloat-equal") 1225 1226 DOCTEST_GCC_SUPPRESS_WARNING_PUSH 1227 DOCTEST_GCC_SUPPRESS_WARNING("-Wsign-conversion") 1228 DOCTEST_GCC_SUPPRESS_WARNING("-Wsign-compare") 1229 //DOCTEST_GCC_SUPPRESS_WARNING("-Wdouble-promotion") 1230 //DOCTEST_GCC_SUPPRESS_WARNING("-Wconversion") 1231 //DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal") 1232 1233 DOCTEST_MSVC_SUPPRESS_WARNING_PUSH 1234 // https://stackoverflow.com/questions/39479163 what's the difference between 4018 and 4389 1235 DOCTEST_MSVC_SUPPRESS_WARNING(4388) // signed/unsigned mismatch 1236 DOCTEST_MSVC_SUPPRESS_WARNING(4389) // 'operator' : signed/unsigned mismatch 1237 DOCTEST_MSVC_SUPPRESS_WARNING(4018) // 'expression' : signed/unsigned mismatch 1238 //DOCTEST_MSVC_SUPPRESS_WARNING(4805) // 'operation' : unsafe mix of type 'type' and type 'type' in operation 1239 1240 #endif // DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION 1241 1242 // clang-format off 1243 #ifndef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 1244 #define DOCTEST_COMPARISON_RETURN_TYPE bool 1245 #else // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 1246 #define DOCTEST_COMPARISON_RETURN_TYPE typename enable_if<can_use_op<L>::value || can_use_op<R>::value, bool>::type 1247 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) 1248 inline bool eq(const char* lhs, const char* rhs) { return String(lhs) == String(rhs); } 1249 inline bool ne(const char* lhs, const char* rhs) { return String(lhs) != String(rhs); } 1250 inline bool lt(const char* lhs, const char* rhs) { return String(lhs) < String(rhs); } 1251 inline bool gt(const char* lhs, const char* rhs) { return String(lhs) > String(rhs); } 1252 inline bool le(const char* lhs, const char* rhs) { return String(lhs) <= String(rhs); } 1253 inline bool ge(const char* lhs, const char* rhs) { return String(lhs) >= String(rhs); } 1254 #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 1255 // clang-format on 1256 1257 #define DOCTEST_RELATIONAL_OP(name, op) \ 1258 template <typename L, typename R> \ 1259 DOCTEST_COMPARISON_RETURN_TYPE name(const DOCTEST_REF_WRAP(L) lhs, \ 1260 const DOCTEST_REF_WRAP(R) rhs) { \ 1261 return lhs op rhs; \ 1262 } 1263 1264 DOCTEST_RELATIONAL_OP(eq, ==) 1265 DOCTEST_RELATIONAL_OP(ne, !=) 1266 DOCTEST_RELATIONAL_OP(lt, <) 1267 DOCTEST_RELATIONAL_OP(gt, >) 1268 DOCTEST_RELATIONAL_OP(le, <=) 1269 DOCTEST_RELATIONAL_OP(ge, >=) 1270 1271 #ifndef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 1272 #define DOCTEST_CMP_EQ(l, r) l == r 1273 #define DOCTEST_CMP_NE(l, r) l != r 1274 #define DOCTEST_CMP_GT(l, r) l > r 1275 #define DOCTEST_CMP_LT(l, r) l < r 1276 #define DOCTEST_CMP_GE(l, r) l >= r 1277 #define DOCTEST_CMP_LE(l, r) l <= r 1278 #else // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 1279 #define DOCTEST_CMP_EQ(l, r) eq(l, r) 1280 #define DOCTEST_CMP_NE(l, r) ne(l, r) 1281 #define DOCTEST_CMP_GT(l, r) gt(l, r) 1282 #define DOCTEST_CMP_LT(l, r) lt(l, r) 1283 #define DOCTEST_CMP_GE(l, r) ge(l, r) 1284 #define DOCTEST_CMP_LE(l, r) le(l, r) 1285 #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 1286 1287 template <typename L> 1288 // cppcheck-suppress copyCtorAndEqOperator 1289 struct Expression_lhs 1290 { 1291 L lhs; 1292 assertType::Enum m_at; 1293 1294 explicit Expression_lhs(L&& in, assertType::Enum at) 1295 : lhs(doctest::detail::forward<L>(in)) 1296 , m_at(at) {} 1297 1298 DOCTEST_NOINLINE operator Result() { 1299 // this is needed only for MSVC 2015: 1300 // https://ci.appveyor.com/project/onqtam/doctest/builds/38181202 1301 DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4800) // 'int': forcing value to bool 1302 bool res = static_cast<bool>(lhs); 1303 DOCTEST_MSVC_SUPPRESS_WARNING_POP 1304 if(m_at & assertType::is_false) //!OCLINT bitwise operator in conditional 1305 res = !res; 1306 1307 if(!res || getContextOptions()->success) 1308 return Result(res, toString(lhs)); 1309 return Result(res); 1310 } 1311 1312 /* This is required for user-defined conversions from Expression_lhs to L */ 1313 //operator L() const { return lhs; } 1314 operator L() const { return lhs; } 1315 1316 // clang-format off 1317 DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(==, " == ", DOCTEST_CMP_EQ) //!OCLINT bitwise operator in conditional 1318 DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(!=, " != ", DOCTEST_CMP_NE) //!OCLINT bitwise operator in conditional 1319 DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(>, " > ", DOCTEST_CMP_GT) //!OCLINT bitwise operator in conditional 1320 DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(<, " < ", DOCTEST_CMP_LT) //!OCLINT bitwise operator in conditional 1321 DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(>=, " >= ", DOCTEST_CMP_GE) //!OCLINT bitwise operator in conditional 1322 DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(<=, " <= ", DOCTEST_CMP_LE) //!OCLINT bitwise operator in conditional 1323 // clang-format on 1324 1325 // forbidding some expressions based on this table: https://en.cppreference.com/w/cpp/language/operator_precedence 1326 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, &) 1327 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, ^) 1328 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, |) 1329 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, &&) 1330 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, ||) 1331 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, =) 1332 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, +=) 1333 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, -=) 1334 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, *=) 1335 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, /=) 1336 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, %=) 1337 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, <<=) 1338 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, >>=) 1339 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, &=) 1340 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, ^=) 1341 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, |=) 1342 // these 2 are unfortunate because they should be allowed - they have higher precedence over the comparisons, but the 1343 // ExpressionDecomposer class uses the left shift operator to capture the left operand of the binary expression... 1344 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, <<) 1345 DOCTEST_FORBIT_EXPRESSION(Expression_lhs, >>) 1346 }; 1347 1348 #ifndef DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION 1349 1350 DOCTEST_CLANG_SUPPRESS_WARNING_POP 1351 DOCTEST_MSVC_SUPPRESS_WARNING_POP 1352 DOCTEST_GCC_SUPPRESS_WARNING_POP 1353 1354 #endif // DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION 1355 1356 #if DOCTEST_CLANG && DOCTEST_CLANG < DOCTEST_COMPILER(3, 6, 0) 1357 DOCTEST_CLANG_SUPPRESS_WARNING_POP 1358 #endif 1359 1360 struct DOCTEST_INTERFACE ExpressionDecomposer 1361 { 1362 assertType::Enum m_at; 1363 1364 ExpressionDecomposer(assertType::Enum at); 1365 1366 // The right operator for capturing expressions is "<=" instead of "<<" (based on the operator precedence table) 1367 // but then there will be warnings from GCC about "-Wparentheses" and since "_Pragma()" is problematic this will stay for now... 1368 // https://github.com/catchorg/Catch2/issues/870 1369 // https://github.com/catchorg/Catch2/issues/565 1370 template <typename L> 1371 Expression_lhs<const L> operator<<(const L &&operand) { 1372 return Expression_lhs<const L>(doctest::detail::forward<const L>(operand), m_at); 1373 } 1374 1375 template <typename L,typename enable_if<!doctest::detail::is_rvalue_reference<L>::value,void >::type* = nullptr> 1376 Expression_lhs<const L&> operator<<(const L &operand) { 1377 return Expression_lhs<const L&>(operand, m_at); 1378 } 1379 }; 1380 1381 struct DOCTEST_INTERFACE TestSuite 1382 { 1383 const char* m_test_suite = nullptr; 1384 const char* m_description = nullptr; 1385 bool m_skip = false; 1386 bool m_no_breaks = false; 1387 bool m_no_output = false; 1388 bool m_may_fail = false; 1389 bool m_should_fail = false; 1390 int m_expected_failures = 0; 1391 double m_timeout = 0; 1392 1393 TestSuite& operator*(const char* in); 1394 1395 template <typename T> 1396 TestSuite& operator*(const T& in) { 1397 in.fill(*this); 1398 return *this; 1399 } 1400 }; 1401 1402 typedef void (*funcType)(); 1403 1404 struct DOCTEST_INTERFACE TestCase : public TestCaseData 1405 { 1406 funcType m_test; // a function pointer to the test case 1407 1408 const char* m_type; // for templated test cases - gets appended to the real name 1409 int m_template_id; // an ID used to distinguish between the different versions of a templated test case 1410 String m_full_name; // contains the name (only for templated test cases!) + the template type 1411 1412 TestCase(funcType test, const char* file, unsigned line, const TestSuite& test_suite, 1413 const char* type = "", int template_id = -1); 1414 1415 TestCase(const TestCase& other); 1416 1417 DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(26434) // hides a non-virtual function 1418 TestCase& operator=(const TestCase& other); 1419 DOCTEST_MSVC_SUPPRESS_WARNING_POP 1420 1421 TestCase& operator*(const char* in); 1422 1423 template <typename T> 1424 TestCase& operator*(const T& in) { 1425 in.fill(*this); 1426 return *this; 1427 } 1428 1429 bool operator<(const TestCase& other) const; 1430 }; 1431 1432 // forward declarations of functions used by the macros 1433 DOCTEST_INTERFACE int regTest(const TestCase& tc); 1434 DOCTEST_INTERFACE int setTestSuite(const TestSuite& ts); 1435 DOCTEST_INTERFACE bool isDebuggerActive(); 1436 1437 template<typename T> 1438 int instantiationHelper(const T&) { return 0; } 1439 1440 namespace binaryAssertComparison { 1441 enum Enum 1442 { 1443 eq = 0, 1444 ne, 1445 gt, 1446 lt, 1447 ge, 1448 le 1449 }; 1450 } // namespace binaryAssertComparison 1451 1452 // clang-format off 1453 template <int, class L, class R> struct RelationalComparator { bool operator()(const DOCTEST_REF_WRAP(L), const DOCTEST_REF_WRAP(R) ) const { return false; } }; 1454 1455 #define DOCTEST_BINARY_RELATIONAL_OP(n, op) \ 1456 template <class L, class R> struct RelationalComparator<n, L, R> { bool operator()(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) const { return op(lhs, rhs); } }; 1457 // clang-format on 1458 1459 DOCTEST_BINARY_RELATIONAL_OP(0, doctest::detail::eq) 1460 DOCTEST_BINARY_RELATIONAL_OP(1, doctest::detail::ne) 1461 DOCTEST_BINARY_RELATIONAL_OP(2, doctest::detail::gt) 1462 DOCTEST_BINARY_RELATIONAL_OP(3, doctest::detail::lt) 1463 DOCTEST_BINARY_RELATIONAL_OP(4, doctest::detail::ge) 1464 DOCTEST_BINARY_RELATIONAL_OP(5, doctest::detail::le) 1465 1466 struct DOCTEST_INTERFACE ResultBuilder : public AssertData 1467 { 1468 ResultBuilder(assertType::Enum at, const char* file, int line, const char* expr, 1469 const char* exception_type = "", const char* exception_string = ""); 1470 1471 void setResult(const Result& res); 1472 1473 template <int comparison, typename L, typename R> 1474 DOCTEST_NOINLINE void binary_assert(const DOCTEST_REF_WRAP(L) lhs, 1475 const DOCTEST_REF_WRAP(R) rhs) { 1476 m_failed = !RelationalComparator<comparison, L, R>()(lhs, rhs); 1477 if(m_failed || getContextOptions()->success) 1478 m_decomp = stringifyBinaryExpr(lhs, ", ", rhs); 1479 } 1480 1481 template <typename L> 1482 DOCTEST_NOINLINE void unary_assert(const DOCTEST_REF_WRAP(L) val) { 1483 m_failed = !val; 1484 1485 if(m_at & assertType::is_false) //!OCLINT bitwise operator in conditional 1486 m_failed = !m_failed; 1487 1488 if(m_failed || getContextOptions()->success) 1489 m_decomp = toString(val); 1490 } 1491 1492 void translateException(); 1493 1494 bool log(); 1495 void react() const; 1496 }; 1497 1498 namespace assertAction { 1499 enum Enum 1500 { 1501 nothing = 0, 1502 dbgbreak = 1, 1503 shouldthrow = 2 1504 }; 1505 } // namespace assertAction 1506 1507 DOCTEST_INTERFACE void failed_out_of_a_testing_context(const AssertData& ad); 1508 1509 DOCTEST_INTERFACE void decomp_assert(assertType::Enum at, const char* file, int line, 1510 const char* expr, Result result); 1511 1512 #define DOCTEST_ASSERT_OUT_OF_TESTS(decomp) \ 1513 do { \ 1514 if(!is_running_in_test) { \ 1515 if(failed) { \ 1516 ResultBuilder rb(at, file, line, expr); \ 1517 rb.m_failed = failed; \ 1518 rb.m_decomp = decomp; \ 1519 failed_out_of_a_testing_context(rb); \ 1520 if(isDebuggerActive() && !getContextOptions()->no_breaks) \ 1521 DOCTEST_BREAK_INTO_DEBUGGER(); \ 1522 if(checkIfShouldThrow(at)) \ 1523 throwException(); \ 1524 } \ 1525 return; \ 1526 } \ 1527 } while(false) 1528 1529 #define DOCTEST_ASSERT_IN_TESTS(decomp) \ 1530 ResultBuilder rb(at, file, line, expr); \ 1531 rb.m_failed = failed; \ 1532 if(rb.m_failed || getContextOptions()->success) \ 1533 rb.m_decomp = decomp; \ 1534 if(rb.log()) \ 1535 DOCTEST_BREAK_INTO_DEBUGGER(); \ 1536 if(rb.m_failed && checkIfShouldThrow(at)) \ 1537 throwException() 1538 1539 template <int comparison, typename L, typename R> 1540 DOCTEST_NOINLINE void binary_assert(assertType::Enum at, const char* file, int line, 1541 const char* expr, const DOCTEST_REF_WRAP(L) lhs, 1542 const DOCTEST_REF_WRAP(R) rhs) { 1543 bool failed = !RelationalComparator<comparison, L, R>()(lhs, rhs); 1544 1545 // ################################################################################### 1546 // IF THE DEBUGGER BREAKS HERE - GO 1 LEVEL UP IN THE CALLSTACK FOR THE FAILING ASSERT 1547 // THIS IS THE EFFECT OF HAVING 'DOCTEST_CONFIG_SUPER_FAST_ASSERTS' DEFINED 1548 // ################################################################################### 1549 DOCTEST_ASSERT_OUT_OF_TESTS(stringifyBinaryExpr(lhs, ", ", rhs)); 1550 DOCTEST_ASSERT_IN_TESTS(stringifyBinaryExpr(lhs, ", ", rhs)); 1551 } 1552 1553 template <typename L> 1554 DOCTEST_NOINLINE void unary_assert(assertType::Enum at, const char* file, int line, 1555 const char* expr, const DOCTEST_REF_WRAP(L) val) { 1556 bool failed = !val; 1557 1558 if(at & assertType::is_false) //!OCLINT bitwise operator in conditional 1559 failed = !failed; 1560 1561 // ################################################################################### 1562 // IF THE DEBUGGER BREAKS HERE - GO 1 LEVEL UP IN THE CALLSTACK FOR THE FAILING ASSERT 1563 // THIS IS THE EFFECT OF HAVING 'DOCTEST_CONFIG_SUPER_FAST_ASSERTS' DEFINED 1564 // ################################################################################### 1565 DOCTEST_ASSERT_OUT_OF_TESTS(toString(val)); 1566 DOCTEST_ASSERT_IN_TESTS(toString(val)); 1567 } 1568 1569 struct DOCTEST_INTERFACE IExceptionTranslator 1570 { 1571 IExceptionTranslator(); 1572 virtual ~IExceptionTranslator(); 1573 virtual bool translate(String&) const = 0; 1574 }; 1575 1576 template <typename T> 1577 class ExceptionTranslator : public IExceptionTranslator //!OCLINT destructor of virtual class 1578 { 1579 public: 1580 explicit ExceptionTranslator(String (*translateFunction)(T)) 1581 : m_translateFunction(translateFunction) {} 1582 1583 bool translate(String& res) const override { 1584 #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS 1585 try { 1586 throw; // lgtm [cpp/rethrow-no-exception] 1587 // cppcheck-suppress catchExceptionByValue 1588 } catch(T ex) { // NOLINT 1589 res = m_translateFunction(ex); //!OCLINT parameter reassignment 1590 return true; 1591 } catch(...) {} //!OCLINT - empty catch statement 1592 #endif // DOCTEST_CONFIG_NO_EXCEPTIONS 1593 static_cast<void>(res); // to silence -Wunused-parameter 1594 return false; 1595 } 1596 1597 private: 1598 String (*m_translateFunction)(T); 1599 }; 1600 1601 DOCTEST_INTERFACE void registerExceptionTranslatorImpl(const IExceptionTranslator* et); 1602 1603 template <bool C> 1604 struct StringStreamBase 1605 { 1606 template <typename T> 1607 static void convert(std::ostream* s, const T& in) { 1608 *s << toString(in); 1609 } 1610 1611 // always treat char* as a string in this context - no matter 1612 // if DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING is defined 1613 static void convert(std::ostream* s, const char* in) { *s << String(in); } 1614 }; 1615 1616 template <> 1617 struct StringStreamBase<true> 1618 { 1619 template <typename T> 1620 static void convert(std::ostream* s, const T& in) { 1621 *s << in; 1622 } 1623 }; 1624 1625 template <typename T> 1626 struct StringStream : public StringStreamBase<has_insertion_operator<T>::value> 1627 {}; 1628 1629 template <typename T> 1630 void toStream(std::ostream* s, const T& value) { 1631 StringStream<T>::convert(s, value); 1632 } 1633 1634 #ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 1635 DOCTEST_INTERFACE void toStream(std::ostream* s, char* in); 1636 DOCTEST_INTERFACE void toStream(std::ostream* s, const char* in); 1637 #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING 1638 DOCTEST_INTERFACE void toStream(std::ostream* s, bool in); 1639 DOCTEST_INTERFACE void toStream(std::ostream* s, float in); 1640 DOCTEST_INTERFACE void toStream(std::ostream* s, double in); 1641 DOCTEST_INTERFACE void toStream(std::ostream* s, double long in); 1642 1643 DOCTEST_INTERFACE void toStream(std::ostream* s, char in); 1644 DOCTEST_INTERFACE void toStream(std::ostream* s, char signed in); 1645 DOCTEST_INTERFACE void toStream(std::ostream* s, char unsigned in); 1646 DOCTEST_INTERFACE void toStream(std::ostream* s, int short in); 1647 DOCTEST_INTERFACE void toStream(std::ostream* s, int short unsigned in); 1648 DOCTEST_INTERFACE void toStream(std::ostream* s, int in); 1649 DOCTEST_INTERFACE void toStream(std::ostream* s, int unsigned in); 1650 DOCTEST_INTERFACE void toStream(std::ostream* s, int long in); 1651 DOCTEST_INTERFACE void toStream(std::ostream* s, int long unsigned in); 1652 DOCTEST_INTERFACE void toStream(std::ostream* s, int long long in); 1653 DOCTEST_INTERFACE void toStream(std::ostream* s, int long long unsigned in); 1654 1655 // ContextScope base class used to allow implementing methods of ContextScope 1656 // that don't depend on the template parameter in doctest.cpp. 1657 class DOCTEST_INTERFACE ContextScopeBase : public IContextScope { 1658 protected: 1659 ContextScopeBase(); 1660 ContextScopeBase(ContextScopeBase&& other); 1661 1662 void destroy(); 1663 bool need_to_destroy{true}; 1664 }; 1665 1666 template <typename L> class ContextScope : public ContextScopeBase 1667 { 1668 const L lambda_; 1669 1670 public: 1671 explicit ContextScope(const L &lambda) : lambda_(lambda) {} 1672 1673 ContextScope(ContextScope &&other) : ContextScopeBase(static_cast<ContextScopeBase&&>(other)), lambda_(other.lambda_) {} 1674 1675 void stringify(std::ostream* s) const override { lambda_(s); } 1676 1677 ~ContextScope() override { 1678 if (need_to_destroy) { 1679 destroy(); 1680 } 1681 } 1682 }; 1683 1684 struct DOCTEST_INTERFACE MessageBuilder : public MessageData 1685 { 1686 std::ostream* m_stream; 1687 1688 MessageBuilder(const char* file, int line, assertType::Enum severity); 1689 MessageBuilder() = delete; 1690 ~MessageBuilder(); 1691 1692 // the preferred way of chaining parameters for stringification 1693 template <typename T> 1694 MessageBuilder& operator,(const T& in) { 1695 toStream(m_stream, in); 1696 return *this; 1697 } 1698 1699 // kept here just for backwards-compatibility - the comma operator should be preferred now 1700 template <typename T> 1701 MessageBuilder& operator<<(const T& in) { return this->operator,(in); } 1702 1703 // the `,` operator has the lowest operator precedence - if `<<` is used by the user then 1704 // the `,` operator will be called last which is not what we want and thus the `*` operator 1705 // is used first (has higher operator precedence compared to `<<`) so that we guarantee that 1706 // an operator of the MessageBuilder class is called first before the rest of the parameters 1707 template <typename T> 1708 MessageBuilder& operator*(const T& in) { return this->operator,(in); } 1709 1710 bool log(); 1711 void react(); 1712 }; 1713 1714 template <typename L> 1715 ContextScope<L> MakeContextScope(const L &lambda) { 1716 return ContextScope<L>(lambda); 1717 } 1718 } // namespace detail 1719 1720 #define DOCTEST_DEFINE_DECORATOR(name, type, def) \ 1721 struct name \ 1722 { \ 1723 type data; \ 1724 name(type in = def) \ 1725 : data(in) {} \ 1726 void fill(detail::TestCase& state) const { state.DOCTEST_CAT(m_, name) = data; } \ 1727 void fill(detail::TestSuite& state) const { state.DOCTEST_CAT(m_, name) = data; } \ 1728 } 1729 1730 DOCTEST_DEFINE_DECORATOR(test_suite, const char*, ""); 1731 DOCTEST_DEFINE_DECORATOR(description, const char*, ""); 1732 DOCTEST_DEFINE_DECORATOR(skip, bool, true); 1733 DOCTEST_DEFINE_DECORATOR(no_breaks, bool, true); 1734 DOCTEST_DEFINE_DECORATOR(no_output, bool, true); 1735 DOCTEST_DEFINE_DECORATOR(timeout, double, 0); 1736 DOCTEST_DEFINE_DECORATOR(may_fail, bool, true); 1737 DOCTEST_DEFINE_DECORATOR(should_fail, bool, true); 1738 DOCTEST_DEFINE_DECORATOR(expected_failures, int, 0); 1739 1740 template <typename T> 1741 int registerExceptionTranslator(String (*translateFunction)(T)) { 1742 DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wexit-time-destructors") 1743 static detail::ExceptionTranslator<T> exceptionTranslator(translateFunction); 1744 DOCTEST_CLANG_SUPPRESS_WARNING_POP 1745 detail::registerExceptionTranslatorImpl(&exceptionTranslator); 1746 return 0; 1747 } 1748 1749 } // namespace doctest 1750 1751 // in a separate namespace outside of doctest because the DOCTEST_TEST_SUITE macro 1752 // introduces an anonymous namespace in which getCurrentTestSuite gets overridden 1753 namespace doctest_detail_test_suite_ns { 1754 DOCTEST_INTERFACE doctest::detail::TestSuite& getCurrentTestSuite(); 1755 } // namespace doctest_detail_test_suite_ns 1756 1757 namespace doctest { 1758 #else // DOCTEST_CONFIG_DISABLE 1759 template <typename T> 1760 int registerExceptionTranslator(String (*)(T)) { 1761 return 0; 1762 } 1763 #endif // DOCTEST_CONFIG_DISABLE 1764 1765 namespace detail { 1766 typedef void (*assert_handler)(const AssertData&); 1767 struct ContextState; 1768 } // namespace detail 1769 1770 class DOCTEST_INTERFACE Context 1771 { 1772 detail::ContextState* p; 1773 1774 void parseArgs(int argc, const char* const* argv, bool withDefaults = false); 1775 1776 public: 1777 explicit Context(int argc = 0, const char* const* argv = nullptr); 1778 1779 ~Context(); 1780 1781 void applyCommandLine(int argc, const char* const* argv); 1782 1783 void addFilter(const char* filter, const char* value); 1784 void clearFilters(); 1785 void setOption(const char* option, bool value); 1786 void setOption(const char* option, int value); 1787 void setOption(const char* option, const char* value); 1788 1789 bool shouldExit(); 1790 1791 void setAsDefaultForAssertsOutOfTestCases(); 1792 1793 void setAssertHandler(detail::assert_handler ah); 1794 1795 void setCout(std::ostream* out); 1796 1797 int run(); 1798 }; 1799 1800 namespace TestCaseFailureReason { 1801 enum Enum 1802 { 1803 None = 0, 1804 AssertFailure = 1, // an assertion has failed in the test case 1805 Exception = 2, // test case threw an exception 1806 Crash = 4, // a crash... 1807 TooManyFailedAsserts = 8, // the abort-after option 1808 Timeout = 16, // see the timeout decorator 1809 ShouldHaveFailedButDidnt = 32, // see the should_fail decorator 1810 ShouldHaveFailedAndDid = 64, // see the should_fail decorator 1811 DidntFailExactlyNumTimes = 128, // see the expected_failures decorator 1812 FailedExactlyNumTimes = 256, // see the expected_failures decorator 1813 CouldHaveFailedAndDid = 512 // see the may_fail decorator 1814 }; 1815 } // namespace TestCaseFailureReason 1816 1817 struct DOCTEST_INTERFACE CurrentTestCaseStats 1818 { 1819 int numAssertsCurrentTest; 1820 int numAssertsFailedCurrentTest; 1821 double seconds; 1822 int failure_flags; // use TestCaseFailureReason::Enum 1823 bool testCaseSuccess; 1824 }; 1825 1826 struct DOCTEST_INTERFACE TestCaseException 1827 { 1828 String error_string; 1829 bool is_crash; 1830 }; 1831 1832 struct DOCTEST_INTERFACE TestRunStats 1833 { 1834 unsigned numTestCases; 1835 unsigned numTestCasesPassingFilters; 1836 unsigned numTestSuitesPassingFilters; 1837 unsigned numTestCasesFailed; 1838 int numAsserts; 1839 int numAssertsFailed; 1840 }; 1841 1842 struct QueryData 1843 { 1844 const TestRunStats* run_stats = nullptr; 1845 const TestCaseData** data = nullptr; 1846 unsigned num_data = 0; 1847 }; 1848 1849 struct DOCTEST_INTERFACE IReporter 1850 { 1851 // The constructor has to accept "const ContextOptions&" as a single argument 1852 // which has most of the options for the run + a pointer to the stdout stream 1853 // Reporter(const ContextOptions& in) 1854 1855 // called when a query should be reported (listing test cases, printing the version, etc.) 1856 virtual void report_query(const QueryData&) = 0; 1857 1858 // called when the whole test run starts 1859 virtual void test_run_start() = 0; 1860 // called when the whole test run ends (caching a pointer to the input doesn't make sense here) 1861 virtual void test_run_end(const TestRunStats&) = 0; 1862 1863 // called when a test case is started (safe to cache a pointer to the input) 1864 virtual void test_case_start(const TestCaseData&) = 0; 1865 // called when a test case is reentered because of unfinished subcases (safe to cache a pointer to the input) 1866 virtual void test_case_reenter(const TestCaseData&) = 0; 1867 // called when a test case has ended 1868 virtual void test_case_end(const CurrentTestCaseStats&) = 0; 1869 1870 // called when an exception is thrown from the test case (or it crashes) 1871 virtual void test_case_exception(const TestCaseException&) = 0; 1872 1873 // called whenever a subcase is entered (don't cache pointers to the input) 1874 virtual void subcase_start(const SubcaseSignature&) = 0; 1875 // called whenever a subcase is exited (don't cache pointers to the input) 1876 virtual void subcase_end() = 0; 1877 1878 // called for each assert (don't cache pointers to the input) 1879 virtual void log_assert(const AssertData&) = 0; 1880 // called for each message (don't cache pointers to the input) 1881 virtual void log_message(const MessageData&) = 0; 1882 1883 // called when a test case is skipped either because it doesn't pass the filters, has a skip decorator 1884 // or isn't in the execution range (between first and last) (safe to cache a pointer to the input) 1885 virtual void test_case_skipped(const TestCaseData&) = 0; 1886 1887 // doctest will not be managing the lifetimes of reporters given to it but this would still be nice to have 1888 virtual ~IReporter(); 1889 1890 // can obtain all currently active contexts and stringify them if one wishes to do so 1891 static int get_num_active_contexts(); 1892 static const IContextScope* const* get_active_contexts(); 1893 1894 // can iterate through contexts which have been stringified automatically in their destructors when an exception has been thrown 1895 static int get_num_stringified_contexts(); 1896 static const String* get_stringified_contexts(); 1897 }; 1898 1899 namespace detail { 1900 typedef IReporter* (*reporterCreatorFunc)(const ContextOptions&); 1901 1902 DOCTEST_INTERFACE void registerReporterImpl(const char* name, int prio, reporterCreatorFunc c, bool isReporter); 1903 1904 template <typename Reporter> 1905 IReporter* reporterCreator(const ContextOptions& o) { 1906 return new Reporter(o); 1907 } 1908 } // namespace detail 1909 1910 template <typename Reporter> 1911 int registerReporter(const char* name, int priority, bool isReporter) { 1912 detail::registerReporterImpl(name, priority, detail::reporterCreator<Reporter>, isReporter); 1913 return 0; 1914 } 1915 } // namespace doctest 1916 1917 // if registering is not disabled 1918 #if !defined(DOCTEST_CONFIG_DISABLE) 1919 1920 // common code in asserts - for convenience 1921 #define DOCTEST_ASSERT_LOG_AND_REACT(b) \ 1922 if(b.log()) \ 1923 DOCTEST_BREAK_INTO_DEBUGGER(); \ 1924 b.react() 1925 1926 #ifdef DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS 1927 #define DOCTEST_WRAP_IN_TRY(x) x; 1928 #else // DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS 1929 #define DOCTEST_WRAP_IN_TRY(x) \ 1930 try { \ 1931 x; \ 1932 } catch(...) { DOCTEST_RB.translateException(); } 1933 #endif // DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS 1934 1935 #ifdef DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS 1936 #define DOCTEST_CAST_TO_VOID(...) \ 1937 DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wuseless-cast") \ 1938 static_cast<void>(__VA_ARGS__); \ 1939 DOCTEST_GCC_SUPPRESS_WARNING_POP 1940 #else // DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS 1941 #define DOCTEST_CAST_TO_VOID(...) __VA_ARGS__; 1942 #endif // DOCTEST_CONFIG_VOID_CAST_EXPRESSIONS 1943 1944 // registers the test by initializing a dummy var with a function 1945 #define DOCTEST_REGISTER_FUNCTION(global_prefix, f, decorators) \ 1946 global_prefix DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(DOCTEST_ANON_VAR_)) = \ 1947 doctest::detail::regTest( \ 1948 doctest::detail::TestCase( \ 1949 f, __FILE__, __LINE__, \ 1950 doctest_detail_test_suite_ns::getCurrentTestSuite()) * \ 1951 decorators); \ 1952 DOCTEST_GLOBAL_NO_WARNINGS_END() 1953 1954 #define DOCTEST_IMPLEMENT_FIXTURE(der, base, func, decorators) \ 1955 namespace { \ 1956 struct der : public base \ 1957 { \ 1958 void f(); \ 1959 }; \ 1960 static void func() { \ 1961 der v; \ 1962 v.f(); \ 1963 } \ 1964 DOCTEST_REGISTER_FUNCTION(DOCTEST_EMPTY, func, decorators) \ 1965 } \ 1966 inline DOCTEST_NOINLINE void der::f() 1967 1968 #define DOCTEST_CREATE_AND_REGISTER_FUNCTION(f, decorators) \ 1969 static void f(); \ 1970 DOCTEST_REGISTER_FUNCTION(DOCTEST_EMPTY, f, decorators) \ 1971 static void f() 1972 1973 #define DOCTEST_CREATE_AND_REGISTER_FUNCTION_IN_CLASS(f, proxy, decorators) \ 1974 static doctest::detail::funcType proxy() { return f; } \ 1975 DOCTEST_REGISTER_FUNCTION(inline, proxy(), decorators) \ 1976 static void f() 1977 1978 // for registering tests 1979 #define DOCTEST_TEST_CASE(decorators) \ 1980 DOCTEST_CREATE_AND_REGISTER_FUNCTION(DOCTEST_ANONYMOUS(DOCTEST_ANON_FUNC_), decorators) 1981 1982 // for registering tests in classes - requires C++17 for inline variables! 1983 #if __cplusplus >= 201703L || (DOCTEST_MSVC >= DOCTEST_COMPILER(19, 12, 0) && _MSVC_LANG >= 201703L) 1984 #define DOCTEST_TEST_CASE_CLASS(decorators) \ 1985 DOCTEST_CREATE_AND_REGISTER_FUNCTION_IN_CLASS(DOCTEST_ANONYMOUS(DOCTEST_ANON_FUNC_), \ 1986 DOCTEST_ANONYMOUS(DOCTEST_ANON_PROXY_), \ 1987 decorators) 1988 #else // DOCTEST_TEST_CASE_CLASS 1989 #define DOCTEST_TEST_CASE_CLASS(...) \ 1990 TEST_CASES_CAN_BE_REGISTERED_IN_CLASSES_ONLY_IN_CPP17_MODE_OR_WITH_VS_2017_OR_NEWER 1991 #endif // DOCTEST_TEST_CASE_CLASS 1992 1993 // for registering tests with a fixture 1994 #define DOCTEST_TEST_CASE_FIXTURE(c, decorators) \ 1995 DOCTEST_IMPLEMENT_FIXTURE(DOCTEST_ANONYMOUS(DOCTEST_ANON_CLASS_), c, \ 1996 DOCTEST_ANONYMOUS(DOCTEST_ANON_FUNC_), decorators) 1997 1998 // for converting types to strings without the <typeinfo> header and demangling 1999 #define DOCTEST_TYPE_TO_STRING_IMPL(...) \ 2000 template <> \ 2001 inline const char* type_to_string<__VA_ARGS__>() { \ 2002 return "<" #__VA_ARGS__ ">"; \ 2003 } 2004 #define DOCTEST_TYPE_TO_STRING(...) \ 2005 namespace doctest { namespace detail { \ 2006 DOCTEST_TYPE_TO_STRING_IMPL(__VA_ARGS__) \ 2007 } \ 2008 } \ 2009 typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2010 2011 #define DOCTEST_TEST_CASE_TEMPLATE_DEFINE_IMPL(dec, T, iter, func) \ 2012 template <typename T> \ 2013 static void func(); \ 2014 namespace { \ 2015 template <typename Tuple> \ 2016 struct iter; \ 2017 template <typename Type, typename... Rest> \ 2018 struct iter<std::tuple<Type, Rest...>> \ 2019 { \ 2020 iter(const char* file, unsigned line, int index) { \ 2021 doctest::detail::regTest(doctest::detail::TestCase(func<Type>, file, line, \ 2022 doctest_detail_test_suite_ns::getCurrentTestSuite(), \ 2023 doctest::detail::type_to_string<Type>(), \ 2024 int(line) * 1000 + index) \ 2025 * dec); \ 2026 iter<std::tuple<Rest...>>(file, line, index + 1); \ 2027 } \ 2028 }; \ 2029 template <> \ 2030 struct iter<std::tuple<>> \ 2031 { \ 2032 iter(const char*, unsigned, int) {} \ 2033 }; \ 2034 } \ 2035 template <typename T> \ 2036 static void func() 2037 2038 #define DOCTEST_TEST_CASE_TEMPLATE_DEFINE(dec, T, id) \ 2039 DOCTEST_TEST_CASE_TEMPLATE_DEFINE_IMPL(dec, T, DOCTEST_CAT(id, ITERATOR), \ 2040 DOCTEST_ANONYMOUS(DOCTEST_ANON_TMP_)) 2041 2042 #define DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE_IMPL(id, anon, ...) \ 2043 DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_CAT(anon, DUMMY)) = \ 2044 doctest::detail::instantiationHelper(DOCTEST_CAT(id, ITERATOR)<__VA_ARGS__>(__FILE__, __LINE__, 0));\ 2045 DOCTEST_GLOBAL_NO_WARNINGS_END() 2046 2047 #define DOCTEST_TEST_CASE_TEMPLATE_INVOKE(id, ...) \ 2048 DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE_IMPL(id, DOCTEST_ANONYMOUS(DOCTEST_ANON_TMP_), std::tuple<__VA_ARGS__>) \ 2049 typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2050 2051 #define DOCTEST_TEST_CASE_TEMPLATE_APPLY(id, ...) \ 2052 DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE_IMPL(id, DOCTEST_ANONYMOUS(DOCTEST_ANON_TMP_), __VA_ARGS__) \ 2053 typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2054 2055 #define DOCTEST_TEST_CASE_TEMPLATE_IMPL(dec, T, anon, ...) \ 2056 DOCTEST_TEST_CASE_TEMPLATE_DEFINE_IMPL(dec, T, DOCTEST_CAT(anon, ITERATOR), anon); \ 2057 DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE_IMPL(anon, anon, std::tuple<__VA_ARGS__>) \ 2058 template <typename T> \ 2059 static void anon() 2060 2061 #define DOCTEST_TEST_CASE_TEMPLATE(dec, T, ...) \ 2062 DOCTEST_TEST_CASE_TEMPLATE_IMPL(dec, T, DOCTEST_ANONYMOUS(DOCTEST_ANON_TMP_), __VA_ARGS__) 2063 2064 // for subcases 2065 #define DOCTEST_SUBCASE(name) \ 2066 if(const doctest::detail::Subcase & DOCTEST_ANONYMOUS(DOCTEST_ANON_SUBCASE_) DOCTEST_UNUSED = \ 2067 doctest::detail::Subcase(name, __FILE__, __LINE__)) 2068 2069 // for grouping tests in test suites by using code blocks 2070 #define DOCTEST_TEST_SUITE_IMPL(decorators, ns_name) \ 2071 namespace ns_name { namespace doctest_detail_test_suite_ns { \ 2072 static DOCTEST_NOINLINE doctest::detail::TestSuite& getCurrentTestSuite() { \ 2073 DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4640) \ 2074 DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wexit-time-destructors") \ 2075 DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wmissing-field-initializers") \ 2076 static doctest::detail::TestSuite data{}; \ 2077 static bool inited = false; \ 2078 DOCTEST_MSVC_SUPPRESS_WARNING_POP \ 2079 DOCTEST_CLANG_SUPPRESS_WARNING_POP \ 2080 DOCTEST_GCC_SUPPRESS_WARNING_POP \ 2081 if(!inited) { \ 2082 data* decorators; \ 2083 inited = true; \ 2084 } \ 2085 return data; \ 2086 } \ 2087 } \ 2088 } \ 2089 namespace ns_name 2090 2091 #define DOCTEST_TEST_SUITE(decorators) \ 2092 DOCTEST_TEST_SUITE_IMPL(decorators, DOCTEST_ANONYMOUS(DOCTEST_ANON_SUITE_)) 2093 2094 // for starting a testsuite block 2095 #define DOCTEST_TEST_SUITE_BEGIN(decorators) \ 2096 DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(DOCTEST_ANON_VAR_)) = \ 2097 doctest::detail::setTestSuite(doctest::detail::TestSuite() * decorators); \ 2098 DOCTEST_GLOBAL_NO_WARNINGS_END() \ 2099 typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2100 2101 // for ending a testsuite block 2102 #define DOCTEST_TEST_SUITE_END \ 2103 DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(DOCTEST_ANON_VAR_)) = \ 2104 doctest::detail::setTestSuite(doctest::detail::TestSuite() * ""); \ 2105 DOCTEST_GLOBAL_NO_WARNINGS_END() \ 2106 typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2107 2108 // for registering exception translators 2109 #define DOCTEST_REGISTER_EXCEPTION_TRANSLATOR_IMPL(translatorName, signature) \ 2110 inline doctest::String translatorName(signature); \ 2111 DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(DOCTEST_ANON_TRANSLATOR_)) = \ 2112 doctest::registerExceptionTranslator(translatorName); \ 2113 DOCTEST_GLOBAL_NO_WARNINGS_END() \ 2114 doctest::String translatorName(signature) 2115 2116 #define DOCTEST_REGISTER_EXCEPTION_TRANSLATOR(signature) \ 2117 DOCTEST_REGISTER_EXCEPTION_TRANSLATOR_IMPL(DOCTEST_ANONYMOUS(DOCTEST_ANON_TRANSLATOR_), \ 2118 signature) 2119 2120 // for registering reporters 2121 #define DOCTEST_REGISTER_REPORTER(name, priority, reporter) \ 2122 DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(DOCTEST_ANON_REPORTER_)) = \ 2123 doctest::registerReporter<reporter>(name, priority, true); \ 2124 DOCTEST_GLOBAL_NO_WARNINGS_END() typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2125 2126 // for registering listeners 2127 #define DOCTEST_REGISTER_LISTENER(name, priority, reporter) \ 2128 DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(DOCTEST_ANON_REPORTER_)) = \ 2129 doctest::registerReporter<reporter>(name, priority, false); \ 2130 DOCTEST_GLOBAL_NO_WARNINGS_END() typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2131 2132 // clang-format off 2133 // for logging - disabling formatting because it's important to have these on 2 separate lines - see PR #557 2134 #define DOCTEST_INFO(...) \ 2135 DOCTEST_INFO_IMPL(DOCTEST_ANONYMOUS(DOCTEST_CAPTURE_), \ 2136 DOCTEST_ANONYMOUS(DOCTEST_CAPTURE_OTHER_), \ 2137 __VA_ARGS__) 2138 // clang-format on 2139 2140 #define DOCTEST_INFO_IMPL(mb_name, s_name, ...) \ 2141 auto DOCTEST_ANONYMOUS(DOCTEST_CAPTURE_) = doctest::detail::MakeContextScope( \ 2142 [&](std::ostream* s_name) { \ 2143 doctest::detail::MessageBuilder mb_name(__FILE__, __LINE__, doctest::assertType::is_warn); \ 2144 mb_name.m_stream = s_name; \ 2145 mb_name * __VA_ARGS__; \ 2146 }) 2147 2148 #define DOCTEST_CAPTURE(x) DOCTEST_INFO(#x " := ", x) 2149 2150 #define DOCTEST_ADD_AT_IMPL(type, file, line, mb, ...) \ 2151 do { \ 2152 doctest::detail::MessageBuilder mb(file, line, doctest::assertType::type); \ 2153 mb * __VA_ARGS__; \ 2154 DOCTEST_ASSERT_LOG_AND_REACT(mb); \ 2155 } while(false) 2156 2157 // clang-format off 2158 #define DOCTEST_ADD_MESSAGE_AT(file, line, ...) DOCTEST_ADD_AT_IMPL(is_warn, file, line, DOCTEST_ANONYMOUS(DOCTEST_MESSAGE_), __VA_ARGS__) 2159 #define DOCTEST_ADD_FAIL_CHECK_AT(file, line, ...) DOCTEST_ADD_AT_IMPL(is_check, file, line, DOCTEST_ANONYMOUS(DOCTEST_MESSAGE_), __VA_ARGS__) 2160 #define DOCTEST_ADD_FAIL_AT(file, line, ...) DOCTEST_ADD_AT_IMPL(is_require, file, line, DOCTEST_ANONYMOUS(DOCTEST_MESSAGE_), __VA_ARGS__) 2161 // clang-format on 2162 2163 #define DOCTEST_MESSAGE(...) DOCTEST_ADD_MESSAGE_AT(__FILE__, __LINE__, __VA_ARGS__) 2164 #define DOCTEST_FAIL_CHECK(...) DOCTEST_ADD_FAIL_CHECK_AT(__FILE__, __LINE__, __VA_ARGS__) 2165 #define DOCTEST_FAIL(...) DOCTEST_ADD_FAIL_AT(__FILE__, __LINE__, __VA_ARGS__) 2166 2167 #define DOCTEST_TO_LVALUE(...) __VA_ARGS__ // Not removed to keep backwards compatibility. 2168 2169 #ifndef DOCTEST_CONFIG_SUPER_FAST_ASSERTS 2170 2171 #define DOCTEST_ASSERT_IMPLEMENT_2(assert_type, ...) \ 2172 DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Woverloaded-shift-op-parentheses") \ 2173 doctest::detail::ResultBuilder DOCTEST_RB(doctest::assertType::assert_type, __FILE__, \ 2174 __LINE__, #__VA_ARGS__); \ 2175 DOCTEST_WRAP_IN_TRY(DOCTEST_RB.setResult( \ 2176 doctest::detail::ExpressionDecomposer(doctest::assertType::assert_type) \ 2177 << __VA_ARGS__)) \ 2178 DOCTEST_ASSERT_LOG_AND_REACT(DOCTEST_RB) \ 2179 DOCTEST_CLANG_SUPPRESS_WARNING_POP 2180 2181 #define DOCTEST_ASSERT_IMPLEMENT_1(assert_type, ...) \ 2182 do { \ 2183 DOCTEST_ASSERT_IMPLEMENT_2(assert_type, __VA_ARGS__); \ 2184 } while(false) 2185 2186 #else // DOCTEST_CONFIG_SUPER_FAST_ASSERTS 2187 2188 // necessary for <ASSERT>_MESSAGE 2189 #define DOCTEST_ASSERT_IMPLEMENT_2 DOCTEST_ASSERT_IMPLEMENT_1 2190 2191 #define DOCTEST_ASSERT_IMPLEMENT_1(assert_type, ...) \ 2192 DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Woverloaded-shift-op-parentheses") \ 2193 doctest::detail::decomp_assert( \ 2194 doctest::assertType::assert_type, __FILE__, __LINE__, #__VA_ARGS__, \ 2195 doctest::detail::ExpressionDecomposer(doctest::assertType::assert_type) \ 2196 << __VA_ARGS__) DOCTEST_CLANG_SUPPRESS_WARNING_POP 2197 2198 #endif // DOCTEST_CONFIG_SUPER_FAST_ASSERTS 2199 2200 #define DOCTEST_WARN(...) DOCTEST_ASSERT_IMPLEMENT_1(DT_WARN, __VA_ARGS__) 2201 #define DOCTEST_CHECK(...) DOCTEST_ASSERT_IMPLEMENT_1(DT_CHECK, __VA_ARGS__) 2202 #define DOCTEST_REQUIRE(...) DOCTEST_ASSERT_IMPLEMENT_1(DT_REQUIRE, __VA_ARGS__) 2203 #define DOCTEST_WARN_FALSE(...) DOCTEST_ASSERT_IMPLEMENT_1(DT_WARN_FALSE, __VA_ARGS__) 2204 #define DOCTEST_CHECK_FALSE(...) DOCTEST_ASSERT_IMPLEMENT_1(DT_CHECK_FALSE, __VA_ARGS__) 2205 #define DOCTEST_REQUIRE_FALSE(...) DOCTEST_ASSERT_IMPLEMENT_1(DT_REQUIRE_FALSE, __VA_ARGS__) 2206 2207 // clang-format off 2208 #define DOCTEST_WARN_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_WARN, cond); } while(false) 2209 #define DOCTEST_CHECK_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_CHECK, cond); } while(false) 2210 #define DOCTEST_REQUIRE_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_REQUIRE, cond); } while(false) 2211 #define DOCTEST_WARN_FALSE_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_WARN_FALSE, cond); } while(false) 2212 #define DOCTEST_CHECK_FALSE_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_CHECK_FALSE, cond); } while(false) 2213 #define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_ASSERT_IMPLEMENT_2(DT_REQUIRE_FALSE, cond); } while(false) 2214 // clang-format on 2215 2216 #define DOCTEST_ASSERT_THROWS_AS(expr, assert_type, message, ...) \ 2217 do { \ 2218 if(!doctest::getContextOptions()->no_throw) { \ 2219 doctest::detail::ResultBuilder DOCTEST_RB(doctest::assertType::assert_type, __FILE__, \ 2220 __LINE__, #expr, #__VA_ARGS__, message); \ 2221 try { \ 2222 DOCTEST_CAST_TO_VOID(expr) \ 2223 } catch(const typename doctest::detail::remove_const< \ 2224 typename doctest::detail::remove_reference<__VA_ARGS__>::type>::type&) { \ 2225 DOCTEST_RB.translateException(); \ 2226 DOCTEST_RB.m_threw_as = true; \ 2227 } catch(...) { DOCTEST_RB.translateException(); } \ 2228 DOCTEST_ASSERT_LOG_AND_REACT(DOCTEST_RB); \ 2229 } \ 2230 } while(false) 2231 2232 #define DOCTEST_ASSERT_THROWS_WITH(expr, expr_str, assert_type, ...) \ 2233 do { \ 2234 if(!doctest::getContextOptions()->no_throw) { \ 2235 doctest::detail::ResultBuilder DOCTEST_RB(doctest::assertType::assert_type, __FILE__, \ 2236 __LINE__, expr_str, "", __VA_ARGS__); \ 2237 try { \ 2238 DOCTEST_CAST_TO_VOID(expr) \ 2239 } catch(...) { DOCTEST_RB.translateException(); } \ 2240 DOCTEST_ASSERT_LOG_AND_REACT(DOCTEST_RB); \ 2241 } \ 2242 } while(false) 2243 2244 #define DOCTEST_ASSERT_NOTHROW(assert_type, ...) \ 2245 do { \ 2246 doctest::detail::ResultBuilder DOCTEST_RB(doctest::assertType::assert_type, __FILE__, \ 2247 __LINE__, #__VA_ARGS__); \ 2248 try { \ 2249 DOCTEST_CAST_TO_VOID(__VA_ARGS__) \ 2250 } catch(...) { DOCTEST_RB.translateException(); } \ 2251 DOCTEST_ASSERT_LOG_AND_REACT(DOCTEST_RB); \ 2252 } while(false) 2253 2254 // clang-format off 2255 #define DOCTEST_WARN_THROWS(...) DOCTEST_ASSERT_THROWS_WITH((__VA_ARGS__), #__VA_ARGS__, DT_WARN_THROWS, "") 2256 #define DOCTEST_CHECK_THROWS(...) DOCTEST_ASSERT_THROWS_WITH((__VA_ARGS__), #__VA_ARGS__, DT_CHECK_THROWS, "") 2257 #define DOCTEST_REQUIRE_THROWS(...) DOCTEST_ASSERT_THROWS_WITH((__VA_ARGS__), #__VA_ARGS__, DT_REQUIRE_THROWS, "") 2258 2259 #define DOCTEST_WARN_THROWS_AS(expr, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_WARN_THROWS_AS, "", __VA_ARGS__) 2260 #define DOCTEST_CHECK_THROWS_AS(expr, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_CHECK_THROWS_AS, "", __VA_ARGS__) 2261 #define DOCTEST_REQUIRE_THROWS_AS(expr, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_REQUIRE_THROWS_AS, "", __VA_ARGS__) 2262 2263 #define DOCTEST_WARN_THROWS_WITH(expr, ...) DOCTEST_ASSERT_THROWS_WITH(expr, #expr, DT_WARN_THROWS_WITH, __VA_ARGS__) 2264 #define DOCTEST_CHECK_THROWS_WITH(expr, ...) DOCTEST_ASSERT_THROWS_WITH(expr, #expr, DT_CHECK_THROWS_WITH, __VA_ARGS__) 2265 #define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) DOCTEST_ASSERT_THROWS_WITH(expr, #expr, DT_REQUIRE_THROWS_WITH, __VA_ARGS__) 2266 2267 #define DOCTEST_WARN_THROWS_WITH_AS(expr, message, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_WARN_THROWS_WITH_AS, message, __VA_ARGS__) 2268 #define DOCTEST_CHECK_THROWS_WITH_AS(expr, message, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_CHECK_THROWS_WITH_AS, message, __VA_ARGS__) 2269 #define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, message, ...) DOCTEST_ASSERT_THROWS_AS(expr, DT_REQUIRE_THROWS_WITH_AS, message, __VA_ARGS__) 2270 2271 #define DOCTEST_WARN_NOTHROW(...) DOCTEST_ASSERT_NOTHROW(DT_WARN_NOTHROW, __VA_ARGS__) 2272 #define DOCTEST_CHECK_NOTHROW(...) DOCTEST_ASSERT_NOTHROW(DT_CHECK_NOTHROW, __VA_ARGS__) 2273 #define DOCTEST_REQUIRE_NOTHROW(...) DOCTEST_ASSERT_NOTHROW(DT_REQUIRE_NOTHROW, __VA_ARGS__) 2274 2275 #define DOCTEST_WARN_THROWS_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_WARN_THROWS(expr); } while(false) 2276 #define DOCTEST_CHECK_THROWS_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_CHECK_THROWS(expr); } while(false) 2277 #define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_REQUIRE_THROWS(expr); } while(false) 2278 #define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_WARN_THROWS_AS(expr, ex); } while(false) 2279 #define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_CHECK_THROWS_AS(expr, ex); } while(false) 2280 #define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_REQUIRE_THROWS_AS(expr, ex); } while(false) 2281 #define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_WARN_THROWS_WITH(expr, with); } while(false) 2282 #define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_CHECK_THROWS_WITH(expr, with); } while(false) 2283 #define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_REQUIRE_THROWS_WITH(expr, with); } while(false) 2284 #define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_WARN_THROWS_WITH_AS(expr, with, ex); } while(false) 2285 #define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ex); } while(false) 2286 #define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ex); } while(false) 2287 #define DOCTEST_WARN_NOTHROW_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_WARN_NOTHROW(expr); } while(false) 2288 #define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_CHECK_NOTHROW(expr); } while(false) 2289 #define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, ...) do { DOCTEST_INFO(__VA_ARGS__); DOCTEST_REQUIRE_NOTHROW(expr); } while(false) 2290 // clang-format on 2291 2292 #ifndef DOCTEST_CONFIG_SUPER_FAST_ASSERTS 2293 2294 #define DOCTEST_BINARY_ASSERT(assert_type, comp, ...) \ 2295 do { \ 2296 doctest::detail::ResultBuilder DOCTEST_RB(doctest::assertType::assert_type, __FILE__, \ 2297 __LINE__, #__VA_ARGS__); \ 2298 DOCTEST_WRAP_IN_TRY( \ 2299 DOCTEST_RB.binary_assert<doctest::detail::binaryAssertComparison::comp>( \ 2300 __VA_ARGS__)) \ 2301 DOCTEST_ASSERT_LOG_AND_REACT(DOCTEST_RB); \ 2302 } while(false) 2303 2304 #define DOCTEST_UNARY_ASSERT(assert_type, ...) \ 2305 do { \ 2306 doctest::detail::ResultBuilder DOCTEST_RB(doctest::assertType::assert_type, __FILE__, \ 2307 __LINE__, #__VA_ARGS__); \ 2308 DOCTEST_WRAP_IN_TRY(DOCTEST_RB.unary_assert(__VA_ARGS__)) \ 2309 DOCTEST_ASSERT_LOG_AND_REACT(DOCTEST_RB); \ 2310 } while(false) 2311 2312 #else // DOCTEST_CONFIG_SUPER_FAST_ASSERTS 2313 2314 #define DOCTEST_BINARY_ASSERT(assert_type, comparison, ...) \ 2315 doctest::detail::binary_assert<doctest::detail::binaryAssertComparison::comparison>( \ 2316 doctest::assertType::assert_type, __FILE__, __LINE__, #__VA_ARGS__, __VA_ARGS__) 2317 2318 #define DOCTEST_UNARY_ASSERT(assert_type, ...) \ 2319 doctest::detail::unary_assert(doctest::assertType::assert_type, __FILE__, __LINE__, \ 2320 #__VA_ARGS__, __VA_ARGS__) 2321 2322 #endif // DOCTEST_CONFIG_SUPER_FAST_ASSERTS 2323 2324 #define DOCTEST_WARN_EQ(...) DOCTEST_BINARY_ASSERT(DT_WARN_EQ, eq, __VA_ARGS__) 2325 #define DOCTEST_CHECK_EQ(...) DOCTEST_BINARY_ASSERT(DT_CHECK_EQ, eq, __VA_ARGS__) 2326 #define DOCTEST_REQUIRE_EQ(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_EQ, eq, __VA_ARGS__) 2327 #define DOCTEST_WARN_NE(...) DOCTEST_BINARY_ASSERT(DT_WARN_NE, ne, __VA_ARGS__) 2328 #define DOCTEST_CHECK_NE(...) DOCTEST_BINARY_ASSERT(DT_CHECK_NE, ne, __VA_ARGS__) 2329 #define DOCTEST_REQUIRE_NE(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_NE, ne, __VA_ARGS__) 2330 #define DOCTEST_WARN_GT(...) DOCTEST_BINARY_ASSERT(DT_WARN_GT, gt, __VA_ARGS__) 2331 #define DOCTEST_CHECK_GT(...) DOCTEST_BINARY_ASSERT(DT_CHECK_GT, gt, __VA_ARGS__) 2332 #define DOCTEST_REQUIRE_GT(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_GT, gt, __VA_ARGS__) 2333 #define DOCTEST_WARN_LT(...) DOCTEST_BINARY_ASSERT(DT_WARN_LT, lt, __VA_ARGS__) 2334 #define DOCTEST_CHECK_LT(...) DOCTEST_BINARY_ASSERT(DT_CHECK_LT, lt, __VA_ARGS__) 2335 #define DOCTEST_REQUIRE_LT(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_LT, lt, __VA_ARGS__) 2336 #define DOCTEST_WARN_GE(...) DOCTEST_BINARY_ASSERT(DT_WARN_GE, ge, __VA_ARGS__) 2337 #define DOCTEST_CHECK_GE(...) DOCTEST_BINARY_ASSERT(DT_CHECK_GE, ge, __VA_ARGS__) 2338 #define DOCTEST_REQUIRE_GE(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_GE, ge, __VA_ARGS__) 2339 #define DOCTEST_WARN_LE(...) DOCTEST_BINARY_ASSERT(DT_WARN_LE, le, __VA_ARGS__) 2340 #define DOCTEST_CHECK_LE(...) DOCTEST_BINARY_ASSERT(DT_CHECK_LE, le, __VA_ARGS__) 2341 #define DOCTEST_REQUIRE_LE(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_LE, le, __VA_ARGS__) 2342 2343 #define DOCTEST_WARN_UNARY(...) DOCTEST_UNARY_ASSERT(DT_WARN_UNARY, __VA_ARGS__) 2344 #define DOCTEST_CHECK_UNARY(...) DOCTEST_UNARY_ASSERT(DT_CHECK_UNARY, __VA_ARGS__) 2345 #define DOCTEST_REQUIRE_UNARY(...) DOCTEST_UNARY_ASSERT(DT_REQUIRE_UNARY, __VA_ARGS__) 2346 #define DOCTEST_WARN_UNARY_FALSE(...) DOCTEST_UNARY_ASSERT(DT_WARN_UNARY_FALSE, __VA_ARGS__) 2347 #define DOCTEST_CHECK_UNARY_FALSE(...) DOCTEST_UNARY_ASSERT(DT_CHECK_UNARY_FALSE, __VA_ARGS__) 2348 #define DOCTEST_REQUIRE_UNARY_FALSE(...) DOCTEST_UNARY_ASSERT(DT_REQUIRE_UNARY_FALSE, __VA_ARGS__) 2349 2350 #ifdef DOCTEST_CONFIG_NO_EXCEPTIONS 2351 2352 #undef DOCTEST_WARN_THROWS 2353 #undef DOCTEST_CHECK_THROWS 2354 #undef DOCTEST_REQUIRE_THROWS 2355 #undef DOCTEST_WARN_THROWS_AS 2356 #undef DOCTEST_CHECK_THROWS_AS 2357 #undef DOCTEST_REQUIRE_THROWS_AS 2358 #undef DOCTEST_WARN_THROWS_WITH 2359 #undef DOCTEST_CHECK_THROWS_WITH 2360 #undef DOCTEST_REQUIRE_THROWS_WITH 2361 #undef DOCTEST_WARN_THROWS_WITH_AS 2362 #undef DOCTEST_CHECK_THROWS_WITH_AS 2363 #undef DOCTEST_REQUIRE_THROWS_WITH_AS 2364 #undef DOCTEST_WARN_NOTHROW 2365 #undef DOCTEST_CHECK_NOTHROW 2366 #undef DOCTEST_REQUIRE_NOTHROW 2367 2368 #undef DOCTEST_WARN_THROWS_MESSAGE 2369 #undef DOCTEST_CHECK_THROWS_MESSAGE 2370 #undef DOCTEST_REQUIRE_THROWS_MESSAGE 2371 #undef DOCTEST_WARN_THROWS_AS_MESSAGE 2372 #undef DOCTEST_CHECK_THROWS_AS_MESSAGE 2373 #undef DOCTEST_REQUIRE_THROWS_AS_MESSAGE 2374 #undef DOCTEST_WARN_THROWS_WITH_MESSAGE 2375 #undef DOCTEST_CHECK_THROWS_WITH_MESSAGE 2376 #undef DOCTEST_REQUIRE_THROWS_WITH_MESSAGE 2377 #undef DOCTEST_WARN_THROWS_WITH_AS_MESSAGE 2378 #undef DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE 2379 #undef DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE 2380 #undef DOCTEST_WARN_NOTHROW_MESSAGE 2381 #undef DOCTEST_CHECK_NOTHROW_MESSAGE 2382 #undef DOCTEST_REQUIRE_NOTHROW_MESSAGE 2383 2384 #ifdef DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS 2385 2386 #define DOCTEST_WARN_THROWS(...) (static_cast<void>(0)) 2387 #define DOCTEST_CHECK_THROWS(...) (static_cast<void>(0)) 2388 #define DOCTEST_REQUIRE_THROWS(...) (static_cast<void>(0)) 2389 #define DOCTEST_WARN_THROWS_AS(expr, ...) (static_cast<void>(0)) 2390 #define DOCTEST_CHECK_THROWS_AS(expr, ...) (static_cast<void>(0)) 2391 #define DOCTEST_REQUIRE_THROWS_AS(expr, ...) (static_cast<void>(0)) 2392 #define DOCTEST_WARN_THROWS_WITH(expr, ...) (static_cast<void>(0)) 2393 #define DOCTEST_CHECK_THROWS_WITH(expr, ...) (static_cast<void>(0)) 2394 #define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) (static_cast<void>(0)) 2395 #define DOCTEST_WARN_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) 2396 #define DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) 2397 #define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) 2398 #define DOCTEST_WARN_NOTHROW(...) (static_cast<void>(0)) 2399 #define DOCTEST_CHECK_NOTHROW(...) (static_cast<void>(0)) 2400 #define DOCTEST_REQUIRE_NOTHROW(...) (static_cast<void>(0)) 2401 2402 #define DOCTEST_WARN_THROWS_MESSAGE(expr, ...) (static_cast<void>(0)) 2403 #define DOCTEST_CHECK_THROWS_MESSAGE(expr, ...) (static_cast<void>(0)) 2404 #define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, ...) (static_cast<void>(0)) 2405 #define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast<void>(0)) 2406 #define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast<void>(0)) 2407 #define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast<void>(0)) 2408 #define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast<void>(0)) 2409 #define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast<void>(0)) 2410 #define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast<void>(0)) 2411 #define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast<void>(0)) 2412 #define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast<void>(0)) 2413 #define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast<void>(0)) 2414 #define DOCTEST_WARN_NOTHROW_MESSAGE(expr, ...) (static_cast<void>(0)) 2415 #define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, ...) (static_cast<void>(0)) 2416 #define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, ...) (static_cast<void>(0)) 2417 2418 #else // DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS 2419 2420 #undef DOCTEST_REQUIRE 2421 #undef DOCTEST_REQUIRE_FALSE 2422 #undef DOCTEST_REQUIRE_MESSAGE 2423 #undef DOCTEST_REQUIRE_FALSE_MESSAGE 2424 #undef DOCTEST_REQUIRE_EQ 2425 #undef DOCTEST_REQUIRE_NE 2426 #undef DOCTEST_REQUIRE_GT 2427 #undef DOCTEST_REQUIRE_LT 2428 #undef DOCTEST_REQUIRE_GE 2429 #undef DOCTEST_REQUIRE_LE 2430 #undef DOCTEST_REQUIRE_UNARY 2431 #undef DOCTEST_REQUIRE_UNARY_FALSE 2432 2433 #endif // DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS 2434 2435 #endif // DOCTEST_CONFIG_NO_EXCEPTIONS 2436 2437 // ================================================================================================= 2438 // == WHAT FOLLOWS IS VERSIONS OF THE MACROS THAT DO NOT DO ANY REGISTERING! == 2439 // == THIS CAN BE ENABLED BY DEFINING DOCTEST_CONFIG_DISABLE GLOBALLY! == 2440 // ================================================================================================= 2441 #else // DOCTEST_CONFIG_DISABLE 2442 2443 #define DOCTEST_IMPLEMENT_FIXTURE(der, base, func, name) \ 2444 namespace { \ 2445 template <typename DOCTEST_UNUSED_TEMPLATE_TYPE> \ 2446 struct der : public base \ 2447 { void f(); }; \ 2448 } \ 2449 template <typename DOCTEST_UNUSED_TEMPLATE_TYPE> \ 2450 inline void der<DOCTEST_UNUSED_TEMPLATE_TYPE>::f() 2451 2452 #define DOCTEST_CREATE_AND_REGISTER_FUNCTION(f, name) \ 2453 template <typename DOCTEST_UNUSED_TEMPLATE_TYPE> \ 2454 static inline void f() 2455 2456 // for registering tests 2457 #define DOCTEST_TEST_CASE(name) \ 2458 DOCTEST_CREATE_AND_REGISTER_FUNCTION(DOCTEST_ANONYMOUS(DOCTEST_ANON_FUNC_), name) 2459 2460 // for registering tests in classes 2461 #define DOCTEST_TEST_CASE_CLASS(name) \ 2462 DOCTEST_CREATE_AND_REGISTER_FUNCTION(DOCTEST_ANONYMOUS(DOCTEST_ANON_FUNC_), name) 2463 2464 // for registering tests with a fixture 2465 #define DOCTEST_TEST_CASE_FIXTURE(x, name) \ 2466 DOCTEST_IMPLEMENT_FIXTURE(DOCTEST_ANONYMOUS(DOCTEST_ANON_CLASS_), x, \ 2467 DOCTEST_ANONYMOUS(DOCTEST_ANON_FUNC_), name) 2468 2469 // for converting types to strings without the <typeinfo> header and demangling 2470 #define DOCTEST_TYPE_TO_STRING(...) typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2471 #define DOCTEST_TYPE_TO_STRING_IMPL(...) 2472 2473 // for typed tests 2474 #define DOCTEST_TEST_CASE_TEMPLATE(name, type, ...) \ 2475 template <typename type> \ 2476 inline void DOCTEST_ANONYMOUS(DOCTEST_ANON_TMP_)() 2477 2478 #define DOCTEST_TEST_CASE_TEMPLATE_DEFINE(name, type, id) \ 2479 template <typename type> \ 2480 inline void DOCTEST_ANONYMOUS(DOCTEST_ANON_TMP_)() 2481 2482 #define DOCTEST_TEST_CASE_TEMPLATE_INVOKE(id, ...) \ 2483 typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2484 2485 #define DOCTEST_TEST_CASE_TEMPLATE_APPLY(id, ...) \ 2486 typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2487 2488 // for subcases 2489 #define DOCTEST_SUBCASE(name) 2490 2491 // for a testsuite block 2492 #define DOCTEST_TEST_SUITE(name) namespace 2493 2494 // for starting a testsuite block 2495 #define DOCTEST_TEST_SUITE_BEGIN(name) typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2496 2497 // for ending a testsuite block 2498 #define DOCTEST_TEST_SUITE_END typedef int DOCTEST_ANONYMOUS(DOCTEST_ANON_FOR_SEMICOLON_) 2499 2500 #define DOCTEST_REGISTER_EXCEPTION_TRANSLATOR(signature) \ 2501 template <typename DOCTEST_UNUSED_TEMPLATE_TYPE> \ 2502 static inline doctest::String DOCTEST_ANONYMOUS(DOCTEST_ANON_TRANSLATOR_)(signature) 2503 2504 #define DOCTEST_REGISTER_REPORTER(name, priority, reporter) 2505 #define DOCTEST_REGISTER_LISTENER(name, priority, reporter) 2506 2507 #define DOCTEST_INFO(...) (static_cast<void>(0)) 2508 #define DOCTEST_CAPTURE(x) (static_cast<void>(0)) 2509 #define DOCTEST_ADD_MESSAGE_AT(file, line, ...) (static_cast<void>(0)) 2510 #define DOCTEST_ADD_FAIL_CHECK_AT(file, line, ...) (static_cast<void>(0)) 2511 #define DOCTEST_ADD_FAIL_AT(file, line, ...) (static_cast<void>(0)) 2512 #define DOCTEST_MESSAGE(...) (static_cast<void>(0)) 2513 #define DOCTEST_FAIL_CHECK(...) (static_cast<void>(0)) 2514 #define DOCTEST_FAIL(...) (static_cast<void>(0)) 2515 2516 #define DOCTEST_WARN(...) (static_cast<void>(0)) 2517 #define DOCTEST_CHECK(...) (static_cast<void>(0)) 2518 #define DOCTEST_REQUIRE(...) (static_cast<void>(0)) 2519 #define DOCTEST_WARN_FALSE(...) (static_cast<void>(0)) 2520 #define DOCTEST_CHECK_FALSE(...) (static_cast<void>(0)) 2521 #define DOCTEST_REQUIRE_FALSE(...) (static_cast<void>(0)) 2522 2523 #define DOCTEST_WARN_MESSAGE(cond, ...) (static_cast<void>(0)) 2524 #define DOCTEST_CHECK_MESSAGE(cond, ...) (static_cast<void>(0)) 2525 #define DOCTEST_REQUIRE_MESSAGE(cond, ...) (static_cast<void>(0)) 2526 #define DOCTEST_WARN_FALSE_MESSAGE(cond, ...) (static_cast<void>(0)) 2527 #define DOCTEST_CHECK_FALSE_MESSAGE(cond, ...) (static_cast<void>(0)) 2528 #define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, ...) (static_cast<void>(0)) 2529 2530 #define DOCTEST_WARN_THROWS(...) (static_cast<void>(0)) 2531 #define DOCTEST_CHECK_THROWS(...) (static_cast<void>(0)) 2532 #define DOCTEST_REQUIRE_THROWS(...) (static_cast<void>(0)) 2533 #define DOCTEST_WARN_THROWS_AS(expr, ...) (static_cast<void>(0)) 2534 #define DOCTEST_CHECK_THROWS_AS(expr, ...) (static_cast<void>(0)) 2535 #define DOCTEST_REQUIRE_THROWS_AS(expr, ...) (static_cast<void>(0)) 2536 #define DOCTEST_WARN_THROWS_WITH(expr, ...) (static_cast<void>(0)) 2537 #define DOCTEST_CHECK_THROWS_WITH(expr, ...) (static_cast<void>(0)) 2538 #define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) (static_cast<void>(0)) 2539 #define DOCTEST_WARN_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) 2540 #define DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) 2541 #define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) 2542 #define DOCTEST_WARN_NOTHROW(...) (static_cast<void>(0)) 2543 #define DOCTEST_CHECK_NOTHROW(...) (static_cast<void>(0)) 2544 #define DOCTEST_REQUIRE_NOTHROW(...) (static_cast<void>(0)) 2545 2546 #define DOCTEST_WARN_THROWS_MESSAGE(expr, ...) (static_cast<void>(0)) 2547 #define DOCTEST_CHECK_THROWS_MESSAGE(expr, ...) (static_cast<void>(0)) 2548 #define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, ...) (static_cast<void>(0)) 2549 #define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast<void>(0)) 2550 #define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast<void>(0)) 2551 #define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, ...) (static_cast<void>(0)) 2552 #define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast<void>(0)) 2553 #define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast<void>(0)) 2554 #define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, ...) (static_cast<void>(0)) 2555 #define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast<void>(0)) 2556 #define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast<void>(0)) 2557 #define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) (static_cast<void>(0)) 2558 #define DOCTEST_WARN_NOTHROW_MESSAGE(expr, ...) (static_cast<void>(0)) 2559 #define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, ...) (static_cast<void>(0)) 2560 #define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, ...) (static_cast<void>(0)) 2561 2562 #define DOCTEST_WARN_EQ(...) (static_cast<void>(0)) 2563 #define DOCTEST_CHECK_EQ(...) (static_cast<void>(0)) 2564 #define DOCTEST_REQUIRE_EQ(...) (static_cast<void>(0)) 2565 #define DOCTEST_WARN_NE(...) (static_cast<void>(0)) 2566 #define DOCTEST_CHECK_NE(...) (static_cast<void>(0)) 2567 #define DOCTEST_REQUIRE_NE(...) (static_cast<void>(0)) 2568 #define DOCTEST_WARN_GT(...) (static_cast<void>(0)) 2569 #define DOCTEST_CHECK_GT(...) (static_cast<void>(0)) 2570 #define DOCTEST_REQUIRE_GT(...) (static_cast<void>(0)) 2571 #define DOCTEST_WARN_LT(...) (static_cast<void>(0)) 2572 #define DOCTEST_CHECK_LT(...) (static_cast<void>(0)) 2573 #define DOCTEST_REQUIRE_LT(...) (static_cast<void>(0)) 2574 #define DOCTEST_WARN_GE(...) (static_cast<void>(0)) 2575 #define DOCTEST_CHECK_GE(...) (static_cast<void>(0)) 2576 #define DOCTEST_REQUIRE_GE(...) (static_cast<void>(0)) 2577 #define DOCTEST_WARN_LE(...) (static_cast<void>(0)) 2578 #define DOCTEST_CHECK_LE(...) (static_cast<void>(0)) 2579 #define DOCTEST_REQUIRE_LE(...) (static_cast<void>(0)) 2580 2581 #define DOCTEST_WARN_UNARY(...) (static_cast<void>(0)) 2582 #define DOCTEST_CHECK_UNARY(...) (static_cast<void>(0)) 2583 #define DOCTEST_REQUIRE_UNARY(...) (static_cast<void>(0)) 2584 #define DOCTEST_WARN_UNARY_FALSE(...) (static_cast<void>(0)) 2585 #define DOCTEST_CHECK_UNARY_FALSE(...) (static_cast<void>(0)) 2586 #define DOCTEST_REQUIRE_UNARY_FALSE(...) (static_cast<void>(0)) 2587 2588 #endif // DOCTEST_CONFIG_DISABLE 2589 2590 // clang-format off 2591 // KEPT FOR BACKWARDS COMPATIBILITY - FORWARDING TO THE RIGHT MACROS 2592 #define DOCTEST_FAST_WARN_EQ DOCTEST_WARN_EQ 2593 #define DOCTEST_FAST_CHECK_EQ DOCTEST_CHECK_EQ 2594 #define DOCTEST_FAST_REQUIRE_EQ DOCTEST_REQUIRE_EQ 2595 #define DOCTEST_FAST_WARN_NE DOCTEST_WARN_NE 2596 #define DOCTEST_FAST_CHECK_NE DOCTEST_CHECK_NE 2597 #define DOCTEST_FAST_REQUIRE_NE DOCTEST_REQUIRE_NE 2598 #define DOCTEST_FAST_WARN_GT DOCTEST_WARN_GT 2599 #define DOCTEST_FAST_CHECK_GT DOCTEST_CHECK_GT 2600 #define DOCTEST_FAST_REQUIRE_GT DOCTEST_REQUIRE_GT 2601 #define DOCTEST_FAST_WARN_LT DOCTEST_WARN_LT 2602 #define DOCTEST_FAST_CHECK_LT DOCTEST_CHECK_LT 2603 #define DOCTEST_FAST_REQUIRE_LT DOCTEST_REQUIRE_LT 2604 #define DOCTEST_FAST_WARN_GE DOCTEST_WARN_GE 2605 #define DOCTEST_FAST_CHECK_GE DOCTEST_CHECK_GE 2606 #define DOCTEST_FAST_REQUIRE_GE DOCTEST_REQUIRE_GE 2607 #define DOCTEST_FAST_WARN_LE DOCTEST_WARN_LE 2608 #define DOCTEST_FAST_CHECK_LE DOCTEST_CHECK_LE 2609 #define DOCTEST_FAST_REQUIRE_LE DOCTEST_REQUIRE_LE 2610 2611 #define DOCTEST_FAST_WARN_UNARY DOCTEST_WARN_UNARY 2612 #define DOCTEST_FAST_CHECK_UNARY DOCTEST_CHECK_UNARY 2613 #define DOCTEST_FAST_REQUIRE_UNARY DOCTEST_REQUIRE_UNARY 2614 #define DOCTEST_FAST_WARN_UNARY_FALSE DOCTEST_WARN_UNARY_FALSE 2615 #define DOCTEST_FAST_CHECK_UNARY_FALSE DOCTEST_CHECK_UNARY_FALSE 2616 #define DOCTEST_FAST_REQUIRE_UNARY_FALSE DOCTEST_REQUIRE_UNARY_FALSE 2617 2618 #define DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE(id, ...) DOCTEST_TEST_CASE_TEMPLATE_INVOKE(id,__VA_ARGS__) 2619 // clang-format on 2620 2621 // BDD style macros 2622 // clang-format off 2623 #define DOCTEST_SCENARIO(name) DOCTEST_TEST_CASE(" Scenario: " name) 2624 #define DOCTEST_SCENARIO_CLASS(name) DOCTEST_TEST_CASE_CLASS(" Scenario: " name) 2625 #define DOCTEST_SCENARIO_TEMPLATE(name, T, ...) DOCTEST_TEST_CASE_TEMPLATE(" Scenario: " name, T, __VA_ARGS__) 2626 #define DOCTEST_SCENARIO_TEMPLATE_DEFINE(name, T, id) DOCTEST_TEST_CASE_TEMPLATE_DEFINE(" Scenario: " name, T, id) 2627 2628 #define DOCTEST_GIVEN(name) DOCTEST_SUBCASE(" Given: " name) 2629 #define DOCTEST_WHEN(name) DOCTEST_SUBCASE(" When: " name) 2630 #define DOCTEST_AND_WHEN(name) DOCTEST_SUBCASE("And when: " name) 2631 #define DOCTEST_THEN(name) DOCTEST_SUBCASE(" Then: " name) 2632 #define DOCTEST_AND_THEN(name) DOCTEST_SUBCASE(" And: " name) 2633 // clang-format on 2634 2635 // == SHORT VERSIONS OF THE MACROS 2636 #if !defined(DOCTEST_CONFIG_NO_SHORT_MACRO_NAMES) 2637 2638 #define TEST_CASE(name) DOCTEST_TEST_CASE(name) 2639 #define TEST_CASE_CLASS(name) DOCTEST_TEST_CASE_CLASS(name) 2640 #define TEST_CASE_FIXTURE(x, name) DOCTEST_TEST_CASE_FIXTURE(x, name) 2641 #define TYPE_TO_STRING(...) DOCTEST_TYPE_TO_STRING(__VA_ARGS__) 2642 #define TEST_CASE_TEMPLATE(name, T, ...) DOCTEST_TEST_CASE_TEMPLATE(name, T, __VA_ARGS__) 2643 #define TEST_CASE_TEMPLATE_DEFINE(name, T, id) DOCTEST_TEST_CASE_TEMPLATE_DEFINE(name, T, id) 2644 #define TEST_CASE_TEMPLATE_INVOKE(id, ...) DOCTEST_TEST_CASE_TEMPLATE_INVOKE(id, __VA_ARGS__) 2645 #define TEST_CASE_TEMPLATE_APPLY(id, ...) DOCTEST_TEST_CASE_TEMPLATE_APPLY(id, __VA_ARGS__) 2646 #define SUBCASE(name) DOCTEST_SUBCASE(name) 2647 #define TEST_SUITE(decorators) DOCTEST_TEST_SUITE(decorators) 2648 #define TEST_SUITE_BEGIN(name) DOCTEST_TEST_SUITE_BEGIN(name) 2649 #define TEST_SUITE_END DOCTEST_TEST_SUITE_END 2650 #define REGISTER_EXCEPTION_TRANSLATOR(signature) DOCTEST_REGISTER_EXCEPTION_TRANSLATOR(signature) 2651 #define REGISTER_REPORTER(name, priority, reporter) DOCTEST_REGISTER_REPORTER(name, priority, reporter) 2652 #define REGISTER_LISTENER(name, priority, reporter) DOCTEST_REGISTER_LISTENER(name, priority, reporter) 2653 #define INFO(...) DOCTEST_INFO(__VA_ARGS__) 2654 #define CAPTURE(x) DOCTEST_CAPTURE(x) 2655 #define ADD_MESSAGE_AT(file, line, ...) DOCTEST_ADD_MESSAGE_AT(file, line, __VA_ARGS__) 2656 #define ADD_FAIL_CHECK_AT(file, line, ...) DOCTEST_ADD_FAIL_CHECK_AT(file, line, __VA_ARGS__) 2657 #define ADD_FAIL_AT(file, line, ...) DOCTEST_ADD_FAIL_AT(file, line, __VA_ARGS__) 2658 #define MESSAGE(...) DOCTEST_MESSAGE(__VA_ARGS__) 2659 #define FAIL_CHECK(...) DOCTEST_FAIL_CHECK(__VA_ARGS__) 2660 #define FAIL(...) DOCTEST_FAIL(__VA_ARGS__) 2661 #define TO_LVALUE(...) DOCTEST_TO_LVALUE(__VA_ARGS__) 2662 2663 #define WARN(...) DOCTEST_WARN(__VA_ARGS__) 2664 #define WARN_FALSE(...) DOCTEST_WARN_FALSE(__VA_ARGS__) 2665 #define WARN_THROWS(...) DOCTEST_WARN_THROWS(__VA_ARGS__) 2666 #define WARN_THROWS_AS(expr, ...) DOCTEST_WARN_THROWS_AS(expr, __VA_ARGS__) 2667 #define WARN_THROWS_WITH(expr, ...) DOCTEST_WARN_THROWS_WITH(expr, __VA_ARGS__) 2668 #define WARN_THROWS_WITH_AS(expr, with, ...) DOCTEST_WARN_THROWS_WITH_AS(expr, with, __VA_ARGS__) 2669 #define WARN_NOTHROW(...) DOCTEST_WARN_NOTHROW(__VA_ARGS__) 2670 #define CHECK(...) DOCTEST_CHECK(__VA_ARGS__) 2671 #define CHECK_FALSE(...) DOCTEST_CHECK_FALSE(__VA_ARGS__) 2672 #define CHECK_THROWS(...) DOCTEST_CHECK_THROWS(__VA_ARGS__) 2673 #define CHECK_THROWS_AS(expr, ...) DOCTEST_CHECK_THROWS_AS(expr, __VA_ARGS__) 2674 #define CHECK_THROWS_WITH(expr, ...) DOCTEST_CHECK_THROWS_WITH(expr, __VA_ARGS__) 2675 #define CHECK_THROWS_WITH_AS(expr, with, ...) DOCTEST_CHECK_THROWS_WITH_AS(expr, with, __VA_ARGS__) 2676 #define CHECK_NOTHROW(...) DOCTEST_CHECK_NOTHROW(__VA_ARGS__) 2677 #define REQUIRE(...) DOCTEST_REQUIRE(__VA_ARGS__) 2678 #define REQUIRE_FALSE(...) DOCTEST_REQUIRE_FALSE(__VA_ARGS__) 2679 #define REQUIRE_THROWS(...) DOCTEST_REQUIRE_THROWS(__VA_ARGS__) 2680 #define REQUIRE_THROWS_AS(expr, ...) DOCTEST_REQUIRE_THROWS_AS(expr, __VA_ARGS__) 2681 #define REQUIRE_THROWS_WITH(expr, ...) DOCTEST_REQUIRE_THROWS_WITH(expr, __VA_ARGS__) 2682 #define REQUIRE_THROWS_WITH_AS(expr, with, ...) DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, __VA_ARGS__) 2683 #define REQUIRE_NOTHROW(...) DOCTEST_REQUIRE_NOTHROW(__VA_ARGS__) 2684 2685 #define WARN_MESSAGE(cond, ...) DOCTEST_WARN_MESSAGE(cond, __VA_ARGS__) 2686 #define WARN_FALSE_MESSAGE(cond, ...) DOCTEST_WARN_FALSE_MESSAGE(cond, __VA_ARGS__) 2687 #define WARN_THROWS_MESSAGE(expr, ...) DOCTEST_WARN_THROWS_MESSAGE(expr, __VA_ARGS__) 2688 #define WARN_THROWS_AS_MESSAGE(expr, ex, ...) DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, __VA_ARGS__) 2689 #define WARN_THROWS_WITH_MESSAGE(expr, with, ...) DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, __VA_ARGS__) 2690 #define WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, __VA_ARGS__) 2691 #define WARN_NOTHROW_MESSAGE(expr, ...) DOCTEST_WARN_NOTHROW_MESSAGE(expr, __VA_ARGS__) 2692 #define CHECK_MESSAGE(cond, ...) DOCTEST_CHECK_MESSAGE(cond, __VA_ARGS__) 2693 #define CHECK_FALSE_MESSAGE(cond, ...) DOCTEST_CHECK_FALSE_MESSAGE(cond, __VA_ARGS__) 2694 #define CHECK_THROWS_MESSAGE(expr, ...) DOCTEST_CHECK_THROWS_MESSAGE(expr, __VA_ARGS__) 2695 #define CHECK_THROWS_AS_MESSAGE(expr, ex, ...) DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, __VA_ARGS__) 2696 #define CHECK_THROWS_WITH_MESSAGE(expr, with, ...) DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, __VA_ARGS__) 2697 #define CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, __VA_ARGS__) 2698 #define CHECK_NOTHROW_MESSAGE(expr, ...) DOCTEST_CHECK_NOTHROW_MESSAGE(expr, __VA_ARGS__) 2699 #define REQUIRE_MESSAGE(cond, ...) DOCTEST_REQUIRE_MESSAGE(cond, __VA_ARGS__) 2700 #define REQUIRE_FALSE_MESSAGE(cond, ...) DOCTEST_REQUIRE_FALSE_MESSAGE(cond, __VA_ARGS__) 2701 #define REQUIRE_THROWS_MESSAGE(expr, ...) DOCTEST_REQUIRE_THROWS_MESSAGE(expr, __VA_ARGS__) 2702 #define REQUIRE_THROWS_AS_MESSAGE(expr, ex, ...) DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, __VA_ARGS__) 2703 #define REQUIRE_THROWS_WITH_MESSAGE(expr, with, ...) DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, __VA_ARGS__) 2704 #define REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, ...) DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, __VA_ARGS__) 2705 #define REQUIRE_NOTHROW_MESSAGE(expr, ...) DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, __VA_ARGS__) 2706 2707 #define SCENARIO(name) DOCTEST_SCENARIO(name) 2708 #define SCENARIO_CLASS(name) DOCTEST_SCENARIO_CLASS(name) 2709 #define SCENARIO_TEMPLATE(name, T, ...) DOCTEST_SCENARIO_TEMPLATE(name, T, __VA_ARGS__) 2710 #define SCENARIO_TEMPLATE_DEFINE(name, T, id) DOCTEST_SCENARIO_TEMPLATE_DEFINE(name, T, id) 2711 #define GIVEN(name) DOCTEST_GIVEN(name) 2712 #define WHEN(name) DOCTEST_WHEN(name) 2713 #define AND_WHEN(name) DOCTEST_AND_WHEN(name) 2714 #define THEN(name) DOCTEST_THEN(name) 2715 #define AND_THEN(name) DOCTEST_AND_THEN(name) 2716 2717 #define WARN_EQ(...) DOCTEST_WARN_EQ(__VA_ARGS__) 2718 #define CHECK_EQ(...) DOCTEST_CHECK_EQ(__VA_ARGS__) 2719 #define REQUIRE_EQ(...) DOCTEST_REQUIRE_EQ(__VA_ARGS__) 2720 #define WARN_NE(...) DOCTEST_WARN_NE(__VA_ARGS__) 2721 #define CHECK_NE(...) DOCTEST_CHECK_NE(__VA_ARGS__) 2722 #define REQUIRE_NE(...) DOCTEST_REQUIRE_NE(__VA_ARGS__) 2723 #define WARN_GT(...) DOCTEST_WARN_GT(__VA_ARGS__) 2724 #define CHECK_GT(...) DOCTEST_CHECK_GT(__VA_ARGS__) 2725 #define REQUIRE_GT(...) DOCTEST_REQUIRE_GT(__VA_ARGS__) 2726 #define WARN_LT(...) DOCTEST_WARN_LT(__VA_ARGS__) 2727 #define CHECK_LT(...) DOCTEST_CHECK_LT(__VA_ARGS__) 2728 #define REQUIRE_LT(...) DOCTEST_REQUIRE_LT(__VA_ARGS__) 2729 #define WARN_GE(...) DOCTEST_WARN_GE(__VA_ARGS__) 2730 #define CHECK_GE(...) DOCTEST_CHECK_GE(__VA_ARGS__) 2731 #define REQUIRE_GE(...) DOCTEST_REQUIRE_GE(__VA_ARGS__) 2732 #define WARN_LE(...) DOCTEST_WARN_LE(__VA_ARGS__) 2733 #define CHECK_LE(...) DOCTEST_CHECK_LE(__VA_ARGS__) 2734 #define REQUIRE_LE(...) DOCTEST_REQUIRE_LE(__VA_ARGS__) 2735 #define WARN_UNARY(...) DOCTEST_WARN_UNARY(__VA_ARGS__) 2736 #define CHECK_UNARY(...) DOCTEST_CHECK_UNARY(__VA_ARGS__) 2737 #define REQUIRE_UNARY(...) DOCTEST_REQUIRE_UNARY(__VA_ARGS__) 2738 #define WARN_UNARY_FALSE(...) DOCTEST_WARN_UNARY_FALSE(__VA_ARGS__) 2739 #define CHECK_UNARY_FALSE(...) DOCTEST_CHECK_UNARY_FALSE(__VA_ARGS__) 2740 #define REQUIRE_UNARY_FALSE(...) DOCTEST_REQUIRE_UNARY_FALSE(__VA_ARGS__) 2741 2742 // KEPT FOR BACKWARDS COMPATIBILITY 2743 #define FAST_WARN_EQ(...) DOCTEST_FAST_WARN_EQ(__VA_ARGS__) 2744 #define FAST_CHECK_EQ(...) DOCTEST_FAST_CHECK_EQ(__VA_ARGS__) 2745 #define FAST_REQUIRE_EQ(...) DOCTEST_FAST_REQUIRE_EQ(__VA_ARGS__) 2746 #define FAST_WARN_NE(...) DOCTEST_FAST_WARN_NE(__VA_ARGS__) 2747 #define FAST_CHECK_NE(...) DOCTEST_FAST_CHECK_NE(__VA_ARGS__) 2748 #define FAST_REQUIRE_NE(...) DOCTEST_FAST_REQUIRE_NE(__VA_ARGS__) 2749 #define FAST_WARN_GT(...) DOCTEST_FAST_WARN_GT(__VA_ARGS__) 2750 #define FAST_CHECK_GT(...) DOCTEST_FAST_CHECK_GT(__VA_ARGS__) 2751 #define FAST_REQUIRE_GT(...) DOCTEST_FAST_REQUIRE_GT(__VA_ARGS__) 2752 #define FAST_WARN_LT(...) DOCTEST_FAST_WARN_LT(__VA_ARGS__) 2753 #define FAST_CHECK_LT(...) DOCTEST_FAST_CHECK_LT(__VA_ARGS__) 2754 #define FAST_REQUIRE_LT(...) DOCTEST_FAST_REQUIRE_LT(__VA_ARGS__) 2755 #define FAST_WARN_GE(...) DOCTEST_FAST_WARN_GE(__VA_ARGS__) 2756 #define FAST_CHECK_GE(...) DOCTEST_FAST_CHECK_GE(__VA_ARGS__) 2757 #define FAST_REQUIRE_GE(...) DOCTEST_FAST_REQUIRE_GE(__VA_ARGS__) 2758 #define FAST_WARN_LE(...) DOCTEST_FAST_WARN_LE(__VA_ARGS__) 2759 #define FAST_CHECK_LE(...) DOCTEST_FAST_CHECK_LE(__VA_ARGS__) 2760 #define FAST_REQUIRE_LE(...) DOCTEST_FAST_REQUIRE_LE(__VA_ARGS__) 2761 2762 #define FAST_WARN_UNARY(...) DOCTEST_FAST_WARN_UNARY(__VA_ARGS__) 2763 #define FAST_CHECK_UNARY(...) DOCTEST_FAST_CHECK_UNARY(__VA_ARGS__) 2764 #define FAST_REQUIRE_UNARY(...) DOCTEST_FAST_REQUIRE_UNARY(__VA_ARGS__) 2765 #define FAST_WARN_UNARY_FALSE(...) DOCTEST_FAST_WARN_UNARY_FALSE(__VA_ARGS__) 2766 #define FAST_CHECK_UNARY_FALSE(...) DOCTEST_FAST_CHECK_UNARY_FALSE(__VA_ARGS__) 2767 #define FAST_REQUIRE_UNARY_FALSE(...) DOCTEST_FAST_REQUIRE_UNARY_FALSE(__VA_ARGS__) 2768 2769 #define TEST_CASE_TEMPLATE_INSTANTIATE(id, ...) DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE(id, __VA_ARGS__) 2770 2771 #endif // DOCTEST_CONFIG_NO_SHORT_MACRO_NAMES 2772 2773 #if !defined(DOCTEST_CONFIG_DISABLE) 2774 2775 // this is here to clear the 'current test suite' for the current translation unit - at the top 2776 DOCTEST_TEST_SUITE_END(); 2777 2778 // add stringification for primitive/fundamental types 2779 namespace doctest { namespace detail { 2780 DOCTEST_TYPE_TO_STRING_IMPL(bool) 2781 DOCTEST_TYPE_TO_STRING_IMPL(float) 2782 DOCTEST_TYPE_TO_STRING_IMPL(double) 2783 DOCTEST_TYPE_TO_STRING_IMPL(long double) 2784 DOCTEST_TYPE_TO_STRING_IMPL(char) 2785 DOCTEST_TYPE_TO_STRING_IMPL(signed char) 2786 DOCTEST_TYPE_TO_STRING_IMPL(unsigned char) 2787 #if !DOCTEST_MSVC || defined(_NATIVE_WCHAR_T_DEFINED) 2788 DOCTEST_TYPE_TO_STRING_IMPL(wchar_t) 2789 #endif // not MSVC or wchar_t support enabled 2790 DOCTEST_TYPE_TO_STRING_IMPL(short int) 2791 DOCTEST_TYPE_TO_STRING_IMPL(unsigned short int) 2792 DOCTEST_TYPE_TO_STRING_IMPL(int) 2793 DOCTEST_TYPE_TO_STRING_IMPL(unsigned int) 2794 DOCTEST_TYPE_TO_STRING_IMPL(long int) 2795 DOCTEST_TYPE_TO_STRING_IMPL(unsigned long int) 2796 DOCTEST_TYPE_TO_STRING_IMPL(long long int) 2797 DOCTEST_TYPE_TO_STRING_IMPL(unsigned long long int) 2798 }} // namespace doctest::detail 2799 2800 #endif // DOCTEST_CONFIG_DISABLE 2801 2802 DOCTEST_CLANG_SUPPRESS_WARNING_POP 2803 DOCTEST_MSVC_SUPPRESS_WARNING_POP 2804 DOCTEST_GCC_SUPPRESS_WARNING_POP 2805 2806 #endif // DOCTEST_LIBRARY_INCLUDED 2807