onqtam | 4a65563 | 2016-05-26 14:20:52 +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 | |
| 8 | static int throws(bool in) { |
| 9 | if(in) |
| 10 | throw 5; |
| 11 | return 42; |
| 12 | } |
| 13 | |
| 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 | } |
| 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 | |
| 39 | SCENARIO("vectors can be sized and resized") { |
| 40 | GIVEN("A vector with some items") { |
| 41 | std::vector<int> v(5); |
| 42 | |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 43 | REQUIRE(v.size() == 5); |
| 44 | REQUIRE(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 45 | |
| 46 | WHEN("the size is increased") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 47 | v.resize(10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 48 | |
| 49 | THEN("the size and capacity change") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 50 | CHECK(v.size() == 20); |
| 51 | CHECK(v.capacity() >= 10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | WHEN("the size is reduced") { |
| 55 | v.resize(0); |
| 56 | |
| 57 | THEN("the size changes but not capacity") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 58 | CHECK(v.size() == 0); |
| 59 | CHECK(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | WHEN("more capacity is reserved") { |
| 63 | v.reserve(10); |
| 64 | |
| 65 | THEN("the capacity changes but not the size") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 66 | CHECK(v.size() == 5); |
| 67 | CHECK(v.capacity() >= 10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | WHEN("less capacity is reserved") { |
| 71 | v.reserve(0); |
| 72 | |
| 73 | THEN("neither size nor capacity are changed") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 74 | CHECK(v.size() == 10); |
| 75 | CHECK(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 76 | } |
| 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 | |
| 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 | } |