blob: 09187ff78d43d1b6365d7a6dec5131a4a0f60899 [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>
onqtamd9bb03a2016-08-02 15:32:49 +03004
onqtam30c5e4b2016-08-02 16:10:22 +03005// intentionally here so there are subcases on the same lines in different files
onqtamd9bb03a2016-08-02 15:32:49 +03006TEST_CASE("subcases") {
7 SUBCASE("1") {
8 SUBCASE("1.1") {}
9 SUBCASE("1.2") {}
10 }
11 SUBCASE("2") { CHECK(0); }
12 SUBCASE("3") {}
13}
14
onqtam658c8702017-03-14 14:08:22 +020015TEST_SUITE_BEGIN("ts1");
onqtamd9bb03a2016-08-02 15:32:49 +030016
onqtamcc9e8652016-08-02 14:23:38 +030017using doctest::Approx;
18
19static int throws(bool in) {
20 if(in)
onqtam4b68df32017-03-17 20:45:54 +020021#ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
onqtamd9bb03a2016-08-02 15:32:49 +030022 throw false;
onqtam4b68df32017-03-17 20:45:54 +020023#else // DOCTEST_CONFIG_NO_EXCEPTIONS
24 return 0;
25#endif // DOCTEST_CONFIG_NO_EXCEPTIONS
onqtamcc9e8652016-08-02 14:23:38 +030026 return 42;
27}
28
onqtamc223b692016-08-02 17:20:06 +030029struct myType
onqtamcc6a6d62016-09-19 17:30:15 +030030{
31 myType() {}
32
33private:
34 myType(const myType&); // non-copyable
35 myType& operator=(const myType&); // non-assignable
36};
onqtamc223b692016-08-02 17:20:06 +030037
38static std::ostream& operator<<(std::ostream& stream, const myType&) {
39 stream << "myType!";
40 return stream;
41}
42
43static bool operator==(const myType&, const myType&) { return false; }
onqtamcc9e8652016-08-02 14:23:38 +030044
onqtamcc6a6d62016-09-19 17:30:15 +030045TEST_CASE("expressions should be evaluated only once") {
46 int a = 5;
47 REQUIRE(++a == 6);
48 REQUIRE_EQ(++a, 7);
49}
50
onqtam4321fd82016-08-02 14:59:17 +030051TEST_CASE("assertions") {
onqtam4e68e662016-08-03 11:37:51 +030052 CHECK(true);
onqtam30c5e4b2016-08-02 16:10:22 +030053 CHECK(1 == 0);
onqtamd9bb03a2016-08-02 15:32:49 +030054 CHECK_FALSE(1);
onqtamf4ea19f2016-09-20 00:07:56 +030055 myType a;
56 myType b;
57 CHECK(a == b);
onqtam4321fd82016-08-02 14:59:17 +030058 CHECK(Approx(0.1) == 0.2);
onqtam30c5e4b2016-08-02 16:10:22 +030059
onqtam4321fd82016-08-02 14:59:17 +030060 CHECK_THROWS(throws(true));
onqtamd9bb03a2016-08-02 15:32:49 +030061 CHECK_THROWS(throws(false));
onqtam4321fd82016-08-02 14:59:17 +030062 CHECK_NOTHROW(throws(false));
onqtamd9bb03a2016-08-02 15:32:49 +030063 CHECK_NOTHROW(throws(true));
onqtam4321fd82016-08-02 14:59:17 +030064 CHECK_THROWS_AS(throws(true), bool);
onqtamc223b692016-08-02 17:20:06 +030065 REQUIRE_THROWS_AS(throws(false), bool);
onqtam4321fd82016-08-02 14:59:17 +030066}
onqtamcc9e8652016-08-02 14:23:38 +030067
onqtam3ef438f2016-09-18 22:31:08 +030068TEST_CASE("assertions - all of them") {
69 WARN(true);
70 CHECK(true);
71 REQUIRE(true);
72 WARN_FALSE(false);
73 CHECK_FALSE(false);
74 REQUIRE_FALSE(false);
75 WARN_THROWS(throws(true));
76 CHECK_THROWS(throws(true));
77 REQUIRE_THROWS(throws(true));
78 WARN_THROWS_AS(throws(true), bool);
79 CHECK_THROWS_AS(throws(true), bool);
80 REQUIRE_THROWS_AS(throws(true), bool);
81 WARN_NOTHROW(throws(false));
82 CHECK_NOTHROW(throws(false));
83 REQUIRE_NOTHROW(throws(false));
84 WARN_EQ(1, 1);
85 CHECK_EQ(1, 1);
86 REQUIRE_EQ(1, 1);
87 WARN_NE(1, 0);
88 CHECK_NE(1, 0);
89 REQUIRE_NE(1, 0);
90 WARN_GT(1, 0);
91 CHECK_GT(1, 0);
92 REQUIRE_GT(1, 0);
93 WARN_LT(0, 1);
94 CHECK_LT(0, 1);
95 REQUIRE_LT(0, 1);
96 WARN_GE(1, 1);
97 CHECK_GE(1, 1);
98 REQUIRE_GE(1, 1);
99 WARN_LE(1, 1);
100 CHECK_LE(1, 1);
101 REQUIRE_LE(1, 1);
102 WARN_UNARY(1);
103 CHECK_UNARY(1);
104 REQUIRE_UNARY(1);
105 WARN_UNARY_FALSE(0);
106 CHECK_UNARY_FALSE(0);
107 REQUIRE_UNARY_FALSE(0);
108 FAST_WARN_EQ(1, 1);
109 FAST_CHECK_EQ(1, 1);
110 FAST_REQUIRE_EQ(1, 1);
111 FAST_WARN_NE(1, 0);
112 FAST_CHECK_NE(1, 0);
113 FAST_REQUIRE_NE(1, 0);
114 FAST_WARN_GT(1, 0);
115 FAST_CHECK_GT(1, 0);
116 FAST_REQUIRE_GT(1, 0);
117 FAST_WARN_LT(0, 1);
118 FAST_CHECK_LT(0, 1);
119 FAST_REQUIRE_LT(0, 1);
120 FAST_WARN_GE(1, 1);
121 FAST_CHECK_GE(1, 1);
122 FAST_REQUIRE_GE(1, 1);
123 FAST_WARN_LE(1, 1);
124 FAST_CHECK_LE(1, 1);
125 FAST_REQUIRE_LE(1, 1);
126 FAST_WARN_UNARY(1);
127 FAST_CHECK_UNARY(1);
128 FAST_REQUIRE_UNARY(1);
129 FAST_WARN_UNARY_FALSE(0);
130 FAST_CHECK_UNARY_FALSE(0);
onqtam8d78e702016-09-18 22:38:44 +0300131 FAST_REQUIRE_UNARY_FALSE(1);
onqtam3ef438f2016-09-18 22:31:08 +0300132}
133
onqtamd9bb03a2016-08-02 15:32:49 +0300134TEST_CASE("throws") { throws(true); }
onqtamcc9e8652016-08-02 14:23:38 +0300135
136TEST_SUITE_END();