onqtam | 15c5f0f | 2016-08-02 17:31:27 +0300 | [diff] [blame] | 1 | #include "doctest.h" |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 2 | |
onqtam | c223b69 | 2016-08-02 17:20:06 +0300 | [diff] [blame] | 3 | #include <ostream> |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 4 | #include <sstream> |
| 5 | |
| 6 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6) |
| 7 | #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" |
| 8 | #endif // > gcc 4.6 |
| 9 | |
| 10 | #ifndef DOCTEST_CONFIG_DISABLE |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 11 | |
onqtam | 98e12af | 2017-04-16 22:11:21 +0300 | [diff] [blame] | 12 | TEST_CASE("doctest internals") { |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 13 | using namespace doctest; |
| 14 | |
onqtam | 98e12af | 2017-04-16 22:11:21 +0300 | [diff] [blame] | 15 | // string stuff |
| 16 | doctest::String a(0); |
| 17 | const doctest::String const_str("omgomgomg"); |
| 18 | a = const_str.c_str(); |
| 19 | CHECK(a.size() == const_str.size()); |
| 20 | CHECK(a.length() == const_str.length()); |
| 21 | CHECK(a.compare(const_str, true) == 0); |
| 22 | CHECK(a.compare("omgomgomg", false) == 0); |
| 23 | |
| 24 | // toString |
| 25 | a += toString("aaa") + toString(0.5f) + toString('c') + toString(true) + |
| 26 | toString(static_cast<long double>(0.1)) // |
| 27 | + toString(static_cast<unsigned char>(1)) // |
| 28 | + toString(static_cast<short>(1)) // |
| 29 | + toString(static_cast<long>(1)) // |
| 30 | + toString(static_cast<unsigned long>(1)) // |
| 31 | + toString(static_cast<unsigned short>(1)); |
| 32 | |
| 33 | // others |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 34 | //a += doctest::detail::fileForOutput("c:\\a"); |
| 35 | //a += doctest::detail::fileForOutput("c:/a"); |
| 36 | //a += doctest::detail::fileForOutput("a"); |
onqtam | 98e12af | 2017-04-16 22:11:21 +0300 | [diff] [blame] | 37 | std::ostringstream oss; |
| 38 | oss << a; |
| 39 | oss << doctest::detail::getAssertString(static_cast<doctest::detail::assertType::Enum>(3)); |
| 40 | a += oss.str().c_str(); |
| 41 | CHECK(doctest::detail::rawMemoryToString(a).length() > 0u); |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 42 | } |
| 43 | |
onqtam | 658c870 | 2017-03-14 14:08:22 +0200 | [diff] [blame] | 44 | TEST_SUITE_BEGIN("ts1"); |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 45 | |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 46 | using doctest::Approx; |
| 47 | |
| 48 | static int throws(bool in) { |
| 49 | if(in) |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame] | 50 | #ifndef DOCTEST_CONFIG_NO_EXCEPTIONS |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 51 | throw false; |
onqtam | 4b68df3 | 2017-03-17 20:45:54 +0200 | [diff] [blame] | 52 | #else // DOCTEST_CONFIG_NO_EXCEPTIONS |
| 53 | return 0; |
| 54 | #endif // DOCTEST_CONFIG_NO_EXCEPTIONS |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 55 | return 42; |
| 56 | } |
| 57 | |
onqtam | c223b69 | 2016-08-02 17:20:06 +0300 | [diff] [blame] | 58 | struct myType |
onqtam | cc6a6d6 | 2016-09-19 17:30:15 +0300 | [diff] [blame] | 59 | { |
| 60 | myType() {} |
| 61 | |
| 62 | private: |
| 63 | myType(const myType&); // non-copyable |
| 64 | myType& operator=(const myType&); // non-assignable |
| 65 | }; |
onqtam | c223b69 | 2016-08-02 17:20:06 +0300 | [diff] [blame] | 66 | |
| 67 | static std::ostream& operator<<(std::ostream& stream, const myType&) { |
| 68 | stream << "myType!"; |
| 69 | return stream; |
| 70 | } |
| 71 | |
| 72 | static bool operator==(const myType&, const myType&) { return false; } |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 73 | |
onqtam | cc6a6d6 | 2016-09-19 17:30:15 +0300 | [diff] [blame] | 74 | TEST_CASE("expressions should be evaluated only once") { |
| 75 | int a = 5; |
| 76 | REQUIRE(++a == 6); |
| 77 | REQUIRE_EQ(++a, 7); |
| 78 | } |
| 79 | |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 80 | TEST_CASE("assertions") { |
onqtam | 4e68e66 | 2016-08-03 11:37:51 +0300 | [diff] [blame] | 81 | CHECK(true); |
onqtam | 30c5e4b | 2016-08-02 16:10:22 +0300 | [diff] [blame] | 82 | CHECK(1 == 0); |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 83 | CHECK_FALSE(1); |
onqtam | f4ea19f | 2016-09-20 00:07:56 +0300 | [diff] [blame] | 84 | myType a; |
| 85 | myType b; |
| 86 | CHECK(a == b); |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 87 | CHECK(Approx(0.1) == 0.2); |
onqtam | 30c5e4b | 2016-08-02 16:10:22 +0300 | [diff] [blame] | 88 | |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 89 | CHECK_THROWS(throws(true)); |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 90 | CHECK_THROWS(throws(false)); |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 91 | CHECK_NOTHROW(throws(false)); |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 92 | CHECK_NOTHROW(throws(true)); |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 93 | CHECK_THROWS_AS(throws(true), bool); |
onqtam | c223b69 | 2016-08-02 17:20:06 +0300 | [diff] [blame] | 94 | REQUIRE_THROWS_AS(throws(false), bool); |
onqtam | 4321fd8 | 2016-08-02 14:59:17 +0300 | [diff] [blame] | 95 | } |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 96 | |
onqtam | 3ef438f | 2016-09-18 22:31:08 +0300 | [diff] [blame] | 97 | TEST_CASE("assertions - all of them") { |
| 98 | WARN(true); |
| 99 | CHECK(true); |
| 100 | REQUIRE(true); |
| 101 | WARN_FALSE(false); |
| 102 | CHECK_FALSE(false); |
| 103 | REQUIRE_FALSE(false); |
| 104 | WARN_THROWS(throws(true)); |
| 105 | CHECK_THROWS(throws(true)); |
| 106 | REQUIRE_THROWS(throws(true)); |
| 107 | WARN_THROWS_AS(throws(true), bool); |
| 108 | CHECK_THROWS_AS(throws(true), bool); |
| 109 | REQUIRE_THROWS_AS(throws(true), bool); |
| 110 | WARN_NOTHROW(throws(false)); |
| 111 | CHECK_NOTHROW(throws(false)); |
| 112 | REQUIRE_NOTHROW(throws(false)); |
| 113 | WARN_EQ(1, 1); |
| 114 | CHECK_EQ(1, 1); |
| 115 | REQUIRE_EQ(1, 1); |
| 116 | WARN_NE(1, 0); |
| 117 | CHECK_NE(1, 0); |
| 118 | REQUIRE_NE(1, 0); |
| 119 | WARN_GT(1, 0); |
| 120 | CHECK_GT(1, 0); |
| 121 | REQUIRE_GT(1, 0); |
| 122 | WARN_LT(0, 1); |
| 123 | CHECK_LT(0, 1); |
| 124 | REQUIRE_LT(0, 1); |
| 125 | WARN_GE(1, 1); |
| 126 | CHECK_GE(1, 1); |
| 127 | REQUIRE_GE(1, 1); |
| 128 | WARN_LE(1, 1); |
| 129 | CHECK_LE(1, 1); |
| 130 | REQUIRE_LE(1, 1); |
| 131 | WARN_UNARY(1); |
| 132 | CHECK_UNARY(1); |
| 133 | REQUIRE_UNARY(1); |
| 134 | WARN_UNARY_FALSE(0); |
| 135 | CHECK_UNARY_FALSE(0); |
| 136 | REQUIRE_UNARY_FALSE(0); |
| 137 | FAST_WARN_EQ(1, 1); |
| 138 | FAST_CHECK_EQ(1, 1); |
| 139 | FAST_REQUIRE_EQ(1, 1); |
| 140 | FAST_WARN_NE(1, 0); |
| 141 | FAST_CHECK_NE(1, 0); |
| 142 | FAST_REQUIRE_NE(1, 0); |
| 143 | FAST_WARN_GT(1, 0); |
| 144 | FAST_CHECK_GT(1, 0); |
| 145 | FAST_REQUIRE_GT(1, 0); |
| 146 | FAST_WARN_LT(0, 1); |
| 147 | FAST_CHECK_LT(0, 1); |
| 148 | FAST_REQUIRE_LT(0, 1); |
| 149 | FAST_WARN_GE(1, 1); |
| 150 | FAST_CHECK_GE(1, 1); |
| 151 | FAST_REQUIRE_GE(1, 1); |
| 152 | FAST_WARN_LE(1, 1); |
| 153 | FAST_CHECK_LE(1, 1); |
| 154 | FAST_REQUIRE_LE(1, 1); |
| 155 | FAST_WARN_UNARY(1); |
| 156 | FAST_CHECK_UNARY(1); |
| 157 | FAST_REQUIRE_UNARY(1); |
| 158 | FAST_WARN_UNARY_FALSE(0); |
| 159 | FAST_CHECK_UNARY_FALSE(0); |
onqtam | 8d78e70 | 2016-09-18 22:38:44 +0300 | [diff] [blame] | 160 | FAST_REQUIRE_UNARY_FALSE(1); |
onqtam | 3ef438f | 2016-09-18 22:31:08 +0300 | [diff] [blame] | 161 | } |
| 162 | |
onqtam | d9bb03a | 2016-08-02 15:32:49 +0300 | [diff] [blame] | 163 | TEST_CASE("throws") { throws(true); } |
onqtam | cc9e865 | 2016-08-02 14:23:38 +0300 | [diff] [blame] | 164 | |
| 165 | TEST_SUITE_END(); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 166 | |
| 167 | #endif // DOCTEST_CONFIG_DISABLE |