blob: 524751ca967c79157ff34c43bf0cd5b51407e568 [file] [log] [blame]
ncihnegnc5458f22019-01-28 05:08:18 -08001#include <doctest/doctest.h>
onqtam4a655632016-05-26 14:20:52 +03002
onqtam7cc0e962017-04-17 23:30:36 +03003#include "header.h"
4
onqtamabf39d22017-10-28 21:30:45 +03005DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam12d55982017-04-16 22:35:27 +03006#include <stdexcept>
Stefan8acee4f2022-01-11 19:35:14 +01007#include <cmath>
onqtamabf39d22017-10-28 21:30:45 +03008DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam12d55982017-04-16 22:35:27 +03009
onqtam4a655632016-05-26 14:20:52 +030010TEST_CASE("normal macros") {
11 int a = 5;
12 int b = 5;
13
onqtam7cc0e962017-04-17 23:30:36 +030014 CHECK(throw_if(true, std::runtime_error("whops!")) == 42);
onqtam4a655632016-05-26 14:20:52 +030015
16 CHECK_FALSE(!(a == b));
17
18 REQUIRE(a == b);
onqtamcc6a6d62016-09-19 17:30:15 +030019
20 CHECK_EQ(a, b);
21
onqtam7cc0e962017-04-17 23:30:36 +030022 CHECK(doctest::Approx(0.1000001) == 0.1000002);
23 CHECK(doctest::Approx(0.502) == 0.501);
onqtamaf07cbb2017-04-19 19:40:43 +030024}
onqtam4a655632016-05-26 14:20:52 +030025
onqtamaf07cbb2017-04-19 19:40:43 +030026TEST_CASE("expressions should be evaluated only once") {
27 int a = 5;
28 REQUIRE(++a == 6);
29 REQUIRE_EQ(++a, 7);
Elias Kosunen3de57e32016-12-20 18:57:03 +020030}
31
onqtam4a655632016-05-26 14:20:52 +030032TEST_CASE("exceptions-related macros") {
onqtamaf07cbb2017-04-19 19:40:43 +030033 CHECK_THROWS(throw_if(true, 0));
34 CHECK_THROWS(throw_if(false, 0)); // fails
onqtam7cc0e962017-04-17 23:30:36 +030035 CHECK_THROWS_AS(throw_if(true, 0), int);
onqtamaf07cbb2017-04-19 19:40:43 +030036 CHECK_THROWS_AS(throw_if(true, 0), char); // fails
37 CHECK_THROWS_AS(throw_if(false, 0), int); // fails
onqtam4a655632016-05-26 14:20:52 +030038
onqtam0e6236a2018-11-30 16:49:58 +020039 CHECK_THROWS_WITH(throw_if(true, "whops!"), "whops! no match!"); // fails
onqtam5856bb92019-09-22 16:33:47 +030040 CHECK_THROWS_WITH_AS(throw_if(true, "whops!"), "whops! no match!", bool); // fails
41 CHECK_THROWS_WITH_AS(throw_if(true, "whops!"), "whops!", int); // fails
onqtam0e6236a2018-11-30 16:49:58 +020042
onqtamaf07cbb2017-04-19 19:40:43 +030043 CHECK_NOTHROW(throw_if(true, 0)); // fails
44 CHECK_NOTHROW(throw_if(false, 0));
Elias Kosunen3de57e32016-12-20 18:57:03 +020045}
46
47TEST_CASE("exceptions-related macros for std::exception") {
onqtam7cc0e962017-04-17 23:30:36 +030048 CHECK_THROWS(throw_if(false, 0));
onqtamfd9560c2017-07-14 15:19:21 +030049 CHECK_THROWS_AS(throw_if(false, std::runtime_error("whops!")), std::exception);
onqtam8cf90412018-11-30 17:05:01 +020050 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), const std::exception&);
onqtam7cc0e962017-04-17 23:30:36 +030051 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), int);
Elias Kosunen3de57e32016-12-20 18:57:03 +020052
onqtam0e6236a2018-11-30 16:49:58 +020053 CHECK_THROWS_WITH(throw_if(false, ""), "whops!");
54
onqtam7cc0e962017-04-17 23:30:36 +030055 REQUIRE_NOTHROW(throw_if(true, std::runtime_error("whops!")));
onqtam4a655632016-05-26 14:20:52 +030056}
onqtamaf07cbb2017-04-19 19:40:43 +030057
58// =================================================================================================
59// == TESTING (ALMOST) ALL ASSERTS THAT THEY ACT ACCORDINGLY - not interesting examples...
60// =================================================================================================
61
62TEST_CASE("WARN level of asserts don't fail the test case") {
63 WARN(0);
64 WARN_FALSE(1);
65 WARN_THROWS(throw_if(false, 0));
onqtamacbcd122019-03-02 21:24:18 +020066 WARN_THROWS_WITH(throw_if(true, ""), "whops!");
67 WARN_THROWS_WITH(throw_if(false, ""), "whops!");
onqtamaf07cbb2017-04-19 19:40:43 +030068 WARN_THROWS_AS(throw_if(false, 0), bool);
69 WARN_THROWS_AS(throw_if(true, 0), bool);
onqtam5856bb92019-09-22 16:33:47 +030070 WARN_THROWS_WITH_AS(throw_if(false, ""), "whops!", int);
71 WARN_THROWS_WITH_AS(throw_if(true, ""), "whops!", int);
onqtamaf07cbb2017-04-19 19:40:43 +030072 WARN_NOTHROW(throw_if(true, 0));
onqtamc4948432018-12-01 18:47:12 +020073
onqtamaf07cbb2017-04-19 19:40:43 +030074 WARN_EQ(1, 0);
75 WARN_UNARY(0);
76 WARN_UNARY_FALSE(1);
onqtamaf07cbb2017-04-19 19:40:43 +030077}
78
79TEST_CASE("CHECK level of asserts fail the test case but don't abort it") {
80 CHECK(0);
81 CHECK_FALSE(1);
82 CHECK_THROWS(throw_if(false, 0));
83 CHECK_THROWS_AS(throw_if(false, 0), bool);
84 CHECK_THROWS_AS(throw_if(true, 0), bool);
onqtam0e6236a2018-11-30 16:49:58 +020085 CHECK_THROWS_WITH(throw_if(true, 0), "unrecognized");
onqtam5856bb92019-09-22 16:33:47 +030086 CHECK_THROWS_WITH_AS(throw_if(true, 0), "unrecognized", int);
onqtamaf07cbb2017-04-19 19:40:43 +030087 CHECK_NOTHROW(throw_if(true, 0));
onqtamc4948432018-12-01 18:47:12 +020088
onqtamaf07cbb2017-04-19 19:40:43 +030089 CHECK_EQ(1, 0);
90 CHECK_UNARY(0);
91 CHECK_UNARY_FALSE(1);
onqtamaf07cbb2017-04-19 19:40:43 +030092
93 MESSAGE("reached!");
94}
95
96TEST_CASE("REQUIRE level of asserts fail and abort the test case - 1") {
97 REQUIRE(0);
98 MESSAGE("should not be reached!");
99}
100TEST_CASE("REQUIRE level of asserts fail and abort the test case - 2") {
101 REQUIRE_FALSE(1);
102 MESSAGE("should not be reached!");
103}
104TEST_CASE("REQUIRE level of asserts fail and abort the test case - 3") {
105 REQUIRE_THROWS(throw_if(false, 0));
106 MESSAGE("should not be reached!");
107}
108TEST_CASE("REQUIRE level of asserts fail and abort the test case - 4") {
109 REQUIRE_THROWS_AS(throw_if(false, 0), bool);
110 MESSAGE("should not be reached!");
111}
112TEST_CASE("REQUIRE level of asserts fail and abort the test case - 5") {
113 REQUIRE_THROWS_AS(throw_if(true, 0), bool);
114 MESSAGE("should not be reached!");
115}
onqtam5856bb92019-09-22 16:33:47 +0300116TEST_CASE("REQUIRE level of asserts fail and abort the test case - 6") {
onqtam803cbd12022-01-07 12:35:25 +0200117 REQUIRE_THROWS_WITH(throw_if(false, ""), "whops!");
onqtamacbcd122019-03-02 21:24:18 +0200118 MESSAGE("should not be reached!");
119}
onqtam5856bb92019-09-22 16:33:47 +0300120TEST_CASE("REQUIRE level of asserts fail and abort the test case - 7") {
onqtam803cbd12022-01-07 12:35:25 +0200121 REQUIRE_THROWS_WITH(throw_if(true, ""), "whops!");
onqtamacbcd122019-03-02 21:24:18 +0200122 MESSAGE("should not be reached!");
123}
onqtamaf07cbb2017-04-19 19:40:43 +0300124TEST_CASE("REQUIRE level of asserts fail and abort the test case - 8") {
onqtam803cbd12022-01-07 12:35:25 +0200125 REQUIRE_THROWS_WITH_AS(throw_if(false, ""), "whops!", bool);
onqtamaf07cbb2017-04-19 19:40:43 +0300126 MESSAGE("should not be reached!");
127}
128TEST_CASE("REQUIRE level of asserts fail and abort the test case - 9") {
onqtam803cbd12022-01-07 12:35:25 +0200129 REQUIRE_THROWS_WITH_AS(throw_if(true, ""), "whops!", bool);
onqtam5856bb92019-09-22 16:33:47 +0300130 MESSAGE("should not be reached!");
131}
132TEST_CASE("REQUIRE level of asserts fail and abort the test case - 10") {
133 REQUIRE_NOTHROW(throw_if(true, 0));
134 MESSAGE("should not be reached!");
135}
136TEST_CASE("REQUIRE level of asserts fail and abort the test case - 11") {
137 REQUIRE_EQ(1, 0);
138 MESSAGE("should not be reached!");
139}
140TEST_CASE("REQUIRE level of asserts fail and abort the test case - 12") {
141 REQUIRE_UNARY(0);
142 MESSAGE("should not be reached!");
143}
144TEST_CASE("REQUIRE level of asserts fail and abort the test case - 13") {
onqtamaf07cbb2017-04-19 19:40:43 +0300145 REQUIRE_UNARY_FALSE(1);
146 MESSAGE("should not be reached!");
147}
onqtamaf07cbb2017-04-19 19:40:43 +0300148
149TEST_CASE("all binary assertions") {
150 WARN_EQ(1, 1);
151 CHECK_EQ(1, 1);
152 REQUIRE_EQ(1, 1);
153 WARN_NE(1, 0);
154 CHECK_NE(1, 0);
155 REQUIRE_NE(1, 0);
156 WARN_GT(1, 0);
157 CHECK_GT(1, 0);
158 REQUIRE_GT(1, 0);
159 WARN_LT(0, 1);
160 CHECK_LT(0, 1);
161 REQUIRE_LT(0, 1);
162 WARN_GE(1, 1);
163 CHECK_GE(1, 1);
164 REQUIRE_GE(1, 1);
165 WARN_LE(1, 1);
166 CHECK_LE(1, 1);
167 REQUIRE_LE(1, 1);
168 WARN_UNARY(1);
169 CHECK_UNARY(1);
170 REQUIRE_UNARY(1);
171 WARN_UNARY_FALSE(0);
172 CHECK_UNARY_FALSE(0);
173 REQUIRE_UNARY_FALSE(0);
onqtamaf07cbb2017-04-19 19:40:43 +0300174}
175
176static void someAssertsInFunction() {
177 int a = 5;
178 int b = 5;
179 CHECK(a == b);
180 CHECK_FALSE(a != b);
181 CHECK_THROWS(throw_if(true, 0));
182 CHECK_THROWS_AS(throw_if(true, 0), int);
onqtam0e6236a2018-11-30 16:49:58 +0200183 CHECK_THROWS_WITH(throw_if(true, false), "unknown exception");
onqtam5856bb92019-09-22 16:33:47 +0300184 CHECK_THROWS_WITH_AS(throw_if(true, false), "unknown exception", int);
onqtamaf07cbb2017-04-19 19:40:43 +0300185 CHECK_NOTHROW(throw_if(false, 0));
186
187 CHECK_EQ(a, b);
188 CHECK_UNARY(a == b);
189 CHECK_UNARY_FALSE(a != b);
onqtamaf07cbb2017-04-19 19:40:43 +0300190}
191
192TEST_CASE("some asserts used in a function called by a test case") {
193 someAssertsInFunction();
194}
Stefane84f16e2022-01-05 15:21:41 +0100195
Salvage860904d2022-01-12 00:52:38 +0100196inline DOCTEST_NOINLINE void comp(int a, int b) {
197 if (CHECK(a == b)) { MESSAGE(":D"); }
198 if (CHECK_FALSE(a != b)) { MESSAGE(":D"); }
199 if (CHECK_EQ(a, b)) { MESSAGE(":D"); }
200 if (CHECK_UNARY(a == b)) { MESSAGE(":D"); }
201 if (CHECK_UNARY_FALSE(a != b)) { MESSAGE(":D"); }
202}
203
Salvagee798deb2022-01-12 02:44:57 +0100204DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4702)
Salvage860904d2022-01-12 00:52:38 +0100205TEST_CASE("check return values") {
206 comp(0, 0);
207
Salvagee798deb2022-01-12 02:44:57 +0100208 if (CHECK_THROWS(throw_if(true, true))) { MESSAGE(":D"); }
209 if (CHECK_THROWS_AS(throw_if(true, 2), int)) { MESSAGE(":D"); }
210 if (CHECK_NOTHROW(throw_if(false, 2))) { MESSAGE(":D"); }
211 if (CHECK_THROWS_WITH(throw_if(true, 2), "2")) { MESSAGE(":D"); }
Salvage860904d2022-01-12 00:52:38 +0100212}
213
214TEST_CASE("check return values no print") {
215 comp(4, 2);
216
Salvagee798deb2022-01-12 02:44:57 +0100217 if (CHECK_THROWS(throw_if(false, false))) { MESSAGE(":D"); }
218 if (CHECK_THROWS_AS(throw_if(true, 2), doctest::Approx)) { MESSAGE(":D"); }
219 if (CHECK_NOTHROW(throw_if(true, 2))) { MESSAGE(":D"); }
220 if (CHECK_THROWS_WITH(throw_if(true, 2), "1")) { MESSAGE(":D"); }
Stefane84f16e2022-01-05 15:21:41 +0100221}
Salvagee798deb2022-01-12 02:44:57 +0100222DOCTEST_MSVC_SUPPRESS_WARNING_POP