Debian Codegraph @

src/external/catch2/tests/SelfTest/UsageTests/Misc.tests.cpp

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