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