blob: aa3167d8e53a9d6defece7178fd4783093d1596b [file] [log] [blame]
Stefane8ba7712022-04-28 02:47:24 +02001#ifdef _MSC_VER
2__pragma(warning(push))
3__pragma(warning(disable : 4643))
4namespace std {
5 template <typename> struct char_traits;
6 template <typename, typename> class basic_ostream;
7 typedef basic_ostream<char, char_traits<char>> ostream;
8 template<class TRAITS>
9 basic_ostream<char, TRAITS>& operator<<(basic_ostream<char, TRAITS>&, const char*);
10}
11__pragma(warning(pop))
12#else
13#include <iostream>
14#endif
15
16namespace N {
17 struct A { };
18 struct B {
19 friend std::ostream& operator<<(std::ostream& os, const B&) { return os << "B"; }
20 };
21 struct C { };
22 static std::ostream& operator<<(std::ostream& os, const C&) { return os << "C"; }
23}
24
25static std::ostream& operator<<(std::ostream& os, const N::A&) { return os << "A"; }
26
ncihnegnc5458f22019-01-28 05:08:18 -080027#include <doctest/doctest.h>
onqtam4a655632016-05-26 14:20:52 +030028
Stefane8ba7712022-04-28 02:47:24 +020029#include <utility>
30
31TEST_CASE("operator<<") {
32 MESSAGE(N::A{ });
33 MESSAGE(N::B{ });
34 MESSAGE(N::C{ });
35}
36
onqtam7cc0e962017-04-17 23:30:36 +030037#include "header.h"
38
Stefane8ba7712022-04-28 02:47:24 +020039// std::move is broken with VS <= 15
40#if defined(_MSC_VER) && _MSC_VER <= 1900
41#define MOVE(...) __VA_ARGS__
42#else
43#define MOVE std::move
44#endif
45
46TEST_CASE("no headers") {
47 char chs[] = { '1', 'a', 's' };
48 MESSAGE(chs); CHECK(chs == nullptr);
49 MESSAGE("1as"); CHECK("1as" == nullptr);
50
51 int ints[] = { 0, 1, 1, 2, 3, 5, 8, 13 };
52 MESSAGE(ints); CHECK(ints == nullptr);
53 MESSAGE(MOVE(ints));
54
55 char* cptr = reinterpret_cast<char*>(ints + 4);
56 const char* ccptr = const_cast<const char*>(cptr);
57 void* vptr = reinterpret_cast<void*>(cptr);
58 CHECK(doctest::toString(cptr) == doctest::toString(ccptr));
59 CHECK(doctest::toString(ccptr) == doctest::toString(vptr));
60
61 char* cnptr = nullptr;
62 MESSAGE(cnptr); CHECK(cnptr != nullptr);
63
64 enum Test {
65 A = 0, B, C = 100,
66 };
67 MESSAGE(A); CHECK(A == C);
68
69 MESSAGE(doctest::toString<int>());
70}
71
onqtamabf39d22017-10-28 21:30:45 +030072DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam4a655632016-05-26 14:20:52 +030073#include <string>
74#include <vector>
75#include <list>
onqtam4a655632016-05-26 14:20:52 +030076#include <sstream>
Stefan58f3ec82022-01-13 21:03:37 +010077#include <limits>
onqtamabf39d22017-10-28 21:30:45 +030078DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam4a655632016-05-26 14:20:52 +030079
onqtam190ba252019-03-23 09:58:34 +020080DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics
81
onqtam4a655632016-05-26 14:20:52 +030082// the standard forbids writing in the std namespace but it works on all compilers
83namespace std
84{
85template <typename T>
86ostream& operator<<(ostream& stream, const vector<T>& in) {
87 stream << "[";
Stefane8ba7712022-04-28 02:47:24 +020088 for (size_t i = 0; i < in.size(); ++i) {
89 if (i != 0) { stream << ", "; }
90 stream << in[i];
91 }
onqtam4a655632016-05-26 14:20:52 +030092 stream << "]";
93 return stream;
94}
95}
96
97// as an alternative you may write a specialization of doctest::StringMaker
98namespace doctest
99{
100template <typename T>
Stefane8ba7712022-04-28 02:47:24 +0200101struct StringMaker<std::list<T>>
onqtam4a655632016-05-26 14:20:52 +0300102{
103 static String convert(const std::list<T>& in) {
104 std::ostringstream oss;
105
106 oss << "[";
Stefane8ba7712022-04-28 02:47:24 +0200107 for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) {
108 oss << *it;
109 if (++it != in.end()) { oss << ", "; }
110 }
onqtam4a655632016-05-26 14:20:52 +0300111 oss << "]";
onqtam4a655632016-05-26 14:20:52 +0300112 return oss.str().c_str();
113 }
114};
115}
116
onqtam4a655632016-05-26 14:20:52 +0300117template <typename T, typename K>
118struct MyType
119{
120 T one;
121 K two;
122};
123
124template <typename T>
onqtam02b7eb72017-10-28 23:59:49 +0300125struct MyTypeInherited : MyType<T, unsigned>
onqtam4a655632016-05-26 14:20:52 +0300126{};
127
128template <typename T, typename K>
129bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) {
130 return lhs.one == rhs.one && lhs.two == rhs.two;
131}
132
133template <typename T, typename K>
134std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) {
135 stream << "[" << in.one << ", " << in.two << "]";
136 return stream;
137}
138
onqtam12d55982017-04-16 22:35:27 +0300139namespace Bar
140{
onqtam4a655632016-05-26 14:20:52 +0300141struct Foo
onqtam12d55982017-04-16 22:35:27 +0300142{
143 friend bool operator==(const Foo&, const Foo&) { return false; }
144};
onqtam4a655632016-05-26 14:20:52 +0300145
onqtamb18680d2016-11-15 14:08:56 +0200146// as a third option you may provide an overload of toString()
onqtam7cc0e962017-04-17 23:30:36 +0300147inline doctest::String toString(const Foo&) { return "Foo{}"; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200148
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200149struct MyOtherType
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200150{
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200151 int data;
152 friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200153};
154
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200155// you also can use a template operator<< if your code does not use std::ostream
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200156template <class OStream>
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200157OStream& operator<<(OStream& stream, const MyOtherType& in) {
158 stream << "MyOtherType: " << in.data;
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200159 return stream;
160}
161
onqtamb18680d2016-11-15 14:08:56 +0200162} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +0300163
onqtam05bcc372017-03-17 02:10:38 +0200164// set an exception translator for MyTypeInherited<int>
165REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) {
onqtam12d55982017-04-16 22:35:27 +0300166 return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " +
167 doctest::toString(ex.two) + ")";
onqtam05bcc372017-03-17 02:10:38 +0200168}
169
Stefane8ba7712022-04-28 02:47:24 +0200170#define CHECK_NOT_DEFAULT_STR(var) CHECK(toString(var) != "{?}")
171
onqtam7cc0e962017-04-17 23:30:36 +0300172TEST_CASE("all asserts should fail and show how the objects get stringified") {
onqtam4a655632016-05-26 14:20:52 +0300173 MyTypeInherited<int> bla1;
174 bla1.one = 5;
onqtam02b7eb72017-10-28 23:59:49 +0300175 bla1.two = 4u;
onqtam4a655632016-05-26 14:20:52 +0300176
onqtamb18680d2016-11-15 14:08:56 +0200177 Bar::Foo f1;
Stefane8ba7712022-04-28 02:47:24 +0200178 MESSAGE(f1);
onqtamb18680d2016-11-15 14:08:56 +0200179 Bar::Foo f2;
onqtam4a655632016-05-26 14:20:52 +0300180 CHECK(f1 == f2);
181
182 // std::string already has an operator<< working with std::ostream
onqtam367098b2021-12-15 15:15:06 +0200183 std::string dummy = "omg";
onqtam4a655632016-05-26 14:20:52 +0300184
Stefane8ba7712022-04-28 02:47:24 +0200185 MESSAGE(dummy);
186
onqtam367098b2021-12-15 15:15:06 +0200187 CHECK(dummy == "tralala"); // should fail
188 CHECK("tralala" == dummy); // should fail
onqtam4a655632016-05-26 14:20:52 +0300189
190 std::vector<int> vec1;
191 vec1.push_back(1);
192 vec1.push_back(2);
193 vec1.push_back(3);
194
Stefane8ba7712022-04-28 02:47:24 +0200195 MESSAGE(vec1);
196
onqtam4a655632016-05-26 14:20:52 +0300197 std::vector<int> vec2;
198 vec2.push_back(1);
199 vec2.push_back(2);
200 vec2.push_back(4);
201
202 CHECK(vec1 == vec2);
203
204 std::list<int> lst_1;
205 lst_1.push_back(1);
206 lst_1.push_back(42);
207 lst_1.push_back(3);
208
Stefane8ba7712022-04-28 02:47:24 +0200209 MESSAGE(lst_1);
210
onqtam4a655632016-05-26 14:20:52 +0300211 std::list<int> lst_2;
212 lst_2.push_back(1);
213 lst_2.push_back(2);
214 lst_2.push_back(666);
215
216 CHECK(lst_1 == lst_2);
onqtam12d55982017-04-16 22:35:27 +0300217
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200218 {
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200219 Bar::MyOtherType s1 {42};
220 Bar::MyOtherType s2 {666};
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200221 INFO("s1=", s1, " s2=", s2);
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200222 CHECK(s1 == s2);
223 CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2);
224 }
225
Stefane8ba7712022-04-28 02:47:24 +0200226 CHECK_NOT_DEFAULT_STR(doctest::IsNaN<double>(0.5));
227 CHECK_NOT_DEFAULT_STR(!doctest::IsNaN<float>(std::numeric_limits<float>::infinity()));
228 CHECK_NOT_DEFAULT_STR(doctest::IsNaN<double long>(std::numeric_limits<double long>::quiet_NaN()));
Stefan58f3ec82022-01-13 21:03:37 +0100229
Menno Fraters8de4cf72022-03-05 23:14:35 +0000230 CHECK("a" == doctest::Contains("aaa"));
231
onqtam05bcc372017-03-17 02:10:38 +0200232 // lets see if this exception gets translated
onqtam7cc0e962017-04-17 23:30:36 +0300233 throw_if(true, bla1);
onqtam05bcc372017-03-17 02:10:38 +0200234}
235
onqtam12d55982017-04-16 22:35:27 +0300236static doctest::String intTranslator(int ex) {
237 return doctest::String("int: ") + doctest::toString(ex);
238}
onqtam246e8172017-03-17 02:48:12 +0200239
onqtam05bcc372017-03-17 02:10:38 +0200240TEST_CASE("a test case that registers an exception translator for int and then throws one") {
241 // set an exception translator for int - note that this shouldn't be done in a test case but
onqtam7cc0e962017-04-17 23:30:36 +0300242 // in main() or somewhere before executing the tests - but here I'm just lazy...
onqtam246e8172017-03-17 02:48:12 +0200243 doctest::registerExceptionTranslator(intTranslator);
onqtam12d55982017-04-16 22:35:27 +0300244
onqtam7cc0e962017-04-17 23:30:36 +0300245 throw_if(true, 5);
onqtam4a655632016-05-26 14:20:52 +0300246}