onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 1 | #include "doctest.h" |
| 2 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 3 | #include "header.h" |
| 4 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 5 | #include <stdexcept> |
| 6 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 7 | TEST_CASE("normal macros") { |
| 8 | int a = 5; |
| 9 | int b = 5; |
| 10 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 11 | CHECK(throw_if(true, std::runtime_error("whops!")) == 42); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 12 | |
| 13 | CHECK_FALSE(!(a == b)); |
| 14 | |
| 15 | REQUIRE(a == b); |
onqtam | cc6a6d6 | 2016-09-19 17:30:15 +0300 | [diff] [blame] | 16 | |
| 17 | CHECK_EQ(a, b); |
| 18 | |
| 19 | FAST_CHECK_EQ(a, b); |
| 20 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 21 | CHECK(doctest::Approx(0.1000001) == 0.1000002); |
| 22 | CHECK(doctest::Approx(0.502) == 0.501); |
onqtam | af07cbb | 2017-04-19 19:40:43 +0300 | [diff] [blame^] | 23 | } |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 24 | |
onqtam | af07cbb | 2017-04-19 19:40:43 +0300 | [diff] [blame^] | 25 | TEST_CASE("expressions should be evaluated only once") { |
| 26 | int a = 5; |
| 27 | REQUIRE(++a == 6); |
| 28 | REQUIRE_EQ(++a, 7); |
Elias Kosunen | 3de57e3 | 2016-12-20 18:57:03 +0200 | [diff] [blame] | 29 | } |
| 30 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 31 | TEST_CASE("exceptions-related macros") { |
onqtam | af07cbb | 2017-04-19 19:40:43 +0300 | [diff] [blame^] | 32 | CHECK_THROWS(throw_if(true, 0)); |
| 33 | CHECK_THROWS(throw_if(false, 0)); // fails |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 34 | CHECK_THROWS_AS(throw_if(true, 0), int); |
onqtam | af07cbb | 2017-04-19 19:40:43 +0300 | [diff] [blame^] | 35 | CHECK_THROWS_AS(throw_if(true, 0), char); // fails |
| 36 | CHECK_THROWS_AS(throw_if(false, 0), int); // fails |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 37 | |
onqtam | af07cbb | 2017-04-19 19:40:43 +0300 | [diff] [blame^] | 38 | CHECK_NOTHROW(throw_if(true, 0)); // fails |
| 39 | CHECK_NOTHROW(throw_if(false, 0)); |
Elias Kosunen | 3de57e3 | 2016-12-20 18:57:03 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | TEST_CASE("exceptions-related macros for std::exception") { |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 43 | CHECK_THROWS(throw_if(false, 0)); |
| 44 | CHECK_THROWS_AS(throw_if(false, std::runtime_error("whops!")), std::exception&); |
| 45 | CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), std::exception&); |
| 46 | CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), int); |
Elias Kosunen | 3de57e3 | 2016-12-20 18:57:03 +0200 | [diff] [blame] | 47 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 48 | REQUIRE_NOTHROW(throw_if(true, std::runtime_error("whops!"))); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 49 | } |
onqtam | af07cbb | 2017-04-19 19:40:43 +0300 | [diff] [blame^] | 50 | |
| 51 | // ================================================================================================= |
| 52 | // == TESTING (ALMOST) ALL ASSERTS THAT THEY ACT ACCORDINGLY - not interesting examples... |
| 53 | // ================================================================================================= |
| 54 | |
| 55 | TEST_CASE("WARN level of asserts don't fail the test case") { |
| 56 | WARN(0); |
| 57 | WARN_FALSE(1); |
| 58 | WARN_THROWS(throw_if(false, 0)); |
| 59 | WARN_THROWS_AS(throw_if(false, 0), bool); |
| 60 | WARN_THROWS_AS(throw_if(true, 0), bool); |
| 61 | WARN_NOTHROW(throw_if(true, 0)); |
| 62 | |
| 63 | WARN_EQ(1, 0); |
| 64 | WARN_UNARY(0); |
| 65 | WARN_UNARY_FALSE(1); |
| 66 | |
| 67 | FAST_WARN_EQ(1, 0); |
| 68 | FAST_WARN_UNARY(0); |
| 69 | FAST_WARN_UNARY_FALSE(1); |
| 70 | } |
| 71 | |
| 72 | TEST_CASE("CHECK level of asserts fail the test case but don't abort it") { |
| 73 | CHECK(0); |
| 74 | CHECK_FALSE(1); |
| 75 | CHECK_THROWS(throw_if(false, 0)); |
| 76 | CHECK_THROWS_AS(throw_if(false, 0), bool); |
| 77 | CHECK_THROWS_AS(throw_if(true, 0), bool); |
| 78 | CHECK_NOTHROW(throw_if(true, 0)); |
| 79 | |
| 80 | CHECK_EQ(1, 0); |
| 81 | CHECK_UNARY(0); |
| 82 | CHECK_UNARY_FALSE(1); |
| 83 | |
| 84 | FAST_CHECK_EQ(1, 0); |
| 85 | FAST_CHECK_UNARY(0); |
| 86 | FAST_CHECK_UNARY_FALSE(1); |
| 87 | |
| 88 | MESSAGE("reached!"); |
| 89 | } |
| 90 | |
| 91 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 1") { |
| 92 | REQUIRE(0); |
| 93 | MESSAGE("should not be reached!"); |
| 94 | } |
| 95 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 2") { |
| 96 | REQUIRE_FALSE(1); |
| 97 | MESSAGE("should not be reached!"); |
| 98 | } |
| 99 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 3") { |
| 100 | REQUIRE_THROWS(throw_if(false, 0)); |
| 101 | MESSAGE("should not be reached!"); |
| 102 | } |
| 103 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 4") { |
| 104 | REQUIRE_THROWS_AS(throw_if(false, 0), bool); |
| 105 | MESSAGE("should not be reached!"); |
| 106 | } |
| 107 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 5") { |
| 108 | REQUIRE_THROWS_AS(throw_if(true, 0), bool); |
| 109 | MESSAGE("should not be reached!"); |
| 110 | } |
| 111 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 6") { |
| 112 | REQUIRE_NOTHROW(throw_if(true, 0)); |
| 113 | MESSAGE("should not be reached!"); |
| 114 | } |
| 115 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 7") { |
| 116 | REQUIRE_EQ(1, 0); |
| 117 | MESSAGE("should not be reached!"); |
| 118 | } |
| 119 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 8") { |
| 120 | REQUIRE_UNARY(0); |
| 121 | MESSAGE("should not be reached!"); |
| 122 | } |
| 123 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 9") { |
| 124 | REQUIRE_UNARY_FALSE(1); |
| 125 | MESSAGE("should not be reached!"); |
| 126 | } |
| 127 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 10") { |
| 128 | FAST_REQUIRE_EQ(1, 0); |
| 129 | MESSAGE("should not be reached!"); |
| 130 | } |
| 131 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 11") { |
| 132 | FAST_REQUIRE_UNARY(0); |
| 133 | MESSAGE("should not be reached!"); |
| 134 | } |
| 135 | TEST_CASE("REQUIRE level of asserts fail and abort the test case - 12") { |
| 136 | FAST_REQUIRE_UNARY_FALSE(1); |
| 137 | MESSAGE("should not be reached!"); |
| 138 | } |
| 139 | |
| 140 | TEST_CASE("all binary assertions") { |
| 141 | WARN_EQ(1, 1); |
| 142 | CHECK_EQ(1, 1); |
| 143 | REQUIRE_EQ(1, 1); |
| 144 | WARN_NE(1, 0); |
| 145 | CHECK_NE(1, 0); |
| 146 | REQUIRE_NE(1, 0); |
| 147 | WARN_GT(1, 0); |
| 148 | CHECK_GT(1, 0); |
| 149 | REQUIRE_GT(1, 0); |
| 150 | WARN_LT(0, 1); |
| 151 | CHECK_LT(0, 1); |
| 152 | REQUIRE_LT(0, 1); |
| 153 | WARN_GE(1, 1); |
| 154 | CHECK_GE(1, 1); |
| 155 | REQUIRE_GE(1, 1); |
| 156 | WARN_LE(1, 1); |
| 157 | CHECK_LE(1, 1); |
| 158 | REQUIRE_LE(1, 1); |
| 159 | WARN_UNARY(1); |
| 160 | CHECK_UNARY(1); |
| 161 | REQUIRE_UNARY(1); |
| 162 | WARN_UNARY_FALSE(0); |
| 163 | CHECK_UNARY_FALSE(0); |
| 164 | REQUIRE_UNARY_FALSE(0); |
| 165 | FAST_WARN_EQ(1, 1); |
| 166 | FAST_CHECK_EQ(1, 1); |
| 167 | FAST_REQUIRE_EQ(1, 1); |
| 168 | FAST_WARN_NE(1, 0); |
| 169 | FAST_CHECK_NE(1, 0); |
| 170 | FAST_REQUIRE_NE(1, 0); |
| 171 | FAST_WARN_GT(1, 0); |
| 172 | FAST_CHECK_GT(1, 0); |
| 173 | FAST_REQUIRE_GT(1, 0); |
| 174 | FAST_WARN_LT(0, 1); |
| 175 | FAST_CHECK_LT(0, 1); |
| 176 | FAST_REQUIRE_LT(0, 1); |
| 177 | FAST_WARN_GE(1, 1); |
| 178 | FAST_CHECK_GE(1, 1); |
| 179 | FAST_REQUIRE_GE(1, 1); |
| 180 | FAST_WARN_LE(1, 1); |
| 181 | FAST_CHECK_LE(1, 1); |
| 182 | FAST_REQUIRE_LE(1, 1); |
| 183 | FAST_WARN_UNARY(1); |
| 184 | FAST_CHECK_UNARY(1); |
| 185 | FAST_REQUIRE_UNARY(1); |
| 186 | FAST_WARN_UNARY_FALSE(0); |
| 187 | FAST_CHECK_UNARY_FALSE(0); |
| 188 | FAST_REQUIRE_UNARY_FALSE(0); |
| 189 | } |
| 190 | |
| 191 | static void someAssertsInFunction() { |
| 192 | int a = 5; |
| 193 | int b = 5; |
| 194 | CHECK(a == b); |
| 195 | CHECK_FALSE(a != b); |
| 196 | CHECK_THROWS(throw_if(true, 0)); |
| 197 | CHECK_THROWS_AS(throw_if(true, 0), int); |
| 198 | CHECK_NOTHROW(throw_if(false, 0)); |
| 199 | |
| 200 | CHECK_EQ(a, b); |
| 201 | CHECK_UNARY(a == b); |
| 202 | CHECK_UNARY_FALSE(a != b); |
| 203 | FAST_CHECK_EQ(a, b); |
| 204 | FAST_CHECK_UNARY(a == b); |
| 205 | FAST_CHECK_UNARY_FALSE(a != b); |
| 206 | } |
| 207 | |
| 208 | TEST_CASE("some asserts used in a function called by a test case") { |
| 209 | someAssertsInFunction(); |
| 210 | } |