/ externals / catch / examples / 100-Fix-Section.cpp
100-Fix-Section.cpp
 1  
 2  //              Copyright Catch2 Authors
 3  // Distributed under the Boost Software License, Version 1.0.
 4  //   (See accompanying file LICENSE.txt or copy at
 5  //        https://www.boost.org/LICENSE_1_0.txt)
 6  
 7  // SPDX-License-Identifier: BSL-1.0
 8  
 9  // 100-Fix-Section.cpp
10  
11  // Catch has two ways to express fixtures:
12  // - Sections (this file)
13  // - Traditional class-based fixtures
14  
15  // main() provided by linkage to Catch2WithMain
16  
17  #include <catch2/catch_test_macros.hpp>
18  #include <vector>
19  
20  TEST_CASE( "vectors can be sized and resized", "[vector]" ) {
21  
22      // For each section, vector v is anew:
23  
24      std::vector<int> v( 5 );
25  
26      REQUIRE( v.size() == 5 );
27      REQUIRE( v.capacity() >= 5 );
28  
29      SECTION( "resizing bigger changes size and capacity" ) {
30          v.resize( 10 );
31  
32          REQUIRE( v.size() == 10 );
33          REQUIRE( v.capacity() >= 10 );
34      }
35      SECTION( "resizing smaller changes size but not capacity" ) {
36          v.resize( 0 );
37  
38          REQUIRE( v.size() == 0 );
39          REQUIRE( v.capacity() >= 5 );
40      }
41      SECTION( "reserving bigger changes capacity but not size" ) {
42          v.reserve( 10 );
43  
44          REQUIRE( v.size() == 5 );
45          REQUIRE( v.capacity() >= 10 );
46      }
47      SECTION( "reserving smaller does not change size or capacity" ) {
48          v.reserve( 0 );
49  
50          REQUIRE( v.size() == 5 );
51          REQUIRE( v.capacity() >= 5 );
52      }
53  }
54  
55  // Compile & run:
56  // - g++ -std=c++14 -Wall -I$(CATCH_SINGLE_INCLUDE) -o 100-Fix-Section 100-Fix-Section.cpp && 100-Fix-Section --success
57  // - cl -EHsc -I%CATCH_SINGLE_INCLUDE% 100-Fix-Section.cpp && 100-Fix-Section --success
58  
59  // Expected compact output (all assertions):
60  //
61  // prompt> 100-Fix-Section.exe --reporter compact --success
62  // 100-Fix-Section.cpp:17: passed: v.size() == 5 for: 5 == 5
63  // 100-Fix-Section.cpp:18: passed: v.capacity() >= 5 for: 5 >= 5
64  // 100-Fix-Section.cpp:23: passed: v.size() == 10 for: 10 == 10
65  // 100-Fix-Section.cpp:24: passed: v.capacity() >= 10 for: 10 >= 10
66  // 100-Fix-Section.cpp:17: passed: v.size() == 5 for: 5 == 5
67  // 100-Fix-Section.cpp:18: passed: v.capacity() >= 5 for: 5 >= 5
68  // 100-Fix-Section.cpp:29: passed: v.size() == 0 for: 0 == 0
69  // 100-Fix-Section.cpp:30: passed: v.capacity() >= 5 for: 5 >= 5
70  // 100-Fix-Section.cpp:17: passed: v.size() == 5 for: 5 == 5
71  // 100-Fix-Section.cpp:18: passed: v.capacity() >= 5 for: 5 >= 5
72  // 100-Fix-Section.cpp:35: passed: v.size() == 5 for: 5 == 5
73  // 100-Fix-Section.cpp:36: passed: v.capacity() >= 10 for: 10 >= 10
74  // 100-Fix-Section.cpp:17: passed: v.size() == 5 for: 5 == 5
75  // 100-Fix-Section.cpp:18: passed: v.capacity() >= 5 for: 5 >= 5
76  // 100-Fix-Section.cpp:41: passed: v.size() == 5 for: 5 == 5
77  // 100-Fix-Section.cpp:42: passed: v.capacity() >= 5 for: 5 >= 5
78  // Passed 1 test case with 16 assertions.