onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 1 | #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN |
| 2 | #include "doctest.h" |
| 3 | |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 4 | template<typename T> |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame] | 5 | static 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 Kosunen | 3de57e3 | 2016-12-20 18:57:03 +0200 | [diff] [blame] | 14 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 15 | using doctest::Approx; |
| 16 | |
onqtam | 658c870 | 2017-03-14 14:08:22 +0200 | [diff] [blame] | 17 | TEST_SUITE("meaningless macros") { |
| 18 | TEST_CASE("an empty test that will succeed") {} |
| 19 | |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 20 | TEST_CASE("an empty test that will fail because of an exception") { conditional_throw(true, 0); } |
onqtam | 658c870 | 2017-03-14 14:08:22 +0200 | [diff] [blame] | 21 | } |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 22 | |
onqtam | 658c870 | 2017-03-14 14:08:22 +0200 | [diff] [blame] | 23 | TEST_SUITE_BEGIN("meaningless macros"); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 24 | |
onqtam | 8327c6c | 2017-03-17 11:18:45 +0200 | [diff] [blame] | 25 | TEST_CASE("an empty test that will fail because of a std::exception") { conditional_throw(true, std::runtime_error("whops!")); } |
Elias Kosunen | 3de57e3 | 2016-12-20 18:57:03 +0200 | [diff] [blame] | 26 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 27 | TEST_SUITE_END(); |
| 28 | |
| 29 | TEST_CASE("normal macros") { |
| 30 | int a = 5; |
| 31 | int b = 5; |
| 32 | |
onqtam | 8327c6c | 2017-03-17 11:18:45 +0200 | [diff] [blame] | 33 | CHECK(conditional_throw(true, std::runtime_error("whops!")) == 42); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 34 | |
| 35 | CHECK_FALSE(!(a == b)); |
| 36 | |
| 37 | REQUIRE(a == b); |
onqtam | cc6a6d6 | 2016-09-19 17:30:15 +0300 | [diff] [blame] | 38 | |
| 39 | CHECK_EQ(a, b); |
| 40 | |
| 41 | FAST_CHECK_EQ(a, b); |
| 42 | |
onqtam | 6cad516 | 2016-06-02 20:11:46 +0300 | [diff] [blame] | 43 | // commented out because 32 vs 64 bit builds will fail when the output is compared |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 44 | //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 | |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 49 | conditional_throw(true, 0); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 50 | } |
| 51 | |
Elias Kosunen | 3de57e3 | 2016-12-20 18:57:03 +0200 | [diff] [blame] | 52 | TEST_CASE("normal macros with std::exception") { |
| 53 | int a = 5; |
| 54 | int b = 5; |
| 55 | |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 56 | CHECK(conditional_throw(true, 0) == 42); |
Elias Kosunen | 3de57e3 | 2016-12-20 18:57:03 +0200 | [diff] [blame] | 57 | |
| 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 | |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 72 | conditional_throw(true, 0); |
Elias Kosunen | 3de57e3 | 2016-12-20 18:57:03 +0200 | [diff] [blame] | 73 | } |
| 74 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 75 | TEST_CASE("exceptions-related macros") { |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 76 | 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); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 80 | |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 81 | CHECK_NOTHROW(conditional_throw(true, 0)); |
Elias Kosunen | 3de57e3 | 2016-12-20 18:57:03 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST_CASE("exceptions-related macros for std::exception") { |
onqtam | 036fa31 | 2017-03-17 11:44:30 +0200 | [diff] [blame] | 85 | CHECK_THROWS(conditional_throw(false, 0)); |
onqtam | 8327c6c | 2017-03-17 11:18:45 +0200 | [diff] [blame] | 86 | 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 Kosunen | 3de57e3 | 2016-12-20 18:57:03 +0200 | [diff] [blame] | 89 | |
onqtam | 8327c6c | 2017-03-17 11:18:45 +0200 | [diff] [blame] | 90 | REQUIRE_NOTHROW(conditional_throw(true, std::runtime_error("whops!"))); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 91 | } |