blob: 837f99e878ec40fb3c81cd365cbf5fdb6bde456e [file] [log] [blame]
onqtame4c75fc2016-05-21 01:24:48 +03001#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
2#include "doctest.h"
3
4#include <iostream>
5#include <vector>
6using namespace std;
7
onqtam733de622016-05-21 16:31:13 +03008static int throws(bool in) {
9 if(in)
10 throw 5;
11 return 42;
12}
13
onqtame4c75fc2016-05-21 01:24:48 +030014TEST_CASE("lots of nested subcases") {
15 cout << endl << "root" << endl;
16 SUBCASE("") {
17 cout << "1" << endl;
18 SUBCASE("") { cout << "1.1" << endl; }
19 }
onqtam733de622016-05-21 16:31:13 +030020 SUBCASE("") {
onqtame4c75fc2016-05-21 01:24:48 +030021 cout << "2" << endl;
22 SUBCASE("") { cout << "2.1" << endl; }
23 SUBCASE("") {
onqtam733de622016-05-21 16:31:13 +030024 // whops! all the subcases below shouldn't be discovered and executed!
25 throws(true);
26
onqtame4c75fc2016-05-21 01:24:48 +030027 cout << "2.2" << endl;
28 SUBCASE("") {
29 cout << "2.2.1" << endl;
30 SUBCASE("") { cout << "2.2.1.1" << endl; }
31 SUBCASE("") { cout << "2.2.1.2" << endl; }
32 }
33 }
34 SUBCASE("") { cout << "2.3" << endl; }
35 SUBCASE("") { cout << "2.4" << endl; }
36 }
37}
38
39SCENARIO("vectors can be sized and resized") {
40 GIVEN("A vector with some items") {
41 std::vector<int> v(5);
42
43 REQUIRE(v.size() == 5u);
44 REQUIRE(v.capacity() >= 5u);
45
46 WHEN("the size is increased") {
47 v.resize(10u);
48
49 THEN("the size and capacity change") {
50 CHECK(v.size() == 20u);
51 CHECK(v.capacity() >= 10u);
52 }
53 }
54 WHEN("the size is reduced") {
55 v.resize(0);
56
57 THEN("the size changes but not capacity") {
58 CHECK(v.size() == 0u);
59 CHECK(v.capacity() >= 5u);
60 }
61 }
62 WHEN("more capacity is reserved") {
63 v.reserve(10);
64
65 THEN("the capacity changes but not the size") {
66 CHECK(v.size() == 5u);
67 CHECK(v.capacity() >= 10u);
68 }
69 }
70 WHEN("less capacity is reserved") {
71 v.reserve(0);
72
73 THEN("neither size nor capacity are changed") {
74 CHECK(v.size() == 10u);
75 CHECK(v.capacity() >= 5u);
76 }
77 }
78 }
onqtamc832deb2016-05-21 01:27:49 +030079}
onqtam733de622016-05-21 16:31:13 +030080
81// to silence GCC warnings when inheriting from the class TheFixture which has no virtual destructor
82#if defined(__GNUC__) && !defined(__clang__)
83#pragma GCC diagnostic ignored "-Weffc++"
84#endif // __GNUC__
85
86struct TheFixture
87{
88 int data;
89 TheFixture()
90 : data(42) {
91 // setup here
92 }
93
94 ~TheFixture() {
95 // teardown here
96 }
97};
98
99TEST_CASE_FIXTURE(TheFixture, "test with a fixture - 1") {
100 data /= 2;
101 CHECK(data == 21);
102}
103
104TEST_CASE_FIXTURE(TheFixture, "test with a fixture - 2") {
105 data *= 2;
106 CHECK(data == 85);
107}