104 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
105 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
107 #include <functional>
113 #include <type_traits>
118 #ifdef GTEST_HAS_ABSL
119 #include "absl/strings/has_absl_stringify.h"
120 #include "absl/strings/str_cat.h"
121 #endif // GTEST_HAS_ABSL
125 #if GTEST_INTERNAL_HAS_STD_SPAN
127 #endif // GTEST_INTERNAL_HAS_STD_SPAN
129 #if GTEST_INTERNAL_HAS_COMPARE_LIB
131 #endif // GTEST_INTERNAL_HAS_COMPARE_LIB
139 template <
typename T>
142 template <
typename T>
144 static constexpr
bool value =
false;
147 #if GTEST_INTERNAL_HAS_STD_SPAN
148 template <
typename E>
150 static constexpr
bool value =
true;
152 #endif // GTEST_INTERNAL_HAS_STD_SPAN
163 template <
typename T,
164 typename =
typename std::enable_if<
165 ((
sizeof(IsContainerTest<T>(0)) ==
sizeof(
IsContainer)) &&
169 const size_t kMaxCount = 32;
172 for (
auto&& elem : container) {
175 if (count == kMaxCount) {
201 template <
typename T,
typename =
typename std::enable_if<
210 *os << reinterpret_cast<const void*>(
p);
216 template <
typename T>
229 namespace internal_stream_operator_without_lexical_name_lookup {
239 template <
typename T,
242 typename =
typename std::enable_if<
249 ::std::ostream* os) -> decltype((
void)(*os <<
value)) {
264 template <
typename T,
265 typename =
typename std::enable_if<
268 std::string pretty_str = value.ShortDebugString();
270 pretty_str =
"\n" + value.DebugString();
272 *os << (
"<" + pretty_str +
">");
290 #if GTEST_INTERNAL_HAS_STRING_VIEW
291 static void PrintValue(internal::StringView
value, ::std::ostream* os) {
297 #ifdef GTEST_HAS_ABSL
298 struct ConvertibleToAbslStringifyPrinter {
299 template <
typename T,
300 typename =
typename std::enable_if<
303 *os << absl::StrCat(value);
306 #endif // GTEST_HAS_ABSL
311 size_t count, ::std::ostream* os);
314 template <
typename T,
size_t = sizeof(T)>
317 static_cast<const unsigned char*>(
319 reinterpret_cast<const void*>(std::addressof(value))),
325 template <
typename T>
327 *os <<
"(incomplete type)";
332 template <
typename T,
typename E,
typename Printer,
typename... Printers>
335 template <
typename T,
typename Printer,
typename... Printers>
337 T, decltype(Printer::
PrintValue(std::declval<const T&>(), nullptr)),
338 Printer, Printers...> {
351 template <
typename T>
356 #ifdef GTEST_HAS_ABSL
357 ConvertibleToAbslStringifyPrinter,
358 #endif // GTEST_HAS_ABSL
380 template <
typename ToPr
int,
typename OtherOperand>
389 template <
typename ToPr
int,
size_t N,
typename OtherOperand>
400 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
401 template <typename OtherOperand> \
402 class FormatForComparison<CharType*, OtherOperand> { \
404 static ::std::string Format(CharType* value) { \
405 return ::testing::PrintToString(static_cast<const void*>(value)); \
413 #ifdef __cpp_lib_char8_t
422 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
427 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
429 class FormatForComparison<CharType*, OtherStringType> { \
431 static ::std::string Format(CharType* value) { \
432 return ::testing::PrintToString(value); \
438 #ifdef __cpp_lib_char8_t
447 #if GTEST_HAS_STD_WSTRING
452 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
462 template <
typename T1,
typename T2>
475 template <
typename T>
489 template <
typename T>
505 PrintTo(static_cast<unsigned char>(c), os);
510 *os << (x ?
"true" :
"false");
523 inline void PrintTo(char16_t
c, ::std::ostream* os) {
524 PrintTo(ImplicitCast_<char32_t>(c), os);
526 #ifdef __cpp_lib_char8_t
527 inline void PrintTo(char8_t
c, ::std::ostream* os) {
528 PrintTo(ImplicitCast_<char32_t>(c), os);
533 #if defined(__SIZEOF_INT128__)
536 #endif // __SIZEOF_INT128__
554 template <
typename FloatType>
556 int full = std::numeric_limits<FloatType>::max_digits10;
557 if (val < 0) val = -
val;
560 #pragma GCC diagnostic push
561 #pragma GCC diagnostic ignored "-Wfloat-equal"
564 FloatType mulfor6 = 1e10;
568 if (val >= static_cast<FloatType>(100000.0)) {
570 }
else if (val >= static_cast<FloatType>(10000.0)) {
572 }
else if (val >= static_cast<FloatType>(1000.0)) {
574 }
else if (val >= static_cast<FloatType>(100.0)) {
576 }
else if (val >= static_cast<FloatType>(10.0)) {
578 }
else if (val >= static_cast<FloatType>(1.0)) {
580 }
else if (val >= static_cast<FloatType>(0.1)) {
582 }
else if (val >= static_cast<FloatType>(0.01)) {
584 }
else if (val >= static_cast<FloatType>(0.001)) {
586 }
else if (val >= static_cast<FloatType>(0.0001)) {
589 if (static_cast<FloatType>(static_cast<int32_t>(
590 val * mulfor6 + (static_cast<FloatType>(0.5)))) /
594 }
else if (val < static_cast<FloatType>(1e10)) {
595 FloatType divfor6 =
static_cast<FloatType
>(1.0);
596 if (val >= static_cast<FloatType>(1e9)) {
599 static_cast<FloatType>(1e8)) {
602 static_cast<FloatType>(1e7)) {
604 }
else if (val >= static_cast<FloatType>(1e6)) {
607 if (static_cast<FloatType>(static_cast<int32_t>(
608 val / divfor6 + (static_cast<FloatType>(0.5)))) *
614 #pragma GCC diagnostic pop
619 inline void PrintTo(
float f, ::std::ostream* os) {
620 auto old_precision = os->precision();
623 os->precision(old_precision);
626 inline void PrintTo(
double d, ::std::ostream* os) {
627 auto old_precision = os->precision();
630 os->precision(old_precision);
635 inline void PrintTo(
char* s, ::std::ostream* os) {
636 PrintTo(ImplicitCast_<const char*>(s), os);
641 inline void PrintTo(
const signed char* s, ::std::ostream* os) {
642 PrintTo(ImplicitCast_<const void*>(s), os);
644 inline void PrintTo(
signed char* s, ::std::ostream* os) {
645 PrintTo(ImplicitCast_<const void*>(s), os);
647 inline void PrintTo(
const unsigned char* s, ::std::ostream* os) {
648 PrintTo(ImplicitCast_<const void*>(s), os);
650 inline void PrintTo(
unsigned char* s, ::std::ostream* os) {
651 PrintTo(ImplicitCast_<const void*>(s), os);
653 #ifdef __cpp_lib_char8_t
656 inline void PrintTo(char8_t* s, ::std::ostream* os) {
657 PrintTo(ImplicitCast_<const char8_t*>(s), os);
662 inline void PrintTo(char16_t* s, ::std::ostream* os) {
663 PrintTo(ImplicitCast_<const char16_t*>(s), os);
667 inline void PrintTo(char32_t* s, ::std::ostream* os) {
668 PrintTo(ImplicitCast_<const char32_t*>(s), os);
676 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
679 inline void PrintTo(
wchar_t* s, ::std::ostream* os) {
680 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
689 template <
typename T>
692 for (
size_t i = 1;
i !=
count;
i++) {
700 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
705 #ifdef __cpp_lib_char8_t
706 GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
707 inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
708 PrintU8StringTo(s, os);
714 inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
720 inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
725 #if GTEST_HAS_STD_WSTRING
726 GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os);
727 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
728 PrintWideStringTo(s, os);
730 #endif // GTEST_HAS_STD_WSTRING
732 #if GTEST_INTERNAL_HAS_STRING_VIEW
734 inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
735 PrintTo(::std::string(sp), os);
737 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
739 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os <<
"(nullptr)"; }
742 inline void PrintTo(
const std::type_info& info, std::ostream* os) {
745 #endif // GTEST_HAS_RTTI
747 template <
typename T>
748 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
754 return const_cast<const void*
>(
p);
757 template <
typename T,
typename Ptr>
759 if (ptr ==
nullptr) {
766 template <
typename T,
typename Ptr,
770 if (ptr ==
nullptr) {
779 template <
typename T,
typename D>
780 void PrintTo(
const std::unique_ptr<T, D>& ptr, std::ostream* os) {
781 (PrintSmartPointer<T>)(ptr, os, 0);
784 template <
typename T>
785 void PrintTo(
const std::shared_ptr<T>& ptr, std::ostream* os) {
786 (PrintSmartPointer<T>)(ptr, os, 0);
789 #if GTEST_INTERNAL_HAS_COMPARE_LIB
790 template <
typename T>
791 void PrintOrderingHelper(
T ordering, std::ostream* os) {
792 if (ordering == T::less) {
794 }
else if (ordering == T::greater) {
796 }
else if (ordering == T::equivalent) {
797 *os <<
"(equivalent)";
799 *os <<
"(unknown ordering)";
803 inline void PrintTo(std::strong_ordering ordering, std::ostream* os) {
804 if (ordering == std::strong_ordering::equal) {
807 PrintOrderingHelper(ordering, os);
811 inline void PrintTo(std::partial_ordering ordering, std::ostream* os) {
812 if (ordering == std::partial_ordering::unordered) {
813 *os <<
"(unordered)";
815 PrintOrderingHelper(ordering, os);
819 inline void PrintTo(std::weak_ordering ordering, std::ostream* os) {
820 PrintOrderingHelper(ordering, os);
826 template <
typename T>
830 template <
typename T,
size_t I>
832 ::std::ostream* os) {
833 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
840 std::get<I - 1>(t), os);
843 template <
typename...
Types>
844 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
851 template <
typename T1,
typename T2>
864 template <
typename T>
865 class UniversalPrinter {
890 template <
typename T>
893 #if GTEST_INTERNAL_HAS_ANY
900 static void Print(
const Any&
value, ::std::ostream* os) {
901 if (value.has_value()) {
913 static_cast<void>(
value);
914 return "<unknown_type>";
915 #endif // GTEST_HAS_RTTI
919 #endif // GTEST_INTERNAL_HAS_ANY
921 #if GTEST_INTERNAL_HAS_OPTIONAL
925 template <
typename T>
926 class UniversalPrinter<Optional<
T>> {
928 static void Print(
const Optional<T>& value, ::std::ostream* os) {
940 class UniversalPrinter<decltype(Nullopt())> {
942 static void Print(decltype(Nullopt()), ::std::ostream* os) {
947 #endif // GTEST_INTERNAL_HAS_OPTIONAL
949 #if GTEST_INTERNAL_HAS_VARIANT
953 template <
typename...
T>
954 class UniversalPrinter<Variant<
T...>> {
956 static void Print(
const Variant<T...>& value, ::std::ostream* os) {
958 #ifdef GTEST_HAS_ABSL
959 absl::visit(Visitor{os, value.index()},
value);
961 std::visit(Visitor{os, value.index()},
value);
962 #endif // GTEST_HAS_ABSL
968 template <
typename U>
969 void operator()(
const U& u)
const {
970 *os <<
"'" << GetTypeName<U>() <<
"(index = " << index
979 #endif // GTEST_INTERNAL_HAS_VARIANT
983 template <
typename T>
989 const size_t kThreshold = 18;
990 const size_t kChunkSize = 8;
994 if (len <= kThreshold) {
1006 ::std::ostream* os);
1008 #ifdef __cpp_lib_char8_t
1011 ::std::ostream* os);
1016 ::std::ostream* os);
1020 ::std::ostream* os);
1024 ::std::ostream* os);
1027 template <
typename T,
size_t N>
1032 static void Print(
const T (&
a)[
N], ::std::ostream* os) {
1038 template <
typename T>
1045 static
void Print(const
T& value, ::std::ostream* os) {
1048 *os <<
"@" <<
reinterpret_cast<const void*
>(&
value) <<
" ";
1061 template <
typename T>
1064 static void Print(
const T& value, ::std::ostream* os) {
1068 template <
typename T>
1071 static void Print(
const T& value, ::std::ostream* os) {
1075 template <
typename T>
1078 static void Print(std::reference_wrapper<T> value, ::std::ostream* os) {
1082 template <
typename T,
size_t N>
1085 static void Print(
const T (&value)[
N], ::std::ostream* os) {
1092 static void Print(
const char* str, ::std::ostream* os) {
1093 if (str ==
nullptr) {
1104 #ifdef __cpp_lib_char8_t
1108 static void Print(
const char8_t* str, ::std::ostream* os) {
1109 if (str ==
nullptr) {
1117 class UniversalTersePrinter<char8_t*>
1118 :
public UniversalTersePrinter<const char8_t*> {};
1124 static void Print(
const char16_t* str, ::std::ostream* os) {
1125 if (str ==
nullptr) {
1139 static void Print(
const char32_t* str, ::std::ostream* os) {
1140 if (str ==
nullptr) {
1151 #if GTEST_HAS_STD_WSTRING
1155 static void Print(
const wchar_t* str, ::std::ostream* os) {
1156 if (str ==
nullptr) {
1168 static void Print(
wchar_t* str, ::std::ostream* os) {
1173 template <
typename T>
1182 template <
typename T>
1194 template <
typename Tuple>
1197 template <
typename Tuple,
size_t I>
1199 std::integral_constant<size_t, I>,
1203 ::std::stringstream ss;
1205 strings->push_back(ss.str());
1211 template <
typename Tuple>
1222 template <
typename T>
1224 ::std::stringstream ss;
1236 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
static void Print(const T &value,::std::ostream *os)
GTEST_API_ void PrintU32StringTo(const ::std::u32string &s,::std::ostream *os)
void PrintRawArrayTo(const T a[], size_t count,::std::ostream *os)
::std::string PrintValue(const T &value)
static void Print(wchar_t *str,::std::ostream *os)
#define GTEST_INTENTIONAL_CONST_COND_PUSH_()
static void Print(const T &value,::std::ostream *os)
GTEST_API_ void PrintStringTo(const ::std::string &s,::std::ostream *os)
::std::string PrintToString(const T &value)
static void Print(const T(&value)[N],::std::ostream *os)
void UniversalTersePrint(const T &value,::std::ostream *os)
std::string Print(const T &value)
static constexpr bool value
static const size_t kProtobufOneLinerMaxLength
static void Print(const T(&a)[N],::std::ostream *os)
::std::vector<::std::string > Strings
GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char,::std::string)
void PrintSmartPointer(const Ptr &ptr, std::ostream *os, char)
static void Print(const char *str,::std::ostream *os)
GTEST_API_ void PrintU16StringTo(const ::std::u16string &s,::std::ostream *os)
std::string FormatForComparisonFailureMessage(const T1 &value, const T2 &)
std::string GetTypeName()
static void Print(std::reference_wrapper< T > value,::std::ostream *os)
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
GTEST_API_ void PrintBytesInObjectTo(const unsigned char *obj_bytes, size_t count,::std::ostream *os)
static void PrintValue(const T &value,::std::ostream *os)
int AppropriateResolution(FloatType val)
static void PrintValue(const T &,::std::ostream *os)
void UniversalPrint(const T &value,::std::ostream *os)
GTEST_DISABLE_MSC_WARNINGS_POP_() inline const char *SkipComma(const char *str)
static void PrintValue(const T &container, std::ostream *os)
static void PrintValue(const T &value,::std::ostream *os)
const void * VoidifyPointer(const void *p)
void operator<<(LookupBlocker, LookupBlocker)
static void PrintValue(T *p,::std::ostream *os)
void PrintTo(const T &value,::std::ostream *os)
void PrintWithFallback(const T &value,::std::ostream *os)
static auto PrintValue(const T &value,::std::ostream *os) -> decltype((void)(*os<< value))
void PrintTupleTo(const T &, std::integral_constant< size_t, 0 >,::std::ostream *)
Strings UniversalTersePrintTupleFieldsToStrings(const Tuple &value)
static void PrintValue(T *p,::std::ostream *os)
void UniversalPrintArray(const T *begin, size_t len,::std::ostream *os)
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251) class GTEST_API_ TypedTestSuitePState
static void Print(const char16_t *str,::std::ostream *os)
void TersePrintPrefixToStrings(const Tuple &, std::integral_constant< size_t, 0 >, Strings *)
GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char)
static void Print(const char32_t *str,::std::ostream *os)
static void Print(const T &value,::std::ostream *os)
static void PrintValue(internal::BiggestInt value,::std::ostream *os)
#define GTEST_INTENTIONAL_CONST_COND_POP_()