Phil Nash | e6b2b0e | 2010-11-29 19:40:44 +0000 | [diff] [blame] | 1 | /* |
Phil Nash | e6b2b0e | 2010-11-29 19:40:44 +0000 | [diff] [blame] | 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) |
Phil Nash | e6b2b0e | 2010-11-29 19:40:44 +0000 | [diff] [blame] | 7 | */ |
Phil Nash | cda2149 | 2012-08-16 18:47:41 +0100 | [diff] [blame] | 8 | |
Phil Nash | 823ea3e | 2011-04-26 08:32:40 +0100 | [diff] [blame] | 9 | #include "catch.hpp" |
Phil Nash | 368714e | 2015-08-07 08:20:56 +0100 | [diff] [blame] | 10 | |
| 11 | #ifdef __clang__ |
| 12 | # pragma clang diagnostic ignored "-Wc++98-compat" |
| 13 | # pragma clang diagnostic ignored "-Wc++98-compat-pedantic" |
| 14 | #endif |
| 15 | |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 16 | |
Phil Nash | 95627c4 | 2011-01-07 10:22:24 +0000 | [diff] [blame] | 17 | #include <iostream> |
Martin Hořeňovský | ace7040 | 2017-03-06 22:07:33 +0100 | [diff] [blame] | 18 | #include <cerrno> |
Martin Hořeňovský | d01fe03 | 2017-07-28 15:11:05 +0200 | [diff] [blame] | 19 | #include <limits> |
Phil Nash | 56e1075 | 2017-11-07 18:01:10 +0000 | [diff] [blame] | 20 | #include <sstream> |
Phil Nash | e6b2b0e | 2010-11-29 19:40:44 +0000 | [diff] [blame] | 21 | |
Phil Nash | 61e838e | 2017-11-15 07:48:21 +0000 | [diff] [blame] | 22 | namespace { 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 | |
| 27 | inline const char* makeString( bool makeNull ) { |
| 28 | return makeNull ? nullptr : "valid string"; |
| 29 | } |
| 30 | inline bool testCheckedIf( bool flag ) { |
| 31 | CHECKED_IF( flag ) |
| 32 | return true; |
| 33 | else |
| 34 | return false; |
| 35 | } |
| 36 | inline bool testCheckedElse( bool flag ) { |
| 37 | CHECKED_ELSE( flag ) |
| 38 | return false; |
| 39 | |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | inline unsigned int Factorial( unsigned int number ) { |
| 44 | return number > 1 ? Factorial(number-1)*number : 1; |
| 45 | } |
| 46 | |
| 47 | static int f() { |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | inline void manuallyRegisteredTestFunction() { |
| 52 | SUCCEED( "was called" ); |
| 53 | } |
| 54 | |
| 55 | struct AutoTestReg { |
| 56 | AutoTestReg() { |
| 57 | REGISTER_TEST_CASE( manuallyRegisteredTestFunction, "ManuallyRegistered" ); |
| 58 | } |
| 59 | }; |
Phil Nash | b5a5d9a | 2017-11-29 19:14:33 +0300 | [diff] [blame] | 60 | CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS |
Phil Nash | 61e838e | 2017-11-15 07:48:21 +0000 | [diff] [blame] | 61 | static AutoTestReg autoTestReg; |
Phil Nash | b5a5d9a | 2017-11-29 19:14:33 +0300 | [diff] [blame] | 62 | CATCH_INTERNAL_UNSUPPRESS_GLOBALS_WARNINGS |
Phil Nash | 61e838e | 2017-11-15 07:48:21 +0000 | [diff] [blame] | 63 | |
| 64 | #endif |
| 65 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 66 | TEST_CASE( "random SECTION tests", "[.][sections][failing]" ) { |
Phil Nash | e6b2b0e | 2010-11-29 19:40:44 +0000 | [diff] [blame] | 67 | int a = 1; |
| 68 | int b = 2; |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 69 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 70 | SECTION( "s1", "doesn't equal" ) { |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 71 | REQUIRE( a != b ); |
| 72 | REQUIRE( b != a ); |
Phil Nash | e6b2b0e | 2010-11-29 19:40:44 +0000 | [diff] [blame] | 73 | } |
Phil Nash | ce319b7 | 2010-11-30 06:48:21 +0000 | [diff] [blame] | 74 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 75 | SECTION( "s2", "not equal" ) { |
Phil Nash | 70e7d7f | 2011-02-17 20:15:20 +0000 | [diff] [blame] | 76 | REQUIRE( a != b); |
Phil Nash | e6b2b0e | 2010-11-29 19:40:44 +0000 | [diff] [blame] | 77 | } |
Phil Nash | e6b2b0e | 2010-11-29 19:40:44 +0000 | [diff] [blame] | 78 | } |
Phil Nash | ce319b7 | 2010-11-30 06:48:21 +0000 | [diff] [blame] | 79 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 80 | TEST_CASE( "nested SECTION tests", "[.][sections][failing]" ) { |
Phil Nash | b3ef7fc | 2010-12-15 19:36:39 +0000 | [diff] [blame] | 81 | int a = 1; |
| 82 | int b = 2; |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 83 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 84 | SECTION( "s1", "doesn't equal" ) { |
Phil Nash | 9a79ee7 | 2010-12-28 17:21:29 +0000 | [diff] [blame] | 85 | REQUIRE( a != b ); |
Phil Nash | b3ef7fc | 2010-12-15 19:36:39 +0000 | [diff] [blame] | 86 | REQUIRE( b != a ); |
| 87 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 88 | SECTION( "s2", "not equal" ) { |
Phil Nash | 70e7d7f | 2011-02-17 20:15:20 +0000 | [diff] [blame] | 89 | REQUIRE( a != b); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 94 | TEST_CASE( "more nested SECTION tests", "[sections][failing][.]" ) { |
Phil Nash | 70e7d7f | 2011-02-17 20:15:20 +0000 | [diff] [blame] | 95 | int a = 1; |
| 96 | int b = 2; |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 97 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 98 | SECTION( "s1", "doesn't equal" ) { |
| 99 | SECTION( "s2", "equal" ) { |
Phil Nash | a243a23 | 2011-02-21 08:50:05 +0000 | [diff] [blame] | 100 | REQUIRE( a == b ); |
Phil Nash | 70e7d7f | 2011-02-17 20:15:20 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 103 | SECTION( "s3", "not equal" ) { |
Phil Nash | a243a23 | 2011-02-21 08:50:05 +0000 | [diff] [blame] | 104 | REQUIRE( a != b ); |
| 105 | } |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 106 | SECTION( "s4", "less than" ) { |
Phil Nash | 088d700 | 2011-02-28 08:18:52 +0000 | [diff] [blame] | 107 | REQUIRE( a < b ); |
Phil Nash | b3ef7fc | 2010-12-15 19:36:39 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 112 | TEST_CASE( "even more nested SECTION tests", "[sections]" ) { |
| 113 | SECTION( "c" ) { |
| 114 | SECTION( "d (leaf)" ) { |
Phil Nash | 015e071 | 2015-11-03 07:46:37 +0000 | [diff] [blame] | 115 | SUCCEED(""); // avoid failing due to no tests |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 116 | } |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 117 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 118 | SECTION( "e (leaf)" ) { |
Phil Nash | 015e071 | 2015-11-03 07:46:37 +0000 | [diff] [blame] | 119 | SUCCEED(""); // avoid failing due to no tests |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 123 | SECTION( "f (leaf)" ) { |
Phil Nash | 015e071 | 2015-11-03 07:46:37 +0000 | [diff] [blame] | 124 | SUCCEED(""); // avoid failing due to no tests |
Phil Nash | b213202 | 2012-05-04 07:55:11 +0100 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 128 | TEST_CASE( "looped SECTION tests", "[.][failing][sections]" ) { |
Phil Nash | 36ff7ba | 2010-12-28 14:41:57 +0000 | [diff] [blame] | 129 | int a = 1; |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 130 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 131 | for( int b = 0; b < 10; ++b ) { |
Phil Nash | 36ff7ba | 2010-12-28 14:41:57 +0000 | [diff] [blame] | 132 | std::ostringstream oss; |
| 133 | oss << "b is currently: " << b; |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 134 | SECTION( "s1", oss.str() ) { |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 135 | CHECK( b > a ); |
Phil Nash | 36ff7ba | 2010-12-28 14:41:57 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 140 | TEST_CASE( "looped tests", "[.][failing]" ) { |
Phil Nash | 36ff7ba | 2010-12-28 14:41:57 +0000 | [diff] [blame] | 141 | static const int fib[] = { 1, 1, 2, 3, 5, 8, 13, 21 }; |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 142 | |
Phil Nash | 8da0d04 | 2017-09-18 17:13:17 +0100 | [diff] [blame] | 143 | for( std::size_t i=0; i < sizeof(fib)/sizeof(int); ++i ) { |
Phil Nash | 36ff7ba | 2010-12-28 14:41:57 +0000 | [diff] [blame] | 144 | INFO( "Testing if fib[" << i << "] (" << fib[i] << ") is even" ); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 145 | CHECK( ( fib[i] % 2 ) == 0 ); |
Phil Nash | 36ff7ba | 2010-12-28 14:41:57 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 149 | TEST_CASE( "Sends stuff to stdout and stderr", "[.]" ) { |
Phil Nash | fb90d38 | 2013-12-19 08:07:33 +0000 | [diff] [blame] | 150 | std::cout << "A string sent directly to stdout" << std::endl; |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 151 | |
Phil Nash | fb90d38 | 2013-12-19 08:07:33 +0000 | [diff] [blame] | 152 | std::cerr << "A string sent directly to stderr" << std::endl; |
Phil Nash | 36ff7ba | 2010-12-28 14:41:57 +0000 | [diff] [blame] | 153 | } |
Phil Nash | 8087f57 | 2011-02-23 20:02:18 +0000 | [diff] [blame] | 154 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 155 | TEST_CASE( "null strings" ) { |
Martin Hořeňovský | 71df663 | 2017-04-25 12:41:30 +0200 | [diff] [blame] | 156 | REQUIRE( makeString( false ) != static_cast<char*>(nullptr)); |
| 157 | REQUIRE( makeString( true ) == static_cast<char*>(nullptr)); |
Phil Nash | 8087f57 | 2011-02-23 20:02:18 +0000 | [diff] [blame] | 158 | } |
Phil Nash | b708789 | 2011-03-09 19:45:05 +0000 | [diff] [blame] | 159 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 160 | TEST_CASE( "checkedIf" ) { |
Phil Nash | a162e22 | 2012-02-10 08:30:13 +0000 | [diff] [blame] | 161 | REQUIRE( testCheckedIf( true ) ); |
| 162 | } |
| 163 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 164 | TEST_CASE( "checkedIf, failing", "[failing][.]" ) { |
Phil Nash | a162e22 | 2012-02-10 08:30:13 +0000 | [diff] [blame] | 165 | REQUIRE( testCheckedIf( false ) ); |
| 166 | } |
| 167 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 168 | TEST_CASE( "checkedElse" ) { |
Phil Nash | a162e22 | 2012-02-10 08:30:13 +0000 | [diff] [blame] | 169 | REQUIRE( testCheckedElse( true ) ); |
| 170 | } |
| 171 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 172 | TEST_CASE( "checkedElse, failing", "[failing][.]" ) { |
Phil Nash | a162e22 | 2012-02-10 08:30:13 +0000 | [diff] [blame] | 173 | REQUIRE( testCheckedElse( false ) ); |
| 174 | } |
Phil Nash | 0b09d1c | 2012-02-10 18:58:06 +0000 | [diff] [blame] | 175 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 176 | TEST_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 Nash | 015e071 | 2015-11-03 07:46:37 +0000 | [diff] [blame] | 178 | SUCCEED(""); // We need this here to stop it failing due to no tests |
Phil Nash | 0b09d1c | 2012-02-10 18:58:06 +0000 | [diff] [blame] | 179 | } |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 180 | SECTION( "encoded chars", "these should all be encoded: &&&\"\"\"<<<&\"<<&\"" ) { |
Phil Nash | 015e071 | 2015-11-03 07:46:37 +0000 | [diff] [blame] | 181 | SUCCEED(""); // We need this here to stop it failing due to no tests |
Phil Nash | 0b09d1c | 2012-02-10 18:58:06 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Phil Nash | af8d0b3 | 2012-02-28 20:04:25 +0000 | [diff] [blame] | 184 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 185 | TEST_CASE( "send a single char to INFO", "[failing][.]" ) { |
Phil Nash | af8d0b3 | 2012-02-28 20:04:25 +0000 | [diff] [blame] | 186 | INFO(3); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 187 | REQUIRE(false); |
Phil Nash | af8d0b3 | 2012-02-28 20:04:25 +0000 | [diff] [blame] | 188 | } |
Phil Nash | 56a02ee | 2012-02-29 08:39:22 +0000 | [diff] [blame] | 189 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 190 | TEST_CASE( "atomic if", "[failing][0]") { |
Phil Nash | 8da0d04 | 2017-09-18 17:13:17 +0100 | [diff] [blame] | 191 | std::size_t x = 0; |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 192 | |
Phil Nash | 56a02ee | 2012-02-29 08:39:22 +0000 | [diff] [blame] | 193 | if( x ) |
| 194 | REQUIRE(x > 0); |
| 195 | else |
| 196 | REQUIRE(x == 0); |
| 197 | } |
Phil Nash | eca5637 | 2012-03-04 11:14:21 +0000 | [diff] [blame] | 198 | |
Phil Nash | 163088a | 2012-05-31 19:40:26 +0100 | [diff] [blame] | 199 | |
Phil Nash | aee9b75 | 2013-06-04 08:37:28 +0100 | [diff] [blame] | 200 | TEST_CASE( "Factorials are computed", "[factorial]" ) { |
Phil Nash | 163088a | 2012-05-31 19:40:26 +0100 | [diff] [blame] | 201 | 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 Nash | a70fbe3 | 2012-08-31 08:10:36 +0100 | [diff] [blame] | 207 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 208 | TEST_CASE( "An empty test with no assertions", "[empty]" ) {} |
Phil Nash | fc1baac | 2012-09-15 17:53:27 +0100 | [diff] [blame] | 209 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 210 | TEST_CASE( "Nice descriptive name", "[tag1][tag2][tag3][.]" ) { |
Phil Nash | fc1baac | 2012-09-15 17:53:27 +0100 | [diff] [blame] | 211 | WARN( "This one ran" ); |
| 212 | } |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 213 | TEST_CASE( "first tag", "[tag1]" ) {} |
| 214 | TEST_CASE( "second tag", "[tag2]" ) {} |
| 215 | |
Phil Nash | 47f679e | 2012-10-04 08:19:09 +0100 | [diff] [blame] | 216 | // |
Phil Nash | 9c39a5e | 2013-06-28 16:05:13 +0100 | [diff] [blame] | 217 | //TEST_CASE( "spawn a new process", "[.]" ) |
Phil Nash | 47f679e | 2012-10-04 08:19:09 +0100 | [diff] [blame] | 218 | //{ |
| 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 Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 225 | |
| 226 | TEST_CASE( "vectors can be sized and resized", "[vector]" ) { |
| 227 | |
| 228 | std::vector<int> v( 5 ); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 229 | |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 230 | REQUIRE( v.size() == 5 ); |
| 231 | REQUIRE( v.capacity() >= 5 ); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 232 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 233 | SECTION( "resizing bigger changes size and capacity" ) { |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 234 | v.resize( 10 ); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 235 | |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 236 | REQUIRE( v.size() == 10 ); |
| 237 | REQUIRE( v.capacity() >= 10 ); |
| 238 | } |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 239 | SECTION( "resizing smaller changes size but not capacity" ) { |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 240 | v.resize( 0 ); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 241 | |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 242 | REQUIRE( v.size() == 0 ); |
| 243 | REQUIRE( v.capacity() >= 5 ); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 244 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 245 | SECTION( "We can use the 'swap trick' to reset the capacity" ) { |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 246 | std::vector<int> empty; |
| 247 | empty.swap( v ); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 248 | |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 249 | REQUIRE( v.capacity() == 0 ); |
| 250 | } |
| 251 | } |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 252 | SECTION( "reserving bigger changes capacity but not size" ) { |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 253 | v.reserve( 10 ); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 254 | |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 255 | REQUIRE( v.size() == 5 ); |
| 256 | REQUIRE( v.capacity() >= 10 ); |
| 257 | } |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 258 | SECTION( "reserving smaller does not change size or capacity" ) { |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 259 | v.reserve( 0 ); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 260 | |
Phil Nash | 29426b6 | 2013-03-25 08:47:36 +0000 | [diff] [blame] | 261 | REQUIRE( v.size() == 5 ); |
| 262 | REQUIRE( v.capacity() >= 5 ); |
| 263 | } |
| 264 | } |
Phil Nash | 0c56269 | 2013-05-13 08:20:45 +0100 | [diff] [blame] | 265 | |
| 266 | // https://github.com/philsquared/Catch/issues/166 |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 267 | TEST_CASE("A couple of nested sections followed by a failure", "[failing][.]") { |
Phil Nash | 9aff9aa | 2013-07-24 19:13:08 +0100 | [diff] [blame] | 268 | 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 Nash | 20cad7c | 2014-04-15 18:44:37 +0100 | [diff] [blame] | 274 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 275 | TEST_CASE("not allowed", "[!throws]") { |
Phil Nash | 20cad7c | 2014-04-15 18:44:37 +0100 | [diff] [blame] | 276 | // This test case should not be included if you run with -e on the command line |
Phil Nash | d657b1b | 2014-06-30 07:53:11 +0100 | [diff] [blame] | 277 | SUCCEED( "" ); |
Phil Nash | 20cad7c | 2014-04-15 18:44:37 +0100 | [diff] [blame] | 278 | } |
Phil Nash | 48153e8 | 2014-04-22 08:19:11 +0100 | [diff] [blame] | 279 | |
| 280 | //TEST_CASE( "Is big endian" ) { |
| 281 | // CHECK( Catch::Detail::Endianness::which() == Catch::Detail::Endianness::Little ); |
| 282 | //} |
Phil Nash | 31caba4 | 2014-04-23 07:07:27 +0100 | [diff] [blame] | 283 | |
| 284 | TEST_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 Arnold | 7b0a84a | 2014-08-14 12:28:23 +0100 | [diff] [blame] | 292 | |
| 293 | TEST_CASE( "toString on const wchar_t const pointer returns the string contents", "[toString]" ) { |
Robert A Zeh | 5095619 | 2016-08-24 09:38:24 -0500 | [diff] [blame] | 294 | const wchar_t * const s = L"wide load"; |
Martin Hořeňovský | 33ed177 | 2017-05-02 23:51:03 +0200 | [diff] [blame] | 295 | std::string result = ::Catch::Detail::stringify( s ); |
Robert A Zeh | 5095619 | 2016-08-24 09:38:24 -0500 | [diff] [blame] | 296 | CHECK( result == "\"wide load\"" ); |
Ben Arnold | 7b0a84a | 2014-08-14 12:28:23 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | TEST_CASE( "toString on const wchar_t pointer returns the string contents", "[toString]" ) { |
Robert A Zeh | 5095619 | 2016-08-24 09:38:24 -0500 | [diff] [blame] | 300 | const wchar_t * s = L"wide load"; |
Martin Hořeňovský | 33ed177 | 2017-05-02 23:51:03 +0200 | [diff] [blame] | 301 | std::string result = ::Catch::Detail::stringify( s ); |
Robert A Zeh | 5095619 | 2016-08-24 09:38:24 -0500 | [diff] [blame] | 302 | CHECK( result == "\"wide load\"" ); |
Ben Arnold | 7b0a84a | 2014-08-14 12:28:23 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | TEST_CASE( "toString on wchar_t const pointer returns the string contents", "[toString]" ) { |
Robert A Zeh | 5095619 | 2016-08-24 09:38:24 -0500 | [diff] [blame] | 306 | wchar_t * const s = const_cast<wchar_t* const>( L"wide load" ); |
Martin Hořeňovský | 33ed177 | 2017-05-02 23:51:03 +0200 | [diff] [blame] | 307 | std::string result = ::Catch::Detail::stringify( s ); |
Robert A Zeh | 5095619 | 2016-08-24 09:38:24 -0500 | [diff] [blame] | 308 | CHECK( result == "\"wide load\"" ); |
Ben Arnold | 7b0a84a | 2014-08-14 12:28:23 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | TEST_CASE( "toString on wchar_t returns the string contents", "[toString]" ) { |
Robert A Zeh | 5095619 | 2016-08-24 09:38:24 -0500 | [diff] [blame] | 312 | wchar_t * s = const_cast<wchar_t*>( L"wide load" ); |
Martin Hořeňovský | 33ed177 | 2017-05-02 23:51:03 +0200 | [diff] [blame] | 313 | std::string result = ::Catch::Detail::stringify( s ); |
Robert A Zeh | 5095619 | 2016-08-24 09:38:24 -0500 | [diff] [blame] | 314 | CHECK( result == "\"wide load\"" ); |
Phil Nash | 4aaf67f | 2014-08-19 08:16:44 +0100 | [diff] [blame] | 315 | } |
Phil Nash | c1a8e1c | 2014-08-22 08:07:39 +0100 | [diff] [blame] | 316 | |
Phil Nash | c874a99 | 2017-07-13 08:52:51 +0100 | [diff] [blame] | 317 | TEST_CASE( "long long" ) { |
Phil Nash | 733ebb6 | 2015-07-23 19:03:33 +0100 | [diff] [blame] | 318 | long long l = std::numeric_limits<long long>::max(); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 319 | |
Phil Nash | 733ebb6 | 2015-07-23 19:03:33 +0100 | [diff] [blame] | 320 | REQUIRE( l == std::numeric_limits<long long>::max() ); |
| 321 | } |
Phil Nash | 733ebb6 | 2015-07-23 19:03:33 +0100 | [diff] [blame] | 322 | |
Phil Nash | 8edf4bf | 2014-08-22 08:13:15 +0100 | [diff] [blame] | 323 | //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 Nash | 447f53e | 2016-03-14 19:13:34 +0000 | [diff] [blame] | 328 | |
Phil Nash | f193698 | 2017-07-13 09:20:37 +0100 | [diff] [blame] | 329 | TEST_CASE( "This test 'should' fail but doesn't", "[.][failing][!shouldfail]" ) { |
Phil Nash | 447f53e | 2016-03-14 19:13:34 +0000 | [diff] [blame] | 330 | SUCCEED( "oops!" ); |
| 331 | } |
Phil Nash | b1eeec7 | 2016-09-27 10:27:28 +0100 | [diff] [blame] | 332 | |
| 333 | TEST_CASE( "# A test name that starts with a #" ) { |
| 334 | SUCCEED( "yay" ); |
| 335 | } |
Martin Hořeňovský | ace7040 | 2017-03-06 22:07:33 +0100 | [diff] [blame] | 336 | |
Phil Nash | df5c31b | 2017-08-11 10:38:29 +0100 | [diff] [blame] | 337 | TEST_CASE( "#835 -- errno should not be touched by Catch", "[.][failing][!shouldfail]" ) { |
Martin Hořeňovský | a2e20b0 | 2017-03-07 10:17:59 +0100 | [diff] [blame] | 338 | errno = 1; |
| 339 | CHECK(f() == 0); |
Martin Hořeňovský | 377c9a7 | 2017-05-27 14:42:54 +0200 | [diff] [blame] | 340 | REQUIRE(errno == 1); // Check that f() doesn't touch errno. |
Martin Hořeňovský | ace7040 | 2017-03-06 22:07:33 +0100 | [diff] [blame] | 341 | } |
Martin Hořeňovský | f06ed85 | 2017-08-10 21:38:07 +0200 | [diff] [blame] | 342 | |
| 343 | TEST_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 Nash | 74d3dfd | 2017-11-13 16:03:27 +0000 | [diff] [blame] | 350 | |
Phil Nash | 61e838e | 2017-11-15 07:48:21 +0000 | [diff] [blame] | 351 | }} // namespace MiscTests |