Misc.tests.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #include <catch2/catch_template_test_macros.hpp>
  8. #include <catch2/internal/catch_config_wchar.hpp>
  9. #include <catch2/internal/catch_windows_h_proxy.hpp>
  10. #ifdef __clang__
  11. # pragma clang diagnostic ignored "-Wc++98-compat"
  12. # pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  13. #endif
  14. #include <iostream>
  15. #include <cerrno>
  16. #include <limits>
  17. #include <array>
  18. #include <tuple>
  19. namespace {
  20. static const char* makeString(bool makeNull) {
  21. return makeNull ? nullptr : "valid string";
  22. }
  23. static bool testCheckedIf(bool flag) {
  24. CHECKED_IF(flag)
  25. return true;
  26. else
  27. return false;
  28. }
  29. static bool testCheckedElse(bool flag) {
  30. CHECKED_ELSE(flag)
  31. return false;
  32. return true;
  33. }
  34. static unsigned int Factorial(unsigned int number) {
  35. return number > 1 ? Factorial(number - 1) * number : 1;
  36. }
  37. static int f() {
  38. return 1;
  39. }
  40. static void manuallyRegisteredTestFunction() {
  41. SUCCEED("was called");
  42. }
  43. struct AutoTestReg {
  44. AutoTestReg() {
  45. REGISTER_TEST_CASE(manuallyRegisteredTestFunction, "ManuallyRegistered");
  46. }
  47. };
  48. CATCH_INTERNAL_START_WARNINGS_SUPPRESSION
  49. CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS
  50. static AutoTestReg autoTestReg;
  51. CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION
  52. template<typename T>
  53. struct Foo {
  54. size_t size() { return 0; }
  55. };
  56. template<typename T, size_t S>
  57. struct Bar {
  58. size_t size() { return S; }
  59. };
  60. }
  61. TEST_CASE( "random SECTION tests", "[.][sections][failing]" ) {
  62. int a = 1;
  63. int b = 2;
  64. SECTION( "doesn't equal" ) {
  65. REQUIRE( a != b );
  66. REQUIRE( b != a );
  67. }
  68. SECTION( "not equal" ) {
  69. REQUIRE( a != b);
  70. }
  71. }
  72. TEST_CASE( "nested SECTION tests", "[.][sections][failing]" ) {
  73. int a = 1;
  74. int b = 2;
  75. SECTION( "doesn't equal" ) {
  76. REQUIRE( a != b );
  77. REQUIRE( b != a );
  78. SECTION( "not equal" ) {
  79. REQUIRE( a != b);
  80. }
  81. }
  82. }
  83. TEST_CASE( "more nested SECTION tests", "[sections][failing][.]" ) {
  84. int a = 1;
  85. int b = 2;
  86. SECTION( "doesn't equal" ) {
  87. SECTION( "equal" ) {
  88. REQUIRE( a == b );
  89. }
  90. SECTION( "not equal" ) {
  91. REQUIRE( a != b );
  92. }
  93. SECTION( "less than" ) {
  94. REQUIRE( a < b );
  95. }
  96. }
  97. }
  98. TEST_CASE( "even more nested SECTION tests", "[sections]" ) {
  99. SECTION( "c" ) {
  100. SECTION( "d (leaf)" ) {
  101. SUCCEED(); // avoid failing due to no tests
  102. }
  103. SECTION( "e (leaf)" ) {
  104. SUCCEED(); // avoid failing due to no tests
  105. }
  106. }
  107. SECTION( "f (leaf)" ) {
  108. SUCCEED(); // avoid failing due to no tests
  109. }
  110. }
  111. TEST_CASE( "looped SECTION tests", "[.][failing][sections]" ) {
  112. int a = 1;
  113. for( int b = 0; b < 10; ++b ) {
  114. DYNAMIC_SECTION( "b is currently: " << b ) {
  115. CHECK( b > a );
  116. }
  117. }
  118. }
  119. TEST_CASE( "looped tests", "[.][failing]" ) {
  120. static const int fib[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
  121. for( std::size_t i=0; i < sizeof(fib)/sizeof(int); ++i ) {
  122. INFO( "Testing if fib[" << i << "] (" << fib[i] << ") is even" );
  123. CHECK( ( fib[i] % 2 ) == 0 );
  124. }
  125. }
  126. TEST_CASE( "Sends stuff to stdout and stderr", "[.]" ) {
  127. std::cout << "A string sent directly to stdout" << std::endl;
  128. std::cerr << "A string sent directly to stderr" << std::endl;
  129. std::clog << "A string sent to stderr via clog" << std::endl;
  130. }
  131. TEST_CASE( "null strings" ) {
  132. REQUIRE( makeString( false ) != static_cast<char*>(nullptr));
  133. REQUIRE( makeString( true ) == static_cast<char*>(nullptr));
  134. }
  135. TEST_CASE( "checkedIf" ) {
  136. REQUIRE( testCheckedIf( true ) );
  137. }
  138. TEST_CASE( "checkedIf, failing", "[failing][.]" ) {
  139. REQUIRE( testCheckedIf( false ) );
  140. }
  141. TEST_CASE( "checkedElse" ) {
  142. REQUIRE( testCheckedElse( true ) );
  143. }
  144. TEST_CASE( "checkedElse, failing", "[failing][.]" ) {
  145. REQUIRE( testCheckedElse( false ) );
  146. }
  147. TEST_CASE("Testing checked-if", "[checked-if]") {
  148. CHECKED_IF(true) {
  149. SUCCEED();
  150. }
  151. CHECKED_IF(false) {
  152. FAIL();
  153. }
  154. CHECKED_ELSE(true) {
  155. FAIL();
  156. }
  157. CHECKED_ELSE(false) {
  158. SUCCEED();
  159. }
  160. }
  161. TEST_CASE("Testing checked-if 2", "[checked-if][!shouldfail]") {
  162. CHECKED_IF(true) {
  163. FAIL();
  164. }
  165. // If the checked if is not entered, this passes and the test
  166. // fails, because of the [!shouldfail] tag.
  167. SUCCEED();
  168. }
  169. TEST_CASE("Testing checked-if 3", "[checked-if][!shouldfail]") {
  170. CHECKED_ELSE(false) {
  171. FAIL();
  172. }
  173. // If the checked false is not entered, this passes and the test
  174. // fails, because of the [!shouldfail] tag.
  175. SUCCEED();
  176. }
  177. TEST_CASE( "xmlentitycheck" ) {
  178. SECTION( "embedded xml: <test>it should be possible to embed xml characters, such as <, \" or &, or even whole <xml>documents</xml> within an attribute</test>" ) {
  179. SUCCEED(); // We need this here to stop it failing due to no tests
  180. }
  181. SECTION( "encoded chars: these should all be encoded: &&&\"\"\"<<<&\"<<&\"" ) {
  182. SUCCEED(); // We need this here to stop it failing due to no tests
  183. }
  184. }
  185. TEST_CASE( "send a single char to INFO", "[failing][.]" ) {
  186. INFO(3);
  187. REQUIRE(false);
  188. }
  189. TEST_CASE( "Factorials are computed", "[factorial]" ) {
  190. REQUIRE( Factorial(0) == 1 );
  191. REQUIRE( Factorial(1) == 1 );
  192. REQUIRE( Factorial(2) == 2 );
  193. REQUIRE( Factorial(3) == 6 );
  194. REQUIRE( Factorial(10) == 3628800 );
  195. }
  196. TEST_CASE( "An empty test with no assertions", "[empty]" ) {}
  197. TEST_CASE( "Nice descriptive name", "[tag1][tag2][tag3][.]" ) {
  198. WARN( "This one ran" );
  199. }
  200. TEST_CASE( "first tag", "[tag1]" ) {}
  201. TEST_CASE( "second tag", "[tag2]" ) {}
  202. TEST_CASE( "vectors can be sized and resized", "[vector]" ) {
  203. std::vector<int> v( 5 );
  204. REQUIRE( v.size() == 5 );
  205. REQUIRE( v.capacity() >= 5 );
  206. SECTION( "resizing bigger changes size and capacity" ) {
  207. v.resize( 10 );
  208. REQUIRE( v.size() == 10 );
  209. REQUIRE( v.capacity() >= 10 );
  210. }
  211. SECTION( "resizing smaller changes size but not capacity" ) {
  212. v.resize( 0 );
  213. REQUIRE( v.size() == 0 );
  214. REQUIRE( v.capacity() >= 5 );
  215. SECTION( "We can use the 'swap trick' to reset the capacity" ) {
  216. std::vector<int> empty;
  217. empty.swap( v );
  218. REQUIRE( v.capacity() == 0 );
  219. }
  220. }
  221. SECTION( "reserving bigger changes capacity but not size" ) {
  222. v.reserve( 10 );
  223. REQUIRE( v.size() == 5 );
  224. REQUIRE( v.capacity() >= 10 );
  225. }
  226. SECTION( "reserving smaller does not change size or capacity" ) {
  227. v.reserve( 0 );
  228. REQUIRE( v.size() == 5 );
  229. REQUIRE( v.capacity() >= 5 );
  230. }
  231. }
  232. TEMPLATE_TEST_CASE( "TemplateTest: vectors can be sized and resized", "[vector][template]", int, float, std::string, (std::tuple<int,float>) ) {
  233. std::vector<TestType> v( 5 );
  234. REQUIRE( v.size() == 5 );
  235. REQUIRE( v.capacity() >= 5 );
  236. SECTION( "resizing bigger changes size and capacity" ) {
  237. v.resize( 10 );
  238. REQUIRE( v.size() == 10 );
  239. REQUIRE( v.capacity() >= 10 );
  240. }
  241. SECTION( "resizing smaller changes size but not capacity" ) {
  242. v.resize( 0 );
  243. REQUIRE( v.size() == 0 );
  244. REQUIRE( v.capacity() >= 5 );
  245. SECTION( "We can use the 'swap trick' to reset the capacity" ) {
  246. std::vector<TestType> empty;
  247. empty.swap( v );
  248. REQUIRE( v.capacity() == 0 );
  249. }
  250. }
  251. SECTION( "reserving bigger changes capacity but not size" ) {
  252. v.reserve( 10 );
  253. REQUIRE( v.size() == 5 );
  254. REQUIRE( v.capacity() >= 10 );
  255. }
  256. SECTION( "reserving smaller does not change size or capacity" ) {
  257. v.reserve( 0 );
  258. REQUIRE( v.size() == 5 );
  259. REQUIRE( v.capacity() >= 5 );
  260. }
  261. }
  262. TEMPLATE_TEST_CASE_SIG("TemplateTestSig: vectors can be sized and resized", "[vector][template][nttp]", ((typename TestType, int V), TestType, V), (int,5), (float,4), (std::string,15), ((std::tuple<int, float>), 6)) {
  263. std::vector<TestType> v(V);
  264. REQUIRE(v.size() == V);
  265. REQUIRE(v.capacity() >= V);
  266. SECTION("resizing bigger changes size and capacity") {
  267. v.resize(2 * V);
  268. REQUIRE(v.size() == 2 * V);
  269. REQUIRE(v.capacity() >= 2 * V);
  270. }
  271. SECTION("resizing smaller changes size but not capacity") {
  272. v.resize(0);
  273. REQUIRE(v.size() == 0);
  274. REQUIRE(v.capacity() >= V);
  275. SECTION("We can use the 'swap trick' to reset the capacity") {
  276. std::vector<TestType> empty;
  277. empty.swap(v);
  278. REQUIRE(v.capacity() == 0);
  279. }
  280. }
  281. SECTION("reserving bigger changes capacity but not size") {
  282. v.reserve(2 * V);
  283. REQUIRE(v.size() == V);
  284. REQUIRE(v.capacity() >= 2 * V);
  285. }
  286. SECTION("reserving smaller does not change size or capacity") {
  287. v.reserve(0);
  288. REQUIRE(v.size() == V);
  289. REQUIRE(v.capacity() >= V);
  290. }
  291. }
  292. TEMPLATE_PRODUCT_TEST_CASE("A Template product test case", "[template][product]", (std::vector, Foo), (int, float)) {
  293. TestType x;
  294. REQUIRE(x.size() == 0);
  295. }
  296. TEMPLATE_PRODUCT_TEST_CASE_SIG("A Template product test case with array signature", "[template][product][nttp]", ((typename T, size_t S), T, S), (std::array, Bar), ((int, 9), (float, 42))) {
  297. TestType x;
  298. REQUIRE(x.size() > 0);
  299. }
  300. TEMPLATE_PRODUCT_TEST_CASE("Product with differing arities", "[template][product]", std::tuple, (int, (int, double), (int, double, float))) {
  301. REQUIRE(std::tuple_size<TestType>::value >= 1);
  302. }
  303. using MyTypes = std::tuple<int, char, float>;
  304. TEMPLATE_LIST_TEST_CASE("Template test case with test types specified inside std::tuple", "[template][list]", MyTypes)
  305. {
  306. REQUIRE(sizeof(TestType) > 0);
  307. }
  308. struct NonDefaultConstructibleType {
  309. NonDefaultConstructibleType() = delete;
  310. };
  311. using MyNonDefaultConstructibleTypes = std::tuple<NonDefaultConstructibleType, float>;
  312. TEMPLATE_LIST_TEST_CASE("Template test case with test types specified inside non-default-constructible std::tuple", "[template][list]", MyNonDefaultConstructibleTypes)
  313. {
  314. REQUIRE(sizeof(TestType) > 0);
  315. }
  316. struct NonCopyableAndNonMovableType {
  317. NonCopyableAndNonMovableType() = default;
  318. NonCopyableAndNonMovableType(NonCopyableAndNonMovableType const &) = delete;
  319. NonCopyableAndNonMovableType(NonCopyableAndNonMovableType &&) = delete;
  320. auto operator=(NonCopyableAndNonMovableType const &) -> NonCopyableAndNonMovableType & = delete;
  321. auto operator=(NonCopyableAndNonMovableType &&) -> NonCopyableAndNonMovableType & = delete;
  322. };
  323. using NonCopyableAndNonMovableTypes = std::tuple<NonCopyableAndNonMovableType, float>;
  324. TEMPLATE_LIST_TEST_CASE("Template test case with test types specified inside non-copyable and non-movable std::tuple", "[template][list]", NonCopyableAndNonMovableTypes)
  325. {
  326. REQUIRE(sizeof(TestType) > 0);
  327. }
  328. // https://github.com/philsquared/Catch/issues/166
  329. TEST_CASE("A couple of nested sections followed by a failure", "[failing][.]") {
  330. SECTION("Outer")
  331. SECTION("Inner")
  332. SUCCEED("that's not flying - that's failing in style");
  333. FAIL("to infinity and beyond");
  334. }
  335. TEST_CASE("not allowed", "[!throws]") {
  336. // This test case should not be included if you run with -e on the command line
  337. SUCCEED();
  338. }
  339. TEST_CASE( "Tabs and newlines show in output", "[.][whitespace][failing]" ) {
  340. // Based on issue #242
  341. std::string s1 = "if ($b == 10) {\n\t\t$a\t= 20;\n}";
  342. std::string s2 = "if ($b == 10) {\n\t$a = 20;\n}\n";
  343. CHECK( s1 == s2 );
  344. }
  345. #if defined(CATCH_CONFIG_WCHAR)
  346. TEST_CASE( "toString on const wchar_t const pointer returns the string contents", "[toString]" ) {
  347. const wchar_t * const s = L"wide load";
  348. std::string result = ::Catch::Detail::stringify( s );
  349. CHECK( result == "\"wide load\"" );
  350. }
  351. TEST_CASE( "toString on const wchar_t pointer returns the string contents", "[toString]" ) {
  352. const wchar_t * s = L"wide load";
  353. std::string result = ::Catch::Detail::stringify( s );
  354. CHECK( result == "\"wide load\"" );
  355. }
  356. TEST_CASE( "toString on wchar_t const pointer returns the string contents", "[toString]" ) {
  357. auto const s = const_cast<wchar_t*>( L"wide load" );
  358. std::string result = ::Catch::Detail::stringify( s );
  359. CHECK( result == "\"wide load\"" );
  360. }
  361. TEST_CASE( "toString on wchar_t returns the string contents", "[toString]" ) {
  362. auto s = const_cast<wchar_t*>( L"wide load" );
  363. std::string result = ::Catch::Detail::stringify( s );
  364. CHECK( result == "\"wide load\"" );
  365. }
  366. #endif // CATCH_CONFIG_WCHAR
  367. TEST_CASE( "long long" ) {
  368. constexpr long long l = std::numeric_limits<long long>::max();
  369. REQUIRE( l == std::numeric_limits<long long>::max() );
  370. }
  371. TEST_CASE( "This test 'should' fail but doesn't", "[.][failing][!shouldfail]" ) {
  372. SUCCEED( "oops!" );
  373. }
  374. TEST_CASE( "# A test name that starts with a #" ) {
  375. SUCCEED( "yay" );
  376. }
  377. TEST_CASE( "#835 -- errno should not be touched by Catch2", "[.][failing][!shouldfail]" ) {
  378. errno = 1;
  379. // Check that reporting failed test doesn't change errno.
  380. CHECK(f() == 0);
  381. // We want to avoid expanding `errno` macro in assertion, because
  382. // we capture the expression after macro expansion, and would have
  383. // to normalize the ways different platforms spell `errno`.
  384. const auto errno_after = errno;
  385. REQUIRE(errno_after == 1);
  386. }
  387. TEST_CASE( "#961 -- Dynamically created sections should all be reported", "[.]" ) {
  388. for (char i = '0'; i < '5'; ++i) {
  389. SECTION(std::string("Looped section ") + i) {
  390. SUCCEED( "Everything is OK" );
  391. }
  392. }
  393. }
  394. TEST_CASE( "#1175 - Hidden Test", "[.]" ) {
  395. // Just for checking that hidden test is not listed by default
  396. SUCCEED();
  397. }
  398. TEMPLATE_TEST_CASE_SIG("#1954 - 7 arg template test case sig compiles", "[regression][.compilation]",
  399. ((int Tnx, int Tnu, int Tny, int Tph, int Tch, int Tineq, int Teq), Tnx, Tnu, Tny, Tph, Tch, Tineq, Teq),
  400. (1, 1, 1, 1, 1, 0, 0), (5, 1, 1, 1, 1, 0, 0), (5, 3, 1, 1, 1, 0, 0)) {
  401. SUCCEED();
  402. }
  403. TEST_CASE("Same test name but with different tags is fine", "[.approvals][some-tag]") {}
  404. TEST_CASE("Same test name but with different tags is fine", "[.approvals][other-tag]") {}
  405. // MinGW doesn't support __try, and Clang has only very partial support
  406. #if defined(_MSC_VER)
  407. void throw_and_catch()
  408. {
  409. __try {
  410. RaiseException(0xC0000005, 0, 0, NULL);
  411. }
  412. __except (1)
  413. {
  414. }
  415. }
  416. TEST_CASE("Validate SEH behavior - handled", "[approvals][FatalConditionHandler][CATCH_PLATFORM_WINDOWS]")
  417. {
  418. // Validate that Catch2 framework correctly handles tests raising and handling SEH exceptions.
  419. throw_and_catch();
  420. }
  421. void throw_no_catch()
  422. {
  423. RaiseException(0xC0000005, 0, 0, NULL);
  424. }
  425. TEST_CASE("Validate SEH behavior - unhandled", "[.approvals][FatalConditionHandler][CATCH_PLATFORM_WINDOWS]")
  426. {
  427. // Validate that Catch2 framework correctly handles tests raising and not handling SEH exceptions.
  428. throw_no_catch();
  429. }
  430. static LONG CALLBACK dummyExceptionFilter(PEXCEPTION_POINTERS ExceptionInfo) {
  431. return EXCEPTION_CONTINUE_SEARCH;
  432. }
  433. TEST_CASE("Validate SEH behavior - no crash for stack unwinding", "[approvals][!throws][!shouldfail][FatalConditionHandler][CATCH_PLATFORM_WINDOWS]")
  434. {
  435. // Trigger stack unwinding with SEH top-level filter changed and validate the test fails expectedly with no application crash
  436. SetUnhandledExceptionFilter(dummyExceptionFilter);
  437. throw 1;
  438. }
  439. #endif // _MSC_VER