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