Lab_1 0.1.1
Matrix Library
Loading...
Searching...
No Matches
gtest-printers.h
1// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
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
13// distribution.
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.
17//
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.
29
30// Google Test - The Google C++ Testing and Mocking Framework
31//
32// This file implements a universal value printer that can print a
33// value of any type T:
34//
35// void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
36//
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
41// foo):
42//
43// 1. foo::PrintTo(const T&, ostream*)
44// 2. operator<<(ostream&, const T&) defined in either foo or the
45// global namespace.
46//
47// However if T is an STL-style container then it is printed element-wise
48// unless foo::PrintTo(const T&, ostream*) is defined. Note that
49// operator<<() is ignored for container types.
50//
51// If none of the above is defined, it will print the debug string of
52// the value if it is a protocol buffer, or print the raw bytes in the
53// value otherwise.
54//
55// To aid debugging: when T is a reference type, the address of the
56// value is also printed; when T is a (const) char pointer, both the
57// pointer value and the NUL-terminated string it points to are
58// printed.
59//
60// We also provide some convenient wrappers:
61//
62// // Prints a value to a string. For a (const or not) char
63// // pointer, the NUL-terminated string (but not the pointer) is
64// // printed.
65// std::string ::testing::PrintToString(const T& value);
66//
67// // Prints a value tersely: for a reference type, the referenced
68// // value (but not the address) is printed; for a (const or not) char
69// // pointer, the NUL-terminated string (but not the pointer) is
70// // printed.
71// void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
72//
73// // Prints value using the type inferred by the compiler. The difference
74// // from UniversalTersePrint() is that this function prints both the
75// // pointer and the NUL-terminated string for a (const or not) char pointer.
76// void ::testing::internal::UniversalPrint(const T& value, ostream*);
77//
78// // Prints the fields of a tuple tersely to a string vector, one
79// // element for each field. Tuple support must be enabled in
80// // gtest-port.h.
81// std::vector<string> UniversalTersePrintTupleFieldsToStrings(
82// const Tuple& value);
83//
84// Known limitation:
85//
86// The print primitives print the elements of an STL-style container
87// using the compiler-inferred type of *iter where iter is a
88// const_iterator of the container. When const_iterator is an input
89// iterator but not a forward iterator, this inferred type may not
90// match value_type, and the print output may be incorrect. In
91// practice, this is rarely a problem as for most containers
92// const_iterator is a forward iterator. We'll fix this if there's an
93// actual need for it. Note that this fix cannot rely on value_type
94// being defined as many user-defined container types don't have
95// value_type.
96
97// IWYU pragma: private, include "gtest/gtest.h"
98// IWYU pragma: friend gtest/.*
99// IWYU pragma: friend gmock/.*
100
101#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
102#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
103
104#include <functional>
105#include <memory>
106#include <ostream> // NOLINT
107#include <sstream>
108#include <string>
109#include <tuple>
110#include <type_traits>
111#include <utility>
112#include <vector>
113
114#include "gtest/internal/gtest-internal.h"
115#include "gtest/internal/gtest-port.h"
116
117namespace testing {
118
119// Definitions in the internal* namespaces are subject to change without notice.
120// DO NOT USE THEM IN USER CODE!
121namespace internal {
122
123template <typename T>
124void UniversalPrint(const T& value, ::std::ostream* os);
125
126// Used to print an STL-style container when the user doesn't define
127// a PrintTo() for it.
129 template <typename T,
130 typename = typename std::enable_if<
131 (sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
133 static void PrintValue(const T& container, std::ostream* os) {
134 const size_t kMaxCount = 32; // The maximum number of elements to print.
135 *os << '{';
136 size_t count = 0;
137 for (auto&& elem : container) {
138 if (count > 0) {
139 *os << ',';
140 if (count == kMaxCount) { // Enough has been printed.
141 *os << " ...";
142 break;
143 }
144 }
145 *os << ' ';
146 // We cannot call PrintTo(elem, os) here as PrintTo() doesn't
147 // handle `elem` being a native array.
148 internal::UniversalPrint(elem, os);
149 ++count;
150 }
151
152 if (count > 0) {
153 *os << ' ';
154 }
155 *os << '}';
156 }
157};
158
159// Used to print a pointer that is neither a char pointer nor a member
160// pointer, when the user doesn't define PrintTo() for it. (A member
161// variable pointer or member function pointer doesn't really point to
162// a location in the address space. Their representation is
163// implementation-defined. Therefore they will be printed as raw
164// bytes.)
166 template <typename T, typename = typename std::enable_if<
167 std::is_function<T>::value>::type>
168 static void PrintValue(T* p, ::std::ostream* os) {
169 if (p == nullptr) {
170 *os << "NULL";
171 } else {
172 // T is a function type, so '*os << p' doesn't do what we want
173 // (it just prints p as bool). We want to print p as a const
174 // void*.
175 *os << reinterpret_cast<const void*>(p);
176 }
177 }
178};
179
181 template <typename T>
182 static void PrintValue(T* p, ::std::ostream* os) {
183 if (p == nullptr) {
184 *os << "NULL";
185 } else {
186 // T is not a function type. We just call << to print p,
187 // relying on ADL to pick up user-defined << for their pointer
188 // types, if any.
189 *os << p;
190 }
191 }
192};
193
194namespace internal_stream_operator_without_lexical_name_lookup {
195
196// The presence of an operator<< here will terminate lexical scope lookup
197// straight away (even though it cannot be a match because of its argument
198// types). Thus, the two operator<< calls in StreamPrinter will find only ADL
199// candidates.
201void operator<<(LookupBlocker, LookupBlocker);
202
204 template <typename T,
205 // Don't accept member pointers here. We'd print them via implicit
206 // conversion to bool, which isn't useful.
207 typename = typename std::enable_if<
208 !std::is_member_pointer<T>::value>::type,
209 // Only accept types for which we can find a streaming operator via
210 // ADL (possibly involving implicit conversions).
211 typename = decltype(std::declval<std::ostream&>()
212 << std::declval<const T&>())>
213 static void PrintValue(const T& value, ::std::ostream* os) {
214 // Call streaming operator found by ADL, possibly with implicit conversions
215 // of the arguments.
216 *os << value;
217 }
218};
219
220} // namespace internal_stream_operator_without_lexical_name_lookup
221
223 // We print a protobuf using its ShortDebugString() when the string
224 // doesn't exceed this many characters; otherwise we print it using
225 // DebugString() for better readability.
226 static const size_t kProtobufOneLinerMaxLength = 50;
227
228 template <typename T,
229 typename = typename std::enable_if<
231 static void PrintValue(const T& value, ::std::ostream* os) {
232 std::string pretty_str = value.ShortDebugString();
233 if (pretty_str.length() > kProtobufOneLinerMaxLength) {
234 pretty_str = "\n" + value.DebugString();
235 }
236 *os << ("<" + pretty_str + ">");
237 }
238};
239
241 // Since T has no << operator or PrintTo() but can be implicitly
242 // converted to BiggestInt, we print it as a BiggestInt.
243 //
244 // Most likely T is an enum type (either named or unnamed), in which
245 // case printing it as an integer is the desired behavior. In case
246 // T is not an enum, printing it as an integer is the best we can do
247 // given that it has no user-defined printer.
248 static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
249 *os << value;
250 }
251};
252
254#if GTEST_INTERNAL_HAS_STRING_VIEW
255 static void PrintValue(internal::StringView value, ::std::ostream* os) {
256 internal::UniversalPrint(value, os);
257 }
258#endif
259};
260
261// Prints the given number of bytes in the given object to the given
262// ostream.
263GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
264 size_t count, ::std::ostream* os);
266 // SFINAE on `sizeof` to make sure we have a complete type.
267 template <typename T, size_t = sizeof(T)>
268 static void PrintValue(const T& value, ::std::ostream* os) {
269 PrintBytesInObjectTo(
270 static_cast<const unsigned char*>(
271 // Load bearing cast to void* to support iOS
272 reinterpret_cast<const void*>(std::addressof(value))),
273 sizeof(value), os);
274 }
275};
276
278 template <typename T>
279 static void PrintValue(const T&, ::std::ostream* os) {
280 *os << "(incomplete type)";
281 }
282};
283
284// Try every printer in order and return the first one that works.
285template <typename T, typename E, typename Printer, typename... Printers>
286struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
287
288template <typename T, typename Printer, typename... Printers>
290 T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
291 Printer, Printers...> {
292 using type = Printer;
293};
294
295// Select the best printer in the following order:
296// - Print containers (they have begin/end/etc).
297// - Print function pointers.
298// - Print object pointers.
299// - Use the stream operator, if available.
300// - Print protocol buffers.
301// - Print types convertible to BiggestInt.
302// - Print types convertible to StringView, if available.
303// - Fallback to printing the raw bytes of the object.
304template <typename T>
305void PrintWithFallback(const T& value, ::std::ostream* os) {
306 using Printer = typename FindFirstPrinter<
311 Printer::PrintValue(value, os);
312}
313
314// FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
315// value of type ToPrint that is an operand of a comparison assertion
316// (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
317// the comparison, and is used to help determine the best way to
318// format the value. In particular, when the value is a C string
319// (char pointer) and the other operand is an STL string object, we
320// want to format the C string as a string, since we know it is
321// compared by value with the string object. If the value is a char
322// pointer but the other operand is not an STL string object, we don't
323// know whether the pointer is supposed to point to a NUL-terminated
324// string, and thus want to print it as a pointer to be safe.
325//
326// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
327
328// The default case.
329template <typename ToPrint, typename OtherOperand>
331 public:
332 static ::std::string Format(const ToPrint& value) {
333 return ::testing::PrintToString(value);
334 }
335};
336
337// Array.
338template <typename ToPrint, size_t N, typename OtherOperand>
339class FormatForComparison<ToPrint[N], OtherOperand> {
340 public:
341 static ::std::string Format(const ToPrint* value) {
343 }
344};
345
346// By default, print C string as pointers to be safe, as we don't know
347// whether they actually point to a NUL-terminated string.
348
349#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
350 template <typename OtherOperand> \
351 class FormatForComparison<CharType*, OtherOperand> { \
352 public: \
353 static ::std::string Format(CharType* value) { \
354 return ::testing::PrintToString(static_cast<const void*>(value)); \
355 } \
356 }
357
358GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
359GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
360GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
361GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
362#ifdef __cpp_lib_char8_t
363GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t);
364GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t);
365#endif
366GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t);
367GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t);
368GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t);
369GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t);
370
371#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
372
373// If a C string is compared with an STL string object, we know it's meant
374// to point to a NUL-terminated string, and thus can print it as a string.
375
376#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
377 template <> \
378 class FormatForComparison<CharType*, OtherStringType> { \
379 public: \
380 static ::std::string Format(CharType* value) { \
381 return ::testing::PrintToString(value); \
382 } \
383 }
384
385GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
386GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
387#ifdef __cpp_lib_char8_t
388GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string);
389GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string);
390#endif
391GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string);
392GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string);
393GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string);
394GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string);
395
396#if GTEST_HAS_STD_WSTRING
397GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
398GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
399#endif
400
401#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
402
403// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
404// operand to be used in a failure message. The type (but not value)
405// of the other operand may affect the format. This allows us to
406// print a char* as a raw pointer when it is compared against another
407// char* or void*, and print it as a C string when it is compared
408// against an std::string object, for example.
409//
410// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
411template <typename T1, typename T2>
412std::string FormatForComparisonFailureMessage(const T1& value,
413 const T2& /* other_operand */) {
415}
416
417// UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
418// value to the given ostream. The caller must ensure that
419// 'ostream_ptr' is not NULL, or the behavior is undefined.
420//
421// We define UniversalPrinter as a class template (as opposed to a
422// function template), as we need to partially specialize it for
423// reference types, which cannot be done with function templates.
424template <typename T>
425class UniversalPrinter;
426
427// Prints the given value using the << operator if it has one;
428// otherwise prints the bytes in it. This is what
429// UniversalPrinter<T>::Print() does when PrintTo() is not specialized
430// or overloaded for type T.
431//
432// A user can override this behavior for a class type Foo by defining
433// an overload of PrintTo() in the namespace where Foo is defined. We
434// give the user this option as sometimes defining a << operator for
435// Foo is not desirable (e.g. the coding style may prevent doing it,
436// or there is already a << operator but it doesn't do what the user
437// wants).
438template <typename T>
439void PrintTo(const T& value, ::std::ostream* os) {
440 internal::PrintWithFallback(value, os);
441}
442
443// The following list of PrintTo() overloads tells
444// UniversalPrinter<T>::Print() how to print standard types (built-in
445// types, strings, plain arrays, and pointers).
446
447// Overloads for various char types.
448GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
449GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
450inline void PrintTo(char c, ::std::ostream* os) {
451 // When printing a plain char, we always treat it as unsigned. This
452 // way, the output won't be affected by whether the compiler thinks
453 // char is signed or not.
454 PrintTo(static_cast<unsigned char>(c), os);
455}
456
457// Overloads for other simple built-in types.
458inline void PrintTo(bool x, ::std::ostream* os) {
459 *os << (x ? "true" : "false");
460}
461
462// Overload for wchar_t type.
463// Prints a wchar_t as a symbol if it is printable or as its internal
464// code otherwise and also as its decimal code (except for L'\0').
465// The L'\0' char is printed as "L'\\0'". The decimal code is printed
466// as signed integer when wchar_t is implemented by the compiler
467// as a signed type and is printed as an unsigned integer when wchar_t
468// is implemented as an unsigned type.
469GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
470
471GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
472inline void PrintTo(char16_t c, ::std::ostream* os) {
473 PrintTo(ImplicitCast_<char32_t>(c), os);
474}
475#ifdef __cpp_char8_t
476inline void PrintTo(char8_t c, ::std::ostream* os) {
477 PrintTo(ImplicitCast_<char32_t>(c), os);
478}
479#endif
480
481// gcc/clang __{u,}int128_t
482#if defined(__SIZEOF_INT128__)
483GTEST_API_ void PrintTo(__uint128_t v, ::std::ostream* os);
484GTEST_API_ void PrintTo(__int128_t v, ::std::ostream* os);
485#endif // __SIZEOF_INT128__
486
487// Overloads for C strings.
488GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
489inline void PrintTo(char* s, ::std::ostream* os) {
490 PrintTo(ImplicitCast_<const char*>(s), os);
491}
492
493// signed/unsigned char is often used for representing binary data, so
494// we print pointers to it as void* to be safe.
495inline void PrintTo(const signed char* s, ::std::ostream* os) {
496 PrintTo(ImplicitCast_<const void*>(s), os);
497}
498inline void PrintTo(signed char* s, ::std::ostream* os) {
499 PrintTo(ImplicitCast_<const void*>(s), os);
500}
501inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
502 PrintTo(ImplicitCast_<const void*>(s), os);
503}
504inline void PrintTo(unsigned char* s, ::std::ostream* os) {
505 PrintTo(ImplicitCast_<const void*>(s), os);
506}
507#ifdef __cpp_char8_t
508// Overloads for u8 strings.
509GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os);
510inline void PrintTo(char8_t* s, ::std::ostream* os) {
511 PrintTo(ImplicitCast_<const char8_t*>(s), os);
512}
513#endif
514// Overloads for u16 strings.
515GTEST_API_ void PrintTo(const char16_t* s, ::std::ostream* os);
516inline void PrintTo(char16_t* s, ::std::ostream* os) {
517 PrintTo(ImplicitCast_<const char16_t*>(s), os);
518}
519// Overloads for u32 strings.
520GTEST_API_ void PrintTo(const char32_t* s, ::std::ostream* os);
521inline void PrintTo(char32_t* s, ::std::ostream* os) {
522 PrintTo(ImplicitCast_<const char32_t*>(s), os);
523}
524
525// MSVC can be configured to define wchar_t as a typedef of unsigned
526// short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
527// type. When wchar_t is a typedef, defining an overload for const
528// wchar_t* would cause unsigned short* be printed as a wide string,
529// possibly causing invalid memory accesses.
530#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
531// Overloads for wide C strings
532GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
533inline void PrintTo(wchar_t* s, ::std::ostream* os) {
534 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
535}
536#endif
537
538// Overload for C arrays. Multi-dimensional arrays are printed
539// properly.
540
541// Prints the given number of elements in an array, without printing
542// the curly braces.
543template <typename T>
544void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
545 UniversalPrint(a[0], os);
546 for (size_t i = 1; i != count; i++) {
547 *os << ", ";
548 UniversalPrint(a[i], os);
549 }
550}
551
552// Overloads for ::std::string.
553GTEST_API_ void PrintStringTo(const ::std::string& s, ::std::ostream* os);
554inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
555 PrintStringTo(s, os);
556}
557
558// Overloads for ::std::u8string
559#ifdef __cpp_lib_char8_t
560GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
561inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
562 PrintU8StringTo(s, os);
563}
564#endif
565
566// Overloads for ::std::u16string
567GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os);
568inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
569 PrintU16StringTo(s, os);
570}
571
572// Overloads for ::std::u32string
573GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os);
574inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
575 PrintU32StringTo(s, os);
576}
577
578// Overloads for ::std::wstring.
579#if GTEST_HAS_STD_WSTRING
580GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os);
581inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
582 PrintWideStringTo(s, os);
583}
584#endif // GTEST_HAS_STD_WSTRING
585
586#if GTEST_INTERNAL_HAS_STRING_VIEW
587// Overload for internal::StringView.
588inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
589 PrintTo(::std::string(sp), os);
590}
591#endif // GTEST_INTERNAL_HAS_STRING_VIEW
592
593inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
594
595#if GTEST_HAS_RTTI
596inline void PrintTo(const std::type_info& info, std::ostream* os) {
597 *os << internal::GetTypeName(info);
598}
599#endif // GTEST_HAS_RTTI
600
601template <typename T>
602void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
603 UniversalPrinter<T&>::Print(ref.get(), os);
604}
605
606inline const void* VoidifyPointer(const void* p) { return p; }
607inline const void* VoidifyPointer(volatile const void* p) {
608 return const_cast<const void*>(p);
609}
610
611template <typename T, typename Ptr>
612void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) {
613 if (ptr == nullptr) {
614 *os << "(nullptr)";
615 } else {
616 // We can't print the value. Just print the pointer..
617 *os << "(" << (VoidifyPointer)(ptr.get()) << ")";
618 }
619}
620template <typename T, typename Ptr,
621 typename = typename std::enable_if<!std::is_void<T>::value &&
622 !std::is_array<T>::value>::type>
623void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) {
624 if (ptr == nullptr) {
625 *os << "(nullptr)";
626 } else {
627 *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = ";
628 UniversalPrinter<T>::Print(*ptr, os);
629 *os << ")";
630 }
631}
632
633template <typename T, typename D>
634void PrintTo(const std::unique_ptr<T, D>& ptr, std::ostream* os) {
635 (PrintSmartPointer<T>)(ptr, os, 0);
636}
637
638template <typename T>
639void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) {
640 (PrintSmartPointer<T>)(ptr, os, 0);
641}
642
643// Helper function for printing a tuple. T must be instantiated with
644// a tuple type.
645template <typename T>
646void PrintTupleTo(const T&, std::integral_constant<size_t, 0>,
647 ::std::ostream*) {}
648
649template <typename T, size_t I>
650void PrintTupleTo(const T& t, std::integral_constant<size_t, I>,
651 ::std::ostream* os) {
652 PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
653 GTEST_INTENTIONAL_CONST_COND_PUSH_()
654 if (I > 1) {
655 GTEST_INTENTIONAL_CONST_COND_POP_()
656 *os << ", ";
657 }
658 UniversalPrinter<typename std::tuple_element<I - 1, T>::type>::Print(
659 std::get<I - 1>(t), os);
660}
661
662template <typename... Types>
663void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
664 *os << "(";
665 PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os);
666 *os << ")";
667}
668
669// Overload for std::pair.
670template <typename T1, typename T2>
671void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
672 *os << '(';
673 // We cannot use UniversalPrint(value.first, os) here, as T1 may be
674 // a reference type. The same for printing value.second.
675 UniversalPrinter<T1>::Print(value.first, os);
676 *os << ", ";
677 UniversalPrinter<T2>::Print(value.second, os);
678 *os << ')';
679}
680
681// Implements printing a non-reference type T by letting the compiler
682// pick the right overload of PrintTo() for T.
683template <typename T>
685 public:
686 // MSVC warns about adding const to a function type, so we want to
687 // disable the warning.
688 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
689
690 // Note: we deliberately don't call this PrintTo(), as that name
691 // conflicts with ::testing::internal::PrintTo in the body of the
692 // function.
693 static void Print(const T& value, ::std::ostream* os) {
694 // By default, ::testing::internal::PrintTo() is used for printing
695 // the value.
696 //
697 // Thanks to Koenig look-up, if T is a class and has its own
698 // PrintTo() function defined in its namespace, that function will
699 // be visible here. Since it is more specific than the generic ones
700 // in ::testing::internal, it will be picked by the compiler in the
701 // following statement - exactly what we want.
702 PrintTo(value, os);
703 }
704
705 GTEST_DISABLE_MSC_WARNINGS_POP_()
706};
707
708// Remove any const-qualifiers before passing a type to UniversalPrinter.
709template <typename T>
710class UniversalPrinter<const T> : public UniversalPrinter<T> {};
711
712#if GTEST_INTERNAL_HAS_ANY
713
714// Printer for std::any / absl::any
715
716template <>
717class UniversalPrinter<Any> {
718 public:
719 static void Print(const Any& value, ::std::ostream* os) {
720 if (value.has_value()) {
721 *os << "value of type " << GetTypeName(value);
722 } else {
723 *os << "no value";
724 }
725 }
726
727 private:
728 static std::string GetTypeName(const Any& value) {
729#if GTEST_HAS_RTTI
730 return internal::GetTypeName(value.type());
731#else
732 static_cast<void>(value); // possibly unused
733 return "<unknown_type>";
734#endif // GTEST_HAS_RTTI
735 }
736};
737
738#endif // GTEST_INTERNAL_HAS_ANY
739
740#if GTEST_INTERNAL_HAS_OPTIONAL
741
742// Printer for std::optional / absl::optional
743
744template <typename T>
745class UniversalPrinter<Optional<T>> {
746 public:
747 static void Print(const Optional<T>& value, ::std::ostream* os) {
748 *os << '(';
749 if (!value) {
750 *os << "nullopt";
751 } else {
752 UniversalPrint(*value, os);
753 }
754 *os << ')';
755 }
756};
757
758template <>
759class UniversalPrinter<decltype(Nullopt())> {
760 public:
761 static void Print(decltype(Nullopt()), ::std::ostream* os) {
762 *os << "(nullopt)";
763 }
764};
765
766#endif // GTEST_INTERNAL_HAS_OPTIONAL
767
768#if GTEST_INTERNAL_HAS_VARIANT
769
770// Printer for std::variant / absl::variant
771
772template <typename... T>
773class UniversalPrinter<Variant<T...>> {
774 public:
775 static void Print(const Variant<T...>& value, ::std::ostream* os) {
776 *os << '(';
777#if GTEST_HAS_ABSL
778 absl::visit(Visitor{os, value.index()}, value);
779#else
780 std::visit(Visitor{os, value.index()}, value);
781#endif // GTEST_HAS_ABSL
782 *os << ')';
783 }
784
785 private:
786 struct Visitor {
787 template <typename U>
788 void operator()(const U& u) const {
789 *os << "'" << GetTypeName<U>() << "(index = " << index
790 << ")' with value ";
791 UniversalPrint(u, os);
792 }
793 ::std::ostream* os;
794 std::size_t index;
795 };
796};
797
798#endif // GTEST_INTERNAL_HAS_VARIANT
799
800// UniversalPrintArray(begin, len, os) prints an array of 'len'
801// elements, starting at address 'begin'.
802template <typename T>
803void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
804 if (len == 0) {
805 *os << "{}";
806 } else {
807 *os << "{ ";
808 const size_t kThreshold = 18;
809 const size_t kChunkSize = 8;
810 // If the array has more than kThreshold elements, we'll have to
811 // omit some details by printing only the first and the last
812 // kChunkSize elements.
813 if (len <= kThreshold) {
814 PrintRawArrayTo(begin, len, os);
815 } else {
816 PrintRawArrayTo(begin, kChunkSize, os);
817 *os << ", ..., ";
818 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
819 }
820 *os << " }";
821 }
822}
823// This overload prints a (const) char array compactly.
824GTEST_API_ void UniversalPrintArray(const char* begin, size_t len,
825 ::std::ostream* os);
826
827#ifdef __cpp_char8_t
828// This overload prints a (const) char8_t array compactly.
829GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len,
830 ::std::ostream* os);
831#endif
832
833// This overload prints a (const) char16_t array compactly.
834GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len,
835 ::std::ostream* os);
836
837// This overload prints a (const) char32_t array compactly.
838GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len,
839 ::std::ostream* os);
840
841// This overload prints a (const) wchar_t array compactly.
842GTEST_API_ void UniversalPrintArray(const wchar_t* begin, size_t len,
843 ::std::ostream* os);
844
845// Implements printing an array type T[N].
846template <typename T, size_t N>
847class UniversalPrinter<T[N]> {
848 public:
849 // Prints the given array, omitting some elements when there are too
850 // many.
851 static void Print(const T (&a)[N], ::std::ostream* os) {
852 UniversalPrintArray(a, N, os);
853 }
854};
855
856// Implements printing a reference type T&.
857template <typename T>
859 public:
860 // MSVC warns about adding const to a function type, so we want to
861 // disable the warning.
862 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
863
864 static void Print(const T& value, ::std::ostream* os) {
865 // Prints the address of the value. We use reinterpret_cast here
866 // as static_cast doesn't compile when T is a function type.
867 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
868
869 // Then prints the value itself.
870 UniversalPrint(value, os);
871 }
872
873 GTEST_DISABLE_MSC_WARNINGS_POP_()
874};
875
876// Prints a value tersely: for a reference type, the referenced value
877// (but not the address) is printed; for a (const) char pointer, the
878// NUL-terminated string (but not the pointer) is printed.
879
880template <typename T>
882 public:
883 static void Print(const T& value, ::std::ostream* os) {
884 UniversalPrint(value, os);
885 }
886};
887template <typename T>
889 public:
890 static void Print(const T& value, ::std::ostream* os) {
891 UniversalPrint(value, os);
892 }
893};
894template <typename T>
895class UniversalTersePrinter<std::reference_wrapper<T>> {
896 public:
897 static void Print(std::reference_wrapper<T> value, ::std::ostream* os) {
898 UniversalTersePrinter<T>::Print(value.get(), os);
899 }
900};
901template <typename T, size_t N>
903 public:
904 static void Print(const T (&value)[N], ::std::ostream* os) {
906 }
907};
908template <>
909class UniversalTersePrinter<const char*> {
910 public:
911 static void Print(const char* str, ::std::ostream* os) {
912 if (str == nullptr) {
913 *os << "NULL";
914 } else {
915 UniversalPrint(std::string(str), os);
916 }
917 }
918};
919template <>
922
923#ifdef __cpp_char8_t
924template <>
925class UniversalTersePrinter<const char8_t*> {
926 public:
927 static void Print(const char8_t* str, ::std::ostream* os) {
928 if (str == nullptr) {
929 *os << "NULL";
930 } else {
931 UniversalPrint(::std::u8string(str), os);
932 }
933 }
934};
935template <>
936class UniversalTersePrinter<char8_t*>
937 : public UniversalTersePrinter<const char8_t*> {};
938#endif
939
940template <>
941class UniversalTersePrinter<const char16_t*> {
942 public:
943 static void Print(const char16_t* str, ::std::ostream* os) {
944 if (str == nullptr) {
945 *os << "NULL";
946 } else {
947 UniversalPrint(::std::u16string(str), os);
948 }
949 }
950};
951template <>
954
955template <>
956class UniversalTersePrinter<const char32_t*> {
957 public:
958 static void Print(const char32_t* str, ::std::ostream* os) {
959 if (str == nullptr) {
960 *os << "NULL";
961 } else {
962 UniversalPrint(::std::u32string(str), os);
963 }
964 }
965};
966template <>
969
970#if GTEST_HAS_STD_WSTRING
971template <>
972class UniversalTersePrinter<const wchar_t*> {
973 public:
974 static void Print(const wchar_t* str, ::std::ostream* os) {
975 if (str == nullptr) {
976 *os << "NULL";
977 } else {
978 UniversalPrint(::std::wstring(str), os);
979 }
980 }
981};
982#endif
983
984template <>
985class UniversalTersePrinter<wchar_t*> {
986 public:
987 static void Print(wchar_t* str, ::std::ostream* os) {
989 }
990};
991
992template <typename T>
993void UniversalTersePrint(const T& value, ::std::ostream* os) {
995}
996
997// Prints a value using the type inferred by the compiler. The
998// difference between this and UniversalTersePrint() is that for a
999// (const) char pointer, this prints both the pointer and the
1000// NUL-terminated string.
1001template <typename T>
1002void UniversalPrint(const T& value, ::std::ostream* os) {
1003 // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
1004 // UniversalPrinter with T directly.
1005 typedef T T1;
1006 UniversalPrinter<T1>::Print(value, os);
1007}
1008
1009typedef ::std::vector<::std::string> Strings;
1010
1011// Tersely prints the first N fields of a tuple to a string vector,
1012// one element for each field.
1013template <typename Tuple>
1014void TersePrintPrefixToStrings(const Tuple&, std::integral_constant<size_t, 0>,
1015 Strings*) {}
1016template <typename Tuple, size_t I>
1017void TersePrintPrefixToStrings(const Tuple& t,
1018 std::integral_constant<size_t, I>,
1019 Strings* strings) {
1020 TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
1021 strings);
1022 ::std::stringstream ss;
1023 UniversalTersePrint(std::get<I - 1>(t), &ss);
1024 strings->push_back(ss.str());
1025}
1026
1027// Prints the fields of a tuple tersely to a string vector, one
1028// element for each field. See the comment before
1029// UniversalTersePrint() for how we define "tersely".
1030template <typename Tuple>
1031Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
1032 Strings result;
1033 TersePrintPrefixToStrings(
1034 value, std::integral_constant<size_t, std::tuple_size<Tuple>::value>(),
1035 &result);
1036 return result;
1037}
1038
1039} // namespace internal
1040
1041template <typename T>
1042::std::string PrintToString(const T& value) {
1043 ::std::stringstream ss;
1044 internal::UniversalTersePrinter<T>::Print(value, &ss);
1045 return ss.str();
1046}
1047
1048} // namespace testing
1049
1050// Include any custom printer added by the local installation.
1051// We must include this header at the end to make sure it can use the
1052// declarations from this file.
1053#include "gtest/internal/custom/gtest-printers.h"
1054
1055#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
Definition gtest-printers.h:330
Definition gtest-printers.h:684
Definition gtest-printers.h:881
Definition gtest-printers.h:128
Definition gtest-printers.h:240
Definition gtest-printers.h:277
Definition gtest-printers.h:286
Definition gtest-printers.h:165
Definition gtest-internal.h:1008
Definition gtest-printers.h:180
Definition gtest-printers.h:222
Definition gtest-printers.h:265