blob: 8566e5a401206704f78e0e76de67dc4df11d332f [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
Salvage996ef322022-01-18 05:45:52 +010021 char* cnptr = nullptr;
22 MESSAGE(cnptr); CHECK(cnptr != nullptr);
23
Salvage7be3b872022-01-13 05:08:34 +010024 enum Test {
25 A = 0, B, C = 100,
26 };
27 MESSAGE(A); CHECK(A == C);
Salvage6545bdc2022-01-13 00:14:21 +010028}
29
onqtamabf39d22017-10-28 21:30:45 +030030DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam4a655632016-05-26 14:20:52 +030031#include <string>
32#include <vector>
33#include <list>
onqtam4a655632016-05-26 14:20:52 +030034#include <sstream>
Salvage2fc3d4e2022-01-12 16:23:19 +010035#include <limits>
onqtamabf39d22017-10-28 21:30:45 +030036DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam4a655632016-05-26 14:20:52 +030037
onqtam190ba252019-03-23 09:58:34 +020038DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics
39
onqtam4a655632016-05-26 14:20:52 +030040// the standard forbids writing in the std namespace but it works on all compilers
41namespace std
42{
43template <typename T>
44ostream& operator<<(ostream& stream, const vector<T>& in) {
45 stream << "[";
Salvage6545bdc2022-01-13 00:14:21 +010046 for (size_t i = 0; i < in.size(); ++i) {
47 if (i != 0) { stream << ", "; }
48 stream << in[i];
49 }
onqtam4a655632016-05-26 14:20:52 +030050 stream << "]";
51 return stream;
52}
53}
54
55// as an alternative you may write a specialization of doctest::StringMaker
Salvage098e17a2022-01-13 20:02:07 +010056namespace doctest
onqtam4a655632016-05-26 14:20:52 +030057{
Salvage098e17a2022-01-13 20:02:07 +010058template <typename T>
59struct StringMaker<std::list<T>>
60{
61 static String convert(const std::list<T>& in) {
62 std::ostringstream oss;
63
64 oss << "[";
Salvage6545bdc2022-01-13 00:14:21 +010065 for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) {
Salvage098e17a2022-01-13 20:02:07 +010066 oss << *it;
67 if (++it != in.end()) { oss << ", "; }
Salvage6545bdc2022-01-13 00:14:21 +010068 }
Salvage098e17a2022-01-13 20:02:07 +010069 oss << "]";
70 return oss.str().c_str();
onqtam4a655632016-05-26 14:20:52 +030071 }
72};
Salvage098e17a2022-01-13 20:02:07 +010073}
onqtam4a655632016-05-26 14:20:52 +030074
onqtam4a655632016-05-26 14:20:52 +030075template <typename T, typename K>
76struct MyType
77{
78 T one;
79 K two;
80};
81
82template <typename T>
onqtam02b7eb72017-10-28 23:59:49 +030083struct MyTypeInherited : MyType<T, unsigned>
onqtam4a655632016-05-26 14:20:52 +030084{};
85
86template <typename T, typename K>
87bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) {
88 return lhs.one == rhs.one && lhs.two == rhs.two;
89}
90
91template <typename T, typename K>
92std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) {
93 stream << "[" << in.one << ", " << in.two << "]";
94 return stream;
95}
96
onqtam12d55982017-04-16 22:35:27 +030097namespace Bar
98{
onqtam4a655632016-05-26 14:20:52 +030099struct Foo
onqtam12d55982017-04-16 22:35:27 +0300100{
101 friend bool operator==(const Foo&, const Foo&) { return false; }
102};
onqtam4a655632016-05-26 14:20:52 +0300103
Salvage098e17a2022-01-13 20:02:07 +0100104// as a third option you may provide an overload of toString()
105inline doctest::String toString(const Foo&) { return "Foo{}"; }
106
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200107struct MyOtherType
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200108{
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200109 int data;
110 friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200111};
112
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200113// you also can use a template operator<< if your code does not use std::ostream
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200114template <class OStream>
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200115OStream& operator<<(OStream& stream, const MyOtherType& in) {
116 stream << "MyOtherType: " << in.data;
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200117 return stream;
118}
119
onqtamb18680d2016-11-15 14:08:56 +0200120} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +0300121
onqtam05bcc372017-03-17 02:10:38 +0200122// set an exception translator for MyTypeInherited<int>
123REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) {
onqtam12d55982017-04-16 22:35:27 +0300124 return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " +
125 doctest::toString(ex.two) + ")";
onqtam05bcc372017-03-17 02:10:38 +0200126}
127
onqtam7cc0e962017-04-17 23:30:36 +0300128TEST_CASE("all asserts should fail and show how the objects get stringified") {
onqtam4a655632016-05-26 14:20:52 +0300129 MyTypeInherited<int> bla1;
130 bla1.one = 5;
onqtam02b7eb72017-10-28 23:59:49 +0300131 bla1.two = 4u;
onqtam4a655632016-05-26 14:20:52 +0300132
onqtamb18680d2016-11-15 14:08:56 +0200133 Bar::Foo f1;
Salvage6545bdc2022-01-13 00:14:21 +0100134 MESSAGE(f1);
onqtamb18680d2016-11-15 14:08:56 +0200135 Bar::Foo f2;
onqtam4a655632016-05-26 14:20:52 +0300136 CHECK(f1 == f2);
137
138 // std::string already has an operator<< working with std::ostream
onqtam367098b2021-12-15 15:15:06 +0200139 std::string dummy = "omg";
onqtam4a655632016-05-26 14:20:52 +0300140
Salvage6545bdc2022-01-13 00:14:21 +0100141 MESSAGE(dummy);
142
onqtam367098b2021-12-15 15:15:06 +0200143 CHECK(dummy == "tralala"); // should fail
144 CHECK("tralala" == dummy); // should fail
onqtam4a655632016-05-26 14:20:52 +0300145
146 std::vector<int> vec1;
147 vec1.push_back(1);
148 vec1.push_back(2);
149 vec1.push_back(3);
150
Salvage6545bdc2022-01-13 00:14:21 +0100151 MESSAGE(vec1);
152
onqtam4a655632016-05-26 14:20:52 +0300153 std::vector<int> vec2;
154 vec2.push_back(1);
155 vec2.push_back(2);
156 vec2.push_back(4);
157
158 CHECK(vec1 == vec2);
159
160 std::list<int> lst_1;
161 lst_1.push_back(1);
162 lst_1.push_back(42);
163 lst_1.push_back(3);
164
Salvage6545bdc2022-01-13 00:14:21 +0100165 MESSAGE(lst_1);
166
onqtam4a655632016-05-26 14:20:52 +0300167 std::list<int> lst_2;
168 lst_2.push_back(1);
169 lst_2.push_back(2);
170 lst_2.push_back(666);
171
172 CHECK(lst_1 == lst_2);
onqtam12d55982017-04-16 22:35:27 +0300173
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200174 {
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200175 Bar::MyOtherType s1 {42};
176 Bar::MyOtherType s2 {666};
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200177 INFO("s1=", s1, " s2=", s2);
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200178 CHECK(s1 == s2);
179 CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2);
180 }
181
Salvage4ddb7932022-01-12 16:17:39 +0100182 CHECK(doctest::IsNaN<double>(0.5));
183 CHECK(doctest::IsNaN<float>(std::numeric_limits<float>::infinity()));
Salvage84c509b2022-01-12 21:52:07 +0100184 // can't test actual nan because it's implementation defined
Salvage245d4b22022-01-12 16:04:16 +0100185
onqtam05bcc372017-03-17 02:10:38 +0200186 // lets see if this exception gets translated
onqtam7cc0e962017-04-17 23:30:36 +0300187 throw_if(true, bla1);
onqtam05bcc372017-03-17 02:10:38 +0200188}
189
onqtam12d55982017-04-16 22:35:27 +0300190static doctest::String intTranslator(int ex) {
191 return doctest::String("int: ") + doctest::toString(ex);
192}
onqtam246e8172017-03-17 02:48:12 +0200193
onqtam05bcc372017-03-17 02:10:38 +0200194TEST_CASE("a test case that registers an exception translator for int and then throws one") {
195 // set an exception translator for int - note that this shouldn't be done in a test case but
onqtam7cc0e962017-04-17 23:30:36 +0300196 // in main() or somewhere before executing the tests - but here I'm just lazy...
onqtam246e8172017-03-17 02:48:12 +0200197 doctest::registerExceptionTranslator(intTranslator);
onqtam12d55982017-04-16 22:35:27 +0300198
onqtam7cc0e962017-04-17 23:30:36 +0300199 throw_if(true, 5);
onqtam4a655632016-05-26 14:20:52 +0300200}