blob: 76bca91c74f4fc2df7e45af44a58dc08d60c2629 [file] [log] [blame]
onqtam4a655632016-05-26 14:20:52 +03001#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
2#include "doctest.h"
3
4#include <iostream>
5#include <vector>
6using namespace std;
7
8static int throws(bool in) {
9 if(in)
10 throw 5;
11 return 42;
12}
13
14TEST_CASE("lots of nested subcases") {
15 cout << endl << "root" << endl;
16 SUBCASE("") {
17 cout << "1" << endl;
18 SUBCASE("") { cout << "1.1" << endl; }
19 }
20 SUBCASE("") {
21 cout << "2" << endl;
22 SUBCASE("") { cout << "2.1" << endl; }
23 SUBCASE("") {
24 // whops! all the subcases below shouldn't be discovered and executed!
25 throws(true);
26
27 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
onqtamf90739e2016-09-14 01:01:45 +030043 REQUIRE(v.size() == 5);
44 REQUIRE(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030045
46 WHEN("the size is increased") {
onqtamf90739e2016-09-14 01:01:45 +030047 v.resize(10);
onqtam4a655632016-05-26 14:20:52 +030048
49 THEN("the size and capacity change") {
onqtamf90739e2016-09-14 01:01:45 +030050 CHECK(v.size() == 20);
51 CHECK(v.capacity() >= 10);
onqtam4a655632016-05-26 14:20:52 +030052 }
53 }
54 WHEN("the size is reduced") {
55 v.resize(0);
56
57 THEN("the size changes but not capacity") {
onqtamf90739e2016-09-14 01:01:45 +030058 CHECK(v.size() == 0);
59 CHECK(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030060 }
61 }
62 WHEN("more capacity is reserved") {
63 v.reserve(10);
64
65 THEN("the capacity changes but not the size") {
onqtamf90739e2016-09-14 01:01:45 +030066 CHECK(v.size() == 5);
67 CHECK(v.capacity() >= 10);
onqtam4a655632016-05-26 14:20:52 +030068 }
69 }
70 WHEN("less capacity is reserved") {
71 v.reserve(0);
72
73 THEN("neither size nor capacity are changed") {
onqtamf90739e2016-09-14 01:01:45 +030074 CHECK(v.size() == 10);
75 CHECK(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030076 }
77 }
78 }
79}
80
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}