ncihnegn | c5458f2 | 2019-01-28 05:08:18 -0800 | [diff] [blame] | 1 | #include <doctest/doctest.h> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 2 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 3 | #include "header.h" |
| 4 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 5 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
| 8 | #include <list> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 9 | #include <sstream> |
Stefan | 58f3ec8 | 2022-01-13 21:03:37 +0100 | [diff] [blame] | 10 | #include <limits> |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 11 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 12 | |
onqtam | 190ba25 | 2019-03-23 09:58:34 +0200 | [diff] [blame] | 13 | DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics |
| 14 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 15 | // the standard forbids writing in the std namespace but it works on all compilers |
| 16 | namespace std |
| 17 | { |
| 18 | template <typename T> |
| 19 | ostream& 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 |
| 32 | namespace doctest |
| 33 | { |
| 34 | template <typename T> |
| 35 | struct 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 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 50 | template <typename T, typename K> |
| 51 | struct MyType |
| 52 | { |
| 53 | T one; |
| 54 | K two; |
| 55 | }; |
| 56 | |
| 57 | template <typename T> |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 58 | struct MyTypeInherited : MyType<T, unsigned> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 59 | {}; |
| 60 | |
| 61 | template <typename T, typename K> |
| 62 | bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) { |
| 63 | return lhs.one == rhs.one && lhs.two == rhs.two; |
| 64 | } |
| 65 | |
| 66 | template <typename T, typename K> |
| 67 | std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) { |
| 68 | stream << "[" << in.one << ", " << in.two << "]"; |
| 69 | return stream; |
| 70 | } |
| 71 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 72 | namespace Bar |
| 73 | { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 74 | struct Foo |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 75 | { |
| 76 | friend bool operator==(const Foo&, const Foo&) { return false; } |
| 77 | }; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 78 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 79 | // as a third option you may provide an overload of toString() |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 80 | inline doctest::String toString(const Foo&) { return "Foo{}"; } |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 81 | |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 82 | struct MyOtherType |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 83 | { |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 84 | int data; |
| 85 | friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; } |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 88 | // you also can use a template operator<< if your code does not use std::ostream |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 89 | template <class OStream> |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 90 | OStream& operator<<(OStream& stream, const MyOtherType& in) { |
| 91 | stream << "MyOtherType: " << in.data; |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 92 | return stream; |
| 93 | } |
| 94 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 95 | } // namespace Bar |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 96 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 97 | // set an exception translator for MyTypeInherited<int> |
| 98 | REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) { |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 99 | return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " + |
| 100 | doctest::toString(ex.two) + ")"; |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 101 | } |
| 102 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 103 | TEST_CASE("all asserts should fail and show how the objects get stringified") { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 104 | MyTypeInherited<int> bla1; |
| 105 | bla1.one = 5; |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 106 | bla1.two = 4u; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 107 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 108 | Bar::Foo f1; |
| 109 | Bar::Foo f2; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 110 | CHECK(f1 == f2); |
| 111 | |
| 112 | // std::string already has an operator<< working with std::ostream |
onqtam | 367098b | 2021-12-15 15:15:06 +0200 | [diff] [blame] | 113 | std::string dummy = "omg"; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 114 | |
onqtam | 367098b | 2021-12-15 15:15:06 +0200 | [diff] [blame] | 115 | CHECK(dummy == "tralala"); // should fail |
| 116 | CHECK("tralala" == dummy); // should fail |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 117 | |
| 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); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 141 | |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 142 | { |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 143 | Bar::MyOtherType s1 {42}; |
| 144 | Bar::MyOtherType s2 {666}; |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 145 | INFO("s1=", s1, " s2=", s2); |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 146 | CHECK(s1 == s2); |
| 147 | CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2); |
| 148 | } |
| 149 | |
Stefan | 58f3ec8 | 2022-01-13 21:03:37 +0100 | [diff] [blame] | 150 | 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 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 154 | // lets see if this exception gets translated |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 155 | throw_if(true, bla1); |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 156 | } |
| 157 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 158 | static doctest::String intTranslator(int ex) { |
| 159 | return doctest::String("int: ") + doctest::toString(ex); |
| 160 | } |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 161 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 162 | TEST_CASE("a test case that registers an exception translator for int and then throws one") { |
| 163 | // set an exception translator for int - note that this shouldn't be done in a test case but |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 164 | // in main() or somewhere before executing the tests - but here I'm just lazy... |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 165 | doctest::registerExceptionTranslator(intTranslator); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 166 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 167 | throw_if(true, 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 168 | } |