blob: c63383c11b3a561d4323b97d969de6f010166724 [file] [log] [blame]
onqtam4a655632016-05-26 14:20:52 +03001#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
2#include "doctest.h"
3
4#include <string>
5#include <vector>
6#include <list>
7
8#include <sstream>
9
10// the standard forbids writing in the std namespace but it works on all compilers
11namespace std
12{
13template <typename T>
14ostream& operator<<(ostream& stream, const vector<T>& in) {
15 stream << "[";
16 for(size_t i = 0; i < in.size(); ++i)
17 if(i < in.size() - 1)
18 stream << in[i] << ", ";
19 else
20 stream << in[i];
21 stream << "]";
22 return stream;
23}
24}
25
26// as an alternative you may write a specialization of doctest::StringMaker
27namespace doctest
28{
29template <typename T>
30struct StringMaker<std::list<T> >
31{
32 static String convert(const std::list<T>& in) {
33 std::ostringstream oss;
34
35 oss << "[";
36 for(typename std::list<T>::const_iterator it = in.begin(); it != in.end(); ++it)
37 oss << *it << ", ";
38 oss << "]";
39
40 return oss.str().c_str();
41 }
42};
43}
44
45// to silence GCC warnings when inheriting from the class MyType which has no virtual destructor
46#if defined(__GNUC__) && !defined(__clang__)
47#pragma GCC diagnostic ignored "-Weffc++"
48#endif // __GNUC__
49
50template <typename T, typename K>
51struct MyType
52{
53 T one;
54 K two;
55};
56
57template <typename T>
58struct MyTypeInherited : MyType<T, float>
59{};
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
onqtamb18680d2016-11-15 14:08:56 +020072namespace Bar {
onqtam4a655632016-05-26 14:20:52 +030073struct Foo
74{};
onqtam4a655632016-05-26 14:20:52 +030075static bool operator==(const Foo&, const Foo&) { return false; }
76
onqtamb18680d2016-11-15 14:08:56 +020077doctest::String toString(const Foo&); // to silence -Wmissing-declarations
78// as a third option you may provide an overload of toString()
79doctest::String toString(const Foo&) {
onqtam4a655632016-05-26 14:20:52 +030080 return "Foo{}";
81}
onqtamb18680d2016-11-15 14:08:56 +020082} // namespace Bar
onqtam4a655632016-05-26 14:20:52 +030083
84TEST_CASE("the only test") {
85 MyTypeInherited<int> bla1;
86 bla1.one = 5;
87 bla1.two = 4.0f;
88 MyTypeInherited<int> bla2;
89 bla2.one = 5;
90 bla2.two = 6.0f;
91
onqtamb18680d2016-11-15 14:08:56 +020092 Bar::Foo f1;
93 Bar::Foo f2;
onqtam4a655632016-05-26 14:20:52 +030094 CHECK(f1 == f2);
95
96 // std::string already has an operator<< working with std::ostream
97 std::string dummy1 = "omg";
98 std::string dummy2 = "tralala";
99
100 CHECK(dummy1 == dummy2);
101
102 std::vector<int> vec1;
103 vec1.push_back(1);
104 vec1.push_back(2);
105 vec1.push_back(3);
106
107 std::vector<int> vec2;
108 vec2.push_back(1);
109 vec2.push_back(2);
110 vec2.push_back(4);
111
112 CHECK(vec1 == vec2);
113
114 std::list<int> lst_1;
115 lst_1.push_back(1);
116 lst_1.push_back(42);
117 lst_1.push_back(3);
118
119 std::list<int> lst_2;
120 lst_2.push_back(1);
121 lst_2.push_back(2);
122 lst_2.push_back(666);
123
124 CHECK(lst_1 == lst_2);
125}