File indexing completed on 2026-11-28 10:15:47
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
0105 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
0106
0107 #include <any>
0108 #include <functional>
0109 #include <memory>
0110 #include <optional>
0111 #include <ostream> // NOLINT
0112 #include <sstream>
0113 #include <string>
0114 #include <string_view>
0115 #include <tuple>
0116 #include <type_traits>
0117 #include <typeinfo>
0118 #include <utility>
0119 #include <variant>
0120 #include <vector>
0121
0122 #ifdef GTEST_HAS_ABSL
0123 #include "absl/strings/has_absl_stringify.h"
0124 #include "absl/strings/str_cat.h"
0125 #endif
0126 #include "gtest/internal/gtest-internal.h"
0127 #include "gtest/internal/gtest-port.h"
0128
0129 #if GTEST_INTERNAL_HAS_STD_SPAN
0130 #include <span> // NOLINT
0131 #endif
0132
0133 #if GTEST_INTERNAL_HAS_COMPARE_LIB
0134 #include <compare> // NOLINT
0135 #endif
0136
0137 namespace testing {
0138
0139
0140
0141 namespace internal {
0142
0143 template <typename T>
0144 void UniversalPrint(const T& value, ::std::ostream* os);
0145
0146 template <typename T>
0147 struct IsStdSpan {
0148 static constexpr bool value = false;
0149 };
0150
0151 #if GTEST_INTERNAL_HAS_STD_SPAN
0152 template <typename E>
0153 struct IsStdSpan<std::span<E>> {
0154 static constexpr bool value = true;
0155 };
0156 #endif
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166 struct ContainerPrinter {
0167 template <typename T,
0168 typename = typename std::enable_if<
0169 ((sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
0170 !IsRecursiveContainer<T>::value) ||
0171 IsStdSpan<T>::value>::type>
0172 static void PrintValue(const T& container, std::ostream* os) {
0173 const size_t kMaxCount = 32;
0174 *os << '{';
0175 size_t count = 0;
0176 for (auto&& elem : container) {
0177 if (count > 0) {
0178 *os << ',';
0179 if (count == kMaxCount) {
0180 *os << " ...";
0181 break;
0182 }
0183 }
0184 *os << ' ';
0185
0186
0187 internal::UniversalPrint(elem, os);
0188 ++count;
0189 }
0190
0191 if (count > 0) {
0192 *os << ' ';
0193 }
0194 *os << '}';
0195 }
0196 };
0197
0198
0199
0200
0201
0202
0203
0204 struct FunctionPointerPrinter {
0205 template <typename T, typename = typename std::enable_if<
0206 std::is_function<T>::value>::type>
0207 static void PrintValue(T* p, ::std::ostream* os) {
0208 if (p == nullptr) {
0209 *os << "NULL";
0210 } else {
0211
0212
0213
0214 *os << reinterpret_cast<const void*>(p);
0215 }
0216 }
0217 };
0218
0219 struct PointerPrinter {
0220 template <typename T>
0221 static void PrintValue(T* p, ::std::ostream* os) {
0222 if (p == nullptr) {
0223 *os << "NULL";
0224 } else {
0225
0226
0227
0228 *os << p;
0229 }
0230 }
0231 };
0232
0233 namespace internal_stream_operator_without_lexical_name_lookup {
0234
0235
0236
0237
0238
0239 struct LookupBlocker {};
0240 void operator<<(LookupBlocker, LookupBlocker);
0241
0242 struct StreamPrinter {
0243 template <typename T,
0244
0245
0246 typename = typename std::enable_if<
0247 !std::is_member_pointer<T>::value>::type>
0248
0249
0250
0251
0252 static auto PrintValue(const T& value, ::std::ostream* os)
0253 -> decltype((void)(*os << value)) {
0254
0255
0256 *os << value;
0257 }
0258 };
0259
0260 }
0261
0262 struct ProtobufPrinter {
0263
0264
0265
0266 static const size_t kProtobufOneLinerMaxLength = 50;
0267
0268 template <typename T,
0269 typename = typename std::enable_if<
0270 internal::HasDebugStringAndShortDebugString<T>::value>::type>
0271 static void PrintValue(const T& value, ::std::ostream* os) {
0272 std::string pretty_str = value.ShortDebugString();
0273 if (pretty_str.length() > kProtobufOneLinerMaxLength) {
0274 pretty_str = "\n" + value.DebugString();
0275 }
0276 *os << ("<" + pretty_str + ">");
0277 }
0278 };
0279
0280 struct ConvertibleToIntegerPrinter {
0281
0282
0283
0284
0285
0286
0287
0288 static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
0289 *os << value;
0290 }
0291 };
0292
0293 struct ConvertibleToStringViewPrinter {
0294 static void PrintValue(internal::StringView value, ::std::ostream* os) {
0295 internal::UniversalPrint(value, os);
0296 }
0297 };
0298
0299 #ifdef GTEST_HAS_ABSL
0300 struct ConvertibleToAbslStringifyPrinter {
0301 template <typename T,
0302 typename = typename std::enable_if<
0303 absl::HasAbslStringify<T>::value>::type>
0304 static void PrintValue(const T& value, ::std::ostream* os) {
0305 *os << absl::StrCat(value);
0306 }
0307 };
0308 #endif
0309
0310
0311
0312 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
0313 size_t count, ::std::ostream* os);
0314 struct RawBytesPrinter {
0315
0316 template <typename T, size_t = sizeof(T)>
0317 static void PrintValue(const T& value, ::std::ostream* os) {
0318 PrintBytesInObjectTo(
0319 static_cast<const unsigned char*>(
0320
0321 reinterpret_cast<const void*>(std::addressof(value))),
0322 sizeof(value), os);
0323 }
0324 };
0325
0326 struct FallbackPrinter {
0327 template <typename T>
0328 static void PrintValue(const T&, ::std::ostream* os) {
0329 *os << "(incomplete type)";
0330 }
0331 };
0332
0333
0334 template <typename T, typename E, typename Printer, typename... Printers>
0335 struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
0336
0337 template <typename T, typename Printer, typename... Printers>
0338 struct FindFirstPrinter<
0339 T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
0340 Printer, Printers...> {
0341 using type = Printer;
0342 };
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353 template <typename T>
0354 void PrintWithFallback(const T& value, ::std::ostream* os) {
0355 using Printer = typename FindFirstPrinter<
0356 T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
0357 ProtobufPrinter,
0358 #ifdef GTEST_HAS_ABSL
0359 ConvertibleToAbslStringifyPrinter,
0360 #endif
0361 internal_stream_operator_without_lexical_name_lookup::StreamPrinter,
0362 ConvertibleToIntegerPrinter, ConvertibleToStringViewPrinter,
0363 RawBytesPrinter, FallbackPrinter>::type;
0364 Printer::PrintValue(value, os);
0365 }
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382 template <typename ToPrint, typename OtherOperand>
0383 class [[nodiscard]] FormatForComparison {
0384 public:
0385 static ::std::string Format(const ToPrint& value) {
0386 return ::testing::PrintToString(value);
0387 }
0388 };
0389
0390
0391 template <typename ToPrint, size_t N, typename OtherOperand>
0392 class [[nodiscard]] FormatForComparison<ToPrint[N], OtherOperand> {
0393 public:
0394 static ::std::string Format(const ToPrint* value) {
0395 return FormatForComparison<const ToPrint*, OtherOperand>::Format(value);
0396 }
0397 };
0398
0399
0400
0401
0402 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
0403 template <typename OtherOperand> \
0404 class FormatForComparison<CharType*, OtherOperand> { \
0405 public: \
0406 static ::std::string Format(CharType* value) { \
0407 return ::testing::PrintToString(static_cast<const void*>(value)); \
0408 } \
0409 }
0410
0411 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
0412 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
0413 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
0414 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
0415 #ifdef __cpp_lib_char8_t
0416 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t);
0417 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t);
0418 #endif
0419 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t);
0420 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t);
0421 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t);
0422 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t);
0423
0424 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
0425
0426
0427
0428
0429 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
0430 template <> \
0431 class FormatForComparison<CharType*, OtherStringType> { \
0432 public: \
0433 static ::std::string Format(CharType* value) { \
0434 return ::testing::PrintToString(value); \
0435 } \
0436 }
0437
0438 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
0439 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
0440 #ifdef __cpp_lib_char8_t
0441 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string);
0442 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string);
0443 #endif
0444 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string);
0445 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string);
0446 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string);
0447 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string);
0448
0449 #if GTEST_HAS_STD_WSTRING
0450 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
0451 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
0452 #endif
0453
0454 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464 template <typename T1, typename T2>
0465 std::string FormatForComparisonFailureMessage(const T1& value,
0466 const T2& ) {
0467 return FormatForComparison<T1, T2>::Format(value);
0468 }
0469
0470
0471
0472
0473
0474
0475
0476
0477 template <typename T>
0478 class [[nodiscard]] UniversalPrinter;
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491 template <typename T>
0492 void PrintTo(const T& value, ::std::ostream* os) {
0493 internal::PrintWithFallback(value, os);
0494 }
0495
0496
0497
0498
0499
0500
0501 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
0502 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
0503 inline void PrintTo(char c, ::std::ostream* os) {
0504
0505
0506
0507 PrintTo(static_cast<unsigned char>(c), os);
0508 }
0509
0510
0511 inline void PrintTo(bool x, ::std::ostream* os) {
0512 *os << (x ? "true" : "false");
0513 }
0514
0515
0516
0517
0518
0519
0520
0521
0522 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
0523
0524 GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
0525 inline void PrintTo(char16_t c, ::std::ostream* os) {
0526
0527
0528 PrintTo(static_cast<char32_t>(c), os);
0529 }
0530 #ifdef __cpp_lib_char8_t
0531 inline void PrintTo(char8_t c, ::std::ostream* os) {
0532
0533
0534 PrintTo(static_cast<char32_t>(c), os);
0535 }
0536 #endif
0537
0538
0539 #if defined(__SIZEOF_INT128__)
0540 GTEST_API_ void PrintTo(__uint128_t v, ::std::ostream* os);
0541 GTEST_API_ void PrintTo(__int128_t v, ::std::ostream* os);
0542 #endif
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560 template <typename FloatType>
0561 int AppropriateResolution(FloatType val) {
0562 int full = std::numeric_limits<FloatType>::max_digits10;
0563 if (val < 0) val = -val;
0564
0565 #ifdef __GNUC__
0566 #pragma GCC diagnostic push
0567 #pragma GCC diagnostic ignored "-Wfloat-equal"
0568 #endif
0569 if (val < 1000000) {
0570 FloatType mulfor6 = 1e10;
0571
0572
0573
0574 if (val >= static_cast<FloatType>(100000.0)) {
0575 mulfor6 = 1.0;
0576 } else if (val >= static_cast<FloatType>(10000.0)) {
0577 mulfor6 = 1e1;
0578 } else if (val >= static_cast<FloatType>(1000.0)) {
0579 mulfor6 = 1e2;
0580 } else if (val >= static_cast<FloatType>(100.0)) {
0581 mulfor6 = 1e3;
0582 } else if (val >= static_cast<FloatType>(10.0)) {
0583 mulfor6 = 1e4;
0584 } else if (val >= static_cast<FloatType>(1.0)) {
0585 mulfor6 = 1e5;
0586 } else if (val >= static_cast<FloatType>(0.1)) {
0587 mulfor6 = 1e6;
0588 } else if (val >= static_cast<FloatType>(0.01)) {
0589 mulfor6 = 1e7;
0590 } else if (val >= static_cast<FloatType>(0.001)) {
0591 mulfor6 = 1e8;
0592 } else if (val >= static_cast<FloatType>(0.0001)) {
0593 mulfor6 = 1e9;
0594 }
0595 if (static_cast<FloatType>(static_cast<int32_t>(
0596 val * mulfor6 + (static_cast<FloatType>(0.5)))) /
0597 mulfor6 ==
0598 val)
0599 return 6;
0600 } else if (val < static_cast<FloatType>(1e10)) {
0601 FloatType divfor6 = static_cast<FloatType>(1.0);
0602 if (val >= static_cast<FloatType>(1e9)) {
0603 divfor6 = 10000;
0604 } else if (val >=
0605 static_cast<FloatType>(1e8)) {
0606 divfor6 = 1000;
0607 } else if (val >=
0608 static_cast<FloatType>(1e7)) {
0609 divfor6 = 100;
0610 } else if (val >= static_cast<FloatType>(1e6)) {
0611 divfor6 = 10;
0612 }
0613 if (static_cast<FloatType>(static_cast<int32_t>(
0614 val / divfor6 + (static_cast<FloatType>(0.5)))) *
0615 divfor6 ==
0616 val)
0617 return 6;
0618 }
0619 #ifdef __GNUC__
0620 #pragma GCC diagnostic pop
0621 #endif
0622 return full;
0623 }
0624
0625 inline void PrintTo(float f, ::std::ostream* os) {
0626 auto old_precision = os->precision();
0627 os->precision(AppropriateResolution(f));
0628 *os << f;
0629 os->precision(old_precision);
0630 }
0631
0632 inline void PrintTo(double d, ::std::ostream* os) {
0633 auto old_precision = os->precision();
0634 os->precision(AppropriateResolution(d));
0635 *os << d;
0636 os->precision(old_precision);
0637 }
0638
0639
0640 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
0641 inline void PrintTo(char* s, ::std::ostream* os) {
0642 PrintTo(ImplicitCast_<const char*>(s), os);
0643 }
0644
0645
0646
0647 inline void PrintTo(const signed char* s, ::std::ostream* os) {
0648 PrintTo(ImplicitCast_<const void*>(s), os);
0649 }
0650 inline void PrintTo(signed char* s, ::std::ostream* os) {
0651 PrintTo(ImplicitCast_<const void*>(s), os);
0652 }
0653 inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
0654 PrintTo(ImplicitCast_<const void*>(s), os);
0655 }
0656 inline void PrintTo(unsigned char* s, ::std::ostream* os) {
0657 PrintTo(ImplicitCast_<const void*>(s), os);
0658 }
0659 #ifdef __cpp_lib_char8_t
0660
0661 GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os);
0662 inline void PrintTo(char8_t* s, ::std::ostream* os) {
0663 PrintTo(ImplicitCast_<const char8_t*>(s), os);
0664 }
0665 #endif
0666
0667 GTEST_API_ void PrintTo(const char16_t* s, ::std::ostream* os);
0668 inline void PrintTo(char16_t* s, ::std::ostream* os) {
0669 PrintTo(ImplicitCast_<const char16_t*>(s), os);
0670 }
0671
0672 GTEST_API_ void PrintTo(const char32_t* s, ::std::ostream* os);
0673 inline void PrintTo(char32_t* s, ::std::ostream* os) {
0674 PrintTo(ImplicitCast_<const char32_t*>(s), os);
0675 }
0676
0677
0678
0679
0680
0681
0682 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
0683
0684 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
0685 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
0686 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
0687 }
0688 #endif
0689
0690
0691
0692
0693
0694
0695 template <typename T>
0696 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
0697 UniversalPrint(a[0], os);
0698 for (size_t i = 1; i != count; i++) {
0699 *os << ", ";
0700 UniversalPrint(a[i], os);
0701 }
0702 }
0703
0704
0705 GTEST_API_ void PrintStringTo(std::string_view s, ::std::ostream* os);
0706 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
0707 PrintStringTo(s, os);
0708 }
0709 inline void PrintTo(std::string_view s, ::std::ostream* os) {
0710 PrintStringTo(s, os);
0711 }
0712
0713
0714 #ifdef __cpp_lib_char8_t
0715 GTEST_API_ void PrintU8StringTo(::std::u8string_view s, ::std::ostream* os);
0716 inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
0717 PrintU8StringTo(s, os);
0718 }
0719 inline void PrintTo(::std::u8string_view s, ::std::ostream* os) {
0720 PrintU8StringTo(s, os);
0721 }
0722 #endif
0723
0724
0725 GTEST_API_ void PrintU16StringTo(::std::u16string_view s, ::std::ostream* os);
0726 inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
0727 PrintU16StringTo(s, os);
0728 }
0729 inline void PrintTo(::std::u16string_view s, ::std::ostream* os) {
0730 PrintU16StringTo(s, os);
0731 }
0732
0733
0734 GTEST_API_ void PrintU32StringTo(::std::u32string_view s, ::std::ostream* os);
0735 inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
0736 PrintU32StringTo(s, os);
0737 }
0738 inline void PrintTo(::std::u32string_view s, ::std::ostream* os) {
0739 PrintU32StringTo(s, os);
0740 }
0741
0742
0743 #if GTEST_HAS_STD_WSTRING
0744 GTEST_API_ void PrintWideStringTo(::std::wstring_view s, ::std::ostream* os);
0745 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
0746 PrintWideStringTo(s, os);
0747 }
0748 inline void PrintTo(::std::wstring_view s, ::std::ostream* os) {
0749 PrintWideStringTo(s, os);
0750 }
0751 #endif
0752
0753
0754
0755
0756 template <int&... ExplicitArgumentBarrier, typename T = internal::StringView,
0757 std::enable_if_t<!std::is_same_v<T, std::string_view>, int> = 0>
0758 inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
0759 PrintStringTo(sp, os);
0760 }
0761
0762 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
0763
0764 #if GTEST_HAS_RTTI
0765 inline void PrintTo(const std::type_info& info, std::ostream* os) {
0766 *os << internal::GetTypeName(info);
0767 }
0768 #endif
0769
0770 template <typename T>
0771 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
0772 UniversalPrinter<T&>::Print(ref.get(), os);
0773 }
0774
0775 inline const void* VoidifyPointer(const void* p) { return p; }
0776 inline const void* VoidifyPointer(volatile const void* p) {
0777 return const_cast<const void*>(p);
0778 }
0779
0780 template <typename T, typename Ptr>
0781 void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) {
0782 if (ptr == nullptr) {
0783 *os << "(nullptr)";
0784 } else {
0785
0786 *os << "(" << (VoidifyPointer)(ptr.get()) << ")";
0787 }
0788 }
0789 template <typename T, typename Ptr,
0790 typename = typename std::enable_if<!std::is_void<T>::value &&
0791 !std::is_array<T>::value>::type>
0792 void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) {
0793 if (ptr == nullptr) {
0794 *os << "(nullptr)";
0795 } else {
0796 *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = ";
0797 UniversalPrinter<T>::Print(*ptr, os);
0798 *os << ")";
0799 }
0800 }
0801
0802 template <typename T, typename D>
0803 void PrintTo(const std::unique_ptr<T, D>& ptr, std::ostream* os) {
0804 (PrintSmartPointer<T>)(ptr, os, 0);
0805 }
0806
0807 template <typename T>
0808 void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) {
0809 (PrintSmartPointer<T>)(ptr, os, 0);
0810 }
0811
0812 #if GTEST_INTERNAL_HAS_COMPARE_LIB
0813 template <typename T>
0814 void PrintOrderingHelper(T ordering, std::ostream* os) {
0815 if (ordering == T::less) {
0816 *os << "(less)";
0817 } else if (ordering == T::greater) {
0818 *os << "(greater)";
0819 } else if (ordering == T::equivalent) {
0820 *os << "(equivalent)";
0821 } else {
0822 *os << "(unknown ordering)";
0823 }
0824 }
0825
0826 inline void PrintTo(std::strong_ordering ordering, std::ostream* os) {
0827 if (ordering == std::strong_ordering::equal) {
0828 *os << "(equal)";
0829 } else {
0830 PrintOrderingHelper(ordering, os);
0831 }
0832 }
0833
0834 inline void PrintTo(std::partial_ordering ordering, std::ostream* os) {
0835 if (ordering == std::partial_ordering::unordered) {
0836 *os << "(unordered)";
0837 } else {
0838 PrintOrderingHelper(ordering, os);
0839 }
0840 }
0841
0842 inline void PrintTo(std::weak_ordering ordering, std::ostream* os) {
0843 PrintOrderingHelper(ordering, os);
0844 }
0845 #endif
0846
0847
0848
0849 template <typename T>
0850 void PrintTupleTo(const T&, std::integral_constant<size_t, 0>,
0851 ::std::ostream*) {}
0852
0853 template <typename T, size_t I>
0854 void PrintTupleTo(const T& t, std::integral_constant<size_t, I>,
0855 ::std::ostream* os) {
0856 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
0857 GTEST_INTENTIONAL_CONST_COND_PUSH_()
0858 if (I > 1) {
0859 GTEST_INTENTIONAL_CONST_COND_POP_()
0860 *os << ", ";
0861 }
0862 UniversalPrinter<std::tuple_element_t<I - 1, T>>::Print(std::get<I - 1>(t),
0863 os);
0864 }
0865
0866 template <typename... Types>
0867 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
0868 *os << "(";
0869 PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os);
0870 *os << ")";
0871 }
0872
0873
0874 template <typename T1, typename T2>
0875 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
0876 *os << '(';
0877
0878
0879 UniversalPrinter<T1>::Print(value.first, os);
0880 *os << ", ";
0881 UniversalPrinter<T2>::Print(value.second, os);
0882 *os << ')';
0883 }
0884
0885
0886
0887 template <typename T>
0888 class [[nodiscard]] UniversalPrinter {
0889 public:
0890
0891
0892 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
0893
0894
0895
0896
0897 static void Print(const T& value, ::std::ostream* os) {
0898
0899
0900
0901
0902
0903
0904
0905
0906 PrintTo(value, os);
0907 }
0908
0909 GTEST_DISABLE_MSC_WARNINGS_POP_()
0910 };
0911
0912
0913 template <typename T>
0914 class [[nodiscard]] UniversalPrinter<const T> : public UniversalPrinter<T> {};
0915
0916
0917 template <>
0918 class [[nodiscard]] UniversalPrinter<std::any> {
0919 public:
0920 static void Print(const std::any& value, ::std::ostream* os) {
0921 if (value.has_value()) {
0922 *os << "value of type " << GetTypeName(value);
0923 } else {
0924 *os << "no value";
0925 }
0926 }
0927
0928 private:
0929 static std::string GetTypeName(const std::any& value) {
0930 #if GTEST_HAS_RTTI
0931 return internal::GetTypeName(value.type());
0932 #else
0933 static_cast<void>(value);
0934 return "<unknown_type>";
0935 #endif
0936 }
0937 };
0938
0939
0940 template <typename T>
0941 class [[nodiscard]] UniversalPrinter<std::optional<T>> {
0942 public:
0943 static void Print(const std::optional<T>& value, ::std::ostream* os) {
0944 if (!value) {
0945 UniversalPrint(std::nullopt, os);
0946 } else {
0947 *os << '(';
0948 UniversalPrint(*value, os);
0949 *os << ')';
0950 }
0951 }
0952 };
0953
0954 template <>
0955 class [[nodiscard]] UniversalPrinter<std::nullopt_t> {
0956 public:
0957 static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; }
0958 };
0959
0960 struct UniversalPrinterVisitor {
0961 template <typename T>
0962 void operator()(const T& arg) const {
0963 *os << "'" << GetTypeName<T>() << "(index = " << index << ")' with value ";
0964 UniversalPrint(arg, os);
0965 }
0966 ::std::ostream* os;
0967 std::size_t index;
0968 };
0969
0970
0971 template <typename... T>
0972 class [[nodiscard]] UniversalPrinter<std::variant<T...>> {
0973 public:
0974 static void Print(const std::variant<T...>& value, ::std::ostream* os) {
0975 if (value.valueless_by_exception()) {
0976 *os << "(valueless)";
0977 } else {
0978 *os << '(';
0979 std::visit(UniversalPrinterVisitor{os, value.index()}, value);
0980 *os << ')';
0981 }
0982 }
0983 };
0984
0985
0986 template <>
0987 class [[nodiscard]] UniversalPrinter<std::monostate> {
0988 public:
0989 static void Print(std::monostate, ::std::ostream* os) {
0990 *os << "(monostate)";
0991 }
0992 };
0993
0994
0995
0996 template <typename T>
0997 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
0998 if (len == 0) {
0999 *os << "{}";
1000 } else {
1001 *os << "{ ";
1002 const size_t kThreshold = 18;
1003 const size_t kChunkSize = 8;
1004
1005
1006
1007 if (len <= kThreshold) {
1008 PrintRawArrayTo(begin, len, os);
1009 } else {
1010 PrintRawArrayTo(begin, kChunkSize, os);
1011 *os << ", ..., ";
1012 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
1013 }
1014 *os << " }";
1015 }
1016 }
1017
1018 GTEST_API_ void UniversalPrintArray(const char* begin, size_t len,
1019 ::std::ostream* os);
1020
1021 #ifdef __cpp_lib_char8_t
1022
1023 GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len,
1024 ::std::ostream* os);
1025 #endif
1026
1027
1028 GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len,
1029 ::std::ostream* os);
1030
1031
1032 GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len,
1033 ::std::ostream* os);
1034
1035
1036 GTEST_API_ void UniversalPrintArray(const wchar_t* begin, size_t len,
1037 ::std::ostream* os);
1038
1039
1040 template <typename T, size_t N>
1041 class [[nodiscard]] UniversalPrinter<T[N]> {
1042 public:
1043
1044
1045 static void Print(const T (&a)[N], ::std::ostream* os) {
1046 UniversalPrintArray(a, N, os);
1047 }
1048 };
1049
1050
1051 template <typename T>
1052 class [[nodiscard]] UniversalPrinter<T&> {
1053 public:
1054
1055
1056 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
1057
1058 static void Print(const T& value, ::std::ostream* os) {
1059
1060
1061 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
1062
1063
1064 UniversalPrint(value, os);
1065 }
1066
1067 GTEST_DISABLE_MSC_WARNINGS_POP_()
1068 };
1069
1070
1071
1072
1073
1074 template <typename T>
1075 class [[nodiscard]] UniversalTersePrinter {
1076 public:
1077 static void Print(const T& value, ::std::ostream* os) {
1078 UniversalPrint(value, os);
1079 }
1080 };
1081 template <typename T>
1082 class [[nodiscard]] UniversalTersePrinter<T&> {
1083 public:
1084 static void Print(const T& value, ::std::ostream* os) {
1085 UniversalPrint(value, os);
1086 }
1087 };
1088 template <typename T>
1089 class [[nodiscard]] UniversalTersePrinter<std::reference_wrapper<T>> {
1090 public:
1091 static void Print(std::reference_wrapper<T> value, ::std::ostream* os) {
1092 UniversalTersePrinter<T>::Print(value.get(), os);
1093 }
1094 };
1095 template <typename T, size_t N>
1096 class [[nodiscard]] UniversalTersePrinter<T[N]> {
1097 public:
1098 static void Print(const T (&value)[N], ::std::ostream* os) {
1099 UniversalPrinter<T[N]>::Print(value, os);
1100 }
1101 };
1102 template <>
1103 class [[nodiscard]] UniversalTersePrinter<const char*> {
1104 public:
1105 static void Print(const char* str, ::std::ostream* os) {
1106 if (str == nullptr) {
1107 *os << "NULL";
1108 } else {
1109 UniversalPrint(std::string(str), os);
1110 }
1111 }
1112 };
1113 template <>
1114 class [[nodiscard]]
1115 UniversalTersePrinter<char*> : public UniversalTersePrinter<const char*> {};
1116
1117 #ifdef __cpp_lib_char8_t
1118 template <>
1119 class [[nodiscard]] UniversalTersePrinter<const char8_t*> {
1120 public:
1121 static void Print(const char8_t* str, ::std::ostream* os) {
1122 if (str == nullptr) {
1123 *os << "NULL";
1124 } else {
1125 UniversalPrint(::std::u8string(str), os);
1126 }
1127 }
1128 };
1129 template <>
1130 class [[nodiscard]] UniversalTersePrinter<char8_t*>
1131 : public UniversalTersePrinter<const char8_t*> {};
1132 #endif
1133
1134 template <>
1135 class [[nodiscard]] UniversalTersePrinter<const char16_t*> {
1136 public:
1137 static void Print(const char16_t* str, ::std::ostream* os) {
1138 if (str == nullptr) {
1139 *os << "NULL";
1140 } else {
1141 UniversalPrint(::std::u16string(str), os);
1142 }
1143 }
1144 };
1145 template <>
1146 class [[nodiscard]] UniversalTersePrinter<char16_t*>
1147 : public UniversalTersePrinter<const char16_t*> {};
1148
1149 template <>
1150 class [[nodiscard]] UniversalTersePrinter<const char32_t*> {
1151 public:
1152 static void Print(const char32_t* str, ::std::ostream* os) {
1153 if (str == nullptr) {
1154 *os << "NULL";
1155 } else {
1156 UniversalPrint(::std::u32string(str), os);
1157 }
1158 }
1159 };
1160 template <>
1161 class [[nodiscard]] UniversalTersePrinter<char32_t*>
1162 : public UniversalTersePrinter<const char32_t*> {};
1163
1164 #if GTEST_HAS_STD_WSTRING
1165 template <>
1166 class [[nodiscard]] UniversalTersePrinter<const wchar_t*> {
1167 public:
1168 static void Print(const wchar_t* str, ::std::ostream* os) {
1169 if (str == nullptr) {
1170 *os << "NULL";
1171 } else {
1172 UniversalPrint(::std::wstring(str), os);
1173 }
1174 }
1175 };
1176 #endif
1177
1178 template <>
1179 class [[nodiscard]] UniversalTersePrinter<wchar_t*> {
1180 public:
1181 static void Print(wchar_t* str, ::std::ostream* os) {
1182 UniversalTersePrinter<const wchar_t*>::Print(str, os);
1183 }
1184 };
1185
1186 template <typename T>
1187 void UniversalTersePrint(const T& value, ::std::ostream* os) {
1188 UniversalTersePrinter<T>::Print(value, os);
1189 }
1190
1191
1192
1193
1194
1195 template <typename T>
1196 void UniversalPrint(const T& value, ::std::ostream* os) {
1197
1198
1199 typedef T T1;
1200 UniversalPrinter<T1>::Print(value, os);
1201 }
1202
1203 typedef ::std::vector<::std::string> Strings;
1204
1205
1206
1207 template <typename Tuple>
1208 void TersePrintPrefixToStrings(const Tuple&, std::integral_constant<size_t, 0>,
1209 Strings*) {}
1210 template <typename Tuple, size_t I>
1211 void TersePrintPrefixToStrings(const Tuple& t,
1212 std::integral_constant<size_t, I>,
1213 Strings* strings) {
1214 TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
1215 strings);
1216 ::std::stringstream ss;
1217 UniversalTersePrint(std::get<I - 1>(t), &ss);
1218 strings->push_back(ss.str());
1219 }
1220
1221
1222
1223
1224 template <typename Tuple>
1225 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
1226 Strings result;
1227 TersePrintPrefixToStrings(
1228 value, std::integral_constant<size_t, std::tuple_size_v<Tuple>>(),
1229 &result);
1230 return result;
1231 }
1232
1233 }
1234
1235 template <typename T>
1236 ::std::string PrintToString(const T& value) {
1237 ::std::stringstream ss;
1238 internal::UniversalTersePrinter<T>::Print(value, &ss);
1239 return ss.str();
1240 }
1241
1242 }
1243
1244
1245
1246
1247 #include "gtest/internal/custom/gtest-printers.h"
1248
1249 #endif