blob: f0fbe9b0eb11414258fdeaccdf892b2c058367b1 [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
onqtam5856bb92019-09-22 16:33:47 +030041 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
onqtam0e6236a2018-11-30 16:49:58 +020043
onqtamaf07cbb2017-04-19 19:40:43 +030044 CHECK_NOTHROW(throw_if(true, 0)); // fails
45 CHECK_NOTHROW(throw_if(false, 0));
Elias Kosunen3de57e32016-12-20 18:57:03 +020046}
47
48TEST_CASE("exceptions-related macros for std::exception") {
onqtam7cc0e962017-04-17 23:30:36 +030049 CHECK_THROWS(throw_if(false, 0));
onqtamfd9560c2017-07-14 15:19:21 +030050 CHECK_THROWS_AS(throw_if(false, std::runtime_error("whops!")), std::exception);
onqtam8cf90412018-11-30 17:05:01 +020051 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), const std::exception&);
onqtam7cc0e962017-04-17 23:30:36 +030052 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), int);
Elias Kosunen3de57e32016-12-20 18:57:03 +020053
onqtam0e6236a2018-11-30 16:49:58 +020054 CHECK_THROWS_WITH(throw_if(false, ""), "whops!");
55
onqtam7cc0e962017-04-17 23:30:36 +030056 REQUIRE_NOTHROW(throw_if(true, std::runtime_error("whops!")));
onqtam4a655632016-05-26 14:20:52 +030057}
onqtamaf07cbb2017-04-19 19:40:43 +030058
59// =================================================================================================
60// == TESTING (ALMOST) ALL ASSERTS THAT THEY ACT ACCORDINGLY - not interesting examples...
61// =================================================================================================
62
63TEST_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));
onqtamacbcd122019-03-02 21:24:18 +020067 WARN_THROWS_WITH(throw_if(true, ""), "whops!");
68 WARN_THROWS_WITH(throw_if(false, ""), "whops!");
onqtamaf07cbb2017-04-19 19:40:43 +030069 WARN_THROWS_AS(throw_if(false, 0), bool);
70 WARN_THROWS_AS(throw_if(true, 0), bool);
onqtam5856bb92019-09-22 16:33:47 +030071 WARN_THROWS_WITH_AS(throw_if(false, ""), "whops!", int);
72 WARN_THROWS_WITH_AS(throw_if(true, ""), "whops!", int);
onqtamaf07cbb2017-04-19 19:40:43 +030073 WARN_NOTHROW(throw_if(true, 0));
onqtamc4948432018-12-01 18:47:12 +020074
onqtamaf07cbb2017-04-19 19:40:43 +030075 WARN_EQ(1, 0);
76 WARN_UNARY(0);
77 WARN_UNARY_FALSE(1);
onqtamaf07cbb2017-04-19 19:40:43 +030078}
79
80TEST_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);
onqtam0e6236a2018-11-30 16:49:58 +020086 CHECK_THROWS_WITH(throw_if(true, 0), "unrecognized");
onqtam5856bb92019-09-22 16:33:47 +030087 CHECK_THROWS_WITH_AS(throw_if(true, 0), "unrecognized", int);
onqtamaf07cbb2017-04-19 19:40:43 +030088 CHECK_NOTHROW(throw_if(true, 0));
onqtamc4948432018-12-01 18:47:12 +020089
onqtamaf07cbb2017-04-19 19:40:43 +030090 CHECK_EQ(1, 0);
91 CHECK_UNARY(0);
92 CHECK_UNARY_FALSE(1);
onqtamaf07cbb2017-04-19 19:40:43 +030093
94 MESSAGE("reached!");
95}
96
97TEST_CASE("REQUIRE level of asserts fail and abort the test case - 1") {
98 REQUIRE(0);
99 MESSAGE("should not be reached!");
100}
101TEST_CASE("REQUIRE level of asserts fail and abort the test case - 2") {
102 REQUIRE_FALSE(1);
103 MESSAGE("should not be reached!");
104}
105TEST_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}
109TEST_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}
113TEST_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}
onqtam5856bb92019-09-22 16:33:47 +0300117TEST_CASE("REQUIRE level of asserts fail and abort the test case - 6") {
onqtam803cbd12022-01-07 12:35:25 +0200118 REQUIRE_THROWS_WITH(throw_if(false, ""), "whops!");
onqtamacbcd122019-03-02 21:24:18 +0200119 MESSAGE("should not be reached!");
120}
onqtam5856bb92019-09-22 16:33:47 +0300121TEST_CASE("REQUIRE level of asserts fail and abort the test case - 7") {
onqtam803cbd12022-01-07 12:35:25 +0200122 REQUIRE_THROWS_WITH(throw_if(true, ""), "whops!");
onqtamacbcd122019-03-02 21:24:18 +0200123 MESSAGE("should not be reached!");
124}
onqtamaf07cbb2017-04-19 19:40:43 +0300125TEST_CASE("REQUIRE level of asserts fail and abort the test case - 8") {
onqtam803cbd12022-01-07 12:35:25 +0200126 REQUIRE_THROWS_WITH_AS(throw_if(false, ""), "whops!", bool);
onqtamaf07cbb2017-04-19 19:40:43 +0300127 MESSAGE("should not be reached!");
128}
129TEST_CASE("REQUIRE level of asserts fail and abort the test case - 9") {
onqtam803cbd12022-01-07 12:35:25 +0200130 REQUIRE_THROWS_WITH_AS(throw_if(true, ""), "whops!", bool);
onqtam5856bb92019-09-22 16:33:47 +0300131 MESSAGE("should not be reached!");
132}
133TEST_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}
137TEST_CASE("REQUIRE level of asserts fail and abort the test case - 11") {
138 REQUIRE_EQ(1, 0);
139 MESSAGE("should not be reached!");
140}
141TEST_CASE("REQUIRE level of asserts fail and abort the test case - 12") {
142 REQUIRE_UNARY(0);
143 MESSAGE("should not be reached!");
144}
145TEST_CASE("REQUIRE level of asserts fail and abort the test case - 13") {
onqtamaf07cbb2017-04-19 19:40:43 +0300146 REQUIRE_UNARY_FALSE(1);
147 MESSAGE("should not be reached!");
148}
onqtamaf07cbb2017-04-19 19:40:43 +0300149
150TEST_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);
onqtamaf07cbb2017-04-19 19:40:43 +0300175}
176
177static 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);
onqtam0e6236a2018-11-30 16:49:58 +0200184 CHECK_THROWS_WITH(throw_if(true, false), "unknown exception");
onqtam5856bb92019-09-22 16:33:47 +0300185 CHECK_THROWS_WITH_AS(throw_if(true, false), "unknown exception", int);
onqtamaf07cbb2017-04-19 19:40:43 +0300186 CHECK_NOTHROW(throw_if(false, 0));
187
188 CHECK_EQ(a, b);
189 CHECK_UNARY(a == b);
190 CHECK_UNARY_FALSE(a != b);
onqtamaf07cbb2017-04-19 19:40:43 +0300191}
192
193TEST_CASE("some asserts used in a function called by a test case") {
194 someAssertsInFunction();
195}
Stefane84f16e2022-01-05 15:21:41 +0100196
Salvage860904d2022-01-12 00:52:38 +0100197inline DOCTEST_NOINLINE void comp(int a, int b) {
198 if (CHECK(a == b)) { MESSAGE(":D"); }
199 if (CHECK_FALSE(a != b)) { MESSAGE(":D"); }
200 if (CHECK_EQ(a, b)) { MESSAGE(":D"); }
201 if (CHECK_UNARY(a == b)) { MESSAGE(":D"); }
202 if (CHECK_UNARY_FALSE(a != b)) { MESSAGE(":D"); }
203}
204
Salvagee798deb2022-01-12 02:44:57 +0100205DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4702)
Salvage860904d2022-01-12 00:52:38 +0100206TEST_CASE("check return values") {
207 comp(0, 0);
208
Salvagee798deb2022-01-12 02:44:57 +0100209 if (CHECK_THROWS(throw_if(true, true))) { MESSAGE(":D"); }
210 if (CHECK_THROWS_AS(throw_if(true, 2), int)) { MESSAGE(":D"); }
211 if (CHECK_NOTHROW(throw_if(false, 2))) { MESSAGE(":D"); }
212 if (CHECK_THROWS_WITH(throw_if(true, 2), "2")) { MESSAGE(":D"); }
Salvage860904d2022-01-12 00:52:38 +0100213}
214
215TEST_CASE("check return values no print") {
216 comp(4, 2);
217
Salvagee798deb2022-01-12 02:44:57 +0100218 if (CHECK_THROWS(throw_if(false, false))) { MESSAGE(":D"); }
219 if (CHECK_THROWS_AS(throw_if(true, 2), doctest::Approx)) { MESSAGE(":D"); }
220 if (CHECK_NOTHROW(throw_if(true, 2))) { MESSAGE(":D"); }
221 if (CHECK_THROWS_WITH(throw_if(true, 2), "1")) { MESSAGE(":D"); }
Stefane84f16e2022-01-05 15:21:41 +0100222}
Salvagee798deb2022-01-12 02:44:57 +0100223DOCTEST_MSVC_SUPPRESS_WARNING_POP