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