1 // 010-TestCase.cpp 2 3 // Let Catch provide main(): 4 #define CATCH_CONFIG_MAIN 5 6 #include <catch2/catch.hpp> 7 Factorial(int number)8int Factorial( int number ) { 9 return number <= 1 ? number : Factorial( number - 1 ) * number; // fail 10 // return number <= 1 ? 1 : Factorial( number - 1 ) * number; // pass 11 } 12 13 TEST_CASE( "Factorial of 0 is 1 (fail)", "[single-file]" ) { 14 REQUIRE( Factorial(0) == 1 ); 15 } 16 17 TEST_CASE( "Factorials of 1 and higher are computed (pass)", "[single-file]" ) { 18 REQUIRE( Factorial(1) == 1 ); 19 REQUIRE( Factorial(2) == 2 ); 20 REQUIRE( Factorial(3) == 6 ); 21 REQUIRE( Factorial(10) == 3628800 ); 22 } 23 24 // Compile & run: 25 // - g++ -std=c++11 -Wall -I$(CATCH_SINGLE_INCLUDE) -o 010-TestCase 010-TestCase.cpp && 010-TestCase --success 26 // - cl -EHsc -I%CATCH_SINGLE_INCLUDE% 010-TestCase.cpp && 010-TestCase --success 27 28 // Expected compact output (all assertions): 29 // 30 // prompt> 010-TestCase --reporter compact --success 31 // 010-TestCase.cpp:14: failed: Factorial(0) == 1 for: 0 == 1 32 // 010-TestCase.cpp:18: passed: Factorial(1) == 1 for: 1 == 1 33 // 010-TestCase.cpp:19: passed: Factorial(2) == 2 for: 2 == 2 34 // 010-TestCase.cpp:20: passed: Factorial(3) == 6 for: 6 == 6 35 // 010-TestCase.cpp:21: passed: Factorial(10) == 3628800 for: 3628800 (0x375f00) == 3628800 (0x375f00) 36 // Failed 1 test case, failed 1 assertion. 37