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 | |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 5 | TEST_CASE("no headers") { |
| 6 | char chs1[] = { '1', 'a', 's' }; |
| 7 | MESSAGE(chs1); CHECK(chs1 == nullptr); |
| 8 | const char* chs2{"1as"}; |
| 9 | MESSAGE(chs2); CHECK(chs2 == nullptr); |
| 10 | MESSAGE("1as"); CHECK("1as" == nullptr); |
| 11 | |
| 12 | int ints[] = { 0, 1, 1, 2, 3, 5, 8, 13 }; |
| 13 | MESSAGE(ints); CHECK(ints == nullptr); |
Salvage | 7be3b87 | 2022-01-13 05:08:34 +0100 | [diff] [blame^] | 14 | |
| 15 | char* cptr = (char*)(ints + 4); |
| 16 | CHECK(cptr == nullptr); |
| 17 | |
| 18 | enum Test { |
| 19 | A = 0, B, C = 100, |
| 20 | }; |
| 21 | MESSAGE(A); CHECK(A == C); |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 22 | } |
| 23 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 24 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
| 27 | #include <list> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 28 | #include <sstream> |
Salvage | 2fc3d4e | 2022-01-12 16:23:19 +0100 | [diff] [blame] | 29 | #include <limits> |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 30 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 31 | |
onqtam | 190ba25 | 2019-03-23 09:58:34 +0200 | [diff] [blame] | 32 | DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics |
| 33 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 34 | // the standard forbids writing in the std namespace but it works on all compilers |
| 35 | namespace std |
| 36 | { |
| 37 | template <typename T> |
| 38 | ostream& operator<<(ostream& stream, const vector<T>& in) { |
| 39 | stream << "["; |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 40 | for (size_t i = 0; i < in.size(); ++i) { |
| 41 | if (i != 0) { stream << ", "; } |
| 42 | stream << in[i]; |
| 43 | } |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 44 | stream << "]"; |
| 45 | return stream; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // as an alternative you may write a specialization of doctest::StringMaker |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 50 | namespace doctest |
| 51 | { namespace detail { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 52 | template <typename T> |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 53 | struct StringStream<std::list<T>> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 54 | { |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 55 | static void convert(std::ostream* s, const std::list<T>& in) { |
| 56 | *s << "["; |
| 57 | for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) { |
| 58 | *s << *it; |
| 59 | if (++it != in.end()) { *s << ", "; } |
| 60 | } |
| 61 | *s << "]"; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 62 | } |
| 63 | }; |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 64 | } } |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 65 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 66 | template <typename T, typename K> |
| 67 | struct MyType |
| 68 | { |
| 69 | T one; |
| 70 | K two; |
| 71 | }; |
| 72 | |
| 73 | template <typename T> |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 74 | struct MyTypeInherited : MyType<T, unsigned> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 75 | {}; |
| 76 | |
| 77 | template <typename T, typename K> |
| 78 | bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) { |
| 79 | return lhs.one == rhs.one && lhs.two == rhs.two; |
| 80 | } |
| 81 | |
| 82 | template <typename T, typename K> |
| 83 | std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) { |
| 84 | stream << "[" << in.one << ", " << in.two << "]"; |
| 85 | return stream; |
| 86 | } |
| 87 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 88 | namespace Bar |
| 89 | { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 90 | struct Foo |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 91 | { |
| 92 | friend bool operator==(const Foo&, const Foo&) { return false; } |
| 93 | }; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 94 | |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 95 | struct MyOtherType |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 96 | { |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 97 | int data; |
| 98 | 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] | 99 | }; |
| 100 | |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 101 | // 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] | 102 | template <class OStream> |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 103 | OStream& operator<<(OStream& stream, const MyOtherType& in) { |
| 104 | stream << "MyOtherType: " << in.data; |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 105 | return stream; |
| 106 | } |
| 107 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 108 | } // namespace Bar |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 109 | |
Salvage | 0bd0889 | 2022-01-13 00:19:20 +0100 | [diff] [blame] | 110 | namespace doctest { |
| 111 | namespace detail { |
| 112 | template <> struct StringStream<Bar::Foo> { |
| 113 | static void convert(std::ostream* s, const Bar::Foo&) { |
| 114 | *s << "Foo{}"; |
| 115 | } |
| 116 | }; |
| 117 | } |
| 118 | } |
| 119 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 120 | // set an exception translator for MyTypeInherited<int> |
| 121 | REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) { |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 122 | return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " + |
| 123 | doctest::toString(ex.two) + ")"; |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 124 | } |
| 125 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 126 | TEST_CASE("all asserts should fail and show how the objects get stringified") { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 127 | MyTypeInherited<int> bla1; |
| 128 | bla1.one = 5; |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 129 | bla1.two = 4u; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 130 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 131 | Bar::Foo f1; |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 132 | MESSAGE(f1); |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 133 | Bar::Foo f2; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 134 | CHECK(f1 == f2); |
| 135 | |
| 136 | // std::string already has an operator<< working with std::ostream |
onqtam | 367098b | 2021-12-15 15:15:06 +0200 | [diff] [blame] | 137 | std::string dummy = "omg"; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 138 | |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 139 | MESSAGE(dummy); |
| 140 | |
onqtam | 367098b | 2021-12-15 15:15:06 +0200 | [diff] [blame] | 141 | CHECK(dummy == "tralala"); // should fail |
| 142 | CHECK("tralala" == dummy); // should fail |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 143 | |
| 144 | std::vector<int> vec1; |
| 145 | vec1.push_back(1); |
| 146 | vec1.push_back(2); |
| 147 | vec1.push_back(3); |
| 148 | |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 149 | MESSAGE(vec1); |
| 150 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 151 | std::vector<int> vec2; |
| 152 | vec2.push_back(1); |
| 153 | vec2.push_back(2); |
| 154 | vec2.push_back(4); |
| 155 | |
| 156 | CHECK(vec1 == vec2); |
| 157 | |
| 158 | std::list<int> lst_1; |
| 159 | lst_1.push_back(1); |
| 160 | lst_1.push_back(42); |
| 161 | lst_1.push_back(3); |
| 162 | |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 163 | MESSAGE(lst_1); |
| 164 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 165 | std::list<int> lst_2; |
| 166 | lst_2.push_back(1); |
| 167 | lst_2.push_back(2); |
| 168 | lst_2.push_back(666); |
| 169 | |
| 170 | CHECK(lst_1 == lst_2); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 171 | |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 172 | { |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 173 | Bar::MyOtherType s1 {42}; |
| 174 | Bar::MyOtherType s2 {666}; |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 175 | INFO("s1=", s1, " s2=", s2); |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 176 | CHECK(s1 == s2); |
| 177 | CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2); |
| 178 | } |
| 179 | |
Salvage | 4ddb793 | 2022-01-12 16:17:39 +0100 | [diff] [blame] | 180 | CHECK(doctest::IsNaN<double>(0.5)); |
| 181 | CHECK(doctest::IsNaN<float>(std::numeric_limits<float>::infinity())); |
Salvage | 84c509b | 2022-01-12 21:52:07 +0100 | [diff] [blame] | 182 | // can't test actual nan because it's implementation defined |
Salvage | 245d4b2 | 2022-01-12 16:04:16 +0100 | [diff] [blame] | 183 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 184 | // lets see if this exception gets translated |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 185 | throw_if(true, bla1); |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 186 | } |
| 187 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 188 | static doctest::String intTranslator(int ex) { |
| 189 | return doctest::String("int: ") + doctest::toString(ex); |
| 190 | } |
onqtam | 246e817 | 2017-03-17 02:48:12 +0200 | [diff] [blame] | 191 | |
onqtam | 05bcc37 | 2017-03-17 02:10:38 +0200 | [diff] [blame] | 192 | TEST_CASE("a test case that registers an exception translator for int and then throws one") { |
| 193 | // 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] | 194 | // 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] | 195 | doctest::registerExceptionTranslator(intTranslator); |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 196 | |
onqtam | 7cc0e96 | 2017-04-17 23:30:36 +0300 | [diff] [blame] | 197 | throw_if(true, 5); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 198 | } |