Misc.tests.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Created by Phil on 29/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. #include "catch.hpp"
  9. #ifdef __clang__
  10. # pragma clang diagnostic ignored "-Wc++98-compat"
  11. # pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  12. #endif
  13. #include <iostream>
  14. #include <cerrno>
  15. #include <limits>
  16. #include <sstream>
  17. #include <array>
  18. namespace { namespace MiscTests {
  19. #ifndef MISC_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
  20. #define MISC_TEST_HELPERS_INCLUDED
  21. inline const char* makeString( bool makeNull ) {
  22. return makeNull ? nullptr : "valid string";
  23. }
  24. inline bool testCheckedIf( bool flag ) {
  25. CHECKED_IF( flag )
  26. return true;
  27. else
  28. return false;
  29. }
  30. inline bool testCheckedElse( bool flag ) {
  31. CHECKED_ELSE( flag )
  32. return false;
  33. return true;
  34. }
  35. inline unsigned int Factorial( unsigned int number ) {
  36. return number > 1 ? Factorial(number-1)*number : 1;
  37. }
  38. static int f() {
  39. return 1;
  40. }
  41. inline void manuallyRegisteredTestFunction() {
  42. SUCCEED( "was called" );
  43. }
  44. struct AutoTestReg {
  45. AutoTestReg() {
  46. REGISTER_TEST_CASE( manuallyRegisteredTestFunction, "ManuallyRegistered" );
  47. }
  48. };
  49. CATCH_INTERNAL_START_WARNINGS_SUPPRESSION
  50. CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS
  51. static AutoTestReg autoTestReg;
  52. CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION
  53. template<typename T>
  54. struct Foo {
  55. size_t size() { return 0; }
  56. };
  57. template<typename T, size_t S>
  58. struct Bar {
  59. size_t size() { return S; }
  60. };
  61. #endif
  62. TEST_CASE( "random SECTION tests", "[.][sections][failing]" ) {
  63. int a = 1;
  64. int b = 2;
  65. SECTION( "doesn't equal" ) {
  66. REQUIRE( a != b );
  67. REQUIRE( b != a );
  68. }
  69. SECTION( "not equal" ) {
  70. REQUIRE( a != b);
  71. }
  72. }
  73. TEST_CASE( "nested SECTION tests", "[.][sections][failing]" ) {
  74. int a = 1;
  75. int b = 2;
  76. SECTION( "doesn't equal" ) {
  77. REQUIRE( a != b );
  78. REQUIRE( b != a );
  79. SECTION( "not equal" ) {
  80. REQUIRE( a != b);
  81. }
  82. }
  83. }
  84. TEST_CASE( "more nested SECTION tests", "[sections][failing][.]" ) {
  85. int a = 1;
  86. int b = 2;
  87. SECTION( "doesn't equal" ) {
  88. SECTION( "equal" ) {
  89. REQUIRE( a == b );
  90. }
  91. SECTION( "not equal" ) {
  92. REQUIRE( a != b );
  93. }
  94. SECTION( "less than" ) {
  95. REQUIRE( a < b );
  96. }
  97. }
  98. }
  99. TEST_CASE( "even more nested SECTION tests", "[sections]" ) {
  100. SECTION( "c" ) {
  101. SECTION( "d (leaf)" ) {
  102. SUCCEED(); // avoid failing due to no tests
  103. }
  104. SECTION( "e (leaf)" ) {
  105. SUCCEED(); // avoid failing due to no tests
  106. }
  107. }
  108. SECTION( "f (leaf)" ) {
  109. SUCCEED(); // avoid failing due to no tests
  110. }
  111. }
  112. TEST_CASE( "looped SECTION tests", "[.][failing][sections]" ) {
  113. int a = 1;
  114. for( int b = 0; b < 10; ++b ) {
  115. DYNAMIC_SECTION( "b is currently: " << b ) {
  116. CHECK( b > a );
  117. }
  118. }
  119. }
  120. TEST_CASE( "looped tests", "[.][failing]" ) {
  121. static const int fib[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
  122. for( std::size_t i=0; i < sizeof(fib)/sizeof(int); ++i ) {
  123. INFO( "Testing if fib[" << i << "] (" << fib[i] << ") is even" );
  124. CHECK( ( fib[i] % 2 ) == 0 );
  125. }
  126. }
  127. TEST_CASE( "Sends stuff to stdout and stderr", "[.]" ) {
  128. std::cout << "A string sent directly to stdout" << std::endl;
  129. std::cerr << "A string sent directly to stderr" << std::endl;
  130. std::clog << "A string sent to stderr via clog" << std::endl;
  131. }
  132. TEST_CASE( "null strings" ) {
  133. REQUIRE( makeString( false ) != static_cast<char*>(nullptr));
  134. REQUIRE( makeString( true ) == static_cast<char*>(nullptr));
  135. }
  136. TEST_CASE( "checkedIf" ) {
  137. REQUIRE( testCheckedIf( true ) );
  138. }
  139. TEST_CASE( "checkedIf, failing", "[failing][.]" ) {
  140. REQUIRE( testCheckedIf( false ) );
  141. }
  142. TEST_CASE( "checkedElse" ) {
  143. REQUIRE( testCheckedElse( true ) );
  144. }
  145. TEST_CASE( "checkedElse, failing", "[failing][.]" ) {
  146. REQUIRE( testCheckedElse( false ) );
  147. }
  148. TEST_CASE( "xmlentitycheck" ) {
  149. 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>" ) {
  150. SUCCEED(); // We need this here to stop it failing due to no tests
  151. }
  152. SECTION( "encoded chars: these should all be encoded: &&&\"\"\"<<<&\"<<&\"" ) {
  153. SUCCEED(); // We need this here to stop it failing due to no tests
  154. }
  155. }
  156. TEST_CASE( "send a single char to INFO", "[failing][.]" ) {
  157. INFO(3);
  158. REQUIRE(false);
  159. }
  160. TEST_CASE( "atomic if", "[failing][0]") {
  161. std::size_t x = 0;
  162. if( x )
  163. REQUIRE(x > 0);
  164. else
  165. REQUIRE(x == 0);
  166. }
  167. TEST_CASE( "Factorials are computed", "[factorial]" ) {
  168. REQUIRE( Factorial(0) == 1 );
  169. REQUIRE( Factorial(1) == 1 );
  170. REQUIRE( Factorial(2) == 2 );
  171. REQUIRE( Factorial(3) == 6 );
  172. REQUIRE( Factorial(10) == 3628800 );
  173. }
  174. TEST_CASE( "An empty test with no assertions", "[empty]" ) {}
  175. TEST_CASE( "Nice descriptive name", "[tag1][tag2][tag3][.]" ) {
  176. WARN( "This one ran" );
  177. }
  178. TEST_CASE( "first tag", "[tag1]" ) {}
  179. TEST_CASE( "second tag", "[tag2]" ) {}
  180. //
  181. //TEST_CASE( "spawn a new process", "[.]" )
  182. //{
  183. // // !TBD Work in progress
  184. // char line[200];
  185. // FILE* output = popen("./CatchSelfTest ./failing/matchers/StartsWith", "r");
  186. // while ( fgets(line, 199, output) )
  187. // std::cout << line;
  188. //}
  189. TEST_CASE( "vectors can be sized and resized", "[vector]" ) {
  190. std::vector<int> v( 5 );
  191. REQUIRE( v.size() == 5 );
  192. REQUIRE( v.capacity() >= 5 );
  193. SECTION( "resizing bigger changes size and capacity" ) {
  194. v.resize( 10 );
  195. REQUIRE( v.size() == 10 );
  196. REQUIRE( v.capacity() >= 10 );
  197. }
  198. SECTION( "resizing smaller changes size but not capacity" ) {
  199. v.resize( 0 );
  200. REQUIRE( v.size() == 0 );
  201. REQUIRE( v.capacity() >= 5 );
  202. SECTION( "We can use the 'swap trick' to reset the capacity" ) {
  203. std::vector<int> empty;
  204. empty.swap( v );
  205. REQUIRE( v.capacity() == 0 );
  206. }
  207. }
  208. SECTION( "reserving bigger changes capacity but not size" ) {
  209. v.reserve( 10 );
  210. REQUIRE( v.size() == 5 );
  211. REQUIRE( v.capacity() >= 10 );
  212. }
  213. SECTION( "reserving smaller does not change size or capacity" ) {
  214. v.reserve( 0 );
  215. REQUIRE( v.size() == 5 );
  216. REQUIRE( v.capacity() >= 5 );
  217. }
  218. }
  219. TEMPLATE_TEST_CASE( "TemplateTest: vectors can be sized and resized", "[vector][template]", int, float, std::string, (std::tuple<int,float>) ) {
  220. std::vector<TestType> v( 5 );
  221. REQUIRE( v.size() == 5 );
  222. REQUIRE( v.capacity() >= 5 );
  223. SECTION( "resizing bigger changes size and capacity" ) {
  224. v.resize( 10 );
  225. REQUIRE( v.size() == 10 );
  226. REQUIRE( v.capacity() >= 10 );
  227. }
  228. SECTION( "resizing smaller changes size but not capacity" ) {
  229. v.resize( 0 );
  230. REQUIRE( v.size() == 0 );
  231. REQUIRE( v.capacity() >= 5 );
  232. SECTION( "We can use the 'swap trick' to reset the capacity" ) {
  233. std::vector<TestType> empty;
  234. empty.swap( v );
  235. REQUIRE( v.capacity() == 0 );
  236. }
  237. }
  238. SECTION( "reserving bigger changes capacity but not size" ) {
  239. v.reserve( 10 );
  240. REQUIRE( v.size() == 5 );
  241. REQUIRE( v.capacity() >= 10 );
  242. }
  243. SECTION( "reserving smaller does not change size or capacity" ) {
  244. v.reserve( 0 );
  245. REQUIRE( v.size() == 5 );
  246. REQUIRE( v.capacity() >= 5 );
  247. }
  248. }
  249. 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)) {
  250. std::vector<TestType> v(V);
  251. REQUIRE(v.size() == V);
  252. REQUIRE(v.capacity() >= V);
  253. SECTION("resizing bigger changes size and capacity") {
  254. v.resize(2 * V);
  255. REQUIRE(v.size() == 2 * V);
  256. REQUIRE(v.capacity() >= 2 * V);
  257. }
  258. SECTION("resizing smaller changes size but not capacity") {
  259. v.resize(0);
  260. REQUIRE(v.size() == 0);
  261. REQUIRE(v.capacity() >= V);
  262. SECTION("We can use the 'swap trick' to reset the capacity") {
  263. std::vector<TestType> empty;
  264. empty.swap(v);
  265. REQUIRE(v.capacity() == 0);
  266. }
  267. }
  268. SECTION("reserving bigger changes capacity but not size") {
  269. v.reserve(2 * V);
  270. REQUIRE(v.size() == V);
  271. REQUIRE(v.capacity() >= 2 * V);
  272. }
  273. SECTION("reserving smaller does not change size or capacity") {
  274. v.reserve(0);
  275. REQUIRE(v.size() == V);
  276. REQUIRE(v.capacity() >= V);
  277. }
  278. }
  279. TEMPLATE_PRODUCT_TEST_CASE("A Template product test case", "[template][product]", (std::vector, Foo), (int, float)) {
  280. TestType x;
  281. REQUIRE(x.size() == 0);
  282. }
  283. 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))) {
  284. TestType x;
  285. REQUIRE(x.size() > 0);
  286. }
  287. TEMPLATE_PRODUCT_TEST_CASE("Product with differing arities", "[template][product]", std::tuple, (int, (int, double), (int, double, float))) {
  288. REQUIRE(std::tuple_size<TestType>::value >= 1);
  289. }
  290. using MyTypes = std::tuple<int, char, float>;
  291. TEMPLATE_LIST_TEST_CASE("Template test case with test types specified inside std::tuple", "[template][list]", MyTypes)
  292. {
  293. REQUIRE(sizeof(TestType) > 0);
  294. }
  295. struct NonDefaultConstructibleType {
  296. NonDefaultConstructibleType() = delete;
  297. };
  298. using MyNonDefaultConstructibleTypes = std::tuple<NonDefaultConstructibleType, float>;
  299. TEMPLATE_LIST_TEST_CASE("Template test case with test types specified inside non-default-constructible std::tuple", "[template][list]", MyNonDefaultConstructibleTypes)
  300. {
  301. REQUIRE(sizeof(TestType) > 0);
  302. }
  303. struct NonCopyableAndNonMovableType {
  304. NonCopyableAndNonMovableType() = default;
  305. NonCopyableAndNonMovableType(NonCopyableAndNonMovableType const &) = delete;
  306. NonCopyableAndNonMovableType(NonCopyableAndNonMovableType &&) = delete;
  307. auto operator=(NonCopyableAndNonMovableType const &) -> NonCopyableAndNonMovableType & = delete;
  308. auto operator=(NonCopyableAndNonMovableType &&) -> NonCopyableAndNonMovableType & = delete;
  309. };
  310. using NonCopyableAndNonMovableTypes = std::tuple<NonCopyableAndNonMovableType, float>;
  311. TEMPLATE_LIST_TEST_CASE("Template test case with test types specified inside non-copyable and non-movable std::tuple", "[template][list]", NonCopyableAndNonMovableTypes)
  312. {
  313. REQUIRE(sizeof(TestType) > 0);
  314. }
  315. // https://github.com/philsquared/Catch/issues/166
  316. TEST_CASE("A couple of nested sections followed by a failure", "[failing][.]") {
  317. SECTION("Outer")
  318. SECTION("Inner")
  319. SUCCEED("that's not flying - that's failing in style");
  320. FAIL("to infinity and beyond");
  321. }
  322. TEST_CASE("not allowed", "[!throws]") {
  323. // This test case should not be included if you run with -e on the command line
  324. SUCCEED();
  325. }
  326. //TEST_CASE( "Is big endian" ) {
  327. // CHECK( Catch::Detail::Endianness::which() == Catch::Detail::Endianness::Little );
  328. //}
  329. TEST_CASE( "Tabs and newlines show in output", "[.][whitespace][failing]" ) {
  330. // Based on issue #242
  331. std::string s1 = "if ($b == 10) {\n\t\t$a\t= 20;\n}";
  332. std::string s2 = "if ($b == 10) {\n\t$a = 20;\n}\n";
  333. CHECK( s1 == s2 );
  334. }
  335. #ifdef CATCH_CONFIG_WCHAR
  336. TEST_CASE( "toString on const wchar_t const pointer returns the string contents", "[toString]" ) {
  337. const wchar_t * const s = L"wide load";
  338. std::string result = ::Catch::Detail::stringify( s );
  339. CHECK( result == "\"wide load\"" );
  340. }
  341. TEST_CASE( "toString on const wchar_t pointer returns the string contents", "[toString]" ) {
  342. const wchar_t * s = L"wide load";
  343. std::string result = ::Catch::Detail::stringify( s );
  344. CHECK( result == "\"wide load\"" );
  345. }
  346. TEST_CASE( "toString on wchar_t const pointer returns the string contents", "[toString]" ) {
  347. auto const s = const_cast<wchar_t*>( L"wide load" );
  348. std::string result = ::Catch::Detail::stringify( s );
  349. CHECK( result == "\"wide load\"" );
  350. }
  351. TEST_CASE( "toString on wchar_t returns the string contents", "[toString]" ) {
  352. auto s = const_cast<wchar_t*>( L"wide load" );
  353. std::string result = ::Catch::Detail::stringify( s );
  354. CHECK( result == "\"wide load\"" );
  355. }
  356. #endif
  357. TEST_CASE( "long long" ) {
  358. long long l = std::numeric_limits<long long>::max();
  359. REQUIRE( l == std::numeric_limits<long long>::max() );
  360. }
  361. TEST_CASE( "This test 'should' fail but doesn't", "[.][failing][!shouldfail]" ) {
  362. SUCCEED( "oops!" );
  363. }
  364. TEST_CASE( "# A test name that starts with a #" ) {
  365. SUCCEED( "yay" );
  366. }
  367. TEST_CASE( "#835 -- errno should not be touched by Catch", "[.][failing][!shouldfail]" ) {
  368. errno = 1;
  369. CHECK(f() == 0);
  370. REQUIRE(errno == 1); // Check that f() doesn't touch errno.
  371. }
  372. TEST_CASE( "#961 -- Dynamically created sections should all be reported", "[.]" ) {
  373. for (char i = '0'; i < '5'; ++i) {
  374. SECTION(std::string("Looped section ") + i) {
  375. SUCCEED( "Everything is OK" );
  376. }
  377. }
  378. }
  379. TEST_CASE( "#1175 - Hidden Test", "[.]" ) {
  380. // Just for checking that hidden test is not listed by default
  381. SUCCEED();
  382. }
  383. TEMPLATE_TEST_CASE_SIG("#1954 - 7 arg template test case sig compiles", "[regression][.compilation]",
  384. ((int Tnx, int Tnu, int Tny, int Tph, int Tch, int Tineq, int Teq), Tnx, Tnu, Tny, Tph, Tch, Tineq, Teq),
  385. (1, 1, 1, 1, 1, 0, 0), (5, 1, 1, 1, 1, 0, 0), (5, 3, 1, 1, 1, 0, 0)) {
  386. SUCCEED();
  387. }
  388. }} // namespace MiscTests