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