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) |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame] | 10 | #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 11 | throw 5; |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame] | 12 | #else // DOCTEST_CONFIG_NO_EXCEPTIONS |
| 13 | return 0; |
| 14 | #endif // DOCTEST_CONFIG_NO_EXCEPTIONS |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 15 | return 42; |
| 16 | } |
| 17 | |
| 18 | TEST_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 | |
| 43 | SCENARIO("vectors can be sized and resized") { |
| 44 | GIVEN("A vector with some items") { |
| 45 | std::vector<int> v(5); |
| 46 | |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 47 | REQUIRE(v.size() == 5); |
| 48 | REQUIRE(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 49 | |
| 50 | WHEN("the size is increased") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 51 | v.resize(10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 52 | |
| 53 | THEN("the size and capacity change") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 54 | CHECK(v.size() == 20); |
| 55 | CHECK(v.capacity() >= 10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | WHEN("the size is reduced") { |
| 59 | v.resize(0); |
| 60 | |
| 61 | THEN("the size changes but not capacity") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 62 | CHECK(v.size() == 0); |
| 63 | CHECK(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | WHEN("more capacity is reserved") { |
| 67 | v.reserve(10); |
| 68 | |
| 69 | THEN("the capacity changes but not the size") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 70 | CHECK(v.size() == 5); |
| 71 | CHECK(v.capacity() >= 10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | WHEN("less capacity is reserved") { |
| 75 | v.reserve(0); |
| 76 | |
| 77 | THEN("neither size nor capacity are changed") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 78 | CHECK(v.size() == 10); |
| 79 | CHECK(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 80 | } |
| 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 | |
| 90 | struct TheFixture |
| 91 | { |
| 92 | int data; |
| 93 | TheFixture() |
| 94 | : data(42) { |
| 95 | // setup here |
| 96 | } |
| 97 | |
| 98 | ~TheFixture() { |
| 99 | // teardown here |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | TEST_CASE_FIXTURE(TheFixture, "test with a fixture - 1") { |
| 104 | data /= 2; |
| 105 | CHECK(data == 21); |
| 106 | } |
| 107 | |
| 108 | TEST_CASE_FIXTURE(TheFixture, "test with a fixture - 2") { |
| 109 | data *= 2; |
| 110 | CHECK(data == 85); |
| 111 | } |