blob: 999d39849b3282c3c11bc1d379d558955c8c0e9d [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") {
Salvage4bc7a752022-01-18 05:35:57 +01006 char chs[] = { '1', 'a', 's' };
7 MESSAGE(chs); CHECK(chs == nullptr);
Salvage6545bdc2022-01-13 00:14:21 +01008 MESSAGE("1as"); CHECK("1as" == nullptr);
9
10 int ints[] = { 0, 1, 1, 2, 3, 5, 8, 13 };
11 MESSAGE(ints); CHECK(ints == nullptr);
Salvage7be3b872022-01-13 05:08:34 +010012
Salvage3f3511c2022-01-13 18:18:52 +010013 char* cptr = reinterpret_cast<char*>(ints + 4);
Salvage4bc7a752022-01-18 05:35:57 +010014 const char* ccptr = const_cast<const char*>(cptr);
15 void* vptr = reinterpret_cast<void*>(cptr);
16 CHECK(doctest::toString(cptr) == doctest::toString(ccptr));
17 CHECK(doctest::toString(ccptr) == doctest::toString(vptr));
18 doctest::String str = doctest::toString(cptr);
19 CHECK(str[0] == '0'); CHECK(str[1] == 'x');
Salvage7be3b872022-01-13 05:08:34 +010020
21 enum Test {
22 A = 0, B, C = 100,
23 };
24 MESSAGE(A); CHECK(A == C);
Salvage6545bdc2022-01-13 00:14:21 +010025}
26
onqtamabf39d22017-10-28 21:30:45 +030027DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam4a655632016-05-26 14:20:52 +030028#include <string>
29#include <vector>
30#include <list>
onqtam4a655632016-05-26 14:20:52 +030031#include <sstream>
Salvage2fc3d4e2022-01-12 16:23:19 +010032#include <limits>
onqtamabf39d22017-10-28 21:30:45 +030033DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam4a655632016-05-26 14:20:52 +030034
onqtam190ba252019-03-23 09:58:34 +020035DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics
36
onqtam4a655632016-05-26 14:20:52 +030037// the standard forbids writing in the std namespace but it works on all compilers
38namespace std
39{
40template <typename T>
41ostream& operator<<(ostream& stream, const vector<T>& in) {
42 stream << "[";
Salvage6545bdc2022-01-13 00:14:21 +010043 for (size_t i = 0; i < in.size(); ++i) {
44 if (i != 0) { stream << ", "; }
45 stream << in[i];
46 }
onqtam4a655632016-05-26 14:20:52 +030047 stream << "]";
48 return stream;
49}
50}
51
52// as an alternative you may write a specialization of doctest::StringMaker
Salvage098e17a2022-01-13 20:02:07 +010053namespace doctest
onqtam4a655632016-05-26 14:20:52 +030054{
Salvage098e17a2022-01-13 20:02:07 +010055template <typename T>
56struct StringMaker<std::list<T>>
57{
58 static String convert(const std::list<T>& in) {
59 std::ostringstream oss;
60
61 oss << "[";
Salvage6545bdc2022-01-13 00:14:21 +010062 for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) {
Salvage098e17a2022-01-13 20:02:07 +010063 oss << *it;
64 if (++it != in.end()) { oss << ", "; }
Salvage6545bdc2022-01-13 00:14:21 +010065 }
Salvage098e17a2022-01-13 20:02:07 +010066 oss << "]";
67 return oss.str().c_str();
onqtam4a655632016-05-26 14:20:52 +030068 }
69};
Salvage098e17a2022-01-13 20:02:07 +010070}
onqtam4a655632016-05-26 14:20:52 +030071
onqtam4a655632016-05-26 14:20:52 +030072template <typename T, typename K>
73struct MyType
74{
75 T one;
76 K two;
77};
78
79template <typename T>
onqtam02b7eb72017-10-28 23:59:49 +030080struct MyTypeInherited : MyType<T, unsigned>
onqtam4a655632016-05-26 14:20:52 +030081{};
82
83template <typename T, typename K>
84bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) {
85 return lhs.one == rhs.one && lhs.two == rhs.two;
86}
87
88template <typename T, typename K>
89std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) {
90 stream << "[" << in.one << ", " << in.two << "]";
91 return stream;
92}
93
onqtam12d55982017-04-16 22:35:27 +030094namespace Bar
95{
onqtam4a655632016-05-26 14:20:52 +030096struct Foo
onqtam12d55982017-04-16 22:35:27 +030097{
98 friend bool operator==(const Foo&, const Foo&) { return false; }
99};
onqtam4a655632016-05-26 14:20:52 +0300100
Salvage098e17a2022-01-13 20:02:07 +0100101// as a third option you may provide an overload of toString()
102inline doctest::String toString(const Foo&) { return "Foo{}"; }
103
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200104struct MyOtherType
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200105{
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200106 int data;
107 friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200108};
109
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200110// you also can use a template operator<< if your code does not use std::ostream
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200111template <class OStream>
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200112OStream& operator<<(OStream& stream, const MyOtherType& in) {
113 stream << "MyOtherType: " << in.data;
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200114 return stream;
115}
116
onqtamb18680d2016-11-15 14:08:56 +0200117} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +0300118
onqtam05bcc372017-03-17 02:10:38 +0200119// set an exception translator for MyTypeInherited<int>
120REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) {
onqtam12d55982017-04-16 22:35:27 +0300121 return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " +
122 doctest::toString(ex.two) + ")";
onqtam05bcc372017-03-17 02:10:38 +0200123}
124
onqtam7cc0e962017-04-17 23:30:36 +0300125TEST_CASE("all asserts should fail and show how the objects get stringified") {
onqtam4a655632016-05-26 14:20:52 +0300126 MyTypeInherited<int> bla1;
127 bla1.one = 5;
onqtam02b7eb72017-10-28 23:59:49 +0300128 bla1.two = 4u;
onqtam4a655632016-05-26 14:20:52 +0300129
onqtamb18680d2016-11-15 14:08:56 +0200130 Bar::Foo f1;
Salvage6545bdc2022-01-13 00:14:21 +0100131 MESSAGE(f1);
onqtamb18680d2016-11-15 14:08:56 +0200132 Bar::Foo f2;
onqtam4a655632016-05-26 14:20:52 +0300133 CHECK(f1 == f2);
134
135 // std::string already has an operator<< working with std::ostream
onqtam367098b2021-12-15 15:15:06 +0200136 std::string dummy = "omg";
onqtam4a655632016-05-26 14:20:52 +0300137
Salvage6545bdc2022-01-13 00:14:21 +0100138 MESSAGE(dummy);
139
onqtam367098b2021-12-15 15:15:06 +0200140 CHECK(dummy == "tralala"); // should fail
141 CHECK("tralala" == dummy); // should fail
onqtam4a655632016-05-26 14:20:52 +0300142
143 std::vector<int> vec1;
144 vec1.push_back(1);
145 vec1.push_back(2);
146 vec1.push_back(3);
147
Salvage6545bdc2022-01-13 00:14:21 +0100148 MESSAGE(vec1);
149
onqtam4a655632016-05-26 14:20:52 +0300150 std::vector<int> vec2;
151 vec2.push_back(1);
152 vec2.push_back(2);
153 vec2.push_back(4);
154
155 CHECK(vec1 == vec2);
156
157 std::list<int> lst_1;
158 lst_1.push_back(1);
159 lst_1.push_back(42);
160 lst_1.push_back(3);
161
Salvage6545bdc2022-01-13 00:14:21 +0100162 MESSAGE(lst_1);
163
onqtam4a655632016-05-26 14:20:52 +0300164 std::list<int> lst_2;
165 lst_2.push_back(1);
166 lst_2.push_back(2);
167 lst_2.push_back(666);
168
169 CHECK(lst_1 == lst_2);
onqtam12d55982017-04-16 22:35:27 +0300170
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200171 {
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200172 Bar::MyOtherType s1 {42};
173 Bar::MyOtherType s2 {666};
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200174 INFO("s1=", s1, " s2=", s2);
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200175 CHECK(s1 == s2);
176 CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2);
177 }
178
Salvage4ddb7932022-01-12 16:17:39 +0100179 CHECK(doctest::IsNaN<double>(0.5));
180 CHECK(doctest::IsNaN<float>(std::numeric_limits<float>::infinity()));
Salvage84c509b2022-01-12 21:52:07 +0100181 // can't test actual nan because it's implementation defined
Salvage245d4b22022-01-12 16:04:16 +0100182
onqtam05bcc372017-03-17 02:10:38 +0200183 // lets see if this exception gets translated
onqtam7cc0e962017-04-17 23:30:36 +0300184 throw_if(true, bla1);
onqtam05bcc372017-03-17 02:10:38 +0200185}
186
onqtam12d55982017-04-16 22:35:27 +0300187static doctest::String intTranslator(int ex) {
188 return doctest::String("int: ") + doctest::toString(ex);
189}
onqtam246e8172017-03-17 02:48:12 +0200190
onqtam05bcc372017-03-17 02:10:38 +0200191TEST_CASE("a test case that registers an exception translator for int and then throws one") {
192 // set an exception translator for int - note that this shouldn't be done in a test case but
onqtam7cc0e962017-04-17 23:30:36 +0300193 // in main() or somewhere before executing the tests - but here I'm just lazy...
onqtam246e8172017-03-17 02:48:12 +0200194 doctest::registerExceptionTranslator(intTranslator);
onqtam12d55982017-04-16 22:35:27 +0300195
onqtam7cc0e962017-04-17 23:30:36 +0300196 throw_if(true, 5);
onqtam4a655632016-05-26 14:20:52 +0300197}