| 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 |
#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ |
| 102 |
#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ |
| 103 |
|
| 104 |
#include <functional> |
| 105 |
#include <memory> |
| 106 |
#include <ostream> |
| 107 |
#include <sstream> |
| 108 |
#include <string> |
| 109 |
#include <tuple> |
| 110 |
#include <type_traits> |
| 111 |
#include <utility> |
| 112 |
#include <vector> |
| 113 |
|
| 114 |
#include "gtest/internal/gtest-internal.h" |
| 115 |
#include "gtest/internal/gtest-port.h" |
| 116 |
|
| 117 |
namespace testing { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
namespace internal { |
| 122 |
|
| 123 |
template <typename T> |
| 124 |
void UniversalPrint(const T& value, ::std::ostream* os); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
struct ContainerPrinter { |
| 129 |
template <typename T, |
| 130 |
typename = typename std::enable_if< |
| 131 |
(sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) && |
| 132 |
!IsRecursiveContainer<T>::value>::type> |
| 133 |
static void PrintValue(const T& container, std::ostream* os) { |
| 134 |
const size_t kMaxCount = 32; |
| 135 |
*os << '{'; |
| 136 |
size_t count = 0; |
| 137 |
for (auto&& elem : container) { |
| 138 |
if (count > 0) { |
| 139 |
*os << ','; |
| 140 |
if (count == kMaxCount) { |
| 141 |
*os << " ..."; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
*os << ' '; |
| 146 |
|
| 147 |
|
| 148 |
internal::UniversalPrint(elem, os); |
| 149 |
++count; |
| 150 |
} |
| 151 |
|
| 152 |
if (count > 0) { |
| 153 |
*os << ' '; |
| 154 |
} |
| 155 |
*os << '}'; |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
struct FunctionPointerPrinter { |
| 166 |
template <typename T, typename = typename std::enable_if< |
| 167 |
std::is_function<T>::value>::type> |
| 168 |
static void PrintValue(T* p, ::std::ostream* os) { |
| 169 |
if (p == nullptr) { |
| 170 |
*os << "NULL"; |
| 171 |
} else { |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
*os << reinterpret_cast<const void*>(p); |
| 176 |
} |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
struct PointerPrinter { |
| 181 |
template <typename T> |
| 182 |
static void PrintValue(T* p, ::std::ostream* os) { |
| 183 |
if (p == nullptr) { |
| 184 |
*os << "NULL"; |
| 185 |
} else { |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
*os << p; |
| 190 |
} |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
namespace internal_stream_operator_without_lexical_name_lookup { |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
struct LookupBlocker {}; |
| 201 |
void operator<<(LookupBlocker, LookupBlocker); |
| 202 |
|
| 203 |
struct StreamPrinter { |
| 204 |
template <typename T, |
| 205 |
|
| 206 |
|
| 207 |
typename = typename std::enable_if< |
| 208 |
!std::is_member_pointer<T>::value>::type, |
| 209 |
|
| 210 |
|
| 211 |
typename = decltype(std::declval<std::ostream&>() |
| 212 |
<< std::declval<const T&>())> |
| 213 |
static void PrintValue(const T& value, ::std::ostream* os) { |
| 214 |
|
| 215 |
|
| 216 |
*os << value; |
| 217 |
} |
| 218 |
}; |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
struct ProtobufPrinter { |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static const size_t kProtobufOneLinerMaxLength = 50; |
| 227 |
|
| 228 |
template <typename T, |
| 229 |
typename = typename std::enable_if< |
| 230 |
internal::HasDebugStringAndShortDebugString<T>::value>::type> |
| 231 |
static void PrintValue(const T& value, ::std::ostream* os) { |
| 232 |
std::string pretty_str = value.ShortDebugString(); |
| 233 |
if (pretty_str.length() > kProtobufOneLinerMaxLength) { |
| 234 |
pretty_str = "\n" + value.DebugString(); |
| 235 |
} |
| 236 |
*os << ("<" + pretty_str + ">"); |
| 237 |
} |
| 238 |
}; |
| 239 |
|
| 240 |
struct ConvertibleToIntegerPrinter { |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
static void PrintValue(internal::BiggestInt value, ::std::ostream* os) { |
| 249 |
*os << value; |
| 250 |
} |
| 251 |
}; |
| 252 |
|
| 253 |
struct ConvertibleToStringViewPrinter { |
| 254 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 255 |
static void PrintValue(internal::StringView value, ::std::ostream* os) { |
| 256 |
internal::UniversalPrint(value, os); |
| 257 |
} |
| 258 |
#endif |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes, |
| 264 |
size_t count, ::std::ostream* os); |
| 265 |
struct RawBytesPrinter { |
| 266 |
|
| 267 |
template <typename T, size_t = sizeof(T)> |
| 268 |
static void PrintValue(const T& value, ::std::ostream* os) { |
| 269 |
PrintBytesInObjectTo( |
| 270 |
static_cast<const unsigned char*>( |
| 271 |
|
| 272 |
reinterpret_cast<const void*>(std::addressof(value))), |
| 273 |
sizeof(value), os); |
| 274 |
} |
| 275 |
}; |
| 276 |
|
| 277 |
struct FallbackPrinter { |
| 278 |
template <typename T> |
| 279 |
static void PrintValue(const T&, ::std::ostream* os) { |
| 280 |
*os << "(incomplete type)"; |
| 281 |
} |
| 282 |
}; |
| 283 |
|
| 284 |
|
| 285 |
template <typename T, typename E, typename Printer, typename... Printers> |
| 286 |
struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {}; |
| 287 |
|
| 288 |
template <typename T, typename Printer, typename... Printers> |
| 289 |
struct FindFirstPrinter< |
| 290 |
T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)), |
| 291 |
Printer, Printers...> { |
| 292 |
using type = Printer; |
| 293 |
}; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
template <typename T> |
| 305 |
void PrintWithFallback(const T& value, ::std::ostream* os) { |
| 306 |
using Printer = typename FindFirstPrinter< |
| 307 |
T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter, |
| 308 |
internal_stream_operator_without_lexical_name_lookup::StreamPrinter, |
| 309 |
ProtobufPrinter, ConvertibleToIntegerPrinter, |
| 310 |
ConvertibleToStringViewPrinter, RawBytesPrinter, FallbackPrinter>::type; |
| 311 |
Printer::PrintValue(value, os); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
template <typename ToPrint, typename OtherOperand> |
| 330 |
class FormatForComparison { |
| 331 |
public: |
| 332 |
static ::std::string Format(const ToPrint& value) { |
| 333 |
return ::testing::PrintToString(value); |
| 334 |
} |
| 335 |
}; |
| 336 |
|
| 337 |
|
| 338 |
template <typename ToPrint, size_t N, typename OtherOperand> |
| 339 |
class FormatForComparison<ToPrint[N], OtherOperand> { |
| 340 |
public: |
| 341 |
static ::std::string Format(const ToPrint* value) { |
| 342 |
return FormatForComparison<const ToPrint*, OtherOperand>::Format(value); |
| 343 |
} |
| 344 |
}; |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \ |
| 350 |
template <typename OtherOperand> \ |
| 351 |
class FormatForComparison<CharType*, OtherOperand> { \ |
| 352 |
public: \ |
| 353 |
static ::std::string Format(CharType* value) { \ |
| 354 |
return ::testing::PrintToString(static_cast<const void*>(value)); \ |
| 355 |
} \ |
| 356 |
} |
| 357 |
|
| 358 |
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char); |
| 359 |
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char); |
| 360 |
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t); |
| 361 |
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t); |
| 362 |
#ifdef __cpp_lib_char8_t |
| 363 |
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t); |
| 364 |
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t); |
| 365 |
#endif |
| 366 |
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t); |
| 367 |
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t); |
| 368 |
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t); |
| 369 |
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t); |
| 370 |
|
| 371 |
#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_ |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \ |
| 377 |
template <> \ |
| 378 |
class FormatForComparison<CharType*, OtherStringType> { \ |
| 379 |
public: \ |
| 380 |
static ::std::string Format(CharType* value) { \ |
| 381 |
return ::testing::PrintToString(value); \ |
| 382 |
} \ |
| 383 |
} |
| 384 |
|
| 385 |
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string); |
| 386 |
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string); |
| 387 |
#ifdef __cpp_char8_t |
| 388 |
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string); |
| 389 |
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string); |
| 390 |
#endif |
| 391 |
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string); |
| 392 |
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string); |
| 393 |
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string); |
| 394 |
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string); |
| 395 |
|
| 396 |
#if GTEST_HAS_STD_WSTRING |
| 397 |
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring); |
| 398 |
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring); |
| 399 |
#endif |
| 400 |
|
| 401 |
#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_ |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
template <typename T1, typename T2> |
| 412 |
std::string FormatForComparisonFailureMessage(const T1& value, |
| 413 |
const T2& ) { |
| 414 |
return FormatForComparison<T1, T2>::Format(value); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
template <typename T> |
| 425 |
class UniversalPrinter; |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
template <typename T> |
| 439 |
void PrintTo(const T& value, ::std::ostream* os) { |
| 440 |
internal::PrintWithFallback(value, os); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os); |
| 449 |
GTEST_API_ void PrintTo(signed char c, ::std::ostream* os); |
| 450 |
inline void PrintTo(char c, ::std::ostream* os) { |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
PrintTo(static_cast<unsigned char>(c), os); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
inline void PrintTo(bool x, ::std::ostream* os) { |
| 459 |
*os << (x ? "true" : "false"); |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os); |
| 470 |
|
| 471 |
GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os); |
| 472 |
inline void PrintTo(char16_t c, ::std::ostream* os) { |
| 473 |
PrintTo(ImplicitCast_<char32_t>(c), os); |
| 474 |
} |
| 475 |
#ifdef __cpp_char8_t |
| 476 |
inline void PrintTo(char8_t c, ::std::ostream* os) { |
| 477 |
PrintTo(ImplicitCast_<char32_t>(c), os); |
| 478 |
} |
| 479 |
#endif |
| 480 |
|
| 481 |
|
| 482 |
#if defined(__SIZEOF_INT128__) |
| 483 |
GTEST_API_ void PrintTo(__uint128_t v, ::std::ostream* os); |
| 484 |
GTEST_API_ void PrintTo(__int128_t v, ::std::ostream* os); |
| 485 |
#endif |
| 486 |
|
| 487 |
|
| 488 |
GTEST_API_ void PrintTo(const char* s, ::std::ostream* os); |
| 489 |
inline void PrintTo(char* s, ::std::ostream* os) { |
| 490 |
PrintTo(ImplicitCast_<const char*>(s), os); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
inline void PrintTo(const signed char* s, ::std::ostream* os) { |
| 496 |
PrintTo(ImplicitCast_<const void*>(s), os); |
| 497 |
} |
| 498 |
inline void PrintTo(signed char* s, ::std::ostream* os) { |
| 499 |
PrintTo(ImplicitCast_<const void*>(s), os); |
| 500 |
} |
| 501 |
inline void PrintTo(const unsigned char* s, ::std::ostream* os) { |
| 502 |
PrintTo(ImplicitCast_<const void*>(s), os); |
| 503 |
} |
| 504 |
inline void PrintTo(unsigned char* s, ::std::ostream* os) { |
| 505 |
PrintTo(ImplicitCast_<const void*>(s), os); |
| 506 |
} |
| 507 |
#ifdef __cpp_char8_t |
| 508 |
|
| 509 |
GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os); |
| 510 |
inline void PrintTo(char8_t* s, ::std::ostream* os) { |
| 511 |
PrintTo(ImplicitCast_<const char8_t*>(s), os); |
| 512 |
} |
| 513 |
#endif |
| 514 |
|
| 515 |
GTEST_API_ void PrintTo(const char16_t* s, ::std::ostream* os); |
| 516 |
inline void PrintTo(char16_t* s, ::std::ostream* os) { |
| 517 |
PrintTo(ImplicitCast_<const char16_t*>(s), os); |
| 518 |
} |
| 519 |
|
| 520 |
GTEST_API_ void PrintTo(const char32_t* s, ::std::ostream* os); |
| 521 |
inline void PrintTo(char32_t* s, ::std::ostream* os) { |
| 522 |
PrintTo(ImplicitCast_<const char32_t*>(s), os); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) |
| 531 |
|
| 532 |
GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os); |
| 533 |
inline void PrintTo(wchar_t* s, ::std::ostream* os) { |
| 534 |
PrintTo(ImplicitCast_<const wchar_t*>(s), os); |
| 535 |
} |
| 536 |
#endif |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
template <typename T> |
| 544 |
void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) { |
| 545 |
UniversalPrint(a[0], os); |
| 546 |
for (size_t i = 1; i != count; i++) { |
| 547 |
*os << ", "; |
| 548 |
UniversalPrint(a[i], os); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
GTEST_API_ void PrintStringTo(const ::std::string& s, ::std::ostream* os); |
| 554 |
inline void PrintTo(const ::std::string& s, ::std::ostream* os) { |
| 555 |
PrintStringTo(s, os); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
#ifdef __cpp_char8_t |
| 560 |
GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os); |
| 561 |
inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) { |
| 562 |
PrintU8StringTo(s, os); |
| 563 |
} |
| 564 |
#endif |
| 565 |
|
| 566 |
|
| 567 |
GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os); |
| 568 |
inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) { |
| 569 |
PrintU16StringTo(s, os); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os); |
| 574 |
inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) { |
| 575 |
PrintU32StringTo(s, os); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
#if GTEST_HAS_STD_WSTRING |
| 580 |
GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os); |
| 581 |
inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) { |
| 582 |
PrintWideStringTo(s, os); |
| 583 |
} |
| 584 |
#endif |
| 585 |
|
| 586 |
#if GTEST_INTERNAL_HAS_STRING_VIEW |
| 587 |
|
| 588 |
inline void PrintTo(internal::StringView sp, ::std::ostream* os) { |
| 589 |
PrintTo(::std::string(sp), os); |
| 590 |
} |
| 591 |
#endif |
| 592 |
|
| 593 |
inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; } |
| 594 |
|
| 595 |
#if GTEST_HAS_RTTI |
| 596 |
inline void PrintTo(const std::type_info& info, std::ostream* os) { |
| 597 |
*os << internal::GetTypeName(info); |
| 598 |
} |
| 599 |
#endif |
| 600 |
|
| 601 |
template <typename T> |
| 602 |
void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) { |
| 603 |
UniversalPrinter<T&>::Print(ref.get(), os); |
| 604 |
} |
| 605 |
|
| 606 |
inline const void* VoidifyPointer(const void* p) { return p; } |
| 607 |
inline const void* VoidifyPointer(volatile const void* p) { |
| 608 |
return const_cast<const void*>(p); |
| 609 |
} |
| 610 |
|
| 611 |
template <typename T, typename Ptr> |
| 612 |
void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) { |
| 613 |
if (ptr == nullptr) { |
| 614 |
*os << "(nullptr)"; |
| 615 |
} else { |
| 616 |
|
| 617 |
*os << "(" << (VoidifyPointer)(ptr.get()) << ")"; |
| 618 |
} |
| 619 |
} |
| 620 |
template <typename T, typename Ptr, |
| 621 |
typename = typename std::enable_if<!std::is_void<T>::value && |
| 622 |
!std::is_array<T>::value>::type> |
| 623 |
void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) { |
| 624 |
if (ptr == nullptr) { |
| 625 |
*os << "(nullptr)"; |
| 626 |
} else { |
| 627 |
*os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = "; |
| 628 |
UniversalPrinter<T>::Print(*ptr, os); |
| 629 |
*os << ")"; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
template <typename T, typename D> |
| 634 |
void PrintTo(const std::unique_ptr<T, D>& ptr, std::ostream* os) { |
| 635 |
(PrintSmartPointer<T>)(ptr, os, 0); |
| 636 |
} |
| 637 |
|
| 638 |
template <typename T> |
| 639 |
void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) { |
| 640 |
(PrintSmartPointer<T>)(ptr, os, 0); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
template <typename T> |
| 646 |
void PrintTupleTo(const T&, std::integral_constant<size_t, 0>, |
| 647 |
::std::ostream*) {} |
| 648 |
|
| 649 |
template <typename T, size_t I> |
| 650 |
void PrintTupleTo(const T& t, std::integral_constant<size_t, I>, |
| 651 |
::std::ostream* os) { |
| 652 |
PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os); |
| 653 |
GTEST_INTENTIONAL_CONST_COND_PUSH_() |
| 654 |
if (I > 1) { |
| 655 |
GTEST_INTENTIONAL_CONST_COND_POP_() |
| 656 |
*os << ", "; |
| 657 |
} |
| 658 |
UniversalPrinter<typename std::tuple_element<I - 1, T>::type>::Print( |
| 659 |
std::get<I - 1>(t), os); |
| 660 |
} |
| 661 |
|
| 662 |
template <typename... Types> |
| 663 |
void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) { |
| 664 |
*os << "("; |
| 665 |
PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os); |
| 666 |
*os << ")"; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
template <typename T1, typename T2> |
| 671 |
void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) { |
| 672 |
*os << '('; |
| 673 |
|
| 674 |
|
| 675 |
UniversalPrinter<T1>::Print(value.first, os); |
| 676 |
*os << ", "; |
| 677 |
UniversalPrinter<T2>::Print(value.second, os); |
| 678 |
*os << ')'; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
template <typename T> |
| 684 |
class UniversalPrinter { |
| 685 |
public: |
| 686 |
|
| 687 |
|
| 688 |
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180) |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
static void Print(const T& value, ::std::ostream* os) { |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
PrintTo(value, os); |
| 703 |
} |
| 704 |
|
| 705 |
GTEST_DISABLE_MSC_WARNINGS_POP_() |
| 706 |
}; |
| 707 |
|
| 708 |
|
| 709 |
template <typename T> |
| 710 |
class UniversalPrinter<const T> : public UniversalPrinter<T> {}; |
| 711 |
|
| 712 |
#if GTEST_INTERNAL_HAS_ANY |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
template <> |
| 717 |
class UniversalPrinter<Any> { |
| 718 |
public: |
| 719 |
static void Print(const Any& value, ::std::ostream* os) { |
| 720 |
if (value.has_value()) { |
| 721 |
*os << "value of type " << GetTypeName(value); |
| 722 |
} else { |
| 723 |
*os << "no value"; |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
private: |
| 728 |
static std::string GetTypeName(const Any& value) { |
| 729 |
#if GTEST_HAS_RTTI |
| 730 |
return internal::GetTypeName(value.type()); |
| 731 |
#else |
| 732 |
static_cast<void>(value); |
| 733 |
return "<unknown_type>"; |
| 734 |
#endif |
| 735 |
} |
| 736 |
}; |
| 737 |
|
| 738 |
#endif |
| 739 |
|
| 740 |
#if GTEST_INTERNAL_HAS_OPTIONAL |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
template <typename T> |
| 745 |
class UniversalPrinter<Optional<T>> { |
| 746 |
public: |
| 747 |
static void Print(const Optional<T>& value, ::std::ostream* os) { |
| 748 |
*os << '('; |
| 749 |
if (!value) { |
| 750 |
*os << "nullopt"; |
| 751 |
} else { |
| 752 |
UniversalPrint(*value, os); |
| 753 |
} |
| 754 |
*os << ')'; |
| 755 |
} |
| 756 |
}; |
| 757 |
|
| 758 |
template <> |
| 759 |
class UniversalPrinter<decltype(Nullopt())> { |
| 760 |
public: |
| 761 |
static void Print(decltype(Nullopt()), ::std::ostream* os) { |
| 762 |
*os << "(nullopt)"; |
| 763 |
} |
| 764 |
}; |
| 765 |
|
| 766 |
#endif |
| 767 |
|
| 768 |
#if GTEST_INTERNAL_HAS_VARIANT |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
template <typename... T> |
| 773 |
class UniversalPrinter<Variant<T...>> { |
| 774 |
public: |
| 775 |
static void Print(const Variant<T...>& value, ::std::ostream* os) { |
| 776 |
*os << '('; |
| 777 |
#if GTEST_HAS_ABSL |
| 778 |
absl::visit(Visitor{os, value.index()}, value); |
| 779 |
#else |
| 780 |
std::visit(Visitor{os, value.index()}, value); |
| 781 |
#endif |
| 782 |
*os << ')'; |
| 783 |
} |
| 784 |
|
| 785 |
private: |
| 786 |
struct Visitor { |
| 787 |
template <typename U> |
| 788 |
void operator()(const U& u) const { |
| 789 |
*os << "'" << GetTypeName<U>() << "(index = " << index |
| 790 |
<< ")' with value "; |
| 791 |
UniversalPrint(u, os); |
| 792 |
} |
| 793 |
::std::ostream* os; |
| 794 |
std::size_t index; |
| 795 |
}; |
| 796 |
}; |
| 797 |
|
| 798 |
#endif |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
template <typename T> |
| 803 |
void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) { |
| 804 |
if (len == 0) { |
| 805 |
*os << "{}"; |
| 806 |
} else { |
| 807 |
*os << "{ "; |
| 808 |
const size_t kThreshold = 18; |
| 809 |
const size_t kChunkSize = 8; |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
if (len <= kThreshold) { |
| 814 |
PrintRawArrayTo(begin, len, os); |
| 815 |
} else { |
| 816 |
PrintRawArrayTo(begin, kChunkSize, os); |
| 817 |
*os << ", ..., "; |
| 818 |
PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os); |
| 819 |
} |
| 820 |
*os << " }"; |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
GTEST_API_ void UniversalPrintArray(const char* begin, size_t len, |
| 825 |
::std::ostream* os); |
| 826 |
|
| 827 |
#ifdef __cpp_char8_t |
| 828 |
|
| 829 |
GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len, |
| 830 |
::std::ostream* os); |
| 831 |
#endif |
| 832 |
|
| 833 |
|
| 834 |
GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len, |
| 835 |
::std::ostream* os); |
| 836 |
|
| 837 |
|
| 838 |
GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len, |
| 839 |
::std::ostream* os); |
| 840 |
|
| 841 |
|
| 842 |
GTEST_API_ void UniversalPrintArray(const wchar_t* begin, size_t len, |
| 843 |
::std::ostream* os); |
| 844 |
|
| 845 |
|
| 846 |
template <typename T, size_t N> |
| 847 |
class UniversalPrinter<T[N]> { |
| 848 |
public: |
| 849 |
|
| 850 |
|
| 851 |
static void Print(const T (&a)[N], ::std::ostream* os) { |
| 852 |
UniversalPrintArray(a, N, os); |
| 853 |
} |
| 854 |
}; |
| 855 |
|
| 856 |
|
| 857 |
template <typename T> |
| 858 |
class UniversalPrinter<T&> { |
| 859 |
public: |
| 860 |
|
| 861 |
|
| 862 |
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180) |
| 863 |
|
| 864 |
static void Print(const T& value, ::std::ostream* os) { |
| 865 |
|
| 866 |
|
| 867 |
*os << "@" << reinterpret_cast<const void*>(&value) << " "; |
| 868 |
|
| 869 |
|
| 870 |
UniversalPrint(value, os); |
| 871 |
} |
| 872 |
|
| 873 |
GTEST_DISABLE_MSC_WARNINGS_POP_() |
| 874 |
}; |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
template <typename T> |
| 881 |
class UniversalTersePrinter { |
| 882 |
public: |
| 883 |
static void Print(const T& value, ::std::ostream* os) { |
| 884 |
UniversalPrint(value, os); |
| 885 |
} |
| 886 |
}; |
| 887 |
template <typename T> |
| 888 |
class UniversalTersePrinter<T&> { |
| 889 |
public: |
| 890 |
static void Print(const T& value, ::std::ostream* os) { |
| 891 |
UniversalPrint(value, os); |
| 892 |
} |
| 893 |
}; |
| 894 |
template <typename T, size_t N> |
| 895 |
class UniversalTersePrinter<T[N]> { |
| 896 |
public: |
| 897 |
static void Print(const T (&value)[N], ::std::ostream* os) { |
| 898 |
UniversalPrinter<T[N]>::Print(value, os); |
| 899 |
} |
| 900 |
}; |
| 901 |
template <> |
| 902 |
class UniversalTersePrinter<const char*> { |
| 903 |
public: |
| 904 |
static void Print(const char* str, ::std::ostream* os) { |
| 905 |
if (str == nullptr) { |
| 906 |
*os << "NULL"; |
| 907 |
} else { |
| 908 |
UniversalPrint(std::string(str), os); |
| 909 |
} |
| 910 |
} |
| 911 |
}; |
| 912 |
template <> |
| 913 |
class UniversalTersePrinter<char*> : public UniversalTersePrinter<const char*> { |
| 914 |
}; |
| 915 |
|
| 916 |
#ifdef __cpp_char8_t |
| 917 |
template <> |
| 918 |
class UniversalTersePrinter<const char8_t*> { |
| 919 |
public: |
| 920 |
static void Print(const char8_t* str, ::std::ostream* os) { |
| 921 |
if (str == nullptr) { |
| 922 |
*os << "NULL"; |
| 923 |
} else { |
| 924 |
UniversalPrint(::std::u8string(str), os); |
| 925 |
} |
| 926 |
} |
| 927 |
}; |
| 928 |
template <> |
| 929 |
class UniversalTersePrinter<char8_t*> |
| 930 |
: public UniversalTersePrinter<const char8_t*> {}; |
| 931 |
#endif |
| 932 |
|
| 933 |
template <> |
| 934 |
class UniversalTersePrinter<const char16_t*> { |
| 935 |
public: |
| 936 |
static void Print(const char16_t* str, ::std::ostream* os) { |
| 937 |
if (str == nullptr) { |
| 938 |
*os << "NULL"; |
| 939 |
} else { |
| 940 |
UniversalPrint(::std::u16string(str), os); |
| 941 |
} |
| 942 |
} |
| 943 |
}; |
| 944 |
template <> |
| 945 |
class UniversalTersePrinter<char16_t*> |
| 946 |
: public UniversalTersePrinter<const char16_t*> {}; |
| 947 |
|
| 948 |
template <> |
| 949 |
class UniversalTersePrinter<const char32_t*> { |
| 950 |
public: |
| 951 |
static void Print(const char32_t* str, ::std::ostream* os) { |
| 952 |
if (str == nullptr) { |
| 953 |
*os << "NULL"; |
| 954 |
} else { |
| 955 |
UniversalPrint(::std::u32string(str), os); |
| 956 |
} |
| 957 |
} |
| 958 |
}; |
| 959 |
template <> |
| 960 |
class UniversalTersePrinter<char32_t*> |
| 961 |
: public UniversalTersePrinter<const char32_t*> {}; |
| 962 |
|
| 963 |
#if GTEST_HAS_STD_WSTRING |
| 964 |
template <> |
| 965 |
class UniversalTersePrinter<const wchar_t*> { |
| 966 |
public: |
| 967 |
static void Print(const wchar_t* str, ::std::ostream* os) { |
| 968 |
if (str == nullptr) { |
| 969 |
*os << "NULL"; |
| 970 |
} else { |
| 971 |
UniversalPrint(::std::wstring(str), os); |
| 972 |
} |
| 973 |
} |
| 974 |
}; |
| 975 |
#endif |
| 976 |
|
| 977 |
template <> |
| 978 |
class UniversalTersePrinter<wchar_t*> { |
| 979 |
public: |
| 980 |
static void Print(wchar_t* str, ::std::ostream* os) { |
| 981 |
UniversalTersePrinter<const wchar_t*>::Print(str, os); |
| 982 |
} |
| 983 |
}; |
| 984 |
|
| 985 |
template <typename T> |
| 986 |
void UniversalTersePrint(const T& value, ::std::ostream* os) { |
| 987 |
UniversalTersePrinter<T>::Print(value, os); |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
template <typename T> |
| 995 |
void UniversalPrint(const T& value, ::std::ostream* os) { |
| 996 |
|
| 997 |
|
| 998 |
typedef T T1; |
| 999 |
UniversalPrinter<T1>::Print(value, os); |
| 1000 |
} |
| 1001 |
|
| 1002 |
typedef ::std::vector<::std::string> Strings; |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
template <typename Tuple> |
| 1007 |
void TersePrintPrefixToStrings(const Tuple&, std::integral_constant<size_t, 0>, |
| 1008 |
Strings*) {} |
| 1009 |
template <typename Tuple, size_t I> |
| 1010 |
void TersePrintPrefixToStrings(const Tuple& t, |
| 1011 |
std::integral_constant<size_t, I>, |
| 1012 |
Strings* strings) { |
| 1013 |
TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(), |
| 1014 |
strings); |
| 1015 |
::std::stringstream ss; |
| 1016 |
UniversalTersePrint(std::get<I - 1>(t), &ss); |
| 1017 |
strings->push_back(ss.str()); |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
template <typename Tuple> |
| 1024 |
Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) { |
| 1025 |
Strings result; |
| 1026 |
TersePrintPrefixToStrings( |
| 1027 |
value, std::integral_constant<size_t, std::tuple_size<Tuple>::value>(), |
| 1028 |
&result); |
| 1029 |
return result; |
| 1030 |
} |
| 1031 |
|
| 1032 |
} |
| 1033 |
|
| 1034 |
template <typename T> |
| 1035 |
::std::string PrintToString(const T& value) { |
| 1036 |
::std::stringstream ss; |
| 1037 |
internal::UniversalTersePrinter<T>::Print(value, &ss); |
| 1038 |
return ss.str(); |
| 1039 |
} |
| 1040 |
|
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
#include "gtest/internal/custom/gtest-printers.h" |
| 1047 |
|
| 1048 |
#endif |