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