Tricky.tests.cpp 9.6 KB

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