Tricky.tests.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #ifdef __clang__
  7. #pragma clang diagnostic ignored "-Wpadded"
  8. #endif
  9. #ifdef _MSC_VER
  10. #pragma warning (disable : 4702) // Disable unreachable code warning for the last test
  11. // that is triggered when compiling as Win32|Release
  12. #endif
  13. #include <catch2/catch_test_macros.hpp>
  14. #include <catch2/generators/catch_generators.hpp>
  15. #include <catch2/generators/catch_generators_range.hpp>
  16. #include <cstdio>
  17. #include <sstream>
  18. #include <iostream>
  19. struct Opaque
  20. {
  21. int val;
  22. bool operator ==( const Opaque& o ) const
  23. {
  24. return val == o.val;
  25. }
  26. };
  27. ///////////////////////////////////////////////////////////////////////////////
  28. TEST_CASE
  29. (
  30. "A failing expression with a non streamable type is still captured",
  31. "[Tricky][failing][.]"
  32. )
  33. {
  34. Opaque o1, o2;
  35. o1.val = 7;
  36. o2.val = 8;
  37. CHECK( &o1 == &o2 );
  38. CHECK( o1 == o2 );
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////
  41. TEST_CASE
  42. (
  43. "An expression with side-effects should only be evaluated once",
  44. "[Tricky]"
  45. )
  46. {
  47. int i = 7;
  48. REQUIRE( i++ == 7 );
  49. REQUIRE( i++ == 8 );
  50. }
  51. namespace A {
  52. struct X
  53. {
  54. X() : a(4), b(2), c(7) {}
  55. X(int v) : a(v), b(2), c(7) {}
  56. int a;
  57. int b;
  58. int c;
  59. };
  60. }
  61. namespace B {
  62. struct Y
  63. {
  64. Y() : a(4), b(2), c(7) {}
  65. Y(int v) : a(v), b(2), c(7) {}
  66. int a;
  67. int b;
  68. int c;
  69. };
  70. }
  71. inline bool operator==(const A::X& lhs, const B::Y& rhs)
  72. {
  73. return (lhs.a == rhs.a);
  74. }
  75. inline bool operator==(const B::Y& lhs, const A::X& rhs)
  76. {
  77. return (lhs.a == rhs.a);
  78. }
  79. ///////////////////////////////////////////////////////////////////////////////
  80. /* This, currently, does not compile with LLVM
  81. TEST_CASE
  82. (
  83. "Operators at different namespace levels not hijacked by Koenig lookup"
  84. "[Tricky]"
  85. )
  86. {
  87. A::X x;
  88. B::Y y;
  89. REQUIRE( x == y );
  90. }
  91. */
  92. namespace ObjectWithConversions
  93. {
  94. struct Object
  95. {
  96. operator unsigned int() const {return 0xc0000000;}
  97. };
  98. ///////////////////////////////////////////////////////////////////////////////
  99. TEST_CASE
  100. (
  101. "Implicit conversions are supported inside assertion macros",
  102. "[Tricky][approvals]"
  103. )
  104. {
  105. Object o;
  106. REQUIRE(0xc0000000 == o );
  107. }
  108. }
  109. namespace EnumBitFieldTests
  110. {
  111. enum Bits : uint32_t {
  112. bit0 = 0x0001,
  113. bit1 = 0x0002,
  114. bit2 = 0x0004,
  115. bit3 = 0x0008,
  116. bit1and2 = bit1 | bit2,
  117. bit30 = 0x40000000,
  118. bit31 = 0x80000000,
  119. bit30and31 = bit30 | bit31
  120. };
  121. TEST_CASE( "Test enum bit values", "[Tricky]" )
  122. {
  123. REQUIRE( 0xc0000000 == bit30and31 );
  124. }
  125. }
  126. struct Obj
  127. {
  128. Obj():prop(&p){}
  129. int p = 0;
  130. int* prop;
  131. };
  132. TEST_CASE("boolean member", "[Tricky]")
  133. {
  134. Obj obj;
  135. REQUIRE( obj.prop != nullptr );
  136. }
  137. // Tests for a problem submitted by Ralph McArdell
  138. //
  139. // The static bool value should not need to be defined outside the
  140. // struct it is declared in - but when evaluating it in a deduced
  141. // context it appears to require the extra definition.
  142. // The issue was fixed by adding bool overloads to bypass the
  143. // templates that were there to deduce it.
  144. template <bool B>
  145. struct is_true
  146. {
  147. static const bool value = B;
  148. };
  149. TEST_CASE( "(unimplemented) static bools can be evaluated", "[Tricky]" )
  150. {
  151. SECTION("compare to true")
  152. {
  153. REQUIRE( is_true<true>::value == true );
  154. REQUIRE( true == is_true<true>::value );
  155. }
  156. SECTION("compare to false")
  157. {
  158. REQUIRE( is_true<false>::value == false );
  159. REQUIRE( false == is_true<false>::value );
  160. }
  161. SECTION("negation")
  162. {
  163. REQUIRE( !is_true<false>::value );
  164. }
  165. SECTION("double negation")
  166. {
  167. REQUIRE( !!is_true<true>::value );
  168. }
  169. SECTION("direct")
  170. {
  171. REQUIRE( is_true<true>::value );
  172. REQUIRE_FALSE( is_true<false>::value );
  173. }
  174. }
  175. struct Boolable
  176. {
  177. explicit Boolable( bool value ) : m_value( value ) {}
  178. explicit operator bool() const {
  179. return m_value;
  180. }
  181. bool m_value;
  182. };
  183. TEST_CASE( "Objects that evaluated in boolean contexts can be checked", "[Tricky][SafeBool]" )
  184. {
  185. Boolable True( true );
  186. Boolable False( false );
  187. CHECK( True );
  188. CHECK( !False );
  189. CHECK_FALSE( False );
  190. }
  191. TEST_CASE( "Assertions then sections", "[Tricky]" )
  192. {
  193. // This was causing a failure due to the way the console reporter was handling
  194. // the current section
  195. REQUIRE( true );
  196. SECTION( "A section" )
  197. {
  198. REQUIRE( true );
  199. SECTION( "Another section" )
  200. {
  201. REQUIRE( true );
  202. }
  203. SECTION( "Another other section" )
  204. {
  205. REQUIRE( true );
  206. }
  207. }
  208. }
  209. struct Awkward
  210. {
  211. operator int() const { return 7; }
  212. };
  213. TEST_CASE( "non streamable - with conv. op", "[Tricky]" )
  214. {
  215. Awkward awkward;
  216. std::string s = ::Catch::Detail::stringify( awkward );
  217. REQUIRE( s == "7" );
  218. }
  219. inline void foo() {}
  220. typedef void (*fooptr_t)();
  221. TEST_CASE( "Comparing function pointers", "[Tricky][function pointer]" )
  222. {
  223. // This was giving a warning in VS2010
  224. // #179
  225. fooptr_t a = foo;
  226. REQUIRE( a );
  227. REQUIRE( a == &foo );
  228. }
  229. struct S
  230. {
  231. void f() {}
  232. };
  233. TEST_CASE( "Comparing member function pointers", "[Tricky][member function pointer][approvals]" )
  234. {
  235. typedef void (S::*MF)();
  236. MF m = &S::f;
  237. CHECK( m == &S::f );
  238. }
  239. class ClassName {};
  240. TEST_CASE( "pointer to class", "[Tricky]" )
  241. {
  242. ClassName *p = 0;
  243. REQUIRE( p == 0 );
  244. }
  245. #include <memory>
  246. TEST_CASE( "null_ptr", "[Tricky]" )
  247. {
  248. std::unique_ptr<int> ptr;
  249. REQUIRE(ptr.get() == nullptr);
  250. }
  251. TEST_CASE( "X/level/0/a", "[Tricky]" ) { SUCCEED(""); }
  252. TEST_CASE( "X/level/0/b", "[Tricky][fizz]" ){ SUCCEED(""); }
  253. TEST_CASE( "X/level/1/a", "[Tricky]" ) { SUCCEED(""); }
  254. TEST_CASE( "X/level/1/b", "[Tricky]" ) { SUCCEED(""); }
  255. TEST_CASE( "has printf" ) {
  256. // This can cause problems as, currently, stdout itself is not redirected - only the cout (and cerr) buffer
  257. printf( "loose text artifact\n" );
  258. }
  259. namespace {
  260. struct constructor_throws {
  261. [[noreturn]] constructor_throws() {
  262. throw 1;
  263. }
  264. };
  265. }
  266. TEST_CASE("Commas in various macros are allowed") {
  267. REQUIRE_THROWS( std::vector<constructor_throws>{constructor_throws{}, constructor_throws{}} );
  268. CHECK_THROWS( std::vector<constructor_throws>{constructor_throws{}, constructor_throws{}} );
  269. REQUIRE_NOTHROW( std::vector<int>{1, 2, 3} == std::vector<int>{1, 2, 3} );
  270. CHECK_NOTHROW( std::vector<int>{1, 2, 3} == std::vector<int>{1, 2, 3} );
  271. REQUIRE(std::vector<int>{1, 2} == std::vector<int>{1, 2});
  272. CHECK( std::vector<int>{1, 2} == std::vector<int>{1, 2} );
  273. REQUIRE_FALSE(std::vector<int>{1, 2} == std::vector<int>{1, 2, 3});
  274. CHECK_FALSE( std::vector<int>{1, 2} == std::vector<int>{1, 2, 3} );
  275. CHECK_NOFAIL( std::vector<int>{1, 2} == std::vector<int>{1, 2} );
  276. CHECKED_IF( std::vector<int>{1, 2} == std::vector<int>{1, 2} ) {
  277. REQUIRE(true);
  278. } CHECKED_ELSE( std::vector<int>{1, 2} == std::vector<int>{1, 2} ) {
  279. CHECK(true);
  280. }
  281. }
  282. TEST_CASE( "non-copyable objects", "[.][failing]" ) {
  283. // Thanks to Agustin Bergé (@k-ballo on the cpplang Slack) for raising this
  284. std::type_info const& ti = typeid(int);
  285. CHECK( ti == typeid(int) );
  286. }
  287. TEST_CASE("#1514: stderr/stdout is not captured in tests aborted by an exception", "[output-capture][regression][.]") {
  288. std::cout << "This would not be caught previously\n" << std::flush;
  289. std::clog << "Nor would this\n" << std::flush;
  290. // FAIL aborts the test by throwing a Catch exception
  291. FAIL("1514");
  292. }
  293. TEST_CASE( "#2025: -c shouldn't cause infinite loop", "[sections][generators][regression][.approvals]" ) {
  294. SECTION( "Check cursor from buffer offset" ) {
  295. auto bufPos = GENERATE_REF( range( 0, 44 ) );
  296. WHEN( "Buffer position is " << bufPos ) { REQUIRE( 1 == 1 ); }
  297. }
  298. }
  299. TEST_CASE("#2025: original repro", "[sections][generators][regression][.approvals]") {
  300. auto fov = GENERATE(true, false);
  301. DYNAMIC_SECTION("fov_" << fov) {
  302. std::cout << "inside with fov: " << fov << '\n';
  303. }
  304. }
  305. TEST_CASE("#2025: same-level sections", "[sections][generators][regression][.approvals]") {
  306. SECTION("A") {
  307. SUCCEED();
  308. }
  309. auto i = GENERATE(1, 2, 3);
  310. SECTION("B") {
  311. REQUIRE(i < 4);
  312. }
  313. }