media/webrtc/trunk/testing/gtest/test/gtest-printers_test.cc

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

     1 // Copyright 2007, Google Inc.
     2 // All rights reserved.
     3 //
     4 // Redistribution and use in source and binary forms, with or without
     5 // modification, are permitted provided that the following conditions are
     6 // met:
     7 //
     8 //     * Redistributions of source code must retain the above copyright
     9 // notice, this list of conditions and the following disclaimer.
    10 //     * Redistributions in binary form must reproduce the above
    11 // copyright notice, this list of conditions and the following disclaimer
    12 // in the documentation and/or other materials provided with the
    13 // distribution.
    14 //     * Neither the name of Google Inc. nor the names of its
    15 // contributors may be used to endorse or promote products derived from
    16 // this software without specific prior written permission.
    17 //
    18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    29 //
    30 // Author: wan@google.com (Zhanyong Wan)
    32 // Google Test - The Google C++ Testing Framework
    33 //
    34 // This file tests the universal value printer.
    36 #include "gtest/gtest-printers.h"
    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>
    51 #include "gtest/gtest.h"
    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
    61 // Some user-defined types for testing the universal value printer.
    63 // An anonymous enum type.
    64 enum AnonymousEnum {
    65   kAE1 = -1,
    66   kAE2 = 1
    67 };
    69 // An enum without a user-defined printer.
    70 enum EnumWithoutPrinter {
    71   kEWP1 = -2,
    72   kEWP2 = 42
    73 };
    75 // An enum with a << operator.
    76 enum EnumWithStreaming {
    77   kEWS1 = 10
    78 };
    80 std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) {
    81   return os << (e == kEWS1 ? "kEWS1" : "invalid");
    82 }
    84 // An enum with a PrintTo() function.
    85 enum EnumWithPrintTo {
    86   kEWPT1 = 1
    87 };
    89 void PrintTo(EnumWithPrintTo e, std::ostream* os) {
    90   *os << (e == kEWPT1 ? "kEWPT1" : "invalid");
    91 }
    93 // A class implicitly convertible to BiggestInt.
    94 class BiggestIntConvertible {
    95  public:
    96   operator ::testing::internal::BiggestInt() const { return 42; }
    97 };
    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 };
   108 // A user-defined streamable type in the global namespace.
   109 class StreamableInGlobal {
   110  public:
   111   virtual ~StreamableInGlobal() {}
   112 };
   114 inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) {
   115   os << "StreamableInGlobal";
   116 }
   118 void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) {
   119   os << "StreamableInGlobal*";
   120 }
   122 namespace foo {
   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 };
   133 // A user-defined printable type in a user-chosen namespace.
   134 struct PrintableViaPrintTo {
   135   PrintableViaPrintTo() : value() {}
   136   int value;
   137 };
   139 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
   140   *os << "PrintableViaPrintTo: " << x.value;
   141 }
   143 // A type with a user-defined << for printing its pointer.
   144 struct PointerPrintable {
   145 };
   147 ::std::ostream& operator<<(::std::ostream& os,
   148                            const PointerPrintable* /* x */) {
   149   return os << "PointerPrintable*";
   150 }
   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) {}
   158   const T& value() const { return value_; }
   159  private:
   160   T value_;
   161 };
   163 template <typename T>
   164 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
   165   *os << "PrintableViaPrintToTemplate: " << x.value();
   166 }
   168 // A user-defined streamable class template in a user namespace.
   169 template <typename T>
   170 class StreamableTemplateInFoo {
   171  public:
   172   StreamableTemplateInFoo() : value_() {}
   174   const T& value() const { return value_; }
   175  private:
   176   T value_;
   177 };
   179 template <typename T>
   180 inline ::std::ostream& operator<<(::std::ostream& os,
   181                                   const StreamableTemplateInFoo<T>& x) {
   182   return os << "StreamableTemplateInFoo: " << x.value();
   183 }
   185 }  // namespace foo
   187 namespace testing {
   188 namespace gtest_printers_test {
   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::FormatForComparisonFailureMessage;
   201 using ::testing::internal::ImplicitCast_;
   202 using ::testing::internal::NativeArray;
   203 using ::testing::internal::RE;
   204 using ::testing::internal::Strings;
   205 using ::testing::internal::UniversalPrint;
   206 using ::testing::internal::UniversalPrinter;
   207 using ::testing::internal::UniversalTersePrint;
   208 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
   209 using ::testing::internal::kReference;
   210 using ::testing::internal::string;
   212 #if GTEST_HAS_TR1_TUPLE
   213 using ::std::tr1::make_tuple;
   214 using ::std::tr1::tuple;
   215 #endif
   217 #if _MSC_VER
   218 // MSVC defines the following classes in the ::stdext namespace while
   219 // gcc defines them in the :: namespace.  Note that they are not part
   220 // of the C++ standard.
   221 using ::stdext::hash_map;
   222 using ::stdext::hash_set;
   223 using ::stdext::hash_multimap;
   224 using ::stdext::hash_multiset;
   225 #endif
   227 // Prints a value to a string using the universal value printer.  This
   228 // is a helper for testing UniversalPrinter<T>::Print() for various types.
   229 template <typename T>
   230 string Print(const T& value) {
   231   ::std::stringstream ss;
   232   UniversalPrinter<T>::Print(value, &ss);
   233   return ss.str();
   234 }
   236 // Prints a value passed by reference to a string, using the universal
   237 // value printer.  This is a helper for testing
   238 // UniversalPrinter<T&>::Print() for various types.
   239 template <typename T>
   240 string PrintByRef(const T& value) {
   241   ::std::stringstream ss;
   242   UniversalPrinter<T&>::Print(value, &ss);
   243   return ss.str();
   244 }
   246 // Tests printing various enum types.
   248 TEST(PrintEnumTest, AnonymousEnum) {
   249   EXPECT_EQ("-1", Print(kAE1));
   250   EXPECT_EQ("1", Print(kAE2));
   251 }
   253 TEST(PrintEnumTest, EnumWithoutPrinter) {
   254   EXPECT_EQ("-2", Print(kEWP1));
   255   EXPECT_EQ("42", Print(kEWP2));
   256 }
   258 TEST(PrintEnumTest, EnumWithStreaming) {
   259   EXPECT_EQ("kEWS1", Print(kEWS1));
   260   EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
   261 }
   263 TEST(PrintEnumTest, EnumWithPrintTo) {
   264   EXPECT_EQ("kEWPT1", Print(kEWPT1));
   265   EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
   266 }
   268 // Tests printing a class implicitly convertible to BiggestInt.
   270 TEST(PrintClassTest, BiggestIntConvertible) {
   271   EXPECT_EQ("42", Print(BiggestIntConvertible()));
   272 }
   274 // Tests printing various char types.
   276 // char.
   277 TEST(PrintCharTest, PlainChar) {
   278   EXPECT_EQ("'\\0'", Print('\0'));
   279   EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
   280   EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
   281   EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
   282   EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
   283   EXPECT_EQ("'\\a' (7)", Print('\a'));
   284   EXPECT_EQ("'\\b' (8)", Print('\b'));
   285   EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
   286   EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
   287   EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
   288   EXPECT_EQ("'\\t' (9)", Print('\t'));
   289   EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
   290   EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
   291   EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
   292   EXPECT_EQ("' ' (32, 0x20)", Print(' '));
   293   EXPECT_EQ("'a' (97, 0x61)", Print('a'));
   294 }
   296 // signed char.
   297 TEST(PrintCharTest, SignedChar) {
   298   EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
   299   EXPECT_EQ("'\\xCE' (-50)",
   300             Print(static_cast<signed char>(-50)));
   301 }
   303 // unsigned char.
   304 TEST(PrintCharTest, UnsignedChar) {
   305   EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
   306   EXPECT_EQ("'b' (98, 0x62)",
   307             Print(static_cast<unsigned char>('b')));
   308 }
   310 // Tests printing other simple, built-in types.
   312 // bool.
   313 TEST(PrintBuiltInTypeTest, Bool) {
   314   EXPECT_EQ("false", Print(false));
   315   EXPECT_EQ("true", Print(true));
   316 }
   318 // wchar_t.
   319 TEST(PrintBuiltInTypeTest, Wchar_t) {
   320   EXPECT_EQ("L'\\0'", Print(L'\0'));
   321   EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
   322   EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
   323   EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
   324   EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
   325   EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
   326   EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
   327   EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
   328   EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
   329   EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
   330   EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
   331   EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
   332   EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
   333   EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
   334   EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
   335   EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
   336   EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
   337   EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
   338 }
   340 // Test that Int64 provides more storage than wchar_t.
   341 TEST(PrintTypeSizeTest, Wchar_t) {
   342   EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
   343 }
   345 // Various integer types.
   346 TEST(PrintBuiltInTypeTest, Integer) {
   347   EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255)));  // uint8
   348   EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128)));  // int8
   349   EXPECT_EQ("65535", Print(USHRT_MAX));  // uint16
   350   EXPECT_EQ("-32768", Print(SHRT_MIN));  // int16
   351   EXPECT_EQ("4294967295", Print(UINT_MAX));  // uint32
   352   EXPECT_EQ("-2147483648", Print(INT_MIN));  // int32
   353   EXPECT_EQ("18446744073709551615",
   354             Print(static_cast<testing::internal::UInt64>(-1)));  // uint64
   355   EXPECT_EQ("-9223372036854775808",
   356             Print(static_cast<testing::internal::Int64>(1) << 63));  // int64
   357 }
   359 // Size types.
   360 TEST(PrintBuiltInTypeTest, Size_t) {
   361   EXPECT_EQ("1", Print(sizeof('a')));  // size_t.
   362 #if !GTEST_OS_WINDOWS
   363   // Windows has no ssize_t type.
   364   EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2)));  // ssize_t.
   365 #endif  // !GTEST_OS_WINDOWS
   366 }
   368 // Floating-points.
   369 TEST(PrintBuiltInTypeTest, FloatingPoints) {
   370   EXPECT_EQ("1.5", Print(1.5f));   // float
   371   EXPECT_EQ("-2.5", Print(-2.5));  // double
   372 }
   374 // Since ::std::stringstream::operator<<(const void *) formats the pointer
   375 // output differently with different compilers, we have to create the expected
   376 // output first and use it as our expectation.
   377 static string PrintPointer(const void *p) {
   378   ::std::stringstream expected_result_stream;
   379   expected_result_stream << p;
   380   return expected_result_stream.str();
   381 }
   383 // Tests printing C strings.
   385 // const char*.
   386 TEST(PrintCStringTest, Const) {
   387   const char* p = "World";
   388   EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
   389 }
   391 // char*.
   392 TEST(PrintCStringTest, NonConst) {
   393   char p[] = "Hi";
   394   EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
   395             Print(static_cast<char*>(p)));
   396 }
   398 // NULL C string.
   399 TEST(PrintCStringTest, Null) {
   400   const char* p = NULL;
   401   EXPECT_EQ("NULL", Print(p));
   402 }
   404 // Tests that C strings are escaped properly.
   405 TEST(PrintCStringTest, EscapesProperly) {
   406   const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
   407   EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
   408             "\\n\\r\\t\\v\\x7F\\xFF a\"",
   409             Print(p));
   410 }
   414 // MSVC compiler can be configured to define whar_t as a typedef
   415 // of unsigned short. Defining an overload for const wchar_t* in that case
   416 // would cause pointers to unsigned shorts be printed as wide strings,
   417 // possibly accessing more memory than intended and causing invalid
   418 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
   419 // wchar_t is implemented as a native type.
   420 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
   422 // const wchar_t*.
   423 TEST(PrintWideCStringTest, Const) {
   424   const wchar_t* p = L"World";
   425   EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
   426 }
   428 // wchar_t*.
   429 TEST(PrintWideCStringTest, NonConst) {
   430   wchar_t p[] = L"Hi";
   431   EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
   432             Print(static_cast<wchar_t*>(p)));
   433 }
   435 // NULL wide C string.
   436 TEST(PrintWideCStringTest, Null) {
   437   const wchar_t* p = NULL;
   438   EXPECT_EQ("NULL", Print(p));
   439 }
   441 // Tests that wide C strings are escaped properly.
   442 TEST(PrintWideCStringTest, EscapesProperly) {
   443   const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r',
   444                        '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
   445   EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f"
   446             "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
   447             Print(static_cast<const wchar_t*>(s)));
   448 }
   449 #endif  // native wchar_t
   451 // Tests printing pointers to other char types.
   453 // signed char*.
   454 TEST(PrintCharPointerTest, SignedChar) {
   455   signed char* p = reinterpret_cast<signed char*>(0x1234);
   456   EXPECT_EQ(PrintPointer(p), Print(p));
   457   p = NULL;
   458   EXPECT_EQ("NULL", Print(p));
   459 }
   461 // const signed char*.
   462 TEST(PrintCharPointerTest, ConstSignedChar) {
   463   signed char* p = reinterpret_cast<signed char*>(0x1234);
   464   EXPECT_EQ(PrintPointer(p), Print(p));
   465   p = NULL;
   466   EXPECT_EQ("NULL", Print(p));
   467 }
   469 // unsigned char*.
   470 TEST(PrintCharPointerTest, UnsignedChar) {
   471   unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
   472   EXPECT_EQ(PrintPointer(p), Print(p));
   473   p = NULL;
   474   EXPECT_EQ("NULL", Print(p));
   475 }
   477 // const unsigned char*.
   478 TEST(PrintCharPointerTest, ConstUnsignedChar) {
   479   const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
   480   EXPECT_EQ(PrintPointer(p), Print(p));
   481   p = NULL;
   482   EXPECT_EQ("NULL", Print(p));
   483 }
   485 // Tests printing pointers to simple, built-in types.
   487 // bool*.
   488 TEST(PrintPointerToBuiltInTypeTest, Bool) {
   489   bool* p = reinterpret_cast<bool*>(0xABCD);
   490   EXPECT_EQ(PrintPointer(p), Print(p));
   491   p = NULL;
   492   EXPECT_EQ("NULL", Print(p));
   493 }
   495 // void*.
   496 TEST(PrintPointerToBuiltInTypeTest, Void) {
   497   void* p = reinterpret_cast<void*>(0xABCD);
   498   EXPECT_EQ(PrintPointer(p), Print(p));
   499   p = NULL;
   500   EXPECT_EQ("NULL", Print(p));
   501 }
   503 // const void*.
   504 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
   505   const void* p = reinterpret_cast<const void*>(0xABCD);
   506   EXPECT_EQ(PrintPointer(p), Print(p));
   507   p = NULL;
   508   EXPECT_EQ("NULL", Print(p));
   509 }
   511 // Tests printing pointers to pointers.
   512 TEST(PrintPointerToPointerTest, IntPointerPointer) {
   513   int** p = reinterpret_cast<int**>(0xABCD);
   514   EXPECT_EQ(PrintPointer(p), Print(p));
   515   p = NULL;
   516   EXPECT_EQ("NULL", Print(p));
   517 }
   519 // Tests printing (non-member) function pointers.
   521 void MyFunction(int /* n */) {}
   523 TEST(PrintPointerTest, NonMemberFunctionPointer) {
   524   // We cannot directly cast &MyFunction to const void* because the
   525   // standard disallows casting between pointers to functions and
   526   // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
   527   // this limitation.
   528   EXPECT_EQ(
   529       PrintPointer(reinterpret_cast<const void*>(
   530           reinterpret_cast<internal::BiggestInt>(&MyFunction))),
   531       Print(&MyFunction));
   532   int (*p)(bool) = NULL;  // NOLINT
   533   EXPECT_EQ("NULL", Print(p));
   534 }
   536 // An assertion predicate determining whether a one string is a prefix for
   537 // another.
   538 template <typename StringType>
   539 AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
   540   if (str.find(prefix, 0) == 0)
   541     return AssertionSuccess();
   543   const bool is_wide_string = sizeof(prefix[0]) > 1;
   544   const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
   545   return AssertionFailure()
   546       << begin_string_quote << prefix << "\" is not a prefix of "
   547       << begin_string_quote << str << "\"\n";
   548 }
   550 // Tests printing member variable pointers.  Although they are called
   551 // pointers, they don't point to a location in the address space.
   552 // Their representation is implementation-defined.  Thus they will be
   553 // printed as raw bytes.
   555 struct Foo {
   556  public:
   557   virtual ~Foo() {}
   558   int MyMethod(char x) { return x + 1; }
   559   virtual char MyVirtualMethod(int /* n */) { return 'a'; }
   561   int value;
   562 };
   564 TEST(PrintPointerTest, MemberVariablePointer) {
   565   EXPECT_TRUE(HasPrefix(Print(&Foo::value),
   566                         Print(sizeof(&Foo::value)) + "-byte object "));
   567   int (Foo::*p) = NULL;  // NOLINT
   568   EXPECT_TRUE(HasPrefix(Print(p),
   569                         Print(sizeof(p)) + "-byte object "));
   570 }
   572 // Tests printing member function pointers.  Although they are called
   573 // pointers, they don't point to a location in the address space.
   574 // Their representation is implementation-defined.  Thus they will be
   575 // printed as raw bytes.
   576 TEST(PrintPointerTest, MemberFunctionPointer) {
   577   EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod),
   578                         Print(sizeof(&Foo::MyMethod)) + "-byte object "));
   579   EXPECT_TRUE(
   580       HasPrefix(Print(&Foo::MyVirtualMethod),
   581                 Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
   582   int (Foo::*p)(char) = NULL;  // NOLINT
   583   EXPECT_TRUE(HasPrefix(Print(p),
   584                         Print(sizeof(p)) + "-byte object "));
   585 }
   587 // Tests printing C arrays.
   589 // The difference between this and Print() is that it ensures that the
   590 // argument is a reference to an array.
   591 template <typename T, size_t N>
   592 string PrintArrayHelper(T (&a)[N]) {
   593   return Print(a);
   594 }
   596 // One-dimensional array.
   597 TEST(PrintArrayTest, OneDimensionalArray) {
   598   int a[5] = { 1, 2, 3, 4, 5 };
   599   EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
   600 }
   602 // Two-dimensional array.
   603 TEST(PrintArrayTest, TwoDimensionalArray) {
   604   int a[2][5] = {
   605     { 1, 2, 3, 4, 5 },
   606     { 6, 7, 8, 9, 0 }
   607   };
   608   EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
   609 }
   611 // Array of const elements.
   612 TEST(PrintArrayTest, ConstArray) {
   613   const bool a[1] = { false };
   614   EXPECT_EQ("{ false }", PrintArrayHelper(a));
   615 }
   617 // char array without terminating NUL.
   618 TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
   619   // Array a contains '\0' in the middle and doesn't end with '\0'.
   620   char a[] = { 'H', '\0', 'i' };
   621   EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
   622 }
   624 // const char array with terminating NUL.
   625 TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) {
   626   const char a[] = "\0Hi";
   627   EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a));
   628 }
   630 // const wchar_t array without terminating NUL.
   631 TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
   632   // Array a contains '\0' in the middle and doesn't end with '\0'.
   633   const wchar_t a[] = { L'H', L'\0', L'i' };
   634   EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
   635 }
   637 // wchar_t array with terminating NUL.
   638 TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
   639   const wchar_t a[] = L"\0Hi";
   640   EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a));
   641 }
   643 // Array of objects.
   644 TEST(PrintArrayTest, ObjectArray) {
   645   string a[3] = { "Hi", "Hello", "Ni hao" };
   646   EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
   647 }
   649 // Array with many elements.
   650 TEST(PrintArrayTest, BigArray) {
   651   int a[100] = { 1, 2, 3 };
   652   EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
   653             PrintArrayHelper(a));
   654 }
   656 // Tests printing ::string and ::std::string.
   658 #if GTEST_HAS_GLOBAL_STRING
   659 // ::string.
   660 TEST(PrintStringTest, StringInGlobalNamespace) {
   661   const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
   662   const ::string str(s, sizeof(s));
   663   EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
   664             Print(str));
   665 }
   666 #endif  // GTEST_HAS_GLOBAL_STRING
   668 // ::std::string.
   669 TEST(PrintStringTest, StringInStdNamespace) {
   670   const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
   671   const ::std::string str(s, sizeof(s));
   672   EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
   673             Print(str));
   674 }
   676 TEST(PrintStringTest, StringAmbiguousHex) {
   677   // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
   678   // '\x6', '\x6B', or '\x6BA'.
   680   // a hex escaping sequence following by a decimal digit
   681   EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
   682   // a hex escaping sequence following by a hex digit (lower-case)
   683   EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
   684   // a hex escaping sequence following by a hex digit (upper-case)
   685   EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
   686   // a hex escaping sequence following by a non-xdigit
   687   EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
   688 }
   690 // Tests printing ::wstring and ::std::wstring.
   692 #if GTEST_HAS_GLOBAL_WSTRING
   693 // ::wstring.
   694 TEST(PrintWideStringTest, StringInGlobalNamespace) {
   695   const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
   696   const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
   697   EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
   698             "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
   699             Print(str));
   700 }
   701 #endif  // GTEST_HAS_GLOBAL_WSTRING
   703 #if GTEST_HAS_STD_WSTRING
   704 // ::std::wstring.
   705 TEST(PrintWideStringTest, StringInStdNamespace) {
   706   const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
   707   const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
   708   EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
   709             "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
   710             Print(str));
   711 }
   713 TEST(PrintWideStringTest, StringAmbiguousHex) {
   714   // same for wide strings.
   715   EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3")));
   716   EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
   717             Print(::std::wstring(L"mm\x6" L"bananas")));
   718   EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
   719             Print(::std::wstring(L"NOM\x6" L"BANANA")));
   720   EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
   721 }
   722 #endif  // GTEST_HAS_STD_WSTRING
   724 // Tests printing types that support generic streaming (i.e. streaming
   725 // to std::basic_ostream<Char, CharTraits> for any valid Char and
   726 // CharTraits types).
   728 // Tests printing a non-template type that supports generic streaming.
   730 class AllowsGenericStreaming {};
   732 template <typename Char, typename CharTraits>
   733 std::basic_ostream<Char, CharTraits>& operator<<(
   734     std::basic_ostream<Char, CharTraits>& os,
   735     const AllowsGenericStreaming& /* a */) {
   736   return os << "AllowsGenericStreaming";
   737 }
   739 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
   740   AllowsGenericStreaming a;
   741   EXPECT_EQ("AllowsGenericStreaming", Print(a));
   742 }
   744 // Tests printing a template type that supports generic streaming.
   746 template <typename T>
   747 class AllowsGenericStreamingTemplate {};
   749 template <typename Char, typename CharTraits, typename T>
   750 std::basic_ostream<Char, CharTraits>& operator<<(
   751     std::basic_ostream<Char, CharTraits>& os,
   752     const AllowsGenericStreamingTemplate<T>& /* a */) {
   753   return os << "AllowsGenericStreamingTemplate";
   754 }
   756 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
   757   AllowsGenericStreamingTemplate<int> a;
   758   EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
   759 }
   761 // Tests printing a type that supports generic streaming and can be
   762 // implicitly converted to another printable type.
   764 template <typename T>
   765 class AllowsGenericStreamingAndImplicitConversionTemplate {
   766  public:
   767   operator bool() const { return false; }
   768 };
   770 template <typename Char, typename CharTraits, typename T>
   771 std::basic_ostream<Char, CharTraits>& operator<<(
   772     std::basic_ostream<Char, CharTraits>& os,
   773     const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) {
   774   return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
   775 }
   777 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
   778   AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
   779   EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
   780 }
   782 #if GTEST_HAS_STRING_PIECE_
   784 // Tests printing StringPiece.
   786 TEST(PrintStringPieceTest, SimpleStringPiece) {
   787   const StringPiece sp = "Hello";
   788   EXPECT_EQ("\"Hello\"", Print(sp));
   789 }
   791 TEST(PrintStringPieceTest, UnprintableCharacters) {
   792   const char str[] = "NUL (\0) and \r\t";
   793   const StringPiece sp(str, sizeof(str) - 1);
   794   EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
   795 }
   797 #endif  // GTEST_HAS_STRING_PIECE_
   799 // Tests printing STL containers.
   801 TEST(PrintStlContainerTest, EmptyDeque) {
   802   deque<char> empty;
   803   EXPECT_EQ("{}", Print(empty));
   804 }
   806 TEST(PrintStlContainerTest, NonEmptyDeque) {
   807   deque<int> non_empty;
   808   non_empty.push_back(1);
   809   non_empty.push_back(3);
   810   EXPECT_EQ("{ 1, 3 }", Print(non_empty));
   811 }
   813 #if GTEST_HAS_HASH_MAP_
   815 TEST(PrintStlContainerTest, OneElementHashMap) {
   816   hash_map<int, char> map1;
   817   map1[1] = 'a';
   818   EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1));
   819 }
   821 TEST(PrintStlContainerTest, HashMultiMap) {
   822   hash_multimap<int, bool> map1;
   823   map1.insert(make_pair(5, true));
   824   map1.insert(make_pair(5, false));
   826   // Elements of hash_multimap can be printed in any order.
   827   const string result = Print(map1);
   828   EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
   829               result == "{ (5, false), (5, true) }")
   830                   << " where Print(map1) returns \"" << result << "\".";
   831 }
   833 #endif  // GTEST_HAS_HASH_MAP_
   835 #if GTEST_HAS_HASH_SET_
   837 TEST(PrintStlContainerTest, HashSet) {
   838   hash_set<string> set1;
   839   set1.insert("hello");
   840   EXPECT_EQ("{ \"hello\" }", Print(set1));
   841 }
   843 TEST(PrintStlContainerTest, HashMultiSet) {
   844   const int kSize = 5;
   845   int a[kSize] = { 1, 1, 2, 5, 1 };
   846   hash_multiset<int> set1(a, a + kSize);
   848   // Elements of hash_multiset can be printed in any order.
   849   const string result = Print(set1);
   850   const string expected_pattern = "{ d, d, d, d, d }";  // d means a digit.
   852   // Verifies the result matches the expected pattern; also extracts
   853   // the numbers in the result.
   854   ASSERT_EQ(expected_pattern.length(), result.length());
   855   std::vector<int> numbers;
   856   for (size_t i = 0; i != result.length(); i++) {
   857     if (expected_pattern[i] == 'd') {
   858       ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0);
   859       numbers.push_back(result[i] - '0');
   860     } else {
   861       EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
   862                                                 << result;
   863     }
   864   }
   866   // Makes sure the result contains the right numbers.
   867   std::sort(numbers.begin(), numbers.end());
   868   std::sort(a, a + kSize);
   869   EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
   870 }
   872 #endif  // GTEST_HAS_HASH_SET_
   874 TEST(PrintStlContainerTest, List) {
   875   const string a[] = {
   876     "hello",
   877     "world"
   878   };
   879   const list<string> strings(a, a + 2);
   880   EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
   881 }
   883 TEST(PrintStlContainerTest, Map) {
   884   map<int, bool> map1;
   885   map1[1] = true;
   886   map1[5] = false;
   887   map1[3] = true;
   888   EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
   889 }
   891 TEST(PrintStlContainerTest, MultiMap) {
   892   multimap<bool, int> map1;
   893   // The make_pair template function would deduce the type as
   894   // pair<bool, int> here, and since the key part in a multimap has to
   895   // be constant, without a templated ctor in the pair class (as in
   896   // libCstd on Solaris), make_pair call would fail to compile as no
   897   // implicit conversion is found.  Thus explicit typename is used
   898   // here instead.
   899   map1.insert(pair<const bool, int>(true, 0));
   900   map1.insert(pair<const bool, int>(true, 1));
   901   map1.insert(pair<const bool, int>(false, 2));
   902   EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
   903 }
   905 TEST(PrintStlContainerTest, Set) {
   906   const unsigned int a[] = { 3, 0, 5 };
   907   set<unsigned int> set1(a, a + 3);
   908   EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
   909 }
   911 TEST(PrintStlContainerTest, MultiSet) {
   912   const int a[] = { 1, 1, 2, 5, 1 };
   913   multiset<int> set1(a, a + 5);
   914   EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
   915 }
   917 TEST(PrintStlContainerTest, Pair) {
   918   pair<const bool, int> p(true, 5);
   919   EXPECT_EQ("(true, 5)", Print(p));
   920 }
   922 TEST(PrintStlContainerTest, Vector) {
   923   vector<int> v;
   924   v.push_back(1);
   925   v.push_back(2);
   926   EXPECT_EQ("{ 1, 2 }", Print(v));
   927 }
   929 TEST(PrintStlContainerTest, LongSequence) {
   930   const int a[100] = { 1, 2, 3 };
   931   const vector<int> v(a, a + 100);
   932   EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
   933             "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
   934 }
   936 TEST(PrintStlContainerTest, NestedContainer) {
   937   const int a1[] = { 1, 2 };
   938   const int a2[] = { 3, 4, 5 };
   939   const list<int> l1(a1, a1 + 2);
   940   const list<int> l2(a2, a2 + 3);
   942   vector<list<int> > v;
   943   v.push_back(l1);
   944   v.push_back(l2);
   945   EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
   946 }
   948 TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
   949   const int a[3] = { 1, 2, 3 };
   950   NativeArray<int> b(a, 3, kReference);
   951   EXPECT_EQ("{ 1, 2, 3 }", Print(b));
   952 }
   954 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
   955   const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
   956   NativeArray<int[3]> b(a, 2, kReference);
   957   EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
   958 }
   960 // Tests that a class named iterator isn't treated as a container.
   962 struct iterator {
   963   char x;
   964 };
   966 TEST(PrintStlContainerTest, Iterator) {
   967   iterator it = {};
   968   EXPECT_EQ("1-byte object <00>", Print(it));
   969 }
   971 // Tests that a class named const_iterator isn't treated as a container.
   973 struct const_iterator {
   974   char x;
   975 };
   977 TEST(PrintStlContainerTest, ConstIterator) {
   978   const_iterator it = {};
   979   EXPECT_EQ("1-byte object <00>", Print(it));
   980 }
   982 #if GTEST_HAS_TR1_TUPLE
   983 // Tests printing tuples.
   985 // Tuples of various arities.
   986 TEST(PrintTupleTest, VariousSizes) {
   987   tuple<> t0;
   988   EXPECT_EQ("()", Print(t0));
   990   tuple<int> t1(5);
   991   EXPECT_EQ("(5)", Print(t1));
   993   tuple<char, bool> t2('a', true);
   994   EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
   996   tuple<bool, int, int> t3(false, 2, 3);
   997   EXPECT_EQ("(false, 2, 3)", Print(t3));
   999   tuple<bool, int, int, int> t4(false, 2, 3, 4);
  1000   EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
  1002   tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
  1003   EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
  1005   tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
  1006   EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
  1008   tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7);
  1009   EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
  1011   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));
  1015   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));
  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   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));
  1031 // Nested tuples.
  1032 TEST(PrintTupleTest, NestedTuple) {
  1033   tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a');
  1034   EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
  1037 #endif  // GTEST_HAS_TR1_TUPLE
  1039 // Tests printing user-defined unprintable types.
  1041 // Unprintable types in the global namespace.
  1042 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
  1043   EXPECT_EQ("1-byte object <00>",
  1044             Print(UnprintableTemplateInGlobal<char>()));
  1047 // Unprintable types in a user namespace.
  1048 TEST(PrintUnprintableTypeTest, InUserNamespace) {
  1049   EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
  1050             Print(::foo::UnprintableInFoo()));
  1053 // Unprintable types are that too big to be printed completely.
  1055 struct Big {
  1056   Big() { memset(array, 0, sizeof(array)); }
  1057   char array[257];
  1058 };
  1060 TEST(PrintUnpritableTypeTest, BigObject) {
  1061   EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
  1062             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
  1063             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
  1064             "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
  1065             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
  1066             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
  1067             "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
  1068             Print(Big()));
  1071 // Tests printing user-defined streamable types.
  1073 // Streamable types in the global namespace.
  1074 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
  1075   StreamableInGlobal x;
  1076   EXPECT_EQ("StreamableInGlobal", Print(x));
  1077   EXPECT_EQ("StreamableInGlobal*", Print(&x));
  1080 // Printable template types in a user namespace.
  1081 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
  1082   EXPECT_EQ("StreamableTemplateInFoo: 0",
  1083             Print(::foo::StreamableTemplateInFoo<int>()));
  1086 // Tests printing user-defined types that have a PrintTo() function.
  1087 TEST(PrintPrintableTypeTest, InUserNamespace) {
  1088   EXPECT_EQ("PrintableViaPrintTo: 0",
  1089             Print(::foo::PrintableViaPrintTo()));
  1092 // Tests printing a pointer to a user-defined type that has a <<
  1093 // operator for its pointer.
  1094 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
  1095   ::foo::PointerPrintable x;
  1096   EXPECT_EQ("PointerPrintable*", Print(&x));
  1099 // Tests printing user-defined class template that have a PrintTo() function.
  1100 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
  1101   EXPECT_EQ("PrintableViaPrintToTemplate: 5",
  1102             Print(::foo::PrintableViaPrintToTemplate<int>(5)));
  1105 #if GTEST_HAS_PROTOBUF_
  1107 // Tests printing a protocol message.
  1108 TEST(PrintProtocolMessageTest, PrintsShortDebugString) {
  1109   testing::internal::TestMessage msg;
  1110   msg.set_member("yes");
  1111   EXPECT_EQ("<member:\"yes\">", Print(msg));
  1114 // Tests printing a short proto2 message.
  1115 TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) {
  1116   testing::internal::FooMessage msg;
  1117   msg.set_int_field(2);
  1118   msg.set_string_field("hello");
  1119   EXPECT_PRED2(RE::FullMatch, Print(msg),
  1120                "<int_field:\\s*2\\s+string_field:\\s*\"hello\">");
  1123 // Tests printing a long proto2 message.
  1124 TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) {
  1125   testing::internal::FooMessage msg;
  1126   msg.set_int_field(2);
  1127   msg.set_string_field("hello");
  1128   msg.add_names("peter");
  1129   msg.add_names("paul");
  1130   msg.add_names("mary");
  1131   EXPECT_PRED2(RE::FullMatch, Print(msg),
  1132                "<\n"
  1133                "int_field:\\s*2\n"
  1134                "string_field:\\s*\"hello\"\n"
  1135                "names:\\s*\"peter\"\n"
  1136                "names:\\s*\"paul\"\n"
  1137                "names:\\s*\"mary\"\n"
  1138                ">");
  1141 #endif  // GTEST_HAS_PROTOBUF_
  1143 // Tests that the universal printer prints both the address and the
  1144 // value of a reference.
  1145 TEST(PrintReferenceTest, PrintsAddressAndValue) {
  1146   int n = 5;
  1147   EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
  1149   int a[2][3] = {
  1150     { 0, 1, 2 },
  1151     { 3, 4, 5 }
  1152   };
  1153   EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
  1154             PrintByRef(a));
  1156   const ::foo::UnprintableInFoo x;
  1157   EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
  1158             "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
  1159             PrintByRef(x));
  1162 // Tests that the universal printer prints a function pointer passed by
  1163 // reference.
  1164 TEST(PrintReferenceTest, HandlesFunctionPointer) {
  1165   void (*fp)(int n) = &MyFunction;
  1166   const string fp_pointer_string =
  1167       PrintPointer(reinterpret_cast<const void*>(&fp));
  1168   // We cannot directly cast &MyFunction to const void* because the
  1169   // standard disallows casting between pointers to functions and
  1170   // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
  1171   // this limitation.
  1172   const string fp_string = PrintPointer(reinterpret_cast<const void*>(
  1173       reinterpret_cast<internal::BiggestInt>(fp)));
  1174   EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
  1175             PrintByRef(fp));
  1178 // Tests that the universal printer prints a member function pointer
  1179 // passed by reference.
  1180 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
  1181   int (Foo::*p)(char ch) = &Foo::MyMethod;
  1182   EXPECT_TRUE(HasPrefix(
  1183       PrintByRef(p),
  1184       "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
  1185           Print(sizeof(p)) + "-byte object "));
  1187   char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
  1188   EXPECT_TRUE(HasPrefix(
  1189       PrintByRef(p2),
  1190       "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
  1191           Print(sizeof(p2)) + "-byte object "));
  1194 // Tests that the universal printer prints a member variable pointer
  1195 // passed by reference.
  1196 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
  1197   int (Foo::*p) = &Foo::value;  // NOLINT
  1198   EXPECT_TRUE(HasPrefix(
  1199       PrintByRef(p),
  1200       "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
  1203 // Tests that FormatForComparisonFailureMessage(), which is used to print
  1204 // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion
  1205 // fails, formats the operand in the desired way.
  1207 // scalar
  1208 TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
  1209   EXPECT_STREQ("123",
  1210                FormatForComparisonFailureMessage(123, 124).c_str());
  1213 // non-char pointer
  1214 TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
  1215   int n = 0;
  1216   EXPECT_EQ(PrintPointer(&n),
  1217             FormatForComparisonFailureMessage(&n, &n).c_str());
  1220 // non-char array
  1221 TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
  1222   // In expression 'array == x', 'array' is compared by pointer.
  1223   // Therefore we want to print an array operand as a pointer.
  1224   int n[] = { 1, 2, 3 };
  1225   EXPECT_EQ(PrintPointer(n),
  1226             FormatForComparisonFailureMessage(n, n).c_str());
  1229 // Tests formatting a char pointer when it's compared with another pointer.
  1230 // In this case we want to print it as a raw pointer, as the comparision is by
  1231 // pointer.
  1233 // char pointer vs pointer
  1234 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
  1235   // In expression 'p == x', where 'p' and 'x' are (const or not) char
  1236   // pointers, the operands are compared by pointer.  Therefore we
  1237   // want to print 'p' as a pointer instead of a C string (we don't
  1238   // even know if it's supposed to point to a valid C string).
  1240   // const char*
  1241   const char* s = "hello";
  1242   EXPECT_EQ(PrintPointer(s),
  1243             FormatForComparisonFailureMessage(s, s).c_str());
  1245   // char*
  1246   char ch = 'a';
  1247   EXPECT_EQ(PrintPointer(&ch),
  1248             FormatForComparisonFailureMessage(&ch, &ch).c_str());
  1251 // wchar_t pointer vs pointer
  1252 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
  1253   // In expression 'p == x', where 'p' and 'x' are (const or not) char
  1254   // pointers, the operands are compared by pointer.  Therefore we
  1255   // want to print 'p' as a pointer instead of a wide C string (we don't
  1256   // even know if it's supposed to point to a valid wide C string).
  1258   // const wchar_t*
  1259   const wchar_t* s = L"hello";
  1260   EXPECT_EQ(PrintPointer(s),
  1261             FormatForComparisonFailureMessage(s, s).c_str());
  1263   // wchar_t*
  1264   wchar_t ch = L'a';
  1265   EXPECT_EQ(PrintPointer(&ch),
  1266             FormatForComparisonFailureMessage(&ch, &ch).c_str());
  1269 // Tests formatting a char pointer when it's compared to a string object.
  1270 // In this case we want to print the char pointer as a C string.
  1272 #if GTEST_HAS_GLOBAL_STRING
  1273 // char pointer vs ::string
  1274 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) {
  1275   const char* s = "hello \"world";
  1276   EXPECT_STREQ("\"hello \\\"world\"",  // The string content should be escaped.
  1277                FormatForComparisonFailureMessage(s, ::string()).c_str());
  1279   // char*
  1280   char str[] = "hi\1";
  1281   char* p = str;
  1282   EXPECT_STREQ("\"hi\\x1\"",  // The string content should be escaped.
  1283                FormatForComparisonFailureMessage(p, ::string()).c_str());
  1285 #endif
  1287 // char pointer vs std::string
  1288 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
  1289   const char* s = "hello \"world";
  1290   EXPECT_STREQ("\"hello \\\"world\"",  // The string content should be escaped.
  1291                FormatForComparisonFailureMessage(s, ::std::string()).c_str());
  1293   // char*
  1294   char str[] = "hi\1";
  1295   char* p = str;
  1296   EXPECT_STREQ("\"hi\\x1\"",  // The string content should be escaped.
  1297                FormatForComparisonFailureMessage(p, ::std::string()).c_str());
  1300 #if GTEST_HAS_GLOBAL_WSTRING
  1301 // wchar_t pointer vs ::wstring
  1302 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) {
  1303   const wchar_t* s = L"hi \"world";
  1304   EXPECT_STREQ("L\"hi \\\"world\"",  // The string content should be escaped.
  1305                FormatForComparisonFailureMessage(s, ::wstring()).c_str());
  1307   // wchar_t*
  1308   wchar_t str[] = L"hi\1";
  1309   wchar_t* p = str;
  1310   EXPECT_STREQ("L\"hi\\x1\"",  // The string content should be escaped.
  1311                FormatForComparisonFailureMessage(p, ::wstring()).c_str());
  1313 #endif
  1315 #if GTEST_HAS_STD_WSTRING
  1316 // wchar_t pointer vs std::wstring
  1317 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
  1318   const wchar_t* s = L"hi \"world";
  1319   EXPECT_STREQ("L\"hi \\\"world\"",  // The string content should be escaped.
  1320                FormatForComparisonFailureMessage(s, ::std::wstring()).c_str());
  1322   // wchar_t*
  1323   wchar_t str[] = L"hi\1";
  1324   wchar_t* p = str;
  1325   EXPECT_STREQ("L\"hi\\x1\"",  // The string content should be escaped.
  1326                FormatForComparisonFailureMessage(p, ::std::wstring()).c_str());
  1328 #endif
  1330 // Tests formatting a char array when it's compared with a pointer or array.
  1331 // In this case we want to print the array as a row pointer, as the comparison
  1332 // is by pointer.
  1334 // char array vs pointer
  1335 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
  1336   char str[] = "hi \"world\"";
  1337   char* p = NULL;
  1338   EXPECT_EQ(PrintPointer(str),
  1339             FormatForComparisonFailureMessage(str, p).c_str());
  1342 // char array vs char array
  1343 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
  1344   const char str[] = "hi \"world\"";
  1345   EXPECT_EQ(PrintPointer(str),
  1346             FormatForComparisonFailureMessage(str, str).c_str());
  1349 // wchar_t array vs pointer
  1350 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
  1351   wchar_t str[] = L"hi \"world\"";
  1352   wchar_t* p = NULL;
  1353   EXPECT_EQ(PrintPointer(str),
  1354             FormatForComparisonFailureMessage(str, p).c_str());
  1357 // wchar_t array vs wchar_t array
  1358 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
  1359   const wchar_t str[] = L"hi \"world\"";
  1360   EXPECT_EQ(PrintPointer(str),
  1361             FormatForComparisonFailureMessage(str, str).c_str());
  1364 // Tests formatting a char array when it's compared with a string object.
  1365 // In this case we want to print the array as a C string.
  1367 #if GTEST_HAS_GLOBAL_STRING
  1368 // char array vs string
  1369 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) {
  1370   const char str[] = "hi \"w\0rld\"";
  1371   EXPECT_STREQ("\"hi \\\"w\"",  // The content should be escaped.
  1372                                 // Embedded NUL terminates the string.
  1373                FormatForComparisonFailureMessage(str, ::string()).c_str());
  1375 #endif
  1377 // char array vs std::string
  1378 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
  1379   const char str[] = "hi \"world\"";
  1380   EXPECT_STREQ("\"hi \\\"world\\\"\"",  // The content should be escaped.
  1381                FormatForComparisonFailureMessage(str, ::std::string()).c_str());
  1384 #if GTEST_HAS_GLOBAL_WSTRING
  1385 // wchar_t array vs wstring
  1386 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) {
  1387   const wchar_t str[] = L"hi \"world\"";
  1388   EXPECT_STREQ("L\"hi \\\"world\\\"\"",  // The content should be escaped.
  1389                FormatForComparisonFailureMessage(str, ::wstring()).c_str());
  1391 #endif
  1393 #if GTEST_HAS_STD_WSTRING
  1394 // wchar_t array vs std::wstring
  1395 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
  1396   const wchar_t str[] = L"hi \"w\0rld\"";
  1397   EXPECT_STREQ(
  1398       "L\"hi \\\"w\"",  // The content should be escaped.
  1399                         // Embedded NUL terminates the string.
  1400       FormatForComparisonFailureMessage(str, ::std::wstring()).c_str());
  1402 #endif
  1404 // Useful for testing PrintToString().  We cannot use EXPECT_EQ()
  1405 // there as its implementation uses PrintToString().  The caller must
  1406 // ensure that 'value' has no side effect.
  1407 #define EXPECT_PRINT_TO_STRING_(value, expected_string)         \
  1408   EXPECT_TRUE(PrintToString(value) == (expected_string))        \
  1409       << " where " #value " prints as " << (PrintToString(value))
  1411 TEST(PrintToStringTest, WorksForScalar) {
  1412   EXPECT_PRINT_TO_STRING_(123, "123");
  1415 TEST(PrintToStringTest, WorksForPointerToConstChar) {
  1416   const char* p = "hello";
  1417   EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
  1420 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
  1421   char s[] = "hello";
  1422   char* p = s;
  1423   EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
  1426 TEST(PrintToStringTest, EscapesForPointerToConstChar) {
  1427   const char* p = "hello\n";
  1428   EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\"");
  1431 TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
  1432   char s[] = "hello\1";
  1433   char* p = s;
  1434   EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\"");
  1437 TEST(PrintToStringTest, WorksForArray) {
  1438   int n[3] = { 1, 2, 3 };
  1439   EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
  1442 TEST(PrintToStringTest, WorksForCharArray) {
  1443   char s[] = "hello";
  1444   EXPECT_PRINT_TO_STRING_(s, "\"hello\"");
  1447 TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
  1448   const char str_with_nul[] = "hello\0 world";
  1449   EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\"");
  1451   char mutable_str_with_nul[] = "hello\0 world";
  1452   EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\"");
  1455 #undef EXPECT_PRINT_TO_STRING_
  1457 TEST(UniversalTersePrintTest, WorksForNonReference) {
  1458   ::std::stringstream ss;
  1459   UniversalTersePrint(123, &ss);
  1460   EXPECT_EQ("123", ss.str());
  1463 TEST(UniversalTersePrintTest, WorksForReference) {
  1464   const int& n = 123;
  1465   ::std::stringstream ss;
  1466   UniversalTersePrint(n, &ss);
  1467   EXPECT_EQ("123", ss.str());
  1470 TEST(UniversalTersePrintTest, WorksForCString) {
  1471   const char* s1 = "abc";
  1472   ::std::stringstream ss1;
  1473   UniversalTersePrint(s1, &ss1);
  1474   EXPECT_EQ("\"abc\"", ss1.str());
  1476   char* s2 = const_cast<char*>(s1);
  1477   ::std::stringstream ss2;
  1478   UniversalTersePrint(s2, &ss2);
  1479   EXPECT_EQ("\"abc\"", ss2.str());
  1481   const char* s3 = NULL;
  1482   ::std::stringstream ss3;
  1483   UniversalTersePrint(s3, &ss3);
  1484   EXPECT_EQ("NULL", ss3.str());
  1487 TEST(UniversalPrintTest, WorksForNonReference) {
  1488   ::std::stringstream ss;
  1489   UniversalPrint(123, &ss);
  1490   EXPECT_EQ("123", ss.str());
  1493 TEST(UniversalPrintTest, WorksForReference) {
  1494   const int& n = 123;
  1495   ::std::stringstream ss;
  1496   UniversalPrint(n, &ss);
  1497   EXPECT_EQ("123", ss.str());
  1500 TEST(UniversalPrintTest, WorksForCString) {
  1501   const char* s1 = "abc";
  1502   ::std::stringstream ss1;
  1503   UniversalPrint(s1, &ss1);
  1504   EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str()));
  1506   char* s2 = const_cast<char*>(s1);
  1507   ::std::stringstream ss2;
  1508   UniversalPrint(s2, &ss2);
  1509   EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str()));
  1511   const char* s3 = NULL;
  1512   ::std::stringstream ss3;
  1513   UniversalPrint(s3, &ss3);
  1514   EXPECT_EQ("NULL", ss3.str());
  1517 TEST(UniversalPrintTest, WorksForCharArray) {
  1518   const char str[] = "\"Line\0 1\"\nLine 2";
  1519   ::std::stringstream ss1;
  1520   UniversalPrint(str, &ss1);
  1521   EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
  1523   const char mutable_str[] = "\"Line\0 1\"\nLine 2";
  1524   ::std::stringstream ss2;
  1525   UniversalPrint(mutable_str, &ss2);
  1526   EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
  1529 #if GTEST_HAS_TR1_TUPLE
  1531 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) {
  1532   Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple());
  1533   EXPECT_EQ(0u, result.size());
  1536 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) {
  1537   Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1));
  1538   ASSERT_EQ(1u, result.size());
  1539   EXPECT_EQ("1", result[0]);
  1542 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) {
  1543   Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a'));
  1544   ASSERT_EQ(2u, result.size());
  1545   EXPECT_EQ("1", result[0]);
  1546   EXPECT_EQ("'a' (97, 0x61)", result[1]);
  1549 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) {
  1550   const int n = 1;
  1551   Strings result = UniversalTersePrintTupleFieldsToStrings(
  1552       tuple<const int&, const char*>(n, "a"));
  1553   ASSERT_EQ(2u, result.size());
  1554   EXPECT_EQ("1", result[0]);
  1555   EXPECT_EQ("\"a\"", result[1]);
  1558 #endif  // GTEST_HAS_TR1_TUPLE
  1560 }  // namespace gtest_printers_test
  1561 }  // namespace testing

mercurial