1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
#ifdef _MSC_VER
__pragma(warning(push))
__pragma(warning(disable : 4643))
namespace std {
template <typename> struct char_traits;
template <typename, typename> class basic_ostream;
typedef basic_ostream<char, char_traits<char>> ostream; // NOLINT(modernize-use-using)
template<class TRAITS>
basic_ostream<char, TRAITS>& operator<<(basic_ostream<char, TRAITS>&, const char*);
}
__pragma(warning(pop))
#else
#include <iostream>
#endif
namespace N {
struct A { };
struct B {
friend std::ostream& operator<<(std::ostream& os, const B&) { return os << "B"; }
};
struct C { };
static std::ostream& operator<<(std::ostream& os, const C&) { return os << "C"; }
}
static std::ostream& operator<<(std::ostream& os, const N::A&) { return os << "A"; }
#include <doctest/doctest.h>
#include <utility>
TEST_CASE("operator<<") {
MESSAGE(N::A{ });
MESSAGE(N::B{ });
MESSAGE(N::C{ });
}
#include "header.h"
// std::move is broken with VS <= 15
#if defined(_MSC_VER) && _MSC_VER <= 1900
#define MOVE(...) __VA_ARGS__
#else
#define MOVE std::move
#endif
TEST_CASE("no headers") {
char chs[] = { '1', 'a', 's' }; // NOLINT(*-avoid-c-arrays)
MESSAGE(chs); CHECK(chs == nullptr);
MESSAGE("1as"); CHECK("1as" == nullptr);
int ints[] = { 0, 1, 1, 2, 3, 5, 8, 13 }; // NOLINT(*-avoid-c-arrays)
MESSAGE(ints); CHECK(ints == nullptr);
MESSAGE(MOVE(ints)); // NOLINT(*-move-const-arg)
char* cptr = reinterpret_cast<char*>(ints + 4); // NOLINT
const char* ccptr = cptr;
void* vptr = reinterpret_cast<void*>(cptr);
CHECK(doctest::toString(cptr) == doctest::toString(ccptr));
CHECK(doctest::toString(ccptr) == doctest::toString(vptr));
char* cnptr = nullptr;
MESSAGE(cnptr); CHECK(cnptr != nullptr);
enum Test {
A = 0, B, C = 100,
};
MESSAGE(A); CHECK(A == C);
MESSAGE(doctest::toString<int>());
}
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
#include <string>
#include <vector>
#include <list>
#include <sstream>
#include <limits>
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
DOCTEST_MSVC_SUPPRESS_WARNING(5045) // Spectre mitigation diagnostics
// the standard forbids writing in the std namespace but it works on all compilers
namespace std // NOLINT(cert-dcl58-cpp)
{
template <typename T>
ostream& operator<<(ostream& stream, const vector<T>& in) {
stream << "[";
for (size_t i = 0; i < in.size(); ++i) {
if (i != 0) { stream << ", "; }
stream << in[i];
}
stream << "]";
return stream;
}
}
// as an alternative you may write a specialization of doctest::StringMaker
namespace doctest
{
template <typename T>
struct StringMaker<std::list<T>>
{
static String convert(const std::list<T>& in) {
std::ostringstream oss;
oss << "[";
// NOLINTNEXTLINE(*-use-auto)
for (typename std::list<T>::const_iterator it = in.begin(); it != in.end();) {
oss << *it;
if (++it != in.end()) { oss << ", "; }
}
oss << "]";
return oss.str().c_str();
}
};
}
template <typename T, typename K>
struct MyType
{
T one;
K two;
};
template <typename T>
struct MyTypeInherited : MyType<T, unsigned>
{};
template <typename T, typename K>
bool operator==(const MyType<T, K>& lhs, const MyType<T, K>& rhs) {
return lhs.one == rhs.one && lhs.two == rhs.two;
}
template <typename T, typename K>
std::ostream& operator<<(std::ostream& stream, const MyType<T, K>& in) {
stream << "[" << in.one << ", " << in.two << "]";
return stream;
}
namespace Bar
{
struct Foo
{
friend bool operator==(const Foo&, const Foo&) { return false; }
};
// as a third option you may provide an overload of toString()
inline doctest::String toString(const Foo&) { return "Foo{}"; }
struct MyOtherType
{
int data;
friend bool operator==(const MyOtherType& l, const MyOtherType& r) { return l.data == r.data; }
};
// you also can use a template operator<< if your code does not use std::ostream
template <class OStream>
OStream& operator<<(OStream& stream, const MyOtherType& in) {
stream << "MyOtherType: " << in.data;
return stream;
}
} // namespace Bar
// set an exception translator for MyTypeInherited<int>
REGISTER_EXCEPTION_TRANSLATOR(MyTypeInherited<int>& ex) {
return doctest::String("MyTypeInherited<int>(") + doctest::toString(ex.one) + ", " +
doctest::toString(ex.two) + ")";
}
#define CHECK_NOT_DEFAULT_STR(var) CHECK(toString(var) != "{?}")
TEST_CASE("all asserts should fail and show how the objects get stringified") {
MyTypeInherited<int> bla1;
bla1.one = 5;
bla1.two = 4u;
Bar::Foo f1;
MESSAGE(f1);
Bar::Foo f2;
CHECK(f1 == f2);
doctest::String str;
CHECK(str == doctest::toString(str));
// std::string already has an operator<< working with std::ostream
std::string dummy = "omg";
MESSAGE(dummy);
CHECK(dummy == "tralala"); // should fail
CHECK("tralala" == dummy); // should fail
std::vector<int> vec1;
vec1.push_back(1);
vec1.push_back(2);
vec1.push_back(3);
MESSAGE(vec1);
std::vector<int> vec2;
vec2.push_back(1);
vec2.push_back(2);
vec2.push_back(4);
CHECK(vec1 == vec2);
std::list<int> lst_1;
lst_1.push_back(1);
lst_1.push_back(42);
lst_1.push_back(3);
MESSAGE(lst_1);
std::list<int> lst_2;
lst_2.push_back(1);
lst_2.push_back(2);
lst_2.push_back(666);
CHECK(lst_1 == lst_2);
{
Bar::MyOtherType s1 {42};
Bar::MyOtherType s2 {666};
INFO("s1=", s1, " s2=", s2);
CHECK(s1 == s2);
CHECK_MESSAGE(s1 == s2, s1, " is not really ", s2);
}
CHECK_NOT_DEFAULT_STR(doctest::IsNaN<double>(0.5));
CHECK_NOT_DEFAULT_STR(!doctest::IsNaN<float>(std::numeric_limits<float>::infinity()));
CHECK_NOT_DEFAULT_STR(doctest::IsNaN<double long>(std::numeric_limits<double long>::quiet_NaN()));
CHECK("a" == doctest::Contains("aaa"));
// lets see if this exception gets translated
throw_if(true, bla1);
}
static doctest::String intTranslator(int ex) {
return doctest::String("int: ") + doctest::toString(ex);
}
TEST_CASE("a test case that registers an exception translator for int and then throws one") {
// set an exception translator for int - note that this shouldn't be done in a test case but
// in main() or somewhere before executing the tests - but here I'm just lazy...
doctest::registerExceptionTranslator(intTranslator);
throw_if(true, 5);
}
static void function() { }
static int*** function2() { return nullptr; }
TEST_CASE("pointer comparisons") {
int i = 42;
int* a = &i;
int* b = a;
CHECK(a == b);
CHECK_EQ(a, b);
void (*functionPointer)() = &function;
CHECK(&function == functionPointer);
CHECK(&function2 == &function2);
}
enum class Foo { };
static std::ostream& operator<<(std::ostream& os, Foo) {
return os << "Foo";
}
TEST_CASE("enum with operator<<") {
CHECK(doctest::toString(Foo()) == "Foo");
}
|