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") { |
Salvage | 4bc7a75 | 2022-01-18 05:35:57 +0100 | [diff] [blame] | 6 | char chs[] = { '1', 'a', 's' }; |
| 7 | MESSAGE(chs); CHECK(chs == nullptr); |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 8 | MESSAGE("1as"); CHECK("1as" == nullptr); |
| 9 | |
| 10 | int ints[] = { 0, 1, 1, 2, 3, 5, 8, 13 }; |
| 11 | MESSAGE(ints); CHECK(ints == nullptr); |
Salvage | 7be3b87 | 2022-01-13 05:08:34 +0100 | [diff] [blame] | 12 | |
Salvage | 3f3511c | 2022-01-13 18:18:52 +0100 | [diff] [blame] | 13 | char* cptr = reinterpret_cast<char*>(ints + 4); |
Salvage | 4bc7a75 | 2022-01-18 05:35:57 +0100 | [diff] [blame] | 14 | const char* ccptr = const_cast<const char*>(cptr); |
| 15 | void* vptr = reinterpret_cast<void*>(cptr); |
| 16 | CHECK(doctest::toString(cptr) == doctest::toString(ccptr)); |
| 17 | CHECK(doctest::toString(ccptr) == doctest::toString(vptr)); |
Salvage | 7be3b87 | 2022-01-13 05:08:34 +0100 | [diff] [blame] | 18 | |
Salvage | 996ef32 | 2022-01-18 05:45:52 +0100 | [diff] [blame] | 19 | char* cnptr = nullptr; |
| 20 | MESSAGE(cnptr); CHECK(cnptr != nullptr); |
| 21 | |
Salvage | 7be3b87 | 2022-01-13 05:08:34 +0100 | [diff] [blame] | 22 | enum Test { |
| 23 | A = 0, B, C = 100, |
| 24 | }; |
| 25 | MESSAGE(A); CHECK(A == C); |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 26 | } |
| 27 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 28 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 29 | #include <string> |
| 30 | #include <vector> |
| 31 | #include <list> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 32 | #include <sstream> |
Salvage | 2fc3d4e | 2022-01-12 16:23:19 +0100 | [diff] [blame] | 33 | #include <limits> |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 34 | DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 35 | |
onqtam | 190ba25 | 2019-03-23 09:58:34 +0200 | [diff] [blame] | 36 | DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics |
| 37 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 38 | // the standard forbids writing in the std namespace but it works on all compilers |
| 39 | namespace std |
| 40 | { |
| 41 | template <typename T> |
| 42 | ostream& operator<<(ostream& stream, const vector<T>& in) { |
| 43 | stream << "["; |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 44 | for (size_t i = 0; i < in.size(); ++i) { |
| 45 | if (i != 0) { stream << ", "; } |
| 46 | stream << in[i]; |
| 47 | } |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 48 | stream << "]"; |
| 49 | return stream; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // as an alternative you may write a specialization of doctest::StringMaker |
Salvage | 098e17a | 2022-01-13 20:02:07 +0100 | [diff] [blame] | 54 | namespace doctest |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 55 | { |
Salvage | 098e17a | 2022-01-13 20:02:07 +0100 | [diff] [blame] | 56 | template <typename T> |
| 57 | struct StringMaker<std::list<T>> |
| 58 | { |
| 59 | static String convert(const std::list<T>& in) { |
| 60 | std::ostringstream oss; |
| 61 | |
| 62 | oss << "["; |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 63 | for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) { |
Salvage | 098e17a | 2022-01-13 20:02:07 +0100 | [diff] [blame] | 64 | oss << *it; |
| 65 | if (++it != in.end()) { oss << ", "; } |
Salvage | 6545bdc | 2022-01-13 00:14:21 +0100 | [diff] [blame] | 66 | } |
Salvage | 098e17a | 2022-01-13 20:02:07 +0100 | [diff] [blame] | 67 | oss << "]"; |
| 68 | return oss.str().c_str(); |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 69 | } |
| 70 | }; |
Salvage | 098e17a | 2022-01-13 20:02:07 +0100 | [diff] [blame] | 71 | } |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 72 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 73 | template <typename T, typename K> |
| 74 | struct MyType |
| 75 | { |
| 76 | T one; |
| 77 | K two; |
| 78 | }; |
| 79 | |
| 80 | template <typename T> |
onqtam | 02b7eb7 | 2017-10-28 23:59:49 +0300 | [diff] [blame] | 81 | struct MyTypeInherited : MyType<T, unsigned> |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 82 | {}; |
| 83 | |
| 84 | template <typename T, typename K> |
| 85 | bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) { |
| 86 | return lhs.one == rhs.one && lhs.two == rhs.two; |
| 87 | } |
| 88 | |
| 89 | template <typename T, typename K> |
| 90 | std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) { |
| 91 | stream << "[" << in.one << ", " << in.two << "]"; |
| 92 | return stream; |
| 93 | } |
| 94 | |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 95 | namespace Bar |
| 96 | { |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 97 | struct Foo |
onqtam | 12d5598 | 2017-04-16 22:35:27 +0300 | [diff] [blame] | 98 | { |
| 99 | friend bool operator==(const Foo&, const Foo&) { return false; } |
| 100 | }; |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 101 | |
Salvage | 098e17a | 2022-01-13 20:02:07 +0100 | [diff] [blame] | 102 | // as a third option you may provide an overload of toString() |
| 103 | inline doctest::String toString(const Foo&) { return "Foo{}"; } |
| 104 | |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 105 | struct MyOtherType |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 106 | { |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 107 | int data; |
| 108 | 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] | 109 | }; |
| 110 | |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 111 | // 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] | 112 | template <class OStream> |
Viktor Kirilov | 81fe2e8 | 2020-12-04 18:59:26 +0200 | [diff] [blame] | 113 | OStream& operator<<(OStream& stream, const MyOtherType& in) { |
| 114 | stream << "MyOtherType: " << in.data; |
Viktor Kirilov | 17d984c | 2020-12-04 15:13:33 +0200 | [diff] [blame] | 115 | return stream; |
| 116 | } |
| 117 | |
onqtam | b18680d | 2016-11-15 14:08:56 +0200 | [diff] [blame] | 118 | } // namespace Bar |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 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())); |
Stefan | 8c43a52 | 2022-01-26 21:01:50 +0100 | [diff] [blame] | 182 | CHECK(!doctest::IsNaN<double long>(std::numeric_limits<double long>::quiet_NaN())); |
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 | } |