010-TestCase.cpp 1.3 KB

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