010-TestCase.cpp 1.3 KB

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