blob: 42ad86bf277c0e2cc3bf3bc83a4c8cf036e9da8c [file] [log] [blame]
Stefand9c290a2022-01-15 20:54:14 +01001#define DOCTEST_CONFIG_ASSERTS_RETURN_VALUES
ncihnegnc5458f22019-01-28 05:08:18 -08002#include <doctest/doctest.h>
onqtam4a655632016-05-26 14:20:52 +03003
onqtam7cc0e962017-04-17 23:30:36 +03004#include "header.h"
5
onqtamabf39d22017-10-28 21:30:45 +03006DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam12d55982017-04-16 22:35:27 +03007#include <stdexcept>
Stefan8acee4f2022-01-11 19:35:14 +01008#include <cmath>
onqtamabf39d22017-10-28 21:30:45 +03009DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam12d55982017-04-16 22:35:27 +030010
onqtam4a655632016-05-26 14:20:52 +030011TEST_CASE("normal macros") {
12 int a = 5;
13 int b = 5;
14
onqtam7cc0e962017-04-17 23:30:36 +030015 CHECK(throw_if(true, std::runtime_error("whops!")) == 42);
onqtam4a655632016-05-26 14:20:52 +030016
17 CHECK_FALSE(!(a == b));
18
19 REQUIRE(a == b);
onqtamcc6a6d62016-09-19 17:30:15 +030020
21 CHECK_EQ(a, b);
22
onqtam7cc0e962017-04-17 23:30:36 +030023 CHECK(doctest::Approx(0.1000001) == 0.1000002);
24 CHECK(doctest::Approx(0.502) == 0.501);
onqtamaf07cbb2017-04-19 19:40:43 +030025}
onqtam4a655632016-05-26 14:20:52 +030026
onqtamaf07cbb2017-04-19 19:40:43 +030027TEST_CASE("expressions should be evaluated only once") {
28 int a = 5;
29 REQUIRE(++a == 6);
30 REQUIRE_EQ(++a, 7);
Elias Kosunen3de57e32016-12-20 18:57:03 +020031}
32
onqtam4a655632016-05-26 14:20:52 +030033TEST_CASE("exceptions-related macros") {
onqtamaf07cbb2017-04-19 19:40:43 +030034 CHECK_THROWS(throw_if(true, 0));
35 CHECK_THROWS(throw_if(false, 0)); // fails
onqtam7cc0e962017-04-17 23:30:36 +030036 CHECK_THROWS_AS(throw_if(true, 0), int);
onqtamaf07cbb2017-04-19 19:40:43 +030037 CHECK_THROWS_AS(throw_if(true, 0), char); // fails
38 CHECK_THROWS_AS(throw_if(false, 0), int); // fails
onqtam4a655632016-05-26 14:20:52 +030039
onqtam0e6236a2018-11-30 16:49:58 +020040 CHECK_THROWS_WITH(throw_if(true, "whops!"), "whops! no match!"); // fails
Menno Fraters8de4cf72022-03-05 23:14:35 +000041 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
onqtam5856bb92019-09-22 16:33:47 +030043 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 Fraters8de4cf72022-03-05 23:14:35 +000045 CHECK_THROWS_WITH_AS(throw_if(true, "whops! does it match?"), doctest::Contains("whops! no match!"), int); // fails
onqtam0e6236a2018-11-30 16:49:58 +020046
onqtamaf07cbb2017-04-19 19:40:43 +030047 CHECK_NOTHROW(throw_if(true, 0)); // fails
48 CHECK_NOTHROW(throw_if(false, 0));
Elias Kosunen3de57e32016-12-20 18:57:03 +020049}
50
51TEST_CASE("exceptions-related macros for std::exception") {
onqtam7cc0e962017-04-17 23:30:36 +030052 CHECK_THROWS(throw_if(false, 0));
onqtamfd9560c2017-07-14 15:19:21 +030053 CHECK_THROWS_AS(throw_if(false, std::runtime_error("whops!")), std::exception);
onqtam8cf90412018-11-30 17:05:01 +020054 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), const std::exception&);
onqtam7cc0e962017-04-17 23:30:36 +030055 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), int);
Elias Kosunen3de57e32016-12-20 18:57:03 +020056
onqtam0e6236a2018-11-30 16:49:58 +020057 CHECK_THROWS_WITH(throw_if(false, ""), "whops!");
58
onqtam7cc0e962017-04-17 23:30:36 +030059 REQUIRE_NOTHROW(throw_if(true, std::runtime_error("whops!")));
onqtam4a655632016-05-26 14:20:52 +030060}
onqtamaf07cbb2017-04-19 19:40:43 +030061
62// =================================================================================================
63// == TESTING (ALMOST) ALL ASSERTS THAT THEY ACT ACCORDINGLY - not interesting examples...
64// =================================================================================================
65
66TEST_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));
onqtamacbcd122019-03-02 21:24:18 +020070 WARN_THROWS_WITH(throw_if(true, ""), "whops!");
71 WARN_THROWS_WITH(throw_if(false, ""), "whops!");
onqtamaf07cbb2017-04-19 19:40:43 +030072 WARN_THROWS_AS(throw_if(false, 0), bool);
73 WARN_THROWS_AS(throw_if(true, 0), bool);
onqtam5856bb92019-09-22 16:33:47 +030074 WARN_THROWS_WITH_AS(throw_if(false, ""), "whops!", int);
75 WARN_THROWS_WITH_AS(throw_if(true, ""), "whops!", int);
onqtamaf07cbb2017-04-19 19:40:43 +030076 WARN_NOTHROW(throw_if(true, 0));
onqtamc4948432018-12-01 18:47:12 +020077
onqtamaf07cbb2017-04-19 19:40:43 +030078 WARN_EQ(1, 0);
Menno Fraters8de4cf72022-03-05 23:14:35 +000079 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"));
onqtamaf07cbb2017-04-19 19:40:43 +030083 WARN_UNARY(0);
84 WARN_UNARY_FALSE(1);
onqtamaf07cbb2017-04-19 19:40:43 +030085}
86
87TEST_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);
onqtam0e6236a2018-11-30 16:49:58 +020093 CHECK_THROWS_WITH(throw_if(true, 0), "unrecognized");
onqtam5856bb92019-09-22 16:33:47 +030094 CHECK_THROWS_WITH_AS(throw_if(true, 0), "unrecognized", int);
onqtamaf07cbb2017-04-19 19:40:43 +030095 CHECK_NOTHROW(throw_if(true, 0));
onqtamc4948432018-12-01 18:47:12 +020096
onqtamaf07cbb2017-04-19 19:40:43 +030097 CHECK_EQ(1, 0);
Menno Fraters8de4cf72022-03-05 23:14:35 +000098 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"));
onqtamaf07cbb2017-04-19 19:40:43 +0300102 CHECK_UNARY(0);
103 CHECK_UNARY_FALSE(1);
onqtamaf07cbb2017-04-19 19:40:43 +0300104
105 MESSAGE("reached!");
106}
107
108TEST_CASE("REQUIRE level of asserts fail and abort the test case - 1") {
109 REQUIRE(0);
110 MESSAGE("should not be reached!");
111}
112TEST_CASE("REQUIRE level of asserts fail and abort the test case - 2") {
113 REQUIRE_FALSE(1);
114 MESSAGE("should not be reached!");
115}
116TEST_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}
120TEST_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}
124TEST_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}
onqtam5856bb92019-09-22 16:33:47 +0300128TEST_CASE("REQUIRE level of asserts fail and abort the test case - 6") {
onqtam803cbd12022-01-07 12:35:25 +0200129 REQUIRE_THROWS_WITH(throw_if(false, ""), "whops!");
onqtamacbcd122019-03-02 21:24:18 +0200130 MESSAGE("should not be reached!");
131}
onqtam5856bb92019-09-22 16:33:47 +0300132TEST_CASE("REQUIRE level of asserts fail and abort the test case - 7") {
onqtam803cbd12022-01-07 12:35:25 +0200133 REQUIRE_THROWS_WITH(throw_if(true, ""), "whops!");
onqtamacbcd122019-03-02 21:24:18 +0200134 MESSAGE("should not be reached!");
135}
onqtamaf07cbb2017-04-19 19:40:43 +0300136TEST_CASE("REQUIRE level of asserts fail and abort the test case - 8") {
onqtam803cbd12022-01-07 12:35:25 +0200137 REQUIRE_THROWS_WITH_AS(throw_if(false, ""), "whops!", bool);
onqtamaf07cbb2017-04-19 19:40:43 +0300138 MESSAGE("should not be reached!");
139}
140TEST_CASE("REQUIRE level of asserts fail and abort the test case - 9") {
onqtam803cbd12022-01-07 12:35:25 +0200141 REQUIRE_THROWS_WITH_AS(throw_if(true, ""), "whops!", bool);
onqtam5856bb92019-09-22 16:33:47 +0300142 MESSAGE("should not be reached!");
143}
144TEST_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}
148TEST_CASE("REQUIRE level of asserts fail and abort the test case - 11") {
149 REQUIRE_EQ(1, 0);
150 MESSAGE("should not be reached!");
151}
152TEST_CASE("REQUIRE level of asserts fail and abort the test case - 12") {
153 REQUIRE_UNARY(0);
154 MESSAGE("should not be reached!");
155}
156TEST_CASE("REQUIRE level of asserts fail and abort the test case - 13") {
onqtamaf07cbb2017-04-19 19:40:43 +0300157 REQUIRE_UNARY_FALSE(1);
158 MESSAGE("should not be reached!");
159}
onqtamaf07cbb2017-04-19 19:40:43 +0300160
161TEST_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);
onqtamaf07cbb2017-04-19 19:40:43 +0300186}
187
188static 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);
onqtam0e6236a2018-11-30 16:49:58 +0200195 CHECK_THROWS_WITH(throw_if(true, false), "unknown exception");
onqtam5856bb92019-09-22 16:33:47 +0300196 CHECK_THROWS_WITH_AS(throw_if(true, false), "unknown exception", int);
onqtamaf07cbb2017-04-19 19:40:43 +0300197 CHECK_NOTHROW(throw_if(false, 0));
198
199 CHECK_EQ(a, b);
200 CHECK_UNARY(a == b);
201 CHECK_UNARY_FALSE(a != b);
onqtamaf07cbb2017-04-19 19:40:43 +0300202}
203
204TEST_CASE("some asserts used in a function called by a test case") {
205 someAssertsInFunction();
206}
Stefane84f16e2022-01-05 15:21:41 +0100207
Stefandb758e02022-06-06 03:42:02 +0200208// TODO: Remove NOLINT (if (false && (__VA_ARGS__));)?
Ilya Isaev28d9d382022-12-17 01:32:33 +0100209DOCTEST_INLINE_NOINLINE void comp(int a, int b) { // NOLINT(misc-unused-parameters)
Salvage860904d2022-01-12 00:52:38 +0100210 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
Salvagee798deb2022-01-12 02:44:57 +0100217DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4702)
Salvage860904d2022-01-12 00:52:38 +0100218TEST_CASE("check return values") {
219 comp(0, 0);
220
Salvagee798deb2022-01-12 02:44:57 +0100221 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"); }
Salvage860904d2022-01-12 00:52:38 +0100225}
226
227TEST_CASE("check return values no print") {
228 comp(4, 2);
229
Salvagee798deb2022-01-12 02:44:57 +0100230 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"); }
Stefane84f16e2022-01-05 15:21:41 +0100234}
Salvagee798deb2022-01-12 02:44:57 +0100235DOCTEST_MSVC_SUPPRESS_WARNING_POP