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