blob: dd37daf332422de982215aa8e40271ce512b9433 [file] [log] [blame]
onqtam4a655632016-05-26 14:20:52 +03001#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
2#include "doctest.h"
3
onqtam036fa312017-03-17 11:44:30 +02004template<typename T>
onqtam4b68df32017-03-17 20:45:54 +02005static int conditional_throw(bool in, const T& ex) {
6 if(in)
7#ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
8 throw ex;
9#else // DOCTEST_CONFIG_NO_EXCEPTIONS
10 ((void)ex);
11#endif // DOCTEST_CONFIG_NO_EXCEPTIONS
12 return 42;
13}
Elias Kosunen3de57e32016-12-20 18:57:03 +020014
onqtam4a655632016-05-26 14:20:52 +030015using doctest::Approx;
16
onqtam658c8702017-03-14 14:08:22 +020017TEST_SUITE("meaningless macros") {
18 TEST_CASE("an empty test that will succeed") {}
19
onqtam036fa312017-03-17 11:44:30 +020020 TEST_CASE("an empty test that will fail because of an exception") { conditional_throw(true, 0); }
onqtam658c8702017-03-14 14:08:22 +020021}
onqtam4a655632016-05-26 14:20:52 +030022
onqtam658c8702017-03-14 14:08:22 +020023TEST_SUITE_BEGIN("meaningless macros");
onqtam4a655632016-05-26 14:20:52 +030024
onqtam8327c6c2017-03-17 11:18:45 +020025TEST_CASE("an empty test that will fail because of a std::exception") { conditional_throw(true, std::runtime_error("whops!")); }
Elias Kosunen3de57e32016-12-20 18:57:03 +020026
onqtam4a655632016-05-26 14:20:52 +030027TEST_SUITE_END();
28
29TEST_CASE("normal macros") {
30 int a = 5;
31 int b = 5;
32
onqtam8327c6c2017-03-17 11:18:45 +020033 CHECK(conditional_throw(true, std::runtime_error("whops!")) == 42);
onqtam4a655632016-05-26 14:20:52 +030034
35 CHECK_FALSE(!(a == b));
36
37 REQUIRE(a == b);
onqtamcc6a6d62016-09-19 17:30:15 +030038
39 CHECK_EQ(a, b);
40
41 FAST_CHECK_EQ(a, b);
42
onqtam6cad5162016-06-02 20:11:46 +030043 // commented out because 32 vs 64 bit builds will fail when the output is compared
onqtam4a655632016-05-26 14:20:52 +030044 //WARN(reinterpret_cast<void*>(1000) == reinterpret_cast<void*>(1004));
45
46 CHECK(Approx(0.1000001) == 0.1000002);
47 CHECK(Approx(0.502) == 0.501);
48
onqtam036fa312017-03-17 11:44:30 +020049 conditional_throw(true, 0);
onqtam4a655632016-05-26 14:20:52 +030050}
51
Elias Kosunen3de57e32016-12-20 18:57:03 +020052TEST_CASE("normal macros with std::exception") {
53 int a = 5;
54 int b = 5;
55
onqtam036fa312017-03-17 11:44:30 +020056 CHECK(conditional_throw(true, 0) == 42);
Elias Kosunen3de57e32016-12-20 18:57:03 +020057
58 CHECK_FALSE(!(a == b));
59
60 REQUIRE(a == b);
61
62 CHECK_EQ(a, b);
63
64 FAST_CHECK_EQ(a, b);
65
66 // commented out because 32 vs 64 bit builds will fail when the output is compared
67 //WARN(reinterpret_cast<void*>(1000) == reinterpret_cast<void*>(1004));
68
69 CHECK(Approx(0.1000001) == 0.1000002);
70 CHECK(Approx(0.502) == 0.501);
71
onqtam036fa312017-03-17 11:44:30 +020072 conditional_throw(true, 0);
Elias Kosunen3de57e32016-12-20 18:57:03 +020073}
74
onqtam4a655632016-05-26 14:20:52 +030075TEST_CASE("exceptions-related macros") {
onqtam036fa312017-03-17 11:44:30 +020076 CHECK_THROWS(conditional_throw(false, 0));
77 CHECK_THROWS_AS(conditional_throw(false, 0), int);
78 CHECK_THROWS_AS(conditional_throw(true, 0), int);
79 CHECK_THROWS_AS(conditional_throw(true, 0), char);
onqtam4a655632016-05-26 14:20:52 +030080
onqtam036fa312017-03-17 11:44:30 +020081 CHECK_NOTHROW(conditional_throw(true, 0));
Elias Kosunen3de57e32016-12-20 18:57:03 +020082}
83
84TEST_CASE("exceptions-related macros for std::exception") {
onqtam036fa312017-03-17 11:44:30 +020085 CHECK_THROWS(conditional_throw(false, 0));
onqtam8327c6c2017-03-17 11:18:45 +020086 CHECK_THROWS_AS(conditional_throw(false, std::runtime_error("whops!")), std::exception);
87 CHECK_THROWS_AS(conditional_throw(true, std::runtime_error("whops!")), std::exception);
88 CHECK_THROWS_AS(conditional_throw(true, std::runtime_error("whops!")), int);
Elias Kosunen3de57e32016-12-20 18:57:03 +020089
onqtam8327c6c2017-03-17 11:18:45 +020090 REQUIRE_NOTHROW(conditional_throw(true, std::runtime_error("whops!")));
onqtam4a655632016-05-26 14:20:52 +030091}