Debian Codegraph @

3rdparty/catch2/tests/SelfTest/UsageTests/Misc.tests.cpp

raw symbols SCIP
1
2 // Copyright Catch2 Authors
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE.txt or copy at
5 // https://www.boost.org/LICENSE_1_0.txt)
6
7 // SPDX-License-Identifier: BSL-1.0
8
9 #include <catch2/catch_test_macros.hpp>
10 #include <catch2/catch_template_test_macros.hpp>
11 #include <catch2/internal/catch_config_wchar.hpp>
12 #include <catch2/internal/catch_windows_h_proxy.hpp>
13
14 #ifdef __clang__
15 # pragma clang diagnostic ignored "-Wc++98-compat"
16 # pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
17 #endif
18
19
20 #include <iostream>
21 #include <cerrno>
22 #include <limits>
23 #include <array>
24 #include <tuple>
25
26 namespace {
27
28 static const char* makeString(bool makeNull) {
29 return makeNull ? nullptr : "valid string";
30 }
31 static bool testCheckedIf(bool flag) {
32 CHECKED_IF(flag)
33 return true;
34 else
35 return false;
36 }
37 static bool testCheckedElse(bool flag) {
38 CHECKED_ELSE(flag)
39 return false;
40
41 return true;
42 }
43
44 static unsigned int Factorial(unsigned int number) {
45 return number > 1 ? Factorial(number - 1) * number : 1;
46 }
47
48 static int f() {
49 return 1;
50 }
51
52 static void manuallyRegisteredTestFunction() {
53 SUCCEED("was called");
54 }
55
56 struct AutoTestReg {
57 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 {
69 size_t size() { return 0; }
70 };
71
72 template<typename T, size_t S>
73 struct Bar {
74 size_t size() { return S; }
75 };
76
77 }
78
79 TEST_CASE( "random SECTION tests", "[.][sections][failing]" ) {
80 int a = 1;
81 int b = 2;
82
83 SECTION( "doesn't equal" ) {
84 REQUIRE( a != b );
85 REQUIRE( b != a );
86 }
87
88 SECTION( "not equal" ) {
89 REQUIRE( a != b);
90 }
91 }
92
93 TEST_CASE( "nested SECTION tests", "[.][sections][failing]" ) {
94 int a = 1;
95 int b = 2;
96
97 SECTION( "doesn't equal" ) {
98 REQUIRE( a != b );
99 REQUIRE( b != a );
100
101 SECTION( "not equal" ) {
102 REQUIRE( a != b);
103 }
104 }
105 }
106
107 TEST_CASE( "more nested SECTION tests", "[sections][failing][.]" ) {
108 int a = 1;
109 int b = 2;
110
111 SECTION( "doesn't equal" ) {
112 SECTION( "equal" ) {
113 REQUIRE( a == b );
114 }
115
116 SECTION( "not equal" ) {
117 REQUIRE( a != b );
118 }
119 SECTION( "less than" ) {
120 REQUIRE( a < b );
121 }
122 }
123 }
124
125 TEST_CASE( "even more nested SECTION tests", "[sections]" ) {
126 SECTION( "c" ) {
127 SECTION( "d (leaf)" ) {
128 SUCCEED(); // avoid failing due to no tests
129 }
130
131 SECTION( "e (leaf)" ) {
132 SUCCEED(); // avoid failing due to no tests
133 }
134 }
135
136 SECTION( "f (leaf)" ) {
137 SUCCEED(); // avoid failing due to no tests
138 }
139 }
140
141 TEST_CASE( "looped SECTION tests", "[.][failing][sections]" ) {
142 int a = 1;
143
144 for( int b = 0; b < 10; ++b ) {
145 DYNAMIC_SECTION( "b is currently: " << b ) {
146 CHECK( b > a );
147 }
148 }
149 }
150
151 TEST_CASE( "looped tests", "[.][failing]" ) {
152 static const int fib[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
153
154 for( std::size_t i=0; i < sizeof(fib)/sizeof(int); ++i ) {
155 INFO( "Testing if fib[" << i << "] (" << fib[i] << ") is even" );
156 CHECK( ( fib[i] % 2 ) == 0 );
157 }
158 }
159
160 TEST_CASE( "Sends stuff to stdout and stderr", "[.]" ) {
161 std::cout << "A string sent directly to stdout" << std::endl;
162 std::cerr << "A string sent directly to stderr" << std::endl;
163 std::clog << "A string sent to stderr via clog" << std::endl;
164 }
165
166 TEST_CASE( "null strings" ) {
167 REQUIRE( makeString( false ) != static_cast<char*>(nullptr));
168 REQUIRE( makeString( true ) == static_cast<char*>(nullptr));
169 }
170
171 TEST_CASE( "checkedIf" ) {
172 REQUIRE( testCheckedIf( true ) );
173 }
174
175 TEST_CASE( "checkedIf, failing", "[failing][.]" ) {
176 REQUIRE( testCheckedIf( false ) );
177 }
178
179 TEST_CASE( "checkedElse" ) {
180 REQUIRE( testCheckedElse( true ) );
181 }
182
183 TEST_CASE( "checkedElse, failing", "[failing][.]" ) {
184 REQUIRE( testCheckedElse( false ) );
185 }
186
187 TEST_CASE("Testing checked-if", "[checked-if]") {
188 CHECKED_IF(true) {
189 SUCCEED();
190 }
191 CHECKED_IF(false) {
192 FAIL();
193 }
194 CHECKED_ELSE(true) {
195 FAIL();
196 }
197 CHECKED_ELSE(false) {
198 SUCCEED();
199 }
200 }
201
202 TEST_CASE("Testing checked-if 2", "[checked-if][!shouldfail]") {
203 CHECKED_IF(true) {
204 FAIL();
205 }
206 // If the checked if is not entered, this passes and the test
207 // fails, because of the [!shouldfail] tag.
208 SUCCEED();
209 }
210
211 TEST_CASE("Testing checked-if 3", "[checked-if][!shouldfail]") {
212 CHECKED_ELSE(false) {
213 FAIL();
214 }
215 // If the checked false is not entered, this passes and the test
216 // fails, because of the [!shouldfail] tag.
217 SUCCEED();
218 }
219
220 TEST_CASE( "xmlentitycheck" ) {
221 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>" ) {
222 SUCCEED(); // We need this here to stop it failing due to no tests
223 }
224 SECTION( "encoded chars: these should all be encoded: &&&\"\"\"<<<&\"<<&\"" ) {
225 SUCCEED(); // We need this here to stop it failing due to no tests
226 }
227 }
228
229 TEST_CASE( "send a single char to INFO", "[failing][.]" ) {
230 INFO(3);
231 REQUIRE(false);
232 }
233
234 TEST_CASE( "Factorials are computed", "[factorial]" ) {
235 REQUIRE( Factorial(0) == 1 );
236 REQUIRE( Factorial(1) == 1 );
237 REQUIRE( Factorial(2) == 2 );
238 REQUIRE( Factorial(3) == 6 );
239 REQUIRE( Factorial(10) == 3628800 );
240 }
241
242 TEST_CASE( "An empty test with no assertions", "[empty]" ) {}
243
244 TEST_CASE( "Nice descriptive name", "[tag1][tag2][tag3][.]" ) {
245 WARN( "This one ran" );
246 }
247 TEST_CASE( "first tag", "[tag1]" ) {}
248 TEST_CASE( "second tag", "[tag2]" ) {}
249
250 TEST_CASE( "vectors can be sized and resized", "[vector]" ) {
251
252 std::vector<int> v( 5 );
253
254 REQUIRE( v.size() == 5 );
255 REQUIRE( v.capacity() >= 5 );
256
257 SECTION( "resizing bigger changes size and capacity" ) {
258 v.resize( 10 );
259
260 REQUIRE( v.size() == 10 );
261 REQUIRE( v.capacity() >= 10 );
262 }
263 SECTION( "resizing smaller changes size but not capacity" ) {
264 v.resize( 0 );
265
266 REQUIRE( v.size() == 0 );
267 REQUIRE( v.capacity() >= 5 );
268
269 SECTION( "We can use the 'swap trick' to reset the capacity" ) {
270 std::vector<int> empty;
271 empty.swap( v );
272
273 REQUIRE( v.capacity() == 0 );
274 }
275 }
276 SECTION( "reserving bigger changes capacity but not size" ) {
277 v.reserve( 10 );
278
279 REQUIRE( v.size() == 5 );
280 REQUIRE( v.capacity() >= 10 );
281 }
282 SECTION( "reserving smaller does not change size or capacity" ) {
283 v.reserve( 0 );
284
285 REQUIRE( v.size() == 5 );
286 REQUIRE( v.capacity() >= 5 );
287 }
288 }
289
290 TEMPLATE_TEST_CASE( "TemplateTest: vectors can be sized and resized", "[vector][template]", int, float, std::string, (std::tuple<int,float>) ) {
291
292 std::vector<TestType> v( 5 );
293
294 REQUIRE( v.size() == 5 );
295 REQUIRE( v.capacity() >= 5 );
296
297 SECTION( "resizing bigger changes size and capacity" ) {
298 v.resize( 10 );
299
300 REQUIRE( v.size() == 10 );
301 REQUIRE( v.capacity() >= 10 );
302 }
303 SECTION( "resizing smaller changes size but not capacity" ) {
304 v.resize( 0 );
305
306 REQUIRE( v.size() == 0 );
307 REQUIRE( v.capacity() >= 5 );
308
309 SECTION( "We can use the 'swap trick' to reset the capacity" ) {
310 std::vector<TestType> empty;
311 empty.swap( v );
312
313 REQUIRE( v.capacity() == 0 );
314 }
315 }
316 SECTION( "reserving bigger changes capacity but not size" ) {
317 v.reserve( 10 );
318
319 REQUIRE( v.size() == 5 );
320 REQUIRE( v.capacity() >= 10 );
321 }
322 SECTION( "reserving smaller does not change size or capacity" ) {
323 v.reserve( 0 );
324
325 REQUIRE( v.size() == 5 );
326 REQUIRE( v.capacity() >= 5 );
327 }
328 }
329
330 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)) {
331
332 std::vector<TestType> v(V);
333
334 REQUIRE(v.size() == V);
335 REQUIRE(v.capacity() >= V);
336
337 SECTION("resizing bigger changes size and capacity") {
338 v.resize(2 * V);
339
340 REQUIRE(v.size() == 2 * V);
341 REQUIRE(v.capacity() >= 2 * V);
342 }
343 SECTION("resizing smaller changes size but not capacity") {
344 v.resize(0);
345
346 REQUIRE(v.size() == 0);
347 REQUIRE(v.capacity() >= V);
348
349 SECTION("We can use the 'swap trick' to reset the capacity") {
350 std::vector<TestType> empty;
351 empty.swap(v);
352
353 REQUIRE(v.capacity() == 0);
354 }
355 }
356 SECTION("reserving bigger changes capacity but not size") {
357 v.reserve(2 * V);
358
359 REQUIRE(v.size() == V);
360 REQUIRE(v.capacity() >= 2 * V);
361 }
362 SECTION("reserving smaller does not change size or capacity") {
363 v.reserve(0);
364
365 REQUIRE(v.size() == V);
366 REQUIRE(v.capacity() >= V);
367 }
368 }
369
370 TEMPLATE_PRODUCT_TEST_CASE("A Template product test case", "[template][product]", (std::vector, Foo), (int, float)) {
371 TestType x;
372 REQUIRE(x.size() == 0);
373 }
374
375 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))) {
376 TestType x;
377 REQUIRE(x.size() > 0);
378 }
379
380 TEMPLATE_PRODUCT_TEST_CASE("Product with differing arities", "[template][product]", std::tuple, (int, (int, double), (int, double, float))) {
381 REQUIRE(std::tuple_size<TestType>::value >= 1);
382 }
383
384 using MyTypes = std::tuple<int, char, float>;
385 TEMPLATE_LIST_TEST_CASE("Template test case with test types specified inside std::tuple", "[template][list]", MyTypes)
386 {
387 REQUIRE(sizeof(TestType) > 0);
388 }
389
390 struct NonDefaultConstructibleType {
391 NonDefaultConstructibleType() = delete;
392 };
393
394 using MyNonDefaultConstructibleTypes = std::tuple<NonDefaultConstructibleType, float>;
395 TEMPLATE_LIST_TEST_CASE("Template test case with test types specified inside non-default-constructible std::tuple", "[template][list]", MyNonDefaultConstructibleTypes)
396 {
397 REQUIRE(sizeof(TestType) > 0);
398 }
399
400 struct NonCopyableAndNonMovableType {
401 NonCopyableAndNonMovableType() = default;
402
403 NonCopyableAndNonMovableType(NonCopyableAndNonMovableType const &) = delete;
404 NonCopyableAndNonMovableType(NonCopyableAndNonMovableType &&) = delete;
405 auto operator=(NonCopyableAndNonMovableType const &) -> NonCopyableAndNonMovableType & = delete;
406 auto operator=(NonCopyableAndNonMovableType &&) -> NonCopyableAndNonMovableType & = delete;
407 };
408
409 using NonCopyableAndNonMovableTypes = std::tuple<NonCopyableAndNonMovableType, float>;
410 TEMPLATE_LIST_TEST_CASE("Template test case with test types specified inside non-copyable and non-movable std::tuple", "[template][list]", NonCopyableAndNonMovableTypes)
411 {
412 REQUIRE(sizeof(TestType) > 0);
413 }
414
415 // https://github.com/philsquared/Catch/issues/166
416 TEST_CASE("A couple of nested sections followed by a failure", "[failing][.]") {
417 SECTION("Outer")
418 SECTION("Inner")
419 SUCCEED("that's not flying - that's failing in style");
420
421 FAIL("to infinity and beyond");
422 }
423
424 TEST_CASE("not allowed", "[!throws]") {
425 // This test case should not be included if you run with -e on the command line
426 SUCCEED();
427 }
428
429 TEST_CASE( "Tabs and newlines show in output", "[.][whitespace][failing]" ) {
430
431 // Based on issue #242
432 std::string s1 = "if ($b == 10) {\n\t\t$a\t= 20;\n}";
433 std::string s2 = "if ($b == 10) {\n\t$a = 20;\n}\n";
434 CHECK( s1 == s2 );
435 }
436
437
438 #if defined(CATCH_CONFIG_WCHAR)
439 TEST_CASE( "toString on const wchar_t const pointer returns the string contents", "[toString]" ) {
440 const wchar_t * const s = L"wide load";
441 std::string result = ::Catch::Detail::stringify( s );
442 CHECK( result == "\"wide load\"" );
443 }
444
445 TEST_CASE( "toString on const wchar_t pointer returns the string contents", "[toString]" ) {
446 const wchar_t * s = L"wide load";
447 std::string result = ::Catch::Detail::stringify( s );
448 CHECK( result == "\"wide load\"" );
449 }
450
451 TEST_CASE( "toString on wchar_t const pointer returns the string contents", "[toString]" ) {
452 auto const s = const_cast<wchar_t*>( L"wide load" );
453 std::string result = ::Catch::Detail::stringify( s );
454 CHECK( result == "\"wide load\"" );
455 }
456
457 TEST_CASE( "toString on wchar_t returns the string contents", "[toString]" ) {
458 auto s = const_cast<wchar_t*>( L"wide load" );
459 std::string result = ::Catch::Detail::stringify( s );
460 CHECK( result == "\"wide load\"" );
461 }
462 #endif // CATCH_CONFIG_WCHAR
463
464 TEST_CASE( "long long" ) {
465 constexpr long long l = std::numeric_limits<long long>::max();
466
467 REQUIRE( l == std::numeric_limits<long long>::max() );
468 }
469
470 TEST_CASE( "This test 'should' fail but doesn't", "[.][failing][!shouldfail]" ) {
471 SUCCEED( "oops!" );
472 }
473
474 TEST_CASE( "# A test name that starts with a #" ) {
475 SUCCEED( "yay" );
476 }
477
478 TEST_CASE( "#835 -- errno should not be touched by Catch2", "[.][failing][!shouldfail]" ) {
479 errno = 1;
480 // Check that reporting failed test doesn't change errno.
481 CHECK(f() == 0);
482 // We want to avoid expanding `errno` macro in assertion, because
483 // we capture the expression after macro expansion, and would have
484 // to normalize the ways different platforms spell `errno`.
485 const auto errno_after = errno;
486 REQUIRE(errno_after == 1);
487 }
488
489 TEST_CASE( "#961 -- Dynamically created sections should all be reported", "[.]" ) {
490 for (char i = '0'; i < '5'; ++i) {
491 SECTION(std::string("Looped section ") + i) {
492 SUCCEED( "Everything is OK" );
493 }
494 }
495 }
496
497 TEST_CASE( "#1175 - Hidden Test", "[.]" ) {
498 // Just for checking that hidden test is not listed by default
499 SUCCEED();
500 }
501
502 TEMPLATE_TEST_CASE_SIG("#1954 - 7 arg template test case sig compiles", "[regression][.compilation]",
503 ((int Tnx, int Tnu, int Tny, int Tph, int Tch, int Tineq, int Teq), Tnx, Tnu, Tny, Tph, Tch, Tineq, Teq),
504 (1, 1, 1, 1, 1, 0, 0), (5, 1, 1, 1, 1, 0, 0), (5, 3, 1, 1, 1, 0, 0)) {
505 SUCCEED();
506 }
507
508 TEST_CASE("Same test name but with different tags is fine", "[.approvals][some-tag]") {}
509 TEST_CASE("Same test name but with different tags is fine", "[.approvals][other-tag]") {}
510
511 // MinGW doesn't support __try, and Clang has only very partial support
512 #if defined(_MSC_VER)
513 void throw_and_catch()
514 {
515 __try {
516 RaiseException(0xC0000005, 0, 0, NULL);
517 }
518 __except (1)
519 {
520
521 }
522 }
523
524
525 TEST_CASE("Validate SEH behavior - handled", "[approvals][FatalConditionHandler][CATCH_PLATFORM_WINDOWS]")
526 {
527 // Validate that Catch2 framework correctly handles tests raising and handling SEH exceptions.
528 throw_and_catch();
529 }
530
531 void throw_no_catch()
532 {
533 RaiseException(0xC0000005, 0, 0, NULL);
534 }
535
536 TEST_CASE("Validate SEH behavior - unhandled", "[.approvals][FatalConditionHandler][CATCH_PLATFORM_WINDOWS]")
537 {
538 // Validate that Catch2 framework correctly handles tests raising and not handling SEH exceptions.
539 throw_no_catch();
540 }
541
542 static LONG CALLBACK dummyExceptionFilter(PEXCEPTION_POINTERS ExceptionInfo) {
543 return EXCEPTION_CONTINUE_SEARCH;
544 }
545
546 TEST_CASE("Validate SEH behavior - no crash for stack unwinding", "[approvals][!throws][!shouldfail][FatalConditionHandler][CATCH_PLATFORM_WINDOWS]")
547 {
548 // Trigger stack unwinding with SEH top-level filter changed and validate the test fails expectedly with no application crash
549 SetUnhandledExceptionFilter(dummyExceptionFilter);
550 throw 1;
551 }
552
553 #endif // _MSC_VER