blob: 2add560deffc910fb253ab1026ce8a28e7a8e792 [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
onqtamabf39d22017-10-28 21:30:45 +03005DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
onqtam4a655632016-05-26 14:20:52 +03006#include <string>
7#include <vector>
8#include <list>
onqtam4a655632016-05-26 14:20:52 +03009#include <sstream>
Stefan58f3ec82022-01-13 21:03:37 +010010#include <limits>
onqtamabf39d22017-10-28 21:30:45 +030011DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
onqtam4a655632016-05-26 14:20:52 +030012
onqtam190ba252019-03-23 09:58:34 +020013DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics
14
onqtam4a655632016-05-26 14:20:52 +030015// the standard forbids writing in the std namespace but it works on all compilers
16namespace std
17{
18template <typename T>
19ostream& operator<<(ostream& stream, const vector<T>& in) {
20 stream << "[";
21 for(size_t i = 0; i < in.size(); ++i)
22 if(i < in.size() - 1)
23 stream << in[i] << ", ";
24 else
25 stream << in[i];
26 stream << "]";
27 return stream;
28}
29}
30
31// as an alternative you may write a specialization of doctest::StringMaker
32namespace doctest
33{
34template <typename T>
35struct StringMaker<std::list<T> >
36{
37 static String convert(const std::list<T>& in) {
38 std::ostringstream oss;
39
40 oss << "[";
41 for(typename std::list<T>::const_iterator it = in.begin(); it != in.end(); ++it)
42 oss << *it << ", ";
43 oss << "]";
44
45 return oss.str().c_str();
46 }
47};
48}
49
onqtam4a655632016-05-26 14:20:52 +030050template <typename T, typename K>
51struct MyType
52{
53 T one;
54 K two;
55};
56
57template <typename T>
onqtam02b7eb72017-10-28 23:59:49 +030058struct MyTypeInherited : MyType<T, unsigned>
onqtam4a655632016-05-26 14:20:52 +030059{};
60
61template <typename T, typename K>
62bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) {
63 return lhs.one == rhs.one && lhs.two == rhs.two;
64}
65
66template <typename T, typename K>
67std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) {
68 stream << "[" << in.one << ", " << in.two << "]";
69 return stream;
70}
71
onqtam12d55982017-04-16 22:35:27 +030072namespace Bar
73{
onqtam4a655632016-05-26 14:20:52 +030074struct Foo
onqtam12d55982017-04-16 22:35:27 +030075{
76 friend bool operator==(const Foo&, const Foo&) { return false; }
77};
onqtam4a655632016-05-26 14:20:52 +030078
onqtamb18680d2016-11-15 14:08:56 +020079// as a third option you may provide an overload of toString()
onqtam7cc0e962017-04-17 23:30:36 +030080inline doctest::String toString(const Foo&) { return "Foo{}"; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +020081
Viktor Kirilov81fe2e82020-12-04 18:59:26 +020082struct MyOtherType
Viktor Kirilov17d984c2020-12-04 15:13:33 +020083{
Viktor Kirilov81fe2e82020-12-04 18:59:26 +020084 int data;
85 friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; }
Viktor Kirilov17d984c2020-12-04 15:13:33 +020086};
87
Viktor Kirilov81fe2e82020-12-04 18:59:26 +020088// you also can use a template operator<< if your code does not use std::ostream
Viktor Kirilov17d984c2020-12-04 15:13:33 +020089template <class OStream>
Viktor Kirilov81fe2e82020-12-04 18:59:26 +020090OStream& operator<<(OStream& stream, const MyOtherType& in) {
91 stream << "MyOtherType: " << in.data;
Viktor Kirilov17d984c2020-12-04 15:13:33 +020092 return stream;
93}
94
onqtamb18680d2016-11-15 14:08:56 +020095} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +030096
onqtam05bcc372017-03-17 02:10:38 +020097// set an exception translator for MyTypeInherited<int>
98REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) {
onqtam12d55982017-04-16 22:35:27 +030099 return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " +
100 doctest::toString(ex.two) + ")";
onqtam05bcc372017-03-17 02:10:38 +0200101}
102
onqtam7cc0e962017-04-17 23:30:36 +0300103TEST_CASE("all asserts should fail and show how the objects get stringified") {
onqtam4a655632016-05-26 14:20:52 +0300104 MyTypeInherited<int> bla1;
105 bla1.one = 5;
onqtam02b7eb72017-10-28 23:59:49 +0300106 bla1.two = 4u;
onqtam4a655632016-05-26 14:20:52 +0300107
onqtamb18680d2016-11-15 14:08:56 +0200108 Bar::Foo f1;
109 Bar::Foo f2;
onqtam4a655632016-05-26 14:20:52 +0300110 CHECK(f1 == f2);
111
112 // std::string already has an operator<< working with std::ostream
onqtam367098b2021-12-15 15:15:06 +0200113 std::string dummy = "omg";
onqtam4a655632016-05-26 14:20:52 +0300114
onqtam367098b2021-12-15 15:15:06 +0200115 CHECK(dummy == "tralala"); // should fail
116 CHECK("tralala" == dummy); // should fail
onqtam4a655632016-05-26 14:20:52 +0300117
118 std::vector<int> vec1;
119 vec1.push_back(1);
120 vec1.push_back(2);
121 vec1.push_back(3);
122
123 std::vector<int> vec2;
124 vec2.push_back(1);
125 vec2.push_back(2);
126 vec2.push_back(4);
127
128 CHECK(vec1 == vec2);
129
130 std::list<int> lst_1;
131 lst_1.push_back(1);
132 lst_1.push_back(42);
133 lst_1.push_back(3);
134
135 std::list<int> lst_2;
136 lst_2.push_back(1);
137 lst_2.push_back(2);
138 lst_2.push_back(666);
139
140 CHECK(lst_1 == lst_2);
onqtam12d55982017-04-16 22:35:27 +0300141
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200142 {
Viktor Kirilov81fe2e82020-12-04 18:59:26 +0200143 Bar::MyOtherType s1 {42};
144 Bar::MyOtherType s2 {666};
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200145 INFO("s1=", s1, " s2=", s2);
Viktor Kirilov17d984c2020-12-04 15:13:33 +0200146 CHECK(s1 == s2);
147 CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2);
148 }
149
Stefan58f3ec82022-01-13 21:03:37 +0100150 CHECK(doctest::IsNaN<double>(0.5));
151 CHECK(doctest::IsNaN<float>(std::numeric_limits<float>::infinity()));
152 // can't test actual nan because it's implementation defined
153
Menno Fraters8de4cf72022-03-05 23:14:35 +0000154 CHECK("a" == doctest::Contains("aaa"));
155
onqtam05bcc372017-03-17 02:10:38 +0200156 // lets see if this exception gets translated
onqtam7cc0e962017-04-17 23:30:36 +0300157 throw_if(true, bla1);
onqtam05bcc372017-03-17 02:10:38 +0200158}
159
onqtam12d55982017-04-16 22:35:27 +0300160static doctest::String intTranslator(int ex) {
161 return doctest::String("int: ") + doctest::toString(ex);
162}
onqtam246e8172017-03-17 02:48:12 +0200163
onqtam05bcc372017-03-17 02:10:38 +0200164TEST_CASE("a test case that registers an exception translator for int and then throws one") {
165 // set an exception translator for int - note that this shouldn't be done in a test case but
onqtam7cc0e962017-04-17 23:30:36 +0300166 // in main() or somewhere before executing the tests - but here I'm just lazy...
onqtam246e8172017-03-17 02:48:12 +0200167 doctest::registerExceptionTranslator(intTranslator);
onqtam12d55982017-04-16 22:35:27 +0300168
onqtam7cc0e962017-04-17 23:30:36 +0300169 throw_if(true, 5);
onqtam4a655632016-05-26 14:20:52 +0300170}