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