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