blob: a217ac105cc5dd3e827966740d87ba78988fa651 [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
72struct Foo
73{};
74
75static bool operator==(const Foo&, const Foo&) { return false; }
76
77// as a third option you may specialize the doctest::toString() template function
78namespace doctest
79{
80template <>
81String toString(const Foo&) {
82 return "Foo{}";
83}
84}
85
86TEST_CASE("the only test") {
87 MyTypeInherited<int> bla1;
88 bla1.one = 5;
89 bla1.two = 4.0f;
90 MyTypeInherited<int> bla2;
91 bla2.one = 5;
92 bla2.two = 6.0f;
93
94 Foo f1;
95 Foo f2;
96 CHECK(f1 == f2);
97
98 // std::string already has an operator<< working with std::ostream
99 std::string dummy1 = "omg";
100 std::string dummy2 = "tralala";
101
102 CHECK(dummy1 == dummy2);
103
104 std::vector<int> vec1;
105 vec1.push_back(1);
106 vec1.push_back(2);
107 vec1.push_back(3);
108
109 std::vector<int> vec2;
110 vec2.push_back(1);
111 vec2.push_back(2);
112 vec2.push_back(4);
113
114 CHECK(vec1 == vec2);
115
116 std::list<int> lst_1;
117 lst_1.push_back(1);
118 lst_1.push_back(42);
119 lst_1.push_back(3);
120
121 std::list<int> lst_2;
122 lst_2.push_back(1);
123 lst_2.push_back(2);
124 lst_2.push_back(666);
125
126 CHECK(lst_1 == lst_2);
127}