onqtam | e4c75fc | 2016-05-21 01:24:48 +0300 | [diff] [blame] | 1 | #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
| 2 | #include "doctest.h"
|
| 3 |
|
| 4 | #include <iostream>
|
| 5 | #include <vector>
|
| 6 | using namespace std;
|
| 7 |
|
onqtam | 733de62 | 2016-05-21 16:31:13 +0300 | [diff] [blame] | 8 | static int throws(bool in) {
|
| 9 | if(in)
|
| 10 | throw 5;
|
| 11 | return 42;
|
| 12 | }
|
| 13 |
|
onqtam | e4c75fc | 2016-05-21 01:24:48 +0300 | [diff] [blame] | 14 | TEST_CASE("lots of nested subcases") {
|
| 15 | cout << endl << "root" << endl;
|
| 16 | SUBCASE("") {
|
| 17 | cout << "1" << endl;
|
| 18 | SUBCASE("") { cout << "1.1" << endl; }
|
| 19 | }
|
onqtam | 733de62 | 2016-05-21 16:31:13 +0300 | [diff] [blame] | 20 | SUBCASE("") {
|
onqtam | e4c75fc | 2016-05-21 01:24:48 +0300 | [diff] [blame] | 21 | cout << "2" << endl;
|
| 22 | SUBCASE("") { cout << "2.1" << endl; }
|
| 23 | SUBCASE("") {
|
onqtam | 733de62 | 2016-05-21 16:31:13 +0300 | [diff] [blame] | 24 | // whops! all the subcases below shouldn't be discovered and executed!
|
| 25 | throws(true);
|
| 26 |
|
onqtam | e4c75fc | 2016-05-21 01:24:48 +0300 | [diff] [blame] | 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 |
|
| 39 | SCENARIO("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 | }
|
onqtam | c832deb | 2016-05-21 01:27:49 +0300 | [diff] [blame] | 79 | }
|
onqtam | 733de62 | 2016-05-21 16:31:13 +0300 | [diff] [blame] | 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 |
|
| 86 | struct TheFixture
|
| 87 | {
|
| 88 | int data;
|
| 89 | TheFixture()
|
| 90 | : data(42) {
|
| 91 | // setup here
|
| 92 | }
|
| 93 |
|
| 94 | ~TheFixture() {
|
| 95 | // teardown here
|
| 96 | }
|
| 97 | };
|
| 98 |
|
| 99 | TEST_CASE_FIXTURE(TheFixture, "test with a fixture - 1") {
|
| 100 | data /= 2;
|
| 101 | CHECK(data == 21);
|
| 102 | }
|
| 103 |
|
| 104 | TEST_CASE_FIXTURE(TheFixture, "test with a fixture - 2") {
|
| 105 | data *= 2;
|
| 106 | CHECK(data == 85);
|
| 107 | }
|