blob: b88e970bc11deb526e71bc553250755e35a38e3b [file] [log] [blame]
onqtam15c5f0f2016-08-02 17:31:27 +03001#include "doctest.h"
onqtamcc9e8652016-08-02 14:23:38 +03002
onqtamc223b692016-08-02 17:20:06 +03003#include <ostream>
onqtam12d55982017-04-16 22:35:27 +03004#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
onqtamd9bb03a2016-08-02 15:32:49 +030011
onqtam98e12af2017-04-16 22:11:21 +030012TEST_CASE("doctest internals") {
onqtam12d55982017-04-16 22:35:27 +030013 using namespace doctest;
14
onqtam98e12af2017-04-16 22:11:21 +030015 // 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
onqtam12d55982017-04-16 22:35:27 +030034 //a += doctest::detail::fileForOutput("c:\\a");
35 //a += doctest::detail::fileForOutput("c:/a");
36 //a += doctest::detail::fileForOutput("a");
onqtam98e12af2017-04-16 22:11:21 +030037 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);
onqtamd9bb03a2016-08-02 15:32:49 +030042}
43
onqtam658c8702017-03-14 14:08:22 +020044TEST_SUITE_BEGIN("ts1");
onqtamd9bb03a2016-08-02 15:32:49 +030045
onqtamcc9e8652016-08-02 14:23:38 +030046using doctest::Approx;
47
48static int throws(bool in) {
49 if(in)
onqtam4b68df32017-03-17 20:45:54 +020050#ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
onqtamd9bb03a2016-08-02 15:32:49 +030051 throw false;
onqtam4b68df32017-03-17 20:45:54 +020052#else // DOCTEST_CONFIG_NO_EXCEPTIONS
53 return 0;
54#endif // DOCTEST_CONFIG_NO_EXCEPTIONS
onqtamcc9e8652016-08-02 14:23:38 +030055 return 42;
56}
57
onqtamc223b692016-08-02 17:20:06 +030058struct myType
onqtamcc6a6d62016-09-19 17:30:15 +030059{
60 myType() {}
61
62private:
63 myType(const myType&); // non-copyable
64 myType& operator=(const myType&); // non-assignable
65};
onqtamc223b692016-08-02 17:20:06 +030066
67static std::ostream& operator<<(std::ostream& stream, const myType&) {
68 stream << "myType!";
69 return stream;
70}
71
72static bool operator==(const myType&, const myType&) { return false; }
onqtamcc9e8652016-08-02 14:23:38 +030073
onqtamcc6a6d62016-09-19 17:30:15 +030074TEST_CASE("expressions should be evaluated only once") {
75 int a = 5;
76 REQUIRE(++a == 6);
77 REQUIRE_EQ(++a, 7);
78}
79
onqtam4321fd82016-08-02 14:59:17 +030080TEST_CASE("assertions") {
onqtam4e68e662016-08-03 11:37:51 +030081 CHECK(true);
onqtam30c5e4b2016-08-02 16:10:22 +030082 CHECK(1 == 0);
onqtamd9bb03a2016-08-02 15:32:49 +030083 CHECK_FALSE(1);
onqtamf4ea19f2016-09-20 00:07:56 +030084 myType a;
85 myType b;
86 CHECK(a == b);
onqtam4321fd82016-08-02 14:59:17 +030087 CHECK(Approx(0.1) == 0.2);
onqtam30c5e4b2016-08-02 16:10:22 +030088
onqtam4321fd82016-08-02 14:59:17 +030089 CHECK_THROWS(throws(true));
onqtamd9bb03a2016-08-02 15:32:49 +030090 CHECK_THROWS(throws(false));
onqtam4321fd82016-08-02 14:59:17 +030091 CHECK_NOTHROW(throws(false));
onqtamd9bb03a2016-08-02 15:32:49 +030092 CHECK_NOTHROW(throws(true));
onqtam4321fd82016-08-02 14:59:17 +030093 CHECK_THROWS_AS(throws(true), bool);
onqtamc223b692016-08-02 17:20:06 +030094 REQUIRE_THROWS_AS(throws(false), bool);
onqtam4321fd82016-08-02 14:59:17 +030095}
onqtamcc9e8652016-08-02 14:23:38 +030096
onqtam3ef438f2016-09-18 22:31:08 +030097TEST_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);
onqtam8d78e702016-09-18 22:38:44 +0300160 FAST_REQUIRE_UNARY_FALSE(1);
onqtam3ef438f2016-09-18 22:31:08 +0300161}
162
onqtamd9bb03a2016-08-02 15:32:49 +0300163TEST_CASE("throws") { throws(true); }
onqtamcc9e8652016-08-02 14:23:38 +0300164
165TEST_SUITE_END();
onqtam12d55982017-04-16 22:35:27 +0300166
167#endif // DOCTEST_CONFIG_DISABLE