blob: 626e4866fc0f98041b1f1a4aea4c983c1d455d1f [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));
Salvage7be3b872022-01-13 05:08:34 +010018
Salvage996ef322022-01-18 05:45:52 +010019 char* cnptr = nullptr;
20 MESSAGE(cnptr); CHECK(cnptr != nullptr);
21
Salvage7be3b872022-01-13 05:08:34 +010022 enum Test {
23 A = 0, B, C = 100,
24 };
25 MESSAGE(A); CHECK(A == C);
Salvage6545bdc2022-01-13 00:14:21 +010026}
27
onqtamabf39d22017-10-28 21:30:45 +030028DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam4a655632016-05-26 14:20:52 +030029#include <string>
30#include <vector>
31#include <list>
onqtam4a655632016-05-26 14:20:52 +030032#include <sstream>
Salvage2fc3d4e2022-01-12 16:23:19 +010033#include <limits>
onqtamabf39d22017-10-28 21:30:45 +030034DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam4a655632016-05-26 14:20:52 +030035
onqtam190ba252019-03-23 09:58:34 +020036DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics
37
onqtam4a655632016-05-26 14:20:52 +030038// the standard forbids writing in the std namespace but it works on all compilers
39namespace std
40{
41template <typename T>
42ostream& operator<<(ostream& stream, const vector<T>& in) {
43 stream << "[";
Salvage6545bdc2022-01-13 00:14:21 +010044 for (size_t i = 0; i < in.size(); ++i) {
45 if (i != 0) { stream << ", "; }
46 stream << in[i];
47 }
onqtam4a655632016-05-26 14:20:52 +030048 stream << "]";
49 return stream;
50}
51}
52
53// as an alternative you may write a specialization of doctest::StringMaker
Salvage098e17a2022-01-13 20:02:07 +010054namespace doctest
onqtam4a655632016-05-26 14:20:52 +030055{
Salvage098e17a2022-01-13 20:02:07 +010056template <typename T>
57struct StringMaker<std::list<T>>
58{
59 static String convert(const std::list<T>& in) {
60 std::ostringstream oss;
61
62 oss << "[";
Salvage6545bdc2022-01-13 00:14:21 +010063 for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) {
Salvage098e17a2022-01-13 20:02:07 +010064 oss << *it;
65 if (++it != in.end()) { oss << ", "; }
Salvage6545bdc2022-01-13 00:14:21 +010066 }
Salvage098e17a2022-01-13 20:02:07 +010067 oss << "]";
68 return oss.str().c_str();
onqtam4a655632016-05-26 14:20:52 +030069 }
70};
Salvage098e17a2022-01-13 20:02:07 +010071}
onqtam4a655632016-05-26 14:20:52 +030072
onqtam4a655632016-05-26 14:20:52 +030073template <typename T, typename K>
74struct MyType
75{
76 T one;
77 K two;
78};
79
80template <typename T>
onqtam02b7eb72017-10-28 23:59:49 +030081struct MyTypeInherited : MyType<T, unsigned>
onqtam4a655632016-05-26 14:20:52 +030082{};
83
84template <typename T, typename K>
85bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) {
86 return lhs.one == rhs.one && lhs.two == rhs.two;
87}
88
89template <typename T, typename K>
90std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) {
91 stream << "[" << in.one << ", " << in.two << "]";
92 return stream;
93}
94
onqtam12d55982017-04-16 22:35:27 +030095namespace Bar
96{
onqtam4a655632016-05-26 14:20:52 +030097struct Foo
onqtam12d55982017-04-16 22:35:27 +030098{
99 friend bool operator==(const Foo&, const Foo&) { return false; }
100};
onqtam4a655632016-05-26 14:20:52 +0300101
Salvage098e17a2022-01-13 20:02:07 +0100102// as a third option you may provide an overload of toString()
103inline doctest::String toString(const Foo&) { return "Foo{}"; }
104
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200105struct MyOtherType
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200106{
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200107 int data;
108 friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200109};
110
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200111// you also can use a template operator<< if your code does not use std::ostream
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200112template <class OStream>
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200113OStream& operator<<(OStream& stream, const MyOtherType& in) {
114 stream << "MyOtherType: " << in.data;
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200115 return stream;
116}
117
onqtamb18680d2016-11-15 14:08:56 +0200118} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +0300119
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()));
Stefan8c43a522022-01-26 21:01:50 +0100182 CHECK(!doctest::IsNaN<double long>(std::numeric_limits<double long>::quiet_NaN()));
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}