1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Google Test - The Google C++ Testing and Mocking Framework
32 // This file implements a universal value printer that can print a
33 // value of any type T:
35 // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
37 // A user can teach this function how to print a class type T by
38 // defining either operator<<() or PrintTo() in the namespace that
39 // defines T. More specifically, the FIRST defined function in the
40 // following list will be used (assuming T is defined in namespace
43 // 1. foo::PrintTo(const T&, ostream*)
44 // 2. operator<<(ostream&, const T&) defined in either foo or the
46 // * Prefer AbslStringify(..) to operator<<(..), per https://abseil.io/tips/215.
47 // * Define foo::PrintTo(..) if the type already has AbslStringify(..), but an
48 // alternative presentation in test results is of interest.
50 // However if T is an STL-style container then it is printed element-wise
51 // unless foo::PrintTo(const T&, ostream*) is defined. Note that
52 // operator<<() is ignored for container types.
54 // If none of the above is defined, it will print the debug string of
55 // the value if it is a protocol buffer, or print the raw bytes in the
58 // To aid debugging: when T is a reference type, the address of the
59 // value is also printed; when T is a (const) char pointer, both the
60 // pointer value and the NUL-terminated string it points to are
63 // We also provide some convenient wrappers:
65 // // Prints a value to a string. For a (const or not) char
66 // // pointer, the NUL-terminated string (but not the pointer) is
68 // std::string ::testing::PrintToString(const T& value);
70 // // Prints a value tersely: for a reference type, the referenced
71 // // value (but not the address) is printed; for a (const or not) char
72 // // pointer, the NUL-terminated string (but not the pointer) is
74 // void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
76 // // Prints value using the type inferred by the compiler. The difference
77 // // from UniversalTersePrint() is that this function prints both the
78 // // pointer and the NUL-terminated string for a (const or not) char pointer.
79 // void ::testing::internal::UniversalPrint(const T& value, ostream*);
81 // // Prints the fields of a tuple tersely to a string vector, one
82 // // element for each field. Tuple support must be enabled in
84 // std::vector<string> UniversalTersePrintTupleFieldsToStrings(
85 // const Tuple& value);
89 // The print primitives print the elements of an STL-style container
90 // using the compiler-inferred type of *iter where iter is a
91 // const_iterator of the container. When const_iterator is an input
92 // iterator but not a forward iterator, this inferred type may not
93 // match value_type, and the print output may be incorrect. In
94 // practice, this is rarely a problem as for most containers
95 // const_iterator is a forward iterator. We'll fix this if there's an
96 // actual need for it. Note that this fix cannot rely on value_type
97 // being defined as many user-defined container types don't have
100 // IWYU pragma: private, include "gtest/gtest.h"
101 // IWYU pragma: friend gtest/.*
102 // IWYU pragma: friend gmock/.*
104 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
105 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
107 #include <functional>
109 #include <ostream> // NOLINT
113 #include <type_traits>
118 #ifdef GTEST_HAS_ABSL
119 #include "absl/strings/internal/has_absl_stringify.h"
120 #include "absl/strings/str_cat.h"
121 #endif // GTEST_HAS_ABSL
122 #include "gtest/internal/gtest-internal.h"
123 #include "gtest/internal/gtest-port.h"
127 // Definitions in the internal* namespaces are subject to change without notice.
128 // DO NOT USE THEM IN USER CODE!
131 template <typename T
>
132 void UniversalPrint(const T
& value
, ::std::ostream
* os
);
134 // Used to print an STL-style container when the user doesn't define
135 // a PrintTo() for it.
136 struct ContainerPrinter
{
137 template <typename T
,
138 typename
= typename
std::enable_if
<
139 (sizeof(IsContainerTest
<T
>(0)) == sizeof(IsContainer
)) &&
140 !IsRecursiveContainer
<T
>::value
>::type
>
141 static void PrintValue(const T
& container
, std::ostream
* os
) {
142 const size_t kMaxCount
= 32; // The maximum number of elements to print.
145 for (auto&& elem
: container
) {
148 if (count
== kMaxCount
) { // Enough has been printed.
154 // We cannot call PrintTo(elem, os) here as PrintTo() doesn't
155 // handle `elem` being a native array.
156 internal::UniversalPrint(elem
, os
);
167 // Used to print a pointer that is neither a char pointer nor a member
168 // pointer, when the user doesn't define PrintTo() for it. (A member
169 // variable pointer or member function pointer doesn't really point to
170 // a location in the address space. Their representation is
171 // implementation-defined. Therefore they will be printed as raw
173 struct FunctionPointerPrinter
{
174 template <typename T
, typename
= typename
std::enable_if
<
175 std::is_function
<T
>::value
>::type
>
176 static void PrintValue(T
* p
, ::std::ostream
* os
) {
180 // T is a function type, so '*os << p' doesn't do what we want
181 // (it just prints p as bool). We want to print p as a const
183 *os
<< reinterpret_cast<const void*>(p
);
188 struct PointerPrinter
{
189 template <typename T
>
190 static void PrintValue(T
* p
, ::std::ostream
* os
) {
194 // T is not a function type. We just call << to print p,
195 // relying on ADL to pick up user-defined << for their pointer
202 namespace internal_stream_operator_without_lexical_name_lookup
{
204 // The presence of an operator<< here will terminate lexical scope lookup
205 // straight away (even though it cannot be a match because of its argument
206 // types). Thus, the two operator<< calls in StreamPrinter will find only ADL
208 struct LookupBlocker
{};
209 void operator<<(LookupBlocker
, LookupBlocker
);
211 struct StreamPrinter
{
212 template <typename T
,
213 // Don't accept member pointers here. We'd print them via implicit
214 // conversion to bool, which isn't useful.
215 typename
= typename
std::enable_if
<
216 !std::is_member_pointer
<T
>::value
>::type
>
217 // Only accept types for which we can find a streaming operator via
218 // ADL (possibly involving implicit conversions).
219 // (Use SFINAE via return type, because it seems GCC < 12 doesn't handle name
220 // lookup properly when we do it in the template parameter list.)
222 // LLVM local change to support llvm printables.
224 // static auto PrintValue(const T& value, ::std::ostream* os)
225 // -> decltype((void)(*os << value)) {
226 // // Call streaming operator found by ADL, possibly with implicit conversions
227 // // of the arguments.
228 // // LLVM local change to support llvm printables.
231 // // LLVM local change end.
233 static auto PrintValue(const T
& value
, ::std::ostream
* os
)
234 -> decltype((void)(*os
<< ::llvm_gtest::printable(value
))) {
235 // Call streaming operator found by ADL, possibly with implicit conversions
237 // LLVM local change to support llvm printables.
239 *os
<< ::llvm_gtest::printable(value
);
240 // LLVM local change end.
244 } // namespace internal_stream_operator_without_lexical_name_lookup
246 struct ProtobufPrinter
{
247 // We print a protobuf using its ShortDebugString() when the string
248 // doesn't exceed this many characters; otherwise we print it using
249 // DebugString() for better readability.
250 static const size_t kProtobufOneLinerMaxLength
= 50;
252 template <typename T
,
253 typename
= typename
std::enable_if
<
254 internal::HasDebugStringAndShortDebugString
<T
>::value
>::type
>
255 static void PrintValue(const T
& value
, ::std::ostream
* os
) {
256 std::string pretty_str
= value
.ShortDebugString();
257 if (pretty_str
.length() > kProtobufOneLinerMaxLength
) {
258 pretty_str
= "\n" + value
.DebugString();
260 *os
<< ("<" + pretty_str
+ ">");
264 struct ConvertibleToIntegerPrinter
{
265 // Since T has no << operator or PrintTo() but can be implicitly
266 // converted to BiggestInt, we print it as a BiggestInt.
268 // Most likely T is an enum type (either named or unnamed), in which
269 // case printing it as an integer is the desired behavior. In case
270 // T is not an enum, printing it as an integer is the best we can do
271 // given that it has no user-defined printer.
272 static void PrintValue(internal::BiggestInt value
, ::std::ostream
* os
) {
277 struct ConvertibleToStringViewPrinter
{
278 #if GTEST_INTERNAL_HAS_STRING_VIEW
279 static void PrintValue(internal::StringView value
, ::std::ostream
* os
) {
280 internal::UniversalPrint(value
, os
);
285 #ifdef GTEST_HAS_ABSL
286 struct ConvertibleToAbslStringifyPrinter
{
289 typename
= typename
std::enable_if
<
290 absl::strings_internal::HasAbslStringify
<T
>::value
>::type
> // NOLINT
291 static void PrintValue(const T
& value
, ::std::ostream
* os
) {
292 *os
<< absl::StrCat(value
);
295 #endif // GTEST_HAS_ABSL
297 // Prints the given number of bytes in the given object to the given
299 GTEST_API_
void PrintBytesInObjectTo(const unsigned char* obj_bytes
,
300 size_t count
, ::std::ostream
* os
);
301 struct RawBytesPrinter
{
302 // SFINAE on `sizeof` to make sure we have a complete type.
303 template <typename T
, size_t = sizeof(T
)>
304 static void PrintValue(const T
& value
, ::std::ostream
* os
) {
305 PrintBytesInObjectTo(
306 static_cast<const unsigned char*>(
307 // Load bearing cast to void* to support iOS
308 reinterpret_cast<const void*>(std::addressof(value
))),
313 struct FallbackPrinter
{
314 template <typename T
>
315 static void PrintValue(const T
&, ::std::ostream
* os
) {
316 *os
<< "(incomplete type)";
320 // Try every printer in order and return the first one that works.
321 template <typename T
, typename E
, typename Printer
, typename
... Printers
>
322 struct FindFirstPrinter
: FindFirstPrinter
<T
, E
, Printers
...> {};
324 template <typename T
, typename Printer
, typename
... Printers
>
325 struct FindFirstPrinter
<
326 T
, decltype(Printer::PrintValue(std::declval
<const T
&>(), nullptr)),
327 Printer
, Printers
...> {
328 using type
= Printer
;
331 // Select the best printer in the following order:
332 // - Print containers (they have begin/end/etc).
333 // - Print function pointers.
334 // - Print object pointers.
335 // - Print protocol buffers.
336 // - Use the stream operator, if available.
337 // - Print types convertible to BiggestInt.
338 // - Print types convertible to StringView, if available.
339 // - Fallback to printing the raw bytes of the object.
340 template <typename T
>
341 void PrintWithFallback(const T
& value
, ::std::ostream
* os
) {
342 using Printer
= typename FindFirstPrinter
<
343 T
, void, ContainerPrinter
, FunctionPointerPrinter
, PointerPrinter
,
345 #ifdef GTEST_HAS_ABSL
346 ConvertibleToAbslStringifyPrinter
,
347 #endif // GTEST_HAS_ABSL
348 internal_stream_operator_without_lexical_name_lookup::StreamPrinter
,
349 ConvertibleToIntegerPrinter
, ConvertibleToStringViewPrinter
,
350 RawBytesPrinter
, FallbackPrinter
>::type
;
351 Printer::PrintValue(value
, os
);
354 // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
355 // value of type ToPrint that is an operand of a comparison assertion
356 // (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
357 // the comparison, and is used to help determine the best way to
358 // format the value. In particular, when the value is a C string
359 // (char pointer) and the other operand is an STL string object, we
360 // want to format the C string as a string, since we know it is
361 // compared by value with the string object. If the value is a char
362 // pointer but the other operand is not an STL string object, we don't
363 // know whether the pointer is supposed to point to a NUL-terminated
364 // string, and thus want to print it as a pointer to be safe.
366 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
369 template <typename ToPrint
, typename OtherOperand
>
370 class FormatForComparison
{
372 static ::std::string
Format(const ToPrint
& value
) {
373 return ::testing::PrintToString(value
);
378 template <typename ToPrint
, size_t N
, typename OtherOperand
>
379 class FormatForComparison
<ToPrint
[N
], OtherOperand
> {
381 static ::std::string
Format(const ToPrint
* value
) {
382 return FormatForComparison
<const ToPrint
*, OtherOperand
>::Format(value
);
386 // By default, print C string as pointers to be safe, as we don't know
387 // whether they actually point to a NUL-terminated string.
389 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
390 template <typename OtherOperand> \
391 class FormatForComparison<CharType*, OtherOperand> { \
393 static ::std::string Format(CharType* value) { \
394 return ::testing::PrintToString(static_cast<const void*>(value)); \
398 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
399 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
400 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
401 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
402 #ifdef __cpp_lib_char8_t
403 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t
);
404 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t
);
406 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t
);
407 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t
);
408 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t
);
409 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t
);
411 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
413 // If a C string is compared with an STL string object, we know it's meant
414 // to point to a NUL-terminated string, and thus can print it as a string.
416 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
418 class FormatForComparison<CharType*, OtherStringType> { \
420 static ::std::string Format(CharType* value) { \
421 return ::testing::PrintToString(value); \
425 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string
);
426 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string
);
427 #ifdef __cpp_lib_char8_t
428 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t
, ::std::u8string
);
429 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t
, ::std::u8string
);
431 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t
, ::std::u16string
);
432 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t
, ::std::u16string
);
433 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t
, ::std::u32string
);
434 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t
, ::std::u32string
);
436 #if GTEST_HAS_STD_WSTRING
437 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring
);
438 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring
);
441 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
443 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
444 // operand to be used in a failure message. The type (but not value)
445 // of the other operand may affect the format. This allows us to
446 // print a char* as a raw pointer when it is compared against another
447 // char* or void*, and print it as a C string when it is compared
448 // against an std::string object, for example.
450 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
451 template <typename T1
, typename T2
>
452 std::string
FormatForComparisonFailureMessage(const T1
& value
,
453 const T2
& /* other_operand */) {
454 return FormatForComparison
<T1
, T2
>::Format(value
);
457 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
458 // value to the given ostream. The caller must ensure that
459 // 'ostream_ptr' is not NULL, or the behavior is undefined.
461 // We define UniversalPrinter as a class template (as opposed to a
462 // function template), as we need to partially specialize it for
463 // reference types, which cannot be done with function templates.
464 template <typename T
>
465 class UniversalPrinter
;
467 // Prints the given value using the << operator if it has one;
468 // otherwise prints the bytes in it. This is what
469 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
470 // or overloaded for type T.
472 // A user can override this behavior for a class type Foo by defining
473 // an overload of PrintTo() in the namespace where Foo is defined. We
474 // give the user this option as sometimes defining a << operator for
475 // Foo is not desirable (e.g. the coding style may prevent doing it,
476 // or there is already a << operator but it doesn't do what the user
478 template <typename T
>
479 void PrintTo(const T
& value
, ::std::ostream
* os
) {
480 internal::PrintWithFallback(value
, os
);
483 // The following list of PrintTo() overloads tells
484 // UniversalPrinter<T>::Print() how to print standard types (built-in
485 // types, strings, plain arrays, and pointers).
487 // Overloads for various char types.
488 GTEST_API_
void PrintTo(unsigned char c
, ::std::ostream
* os
);
489 GTEST_API_
void PrintTo(signed char c
, ::std::ostream
* os
);
490 inline void PrintTo(char c
, ::std::ostream
* os
) {
491 // When printing a plain char, we always treat it as unsigned. This
492 // way, the output won't be affected by whether the compiler thinks
493 // char is signed or not.
494 PrintTo(static_cast<unsigned char>(c
), os
);
497 // Overloads for other simple built-in types.
498 inline void PrintTo(bool x
, ::std::ostream
* os
) {
499 *os
<< (x
? "true" : "false");
502 // Overload for wchar_t type.
503 // Prints a wchar_t as a symbol if it is printable or as its internal
504 // code otherwise and also as its decimal code (except for L'\0').
505 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
506 // as signed integer when wchar_t is implemented by the compiler
507 // as a signed type and is printed as an unsigned integer when wchar_t
508 // is implemented as an unsigned type.
509 GTEST_API_
void PrintTo(wchar_t wc
, ::std::ostream
* os
);
511 GTEST_API_
void PrintTo(char32_t c
, ::std::ostream
* os
);
512 inline void PrintTo(char16_t c
, ::std::ostream
* os
) {
513 PrintTo(ImplicitCast_
<char32_t
>(c
), os
);
515 #ifdef __cpp_lib_char8_t
516 inline void PrintTo(char8_t c
, ::std::ostream
* os
) {
517 PrintTo(ImplicitCast_
<char32_t
>(c
), os
);
521 // gcc/clang __{u,}int128_t
522 #if defined(__SIZEOF_INT128__)
523 GTEST_API_
void PrintTo(__uint128_t v
, ::std::ostream
* os
);
524 GTEST_API_
void PrintTo(__int128_t v
, ::std::ostream
* os
);
525 #endif // __SIZEOF_INT128__
527 // The default resolution used to print floating-point values uses only
528 // 6 digits, which can be confusing if a test compares two values whose
529 // difference lies in the 7th digit. So we'd like to print out numbers
530 // in full precision.
531 // However if the value is something simple like 1.1, full will print a
532 // long string like 1.100000001 due to floating-point numbers not using
533 // a base of 10. This routiune returns an appropriate resolution for a
534 // given floating-point number, that is, 6 if it will be accurate, or a
535 // max_digits10 value (full precision) if it won't, for values between
536 // 0.0001 and one million.
537 // It does this by computing what those digits would be (by multiplying
538 // by an appropriate power of 10), then dividing by that power again to
539 // see if gets the original value back.
540 // A similar algorithm applies for values larger than one million; note
541 // that for those values, we must divide to get a six-digit number, and
542 // then multiply to possibly get the original value again.
543 template <typename FloatType
>
544 int AppropriateResolution(FloatType val
) {
545 int full
= std::numeric_limits
<FloatType
>::max_digits10
;
546 if (val
< 0) val
= -val
;
549 FloatType mulfor6
= 1e10
;
550 if (val
>= 100000.0) { // 100,000 to 999,999
552 } else if (val
>= 10000.0) {
554 } else if (val
>= 1000.0) {
556 } else if (val
>= 100.0) {
558 } else if (val
>= 10.0) {
560 } else if (val
>= 1.0) {
562 } else if (val
>= 0.1) {
564 } else if (val
>= 0.01) {
566 } else if (val
>= 0.001) {
568 } else if (val
>= 0.0001) {
571 if (static_cast<FloatType
>(static_cast<int32_t>(val
* mulfor6
+ 0.5)) /
575 } else if (val
< 1e10
) {
576 FloatType divfor6
= 1.0;
577 if (val
>= 1e9
) { // 1,000,000,000 to 9,999,999,999
579 } else if (val
>= 1e8
) { // 100,000,000 to 999,999,999
581 } else if (val
>= 1e7
) { // 10,000,000 to 99,999,999
583 } else if (val
>= 1e6
) { // 1,000,000 to 9,999,999
586 if (static_cast<FloatType
>(static_cast<int32_t>(val
/ divfor6
+ 0.5)) *
594 inline void PrintTo(float f
, ::std::ostream
* os
) {
595 auto old_precision
= os
->precision();
596 os
->precision(AppropriateResolution(f
));
598 os
->precision(old_precision
);
601 inline void PrintTo(double d
, ::std::ostream
* os
) {
602 auto old_precision
= os
->precision();
603 os
->precision(AppropriateResolution(d
));
605 os
->precision(old_precision
);
608 // Overloads for C strings.
609 GTEST_API_
void PrintTo(const char* s
, ::std::ostream
* os
);
610 inline void PrintTo(char* s
, ::std::ostream
* os
) {
611 PrintTo(ImplicitCast_
<const char*>(s
), os
);
614 // signed/unsigned char is often used for representing binary data, so
615 // we print pointers to it as void* to be safe.
616 inline void PrintTo(const signed char* s
, ::std::ostream
* os
) {
617 PrintTo(ImplicitCast_
<const void*>(s
), os
);
619 inline void PrintTo(signed char* s
, ::std::ostream
* os
) {
620 PrintTo(ImplicitCast_
<const void*>(s
), os
);
622 inline void PrintTo(const unsigned char* s
, ::std::ostream
* os
) {
623 PrintTo(ImplicitCast_
<const void*>(s
), os
);
625 inline void PrintTo(unsigned char* s
, ::std::ostream
* os
) {
626 PrintTo(ImplicitCast_
<const void*>(s
), os
);
628 #ifdef __cpp_lib_char8_t
629 // Overloads for u8 strings.
630 GTEST_API_
void PrintTo(const char8_t
* s
, ::std::ostream
* os
);
631 inline void PrintTo(char8_t
* s
, ::std::ostream
* os
) {
632 PrintTo(ImplicitCast_
<const char8_t
*>(s
), os
);
635 // Overloads for u16 strings.
636 GTEST_API_
void PrintTo(const char16_t
* s
, ::std::ostream
* os
);
637 inline void PrintTo(char16_t
* s
, ::std::ostream
* os
) {
638 PrintTo(ImplicitCast_
<const char16_t
*>(s
), os
);
640 // Overloads for u32 strings.
641 GTEST_API_
void PrintTo(const char32_t
* s
, ::std::ostream
* os
);
642 inline void PrintTo(char32_t
* s
, ::std::ostream
* os
) {
643 PrintTo(ImplicitCast_
<const char32_t
*>(s
), os
);
646 // MSVC can be configured to define wchar_t as a typedef of unsigned
647 // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
648 // type. When wchar_t is a typedef, defining an overload for const
649 // wchar_t* would cause unsigned short* be printed as a wide string,
650 // possibly causing invalid memory accesses.
651 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
652 // Overloads for wide C strings
653 GTEST_API_
void PrintTo(const wchar_t* s
, ::std::ostream
* os
);
654 inline void PrintTo(wchar_t* s
, ::std::ostream
* os
) {
655 PrintTo(ImplicitCast_
<const wchar_t*>(s
), os
);
659 // Overload for C arrays. Multi-dimensional arrays are printed
662 // Prints the given number of elements in an array, without printing
664 template <typename T
>
665 void PrintRawArrayTo(const T a
[], size_t count
, ::std::ostream
* os
) {
666 UniversalPrint(a
[0], os
);
667 for (size_t i
= 1; i
!= count
; i
++) {
669 UniversalPrint(a
[i
], os
);
673 // Overloads for ::std::string.
674 GTEST_API_
void PrintStringTo(const ::std::string
& s
, ::std::ostream
* os
);
675 inline void PrintTo(const ::std::string
& s
, ::std::ostream
* os
) {
676 PrintStringTo(s
, os
);
679 // Overloads for ::std::u8string
680 #ifdef __cpp_lib_char8_t
681 GTEST_API_
void PrintU8StringTo(const ::std::u8string
& s
, ::std::ostream
* os
);
682 inline void PrintTo(const ::std::u8string
& s
, ::std::ostream
* os
) {
683 PrintU8StringTo(s
, os
);
687 // Overloads for ::std::u16string
688 GTEST_API_
void PrintU16StringTo(const ::std::u16string
& s
, ::std::ostream
* os
);
689 inline void PrintTo(const ::std::u16string
& s
, ::std::ostream
* os
) {
690 PrintU16StringTo(s
, os
);
693 // Overloads for ::std::u32string
694 GTEST_API_
void PrintU32StringTo(const ::std::u32string
& s
, ::std::ostream
* os
);
695 inline void PrintTo(const ::std::u32string
& s
, ::std::ostream
* os
) {
696 PrintU32StringTo(s
, os
);
699 // Overloads for ::std::wstring.
700 #if GTEST_HAS_STD_WSTRING
701 GTEST_API_
void PrintWideStringTo(const ::std::wstring
& s
, ::std::ostream
* os
);
702 inline void PrintTo(const ::std::wstring
& s
, ::std::ostream
* os
) {
703 PrintWideStringTo(s
, os
);
705 #endif // GTEST_HAS_STD_WSTRING
707 #if GTEST_INTERNAL_HAS_STRING_VIEW
708 // Overload for internal::StringView.
709 inline void PrintTo(internal::StringView sp
, ::std::ostream
* os
) {
710 PrintTo(::std::string(sp
), os
);
712 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
714 inline void PrintTo(std::nullptr_t
, ::std::ostream
* os
) { *os
<< "(nullptr)"; }
717 inline void PrintTo(const std::type_info
& info
, std::ostream
* os
) {
718 *os
<< internal::GetTypeName(info
);
720 #endif // GTEST_HAS_RTTI
722 template <typename T
>
723 void PrintTo(std::reference_wrapper
<T
> ref
, ::std::ostream
* os
) {
724 UniversalPrinter
<T
&>::Print(ref
.get(), os
);
727 inline const void* VoidifyPointer(const void* p
) { return p
; }
728 inline const void* VoidifyPointer(volatile const void* p
) {
729 return const_cast<const void*>(p
);
732 template <typename T
, typename Ptr
>
733 void PrintSmartPointer(const Ptr
& ptr
, std::ostream
* os
, char) {
734 if (ptr
== nullptr) {
737 // We can't print the value. Just print the pointer..
738 *os
<< "(" << (VoidifyPointer
)(ptr
.get()) << ")";
741 template <typename T
, typename Ptr
,
742 typename
= typename
std::enable_if
<!std::is_void
<T
>::value
&&
743 !std::is_array
<T
>::value
>::type
>
744 void PrintSmartPointer(const Ptr
& ptr
, std::ostream
* os
, int) {
745 if (ptr
== nullptr) {
748 *os
<< "(ptr = " << (VoidifyPointer
)(ptr
.get()) << ", value = ";
749 UniversalPrinter
<T
>::Print(*ptr
, os
);
754 template <typename T
, typename D
>
755 void PrintTo(const std::unique_ptr
<T
, D
>& ptr
, std::ostream
* os
) {
756 (PrintSmartPointer
<T
>)(ptr
, os
, 0);
759 template <typename T
>
760 void PrintTo(const std::shared_ptr
<T
>& ptr
, std::ostream
* os
) {
761 (PrintSmartPointer
<T
>)(ptr
, os
, 0);
764 // Helper function for printing a tuple. T must be instantiated with
766 template <typename T
>
767 void PrintTupleTo(const T
&, std::integral_constant
<size_t, 0>,
770 template <typename T
, size_t I
>
771 void PrintTupleTo(const T
& t
, std::integral_constant
<size_t, I
>,
772 ::std::ostream
* os
) {
773 PrintTupleTo(t
, std::integral_constant
<size_t, I
- 1>(), os
);
774 GTEST_INTENTIONAL_CONST_COND_PUSH_()
776 GTEST_INTENTIONAL_CONST_COND_POP_()
779 UniversalPrinter
<typename
std::tuple_element
<I
- 1, T
>::type
>::Print(
780 std::get
<I
- 1>(t
), os
);
783 template <typename
... Types
>
784 void PrintTo(const ::std::tuple
<Types
...>& t
, ::std::ostream
* os
) {
786 PrintTupleTo(t
, std::integral_constant
<size_t, sizeof...(Types
)>(), os
);
790 // Overload for std::pair.
791 template <typename T1
, typename T2
>
792 void PrintTo(const ::std::pair
<T1
, T2
>& value
, ::std::ostream
* os
) {
794 // We cannot use UniversalPrint(value.first, os) here, as T1 may be
795 // a reference type. The same for printing value.second.
796 UniversalPrinter
<T1
>::Print(value
.first
, os
);
798 UniversalPrinter
<T2
>::Print(value
.second
, os
);
802 // Implements printing a non-reference type T by letting the compiler
803 // pick the right overload of PrintTo() for T.
804 template <typename T
>
805 class UniversalPrinter
{
807 // MSVC warns about adding const to a function type, so we want to
808 // disable the warning.
809 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
811 // Note: we deliberately don't call this PrintTo(), as that name
812 // conflicts with ::testing::internal::PrintTo in the body of the
814 static void Print(const T
& value
, ::std::ostream
* os
) {
815 // By default, ::testing::internal::PrintTo() is used for printing
818 // Thanks to Koenig look-up, if T is a class and has its own
819 // PrintTo() function defined in its namespace, that function will
820 // be visible here. Since it is more specific than the generic ones
821 // in ::testing::internal, it will be picked by the compiler in the
822 // following statement - exactly what we want.
826 GTEST_DISABLE_MSC_WARNINGS_POP_()
829 // Remove any const-qualifiers before passing a type to UniversalPrinter.
830 template <typename T
>
831 class UniversalPrinter
<const T
> : public UniversalPrinter
<T
> {};
833 #if GTEST_INTERNAL_HAS_ANY
835 // Printer for std::any / absl::any
838 class UniversalPrinter
<Any
> {
840 static void Print(const Any
& value
, ::std::ostream
* os
) {
841 if (value
.has_value()) {
842 *os
<< "value of type " << GetTypeName(value
);
849 static std::string
GetTypeName(const Any
& value
) {
851 return internal::GetTypeName(value
.type());
853 static_cast<void>(value
); // possibly unused
854 return "<unknown_type>";
855 #endif // GTEST_HAS_RTTI
859 #endif // GTEST_INTERNAL_HAS_ANY
861 #if GTEST_INTERNAL_HAS_OPTIONAL
863 // Printer for std::optional / absl::optional
865 template <typename T
>
866 class UniversalPrinter
<Optional
<T
>> {
868 static void Print(const Optional
<T
>& value
, ::std::ostream
* os
) {
873 UniversalPrint(*value
, os
);
880 class UniversalPrinter
<decltype(Nullopt())> {
882 static void Print(decltype(Nullopt()), ::std::ostream
* os
) {
887 #endif // GTEST_INTERNAL_HAS_OPTIONAL
889 #if GTEST_INTERNAL_HAS_VARIANT
891 // Printer for std::variant / absl::variant
893 template <typename
... T
>
894 class UniversalPrinter
<Variant
<T
...>> {
896 static void Print(const Variant
<T
...>& value
, ::std::ostream
* os
) {
898 #ifdef GTEST_HAS_ABSL
899 absl::visit(Visitor
{os
, value
.index()}, value
);
901 std::visit(Visitor
{os
, value
.index()}, value
);
902 #endif // GTEST_HAS_ABSL
908 template <typename U
>
909 void operator()(const U
& u
) const {
910 *os
<< "'" << GetTypeName
<U
>() << "(index = " << index
912 UniversalPrint(u
, os
);
919 #endif // GTEST_INTERNAL_HAS_VARIANT
921 // UniversalPrintArray(begin, len, os) prints an array of 'len'
922 // elements, starting at address 'begin'.
923 template <typename T
>
924 void UniversalPrintArray(const T
* begin
, size_t len
, ::std::ostream
* os
) {
929 const size_t kThreshold
= 18;
930 const size_t kChunkSize
= 8;
931 // If the array has more than kThreshold elements, we'll have to
932 // omit some details by printing only the first and the last
933 // kChunkSize elements.
934 if (len
<= kThreshold
) {
935 PrintRawArrayTo(begin
, len
, os
);
937 PrintRawArrayTo(begin
, kChunkSize
, os
);
939 PrintRawArrayTo(begin
+ len
- kChunkSize
, kChunkSize
, os
);
944 // This overload prints a (const) char array compactly.
945 GTEST_API_
void UniversalPrintArray(const char* begin
, size_t len
,
948 #ifdef __cpp_lib_char8_t
949 // This overload prints a (const) char8_t array compactly.
950 GTEST_API_
void UniversalPrintArray(const char8_t
* begin
, size_t len
,
954 // This overload prints a (const) char16_t array compactly.
955 GTEST_API_
void UniversalPrintArray(const char16_t
* begin
, size_t len
,
958 // This overload prints a (const) char32_t array compactly.
959 GTEST_API_
void UniversalPrintArray(const char32_t
* begin
, size_t len
,
962 // This overload prints a (const) wchar_t array compactly.
963 GTEST_API_
void UniversalPrintArray(const wchar_t* begin
, size_t len
,
966 // Implements printing an array type T[N].
967 template <typename T
, size_t N
>
968 class UniversalPrinter
<T
[N
]> {
970 // Prints the given array, omitting some elements when there are too
972 static void Print(const T (&a
)[N
], ::std::ostream
* os
) {
973 UniversalPrintArray(a
, N
, os
);
977 // Implements printing a reference type T&.
978 template <typename T
>
979 class UniversalPrinter
<T
&> {
981 // MSVC warns about adding const to a function type, so we want to
982 // disable the warning.
983 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
985 static void Print(const T
& value
, ::std::ostream
* os
) {
986 // Prints the address of the value. We use reinterpret_cast here
987 // as static_cast doesn't compile when T is a function type.
988 *os
<< "@" << reinterpret_cast<const void*>(&value
) << " ";
990 // Then prints the value itself.
991 UniversalPrint(value
, os
);
994 GTEST_DISABLE_MSC_WARNINGS_POP_()
997 // Prints a value tersely: for a reference type, the referenced value
998 // (but not the address) is printed; for a (const) char pointer, the
999 // NUL-terminated string (but not the pointer) is printed.
1001 template <typename T
>
1002 class UniversalTersePrinter
{
1004 static void Print(const T
& value
, ::std::ostream
* os
) {
1005 UniversalPrint(value
, os
);
1008 template <typename T
>
1009 class UniversalTersePrinter
<T
&> {
1011 static void Print(const T
& value
, ::std::ostream
* os
) {
1012 UniversalPrint(value
, os
);
1015 template <typename T
>
1016 class UniversalTersePrinter
<std::reference_wrapper
<T
>> {
1018 static void Print(std::reference_wrapper
<T
> value
, ::std::ostream
* os
) {
1019 UniversalTersePrinter
<T
>::Print(value
.get(), os
);
1022 template <typename T
, size_t N
>
1023 class UniversalTersePrinter
<T
[N
]> {
1025 static void Print(const T (&value
)[N
], ::std::ostream
* os
) {
1026 UniversalPrinter
<T
[N
]>::Print(value
, os
);
1030 class UniversalTersePrinter
<const char*> {
1032 static void Print(const char* str
, ::std::ostream
* os
) {
1033 if (str
== nullptr) {
1036 UniversalPrint(std::string(str
), os
);
1041 class UniversalTersePrinter
<char*> : public UniversalTersePrinter
<const char*> {
1044 #ifdef __cpp_lib_char8_t
1046 class UniversalTersePrinter
<const char8_t
*> {
1048 static void Print(const char8_t
* str
, ::std::ostream
* os
) {
1049 if (str
== nullptr) {
1052 UniversalPrint(::std::u8string(str
), os
);
1057 class UniversalTersePrinter
<char8_t
*>
1058 : public UniversalTersePrinter
<const char8_t
*> {};
1062 class UniversalTersePrinter
<const char16_t
*> {
1064 static void Print(const char16_t
* str
, ::std::ostream
* os
) {
1065 if (str
== nullptr) {
1068 UniversalPrint(::std::u16string(str
), os
);
1073 class UniversalTersePrinter
<char16_t
*>
1074 : public UniversalTersePrinter
<const char16_t
*> {};
1077 class UniversalTersePrinter
<const char32_t
*> {
1079 static void Print(const char32_t
* str
, ::std::ostream
* os
) {
1080 if (str
== nullptr) {
1083 UniversalPrint(::std::u32string(str
), os
);
1088 class UniversalTersePrinter
<char32_t
*>
1089 : public UniversalTersePrinter
<const char32_t
*> {};
1091 #if GTEST_HAS_STD_WSTRING
1093 class UniversalTersePrinter
<const wchar_t*> {
1095 static void Print(const wchar_t* str
, ::std::ostream
* os
) {
1096 if (str
== nullptr) {
1099 UniversalPrint(::std::wstring(str
), os
);
1106 class UniversalTersePrinter
<wchar_t*> {
1108 static void Print(wchar_t* str
, ::std::ostream
* os
) {
1109 UniversalTersePrinter
<const wchar_t*>::Print(str
, os
);
1113 template <typename T
>
1114 void UniversalTersePrint(const T
& value
, ::std::ostream
* os
) {
1115 UniversalTersePrinter
<T
>::Print(value
, os
);
1118 // Prints a value using the type inferred by the compiler. The
1119 // difference between this and UniversalTersePrint() is that for a
1120 // (const) char pointer, this prints both the pointer and the
1121 // NUL-terminated string.
1122 template <typename T
>
1123 void UniversalPrint(const T
& value
, ::std::ostream
* os
) {
1124 // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
1125 // UniversalPrinter with T directly.
1127 UniversalPrinter
<T1
>::Print(value
, os
);
1130 typedef ::std::vector
<::std::string
> Strings
;
1132 // Tersely prints the first N fields of a tuple to a string vector,
1133 // one element for each field.
1134 template <typename Tuple
>
1135 void TersePrintPrefixToStrings(const Tuple
&, std::integral_constant
<size_t, 0>,
1137 template <typename Tuple
, size_t I
>
1138 void TersePrintPrefixToStrings(const Tuple
& t
,
1139 std::integral_constant
<size_t, I
>,
1141 TersePrintPrefixToStrings(t
, std::integral_constant
<size_t, I
- 1>(),
1143 ::std::stringstream ss
;
1144 UniversalTersePrint(std::get
<I
- 1>(t
), &ss
);
1145 strings
->push_back(ss
.str());
1148 // Prints the fields of a tuple tersely to a string vector, one
1149 // element for each field. See the comment before
1150 // UniversalTersePrint() for how we define "tersely".
1151 template <typename Tuple
>
1152 Strings
UniversalTersePrintTupleFieldsToStrings(const Tuple
& value
) {
1154 TersePrintPrefixToStrings(
1155 value
, std::integral_constant
<size_t, std::tuple_size
<Tuple
>::value
>(),
1160 } // namespace internal
1162 template <typename T
>
1163 ::std::string
PrintToString(const T
& value
) {
1164 ::std::stringstream ss
;
1165 internal::UniversalTersePrinter
<T
>::Print(value
, &ss
);
1169 } // namespace testing
1171 // Include any custom printer added by the local installation.
1172 // We must include this header at the end to make sure it can use the
1173 // declarations from this file.
1174 #include "gtest/internal/custom/gtest-printers.h"
1176 #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_