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  private:
    129   char xy_[8];
    130   double z_;
    131 };
    132 
    133 // A user-defined printable type in a user-chosen namespace.
    134 struct PrintableViaPrintTo {
    135   PrintableViaPrintTo() : value() {}
    136   int value;
    137 };
    138 
    139 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
    140   *os << "PrintableViaPrintTo: " << x.value;
    141 }
    142 
    143 // A type with a user-defined << for printing its pointer.
    144 struct PointerPrintable {
    145 };
    146 
    147 ::std::ostream& operator<<(::std::ostream& os,
    148                            const PointerPrintable* /* x */) {
    149   return os << "PointerPrintable*";
    150 }
    151 
    152 // A user-defined printable class template in a user-chosen namespace.
    153 template <typename T>
    154 class PrintableViaPrintToTemplate {
    155  public:
    156   explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
    157 
    158   const T& value() const { return value_; }
    159  private:
    160   T value_;
    161 };
    162 
    163 template <typename T>
    164 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
    165   *os << "PrintableViaPrintToTemplate: " << x.value();
    166 }
    167 
    168 // A user-defined streamable class template in a user namespace.
    169 template <typename T>
    170 class StreamableTemplateInFoo {
    171  public:
    172   StreamableTemplateInFoo() : value_() {}
    173 
    174   const T& value() const { return value_; }
    175  private:
    176   T value_;
    177 };
    178 
    179 template <typename T>
    180 inline ::std::ostream& operator<<(::std::ostream& os,
    181                                   const StreamableTemplateInFoo<T>& x) {
    182   return os << "StreamableTemplateInFoo: " << x.value();
    183 }
    184 
    185 }  // namespace foo
    186 
    187 namespace testing {
    188 namespace gtest_printers_test {
    189 
    190 using ::std::deque;
    191 using ::std::list;
    192 using ::std::make_pair;
    193 using ::std::map;
    194 using ::std::multimap;
    195 using ::std::multiset;
    196 using ::std::pair;
    197 using ::std::set;
    198 using ::std::vector;
    199 using ::testing::PrintToString;
    200 using ::testing::internal::NativeArray;
    201 using ::testing::internal::RE;
    202 using ::testing::internal::Strings;
    203 using ::testing::internal::UniversalTersePrint;
    204 using ::testing::internal::UniversalPrint;
    205 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
    206 using ::testing::internal::UniversalPrinter;
    207 using ::testing::internal::kReference;
    208 using ::testing::internal::string;
    209 
    210 #if GTEST_HAS_TR1_TUPLE
    211 using ::std::tr1::make_tuple;
    212 using ::std::tr1::tuple;
    213 #endif
    214 
    215 #if _MSC_VER
    216 // MSVC defines the following classes in the ::stdext namespace while
    217 // gcc defines them in the :: namespace.  Note that they are not part
    218 // of the C++ standard.
    219 using ::stdext::hash_map;
    220 using ::stdext::hash_set;
    221 using ::stdext::hash_multimap;
    222 using ::stdext::hash_multiset;
    223 #endif
    224 
    225 // Prints a value to a string using the universal value printer.  This
    226 // is a helper for testing UniversalPrinter<T>::Print() for various types.
    227 template <typename T>
    228 string Print(const T& value) {
    229   ::std::stringstream ss;
    230   UniversalPrinter<T>::Print(value, &ss);
    231   return ss.str();
    232 }
    233 
    234 // Prints a value passed by reference to a string, using the universal
    235 // value printer.  This is a helper for testing
    236 // UniversalPrinter<T&>::Print() for various types.
    237 template <typename T>
    238 string PrintByRef(const T& value) {
    239   ::std::stringstream ss;
    240   UniversalPrinter<T&>::Print(value, &ss);
    241   return ss.str();
    242 }
    243 
    244 // Tests printing various enum types.
    245 
    246 TEST(PrintEnumTest, AnonymousEnum) {
    247   EXPECT_EQ("-1", Print(kAE1));
    248   EXPECT_EQ("1", Print(kAE2));
    249 }
    250 
    251 TEST(PrintEnumTest, EnumWithoutPrinter) {
    252   EXPECT_EQ("-2", Print(kEWP1));
    253   EXPECT_EQ("42", Print(kEWP2));
    254 }
    255 
    256 TEST(PrintEnumTest, EnumWithStreaming) {
    257   EXPECT_EQ("kEWS1", Print(kEWS1));
    258   EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
    259 }
    260 
    261 TEST(PrintEnumTest, EnumWithPrintTo) {
    262   EXPECT_EQ("kEWPT1", Print(kEWPT1));
    263   EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
    264 }
    265 
    266 // Tests printing a class implicitly convertible to BiggestInt.
    267 
    268 TEST(PrintClassTest, BiggestIntConvertible) {
    269   EXPECT_EQ("42", Print(BiggestIntConvertible()));
    270 }
    271 
    272 // Tests printing various char types.
    273 
    274 // char.
    275 TEST(PrintCharTest, PlainChar) {
    276   EXPECT_EQ("'\\0'", Print('\0'));
    277   EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
    278   EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
    279   EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
    280   EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
    281   EXPECT_EQ("'\\a' (7)", Print('\a'));
    282   EXPECT_EQ("'\\b' (8)", Print('\b'));
    283   EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
    284   EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
    285   EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
    286   EXPECT_EQ("'\\t' (9)", Print('\t'));
    287   EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
    288   EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
    289   EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
    290   EXPECT_EQ("' ' (32, 0x20)", Print(' '));
    291   EXPECT_EQ("'a' (97, 0x61)", Print('a'));
    292 }
    293 
    294 // signed char.
    295 TEST(PrintCharTest, SignedChar) {
    296   EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
    297   EXPECT_EQ("'\\xCE' (-50)",
    298             Print(static_cast<signed char>(-50)));
    299 }
    300 
    301 // unsigned char.
    302 TEST(PrintCharTest, UnsignedChar) {
    303   EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
    304   EXPECT_EQ("'b' (98, 0x62)",
    305             Print(static_cast<unsigned char>('b')));
    306 }
    307 
    308 // Tests printing other simple, built-in types.
    309 
    310 // bool.
    311 TEST(PrintBuiltInTypeTest, Bool) {
    312   EXPECT_EQ("false", Print(false));
    313   EXPECT_EQ("true", Print(true));
    314 }
    315 
    316 // wchar_t.
    317 TEST(PrintBuiltInTypeTest, Wchar_t) {
    318   EXPECT_EQ("L'\\0'", Print(L'\0'));
    319   EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
    320   EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
    321   EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
    322   EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
    323   EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
    324   EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
    325   EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
    326   EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
    327   EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
    328   EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
    329   EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
    330   EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
    331   EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
    332   EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
    333   EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
    334   EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
    335   EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
    336 }
    337 
    338 // Test that Int64 provides more storage than wchar_t.
    339 TEST(PrintTypeSizeTest, Wchar_t) {
    340   EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
    341 }
    342 
    343 // Various integer types.
    344 TEST(PrintBuiltInTypeTest, Integer) {
    345   EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255)));  // uint8
    346   EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128)));  // int8
    347   EXPECT_EQ("65535", Print(USHRT_MAX));  // uint16
    348   EXPECT_EQ("-32768", Print(SHRT_MIN));  // int16
    349   EXPECT_EQ("4294967295", Print(UINT_MAX));  // uint32
    350   EXPECT_EQ("-2147483648", Print(INT_MIN));  // int32
    351   EXPECT_EQ("18446744073709551615",
    352             Print(static_cast<testing::internal::UInt64>(-1)));  // uint64
    353   EXPECT_EQ("-9223372036854775808",
    354             Print(static_cast<testing::internal::Int64>(1) << 63));  // int64
    355 }
    356 
    357 // Size types.
    358 TEST(PrintBuiltInTypeTest, Size_t) {
    359   EXPECT_EQ("1", Print(sizeof('a')));  // size_t.
    360 #if !GTEST_OS_WINDOWS
    361   // Windows has no ssize_t type.
    362   EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2)));  // ssize_t.
    363 #endif  // !GTEST_OS_WINDOWS
    364 }
    365 
    366 // Floating-points.
    367 TEST(PrintBuiltInTypeTest, FloatingPoints) {
    368   EXPECT_EQ("1.5", Print(1.5f));   // float
    369   EXPECT_EQ("-2.5", Print(-2.5));  // double
    370 }
    371 
    372 // Since ::std::stringstream::operator<<(const void *) formats the pointer
    373 // output differently with different compilers, we have to create the expected
    374 // output first and use it as our expectation.
    375 static string PrintPointer(const void *p) {
    376   ::std::stringstream expected_result_stream;
    377   expected_result_stream << p;
    378   return expected_result_stream.str();
    379 }
    380 
    381 // Tests printing C strings.
    382 
    383 // const char*.
    384 TEST(PrintCStringTest, Const) {
    385   const char* p = "World";
    386   EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
    387 }
    388 
    389 // char*.
    390 TEST(PrintCStringTest, NonConst) {
    391   char p[] = "Hi";
    392   EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
    393             Print(static_cast<char*>(p)));
    394 }
    395 
    396 // NULL C string.
    397 TEST(PrintCStringTest, Null) {
    398   const char* p = NULL;
    399   EXPECT_EQ("NULL", Print(p));
    400 }
    401 
    402 // Tests that C strings are escaped properly.
    403 TEST(PrintCStringTest, EscapesProperly) {
    404   const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
    405   EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
    406             "\\n\\r\\t\\v\\x7F\\xFF a\"",
    407             Print(p));
    408 }
    409 
    410 
    411 
    412 // MSVC compiler can be configured to define whar_t as a typedef
    413 // of unsigned short. Defining an overload for const wchar_t* in that case
    414 // would cause pointers to unsigned shorts be printed as wide strings,
    415 // possibly accessing more memory than intended and causing invalid
    416 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
    417 // wchar_t is implemented as a native type.
    418 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
    419 
    420 // const wchar_t*.
    421 TEST(PrintWideCStringTest, Const) {
    422   const wchar_t* p = L"World";
    423   EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
    424 }
    425 
    426 // wchar_t*.
    427 TEST(PrintWideCStringTest, NonConst) {
    428   wchar_t p[] = L"Hi";
    429   EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
    430             Print(static_cast<wchar_t*>(p)));
    431 }
    432 
    433 // NULL wide C string.
    434 TEST(PrintWideCStringTest, Null) {
    435   const wchar_t* p = NULL;
    436   EXPECT_EQ("NULL", Print(p));
    437 }
    438 
    439 // Tests that wide C strings are escaped properly.
    440 TEST(PrintWideCStringTest, EscapesProperly) {
    441   const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r',
    442                        '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
    443   EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f"
    444             "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
    445             Print(static_cast<const wchar_t*>(s)));
    446 }
    447 #endif  // native wchar_t
    448 
    449 // Tests printing pointers to other char types.
    450 
    451 // signed char*.
    452 TEST(PrintCharPointerTest, SignedChar) {
    453   signed char* p = reinterpret_cast<signed char*>(0x1234);
    454   EXPECT_EQ(PrintPointer(p), Print(p));
    455   p = NULL;
    456   EXPECT_EQ("NULL", Print(p));
    457 }
    458 
    459 // const signed char*.
    460 TEST(PrintCharPointerTest, ConstSignedChar) {
    461   signed char* p = reinterpret_cast<signed char*>(0x1234);
    462   EXPECT_EQ(PrintPointer(p), Print(p));
    463   p = NULL;
    464   EXPECT_EQ("NULL", Print(p));
    465 }
    466 
    467 // unsigned char*.
    468 TEST(PrintCharPointerTest, UnsignedChar) {
    469   unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
    470   EXPECT_EQ(PrintPointer(p), Print(p));
    471   p = NULL;
    472   EXPECT_EQ("NULL", Print(p));
    473 }
    474 
    475 // const unsigned char*.
    476 TEST(PrintCharPointerTest, ConstUnsignedChar) {
    477   const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
    478   EXPECT_EQ(PrintPointer(p), Print(p));
    479   p = NULL;
    480   EXPECT_EQ("NULL", Print(p));
    481 }
    482 
    483 // Tests printing pointers to simple, built-in types.
    484 
    485 // bool*.
    486 TEST(PrintPointerToBuiltInTypeTest, Bool) {
    487   bool* p = reinterpret_cast<bool*>(0xABCD);
    488   EXPECT_EQ(PrintPointer(p), Print(p));
    489   p = NULL;
    490   EXPECT_EQ("NULL", Print(p));
    491 }
    492 
    493 // void*.
    494 TEST(PrintPointerToBuiltInTypeTest, Void) {
    495   void* p = reinterpret_cast<void*>(0xABCD);
    496   EXPECT_EQ(PrintPointer(p), Print(p));
    497   p = NULL;
    498   EXPECT_EQ("NULL", Print(p));
    499 }
    500 
    501 // const void*.
    502 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
    503   const void* p = reinterpret_cast<const void*>(0xABCD);
    504   EXPECT_EQ(PrintPointer(p), Print(p));
    505   p = NULL;
    506   EXPECT_EQ("NULL", Print(p));
    507 }
    508 
    509 // Tests printing pointers to pointers.
    510 TEST(PrintPointerToPointerTest, IntPointerPointer) {
    511   int** p = reinterpret_cast<int**>(0xABCD);
    512   EXPECT_EQ(PrintPointer(p), Print(p));
    513   p = NULL;
    514   EXPECT_EQ("NULL", Print(p));
    515 }
    516 
    517 // Tests printing (non-member) function pointers.
    518 
    519 void MyFunction(int /* n */) {}
    520 
    521 TEST(PrintPointerTest, NonMemberFunctionPointer) {
    522   // We cannot directly cast &MyFunction to const void* because the
    523   // standard disallows casting between pointers to functions and
    524   // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
    525   // this limitation.
    526   EXPECT_EQ(
    527       PrintPointer(reinterpret_cast<const void*>(
    528           reinterpret_cast<internal::BiggestInt>(&MyFunction))),
    529       Print(&MyFunction));
    530   int (*p)(bool) = NULL;  // NOLINT
    531   EXPECT_EQ("NULL", Print(p));
    532 }
    533 
    534 // An assertion predicate determining whether a one string is a prefix for
    535 // another.
    536 template <typename StringType>
    537 AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
    538   if (str.find(prefix, 0) == 0)
    539     return AssertionSuccess();
    540 
    541   const bool is_wide_string = sizeof(prefix[0]) > 1;
    542   const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
    543   return AssertionFailure()
    544       << begin_string_quote << prefix << "\" is not a prefix of "
    545       << begin_string_quote << str << "\"\n";
    546 }
    547 
    548 // Tests printing member variable pointers.  Although they are called
    549 // pointers, they don't point to a location in the address space.
    550 // Their representation is implementation-defined.  Thus they will be
    551 // printed as raw bytes.
    552 
    553 struct Foo {
    554  public:
    555   virtual ~Foo() {}
    556   int MyMethod(char x) { return x + 1; }
    557   virtual char MyVirtualMethod(int /* n */) { return 'a'; }
    558 
    559   int value;
    560 };
    561 
    562 TEST(PrintPointerTest, MemberVariablePointer) {
    563   EXPECT_TRUE(HasPrefix(Print(&Foo::value),
    564                         Print(sizeof(&Foo::value)) + "-byte object "));
    565   int (Foo::*p) = NULL;  // NOLINT
    566   EXPECT_TRUE(HasPrefix(Print(p),
    567                         Print(sizeof(p)) + "-byte object "));
    568 }
    569 
    570 // Tests printing member function pointers.  Although they are called
    571 // pointers, they don't point to a location in the address space.
    572 // Their representation is implementation-defined.  Thus they will be
    573 // printed as raw bytes.
    574 TEST(PrintPointerTest, MemberFunctionPointer) {
    575   EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod),
    576                         Print(sizeof(&Foo::MyMethod)) + "-byte object "));
    577   EXPECT_TRUE(
    578       HasPrefix(Print(&Foo::MyVirtualMethod),
    579                 Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
    580   int (Foo::*p)(char) = NULL;  // NOLINT
    581   EXPECT_TRUE(HasPrefix(Print(p),
    582                         Print(sizeof(p)) + "-byte object "));
    583 }
    584 
    585 // Tests printing C arrays.
    586 
    587 // The difference between this and Print() is that it ensures that the
    588 // argument is a reference to an array.
    589 template <typename T, size_t N>
    590 string PrintArrayHelper(T (&a)[N]) {
    591   return Print(a);
    592 }
    593 
    594 // One-dimensional array.
    595 TEST(PrintArrayTest, OneDimensionalArray) {
    596   int a[5] = { 1, 2, 3, 4, 5 };
    597   EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
    598 }
    599 
    600 // Two-dimensional array.
    601 TEST(PrintArrayTest, TwoDimensionalArray) {
    602   int a[2][5] = {
    603     { 1, 2, 3, 4, 5 },
    604     { 6, 7, 8, 9, 0 }
    605   };
    606   EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
    607 }
    608 
    609 // Array of const elements.
    610 TEST(PrintArrayTest, ConstArray) {
    611   const bool a[1] = { false };
    612   EXPECT_EQ("{ false }", PrintArrayHelper(a));
    613 }
    614 
    615 // Char array.
    616 TEST(PrintArrayTest, CharArray) {
    617   // Array a contains '\0' in the middle and doesn't end with '\0'.
    618   char a[3] = { 'H', '\0', 'i' };
    619   EXPECT_EQ("\"H\\0i\"", PrintArrayHelper(a));
    620 }
    621 
    622 // Const char array.
    623 TEST(PrintArrayTest, ConstCharArray) {
    624   const char a[4] = "\0Hi";
    625   EXPECT_EQ("\"\\0Hi\\0\"", PrintArrayHelper(a));
    626 }
    627 
    628 // Array of objects.
    629 TEST(PrintArrayTest, ObjectArray) {
    630   string a[3] = { "Hi", "Hello", "Ni hao" };
    631   EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
    632 }
    633 
    634 // Array with many elements.
    635 TEST(PrintArrayTest, BigArray) {
    636   int a[100] = { 1, 2, 3 };
    637   EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
    638             PrintArrayHelper(a));
    639 }
    640 
    641 // Tests printing ::string and ::std::string.
    642 
    643 #if GTEST_HAS_GLOBAL_STRING
    644 // ::string.
    645 TEST(PrintStringTest, StringInGlobalNamespace) {
    646   const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
    647   const ::string str(s, sizeof(s));
    648   EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
    649             Print(str));
    650 }
    651 #endif  // GTEST_HAS_GLOBAL_STRING
    652 
    653 // ::std::string.
    654 TEST(PrintStringTest, StringInStdNamespace) {
    655   const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
    656   const ::std::string str(s, sizeof(s));
    657   EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
    658             Print(str));
    659 }
    660 
    661 TEST(PrintStringTest, StringAmbiguousHex) {
    662   // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
    663   // '\x6', '\x6B', or '\x6BA'.
    664 
    665   // a hex escaping sequence following by a decimal digit
    666   EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
    667   // a hex escaping sequence following by a hex digit (lower-case)
    668   EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
    669   // a hex escaping sequence following by a hex digit (upper-case)
    670   EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
    671   // a hex escaping sequence following by a non-xdigit
    672   EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
    673 }
    674 
    675 // Tests printing ::wstring and ::std::wstring.
    676 
    677 #if GTEST_HAS_GLOBAL_WSTRING
    678 // ::wstring.
    679 TEST(PrintWideStringTest, StringInGlobalNamespace) {
    680   const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
    681   const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
    682   EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
    683             "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
    684             Print(str));
    685 }
    686 #endif  // GTEST_HAS_GLOBAL_WSTRING
    687 
    688 #if GTEST_HAS_STD_WSTRING
    689 // ::std::wstring.
    690 TEST(PrintWideStringTest, StringInStdNamespace) {
    691   const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
    692   const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
    693   EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
    694             "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
    695             Print(str));
    696 }
    697 
    698 TEST(PrintWideStringTest, StringAmbiguousHex) {
    699   // same for wide strings.
    700   EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3")));
    701   EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
    702             Print(::std::wstring(L"mm\x6" L"bananas")));
    703   EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
    704             Print(::std::wstring(L"NOM\x6" L"BANANA")));
    705   EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
    706 }
    707 #endif  // GTEST_HAS_STD_WSTRING
    708 
    709 // Tests printing types that support generic streaming (i.e. streaming
    710 // to std::basic_ostream<Char, CharTraits> for any valid Char and
    711 // CharTraits types).
    712 
    713 // Tests printing a non-template type that supports generic streaming.
    714 
    715 class AllowsGenericStreaming {};
    716 
    717 template <typename Char, typename CharTraits>
    718 std::basic_ostream<Char, CharTraits>& operator<<(
    719     std::basic_ostream<Char, CharTraits>& os,
    720     const AllowsGenericStreaming& /* a */) {
    721   return os << "AllowsGenericStreaming";
    722 }
    723 
    724 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
    725   AllowsGenericStreaming a;
    726   EXPECT_EQ("AllowsGenericStreaming", Print(a));
    727 }
    728 
    729 // Tests printing a template type that supports generic streaming.
    730 
    731 template <typename T>
    732 class AllowsGenericStreamingTemplate {};
    733 
    734 template <typename Char, typename CharTraits, typename T>
    735 std::basic_ostream<Char, CharTraits>& operator<<(
    736     std::basic_ostream<Char, CharTraits>& os,
    737     const AllowsGenericStreamingTemplate<T>& /* a */) {
    738   return os << "AllowsGenericStreamingTemplate";
    739 }
    740 
    741 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
    742   AllowsGenericStreamingTemplate<int> a;
    743   EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
    744 }
    745 
    746 // Tests printing a type that supports generic streaming and can be
    747 // implicitly converted to another printable type.
    748 
    749 template <typename T>
    750 class AllowsGenericStreamingAndImplicitConversionTemplate {
    751  public:
    752   operator bool() const { return false; }
    753 };
    754 
    755 template <typename Char, typename CharTraits, typename T>
    756 std::basic_ostream<Char, CharTraits>& operator<<(
    757     std::basic_ostream<Char, CharTraits>& os,
    758     const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) {
    759   return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
    760 }
    761 
    762 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
    763   AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
    764   EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
    765 }
    766 
    767 #if GTEST_HAS_STRING_PIECE_
    768 
    769 // Tests printing StringPiece.
    770 
    771 TEST(PrintStringPieceTest, SimpleStringPiece) {
    772   const StringPiece sp = "Hello";
    773   EXPECT_EQ("\"Hello\"", Print(sp));
    774 }
    775 
    776 TEST(PrintStringPieceTest, UnprintableCharacters) {
    777   const char str[] = "NUL (\0) and \r\t";
    778   const StringPiece sp(str, sizeof(str) - 1);
    779   EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
    780 }
    781 
    782 #endif  // GTEST_HAS_STRING_PIECE_
    783 
    784 // Tests printing STL containers.
    785 
    786 TEST(PrintStlContainerTest, EmptyDeque) {
    787   deque<char> empty;
    788   EXPECT_EQ("{}", Print(empty));
    789 }
    790 
    791 TEST(PrintStlContainerTest, NonEmptyDeque) {
    792   deque<int> non_empty;
    793   non_empty.push_back(1);
    794   non_empty.push_back(3);
    795   EXPECT_EQ("{ 1, 3 }", Print(non_empty));
    796 }
    797 
    798 #if GTEST_HAS_HASH_MAP_
    799 
    800 TEST(PrintStlContainerTest, OneElementHashMap) {
    801   hash_map<int, char> map1;
    802   map1[1] = 'a';
    803   EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1));
    804 }
    805 
    806 TEST(PrintStlContainerTest, HashMultiMap) {
    807   hash_multimap<int, bool> map1;
    808   map1.insert(make_pair(5, true));
    809   map1.insert(make_pair(5, false));
    810 
    811   // Elements of hash_multimap can be printed in any order.
    812   const string result = Print(map1);
    813   EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
    814               result == "{ (5, false), (5, true) }")
    815                   << " where Print(map1) returns \"" << result << "\".";
    816 }
    817 
    818 #endif  // GTEST_HAS_HASH_MAP_
    819 
    820 #if GTEST_HAS_HASH_SET_
    821 
    822 TEST(PrintStlContainerTest, HashSet) {
    823   hash_set<string> set1;
    824   set1.insert("hello");
    825   EXPECT_EQ("{ \"hello\" }", Print(set1));
    826 }
    827 
    828 TEST(PrintStlContainerTest, HashMultiSet) {
    829   const int kSize = 5;
    830   int a[kSize] = { 1, 1, 2, 5, 1 };
    831   hash_multiset<int> set1(a, a + kSize);
    832 
    833   // Elements of hash_multiset can be printed in any order.
    834   const string result = Print(set1);
    835   const string expected_pattern = "{ d, d, d, d, d }";  // d means a digit.
    836 
    837   // Verifies the result matches the expected pattern; also extracts
    838   // the numbers in the result.
    839   ASSERT_EQ(expected_pattern.length(), result.length());
    840   std::vector<int> numbers;
    841   for (size_t i = 0; i != result.length(); i++) {
    842     if (expected_pattern[i] == 'd') {
    843       ASSERT_TRUE(isdigit(static_cast<unsigned char>(result[i])) != 0);
    844       numbers.push_back(result[i] - '0');
    845     } else {
    846       EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
    847                                                 << result;
    848     }
    849   }
    850 
    851   // Makes sure the result contains the right numbers.
    852   std::sort(numbers.begin(), numbers.end());
    853   std::sort(a, a + kSize);
    854   EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
    855 }
    856 
    857 #endif  // GTEST_HAS_HASH_SET_
    858 
    859 TEST(PrintStlContainerTest, List) {
    860   const char* a[] = {
    861     "hello",
    862     "world"
    863   };
    864   const list<string> strings(a, a + 2);
    865   EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
    866 }
    867 
    868 TEST(PrintStlContainerTest, Map) {
    869   map<int, bool> map1;
    870   map1[1] = true;
    871   map1[5] = false;
    872   map1[3] = true;
    873   EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
    874 }
    875 
    876 TEST(PrintStlContainerTest, MultiMap) {
    877   multimap<bool, int> map1;
    878   map1.insert(make_pair(true, 0));
    879   map1.insert(make_pair(true, 1));
    880   map1.insert(make_pair(false, 2));
    881   EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
    882 }
    883 
    884 TEST(PrintStlContainerTest, Set) {
    885   const unsigned int a[] = { 3, 0, 5 };
    886   set<unsigned int> set1(a, a + 3);
    887   EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
    888 }
    889 
    890 TEST(PrintStlContainerTest, MultiSet) {
    891   const int a[] = { 1, 1, 2, 5, 1 };
    892   multiset<int> set1(a, a + 5);
    893   EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
    894 }
    895 
    896 TEST(PrintStlContainerTest, Pair) {
    897   pair<const bool, int> p(true, 5);
    898   EXPECT_EQ("(true, 5)", Print(p));
    899 }
    900 
    901 TEST(PrintStlContainerTest, Vector) {
    902   vector<int> v;
    903   v.push_back(1);
    904   v.push_back(2);
    905   EXPECT_EQ("{ 1, 2 }", Print(v));
    906 }
    907 
    908 TEST(PrintStlContainerTest, LongSequence) {
    909   const int a[100] = { 1, 2, 3 };
    910   const vector<int> v(a, a + 100);
    911   EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
    912             "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
    913 }
    914 
    915 TEST(PrintStlContainerTest, NestedContainer) {
    916   const int a1[] = { 1, 2 };
    917   const int a2[] = { 3, 4, 5 };
    918   const list<int> l1(a1, a1 + 2);
    919   const list<int> l2(a2, a2 + 3);
    920 
    921   vector<list<int> > v;
    922   v.push_back(l1);
    923   v.push_back(l2);
    924   EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
    925 }
    926 
    927 TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
    928   const int a[3] = { 1, 2, 3 };
    929   NativeArray<int> b(a, 3, kReference);
    930   EXPECT_EQ("{ 1, 2, 3 }", Print(b));
    931 }
    932 
    933 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
    934   const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
    935   NativeArray<int[3]> b(a, 2, kReference);
    936   EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
    937 }
    938 
    939 // Tests that a class named iterator isn't treated as a container.
    940 
    941 struct iterator {
    942   char x;
    943 };
    944 
    945 TEST(PrintStlContainerTest, Iterator) {
    946   iterator it = {};
    947   EXPECT_EQ("1-byte object <00>", Print(it));
    948 }
    949 
    950 // Tests that a class named const_iterator isn't treated as a container.
    951 
    952 struct const_iterator {
    953   char x;
    954 };
    955 
    956 TEST(PrintStlContainerTest, ConstIterator) {
    957   const_iterator it = {};
    958   EXPECT_EQ("1-byte object <00>", Print(it));
    959 }
    960 
    961 #if GTEST_HAS_TR1_TUPLE
    962 // Tests printing tuples.
    963 
    964 // Tuples of various arities.
    965 TEST(PrintTupleTest, VariousSizes) {
    966   tuple<> t0;
    967   EXPECT_EQ("()", Print(t0));
    968 
    969   tuple<int> t1(5);
    970   EXPECT_EQ("(5)", Print(t1));
    971 
    972   tuple<char, bool> t2('a', true);
    973   EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
    974 
    975   tuple<bool, int, int> t3(false, 2, 3);
    976   EXPECT_EQ("(false, 2, 3)", Print(t3));
    977 
    978   tuple<bool, int, int, int> t4(false, 2, 3, 4);
    979   EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
    980 
    981   tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
    982   EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
    983 
    984   tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
    985   EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
    986 
    987   tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7);
    988   EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
    989 
    990   tuple<bool, int, int, int, bool, int, int, bool> t8(
    991       false, 2, 3, 4, true, 6, 7, true);
    992   EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
    993 
    994   tuple<bool, int, int, int, bool, int, int, bool, int> t9(
    995       false, 2, 3, 4, true, 6, 7, true, 9);
    996   EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
    997 
    998   const char* const str = "8";
    999   tuple<bool, char, short, testing::internal::Int32,  // NOLINT
   1000       testing::internal::Int64, float, double, const char*, void*, string>
   1001       t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, NULL, "10");
   1002   EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
   1003             " pointing to \"8\", NULL, \"10\")",
   1004             Print(t10));
   1005 }
   1006 
   1007 // Nested tuples.
   1008 TEST(PrintTupleTest, NestedTuple) {
   1009   tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a');
   1010   EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
   1011 }
   1012 
   1013 #endif  // GTEST_HAS_TR1_TUPLE
   1014 
   1015 // Tests printing user-defined unprintable types.
   1016 
   1017 // Unprintable types in the global namespace.
   1018 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
   1019   EXPECT_EQ("1-byte object <00>",
   1020             Print(UnprintableTemplateInGlobal<char>()));
   1021 }
   1022 
   1023 // Unprintable types in a user namespace.
   1024 TEST(PrintUnprintableTypeTest, InUserNamespace) {
   1025   EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
   1026             Print(::foo::UnprintableInFoo()));
   1027 }
   1028 
   1029 // Unprintable types are that too big to be printed completely.
   1030 
   1031 struct Big {
   1032   Big() { memset(array, 0, sizeof(array)); }
   1033   char array[257];
   1034 };
   1035 
   1036 TEST(PrintUnpritableTypeTest, BigObject) {
   1037   EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
   1038             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
   1039             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
   1040             "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
   1041             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
   1042             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
   1043             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
   1044             Print(Big()));
   1045 }
   1046 
   1047 // Tests printing user-defined streamable types.
   1048 
   1049 // Streamable types in the global namespace.
   1050 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
   1051   StreamableInGlobal x;
   1052   EXPECT_EQ("StreamableInGlobal", Print(x));
   1053   EXPECT_EQ("StreamableInGlobal*", Print(&x));
   1054 }
   1055 
   1056 // Printable template types in a user namespace.
   1057 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
   1058   EXPECT_EQ("StreamableTemplateInFoo: 0",
   1059             Print(::foo::StreamableTemplateInFoo<int>()));
   1060 }
   1061 
   1062 // Tests printing user-defined types that have a PrintTo() function.
   1063 TEST(PrintPrintableTypeTest, InUserNamespace) {
   1064   EXPECT_EQ("PrintableViaPrintTo: 0",
   1065             Print(::foo::PrintableViaPrintTo()));
   1066 }
   1067 
   1068 // Tests printing a pointer to a user-defined type that has a <<
   1069 // operator for its pointer.
   1070 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
   1071   ::foo::PointerPrintable x;
   1072   EXPECT_EQ("PointerPrintable*", Print(&x));
   1073 }
   1074 
   1075 // Tests printing user-defined class template that have a PrintTo() function.
   1076 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
   1077   EXPECT_EQ("PrintableViaPrintToTemplate: 5",
   1078             Print(::foo::PrintableViaPrintToTemplate<int>(5)));
   1079 }
   1080 
   1081 #if GTEST_HAS_PROTOBUF_
   1082 
   1083 // Tests printing a protocol message.
   1084 TEST(PrintProtocolMessageTest, PrintsShortDebugString) {
   1085   testing::internal::TestMessage msg;
   1086   msg.set_member("yes");
   1087   EXPECT_EQ("<member:\"yes\">", Print(msg));
   1088 }
   1089 
   1090 // Tests printing a short proto2 message.
   1091 TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) {
   1092   testing::internal::FooMessage msg;
   1093   msg.set_int_field(2);
   1094   msg.set_string_field("hello");
   1095   EXPECT_PRED2(RE::FullMatch, Print(msg),
   1096                "<int_field:\\s*2\\s+string_field:\\s*\"hello\">");
   1097 }
   1098 
   1099 // Tests printing a long proto2 message.
   1100 TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) {
   1101   testing::internal::FooMessage msg;
   1102   msg.set_int_field(2);
   1103   msg.set_string_field("hello");
   1104   msg.add_names("peter");
   1105   msg.add_names("paul");
   1106   msg.add_names("mary");
   1107   EXPECT_PRED2(RE::FullMatch, Print(msg),
   1108                "<\n"
   1109                "int_field:\\s*2\n"
   1110                "string_field:\\s*\"hello\"\n"
   1111                "names:\\s*\"peter\"\n"
   1112                "names:\\s*\"paul\"\n"
   1113                "names:\\s*\"mary\"\n"
   1114                ">");
   1115 }
   1116 
   1117 #endif  // GTEST_HAS_PROTOBUF_
   1118 
   1119 // Tests that the universal printer prints both the address and the
   1120 // value of a reference.
   1121 TEST(PrintReferenceTest, PrintsAddressAndValue) {
   1122   int n = 5;
   1123   EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
   1124 
   1125   int a[2][3] = {
   1126     { 0, 1, 2 },
   1127     { 3, 4, 5 }
   1128   };
   1129   EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
   1130             PrintByRef(a));
   1131 
   1132   const ::foo::UnprintableInFoo x;
   1133   EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
   1134             "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
   1135             PrintByRef(x));
   1136 }
   1137 
   1138 // Tests that the universal printer prints a function pointer passed by
   1139 // reference.
   1140 TEST(PrintReferenceTest, HandlesFunctionPointer) {
   1141   void (*fp)(int n) = &MyFunction;
   1142   const string fp_pointer_string =
   1143       PrintPointer(reinterpret_cast<const void*>(&fp));
   1144   // We cannot directly cast &MyFunction to const void* because the
   1145   // standard disallows casting between pointers to functions and
   1146   // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
   1147   // this limitation.
   1148   const string fp_string = PrintPointer(reinterpret_cast<const void*>(
   1149       reinterpret_cast<internal::BiggestInt>(fp)));
   1150   EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
   1151             PrintByRef(fp));
   1152 }
   1153 
   1154 // Tests that the universal printer prints a member function pointer
   1155 // passed by reference.
   1156 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
   1157   int (Foo::*p)(char ch) = &Foo::MyMethod;
   1158   EXPECT_TRUE(HasPrefix(
   1159       PrintByRef(p),
   1160       "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
   1161           Print(sizeof(p)) + "-byte object "));
   1162 
   1163   char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
   1164   EXPECT_TRUE(HasPrefix(
   1165       PrintByRef(p2),
   1166       "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
   1167           Print(sizeof(p2)) + "-byte object "));
   1168 }
   1169 
   1170 // Tests that the universal printer prints a member variable pointer
   1171 // passed by reference.
   1172 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
   1173   int (Foo::*p) = &Foo::value;  // NOLINT
   1174   EXPECT_TRUE(HasPrefix(
   1175       PrintByRef(p),
   1176       "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
   1177 }
   1178 
   1179 // Useful for testing PrintToString().  We cannot use EXPECT_EQ()
   1180 // there as its implementation uses PrintToString().  The caller must
   1181 // ensure that 'value' has no side effect.
   1182 #define EXPECT_PRINT_TO_STRING_(value, expected_string)         \
   1183   EXPECT_TRUE(PrintToString(value) == (expected_string))        \
   1184       << " where " #value " prints as " << (PrintToString(value))
   1185 
   1186 TEST(PrintToStringTest, WorksForScalar) {
   1187   EXPECT_PRINT_TO_STRING_(123, "123");
   1188 }
   1189 
   1190 TEST(PrintToStringTest, WorksForPointerToConstChar) {
   1191   const char* p = "hello";
   1192   EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
   1193 }
   1194 
   1195 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
   1196   char s[] = "hello";
   1197   char* p = s;
   1198   EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
   1199 }
   1200 
   1201 TEST(PrintToStringTest, WorksForArray) {
   1202   int n[3] = { 1, 2, 3 };
   1203   EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
   1204 }
   1205 
   1206 #undef EXPECT_PRINT_TO_STRING_
   1207 
   1208 TEST(UniversalTersePrintTest, WorksForNonReference) {
   1209   ::std::stringstream ss;
   1210   UniversalTersePrint(123, &ss);
   1211   EXPECT_EQ("123", ss.str());
   1212 }
   1213 
   1214 TEST(UniversalTersePrintTest, WorksForReference) {
   1215   const int& n = 123;
   1216   ::std::stringstream ss;
   1217   UniversalTersePrint(n, &ss);
   1218   EXPECT_EQ("123", ss.str());
   1219 }
   1220 
   1221 TEST(UniversalTersePrintTest, WorksForCString) {
   1222   const char* s1 = "abc";
   1223   ::std::stringstream ss1;
   1224   UniversalTersePrint(s1, &ss1);
   1225   EXPECT_EQ("\"abc\"", ss1.str());
   1226 
   1227   char* s2 = const_cast<char*>(s1);
   1228   ::std::stringstream ss2;
   1229   UniversalTersePrint(s2, &ss2);
   1230   EXPECT_EQ("\"abc\"", ss2.str());
   1231 
   1232   const char* s3 = NULL;
   1233   ::std::stringstream ss3;
   1234   UniversalTersePrint(s3, &ss3);
   1235   EXPECT_EQ("NULL", ss3.str());
   1236 }
   1237 
   1238 TEST(UniversalPrintTest, WorksForNonReference) {
   1239   ::std::stringstream ss;
   1240   UniversalPrint(123, &ss);
   1241   EXPECT_EQ("123", ss.str());
   1242 }
   1243 
   1244 TEST(UniversalPrintTest, WorksForReference) {
   1245   const int& n = 123;
   1246   ::std::stringstream ss;
   1247   UniversalPrint(n, &ss);
   1248   EXPECT_EQ("123", ss.str());
   1249 }
   1250 
   1251 TEST(UniversalPrintTest, WorksForCString) {
   1252   const char* s1 = "abc";
   1253   ::std::stringstream ss1;
   1254   UniversalPrint(s1, &ss1);
   1255   EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str()));
   1256 
   1257   char* s2 = const_cast<char*>(s1);
   1258   ::std::stringstream ss2;
   1259   UniversalPrint(s2, &ss2);
   1260   EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str()));
   1261 
   1262   const char* s3 = NULL;
   1263   ::std::stringstream ss3;
   1264   UniversalPrint(s3, &ss3);
   1265   EXPECT_EQ("NULL", ss3.str());
   1266 }
   1267 
   1268 
   1269 #if GTEST_HAS_TR1_TUPLE
   1270 
   1271 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) {
   1272   Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple());
   1273   EXPECT_EQ(0u, result.size());
   1274 }
   1275 
   1276 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) {
   1277   Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1));
   1278   ASSERT_EQ(1u, result.size());
   1279   EXPECT_EQ("1", result[0]);
   1280 }
   1281 
   1282 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) {
   1283   Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a'));
   1284   ASSERT_EQ(2u, result.size());
   1285   EXPECT_EQ("1", result[0]);
   1286   EXPECT_EQ("'a' (97, 0x61)", result[1]);
   1287 }
   1288 
   1289 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) {
   1290   const int n = 1;
   1291   Strings result = UniversalTersePrintTupleFieldsToStrings(
   1292       tuple<const int&, const char*>(n, "a"));
   1293   ASSERT_EQ(2u, result.size());
   1294   EXPECT_EQ("1", result[0]);
   1295   EXPECT_EQ("\"a\"", result[1]);
   1296 }
   1297 
   1298 #endif  // GTEST_HAS_TR1_TUPLE
   1299 
   1300 }  // namespace gtest_printers_test
   1301 }  // namespace testing
   1302