The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

5696 lines
254 KiB

2 months ago
  1. // ======================================================================
  2. // == DO NOT MODIFY THIS FILE BY HAND - IT IS AUTO GENERATED BY CMAKE! ==
  3. // ======================================================================
  4. //
  5. // doctest.h - the lightest feature-rich C++ single-header testing framework for unit tests and TDD
  6. //
  7. // Copyright (c) 2016-2017 Viktor Kirilov
  8. //
  9. // Distributed under the MIT Software License
  10. // See accompanying file LICENSE.txt or copy at
  11. // https://opensource.org/licenses/MIT
  12. //
  13. // The documentation can be found at the library's page:
  14. // https://github.com/onqtam/doctest/blob/master/doc/markdown/readme.md
  15. //
  16. // =================================================================================================
  17. // =================================================================================================
  18. // =================================================================================================
  19. //
  20. // The library is heavily influenced by Catch - https://github.com/philsquared/Catch
  21. // which uses the Boost Software License - Version 1.0
  22. // see here - https://github.com/philsquared/Catch/blob/master/LICENSE_1_0.txt
  23. //
  24. // The concept of subcases (sections in Catch) and expression decomposition are from there.
  25. // Some parts of the code are taken directly:
  26. // - stringification - the detection of "ostream& operator<<(ostream&, const T&)" and StringMaker<>
  27. // - the Approx() helper class for floating point comparison
  28. // - colors in the console
  29. // - breaking into a debugger
  30. // - signal / SEH handling
  31. // - timer
  32. //
  33. // The expression decomposing templates are taken from lest - https://github.com/martinmoene/lest
  34. // which uses the Boost Software License - Version 1.0
  35. // see here - https://github.com/martinmoene/lest/blob/master/LICENSE_1_0.txt
  36. //
  37. // The type list and the foreach algorithm on it for C++98 are taken from Loki
  38. // - http://loki-lib.sourceforge.net/
  39. // - https://en.wikipedia.org/wiki/Loki_%28C%2B%2B%29
  40. // - https://github.com/snaewe/loki-lib
  41. // which uses the MIT Software License
  42. //
  43. // =================================================================================================
  44. // =================================================================================================
  45. // =================================================================================================
  46. // Suppress this globally (without push/pop) - there is no way to silence it in the
  47. // expression decomposition macros _Pragma() in macros doesn't work for the c++ front-end of g++
  48. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578
  49. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69543
  50. // Also the warning is completely worthless nowadays - http://stackoverflow.com/questions/14016993
  51. #if defined(__GNUC__) && !defined(__clang__)
  52. #pragma GCC diagnostic ignored "-Waggregate-return"
  53. #endif
  54. #if defined(__clang__)
  55. #pragma clang diagnostic push
  56. #pragma clang diagnostic ignored "-Wunknown-pragmas"
  57. #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  58. #pragma clang diagnostic ignored "-Wweak-vtables"
  59. #pragma clang diagnostic ignored "-Wpadded"
  60. #pragma clang diagnostic ignored "-Wdeprecated"
  61. #pragma clang diagnostic ignored "-Wmissing-prototypes"
  62. #pragma clang diagnostic ignored "-Wunused-local-typedef"
  63. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
  64. #pragma clang diagnostic ignored "-Wc++11-long-long"
  65. #endif // __clang__
  66. #if defined(__GNUC__) && !defined(__clang__)
  67. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)
  68. #pragma GCC diagnostic push
  69. #endif // > gcc 4.6
  70. #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  71. #pragma GCC diagnostic ignored "-Weffc++"
  72. #pragma GCC diagnostic ignored "-Wstrict-overflow"
  73. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  74. #pragma GCC diagnostic ignored "-Wctor-dtor-privacy"
  75. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  76. #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  77. #pragma GCC diagnostic ignored "-Winline"
  78. #pragma GCC diagnostic ignored "-Wlong-long"
  79. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)
  80. #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
  81. #endif // > gcc 4.6
  82. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 7)
  83. #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
  84. #endif // > gcc 4.7
  85. #if __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 3)
  86. #pragma GCC diagnostic ignored "-Wuseless-cast"
  87. #endif // > gcc 5.3
  88. #endif // __GNUC__
  89. #ifdef _MSC_VER
  90. #pragma warning(push)
  91. #pragma warning(disable : 4996) // The compiler encountered a deprecated declaration
  92. #pragma warning(disable : 4706) // assignment within conditional expression
  93. #pragma warning(disable : 4512) // 'class' : assignment operator could not be generated
  94. #pragma warning(disable : 4127) // conditional expression is constant
  95. #endif // _MSC_VER
  96. #ifndef DOCTEST_LIBRARY_INCLUDED
  97. #define DOCTEST_LIBRARY_INCLUDED
  98. #define DOCTEST_VERSION_MAJOR 1
  99. #define DOCTEST_VERSION_MINOR 2
  100. #define DOCTEST_VERSION_PATCH 1
  101. #define DOCTEST_VERSION_STR "1.2.1"
  102. #define DOCTEST_VERSION \
  103. (DOCTEST_VERSION_MAJOR * 10000 + DOCTEST_VERSION_MINOR * 100 + DOCTEST_VERSION_PATCH)
  104. // =================================================================================================
  105. // == FEATURE DETECTION ============================================================================
  106. // =================================================================================================
  107. #if __cplusplus >= 201103L
  108. #ifndef DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS
  109. #define DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS
  110. #endif // DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS
  111. #ifndef DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  112. #define DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  113. #endif // DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  114. #ifndef DOCTEST_CONFIG_WITH_NULLPTR
  115. #define DOCTEST_CONFIG_WITH_NULLPTR
  116. #endif // DOCTEST_CONFIG_WITH_NULLPTR
  117. #ifndef DOCTEST_CONFIG_WITH_LONG_LONG
  118. #define DOCTEST_CONFIG_WITH_LONG_LONG
  119. #endif // DOCTEST_CONFIG_WITH_LONG_LONG
  120. #ifndef DOCTEST_CONFIG_WITH_STATIC_ASSERT
  121. #define DOCTEST_CONFIG_WITH_STATIC_ASSERT
  122. #endif // DOCTEST_CONFIG_WITH_STATIC_ASSERT
  123. #ifndef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  124. #define DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  125. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  126. #endif // __cplusplus >= 201103L
  127. #ifndef __has_feature
  128. #define __has_feature(x) 0
  129. #endif // __has_feature
  130. // MSVC C++11 feature support table: https://msdn.microsoft.com/en-us/library/hh567368.aspx
  131. // GCC C++11 feature support table: https://gcc.gnu.org/projects/cxx-status.html
  132. // MSVC version table:
  133. // MSVC++ 15.0 _MSC_VER == 1910 (Visual Studio 2017)
  134. // MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
  135. // MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
  136. // MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012)
  137. // MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
  138. // MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
  139. // MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005)
  140. // deleted functions
  141. #ifndef DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS
  142. #if defined(_MSC_VER) && (_MSC_VER >= 1800)
  143. #define DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS
  144. #endif // _MSC_VER
  145. #if defined(__clang__) && __has_feature(cxx_deleted_functions)
  146. #define DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS
  147. #endif // __clang__
  148. #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4) && \
  149. defined(__GXX_EXPERIMENTAL_CXX0X__)
  150. #define DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS
  151. #endif // __GNUC__
  152. #endif // DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS
  153. #if defined(DOCTEST_CONFIG_NO_DELETED_FUNCTIONS) && defined(DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS)
  154. #undef DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS
  155. #endif // DOCTEST_CONFIG_NO_DELETED_FUNCTIONS
  156. // rvalue references
  157. #ifndef DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  158. #if defined(_MSC_VER) && (_MSC_VER >= 1600)
  159. #define DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  160. #endif // _MSC_VER
  161. #if defined(__clang__) && __has_feature(cxx_rvalue_references)
  162. #define DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  163. #endif // __clang__
  164. #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4) && \
  165. defined(__GXX_EXPERIMENTAL_CXX0X__)
  166. #define DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  167. #endif // __GNUC__
  168. #endif // DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  169. #if defined(DOCTEST_CONFIG_NO_RVALUE_REFERENCES) && defined(DOCTEST_CONFIG_WITH_RVALUE_REFERENCES)
  170. #undef DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  171. #endif // DOCTEST_CONFIG_NO_RVALUE_REFERENCES
  172. // nullptr
  173. #ifndef DOCTEST_CONFIG_WITH_NULLPTR
  174. #if defined(__clang__) && __has_feature(cxx_nullptr)
  175. #define DOCTEST_CONFIG_WITH_NULLPTR
  176. #endif // __clang__
  177. #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4) && \
  178. defined(__GXX_EXPERIMENTAL_CXX0X__)
  179. #define DOCTEST_CONFIG_WITH_NULLPTR
  180. #endif // __GNUC__
  181. #if defined(_MSC_VER) && (_MSC_VER >= 1600) // MSVC 2010
  182. #define DOCTEST_CONFIG_WITH_NULLPTR
  183. #endif // _MSC_VER
  184. #endif // DOCTEST_CONFIG_WITH_NULLPTR
  185. #if defined(DOCTEST_CONFIG_NO_NULLPTR) && defined(DOCTEST_CONFIG_WITH_NULLPTR)
  186. #undef DOCTEST_CONFIG_WITH_NULLPTR
  187. #endif // DOCTEST_CONFIG_NO_NULLPTR
  188. // variadic macros
  189. #ifndef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  190. #if defined(_MSC_VER) && _MSC_VER > 1400 && !defined(__EDGE__)
  191. #define DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  192. #endif // _MSC_VER
  193. #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || __GNUC__ > 4) && \
  194. defined(__GXX_EXPERIMENTAL_CXX0X__)
  195. #define DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  196. #endif // __GNUC__ and clang
  197. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  198. #if defined(DOCTEST_CONFIG_NO_VARIADIC_MACROS) && defined(DOCTEST_CONFIG_WITH_VARIADIC_MACROS)
  199. #undef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  200. #endif // DOCTEST_CONFIG_NO_VARIADIC_MACROS
  201. // long long
  202. #ifndef DOCTEST_CONFIG_WITH_LONG_LONG
  203. #if defined(_MSC_VER) && (_MSC_VER >= 1400)
  204. #define DOCTEST_CONFIG_WITH_LONG_LONG
  205. #endif // _MSC_VER
  206. #if(defined(__clang__) || \
  207. (defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 5) || __GNUC__ > 4))) && \
  208. defined(__GXX_EXPERIMENTAL_CXX0X__)
  209. #define DOCTEST_CONFIG_WITH_LONG_LONG
  210. #endif // __GNUC__ and clang
  211. #endif // DOCTEST_CONFIG_WITH_LONG_LONG
  212. #if defined(DOCTEST_CONFIG_NO_LONG_LONG) && defined(DOCTEST_CONFIG_WITH_LONG_LONG)
  213. #undef DOCTEST_CONFIG_WITH_LONG_LONG
  214. #endif // DOCTEST_CONFIG_NO_LONG_LONG
  215. // static_assert
  216. #ifndef DOCTEST_CONFIG_WITH_STATIC_ASSERT
  217. #if defined(__clang__) && __has_feature(cxx_static_assert)
  218. #define DOCTEST_CONFIG_WITH_STATIC_ASSERT
  219. #endif // __clang__
  220. #if defined(__GNUC__) && ((__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4) && \
  221. defined(__GXX_EXPERIMENTAL_CXX0X__)
  222. #define DOCTEST_CONFIG_WITH_STATIC_ASSERT
  223. #endif // __GNUC__
  224. #if defined(_MSC_VER) && (_MSC_VER >= 1600) // MSVC 2010
  225. #define DOCTEST_CONFIG_WITH_STATIC_ASSERT
  226. #endif // _MSC_VER
  227. #endif // DOCTEST_CONFIG_WITH_STATIC_ASSERT
  228. #if defined(DOCTEST_CONFIG_NO_STATIC_ASSERT) && defined(DOCTEST_CONFIG_WITH_STATIC_ASSERT)
  229. #undef DOCTEST_CONFIG_WITH_STATIC_ASSERT
  230. #endif // DOCTEST_CONFIG_NO_STATIC_ASSERT
  231. // other stuff...
  232. #if defined(DOCTEST_CONFIG_WITH_RVALUE_REFERENCES) || defined(DOCTEST_CONFIG_WITH_LONG_LONG) || \
  233. defined(DOCTEST_CONFIG_WITH_DELETED_FUNCTIONS) || defined(DOCTEST_CONFIG_WITH_NULLPTR) || \
  234. defined(DOCTEST_CONFIG_WITH_VARIADIC_MACROS) || defined(DOCTEST_CONFIG_WITH_STATIC_ASSERT)
  235. #define DOCTEST_NO_CPP11_COMPAT
  236. #endif // c++11 stuff
  237. #if defined(__clang__) && defined(DOCTEST_NO_CPP11_COMPAT)
  238. #pragma clang diagnostic ignored "-Wc++98-compat"
  239. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  240. #endif // __clang__ && DOCTEST_NO_CPP11_COMPAT
  241. #if defined(_MSC_VER) && !defined(DOCTEST_CONFIG_WINDOWS_SEH)
  242. #define DOCTEST_CONFIG_WINDOWS_SEH
  243. #endif // _MSC_VER
  244. #if defined(DOCTEST_CONFIG_NO_WINDOWS_SEH) && defined(DOCTEST_CONFIG_WINDOWS_SEH)
  245. #undef DOCTEST_CONFIG_WINDOWS_SEH
  246. #endif // DOCTEST_CONFIG_NO_WINDOWS_SEH
  247. #if !defined(_WIN32) && !defined(__QNX__) && !defined(DOCTEST_CONFIG_POSIX_SIGNALS)
  248. #define DOCTEST_CONFIG_POSIX_SIGNALS
  249. #endif // _WIN32
  250. #if defined(DOCTEST_CONFIG_NO_POSIX_SIGNALS) && defined(DOCTEST_CONFIG_POSIX_SIGNALS)
  251. #undef DOCTEST_CONFIG_POSIX_SIGNALS
  252. #endif // DOCTEST_CONFIG_NO_POSIX_SIGNALS
  253. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  254. #if defined(__GNUC__) && !defined(__EXCEPTIONS)
  255. #define DOCTEST_CONFIG_NO_EXCEPTIONS
  256. #endif // clang and gcc
  257. // in MSVC _HAS_EXCEPTIONS is defined in a header instead of as a project define
  258. // so we can't do the automatic detection for MSVC without including some header
  259. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  260. #ifdef DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS
  261. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  262. #define DOCTEST_CONFIG_NO_EXCEPTIONS
  263. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  264. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS
  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. #if defined(DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN) && !defined(DOCTEST_CONFIG_IMPLEMENT)
  269. #define DOCTEST_CONFIG_IMPLEMENT
  270. #endif // DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
  271. #if defined _WIN32 || defined __CYGWIN__
  272. #ifdef __GNUC__
  273. #define DOCTEST_SYMBOL_EXPORT __attribute__((dllexport))
  274. #define DOCTEST_SYMBOL_IMPORT __attribute__((dllimport))
  275. #else // __GNUC__
  276. #define DOCTEST_SYMBOL_EXPORT __declspec(dllexport)
  277. #define DOCTEST_SYMBOL_IMPORT __declspec(dllimport)
  278. #endif // __GNUC__
  279. #else // _WIN32
  280. #define DOCTEST_SYMBOL_EXPORT __attribute__((visibility("default")))
  281. #define DOCTEST_SYMBOL_IMPORT
  282. #endif // _WIN32
  283. #ifdef DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
  284. #ifdef DOCTEST_CONFIG_IMPLEMENT
  285. #define DOCTEST_INTERFACE DOCTEST_SYMBOL_EXPORT
  286. #else // DOCTEST_CONFIG_IMPLEMENT
  287. #define DOCTEST_INTERFACE DOCTEST_SYMBOL_IMPORT
  288. #endif // DOCTEST_CONFIG_IMPLEMENT
  289. #else // DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
  290. #define DOCTEST_INTERFACE
  291. #endif // DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
  292. #ifdef _MSC_VER
  293. #define DOCTEST_NOINLINE __declspec(noinline)
  294. #else // _MSC_VER
  295. #define DOCTEST_NOINLINE __attribute__((noinline))
  296. #endif // _MSC_VER
  297. #ifndef DOCTEST_CONFIG_NUM_CAPTURES_ON_STACK
  298. #define DOCTEST_CONFIG_NUM_CAPTURES_ON_STACK 5
  299. #endif // DOCTEST_CONFIG_NUM_CAPTURES_ON_STACK
  300. // =================================================================================================
  301. // == FEATURE DETECTION END ========================================================================
  302. // =================================================================================================
  303. // internal macros for string concatenation and anonymous variable name generation
  304. #define DOCTEST_CAT_IMPL(s1, s2) s1##s2
  305. #define DOCTEST_CAT(s1, s2) DOCTEST_CAT_IMPL(s1, s2)
  306. #ifdef __COUNTER__ // not standard and may be missing for some compilers
  307. #define DOCTEST_ANONYMOUS(x) DOCTEST_CAT(x, __COUNTER__)
  308. #else // __COUNTER__
  309. #define DOCTEST_ANONYMOUS(x) DOCTEST_CAT(x, __LINE__)
  310. #endif // __COUNTER__
  311. // macro for making a string out of an identifier
  312. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  313. #define DOCTEST_TOSTR_IMPL(...) #__VA_ARGS__
  314. #define DOCTEST_TOSTR(...) DOCTEST_TOSTR_IMPL(__VA_ARGS__)
  315. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  316. #define DOCTEST_TOSTR_IMPL(x) #x
  317. #define DOCTEST_TOSTR(x) DOCTEST_TOSTR_IMPL(x)
  318. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  319. // for concatenating literals and making the result a string
  320. #define DOCTEST_STR_CONCAT_TOSTR(s1, s2) DOCTEST_TOSTR(s1) DOCTEST_TOSTR(s2)
  321. // counts the number of elements in a C string
  322. #define DOCTEST_COUNTOF(x) (sizeof(x) / sizeof(x[0]))
  323. #ifndef DOCTEST_CONFIG_ASSERTION_PARAMETERS_BY_VALUE
  324. #define DOCTEST_REF_WRAP(x) x&
  325. #else // DOCTEST_CONFIG_ASSERTION_PARAMETERS_BY_VALUE
  326. #define DOCTEST_REF_WRAP(x) x
  327. #endif // DOCTEST_CONFIG_ASSERTION_PARAMETERS_BY_VALUE
  328. // not using __APPLE__ because... this is how Catch does it
  329. #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
  330. #define DOCTEST_PLATFORM_MAC
  331. #elif defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  332. #define DOCTEST_PLATFORM_IPHONE
  333. #elif defined(_WIN32) || defined(_MSC_VER)
  334. #define DOCTEST_PLATFORM_WINDOWS
  335. #else
  336. #define DOCTEST_PLATFORM_LINUX
  337. #endif
  338. #if defined(__clang__)
  339. #define DOCTEST_GLOBAL_NO_WARNINGS(var) \
  340. _Pragma("clang diagnostic push") \
  341. _Pragma("clang diagnostic ignored \"-Wglobal-constructors\"") static int var
  342. #define DOCTEST_GLOBAL_NO_WARNINGS_END() _Pragma("clang diagnostic pop")
  343. #elif defined(__GNUC__)
  344. #define DOCTEST_GLOBAL_NO_WARNINGS(var) static int var __attribute__((unused))
  345. #define DOCTEST_GLOBAL_NO_WARNINGS_END()
  346. #else // MSVC / other
  347. #define DOCTEST_GLOBAL_NO_WARNINGS(var) static int var
  348. #define DOCTEST_GLOBAL_NO_WARNINGS_END()
  349. #endif // MSVC / other
  350. // should probably take a look at https://github.com/scottt/debugbreak
  351. #ifdef DOCTEST_PLATFORM_MAC
  352. #define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :)
  353. #elif defined(_MSC_VER)
  354. #define DOCTEST_BREAK_INTO_DEBUGGER() __debugbreak()
  355. #elif defined(__MINGW32__)
  356. extern "C" __declspec(dllimport) void __stdcall DebugBreak();
  357. #define DOCTEST_BREAK_INTO_DEBUGGER() ::DebugBreak()
  358. #else // linux
  359. #define DOCTEST_BREAK_INTO_DEBUGGER() ((void)0)
  360. #endif // linux
  361. #ifdef __clang__
  362. // to detect if libc++ is being used with clang (the _LIBCPP_VERSION identifier)
  363. #include <ciso646>
  364. #endif // __clang__
  365. #ifdef _LIBCPP_VERSION
  366. // not forward declaring ostream for libc++ because I had some problems (inline namespaces vs c++98)
  367. // so the <iosfwd> header is used - also it is very light and doesn't drag a ton of stuff
  368. #include <iosfwd>
  369. #else // _LIBCPP_VERSION
  370. #ifndef DOCTEST_CONFIG_USE_IOSFWD
  371. namespace std
  372. {
  373. template <class charT>
  374. struct char_traits;
  375. template <>
  376. struct char_traits<char>;
  377. template <class charT, class traits>
  378. class basic_ostream;
  379. typedef basic_ostream<char, char_traits<char> > ostream;
  380. } // namespace std
  381. #else // DOCTEST_CONFIG_USE_IOSFWD
  382. #include <iosfwd>
  383. #endif // DOCTEST_CONFIG_USE_IOSFWD
  384. #endif // _LIBCPP_VERSION
  385. // static assert macro - because of the c++98 support requires that the message is an
  386. // identifier (no spaces and not a C string) - example without quotes: I_am_a_message
  387. // taken from here: http://stackoverflow.com/a/1980156/3162383
  388. #ifdef DOCTEST_CONFIG_WITH_STATIC_ASSERT
  389. #define DOCTEST_STATIC_ASSERT(expression, message) static_assert(expression, #message)
  390. #else // DOCTEST_CONFIG_WITH_STATIC_ASSERT
  391. #define DOCTEST_STATIC_ASSERT(expression, message) \
  392. struct DOCTEST_CAT(__static_assertion_at_line_, __LINE__) \
  393. { \
  394. doctest::detail::static_assert_impl::StaticAssertion<static_cast<bool>((expression))> \
  395. DOCTEST_CAT(DOCTEST_CAT(DOCTEST_CAT(STATIC_ASSERTION_FAILED_AT_LINE_, __LINE__), \
  396. _), \
  397. message); \
  398. }; \
  399. typedef doctest::detail::static_assert_impl::StaticAssertionTest<static_cast<int>( \
  400. sizeof(DOCTEST_CAT(__static_assertion_at_line_, __LINE__)))> \
  401. DOCTEST_CAT(__static_assertion_test_at_line_, __LINE__)
  402. #endif // DOCTEST_CONFIG_WITH_STATIC_ASSERT
  403. #ifdef DOCTEST_CONFIG_WITH_NULLPTR
  404. #ifdef _LIBCPP_VERSION
  405. #include <cstddef>
  406. #else // _LIBCPP_VERSION
  407. namespace std
  408. { typedef decltype(nullptr) nullptr_t; }
  409. #endif // _LIBCPP_VERSION
  410. #endif // DOCTEST_CONFIG_WITH_NULLPTR
  411. #ifndef DOCTEST_CONFIG_DISABLE
  412. namespace doctest
  413. {
  414. namespace detail
  415. {
  416. struct TestSuite
  417. {
  418. const char* m_test_suite;
  419. const char* m_description;
  420. bool m_skip;
  421. bool m_may_fail;
  422. bool m_should_fail;
  423. int m_expected_failures;
  424. double m_timeout;
  425. TestSuite& operator*(const char* in) {
  426. m_test_suite = in;
  427. // clear state
  428. m_description = 0;
  429. m_skip = false;
  430. m_may_fail = false;
  431. m_should_fail = false;
  432. m_expected_failures = 0;
  433. m_timeout = 0;
  434. return *this;
  435. }
  436. template <typename T>
  437. TestSuite& operator*(const T& in) {
  438. in.fill(*this);
  439. return *this;
  440. }
  441. };
  442. } // namespace detail
  443. } // namespace doctest
  444. // in a separate namespace outside of doctest because the DOCTEST_TEST_SUITE macro
  445. // introduces an anonymous namespace in which getCurrentTestSuite gets overridden
  446. namespace doctest_detail_test_suite_ns
  447. {
  448. DOCTEST_INTERFACE doctest::detail::TestSuite& getCurrentTestSuite();
  449. } // namespace doctest_detail_test_suite_ns
  450. #endif // DOCTEST_CONFIG_DISABLE
  451. namespace doctest
  452. {
  453. // A 24 byte string class (can be as small as 17 for x64 and 13 for x86) that can hold strings with length
  454. // of up to 23 chars on the stack before going on the heap - the last byte of the buffer is used for:
  455. // - "is small" bit - the highest bit - if "0" then it is small - otherwise its "1" (128)
  456. // - if small - capacity left before going on the heap - using the lowest 5 bits
  457. // - if small - 2 bits are left unused - the second and third highest ones
  458. // - if small - acts as a null terminator if strlen() is 23 (24 including the null terminator)
  459. // and the "is small" bit remains "0" ("as well as the capacity left") so its OK
  460. // Idea taken from this lecture about the string implementation of facebook/folly - fbstring
  461. // https://www.youtube.com/watch?v=kPR8h4-qZdk
  462. // TODO:
  463. // - optimizations - like not deleting memory unnecessarily in operator= and etc.
  464. // - resize/reserve/clear
  465. // - substr
  466. // - replace
  467. // - back/front
  468. // - iterator stuff
  469. // - find & friends
  470. // - push_back/pop_back
  471. // - assign/insert/erase
  472. // - relational operators as free functions - taking const char* as one of the params
  473. class DOCTEST_INTERFACE String
  474. {
  475. static const unsigned len = 24; //!OCLINT avoid private static members
  476. static const unsigned last = len - 1; //!OCLINT avoid private static members
  477. struct view // len should be more than sizeof(view) - because of the final byte for flags
  478. {
  479. char* ptr;
  480. unsigned size;
  481. unsigned capacity;
  482. };
  483. union
  484. {
  485. char buf[len];
  486. view data;
  487. };
  488. void copy(const String& other);
  489. void setOnHeap() { *reinterpret_cast<unsigned char*>(&buf[last]) = 128; }
  490. void setLast(unsigned in = last) { buf[last] = char(in); }
  491. public:
  492. String() {
  493. buf[0] = '\0';
  494. setLast();
  495. }
  496. String(const char* in);
  497. String(const String& other) { copy(other); }
  498. ~String() {
  499. if(!isOnStack())
  500. delete[] data.ptr;
  501. }
  502. // GCC 4.9/5/6 report Wstrict-overflow when optimizations are ON and it got inlined in the vector class somewhere...
  503. // see commit 574ef95f0cd379118be5011704664e4b5351f1e0 and build https://travis-ci.org/onqtam/doctest/builds/230671611
  504. DOCTEST_NOINLINE String& operator=(const String& other) {
  505. if(!isOnStack())
  506. delete[] data.ptr;
  507. copy(other);
  508. return *this;
  509. }
  510. String& operator+=(const String& other);
  511. String operator+(const String& other) const { return String(*this) += other; }
  512. #ifdef DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  513. String(String&& other);
  514. String& operator=(String&& other);
  515. #endif // DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  516. bool isOnStack() const { return (buf[last] & 128) == 0; }
  517. char operator[](unsigned i) const { return const_cast<String*>(this)->operator[](i); } // NOLINT
  518. char& operator[](unsigned i) {
  519. if(isOnStack())
  520. return reinterpret_cast<char*>(buf)[i];
  521. return data.ptr[i];
  522. }
  523. const char* c_str() const { return const_cast<String*>(this)->c_str(); } // NOLINT
  524. char* c_str() {
  525. if(isOnStack())
  526. return reinterpret_cast<char*>(buf);
  527. return data.ptr;
  528. }
  529. unsigned size() const {
  530. if(isOnStack())
  531. return last - (unsigned(buf[last]) & 31); // using "last" would work only if "len" is 32
  532. return data.size;
  533. }
  534. unsigned capacity() const {
  535. if(isOnStack())
  536. return len;
  537. return data.capacity;
  538. }
  539. int compare(const char* other, bool no_case = false) const;
  540. int compare(const String& other, bool no_case = false) const;
  541. };
  542. // clang-format off
  543. inline bool operator==(const String& lhs, const String& rhs) { return lhs.compare(rhs) == 0; }
  544. inline bool operator!=(const String& lhs, const String& rhs) { return lhs.compare(rhs) != 0; }
  545. inline bool operator< (const String& lhs, const String& rhs) { return lhs.compare(rhs) < 0; }
  546. inline bool operator> (const String& lhs, const String& rhs) { return lhs.compare(rhs) > 0; }
  547. inline bool operator<=(const String& lhs, const String& rhs) { return (lhs != rhs) ? lhs.compare(rhs) < 0 : true; }
  548. inline bool operator>=(const String& lhs, const String& rhs) { return (lhs != rhs) ? lhs.compare(rhs) > 0 : true; }
  549. // clang-format on
  550. DOCTEST_INTERFACE std::ostream& operator<<(std::ostream& stream, const String& in);
  551. namespace detail
  552. {
  553. #ifndef DOCTEST_CONFIG_WITH_STATIC_ASSERT
  554. namespace static_assert_impl
  555. {
  556. template <bool>
  557. struct StaticAssertion;
  558. template <>
  559. struct StaticAssertion<true>
  560. {};
  561. template <int i>
  562. struct StaticAssertionTest
  563. {};
  564. } // namespace static_assert_impl
  565. #endif // DOCTEST_CONFIG_WITH_STATIC_ASSERT
  566. namespace traits
  567. {
  568. template <typename T>
  569. struct remove_const
  570. { typedef T type; };
  571. template <typename T>
  572. struct remove_const<const T>
  573. { typedef T type; };
  574. template <typename T>
  575. struct remove_volatile
  576. { typedef T type; };
  577. template <typename T>
  578. struct remove_volatile<volatile T>
  579. { typedef T type; };
  580. template <typename T>
  581. struct remove_cv
  582. { typedef typename remove_volatile<typename remove_const<T>::type>::type type; };
  583. template <typename T>
  584. struct is_pointer_helper
  585. { static const bool value = false; };
  586. template <typename T>
  587. struct is_pointer_helper<T*>
  588. // cppcheck-suppress unusedStructMember
  589. { static const bool value = true; };
  590. template <typename T>
  591. struct is_pointer
  592. // cppcheck-suppress unusedStructMember
  593. { static const bool value = is_pointer_helper<typename remove_cv<T>::type>::value; };
  594. template <bool CONDITION, typename TYPE = void>
  595. struct enable_if
  596. {};
  597. template <typename TYPE>
  598. struct enable_if<true, TYPE>
  599. { typedef TYPE type; };
  600. template <typename T>
  601. struct remove_reference
  602. { typedef T type; };
  603. template <typename T>
  604. struct remove_reference<T&>
  605. { typedef T type; };
  606. template <typename T, typename AT_1 = void>
  607. class is_constructible_impl
  608. {
  609. private:
  610. template <typename C_T, typename C_AT_1>
  611. static bool test(typename enable_if< //!OCLINT avoid private static members
  612. sizeof(C_T) ==
  613. sizeof(C_T(static_cast<C_AT_1>(
  614. *static_cast<typename remove_reference<C_AT_1>::type*>(
  615. 0))))>::type*);
  616. template <typename, typename>
  617. static int test(...); //!OCLINT avoid private static members
  618. public:
  619. static const bool value = sizeof(test<T, AT_1>(0)) == sizeof(bool);
  620. };
  621. template <typename T>
  622. class is_constructible_impl<T, void>
  623. {
  624. private:
  625. template <typename C_T>
  626. static C_T testFun(C_T); //!OCLINT avoid private static members
  627. template <typename C_T>
  628. static bool test(typename enable_if< //!OCLINT avoid private static members
  629. sizeof(C_T) == sizeof(testFun(C_T()))>::type*);
  630. template <typename>
  631. static int test(...); //!OCLINT avoid private static members
  632. public:
  633. static const bool value = sizeof(test<T>(0)) == sizeof(bool);
  634. };
  635. // is_constructible<> taken from here: http://stackoverflow.com/a/40443701/3162383
  636. // for GCC/Clang gives the same results as std::is_constructible<> - see here: https://wandbox.org/permlink/bNWr7Ii2fuz4Vf7A
  637. // modifications:
  638. // - reworked to support only 1 argument (mainly because of MSVC...)
  639. // - removed pointer support
  640. // MSVC support:
  641. // - for versions before 2012 read the CAUTION comment below
  642. // currently intended for use only in the Approx() helper for strong typedefs of double - see issue #62
  643. #ifndef _MSC_VER
  644. template <typename T, typename AT_1 = void>
  645. class is_constructible
  646. {
  647. public:
  648. static const bool value = is_pointer<typename remove_reference<T>::type>::value ?
  649. false :
  650. is_constructible_impl<T, AT_1>::value;
  651. };
  652. #elif defined(_MSC_VER) && (_MSC_VER >= 1700)
  653. template <typename T, typename AT_1>
  654. struct is_constructible
  655. { static const bool value = __is_constructible(T, AT_1); };
  656. #elif defined(_MSC_VER)
  657. // !!! USE WITH CAUTION !!!
  658. // will always return false - unable to implement this for versions of MSVC older than 2012 for now...
  659. template <typename T, typename AT_1>
  660. struct is_constructible
  661. { static const bool value = false; };
  662. #endif // _MSC_VER
  663. } // namespace traits
  664. template <typename T>
  665. struct deferred_false
  666. // cppcheck-suppress unusedStructMember
  667. { static const bool value = false; };
  668. // to silence the warning "-Wzero-as-null-pointer-constant" only for gcc 5 for the Approx template ctor - pragmas don't work for it...
  669. inline void* getNull() { return 0; }
  670. namespace has_insertion_operator_impl
  671. {
  672. typedef char no;
  673. typedef char yes[2];
  674. struct any_t
  675. {
  676. template <typename T>
  677. // cppcheck-suppress noExplicitConstructor
  678. any_t(const DOCTEST_REF_WRAP(T));
  679. };
  680. yes& testStreamable(std::ostream&);
  681. no testStreamable(no);
  682. no operator<<(const std::ostream&, const any_t&);
  683. template <typename T>
  684. struct has_insertion_operator
  685. {
  686. static std::ostream& s;
  687. static const DOCTEST_REF_WRAP(T) t;
  688. static const bool value = sizeof(testStreamable(s << t)) == sizeof(yes);
  689. };
  690. } // namespace has_insertion_operator_impl
  691. template <typename T>
  692. struct has_insertion_operator : has_insertion_operator_impl::has_insertion_operator<T>
  693. {};
  694. DOCTEST_INTERFACE void my_memcpy(void* dest, const void* src, unsigned num);
  695. DOCTEST_INTERFACE unsigned my_strlen(const char* in);
  696. DOCTEST_INTERFACE std::ostream* createStream();
  697. DOCTEST_INTERFACE String getStreamResult(std::ostream*);
  698. DOCTEST_INTERFACE void freeStream(std::ostream*);
  699. template <bool C>
  700. struct StringMakerBase
  701. {
  702. template <typename T>
  703. static String convert(const DOCTEST_REF_WRAP(T)) {
  704. return "{?}";
  705. }
  706. };
  707. template <>
  708. struct StringMakerBase<true>
  709. {
  710. template <typename T>
  711. static String convert(const DOCTEST_REF_WRAP(T) in) {
  712. std::ostream* stream = createStream();
  713. *stream << in;
  714. String result = getStreamResult(stream);
  715. freeStream(stream);
  716. return result;
  717. }
  718. };
  719. DOCTEST_INTERFACE String rawMemoryToString(const void* object, unsigned size);
  720. template <typename T>
  721. String rawMemoryToString(const DOCTEST_REF_WRAP(T) object) {
  722. return rawMemoryToString(&object, sizeof(object));
  723. }
  724. class NullType
  725. {};
  726. template <class T, class U>
  727. struct Typelist
  728. {
  729. typedef T Head;
  730. typedef U Tail;
  731. };
  732. // type of recursive function
  733. template <class TList, class Callable>
  734. struct ForEachType;
  735. // Recursion rule
  736. template <class Head, class Tail, class Callable>
  737. struct ForEachType<Typelist<Head, Tail>, Callable> : public ForEachType<Tail, Callable>
  738. {
  739. enum
  740. {
  741. value = 1 + ForEachType<Tail, Callable>::value
  742. };
  743. explicit ForEachType(Callable& callable)
  744. : ForEachType<Tail, Callable>(callable) {
  745. #if defined(_MSC_VER) && _MSC_VER <= 1900
  746. callable.operator()<value, Head>();
  747. #else // _MSC_VER
  748. callable.template operator()<value, Head>();
  749. #endif // _MSC_VER
  750. }
  751. };
  752. // Recursion end
  753. template <class Head, class Callable>
  754. struct ForEachType<Typelist<Head, NullType>, Callable>
  755. {
  756. public:
  757. enum
  758. {
  759. value = 0
  760. };
  761. explicit ForEachType(Callable& callable) {
  762. #if defined(_MSC_VER) && _MSC_VER <= 1900
  763. callable.operator()<value, Head>();
  764. #else // _MSC_VER
  765. callable.template operator()<value, Head>();
  766. #endif // _MSC_VER
  767. }
  768. };
  769. template <typename T>
  770. const char* type_to_string() {
  771. return "<>";
  772. }
  773. } // namespace detail
  774. template <typename T1 = detail::NullType, typename T2 = detail::NullType,
  775. typename T3 = detail::NullType, typename T4 = detail::NullType,
  776. typename T5 = detail::NullType, typename T6 = detail::NullType,
  777. typename T7 = detail::NullType, typename T8 = detail::NullType,
  778. typename T9 = detail::NullType, typename T10 = detail::NullType,
  779. typename T11 = detail::NullType, typename T12 = detail::NullType,
  780. typename T13 = detail::NullType, typename T14 = detail::NullType,
  781. typename T15 = detail::NullType, typename T16 = detail::NullType,
  782. typename T17 = detail::NullType, typename T18 = detail::NullType,
  783. typename T19 = detail::NullType, typename T20 = detail::NullType,
  784. typename T21 = detail::NullType, typename T22 = detail::NullType,
  785. typename T23 = detail::NullType, typename T24 = detail::NullType,
  786. typename T25 = detail::NullType, typename T26 = detail::NullType,
  787. typename T27 = detail::NullType, typename T28 = detail::NullType,
  788. typename T29 = detail::NullType, typename T30 = detail::NullType,
  789. typename T31 = detail::NullType, typename T32 = detail::NullType,
  790. typename T33 = detail::NullType, typename T34 = detail::NullType,
  791. typename T35 = detail::NullType, typename T36 = detail::NullType,
  792. typename T37 = detail::NullType, typename T38 = detail::NullType,
  793. typename T39 = detail::NullType, typename T40 = detail::NullType,
  794. typename T41 = detail::NullType, typename T42 = detail::NullType,
  795. typename T43 = detail::NullType, typename T44 = detail::NullType,
  796. typename T45 = detail::NullType, typename T46 = detail::NullType,
  797. typename T47 = detail::NullType, typename T48 = detail::NullType,
  798. typename T49 = detail::NullType, typename T50 = detail::NullType,
  799. typename T51 = detail::NullType, typename T52 = detail::NullType,
  800. typename T53 = detail::NullType, typename T54 = detail::NullType,
  801. typename T55 = detail::NullType, typename T56 = detail::NullType,
  802. typename T57 = detail::NullType, typename T58 = detail::NullType,
  803. typename T59 = detail::NullType, typename T60 = detail::NullType>
  804. struct Types
  805. {
  806. private:
  807. typedef typename Types<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17,
  808. T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30, T31,
  809. T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44, T45,
  810. T46, T47, T48, T49, T50, T51, T52, T53, T54, T55, T56, T57, T58, T59,
  811. T60>::Result TailResult;
  812. public:
  813. typedef detail::Typelist<T1, TailResult> Result;
  814. };
  815. template <>
  816. struct Types<>
  817. { typedef detail::NullType Result; };
  818. template <typename T>
  819. struct StringMaker : detail::StringMakerBase<detail::has_insertion_operator<T>::value>
  820. {};
  821. template <typename T>
  822. struct StringMaker<T*>
  823. {
  824. template <typename U>
  825. static String convert(U* p) {
  826. if(p)
  827. return detail::rawMemoryToString(p);
  828. return "NULL";
  829. }
  830. };
  831. template <typename R, typename C>
  832. struct StringMaker<R C::*>
  833. {
  834. static String convert(R C::*p) {
  835. if(p)
  836. return detail::rawMemoryToString(p);
  837. return "NULL";
  838. }
  839. };
  840. template <typename T>
  841. String toString(const DOCTEST_REF_WRAP(T) value) {
  842. return StringMaker<T>::convert(value);
  843. }
  844. #ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  845. DOCTEST_INTERFACE String toString(char* in);
  846. DOCTEST_INTERFACE String toString(const char* in);
  847. #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  848. DOCTEST_INTERFACE String toString(bool in);
  849. DOCTEST_INTERFACE String toString(float in);
  850. DOCTEST_INTERFACE String toString(double in);
  851. DOCTEST_INTERFACE String toString(double long in);
  852. DOCTEST_INTERFACE String toString(char in);
  853. DOCTEST_INTERFACE String toString(char signed in);
  854. DOCTEST_INTERFACE String toString(char unsigned in);
  855. DOCTEST_INTERFACE String toString(int short in);
  856. DOCTEST_INTERFACE String toString(int short unsigned in);
  857. DOCTEST_INTERFACE String toString(int in);
  858. DOCTEST_INTERFACE String toString(int unsigned in);
  859. DOCTEST_INTERFACE String toString(int long in);
  860. DOCTEST_INTERFACE String toString(int long unsigned in);
  861. #ifdef DOCTEST_CONFIG_WITH_LONG_LONG
  862. DOCTEST_INTERFACE String toString(int long long in);
  863. DOCTEST_INTERFACE String toString(int long long unsigned in);
  864. #endif // DOCTEST_CONFIG_WITH_LONG_LONG
  865. #ifdef DOCTEST_CONFIG_WITH_NULLPTR
  866. DOCTEST_INTERFACE String toString(std::nullptr_t in);
  867. #endif // DOCTEST_CONFIG_WITH_NULLPTR
  868. class DOCTEST_INTERFACE Approx
  869. {
  870. public:
  871. explicit Approx(double value);
  872. Approx operator()(double value) const {
  873. Approx approx(value);
  874. approx.epsilon(m_epsilon);
  875. approx.scale(m_scale);
  876. return approx;
  877. }
  878. template <typename T>
  879. explicit Approx(const T& value,
  880. typename detail::traits::enable_if<
  881. detail::traits::is_constructible<double, T>::value>::type* =
  882. static_cast<T*>(detail::getNull())) {
  883. *this = Approx(static_cast<double>(value));
  884. }
  885. // clang-format off
  886. // overloads for double - the first one is necessary as it is in the implementation part of doctest
  887. // as for the others - keeping them for potentially faster compile times
  888. DOCTEST_INTERFACE friend bool operator==(double lhs, Approx const& rhs);
  889. friend bool operator==(Approx const& lhs, double rhs) { return operator==(rhs, lhs); }
  890. friend bool operator!=(double lhs, Approx const& rhs) { return !operator==(lhs, rhs); }
  891. friend bool operator!=(Approx const& lhs, double rhs) { return !operator==(rhs, lhs); }
  892. friend bool operator<=(double lhs, Approx const& rhs) { return lhs < rhs.m_value || lhs == rhs; }
  893. friend bool operator<=(Approx const& lhs, double rhs) { return lhs.m_value < rhs || lhs == rhs; }
  894. friend bool operator>=(double lhs, Approx const& rhs) { return lhs > rhs.m_value || lhs == rhs; }
  895. friend bool operator>=(Approx const& lhs, double rhs) { return lhs.m_value > rhs || lhs == rhs; }
  896. friend bool operator< (double lhs, Approx const& rhs) { return lhs < rhs.m_value && lhs != rhs; }
  897. friend bool operator< (Approx const& lhs, double rhs) { return lhs.m_value < rhs && lhs != rhs; }
  898. friend bool operator> (double lhs, Approx const& rhs) { return lhs > rhs.m_value && lhs != rhs; }
  899. friend bool operator> (Approx const& lhs, double rhs) { return lhs.m_value > rhs && lhs != rhs; }
  900. #define DOCTEST_APPROX_PREFIX \
  901. template <typename T> friend typename detail::traits::enable_if<detail::traits::is_constructible<double, T>::value, bool>::type
  902. DOCTEST_APPROX_PREFIX operator==(const T& lhs, const Approx& rhs) { return operator==(double(lhs), rhs); }
  903. DOCTEST_APPROX_PREFIX operator==(const Approx& lhs, const T& rhs) { return operator==(rhs, lhs); }
  904. DOCTEST_APPROX_PREFIX operator!=(const T& lhs, const Approx& rhs) { return !operator==(lhs, rhs); }
  905. DOCTEST_APPROX_PREFIX operator!=(const Approx& lhs, const T& rhs) { return !operator==(rhs, lhs); }
  906. DOCTEST_APPROX_PREFIX operator<=(const T& lhs, const Approx& rhs) { return double(lhs) < rhs.m_value || lhs == rhs; }
  907. DOCTEST_APPROX_PREFIX operator<=(const Approx& lhs, const T& rhs) { return lhs.m_value < double(rhs) || lhs == rhs; }
  908. DOCTEST_APPROX_PREFIX operator>=(const T& lhs, const Approx& rhs) { return double(lhs) > rhs.m_value || lhs == rhs; }
  909. DOCTEST_APPROX_PREFIX operator>=(const Approx& lhs, const T& rhs) { return lhs.m_value > double(rhs) || lhs == rhs; }
  910. DOCTEST_APPROX_PREFIX operator< (const T& lhs, const Approx& rhs) { return double(lhs) < rhs.m_value && lhs != rhs; }
  911. DOCTEST_APPROX_PREFIX operator< (const Approx& lhs, const T& rhs) { return lhs.m_value < double(rhs) && lhs != rhs; }
  912. DOCTEST_APPROX_PREFIX operator> (const T& lhs, const Approx& rhs) { return double(lhs) > rhs.m_value && lhs != rhs; }
  913. DOCTEST_APPROX_PREFIX operator> (const Approx& lhs, const T& rhs) { return lhs.m_value > double(rhs) && lhs != rhs; }
  914. #undef DOCTEST_APPROX_PREFIX
  915. // clang-format on
  916. Approx& epsilon(double newEpsilon) {
  917. m_epsilon = (newEpsilon);
  918. return *this;
  919. }
  920. template <typename T>
  921. typename detail::traits::enable_if<detail::traits::is_constructible<double, T>::value,
  922. Approx&>::type
  923. epsilon(const T& newEpsilon) {
  924. m_epsilon = static_cast<double>(newEpsilon);
  925. return *this;
  926. }
  927. Approx& scale(double newScale) {
  928. m_scale = (newScale);
  929. return *this;
  930. }
  931. template <typename T>
  932. typename detail::traits::enable_if<detail::traits::is_constructible<double, T>::value,
  933. Approx&>::type
  934. scale(const T& newScale) {
  935. m_scale = static_cast<double>(newScale);
  936. return *this;
  937. }
  938. String toString() const;
  939. private:
  940. double m_epsilon;
  941. double m_scale;
  942. double m_value;
  943. };
  944. template <>
  945. inline String toString<Approx>(const DOCTEST_REF_WRAP(Approx) value) {
  946. return value.toString();
  947. }
  948. #if !defined(DOCTEST_CONFIG_DISABLE)
  949. namespace detail
  950. {
  951. // the function type this library works with
  952. typedef void (*funcType)();
  953. namespace assertType
  954. {
  955. enum Enum
  956. {
  957. // macro traits
  958. is_warn = 1,
  959. is_check = 2,
  960. is_require = 4,
  961. is_throws = 8,
  962. is_throws_as = 16,
  963. is_nothrow = 32,
  964. is_fast = 64, // not checked anywhere - used just to distinguish the types
  965. is_false = 128,
  966. is_unary = 256,
  967. is_eq = 512,
  968. is_ne = 1024,
  969. is_lt = 2048,
  970. is_gt = 4096,
  971. is_ge = 8192,
  972. is_le = 16384,
  973. // macro types
  974. DT_WARN = is_warn,
  975. DT_CHECK = is_check,
  976. DT_REQUIRE = is_require,
  977. DT_WARN_FALSE = is_false | is_warn,
  978. DT_CHECK_FALSE = is_false | is_check,
  979. DT_REQUIRE_FALSE = is_false | is_require,
  980. DT_WARN_THROWS = is_throws | is_warn,
  981. DT_CHECK_THROWS = is_throws | is_check,
  982. DT_REQUIRE_THROWS = is_throws | is_require,
  983. DT_WARN_THROWS_AS = is_throws_as | is_warn,
  984. DT_CHECK_THROWS_AS = is_throws_as | is_check,
  985. DT_REQUIRE_THROWS_AS = is_throws_as | is_require,
  986. DT_WARN_NOTHROW = is_nothrow | is_warn,
  987. DT_CHECK_NOTHROW = is_nothrow | is_check,
  988. DT_REQUIRE_NOTHROW = is_nothrow | is_require,
  989. DT_WARN_EQ = is_eq | is_warn,
  990. DT_CHECK_EQ = is_eq | is_check,
  991. DT_REQUIRE_EQ = is_eq | is_require,
  992. DT_WARN_NE = is_ne | is_warn,
  993. DT_CHECK_NE = is_ne | is_check,
  994. DT_REQUIRE_NE = is_ne | is_require,
  995. DT_WARN_GT = is_gt | is_warn,
  996. DT_CHECK_GT = is_gt | is_check,
  997. DT_REQUIRE_GT = is_gt | is_require,
  998. DT_WARN_LT = is_lt | is_warn,
  999. DT_CHECK_LT = is_lt | is_check,
  1000. DT_REQUIRE_LT = is_lt | is_require,
  1001. DT_WARN_GE = is_ge | is_warn,
  1002. DT_CHECK_GE = is_ge | is_check,
  1003. DT_REQUIRE_GE = is_ge | is_require,
  1004. DT_WARN_LE = is_le | is_warn,
  1005. DT_CHECK_LE = is_le | is_check,
  1006. DT_REQUIRE_LE = is_le | is_require,
  1007. DT_WARN_UNARY = is_unary | is_warn,
  1008. DT_CHECK_UNARY = is_unary | is_check,
  1009. DT_REQUIRE_UNARY = is_unary | is_require,
  1010. DT_WARN_UNARY_FALSE = is_false | is_unary | is_warn,
  1011. DT_CHECK_UNARY_FALSE = is_false | is_unary | is_check,
  1012. DT_REQUIRE_UNARY_FALSE = is_false | is_unary | is_require,
  1013. DT_FAST_WARN_EQ = is_fast | is_eq | is_warn,
  1014. DT_FAST_CHECK_EQ = is_fast | is_eq | is_check,
  1015. DT_FAST_REQUIRE_EQ = is_fast | is_eq | is_require,
  1016. DT_FAST_WARN_NE = is_fast | is_ne | is_warn,
  1017. DT_FAST_CHECK_NE = is_fast | is_ne | is_check,
  1018. DT_FAST_REQUIRE_NE = is_fast | is_ne | is_require,
  1019. DT_FAST_WARN_GT = is_fast | is_gt | is_warn,
  1020. DT_FAST_CHECK_GT = is_fast | is_gt | is_check,
  1021. DT_FAST_REQUIRE_GT = is_fast | is_gt | is_require,
  1022. DT_FAST_WARN_LT = is_fast | is_lt | is_warn,
  1023. DT_FAST_CHECK_LT = is_fast | is_lt | is_check,
  1024. DT_FAST_REQUIRE_LT = is_fast | is_lt | is_require,
  1025. DT_FAST_WARN_GE = is_fast | is_ge | is_warn,
  1026. DT_FAST_CHECK_GE = is_fast | is_ge | is_check,
  1027. DT_FAST_REQUIRE_GE = is_fast | is_ge | is_require,
  1028. DT_FAST_WARN_LE = is_fast | is_le | is_warn,
  1029. DT_FAST_CHECK_LE = is_fast | is_le | is_check,
  1030. DT_FAST_REQUIRE_LE = is_fast | is_le | is_require,
  1031. DT_FAST_WARN_UNARY = is_fast | is_unary | is_warn,
  1032. DT_FAST_CHECK_UNARY = is_fast | is_unary | is_check,
  1033. DT_FAST_REQUIRE_UNARY = is_fast | is_unary | is_require,
  1034. DT_FAST_WARN_UNARY_FALSE = is_fast | is_false | is_unary | is_warn,
  1035. DT_FAST_CHECK_UNARY_FALSE = is_fast | is_false | is_unary | is_check,
  1036. DT_FAST_REQUIRE_UNARY_FALSE = is_fast | is_false | is_unary | is_require
  1037. };
  1038. } // namespace assertType
  1039. DOCTEST_INTERFACE const char* getAssertString(assertType::Enum val);
  1040. // clang-format off
  1041. template<class T> struct decay_array { typedef T type; };
  1042. template<class T, unsigned N> struct decay_array<T[N]> { typedef T* type; };
  1043. template<class T> struct decay_array<T[]> { typedef T* type; };
  1044. template<class T> struct not_char_pointer { enum { value = 1 }; };
  1045. template<> struct not_char_pointer<char*> { enum { value = 0 }; };
  1046. template<> struct not_char_pointer<const char*> { enum { value = 0 }; };
  1047. template<class T> struct can_use_op : not_char_pointer<typename decay_array<T>::type> {};
  1048. // clang-format on
  1049. struct TestFailureException
  1050. {};
  1051. DOCTEST_INTERFACE bool checkIfShouldThrow(assertType::Enum assert_type);
  1052. DOCTEST_INTERFACE void fastAssertThrowIfFlagSet(int flags);
  1053. DOCTEST_INTERFACE void throwException();
  1054. struct TestAccessibleContextState
  1055. {
  1056. bool no_throw; // to skip exceptions-related assertion macros
  1057. bool success; // include successful assertions in output
  1058. };
  1059. struct ContextState;
  1060. DOCTEST_INTERFACE TestAccessibleContextState* getTestsContextState();
  1061. struct DOCTEST_INTERFACE SubcaseSignature
  1062. {
  1063. const char* m_name;
  1064. const char* m_file;
  1065. int m_line;
  1066. SubcaseSignature(const char* name, const char* file, int line)
  1067. : m_name(name)
  1068. , m_file(file)
  1069. , m_line(line) {}
  1070. bool operator<(const SubcaseSignature& other) const;
  1071. };
  1072. // cppcheck-suppress copyCtorAndEqOperator
  1073. struct DOCTEST_INTERFACE Subcase
  1074. {
  1075. SubcaseSignature m_signature;
  1076. bool m_entered;
  1077. Subcase(const char* name, const char* file, int line);
  1078. Subcase(const Subcase& other);
  1079. ~Subcase();
  1080. operator bool() const { return m_entered; }
  1081. };
  1082. template <typename L, typename R>
  1083. String stringifyBinaryExpr(const DOCTEST_REF_WRAP(L) lhs, const char* op,
  1084. const DOCTEST_REF_WRAP(R) rhs) {
  1085. return toString(lhs) + op + toString(rhs);
  1086. }
  1087. struct DOCTEST_INTERFACE Result
  1088. {
  1089. bool m_passed;
  1090. String m_decomposition;
  1091. ~Result();
  1092. DOCTEST_NOINLINE Result(bool passed = false, const String& decomposition = String())
  1093. : m_passed(passed)
  1094. , m_decomposition(decomposition) {}
  1095. DOCTEST_NOINLINE Result(const Result& other)
  1096. : m_passed(other.m_passed)
  1097. , m_decomposition(other.m_decomposition) {}
  1098. Result& operator=(const Result& other);
  1099. operator bool() { return !m_passed; }
  1100. // clang-format off
  1101. // forbidding some expressions based on this table: http://en.cppreference.com/w/cpp/language/operator_precedence
  1102. template <typename R> Result& operator& (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1103. template <typename R> Result& operator^ (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1104. template <typename R> Result& operator| (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1105. template <typename R> Result& operator&& (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1106. template <typename R> Result& operator|| (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1107. template <typename R> Result& operator== (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1108. template <typename R> Result& operator!= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1109. template <typename R> Result& operator< (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1110. template <typename R> Result& operator> (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1111. template <typename R> Result& operator<= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1112. template <typename R> Result& operator>= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1113. template <typename R> Result& operator= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1114. template <typename R> Result& operator+= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1115. template <typename R> Result& operator-= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1116. template <typename R> Result& operator*= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1117. template <typename R> Result& operator/= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1118. template <typename R> Result& operator%= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1119. template <typename R> Result& operator<<=(const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1120. template <typename R> Result& operator>>=(const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1121. template <typename R> Result& operator&= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1122. template <typename R> Result& operator^= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1123. template <typename R> Result& operator|= (const R&) { DOCTEST_STATIC_ASSERT(deferred_false<R>::value, Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); return *this; }
  1124. // clang-format on
  1125. };
  1126. #ifndef DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION
  1127. #if defined(__clang__)
  1128. #pragma clang diagnostic push
  1129. #pragma clang diagnostic ignored "-Wsign-conversion"
  1130. #pragma clang diagnostic ignored "-Wsign-compare"
  1131. //#pragma clang diagnostic ignored "-Wdouble-promotion"
  1132. //#pragma clang diagnostic ignored "-Wconversion"
  1133. //#pragma clang diagnostic ignored "-Wfloat-equal"
  1134. #endif // __clang__
  1135. #if defined(__GNUC__) && !defined(__clang__)
  1136. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)
  1137. #pragma GCC diagnostic push
  1138. #endif // > gcc 4.6
  1139. #pragma GCC diagnostic ignored "-Wsign-conversion"
  1140. #pragma GCC diagnostic ignored "-Wsign-compare"
  1141. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5)
  1142. //#pragma GCC diagnostic ignored "-Wdouble-promotion"
  1143. #endif // > gcc 4.5
  1144. //#pragma GCC diagnostic ignored "-Wconversion"
  1145. //#pragma GCC diagnostic ignored "-Wfloat-equal"
  1146. #endif // __GNUC__
  1147. #ifdef _MSC_VER
  1148. #pragma warning(push)
  1149. // http://stackoverflow.com/questions/39479163 what's the difference between C4018 and C4389
  1150. #pragma warning(disable : 4389) // 'operator' : signed/unsigned mismatch
  1151. #pragma warning(disable : 4018) // 'expression' : signed/unsigned mismatch
  1152. //#pragma warning(disable : 4805) // 'operation' : unsafe mix of type 'type' and type 'type' in operation
  1153. #endif // _MSC_VER
  1154. #endif // DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION
  1155. // clang-format off
  1156. #ifndef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  1157. #define DOCTEST_COMPARISON_RETURN_TYPE bool
  1158. #else // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  1159. #define DOCTEST_COMPARISON_RETURN_TYPE typename traits::enable_if<can_use_op<L>::value || can_use_op<R>::value, bool>::type
  1160. inline bool eq(const char* lhs, const char* rhs) { return String(lhs) == String(rhs); }
  1161. inline bool ne(const char* lhs, const char* rhs) { return String(lhs) != String(rhs); }
  1162. inline bool lt(const char* lhs, const char* rhs) { return String(lhs) < String(rhs); }
  1163. inline bool gt(const char* lhs, const char* rhs) { return String(lhs) > String(rhs); }
  1164. inline bool le(const char* lhs, const char* rhs) { return String(lhs) <= String(rhs); }
  1165. inline bool ge(const char* lhs, const char* rhs) { return String(lhs) >= String(rhs); }
  1166. #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  1167. template <typename L, typename R> DOCTEST_COMPARISON_RETURN_TYPE eq(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) { return lhs == rhs; }
  1168. template <typename L, typename R> DOCTEST_COMPARISON_RETURN_TYPE ne(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) { return lhs != rhs; }
  1169. template <typename L, typename R> DOCTEST_COMPARISON_RETURN_TYPE lt(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) { return lhs < rhs; }
  1170. template <typename L, typename R> DOCTEST_COMPARISON_RETURN_TYPE gt(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) { return lhs > rhs; }
  1171. template <typename L, typename R> DOCTEST_COMPARISON_RETURN_TYPE le(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) { return lhs <= rhs; }
  1172. template <typename L, typename R> DOCTEST_COMPARISON_RETURN_TYPE ge(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) { return lhs >= rhs; }
  1173. // clang-format on
  1174. #ifndef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  1175. #define DOCTEST_CMP_EQ(l, r) l == r
  1176. #define DOCTEST_CMP_NE(l, r) l != r
  1177. #define DOCTEST_CMP_GT(l, r) l > r
  1178. #define DOCTEST_CMP_LT(l, r) l < r
  1179. #define DOCTEST_CMP_GE(l, r) l >= r
  1180. #define DOCTEST_CMP_LE(l, r) l <= r
  1181. #else // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  1182. #define DOCTEST_CMP_EQ(l, r) eq(l, r)
  1183. #define DOCTEST_CMP_NE(l, r) ne(l, r)
  1184. #define DOCTEST_CMP_GT(l, r) gt(l, r)
  1185. #define DOCTEST_CMP_LT(l, r) lt(l, r)
  1186. #define DOCTEST_CMP_GE(l, r) ge(l, r)
  1187. #define DOCTEST_CMP_LE(l, r) le(l, r)
  1188. #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  1189. #define DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(op, op_str, op_macro) \
  1190. template <typename R> \
  1191. DOCTEST_NOINLINE Result operator op(const DOCTEST_REF_WRAP(R) rhs) { \
  1192. bool res = op_macro(lhs, rhs); \
  1193. if(m_assert_type & assertType::is_false) \
  1194. res = !res; \
  1195. if(!res || doctest::detail::getTestsContextState()->success) \
  1196. return Result(res, stringifyBinaryExpr(lhs, op_str, rhs)); \
  1197. return Result(res); \
  1198. }
  1199. #define DOCTEST_FORBIT_EXPRESSION(op) \
  1200. template <typename R> \
  1201. Expression_lhs& operator op(const R&) { \
  1202. DOCTEST_STATIC_ASSERT(deferred_false<R>::value, \
  1203. Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison); \
  1204. return *this; \
  1205. }
  1206. template <typename L>
  1207. // cppcheck-suppress copyCtorAndEqOperator
  1208. struct Expression_lhs
  1209. {
  1210. L lhs;
  1211. assertType::Enum m_assert_type;
  1212. explicit Expression_lhs(L in, assertType::Enum assert_type)
  1213. : lhs(in)
  1214. , m_assert_type(assert_type) {}
  1215. Expression_lhs(const Expression_lhs& other)
  1216. : lhs(other.lhs) {}
  1217. DOCTEST_NOINLINE operator Result() {
  1218. bool res = !!lhs;
  1219. if(m_assert_type & assertType::is_false) //!OCLINT bitwise operator in conditional
  1220. res = !res;
  1221. if(!res || getTestsContextState()->success)
  1222. return Result(res, toString(lhs));
  1223. return Result(res);
  1224. }
  1225. // clang-format off
  1226. DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(==, " == ", DOCTEST_CMP_EQ) //!OCLINT bitwise operator in conditional
  1227. DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(!=, " != ", DOCTEST_CMP_NE) //!OCLINT bitwise operator in conditional
  1228. DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(>, " > ", DOCTEST_CMP_GT) //!OCLINT bitwise operator in conditional
  1229. DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(<, " < ", DOCTEST_CMP_LT) //!OCLINT bitwise operator in conditional
  1230. DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(>=, " >= ", DOCTEST_CMP_GE) //!OCLINT bitwise operator in conditional
  1231. DOCTEST_DO_BINARY_EXPRESSION_COMPARISON(<=, " <= ", DOCTEST_CMP_LE) //!OCLINT bitwise operator in conditional
  1232. // clang-format on
  1233. // forbidding some expressions based on this table: http://en.cppreference.com/w/cpp/language/operator_precedence
  1234. DOCTEST_FORBIT_EXPRESSION(&)
  1235. DOCTEST_FORBIT_EXPRESSION (^)
  1236. DOCTEST_FORBIT_EXPRESSION(|)
  1237. DOCTEST_FORBIT_EXPRESSION(&&)
  1238. DOCTEST_FORBIT_EXPRESSION(||)
  1239. DOCTEST_FORBIT_EXPRESSION(=)
  1240. DOCTEST_FORBIT_EXPRESSION(+=)
  1241. DOCTEST_FORBIT_EXPRESSION(-=)
  1242. DOCTEST_FORBIT_EXPRESSION(*=)
  1243. DOCTEST_FORBIT_EXPRESSION(/=)
  1244. DOCTEST_FORBIT_EXPRESSION(%=)
  1245. DOCTEST_FORBIT_EXPRESSION(<<=)
  1246. DOCTEST_FORBIT_EXPRESSION(>>=)
  1247. DOCTEST_FORBIT_EXPRESSION(&=)
  1248. DOCTEST_FORBIT_EXPRESSION(^=)
  1249. DOCTEST_FORBIT_EXPRESSION(|=)
  1250. // these 2 are unfortunate because they should be allowed - they have higher precedence over the comparisons, but the
  1251. // ExpressionDecomposer class uses the left shift operator to capture the left operand of the binary expression...
  1252. DOCTEST_FORBIT_EXPRESSION(<<)
  1253. DOCTEST_FORBIT_EXPRESSION(>>)
  1254. };
  1255. #ifndef DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION
  1256. #if defined(__clang__)
  1257. #pragma clang diagnostic pop
  1258. #endif // __clang__
  1259. #if defined(__GNUC__) && !defined(__clang__)
  1260. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)
  1261. #pragma GCC diagnostic pop
  1262. #endif // > gcc 4.6
  1263. #endif // __GNUC__
  1264. #ifdef _MSC_VER
  1265. #pragma warning(pop)
  1266. #endif // _MSC_VER
  1267. #endif // DOCTEST_CONFIG_NO_COMPARISON_WARNING_SUPPRESSION
  1268. struct ExpressionDecomposer
  1269. {
  1270. assertType::Enum m_assert_type;
  1271. ExpressionDecomposer(assertType::Enum assert_type)
  1272. : m_assert_type(assert_type) {}
  1273. // The right operator for capturing expressions is "<=" instead of "<<" (based on the operator precedence table)
  1274. // but then there will be warnings from GCC about "-Wparentheses" and since "_Pragma()" is problematic this will stay for now...
  1275. // https://github.com/philsquared/Catch/issues/870
  1276. // https://github.com/philsquared/Catch/issues/565
  1277. template <typename L>
  1278. Expression_lhs<const DOCTEST_REF_WRAP(L)> operator<<(const DOCTEST_REF_WRAP(L) operand) {
  1279. return Expression_lhs<const DOCTEST_REF_WRAP(L)>(operand, m_assert_type);
  1280. }
  1281. };
  1282. struct DOCTEST_INTERFACE TestCase
  1283. {
  1284. // not used for determining uniqueness
  1285. funcType m_test; // a function pointer to the test case
  1286. String m_full_name; // contains the name (only for templated test cases!) + the template type
  1287. const char* m_name; // name of the test case
  1288. const char* m_type; // for templated test cases - gets appended to the real name
  1289. const char* m_test_suite; // the test suite in which the test was added
  1290. const char* m_description;
  1291. bool m_skip;
  1292. bool m_may_fail;
  1293. bool m_should_fail;
  1294. int m_expected_failures;
  1295. double m_timeout;
  1296. // fields by which uniqueness of test cases shall be determined
  1297. const char* m_file; // the file in which the test was registered
  1298. unsigned m_line; // the line where the test was registered
  1299. int m_template_id; // an ID used to distinguish between the different versions of a templated test case
  1300. TestCase(funcType test, const char* file, unsigned line, const TestSuite& test_suite,
  1301. const char* type = "", int template_id = -1);
  1302. // for gcc 4.7
  1303. DOCTEST_NOINLINE ~TestCase() {}
  1304. TestCase& operator*(const char* in);
  1305. template <typename T>
  1306. TestCase& operator*(const T& in) {
  1307. in.fill(*this);
  1308. return *this;
  1309. }
  1310. TestCase(const TestCase& other) { *this = other; }
  1311. TestCase& operator=(const TestCase& other);
  1312. bool operator<(const TestCase& other) const;
  1313. };
  1314. // forward declarations of functions used by the macros
  1315. DOCTEST_INTERFACE int regTest(const TestCase& tc);
  1316. DOCTEST_INTERFACE int setTestSuite(const TestSuite& ts);
  1317. DOCTEST_INTERFACE void addFailedAssert(assertType::Enum assert_type);
  1318. DOCTEST_INTERFACE void logTestStart(const TestCase& tc);
  1319. DOCTEST_INTERFACE void logTestEnd();
  1320. DOCTEST_INTERFACE void logTestException(const String& what, bool crash = false);
  1321. DOCTEST_INTERFACE void logAssert(bool passed, const char* decomposition, bool threw,
  1322. const String& exception, const char* expr,
  1323. assertType::Enum assert_type, const char* file, int line);
  1324. DOCTEST_INTERFACE void logAssertThrows(bool threw, const char* expr,
  1325. assertType::Enum assert_type, const char* file,
  1326. int line);
  1327. DOCTEST_INTERFACE void logAssertThrowsAs(bool threw, bool threw_as, const char* as,
  1328. const String& exception, const char* expr,
  1329. assertType::Enum assert_type, const char* file,
  1330. int line);
  1331. DOCTEST_INTERFACE void logAssertNothrow(bool threw, const String& exception, const char* expr,
  1332. assertType::Enum assert_type, const char* file,
  1333. int line);
  1334. DOCTEST_INTERFACE bool isDebuggerActive();
  1335. DOCTEST_INTERFACE void writeToDebugConsole(const String&);
  1336. namespace binaryAssertComparison
  1337. {
  1338. enum Enum
  1339. {
  1340. eq = 0,
  1341. ne,
  1342. gt,
  1343. lt,
  1344. ge,
  1345. le
  1346. };
  1347. } // namespace binaryAssertComparison
  1348. // clang-format off
  1349. template <int, class L, class R> struct RelationalComparator { bool operator()(const DOCTEST_REF_WRAP(L), const DOCTEST_REF_WRAP(R) ) const { return false; } };
  1350. template <class L, class R> struct RelationalComparator<0, L, R> { bool operator()(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) const { return eq(lhs, rhs); } };
  1351. template <class L, class R> struct RelationalComparator<1, L, R> { bool operator()(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) const { return ne(lhs, rhs); } };
  1352. template <class L, class R> struct RelationalComparator<2, L, R> { bool operator()(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) const { return gt(lhs, rhs); } };
  1353. template <class L, class R> struct RelationalComparator<3, L, R> { bool operator()(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) const { return lt(lhs, rhs); } };
  1354. template <class L, class R> struct RelationalComparator<4, L, R> { bool operator()(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) const { return ge(lhs, rhs); } };
  1355. template <class L, class R> struct RelationalComparator<5, L, R> { bool operator()(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) const { return le(lhs, rhs); } };
  1356. // clang-format on
  1357. struct DOCTEST_INTERFACE ResultBuilder
  1358. {
  1359. assertType::Enum m_assert_type;
  1360. const char* m_file;
  1361. int m_line;
  1362. const char* m_expr;
  1363. const char* m_exception_type;
  1364. Result m_result;
  1365. bool m_threw;
  1366. bool m_threw_as;
  1367. bool m_failed;
  1368. String m_exception;
  1369. ResultBuilder(assertType::Enum assert_type, const char* file, int line, const char* expr,
  1370. const char* exception_type = "");
  1371. ~ResultBuilder();
  1372. void setResult(const Result& res) { m_result = res; }
  1373. template <int comparison, typename L, typename R>
  1374. DOCTEST_NOINLINE void binary_assert(const DOCTEST_REF_WRAP(L) lhs,
  1375. const DOCTEST_REF_WRAP(R) rhs) {
  1376. m_result.m_passed = RelationalComparator<comparison, L, R>()(lhs, rhs);
  1377. if(!m_result.m_passed || getTestsContextState()->success)
  1378. m_result.m_decomposition = stringifyBinaryExpr(lhs, ", ", rhs);
  1379. }
  1380. template <typename L>
  1381. DOCTEST_NOINLINE void unary_assert(const DOCTEST_REF_WRAP(L) val) {
  1382. m_result.m_passed = !!val;
  1383. if(m_assert_type & assertType::is_false) //!OCLINT bitwise operator in conditional
  1384. m_result.m_passed = !m_result.m_passed;
  1385. if(!m_result.m_passed || getTestsContextState()->success)
  1386. m_result.m_decomposition = toString(val);
  1387. }
  1388. void unexpectedExceptionOccurred();
  1389. bool log();
  1390. void react() const;
  1391. };
  1392. namespace assertAction
  1393. {
  1394. enum Enum
  1395. {
  1396. nothing = 0,
  1397. dbgbreak = 1,
  1398. shouldthrow = 2
  1399. };
  1400. } // namespace assertAction
  1401. template <int comparison, typename L, typename R>
  1402. DOCTEST_NOINLINE int fast_binary_assert(assertType::Enum assert_type, const char* file,
  1403. int line, const char* expr,
  1404. const DOCTEST_REF_WRAP(L) lhs,
  1405. const DOCTEST_REF_WRAP(R) rhs) {
  1406. ResultBuilder rb(assert_type, file, line, expr);
  1407. rb.m_result.m_passed = RelationalComparator<comparison, L, R>()(lhs, rhs);
  1408. if(!rb.m_result.m_passed || getTestsContextState()->success)
  1409. rb.m_result.m_decomposition = stringifyBinaryExpr(lhs, ", ", rhs);
  1410. int res = 0;
  1411. if(rb.log())
  1412. res |= assertAction::dbgbreak;
  1413. if(rb.m_failed && checkIfShouldThrow(assert_type))
  1414. res |= assertAction::shouldthrow;
  1415. #ifdef DOCTEST_CONFIG_SUPER_FAST_ASSERTS
  1416. // #########################################################################################
  1417. // IF THE DEBUGGER BREAKS HERE - GO 1 LEVEL UP IN THE CALLSTACK TO SEE THE FAILING ASSERTION
  1418. // THIS IS THE EFFECT OF HAVING 'DOCTEST_CONFIG_SUPER_FAST_ASSERTS' DEFINED
  1419. // #########################################################################################
  1420. if(res & assertAction::dbgbreak)
  1421. DOCTEST_BREAK_INTO_DEBUGGER();
  1422. fastAssertThrowIfFlagSet(res);
  1423. #endif // DOCTEST_CONFIG_SUPER_FAST_ASSERTS
  1424. return res;
  1425. }
  1426. template <typename L>
  1427. DOCTEST_NOINLINE int fast_unary_assert(assertType::Enum assert_type, const char* file, int line,
  1428. const char* val_str, const DOCTEST_REF_WRAP(L) val) {
  1429. ResultBuilder rb(assert_type, file, line, val_str);
  1430. rb.m_result.m_passed = !!val;
  1431. if(assert_type & assertType::is_false) //!OCLINT bitwise operator in conditional
  1432. rb.m_result.m_passed = !rb.m_result.m_passed;
  1433. if(!rb.m_result.m_passed || getTestsContextState()->success)
  1434. rb.m_result.m_decomposition = toString(val);
  1435. int res = 0;
  1436. if(rb.log())
  1437. res |= assertAction::dbgbreak;
  1438. if(rb.m_failed && checkIfShouldThrow(assert_type))
  1439. res |= assertAction::shouldthrow;
  1440. #ifdef DOCTEST_CONFIG_SUPER_FAST_ASSERTS
  1441. // #########################################################################################
  1442. // IF THE DEBUGGER BREAKS HERE - GO 1 LEVEL UP IN THE CALLSTACK TO SEE THE FAILING ASSERTION
  1443. // THIS IS THE EFFECT OF HAVING 'DOCTEST_CONFIG_SUPER_FAST_ASSERTS' DEFINED
  1444. // #########################################################################################
  1445. if(res & assertAction::dbgbreak)
  1446. DOCTEST_BREAK_INTO_DEBUGGER();
  1447. fastAssertThrowIfFlagSet(res);
  1448. #endif // DOCTEST_CONFIG_SUPER_FAST_ASSERTS
  1449. return res;
  1450. }
  1451. struct DOCTEST_INTERFACE IExceptionTranslator //!OCLINT destructor of virtual class
  1452. {
  1453. virtual ~IExceptionTranslator();
  1454. virtual bool translate(String&) const = 0;
  1455. };
  1456. template <typename T>
  1457. class ExceptionTranslator : public IExceptionTranslator //!OCLINT destructor of virtual class
  1458. {
  1459. public:
  1460. explicit ExceptionTranslator(String (*translateFunction)(T))
  1461. : m_translateFunction(translateFunction) {}
  1462. bool translate(String& res) const {
  1463. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  1464. try {
  1465. throw;
  1466. // cppcheck-suppress catchExceptionByValue
  1467. } catch(T ex) { // NOLINT
  1468. res = m_translateFunction(ex); //!OCLINT parameter reassignment
  1469. return true;
  1470. } catch(...) {} //!OCLINT - empty catch statement
  1471. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  1472. ((void)res); // to silence -Wunused-parameter
  1473. return false;
  1474. }
  1475. protected:
  1476. String (*m_translateFunction)(T);
  1477. };
  1478. DOCTEST_INTERFACE void registerExceptionTranslatorImpl(
  1479. const IExceptionTranslator* translateFunction);
  1480. // FIX FOR VISUAL STUDIO VERSIONS PRIOR TO 2015 - they failed to compile the call to operator<< with
  1481. // std::ostream passed as a reference noting that there is a use of an undefined type (which there isn't)
  1482. DOCTEST_INTERFACE void writeStringToStream(std::ostream* stream, const String& str);
  1483. template <bool C>
  1484. struct StringStreamBase
  1485. {
  1486. template <typename T>
  1487. static void convert(std::ostream* stream, const T& in) {
  1488. writeStringToStream(stream, toString(in));
  1489. }
  1490. // always treat char* as a string in this context - no matter
  1491. // if DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING is defined
  1492. static void convert(std::ostream* stream, const char* in) {
  1493. writeStringToStream(stream, String(in));
  1494. }
  1495. };
  1496. template <>
  1497. struct StringStreamBase<true>
  1498. {
  1499. template <typename T>
  1500. static void convert(std::ostream* stream, const T& in) {
  1501. *stream << in;
  1502. }
  1503. };
  1504. template <typename T>
  1505. struct StringStream : StringStreamBase<has_insertion_operator<T>::value>
  1506. {};
  1507. template <typename T>
  1508. void toStream(std::ostream* stream, const T& value) {
  1509. StringStream<T>::convert(stream, value);
  1510. }
  1511. #ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  1512. DOCTEST_INTERFACE void toStream(std::ostream* stream, char* in);
  1513. DOCTEST_INTERFACE void toStream(std::ostream* stream, const char* in);
  1514. #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  1515. DOCTEST_INTERFACE void toStream(std::ostream* stream, bool in);
  1516. DOCTEST_INTERFACE void toStream(std::ostream* stream, float in);
  1517. DOCTEST_INTERFACE void toStream(std::ostream* stream, double in);
  1518. DOCTEST_INTERFACE void toStream(std::ostream* stream, double long in);
  1519. DOCTEST_INTERFACE void toStream(std::ostream* stream, char in);
  1520. DOCTEST_INTERFACE void toStream(std::ostream* stream, char signed in);
  1521. DOCTEST_INTERFACE void toStream(std::ostream* stream, char unsigned in);
  1522. DOCTEST_INTERFACE void toStream(std::ostream* stream, int short in);
  1523. DOCTEST_INTERFACE void toStream(std::ostream* stream, int short unsigned in);
  1524. DOCTEST_INTERFACE void toStream(std::ostream* stream, int in);
  1525. DOCTEST_INTERFACE void toStream(std::ostream* stream, int unsigned in);
  1526. DOCTEST_INTERFACE void toStream(std::ostream* stream, int long in);
  1527. DOCTEST_INTERFACE void toStream(std::ostream* stream, int long unsigned in);
  1528. #ifdef DOCTEST_CONFIG_WITH_LONG_LONG
  1529. DOCTEST_INTERFACE void toStream(std::ostream* stream, int long long in);
  1530. DOCTEST_INTERFACE void toStream(std::ostream* stream, int long long unsigned in);
  1531. #endif // DOCTEST_CONFIG_WITH_LONG_LONG
  1532. struct IContextScope //!OCLINT destructor of virtual class
  1533. { virtual void build(std::ostream*) = 0; };
  1534. DOCTEST_INTERFACE void addToContexts(IContextScope* ptr);
  1535. DOCTEST_INTERFACE void popFromContexts();
  1536. DOCTEST_INTERFACE void useContextIfExceptionOccurred(IContextScope* ptr);
  1537. // cppcheck-suppress copyCtorAndEqOperator
  1538. class ContextBuilder
  1539. {
  1540. friend class ContextScope;
  1541. struct ICapture //!OCLINT destructor of virtual class
  1542. { virtual void toStream(std::ostream*) const = 0; };
  1543. template <typename T>
  1544. struct Capture : ICapture //!OCLINT destructor of virtual class
  1545. {
  1546. const T* capture;
  1547. explicit Capture(const T* in)
  1548. : capture(in) {}
  1549. virtual void toStream(std::ostream* stream) const { // override
  1550. doctest::detail::toStream(stream, *capture);
  1551. }
  1552. };
  1553. struct Chunk
  1554. {
  1555. char buf[sizeof(Capture<char>)]; // place to construct a Capture<T>
  1556. };
  1557. struct Node
  1558. {
  1559. Chunk chunk;
  1560. Node* next;
  1561. };
  1562. Chunk stackChunks[DOCTEST_CONFIG_NUM_CAPTURES_ON_STACK];
  1563. int numCaptures;
  1564. Node* head;
  1565. Node* tail;
  1566. void build(std::ostream* stream) const {
  1567. int curr = 0;
  1568. // iterate over small buffer
  1569. while(curr < numCaptures && curr < DOCTEST_CONFIG_NUM_CAPTURES_ON_STACK)
  1570. reinterpret_cast<const ICapture*>(stackChunks[curr++].buf)->toStream(stream);
  1571. // iterate over list
  1572. Node* curr_elem = head;
  1573. while(curr < numCaptures) {
  1574. reinterpret_cast<const ICapture*>(curr_elem->chunk.buf)->toStream(stream);
  1575. curr_elem = curr_elem->next;
  1576. ++curr;
  1577. }
  1578. }
  1579. // steal the contents of the other - acting as a move constructor...
  1580. DOCTEST_NOINLINE ContextBuilder(ContextBuilder& other)
  1581. : numCaptures(other.numCaptures)
  1582. , head(other.head)
  1583. , tail(other.tail) {
  1584. other.numCaptures = 0;
  1585. other.head = 0;
  1586. other.tail = 0;
  1587. my_memcpy(stackChunks, other.stackChunks,
  1588. unsigned(int(sizeof(Chunk)) * DOCTEST_CONFIG_NUM_CAPTURES_ON_STACK));
  1589. }
  1590. public:
  1591. // cppcheck-suppress uninitMemberVar
  1592. DOCTEST_NOINLINE ContextBuilder() // NOLINT
  1593. : numCaptures(0)
  1594. , head(0)
  1595. , tail(0) {}
  1596. template <typename T>
  1597. DOCTEST_NOINLINE ContextBuilder& operator<<(T& in) {
  1598. Capture<T> temp(&in);
  1599. // construct either on stack or on heap
  1600. // copy the bytes for the whole object - including the vtable because we cant construct
  1601. // the object directly in the buffer using placement new - need the <new> header...
  1602. if(numCaptures < DOCTEST_CONFIG_NUM_CAPTURES_ON_STACK) {
  1603. my_memcpy(stackChunks[numCaptures].buf, &temp, sizeof(Chunk));
  1604. } else {
  1605. Node* curr = new Node;
  1606. curr->next = 0;
  1607. if(tail) {
  1608. tail->next = curr;
  1609. tail = curr;
  1610. } else {
  1611. head = tail = curr;
  1612. }
  1613. my_memcpy(tail->chunk.buf, &temp, sizeof(Chunk));
  1614. }
  1615. ++numCaptures;
  1616. return *this;
  1617. }
  1618. DOCTEST_NOINLINE ~ContextBuilder() {
  1619. // free the linked list - the ones on the stack are left as-is
  1620. // no destructors are called at all - there is no need
  1621. while(head) {
  1622. Node* next = head->next;
  1623. delete head;
  1624. head = next;
  1625. }
  1626. }
  1627. #ifdef DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  1628. template <typename T>
  1629. ContextBuilder& operator<<(const T&&) {
  1630. DOCTEST_STATIC_ASSERT(
  1631. deferred_false<T>::value,
  1632. Cannot_pass_temporaries_or_rvalues_to_the_streaming_operator_because_it_caches_pointers_to_the_passed_objects_for_lazy_evaluation);
  1633. return *this;
  1634. }
  1635. #endif // DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  1636. };
  1637. class ContextScope : public IContextScope //!OCLINT destructor of virtual class
  1638. {
  1639. ContextBuilder contextBuilder;
  1640. bool built;
  1641. public:
  1642. DOCTEST_NOINLINE explicit ContextScope(ContextBuilder& temp)
  1643. : contextBuilder(temp)
  1644. , built(false) {
  1645. addToContexts(this);
  1646. }
  1647. DOCTEST_NOINLINE ~ContextScope() {
  1648. if(!built)
  1649. useContextIfExceptionOccurred(this);
  1650. popFromContexts();
  1651. }
  1652. void build(std::ostream* stream) {
  1653. built = true;
  1654. contextBuilder.build(stream);
  1655. }
  1656. };
  1657. class DOCTEST_INTERFACE MessageBuilder
  1658. {
  1659. std::ostream* m_stream;
  1660. const char* m_file;
  1661. int m_line;
  1662. doctest::detail::assertType::Enum m_severity;
  1663. public:
  1664. MessageBuilder(const char* file, int line, doctest::detail::assertType::Enum severity);
  1665. ~MessageBuilder();
  1666. template <typename T>
  1667. MessageBuilder& operator<<(const T& in) {
  1668. doctest::detail::toStream(m_stream, in);
  1669. return *this;
  1670. }
  1671. bool log();
  1672. void react();
  1673. };
  1674. } // namespace detail
  1675. struct test_suite
  1676. {
  1677. const char* data;
  1678. test_suite(const char* in)
  1679. : data(in) {}
  1680. void fill(detail::TestCase& state) const { state.m_test_suite = data; }
  1681. void fill(detail::TestSuite& state) const { state.m_test_suite = data; }
  1682. };
  1683. struct description
  1684. {
  1685. const char* data;
  1686. description(const char* in)
  1687. : data(in) {}
  1688. void fill(detail::TestCase& state) const { state.m_description = data; }
  1689. void fill(detail::TestSuite& state) const { state.m_description = data; }
  1690. };
  1691. struct skip
  1692. {
  1693. bool data;
  1694. skip(bool in = true)
  1695. : data(in) {}
  1696. void fill(detail::TestCase& state) const { state.m_skip = data; }
  1697. void fill(detail::TestSuite& state) const { state.m_skip = data; }
  1698. };
  1699. struct timeout
  1700. {
  1701. double data;
  1702. timeout(double in)
  1703. : data(in) {}
  1704. void fill(detail::TestCase& state) const { state.m_timeout = data; }
  1705. void fill(detail::TestSuite& state) const { state.m_timeout = data; }
  1706. };
  1707. struct may_fail
  1708. {
  1709. bool data;
  1710. may_fail(bool in = true)
  1711. : data(in) {}
  1712. void fill(detail::TestCase& state) const { state.m_may_fail = data; }
  1713. void fill(detail::TestSuite& state) const { state.m_may_fail = data; }
  1714. };
  1715. struct should_fail
  1716. {
  1717. bool data;
  1718. should_fail(bool in = true)
  1719. : data(in) {}
  1720. void fill(detail::TestCase& state) const { state.m_should_fail = data; }
  1721. void fill(detail::TestSuite& state) const { state.m_should_fail = data; }
  1722. };
  1723. struct expected_failures
  1724. {
  1725. int data;
  1726. expected_failures(int in)
  1727. : data(in) {}
  1728. void fill(detail::TestCase& state) const { state.m_expected_failures = data; }
  1729. void fill(detail::TestSuite& state) const { state.m_expected_failures = data; }
  1730. };
  1731. #endif // DOCTEST_CONFIG_DISABLE
  1732. #ifndef DOCTEST_CONFIG_DISABLE
  1733. template <typename T>
  1734. int registerExceptionTranslator(String (*translateFunction)(T)) {
  1735. #if defined(__clang__)
  1736. #pragma clang diagnostic push
  1737. #pragma clang diagnostic ignored "-Wexit-time-destructors"
  1738. #endif // __clang__
  1739. static detail::ExceptionTranslator<T> exceptionTranslator(translateFunction);
  1740. #if defined(__clang__)
  1741. #pragma clang diagnostic pop
  1742. #endif // __clang__
  1743. detail::registerExceptionTranslatorImpl(&exceptionTranslator);
  1744. return 0;
  1745. }
  1746. #else // DOCTEST_CONFIG_DISABLE
  1747. template <typename T>
  1748. int registerExceptionTranslator(String (*)(T)) {
  1749. return 0;
  1750. }
  1751. #endif // DOCTEST_CONFIG_DISABLE
  1752. DOCTEST_INTERFACE bool isRunningInTest();
  1753. // cppcheck-suppress noCopyConstructor
  1754. class DOCTEST_INTERFACE Context
  1755. {
  1756. #if !defined(DOCTEST_CONFIG_DISABLE)
  1757. detail::ContextState* p;
  1758. void parseArgs(int argc, const char* const* argv, bool withDefaults = false);
  1759. #endif // DOCTEST_CONFIG_DISABLE
  1760. public:
  1761. explicit Context(int argc = 0, const char* const* argv = 0);
  1762. ~Context();
  1763. void applyCommandLine(int argc, const char* const* argv);
  1764. void addFilter(const char* filter, const char* value);
  1765. void clearFilters();
  1766. void setOption(const char* option, int value);
  1767. void setOption(const char* option, const char* value);
  1768. bool shouldExit();
  1769. int run();
  1770. };
  1771. } // namespace doctest
  1772. // if registering is not disabled
  1773. #if !defined(DOCTEST_CONFIG_DISABLE)
  1774. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1775. #define DOCTEST_EXPAND_VA_ARGS(...) __VA_ARGS__
  1776. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1777. #define DOCTEST_EXPAND_VA_ARGS
  1778. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1779. #define DOCTEST_STRIP_PARENS(x) x
  1780. #define DOCTEST_HANDLE_BRACED_VA_ARGS(expr) DOCTEST_STRIP_PARENS(DOCTEST_EXPAND_VA_ARGS expr)
  1781. // registers the test by initializing a dummy var with a function
  1782. #define DOCTEST_REGISTER_FUNCTION(f, decorators) \
  1783. DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(_DOCTEST_ANON_VAR_)) = doctest::detail::regTest( \
  1784. doctest::detail::TestCase(f, __FILE__, __LINE__, \
  1785. doctest_detail_test_suite_ns::getCurrentTestSuite()) * \
  1786. decorators); \
  1787. DOCTEST_GLOBAL_NO_WARNINGS_END()
  1788. #define DOCTEST_IMPLEMENT_FIXTURE(der, base, func, decorators) \
  1789. namespace \
  1790. { \
  1791. struct der : base \
  1792. { void f(); }; \
  1793. static void func() { \
  1794. der v; \
  1795. v.f(); \
  1796. } \
  1797. DOCTEST_REGISTER_FUNCTION(func, decorators) \
  1798. } \
  1799. inline DOCTEST_NOINLINE void der::f()
  1800. #define DOCTEST_CREATE_AND_REGISTER_FUNCTION(f, decorators) \
  1801. static void f(); \
  1802. DOCTEST_REGISTER_FUNCTION(f, decorators) \
  1803. static void f()
  1804. // for registering tests
  1805. #define DOCTEST_TEST_CASE(decorators) \
  1806. DOCTEST_CREATE_AND_REGISTER_FUNCTION(DOCTEST_ANONYMOUS(_DOCTEST_ANON_FUNC_), decorators)
  1807. // for registering tests with a fixture
  1808. #define DOCTEST_TEST_CASE_FIXTURE(c, decorators) \
  1809. DOCTEST_IMPLEMENT_FIXTURE(DOCTEST_ANONYMOUS(_DOCTEST_ANON_CLASS_), c, \
  1810. DOCTEST_ANONYMOUS(_DOCTEST_ANON_FUNC_), decorators)
  1811. // for converting types to strings without the <typeinfo> header and demangling
  1812. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1813. #define DOCTEST_TYPE_TO_STRING_IMPL(...) \
  1814. template <> \
  1815. inline const char* type_to_string<__VA_ARGS__>() { \
  1816. return "<" #__VA_ARGS__ ">"; \
  1817. }
  1818. #define DOCTEST_TYPE_TO_STRING(...) \
  1819. namespace doctest \
  1820. { \
  1821. namespace detail \
  1822. { DOCTEST_TYPE_TO_STRING_IMPL(__VA_ARGS__) } \
  1823. } \
  1824. typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_)
  1825. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1826. #define DOCTEST_TYPE_TO_STRING_IMPL(x) \
  1827. template <> \
  1828. inline const char* type_to_string<x>() { \
  1829. return "<" #x ">"; \
  1830. }
  1831. #define DOCTEST_TYPE_TO_STRING(x) \
  1832. namespace doctest \
  1833. { \
  1834. namespace detail \
  1835. { DOCTEST_TYPE_TO_STRING_IMPL(x) } \
  1836. } \
  1837. typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_)
  1838. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1839. // for typed tests
  1840. #define DOCTEST_TEST_CASE_TEMPLATE_IMPL(decorators, T, types, anon) \
  1841. template <typename T> \
  1842. inline void anon(); \
  1843. struct DOCTEST_CAT(anon, FUNCTOR) \
  1844. { \
  1845. template <int Index, typename Type> \
  1846. void operator()() { \
  1847. doctest::detail::regTest( \
  1848. doctest::detail::TestCase(anon<Type>, __FILE__, __LINE__, \
  1849. doctest_detail_test_suite_ns::getCurrentTestSuite(), \
  1850. doctest::detail::type_to_string<Type>(), Index) * \
  1851. decorators); \
  1852. } \
  1853. }; \
  1854. inline int DOCTEST_CAT(anon, REG_FUNC)() { \
  1855. DOCTEST_CAT(anon, FUNCTOR) registrar; \
  1856. doctest::detail::ForEachType<DOCTEST_HANDLE_BRACED_VA_ARGS(types)::Result, \
  1857. DOCTEST_CAT(anon, FUNCTOR)> \
  1858. doIt(registrar); \
  1859. return 0; \
  1860. } \
  1861. DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_CAT(anon, DUMMY)) = DOCTEST_CAT(anon, REG_FUNC)(); \
  1862. DOCTEST_GLOBAL_NO_WARNINGS_END() \
  1863. template <typename T> \
  1864. inline void anon()
  1865. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1866. #define DOCTEST_TEST_CASE_TEMPLATE(decorators, T, ...) \
  1867. DOCTEST_TEST_CASE_TEMPLATE_IMPL(decorators, T, (__VA_ARGS__), \
  1868. DOCTEST_ANONYMOUS(_DOCTEST_ANON_TMP_))
  1869. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1870. #define DOCTEST_TEST_CASE_TEMPLATE(decorators, T, types) \
  1871. DOCTEST_TEST_CASE_TEMPLATE_IMPL(decorators, T, types, DOCTEST_ANONYMOUS(_DOCTEST_ANON_TMP_))
  1872. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1873. #define DOCTEST_TEST_CASE_TEMPLATE_DEFINE_IMPL(decorators, T, id, anon) \
  1874. template <typename T> \
  1875. inline void anon(); \
  1876. struct DOCTEST_CAT(id, _FUNCTOR) \
  1877. { \
  1878. int m_line; \
  1879. DOCTEST_CAT(id, _FUNCTOR) \
  1880. (int line) \
  1881. : m_line(line) {} \
  1882. template <int Index, typename Type> \
  1883. void operator()() { \
  1884. doctest::detail::regTest( \
  1885. doctest::detail::TestCase(anon<Type>, __FILE__, __LINE__, \
  1886. doctest_detail_test_suite_ns::getCurrentTestSuite(), \
  1887. doctest::detail::type_to_string<Type>(), \
  1888. m_line * 1000 + Index) * \
  1889. decorators); \
  1890. } \
  1891. }; \
  1892. template <typename T> \
  1893. inline void anon()
  1894. #define DOCTEST_TEST_CASE_TEMPLATE_DEFINE(decorators, T, id) \
  1895. DOCTEST_TEST_CASE_TEMPLATE_DEFINE_IMPL(decorators, T, id, DOCTEST_ANONYMOUS(_DOCTEST_ANON_TMP_))
  1896. #define DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE_IMPL(id, types, anon) \
  1897. static int DOCTEST_CAT(anon, REG_FUNC)() { \
  1898. DOCTEST_CAT(id, _FUNCTOR) registrar(__LINE__); \
  1899. doctest::detail::ForEachType<DOCTEST_HANDLE_BRACED_VA_ARGS(types)::Result, \
  1900. DOCTEST_CAT(id, _FUNCTOR)> \
  1901. doIt(registrar); \
  1902. return 0; \
  1903. } \
  1904. DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_CAT(anon, DUMMY)) = DOCTEST_CAT(anon, REG_FUNC)(); \
  1905. DOCTEST_GLOBAL_NO_WARNINGS_END() typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_)
  1906. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1907. #define DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE(id, ...) \
  1908. DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE_IMPL(id, (__VA_ARGS__), \
  1909. DOCTEST_ANONYMOUS(_DOCTEST_ANON_TMP_))
  1910. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1911. #define DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE(id, types) \
  1912. DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE_IMPL(id, types, DOCTEST_ANONYMOUS(_DOCTEST_ANON_TMP_))
  1913. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1914. // for subcases
  1915. #if defined(__GNUC__)
  1916. #define DOCTEST_SUBCASE(name) \
  1917. if(const doctest::detail::Subcase & DOCTEST_ANONYMOUS(_DOCTEST_ANON_SUBCASE_) \
  1918. __attribute__((unused)) = \
  1919. doctest::detail::Subcase(name, __FILE__, __LINE__))
  1920. #else // __GNUC__
  1921. #define DOCTEST_SUBCASE(name) \
  1922. if(const doctest::detail::Subcase & DOCTEST_ANONYMOUS(_DOCTEST_ANON_SUBCASE_) = \
  1923. doctest::detail::Subcase(name, __FILE__, __LINE__))
  1924. #endif // __GNUC__
  1925. // for grouping tests in test suites by using code blocks
  1926. #define DOCTEST_TEST_SUITE_IMPL(decorators, ns_name) \
  1927. namespace ns_name \
  1928. { \
  1929. namespace doctest_detail_test_suite_ns \
  1930. { \
  1931. inline DOCTEST_NOINLINE doctest::detail::TestSuite& getCurrentTestSuite() { \
  1932. static doctest::detail::TestSuite data; \
  1933. static bool inited = false; \
  1934. if(!inited) { \
  1935. data* decorators; \
  1936. inited = true; \
  1937. } \
  1938. return data; \
  1939. } \
  1940. } \
  1941. } \
  1942. namespace ns_name
  1943. #define DOCTEST_TEST_SUITE(decorators) \
  1944. DOCTEST_TEST_SUITE_IMPL(decorators, DOCTEST_ANONYMOUS(_DOCTEST_ANON_SUITE_))
  1945. // for starting a testsuite block
  1946. #define DOCTEST_TEST_SUITE_BEGIN(decorators) \
  1947. DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(_DOCTEST_ANON_VAR_)) = \
  1948. doctest::detail::setTestSuite(doctest::detail::TestSuite() * decorators); \
  1949. DOCTEST_GLOBAL_NO_WARNINGS_END() \
  1950. typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_)
  1951. // for ending a testsuite block
  1952. #define DOCTEST_TEST_SUITE_END \
  1953. DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(_DOCTEST_ANON_VAR_)) = \
  1954. doctest::detail::setTestSuite(doctest::detail::TestSuite() * ""); \
  1955. DOCTEST_GLOBAL_NO_WARNINGS_END() \
  1956. typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_)
  1957. // for registering exception translators
  1958. #define DOCTEST_REGISTER_EXCEPTION_TRANSLATOR_IMPL(translatorName, signature) \
  1959. static doctest::String translatorName(signature); \
  1960. DOCTEST_GLOBAL_NO_WARNINGS(DOCTEST_ANONYMOUS(_DOCTEST_ANON_TRANSLATOR_)) = \
  1961. doctest::registerExceptionTranslator(translatorName); \
  1962. DOCTEST_GLOBAL_NO_WARNINGS_END() \
  1963. static doctest::String translatorName(signature)
  1964. #define DOCTEST_REGISTER_EXCEPTION_TRANSLATOR(signature) \
  1965. DOCTEST_REGISTER_EXCEPTION_TRANSLATOR_IMPL(DOCTEST_ANONYMOUS(_DOCTEST_ANON_TRANSLATOR_), \
  1966. signature)
  1967. // for logging
  1968. #define DOCTEST_INFO(x) \
  1969. doctest::detail::ContextScope DOCTEST_ANONYMOUS(_DOCTEST_CAPTURE_)( \
  1970. doctest::detail::ContextBuilder() << x)
  1971. #define DOCTEST_CAPTURE(x) DOCTEST_INFO(#x " := " << x)
  1972. #define DOCTEST_ADD_AT_IMPL(type, file, line, mb, x) \
  1973. do { \
  1974. doctest::detail::MessageBuilder mb(file, line, doctest::detail::assertType::type); \
  1975. mb << x; \
  1976. if(mb.log()) \
  1977. DOCTEST_BREAK_INTO_DEBUGGER(); \
  1978. mb.react(); \
  1979. } while((void)0, 0)
  1980. // clang-format off
  1981. #define DOCTEST_ADD_MESSAGE_AT(file, line, x) DOCTEST_ADD_AT_IMPL(is_warn, file, line, DOCTEST_ANONYMOUS(_DOCTEST_MESSAGE_), x)
  1982. #define DOCTEST_ADD_FAIL_CHECK_AT(file, line, x) DOCTEST_ADD_AT_IMPL(is_check, file, line, DOCTEST_ANONYMOUS(_DOCTEST_MESSAGE_), x)
  1983. #define DOCTEST_ADD_FAIL_AT(file, line, x) DOCTEST_ADD_AT_IMPL(is_require, file, line, DOCTEST_ANONYMOUS(_DOCTEST_MESSAGE_), x)
  1984. // clang-format on
  1985. #define DOCTEST_MESSAGE(x) DOCTEST_ADD_MESSAGE_AT(__FILE__, __LINE__, x)
  1986. #define DOCTEST_FAIL_CHECK(x) DOCTEST_ADD_FAIL_CHECK_AT(__FILE__, __LINE__, x)
  1987. #define DOCTEST_FAIL(x) DOCTEST_ADD_FAIL_AT(__FILE__, __LINE__, x)
  1988. #if __cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1910)
  1989. template <class T, T x>
  1990. constexpr T to_lvalue = x;
  1991. #define DOCTEST_TO_LVALUE(...) to_lvalue<decltype(__VA_ARGS__), __VA_ARGS__>
  1992. #else
  1993. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1994. #define DOCTEST_TO_LVALUE(...) TO_LVALUE_CAN_BE_USED_ONLY_IN_CPP14_MODE_OR_WITH_VS_2017_OR_NEWER
  1995. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1996. #define DOCTEST_TO_LVALUE(x) TO_LVALUE_CAN_BE_USED_ONLY_IN_CPP14_MODE_OR_WITH_VS_2017_OR_NEWER
  1997. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  1998. #endif // TO_LVALUE hack for logging macros like INFO()
  1999. // common code in asserts - for convenience
  2000. #define DOCTEST_ASSERT_LOG_AND_REACT(rb) \
  2001. if(rb.log()) \
  2002. DOCTEST_BREAK_INTO_DEBUGGER(); \
  2003. rb.react()
  2004. #ifdef DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS
  2005. #define DOCTEST_WRAP_IN_TRY(x) x;
  2006. #else // DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS
  2007. #define DOCTEST_WRAP_IN_TRY(x) \
  2008. try { \
  2009. x; \
  2010. } catch(...) { _DOCTEST_RB.unexpectedExceptionOccurred(); }
  2011. #endif // DOCTEST_CONFIG_NO_TRY_CATCH_IN_ASSERTS
  2012. #define DOCTEST_ASSERT_IMPLEMENT_3(expr, assert_type) \
  2013. doctest::detail::ResultBuilder _DOCTEST_RB( \
  2014. doctest::detail::assertType::assert_type, __FILE__, __LINE__, \
  2015. DOCTEST_TOSTR(DOCTEST_HANDLE_BRACED_VA_ARGS(expr))); \
  2016. DOCTEST_WRAP_IN_TRY(_DOCTEST_RB.setResult( \
  2017. doctest::detail::ExpressionDecomposer(doctest::detail::assertType::assert_type) \
  2018. << DOCTEST_HANDLE_BRACED_VA_ARGS(expr))) \
  2019. DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB)
  2020. #if defined(__clang__)
  2021. #define DOCTEST_ASSERT_IMPLEMENT_2(expr, assert_type) \
  2022. _Pragma("clang diagnostic push") \
  2023. _Pragma("clang diagnostic ignored \"-Woverloaded-shift-op-parentheses\"") \
  2024. DOCTEST_ASSERT_IMPLEMENT_3(expr, assert_type); \
  2025. _Pragma("clang diagnostic pop")
  2026. #else // __clang__
  2027. #define DOCTEST_ASSERT_IMPLEMENT_2(expr, assert_type) DOCTEST_ASSERT_IMPLEMENT_3(expr, assert_type);
  2028. #endif // __clang__
  2029. #define DOCTEST_ASSERT_IMPLEMENT_1(expr, assert_type) \
  2030. do { \
  2031. DOCTEST_ASSERT_IMPLEMENT_2(expr, assert_type); \
  2032. } while((void)0, 0)
  2033. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2034. #define DOCTEST_WARN(...) DOCTEST_ASSERT_IMPLEMENT_1((__VA_ARGS__), DT_WARN)
  2035. #define DOCTEST_CHECK(...) DOCTEST_ASSERT_IMPLEMENT_1((__VA_ARGS__), DT_CHECK)
  2036. #define DOCTEST_REQUIRE(...) DOCTEST_ASSERT_IMPLEMENT_1((__VA_ARGS__), DT_REQUIRE)
  2037. #define DOCTEST_WARN_FALSE(...) DOCTEST_ASSERT_IMPLEMENT_1((__VA_ARGS__), DT_WARN_FALSE)
  2038. #define DOCTEST_CHECK_FALSE(...) DOCTEST_ASSERT_IMPLEMENT_1((__VA_ARGS__), DT_CHECK_FALSE)
  2039. #define DOCTEST_REQUIRE_FALSE(...) DOCTEST_ASSERT_IMPLEMENT_1((__VA_ARGS__), DT_REQUIRE_FALSE)
  2040. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2041. #define DOCTEST_WARN(expr) DOCTEST_ASSERT_IMPLEMENT_1(expr, DT_WARN)
  2042. #define DOCTEST_CHECK(expr) DOCTEST_ASSERT_IMPLEMENT_1(expr, DT_CHECK)
  2043. #define DOCTEST_REQUIRE(expr) DOCTEST_ASSERT_IMPLEMENT_1(expr, DT_REQUIRE)
  2044. #define DOCTEST_WARN_FALSE(expr) DOCTEST_ASSERT_IMPLEMENT_1(expr, DT_WARN_FALSE)
  2045. #define DOCTEST_CHECK_FALSE(expr) DOCTEST_ASSERT_IMPLEMENT_1(expr, DT_CHECK_FALSE)
  2046. #define DOCTEST_REQUIRE_FALSE(expr) DOCTEST_ASSERT_IMPLEMENT_1(expr, DT_REQUIRE_FALSE)
  2047. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2048. // clang-format off
  2049. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2050. #define DOCTEST_WARN_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2((cond), DT_WARN); } while((void)0, 0)
  2051. #define DOCTEST_CHECK_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2((cond), DT_CHECK); } while((void)0, 0)
  2052. #define DOCTEST_REQUIRE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2((cond), DT_REQUIRE); } while((void)0, 0)
  2053. #define DOCTEST_WARN_FALSE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2((cond), DT_WARN_FALSE); } while((void)0, 0)
  2054. #define DOCTEST_CHECK_FALSE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2((cond), DT_CHECK_FALSE); } while((void)0, 0)
  2055. #define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2((cond), DT_REQUIRE_FALSE); } while((void)0, 0)
  2056. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2057. #define DOCTEST_WARN_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(cond, DT_WARN); } while((void)0, 0)
  2058. #define DOCTEST_CHECK_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(cond, DT_CHECK); } while((void)0, 0)
  2059. #define DOCTEST_REQUIRE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(cond, DT_REQUIRE); } while((void)0, 0)
  2060. #define DOCTEST_WARN_FALSE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(cond, DT_WARN_FALSE); } while((void)0, 0)
  2061. #define DOCTEST_CHECK_FALSE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(cond, DT_CHECK_FALSE); } while((void)0, 0)
  2062. #define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, msg) do { DOCTEST_INFO(msg); DOCTEST_ASSERT_IMPLEMENT_2(cond, DT_REQUIRE_FALSE); } while((void)0, 0)
  2063. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2064. // clang-format on
  2065. #define DOCTEST_ASSERT_THROWS(expr, assert_type) \
  2066. do { \
  2067. if(!doctest::detail::getTestsContextState()->no_throw) { \
  2068. doctest::detail::ResultBuilder _DOCTEST_RB(doctest::detail::assertType::assert_type, \
  2069. __FILE__, __LINE__, #expr); \
  2070. try { \
  2071. expr; \
  2072. } catch(...) { _DOCTEST_RB.m_threw = true; } \
  2073. DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \
  2074. } \
  2075. } while((void)0, 0)
  2076. #define DOCTEST_ASSERT_THROWS_AS(expr, as, assert_type) \
  2077. do { \
  2078. if(!doctest::detail::getTestsContextState()->no_throw) { \
  2079. doctest::detail::ResultBuilder _DOCTEST_RB( \
  2080. doctest::detail::assertType::assert_type, __FILE__, __LINE__, #expr, \
  2081. DOCTEST_TOSTR(DOCTEST_HANDLE_BRACED_VA_ARGS(as))); \
  2082. try { \
  2083. expr; \
  2084. } catch(DOCTEST_HANDLE_BRACED_VA_ARGS(as)) { \
  2085. _DOCTEST_RB.m_threw = true; \
  2086. _DOCTEST_RB.m_threw_as = true; \
  2087. } catch(...) { _DOCTEST_RB.unexpectedExceptionOccurred(); } \
  2088. DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \
  2089. } \
  2090. } while((void)0, 0)
  2091. #define DOCTEST_ASSERT_NOTHROW(expr, assert_type) \
  2092. do { \
  2093. if(!doctest::detail::getTestsContextState()->no_throw) { \
  2094. doctest::detail::ResultBuilder _DOCTEST_RB(doctest::detail::assertType::assert_type, \
  2095. __FILE__, __LINE__, #expr); \
  2096. try { \
  2097. expr; \
  2098. } catch(...) { _DOCTEST_RB.unexpectedExceptionOccurred(); } \
  2099. DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \
  2100. } \
  2101. } while((void)0, 0)
  2102. #define DOCTEST_WARN_THROWS(expr) DOCTEST_ASSERT_THROWS(expr, DT_WARN_THROWS)
  2103. #define DOCTEST_CHECK_THROWS(expr) DOCTEST_ASSERT_THROWS(expr, DT_CHECK_THROWS)
  2104. #define DOCTEST_REQUIRE_THROWS(expr) DOCTEST_ASSERT_THROWS(expr, DT_REQUIRE_THROWS)
  2105. // clang-format off
  2106. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2107. #define DOCTEST_WARN_THROWS_AS(expr, ...) DOCTEST_ASSERT_THROWS_AS(expr, (__VA_ARGS__), DT_WARN_THROWS_AS)
  2108. #define DOCTEST_CHECK_THROWS_AS(expr, ...) DOCTEST_ASSERT_THROWS_AS(expr, (__VA_ARGS__), DT_CHECK_THROWS_AS)
  2109. #define DOCTEST_REQUIRE_THROWS_AS(expr, ...) DOCTEST_ASSERT_THROWS_AS(expr, (__VA_ARGS__), DT_REQUIRE_THROWS_AS)
  2110. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2111. #define DOCTEST_WARN_THROWS_AS(expr, ex) DOCTEST_ASSERT_THROWS_AS(expr, ex, DT_WARN_THROWS_AS)
  2112. #define DOCTEST_CHECK_THROWS_AS(expr, ex) DOCTEST_ASSERT_THROWS_AS(expr, ex, DT_CHECK_THROWS_AS)
  2113. #define DOCTEST_REQUIRE_THROWS_AS(expr, ex) DOCTEST_ASSERT_THROWS_AS(expr, ex, DT_REQUIRE_THROWS_AS)
  2114. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2115. // clang-format on
  2116. #define DOCTEST_WARN_NOTHROW(expr) DOCTEST_ASSERT_NOTHROW(expr, DT_WARN_NOTHROW)
  2117. #define DOCTEST_CHECK_NOTHROW(expr) DOCTEST_ASSERT_NOTHROW(expr, DT_CHECK_NOTHROW)
  2118. #define DOCTEST_REQUIRE_NOTHROW(expr) DOCTEST_ASSERT_NOTHROW(expr, DT_REQUIRE_NOTHROW)
  2119. // clang-format off
  2120. #define DOCTEST_WARN_THROWS_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_WARN_THROWS(expr); } while((void)0, 0)
  2121. #define DOCTEST_CHECK_THROWS_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_CHECK_THROWS(expr); } while((void)0, 0)
  2122. #define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_REQUIRE_THROWS(expr); } while((void)0, 0)
  2123. #define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, msg) do { DOCTEST_INFO(msg); DOCTEST_WARN_THROWS_AS(expr, ex); } while((void)0, 0)
  2124. #define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, msg) do { DOCTEST_INFO(msg); DOCTEST_CHECK_THROWS_AS(expr, ex); } while((void)0, 0)
  2125. #define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, msg) do { DOCTEST_INFO(msg); DOCTEST_REQUIRE_THROWS_AS(expr, ex); } while((void)0, 0)
  2126. #define DOCTEST_WARN_NOTHROW_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_WARN_NOTHROW(expr); } while((void)0, 0)
  2127. #define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_CHECK_NOTHROW(expr); } while((void)0, 0)
  2128. #define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, msg) do { DOCTEST_INFO(msg); DOCTEST_REQUIRE_NOTHROW(expr); } while((void)0, 0)
  2129. // clang-format on
  2130. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2131. #define DOCTEST_BINARY_ASSERT(assert_type, expr, comp) \
  2132. do { \
  2133. doctest::detail::ResultBuilder _DOCTEST_RB( \
  2134. doctest::detail::assertType::assert_type, __FILE__, __LINE__, \
  2135. DOCTEST_TOSTR(DOCTEST_HANDLE_BRACED_VA_ARGS(expr))); \
  2136. DOCTEST_WRAP_IN_TRY( \
  2137. _DOCTEST_RB.binary_assert<doctest::detail::binaryAssertComparison::comp>( \
  2138. DOCTEST_HANDLE_BRACED_VA_ARGS(expr))) \
  2139. DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \
  2140. } while((void)0, 0)
  2141. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2142. #define DOCTEST_BINARY_ASSERT(assert_type, lhs, rhs, comp) \
  2143. do { \
  2144. doctest::detail::ResultBuilder _DOCTEST_RB(doctest::detail::assertType::assert_type, \
  2145. __FILE__, __LINE__, #lhs ", " #rhs); \
  2146. DOCTEST_WRAP_IN_TRY( \
  2147. _DOCTEST_RB.binary_assert<doctest::detail::binaryAssertComparison::comp>(lhs, \
  2148. rhs)) \
  2149. DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \
  2150. } while((void)0, 0)
  2151. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2152. #define DOCTEST_UNARY_ASSERT(assert_type, expr) \
  2153. do { \
  2154. doctest::detail::ResultBuilder _DOCTEST_RB( \
  2155. doctest::detail::assertType::assert_type, __FILE__, __LINE__, \
  2156. DOCTEST_TOSTR(DOCTEST_HANDLE_BRACED_VA_ARGS(expr))); \
  2157. DOCTEST_WRAP_IN_TRY(_DOCTEST_RB.unary_assert(DOCTEST_HANDLE_BRACED_VA_ARGS(expr))) \
  2158. DOCTEST_ASSERT_LOG_AND_REACT(_DOCTEST_RB); \
  2159. } while((void)0, 0)
  2160. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2161. #define DOCTEST_WARN_EQ(...) DOCTEST_BINARY_ASSERT(DT_WARN_EQ, (__VA_ARGS__), eq)
  2162. #define DOCTEST_CHECK_EQ(...) DOCTEST_BINARY_ASSERT(DT_CHECK_EQ, (__VA_ARGS__), eq)
  2163. #define DOCTEST_REQUIRE_EQ(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_EQ, (__VA_ARGS__), eq)
  2164. #define DOCTEST_WARN_NE(...) DOCTEST_BINARY_ASSERT(DT_WARN_NE, (__VA_ARGS__), ne)
  2165. #define DOCTEST_CHECK_NE(...) DOCTEST_BINARY_ASSERT(DT_CHECK_NE, (__VA_ARGS__), ne)
  2166. #define DOCTEST_REQUIRE_NE(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_NE, (__VA_ARGS__), ne)
  2167. #define DOCTEST_WARN_GT(...) DOCTEST_BINARY_ASSERT(DT_WARN_GT, (__VA_ARGS__), gt)
  2168. #define DOCTEST_CHECK_GT(...) DOCTEST_BINARY_ASSERT(DT_CHECK_GT, (__VA_ARGS__), gt)
  2169. #define DOCTEST_REQUIRE_GT(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_GT, (__VA_ARGS__), gt)
  2170. #define DOCTEST_WARN_LT(...) DOCTEST_BINARY_ASSERT(DT_WARN_LT, (__VA_ARGS__), lt)
  2171. #define DOCTEST_CHECK_LT(...) DOCTEST_BINARY_ASSERT(DT_CHECK_LT, (__VA_ARGS__), lt)
  2172. #define DOCTEST_REQUIRE_LT(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_LT, (__VA_ARGS__), lt)
  2173. #define DOCTEST_WARN_GE(...) DOCTEST_BINARY_ASSERT(DT_WARN_GE, (__VA_ARGS__), ge)
  2174. #define DOCTEST_CHECK_GE(...) DOCTEST_BINARY_ASSERT(DT_CHECK_GE, (__VA_ARGS__), ge)
  2175. #define DOCTEST_REQUIRE_GE(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_GE, (__VA_ARGS__), ge)
  2176. #define DOCTEST_WARN_LE(...) DOCTEST_BINARY_ASSERT(DT_WARN_LE, (__VA_ARGS__), le)
  2177. #define DOCTEST_CHECK_LE(...) DOCTEST_BINARY_ASSERT(DT_CHECK_LE, (__VA_ARGS__), le)
  2178. #define DOCTEST_REQUIRE_LE(...) DOCTEST_BINARY_ASSERT(DT_REQUIRE_LE, (__VA_ARGS__), le)
  2179. #define DOCTEST_WARN_UNARY(...) DOCTEST_UNARY_ASSERT(DT_WARN_UNARY, (__VA_ARGS__))
  2180. #define DOCTEST_CHECK_UNARY(...) DOCTEST_UNARY_ASSERT(DT_CHECK_UNARY, (__VA_ARGS__))
  2181. #define DOCTEST_REQUIRE_UNARY(...) DOCTEST_UNARY_ASSERT(DT_REQUIRE_UNARY, (__VA_ARGS__))
  2182. #define DOCTEST_WARN_UNARY_FALSE(...) DOCTEST_UNARY_ASSERT(DT_WARN_UNARY_FALSE, (__VA_ARGS__))
  2183. #define DOCTEST_CHECK_UNARY_FALSE(...) DOCTEST_UNARY_ASSERT(DT_CHECK_UNARY_FALSE, (__VA_ARGS__))
  2184. #define DOCTEST_REQUIRE_UNARY_FALSE(...) DOCTEST_UNARY_ASSERT(DT_REQUIRE_UNARY_FALSE, (__VA_ARGS__))
  2185. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2186. #define DOCTEST_WARN_EQ(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_WARN_EQ, lhs, rhs, eq)
  2187. #define DOCTEST_CHECK_EQ(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_CHECK_EQ, lhs, rhs, eq)
  2188. #define DOCTEST_REQUIRE_EQ(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_REQUIRE_EQ, lhs, rhs, eq)
  2189. #define DOCTEST_WARN_NE(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_WARN_NE, lhs, rhs, ne)
  2190. #define DOCTEST_CHECK_NE(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_CHECK_NE, lhs, rhs, ne)
  2191. #define DOCTEST_REQUIRE_NE(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_REQUIRE_NE, lhs, rhs, ne)
  2192. #define DOCTEST_WARN_GT(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_WARN_GT, lhs, rhs, gt)
  2193. #define DOCTEST_CHECK_GT(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_CHECK_GT, lhs, rhs, gt)
  2194. #define DOCTEST_REQUIRE_GT(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_REQUIRE_GT, lhs, rhs, gt)
  2195. #define DOCTEST_WARN_LT(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_WARN_LT, lhs, rhs, lt)
  2196. #define DOCTEST_CHECK_LT(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_CHECK_LT, lhs, rhs, lt)
  2197. #define DOCTEST_REQUIRE_LT(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_REQUIRE_LT, lhs, rhs, lt)
  2198. #define DOCTEST_WARN_GE(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_WARN_GE, lhs, rhs, ge)
  2199. #define DOCTEST_CHECK_GE(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_CHECK_GE, lhs, rhs, ge)
  2200. #define DOCTEST_REQUIRE_GE(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_REQUIRE_GE, lhs, rhs, ge)
  2201. #define DOCTEST_WARN_LE(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_WARN_LE, lhs, rhs, le)
  2202. #define DOCTEST_CHECK_LE(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_CHECK_LE, lhs, rhs, le)
  2203. #define DOCTEST_REQUIRE_LE(lhs, rhs) DOCTEST_BINARY_ASSERT(DT_REQUIRE_LE, lhs, rhs, le)
  2204. #define DOCTEST_WARN_UNARY(v) DOCTEST_UNARY_ASSERT(DT_WARN_UNARY, v)
  2205. #define DOCTEST_CHECK_UNARY(v) DOCTEST_UNARY_ASSERT(DT_CHECK_UNARY, v)
  2206. #define DOCTEST_REQUIRE_UNARY(v) DOCTEST_UNARY_ASSERT(DT_REQUIRE_UNARY, v)
  2207. #define DOCTEST_WARN_UNARY_FALSE(v) DOCTEST_UNARY_ASSERT(DT_WARN_UNARY_FALSE, v)
  2208. #define DOCTEST_CHECK_UNARY_FALSE(v) DOCTEST_UNARY_ASSERT(DT_CHECK_UNARY_FALSE, v)
  2209. #define DOCTEST_REQUIRE_UNARY_FALSE(v) DOCTEST_UNARY_ASSERT(DT_REQUIRE_UNARY_FALSE, v)
  2210. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2211. #ifndef DOCTEST_CONFIG_SUPER_FAST_ASSERTS
  2212. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2213. #define DOCTEST_FAST_BINARY_ASSERT(assert_type, expr, comparison) \
  2214. do { \
  2215. int _DOCTEST_FAST_RES = doctest::detail::fast_binary_assert< \
  2216. doctest::detail::binaryAssertComparison::comparison>( \
  2217. doctest::detail::assertType::assert_type, __FILE__, __LINE__, \
  2218. DOCTEST_TOSTR(DOCTEST_HANDLE_BRACED_VA_ARGS(expr)), \
  2219. DOCTEST_HANDLE_BRACED_VA_ARGS(expr)); \
  2220. if(_DOCTEST_FAST_RES & doctest::detail::assertAction::dbgbreak) \
  2221. DOCTEST_BREAK_INTO_DEBUGGER(); \
  2222. doctest::detail::fastAssertThrowIfFlagSet(_DOCTEST_FAST_RES); \
  2223. } while((void)0, 0)
  2224. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2225. #define DOCTEST_FAST_BINARY_ASSERT(assert_type, lhs, rhs, comparison) \
  2226. do { \
  2227. int _DOCTEST_FAST_RES = doctest::detail::fast_binary_assert< \
  2228. doctest::detail::binaryAssertComparison::comparison>( \
  2229. doctest::detail::assertType::assert_type, __FILE__, __LINE__, #lhs ", " #rhs, lhs, \
  2230. rhs); \
  2231. if(_DOCTEST_FAST_RES & doctest::detail::assertAction::dbgbreak) \
  2232. DOCTEST_BREAK_INTO_DEBUGGER(); \
  2233. doctest::detail::fastAssertThrowIfFlagSet(_DOCTEST_FAST_RES); \
  2234. } while((void)0, 0)
  2235. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2236. #define DOCTEST_FAST_UNARY_ASSERT(assert_type, expr) \
  2237. do { \
  2238. int _DOCTEST_FAST_RES = doctest::detail::fast_unary_assert( \
  2239. doctest::detail::assertType::assert_type, __FILE__, __LINE__, \
  2240. DOCTEST_TOSTR(DOCTEST_HANDLE_BRACED_VA_ARGS(expr)), \
  2241. DOCTEST_HANDLE_BRACED_VA_ARGS(expr)); \
  2242. if(_DOCTEST_FAST_RES & doctest::detail::assertAction::dbgbreak) \
  2243. DOCTEST_BREAK_INTO_DEBUGGER(); \
  2244. doctest::detail::fastAssertThrowIfFlagSet(_DOCTEST_FAST_RES); \
  2245. } while((void)0, 0)
  2246. #else // DOCTEST_CONFIG_SUPER_FAST_ASSERTS
  2247. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2248. #define DOCTEST_FAST_BINARY_ASSERT(assert_type, expr, comparison) \
  2249. doctest::detail::fast_binary_assert<doctest::detail::binaryAssertComparison::comparison>( \
  2250. doctest::detail::assertType::assert_type, __FILE__, __LINE__, \
  2251. DOCTEST_TOSTR(DOCTEST_HANDLE_BRACED_VA_ARGS(expr)), \
  2252. DOCTEST_HANDLE_BRACED_VA_ARGS(expr))
  2253. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2254. #define DOCTEST_FAST_BINARY_ASSERT(assert_type, lhs, rhs, comparison) \
  2255. doctest::detail::fast_binary_assert<doctest::detail::binaryAssertComparison::comparison>( \
  2256. doctest::detail::assertType::assert_type, __FILE__, __LINE__, #lhs ", " #rhs, lhs, \
  2257. rhs)
  2258. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2259. #define DOCTEST_FAST_UNARY_ASSERT(assert_type, expr) \
  2260. doctest::detail::fast_unary_assert(doctest::detail::assertType::assert_type, __FILE__, \
  2261. __LINE__, \
  2262. DOCTEST_TOSTR(DOCTEST_HANDLE_BRACED_VA_ARGS(expr)), \
  2263. DOCTEST_HANDLE_BRACED_VA_ARGS(expr))
  2264. #endif // DOCTEST_CONFIG_SUPER_FAST_ASSERTS
  2265. // clang-format off
  2266. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2267. #define DOCTEST_FAST_WARN_EQ(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_EQ, (__VA_ARGS__), eq)
  2268. #define DOCTEST_FAST_CHECK_EQ(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_EQ, (__VA_ARGS__), eq)
  2269. #define DOCTEST_FAST_REQUIRE_EQ(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_EQ, (__VA_ARGS__), eq)
  2270. #define DOCTEST_FAST_WARN_NE(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_NE, (__VA_ARGS__), ne)
  2271. #define DOCTEST_FAST_CHECK_NE(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_NE, (__VA_ARGS__), ne)
  2272. #define DOCTEST_FAST_REQUIRE_NE(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_NE, (__VA_ARGS__), ne)
  2273. #define DOCTEST_FAST_WARN_GT(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_GT, (__VA_ARGS__), gt)
  2274. #define DOCTEST_FAST_CHECK_GT(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_GT, (__VA_ARGS__), gt)
  2275. #define DOCTEST_FAST_REQUIRE_GT(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_GT, (__VA_ARGS__), gt)
  2276. #define DOCTEST_FAST_WARN_LT(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_LT, (__VA_ARGS__), lt)
  2277. #define DOCTEST_FAST_CHECK_LT(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_LT, (__VA_ARGS__), lt)
  2278. #define DOCTEST_FAST_REQUIRE_LT(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_LT, (__VA_ARGS__), lt)
  2279. #define DOCTEST_FAST_WARN_GE(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_GE, (__VA_ARGS__), ge)
  2280. #define DOCTEST_FAST_CHECK_GE(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_GE, (__VA_ARGS__), ge)
  2281. #define DOCTEST_FAST_REQUIRE_GE(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_GE, (__VA_ARGS__), ge)
  2282. #define DOCTEST_FAST_WARN_LE(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_LE, (__VA_ARGS__), le)
  2283. #define DOCTEST_FAST_CHECK_LE(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_LE, (__VA_ARGS__), le)
  2284. #define DOCTEST_FAST_REQUIRE_LE(...) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_LE, (__VA_ARGS__), le)
  2285. #define DOCTEST_FAST_WARN_UNARY(...) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_WARN_UNARY, (__VA_ARGS__))
  2286. #define DOCTEST_FAST_CHECK_UNARY(...) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_CHECK_UNARY, (__VA_ARGS__))
  2287. #define DOCTEST_FAST_REQUIRE_UNARY(...) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_REQUIRE_UNARY, (__VA_ARGS__))
  2288. #define DOCTEST_FAST_WARN_UNARY_FALSE(...) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_WARN_UNARY_FALSE, (__VA_ARGS__))
  2289. #define DOCTEST_FAST_CHECK_UNARY_FALSE(...) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_CHECK_UNARY_FALSE, (__VA_ARGS__))
  2290. #define DOCTEST_FAST_REQUIRE_UNARY_FALSE(...) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_REQUIRE_UNARY_FALSE, (__VA_ARGS__))
  2291. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2292. #define DOCTEST_FAST_WARN_EQ(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_EQ, l, r, eq)
  2293. #define DOCTEST_FAST_CHECK_EQ(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_EQ, l, r, eq)
  2294. #define DOCTEST_FAST_REQUIRE_EQ(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_EQ, l, r, eq)
  2295. #define DOCTEST_FAST_WARN_NE(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_NE, l, r, ne)
  2296. #define DOCTEST_FAST_CHECK_NE(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_NE, l, r, ne)
  2297. #define DOCTEST_FAST_REQUIRE_NE(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_NE, l, r, ne)
  2298. #define DOCTEST_FAST_WARN_GT(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_GT, l, r, gt)
  2299. #define DOCTEST_FAST_CHECK_GT(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_GT, l, r, gt)
  2300. #define DOCTEST_FAST_REQUIRE_GT(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_GT, l, r, gt)
  2301. #define DOCTEST_FAST_WARN_LT(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_LT, l, r, lt)
  2302. #define DOCTEST_FAST_CHECK_LT(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_LT, l, r, lt)
  2303. #define DOCTEST_FAST_REQUIRE_LT(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_LT, l, r, lt)
  2304. #define DOCTEST_FAST_WARN_GE(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_GE, l, r, ge)
  2305. #define DOCTEST_FAST_CHECK_GE(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_GE, l, r, ge)
  2306. #define DOCTEST_FAST_REQUIRE_GE(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_GE, l, r, ge)
  2307. #define DOCTEST_FAST_WARN_LE(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_WARN_LE, l, r, le)
  2308. #define DOCTEST_FAST_CHECK_LE(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_CHECK_LE, l, r, le)
  2309. #define DOCTEST_FAST_REQUIRE_LE(l, r) DOCTEST_FAST_BINARY_ASSERT(DT_FAST_REQUIRE_LE, l, r, le)
  2310. #define DOCTEST_FAST_WARN_UNARY(v) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_WARN_UNARY, v)
  2311. #define DOCTEST_FAST_CHECK_UNARY(v) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_CHECK_UNARY, v)
  2312. #define DOCTEST_FAST_REQUIRE_UNARY(v) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_REQUIRE_UNARY, v)
  2313. #define DOCTEST_FAST_WARN_UNARY_FALSE(v) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_WARN_UNARY_FALSE, v)
  2314. #define DOCTEST_FAST_CHECK_UNARY_FALSE(v) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_CHECK_UNARY_FALSE, v)
  2315. #define DOCTEST_FAST_REQUIRE_UNARY_FALSE(v) DOCTEST_FAST_UNARY_ASSERT(DT_FAST_REQUIRE_UNARY_FALSE, v)
  2316. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2317. // clang-format on
  2318. #ifdef DOCTEST_CONFIG_NO_EXCEPTIONS
  2319. #undef DOCTEST_WARN_THROWS
  2320. #undef DOCTEST_CHECK_THROWS
  2321. #undef DOCTEST_REQUIRE_THROWS
  2322. #undef DOCTEST_WARN_THROWS_AS
  2323. #undef DOCTEST_CHECK_THROWS_AS
  2324. #undef DOCTEST_REQUIRE_THROWS_AS
  2325. #undef DOCTEST_WARN_NOTHROW
  2326. #undef DOCTEST_CHECK_NOTHROW
  2327. #undef DOCTEST_REQUIRE_NOTHROW
  2328. #undef DOCTEST_WARN_THROWS_MESSAGE
  2329. #undef DOCTEST_CHECK_THROWS_MESSAGE
  2330. #undef DOCTEST_REQUIRE_THROWS_MESSAGE
  2331. #undef DOCTEST_WARN_THROWS_AS_MESSAGE
  2332. #undef DOCTEST_CHECK_THROWS_AS_MESSAGE
  2333. #undef DOCTEST_REQUIRE_THROWS_AS_MESSAGE
  2334. #undef DOCTEST_WARN_NOTHROW_MESSAGE
  2335. #undef DOCTEST_CHECK_NOTHROW_MESSAGE
  2336. #undef DOCTEST_REQUIRE_NOTHROW_MESSAGE
  2337. #ifdef DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS
  2338. #define DOCTEST_WARN_THROWS(expr) ((void)0)
  2339. #define DOCTEST_CHECK_THROWS(expr) ((void)0)
  2340. #define DOCTEST_REQUIRE_THROWS(expr) ((void)0)
  2341. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2342. #define DOCTEST_WARN_THROWS_AS(expr, ...) ((void)0)
  2343. #define DOCTEST_CHECK_THROWS_AS(expr, ...) ((void)0)
  2344. #define DOCTEST_REQUIRE_THROWS_AS(expr, ...) ((void)0)
  2345. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2346. #define DOCTEST_WARN_THROWS_AS(expr, ex) ((void)0)
  2347. #define DOCTEST_CHECK_THROWS_AS(expr, ex) ((void)0)
  2348. #define DOCTEST_REQUIRE_THROWS_AS(expr, ex) ((void)0)
  2349. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2350. #define DOCTEST_WARN_NOTHROW(expr) ((void)0)
  2351. #define DOCTEST_CHECK_NOTHROW(expr) ((void)0)
  2352. #define DOCTEST_REQUIRE_NOTHROW(expr) ((void)0)
  2353. #define DOCTEST_WARN_THROWS_MESSAGE(expr, msg) ((void)0)
  2354. #define DOCTEST_CHECK_THROWS_MESSAGE(expr, msg) ((void)0)
  2355. #define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, msg) ((void)0)
  2356. #define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0)
  2357. #define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0)
  2358. #define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0)
  2359. #define DOCTEST_WARN_NOTHROW_MESSAGE(expr, msg) ((void)0)
  2360. #define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, msg) ((void)0)
  2361. #define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, msg) ((void)0)
  2362. #else // DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS
  2363. #undef DOCTEST_REQUIRE
  2364. #undef DOCTEST_REQUIRE_FALSE
  2365. #undef DOCTEST_REQUIRE_MESSAGE
  2366. #undef DOCTEST_REQUIRE_FALSE_MESSAGE
  2367. #undef DOCTEST_REQUIRE_EQ
  2368. #undef DOCTEST_REQUIRE_NE
  2369. #undef DOCTEST_REQUIRE_GT
  2370. #undef DOCTEST_REQUIRE_LT
  2371. #undef DOCTEST_REQUIRE_GE
  2372. #undef DOCTEST_REQUIRE_LE
  2373. #undef DOCTEST_REQUIRE_UNARY
  2374. #undef DOCTEST_REQUIRE_UNARY_FALSE
  2375. #undef DOCTEST_FAST_REQUIRE_EQ
  2376. #undef DOCTEST_FAST_REQUIRE_NE
  2377. #undef DOCTEST_FAST_REQUIRE_GT
  2378. #undef DOCTEST_FAST_REQUIRE_LT
  2379. #undef DOCTEST_FAST_REQUIRE_GE
  2380. #undef DOCTEST_FAST_REQUIRE_LE
  2381. #undef DOCTEST_FAST_REQUIRE_UNARY
  2382. #undef DOCTEST_FAST_REQUIRE_UNARY_FALSE
  2383. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS
  2384. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  2385. // =================================================================================================
  2386. // == WHAT FOLLOWS IS VERSIONS OF THE MACROS THAT DO NOT DO ANY REGISTERING! ==
  2387. // == THIS CAN BE ENABLED BY DEFINING DOCTEST_CONFIG_DISABLE GLOBALLY! ==
  2388. // =================================================================================================
  2389. #else // DOCTEST_CONFIG_DISABLE
  2390. #define DOCTEST_IMPLEMENT_FIXTURE(der, base, func, name) \
  2391. namespace \
  2392. { \
  2393. template <typename T> \
  2394. struct der : base \
  2395. { void f(); }; \
  2396. } \
  2397. template <typename T> \
  2398. inline void der<T>::f()
  2399. #define DOCTEST_CREATE_AND_REGISTER_FUNCTION(f, name) \
  2400. template <typename T> \
  2401. static inline void f()
  2402. // for registering tests
  2403. #define DOCTEST_TEST_CASE(name) \
  2404. DOCTEST_CREATE_AND_REGISTER_FUNCTION(DOCTEST_ANONYMOUS(_DOCTEST_ANON_FUNC_), name)
  2405. // for registering tests with a fixture
  2406. #define DOCTEST_TEST_CASE_FIXTURE(x, name) \
  2407. DOCTEST_IMPLEMENT_FIXTURE(DOCTEST_ANONYMOUS(_DOCTEST_ANON_CLASS_), x, \
  2408. DOCTEST_ANONYMOUS(_DOCTEST_ANON_FUNC_), name)
  2409. // for converting types to strings without the <typeinfo> header and demangling
  2410. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2411. #define DOCTEST_TYPE_TO_STRING(...) typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_)
  2412. #define DOCTEST_TYPE_TO_STRING_IMPL(...)
  2413. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2414. #define DOCTEST_TYPE_TO_STRING(x) typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_)
  2415. #define DOCTEST_TYPE_TO_STRING_IMPL(x)
  2416. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2417. // for typed tests
  2418. #define DOCTEST_TEST_CASE_TEMPLATE(name, type, types) \
  2419. template <typename type> \
  2420. inline void DOCTEST_ANONYMOUS(_DOCTEST_ANON_TMP_)()
  2421. #define DOCTEST_TEST_CASE_TEMPLATE_DEFINE(name, type, id) \
  2422. template <typename type> \
  2423. inline void DOCTEST_ANONYMOUS(_DOCTEST_ANON_TMP_)()
  2424. #define DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE(id, types) \
  2425. typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_)
  2426. // for subcases
  2427. #define DOCTEST_SUBCASE(name)
  2428. // for a testsuite block
  2429. #define DOCTEST_TEST_SUITE(name) namespace
  2430. // for starting a testsuite block
  2431. #define DOCTEST_TEST_SUITE_BEGIN(name) typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_)
  2432. // for ending a testsuite block
  2433. #define DOCTEST_TEST_SUITE_END typedef int DOCTEST_ANONYMOUS(_DOCTEST_ANON_FOR_SEMICOLON_)
  2434. #define DOCTEST_REGISTER_EXCEPTION_TRANSLATOR(signature) \
  2435. template <typename T> \
  2436. static inline doctest::String DOCTEST_ANONYMOUS(_DOCTEST_ANON_TRANSLATOR_)(signature)
  2437. #define DOCTEST_INFO(x) ((void)0)
  2438. #define DOCTEST_CAPTURE(x) ((void)0)
  2439. #define DOCTEST_ADD_MESSAGE_AT(file, line, x) ((void)0)
  2440. #define DOCTEST_ADD_FAIL_CHECK_AT(file, line, x) ((void)0)
  2441. #define DOCTEST_ADD_FAIL_AT(file, line, x) ((void)0)
  2442. #define DOCTEST_MESSAGE(x) ((void)0)
  2443. #define DOCTEST_FAIL_CHECK(x) ((void)0)
  2444. #define DOCTEST_FAIL(x) ((void)0)
  2445. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2446. #define DOCTEST_WARN(...) ((void)0)
  2447. #define DOCTEST_CHECK(...) ((void)0)
  2448. #define DOCTEST_REQUIRE(...) ((void)0)
  2449. #define DOCTEST_WARN_FALSE(...) ((void)0)
  2450. #define DOCTEST_CHECK_FALSE(...) ((void)0)
  2451. #define DOCTEST_REQUIRE_FALSE(...) ((void)0)
  2452. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2453. #define DOCTEST_WARN(expr) ((void)0)
  2454. #define DOCTEST_CHECK(expr) ((void)0)
  2455. #define DOCTEST_REQUIRE(expr) ((void)0)
  2456. #define DOCTEST_WARN_FALSE(expr) ((void)0)
  2457. #define DOCTEST_CHECK_FALSE(expr) ((void)0)
  2458. #define DOCTEST_REQUIRE_FALSE(expr) ((void)0)
  2459. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2460. #define DOCTEST_WARN_MESSAGE(cond, msg) ((void)0)
  2461. #define DOCTEST_CHECK_MESSAGE(cond, msg) ((void)0)
  2462. #define DOCTEST_REQUIRE_MESSAGE(cond, msg) ((void)0)
  2463. #define DOCTEST_WARN_FALSE_MESSAGE(cond, msg) ((void)0)
  2464. #define DOCTEST_CHECK_FALSE_MESSAGE(cond, msg) ((void)0)
  2465. #define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, msg) ((void)0)
  2466. #define DOCTEST_WARN_THROWS(expr) ((void)0)
  2467. #define DOCTEST_CHECK_THROWS(expr) ((void)0)
  2468. #define DOCTEST_REQUIRE_THROWS(expr) ((void)0)
  2469. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2470. #define DOCTEST_WARN_THROWS_AS(expr, ...) ((void)0)
  2471. #define DOCTEST_CHECK_THROWS_AS(expr, ...) ((void)0)
  2472. #define DOCTEST_REQUIRE_THROWS_AS(expr, ...) ((void)0)
  2473. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2474. #define DOCTEST_WARN_THROWS_AS(expr, ex) ((void)0)
  2475. #define DOCTEST_CHECK_THROWS_AS(expr, ex) ((void)0)
  2476. #define DOCTEST_REQUIRE_THROWS_AS(expr, ex) ((void)0)
  2477. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2478. #define DOCTEST_WARN_NOTHROW(expr) ((void)0)
  2479. #define DOCTEST_CHECK_NOTHROW(expr) ((void)0)
  2480. #define DOCTEST_REQUIRE_NOTHROW(expr) ((void)0)
  2481. #define DOCTEST_WARN_THROWS_MESSAGE(expr, msg) ((void)0)
  2482. #define DOCTEST_CHECK_THROWS_MESSAGE(expr, msg) ((void)0)
  2483. #define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, msg) ((void)0)
  2484. #define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0)
  2485. #define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0)
  2486. #define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0)
  2487. #define DOCTEST_WARN_NOTHROW_MESSAGE(expr, msg) ((void)0)
  2488. #define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, msg) ((void)0)
  2489. #define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, msg) ((void)0)
  2490. #ifdef DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2491. #define DOCTEST_WARN_EQ(...) ((void)0)
  2492. #define DOCTEST_CHECK_EQ(...) ((void)0)
  2493. #define DOCTEST_REQUIRE_EQ(...) ((void)0)
  2494. #define DOCTEST_WARN_NE(...) ((void)0)
  2495. #define DOCTEST_CHECK_NE(...) ((void)0)
  2496. #define DOCTEST_REQUIRE_NE(...) ((void)0)
  2497. #define DOCTEST_WARN_GT(...) ((void)0)
  2498. #define DOCTEST_CHECK_GT(...) ((void)0)
  2499. #define DOCTEST_REQUIRE_GT(...) ((void)0)
  2500. #define DOCTEST_WARN_LT(...) ((void)0)
  2501. #define DOCTEST_CHECK_LT(...) ((void)0)
  2502. #define DOCTEST_REQUIRE_LT(...) ((void)0)
  2503. #define DOCTEST_WARN_GE(...) ((void)0)
  2504. #define DOCTEST_CHECK_GE(...) ((void)0)
  2505. #define DOCTEST_REQUIRE_GE(...) ((void)0)
  2506. #define DOCTEST_WARN_LE(...) ((void)0)
  2507. #define DOCTEST_CHECK_LE(...) ((void)0)
  2508. #define DOCTEST_REQUIRE_LE(...) ((void)0)
  2509. #define DOCTEST_WARN_UNARY(...) ((void)0)
  2510. #define DOCTEST_CHECK_UNARY(...) ((void)0)
  2511. #define DOCTEST_REQUIRE_UNARY(...) ((void)0)
  2512. #define DOCTEST_WARN_UNARY_FALSE(...) ((void)0)
  2513. #define DOCTEST_CHECK_UNARY_FALSE(...) ((void)0)
  2514. #define DOCTEST_REQUIRE_UNARY_FALSE(...) ((void)0)
  2515. #define DOCTEST_FAST_WARN_EQ(...) ((void)0)
  2516. #define DOCTEST_FAST_CHECK_EQ(...) ((void)0)
  2517. #define DOCTEST_FAST_REQUIRE_EQ(...) ((void)0)
  2518. #define DOCTEST_FAST_WARN_NE(...) ((void)0)
  2519. #define DOCTEST_FAST_CHECK_NE(...) ((void)0)
  2520. #define DOCTEST_FAST_REQUIRE_NE(...) ((void)0)
  2521. #define DOCTEST_FAST_WARN_GT(...) ((void)0)
  2522. #define DOCTEST_FAST_CHECK_GT(...) ((void)0)
  2523. #define DOCTEST_FAST_REQUIRE_GT(...) ((void)0)
  2524. #define DOCTEST_FAST_WARN_LT(...) ((void)0)
  2525. #define DOCTEST_FAST_CHECK_LT(...) ((void)0)
  2526. #define DOCTEST_FAST_REQUIRE_LT(...) ((void)0)
  2527. #define DOCTEST_FAST_WARN_GE(...) ((void)0)
  2528. #define DOCTEST_FAST_CHECK_GE(...) ((void)0)
  2529. #define DOCTEST_FAST_REQUIRE_GE(...) ((void)0)
  2530. #define DOCTEST_FAST_WARN_LE(...) ((void)0)
  2531. #define DOCTEST_FAST_CHECK_LE(...) ((void)0)
  2532. #define DOCTEST_FAST_REQUIRE_LE(...) ((void)0)
  2533. #define DOCTEST_FAST_WARN_UNARY(...) ((void)0)
  2534. #define DOCTEST_FAST_CHECK_UNARY(...) ((void)0)
  2535. #define DOCTEST_FAST_REQUIRE_UNARY(...) ((void)0)
  2536. #define DOCTEST_FAST_WARN_UNARY_FALSE(...) ((void)0)
  2537. #define DOCTEST_FAST_CHECK_UNARY_FALSE(...) ((void)0)
  2538. #define DOCTEST_FAST_REQUIRE_UNARY_FALSE(...) ((void)0)
  2539. #else // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2540. #define DOCTEST_WARN_EQ(lhs, rhs) ((void)0)
  2541. #define DOCTEST_CHECK_EQ(lhs, rhs) ((void)0)
  2542. #define DOCTEST_REQUIRE_EQ(lhs, rhs) ((void)0)
  2543. #define DOCTEST_WARN_NE(lhs, rhs) ((void)0)
  2544. #define DOCTEST_CHECK_NE(lhs, rhs) ((void)0)
  2545. #define DOCTEST_REQUIRE_NE(lhs, rhs) ((void)0)
  2546. #define DOCTEST_WARN_GT(lhs, rhs) ((void)0)
  2547. #define DOCTEST_CHECK_GT(lhs, rhs) ((void)0)
  2548. #define DOCTEST_REQUIRE_GT(lhs, rhs) ((void)0)
  2549. #define DOCTEST_WARN_LT(lhs, rhs) ((void)0)
  2550. #define DOCTEST_CHECK_LT(lhs, rhs) ((void)0)
  2551. #define DOCTEST_REQUIRE_LT(lhs, rhs) ((void)0)
  2552. #define DOCTEST_WARN_GE(lhs, rhs) ((void)0)
  2553. #define DOCTEST_CHECK_GE(lhs, rhs) ((void)0)
  2554. #define DOCTEST_REQUIRE_GE(lhs, rhs) ((void)0)
  2555. #define DOCTEST_WARN_LE(lhs, rhs) ((void)0)
  2556. #define DOCTEST_CHECK_LE(lhs, rhs) ((void)0)
  2557. #define DOCTEST_REQUIRE_LE(lhs, rhs) ((void)0)
  2558. #define DOCTEST_WARN_UNARY(val) ((void)0)
  2559. #define DOCTEST_CHECK_UNARY(val) ((void)0)
  2560. #define DOCTEST_REQUIRE_UNARY(val) ((void)0)
  2561. #define DOCTEST_WARN_UNARY_FALSE(val) ((void)0)
  2562. #define DOCTEST_CHECK_UNARY_FALSE(val) ((void)0)
  2563. #define DOCTEST_REQUIRE_UNARY_FALSE(val) ((void)0)
  2564. #define DOCTEST_FAST_WARN_EQ(lhs, rhs) ((void)0)
  2565. #define DOCTEST_FAST_CHECK_EQ(lhs, rhs) ((void)0)
  2566. #define DOCTEST_FAST_REQUIRE_EQ(lhs, rhs) ((void)0)
  2567. #define DOCTEST_FAST_WARN_NE(lhs, rhs) ((void)0)
  2568. #define DOCTEST_FAST_CHECK_NE(lhs, rhs) ((void)0)
  2569. #define DOCTEST_FAST_REQUIRE_NE(lhs, rhs) ((void)0)
  2570. #define DOCTEST_FAST_WARN_GT(lhs, rhs) ((void)0)
  2571. #define DOCTEST_FAST_CHECK_GT(lhs, rhs) ((void)0)
  2572. #define DOCTEST_FAST_REQUIRE_GT(lhs, rhs) ((void)0)
  2573. #define DOCTEST_FAST_WARN_LT(lhs, rhs) ((void)0)
  2574. #define DOCTEST_FAST_CHECK_LT(lhs, rhs) ((void)0)
  2575. #define DOCTEST_FAST_REQUIRE_LT(lhs, rhs) ((void)0)
  2576. #define DOCTEST_FAST_WARN_GE(lhs, rhs) ((void)0)
  2577. #define DOCTEST_FAST_CHECK_GE(lhs, rhs) ((void)0)
  2578. #define DOCTEST_FAST_REQUIRE_GE(lhs, rhs) ((void)0)
  2579. #define DOCTEST_FAST_WARN_LE(lhs, rhs) ((void)0)
  2580. #define DOCTEST_FAST_CHECK_LE(lhs, rhs) ((void)0)
  2581. #define DOCTEST_FAST_REQUIRE_LE(lhs, rhs) ((void)0)
  2582. #define DOCTEST_FAST_WARN_UNARY(val) ((void)0)
  2583. #define DOCTEST_FAST_CHECK_UNARY(val) ((void)0)
  2584. #define DOCTEST_FAST_REQUIRE_UNARY(val) ((void)0)
  2585. #define DOCTEST_FAST_WARN_UNARY_FALSE(val) ((void)0)
  2586. #define DOCTEST_FAST_CHECK_UNARY_FALSE(val) ((void)0)
  2587. #define DOCTEST_FAST_REQUIRE_UNARY_FALSE(val) ((void)0)
  2588. #endif // DOCTEST_CONFIG_WITH_VARIADIC_MACROS
  2589. #endif // DOCTEST_CONFIG_DISABLE
  2590. // BDD style macros
  2591. // clang-format off
  2592. #define DOCTEST_SCENARIO(name) TEST_CASE(" Scenario: " name)
  2593. #define DOCTEST_GIVEN(name) SUBCASE(" Given: " name)
  2594. #define DOCTEST_WHEN(name) SUBCASE(" When: " name)
  2595. #define DOCTEST_AND_WHEN(name) SUBCASE("And when: " name)
  2596. #define DOCTEST_THEN(name) SUBCASE(" Then: " name)
  2597. #define DOCTEST_AND_THEN(name) SUBCASE(" And: " name)
  2598. // clang-format on
  2599. // == SHORT VERSIONS OF THE MACROS
  2600. #if !defined(DOCTEST_CONFIG_NO_SHORT_MACRO_NAMES)
  2601. #define TEST_CASE DOCTEST_TEST_CASE
  2602. #define TEST_CASE_FIXTURE DOCTEST_TEST_CASE_FIXTURE
  2603. #define TYPE_TO_STRING DOCTEST_TYPE_TO_STRING
  2604. #define TEST_CASE_TEMPLATE DOCTEST_TEST_CASE_TEMPLATE
  2605. #define TEST_CASE_TEMPLATE_DEFINE DOCTEST_TEST_CASE_TEMPLATE_DEFINE
  2606. #define TEST_CASE_TEMPLATE_INSTANTIATE DOCTEST_TEST_CASE_TEMPLATE_INSTANTIATE
  2607. #define SUBCASE DOCTEST_SUBCASE
  2608. #define TEST_SUITE DOCTEST_TEST_SUITE
  2609. #define TEST_SUITE_BEGIN DOCTEST_TEST_SUITE_BEGIN
  2610. #define TEST_SUITE_END DOCTEST_TEST_SUITE_END
  2611. #define REGISTER_EXCEPTION_TRANSLATOR DOCTEST_REGISTER_EXCEPTION_TRANSLATOR
  2612. #define INFO DOCTEST_INFO
  2613. #define CAPTURE DOCTEST_CAPTURE
  2614. #define ADD_MESSAGE_AT DOCTEST_ADD_MESSAGE_AT
  2615. #define ADD_FAIL_CHECK_AT DOCTEST_ADD_FAIL_CHECK_AT
  2616. #define ADD_FAIL_AT DOCTEST_ADD_FAIL_AT
  2617. #define MESSAGE DOCTEST_MESSAGE
  2618. #define FAIL_CHECK DOCTEST_FAIL_CHECK
  2619. #define FAIL DOCTEST_FAIL
  2620. #define TO_LVALUE DOCTEST_TO_LVALUE
  2621. #define WARN DOCTEST_WARN
  2622. #define WARN_FALSE DOCTEST_WARN_FALSE
  2623. #define WARN_THROWS DOCTEST_WARN_THROWS
  2624. #define WARN_THROWS_AS DOCTEST_WARN_THROWS_AS
  2625. #define WARN_NOTHROW DOCTEST_WARN_NOTHROW
  2626. #define CHECK DOCTEST_CHECK
  2627. #define CHECK_FALSE DOCTEST_CHECK_FALSE
  2628. #define CHECK_THROWS DOCTEST_CHECK_THROWS
  2629. #define CHECK_THROWS_AS DOCTEST_CHECK_THROWS_AS
  2630. #define CHECK_NOTHROW DOCTEST_CHECK_NOTHROW
  2631. #define REQUIRE DOCTEST_REQUIRE
  2632. #define REQUIRE_FALSE DOCTEST_REQUIRE_FALSE
  2633. #define REQUIRE_THROWS DOCTEST_REQUIRE_THROWS
  2634. #define REQUIRE_THROWS_AS DOCTEST_REQUIRE_THROWS_AS
  2635. #define REQUIRE_NOTHROW DOCTEST_REQUIRE_NOTHROW
  2636. #define WARN_MESSAGE DOCTEST_WARN_MESSAGE
  2637. #define WARN_FALSE_MESSAGE DOCTEST_WARN_FALSE_MESSAGE
  2638. #define WARN_THROWS_MESSAGE DOCTEST_WARN_THROWS_MESSAGE
  2639. #define WARN_THROWS_AS_MESSAGE DOCTEST_WARN_THROWS_AS_MESSAGE
  2640. #define WARN_NOTHROW_MESSAGE DOCTEST_WARN_NOTHROW_MESSAGE
  2641. #define CHECK_MESSAGE DOCTEST_CHECK_MESSAGE
  2642. #define CHECK_FALSE_MESSAGE DOCTEST_CHECK_FALSE_MESSAGE
  2643. #define CHECK_THROWS_MESSAGE DOCTEST_CHECK_THROWS_MESSAGE
  2644. #define CHECK_THROWS_AS_MESSAGE DOCTEST_CHECK_THROWS_AS_MESSAGE
  2645. #define CHECK_NOTHROW_MESSAGE DOCTEST_CHECK_NOTHROW_MESSAGE
  2646. #define REQUIRE_MESSAGE DOCTEST_REQUIRE_MESSAGE
  2647. #define REQUIRE_FALSE_MESSAGE DOCTEST_REQUIRE_FALSE_MESSAGE
  2648. #define REQUIRE_THROWS_MESSAGE DOCTEST_REQUIRE_THROWS_MESSAGE
  2649. #define REQUIRE_THROWS_AS_MESSAGE DOCTEST_REQUIRE_THROWS_AS_MESSAGE
  2650. #define REQUIRE_NOTHROW_MESSAGE DOCTEST_REQUIRE_NOTHROW_MESSAGE
  2651. #define SCENARIO DOCTEST_SCENARIO
  2652. #define GIVEN DOCTEST_GIVEN
  2653. #define WHEN DOCTEST_WHEN
  2654. #define AND_WHEN DOCTEST_AND_WHEN
  2655. #define THEN DOCTEST_THEN
  2656. #define AND_THEN DOCTEST_AND_THEN
  2657. #define WARN_EQ DOCTEST_WARN_EQ
  2658. #define CHECK_EQ DOCTEST_CHECK_EQ
  2659. #define REQUIRE_EQ DOCTEST_REQUIRE_EQ
  2660. #define WARN_NE DOCTEST_WARN_NE
  2661. #define CHECK_NE DOCTEST_CHECK_NE
  2662. #define REQUIRE_NE DOCTEST_REQUIRE_NE
  2663. #define WARN_GT DOCTEST_WARN_GT
  2664. #define CHECK_GT DOCTEST_CHECK_GT
  2665. #define REQUIRE_GT DOCTEST_REQUIRE_GT
  2666. #define WARN_LT DOCTEST_WARN_LT
  2667. #define CHECK_LT DOCTEST_CHECK_LT
  2668. #define REQUIRE_LT DOCTEST_REQUIRE_LT
  2669. #define WARN_GE DOCTEST_WARN_GE
  2670. #define CHECK_GE DOCTEST_CHECK_GE
  2671. #define REQUIRE_GE DOCTEST_REQUIRE_GE
  2672. #define WARN_LE DOCTEST_WARN_LE
  2673. #define CHECK_LE DOCTEST_CHECK_LE
  2674. #define REQUIRE_LE DOCTEST_REQUIRE_LE
  2675. #define WARN_UNARY DOCTEST_WARN_UNARY
  2676. #define CHECK_UNARY DOCTEST_CHECK_UNARY
  2677. #define REQUIRE_UNARY DOCTEST_REQUIRE_UNARY
  2678. #define WARN_UNARY_FALSE DOCTEST_WARN_UNARY_FALSE
  2679. #define CHECK_UNARY_FALSE DOCTEST_CHECK_UNARY_FALSE
  2680. #define REQUIRE_UNARY_FALSE DOCTEST_REQUIRE_UNARY_FALSE
  2681. #define FAST_WARN_EQ DOCTEST_FAST_WARN_EQ
  2682. #define FAST_CHECK_EQ DOCTEST_FAST_CHECK_EQ
  2683. #define FAST_REQUIRE_EQ DOCTEST_FAST_REQUIRE_EQ
  2684. #define FAST_WARN_NE DOCTEST_FAST_WARN_NE
  2685. #define FAST_CHECK_NE DOCTEST_FAST_CHECK_NE
  2686. #define FAST_REQUIRE_NE DOCTEST_FAST_REQUIRE_NE
  2687. #define FAST_WARN_GT DOCTEST_FAST_WARN_GT
  2688. #define FAST_CHECK_GT DOCTEST_FAST_CHECK_GT
  2689. #define FAST_REQUIRE_GT DOCTEST_FAST_REQUIRE_GT
  2690. #define FAST_WARN_LT DOCTEST_FAST_WARN_LT
  2691. #define FAST_CHECK_LT DOCTEST_FAST_CHECK_LT
  2692. #define FAST_REQUIRE_LT DOCTEST_FAST_REQUIRE_LT
  2693. #define FAST_WARN_GE DOCTEST_FAST_WARN_GE
  2694. #define FAST_CHECK_GE DOCTEST_FAST_CHECK_GE
  2695. #define FAST_REQUIRE_GE DOCTEST_FAST_REQUIRE_GE
  2696. #define FAST_WARN_LE DOCTEST_FAST_WARN_LE
  2697. #define FAST_CHECK_LE DOCTEST_FAST_CHECK_LE
  2698. #define FAST_REQUIRE_LE DOCTEST_FAST_REQUIRE_LE
  2699. #define FAST_WARN_UNARY DOCTEST_FAST_WARN_UNARY
  2700. #define FAST_CHECK_UNARY DOCTEST_FAST_CHECK_UNARY
  2701. #define FAST_REQUIRE_UNARY DOCTEST_FAST_REQUIRE_UNARY
  2702. #define FAST_WARN_UNARY_FALSE DOCTEST_FAST_WARN_UNARY_FALSE
  2703. #define FAST_CHECK_UNARY_FALSE DOCTEST_FAST_CHECK_UNARY_FALSE
  2704. #define FAST_REQUIRE_UNARY_FALSE DOCTEST_FAST_REQUIRE_UNARY_FALSE
  2705. #endif // DOCTEST_CONFIG_NO_SHORT_MACRO_NAMES
  2706. // this is here to clear the 'current test suite' for the current translation unit - at the top
  2707. DOCTEST_TEST_SUITE_END();
  2708. // add stringification for primitive/fundamental types
  2709. namespace doctest
  2710. {
  2711. namespace detail
  2712. {
  2713. DOCTEST_TYPE_TO_STRING_IMPL(bool)
  2714. DOCTEST_TYPE_TO_STRING_IMPL(float)
  2715. DOCTEST_TYPE_TO_STRING_IMPL(double)
  2716. DOCTEST_TYPE_TO_STRING_IMPL(long double)
  2717. DOCTEST_TYPE_TO_STRING_IMPL(char)
  2718. DOCTEST_TYPE_TO_STRING_IMPL(signed char)
  2719. DOCTEST_TYPE_TO_STRING_IMPL(unsigned char)
  2720. DOCTEST_TYPE_TO_STRING_IMPL(wchar_t)
  2721. DOCTEST_TYPE_TO_STRING_IMPL(short int)
  2722. DOCTEST_TYPE_TO_STRING_IMPL(unsigned short int)
  2723. DOCTEST_TYPE_TO_STRING_IMPL(int)
  2724. DOCTEST_TYPE_TO_STRING_IMPL(unsigned int)
  2725. DOCTEST_TYPE_TO_STRING_IMPL(long int)
  2726. DOCTEST_TYPE_TO_STRING_IMPL(unsigned long int)
  2727. #ifdef DOCTEST_CONFIG_WITH_LONG_LONG
  2728. DOCTEST_TYPE_TO_STRING_IMPL(long long int)
  2729. DOCTEST_TYPE_TO_STRING_IMPL(unsigned long long int)
  2730. #endif // DOCTEST_CONFIG_WITH_LONG_LONG
  2731. } // namespace detail
  2732. } // namespace doctest
  2733. #endif // DOCTEST_LIBRARY_INCLUDED
  2734. #if defined(__clang__)
  2735. #pragma clang diagnostic pop
  2736. #endif // __clang__
  2737. #if defined(__GNUC__) && !defined(__clang__)
  2738. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)
  2739. #pragma GCC diagnostic pop
  2740. #endif // > gcc 4.6
  2741. #endif // __GNUC__
  2742. #ifdef _MSC_VER
  2743. #pragma warning(pop)
  2744. #endif // _MSC_VER
  2745. #ifndef DOCTEST_SINGLE_HEADER
  2746. #define DOCTEST_SINGLE_HEADER
  2747. #endif // DOCTEST_SINGLE_HEADER
  2748. #if defined(__clang__)
  2749. #pragma clang diagnostic push
  2750. #pragma clang diagnostic ignored "-Wunknown-pragmas"
  2751. #pragma clang diagnostic ignored "-Wpadded"
  2752. #pragma clang diagnostic ignored "-Wglobal-constructors"
  2753. #pragma clang diagnostic ignored "-Wexit-time-destructors"
  2754. #pragma clang diagnostic ignored "-Wmissing-prototypes"
  2755. #pragma clang diagnostic ignored "-Wsign-conversion"
  2756. #pragma clang diagnostic ignored "-Wshorten-64-to-32"
  2757. #pragma clang diagnostic ignored "-Wmissing-variable-declarations"
  2758. #pragma clang diagnostic ignored "-Wswitch"
  2759. #pragma clang diagnostic ignored "-Wswitch-enum"
  2760. #pragma clang diagnostic ignored "-Wcovered-switch-default"
  2761. #pragma clang diagnostic ignored "-Wmissing-noreturn"
  2762. #pragma clang diagnostic ignored "-Wunused-local-typedef"
  2763. #pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
  2764. #pragma clang diagnostic ignored "-Wmissing-braces"
  2765. #pragma clang diagnostic ignored "-Wmissing-field-initializers"
  2766. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
  2767. #pragma clang diagnostic ignored "-Wc++11-long-long"
  2768. #endif // __clang__
  2769. #if defined(__GNUC__) && !defined(__clang__)
  2770. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)
  2771. #pragma GCC diagnostic push
  2772. #endif // > gcc 4.6
  2773. #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  2774. #pragma GCC diagnostic ignored "-Wconversion"
  2775. #pragma GCC diagnostic ignored "-Weffc++"
  2776. #pragma GCC diagnostic ignored "-Wsign-conversion"
  2777. #pragma GCC diagnostic ignored "-Wstrict-overflow"
  2778. #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  2779. #pragma GCC diagnostic ignored "-Wmissing-braces"
  2780. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  2781. #pragma GCC diagnostic ignored "-Winline"
  2782. #pragma GCC diagnostic ignored "-Wswitch"
  2783. #pragma GCC diagnostic ignored "-Wswitch-enum"
  2784. #pragma GCC diagnostic ignored "-Wswitch-default"
  2785. #pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations"
  2786. #pragma GCC diagnostic ignored "-Wlong-long"
  2787. #pragma GCC diagnostic ignored "-Wold-style-cast"
  2788. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)
  2789. #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
  2790. #endif // > gcc 4.6
  2791. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 7)
  2792. #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
  2793. #endif // > gcc 4.7
  2794. #if __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 3)
  2795. #pragma GCC diagnostic ignored "-Wuseless-cast"
  2796. #endif // > gcc 5.3
  2797. #endif // __GNUC__
  2798. #ifdef _MSC_VER
  2799. #pragma warning(push)
  2800. #pragma warning(disable : 4996) // The compiler encountered a deprecated declaration
  2801. #pragma warning(disable : 4267) // 'var' : conversion from 'size_t' to 'type', possible loss of data
  2802. #pragma warning(disable : 4706) // assignment within conditional expression
  2803. #pragma warning(disable : 4512) // 'class' : assignment operator could not be generated
  2804. #pragma warning(disable : 4127) // conditional expression is constant
  2805. #pragma warning(disable : 4530) // C++ exception handler used, but unwind semantics are not enabled
  2806. #pragma warning(disable : 4577) // 'noexcept' used with no exception handling mode specified
  2807. #endif // _MSC_VER
  2808. #if defined(DOCTEST_CONFIG_IMPLEMENT) || !defined(DOCTEST_SINGLE_HEADER)
  2809. #ifndef DOCTEST_LIBRARY_IMPLEMENTATION
  2810. #define DOCTEST_LIBRARY_IMPLEMENTATION
  2811. #ifndef DOCTEST_SINGLE_HEADER
  2812. #include "doctest_fwd.h"
  2813. #endif // DOCTEST_SINGLE_HEADER
  2814. #if defined(__clang__) && defined(DOCTEST_NO_CPP11_COMPAT)
  2815. #pragma clang diagnostic ignored "-Wc++98-compat"
  2816. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  2817. #endif // __clang__ && DOCTEST_NO_CPP11_COMPAT
  2818. // snprintf() not in the C++98 standard
  2819. #ifdef _MSC_VER
  2820. #define DOCTEST_SNPRINTF _snprintf
  2821. #else
  2822. #define DOCTEST_SNPRINTF std::snprintf
  2823. #endif
  2824. #define DOCTEST_LOG_START() \
  2825. do { \
  2826. if(!contextState->hasLoggedCurrentTestStart) { \
  2827. doctest::detail::logTestStart(*contextState->currentTest); \
  2828. contextState->hasLoggedCurrentTestStart = true; \
  2829. } \
  2830. } while(false)
  2831. // required includes - will go only in one translation unit!
  2832. #include <ctime>
  2833. #include <cmath>
  2834. // borland (Embarcadero) compiler requires math.h and not cmath - https://github.com/onqtam/doctest/pull/37
  2835. #ifdef __BORLANDC__
  2836. #include <math.h>
  2837. #endif // __BORLANDC__
  2838. #include <new>
  2839. #include <cstdio>
  2840. #include <cstdlib>
  2841. #include <cstring>
  2842. #include <limits>
  2843. #include <utility>
  2844. #include <sstream>
  2845. #include <iomanip>
  2846. #include <vector>
  2847. #include <set>
  2848. #include <exception>
  2849. #include <stdexcept>
  2850. #include <csignal>
  2851. #include <cfloat>
  2852. #ifndef _MSC_VER
  2853. #include <stdint.h>
  2854. #endif // _MSC_VER
  2855. namespace doctest
  2856. {
  2857. namespace detail
  2858. {
  2859. // lowers ascii letters
  2860. char tolower(const char c) { return (c >= 'A' && c <= 'Z') ? static_cast<char>(c + 32) : c; }
  2861. template <typename T>
  2862. T my_max(const T& lhs, const T& rhs) {
  2863. return lhs > rhs ? lhs : rhs;
  2864. }
  2865. // case insensitive strcmp
  2866. int stricmp(char const* a, char const* b) {
  2867. for(;; a++, b++) {
  2868. int d = tolower(*a) - tolower(*b);
  2869. if(d != 0 || !*a)
  2870. return d;
  2871. }
  2872. }
  2873. void my_memcpy(void* dest, const void* src, unsigned num) {
  2874. const char* csrc = static_cast<const char*>(src);
  2875. char* cdest = static_cast<char*>(dest);
  2876. for(unsigned i = 0; i < num; ++i)
  2877. cdest[i] = csrc[i];
  2878. }
  2879. // not using std::strlen() because of valgrind errors when optimizations are turned on
  2880. // 'Invalid read of size 4' when the test suite len (with '\0') is not a multiple of 4
  2881. // for details see http://stackoverflow.com/questions/35671155
  2882. unsigned my_strlen(const char* in) {
  2883. const char* temp = in;
  2884. while(temp && *temp)
  2885. ++temp;
  2886. return unsigned(temp - in);
  2887. }
  2888. template <typename T>
  2889. String fpToString(T value, int precision) {
  2890. std::ostringstream oss;
  2891. oss << std::setprecision(precision) << std::fixed << value;
  2892. std::string d = oss.str();
  2893. size_t i = d.find_last_not_of('0');
  2894. if(i != std::string::npos && i != d.size() - 1) {
  2895. if(d[i] == '.')
  2896. i++;
  2897. d = d.substr(0, i + 1);
  2898. }
  2899. return d.c_str();
  2900. }
  2901. struct Endianness
  2902. {
  2903. enum Arch
  2904. {
  2905. Big,
  2906. Little
  2907. };
  2908. static Arch which() {
  2909. union _
  2910. {
  2911. int asInt;
  2912. char asChar[sizeof(int)];
  2913. } u;
  2914. u.asInt = 1; // NOLINT
  2915. return (u.asChar[sizeof(int) - 1] == 1) ? Big : Little; // NOLINT
  2916. }
  2917. };
  2918. String rawMemoryToString(const void* object, unsigned size) {
  2919. // Reverse order for little endian architectures
  2920. int i = 0, end = static_cast<int>(size), inc = 1;
  2921. if(Endianness::which() == Endianness::Little) {
  2922. i = end - 1;
  2923. end = inc = -1;
  2924. }
  2925. unsigned char const* bytes = static_cast<unsigned char const*>(object);
  2926. std::ostringstream os;
  2927. os << "0x" << std::setfill('0') << std::hex;
  2928. for(; i != end; i += inc)
  2929. os << std::setw(2) << static_cast<unsigned>(bytes[i]);
  2930. return os.str().c_str();
  2931. }
  2932. std::ostream* createStream() { return new std::ostringstream(); }
  2933. String getStreamResult(std::ostream* in) {
  2934. return static_cast<std::ostringstream*>(in)->str().c_str(); // NOLINT
  2935. }
  2936. void freeStream(std::ostream* in) { delete in; }
  2937. #ifndef DOCTEST_CONFIG_DISABLE
  2938. // this holds both parameters for the command line and runtime data for tests
  2939. struct ContextState : TestAccessibleContextState //!OCLINT too many fields
  2940. {
  2941. // == parameters from the command line
  2942. std::vector<std::vector<String> > filters;
  2943. String order_by; // how tests should be ordered
  2944. unsigned rand_seed; // the seed for rand ordering
  2945. unsigned first; // the first (matching) test to be executed
  2946. unsigned last; // the last (matching) test to be executed
  2947. int abort_after; // stop tests after this many failed assertions
  2948. int subcase_filter_levels; // apply the subcase filters for the first N levels
  2949. bool case_sensitive; // if filtering should be case sensitive
  2950. bool exit; // if the program should be exited after the tests are ran/whatever
  2951. bool duration; // print the time duration of each test case
  2952. bool no_exitcode; // if the framework should return 0 as the exitcode
  2953. bool no_run; // to not run the tests at all (can be done with an "*" exclude)
  2954. bool no_version; // to not print the version of the framework
  2955. bool no_colors; // if output to the console should be colorized
  2956. bool force_colors; // forces the use of colors even when a tty cannot be detected
  2957. bool no_breaks; // to not break into the debugger
  2958. bool no_skip; // don't skip test cases which are marked to be skipped
  2959. bool no_path_in_filenames; // if the path to files should be removed from the output
  2960. bool no_line_numbers; // if source code line numbers should be omitted from the output
  2961. bool no_skipped_summary; // don't print "skipped" in the summary !!! UNDOCUMENTED !!!
  2962. bool help; // to print the help
  2963. bool version; // to print the version
  2964. bool count; // if only the count of matching tests is to be retreived
  2965. bool list_test_cases; // to list all tests matching the filters
  2966. bool list_test_suites; // to list all suites matching the filters
  2967. // == data for the tests being ran
  2968. unsigned numTestsPassingFilters;
  2969. unsigned numTestSuitesPassingFilters;
  2970. unsigned numFailed;
  2971. const TestCase* currentTest;
  2972. bool hasLoggedCurrentTestStart;
  2973. int numAssertionsForCurrentTestcase;
  2974. int numAssertions;
  2975. int numFailedAssertionsForCurrentTestcase;
  2976. int numFailedAssertions;
  2977. bool hasCurrentTestFailed;
  2978. std::vector<IContextScope*> contexts; // for logging with INFO() and friends
  2979. std::vector<std::string> exceptionalContexts; // logging from INFO() due to an exception
  2980. // stuff for subcases
  2981. std::set<SubcaseSignature> subcasesPassed;
  2982. std::set<int> subcasesEnteredLevels;
  2983. std::vector<Subcase> subcasesStack;
  2984. int subcasesCurrentLevel;
  2985. bool subcasesHasSkipped;
  2986. void resetRunData() {
  2987. numTestsPassingFilters = 0;
  2988. numTestSuitesPassingFilters = 0;
  2989. numFailed = 0;
  2990. numAssertions = 0;
  2991. numFailedAssertions = 0;
  2992. numFailedAssertionsForCurrentTestcase = 0;
  2993. }
  2994. // cppcheck-suppress uninitMemberVar
  2995. ContextState()
  2996. : filters(8) // 8 different filters total
  2997. {
  2998. resetRunData();
  2999. }
  3000. };
  3001. ContextState* contextState = 0;
  3002. #endif // DOCTEST_CONFIG_DISABLE
  3003. } // namespace detail
  3004. void String::copy(const String& other) {
  3005. if(other.isOnStack()) {
  3006. detail::my_memcpy(buf, other.buf, len);
  3007. } else {
  3008. setOnHeap();
  3009. data.size = other.data.size;
  3010. data.capacity = data.size + 1;
  3011. data.ptr = new char[data.capacity];
  3012. detail::my_memcpy(data.ptr, other.data.ptr, data.size + 1);
  3013. }
  3014. }
  3015. String::String(const char* in) {
  3016. unsigned in_len = detail::my_strlen(in);
  3017. if(in_len <= last) {
  3018. detail::my_memcpy(buf, in, in_len + 1);
  3019. setLast(last - in_len);
  3020. } else {
  3021. setOnHeap();
  3022. data.size = in_len;
  3023. data.capacity = data.size + 1;
  3024. data.ptr = new char[data.capacity];
  3025. detail::my_memcpy(data.ptr, in, in_len + 1);
  3026. }
  3027. }
  3028. String& String::operator+=(const String& other) {
  3029. unsigned my_old_size = size();
  3030. unsigned other_size = other.size();
  3031. unsigned total_size = my_old_size + other_size;
  3032. if(isOnStack()) {
  3033. if(total_size < len) {
  3034. // append to the current stack space
  3035. detail::my_memcpy(buf + my_old_size, other.c_str(), other_size + 1);
  3036. setLast(last - total_size);
  3037. } else {
  3038. // alloc new chunk
  3039. char* temp = new char[total_size + 1];
  3040. // copy current data to new location before writing in the union
  3041. detail::my_memcpy(temp, buf, my_old_size); // skip the +1 ('\0') for speed
  3042. // update data in union
  3043. setOnHeap();
  3044. data.size = total_size;
  3045. data.capacity = data.size + 1;
  3046. data.ptr = temp;
  3047. // transfer the rest of the data
  3048. detail::my_memcpy(data.ptr + my_old_size, other.c_str(), other_size + 1);
  3049. }
  3050. } else {
  3051. if(data.capacity > total_size) {
  3052. // append to the current heap block
  3053. data.size = total_size;
  3054. detail::my_memcpy(data.ptr + my_old_size, other.c_str(), other_size + 1);
  3055. } else {
  3056. // resize
  3057. data.capacity *= 2;
  3058. if(data.capacity <= total_size)
  3059. data.capacity = total_size + 1;
  3060. // alloc new chunk
  3061. char* temp = new char[data.capacity];
  3062. // copy current data to new location before releasing it
  3063. detail::my_memcpy(temp, data.ptr, my_old_size); // skip the +1 ('\0') for speed
  3064. // release old chunk
  3065. delete[] data.ptr;
  3066. // update the rest of the union members
  3067. data.size = total_size;
  3068. data.ptr = temp;
  3069. // transfer the rest of the data
  3070. detail::my_memcpy(data.ptr + my_old_size, other.c_str(), other_size + 1);
  3071. }
  3072. }
  3073. return *this;
  3074. }
  3075. #ifdef DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  3076. String::String(String&& other) {
  3077. detail::my_memcpy(buf, other.buf, len);
  3078. other.buf[0] = '\0';
  3079. other.setLast();
  3080. }
  3081. String& String::operator=(String&& other) {
  3082. if(!isOnStack())
  3083. delete[] data.ptr;
  3084. detail::my_memcpy(buf, other.buf, len);
  3085. other.buf[0] = '\0';
  3086. other.setLast();
  3087. return *this;
  3088. }
  3089. #endif // DOCTEST_CONFIG_WITH_RVALUE_REFERENCES
  3090. int String::compare(const char* other, bool no_case) const {
  3091. if(no_case)
  3092. return detail::stricmp(c_str(), other);
  3093. return std::strcmp(c_str(), other);
  3094. }
  3095. int String::compare(const String& other, bool no_case) const {
  3096. return compare(other.c_str(), no_case);
  3097. }
  3098. std::ostream& operator<<(std::ostream& stream, const String& in) {
  3099. stream << in.c_str();
  3100. return stream;
  3101. }
  3102. Approx::Approx(double value)
  3103. : m_epsilon(static_cast<double>(std::numeric_limits<float>::epsilon()) * 100)
  3104. , m_scale(1.0)
  3105. , m_value(value) {}
  3106. bool operator==(double lhs, Approx const& rhs) {
  3107. // Thanks to Richard Harris for his help refining this formula
  3108. return std::fabs(lhs - rhs.m_value) <
  3109. rhs.m_epsilon * (rhs.m_scale + detail::my_max(std::fabs(lhs), std::fabs(rhs.m_value)));
  3110. }
  3111. String Approx::toString() const { return String("Approx( ") + doctest::toString(m_value) + " )"; }
  3112. #ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  3113. String toString(char* in) { return toString(static_cast<const char*>(in)); }
  3114. String toString(const char* in) { return String("\"") + (in ? in : "{null string}") + "\""; }
  3115. #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  3116. String toString(bool in) { return in ? "true" : "false"; }
  3117. String toString(float in) { return detail::fpToString(in, 5) + "f"; }
  3118. String toString(double in) { return detail::fpToString(in, 10); }
  3119. String toString(double long in) { return detail::fpToString(in, 15); }
  3120. String toString(char in) {
  3121. char buf[64];
  3122. std::sprintf(buf, "%d", in);
  3123. return buf;
  3124. }
  3125. String toString(char signed in) {
  3126. char buf[64];
  3127. std::sprintf(buf, "%d", in);
  3128. return buf;
  3129. }
  3130. String toString(char unsigned in) {
  3131. char buf[64];
  3132. std::sprintf(buf, "%ud", in);
  3133. return buf;
  3134. }
  3135. String toString(int short in) {
  3136. char buf[64];
  3137. std::sprintf(buf, "%d", in);
  3138. return buf;
  3139. }
  3140. String toString(int short unsigned in) {
  3141. char buf[64];
  3142. std::sprintf(buf, "%u", in);
  3143. return buf;
  3144. }
  3145. String toString(int in) {
  3146. char buf[64];
  3147. std::sprintf(buf, "%d", in);
  3148. return buf;
  3149. }
  3150. String toString(int unsigned in) {
  3151. char buf[64];
  3152. std::sprintf(buf, "%u", in);
  3153. return buf;
  3154. }
  3155. String toString(int long in) {
  3156. char buf[64];
  3157. std::sprintf(buf, "%ld", in);
  3158. return buf;
  3159. }
  3160. String toString(int long unsigned in) {
  3161. char buf[64];
  3162. std::sprintf(buf, "%lu", in);
  3163. return buf;
  3164. }
  3165. #ifdef DOCTEST_CONFIG_WITH_LONG_LONG
  3166. String toString(int long long in) {
  3167. char buf[64];
  3168. std::sprintf(buf, "%lld", in);
  3169. return buf;
  3170. }
  3171. String toString(int long long unsigned in) {
  3172. char buf[64];
  3173. std::sprintf(buf, "%llu", in);
  3174. return buf;
  3175. }
  3176. #endif // DOCTEST_CONFIG_WITH_LONG_LONG
  3177. #ifdef DOCTEST_CONFIG_WITH_NULLPTR
  3178. String toString(std::nullptr_t) { return "nullptr"; }
  3179. #endif // DOCTEST_CONFIG_WITH_NULLPTR
  3180. } // namespace doctest
  3181. #ifdef DOCTEST_CONFIG_DISABLE
  3182. namespace doctest
  3183. {
  3184. bool isRunningInTest() { return false; }
  3185. Context::Context(int, const char* const*) {}
  3186. Context::~Context() {}
  3187. void Context::applyCommandLine(int, const char* const*) {}
  3188. void Context::addFilter(const char*, const char*) {}
  3189. void Context::clearFilters() {}
  3190. void Context::setOption(const char*, int) {}
  3191. void Context::setOption(const char*, const char*) {}
  3192. bool Context::shouldExit() { return false; }
  3193. int Context::run() { return 0; }
  3194. } // namespace doctest
  3195. #else // DOCTEST_CONFIG_DISABLE
  3196. #if !defined(DOCTEST_CONFIG_COLORS_NONE)
  3197. #if !defined(DOCTEST_CONFIG_COLORS_WINDOWS) && !defined(DOCTEST_CONFIG_COLORS_ANSI)
  3198. #ifdef DOCTEST_PLATFORM_WINDOWS
  3199. #define DOCTEST_CONFIG_COLORS_WINDOWS
  3200. #else // linux
  3201. #define DOCTEST_CONFIG_COLORS_ANSI
  3202. #endif // platform
  3203. #endif // DOCTEST_CONFIG_COLORS_WINDOWS && DOCTEST_CONFIG_COLORS_ANSI
  3204. #endif // DOCTEST_CONFIG_COLORS_NONE
  3205. #define DOCTEST_PRINTF_COLORED(buffer, color) \
  3206. do { \
  3207. doctest::detail::Color col(color); \
  3208. std::printf("%s", buffer); \
  3209. } while((void)0, 0)
  3210. // the buffer size used for snprintf() calls
  3211. #if !defined(DOCTEST_SNPRINTF_BUFFER_LENGTH)
  3212. #define DOCTEST_SNPRINTF_BUFFER_LENGTH 1024
  3213. #endif // DOCTEST_SNPRINTF_BUFFER_LENGTH
  3214. #if defined(_MSC_VER) || defined(__MINGW32__)
  3215. #if defined(_MSC_VER) && _MSC_VER >= 1700
  3216. #define DOCTEST_WINDOWS_SAL_IN_OPT _In_opt_
  3217. #else // _MSC_VER
  3218. #define DOCTEST_WINDOWS_SAL_IN_OPT
  3219. #endif // _MSC_VER
  3220. extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(
  3221. DOCTEST_WINDOWS_SAL_IN_OPT const char*);
  3222. extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
  3223. #endif // _MSC_VER || __MINGW32__
  3224. #ifdef DOCTEST_CONFIG_COLORS_ANSI
  3225. #include <unistd.h>
  3226. #endif // DOCTEST_CONFIG_COLORS_ANSI
  3227. #ifdef _WIN32
  3228. // defines for a leaner windows.h
  3229. #ifndef WIN32_MEAN_AND_LEAN
  3230. #define WIN32_MEAN_AND_LEAN
  3231. #endif // WIN32_MEAN_AND_LEAN
  3232. #ifndef VC_EXTRA_LEAN
  3233. #define VC_EXTRA_LEAN
  3234. #endif // VC_EXTRA_LEAN
  3235. #ifndef NOMINMAX
  3236. #define NOMINMAX
  3237. #endif // NOMINMAX
  3238. // not sure what AfxWin.h is for - here I do what Catch does
  3239. #ifdef __AFXDLL
  3240. #include <AfxWin.h>
  3241. #else
  3242. #include <windows.h>
  3243. #endif
  3244. #include <io.h>
  3245. #else // _WIN32
  3246. #include <sys/time.h>
  3247. #endif // _WIN32
  3248. namespace doctest_detail_test_suite_ns
  3249. {
  3250. // holds the current test suite
  3251. doctest::detail::TestSuite& getCurrentTestSuite() {
  3252. static doctest::detail::TestSuite data;
  3253. return data;
  3254. }
  3255. } // namespace doctest_detail_test_suite_ns
  3256. namespace doctest
  3257. {
  3258. namespace detail
  3259. {
  3260. TestCase::TestCase(funcType test, const char* file, unsigned line, const TestSuite& test_suite,
  3261. const char* type, int template_id)
  3262. : m_test(test)
  3263. , m_name(0)
  3264. , m_type(type)
  3265. , m_test_suite(test_suite.m_test_suite)
  3266. , m_description(test_suite.m_description)
  3267. , m_skip(test_suite.m_skip)
  3268. , m_may_fail(test_suite.m_may_fail)
  3269. , m_should_fail(test_suite.m_should_fail)
  3270. , m_expected_failures(test_suite.m_expected_failures)
  3271. , m_timeout(test_suite.m_timeout)
  3272. , m_file(file)
  3273. , m_line(line)
  3274. , m_template_id(template_id) {}
  3275. TestCase& TestCase::operator*(const char* in) {
  3276. m_name = in;
  3277. // make a new name with an appended type for templated test case
  3278. if(m_template_id != -1) {
  3279. m_full_name = String(m_name) + m_type;
  3280. // redirect the name to point to the newly constructed full name
  3281. m_name = m_full_name.c_str();
  3282. }
  3283. return *this;
  3284. }
  3285. TestCase& TestCase::operator=(const TestCase& other) {
  3286. m_test = other.m_test;
  3287. m_full_name = other.m_full_name;
  3288. m_name = other.m_name;
  3289. m_type = other.m_type;
  3290. m_test_suite = other.m_test_suite;
  3291. m_description = other.m_description;
  3292. m_skip = other.m_skip;
  3293. m_may_fail = other.m_may_fail;
  3294. m_should_fail = other.m_should_fail;
  3295. m_expected_failures = other.m_expected_failures;
  3296. m_timeout = other.m_timeout;
  3297. m_file = other.m_file;
  3298. m_line = other.m_line;
  3299. m_template_id = other.m_template_id;
  3300. if(m_template_id != -1)
  3301. m_name = m_full_name.c_str();
  3302. return *this;
  3303. }
  3304. bool TestCase::operator<(const TestCase& other) const {
  3305. if(m_line != other.m_line)
  3306. return m_line < other.m_line;
  3307. int file_cmp = std::strcmp(m_file, other.m_file);
  3308. if(file_cmp != 0)
  3309. return file_cmp < 0;
  3310. return m_template_id < other.m_template_id;
  3311. }
  3312. const char* getAssertString(assertType::Enum val) {
  3313. switch(val) { //!OCLINT missing default in switch statements
  3314. // clang-format off
  3315. case assertType::DT_WARN : return "WARN";
  3316. case assertType::DT_CHECK : return "CHECK";
  3317. case assertType::DT_REQUIRE : return "REQUIRE";
  3318. case assertType::DT_WARN_FALSE : return "WARN_FALSE";
  3319. case assertType::DT_CHECK_FALSE : return "CHECK_FALSE";
  3320. case assertType::DT_REQUIRE_FALSE : return "REQUIRE_FALSE";
  3321. case assertType::DT_WARN_THROWS : return "WARN_THROWS";
  3322. case assertType::DT_CHECK_THROWS : return "CHECK_THROWS";
  3323. case assertType::DT_REQUIRE_THROWS : return "REQUIRE_THROWS";
  3324. case assertType::DT_WARN_THROWS_AS : return "WARN_THROWS_AS";
  3325. case assertType::DT_CHECK_THROWS_AS : return "CHECK_THROWS_AS";
  3326. case assertType::DT_REQUIRE_THROWS_AS : return "REQUIRE_THROWS_AS";
  3327. case assertType::DT_WARN_NOTHROW : return "WARN_NOTHROW";
  3328. case assertType::DT_CHECK_NOTHROW : return "CHECK_NOTHROW";
  3329. case assertType::DT_REQUIRE_NOTHROW : return "REQUIRE_NOTHROW";
  3330. case assertType::DT_WARN_EQ : return "WARN_EQ";
  3331. case assertType::DT_CHECK_EQ : return "CHECK_EQ";
  3332. case assertType::DT_REQUIRE_EQ : return "REQUIRE_EQ";
  3333. case assertType::DT_WARN_NE : return "WARN_NE";
  3334. case assertType::DT_CHECK_NE : return "CHECK_NE";
  3335. case assertType::DT_REQUIRE_NE : return "REQUIRE_NE";
  3336. case assertType::DT_WARN_GT : return "WARN_GT";
  3337. case assertType::DT_CHECK_GT : return "CHECK_GT";
  3338. case assertType::DT_REQUIRE_GT : return "REQUIRE_GT";
  3339. case assertType::DT_WARN_LT : return "WARN_LT";
  3340. case assertType::DT_CHECK_LT : return "CHECK_LT";
  3341. case assertType::DT_REQUIRE_LT : return "REQUIRE_LT";
  3342. case assertType::DT_WARN_GE : return "WARN_GE";
  3343. case assertType::DT_CHECK_GE : return "CHECK_GE";
  3344. case assertType::DT_REQUIRE_GE : return "REQUIRE_GE";
  3345. case assertType::DT_WARN_LE : return "WARN_LE";
  3346. case assertType::DT_CHECK_LE : return "CHECK_LE";
  3347. case assertType::DT_REQUIRE_LE : return "REQUIRE_LE";
  3348. case assertType::DT_WARN_UNARY : return "WARN_UNARY";
  3349. case assertType::DT_CHECK_UNARY : return "CHECK_UNARY";
  3350. case assertType::DT_REQUIRE_UNARY : return "REQUIRE_UNARY";
  3351. case assertType::DT_WARN_UNARY_FALSE : return "WARN_UNARY_FALSE";
  3352. case assertType::DT_CHECK_UNARY_FALSE : return "CHECK_UNARY_FALSE";
  3353. case assertType::DT_REQUIRE_UNARY_FALSE : return "REQUIRE_UNARY_FALSE";
  3354. case assertType::DT_FAST_WARN_EQ : return "FAST_WARN_EQ";
  3355. case assertType::DT_FAST_CHECK_EQ : return "FAST_CHECK_EQ";
  3356. case assertType::DT_FAST_REQUIRE_EQ : return "FAST_REQUIRE_EQ";
  3357. case assertType::DT_FAST_WARN_NE : return "FAST_WARN_NE";
  3358. case assertType::DT_FAST_CHECK_NE : return "FAST_CHECK_NE";
  3359. case assertType::DT_FAST_REQUIRE_NE : return "FAST_REQUIRE_NE";
  3360. case assertType::DT_FAST_WARN_GT : return "FAST_WARN_GT";
  3361. case assertType::DT_FAST_CHECK_GT : return "FAST_CHECK_GT";
  3362. case assertType::DT_FAST_REQUIRE_GT : return "FAST_REQUIRE_GT";
  3363. case assertType::DT_FAST_WARN_LT : return "FAST_WARN_LT";
  3364. case assertType::DT_FAST_CHECK_LT : return "FAST_CHECK_LT";
  3365. case assertType::DT_FAST_REQUIRE_LT : return "FAST_REQUIRE_LT";
  3366. case assertType::DT_FAST_WARN_GE : return "FAST_WARN_GE";
  3367. case assertType::DT_FAST_CHECK_GE : return "FAST_CHECK_GE";
  3368. case assertType::DT_FAST_REQUIRE_GE : return "FAST_REQUIRE_GE";
  3369. case assertType::DT_FAST_WARN_LE : return "FAST_WARN_LE";
  3370. case assertType::DT_FAST_CHECK_LE : return "FAST_CHECK_LE";
  3371. case assertType::DT_FAST_REQUIRE_LE : return "FAST_REQUIRE_LE";
  3372. case assertType::DT_FAST_WARN_UNARY : return "FAST_WARN_UNARY";
  3373. case assertType::DT_FAST_CHECK_UNARY : return "FAST_CHECK_UNARY";
  3374. case assertType::DT_FAST_REQUIRE_UNARY : return "FAST_REQUIRE_UNARY";
  3375. case assertType::DT_FAST_WARN_UNARY_FALSE : return "FAST_WARN_UNARY_FALSE";
  3376. case assertType::DT_FAST_CHECK_UNARY_FALSE : return "FAST_CHECK_UNARY_FALSE";
  3377. case assertType::DT_FAST_REQUIRE_UNARY_FALSE: return "FAST_REQUIRE_UNARY_FALSE";
  3378. // clang-format on
  3379. }
  3380. return "";
  3381. }
  3382. bool checkIfShouldThrow(assertType::Enum assert_type) {
  3383. if(assert_type & assertType::is_require) //!OCLINT bitwise operator in conditional
  3384. return true;
  3385. if((assert_type & assertType::is_check) //!OCLINT bitwise operator in conditional
  3386. && contextState->abort_after > 0 &&
  3387. contextState->numFailedAssertions >= contextState->abort_after)
  3388. return true;
  3389. return false;
  3390. }
  3391. void fastAssertThrowIfFlagSet(int flags) {
  3392. if(flags & assertAction::shouldthrow) //!OCLINT bitwise operator in conditional
  3393. throwException();
  3394. }
  3395. void throwException() {
  3396. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  3397. throw TestFailureException();
  3398. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  3399. }
  3400. // matching of a string against a wildcard mask (case sensitivity configurable) taken from
  3401. // http://www.emoticode.net/c/simple-wildcard-string-compare-globbing-function.html
  3402. int wildcmp(const char* str, const char* wild, bool caseSensitive) {
  3403. const char* cp = 0;
  3404. const char* mp = 0;
  3405. // rolled my own tolower() to not include more headers
  3406. while((*str) && (*wild != '*')) {
  3407. if((caseSensitive ? (*wild != *str) : (tolower(*wild) != tolower(*str))) &&
  3408. (*wild != '?')) {
  3409. return 0;
  3410. }
  3411. wild++;
  3412. str++;
  3413. }
  3414. while(*str) {
  3415. if(*wild == '*') {
  3416. if(!*++wild) {
  3417. return 1;
  3418. }
  3419. mp = wild;
  3420. cp = str + 1;
  3421. } else if((caseSensitive ? (*wild == *str) : (tolower(*wild) == tolower(*str))) ||
  3422. (*wild == '?')) {
  3423. wild++;
  3424. str++;
  3425. } else {
  3426. wild = mp; //!OCLINT parameter reassignment
  3427. str = cp++; //!OCLINT parameter reassignment
  3428. }
  3429. }
  3430. while(*wild == '*') {
  3431. wild++;
  3432. }
  3433. return !*wild;
  3434. }
  3435. //// C string hash function (djb2) - taken from http://www.cse.yorku.ca/~oz/hash.html
  3436. //unsigned hashStr(unsigned const char* str) {
  3437. // unsigned long hash = 5381;
  3438. // char c;
  3439. // while((c = *str++))
  3440. // hash = ((hash << 5) + hash) + c; // hash * 33 + c
  3441. // return hash;
  3442. //}
  3443. // checks if the name matches any of the filters (and can be configured what to do when empty)
  3444. bool matchesAny(const char* name, const std::vector<String>& filters, int matchEmpty,
  3445. bool caseSensitive) {
  3446. if(filters.empty() && matchEmpty)
  3447. return true;
  3448. for(unsigned i = 0; i < filters.size(); ++i)
  3449. if(wildcmp(name, filters[i].c_str(), caseSensitive))
  3450. return true;
  3451. return false;
  3452. }
  3453. #ifdef _WIN32
  3454. typedef unsigned long long UInt64;
  3455. UInt64 getCurrentTicks() {
  3456. static UInt64 hz = 0, hzo = 0;
  3457. if(!hz) {
  3458. QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&hz));
  3459. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&hzo));
  3460. }
  3461. UInt64 t;
  3462. QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&t));
  3463. return ((t - hzo) * 1000000) / hz;
  3464. }
  3465. #else // _WIN32
  3466. typedef uint64_t UInt64;
  3467. UInt64 getCurrentTicks() {
  3468. timeval t;
  3469. gettimeofday(&t, 0);
  3470. return static_cast<UInt64>(t.tv_sec) * 1000000 + static_cast<UInt64>(t.tv_usec);
  3471. }
  3472. #endif // _WIN32
  3473. class Timer
  3474. {
  3475. public:
  3476. Timer()
  3477. : m_ticks(0) {}
  3478. void start() { m_ticks = getCurrentTicks(); }
  3479. unsigned int getElapsedMicroseconds() const {
  3480. return static_cast<unsigned int>(getCurrentTicks() - m_ticks);
  3481. }
  3482. unsigned int getElapsedMilliseconds() const {
  3483. return static_cast<unsigned int>(getElapsedMicroseconds() / 1000);
  3484. }
  3485. double getElapsedSeconds() const { return getElapsedMicroseconds() / 1000000.0; }
  3486. private:
  3487. UInt64 m_ticks;
  3488. };
  3489. TestAccessibleContextState* getTestsContextState() { return contextState; }
  3490. bool SubcaseSignature::operator<(const SubcaseSignature& other) const {
  3491. if(m_line != other.m_line)
  3492. return m_line < other.m_line;
  3493. if(std::strcmp(m_file, other.m_file) != 0)
  3494. return std::strcmp(m_file, other.m_file) < 0;
  3495. return std::strcmp(m_name, other.m_name) < 0;
  3496. }
  3497. Subcase::Subcase(const char* name, const char* file, int line)
  3498. : m_signature(name, file, line)
  3499. , m_entered(false) {
  3500. ContextState* s = contextState;
  3501. // if we have already completed it
  3502. if(s->subcasesPassed.count(m_signature) != 0)
  3503. return;
  3504. // check subcase filters
  3505. if(s->subcasesCurrentLevel < s->subcase_filter_levels) {
  3506. if(!matchesAny(m_signature.m_name, s->filters[6], 1, s->case_sensitive))
  3507. return;
  3508. if(matchesAny(m_signature.m_name, s->filters[7], 0, s->case_sensitive))
  3509. return;
  3510. }
  3511. // if a Subcase on the same level has already been entered
  3512. if(s->subcasesEnteredLevels.count(s->subcasesCurrentLevel) != 0) {
  3513. s->subcasesHasSkipped = true;
  3514. return;
  3515. }
  3516. s->subcasesStack.push_back(*this);
  3517. if(s->hasLoggedCurrentTestStart)
  3518. logTestEnd();
  3519. s->hasLoggedCurrentTestStart = false;
  3520. s->subcasesEnteredLevels.insert(s->subcasesCurrentLevel++);
  3521. m_entered = true;
  3522. }
  3523. Subcase::Subcase(const Subcase& other)
  3524. : m_signature(other.m_signature.m_name, other.m_signature.m_file,
  3525. other.m_signature.m_line)
  3526. , m_entered(other.m_entered) {}
  3527. Subcase::~Subcase() {
  3528. if(m_entered) {
  3529. ContextState* s = contextState;
  3530. s->subcasesCurrentLevel--;
  3531. // only mark the subcase as passed if no subcases have been skipped
  3532. if(s->subcasesHasSkipped == false)
  3533. s->subcasesPassed.insert(m_signature);
  3534. if(!s->subcasesStack.empty())
  3535. s->subcasesStack.pop_back();
  3536. if(s->hasLoggedCurrentTestStart)
  3537. logTestEnd();
  3538. s->hasLoggedCurrentTestStart = false;
  3539. }
  3540. }
  3541. Result::~Result() {}
  3542. Result& Result::operator=(const Result& other) {
  3543. m_passed = other.m_passed;
  3544. m_decomposition = other.m_decomposition;
  3545. return *this;
  3546. }
  3547. // for sorting tests by file/line
  3548. int fileOrderComparator(const void* a, const void* b) {
  3549. const TestCase* lhs = *static_cast<TestCase* const*>(a);
  3550. const TestCase* rhs = *static_cast<TestCase* const*>(b);
  3551. #ifdef _MSC_VER
  3552. // this is needed because MSVC gives different case for drive letters
  3553. // for __FILE__ when evaluated in a header and a source file
  3554. int res = stricmp(lhs->m_file, rhs->m_file);
  3555. #else // _MSC_VER
  3556. int res = std::strcmp(lhs->m_file, rhs->m_file);
  3557. #endif // _MSC_VER
  3558. if(res != 0)
  3559. return res;
  3560. return static_cast<int>(lhs->m_line - rhs->m_line);
  3561. }
  3562. // for sorting tests by suite/file/line
  3563. int suiteOrderComparator(const void* a, const void* b) {
  3564. const TestCase* lhs = *static_cast<TestCase* const*>(a);
  3565. const TestCase* rhs = *static_cast<TestCase* const*>(b);
  3566. int res = std::strcmp(lhs->m_test_suite, rhs->m_test_suite);
  3567. if(res != 0)
  3568. return res;
  3569. return fileOrderComparator(a, b);
  3570. }
  3571. // for sorting tests by name/suite/file/line
  3572. int nameOrderComparator(const void* a, const void* b) {
  3573. const TestCase* lhs = *static_cast<TestCase* const*>(a);
  3574. const TestCase* rhs = *static_cast<TestCase* const*>(b);
  3575. int res_name = std::strcmp(lhs->m_name, rhs->m_name);
  3576. if(res_name != 0)
  3577. return res_name;
  3578. return suiteOrderComparator(a, b);
  3579. }
  3580. // sets the current test suite
  3581. int setTestSuite(const TestSuite& ts) {
  3582. doctest_detail_test_suite_ns::getCurrentTestSuite() = ts;
  3583. return 0;
  3584. }
  3585. // all the registered tests
  3586. std::set<TestCase>& getRegisteredTests() {
  3587. static std::set<TestCase> data;
  3588. return data;
  3589. }
  3590. // used by the macros for registering tests
  3591. int regTest(const TestCase& tc) {
  3592. getRegisteredTests().insert(tc);
  3593. return 0;
  3594. }
  3595. struct Color
  3596. {
  3597. enum Code
  3598. {
  3599. None = 0,
  3600. White,
  3601. Red,
  3602. Green,
  3603. Blue,
  3604. Cyan,
  3605. Yellow,
  3606. Grey,
  3607. Bright = 0x10,
  3608. BrightRed = Bright | Red,
  3609. BrightGreen = Bright | Green,
  3610. LightGrey = Bright | Grey,
  3611. BrightWhite = Bright | White
  3612. };
  3613. explicit Color(Code code) { use(code); }
  3614. ~Color() { use(None); }
  3615. static void use(Code code);
  3616. static void init();
  3617. };
  3618. #ifdef DOCTEST_CONFIG_COLORS_WINDOWS
  3619. HANDLE g_stdoutHandle;
  3620. WORD g_originalForegroundAttributes;
  3621. WORD g_originalBackgroundAttributes;
  3622. bool g_attrsInitted = false;
  3623. #endif // DOCTEST_CONFIG_COLORS_WINDOWS
  3624. void Color::init() {
  3625. #ifdef DOCTEST_CONFIG_COLORS_WINDOWS
  3626. if(!g_attrsInitted) {
  3627. g_stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  3628. g_attrsInitted = true;
  3629. CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  3630. GetConsoleScreenBufferInfo(g_stdoutHandle, &csbiInfo);
  3631. g_originalForegroundAttributes =
  3632. csbiInfo.wAttributes &
  3633. ~(BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
  3634. g_originalBackgroundAttributes =
  3635. csbiInfo.wAttributes &
  3636. ~(FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
  3637. }
  3638. #endif // DOCTEST_CONFIG_COLORS_WINDOWS
  3639. }
  3640. void Color::use(Code
  3641. #ifndef DOCTEST_CONFIG_COLORS_NONE
  3642. code
  3643. #endif // DOCTEST_CONFIG_COLORS_NONE
  3644. ) {
  3645. const ContextState* p = contextState;
  3646. if(p->no_colors)
  3647. return;
  3648. #ifdef DOCTEST_CONFIG_COLORS_ANSI
  3649. if(isatty(STDOUT_FILENO) == false && p->force_colors == false)
  3650. return;
  3651. const char* col = "";
  3652. // clang-format off
  3653. switch(code) { //!OCLINT missing break in switch statement / unnecessary default statement in covered switch statement
  3654. case Color::Red: col = "[0;31m"; break;
  3655. case Color::Green: col = "[0;32m"; break;
  3656. case Color::Blue: col = "[0;34m"; break;
  3657. case Color::Cyan: col = "[0;36m"; break;
  3658. case Color::Yellow: col = "[0;33m"; break;
  3659. case Color::Grey: col = "[1;30m"; break;
  3660. case Color::LightGrey: col = "[0;37m"; break;
  3661. case Color::BrightRed: col = "[1;31m"; break;
  3662. case Color::BrightGreen: col = "[1;32m"; break;
  3663. case Color::BrightWhite: col = "[1;37m"; break;
  3664. case Color::Bright: // invalid
  3665. case Color::None:
  3666. case Color::White:
  3667. default: col = "[0m";
  3668. }
  3669. // clang-format on
  3670. std::printf("\033%s", col);
  3671. #endif // DOCTEST_CONFIG_COLORS_ANSI
  3672. #ifdef DOCTEST_CONFIG_COLORS_WINDOWS
  3673. if(isatty(fileno(stdout)) == false && p->force_colors == false)
  3674. return;
  3675. #define DOCTEST_SET_ATTR(x) \
  3676. SetConsoleTextAttribute(g_stdoutHandle, x | g_originalBackgroundAttributes)
  3677. // clang-format off
  3678. switch (code) {
  3679. case Color::White: DOCTEST_SET_ATTR(FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE); break;
  3680. case Color::Red: DOCTEST_SET_ATTR(FOREGROUND_RED); break;
  3681. case Color::Green: DOCTEST_SET_ATTR(FOREGROUND_GREEN); break;
  3682. case Color::Blue: DOCTEST_SET_ATTR(FOREGROUND_BLUE); break;
  3683. case Color::Cyan: DOCTEST_SET_ATTR(FOREGROUND_BLUE | FOREGROUND_GREEN); break;
  3684. case Color::Yellow: DOCTEST_SET_ATTR(FOREGROUND_RED | FOREGROUND_GREEN); break;
  3685. case Color::Grey: DOCTEST_SET_ATTR(0); break;
  3686. case Color::LightGrey: DOCTEST_SET_ATTR(FOREGROUND_INTENSITY); break;
  3687. case Color::BrightRed: DOCTEST_SET_ATTR(FOREGROUND_INTENSITY | FOREGROUND_RED); break;
  3688. case Color::BrightGreen: DOCTEST_SET_ATTR(FOREGROUND_INTENSITY | FOREGROUND_GREEN); break;
  3689. case Color::BrightWhite: DOCTEST_SET_ATTR(FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE); break;
  3690. case Color::None:
  3691. case Color::Bright: // invalid
  3692. default: DOCTEST_SET_ATTR(g_originalForegroundAttributes);
  3693. }
  3694. // clang-format on
  3695. #undef DOCTEST_SET_ATTR
  3696. #endif // DOCTEST_CONFIG_COLORS_WINDOWS
  3697. }
  3698. IExceptionTranslator::~IExceptionTranslator() {}
  3699. std::vector<const IExceptionTranslator*>& getExceptionTranslators() {
  3700. static std::vector<const IExceptionTranslator*> data;
  3701. return data;
  3702. }
  3703. void registerExceptionTranslatorImpl(const IExceptionTranslator* translateFunction) {
  3704. getExceptionTranslators().push_back(translateFunction);
  3705. }
  3706. String translateActiveException() {
  3707. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  3708. String res;
  3709. std::vector<const IExceptionTranslator*>& translators = getExceptionTranslators();
  3710. for(size_t i = 0; i < translators.size(); ++i)
  3711. if(translators[i]->translate(res))
  3712. return res;
  3713. // clang-format off
  3714. try {
  3715. throw;
  3716. } catch(std::exception& ex) {
  3717. return ex.what();
  3718. } catch(std::string& msg) {
  3719. return msg.c_str();
  3720. } catch(const char* msg) {
  3721. return msg;
  3722. } catch(...) {
  3723. return "unknown exception";
  3724. }
  3725. // clang-format on
  3726. #else // DOCTEST_CONFIG_NO_EXCEPTIONS
  3727. return "";
  3728. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  3729. }
  3730. void writeStringToStream(std::ostream* stream, const String& str) { *stream << str; }
  3731. #ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  3732. void toStream(std::ostream* stream, char* in) { *stream << in; }
  3733. void toStream(std::ostream* stream, const char* in) { *stream << in; }
  3734. #endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
  3735. void toStream(std::ostream* stream, bool in) {
  3736. *stream << std::boolalpha << in << std::noboolalpha;
  3737. }
  3738. void toStream(std::ostream* stream, float in) { *stream << in; }
  3739. void toStream(std::ostream* stream, double in) { *stream << in; }
  3740. void toStream(std::ostream* stream, double long in) { *stream << in; }
  3741. void toStream(std::ostream* stream, char in) { *stream << in; }
  3742. void toStream(std::ostream* stream, char signed in) { *stream << in; }
  3743. void toStream(std::ostream* stream, char unsigned in) { *stream << in; }
  3744. void toStream(std::ostream* stream, int short in) { *stream << in; }
  3745. void toStream(std::ostream* stream, int short unsigned in) { *stream << in; }
  3746. void toStream(std::ostream* stream, int in) { *stream << in; }
  3747. void toStream(std::ostream* stream, int unsigned in) { *stream << in; }
  3748. void toStream(std::ostream* stream, int long in) { *stream << in; }
  3749. void toStream(std::ostream* stream, int long unsigned in) { *stream << in; }
  3750. #ifdef DOCTEST_CONFIG_WITH_LONG_LONG
  3751. void toStream(std::ostream* stream, int long long in) { *stream << in; }
  3752. void toStream(std::ostream* stream, int long long unsigned in) { *stream << in; }
  3753. #endif // DOCTEST_CONFIG_WITH_LONG_LONG
  3754. void addToContexts(IContextScope* ptr) { contextState->contexts.push_back(ptr); }
  3755. void popFromContexts() { contextState->contexts.pop_back(); }
  3756. void useContextIfExceptionOccurred(IContextScope* ptr) {
  3757. if(std::uncaught_exception()) {
  3758. std::ostringstream stream;
  3759. ptr->build(&stream);
  3760. contextState->exceptionalContexts.push_back(stream.str());
  3761. }
  3762. }
  3763. void printSummary();
  3764. #if !defined(DOCTEST_CONFIG_POSIX_SIGNALS) && !defined(DOCTEST_CONFIG_WINDOWS_SEH)
  3765. void reportFatal(const std::string&) {}
  3766. struct FatalConditionHandler
  3767. {
  3768. void reset() {}
  3769. };
  3770. #else // DOCTEST_CONFIG_POSIX_SIGNALS || DOCTEST_CONFIG_WINDOWS_SEH
  3771. void reportFatal(const std::string& message) {
  3772. DOCTEST_LOG_START();
  3773. contextState->numAssertions += contextState->numAssertionsForCurrentTestcase;
  3774. logTestException(message.c_str(), true);
  3775. logTestEnd();
  3776. contextState->numFailed++;
  3777. printSummary();
  3778. }
  3779. #ifdef DOCTEST_PLATFORM_WINDOWS
  3780. struct SignalDefs
  3781. {
  3782. DWORD id;
  3783. const char* name;
  3784. };
  3785. // There is no 1-1 mapping between signals and windows exceptions.
  3786. // Windows can easily distinguish between SO and SigSegV,
  3787. // but SigInt, SigTerm, etc are handled differently.
  3788. SignalDefs signalDefs[] = {
  3789. {EXCEPTION_ILLEGAL_INSTRUCTION, "SIGILL - Illegal instruction signal"},
  3790. {EXCEPTION_STACK_OVERFLOW, "SIGSEGV - Stack overflow"},
  3791. {EXCEPTION_ACCESS_VIOLATION, "SIGSEGV - Segmentation violation signal"},
  3792. {EXCEPTION_INT_DIVIDE_BY_ZERO, "Divide by zero error"},
  3793. };
  3794. struct FatalConditionHandler
  3795. {
  3796. static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) {
  3797. for(size_t i = 0; i < sizeof(signalDefs) / sizeof(SignalDefs); ++i) {
  3798. if(ExceptionInfo->ExceptionRecord->ExceptionCode == signalDefs[i].id) {
  3799. reportFatal(signalDefs[i].name);
  3800. }
  3801. }
  3802. // If its not an exception we care about, pass it along.
  3803. // This stops us from eating debugger breaks etc.
  3804. return EXCEPTION_CONTINUE_SEARCH;
  3805. }
  3806. FatalConditionHandler() {
  3807. isSet = true;
  3808. // 32k seems enough for doctest to handle stack overflow,
  3809. // but the value was found experimentally, so there is no strong guarantee
  3810. guaranteeSize = 32 * 1024;
  3811. exceptionHandlerHandle = 0;
  3812. // Register as first handler in current chain
  3813. exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException);
  3814. // Pass in guarantee size to be filled
  3815. SetThreadStackGuarantee(&guaranteeSize);
  3816. }
  3817. static void reset() {
  3818. if(isSet) {
  3819. // Unregister handler and restore the old guarantee
  3820. RemoveVectoredExceptionHandler(exceptionHandlerHandle);
  3821. SetThreadStackGuarantee(&guaranteeSize);
  3822. exceptionHandlerHandle = 0;
  3823. isSet = false;
  3824. }
  3825. }
  3826. ~FatalConditionHandler() { reset(); }
  3827. private:
  3828. static bool isSet;
  3829. static ULONG guaranteeSize;
  3830. static PVOID exceptionHandlerHandle;
  3831. };
  3832. bool FatalConditionHandler::isSet = false;
  3833. ULONG FatalConditionHandler::guaranteeSize = 0;
  3834. PVOID FatalConditionHandler::exceptionHandlerHandle = 0;
  3835. #else // DOCTEST_PLATFORM_WINDOWS
  3836. struct SignalDefs
  3837. {
  3838. int id;
  3839. const char* name;
  3840. };
  3841. SignalDefs signalDefs[] = {{SIGINT, "SIGINT - Terminal interrupt signal"},
  3842. {SIGILL, "SIGILL - Illegal instruction signal"},
  3843. {SIGFPE, "SIGFPE - Floating point error signal"},
  3844. {SIGSEGV, "SIGSEGV - Segmentation violation signal"},
  3845. {SIGTERM, "SIGTERM - Termination request signal"},
  3846. {SIGABRT, "SIGABRT - Abort (abnormal termination) signal"}};
  3847. struct FatalConditionHandler
  3848. {
  3849. static bool isSet;
  3850. static struct sigaction oldSigActions[sizeof(signalDefs) / sizeof(SignalDefs)];
  3851. static stack_t oldSigStack;
  3852. static char altStackMem[SIGSTKSZ];
  3853. static void handleSignal(int sig) {
  3854. std::string name = "<unknown signal>";
  3855. for(std::size_t i = 0; i < sizeof(signalDefs) / sizeof(SignalDefs); ++i) {
  3856. SignalDefs& def = signalDefs[i];
  3857. if(sig == def.id) {
  3858. name = def.name;
  3859. break;
  3860. }
  3861. }
  3862. reset();
  3863. reportFatal(name);
  3864. raise(sig);
  3865. }
  3866. FatalConditionHandler() {
  3867. isSet = true;
  3868. stack_t sigStack;
  3869. sigStack.ss_sp = altStackMem;
  3870. sigStack.ss_size = SIGSTKSZ;
  3871. sigStack.ss_flags = 0;
  3872. sigaltstack(&sigStack, &oldSigStack);
  3873. struct sigaction sa = {0};
  3874. sa.sa_handler = handleSignal; // NOLINT
  3875. sa.sa_flags = SA_ONSTACK;
  3876. for(std::size_t i = 0; i < sizeof(signalDefs) / sizeof(SignalDefs); ++i) {
  3877. sigaction(signalDefs[i].id, &sa, &oldSigActions[i]);
  3878. }
  3879. }
  3880. ~FatalConditionHandler() { reset(); }
  3881. static void reset() {
  3882. if(isSet) {
  3883. // Set signals back to previous values -- hopefully nobody overwrote them in the meantime
  3884. for(std::size_t i = 0; i < sizeof(signalDefs) / sizeof(SignalDefs); ++i) {
  3885. sigaction(signalDefs[i].id, &oldSigActions[i], 0);
  3886. }
  3887. // Return the old stack
  3888. sigaltstack(&oldSigStack, 0);
  3889. isSet = false;
  3890. }
  3891. }
  3892. };
  3893. bool FatalConditionHandler::isSet = false;
  3894. struct sigaction FatalConditionHandler::oldSigActions[sizeof(signalDefs) / sizeof(SignalDefs)] =
  3895. {};
  3896. stack_t FatalConditionHandler::oldSigStack = {};
  3897. char FatalConditionHandler::altStackMem[SIGSTKSZ] = {};
  3898. #endif // DOCTEST_PLATFORM_WINDOWS
  3899. #endif // DOCTEST_CONFIG_POSIX_SIGNALS || DOCTEST_CONFIG_WINDOWS_SEH
  3900. // depending on the current options this will remove the path of filenames
  3901. const char* fileForOutput(const char* file) {
  3902. if(contextState->no_path_in_filenames) {
  3903. const char* back = std::strrchr(file, '\\');
  3904. const char* forward = std::strrchr(file, '/');
  3905. if(back || forward) {
  3906. if(back > forward)
  3907. forward = back;
  3908. return forward + 1;
  3909. }
  3910. }
  3911. return file;
  3912. }
  3913. // depending on the current options this will substitute the line numbers with 0
  3914. int lineForOutput(int line) {
  3915. if(contextState->no_line_numbers)
  3916. return 0;
  3917. return line;
  3918. }
  3919. #ifdef DOCTEST_PLATFORM_MAC
  3920. #include <sys/types.h>
  3921. #include <unistd.h>
  3922. #include <sys/sysctl.h>
  3923. // The following function is taken directly from the following technical note:
  3924. // http://developer.apple.com/library/mac/#qa/qa2004/qa1361.html
  3925. // Returns true if the current process is being debugged (either
  3926. // running under the debugger or has a debugger attached post facto).
  3927. bool isDebuggerActive() {
  3928. int mib[4];
  3929. kinfo_proc info;
  3930. size_t size;
  3931. // Initialize the flags so that, if sysctl fails for some bizarre
  3932. // reason, we get a predictable result.
  3933. info.kp_proc.p_flag = 0;
  3934. // Initialize mib, which tells sysctl the info we want, in this case
  3935. // we're looking for information about a specific process ID.
  3936. mib[0] = CTL_KERN;
  3937. mib[1] = KERN_PROC;
  3938. mib[2] = KERN_PROC_PID;
  3939. mib[3] = getpid();
  3940. // Call sysctl.
  3941. size = sizeof(info);
  3942. if(sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, 0, 0) != 0) {
  3943. fprintf(stderr, "\n** Call to sysctl failed - unable to determine if debugger is "
  3944. "active **\n\n");
  3945. return false;
  3946. }
  3947. // We're being debugged if the P_TRACED flag is set.
  3948. return ((info.kp_proc.p_flag & P_TRACED) != 0);
  3949. }
  3950. #elif defined(_MSC_VER) || defined(__MINGW32__)
  3951. bool isDebuggerActive() { return ::IsDebuggerPresent() != 0; }
  3952. #else
  3953. bool isDebuggerActive() { return false; }
  3954. #endif // Platform
  3955. #ifdef DOCTEST_PLATFORM_WINDOWS
  3956. void myOutputDebugString(const String& text) { ::OutputDebugStringA(text.c_str()); }
  3957. #else
  3958. // TODO: integration with XCode and other IDEs
  3959. void myOutputDebugString(const String&) {}
  3960. #endif // Platform
  3961. const char* getSeparator() {
  3962. return "===============================================================================\n";
  3963. }
  3964. void printToDebugConsole(const String& text) {
  3965. if(isDebuggerActive())
  3966. myOutputDebugString(text.c_str());
  3967. }
  3968. void addFailedAssert(assertType::Enum assert_type) {
  3969. if((assert_type & assertType::is_warn) == 0) { //!OCLINT bitwise operator in conditional
  3970. contextState->numFailedAssertions++;
  3971. contextState->numFailedAssertionsForCurrentTestcase++;
  3972. contextState->hasCurrentTestFailed = true;
  3973. }
  3974. }
  3975. void logTestStart(const TestCase& tc) {
  3976. char loc[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  3977. DOCTEST_SNPRINTF(loc, DOCTEST_COUNTOF(loc), "%s(%d)\n", fileForOutput(tc.m_file),
  3978. lineForOutput(tc.m_line));
  3979. char ts1[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  3980. DOCTEST_SNPRINTF(ts1, DOCTEST_COUNTOF(ts1), "TEST SUITE: ");
  3981. char ts2[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  3982. DOCTEST_SNPRINTF(ts2, DOCTEST_COUNTOF(ts2), "%s\n", tc.m_test_suite);
  3983. char n1[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  3984. DOCTEST_SNPRINTF(n1, DOCTEST_COUNTOF(n1), "TEST CASE: ");
  3985. char n2[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  3986. DOCTEST_SNPRINTF(n2, DOCTEST_COUNTOF(n2), "%s\n", tc.m_name);
  3987. char d1[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  3988. DOCTEST_SNPRINTF(d1, DOCTEST_COUNTOF(d1), "DESCRIPTION: ");
  3989. char d2[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  3990. DOCTEST_SNPRINTF(d2, DOCTEST_COUNTOF(d2), "%s\n", tc.m_description);
  3991. // hack for BDD style of macros - to not print "TEST CASE:"
  3992. char scenario[] = " Scenario:";
  3993. if(std::string(tc.m_name).substr(0, DOCTEST_COUNTOF(scenario) - 1) == scenario)
  3994. n1[0] = '\0';
  3995. DOCTEST_PRINTF_COLORED(getSeparator(), Color::Yellow);
  3996. DOCTEST_PRINTF_COLORED(loc, Color::LightGrey);
  3997. String forDebugConsole;
  3998. if(tc.m_description) {
  3999. DOCTEST_PRINTF_COLORED(d1, Color::Yellow);
  4000. DOCTEST_PRINTF_COLORED(d2, Color::None);
  4001. forDebugConsole += d1;
  4002. forDebugConsole += d2;
  4003. }
  4004. if(tc.m_test_suite[0] != '\0') {
  4005. DOCTEST_PRINTF_COLORED(ts1, Color::Yellow);
  4006. DOCTEST_PRINTF_COLORED(ts2, Color::None);
  4007. forDebugConsole += ts1;
  4008. forDebugConsole += ts2;
  4009. }
  4010. DOCTEST_PRINTF_COLORED(n1, Color::Yellow);
  4011. DOCTEST_PRINTF_COLORED(n2, Color::None);
  4012. String subcaseStuff;
  4013. std::vector<Subcase>& subcasesStack = contextState->subcasesStack;
  4014. for(unsigned i = 0; i < subcasesStack.size(); ++i) {
  4015. if(subcasesStack[i].m_signature.m_name[0] != '\0') {
  4016. char subcase[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4017. DOCTEST_SNPRINTF(subcase, DOCTEST_COUNTOF(loc), " %s\n",
  4018. subcasesStack[i].m_signature.m_name);
  4019. DOCTEST_PRINTF_COLORED(subcase, Color::None);
  4020. subcaseStuff += subcase;
  4021. }
  4022. }
  4023. DOCTEST_PRINTF_COLORED("\n", Color::None);
  4024. printToDebugConsole(String(getSeparator()) + loc + forDebugConsole.c_str() + n1 + n2 +
  4025. subcaseStuff.c_str() + "\n");
  4026. }
  4027. void logTestEnd() {}
  4028. void logTestException(const String& what, bool crash) {
  4029. char msg[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4030. DOCTEST_SNPRINTF(msg, DOCTEST_COUNTOF(msg), "TEST CASE FAILED!\n");
  4031. char info1[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4032. char info2[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4033. info1[0] = 0;
  4034. info2[0] = 0;
  4035. DOCTEST_SNPRINTF(info1, DOCTEST_COUNTOF(info1),
  4036. crash ? "crashed:\n" : "threw exception:\n");
  4037. DOCTEST_SNPRINTF(info2, DOCTEST_COUNTOF(info2), " %s\n", what.c_str());
  4038. std::string contextStr;
  4039. if(!contextState->exceptionalContexts.empty()) {
  4040. contextStr += "with context:\n";
  4041. for(size_t i = contextState->exceptionalContexts.size(); i > 0; --i) {
  4042. contextStr += " ";
  4043. contextStr += contextState->exceptionalContexts[i - 1];
  4044. contextStr += "\n";
  4045. }
  4046. }
  4047. DOCTEST_PRINTF_COLORED(msg, Color::Red);
  4048. DOCTEST_PRINTF_COLORED(info1, Color::None);
  4049. DOCTEST_PRINTF_COLORED(info2, Color::Cyan);
  4050. DOCTEST_PRINTF_COLORED(contextStr.c_str(), Color::None);
  4051. DOCTEST_PRINTF_COLORED("\n", Color::None);
  4052. printToDebugConsole(String(msg) + info1 + info2 + contextStr.c_str() + "\n");
  4053. }
  4054. String logContext() {
  4055. std::ostringstream stream;
  4056. std::vector<IContextScope*>& contexts = contextState->contexts;
  4057. if(!contexts.empty())
  4058. stream << "with context:\n";
  4059. for(size_t i = 0; i < contexts.size(); ++i) {
  4060. stream << " ";
  4061. contexts[i]->build(&stream);
  4062. stream << "\n";
  4063. }
  4064. return stream.str().c_str();
  4065. }
  4066. const char* getFailString(assertType::Enum assert_type) {
  4067. if(assert_type & assertType::is_warn) //!OCLINT bitwise operator in conditional
  4068. return "WARNING";
  4069. if(assert_type & assertType::is_check) //!OCLINT bitwise operator in conditional
  4070. return "ERROR";
  4071. if(assert_type & assertType::is_require) //!OCLINT bitwise operator in conditional
  4072. return "FATAL ERROR";
  4073. return "";
  4074. }
  4075. void logAssert(bool passed, const char* decomposition, bool threw, const String& exception,
  4076. const char* expr, assertType::Enum assert_type, const char* file, int line) {
  4077. char loc[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4078. DOCTEST_SNPRINTF(loc, DOCTEST_COUNTOF(loc), "%s(%d)", fileForOutput(file),
  4079. lineForOutput(line));
  4080. char msg[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4081. DOCTEST_SNPRINTF(msg, DOCTEST_COUNTOF(msg), " %s!\n",
  4082. passed ? "PASSED" : getFailString(assert_type));
  4083. char info1[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4084. DOCTEST_SNPRINTF(info1, DOCTEST_COUNTOF(info1), " %s( %s )\n",
  4085. getAssertString(assert_type), expr);
  4086. char info2[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4087. char info3[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4088. info2[0] = 0;
  4089. info3[0] = 0;
  4090. if(threw) {
  4091. DOCTEST_SNPRINTF(info2, DOCTEST_COUNTOF(info2), "threw exception:\n");
  4092. DOCTEST_SNPRINTF(info3, DOCTEST_COUNTOF(info3), " %s\n", exception.c_str());
  4093. } else {
  4094. DOCTEST_SNPRINTF(info2, DOCTEST_COUNTOF(info2), "with expansion:\n");
  4095. DOCTEST_SNPRINTF(info3, DOCTEST_COUNTOF(info3), " %s( %s )\n",
  4096. getAssertString(assert_type), decomposition);
  4097. }
  4098. bool isWarn = assert_type & assertType::is_warn;
  4099. DOCTEST_PRINTF_COLORED(loc, Color::LightGrey);
  4100. DOCTEST_PRINTF_COLORED(msg,
  4101. passed ? Color::BrightGreen : isWarn ? Color::Yellow : Color::Red);
  4102. DOCTEST_PRINTF_COLORED(info1, Color::Cyan);
  4103. DOCTEST_PRINTF_COLORED(info2, Color::None);
  4104. DOCTEST_PRINTF_COLORED(info3, Color::Cyan);
  4105. String context = logContext();
  4106. DOCTEST_PRINTF_COLORED(context.c_str(), Color::None);
  4107. DOCTEST_PRINTF_COLORED("\n", Color::None);
  4108. printToDebugConsole(String(loc) + msg + info1 + info2 + info3 + context.c_str() + "\n");
  4109. }
  4110. void logAssertThrows(bool threw, const char* expr, assertType::Enum assert_type,
  4111. const char* file, int line) {
  4112. char loc[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4113. DOCTEST_SNPRINTF(loc, DOCTEST_COUNTOF(loc), "%s(%d)", fileForOutput(file),
  4114. lineForOutput(line));
  4115. char msg[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4116. DOCTEST_SNPRINTF(msg, DOCTEST_COUNTOF(msg), " %s!\n",
  4117. threw ? "PASSED" : getFailString(assert_type));
  4118. char info1[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4119. DOCTEST_SNPRINTF(info1, DOCTEST_COUNTOF(info1), " %s( %s )\n",
  4120. getAssertString(assert_type), expr);
  4121. char info2[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4122. info2[0] = 0;
  4123. if(!threw)
  4124. DOCTEST_SNPRINTF(info2, DOCTEST_COUNTOF(info2), "didn't throw at all\n");
  4125. bool isWarn = assert_type & assertType::is_warn;
  4126. DOCTEST_PRINTF_COLORED(loc, Color::LightGrey);
  4127. DOCTEST_PRINTF_COLORED(msg,
  4128. threw ? Color::BrightGreen : isWarn ? Color::Yellow : Color::Red);
  4129. DOCTEST_PRINTF_COLORED(info1, Color::Cyan);
  4130. DOCTEST_PRINTF_COLORED(info2, Color::None);
  4131. String context = logContext();
  4132. DOCTEST_PRINTF_COLORED(context.c_str(), Color::None);
  4133. DOCTEST_PRINTF_COLORED("\n", Color::None);
  4134. printToDebugConsole(String(loc) + msg + info1 + info2 + context.c_str() + "\n");
  4135. }
  4136. void logAssertThrowsAs(bool threw, bool threw_as, const char* as, const String& exception,
  4137. const char* expr, assertType::Enum assert_type, const char* file,
  4138. int line) {
  4139. char loc[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4140. DOCTEST_SNPRINTF(loc, DOCTEST_COUNTOF(loc), "%s(%d)", fileForOutput(file),
  4141. lineForOutput(line));
  4142. char msg[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4143. DOCTEST_SNPRINTF(msg, DOCTEST_COUNTOF(msg), " %s!\n",
  4144. threw_as ? "PASSED" : getFailString(assert_type));
  4145. char info1[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4146. DOCTEST_SNPRINTF(info1, DOCTEST_COUNTOF(info1), " %s( %s, %s )\n",
  4147. getAssertString(assert_type), expr, as);
  4148. char info2[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4149. char info3[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4150. info2[0] = 0;
  4151. info3[0] = 0;
  4152. if(!threw) { //!OCLINT inverted logic
  4153. DOCTEST_SNPRINTF(info2, DOCTEST_COUNTOF(info2), "didn't throw at all\n");
  4154. } else if(!threw_as) {
  4155. DOCTEST_SNPRINTF(info2, DOCTEST_COUNTOF(info2), "threw a different exception:\n");
  4156. DOCTEST_SNPRINTF(info3, DOCTEST_COUNTOF(info3), " %s\n", exception.c_str());
  4157. }
  4158. bool isWarn = assert_type & assertType::is_warn;
  4159. DOCTEST_PRINTF_COLORED(loc, Color::LightGrey);
  4160. DOCTEST_PRINTF_COLORED(msg,
  4161. threw_as ? Color::BrightGreen : isWarn ? Color::Yellow : Color::Red);
  4162. DOCTEST_PRINTF_COLORED(info1, Color::Cyan);
  4163. DOCTEST_PRINTF_COLORED(info2, Color::None);
  4164. DOCTEST_PRINTF_COLORED(info3, Color::Cyan);
  4165. String context = logContext();
  4166. DOCTEST_PRINTF_COLORED(context.c_str(), Color::None);
  4167. DOCTEST_PRINTF_COLORED("\n", Color::None);
  4168. printToDebugConsole(String(loc) + msg + info1 + info2 + info3 + context.c_str() + "\n");
  4169. }
  4170. void logAssertNothrow(bool threw, const String& exception, const char* expr,
  4171. assertType::Enum assert_type, const char* file, int line) {
  4172. char loc[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4173. DOCTEST_SNPRINTF(loc, DOCTEST_COUNTOF(loc), "%s(%d)", fileForOutput(file),
  4174. lineForOutput(line));
  4175. char msg[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4176. DOCTEST_SNPRINTF(msg, DOCTEST_COUNTOF(msg), " %s!\n",
  4177. threw ? getFailString(assert_type) : "PASSED");
  4178. char info1[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4179. DOCTEST_SNPRINTF(info1, DOCTEST_COUNTOF(info1), " %s( %s )\n",
  4180. getAssertString(assert_type), expr);
  4181. char info2[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4182. char info3[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4183. info2[0] = 0;
  4184. info3[0] = 0;
  4185. if(threw) {
  4186. DOCTEST_SNPRINTF(info2, DOCTEST_COUNTOF(info2), "threw exception:\n");
  4187. DOCTEST_SNPRINTF(info3, DOCTEST_COUNTOF(info3), " %s\n", exception.c_str());
  4188. }
  4189. bool isWarn = assert_type & assertType::is_warn;
  4190. DOCTEST_PRINTF_COLORED(loc, Color::LightGrey);
  4191. DOCTEST_PRINTF_COLORED(msg,
  4192. threw ? isWarn ? Color::Yellow : Color::Red : Color::BrightGreen);
  4193. DOCTEST_PRINTF_COLORED(info1, Color::Cyan);
  4194. DOCTEST_PRINTF_COLORED(info2, Color::None);
  4195. DOCTEST_PRINTF_COLORED(info3, Color::Cyan);
  4196. String context = logContext();
  4197. DOCTEST_PRINTF_COLORED(context.c_str(), Color::None);
  4198. DOCTEST_PRINTF_COLORED("\n", Color::None);
  4199. printToDebugConsole(String(loc) + msg + info1 + info2 + info3 + context.c_str() + "\n");
  4200. }
  4201. ResultBuilder::ResultBuilder(assertType::Enum assert_type, const char* file, int line,
  4202. const char* expr, const char* exception_type)
  4203. : m_assert_type(assert_type)
  4204. , m_file(file)
  4205. , m_line(line)
  4206. , m_expr(expr)
  4207. , m_exception_type(exception_type)
  4208. , m_threw(false)
  4209. , m_threw_as(false)
  4210. , m_failed(false) {
  4211. #ifdef _MSC_VER
  4212. if(m_expr[0] == ' ') // this happens when variadic macros are disabled under MSVC
  4213. ++m_expr;
  4214. #endif // _MSC_VER
  4215. }
  4216. ResultBuilder::~ResultBuilder() {}
  4217. void ResultBuilder::unexpectedExceptionOccurred() {
  4218. m_threw = true;
  4219. m_exception = translateActiveException();
  4220. }
  4221. bool ResultBuilder::log() {
  4222. if((m_assert_type & assertType::is_warn) == 0) //!OCLINT bitwise operator in conditional
  4223. contextState->numAssertionsForCurrentTestcase++;
  4224. if(m_assert_type & assertType::is_throws) { //!OCLINT bitwise operator in conditional
  4225. m_failed = !m_threw;
  4226. } else if(m_assert_type & //!OCLINT bitwise operator in conditional
  4227. assertType::is_throws_as) {
  4228. m_failed = !m_threw_as;
  4229. } else if(m_assert_type & //!OCLINT bitwise operator in conditional
  4230. assertType::is_nothrow) {
  4231. m_failed = m_threw;
  4232. } else {
  4233. m_failed = m_result;
  4234. }
  4235. if(m_failed || contextState->success) {
  4236. DOCTEST_LOG_START();
  4237. if(m_assert_type & assertType::is_throws) { //!OCLINT bitwise operator in conditional
  4238. logAssertThrows(m_threw, m_expr, m_assert_type, m_file, m_line);
  4239. } else if(m_assert_type & //!OCLINT bitwise operator in conditional
  4240. assertType::is_throws_as) {
  4241. logAssertThrowsAs(m_threw, m_threw_as, m_exception_type, m_exception, m_expr,
  4242. m_assert_type, m_file, m_line);
  4243. } else if(m_assert_type & //!OCLINT bitwise operator in conditional
  4244. assertType::is_nothrow) {
  4245. logAssertNothrow(m_threw, m_exception, m_expr, m_assert_type, m_file, m_line);
  4246. } else {
  4247. logAssert(m_result.m_passed, m_result.m_decomposition.c_str(), m_threw, m_exception,
  4248. m_expr, m_assert_type, m_file, m_line);
  4249. }
  4250. }
  4251. if(m_failed)
  4252. addFailedAssert(m_assert_type);
  4253. return m_failed && isDebuggerActive() && !contextState->no_breaks; // break into debugger
  4254. }
  4255. void ResultBuilder::react() const {
  4256. if(m_failed && checkIfShouldThrow(m_assert_type))
  4257. throwException();
  4258. }
  4259. MessageBuilder::MessageBuilder(const char* file, int line,
  4260. doctest::detail::assertType::Enum severity)
  4261. : m_stream(createStream())
  4262. , m_file(file)
  4263. , m_line(line)
  4264. , m_severity(severity) {}
  4265. bool MessageBuilder::log() {
  4266. DOCTEST_LOG_START();
  4267. bool is_warn = m_severity & doctest::detail::assertType::is_warn;
  4268. // warn is just a message in this context so we dont treat it as an assert
  4269. if(!is_warn) {
  4270. contextState->numAssertionsForCurrentTestcase++;
  4271. addFailedAssert(m_severity);
  4272. }
  4273. char loc[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4274. DOCTEST_SNPRINTF(loc, DOCTEST_COUNTOF(loc), "%s(%d)", fileForOutput(m_file),
  4275. lineForOutput(m_line));
  4276. char msg[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4277. DOCTEST_SNPRINTF(msg, DOCTEST_COUNTOF(msg), " %s!\n",
  4278. is_warn ? "MESSAGE" : getFailString(m_severity));
  4279. DOCTEST_PRINTF_COLORED(loc, Color::LightGrey);
  4280. DOCTEST_PRINTF_COLORED(msg, is_warn ? Color::Yellow : Color::Red);
  4281. String info = getStreamResult(m_stream);
  4282. if(info.size()) {
  4283. DOCTEST_PRINTF_COLORED(" ", Color::None);
  4284. DOCTEST_PRINTF_COLORED(info.c_str(), Color::None);
  4285. DOCTEST_PRINTF_COLORED("\n", Color::None);
  4286. }
  4287. String context = logContext();
  4288. DOCTEST_PRINTF_COLORED(context.c_str(), Color::None);
  4289. DOCTEST_PRINTF_COLORED("\n", Color::None);
  4290. printToDebugConsole(String(loc) + msg + " " + info.c_str() + "\n" + context.c_str() +
  4291. "\n");
  4292. return isDebuggerActive() && !contextState->no_breaks && !is_warn; // break into debugger
  4293. }
  4294. void MessageBuilder::react() {
  4295. if(m_severity & assertType::is_require) //!OCLINT bitwise operator in conditional
  4296. throwException();
  4297. }
  4298. MessageBuilder::~MessageBuilder() { freeStream(m_stream); }
  4299. // the implementation of parseFlag()
  4300. bool parseFlagImpl(int argc, const char* const* argv, const char* pattern) {
  4301. for(int i = argc - 1; i >= 0; --i) {
  4302. const char* temp = std::strstr(argv[i], pattern);
  4303. if(temp && my_strlen(temp) == my_strlen(pattern)) {
  4304. // eliminate strings in which the chars before the option are not '-'
  4305. bool noBadCharsFound = true; //!OCLINT prefer early exits and continue
  4306. while(temp != argv[i]) {
  4307. if(*--temp != '-') {
  4308. noBadCharsFound = false;
  4309. break;
  4310. }
  4311. }
  4312. if(noBadCharsFound && argv[i][0] == '-')
  4313. return true;
  4314. }
  4315. }
  4316. return false;
  4317. }
  4318. // locates a flag on the command line
  4319. bool parseFlag(int argc, const char* const* argv, const char* pattern) {
  4320. #ifndef DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
  4321. if(!parseFlagImpl(argc, argv, pattern))
  4322. return parseFlagImpl(argc, argv, pattern + 3); // 3 for "dt-"
  4323. return true;
  4324. #else // DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
  4325. return parseFlagImpl(argc, argv, pattern);
  4326. #endif // DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
  4327. }
  4328. // the implementation of parseOption()
  4329. bool parseOptionImpl(int argc, const char* const* argv, const char* pattern, String& res) {
  4330. for(int i = argc - 1; i >= 0; --i) {
  4331. const char* temp = std::strstr(argv[i], pattern);
  4332. if(temp) { //!OCLINT prefer early exits and continue
  4333. // eliminate matches in which the chars before the option are not '-'
  4334. bool noBadCharsFound = true;
  4335. const char* curr = argv[i];
  4336. while(curr != temp) {
  4337. if(*curr++ != '-') {
  4338. noBadCharsFound = false;
  4339. break;
  4340. }
  4341. }
  4342. if(noBadCharsFound && argv[i][0] == '-') {
  4343. temp += my_strlen(pattern);
  4344. unsigned len = my_strlen(temp);
  4345. if(len) {
  4346. res = temp;
  4347. return true;
  4348. }
  4349. }
  4350. }
  4351. }
  4352. return false;
  4353. }
  4354. // parses an option and returns the string after the '=' character
  4355. bool parseOption(int argc, const char* const* argv, const char* pattern, String& res,
  4356. const String& defaultVal = String()) {
  4357. res = defaultVal;
  4358. #ifndef DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
  4359. if(!parseOptionImpl(argc, argv, pattern, res))
  4360. return parseOptionImpl(argc, argv, pattern + 3, res); // 3 for "dt-"
  4361. return true;
  4362. #else // DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
  4363. return parseOptionImpl(argc, argv, pattern, res);
  4364. #endif // DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
  4365. }
  4366. // parses a comma separated list of words after a pattern in one of the arguments in argv
  4367. bool parseCommaSepArgs(int argc, const char* const* argv, const char* pattern,
  4368. std::vector<String>& res) {
  4369. String filtersString;
  4370. if(parseOption(argc, argv, pattern, filtersString)) {
  4371. // tokenize with "," as a separator
  4372. // cppcheck-suppress strtokCalled
  4373. char* pch = std::strtok(filtersString.c_str(), ","); // modifies the string
  4374. while(pch != 0) {
  4375. if(my_strlen(pch))
  4376. res.push_back(pch);
  4377. // uses the strtok() internal state to go to the next token
  4378. // cppcheck-suppress strtokCalled
  4379. pch = std::strtok(0, ",");
  4380. }
  4381. return true;
  4382. }
  4383. return false;
  4384. }
  4385. enum optionType
  4386. {
  4387. option_bool,
  4388. option_int
  4389. };
  4390. // parses an int/bool option from the command line
  4391. bool parseIntOption(int argc, const char* const* argv, const char* pattern, optionType type,
  4392. int& res) {
  4393. String parsedValue;
  4394. if(!parseOption(argc, argv, pattern, parsedValue))
  4395. return false;
  4396. if(type == 0) {
  4397. // boolean
  4398. const char positive[][5] = {"1", "true", "on", "yes"}; // 5 - strlen("true") + 1
  4399. const char negative[][6] = {"0", "false", "off", "no"}; // 6 - strlen("false") + 1
  4400. // if the value matches any of the positive/negative possibilities
  4401. for(unsigned i = 0; i < 4; i++) {
  4402. if(parsedValue.compare(positive[i], true) == 0) {
  4403. res = 1; //!OCLINT parameter reassignment
  4404. return true;
  4405. }
  4406. if(parsedValue.compare(negative[i], true) == 0) {
  4407. res = 0; //!OCLINT parameter reassignment
  4408. return true;
  4409. }
  4410. }
  4411. } else {
  4412. // integer
  4413. int theInt = std::atoi(parsedValue.c_str()); // NOLINT
  4414. if(theInt != 0) {
  4415. res = theInt; //!OCLINT parameter reassignment
  4416. return true;
  4417. }
  4418. }
  4419. return false;
  4420. }
  4421. void printVersion() {
  4422. if(contextState->no_version == false) {
  4423. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4424. std::printf("doctest version is \"%s\"\n", DOCTEST_VERSION_STR);
  4425. }
  4426. }
  4427. void printHelp() {
  4428. printVersion();
  4429. // clang-format off
  4430. DOCTEST_PRINTF_COLORED("[doctest]\n", Color::Cyan);
  4431. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4432. std::printf("boolean values: \"1/on/yes/true\" or \"0/off/no/false\"\n");
  4433. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4434. std::printf("filter values: \"str1,str2,str3\" (comma separated strings)\n");
  4435. DOCTEST_PRINTF_COLORED("[doctest]\n", Color::Cyan);
  4436. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4437. std::printf("filters use wildcards for matching strings\n");
  4438. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4439. std::printf("something passes a filter if any of the strings in a filter matches\n");
  4440. DOCTEST_PRINTF_COLORED("[doctest]\n", Color::Cyan);
  4441. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4442. std::printf("ALL FLAGS, OPTIONS AND FILTERS ALSO AVAILABLE WITH A \"dt-\" PREFIX!!!\n");
  4443. DOCTEST_PRINTF_COLORED("[doctest]\n", Color::Cyan);
  4444. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4445. std::printf("Query flags - the program quits after them. Available:\n\n");
  4446. std::printf(" -?, --help, -h prints this message\n");
  4447. std::printf(" -v, --version prints the version\n");
  4448. std::printf(" -c, --count prints the number of matching tests\n");
  4449. std::printf(" -ltc, --list-test-cases lists all matching tests by name\n");
  4450. std::printf(" -lts, --list-test-suites lists all matching test suites\n\n");
  4451. // ========================================================================================= << 79
  4452. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4453. std::printf("The available <int>/<string> options/filters are:\n\n");
  4454. std::printf(" -tc, --test-case=<filters> filters tests by their name\n");
  4455. std::printf(" -tce, --test-case-exclude=<filters> filters OUT tests by their name\n");
  4456. std::printf(" -sf, --source-file=<filters> filters tests by their file\n");
  4457. std::printf(" -sfe, --source-file-exclude=<filters> filters OUT tests by their file\n");
  4458. std::printf(" -ts, --test-suite=<filters> filters tests by their test suite\n");
  4459. std::printf(" -tse, --test-suite-exclude=<filters> filters OUT tests by their test suite\n");
  4460. std::printf(" -sc, --subcase=<filters> filters subcases by their name\n");
  4461. std::printf(" -sce, --subcase-exclude=<filters> filters OUT subcases by their name\n");
  4462. std::printf(" -ob, --order-by=<string> how the tests should be ordered\n");
  4463. std::printf(" <string> - by [file/suite/name/rand]\n");
  4464. std::printf(" -rs, --rand-seed=<int> seed for random ordering\n");
  4465. std::printf(" -f, --first=<int> the first test passing the filters to\n");
  4466. std::printf(" execute - for range-based execution\n");
  4467. std::printf(" -l, --last=<int> the last test passing the filters to\n");
  4468. std::printf(" execute - for range-based execution\n");
  4469. std::printf(" -aa, --abort-after=<int> stop after <int> failed assertions\n");
  4470. std::printf(" -scfl,--subcase-filter-levels=<int> apply filters for the first <int> levels\n");
  4471. DOCTEST_PRINTF_COLORED("\n[doctest] ", Color::Cyan);
  4472. std::printf("Bool options - can be used like flags and true is assumed. Available:\n\n");
  4473. std::printf(" -s, --success=<bool> include successful assertions in output\n");
  4474. std::printf(" -cs, --case-sensitive=<bool> filters being treated as case sensitive\n");
  4475. std::printf(" -e, --exit=<bool> exits after the tests finish\n");
  4476. std::printf(" -d, --duration=<bool> prints the time duration of each test\n");
  4477. std::printf(" -nt, --no-throw=<bool> skips exceptions-related assert checks\n");
  4478. std::printf(" -ne, --no-exitcode=<bool> returns (or exits) always with success\n");
  4479. std::printf(" -nr, --no-run=<bool> skips all runtime doctest operations\n");
  4480. std::printf(" -nv, --no-version=<bool> omit the framework version in the output\n");
  4481. std::printf(" -nc, --no-colors=<bool> disables colors in output\n");
  4482. std::printf(" -fc, --force-colors=<bool> use colors even when not in a tty\n");
  4483. std::printf(" -nb, --no-breaks=<bool> disables breakpoints in debuggers\n");
  4484. std::printf(" -ns, --no-skip=<bool> don't skip test cases marked as skip\n");
  4485. std::printf(" -npf, --no-path-filenames=<bool> only filenames and no paths in output\n");
  4486. std::printf(" -nln, --no-line-numbers=<bool> 0 instead of real line numbers in output\n");
  4487. // ========================================================================================= << 79
  4488. // clang-format on
  4489. DOCTEST_PRINTF_COLORED("\n[doctest] ", Color::Cyan);
  4490. std::printf("for more information visit the project documentation\n\n");
  4491. }
  4492. void printSummary() {
  4493. const ContextState* p = contextState;
  4494. DOCTEST_PRINTF_COLORED(getSeparator(), Color::Yellow);
  4495. if(p->count || p->list_test_cases) {
  4496. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4497. std::printf("unskipped test cases passing the current filters: %u\n",
  4498. p->numTestsPassingFilters);
  4499. } else if(p->list_test_suites) {
  4500. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4501. std::printf("unskipped test cases passing the current filters: %u\n",
  4502. p->numTestsPassingFilters);
  4503. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4504. std::printf("test suites with unskipped test cases passing the current filters: %u\n",
  4505. p->numTestSuitesPassingFilters);
  4506. } else {
  4507. bool anythingFailed = p->numFailed > 0 || p->numFailedAssertions > 0;
  4508. char buff[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4509. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4510. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), "test cases: %6u",
  4511. p->numTestsPassingFilters);
  4512. DOCTEST_PRINTF_COLORED(buff, Color::None);
  4513. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), " | ");
  4514. DOCTEST_PRINTF_COLORED(buff, Color::None);
  4515. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), "%6d passed",
  4516. p->numTestsPassingFilters - p->numFailed);
  4517. DOCTEST_PRINTF_COLORED(buff,
  4518. (p->numTestsPassingFilters == 0 || anythingFailed) ?
  4519. Color::None :
  4520. Color::Green);
  4521. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), " | ");
  4522. DOCTEST_PRINTF_COLORED(buff, Color::None);
  4523. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), "%6u failed", p->numFailed);
  4524. DOCTEST_PRINTF_COLORED(buff, p->numFailed > 0 ? Color::Red : Color::None);
  4525. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), " | ");
  4526. DOCTEST_PRINTF_COLORED(buff, Color::None);
  4527. if(p->no_skipped_summary == false) {
  4528. int numSkipped = static_cast<unsigned>(getRegisteredTests().size()) -
  4529. p->numTestsPassingFilters;
  4530. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), "%6d skipped", numSkipped);
  4531. DOCTEST_PRINTF_COLORED(buff, numSkipped == 0 ? Color::None : Color::Yellow);
  4532. }
  4533. DOCTEST_PRINTF_COLORED("\n", Color::None);
  4534. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4535. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), "assertions: %6d", p->numAssertions);
  4536. DOCTEST_PRINTF_COLORED(buff, Color::None);
  4537. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), " | ");
  4538. DOCTEST_PRINTF_COLORED(buff, Color::None);
  4539. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), "%6d passed",
  4540. p->numAssertions - p->numFailedAssertions);
  4541. DOCTEST_PRINTF_COLORED(
  4542. buff, (p->numAssertions == 0 || anythingFailed) ? Color::None : Color::Green);
  4543. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), " | ");
  4544. DOCTEST_PRINTF_COLORED(buff, Color::None);
  4545. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), "%6d failed", p->numFailedAssertions);
  4546. DOCTEST_PRINTF_COLORED(buff, p->numFailedAssertions > 0 ? Color::Red : Color::None);
  4547. DOCTEST_SNPRINTF(buff, DOCTEST_COUNTOF(buff), " |\n");
  4548. DOCTEST_PRINTF_COLORED(buff, Color::None);
  4549. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4550. DOCTEST_PRINTF_COLORED("Status: ", Color::None);
  4551. const char* result = (p->numFailed > 0) ? "FAILURE!\n" : "SUCCESS!\n";
  4552. DOCTEST_PRINTF_COLORED(result, p->numFailed > 0 ? Color::Red : Color::Green);
  4553. }
  4554. // remove any coloring
  4555. DOCTEST_PRINTF_COLORED("", Color::None);
  4556. }
  4557. } // namespace detail
  4558. bool isRunningInTest() { return detail::contextState != 0; }
  4559. Context::Context(int argc, const char* const* argv)
  4560. : p(new detail::ContextState) {
  4561. parseArgs(argc, argv, true);
  4562. }
  4563. Context::~Context() { delete p; }
  4564. void Context::applyCommandLine(int argc, const char* const* argv) { parseArgs(argc, argv); }
  4565. // parses args
  4566. void Context::parseArgs(int argc, const char* const* argv, bool withDefaults) {
  4567. using namespace detail;
  4568. // clang-format off
  4569. parseCommaSepArgs(argc, argv, "dt-source-file=", p->filters[0]);
  4570. parseCommaSepArgs(argc, argv, "dt-sf=", p->filters[0]);
  4571. parseCommaSepArgs(argc, argv, "dt-source-file-exclude=",p->filters[1]);
  4572. parseCommaSepArgs(argc, argv, "dt-sfe=", p->filters[1]);
  4573. parseCommaSepArgs(argc, argv, "dt-test-suite=", p->filters[2]);
  4574. parseCommaSepArgs(argc, argv, "dt-ts=", p->filters[2]);
  4575. parseCommaSepArgs(argc, argv, "dt-test-suite-exclude=", p->filters[3]);
  4576. parseCommaSepArgs(argc, argv, "dt-tse=", p->filters[3]);
  4577. parseCommaSepArgs(argc, argv, "dt-test-case=", p->filters[4]);
  4578. parseCommaSepArgs(argc, argv, "dt-tc=", p->filters[4]);
  4579. parseCommaSepArgs(argc, argv, "dt-test-case-exclude=", p->filters[5]);
  4580. parseCommaSepArgs(argc, argv, "dt-tce=", p->filters[5]);
  4581. parseCommaSepArgs(argc, argv, "dt-subcase=", p->filters[6]);
  4582. parseCommaSepArgs(argc, argv, "dt-sc=", p->filters[6]);
  4583. parseCommaSepArgs(argc, argv, "dt-subcase-exclude=", p->filters[7]);
  4584. parseCommaSepArgs(argc, argv, "dt-sce=", p->filters[7]);
  4585. // clang-format on
  4586. int intRes = 0;
  4587. String strRes;
  4588. #define DOCTEST_PARSE_AS_BOOL_OR_FLAG(name, sname, var, default) \
  4589. if(parseIntOption(argc, argv, DOCTEST_STR_CONCAT_TOSTR(name, =), option_bool, intRes) || \
  4590. parseIntOption(argc, argv, DOCTEST_STR_CONCAT_TOSTR(sname, =), option_bool, intRes)) \
  4591. p->var = !!intRes; \
  4592. else if(parseFlag(argc, argv, #name) || parseFlag(argc, argv, #sname)) \
  4593. p->var = true; \
  4594. else if(withDefaults) \
  4595. p->var = default
  4596. #define DOCTEST_PARSE_INT_OPTION(name, sname, var, default) \
  4597. if(parseIntOption(argc, argv, DOCTEST_STR_CONCAT_TOSTR(name, =), option_int, intRes) || \
  4598. parseIntOption(argc, argv, DOCTEST_STR_CONCAT_TOSTR(sname, =), option_int, intRes)) \
  4599. p->var = intRes; \
  4600. else if(withDefaults) \
  4601. p->var = default
  4602. #define DOCTEST_PARSE_STR_OPTION(name, sname, var, default) \
  4603. if(parseOption(argc, argv, DOCTEST_STR_CONCAT_TOSTR(name, =), strRes, default) || \
  4604. parseOption(argc, argv, DOCTEST_STR_CONCAT_TOSTR(sname, =), strRes, default) || \
  4605. withDefaults) \
  4606. p->var = strRes
  4607. // clang-format off
  4608. DOCTEST_PARSE_STR_OPTION(dt-order-by, dt-ob, order_by, "file");
  4609. DOCTEST_PARSE_INT_OPTION(dt-rand-seed, dt-rs, rand_seed, 0);
  4610. DOCTEST_PARSE_INT_OPTION(dt-first, dt-f, first, 1);
  4611. DOCTEST_PARSE_INT_OPTION(dt-last, dt-l, last, 0);
  4612. DOCTEST_PARSE_INT_OPTION(dt-abort-after, dt-aa, abort_after, 0);
  4613. DOCTEST_PARSE_INT_OPTION(dt-subcase-filter-levels, dt-scfl, subcase_filter_levels, 2000000000);
  4614. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-success, dt-s, success, false);
  4615. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-case-sensitive, dt-cs, case_sensitive, false);
  4616. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-exit, dt-e, exit, false);
  4617. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-duration, dt-d, duration, false);
  4618. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-no-throw, dt-nt, no_throw, false);
  4619. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-no-exitcode, dt-ne, no_exitcode, false);
  4620. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-no-run, dt-nr, no_run, false);
  4621. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-no-version, dt-nv, no_version, false);
  4622. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-no-colors, dt-nc, no_colors, false);
  4623. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-force-colors, dt-fc, force_colors, false);
  4624. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-no-breaks, dt-nb, no_breaks, false);
  4625. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-no-skip, dt-ns, no_skip, false);
  4626. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-no-path-filenames, dt-npf, no_path_in_filenames, false);
  4627. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-no-line-numbers, dt-nln, no_line_numbers, false);
  4628. DOCTEST_PARSE_AS_BOOL_OR_FLAG(dt-no-skipped-summary, dt-nss, no_skipped_summary, false);
  4629. // clang-format on
  4630. #undef DOCTEST_PARSE_STR_OPTION
  4631. #undef DOCTEST_PARSE_INT_OPTION
  4632. #undef DOCTEST_PARSE_AS_BOOL_OR_FLAG
  4633. if(withDefaults) {
  4634. p->help = false;
  4635. p->version = false;
  4636. p->count = false;
  4637. p->list_test_cases = false;
  4638. p->list_test_suites = false;
  4639. }
  4640. if(parseFlag(argc, argv, "dt-help") || parseFlag(argc, argv, "dt-h") ||
  4641. parseFlag(argc, argv, "dt-?")) {
  4642. p->help = true;
  4643. p->exit = true;
  4644. }
  4645. if(parseFlag(argc, argv, "dt-version") || parseFlag(argc, argv, "dt-v")) {
  4646. p->version = true;
  4647. p->exit = true;
  4648. }
  4649. if(parseFlag(argc, argv, "dt-count") || parseFlag(argc, argv, "dt-c")) {
  4650. p->count = true;
  4651. p->exit = true;
  4652. }
  4653. if(parseFlag(argc, argv, "dt-list-test-cases") || parseFlag(argc, argv, "dt-ltc")) {
  4654. p->list_test_cases = true;
  4655. p->exit = true;
  4656. }
  4657. if(parseFlag(argc, argv, "dt-list-test-suites") || parseFlag(argc, argv, "dt-lts")) {
  4658. p->list_test_suites = true;
  4659. p->exit = true;
  4660. }
  4661. }
  4662. // allows the user to add procedurally to the filters from the command line
  4663. void Context::addFilter(const char* filter, const char* value) { setOption(filter, value); }
  4664. // allows the user to clear all filters from the command line
  4665. void Context::clearFilters() {
  4666. for(unsigned i = 0; i < p->filters.size(); ++i)
  4667. p->filters[i].clear();
  4668. }
  4669. // allows the user to override procedurally the int/bool options from the command line
  4670. void Context::setOption(const char* option, int value) {
  4671. setOption(option, toString(value).c_str());
  4672. }
  4673. // allows the user to override procedurally the string options from the command line
  4674. void Context::setOption(const char* option, const char* value) {
  4675. String argv = String("-") + option + "=" + value;
  4676. const char* lvalue = argv.c_str();
  4677. parseArgs(1, &lvalue);
  4678. }
  4679. // users should query this in their main() and exit the program if true
  4680. bool Context::shouldExit() { return p->exit; }
  4681. // the main function that does all the filtering and test running
  4682. int Context::run() {
  4683. using namespace detail;
  4684. Color::init();
  4685. contextState = p;
  4686. p->resetRunData();
  4687. // handle version, help and no_run
  4688. if(p->no_run || p->version || p->help) {
  4689. if(p->version)
  4690. printVersion();
  4691. if(p->help)
  4692. printHelp();
  4693. contextState = 0;
  4694. return EXIT_SUCCESS;
  4695. }
  4696. printVersion();
  4697. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4698. std::printf("run with \"--help\" for options\n");
  4699. unsigned i = 0; // counter used for loops - here for VC6
  4700. std::set<TestCase>& registeredTests = getRegisteredTests();
  4701. std::vector<const TestCase*> testArray;
  4702. for(std::set<TestCase>::iterator it = registeredTests.begin(); it != registeredTests.end();
  4703. ++it)
  4704. testArray.push_back(&(*it));
  4705. // sort the collected records
  4706. if(!testArray.empty()) {
  4707. if(p->order_by.compare("file", true) == 0) {
  4708. std::qsort(&testArray[0], testArray.size(), sizeof(TestCase*), fileOrderComparator);
  4709. } else if(p->order_by.compare("suite", true) == 0) {
  4710. std::qsort(&testArray[0], testArray.size(), sizeof(TestCase*), suiteOrderComparator);
  4711. } else if(p->order_by.compare("name", true) == 0) {
  4712. std::qsort(&testArray[0], testArray.size(), sizeof(TestCase*), nameOrderComparator);
  4713. } else if(p->order_by.compare("rand", true) == 0) {
  4714. std::srand(p->rand_seed);
  4715. // random_shuffle implementation
  4716. const TestCase** first = &testArray[0];
  4717. for(i = testArray.size() - 1; i > 0; --i) {
  4718. int idxToSwap = std::rand() % (i + 1); // NOLINT
  4719. const TestCase* temp = first[i];
  4720. first[i] = first[idxToSwap];
  4721. first[idxToSwap] = temp;
  4722. }
  4723. }
  4724. }
  4725. if(p->list_test_cases) {
  4726. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4727. std::printf("listing all test case names\n");
  4728. DOCTEST_PRINTF_COLORED(getSeparator(), Color::Yellow);
  4729. }
  4730. std::set<String> testSuitesPassingFilters;
  4731. if(p->list_test_suites) {
  4732. DOCTEST_PRINTF_COLORED("[doctest] ", Color::Cyan);
  4733. std::printf("listing all test suites\n");
  4734. DOCTEST_PRINTF_COLORED(getSeparator(), Color::Yellow);
  4735. }
  4736. // invoke the registered functions if they match the filter criteria (or just count them)
  4737. for(i = 0; i < testArray.size(); i++) {
  4738. const TestCase& data = *testArray[i];
  4739. if(data.m_skip && !p->no_skip)
  4740. continue;
  4741. if(!matchesAny(data.m_file, p->filters[0], 1, p->case_sensitive))
  4742. continue;
  4743. if(matchesAny(data.m_file, p->filters[1], 0, p->case_sensitive))
  4744. continue;
  4745. if(!matchesAny(data.m_test_suite, p->filters[2], 1, p->case_sensitive))
  4746. continue;
  4747. if(matchesAny(data.m_test_suite, p->filters[3], 0, p->case_sensitive))
  4748. continue;
  4749. if(!matchesAny(data.m_name, p->filters[4], 1, p->case_sensitive))
  4750. continue;
  4751. if(matchesAny(data.m_name, p->filters[5], 0, p->case_sensitive))
  4752. continue;
  4753. p->numTestsPassingFilters++;
  4754. // do not execute the test if we are to only count the number of filter passing tests
  4755. if(p->count)
  4756. continue;
  4757. // print the name of the test and don't execute it
  4758. if(p->list_test_cases) {
  4759. std::printf("%s\n", data.m_name);
  4760. continue;
  4761. }
  4762. // print the name of the test suite if not done already and don't execute it
  4763. if(p->list_test_suites) {
  4764. if((testSuitesPassingFilters.count(data.m_test_suite) == 0) &&
  4765. data.m_test_suite[0] != '\0') {
  4766. std::printf("%s\n", data.m_test_suite);
  4767. testSuitesPassingFilters.insert(data.m_test_suite);
  4768. p->numTestSuitesPassingFilters++;
  4769. }
  4770. continue;
  4771. }
  4772. // skip the test if it is not in the execution range
  4773. if((p->last < p->numTestsPassingFilters && p->first <= p->last) ||
  4774. (p->first > p->numTestsPassingFilters))
  4775. continue;
  4776. // execute the test if it passes all the filtering
  4777. {
  4778. p->currentTest = &data;
  4779. bool failed = false;
  4780. p->hasLoggedCurrentTestStart = false;
  4781. p->numFailedAssertionsForCurrentTestcase = 0;
  4782. p->subcasesPassed.clear();
  4783. double duration = 0;
  4784. Timer timer;
  4785. timer.start();
  4786. do {
  4787. // if the start has been logged from a previous iteration of this loop
  4788. if(p->hasLoggedCurrentTestStart)
  4789. logTestEnd();
  4790. p->hasLoggedCurrentTestStart = false;
  4791. // if logging successful tests - force the start log
  4792. if(p->success)
  4793. DOCTEST_LOG_START();
  4794. // reset the assertion state
  4795. p->numAssertionsForCurrentTestcase = 0;
  4796. p->hasCurrentTestFailed = false;
  4797. // reset some of the fields for subcases (except for the set of fully passed ones)
  4798. p->subcasesHasSkipped = false;
  4799. p->subcasesCurrentLevel = 0;
  4800. p->subcasesEnteredLevels.clear();
  4801. // reset stuff for logging with INFO()
  4802. p->exceptionalContexts.clear();
  4803. // execute the test
  4804. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  4805. try {
  4806. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  4807. FatalConditionHandler fatalConditionHandler; // Handle signals
  4808. data.m_test();
  4809. fatalConditionHandler.reset();
  4810. if(contextState->hasCurrentTestFailed)
  4811. failed = true;
  4812. #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
  4813. } catch(const TestFailureException&) { failed = true; } catch(...) {
  4814. DOCTEST_LOG_START();
  4815. logTestException(translateActiveException());
  4816. failed = true;
  4817. }
  4818. #endif // DOCTEST_CONFIG_NO_EXCEPTIONS
  4819. p->numAssertions += p->numAssertionsForCurrentTestcase;
  4820. // exit this loop if enough assertions have failed
  4821. if(p->abort_after > 0 && p->numFailedAssertions >= p->abort_after) {
  4822. p->subcasesHasSkipped = false;
  4823. DOCTEST_PRINTF_COLORED("Aborting - too many failed asserts!\n", Color::Red);
  4824. }
  4825. } while(p->subcasesHasSkipped == true);
  4826. duration = timer.getElapsedSeconds();
  4827. if(Approx(p->currentTest->m_timeout).epsilon(DBL_EPSILON) != 0 &&
  4828. Approx(duration).epsilon(DBL_EPSILON) > p->currentTest->m_timeout) {
  4829. failed = true;
  4830. DOCTEST_LOG_START();
  4831. char msg[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4832. DOCTEST_SNPRINTF(msg, DOCTEST_COUNTOF(msg),
  4833. "Test case exceeded time limit of %.6f!\n",
  4834. p->currentTest->m_timeout);
  4835. DOCTEST_PRINTF_COLORED(msg, Color::Red);
  4836. }
  4837. if(p->duration) {
  4838. char msg[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4839. DOCTEST_SNPRINTF(msg, DOCTEST_COUNTOF(msg), "%.6f s: %s\n", duration,
  4840. p->currentTest->m_name);
  4841. DOCTEST_PRINTF_COLORED(msg, Color::None);
  4842. }
  4843. if(data.m_should_fail) {
  4844. DOCTEST_LOG_START();
  4845. if(failed) {
  4846. failed = false;
  4847. DOCTEST_PRINTF_COLORED("Failed as expected so marking it as not failed\n",
  4848. Color::Yellow);
  4849. } else {
  4850. failed = true;
  4851. DOCTEST_PRINTF_COLORED("Should have failed but didn't! Marking it as failed!\n",
  4852. Color::Red);
  4853. }
  4854. } else if(failed && data.m_may_fail) {
  4855. DOCTEST_LOG_START();
  4856. failed = false;
  4857. DOCTEST_PRINTF_COLORED("Allowed to fail so marking it as not failed\n",
  4858. Color::Yellow);
  4859. } else if(data.m_expected_failures > 0) {
  4860. DOCTEST_LOG_START();
  4861. char msg[DOCTEST_SNPRINTF_BUFFER_LENGTH];
  4862. if(p->numFailedAssertionsForCurrentTestcase == data.m_expected_failures) {
  4863. failed = false;
  4864. DOCTEST_SNPRINTF(
  4865. msg, DOCTEST_COUNTOF(msg),
  4866. "Failed exactly %d times as expected so marking it as not failed!\n",
  4867. data.m_expected_failures);
  4868. DOCTEST_PRINTF_COLORED(msg, Color::Yellow);
  4869. } else {
  4870. failed = true;
  4871. DOCTEST_SNPRINTF(msg, DOCTEST_COUNTOF(msg),
  4872. "Didn't fail exactly %d times so marking it as failed!\n",
  4873. data.m_expected_failures);
  4874. DOCTEST_PRINTF_COLORED(msg, Color::Red);
  4875. }
  4876. }
  4877. if(p->hasLoggedCurrentTestStart)
  4878. logTestEnd();
  4879. if(failed) // if any subcase has failed - the whole test case has failed
  4880. p->numFailed++;
  4881. // stop executing tests if enough assertions have failed
  4882. if(p->abort_after > 0 && p->numFailedAssertions >= p->abort_after)
  4883. break;
  4884. }
  4885. }
  4886. printSummary();
  4887. contextState = 0;
  4888. if(p->numFailed && !p->no_exitcode)
  4889. return EXIT_FAILURE;
  4890. return EXIT_SUCCESS;
  4891. }
  4892. } // namespace doctest
  4893. #endif // DOCTEST_CONFIG_DISABLE
  4894. #ifdef DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
  4895. int main(int argc, char** argv) { return doctest::Context(argc, argv).run(); }
  4896. #endif // DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
  4897. #endif // DOCTEST_LIBRARY_IMPLEMENTATION
  4898. #endif // DOCTEST_CONFIG_IMPLEMENT
  4899. #if defined(__clang__)
  4900. #pragma clang diagnostic pop
  4901. #endif // __clang__
  4902. #if defined(__GNUC__) && !defined(__clang__)
  4903. #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6)
  4904. #pragma GCC diagnostic pop
  4905. #endif // > gcc 4.6
  4906. #endif // __GNUC__
  4907. #ifdef _MSC_VER
  4908. #pragma warning(pop)
  4909. #endif // _MSC_VER