100-Fix-Section.cpp 2.5 KB

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