blob: 31ffc4789f5e656760e626dae37a44fb4ff8d897 [file] [log] [blame]
Phil Nashe6b2b0e2010-11-29 19:40:44 +00001/*
Phil Nashe6b2b0e2010-11-29 19:40:44 +00002 * 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)
Phil Nashe6b2b0e2010-11-29 19:40:44 +00007 */
Phil Nashcda21492012-08-16 18:47:41 +01008
Phil Nash823ea3e2011-04-26 08:32:40 +01009#include "catch.hpp"
Phil Nash368714e2015-08-07 08:20:56 +010010
11#ifdef __clang__
12# pragma clang diagnostic ignored "-Wc++98-compat"
13# pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
14#endif
15
Phil Nashb2132022012-05-04 07:55:11 +010016
Phil Nash95627c42011-01-07 10:22:24 +000017#include <iostream>
Martin Hořeňovskýace70402017-03-06 22:07:33 +010018#include <cerrno>
Martin Hořeňovskýd01fe032017-07-28 15:11:05 +020019#include <limits>
Phil Nash56e10752017-11-07 18:01:10 +000020#include <sstream>
Phil Nashe6b2b0e2010-11-29 19:40:44 +000021
Phil Nash61e838e2017-11-15 07:48:21 +000022namespace { namespace MiscTests {
23
24#ifndef MISC_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
25#define MISC_TEST_HELPERS_INCLUDED
26
27inline const char* makeString( bool makeNull ) {
28 return makeNull ? nullptr : "valid string";
29}
30inline bool testCheckedIf( bool flag ) {
31 CHECKED_IF( flag )
32 return true;
33 else
34 return false;
35}
36inline bool testCheckedElse( bool flag ) {
37 CHECKED_ELSE( flag )
38 return false;
39
40 return true;
41}
42
43inline unsigned int Factorial( unsigned int number ) {
44 return number > 1 ? Factorial(number-1)*number : 1;
45}
46
47static int f() {
48 return 1;
49}
50
51inline void manuallyRegisteredTestFunction() {
52 SUCCEED( "was called" );
53}
54
55struct AutoTestReg {
56 AutoTestReg() {
57 REGISTER_TEST_CASE( manuallyRegisteredTestFunction, "ManuallyRegistered" );
58 }
59};
Phil Nashb5a5d9a2017-11-29 19:14:33 +030060CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS
Phil Nash61e838e2017-11-15 07:48:21 +000061static AutoTestReg autoTestReg;
Phil Nashb5a5d9a2017-11-29 19:14:33 +030062CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS
Phil Nash61e838e2017-11-15 07:48:21 +000063
64#endif
65
Phil Nashf1936982017-07-13 09:20:37 +010066TEST_CASE( "random SECTION tests", "[.][sections][failing]" ) {
Phil Nashe6b2b0e2010-11-29 19:40:44 +000067 int a = 1;
68 int b = 2;
Phil Nashe9173812015-11-04 18:01:28 +000069
Phil Nashf1936982017-07-13 09:20:37 +010070 SECTION( "s1", "doesn't equal" ) {
Phil Nasha2d20952010-12-14 09:00:09 +000071 REQUIRE( a != b );
72 REQUIRE( b != a );
Phil Nashe6b2b0e2010-11-29 19:40:44 +000073 }
Phil Nashce319b72010-11-30 06:48:21 +000074
Phil Nashf1936982017-07-13 09:20:37 +010075 SECTION( "s2", "not equal" ) {
Phil Nash70e7d7f2011-02-17 20:15:20 +000076 REQUIRE( a != b);
Phil Nashe6b2b0e2010-11-29 19:40:44 +000077 }
Phil Nashe6b2b0e2010-11-29 19:40:44 +000078}
Phil Nashce319b72010-11-30 06:48:21 +000079
Phil Nashf1936982017-07-13 09:20:37 +010080TEST_CASE( "nested SECTION tests", "[.][sections][failing]" ) {
Phil Nashb3ef7fc2010-12-15 19:36:39 +000081 int a = 1;
82 int b = 2;
Phil Nashe9173812015-11-04 18:01:28 +000083
Phil Nashf1936982017-07-13 09:20:37 +010084 SECTION( "s1", "doesn't equal" ) {
Phil Nash9a79ee72010-12-28 17:21:29 +000085 REQUIRE( a != b );
Phil Nashb3ef7fc2010-12-15 19:36:39 +000086 REQUIRE( b != a );
87
Phil Nashf1936982017-07-13 09:20:37 +010088 SECTION( "s2", "not equal" ) {
Phil Nash70e7d7f2011-02-17 20:15:20 +000089 REQUIRE( a != b);
90 }
91 }
92}
93
Phil Nashf1936982017-07-13 09:20:37 +010094TEST_CASE( "more nested SECTION tests", "[sections][failing][.]" ) {
Phil Nash70e7d7f2011-02-17 20:15:20 +000095 int a = 1;
96 int b = 2;
Phil Nashe9173812015-11-04 18:01:28 +000097
Phil Nashf1936982017-07-13 09:20:37 +010098 SECTION( "s1", "doesn't equal" ) {
99 SECTION( "s2", "equal" ) {
Phil Nasha243a232011-02-21 08:50:05 +0000100 REQUIRE( a == b );
Phil Nash70e7d7f2011-02-17 20:15:20 +0000101 }
102
Phil Nashf1936982017-07-13 09:20:37 +0100103 SECTION( "s3", "not equal" ) {
Phil Nasha243a232011-02-21 08:50:05 +0000104 REQUIRE( a != b );
105 }
Phil Nashf1936982017-07-13 09:20:37 +0100106 SECTION( "s4", "less than" ) {
Phil Nash088d7002011-02-28 08:18:52 +0000107 REQUIRE( a < b );
Phil Nashb3ef7fc2010-12-15 19:36:39 +0000108 }
109 }
110}
111
Phil Nashf1936982017-07-13 09:20:37 +0100112TEST_CASE( "even more nested SECTION tests", "[sections]" ) {
113 SECTION( "c" ) {
114 SECTION( "d (leaf)" ) {
Phil Nash015e0712015-11-03 07:46:37 +0000115 SUCCEED(""); // avoid failing due to no tests
Phil Nashb2132022012-05-04 07:55:11 +0100116 }
Phil Nashe9173812015-11-04 18:01:28 +0000117
Phil Nashf1936982017-07-13 09:20:37 +0100118 SECTION( "e (leaf)" ) {
Phil Nash015e0712015-11-03 07:46:37 +0000119 SUCCEED(""); // avoid failing due to no tests
Phil Nashb2132022012-05-04 07:55:11 +0100120 }
121 }
122
Phil Nashf1936982017-07-13 09:20:37 +0100123 SECTION( "f (leaf)" ) {
Phil Nash015e0712015-11-03 07:46:37 +0000124 SUCCEED(""); // avoid failing due to no tests
Phil Nashb2132022012-05-04 07:55:11 +0100125 }
126}
127
Phil Nashf1936982017-07-13 09:20:37 +0100128TEST_CASE( "looped SECTION tests", "[.][failing][sections]" ) {
Phil Nash36ff7ba2010-12-28 14:41:57 +0000129 int a = 1;
Phil Nashe9173812015-11-04 18:01:28 +0000130
Phil Nashf1936982017-07-13 09:20:37 +0100131 for( int b = 0; b < 10; ++b ) {
Phil Nash36ff7ba2010-12-28 14:41:57 +0000132 std::ostringstream oss;
133 oss << "b is currently: " << b;
Phil Nashf1936982017-07-13 09:20:37 +0100134 SECTION( "s1", oss.str() ) {
Phil Nashe9173812015-11-04 18:01:28 +0000135 CHECK( b > a );
Phil Nash36ff7ba2010-12-28 14:41:57 +0000136 }
137 }
138}
139
Phil Nashf1936982017-07-13 09:20:37 +0100140TEST_CASE( "looped tests", "[.][failing]" ) {
Phil Nash36ff7ba2010-12-28 14:41:57 +0000141 static const int fib[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
Phil Nashe9173812015-11-04 18:01:28 +0000142
Phil Nash8da0d042017-09-18 17:13:17 +0100143 for( std::size_t i=0; i < sizeof(fib)/sizeof(int); ++i ) {
Phil Nash36ff7ba2010-12-28 14:41:57 +0000144 INFO( "Testing if fib[" << i << "] (" << fib[i] << ") is even" );
Phil Nashe9173812015-11-04 18:01:28 +0000145 CHECK( ( fib[i] % 2 ) == 0 );
Phil Nash36ff7ba2010-12-28 14:41:57 +0000146 }
147}
148
Phil Nashf1936982017-07-13 09:20:37 +0100149TEST_CASE( "Sends stuff to stdout and stderr", "[.]" ) {
Phil Nashfb90d382013-12-19 08:07:33 +0000150 std::cout << "A string sent directly to stdout" << std::endl;
Phil Nashe9173812015-11-04 18:01:28 +0000151
Phil Nashfb90d382013-12-19 08:07:33 +0000152 std::cerr << "A string sent directly to stderr" << std::endl;
Phil Nash36ff7ba2010-12-28 14:41:57 +0000153}
Phil Nash8087f572011-02-23 20:02:18 +0000154
Phil Nashf1936982017-07-13 09:20:37 +0100155TEST_CASE( "null strings" ) {
Martin Hořeňovský71df6632017-04-25 12:41:30 +0200156 REQUIRE( makeString( false ) != static_cast<char*>(nullptr));
157 REQUIRE( makeString( true ) == static_cast<char*>(nullptr));
Phil Nash8087f572011-02-23 20:02:18 +0000158}
Phil Nashb7087892011-03-09 19:45:05 +0000159
Phil Nashf1936982017-07-13 09:20:37 +0100160TEST_CASE( "checkedIf" ) {
Phil Nasha162e222012-02-10 08:30:13 +0000161 REQUIRE( testCheckedIf( true ) );
162}
163
Phil Nashf1936982017-07-13 09:20:37 +0100164TEST_CASE( "checkedIf, failing", "[failing][.]" ) {
Phil Nasha162e222012-02-10 08:30:13 +0000165 REQUIRE( testCheckedIf( false ) );
166}
167
Phil Nashf1936982017-07-13 09:20:37 +0100168TEST_CASE( "checkedElse" ) {
Phil Nasha162e222012-02-10 08:30:13 +0000169 REQUIRE( testCheckedElse( true ) );
170}
171
Phil Nashf1936982017-07-13 09:20:37 +0100172TEST_CASE( "checkedElse, failing", "[failing][.]" ) {
Phil Nasha162e222012-02-10 08:30:13 +0000173 REQUIRE( testCheckedElse( false ) );
174}
Phil Nash0b09d1c2012-02-10 18:58:06 +0000175
Phil Nashf1936982017-07-13 09:20:37 +0100176TEST_CASE( "xmlentitycheck" ) {
177 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>" ) {
Phil Nash015e0712015-11-03 07:46:37 +0000178 SUCCEED(""); // We need this here to stop it failing due to no tests
Phil Nash0b09d1c2012-02-10 18:58:06 +0000179 }
Phil Nashf1936982017-07-13 09:20:37 +0100180 SECTION( "encoded chars", "these should all be encoded: &&&\"\"\"<<<&\"<<&\"" ) {
Phil Nash015e0712015-11-03 07:46:37 +0000181 SUCCEED(""); // We need this here to stop it failing due to no tests
Phil Nash0b09d1c2012-02-10 18:58:06 +0000182 }
183}
Phil Nashaf8d0b32012-02-28 20:04:25 +0000184
Phil Nashf1936982017-07-13 09:20:37 +0100185TEST_CASE( "send a single char to INFO", "[failing][.]" ) {
Phil Nashaf8d0b32012-02-28 20:04:25 +0000186 INFO(3);
Phil Nashe9173812015-11-04 18:01:28 +0000187 REQUIRE(false);
Phil Nashaf8d0b32012-02-28 20:04:25 +0000188}
Phil Nash56a02ee2012-02-29 08:39:22 +0000189
Phil Nashf1936982017-07-13 09:20:37 +0100190TEST_CASE( "atomic if", "[failing][0]") {
Phil Nash8da0d042017-09-18 17:13:17 +0100191 std::size_t x = 0;
Phil Nashe9173812015-11-04 18:01:28 +0000192
Phil Nash56a02ee2012-02-29 08:39:22 +0000193 if( x )
194 REQUIRE(x > 0);
195 else
196 REQUIRE(x == 0);
197}
Phil Nasheca56372012-03-04 11:14:21 +0000198
Phil Nash163088a2012-05-31 19:40:26 +0100199
Phil Nashaee9b752013-06-04 08:37:28 +0100200TEST_CASE( "Factorials are computed", "[factorial]" ) {
Phil Nash163088a2012-05-31 19:40:26 +0100201 REQUIRE( Factorial(0) == 1 );
202 REQUIRE( Factorial(1) == 1 );
203 REQUIRE( Factorial(2) == 2 );
204 REQUIRE( Factorial(3) == 6 );
205 REQUIRE( Factorial(10) == 3628800 );
206}
Phil Nasha70fbe32012-08-31 08:10:36 +0100207
Phil Nashf1936982017-07-13 09:20:37 +0100208TEST_CASE( "An empty test with no assertions", "[empty]" ) {}
Phil Nashfc1baac2012-09-15 17:53:27 +0100209
Phil Nashf1936982017-07-13 09:20:37 +0100210TEST_CASE( "Nice descriptive name", "[tag1][tag2][tag3][.]" ) {
Phil Nashfc1baac2012-09-15 17:53:27 +0100211 WARN( "This one ran" );
212}
Phil Nashf1936982017-07-13 09:20:37 +0100213TEST_CASE( "first tag", "[tag1]" ) {}
214TEST_CASE( "second tag", "[tag2]" ) {}
215
Phil Nash47f679e2012-10-04 08:19:09 +0100216//
Phil Nash9c39a5e2013-06-28 16:05:13 +0100217//TEST_CASE( "spawn a new process", "[.]" )
Phil Nash47f679e2012-10-04 08:19:09 +0100218//{
219// // !TBD Work in progress
220// char line[200];
221// FILE* output = popen("./CatchSelfTest ./failing/matchers/StartsWith", "r");
222// while ( fgets(line, 199, output) )
223// std::cout << line;
224//}
Phil Nash29426b62013-03-25 08:47:36 +0000225
226TEST_CASE( "vectors can be sized and resized", "[vector]" ) {
227
228 std::vector<int> v( 5 );
Phil Nashe9173812015-11-04 18:01:28 +0000229
Phil Nash29426b62013-03-25 08:47:36 +0000230 REQUIRE( v.size() == 5 );
231 REQUIRE( v.capacity() >= 5 );
Phil Nashe9173812015-11-04 18:01:28 +0000232
Phil Nashf1936982017-07-13 09:20:37 +0100233 SECTION( "resizing bigger changes size and capacity" ) {
Phil Nash29426b62013-03-25 08:47:36 +0000234 v.resize( 10 );
Phil Nashe9173812015-11-04 18:01:28 +0000235
Phil Nash29426b62013-03-25 08:47:36 +0000236 REQUIRE( v.size() == 10 );
237 REQUIRE( v.capacity() >= 10 );
238 }
Phil Nashf1936982017-07-13 09:20:37 +0100239 SECTION( "resizing smaller changes size but not capacity" ) {
Phil Nash29426b62013-03-25 08:47:36 +0000240 v.resize( 0 );
Phil Nashe9173812015-11-04 18:01:28 +0000241
Phil Nash29426b62013-03-25 08:47:36 +0000242 REQUIRE( v.size() == 0 );
243 REQUIRE( v.capacity() >= 5 );
Phil Nashe9173812015-11-04 18:01:28 +0000244
Phil Nashf1936982017-07-13 09:20:37 +0100245 SECTION( "We can use the 'swap trick' to reset the capacity" ) {
Phil Nash29426b62013-03-25 08:47:36 +0000246 std::vector<int> empty;
247 empty.swap( v );
Phil Nashe9173812015-11-04 18:01:28 +0000248
Phil Nash29426b62013-03-25 08:47:36 +0000249 REQUIRE( v.capacity() == 0 );
250 }
251 }
Phil Nashf1936982017-07-13 09:20:37 +0100252 SECTION( "reserving bigger changes capacity but not size" ) {
Phil Nash29426b62013-03-25 08:47:36 +0000253 v.reserve( 10 );
Phil Nashe9173812015-11-04 18:01:28 +0000254
Phil Nash29426b62013-03-25 08:47:36 +0000255 REQUIRE( v.size() == 5 );
256 REQUIRE( v.capacity() >= 10 );
257 }
Phil Nashf1936982017-07-13 09:20:37 +0100258 SECTION( "reserving smaller does not change size or capacity" ) {
Phil Nash29426b62013-03-25 08:47:36 +0000259 v.reserve( 0 );
Phil Nashe9173812015-11-04 18:01:28 +0000260
Phil Nash29426b62013-03-25 08:47:36 +0000261 REQUIRE( v.size() == 5 );
262 REQUIRE( v.capacity() >= 5 );
263 }
264}
Phil Nash0c562692013-05-13 08:20:45 +0100265
266// https://github.com/philsquared/Catch/issues/166
Phil Nashf1936982017-07-13 09:20:37 +0100267TEST_CASE("A couple of nested sections followed by a failure", "[failing][.]") {
Phil Nash9aff9aa2013-07-24 19:13:08 +0100268 SECTION("Outer", "")
269 SECTION("Inner", "")
270 SUCCEED("that's not flying - that's failing in style");
271
272 FAIL("to infinity and beyond");
273}
Phil Nash20cad7c2014-04-15 18:44:37 +0100274
Phil Nashf1936982017-07-13 09:20:37 +0100275TEST_CASE("not allowed", "[!throws]") {
Phil Nash20cad7c2014-04-15 18:44:37 +0100276 // This test case should not be included if you run with -e on the command line
Phil Nashd657b1b2014-06-30 07:53:11 +0100277 SUCCEED( "" );
Phil Nash20cad7c2014-04-15 18:44:37 +0100278}
Phil Nash48153e82014-04-22 08:19:11 +0100279
280//TEST_CASE( "Is big endian" ) {
281// CHECK( Catch::Detail::Endianness::which() == Catch::Detail::Endianness::Little );
282//}
Phil Nash31caba42014-04-23 07:07:27 +0100283
284TEST_CASE( "Tabs and newlines show in output", "[.][whitespace][failing]" ) {
285
286 // Based on issue #242
287 std::string s1 = "if ($b == 10) {\n\t\t$a\t= 20;\n}";
288 std::string s2 = "if ($b == 10) {\n\t$a = 20;\n}\n";
289 CHECK( s1 == s2 );
290}
291
Ben Arnold7b0a84a2014-08-14 12:28:23 +0100292
293TEST_CASE( "toString on const wchar_t const pointer returns the string contents", "[toString]" ) {
Robert A Zeh50956192016-08-24 09:38:24 -0500294 const wchar_t * const s = L"wide load";
Martin Hořeňovský33ed1772017-05-02 23:51:03 +0200295 std::string result = ::Catch::Detail::stringify( s );
Robert A Zeh50956192016-08-24 09:38:24 -0500296 CHECK( result == "\"wide load\"" );
Ben Arnold7b0a84a2014-08-14 12:28:23 +0100297}
298
299TEST_CASE( "toString on const wchar_t pointer returns the string contents", "[toString]" ) {
Robert A Zeh50956192016-08-24 09:38:24 -0500300 const wchar_t * s = L"wide load";
Martin Hořeňovský33ed1772017-05-02 23:51:03 +0200301 std::string result = ::Catch::Detail::stringify( s );
Robert A Zeh50956192016-08-24 09:38:24 -0500302 CHECK( result == "\"wide load\"" );
Ben Arnold7b0a84a2014-08-14 12:28:23 +0100303}
304
305TEST_CASE( "toString on wchar_t const pointer returns the string contents", "[toString]" ) {
Robert A Zeh50956192016-08-24 09:38:24 -0500306 wchar_t * const s = const_cast<wchar_t* const>( L"wide load" );
Martin Hořeňovský33ed1772017-05-02 23:51:03 +0200307 std::string result = ::Catch::Detail::stringify( s );
Robert A Zeh50956192016-08-24 09:38:24 -0500308 CHECK( result == "\"wide load\"" );
Ben Arnold7b0a84a2014-08-14 12:28:23 +0100309}
310
311TEST_CASE( "toString on wchar_t returns the string contents", "[toString]" ) {
Robert A Zeh50956192016-08-24 09:38:24 -0500312 wchar_t * s = const_cast<wchar_t*>( L"wide load" );
Martin Hořeňovský33ed1772017-05-02 23:51:03 +0200313 std::string result = ::Catch::Detail::stringify( s );
Robert A Zeh50956192016-08-24 09:38:24 -0500314 CHECK( result == "\"wide load\"" );
Phil Nash4aaf67f2014-08-19 08:16:44 +0100315}
Phil Nashc1a8e1c2014-08-22 08:07:39 +0100316
Phil Nashc874a992017-07-13 08:52:51 +0100317TEST_CASE( "long long" ) {
Phil Nash733ebb62015-07-23 19:03:33 +0100318 long long l = std::numeric_limits<long long>::max();
Phil Nashe9173812015-11-04 18:01:28 +0000319
Phil Nash733ebb62015-07-23 19:03:33 +0100320 REQUIRE( l == std::numeric_limits<long long>::max() );
321}
Phil Nash733ebb62015-07-23 19:03:33 +0100322
Phil Nash8edf4bf2014-08-22 08:13:15 +0100323//TEST_CASE( "Divide by Zero signal handler", "[.][sig]" ) {
324// int i = 0;
325// int x = 10/i; // This should cause the signal to fire
326// CHECK( x == 0 );
327//}
Phil Nash447f53e2016-03-14 19:13:34 +0000328
Phil Nashf1936982017-07-13 09:20:37 +0100329TEST_CASE( "This test 'should' fail but doesn't", "[.][failing][!shouldfail]" ) {
Phil Nash447f53e2016-03-14 19:13:34 +0000330 SUCCEED( "oops!" );
331}
Phil Nashb1eeec72016-09-27 10:27:28 +0100332
333TEST_CASE( "# A test name that starts with a #" ) {
334 SUCCEED( "yay" );
335}
Martin Hořeňovskýace70402017-03-06 22:07:33 +0100336
Phil Nashdf5c31b2017-08-11 10:38:29 +0100337TEST_CASE( "#835 -- errno should not be touched by Catch", "[.][failing][!shouldfail]" ) {
Martin Hořeňovskýa2e20b02017-03-07 10:17:59 +0100338 errno = 1;
339 CHECK(f() == 0);
Martin Hořeňovský377c9a72017-05-27 14:42:54 +0200340 REQUIRE(errno == 1); // Check that f() doesn't touch errno.
Martin Hořeňovskýace70402017-03-06 22:07:33 +0100341}
Martin Hořeňovskýf06ed852017-08-10 21:38:07 +0200342
343TEST_CASE( "#961 -- Dynamically created sections should all be reported", "[.]" ) {
344 for (char i = '0'; i < '5'; ++i) {
345 SECTION(std::string("Looped section ") + i) {
346 SUCCEED( "Everything is OK" );
347 }
348 }
349}
Phil Nash74d3dfd2017-11-13 16:03:27 +0000350
Phil Nash61e838e2017-11-15 07:48:21 +0000351}} // namespace MiscTests