onqtam | 15c5f0f | 2016-08-02 17:31:27 +0300 | [diff] [blame] | 1 | #include "doctest.h" |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 2 | |
onqtam | c223b69 | 2016-08-02 17:20:06 +0300 | [diff] [blame] | 3 | #include <ostream> |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 4 | |
onqtam | 30c5e4b | 2016-08-02 16:10:22 +0300 | [diff] [blame] | 5 | // intentionally here so there are subcases on the same lines in different files |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 6 | TEST_CASE("subcases") { |
| 7 | SUBCASE("1") { |
| 8 | SUBCASE("1.1") {} |
| 9 | SUBCASE("1.2") {} |
| 10 | } |
| 11 | SUBCASE("2") { CHECK(0); } |
| 12 | SUBCASE("3") {} |
| 13 | } |
| 14 | |
onqtam | 658c870 | 2017-03-14 14:08:22 +0200 | [diff] [blame] | 15 | TEST_SUITE_BEGIN("ts1"); |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 16 | |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 17 | using doctest::Approx; |
| 18 | |
| 19 | static int throws(bool in) { |
| 20 | if(in) |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame^] | 21 | #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 22 | throw false; |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame^] | 23 | #else // DOCTEST_CONFIG_NO_EXCEPTIONS |
| 24 | return 0; |
| 25 | #endif // DOCTEST_CONFIG_NO_EXCEPTIONS |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 26 | return 42; |
| 27 | } |
| 28 | |
onqtam | c223b69 | 2016-08-02 17:20:06 +0300 | [diff] [blame] | 29 | struct myType |
onqtam | cc6a6d6 | 2016-09-19 17:30:15 +0300 | [diff] [blame] | 30 | { |
| 31 | myType() {} |
| 32 | |
| 33 | private: |
| 34 | myType(const myType&); // non-copyable |
| 35 | myType& operator=(const myType&); // non-assignable |
| 36 | }; |
onqtam | c223b69 | 2016-08-02 17:20:06 +0300 | [diff] [blame] | 37 | |
| 38 | static std::ostream& operator<<(std::ostream& stream, const myType&) { |
| 39 | stream << "myType!"; |
| 40 | return stream; |
| 41 | } |
| 42 | |
| 43 | static bool operator==(const myType&, const myType&) { return false; } |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 44 | |
onqtam | cc6a6d6 | 2016-09-19 17:30:15 +0300 | [diff] [blame] | 45 | TEST_CASE("expressions should be evaluated only once") { |
| 46 | int a = 5; |
| 47 | REQUIRE(++a == 6); |
| 48 | REQUIRE_EQ(++a, 7); |
| 49 | } |
| 50 | |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 51 | TEST_CASE("assertions") { |
onqtam | 4e68e66 | 2016-08-03 11:37:51 +0300 | [diff] [blame] | 52 | CHECK(true); |
onqtam | 30c5e4b | 2016-08-02 16:10:22 +0300 | [diff] [blame] | 53 | CHECK(1 == 0); |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 54 | CHECK_FALSE(1); |
onqtam | f4ea19f | 2016-09-20 00:07:56 +0300 | [diff] [blame] | 55 | myType a; |
| 56 | myType b; |
| 57 | CHECK(a == b); |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 58 | CHECK(Approx(0.1) == 0.2); |
onqtam | 30c5e4b | 2016-08-02 16:10:22 +0300 | [diff] [blame] | 59 | |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 60 | CHECK_THROWS(throws(true)); |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 61 | CHECK_THROWS(throws(false)); |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 62 | CHECK_NOTHROW(throws(false)); |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 63 | CHECK_NOTHROW(throws(true)); |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 64 | CHECK_THROWS_AS(throws(true), bool); |
onqtam | c223b69 | 2016-08-02 17:20:06 +0300 | [diff] [blame] | 65 | REQUIRE_THROWS_AS(throws(false), bool); |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 66 | } |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 67 | |
onqtam | 3ef438f | 2016-09-18 22:31:08 +0300 | [diff] [blame] | 68 | TEST_CASE("assertions - all of them") { |
| 69 | WARN(true); |
| 70 | CHECK(true); |
| 71 | REQUIRE(true); |
| 72 | WARN_FALSE(false); |
| 73 | CHECK_FALSE(false); |
| 74 | REQUIRE_FALSE(false); |
| 75 | WARN_THROWS(throws(true)); |
| 76 | CHECK_THROWS(throws(true)); |
| 77 | REQUIRE_THROWS(throws(true)); |
| 78 | WARN_THROWS_AS(throws(true), bool); |
| 79 | CHECK_THROWS_AS(throws(true), bool); |
| 80 | REQUIRE_THROWS_AS(throws(true), bool); |
| 81 | WARN_NOTHROW(throws(false)); |
| 82 | CHECK_NOTHROW(throws(false)); |
| 83 | REQUIRE_NOTHROW(throws(false)); |
| 84 | WARN_EQ(1, 1); |
| 85 | CHECK_EQ(1, 1); |
| 86 | REQUIRE_EQ(1, 1); |
| 87 | WARN_NE(1, 0); |
| 88 | CHECK_NE(1, 0); |
| 89 | REQUIRE_NE(1, 0); |
| 90 | WARN_GT(1, 0); |
| 91 | CHECK_GT(1, 0); |
| 92 | REQUIRE_GT(1, 0); |
| 93 | WARN_LT(0, 1); |
| 94 | CHECK_LT(0, 1); |
| 95 | REQUIRE_LT(0, 1); |
| 96 | WARN_GE(1, 1); |
| 97 | CHECK_GE(1, 1); |
| 98 | REQUIRE_GE(1, 1); |
| 99 | WARN_LE(1, 1); |
| 100 | CHECK_LE(1, 1); |
| 101 | REQUIRE_LE(1, 1); |
| 102 | WARN_UNARY(1); |
| 103 | CHECK_UNARY(1); |
| 104 | REQUIRE_UNARY(1); |
| 105 | WARN_UNARY_FALSE(0); |
| 106 | CHECK_UNARY_FALSE(0); |
| 107 | REQUIRE_UNARY_FALSE(0); |
| 108 | FAST_WARN_EQ(1, 1); |
| 109 | FAST_CHECK_EQ(1, 1); |
| 110 | FAST_REQUIRE_EQ(1, 1); |
| 111 | FAST_WARN_NE(1, 0); |
| 112 | FAST_CHECK_NE(1, 0); |
| 113 | FAST_REQUIRE_NE(1, 0); |
| 114 | FAST_WARN_GT(1, 0); |
| 115 | FAST_CHECK_GT(1, 0); |
| 116 | FAST_REQUIRE_GT(1, 0); |
| 117 | FAST_WARN_LT(0, 1); |
| 118 | FAST_CHECK_LT(0, 1); |
| 119 | FAST_REQUIRE_LT(0, 1); |
| 120 | FAST_WARN_GE(1, 1); |
| 121 | FAST_CHECK_GE(1, 1); |
| 122 | FAST_REQUIRE_GE(1, 1); |
| 123 | FAST_WARN_LE(1, 1); |
| 124 | FAST_CHECK_LE(1, 1); |
| 125 | FAST_REQUIRE_LE(1, 1); |
| 126 | FAST_WARN_UNARY(1); |
| 127 | FAST_CHECK_UNARY(1); |
| 128 | FAST_REQUIRE_UNARY(1); |
| 129 | FAST_WARN_UNARY_FALSE(0); |
| 130 | FAST_CHECK_UNARY_FALSE(0); |
onqtam | 8d78e70 | 2016-09-18 22:38:44 +0300 | [diff] [blame] | 131 | FAST_REQUIRE_UNARY_FALSE(1); |
onqtam | 3ef438f | 2016-09-18 22:31:08 +0300 | [diff] [blame] | 132 | } |
| 133 | |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 134 | TEST_CASE("throws") { throws(true); } |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 135 | |
| 136 | TEST_SUITE_END(); |