100-Fix-Section.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // 100-Fix-Section.cpp
  2. // Catch has two ways to express fixtures:
  3. // - Sections (this file)
  4. // - Traditional class-based fixtures
  5. // main() provided in 000-CatchMain.cpp
  6. #include <catch2/catch.hpp>
  7. TEST_CASE( "vectors can be sized and resized", "[vector]" ) {
  8. // For each section, vector v is anew:
  9. std::vector<int> v( 5 );
  10. REQUIRE( v.size() == 5 );
  11. REQUIRE( v.capacity() >= 5 );
  12. SECTION( "resizing bigger changes size and capacity" ) {
  13. v.resize( 10 );
  14. REQUIRE( v.size() == 10 );
  15. REQUIRE( v.capacity() >= 10 );
  16. }
  17. SECTION( "resizing smaller changes size but not capacity" ) {
  18. v.resize( 0 );
  19. REQUIRE( v.size() == 0 );
  20. REQUIRE( v.capacity() >= 5 );
  21. }
  22. SECTION( "reserving bigger changes capacity but not size" ) {
  23. v.reserve( 10 );
  24. REQUIRE( v.size() == 5 );
  25. REQUIRE( v.capacity() >= 10 );
  26. }
  27. SECTION( "reserving smaller does not change size or capacity" ) {
  28. v.reserve( 0 );
  29. REQUIRE( v.size() == 5 );
  30. REQUIRE( v.capacity() >= 5 );
  31. }
  32. }
  33. // Compile & run:
  34. // - g++ -std=c++11 -Wall -I$(CATCH_SINGLE_INCLUDE) -o 100-Fix-Section 100-Fix-Section.cpp 000-CatchMain.o && 100-Fix-Section --success
  35. // - cl -EHsc -I%CATCH_SINGLE_INCLUDE% 100-Fix-Section.cpp 000-CatchMain.obj && 100-Fix-Section --success
  36. // Expected compact output (all assertions):
  37. //
  38. // prompt> 100-Fix-Section.exe --reporter compact --success
  39. // 100-Fix-Section.cpp:17: passed: v.size() == 5 for: 5 == 5
  40. // 100-Fix-Section.cpp:18: passed: v.capacity() >= 5 for: 5 >= 5
  41. // 100-Fix-Section.cpp:23: passed: v.size() == 10 for: 10 == 10
  42. // 100-Fix-Section.cpp:24: passed: v.capacity() >= 10 for: 10 >= 10
  43. // 100-Fix-Section.cpp:17: passed: v.size() == 5 for: 5 == 5
  44. // 100-Fix-Section.cpp:18: passed: v.capacity() >= 5 for: 5 >= 5
  45. // 100-Fix-Section.cpp:29: passed: v.size() == 0 for: 0 == 0
  46. // 100-Fix-Section.cpp:30: passed: v.capacity() >= 5 for: 5 >= 5
  47. // 100-Fix-Section.cpp:17: passed: v.size() == 5 for: 5 == 5
  48. // 100-Fix-Section.cpp:18: passed: v.capacity() >= 5 for: 5 >= 5
  49. // 100-Fix-Section.cpp:35: passed: v.size() == 5 for: 5 == 5
  50. // 100-Fix-Section.cpp:36: passed: v.capacity() >= 10 for: 10 >= 10
  51. // 100-Fix-Section.cpp:17: passed: v.size() == 5 for: 5 == 5
  52. // 100-Fix-Section.cpp:18: passed: v.capacity() >= 5 for: 5 >= 5
  53. // 100-Fix-Section.cpp:41: passed: v.size() == 5 for: 5 == 5
  54. // 100-Fix-Section.cpp:42: passed: v.capacity() >= 5 for: 5 >= 5
  55. // Passed 1 test case with 16 assertions.