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