blob: 67339699d75d9c4d9e22cf2e194dd1ddba9d448d [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>
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
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
onqtam0e6236a2018-11-30 16:49:58 +020038 CHECK_THROWS_WITH(throw_if(true, "whops!"), "whops! no match!"); // fails
39
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);
onqtam8cf90412018-11-30 17:05:01 +020047 CHECK_THROWS_AS(throw_if(true, std::runtime_error("whops!")), const 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
onqtam0e6236a2018-11-30 16:49:58 +020050 CHECK_THROWS_WITH(throw_if(false, ""), "whops!");
51
onqtam7cc0e962017-04-17 23:30:36 +030052 REQUIRE_NOTHROW(throw_if(true, std::runtime_error("whops!")));
onqtam4a655632016-05-26 14:20:52 +030053}
onqtamaf07cbb2017-04-19 19:40:43 +030054
55// =================================================================================================
56// == TESTING (ALMOST) ALL ASSERTS THAT THEY ACT ACCORDINGLY - not interesting examples...
57// =================================================================================================
58
59TEST_CASE("WARN level of asserts don't fail the test case") {
60 WARN(0);
61 WARN_FALSE(1);
62 WARN_THROWS(throw_if(false, 0));
63 WARN_THROWS_AS(throw_if(false, 0), bool);
64 WARN_THROWS_AS(throw_if(true, 0), bool);
65 WARN_NOTHROW(throw_if(true, 0));
onqtamc4948432018-12-01 18:47:12 +020066
onqtamaf07cbb2017-04-19 19:40:43 +030067 WARN_EQ(1, 0);
68 WARN_UNARY(0);
69 WARN_UNARY_FALSE(1);
onqtamaf07cbb2017-04-19 19:40:43 +030070}
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);
onqtam0e6236a2018-11-30 16:49:58 +020078 CHECK_THROWS_WITH(throw_if(true, 0), "unrecognized");
onqtamaf07cbb2017-04-19 19:40:43 +030079 CHECK_NOTHROW(throw_if(true, 0));
onqtamc4948432018-12-01 18:47:12 +020080
onqtamaf07cbb2017-04-19 19:40:43 +030081 CHECK_EQ(1, 0);
82 CHECK_UNARY(0);
83 CHECK_UNARY_FALSE(1);
onqtamaf07cbb2017-04-19 19:40:43 +030084
85 MESSAGE("reached!");
86}
87
88TEST_CASE("REQUIRE level of asserts fail and abort the test case - 1") {
89 REQUIRE(0);
90 MESSAGE("should not be reached!");
91}
92TEST_CASE("REQUIRE level of asserts fail and abort the test case - 2") {
93 REQUIRE_FALSE(1);
94 MESSAGE("should not be reached!");
95}
96TEST_CASE("REQUIRE level of asserts fail and abort the test case - 3") {
97 REQUIRE_THROWS(throw_if(false, 0));
98 MESSAGE("should not be reached!");
99}
100TEST_CASE("REQUIRE level of asserts fail and abort the test case - 4") {
101 REQUIRE_THROWS_AS(throw_if(false, 0), bool);
102 MESSAGE("should not be reached!");
103}
104TEST_CASE("REQUIRE level of asserts fail and abort the test case - 5") {
105 REQUIRE_THROWS_AS(throw_if(true, 0), bool);
106 MESSAGE("should not be reached!");
107}
108TEST_CASE("REQUIRE level of asserts fail and abort the test case - 6") {
109 REQUIRE_NOTHROW(throw_if(true, 0));
110 MESSAGE("should not be reached!");
111}
112TEST_CASE("REQUIRE level of asserts fail and abort the test case - 7") {
113 REQUIRE_EQ(1, 0);
114 MESSAGE("should not be reached!");
115}
116TEST_CASE("REQUIRE level of asserts fail and abort the test case - 8") {
117 REQUIRE_UNARY(0);
118 MESSAGE("should not be reached!");
119}
120TEST_CASE("REQUIRE level of asserts fail and abort the test case - 9") {
121 REQUIRE_UNARY_FALSE(1);
122 MESSAGE("should not be reached!");
123}
onqtamaf07cbb2017-04-19 19:40:43 +0300124
125TEST_CASE("all binary assertions") {
126 WARN_EQ(1, 1);
127 CHECK_EQ(1, 1);
128 REQUIRE_EQ(1, 1);
129 WARN_NE(1, 0);
130 CHECK_NE(1, 0);
131 REQUIRE_NE(1, 0);
132 WARN_GT(1, 0);
133 CHECK_GT(1, 0);
134 REQUIRE_GT(1, 0);
135 WARN_LT(0, 1);
136 CHECK_LT(0, 1);
137 REQUIRE_LT(0, 1);
138 WARN_GE(1, 1);
139 CHECK_GE(1, 1);
140 REQUIRE_GE(1, 1);
141 WARN_LE(1, 1);
142 CHECK_LE(1, 1);
143 REQUIRE_LE(1, 1);
144 WARN_UNARY(1);
145 CHECK_UNARY(1);
146 REQUIRE_UNARY(1);
147 WARN_UNARY_FALSE(0);
148 CHECK_UNARY_FALSE(0);
149 REQUIRE_UNARY_FALSE(0);
onqtamaf07cbb2017-04-19 19:40:43 +0300150}
151
152static void someAssertsInFunction() {
153 int a = 5;
154 int b = 5;
155 CHECK(a == b);
156 CHECK_FALSE(a != b);
157 CHECK_THROWS(throw_if(true, 0));
158 CHECK_THROWS_AS(throw_if(true, 0), int);
onqtam0e6236a2018-11-30 16:49:58 +0200159 CHECK_THROWS_WITH(throw_if(true, false), "unknown exception");
onqtamaf07cbb2017-04-19 19:40:43 +0300160 CHECK_NOTHROW(throw_if(false, 0));
161
162 CHECK_EQ(a, b);
163 CHECK_UNARY(a == b);
164 CHECK_UNARY_FALSE(a != b);
onqtamaf07cbb2017-04-19 19:40:43 +0300165}
166
167TEST_CASE("some asserts used in a function called by a test case") {
168 someAssertsInFunction();
169}