ncihnegn | c5458f2 | 2019-01-28 05:08:18 -0800 | [diff] [blame] | 1 | #include <doctest/doctest.h> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 2 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 3 | #include "header.h" |
| 4 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 5 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 6 | #include <iostream> |
| 7 | #include <vector> |
| 8 | using namespace std; |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 9 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 10 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 11 | TEST_CASE("lots of nested subcases") { |
| 12 | cout << endl << "root" << endl; |
| 13 | SUBCASE("") { |
| 14 | cout << "1" << endl; |
| 15 | SUBCASE("") { cout << "1.1" << endl; } |
| 16 | } |
| 17 | SUBCASE("") { |
| 18 | cout << "2" << endl; |
| 19 | SUBCASE("") { cout << "2.1" << endl; } |
| 20 | SUBCASE("") { |
| 21 | // whops! all the subcases below shouldn't be discovered and executed! |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 22 | FAIL(""); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 23 | |
| 24 | cout << "2.2" << endl; |
| 25 | SUBCASE("") { |
| 26 | cout << "2.2.1" << endl; |
| 27 | SUBCASE("") { cout << "2.2.1.1" << endl; } |
| 28 | SUBCASE("") { cout << "2.2.1.2" << endl; } |
| 29 | } |
| 30 | } |
| 31 | SUBCASE("") { cout << "2.3" << endl; } |
| 32 | SUBCASE("") { cout << "2.4" << endl; } |
| 33 | } |
| 34 | } |
| 35 | |
onqtam | 378d670 | 2017-04-19 11:30:03 +0300 | [diff] [blame] | 36 | static void call_func() { |
onqtam | 5dbcb1e | 2017-05-02 23:07:56 +0300 | [diff] [blame] | 37 | SUBCASE("from function...") { |
| 38 | MESSAGE("print me twice"); |
| 39 | SUBCASE("sc1") { |
| 40 | MESSAGE("hello! from sc1"); |
| 41 | } |
| 42 | SUBCASE("sc2") { |
| 43 | MESSAGE("hello! from sc2"); |
| 44 | } |
onqtam | 378d670 | 2017-04-19 11:30:03 +0300 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | TEST_CASE("subcases can be used in a separate function as well") { |
| 49 | call_func(); |
onqtam | a82c1e4 | 2017-05-07 17:36:41 +0300 | [diff] [blame] | 50 | MESSAGE("lala"); |
onqtam | 378d670 | 2017-04-19 11:30:03 +0300 | [diff] [blame] | 51 | } |
| 52 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 53 | SCENARIO("vectors can be sized and resized") { |
| 54 | GIVEN("A vector with some items") { |
| 55 | std::vector<int> v(5); |
| 56 | |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 57 | REQUIRE(v.size() == 5); |
| 58 | REQUIRE(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 59 | |
| 60 | WHEN("the size is increased") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 61 | v.resize(10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 62 | |
| 63 | THEN("the size and capacity change") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 64 | CHECK(v.size() == 20); |
| 65 | CHECK(v.capacity() >= 10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | WHEN("the size is reduced") { |
| 69 | v.resize(0); |
| 70 | |
| 71 | THEN("the size changes but not capacity") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 72 | CHECK(v.size() == 0); |
| 73 | CHECK(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | WHEN("more capacity is reserved") { |
| 77 | v.reserve(10); |
| 78 | |
| 79 | THEN("the capacity changes but not the size") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 80 | CHECK(v.size() == 5); |
| 81 | CHECK(v.capacity() >= 10); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | WHEN("less capacity is reserved") { |
| 85 | v.reserve(0); |
| 86 | |
| 87 | THEN("neither size nor capacity are changed") { |
onqtam | f90739e | 2016-09-14 01:01:45 +0300 | [diff] [blame] | 88 | CHECK(v.size() == 10); |
| 89 | CHECK(v.capacity() >= 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
onqtam | dc01198 | 2018-10-24 16:57:10 +0300 | [diff] [blame] | 94 | |
| 95 | TEST_CASE("test case should fail even though the last subcase passes") { |
| 96 | SUBCASE("one") { |
| 97 | CHECK(false); |
| 98 | } |
| 99 | SUBCASE("two") { |
| 100 | CHECK(true); |
| 101 | } |
| 102 | } |
onqtam | b656d60 | 2019-03-18 18:33:00 +0200 | [diff] [blame] | 103 | |
| 104 | TEST_CASE("fails from an exception but gets re-entered to traverse all subcases") { |
| 105 | SUBCASE("level zero") { |
| 106 | SUBCASE("one") { |
| 107 | CHECK(false); |
| 108 | } |
| 109 | SUBCASE("two") { |
| 110 | CHECK(false); |
| 111 | } |
| 112 | |
| 113 | throw_if(true, "failure... but the show must go on!"); |
| 114 | } |
| 115 | } |