Home | History | Annotate | Download | only in test
      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 (at) google.com (Zhanyong Wan)
     31 
     32 // Google Test - The Google C++ Testing Framework
     33 //
     34 // This file tests the universal value printer.
     35 
     36 #include "gtest/gtest-printers.h"
     37 
     38 #include <ctype.h>
     39 #include <limits.h>
     40 #include <string.h>
     41 #include <algorithm>
     42 #include <deque>
     43 #include <list>
     44 #include <map>
     45 #include <set>
     46 #include <sstream>
     47 #include <string>
     48 #include <utility>
     49 #include <vector>
     50 
     51 #include "gtest/gtest.h"
     52 
     53 // hash_map and hash_set are available under Visual C++.
     54 #if _MSC_VER
     55 # define GTEST_HAS_HASH_MAP_ 1  // Indicates that hash_map is available.
     56 # include <hash_map>            // NOLINT
     57 # define GTEST_HAS_HASH_SET_ 1  // Indicates that hash_set is available.
     58 # include <hash_set>            // NOLINT
     59 #endif  // GTEST_OS_WINDOWS
     60 
     61 // Some user-defined types for testing the universal value printer.
     62 
     63 // An anonymous enum type.
     64 enum AnonymousEnum {
     65   kAE1 = -1,
     66   kAE2 = 1
     67 };
     68 
     69 // An enum without a user-defined printer.
     70 enum EnumWithoutPrinter {
     71   kEWP1 = -2,
     72   kEWP2 = 42
     73 };
     74 
     75 // An enum with a << operator.
     76 enum EnumWithStreaming {
     77   kEWS1 = 10
     78 };
     79 
     80 std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) {
     81   return os << (e == kEWS1 ? "kEWS1" : "invalid");
     82 }
     83 
     84 // An enum with a PrintTo() function.
     85 enum EnumWithPrintTo {
     86   kEWPT1 = 1
     87 };
     88 
     89 void PrintTo(EnumWithPrintTo e, std::ostream* os) {
     90   *os << (e == kEWPT1 ? "kEWPT1" : "invalid");
     91 }
     92 
     93 // A class implicitly convertible to BiggestInt.
     94 class BiggestIntConvertible {
     95  public:
     96   operator ::testing::internal::BiggestInt() const { return 42; }
     97 };
     98 
     99 // A user-defined unprintable class template in the global namespace.
    100 template <typename T>
    101 class UnprintableTemplateInGlobal {
    102  public:
    103   UnprintableTemplateInGlobal() : value_() {}
    104  private:
    105   T value_;
    106 };
    107 
    108 // A user-defined streamable type in the global namespace.
    109 class StreamableInGlobal {
    110  public:
    111   virtual ~StreamableInGlobal() {}
    112 };
    113 
    114 inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) {
    115   os << "StreamableInGlobal";
    116 }
    117 
    118 void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) {
    119   os << "StreamableInGlobal*";
    120 }
    121 
    122 namespace foo {
    123 
    124 // A user-defined unprintable type in a user namespace.
    125 class UnprintableInFoo {
    126  public:
    127   UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); }
    128   double z() const { return z_; }
    129  private:
    130   char xy_[8];
    131   double z_;
    132 };
    133 
    134 // A user-defined printable type in a user-chosen namespace.
    135 struct PrintableViaPrintTo {
    136   PrintableViaPrintTo() : value() {}
    137   int value;
    138 };
    139 
    140 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
    141   *os << "PrintableViaPrintTo: " << x.value;
    142 }
    143 
    144 // A type with a user-defined << for printing its pointer.
    145 struct PointerPrintable {
    146 };
    147 
    148 ::std::ostream& operator<<(::std::ostream& os,
    149                            const PointerPrintable* /* x */) {
    150   return os << "PointerPrintable*";
    151 }
    152 
    153 // A user-defined printable class template in a user-chosen namespace.
    154 template <typename T>
    155 class PrintableViaPrintToTemplate {
    156  public:
    157   explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
    158 
    159   const T& value() const { return value_; }
    160  private:
    161   T value_;
    162 };
    163 
    164 template <typename T>
    165 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
    166   *os << "PrintableViaPrintToTemplate: " << x.value();
    167 }
    168 
    169 // A user-defined streamable class template in a user namespace.
    170 template <typename T>
    171 class StreamableTemplateInFoo {
    172  public:
    173   StreamableTemplateInFoo() : value_() {}
    174 
    175   const T& value() const { return value_; }
    176  private:
    177   T value_;
    178 };
    179 
    180 template <typename T>
    181 inline ::std::ostream& operator<<(::std::ostream& os,
    182                                   const StreamableTemplateInFoo<T>& x) {
    183   return os << "StreamableTemplateInFoo: " << x.value();
    184 }
    185 
    186 }  // namespace foo
    187 
    188 namespace testing {
    189 namespace gtest_printers_test {
    190 
    191 using ::std::deque;
    192 using ::std::list;
    193 using ::std::make_pair;
    194 using ::std::map;
    195 using ::std::multimap;
    196 using ::std::multiset;
    197 using ::std::pair;
    198 using ::std::set;
    199 using ::std::vector;
    200 using ::testing::PrintToString;
    201 using ::testing::internal::FormatForComparisonFailureMessage;
    202 using ::testing::internal::ImplicitCast_;
    203 using ::testing::internal::NativeArray;
    204 using ::testing::internal::RE;
    205 using ::testing::internal::Strings;
    206 using ::testing::internal::UniversalPrint;
    207 using ::testing::internal::UniversalPrinter;
    208 using ::testing::internal::UniversalTersePrint;
    209 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
    210 using ::testing::internal::kReference;
    211 using ::testing::internal::string;
    212 
    213 // The hash_* classes are not part of the C++ standard.  STLport
    214 // defines them in namespace std.  MSVC defines them in ::stdext.  GCC
    215 // defines them in ::.
    216 #ifdef _STLP_HASH_MAP  // We got <hash_map> from STLport.
    217 using ::std::hash_map;
    218 using ::std::hash_set;
    219 using ::std::hash_multimap;
    220 using ::std::hash_multiset;
    221 #elif _MSC_VER
    222 using ::stdext::hash_map;
    223 using ::stdext::hash_set;
    224 using ::stdext::hash_multimap;
    225 using ::stdext::hash_multiset;
    226 #endif
    227 
    228 // Prints a value to a string using the universal value printer.  This
    229 // is a helper for testing UniversalPrinter<T>::Print() for various types.
    230 template <typename T>
    231 string Print(const T& value) {
    232   ::std::stringstream ss;
    233   UniversalPrinter<T>::Print(value, &ss);
    234   return ss.str();
    235 }
    236 
    237 // Prints a value passed by reference to a string, using the universal
    238 // value printer.  This is a helper for testing
    239 // UniversalPrinter<T&>::Print() for various types.
    240 template <typename T>
    241 string PrintByRef(const T& value) {
    242   ::std::stringstream ss;
    243   UniversalPrinter<T&>::Print(value, &ss);
    244   return ss.str();
    245 }
    246 
    247 // Tests printing various enum types.
    248 
    249 TEST(PrintEnumTest, AnonymousEnum) {
    250   EXPECT_EQ("-1", Print(kAE1));
    251   EXPECT_EQ("1", Print(kAE2));
    252 }
    253 
    254 TEST(PrintEnumTest, EnumWithoutPrinter) {
    255   EXPECT_EQ("-2", Print(kEWP1));
    256   EXPECT_EQ("42", Print(kEWP2));
    257 }
    258 
    259 TEST(PrintEnumTest, EnumWithStreaming) {
    260   EXPECT_EQ("kEWS1", Print(kEWS1));
    261   EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
    262 }
    263 
    264 TEST(PrintEnumTest, EnumWithPrintTo) {
    265   EXPECT_EQ("kEWPT1", Print(kEWPT1));
    266   EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
    267 }
    268 
    269 // Tests printing a class implicitly convertible to BiggestInt.
    270 
    271 TEST(PrintClassTest, BiggestIntConvertible) {
    272   EXPECT_EQ("42", Print(BiggestIntConvertible()));
    273 }
    274 
    275 // Tests printing various char types.
    276 
    277 // char.
    278 TEST(PrintCharTest, PlainChar) {
    279   EXPECT_EQ("'\\0'", Print('\0'));
    280   EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
    281   EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
    282   EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
    283   EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
    284   EXPECT_EQ("'\\a' (7)", Print('\a'));
    285   EXPECT_EQ("'\\b' (8)", Print('\b'));
    286   EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
    287   EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
    288   EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
    289   EXPECT_EQ("'\\t' (9)", Print('\t'));
    290   EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
    291   EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
    292   EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
    293   EXPECT_EQ("' ' (32, 0x20)", Print(' '));
    294   EXPECT_EQ("'a' (97, 0x61)", Print('a'));
    295 }
    296 
    297 // signed char.
    298 TEST(PrintCharTest, SignedChar) {
    299   EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
    300   EXPECT_EQ("'\\xCE' (-50)",
    301             Print(static_cast<signed char>(-50)));
    302 }
    303 
    304 // unsigned char.
    305 TEST(PrintCharTest, UnsignedChar) {
    306   EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
    307   EXPECT_EQ("'b' (98, 0x62)",
    308             Print(static_cast<unsigned char>('b')));
    309 }
    310 
    311 // Tests printing other simple, built-in types.
    312 
    313 // bool.
    314 TEST(PrintBuiltInTypeTest, Bool) {
    315   EXPECT_EQ("false", Print(false));
    316   EXPECT_EQ("true", Print(true));
    317 }
    318 
    319 // wchar_t.
    320 TEST(PrintBuiltInTypeTest, Wchar_t) {
    321   EXPECT_EQ("L'\\0'", Print(L'\0'));
    322   EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
    323   EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
    324   EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
    325   EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
    326   EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
    327   EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
    328   EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
    329   EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
    330   EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
    331   EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
    332   EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
    333   EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
    334   EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
    335   EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
    336   EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
    337   EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
    338   EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
    339 }
    340 
    341 // Test that Int64 provides more storage than wchar_t.
    342 TEST(PrintTypeSizeTest, Wchar_t) {
    343   EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
    344 }
    345 
    346 // Various integer types.
    347 TEST(PrintBuiltInTypeTest, Integer) {
    348   EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255)));  // uint8
    349   EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128)));  // int8
    350   EXPECT_EQ("65535", Print(USHRT_MAX));  // uint16
    351   EXPECT_EQ("-32768", Print(SHRT_MIN));  // int16
    352   EXPECT_EQ("4294967295", Print(UINT_MAX));  // uint32
    353   EXPECT_EQ("-2147483648", Print(INT_MIN));  // int32
    354   EXPECT_EQ("18446744073709551615",
    355             Print(static_cast<testing::internal::UInt64>(-1)));  // uint64
    356   EXPECT_EQ("-9223372036854775808",
    357             Print(static_cast<testing::internal::Int64>(1) << 63));  // int64
    358 }
    359 
    360 // Size types.
    361 TEST(PrintBuiltInTypeTest, Size_t) {
    362   EXPECT_EQ("1", Print(sizeof('a')));  // size_t.
    363 #if !GTEST_OS_WINDOWS
    364   // Windows has no ssize_t type.
    365   EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2)));  // ssize_t.
    366 #endif  // !GTEST_OS_WINDOWS
    367 }
    368 
    369 // Floating-points.
    370 TEST(PrintBuiltInTypeTest, FloatingPoints) {
    371   EXPECT_EQ("1.5", Print(1.5f));   // float
    372   EXPECT_EQ("-2.5", Print(-2.5));  // double
    373 }
    374 
    375 // Since ::std::stringstream::operator<<(const void *) formats the pointer
    376 // output differently with different compilers, we have to create the expected
    377 // output first and use it as our expectation.
    378 static string PrintPointer(const void *p) {
    379   ::std::stringstream expected_result_stream;
    380   expected_result_stream << p;
    381   return expected_result_stream.str();
    382 }
    383 
    384 // Tests printing C strings.
    385 
    386 // const char*.
    387 TEST(PrintCStringTest, Const) {
    388   const char* p = "World";
    389   EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
    390 }
    391 
    392 // char*.
    393 TEST(PrintCStringTest, NonConst) {
    394   char p[] = "Hi";
    395   EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
    396             Print(static_cast<char*>(p)));
    397 }
    398 
    399 // NULL C string.
    400 TEST(PrintCStringTest, Null) {
    401   const char* p = NULL;
    402   EXPECT_EQ("NULL", Print(p));
    403 }
    404 
    405 // Tests that C strings are escaped properly.
    406 TEST(PrintCStringTest, EscapesProperly) {
    407   const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
    408   EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
    409             "\\n\\r\\t\\v\\x7F\\xFF a\"",
    410             Print(p));
    411 }
    412 
    413 // MSVC compiler can be configured to define whar_t as a typedef
    414 // of unsigned short. Defining an overload for const wchar_t* in that case
    415 // would cause pointers to unsigned shorts be printed as wide strings,
    416 // possibly accessing more memory than intended and causing invalid
    417 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
    418 // wchar_t is implemented as a native type.
    419 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
    420 
    421 // const wchar_t*.
    422 TEST(PrintWideCStringTest, Const) {
    423   const wchar_t* p = L"World";
    424   EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
    425 }
    426 
    427 // wchar_t*.
    428 TEST(PrintWideCStringTest, NonConst) {
    429   wchar_t p[] = L"Hi";
    430   EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
    431             Print(static_cast<wchar_t*>(p)));
    432 }
    433 
    434 // NULL wide C string.
    435 TEST(PrintWideCStringTest, Null) {
    436   const wchar_t* p = NULL;
    437   EXPECT_EQ("NULL", Print(p));
    438 }
    439 
    440 // Tests that wide C strings are escaped properly.
    441 TEST(PrintWideCStringTest, EscapesProperly) {
    442   const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r',
    443                        '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
    444   EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f"
    445             "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
    446             Print(static_cast<const wchar_t*>(s)));
    447 }
    448 #endif  // native wchar_t
    449 
    450 // Tests printing pointers to other char types.
    451 
    452 // signed char*.
    453 TEST(PrintCharPointerTest, SignedChar) {
    454   signed char* p = reinterpret_cast<signed char*>(0x1234);
    455   EXPECT_EQ(PrintPointer(p), Print(p));
    456   p = NULL;
    457   EXPECT_EQ("NULL", Print(p));
    458 }
    459 
    460 // const signed char*.
    461 TEST(PrintCharPointerTest, ConstSignedChar) {
    462   signed char* p = reinterpret_cast<signed char*>(0x1234);
    463   EXPECT_EQ(PrintPointer(p), Print(p));
    464   p = NULL;
    465   EXPECT_EQ("NULL", Print(p));
    466 }
    467 
    468 // unsigned char*.
    469 TEST(PrintCharPointerTest, UnsignedChar) {
    470   unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
    471   EXPECT_EQ(PrintPointer(p), Print(p));
    472   p = NULL;
    473   EXPECT_EQ("NULL", Print(p));
    474 }
    475 
    476 // const unsigned char*.
    477 TEST(PrintCharPointerTest, ConstUnsignedChar) {
    478   const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
    479   EXPECT_EQ(PrintPointer(p), Print(p));
    480   p = NULL;
    481   EXPECT_EQ("NULL", Print(p));
    482 }
    483 
    484 // Tests printing pointers to simple, built-in types.
    485 
    486 // bool*.
    487 TEST(PrintPointerToBuiltInTypeTest, Bool) {
    488   bool* p = reinterpret_cast<bool*>(0xABCD);
    489   EXPECT_EQ(PrintPointer(p), Print(p));
    490   p = NULL;
    491   EXPECT_EQ("NULL", Print(p));
    492 }
    493 
    494 // void*.
    495 TEST(PrintPointerToBuiltInTypeTest, Void) {
    496   void* p = reinterpret_cast<void*>(0xABCD);
    497   EXPECT_EQ(PrintPointer(p), Print(p));
    498   p = NULL;
    499   EXPECT_EQ("NULL", Print(p));
    500 }
    501 
    502 // const void*.
    503 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
    504   const void* p = reinterpret_cast<const void*>(0xABCD);
    505   EXPECT_EQ(PrintPointer(p), Print(p));
    506   p = NULL;
    507   EXPECT_EQ("NULL", Print(p));
    508 }
    509 
    510 // Tests printing pointers to pointers.
    511 TEST(PrintPointerToPointerTest, IntPointerPointer) {
    512   int** p = reinterpret_cast<int**>(0xABCD);
    513   EXPECT_EQ(PrintPointer(p), Print(p));
    514   p = NULL;
    515   EXPECT_EQ("NULL", Print(p));
    516 }
    517 
    518 // Tests printing (non-member) function pointers.
    519 
    520 void MyFunction(int /* n */) {}
    521 
    522 TEST(PrintPointerTest, NonMemberFunctionPointer) {
    523   // We cannot directly cast &MyFunction to const void* because the
    524   // standard disallows casting between pointers to functions and
    525   // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
    526   // this limitation.
    527   EXPECT_EQ(
    528       PrintPointer(reinterpret_cast<const void*>(
    529           reinterpret_cast<internal::BiggestInt>(&MyFunction))),
    530       Print(&MyFunction));
    531   int (*p)(bool) = NULL;  // NOLINT
    532   EXPECT_EQ("NULL", Print(p));
    533 }
    534 
    535 // An assertion predicate determining whether a one string is a prefix for
    536 // another.
    537 template <typename StringType>
    538 AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
    539   if (str.find(prefix, 0) == 0)
    540     return AssertionSuccess();
    541 
    542   const bool is_wide_string = sizeof(prefix[0]) > 1;
    543   const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
    544   return AssertionFailure()
    545       << begin_string_quote << prefix << "\" is not a prefix of "
    546       << begin_string_quote << str << "\"\n";
    547 }
    548 
    549 // Tests printing member variable pointers.  Although they are called
    550 // pointers, they don't point to a location in the address space.
    551 // Their representation is implementation-defined.  Thus they will be
    552 // printed as raw bytes.
    553 
    554 struct Foo {
    555  public:
    556   virtual ~Foo() {}
    557   int MyMethod(char x) { return x + 1; }
    558   virtual char MyVirtualMethod(int /* n */) { return 'a'; }
    559 
    560   int value;
    561 };
    562 
    563 TEST(PrintPointerTest, MemberVariablePointer) {
    564   EXPECT_TRUE(HasPrefix(Print(&Foo::value),
    565                         Print(sizeof(&Foo::value)) + "-byte object "));
    566   int (Foo::*p) = NULL;  // NOLINT
    567   EXPECT_TRUE(HasPrefix(Print(p),
    568                         Print(sizeof(p)) + "-byte object "));
    569 }
    570 
    571 // Tests printing member function pointers.  Although they are called
    572 // pointers, they don't point to a location in the address space.
    573 // Their representation is implementation-defined.  Thus they will be
    574 // printed as raw bytes.
    575 TEST(PrintPointerTest, MemberFunctionPointer) {
    576   EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod),
    577                         Print(sizeof(&Foo::MyMethod)) + "-byte object "));
    578   EXPECT_TRUE(
    579       HasPrefix(Print(&Foo::MyVirtualMethod),
    580                 Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
    581   int (Foo::*p)(char) = NULL;  // NOLINT
    582   EXPECT_TRUE(HasPrefix(Print(p),
    583                         Print(sizeof(p)) + "-byte object "));
    584 }
    585 
    586 // Tests printing C arrays.
    587 
    588 // The difference between this and Print() is that it ensures that the
    589 // argument is a reference to an array.
    590 template <typename T, size_t N>
    591 string PrintArrayHelper(T (&a)[N]) {
    592   return Print(a);
    593 }
    594 
    595 // One-dimensional array.
    596 TEST(PrintArrayTest, OneDimensionalArray) {
    597   int a[5] = { 1, 2, 3, 4, 5 };
    598   EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
    599 }
    600 
    601 // Two-dimensional array.
    602 TEST(PrintArrayTest, TwoDimensionalArray) {
    603   int a[2][5] = {
    604     { 1, 2, 3, 4, 5 },
    605     { 6, 7, 8, 9, 0 }
    606   };
    607   EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
    608 }
    609 
    610 // Array of const elements.
    611 TEST(PrintArrayTest, ConstArray) {
    612   const bool a[1] = { false };
    613   EXPECT_EQ("{ false }", PrintArrayHelper(a));
    614 }
    615 
    616 // char array without terminating NUL.
    617 TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
    618   // Array a contains '\0' in the middle and doesn't end with '\0'.
    619   char a[] = { 'H', '\0', 'i' };
    620   EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
    621 }
    622 
    623 // const char array with terminating NUL.
    624 TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) {
    625   const char a[] = "\0Hi";
    626   EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a));
    627 }
    628 
    629 // const wchar_t array without terminating NUL.
    630 TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
    631   // Array a contains '\0' in the middle and doesn't end with '\0'.
    632   const wchar_t a[] = { L'H', L'\0', L'i' };
    633   EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
    634 }
    635 
    636 // wchar_t array with terminating NUL.
    637 TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
    638   const wchar_t a[] = L"\0Hi";
    639   EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a));
    640 }
    641 
    642 // Array of objects.
    643 TEST(PrintArrayTest, ObjectArray) {
    644   string a[3] = { "Hi", "Hello", "Ni hao" };
    645   EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
    646 }
    647 
    648 // Array with many elements.
    649 TEST(PrintArrayTest, BigArray) {
    650   int a[100] = { 1, 2, 3 };
    651   EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
    652             PrintArrayHelper(a));
    653 }
    654 
    655 // Tests printing ::string and ::std::string.
    656 
    657 #if GTEST_HAS_GLOBAL_STRING
    658 // ::string.
    659 TEST(PrintStringTest, StringInGlobalNamespace) {
    660   const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
    661   const ::string str(s, sizeof(s));
    662   EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
    663             Print(str));
    664 }
    665 #endif  // GTEST_HAS_GLOBAL_STRING
    666 
    667 // ::std::string.
    668 TEST(PrintStringTest, StringInStdNamespace) {
    669   const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
    670   const ::std::string str(s, sizeof(s));
    671   EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
    672             Print(str));
    673 }
    674 
    675 TEST(PrintStringTest, StringAmbiguousHex) {
    676   // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
    677   // '\x6', '\x6B', or '\x6BA'.
    678 
    679   // a hex escaping sequence following by a decimal digit
    680   EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
    681   // a hex escaping sequence following by a hex digit (lower-case)
    682   EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
    683   // a hex escaping sequence following by a hex digit (upper-case)
    684   EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
    685   // a hex escaping sequence following by a non-xdigit
    686   EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
    687 }
    688 
    689 // Tests printing ::wstring and ::std::wstring.
    690 
    691 #if GTEST_HAS_GLOBAL_WSTRING
    692 // ::wstring.
    693 TEST(PrintWideStringTest, StringInGlobalNamespace) {
    694   const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
    695   const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
    696   EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
    697             "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
    698             Print(str));
    699 }
    700 #endif  // GTEST_HAS_GLOBAL_WSTRING
    701 
    702 #if GTEST_HAS_STD_WSTRING
    703 // ::std::wstring.
    704 TEST(PrintWideStringTest, StringInStdNamespace) {
    705   const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
    706   const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
    707   EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
    708             "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
    709             Print(str));
    710 }
    711 
    712 TEST(PrintWideStringTest, StringAmbiguousHex) {
    713   // same for wide strings.
    714   EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3")));
    715   EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
    716             Print(::std::wstring(L"mm\x6" L"bananas")));
    717   EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
    718             Print(::std::wstring(L"NOM\x6" L"BANANA")));
    719   EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
    720 }
    721 #endif  // GTEST_HAS_STD_WSTRING
    722 
    723 // Tests printing types that support generic streaming (i.e. streaming
    724 // to std::basic_ostream<Char, CharTraits> for any valid Char and
    725 // CharTraits types).
    726 
    727 // Tests printing a non-template type that supports generic streaming.
    728 
    729 class AllowsGenericStreaming {};
    730 
    731 template <typename Char, typename CharTraits>
    732 std::basic_ostream<Char, CharTraits>& operator<<(
    733     std::basic_ostream<Char, CharTraits>& os,
    734     const AllowsGenericStreaming& /* a */) {
    735   return os << "AllowsGenericStreaming";
    736 }
    737 
    738 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
    739   AllowsGenericStreaming a;
    740   EXPECT_EQ("AllowsGenericStreaming", Print(a));
    741 }
    742 
    743 // Tests printing a template type that supports generic streaming.
    744 
    745 template <typename T>
    746 class AllowsGenericStreamingTemplate {};
    747 
    748 template <typename Char, typename CharTraits, typename T>
    749 std::basic_ostream<Char, CharTraits>& operator<<(
    750     std::basic_ostream<Char, CharTraits>& os,
    751     const AllowsGenericStreamingTemplate<T>& /* a */) {
    752   return os << "AllowsGenericStreamingTemplate";
    753 }
    754 
    755 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
    756   AllowsGenericStreamingTemplate<int> a;
    757   EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
    758 }
    759 
    760 // Tests printing a type that supports generic streaming and can be
    761 // implicitly converted to another printable type.
    762 
    763 template <typename T>
    764 class AllowsGenericStreamingAndImplicitConversionTemplate {
    765  public:
    766   operator bool() const { return false; }
    767 };
    768 
    769 template <typename Char, typename CharTraits, typename T>
    770 std::basic_ostream<Char, CharTraits>& operator<<(
    771     std::basic_ostream<Char, CharTraits>& os,
    772     const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) {
    773   return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
    774 }
    775 
    776 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
    777   AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
    778   EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
    779 }
    780 
    781 #if GTEST_HAS_STRING_PIECE_
    782 
    783 // Tests printing StringPiece.
    784 
    785 TEST(PrintStringPieceTest, SimpleStringPiece) {
    786   const StringPiece sp = "Hello";
    787   EXPECT_EQ("\"Hello\"", Print(sp));
    788 }
    789 
    790 TEST(PrintStringPieceTest, UnprintableCharacters) {
    791   const char str[] = "NUL (\0) and \r\t";
    792   const StringPiece sp(str, sizeof(str) - 1);
    793   EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
    794 }
    795 
    796 #endif  // GTEST_HAS_STRING_PIECE_
    797 
    798 // Tests printing STL containers.
    799 
    800 TEST(PrintStlContainerTest, EmptyDeque) {
    801   deque<char> empty;
    802   EXPECT_EQ("{}", Print(empty));
    803 }
    804 
    805 TEST(PrintStlContainerTest, NonEmptyDeque) {
    806   deque<int> non_empty;
    807   non_empty.push_back(1);
    808   non_empty.push_back(3);
    809   EXPECT_EQ("{ 1, 3 }", Print(non_empty));
    810 }
    811 
    812 #if GTEST_HAS_HASH_MAP_
    813 
    814 TEST(PrintStlContainerTest, OneElementHashMap) {
    815   hash_map<int, char> map1;
    816   map1[1] = 'a';
    817   EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1));
    818 }
    819 
    820 TEST(PrintStlContainerTest, HashMultiMap) {
    821   hash_multimap<int, bool> map1;
    822   map1.insert(make_pair(5, true));
    823   map1.insert(make_pair(5, false));
    824 
    825   // Elements of hash_multimap can be printed in any order.
    826   const string result = Print(map1);
    827   EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
    828               result == "{ (5, false), (5, true) }")
    829                   << " where Print(map1) returns \"" << result << "\".";
    830 }
    831 
    832 #endif  // GTEST_HAS_HASH_MAP_
    833 
    834 #if GTEST_HAS_HASH_SET_
    835 
    836 TEST(PrintStlContainerTest, HashSet) {
    837   hash_set<string> set1;
    838   set1.insert("hello");
    839   EXPECT_EQ("{ \"hello\" }", Print(set1));
    840 }
    841 
    842 TEST(PrintStlContainerTest, HashMultiSet) {
    843   const int kSize = 5;
    844   int a[kSize] = { 1, 1, 2, 5, 1 };
    845   hash_multiset<int> set1(a, a + kSize);
    846 
    847   // Elements of hash_multiset can be printed in any order.
    848   const string result = Print(set1);
    849   const string expected_pattern = "{ d, d, d, d, d }";  // d means a digit.
    850 
    851   // Verifies the result matches the expected pattern; also extracts
    852   // the numbers in the result.
    853   ASSERT_EQ(expected_pattern.length(), result.length());
    854   std::vector<int> numbers;
    855   for (size_t i = 0; i != result.length(); i++) {
    856     if (expected_pattern[i] == 'd') {
    857       ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0);
    858       numbers.push_back(result[i] - '0');
    859     } else {
    860       EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
    861                                                 << result;
    862     }
    863   }
    864 
    865   // Makes sure the result contains the right numbers.
    866   std::sort(numbers.begin(), numbers.end());
    867   std::sort(a, a + kSize);
    868   EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
    869 }
    870 
    871 #endif  // GTEST_HAS_HASH_SET_
    872 
    873 TEST(PrintStlContainerTest, List) {
    874   const string a[] = {
    875     "hello",
    876     "world"
    877   };
    878   const list<string> strings(a, a + 2);
    879   EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
    880 }
    881 
    882 TEST(PrintStlContainerTest, Map) {
    883   map<int, bool> map1;
    884   map1[1] = true;
    885   map1[5] = false;
    886   map1[3] = true;
    887   EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
    888 }
    889 
    890 TEST(PrintStlContainerTest, MultiMap) {
    891   multimap<bool, int> map1;
    892   // The make_pair template function would deduce the type as
    893   // pair<bool, int> here, and since the key part in a multimap has to
    894   // be constant, without a templated ctor in the pair class (as in
    895   // libCstd on Solaris), make_pair call would fail to compile as no
    896   // implicit conversion is found.  Thus explicit typename is used
    897   // here instead.
    898   map1.insert(pair<const bool, int>(true, 0));
    899   map1.insert(pair<const bool, int>(true, 1));
    900   map1.insert(pair<const bool, int>(false, 2));
    901   EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
    902 }
    903 
    904 TEST(PrintStlContainerTest, Set) {
    905   const unsigned int a[] = { 3, 0, 5 };
    906   set<unsigned int> set1(a, a + 3);
    907   EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
    908 }
    909 
    910 TEST(PrintStlContainerTest, MultiSet) {
    911   const int a[] = { 1, 1, 2, 5, 1 };
    912   multiset<int> set1(a, a + 5);
    913   EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
    914 }
    915 
    916 TEST(PrintStlContainerTest, Pair) {
    917   pair<const bool, int> p(true, 5);
    918   EXPECT_EQ("(true, 5)", Print(p));
    919 }
    920 
    921 TEST(PrintStlContainerTest, Vector) {
    922   vector<int> v;
    923   v.push_back(1);
    924   v.push_back(2);
    925   EXPECT_EQ("{ 1, 2 }", Print(v));
    926 }
    927 
    928 TEST(PrintStlContainerTest, LongSequence) {
    929   const int a[100] = { 1, 2, 3 };
    930   const vector<int> v(a, a + 100);
    931   EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
    932             "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
    933 }
    934 
    935 TEST(PrintStlContainerTest, NestedContainer) {
    936   const int a1[] = { 1, 2 };
    937   const int a2[] = { 3, 4, 5 };
    938   const list<int> l1(a1, a1 + 2);
    939   const list<int> l2(a2, a2 + 3);
    940 
    941   vector<list<int> > v;
    942   v.push_back(l1);
    943   v.push_back(l2);
    944   EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
    945 }
    946 
    947 TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
    948   const int a[3] = { 1, 2, 3 };
    949   NativeArray<int> b(a, 3, kReference);
    950   EXPECT_EQ("{ 1, 2, 3 }", Print(b));
    951 }
    952 
    953 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
    954   const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
    955   NativeArray<int[3]> b(a, 2, kReference);
    956   EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
    957 }
    958 
    959 // Tests that a class named iterator isn't treated as a container.
    960 
    961 struct iterator {
    962   char x;
    963 };
    964 
    965 TEST(PrintStlContainerTest, Iterator) {
    966   iterator it = {};
    967   EXPECT_EQ("1-byte object <00>", Print(it));
    968 }
    969 
    970 // Tests that a class named const_iterator isn't treated as a container.
    971 
    972 struct const_iterator {
    973   char x;
    974 };
    975 
    976 TEST(PrintStlContainerTest, ConstIterator) {
    977   const_iterator it = {};
    978   EXPECT_EQ("1-byte object <00>", Print(it));
    979 }
    980 
    981 #if GTEST_HAS_TR1_TUPLE
    982 // Tests printing ::std::tr1::tuples.
    983 
    984 // Tuples of various arities.
    985 TEST(PrintTr1TupleTest, VariousSizes) {
    986   ::std::tr1::tuple<> t0;
    987   EXPECT_EQ("()", Print(t0));
    988 
    989   ::std::tr1::tuple<int> t1(5);
    990   EXPECT_EQ("(5)", Print(t1));
    991 
    992   ::std::tr1::tuple<char, bool> t2('a', true);
    993   EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
    994 
    995   ::std::tr1::tuple<bool, int, int> t3(false, 2, 3);
    996   EXPECT_EQ("(false, 2, 3)", Print(t3));
    997 
    998   ::std::tr1::tuple<bool, int, int, int> t4(false, 2, 3, 4);
    999   EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
   1000 
   1001   ::std::tr1::tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
   1002   EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
   1003 
   1004   ::std::tr1::tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
   1005   EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
   1006 
   1007   ::std::tr1::tuple<bool, int, int, int, bool, int, int> t7(
   1008       false, 2, 3, 4, true, 6, 7);
   1009   EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
   1010 
   1011   ::std::tr1::tuple<bool, int, int, int, bool, int, int, bool> t8(
   1012       false, 2, 3, 4, true, 6, 7, true);
   1013   EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
   1014 
   1015   ::std::tr1::tuple<bool, int, int, int, bool, int, int, bool, int> t9(
   1016       false, 2, 3, 4, true, 6, 7, true, 9);
   1017   EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
   1018 
   1019   const char* const str = "8";
   1020   // VC++ 2010's implementation of tuple of C++0x is deficient, requiring
   1021   // an explicit type cast of NULL to be used.
   1022   ::std::tr1::tuple<bool, char, short, testing::internal::Int32,  // NOLINT
   1023       testing::internal::Int64, float, double, const char*, void*, string>
   1024       t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str,
   1025           ImplicitCast_<void*>(NULL), "10");
   1026   EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
   1027             " pointing to \"8\", NULL, \"10\")",
   1028             Print(t10));
   1029 }
   1030 
   1031 // Nested tuples.
   1032 TEST(PrintTr1TupleTest, NestedTuple) {
   1033   ::std::tr1::tuple< ::std::tr1::tuple<int, bool>, char> nested(
   1034       ::std::tr1::make_tuple(5, true), 'a');
   1035   EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
   1036 }
   1037 
   1038 #endif  // GTEST_HAS_TR1_TUPLE
   1039 
   1040 #if GTEST_LANG_CXX11
   1041 // Tests printing ::std::tuples.
   1042 
   1043 // Tuples of various arities.
   1044 TEST(PrintStdTupleTest, VariousSizes) {
   1045   ::std::tuple<> t0;
   1046   EXPECT_EQ("()", Print(t0));
   1047 
   1048   ::std::tuple<int> t1(5);
   1049   EXPECT_EQ("(5)", Print(t1));
   1050 
   1051   ::std::tuple<char, bool> t2('a', true);
   1052   EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
   1053 
   1054   ::std::tuple<bool, int, int> t3(false, 2, 3);
   1055   EXPECT_EQ("(false, 2, 3)", Print(t3));
   1056 
   1057   ::std::tuple<bool, int, int, int> t4(false, 2, 3, 4);
   1058   EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
   1059 
   1060   ::std::tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
   1061   EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
   1062 
   1063   ::std::tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
   1064   EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
   1065 
   1066   ::std::tuple<bool, int, int, int, bool, int, int> t7(
   1067       false, 2, 3, 4, true, 6, 7);
   1068   EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
   1069 
   1070   ::std::tuple<bool, int, int, int, bool, int, int, bool> t8(
   1071       false, 2, 3, 4, true, 6, 7, true);
   1072   EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
   1073 
   1074   ::std::tuple<bool, int, int, int, bool, int, int, bool, int> t9(
   1075       false, 2, 3, 4, true, 6, 7, true, 9);
   1076   EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
   1077 
   1078   const char* const str = "8";
   1079   // VC++ 2010's implementation of tuple of C++0x is deficient, requiring
   1080   // an explicit type cast of NULL to be used.
   1081   ::std::tuple<bool, char, short, testing::internal::Int32,  // NOLINT
   1082       testing::internal::Int64, float, double, const char*, void*, string>
   1083       t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str,
   1084           ImplicitCast_<void*>(NULL), "10");
   1085   EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
   1086             " pointing to \"8\", NULL, \"10\")",
   1087             Print(t10));
   1088 }
   1089 
   1090 // Nested tuples.
   1091 TEST(PrintStdTupleTest, NestedTuple) {
   1092   ::std::tuple< ::std::tuple<int, bool>, char> nested(
   1093       ::std::make_tuple(5, true), 'a');
   1094   EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
   1095 }
   1096 
   1097 #endif  // GTEST_LANG_CXX11
   1098 
   1099 // Tests printing user-defined unprintable types.
   1100 
   1101 // Unprintable types in the global namespace.
   1102 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
   1103   EXPECT_EQ("1-byte object <00>",
   1104             Print(UnprintableTemplateInGlobal<char>()));
   1105 }
   1106 
   1107 // Unprintable types in a user namespace.
   1108 TEST(PrintUnprintableTypeTest, InUserNamespace) {
   1109   EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
   1110             Print(::foo::UnprintableInFoo()));
   1111 }
   1112 
   1113 // Unprintable types are that too big to be printed completely.
   1114 
   1115 struct Big {
   1116   Big() { memset(array, 0, sizeof(array)); }
   1117   char array[257];
   1118 };
   1119 
   1120 TEST(PrintUnpritableTypeTest, BigObject) {
   1121   EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
   1122             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
   1123             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
   1124             "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
   1125             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
   1126             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
   1127             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
   1128             Print(Big()));
   1129 }
   1130 
   1131 // Tests printing user-defined streamable types.
   1132 
   1133 // Streamable types in the global namespace.
   1134 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
   1135   StreamableInGlobal x;
   1136   EXPECT_EQ("StreamableInGlobal", Print(x));
   1137   EXPECT_EQ("StreamableInGlobal*", Print(&x));
   1138 }
   1139 
   1140 // Printable template types in a user namespace.
   1141 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
   1142   EXPECT_EQ("StreamableTemplateInFoo: 0",
   1143             Print(::foo::StreamableTemplateInFoo<int>()));
   1144 }
   1145 
   1146 // Tests printing user-defined types that have a PrintTo() function.
   1147 TEST(PrintPrintableTypeTest, InUserNamespace) {
   1148   EXPECT_EQ("PrintableViaPrintTo: 0",
   1149             Print(::foo::PrintableViaPrintTo()));
   1150 }
   1151 
   1152 // Tests printing a pointer to a user-defined type that has a <<
   1153 // operator for its pointer.
   1154 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
   1155   ::foo::PointerPrintable x;
   1156   EXPECT_EQ("PointerPrintable*", Print(&x));
   1157 }
   1158 
   1159 // Tests printing user-defined class template that have a PrintTo() function.
   1160 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
   1161   EXPECT_EQ("PrintableViaPrintToTemplate: 5",
   1162             Print(::foo::PrintableViaPrintToTemplate<int>(5)));
   1163 }
   1164 
   1165 #if GTEST_HAS_PROTOBUF_
   1166 
   1167 // Tests printing a protocol message.
   1168 TEST(PrintProtocolMessageTest, PrintsShortDebugString) {
   1169   testing::internal::TestMessage msg;
   1170   msg.set_member("yes");
   1171   EXPECT_EQ("<member:\"yes\">", Print(msg));
   1172 }
   1173 
   1174 // Tests printing a short proto2 message.
   1175 TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) {
   1176   testing::internal::FooMessage msg;
   1177   msg.set_int_field(2);
   1178   msg.set_string_field("hello");
   1179   EXPECT_PRED2(RE::FullMatch, Print(msg),
   1180                "<int_field:\\s*2\\s+string_field:\\s*\"hello\">");
   1181 }
   1182 
   1183 // Tests printing a long proto2 message.
   1184 TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) {
   1185   testing::internal::FooMessage msg;
   1186   msg.set_int_field(2);
   1187   msg.set_string_field("hello");
   1188   msg.add_names("peter");
   1189   msg.add_names("paul");
   1190   msg.add_names("mary");
   1191   EXPECT_PRED2(RE::FullMatch, Print(msg),
   1192                "<\n"
   1193                "int_field:\\s*2\n"
   1194                "string_field:\\s*\"hello\"\n"
   1195                "names:\\s*\"peter\"\n"
   1196                "names:\\s*\"paul\"\n"
   1197                "names:\\s*\"mary\"\n"
   1198                ">");
   1199 }
   1200 
   1201 #endif  // GTEST_HAS_PROTOBUF_
   1202 
   1203 // Tests that the universal printer prints both the address and the
   1204 // value of a reference.
   1205 TEST(PrintReferenceTest, PrintsAddressAndValue) {
   1206   int n = 5;
   1207   EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
   1208 
   1209   int a[2][3] = {
   1210     { 0, 1, 2 },
   1211     { 3, 4, 5 }
   1212   };
   1213   EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
   1214             PrintByRef(a));
   1215 
   1216   const ::foo::UnprintableInFoo x;
   1217   EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
   1218             "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
   1219             PrintByRef(x));
   1220 }
   1221 
   1222 // Tests that the universal printer prints a function pointer passed by
   1223 // reference.
   1224 TEST(PrintReferenceTest, HandlesFunctionPointer) {
   1225   void (*fp)(int n) = &MyFunction;
   1226   const string fp_pointer_string =
   1227       PrintPointer(reinterpret_cast<const void*>(&fp));
   1228   // We cannot directly cast &MyFunction to const void* because the
   1229   // standard disallows casting between pointers to functions and
   1230   // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
   1231   // this limitation.
   1232   const string fp_string = PrintPointer(reinterpret_cast<const void*>(
   1233       reinterpret_cast<internal::BiggestInt>(fp)));
   1234   EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
   1235             PrintByRef(fp));
   1236 }
   1237 
   1238 // Tests that the universal printer prints a member function pointer
   1239 // passed by reference.
   1240 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
   1241   int (Foo::*p)(char ch) = &Foo::MyMethod;
   1242   EXPECT_TRUE(HasPrefix(
   1243       PrintByRef(p),
   1244       "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
   1245           Print(sizeof(p)) + "-byte object "));
   1246 
   1247   char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
   1248   EXPECT_TRUE(HasPrefix(
   1249       PrintByRef(p2),
   1250       "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
   1251           Print(sizeof(p2)) + "-byte object "));
   1252 }
   1253 
   1254 // Tests that the universal printer prints a member variable pointer
   1255 // passed by reference.
   1256 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
   1257   int (Foo::*p) = &Foo::value;  // NOLINT
   1258   EXPECT_TRUE(HasPrefix(
   1259       PrintByRef(p),
   1260       "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
   1261 }
   1262 
   1263 // Tests that FormatForComparisonFailureMessage(), which is used to print
   1264 // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion
   1265 // fails, formats the operand in the desired way.
   1266 
   1267 // scalar
   1268 TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
   1269   EXPECT_STREQ("123",
   1270                FormatForComparisonFailureMessage(123, 124).c_str());
   1271 }
   1272 
   1273 // non-char pointer
   1274 TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
   1275   int n = 0;
   1276   EXPECT_EQ(PrintPointer(&n),
   1277             FormatForComparisonFailureMessage(&n, &n).c_str());
   1278 }
   1279 
   1280 // non-char array
   1281 TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
   1282   // In expression 'array == x', 'array' is compared by pointer.
   1283   // Therefore we want to print an array operand as a pointer.
   1284   int n[] = { 1, 2, 3 };
   1285   EXPECT_EQ(PrintPointer(n),
   1286             FormatForComparisonFailureMessage(n, n).c_str());
   1287 }
   1288 
   1289 // Tests formatting a char pointer when it's compared with another pointer.
   1290 // In this case we want to print it as a raw pointer, as the comparision is by
   1291 // pointer.
   1292 
   1293 // char pointer vs pointer
   1294 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
   1295   // In expression 'p == x', where 'p' and 'x' are (const or not) char
   1296   // pointers, the operands are compared by pointer.  Therefore we
   1297   // want to print 'p' as a pointer instead of a C string (we don't
   1298   // even know if it's supposed to point to a valid C string).
   1299 
   1300   // const char*
   1301   const char* s = "hello";
   1302   EXPECT_EQ(PrintPointer(s),
   1303             FormatForComparisonFailureMessage(s, s).c_str());
   1304 
   1305   // char*
   1306   char ch = 'a';
   1307   EXPECT_EQ(PrintPointer(&ch),
   1308             FormatForComparisonFailureMessage(&ch, &ch).c_str());
   1309 }
   1310 
   1311 // wchar_t pointer vs pointer
   1312 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
   1313   // In expression 'p == x', where 'p' and 'x' are (const or not) char
   1314   // pointers, the operands are compared by pointer.  Therefore we
   1315   // want to print 'p' as a pointer instead of a wide C string (we don't
   1316   // even know if it's supposed to point to a valid wide C string).
   1317 
   1318   // const wchar_t*
   1319   const wchar_t* s = L"hello";
   1320   EXPECT_EQ(PrintPointer(s),
   1321             FormatForComparisonFailureMessage(s, s).c_str());
   1322 
   1323   // wchar_t*
   1324   wchar_t ch = L'a';
   1325   EXPECT_EQ(PrintPointer(&ch),
   1326             FormatForComparisonFailureMessage(&ch, &ch).c_str());
   1327 }
   1328 
   1329 // Tests formatting a char pointer when it's compared to a string object.
   1330 // In this case we want to print the char pointer as a C string.
   1331 
   1332 #if GTEST_HAS_GLOBAL_STRING
   1333 // char pointer vs ::string
   1334 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) {
   1335   const char* s = "hello \"world";
   1336   EXPECT_STREQ("\"hello \\\"world\"",  // The string content should be escaped.
   1337                FormatForComparisonFailureMessage(s, ::string()).c_str());
   1338 
   1339   // char*
   1340   char str[] = "hi\1";
   1341   char* p = str;
   1342   EXPECT_STREQ("\"hi\\x1\"",  // The string content should be escaped.
   1343                FormatForComparisonFailureMessage(p, ::string()).c_str());
   1344 }
   1345 #endif
   1346 
   1347 // char pointer vs std::string
   1348 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
   1349   const char* s = "hello \"world";
   1350   EXPECT_STREQ("\"hello \\\"world\"",  // The string content should be escaped.
   1351                FormatForComparisonFailureMessage(s, ::std::string()).c_str());
   1352 
   1353   // char*
   1354   char str[] = "hi\1";
   1355   char* p = str;
   1356   EXPECT_STREQ("\"hi\\x1\"",  // The string content should be escaped.
   1357                FormatForComparisonFailureMessage(p, ::std::string()).c_str());
   1358 }
   1359 
   1360 #if GTEST_HAS_GLOBAL_WSTRING
   1361 // wchar_t pointer vs ::wstring
   1362 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) {
   1363   const wchar_t* s = L"hi \"world";
   1364   EXPECT_STREQ("L\"hi \\\"world\"",  // The string content should be escaped.
   1365                FormatForComparisonFailureMessage(s, ::wstring()).c_str());
   1366 
   1367   // wchar_t*
   1368   wchar_t str[] = L"hi\1";
   1369   wchar_t* p = str;
   1370   EXPECT_STREQ("L\"hi\\x1\"",  // The string content should be escaped.
   1371                FormatForComparisonFailureMessage(p, ::wstring()).c_str());
   1372 }
   1373 #endif
   1374 
   1375 #if GTEST_HAS_STD_WSTRING
   1376 // wchar_t pointer vs std::wstring
   1377 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
   1378   const wchar_t* s = L"hi \"world";
   1379   EXPECT_STREQ("L\"hi \\\"world\"",  // The string content should be escaped.
   1380                FormatForComparisonFailureMessage(s, ::std::wstring()).c_str());
   1381 
   1382   // wchar_t*
   1383   wchar_t str[] = L"hi\1";
   1384   wchar_t* p = str;
   1385   EXPECT_STREQ("L\"hi\\x1\"",  // The string content should be escaped.
   1386                FormatForComparisonFailureMessage(p, ::std::wstring()).c_str());
   1387 }
   1388 #endif
   1389 
   1390 // Tests formatting a char array when it's compared with a pointer or array.
   1391 // In this case we want to print the array as a row pointer, as the comparison
   1392 // is by pointer.
   1393 
   1394 // char array vs pointer
   1395 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
   1396   char str[] = "hi \"world\"";
   1397   char* p = NULL;
   1398   EXPECT_EQ(PrintPointer(str),
   1399             FormatForComparisonFailureMessage(str, p).c_str());
   1400 }
   1401 
   1402 // char array vs char array
   1403 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
   1404   const char str[] = "hi \"world\"";
   1405   EXPECT_EQ(PrintPointer(str),
   1406             FormatForComparisonFailureMessage(str, str).c_str());
   1407 }
   1408 
   1409 // wchar_t array vs pointer
   1410 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
   1411   wchar_t str[] = L"hi \"world\"";
   1412   wchar_t* p = NULL;
   1413   EXPECT_EQ(PrintPointer(str),
   1414             FormatForComparisonFailureMessage(str, p).c_str());
   1415 }
   1416 
   1417 // wchar_t array vs wchar_t array
   1418 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
   1419   const wchar_t str[] = L"hi \"world\"";
   1420   EXPECT_EQ(PrintPointer(str),
   1421             FormatForComparisonFailureMessage(str, str).c_str());
   1422 }
   1423 
   1424 // Tests formatting a char array when it's compared with a string object.
   1425 // In this case we want to print the array as a C string.
   1426 
   1427 #if GTEST_HAS_GLOBAL_STRING
   1428 // char array vs string
   1429 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) {
   1430   const char str[] = "hi \"w\0rld\"";
   1431   EXPECT_STREQ("\"hi \\\"w\"",  // The content should be escaped.
   1432                                 // Embedded NUL terminates the string.
   1433                FormatForComparisonFailureMessage(str, ::string()).c_str());
   1434 }
   1435 #endif
   1436 
   1437 // char array vs std::string
   1438 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
   1439   const char str[] = "hi \"world\"";
   1440   EXPECT_STREQ("\"hi \\\"world\\\"\"",  // The content should be escaped.
   1441                FormatForComparisonFailureMessage(str, ::std::string()).c_str());
   1442 }
   1443 
   1444 #if GTEST_HAS_GLOBAL_WSTRING
   1445 // wchar_t array vs wstring
   1446 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) {
   1447   const wchar_t str[] = L"hi \"world\"";
   1448   EXPECT_STREQ("L\"hi \\\"world\\\"\"",  // The content should be escaped.
   1449                FormatForComparisonFailureMessage(str, ::wstring()).c_str());
   1450 }
   1451 #endif
   1452 
   1453 #if GTEST_HAS_STD_WSTRING
   1454 // wchar_t array vs std::wstring
   1455 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
   1456   const wchar_t str[] = L"hi \"w\0rld\"";
   1457   EXPECT_STREQ(
   1458       "L\"hi \\\"w\"",  // The content should be escaped.
   1459                         // Embedded NUL terminates the string.
   1460       FormatForComparisonFailureMessage(str, ::std::wstring()).c_str());
   1461 }
   1462 #endif
   1463 
   1464 // Useful for testing PrintToString().  We cannot use EXPECT_EQ()
   1465 // there as its implementation uses PrintToString().  The caller must
   1466 // ensure that 'value' has no side effect.
   1467 #define EXPECT_PRINT_TO_STRING_(value, expected_string)         \
   1468   EXPECT_TRUE(PrintToString(value) == (expected_string))        \
   1469       << " where " #value " prints as " << (PrintToString(value))
   1470 
   1471 TEST(PrintToStringTest, WorksForScalar) {
   1472   EXPECT_PRINT_TO_STRING_(123, "123");
   1473 }
   1474 
   1475 TEST(PrintToStringTest, WorksForPointerToConstChar) {
   1476   const char* p = "hello";
   1477   EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
   1478 }
   1479 
   1480 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
   1481   char s[] = "hello";
   1482   char* p = s;
   1483   EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
   1484 }
   1485 
   1486 TEST(PrintToStringTest, EscapesForPointerToConstChar) {
   1487   const char* p = "hello\n";
   1488   EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\"");
   1489 }
   1490 
   1491 TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
   1492   char s[] = "hello\1";
   1493   char* p = s;
   1494   EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\"");
   1495 }
   1496 
   1497 TEST(PrintToStringTest, WorksForArray) {
   1498   int n[3] = { 1, 2, 3 };
   1499   EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
   1500 }
   1501 
   1502 TEST(PrintToStringTest, WorksForCharArray) {
   1503   char s[] = "hello";
   1504   EXPECT_PRINT_TO_STRING_(s, "\"hello\"");
   1505 }
   1506 
   1507 TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
   1508   const char str_with_nul[] = "hello\0 world";
   1509   EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\"");
   1510 
   1511   char mutable_str_with_nul[] = "hello\0 world";
   1512   EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\"");
   1513 }
   1514 
   1515 #undef EXPECT_PRINT_TO_STRING_
   1516 
   1517 TEST(UniversalTersePrintTest, WorksForNonReference) {
   1518   ::std::stringstream ss;
   1519   UniversalTersePrint(123, &ss);
   1520   EXPECT_EQ("123", ss.str());
   1521 }
   1522 
   1523 TEST(UniversalTersePrintTest, WorksForReference) {
   1524   const int& n = 123;
   1525   ::std::stringstream ss;
   1526   UniversalTersePrint(n, &ss);
   1527   EXPECT_EQ("123", ss.str());
   1528 }
   1529 
   1530 TEST(UniversalTersePrintTest, WorksForCString) {
   1531   const char* s1 = "abc";
   1532   ::std::stringstream ss1;
   1533   UniversalTersePrint(s1, &ss1);
   1534   EXPECT_EQ("\"abc\"", ss1.str());
   1535 
   1536   char* s2 = const_cast<char*>(s1);
   1537   ::std::stringstream ss2;
   1538   UniversalTersePrint(s2, &ss2);
   1539   EXPECT_EQ("\"abc\"", ss2.str());
   1540 
   1541   const char* s3 = NULL;
   1542   ::std::stringstream ss3;
   1543   UniversalTersePrint(s3, &ss3);
   1544   EXPECT_EQ("NULL", ss3.str());
   1545 }
   1546 
   1547 TEST(UniversalPrintTest, WorksForNonReference) {
   1548   ::std::stringstream ss;
   1549   UniversalPrint(123, &ss);
   1550   EXPECT_EQ("123", ss.str());
   1551 }
   1552 
   1553 TEST(UniversalPrintTest, WorksForReference) {
   1554   const int& n = 123;
   1555   ::std::stringstream ss;
   1556   UniversalPrint(n, &ss);
   1557   EXPECT_EQ("123", ss.str());
   1558 }
   1559 
   1560 TEST(UniversalPrintTest, WorksForCString) {
   1561   const char* s1 = "abc";
   1562   ::std::stringstream ss1;
   1563   UniversalPrint(s1, &ss1);
   1564   EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str()));
   1565 
   1566   char* s2 = const_cast<char*>(s1);
   1567   ::std::stringstream ss2;
   1568   UniversalPrint(s2, &ss2);
   1569   EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str()));
   1570 
   1571   const char* s3 = NULL;
   1572   ::std::stringstream ss3;
   1573   UniversalPrint(s3, &ss3);
   1574   EXPECT_EQ("NULL", ss3.str());
   1575 }
   1576 
   1577 TEST(UniversalPrintTest, WorksForCharArray) {
   1578   const char str[] = "\"Line\0 1\"\nLine 2";
   1579   ::std::stringstream ss1;
   1580   UniversalPrint(str, &ss1);
   1581   EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
   1582 
   1583   const char mutable_str[] = "\"Line\0 1\"\nLine 2";
   1584   ::std::stringstream ss2;
   1585   UniversalPrint(mutable_str, &ss2);
   1586   EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
   1587 }
   1588 
   1589 #if GTEST_HAS_TR1_TUPLE
   1590 
   1591 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsEmptyTuple) {
   1592   Strings result = UniversalTersePrintTupleFieldsToStrings(
   1593       ::std::tr1::make_tuple());
   1594   EXPECT_EQ(0u, result.size());
   1595 }
   1596 
   1597 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsOneTuple) {
   1598   Strings result = UniversalTersePrintTupleFieldsToStrings(
   1599       ::std::tr1::make_tuple(1));
   1600   ASSERT_EQ(1u, result.size());
   1601   EXPECT_EQ("1", result[0]);
   1602 }
   1603 
   1604 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTwoTuple) {
   1605   Strings result = UniversalTersePrintTupleFieldsToStrings(
   1606       ::std::tr1::make_tuple(1, 'a'));
   1607   ASSERT_EQ(2u, result.size());
   1608   EXPECT_EQ("1", result[0]);
   1609   EXPECT_EQ("'a' (97, 0x61)", result[1]);
   1610 }
   1611 
   1612 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTersely) {
   1613   const int n = 1;
   1614   Strings result = UniversalTersePrintTupleFieldsToStrings(
   1615       ::std::tr1::tuple<const int&, const char*>(n, "a"));
   1616   ASSERT_EQ(2u, result.size());
   1617   EXPECT_EQ("1", result[0]);
   1618   EXPECT_EQ("\"a\"", result[1]);
   1619 }
   1620 
   1621 #endif  // GTEST_HAS_TR1_TUPLE
   1622 
   1623 #if GTEST_HAS_STD_TUPLE_
   1624 
   1625 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
   1626   Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
   1627   EXPECT_EQ(0u, result.size());
   1628 }
   1629 
   1630 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
   1631   Strings result = UniversalTersePrintTupleFieldsToStrings(
   1632       ::std::make_tuple(1));
   1633   ASSERT_EQ(1u, result.size());
   1634   EXPECT_EQ("1", result[0]);
   1635 }
   1636 
   1637 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
   1638   Strings result = UniversalTersePrintTupleFieldsToStrings(
   1639       ::std::make_tuple(1, 'a'));
   1640   ASSERT_EQ(2u, result.size());
   1641   EXPECT_EQ("1", result[0]);
   1642   EXPECT_EQ("'a' (97, 0x61)", result[1]);
   1643 }
   1644 
   1645 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
   1646   const int n = 1;
   1647   Strings result = UniversalTersePrintTupleFieldsToStrings(
   1648       ::std::tuple<const int&, const char*>(n, "a"));
   1649   ASSERT_EQ(2u, result.size());
   1650   EXPECT_EQ("1", result[0]);
   1651   EXPECT_EQ("\"a\"", result[1]);
   1652 }
   1653 
   1654 #endif  // GTEST_HAS_STD_TUPLE_
   1655 
   1656 }  // namespace gtest_printers_test
   1657 }  // namespace testing
   1658