blob: a723ffd5922d384962a0b00c20e7815f1c2847ac [file] [log] [blame]
onqtam4a655632016-05-26 14:20:52 +03001#include "doctest.h"
2
onqtam7cc0e962017-04-17 23:30:36 +03003#include "header.h"
4
onqtam12d55982017-04-16 22:35:27 +03005#include <stdexcept>
6
onqtam4a655632016-05-26 14:20:52 +03007TEST_CASE("normal macros") {
8 int a = 5;
9 int b = 5;
10
onqtam7cc0e962017-04-17 23:30:36 +030011 CHECK(throw_if(true, std::runtime_error("whops!")) == 42);
onqtam4a655632016-05-26 14:20:52 +030012
13 CHECK_FALSE(!(a == b));
14
15 REQUIRE(a == b);
onqtamcc6a6d62016-09-19 17:30:15 +030016
17 CHECK_EQ(a, b);
18
19 FAST_CHECK_EQ(a, b);
20
onqtam7cc0e962017-04-17 23:30:36 +030021 CHECK(doctest::Approx(0.1000001) == 0.1000002);
22 CHECK(doctest::Approx(0.502) == 0.501);
onqtamaf07cbb2017-04-19 19:40:43 +030023}
onqtam4a655632016-05-26 14:20:52 +030024
onqtamaf07cbb2017-04-19 19:40:43 +030025TEST_CASE("expressions should be evaluated only once") {
26 int a = 5;
27 REQUIRE(++a == 6);
28 REQUIRE_EQ(++a, 7);
Elias Kosunen3de57e32016-12-20 18:57:03 +020029}
30
onqtam4a655632016-05-26 14:20:52 +030031TEST_CASE("exceptions-related macros") {
onqtamaf07cbb2017-04-19 19:40:43 +030032 CHECK_THROWS(throw_if(true, 0));
33 CHECK_THROWS(throw_if(false, 0)); // fails
onqtam7cc0e962017-04-17 23:30:36 +030034 CHECK_THROWS_AS(throw_if(true, 0), int);
onqtamaf07cbb2017-04-19 19:40:43 +030035 CHECK_THROWS_AS(throw_if(true, 0), char); // fails
36 CHECK_THROWS_AS(throw_if(false, 0), int); // fails
onqtam4a655632016-05-26 14:20:52 +030037
onqtamaf07cbb2017-04-19 19:40:43 +030038 CHECK_NOTHROW(throw_if(true, 0)); // fails
39 CHECK_NOTHROW(throw_if(false, 0));
Elias Kosunen3de57e32016-12-20 18:57:03 +020040}
41
42TEST_CASE("exceptions-related macros for std::exception") {
onqtam7cc0e962017-04-17 23:30:36 +030043 CHECK_THROWS(throw_if(false, 0));
onqtamfd9560c2017-07-14 15:19:21 +030044 CHECK_THROWS_AS(throw_if(false, std::runtime_error("whops!")), std::exception);
45 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), std::exception);
onqtam7cc0e962017-04-17 23:30:36 +030046 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), int);
Elias Kosunen3de57e32016-12-20 18:57:03 +020047
onqtam7cc0e962017-04-17 23:30:36 +030048 REQUIRE_NOTHROW(throw_if(true, std::runtime_error("whops!")));
onqtam4a655632016-05-26 14:20:52 +030049}
onqtamaf07cbb2017-04-19 19:40:43 +030050
51// =================================================================================================
52// == TESTING (ALMOST) ALL ASSERTS THAT THEY ACT ACCORDINGLY - not interesting examples...
53// =================================================================================================
54
55TEST_CASE("WARN level of asserts don't fail the test case") {
56 WARN(0);
57 WARN_FALSE(1);
58 WARN_THROWS(throw_if(false, 0));
59 WARN_THROWS_AS(throw_if(false, 0), bool);
60 WARN_THROWS_AS(throw_if(true, 0), bool);
61 WARN_NOTHROW(throw_if(true, 0));
62
63 WARN_EQ(1, 0);
64 WARN_UNARY(0);
65 WARN_UNARY_FALSE(1);
66
67 FAST_WARN_EQ(1, 0);
68 FAST_WARN_UNARY(0);
69 FAST_WARN_UNARY_FALSE(1);
70}
71
72TEST_CASE("CHECK level of asserts fail the test case but don't abort it") {
73 CHECK(0);
74 CHECK_FALSE(1);
75 CHECK_THROWS(throw_if(false, 0));
76 CHECK_THROWS_AS(throw_if(false, 0), bool);
77 CHECK_THROWS_AS(throw_if(true, 0), bool);
78 CHECK_NOTHROW(throw_if(true, 0));
79
80 CHECK_EQ(1, 0);
81 CHECK_UNARY(0);
82 CHECK_UNARY_FALSE(1);
83
84 FAST_CHECK_EQ(1, 0);
85 FAST_CHECK_UNARY(0);
86 FAST_CHECK_UNARY_FALSE(1);
87
88 MESSAGE("reached!");
89}
90
91TEST_CASE("REQUIRE level of asserts fail and abort the test case - 1") {
92 REQUIRE(0);
93 MESSAGE("should not be reached!");
94}
95TEST_CASE("REQUIRE level of asserts fail and abort the test case - 2") {
96 REQUIRE_FALSE(1);
97 MESSAGE("should not be reached!");
98}
99TEST_CASE("REQUIRE level of asserts fail and abort the test case - 3") {
100 REQUIRE_THROWS(throw_if(false, 0));
101 MESSAGE("should not be reached!");
102}
103TEST_CASE("REQUIRE level of asserts fail and abort the test case - 4") {
104 REQUIRE_THROWS_AS(throw_if(false, 0), bool);
105 MESSAGE("should not be reached!");
106}
107TEST_CASE("REQUIRE level of asserts fail and abort the test case - 5") {
108 REQUIRE_THROWS_AS(throw_if(true, 0), bool);
109 MESSAGE("should not be reached!");
110}
111TEST_CASE("REQUIRE level of asserts fail and abort the test case - 6") {
112 REQUIRE_NOTHROW(throw_if(true, 0));
113 MESSAGE("should not be reached!");
114}
115TEST_CASE("REQUIRE level of asserts fail and abort the test case - 7") {
116 REQUIRE_EQ(1, 0);
117 MESSAGE("should not be reached!");
118}
119TEST_CASE("REQUIRE level of asserts fail and abort the test case - 8") {
120 REQUIRE_UNARY(0);
121 MESSAGE("should not be reached!");
122}
123TEST_CASE("REQUIRE level of asserts fail and abort the test case - 9") {
124 REQUIRE_UNARY_FALSE(1);
125 MESSAGE("should not be reached!");
126}
127TEST_CASE("REQUIRE level of asserts fail and abort the test case - 10") {
128 FAST_REQUIRE_EQ(1, 0);
129 MESSAGE("should not be reached!");
130}
131TEST_CASE("REQUIRE level of asserts fail and abort the test case - 11") {
132 FAST_REQUIRE_UNARY(0);
133 MESSAGE("should not be reached!");
134}
135TEST_CASE("REQUIRE level of asserts fail and abort the test case - 12") {
136 FAST_REQUIRE_UNARY_FALSE(1);
137 MESSAGE("should not be reached!");
138}
139
140TEST_CASE("all binary assertions") {
141 WARN_EQ(1, 1);
142 CHECK_EQ(1, 1);
143 REQUIRE_EQ(1, 1);
144 WARN_NE(1, 0);
145 CHECK_NE(1, 0);
146 REQUIRE_NE(1, 0);
147 WARN_GT(1, 0);
148 CHECK_GT(1, 0);
149 REQUIRE_GT(1, 0);
150 WARN_LT(0, 1);
151 CHECK_LT(0, 1);
152 REQUIRE_LT(0, 1);
153 WARN_GE(1, 1);
154 CHECK_GE(1, 1);
155 REQUIRE_GE(1, 1);
156 WARN_LE(1, 1);
157 CHECK_LE(1, 1);
158 REQUIRE_LE(1, 1);
159 WARN_UNARY(1);
160 CHECK_UNARY(1);
161 REQUIRE_UNARY(1);
162 WARN_UNARY_FALSE(0);
163 CHECK_UNARY_FALSE(0);
164 REQUIRE_UNARY_FALSE(0);
165 FAST_WARN_EQ(1, 1);
166 FAST_CHECK_EQ(1, 1);
167 FAST_REQUIRE_EQ(1, 1);
168 FAST_WARN_NE(1, 0);
169 FAST_CHECK_NE(1, 0);
170 FAST_REQUIRE_NE(1, 0);
171 FAST_WARN_GT(1, 0);
172 FAST_CHECK_GT(1, 0);
173 FAST_REQUIRE_GT(1, 0);
174 FAST_WARN_LT(0, 1);
175 FAST_CHECK_LT(0, 1);
176 FAST_REQUIRE_LT(0, 1);
177 FAST_WARN_GE(1, 1);
178 FAST_CHECK_GE(1, 1);
179 FAST_REQUIRE_GE(1, 1);
180 FAST_WARN_LE(1, 1);
181 FAST_CHECK_LE(1, 1);
182 FAST_REQUIRE_LE(1, 1);
183 FAST_WARN_UNARY(1);
184 FAST_CHECK_UNARY(1);
185 FAST_REQUIRE_UNARY(1);
186 FAST_WARN_UNARY_FALSE(0);
187 FAST_CHECK_UNARY_FALSE(0);
188 FAST_REQUIRE_UNARY_FALSE(0);
189}
190
191static void someAssertsInFunction() {
192 int a = 5;
193 int b = 5;
194 CHECK(a == b);
195 CHECK_FALSE(a != b);
196 CHECK_THROWS(throw_if(true, 0));
197 CHECK_THROWS_AS(throw_if(true, 0), int);
198 CHECK_NOTHROW(throw_if(false, 0));
199
200 CHECK_EQ(a, b);
201 CHECK_UNARY(a == b);
202 CHECK_UNARY_FALSE(a != b);
203 FAST_CHECK_EQ(a, b);
204 FAST_CHECK_UNARY(a == b);
205 FAST_CHECK_UNARY_FALSE(a != b);
206}
207
208TEST_CASE("some asserts used in a function called by a test case") {
209 someAssertsInFunction();
210}