]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - utils/unittest/googletest/include/gtest/gtest-printers.h
Vendor import of llvm trunk r338150:
[FreeBSD/FreeBSD.git] / utils / unittest / googletest / include / gtest / 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 // Author: wan@google.com (Zhanyong Wan)
31
32 // Google Test - The Google C++ Testing Framework
33 //
34 // This file implements a universal value printer that can print a
35 // value of any type T:
36 //
37 //   void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
38 //
39 // A user can teach this function how to print a class type T by
40 // defining either operator<<() or PrintTo() in the namespace that
41 // defines T.  More specifically, the FIRST defined function in the
42 // following list will be used (assuming T is defined in namespace
43 // foo):
44 //
45 //   1. foo::PrintTo(const T&, ostream*)
46 //   2. operator<<(ostream&, const T&) defined in either foo or the
47 //      global namespace.
48 //
49 // If none of the above is defined, it will print the debug string of
50 // the value if it is a protocol buffer, or print the raw bytes in the
51 // value otherwise.
52 //
53 // To aid debugging: when T is a reference type, the address of the
54 // value is also printed; when T is a (const) char pointer, both the
55 // pointer value and the NUL-terminated string it points to are
56 // printed.
57 //
58 // We also provide some convenient wrappers:
59 //
60 //   // Prints a value to a string.  For a (const or not) char
61 //   // pointer, the NUL-terminated string (but not the pointer) is
62 //   // printed.
63 //   std::string ::testing::PrintToString(const T& value);
64 //
65 //   // Prints a value tersely: for a reference type, the referenced
66 //   // value (but not the address) is printed; for a (const or not) char
67 //   // pointer, the NUL-terminated string (but not the pointer) is
68 //   // printed.
69 //   void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
70 //
71 //   // Prints value using the type inferred by the compiler.  The difference
72 //   // from UniversalTersePrint() is that this function prints both the
73 //   // pointer and the NUL-terminated string for a (const or not) char pointer.
74 //   void ::testing::internal::UniversalPrint(const T& value, ostream*);
75 //
76 //   // Prints the fields of a tuple tersely to a string vector, one
77 //   // element for each field. Tuple support must be enabled in
78 //   // gtest-port.h.
79 //   std::vector<string> UniversalTersePrintTupleFieldsToStrings(
80 //       const Tuple& value);
81 //
82 // Known limitation:
83 //
84 // The print primitives print the elements of an STL-style container
85 // using the compiler-inferred type of *iter where iter is a
86 // const_iterator of the container.  When const_iterator is an input
87 // iterator but not a forward iterator, this inferred type may not
88 // match value_type, and the print output may be incorrect.  In
89 // practice, this is rarely a problem as for most containers
90 // const_iterator is a forward iterator.  We'll fix this if there's an
91 // actual need for it.  Note that this fix cannot rely on value_type
92 // being defined as many user-defined container types don't have
93 // value_type.
94
95 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
96 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
97
98 #include <ostream>  // NOLINT
99 #include <sstream>
100 #include <string>
101 #include <utility>
102 #include <vector>
103 #include "gtest/internal/gtest-port.h"
104 #include "gtest/internal/gtest-internal.h"
105 #include "gtest/internal/custom/raw-ostream.h"
106
107 #if GTEST_HAS_STD_TUPLE_
108 # include <tuple>
109 #endif
110
111 namespace testing {
112
113 // Definitions in the 'internal' and 'internal2' name spaces are
114 // subject to change without notice.  DO NOT USE THEM IN USER CODE!
115 namespace internal2 {
116
117 // Prints the given number of bytes in the given object to the given
118 // ostream.
119 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
120                                      size_t count,
121                                      ::std::ostream* os);
122
123 // For selecting which printer to use when a given type has neither <<
124 // nor PrintTo().
125 enum TypeKind {
126   kProtobuf,              // a protobuf type
127   kConvertibleToInteger,  // a type implicitly convertible to BiggestInt
128                           // (e.g. a named or unnamed enum type)
129   kOtherType              // anything else
130 };
131
132 // TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
133 // by the universal printer to print a value of type T when neither
134 // operator<< nor PrintTo() is defined for T, where kTypeKind is the
135 // "kind" of T as defined by enum TypeKind.
136 template <typename T, TypeKind kTypeKind>
137 class TypeWithoutFormatter {
138  public:
139   // This default version is called when kTypeKind is kOtherType.
140   static void PrintValue(const T& value, ::std::ostream* os) {
141     PrintBytesInObjectTo(reinterpret_cast<const unsigned char*>(&value),
142                          sizeof(value), os);
143   }
144 };
145
146 // We print a protobuf using its ShortDebugString() when the string
147 // doesn't exceed this many characters; otherwise we print it using
148 // DebugString() for better readability.
149 const size_t kProtobufOneLinerMaxLength = 50;
150
151 template <typename T>
152 class TypeWithoutFormatter<T, kProtobuf> {
153  public:
154   static void PrintValue(const T& value, ::std::ostream* os) {
155     const ::testing::internal::string short_str = value.ShortDebugString();
156     const ::testing::internal::string pretty_str =
157         short_str.length() <= kProtobufOneLinerMaxLength ?
158         short_str : ("\n" + value.DebugString());
159     *os << ("<" + pretty_str + ">");
160   }
161 };
162
163 template <typename T>
164 class TypeWithoutFormatter<T, kConvertibleToInteger> {
165  public:
166   // Since T has no << operator or PrintTo() but can be implicitly
167   // converted to BiggestInt, we print it as a BiggestInt.
168   //
169   // Most likely T is an enum type (either named or unnamed), in which
170   // case printing it as an integer is the desired behavior.  In case
171   // T is not an enum, printing it as an integer is the best we can do
172   // given that it has no user-defined printer.
173   static void PrintValue(const T& value, ::std::ostream* os) {
174     const internal::BiggestInt kBigInt = value;
175     *os << kBigInt;
176   }
177 };
178
179 // Prints the given value to the given ostream.  If the value is a
180 // protocol message, its debug string is printed; if it's an enum or
181 // of a type implicitly convertible to BiggestInt, it's printed as an
182 // integer; otherwise the bytes in the value are printed.  This is
183 // what UniversalPrinter<T>::Print() does when it knows nothing about
184 // type T and T has neither << operator nor PrintTo().
185 //
186 // A user can override this behavior for a class type Foo by defining
187 // a << operator in the namespace where Foo is defined.
188 //
189 // We put this operator in namespace 'internal2' instead of 'internal'
190 // to simplify the implementation, as much code in 'internal' needs to
191 // use << in STL, which would conflict with our own << were it defined
192 // in 'internal'.
193 //
194 // Note that this operator<< takes a generic std::basic_ostream<Char,
195 // CharTraits> type instead of the more restricted std::ostream.  If
196 // we define it to take an std::ostream instead, we'll get an
197 // "ambiguous overloads" compiler error when trying to print a type
198 // Foo that supports streaming to std::basic_ostream<Char,
199 // CharTraits>, as the compiler cannot tell whether
200 // operator<<(std::ostream&, const T&) or
201 // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
202 // specific.
203 template <typename Char, typename CharTraits, typename T>
204 ::std::basic_ostream<Char, CharTraits>& operator<<(
205     ::std::basic_ostream<Char, CharTraits>& os, const T& x) {
206   TypeWithoutFormatter<T,
207       (internal::IsAProtocolMessage<T>::value ? kProtobuf :
208        internal::ImplicitlyConvertible<const T&, internal::BiggestInt>::value ?
209        kConvertibleToInteger : kOtherType)>::PrintValue(x, &os);
210   return os;
211 }
212
213 }  // namespace internal2
214 }  // namespace testing
215
216 // This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
217 // magic needed for implementing UniversalPrinter won't work.
218 namespace testing_internal {
219
220 // Used to print a value that is not an STL-style container when the
221 // user doesn't define PrintTo() for it.
222 template <typename T>
223 void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
224   // With the following statement, during unqualified name lookup,
225   // testing::internal2::operator<< appears as if it was declared in
226   // the nearest enclosing namespace that contains both
227   // ::testing_internal and ::testing::internal2, i.e. the global
228   // namespace.  For more details, refer to the C++ Standard section
229   // 7.3.4-1 [namespace.udir].  This allows us to fall back onto
230   // testing::internal2::operator<< in case T doesn't come with a <<
231   // operator.
232   //
233   // We cannot write 'using ::testing::internal2::operator<<;', which
234   // gcc 3.3 fails to compile due to a compiler bug.
235   using namespace ::testing::internal2;  // NOLINT
236
237   // Assuming T is defined in namespace foo, in the next statement,
238   // the compiler will consider all of:
239   //
240   //   1. foo::operator<< (thanks to Koenig look-up),
241   //   2. ::operator<< (as the current namespace is enclosed in ::),
242   //   3. testing::internal2::operator<< (thanks to the using statement above).
243   //
244   // The operator<< whose type matches T best will be picked.
245   //
246   // We deliberately allow #2 to be a candidate, as sometimes it's
247   // impossible to define #1 (e.g. when foo is ::std, defining
248   // anything in it is undefined behavior unless you are a compiler
249   // vendor.).
250   *os << ::llvm_gtest::printable(value);
251 }
252
253 }  // namespace testing_internal
254
255 namespace testing {
256 namespace internal {
257
258 // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
259 // value of type ToPrint that is an operand of a comparison assertion
260 // (e.g. ASSERT_EQ).  OtherOperand is the type of the other operand in
261 // the comparison, and is used to help determine the best way to
262 // format the value.  In particular, when the value is a C string
263 // (char pointer) and the other operand is an STL string object, we
264 // want to format the C string as a string, since we know it is
265 // compared by value with the string object.  If the value is a char
266 // pointer but the other operand is not an STL string object, we don't
267 // know whether the pointer is supposed to point to a NUL-terminated
268 // string, and thus want to print it as a pointer to be safe.
269 //
270 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
271
272 // The default case.
273 template <typename ToPrint, typename OtherOperand>
274 class FormatForComparison {
275  public:
276   static ::std::string Format(const ToPrint& value) {
277     return ::testing::PrintToString(value);
278   }
279 };
280
281 // Array.
282 template <typename ToPrint, size_t N, typename OtherOperand>
283 class FormatForComparison<ToPrint[N], OtherOperand> {
284  public:
285   static ::std::string Format(const ToPrint* value) {
286     return FormatForComparison<const ToPrint*, OtherOperand>::Format(value);
287   }
288 };
289
290 // By default, print C string as pointers to be safe, as we don't know
291 // whether they actually point to a NUL-terminated string.
292
293 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType)                \
294   template <typename OtherOperand>                                      \
295   class FormatForComparison<CharType*, OtherOperand> {                  \
296    public:                                                              \
297     static ::std::string Format(CharType* value) {                      \
298       return ::testing::PrintToString(static_cast<const void*>(value)); \
299     }                                                                   \
300   }
301
302 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
303 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
304 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
305 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
306
307 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
308
309 // If a C string is compared with an STL string object, we know it's meant
310 // to point to a NUL-terminated string, and thus can print it as a string.
311
312 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
313   template <>                                                           \
314   class FormatForComparison<CharType*, OtherStringType> {               \
315    public:                                                              \
316     static ::std::string Format(CharType* value) {                      \
317       return ::testing::PrintToString(value);                           \
318     }                                                                   \
319   }
320
321 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
322 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
323
324 #if GTEST_HAS_GLOBAL_STRING
325 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::string);
326 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::string);
327 #endif
328
329 #if GTEST_HAS_GLOBAL_WSTRING
330 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::wstring);
331 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::wstring);
332 #endif
333
334 #if GTEST_HAS_STD_WSTRING
335 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
336 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
337 #endif
338
339 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
340
341 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
342 // operand to be used in a failure message.  The type (but not value)
343 // of the other operand may affect the format.  This allows us to
344 // print a char* as a raw pointer when it is compared against another
345 // char* or void*, and print it as a C string when it is compared
346 // against an std::string object, for example.
347 //
348 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
349 template <typename T1, typename T2>
350 std::string FormatForComparisonFailureMessage(
351     const T1& value, const T2& /* other_operand */) {
352   return FormatForComparison<T1, T2>::Format(value);
353 }
354
355 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
356 // value to the given ostream.  The caller must ensure that
357 // 'ostream_ptr' is not NULL, or the behavior is undefined.
358 //
359 // We define UniversalPrinter as a class template (as opposed to a
360 // function template), as we need to partially specialize it for
361 // reference types, which cannot be done with function templates.
362 template <typename T>
363 class UniversalPrinter;
364
365 template <typename T>
366 void UniversalPrint(const T& value, ::std::ostream* os);
367
368 // Used to print an STL-style container when the user doesn't define
369 // a PrintTo() for it.
370 template <typename C>
371 void DefaultPrintTo(IsContainer /* dummy */,
372                     false_type /* is not a pointer */,
373                     const C& container, ::std::ostream* os) {
374   const size_t kMaxCount = 32;  // The maximum number of elements to print.
375   *os << '{';
376   size_t count = 0;
377   for (typename C::const_iterator it = container.begin();
378        it != container.end(); ++it, ++count) {
379     if (count > 0) {
380       *os << ',';
381       if (count == kMaxCount) {  // Enough has been printed.
382         *os << " ...";
383         break;
384       }
385     }
386     *os << ' ';
387     // We cannot call PrintTo(*it, os) here as PrintTo() doesn't
388     // handle *it being a native array.
389     internal::UniversalPrint(*it, os);
390   }
391
392   if (count > 0) {
393     *os << ' ';
394   }
395   *os << '}';
396 }
397
398 // Used to print a pointer that is neither a char pointer nor a member
399 // pointer, when the user doesn't define PrintTo() for it.  (A member
400 // variable pointer or member function pointer doesn't really point to
401 // a location in the address space.  Their representation is
402 // implementation-defined.  Therefore they will be printed as raw
403 // bytes.)
404 template <typename T>
405 void DefaultPrintTo(IsNotContainer /* dummy */,
406                     true_type /* is a pointer */,
407                     T* p, ::std::ostream* os) {
408   if (p == NULL) {
409     *os << "NULL";
410   } else {
411     // C++ doesn't allow casting from a function pointer to any object
412     // pointer.
413     //
414     // IsTrue() silences warnings: "Condition is always true",
415     // "unreachable code".
416     if (IsTrue(ImplicitlyConvertible<T*, const void*>::value)) {
417       // T is not a function type.  We just call << to print p,
418       // relying on ADL to pick up user-defined << for their pointer
419       // types, if any.
420       *os << p;
421     } else {
422       // T is a function type, so '*os << p' doesn't do what we want
423       // (it just prints p as bool).  We want to print p as a const
424       // void*.  However, we cannot cast it to const void* directly,
425       // even using reinterpret_cast, as earlier versions of gcc
426       // (e.g. 3.4.5) cannot compile the cast when p is a function
427       // pointer.  Casting to UInt64 first solves the problem.
428       *os << reinterpret_cast<const void*>(
429           reinterpret_cast<internal::UInt64>(p));
430     }
431   }
432 }
433
434 // Used to print a non-container, non-pointer value when the user
435 // doesn't define PrintTo() for it.
436 template <typename T>
437 void DefaultPrintTo(IsNotContainer /* dummy */,
438                     false_type /* is not a pointer */,
439                     const T& value, ::std::ostream* os) {
440   ::testing_internal::DefaultPrintNonContainerTo(value, os);
441 }
442
443 // Prints the given value using the << operator if it has one;
444 // otherwise prints the bytes in it.  This is what
445 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
446 // or overloaded for type T.
447 //
448 // A user can override this behavior for a class type Foo by defining
449 // an overload of PrintTo() in the namespace where Foo is defined.  We
450 // give the user this option as sometimes defining a << operator for
451 // Foo is not desirable (e.g. the coding style may prevent doing it,
452 // or there is already a << operator but it doesn't do what the user
453 // wants).
454 template <typename T>
455 void PrintTo(const T& value, ::std::ostream* os) {
456   // DefaultPrintTo() is overloaded.  The type of its first two
457   // arguments determine which version will be picked.  If T is an
458   // STL-style container, the version for container will be called; if
459   // T is a pointer, the pointer version will be called; otherwise the
460   // generic version will be called.
461   //
462   // Note that we check for container types here, prior to we check
463   // for protocol message types in our operator<<.  The rationale is:
464   //
465   // For protocol messages, we want to give people a chance to
466   // override Google Mock's format by defining a PrintTo() or
467   // operator<<.  For STL containers, other formats can be
468   // incompatible with Google Mock's format for the container
469   // elements; therefore we check for container types here to ensure
470   // that our format is used.
471   //
472   // The second argument of DefaultPrintTo() is needed to bypass a bug
473   // in Symbian's C++ compiler that prevents it from picking the right
474   // overload between:
475   //
476   //   PrintTo(const T& x, ...);
477   //   PrintTo(T* x, ...);
478   DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
479 }
480
481 // The following list of PrintTo() overloads tells
482 // UniversalPrinter<T>::Print() how to print standard types (built-in
483 // types, strings, plain arrays, and pointers).
484
485 // Overloads for various char types.
486 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
487 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
488 inline void PrintTo(char c, ::std::ostream* os) {
489   // When printing a plain char, we always treat it as unsigned.  This
490   // way, the output won't be affected by whether the compiler thinks
491   // char is signed or not.
492   PrintTo(static_cast<unsigned char>(c), os);
493 }
494
495 // Overloads for other simple built-in types.
496 inline void PrintTo(bool x, ::std::ostream* os) {
497   *os << (x ? "true" : "false");
498 }
499
500 // Overload for wchar_t type.
501 // Prints a wchar_t as a symbol if it is printable or as its internal
502 // code otherwise and also as its decimal code (except for L'\0').
503 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
504 // as signed integer when wchar_t is implemented by the compiler
505 // as a signed type and is printed as an unsigned integer when wchar_t
506 // is implemented as an unsigned type.
507 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
508
509 // Overloads for C strings.
510 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
511 inline void PrintTo(char* s, ::std::ostream* os) {
512   PrintTo(ImplicitCast_<const char*>(s), os);
513 }
514
515 // signed/unsigned char is often used for representing binary data, so
516 // we print pointers to it as void* to be safe.
517 inline void PrintTo(const signed char* s, ::std::ostream* os) {
518   PrintTo(ImplicitCast_<const void*>(s), os);
519 }
520 inline void PrintTo(signed char* s, ::std::ostream* os) {
521   PrintTo(ImplicitCast_<const void*>(s), os);
522 }
523 inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
524   PrintTo(ImplicitCast_<const void*>(s), os);
525 }
526 inline void PrintTo(unsigned char* s, ::std::ostream* os) {
527   PrintTo(ImplicitCast_<const void*>(s), os);
528 }
529
530 // MSVC can be configured to define wchar_t as a typedef of unsigned
531 // short.  It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
532 // type.  When wchar_t is a typedef, defining an overload for const
533 // wchar_t* would cause unsigned short* be printed as a wide string,
534 // possibly causing invalid memory accesses.
535 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
536 // Overloads for wide C strings
537 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
538 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
539   PrintTo(ImplicitCast_<const wchar_t*>(s), os);
540 }
541 #endif
542
543 // Overload for C arrays.  Multi-dimensional arrays are printed
544 // properly.
545
546 // Prints the given number of elements in an array, without printing
547 // the curly braces.
548 template <typename T>
549 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
550   UniversalPrint(a[0], os);
551   for (size_t i = 1; i != count; i++) {
552     *os << ", ";
553     UniversalPrint(a[i], os);
554   }
555 }
556
557 // Overloads for ::string and ::std::string.
558 #if GTEST_HAS_GLOBAL_STRING
559 GTEST_API_ void PrintStringTo(const ::string&s, ::std::ostream* os);
560 inline void PrintTo(const ::string& s, ::std::ostream* os) {
561   PrintStringTo(s, os);
562 }
563 #endif  // GTEST_HAS_GLOBAL_STRING
564
565 GTEST_API_ void PrintStringTo(const ::std::string&s, ::std::ostream* os);
566 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
567   PrintStringTo(s, os);
568 }
569
570 // Overloads for ::wstring and ::std::wstring.
571 #if GTEST_HAS_GLOBAL_WSTRING
572 GTEST_API_ void PrintWideStringTo(const ::wstring&s, ::std::ostream* os);
573 inline void PrintTo(const ::wstring& s, ::std::ostream* os) {
574   PrintWideStringTo(s, os);
575 }
576 #endif  // GTEST_HAS_GLOBAL_WSTRING
577
578 #if GTEST_HAS_STD_WSTRING
579 GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
580 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
581   PrintWideStringTo(s, os);
582 }
583 #endif  // GTEST_HAS_STD_WSTRING
584
585 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
586 // Helper function for printing a tuple.  T must be instantiated with
587 // a tuple type.
588 template <typename T>
589 void PrintTupleTo(const T& t, ::std::ostream* os);
590 #endif  // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
591
592 #if GTEST_HAS_TR1_TUPLE
593 // Overload for ::std::tr1::tuple.  Needed for printing function arguments,
594 // which are packed as tuples.
595
596 // Overloaded PrintTo() for tuples of various arities.  We support
597 // tuples of up-to 10 fields.  The following implementation works
598 // regardless of whether tr1::tuple is implemented using the
599 // non-standard variadic template feature or not.
600
601 inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
602   PrintTupleTo(t, os);
603 }
604
605 template <typename T1>
606 void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
607   PrintTupleTo(t, os);
608 }
609
610 template <typename T1, typename T2>
611 void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
612   PrintTupleTo(t, os);
613 }
614
615 template <typename T1, typename T2, typename T3>
616 void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
617   PrintTupleTo(t, os);
618 }
619
620 template <typename T1, typename T2, typename T3, typename T4>
621 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
622   PrintTupleTo(t, os);
623 }
624
625 template <typename T1, typename T2, typename T3, typename T4, typename T5>
626 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
627              ::std::ostream* os) {
628   PrintTupleTo(t, os);
629 }
630
631 template <typename T1, typename T2, typename T3, typename T4, typename T5,
632           typename T6>
633 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
634              ::std::ostream* os) {
635   PrintTupleTo(t, os);
636 }
637
638 template <typename T1, typename T2, typename T3, typename T4, typename T5,
639           typename T6, typename T7>
640 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
641              ::std::ostream* os) {
642   PrintTupleTo(t, os);
643 }
644
645 template <typename T1, typename T2, typename T3, typename T4, typename T5,
646           typename T6, typename T7, typename T8>
647 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
648              ::std::ostream* os) {
649   PrintTupleTo(t, os);
650 }
651
652 template <typename T1, typename T2, typename T3, typename T4, typename T5,
653           typename T6, typename T7, typename T8, typename T9>
654 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t,
655              ::std::ostream* os) {
656   PrintTupleTo(t, os);
657 }
658
659 template <typename T1, typename T2, typename T3, typename T4, typename T5,
660           typename T6, typename T7, typename T8, typename T9, typename T10>
661 void PrintTo(
662     const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
663     ::std::ostream* os) {
664   PrintTupleTo(t, os);
665 }
666 #endif  // GTEST_HAS_TR1_TUPLE
667
668 #if GTEST_HAS_STD_TUPLE_
669 template <typename... Types>
670 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
671   PrintTupleTo(t, os);
672 }
673 #endif  // GTEST_HAS_STD_TUPLE_
674
675 // Overload for std::pair.
676 template <typename T1, typename T2>
677 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
678   *os << '(';
679   // We cannot use UniversalPrint(value.first, os) here, as T1 may be
680   // a reference type.  The same for printing value.second.
681   UniversalPrinter<T1>::Print(value.first, os);
682   *os << ", ";
683   UniversalPrinter<T2>::Print(value.second, os);
684   *os << ')';
685 }
686
687 // Implements printing a non-reference type T by letting the compiler
688 // pick the right overload of PrintTo() for T.
689 template <typename T>
690 class UniversalPrinter {
691  public:
692   // MSVC warns about adding const to a function type, so we want to
693   // disable the warning.
694   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
695
696   // Note: we deliberately don't call this PrintTo(), as that name
697   // conflicts with ::testing::internal::PrintTo in the body of the
698   // function.
699   static void Print(const T& value, ::std::ostream* os) {
700     // By default, ::testing::internal::PrintTo() is used for printing
701     // the value.
702     //
703     // Thanks to Koenig look-up, if T is a class and has its own
704     // PrintTo() function defined in its namespace, that function will
705     // be visible here.  Since it is more specific than the generic ones
706     // in ::testing::internal, it will be picked by the compiler in the
707     // following statement - exactly what we want.
708     PrintTo(value, os);
709   }
710
711   GTEST_DISABLE_MSC_WARNINGS_POP_()
712 };
713
714 // UniversalPrintArray(begin, len, os) prints an array of 'len'
715 // elements, starting at address 'begin'.
716 template <typename T>
717 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
718   if (len == 0) {
719     *os << "{}";
720   } else {
721     *os << "{ ";
722     const size_t kThreshold = 18;
723     const size_t kChunkSize = 8;
724     // If the array has more than kThreshold elements, we'll have to
725     // omit some details by printing only the first and the last
726     // kChunkSize elements.
727     // TODO(wan@google.com): let the user control the threshold using a flag.
728     if (len <= kThreshold) {
729       PrintRawArrayTo(begin, len, os);
730     } else {
731       PrintRawArrayTo(begin, kChunkSize, os);
732       *os << ", ..., ";
733       PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
734     }
735     *os << " }";
736   }
737 }
738 // This overload prints a (const) char array compactly.
739 GTEST_API_ void UniversalPrintArray(
740     const char* begin, size_t len, ::std::ostream* os);
741
742 // This overload prints a (const) wchar_t array compactly.
743 GTEST_API_ void UniversalPrintArray(
744     const wchar_t* begin, size_t len, ::std::ostream* os);
745
746 // Implements printing an array type T[N].
747 template <typename T, size_t N>
748 class UniversalPrinter<T[N]> {
749  public:
750   // Prints the given array, omitting some elements when there are too
751   // many.
752   static void Print(const T (&a)[N], ::std::ostream* os) {
753     UniversalPrintArray(a, N, os);
754   }
755 };
756
757 // Implements printing a reference type T&.
758 template <typename T>
759 class UniversalPrinter<T&> {
760  public:
761   // MSVC warns about adding const to a function type, so we want to
762   // disable the warning.
763   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
764
765   static void Print(const T& value, ::std::ostream* os) {
766     // Prints the address of the value.  We use reinterpret_cast here
767     // as static_cast doesn't compile when T is a function type.
768     *os << "@" << reinterpret_cast<const void*>(&value) << " ";
769
770     // Then prints the value itself.
771     UniversalPrint(value, os);
772   }
773
774   GTEST_DISABLE_MSC_WARNINGS_POP_()
775 };
776
777 // Prints a value tersely: for a reference type, the referenced value
778 // (but not the address) is printed; for a (const) char pointer, the
779 // NUL-terminated string (but not the pointer) is printed.
780
781 template <typename T>
782 class UniversalTersePrinter {
783  public:
784   static void Print(const T& value, ::std::ostream* os) {
785     UniversalPrint(value, os);
786   }
787 };
788 template <typename T>
789 class UniversalTersePrinter<T&> {
790  public:
791   static void Print(const T& value, ::std::ostream* os) {
792     UniversalPrint(value, os);
793   }
794 };
795 template <typename T, size_t N>
796 class UniversalTersePrinter<T[N]> {
797  public:
798   static void Print(const T (&value)[N], ::std::ostream* os) {
799     UniversalPrinter<T[N]>::Print(value, os);
800   }
801 };
802 template <>
803 class UniversalTersePrinter<const char*> {
804  public:
805   static void Print(const char* str, ::std::ostream* os) {
806     if (str == NULL) {
807       *os << "NULL";
808     } else {
809       UniversalPrint(string(str), os);
810     }
811   }
812 };
813 template <>
814 class UniversalTersePrinter<char*> {
815  public:
816   static void Print(char* str, ::std::ostream* os) {
817     UniversalTersePrinter<const char*>::Print(str, os);
818   }
819 };
820
821 #if GTEST_HAS_STD_WSTRING
822 template <>
823 class UniversalTersePrinter<const wchar_t*> {
824  public:
825   static void Print(const wchar_t* str, ::std::ostream* os) {
826     if (str == NULL) {
827       *os << "NULL";
828     } else {
829       UniversalPrint(::std::wstring(str), os);
830     }
831   }
832 };
833 #endif
834
835 template <>
836 class UniversalTersePrinter<wchar_t*> {
837  public:
838   static void Print(wchar_t* str, ::std::ostream* os) {
839     UniversalTersePrinter<const wchar_t*>::Print(str, os);
840   }
841 };
842
843 template <typename T>
844 void UniversalTersePrint(const T& value, ::std::ostream* os) {
845   UniversalTersePrinter<T>::Print(value, os);
846 }
847
848 // Prints a value using the type inferred by the compiler.  The
849 // difference between this and UniversalTersePrint() is that for a
850 // (const) char pointer, this prints both the pointer and the
851 // NUL-terminated string.
852 template <typename T>
853 void UniversalPrint(const T& value, ::std::ostream* os) {
854   // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
855   // UniversalPrinter with T directly.
856   typedef T T1;
857   UniversalPrinter<T1>::Print(value, os);
858 }
859
860 typedef ::std::vector<string> Strings;
861
862 // TuplePolicy<TupleT> must provide:
863 // - tuple_size
864 //     size of tuple TupleT.
865 // - get<size_t I>(const TupleT& t)
866 //     static function extracting element I of tuple TupleT.
867 // - tuple_element<size_t I>::type
868 //     type of element I of tuple TupleT.
869 template <typename TupleT>
870 struct TuplePolicy;
871
872 #if GTEST_HAS_TR1_TUPLE
873 template <typename TupleT>
874 struct TuplePolicy {
875   typedef TupleT Tuple;
876   static const size_t tuple_size = ::std::tr1::tuple_size<Tuple>::value;
877
878   template <size_t I>
879   struct tuple_element : ::std::tr1::tuple_element<I, Tuple> {};
880
881   template <size_t I>
882   static typename AddReference<
883       const typename ::std::tr1::tuple_element<I, Tuple>::type>::type get(
884       const Tuple& tuple) {
885     return ::std::tr1::get<I>(tuple);
886   }
887 };
888 template <typename TupleT>
889 const size_t TuplePolicy<TupleT>::tuple_size;
890 #endif  // GTEST_HAS_TR1_TUPLE
891
892 #if GTEST_HAS_STD_TUPLE_
893 template <typename... Types>
894 struct TuplePolicy< ::std::tuple<Types...> > {
895   typedef ::std::tuple<Types...> Tuple;
896   static const size_t tuple_size = ::std::tuple_size<Tuple>::value;
897
898   template <size_t I>
899   struct tuple_element : ::std::tuple_element<I, Tuple> {};
900
901   template <size_t I>
902   static const typename ::std::tuple_element<I, Tuple>::type& get(
903       const Tuple& tuple) {
904     return ::std::get<I>(tuple);
905   }
906 };
907 template <typename... Types>
908 const size_t TuplePolicy< ::std::tuple<Types...> >::tuple_size;
909 #endif  // GTEST_HAS_STD_TUPLE_
910
911 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
912 // This helper template allows PrintTo() for tuples and
913 // UniversalTersePrintTupleFieldsToStrings() to be defined by
914 // induction on the number of tuple fields.  The idea is that
915 // TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
916 // fields in tuple t, and can be defined in terms of
917 // TuplePrefixPrinter<N - 1>.
918 //
919 // The inductive case.
920 template <size_t N>
921 struct TuplePrefixPrinter {
922   // Prints the first N fields of a tuple.
923   template <typename Tuple>
924   static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
925     TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
926     GTEST_INTENTIONAL_CONST_COND_PUSH_()
927     if (N > 1) {
928     GTEST_INTENTIONAL_CONST_COND_POP_()
929       *os << ", ";
930     }
931     UniversalPrinter<
932         typename TuplePolicy<Tuple>::template tuple_element<N - 1>::type>
933         ::Print(TuplePolicy<Tuple>::template get<N - 1>(t), os);
934   }
935
936   // Tersely prints the first N fields of a tuple to a string vector,
937   // one element for each field.
938   template <typename Tuple>
939   static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
940     TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
941     ::std::stringstream ss;
942     UniversalTersePrint(TuplePolicy<Tuple>::template get<N - 1>(t), &ss);
943     strings->push_back(ss.str());
944   }
945 };
946
947 // Base case.
948 template <>
949 struct TuplePrefixPrinter<0> {
950   template <typename Tuple>
951   static void PrintPrefixTo(const Tuple&, ::std::ostream*) {}
952
953   template <typename Tuple>
954   static void TersePrintPrefixToStrings(const Tuple&, Strings*) {}
955 };
956
957 // Helper function for printing a tuple.
958 // Tuple must be either std::tr1::tuple or std::tuple type.
959 template <typename Tuple>
960 void PrintTupleTo(const Tuple& t, ::std::ostream* os) {
961   *os << "(";
962   TuplePrefixPrinter<TuplePolicy<Tuple>::tuple_size>::PrintPrefixTo(t, os);
963   *os << ")";
964 }
965
966 // Prints the fields of a tuple tersely to a string vector, one
967 // element for each field.  See the comment before
968 // UniversalTersePrint() for how we define "tersely".
969 template <typename Tuple>
970 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
971   Strings result;
972   TuplePrefixPrinter<TuplePolicy<Tuple>::tuple_size>::
973       TersePrintPrefixToStrings(value, &result);
974   return result;
975 }
976 #endif  // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
977
978 }  // namespace internal
979
980 template <typename T>
981 ::std::string PrintToString(const T& value) {
982   ::std::stringstream ss;
983   internal::UniversalTersePrinter<T>::Print(value, &ss);
984   return ss.str();
985 }
986
987 }  // namespace testing
988
989 // Include any custom printer added by the local installation.
990 // We must include this header at the end to make sure it can use the
991 // declarations from this file.
992 #include "gtest/internal/custom/gtest-printers.h"
993
994 #endif  // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_