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