101 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ 102 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ 104 #include <functional> 110 #include <type_traits> 124 template <
typename T>
130 template <
typename T,
131 typename =
typename std::enable_if<
132 (
sizeof(IsContainerTest<T>(0)) ==
sizeof(
IsContainer)) &&
134 static void PrintValue(
const T& container, std::ostream* os) {
135 const size_t kMaxCount = 32;
138 for (
auto&& elem : container) {
141 if (count == kMaxCount) {
167 template <
typename T,
typename =
typename std::enable_if<
168 std::is_function<T>::value>
::type>
176 *os << reinterpret_cast<const void*>(p);
182 template <
typename T>
195 namespace internal_stream_operator_without_lexical_name_lookup {
205 template <
typename T,
208 typename =
typename std::enable_if<
209 !std::is_member_pointer<T>::value>
::type,
212 typename = decltype(std::declval<std::ostream&>()
213 << std::declval<const T&>())>
227 static const size_t kProtobufOneLinerMaxLength = 50;
229 template <
typename T,
230 typename =
typename std::enable_if<
233 std::string pretty_str = value.ShortDebugString();
234 if (pretty_str.length() > kProtobufOneLinerMaxLength) {
235 pretty_str =
"\n" + value.DebugString();
237 *os << (
"<" + pretty_str +
">");
255 #if GTEST_INTERNAL_HAS_STRING_VIEW 256 static void PrintValue(internal::StringView value, ::std::ostream* os) {
265 size_t count, ::std::ostream* os);
268 template <
typename T,
size_t = sizeof(T)>
271 static_cast<const unsigned char*>(
273 reinterpret_cast<const void*>(std::addressof(value))),
279 template <
typename T>
281 *os <<
"(incomplete type)";
286 template <
typename T,
typename E,
typename Printer,
typename... Printers>
289 template <
typename T,
typename Printer,
typename... Printers>
291 T, decltype(Printer::
PrintValue(std::declval<const T&>(), nullptr)),
292 Printer, Printers...> {
305 template <
typename T>
312 Printer::PrintValue(value, os);
330 template <
typename ToPr
int,
typename OtherOperand>
333 static ::std::string
Format(
const ToPrint& value) {
339 template <
typename ToPr
int,
size_t N,
typename OtherOperand>
342 static ::std::string
Format(
const ToPrint* value) {
350 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \ 351 template <typename OtherOperand> \ 352 class FormatForComparison<CharType*, OtherOperand> { \ 354 static ::std::string Format(CharType* value) { \ 355 return ::testing::PrintToString(static_cast<const void*>(value)); \ 363 #ifdef __cpp_lib_char8_t 372 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_ 377 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \ 379 class FormatForComparison<CharType*, OtherStringType> { \ 381 static ::std::string Format(CharType* value) { \ 382 return ::testing::PrintToString(value); \ 388 #ifdef __cpp_lib_char8_t 397 #if GTEST_HAS_STD_WSTRING 402 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_ 412 template <
typename T1,
typename T2>
425 template <
typename T>
439 template <
typename T>
440 void PrintTo(
const T& value, ::std::ostream* os) {
451 inline void PrintTo(
char c, ::std::ostream* os) {
455 PrintTo(static_cast<unsigned char>(c), os);
460 *os << (x ?
"true" :
"false");
473 inline void PrintTo(char16_t c, ::std::ostream* os) {
474 PrintTo(ImplicitCast_<char32_t>(c), os);
477 inline void PrintTo(char8_t c, ::std::ostream* os) {
478 PrintTo(ImplicitCast_<char32_t>(c), os);
483 #if defined(__SIZEOF_INT128__) 486 #endif // __SIZEOF_INT128__ 504 template <
typename FloatType>
506 int full = std::numeric_limits<FloatType>::max_digits10;
507 if (val < 0) val = -val;
510 FloatType mulfor6 = 1e10;
511 if (val >= 100000.0) {
513 }
else if (val >= 10000.0) {
515 }
else if (val >= 1000.0) {
517 }
else if (val >= 100.0) {
519 }
else if (val >= 10.0) {
521 }
else if (val >= 1.0) {
523 }
else if (val >= 0.1) {
525 }
else if (val >= 0.01) {
527 }
else if (val >= 0.001) {
529 }
else if (val >= 0.0001) {
532 if (static_cast<int32_t>(val * mulfor6 + 0.5) / mulfor6 == val)
return 6;
533 }
else if (val < 1e10) {
534 FloatType divfor6 = 1.0;
537 }
else if (val >= 1e8) {
539 }
else if (val >= 1e7) {
541 }
else if (val >= 1e6) {
544 if (static_cast<int32_t>(val / divfor6 + 0.5) * divfor6 == val)
return 6;
550 auto old_precision = os->precision();
553 os->precision(old_precision);
556 inline void PrintTo(
double d, ::std::ostream* os) {
557 auto old_precision = os->precision();
560 os->precision(old_precision);
565 inline void PrintTo(
char* s, ::std::ostream* os) {
566 PrintTo(ImplicitCast_<const char*>(s), os);
571 inline void PrintTo(
const signed char* s, ::std::ostream* os) {
572 PrintTo(ImplicitCast_<const void*>(s), os);
574 inline void PrintTo(
signed char* s, ::std::ostream* os) {
575 PrintTo(ImplicitCast_<const void*>(s), os);
577 inline void PrintTo(
const unsigned char* s, ::std::ostream* os) {
578 PrintTo(ImplicitCast_<const void*>(s), os);
580 inline void PrintTo(
unsigned char* s, ::std::ostream* os) {
581 PrintTo(ImplicitCast_<const void*>(s), os);
586 inline void PrintTo(char8_t* s, ::std::ostream* os) {
587 PrintTo(ImplicitCast_<const char8_t*>(s), os);
592 inline void PrintTo(char16_t* s, ::std::ostream* os) {
593 PrintTo(ImplicitCast_<const char16_t*>(s), os);
597 inline void PrintTo(char32_t* s, ::std::ostream* os) {
598 PrintTo(ImplicitCast_<const char32_t*>(s), os);
606 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) 609 inline void PrintTo(
wchar_t* s, ::std::ostream* os) {
610 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
619 template <
typename T>
622 for (
size_t i = 1;
i != count;
i++) {
630 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
635 #ifdef __cpp_lib_char8_t 636 GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
637 inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
638 PrintU8StringTo(s, os);
644 inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
650 inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
655 #if GTEST_HAS_STD_WSTRING 656 GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os);
657 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
658 PrintWideStringTo(s, os);
660 #endif // GTEST_HAS_STD_WSTRING 662 #if GTEST_INTERNAL_HAS_STRING_VIEW 664 inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
665 PrintTo(::std::string(sp), os);
667 #endif // GTEST_INTERNAL_HAS_STRING_VIEW 669 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os <<
"(nullptr)"; }
672 inline void PrintTo(
const std::type_info& info, std::ostream* os) {
675 #endif // GTEST_HAS_RTTI 677 template <
typename T>
678 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
684 return const_cast<const void*
>(p);
687 template <
typename T,
typename Ptr>
689 if (ptr ==
nullptr) {
696 template <
typename T,
typename Ptr,
697 typename =
typename std::enable_if<!std::is_void<T>::value &&
698 !std::is_array<T>::value>
::type>
700 if (ptr ==
nullptr) {
709 template <
typename T,
typename D>
710 void PrintTo(
const std::unique_ptr<T, D>& ptr, std::ostream* os) {
711 (PrintSmartPointer<T>)(ptr, os, 0);
714 template <
typename T>
715 void PrintTo(
const std::shared_ptr<T>& ptr, std::ostream* os) {
716 (PrintSmartPointer<T>)(ptr, os, 0);
721 template <
typename T>
725 template <
typename T,
size_t I>
727 ::std::ostream* os) {
728 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
735 std::get<I - 1>(t), os);
738 template <
typename...
Types>
739 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
746 template <
typename T1,
typename T2>
747 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
759 template <
typename T>
769 static
void Print(const T& value, ::
std::ostream* os) {
785 template <
typename T>
788 #if GTEST_INTERNAL_HAS_ANY 795 static void Print(
const Any& value, ::std::ostream* os) {
796 if (value.has_value()) {
808 static_cast<void>(value);
809 return "<unknown_type>";
810 #endif // GTEST_HAS_RTTI 814 #endif // GTEST_INTERNAL_HAS_ANY 816 #if GTEST_INTERNAL_HAS_OPTIONAL 820 template <
typename T>
823 static void Print(
const Optional<T>& value, ::std::ostream* os) {
837 static void Print(decltype(Nullopt()), ::std::ostream* os) {
842 #endif // GTEST_INTERNAL_HAS_OPTIONAL 844 #if GTEST_INTERNAL_HAS_VARIANT 848 template <
typename... T>
851 static void Print(
const Variant<T...>& value, ::std::ostream* os) {
854 absl::visit(Visitor{os, value.index()}, value);
856 std::visit(Visitor{os, value.index()}, value);
857 #endif // GTEST_HAS_ABSL 863 template <
typename U>
864 void operator()(
const U&
u)
const {
865 *os <<
"'" << GetTypeName<U>() <<
"(index = " <<
index 874 #endif // GTEST_INTERNAL_HAS_VARIANT 878 template <
typename T>
884 const size_t kThreshold = 18;
885 const size_t kChunkSize = 8;
889 if (len <= kThreshold) {
922 template <
typename T,
size_t N>
927 static void Print(
const T (&
a)[N], ::std::ostream* os) {
933 template <
typename T>
940 static
void Print(const T& value, ::
std::ostream* os) {
943 *os <<
"@" <<
reinterpret_cast<const void*
>(&value) <<
" ";
956 template <
typename T>
959 static void Print(
const T& value, ::std::ostream* os) {
963 template <
typename T>
966 static void Print(
const T& value, ::std::ostream* os) {
970 template <
typename T>
973 static void Print(std::reference_wrapper<T> value, ::std::ostream* os) {
977 template <
typename T,
size_t N>
980 static void Print(
const T (&value)[N], ::std::ostream* os) {
987 static void Print(
const char* str, ::std::ostream* os) {
988 if (str ==
nullptr) {
1003 static void Print(
const char8_t* str, ::std::ostream* os) {
1004 if (str ==
nullptr) {
1019 static void Print(
const char16_t* str, ::std::ostream* os) {
1020 if (str ==
nullptr) {
1034 static void Print(
const char32_t* str, ::std::ostream* os) {
1035 if (str ==
nullptr) {
1046 #if GTEST_HAS_STD_WSTRING 1050 static void Print(
const wchar_t* str, ::std::ostream* os) {
1051 if (str ==
nullptr) {
1063 static void Print(
wchar_t* str, ::std::ostream* os) {
1068 template <
typename T>
1077 template <
typename T>
1089 template <
typename Tuple>
1092 template <
typename Tuple,
size_t I>
1094 std::integral_constant<size_t, I>,
1098 ::std::stringstream ss;
1100 strings->push_back(ss.str());
1106 template <
typename Tuple>
1110 value, std::integral_constant<
size_t, std::tuple_size<Tuple>::value>(),
1117 template <
typename T>
1119 ::std::stringstream ss;
1131 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
static void PrintValue(const T &value, ::std::ostream *os)
static void PrintValue(T *p, ::std::ostream *os)
void PrintTo(const T &value, ::std::ostream *os)
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
::std::string PrintToString(const T &value)
static void Print(const T &value, ::std::ostream *os)
static void PrintValue(const T &, ::std::ostream *os)
GTEST_API_ void PrintStringTo(const ::std::string &s, ::std::ostream *os)
GTEST_API_ void PrintU32StringTo(const ::std::u32string &s, ::std::ostream *os)
void PrintWithFallback(const T &value, ::std::ostream *os)
::std::vector<::std::string > Strings
GTEST_API_ void PrintBytesInObjectTo(const unsigned char *obj_bytes, size_t count, ::std::ostream *os)
void PrintSmartPointer(const Ptr &ptr, std::ostream *os, char)
std::string FormatForComparisonFailureMessage(const T1 &value, const T2 &)
std::ostream & operator<<(std::ostream &os, const Message &sb)
static void Print(wchar_t *str, ::std::ostream *os)
static void Print(const T &value, ::std::ostream *os)
std::string GetTypeName()
void PrintTupleTo(const T &, std::integral_constant< size_t, 0 >, ::std::ostream *)
void UniversalPrintArray(const T *begin, size_t len, ::std::ostream *os)
static void PrintValue(internal::BiggestInt value, ::std::ostream *os)
static void Print(const T &value, ::std::ostream *os)
static void Print(std::reference_wrapper< T > value, ::std::ostream *os)
int AppropriateResolution(FloatType val)
void UniversalTersePrint(const T &value, ::std::ostream *os)
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string)
GTEST_DISABLE_MSC_WARNINGS_POP_() inline const char *SkipComma(const char *str)
static void PrintValue(const T &container, std::ostream *os)
static void Print(const T(&value)[N], ::std::ostream *os)
void PrintRawArrayTo(const T a[], size_t count, ::std::ostream *os)
const void * VoidifyPointer(const void *p)
static void PrintValue(T *p, ::std::ostream *os)
static void Print(const char16_t *str, ::std::ostream *os)
static void Print(const char *str, ::std::ostream *os)
static void Print(const char32_t *str, ::std::ostream *os)
Strings UniversalTersePrintTupleFieldsToStrings(const Tuple &value)
static void PrintValue(const T &value, ::std::ostream *os)
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251) class GTEST_API_ TypedTestSuitePState
void TersePrintPrefixToStrings(const Tuple &, std::integral_constant< size_t, 0 >, Strings *)
static void PrintValue(const T &value, ::std::ostream *os)
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char)
static void Print(const T(&a)[N], ::std::ostream *os)
GTEST_API_ void PrintU16StringTo(const ::std::u16string &s, ::std::ostream *os)
#define GTEST_INTENTIONAL_CONST_COND_POP_()
void UniversalPrint(const T &value, ::std::ostream *os)