blob: de82bb033eb3fafb090e5b97ae37f0545aaf6154 [file] [log] [blame]
ncihnegnc5458f22019-01-28 05:08:18 -08001#include <doctest/doctest.h>
onqtam4a655632016-05-26 14:20:52 +03002
onqtam7cc0e962017-04-17 23:30:36 +03003#include "header.h"
4
Salvage6545bdc2022-01-13 00:14:21 +01005TEST_CASE("no headers") {
6 char chs1[] = { '1', 'a', 's' };
7 MESSAGE(chs1); CHECK(chs1 == nullptr);
8 const char* chs2{"1as"};
9 MESSAGE(chs2); CHECK(chs2 == nullptr);
10 MESSAGE("1as"); CHECK("1as" == nullptr);
11
12 int ints[] = { 0, 1, 1, 2, 3, 5, 8, 13 };
13 MESSAGE(ints); CHECK(ints == nullptr);
Salvage7be3b872022-01-13 05:08:34 +010014
15 char* cptr = (char*)(ints + 4);
16 CHECK(cptr == nullptr);
17
18 enum Test {
19 A = 0, B, C = 100,
20 };
21 MESSAGE(A); CHECK(A == C);
Salvage6545bdc2022-01-13 00:14:21 +010022}
23
onqtamabf39d22017-10-28 21:30:45 +030024DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam4a655632016-05-26 14:20:52 +030025#include <string>
26#include <vector>
27#include <list>
onqtam4a655632016-05-26 14:20:52 +030028#include <sstream>
Salvage2fc3d4e2022-01-12 16:23:19 +010029#include <limits>
onqtamabf39d22017-10-28 21:30:45 +030030DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam4a655632016-05-26 14:20:52 +030031
onqtam190ba252019-03-23 09:58:34 +020032DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics
33
onqtam4a655632016-05-26 14:20:52 +030034// the standard forbids writing in the std namespace but it works on all compilers
35namespace std
36{
37template <typename T>
38ostream& operator<<(ostream& stream, const vector<T>& in) {
39 stream << "[";
Salvage6545bdc2022-01-13 00:14:21 +010040 for (size_t i = 0; i < in.size(); ++i) {
41 if (i != 0) { stream << ", "; }
42 stream << in[i];
43 }
onqtam4a655632016-05-26 14:20:52 +030044 stream << "]";
45 return stream;
46}
47}
48
49// as an alternative you may write a specialization of doctest::StringMaker
Salvage6545bdc2022-01-13 00:14:21 +010050namespace doctest
51{ namespace detail {
onqtam4a655632016-05-26 14:20:52 +030052template <typename T>
Salvage6545bdc2022-01-13 00:14:21 +010053struct StringStream<std::list<T>>
onqtam4a655632016-05-26 14:20:52 +030054{
Salvage6545bdc2022-01-13 00:14:21 +010055 static void convert(std::ostream* s, const std::list<T>& in) {
56 *s << "[";
57 for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) {
58 *s << *it;
59 if (++it != in.end()) { *s << ", "; }
60 }
61 *s << "]";
onqtam4a655632016-05-26 14:20:52 +030062 }
63};
Salvage6545bdc2022-01-13 00:14:21 +010064} }
onqtam4a655632016-05-26 14:20:52 +030065
onqtam4a655632016-05-26 14:20:52 +030066template <typename T, typename K>
67struct MyType
68{
69 T one;
70 K two;
71};
72
73template <typename T>
onqtam02b7eb72017-10-28 23:59:49 +030074struct MyTypeInherited : MyType<T, unsigned>
onqtam4a655632016-05-26 14:20:52 +030075{};
76
77template <typename T, typename K>
78bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) {
79 return lhs.one == rhs.one && lhs.two == rhs.two;
80}
81
82template <typename T, typename K>
83std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) {
84 stream << "[" << in.one << ", " << in.two << "]";
85 return stream;
86}
87
onqtam12d55982017-04-16 22:35:27 +030088namespace Bar
89{
onqtam4a655632016-05-26 14:20:52 +030090struct Foo
onqtam12d55982017-04-16 22:35:27 +030091{
92 friend bool operator==(const Foo&, const Foo&) { return false; }
93};
onqtam4a655632016-05-26 14:20:52 +030094
Viktor Kirilov81fe2e82020-12-04 18:59:26 +020095struct MyOtherType
Viktor Kirilov17d984c2020-12-04 15:13:33 +020096{
Viktor Kirilov81fe2e82020-12-04 18:59:26 +020097 int data;
98 friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +020099};
100
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200101// you also can use a template operator<< if your code does not use std::ostream
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200102template <class OStream>
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200103OStream& operator<<(OStream& stream, const MyOtherType& in) {
104 stream << "MyOtherType: " << in.data;
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200105 return stream;
106}
107
onqtamb18680d2016-11-15 14:08:56 +0200108} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +0300109
Salvage0bd08892022-01-13 00:19:20 +0100110namespace doctest {
111 namespace detail {
112 template <> struct StringStream<Bar::Foo> {
113 static void convert(std::ostream* s, const Bar::Foo&) {
114 *s << "Foo{}";
115 }
116 };
117 }
118}
119
onqtam05bcc372017-03-17 02:10:38 +0200120// set an exception translator for MyTypeInherited<int>
121REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) {
onqtam12d55982017-04-16 22:35:27 +0300122 return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " +
123 doctest::toString(ex.two) + ")";
onqtam05bcc372017-03-17 02:10:38 +0200124}
125
onqtam7cc0e962017-04-17 23:30:36 +0300126TEST_CASE("all asserts should fail and show how the objects get stringified") {
onqtam4a655632016-05-26 14:20:52 +0300127 MyTypeInherited<int> bla1;
128 bla1.one = 5;
onqtam02b7eb72017-10-28 23:59:49 +0300129 bla1.two = 4u;
onqtam4a655632016-05-26 14:20:52 +0300130
onqtamb18680d2016-11-15 14:08:56 +0200131 Bar::Foo f1;
Salvage6545bdc2022-01-13 00:14:21 +0100132 MESSAGE(f1);
onqtamb18680d2016-11-15 14:08:56 +0200133 Bar::Foo f2;
onqtam4a655632016-05-26 14:20:52 +0300134 CHECK(f1 == f2);
135
136 // std::string already has an operator<< working with std::ostream
onqtam367098b2021-12-15 15:15:06 +0200137 std::string dummy = "omg";
onqtam4a655632016-05-26 14:20:52 +0300138
Salvage6545bdc2022-01-13 00:14:21 +0100139 MESSAGE(dummy);
140
onqtam367098b2021-12-15 15:15:06 +0200141 CHECK(dummy == "tralala"); // should fail
142 CHECK("tralala" == dummy); // should fail
onqtam4a655632016-05-26 14:20:52 +0300143
144 std::vector<int> vec1;
145 vec1.push_back(1);
146 vec1.push_back(2);
147 vec1.push_back(3);
148
Salvage6545bdc2022-01-13 00:14:21 +0100149 MESSAGE(vec1);
150
onqtam4a655632016-05-26 14:20:52 +0300151 std::vector<int> vec2;
152 vec2.push_back(1);
153 vec2.push_back(2);
154 vec2.push_back(4);
155
156 CHECK(vec1 == vec2);
157
158 std::list<int> lst_1;
159 lst_1.push_back(1);
160 lst_1.push_back(42);
161 lst_1.push_back(3);
162
Salvage6545bdc2022-01-13 00:14:21 +0100163 MESSAGE(lst_1);
164
onqtam4a655632016-05-26 14:20:52 +0300165 std::list<int> lst_2;
166 lst_2.push_back(1);
167 lst_2.push_back(2);
168 lst_2.push_back(666);
169
170 CHECK(lst_1 == lst_2);
onqtam12d55982017-04-16 22:35:27 +0300171
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200172 {
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200173 Bar::MyOtherType s1 {42};
174 Bar::MyOtherType s2 {666};
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200175 INFO("s1=", s1, " s2=", s2);
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200176 CHECK(s1 == s2);
177 CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2);
178 }
179
Salvage4ddb7932022-01-12 16:17:39 +0100180 CHECK(doctest::IsNaN<double>(0.5));
181 CHECK(doctest::IsNaN<float>(std::numeric_limits<float>::infinity()));
Salvage84c509b2022-01-12 21:52:07 +0100182 // can't test actual nan because it's implementation defined
Salvage245d4b22022-01-12 16:04:16 +0100183
onqtam05bcc372017-03-17 02:10:38 +0200184 // lets see if this exception gets translated
onqtam7cc0e962017-04-17 23:30:36 +0300185 throw_if(true, bla1);
onqtam05bcc372017-03-17 02:10:38 +0200186}
187
onqtam12d55982017-04-16 22:35:27 +0300188static doctest::String intTranslator(int ex) {
189 return doctest::String("int: ") + doctest::toString(ex);
190}
onqtam246e8172017-03-17 02:48:12 +0200191
onqtam05bcc372017-03-17 02:10:38 +0200192TEST_CASE("a test case that registers an exception translator for int and then throws one") {
193 // set an exception translator for int - note that this shouldn't be done in a test case but
onqtam7cc0e962017-04-17 23:30:36 +0300194 // in main() or somewhere before executing the tests - but here I'm just lazy...
onqtam246e8172017-03-17 02:48:12 +0200195 doctest::registerExceptionTranslator(intTranslator);
onqtam12d55982017-04-16 22:35:27 +0300196
onqtam7cc0e962017-04-17 23:30:36 +0300197 throw_if(true, 5);
onqtam4a655632016-05-26 14:20:52 +0300198}