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