blob: 6598d6ecf5879131d4a8fbcac6efe2909ec55946 [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)
onqtam4b68df32017-03-17 20:45:54 +020010#ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
onqtam4a655632016-05-26 14:20:52 +030011 throw 5;
onqtam4b68df32017-03-17 20:45:54 +020012#else // DOCTEST_CONFIG_NO_EXCEPTIONS
13 return 0;
14#endif // DOCTEST_CONFIG_NO_EXCEPTIONS
onqtam4a655632016-05-26 14:20:52 +030015 return 42;
16}
17
18TEST_CASE("lots of nested subcases") {
19 cout << endl << "root" << endl;
20 SUBCASE("") {
21 cout << "1" << endl;
22 SUBCASE("") { cout << "1.1" << endl; }
23 }
24 SUBCASE("") {
25 cout << "2" << endl;
26 SUBCASE("") { cout << "2.1" << endl; }
27 SUBCASE("") {
28 // whops! all the subcases below shouldn't be discovered and executed!
29 throws(true);
30
31 cout << "2.2" << endl;
32 SUBCASE("") {
33 cout << "2.2.1" << endl;
34 SUBCASE("") { cout << "2.2.1.1" << endl; }
35 SUBCASE("") { cout << "2.2.1.2" << endl; }
36 }
37 }
38 SUBCASE("") { cout << "2.3" << endl; }
39 SUBCASE("") { cout << "2.4" << endl; }
40 }
41}
42
43SCENARIO("vectors can be sized and resized") {
44 GIVEN("A vector with some items") {
45 std::vector<int> v(5);
46
onqtamf90739e2016-09-14 01:01:45 +030047 REQUIRE(v.size() == 5);
48 REQUIRE(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030049
50 WHEN("the size is increased") {
onqtamf90739e2016-09-14 01:01:45 +030051 v.resize(10);
onqtam4a655632016-05-26 14:20:52 +030052
53 THEN("the size and capacity change") {
onqtamf90739e2016-09-14 01:01:45 +030054 CHECK(v.size() == 20);
55 CHECK(v.capacity() >= 10);
onqtam4a655632016-05-26 14:20:52 +030056 }
57 }
58 WHEN("the size is reduced") {
59 v.resize(0);
60
61 THEN("the size changes but not capacity") {
onqtamf90739e2016-09-14 01:01:45 +030062 CHECK(v.size() == 0);
63 CHECK(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030064 }
65 }
66 WHEN("more capacity is reserved") {
67 v.reserve(10);
68
69 THEN("the capacity changes but not the size") {
onqtamf90739e2016-09-14 01:01:45 +030070 CHECK(v.size() == 5);
71 CHECK(v.capacity() >= 10);
onqtam4a655632016-05-26 14:20:52 +030072 }
73 }
74 WHEN("less capacity is reserved") {
75 v.reserve(0);
76
77 THEN("neither size nor capacity are changed") {
onqtamf90739e2016-09-14 01:01:45 +030078 CHECK(v.size() == 10);
79 CHECK(v.capacity() >= 5);
onqtam4a655632016-05-26 14:20:52 +030080 }
81 }
82 }
83}
84
85// to silence GCC warnings when inheriting from the class TheFixture which has no virtual destructor
86#if defined(__GNUC__) && !defined(__clang__)
87#pragma GCC diagnostic ignored "-Weffc++"
88#endif // __GNUC__
89
90struct TheFixture
91{
92 int data;
93 TheFixture()
94 : data(42) {
95 // setup here
96 }
97
98 ~TheFixture() {
99 // teardown here
100 }
101};
102
103TEST_CASE_FIXTURE(TheFixture, "test with a fixture - 1") {
104 data /= 2;
105 CHECK(data == 21);
106}
107
108TEST_CASE_FIXTURE(TheFixture, "test with a fixture - 2") {
109 data *= 2;
110 CHECK(data == 85);
111}