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