michael@0: // Copyright 2007, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: wan@google.com (Zhanyong Wan) michael@0: michael@0: // Google Test - The Google C++ Testing Framework michael@0: // michael@0: // This file tests the universal value printer. michael@0: michael@0: #include "gtest/gtest-printers.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: // hash_map and hash_set are available under Visual C++. michael@0: #if _MSC_VER michael@0: # define GTEST_HAS_HASH_MAP_ 1 // Indicates that hash_map is available. michael@0: # include // NOLINT michael@0: # define GTEST_HAS_HASH_SET_ 1 // Indicates that hash_set is available. michael@0: # include // NOLINT michael@0: #endif // GTEST_OS_WINDOWS michael@0: michael@0: // Some user-defined types for testing the universal value printer. michael@0: michael@0: // An anonymous enum type. michael@0: enum AnonymousEnum { michael@0: kAE1 = -1, michael@0: kAE2 = 1 michael@0: }; michael@0: michael@0: // An enum without a user-defined printer. michael@0: enum EnumWithoutPrinter { michael@0: kEWP1 = -2, michael@0: kEWP2 = 42 michael@0: }; michael@0: michael@0: // An enum with a << operator. michael@0: enum EnumWithStreaming { michael@0: kEWS1 = 10 michael@0: }; michael@0: michael@0: std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) { michael@0: return os << (e == kEWS1 ? "kEWS1" : "invalid"); michael@0: } michael@0: michael@0: // An enum with a PrintTo() function. michael@0: enum EnumWithPrintTo { michael@0: kEWPT1 = 1 michael@0: }; michael@0: michael@0: void PrintTo(EnumWithPrintTo e, std::ostream* os) { michael@0: *os << (e == kEWPT1 ? "kEWPT1" : "invalid"); michael@0: } michael@0: michael@0: // A class implicitly convertible to BiggestInt. michael@0: class BiggestIntConvertible { michael@0: public: michael@0: operator ::testing::internal::BiggestInt() const { return 42; } michael@0: }; michael@0: michael@0: // A user-defined unprintable class template in the global namespace. michael@0: template michael@0: class UnprintableTemplateInGlobal { michael@0: public: michael@0: UnprintableTemplateInGlobal() : value_() {} michael@0: private: michael@0: T value_; michael@0: }; michael@0: michael@0: // A user-defined streamable type in the global namespace. michael@0: class StreamableInGlobal { michael@0: public: michael@0: virtual ~StreamableInGlobal() {} michael@0: }; michael@0: michael@0: inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) { michael@0: os << "StreamableInGlobal"; michael@0: } michael@0: michael@0: void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) { michael@0: os << "StreamableInGlobal*"; michael@0: } michael@0: michael@0: namespace foo { michael@0: michael@0: // A user-defined unprintable type in a user namespace. michael@0: class UnprintableInFoo { michael@0: public: michael@0: UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); } michael@0: private: michael@0: char xy_[8]; michael@0: double z_; michael@0: }; michael@0: michael@0: // A user-defined printable type in a user-chosen namespace. michael@0: struct PrintableViaPrintTo { michael@0: PrintableViaPrintTo() : value() {} michael@0: int value; michael@0: }; michael@0: michael@0: void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) { michael@0: *os << "PrintableViaPrintTo: " << x.value; michael@0: } michael@0: michael@0: // A type with a user-defined << for printing its pointer. michael@0: struct PointerPrintable { michael@0: }; michael@0: michael@0: ::std::ostream& operator<<(::std::ostream& os, michael@0: const PointerPrintable* /* x */) { michael@0: return os << "PointerPrintable*"; michael@0: } michael@0: michael@0: // A user-defined printable class template in a user-chosen namespace. michael@0: template michael@0: class PrintableViaPrintToTemplate { michael@0: public: michael@0: explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {} michael@0: michael@0: const T& value() const { return value_; } michael@0: private: michael@0: T value_; michael@0: }; michael@0: michael@0: template michael@0: void PrintTo(const PrintableViaPrintToTemplate& x, ::std::ostream* os) { michael@0: *os << "PrintableViaPrintToTemplate: " << x.value(); michael@0: } michael@0: michael@0: // A user-defined streamable class template in a user namespace. michael@0: template michael@0: class StreamableTemplateInFoo { michael@0: public: michael@0: StreamableTemplateInFoo() : value_() {} michael@0: michael@0: const T& value() const { return value_; } michael@0: private: michael@0: T value_; michael@0: }; michael@0: michael@0: template michael@0: inline ::std::ostream& operator<<(::std::ostream& os, michael@0: const StreamableTemplateInFoo& x) { michael@0: return os << "StreamableTemplateInFoo: " << x.value(); michael@0: } michael@0: michael@0: } // namespace foo michael@0: michael@0: namespace testing { michael@0: namespace gtest_printers_test { michael@0: michael@0: using ::std::deque; michael@0: using ::std::list; michael@0: using ::std::make_pair; michael@0: using ::std::map; michael@0: using ::std::multimap; michael@0: using ::std::multiset; michael@0: using ::std::pair; michael@0: using ::std::set; michael@0: using ::std::vector; michael@0: using ::testing::PrintToString; michael@0: using ::testing::internal::FormatForComparisonFailureMessage; michael@0: using ::testing::internal::ImplicitCast_; michael@0: using ::testing::internal::NativeArray; michael@0: using ::testing::internal::RE; michael@0: using ::testing::internal::Strings; michael@0: using ::testing::internal::UniversalPrint; michael@0: using ::testing::internal::UniversalPrinter; michael@0: using ::testing::internal::UniversalTersePrint; michael@0: using ::testing::internal::UniversalTersePrintTupleFieldsToStrings; michael@0: using ::testing::internal::kReference; michael@0: using ::testing::internal::string; michael@0: michael@0: #if GTEST_HAS_TR1_TUPLE michael@0: using ::std::tr1::make_tuple; michael@0: using ::std::tr1::tuple; michael@0: #endif michael@0: michael@0: #if _MSC_VER michael@0: // MSVC defines the following classes in the ::stdext namespace while michael@0: // gcc defines them in the :: namespace. Note that they are not part michael@0: // of the C++ standard. michael@0: using ::stdext::hash_map; michael@0: using ::stdext::hash_set; michael@0: using ::stdext::hash_multimap; michael@0: using ::stdext::hash_multiset; michael@0: #endif michael@0: michael@0: // Prints a value to a string using the universal value printer. This michael@0: // is a helper for testing UniversalPrinter::Print() for various types. michael@0: template michael@0: string Print(const T& value) { michael@0: ::std::stringstream ss; michael@0: UniversalPrinter::Print(value, &ss); michael@0: return ss.str(); michael@0: } michael@0: michael@0: // Prints a value passed by reference to a string, using the universal michael@0: // value printer. This is a helper for testing michael@0: // UniversalPrinter::Print() for various types. michael@0: template michael@0: string PrintByRef(const T& value) { michael@0: ::std::stringstream ss; michael@0: UniversalPrinter::Print(value, &ss); michael@0: return ss.str(); michael@0: } michael@0: michael@0: // Tests printing various enum types. michael@0: michael@0: TEST(PrintEnumTest, AnonymousEnum) { michael@0: EXPECT_EQ("-1", Print(kAE1)); michael@0: EXPECT_EQ("1", Print(kAE2)); michael@0: } michael@0: michael@0: TEST(PrintEnumTest, EnumWithoutPrinter) { michael@0: EXPECT_EQ("-2", Print(kEWP1)); michael@0: EXPECT_EQ("42", Print(kEWP2)); michael@0: } michael@0: michael@0: TEST(PrintEnumTest, EnumWithStreaming) { michael@0: EXPECT_EQ("kEWS1", Print(kEWS1)); michael@0: EXPECT_EQ("invalid", Print(static_cast(0))); michael@0: } michael@0: michael@0: TEST(PrintEnumTest, EnumWithPrintTo) { michael@0: EXPECT_EQ("kEWPT1", Print(kEWPT1)); michael@0: EXPECT_EQ("invalid", Print(static_cast(0))); michael@0: } michael@0: michael@0: // Tests printing a class implicitly convertible to BiggestInt. michael@0: michael@0: TEST(PrintClassTest, BiggestIntConvertible) { michael@0: EXPECT_EQ("42", Print(BiggestIntConvertible())); michael@0: } michael@0: michael@0: // Tests printing various char types. michael@0: michael@0: // char. michael@0: TEST(PrintCharTest, PlainChar) { michael@0: EXPECT_EQ("'\\0'", Print('\0')); michael@0: EXPECT_EQ("'\\'' (39, 0x27)", Print('\'')); michael@0: EXPECT_EQ("'\"' (34, 0x22)", Print('"')); michael@0: EXPECT_EQ("'?' (63, 0x3F)", Print('?')); michael@0: EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\')); michael@0: EXPECT_EQ("'\\a' (7)", Print('\a')); michael@0: EXPECT_EQ("'\\b' (8)", Print('\b')); michael@0: EXPECT_EQ("'\\f' (12, 0xC)", Print('\f')); michael@0: EXPECT_EQ("'\\n' (10, 0xA)", Print('\n')); michael@0: EXPECT_EQ("'\\r' (13, 0xD)", Print('\r')); michael@0: EXPECT_EQ("'\\t' (9)", Print('\t')); michael@0: EXPECT_EQ("'\\v' (11, 0xB)", Print('\v')); michael@0: EXPECT_EQ("'\\x7F' (127)", Print('\x7F')); michael@0: EXPECT_EQ("'\\xFF' (255)", Print('\xFF')); michael@0: EXPECT_EQ("' ' (32, 0x20)", Print(' ')); michael@0: EXPECT_EQ("'a' (97, 0x61)", Print('a')); michael@0: } michael@0: michael@0: // signed char. michael@0: TEST(PrintCharTest, SignedChar) { michael@0: EXPECT_EQ("'\\0'", Print(static_cast('\0'))); michael@0: EXPECT_EQ("'\\xCE' (-50)", michael@0: Print(static_cast(-50))); michael@0: } michael@0: michael@0: // unsigned char. michael@0: TEST(PrintCharTest, UnsignedChar) { michael@0: EXPECT_EQ("'\\0'", Print(static_cast('\0'))); michael@0: EXPECT_EQ("'b' (98, 0x62)", michael@0: Print(static_cast('b'))); michael@0: } michael@0: michael@0: // Tests printing other simple, built-in types. michael@0: michael@0: // bool. michael@0: TEST(PrintBuiltInTypeTest, Bool) { michael@0: EXPECT_EQ("false", Print(false)); michael@0: EXPECT_EQ("true", Print(true)); michael@0: } michael@0: michael@0: // wchar_t. michael@0: TEST(PrintBuiltInTypeTest, Wchar_t) { michael@0: EXPECT_EQ("L'\\0'", Print(L'\0')); michael@0: EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\'')); michael@0: EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"')); michael@0: EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?')); michael@0: EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\')); michael@0: EXPECT_EQ("L'\\a' (7)", Print(L'\a')); michael@0: EXPECT_EQ("L'\\b' (8)", Print(L'\b')); michael@0: EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f')); michael@0: EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n')); michael@0: EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r')); michael@0: EXPECT_EQ("L'\\t' (9)", Print(L'\t')); michael@0: EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v')); michael@0: EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F')); michael@0: EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF')); michael@0: EXPECT_EQ("L' ' (32, 0x20)", Print(L' ')); michael@0: EXPECT_EQ("L'a' (97, 0x61)", Print(L'a')); michael@0: EXPECT_EQ("L'\\x576' (1398)", Print(static_cast(0x576))); michael@0: EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast(0xC74D))); michael@0: } michael@0: michael@0: // Test that Int64 provides more storage than wchar_t. michael@0: TEST(PrintTypeSizeTest, Wchar_t) { michael@0: EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64)); michael@0: } michael@0: michael@0: // Various integer types. michael@0: TEST(PrintBuiltInTypeTest, Integer) { michael@0: EXPECT_EQ("'\\xFF' (255)", Print(static_cast(255))); // uint8 michael@0: EXPECT_EQ("'\\x80' (-128)", Print(static_cast(-128))); // int8 michael@0: EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16 michael@0: EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16 michael@0: EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32 michael@0: EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32 michael@0: EXPECT_EQ("18446744073709551615", michael@0: Print(static_cast(-1))); // uint64 michael@0: EXPECT_EQ("-9223372036854775808", michael@0: Print(static_cast(1) << 63)); // int64 michael@0: } michael@0: michael@0: // Size types. michael@0: TEST(PrintBuiltInTypeTest, Size_t) { michael@0: EXPECT_EQ("1", Print(sizeof('a'))); // size_t. michael@0: #if !GTEST_OS_WINDOWS michael@0: // Windows has no ssize_t type. michael@0: EXPECT_EQ("-2", Print(static_cast(-2))); // ssize_t. michael@0: #endif // !GTEST_OS_WINDOWS michael@0: } michael@0: michael@0: // Floating-points. michael@0: TEST(PrintBuiltInTypeTest, FloatingPoints) { michael@0: EXPECT_EQ("1.5", Print(1.5f)); // float michael@0: EXPECT_EQ("-2.5", Print(-2.5)); // double michael@0: } michael@0: michael@0: // Since ::std::stringstream::operator<<(const void *) formats the pointer michael@0: // output differently with different compilers, we have to create the expected michael@0: // output first and use it as our expectation. michael@0: static string PrintPointer(const void *p) { michael@0: ::std::stringstream expected_result_stream; michael@0: expected_result_stream << p; michael@0: return expected_result_stream.str(); michael@0: } michael@0: michael@0: // Tests printing C strings. michael@0: michael@0: // const char*. michael@0: TEST(PrintCStringTest, Const) { michael@0: const char* p = "World"; michael@0: EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p)); michael@0: } michael@0: michael@0: // char*. michael@0: TEST(PrintCStringTest, NonConst) { michael@0: char p[] = "Hi"; michael@0: EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"", michael@0: Print(static_cast(p))); michael@0: } michael@0: michael@0: // NULL C string. michael@0: TEST(PrintCStringTest, Null) { michael@0: const char* p = NULL; michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // Tests that C strings are escaped properly. michael@0: TEST(PrintCStringTest, EscapesProperly) { michael@0: const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a"; michael@0: EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f" michael@0: "\\n\\r\\t\\v\\x7F\\xFF a\"", michael@0: Print(p)); michael@0: } michael@0: michael@0: michael@0: michael@0: // MSVC compiler can be configured to define whar_t as a typedef michael@0: // of unsigned short. Defining an overload for const wchar_t* in that case michael@0: // would cause pointers to unsigned shorts be printed as wide strings, michael@0: // possibly accessing more memory than intended and causing invalid michael@0: // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when michael@0: // wchar_t is implemented as a native type. michael@0: #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) michael@0: michael@0: // const wchar_t*. michael@0: TEST(PrintWideCStringTest, Const) { michael@0: const wchar_t* p = L"World"; michael@0: EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p)); michael@0: } michael@0: michael@0: // wchar_t*. michael@0: TEST(PrintWideCStringTest, NonConst) { michael@0: wchar_t p[] = L"Hi"; michael@0: EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"", michael@0: Print(static_cast(p))); michael@0: } michael@0: michael@0: // NULL wide C string. michael@0: TEST(PrintWideCStringTest, Null) { michael@0: const wchar_t* p = NULL; michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // Tests that wide C strings are escaped properly. michael@0: TEST(PrintWideCStringTest, EscapesProperly) { michael@0: const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r', michael@0: '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'}; michael@0: EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f" michael@0: "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"", michael@0: Print(static_cast(s))); michael@0: } michael@0: #endif // native wchar_t michael@0: michael@0: // Tests printing pointers to other char types. michael@0: michael@0: // signed char*. michael@0: TEST(PrintCharPointerTest, SignedChar) { michael@0: signed char* p = reinterpret_cast(0x1234); michael@0: EXPECT_EQ(PrintPointer(p), Print(p)); michael@0: p = NULL; michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // const signed char*. michael@0: TEST(PrintCharPointerTest, ConstSignedChar) { michael@0: signed char* p = reinterpret_cast(0x1234); michael@0: EXPECT_EQ(PrintPointer(p), Print(p)); michael@0: p = NULL; michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // unsigned char*. michael@0: TEST(PrintCharPointerTest, UnsignedChar) { michael@0: unsigned char* p = reinterpret_cast(0x1234); michael@0: EXPECT_EQ(PrintPointer(p), Print(p)); michael@0: p = NULL; michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // const unsigned char*. michael@0: TEST(PrintCharPointerTest, ConstUnsignedChar) { michael@0: const unsigned char* p = reinterpret_cast(0x1234); michael@0: EXPECT_EQ(PrintPointer(p), Print(p)); michael@0: p = NULL; michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // Tests printing pointers to simple, built-in types. michael@0: michael@0: // bool*. michael@0: TEST(PrintPointerToBuiltInTypeTest, Bool) { michael@0: bool* p = reinterpret_cast(0xABCD); michael@0: EXPECT_EQ(PrintPointer(p), Print(p)); michael@0: p = NULL; michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // void*. michael@0: TEST(PrintPointerToBuiltInTypeTest, Void) { michael@0: void* p = reinterpret_cast(0xABCD); michael@0: EXPECT_EQ(PrintPointer(p), Print(p)); michael@0: p = NULL; michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // const void*. michael@0: TEST(PrintPointerToBuiltInTypeTest, ConstVoid) { michael@0: const void* p = reinterpret_cast(0xABCD); michael@0: EXPECT_EQ(PrintPointer(p), Print(p)); michael@0: p = NULL; michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // Tests printing pointers to pointers. michael@0: TEST(PrintPointerToPointerTest, IntPointerPointer) { michael@0: int** p = reinterpret_cast(0xABCD); michael@0: EXPECT_EQ(PrintPointer(p), Print(p)); michael@0: p = NULL; michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // Tests printing (non-member) function pointers. michael@0: michael@0: void MyFunction(int /* n */) {} michael@0: michael@0: TEST(PrintPointerTest, NonMemberFunctionPointer) { michael@0: // We cannot directly cast &MyFunction to const void* because the michael@0: // standard disallows casting between pointers to functions and michael@0: // pointers to objects, and some compilers (e.g. GCC 3.4) enforce michael@0: // this limitation. michael@0: EXPECT_EQ( michael@0: PrintPointer(reinterpret_cast( michael@0: reinterpret_cast(&MyFunction))), michael@0: Print(&MyFunction)); michael@0: int (*p)(bool) = NULL; // NOLINT michael@0: EXPECT_EQ("NULL", Print(p)); michael@0: } michael@0: michael@0: // An assertion predicate determining whether a one string is a prefix for michael@0: // another. michael@0: template michael@0: AssertionResult HasPrefix(const StringType& str, const StringType& prefix) { michael@0: if (str.find(prefix, 0) == 0) michael@0: return AssertionSuccess(); michael@0: michael@0: const bool is_wide_string = sizeof(prefix[0]) > 1; michael@0: const char* const begin_string_quote = is_wide_string ? "L\"" : "\""; michael@0: return AssertionFailure() michael@0: << begin_string_quote << prefix << "\" is not a prefix of " michael@0: << begin_string_quote << str << "\"\n"; michael@0: } michael@0: michael@0: // Tests printing member variable pointers. Although they are called michael@0: // pointers, they don't point to a location in the address space. michael@0: // Their representation is implementation-defined. Thus they will be michael@0: // printed as raw bytes. michael@0: michael@0: struct Foo { michael@0: public: michael@0: virtual ~Foo() {} michael@0: int MyMethod(char x) { return x + 1; } michael@0: virtual char MyVirtualMethod(int /* n */) { return 'a'; } michael@0: michael@0: int value; michael@0: }; michael@0: michael@0: TEST(PrintPointerTest, MemberVariablePointer) { michael@0: EXPECT_TRUE(HasPrefix(Print(&Foo::value), michael@0: Print(sizeof(&Foo::value)) + "-byte object ")); michael@0: int (Foo::*p) = NULL; // NOLINT michael@0: EXPECT_TRUE(HasPrefix(Print(p), michael@0: Print(sizeof(p)) + "-byte object ")); michael@0: } michael@0: michael@0: // Tests printing member function pointers. Although they are called michael@0: // pointers, they don't point to a location in the address space. michael@0: // Their representation is implementation-defined. Thus they will be michael@0: // printed as raw bytes. michael@0: TEST(PrintPointerTest, MemberFunctionPointer) { michael@0: EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod), michael@0: Print(sizeof(&Foo::MyMethod)) + "-byte object ")); michael@0: EXPECT_TRUE( michael@0: HasPrefix(Print(&Foo::MyVirtualMethod), michael@0: Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object ")); michael@0: int (Foo::*p)(char) = NULL; // NOLINT michael@0: EXPECT_TRUE(HasPrefix(Print(p), michael@0: Print(sizeof(p)) + "-byte object ")); michael@0: } michael@0: michael@0: // Tests printing C arrays. michael@0: michael@0: // The difference between this and Print() is that it ensures that the michael@0: // argument is a reference to an array. michael@0: template michael@0: string PrintArrayHelper(T (&a)[N]) { michael@0: return Print(a); michael@0: } michael@0: michael@0: // One-dimensional array. michael@0: TEST(PrintArrayTest, OneDimensionalArray) { michael@0: int a[5] = { 1, 2, 3, 4, 5 }; michael@0: EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a)); michael@0: } michael@0: michael@0: // Two-dimensional array. michael@0: TEST(PrintArrayTest, TwoDimensionalArray) { michael@0: int a[2][5] = { michael@0: { 1, 2, 3, 4, 5 }, michael@0: { 6, 7, 8, 9, 0 } michael@0: }; michael@0: EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a)); michael@0: } michael@0: michael@0: // Array of const elements. michael@0: TEST(PrintArrayTest, ConstArray) { michael@0: const bool a[1] = { false }; michael@0: EXPECT_EQ("{ false }", PrintArrayHelper(a)); michael@0: } michael@0: michael@0: // char array without terminating NUL. michael@0: TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) { michael@0: // Array a contains '\0' in the middle and doesn't end with '\0'. michael@0: char a[] = { 'H', '\0', 'i' }; michael@0: EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a)); michael@0: } michael@0: michael@0: // const char array with terminating NUL. michael@0: TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) { michael@0: const char a[] = "\0Hi"; michael@0: EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a)); michael@0: } michael@0: michael@0: // const wchar_t array without terminating NUL. michael@0: TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) { michael@0: // Array a contains '\0' in the middle and doesn't end with '\0'. michael@0: const wchar_t a[] = { L'H', L'\0', L'i' }; michael@0: EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a)); michael@0: } michael@0: michael@0: // wchar_t array with terminating NUL. michael@0: TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) { michael@0: const wchar_t a[] = L"\0Hi"; michael@0: EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a)); michael@0: } michael@0: michael@0: // Array of objects. michael@0: TEST(PrintArrayTest, ObjectArray) { michael@0: string a[3] = { "Hi", "Hello", "Ni hao" }; michael@0: EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a)); michael@0: } michael@0: michael@0: // Array with many elements. michael@0: TEST(PrintArrayTest, BigArray) { michael@0: int a[100] = { 1, 2, 3 }; michael@0: EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }", michael@0: PrintArrayHelper(a)); michael@0: } michael@0: michael@0: // Tests printing ::string and ::std::string. michael@0: michael@0: #if GTEST_HAS_GLOBAL_STRING michael@0: // ::string. michael@0: TEST(PrintStringTest, StringInGlobalNamespace) { michael@0: const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; michael@0: const ::string str(s, sizeof(s)); michael@0: EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", michael@0: Print(str)); michael@0: } michael@0: #endif // GTEST_HAS_GLOBAL_STRING michael@0: michael@0: // ::std::string. michael@0: TEST(PrintStringTest, StringInStdNamespace) { michael@0: const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; michael@0: const ::std::string str(s, sizeof(s)); michael@0: EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", michael@0: Print(str)); michael@0: } michael@0: michael@0: TEST(PrintStringTest, StringAmbiguousHex) { michael@0: // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of: michael@0: // '\x6', '\x6B', or '\x6BA'. michael@0: michael@0: // a hex escaping sequence following by a decimal digit michael@0: EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3"))); michael@0: // a hex escaping sequence following by a hex digit (lower-case) michael@0: EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas"))); michael@0: // a hex escaping sequence following by a hex digit (upper-case) michael@0: EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA"))); michael@0: // a hex escaping sequence following by a non-xdigit michael@0: EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!"))); michael@0: } michael@0: michael@0: // Tests printing ::wstring and ::std::wstring. michael@0: michael@0: #if GTEST_HAS_GLOBAL_WSTRING michael@0: // ::wstring. michael@0: TEST(PrintWideStringTest, StringInGlobalNamespace) { michael@0: const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; michael@0: const ::wstring str(s, sizeof(s)/sizeof(wchar_t)); michael@0: EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" michael@0: "\\xD3\\x576\\x8D3\\xC74D a\\0\"", michael@0: Print(str)); michael@0: } michael@0: #endif // GTEST_HAS_GLOBAL_WSTRING michael@0: michael@0: #if GTEST_HAS_STD_WSTRING michael@0: // ::std::wstring. michael@0: TEST(PrintWideStringTest, StringInStdNamespace) { michael@0: const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; michael@0: const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t)); michael@0: EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" michael@0: "\\xD3\\x576\\x8D3\\xC74D a\\0\"", michael@0: Print(str)); michael@0: } michael@0: michael@0: TEST(PrintWideStringTest, StringAmbiguousHex) { michael@0: // same for wide strings. michael@0: EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3"))); michael@0: EXPECT_EQ("L\"mm\\x6\" L\"bananas\"", michael@0: Print(::std::wstring(L"mm\x6" L"bananas"))); michael@0: EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"", michael@0: Print(::std::wstring(L"NOM\x6" L"BANANA"))); michael@0: EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!"))); michael@0: } michael@0: #endif // GTEST_HAS_STD_WSTRING michael@0: michael@0: // Tests printing types that support generic streaming (i.e. streaming michael@0: // to std::basic_ostream for any valid Char and michael@0: // CharTraits types). michael@0: michael@0: // Tests printing a non-template type that supports generic streaming. michael@0: michael@0: class AllowsGenericStreaming {}; michael@0: michael@0: template michael@0: std::basic_ostream& operator<<( michael@0: std::basic_ostream& os, michael@0: const AllowsGenericStreaming& /* a */) { michael@0: return os << "AllowsGenericStreaming"; michael@0: } michael@0: michael@0: TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) { michael@0: AllowsGenericStreaming a; michael@0: EXPECT_EQ("AllowsGenericStreaming", Print(a)); michael@0: } michael@0: michael@0: // Tests printing a template type that supports generic streaming. michael@0: michael@0: template michael@0: class AllowsGenericStreamingTemplate {}; michael@0: michael@0: template michael@0: std::basic_ostream& operator<<( michael@0: std::basic_ostream& os, michael@0: const AllowsGenericStreamingTemplate& /* a */) { michael@0: return os << "AllowsGenericStreamingTemplate"; michael@0: } michael@0: michael@0: TEST(PrintTypeWithGenericStreamingTest, TemplateType) { michael@0: AllowsGenericStreamingTemplate a; michael@0: EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a)); michael@0: } michael@0: michael@0: // Tests printing a type that supports generic streaming and can be michael@0: // implicitly converted to another printable type. michael@0: michael@0: template michael@0: class AllowsGenericStreamingAndImplicitConversionTemplate { michael@0: public: michael@0: operator bool() const { return false; } michael@0: }; michael@0: michael@0: template michael@0: std::basic_ostream& operator<<( michael@0: std::basic_ostream& os, michael@0: const AllowsGenericStreamingAndImplicitConversionTemplate& /* a */) { michael@0: return os << "AllowsGenericStreamingAndImplicitConversionTemplate"; michael@0: } michael@0: michael@0: TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) { michael@0: AllowsGenericStreamingAndImplicitConversionTemplate a; michael@0: EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a)); michael@0: } michael@0: michael@0: #if GTEST_HAS_STRING_PIECE_ michael@0: michael@0: // Tests printing StringPiece. michael@0: michael@0: TEST(PrintStringPieceTest, SimpleStringPiece) { michael@0: const StringPiece sp = "Hello"; michael@0: EXPECT_EQ("\"Hello\"", Print(sp)); michael@0: } michael@0: michael@0: TEST(PrintStringPieceTest, UnprintableCharacters) { michael@0: const char str[] = "NUL (\0) and \r\t"; michael@0: const StringPiece sp(str, sizeof(str) - 1); michael@0: EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp)); michael@0: } michael@0: michael@0: #endif // GTEST_HAS_STRING_PIECE_ michael@0: michael@0: // Tests printing STL containers. michael@0: michael@0: TEST(PrintStlContainerTest, EmptyDeque) { michael@0: deque empty; michael@0: EXPECT_EQ("{}", Print(empty)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, NonEmptyDeque) { michael@0: deque non_empty; michael@0: non_empty.push_back(1); michael@0: non_empty.push_back(3); michael@0: EXPECT_EQ("{ 1, 3 }", Print(non_empty)); michael@0: } michael@0: michael@0: #if GTEST_HAS_HASH_MAP_ michael@0: michael@0: TEST(PrintStlContainerTest, OneElementHashMap) { michael@0: hash_map map1; michael@0: map1[1] = 'a'; michael@0: EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, HashMultiMap) { michael@0: hash_multimap map1; michael@0: map1.insert(make_pair(5, true)); michael@0: map1.insert(make_pair(5, false)); michael@0: michael@0: // Elements of hash_multimap can be printed in any order. michael@0: const string result = Print(map1); michael@0: EXPECT_TRUE(result == "{ (5, true), (5, false) }" || michael@0: result == "{ (5, false), (5, true) }") michael@0: << " where Print(map1) returns \"" << result << "\"."; michael@0: } michael@0: michael@0: #endif // GTEST_HAS_HASH_MAP_ michael@0: michael@0: #if GTEST_HAS_HASH_SET_ michael@0: michael@0: TEST(PrintStlContainerTest, HashSet) { michael@0: hash_set set1; michael@0: set1.insert("hello"); michael@0: EXPECT_EQ("{ \"hello\" }", Print(set1)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, HashMultiSet) { michael@0: const int kSize = 5; michael@0: int a[kSize] = { 1, 1, 2, 5, 1 }; michael@0: hash_multiset set1(a, a + kSize); michael@0: michael@0: // Elements of hash_multiset can be printed in any order. michael@0: const string result = Print(set1); michael@0: const string expected_pattern = "{ d, d, d, d, d }"; // d means a digit. michael@0: michael@0: // Verifies the result matches the expected pattern; also extracts michael@0: // the numbers in the result. michael@0: ASSERT_EQ(expected_pattern.length(), result.length()); michael@0: std::vector numbers; michael@0: for (size_t i = 0; i != result.length(); i++) { michael@0: if (expected_pattern[i] == 'd') { michael@0: ASSERT_NE(isdigit(static_cast(result[i])), 0); michael@0: numbers.push_back(result[i] - '0'); michael@0: } else { michael@0: EXPECT_EQ(expected_pattern[i], result[i]) << " where result is " michael@0: << result; michael@0: } michael@0: } michael@0: michael@0: // Makes sure the result contains the right numbers. michael@0: std::sort(numbers.begin(), numbers.end()); michael@0: std::sort(a, a + kSize); michael@0: EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin())); michael@0: } michael@0: michael@0: #endif // GTEST_HAS_HASH_SET_ michael@0: michael@0: TEST(PrintStlContainerTest, List) { michael@0: const string a[] = { michael@0: "hello", michael@0: "world" michael@0: }; michael@0: const list strings(a, a + 2); michael@0: EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, Map) { michael@0: map map1; michael@0: map1[1] = true; michael@0: map1[5] = false; michael@0: map1[3] = true; michael@0: EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, MultiMap) { michael@0: multimap map1; michael@0: // The make_pair template function would deduce the type as michael@0: // pair here, and since the key part in a multimap has to michael@0: // be constant, without a templated ctor in the pair class (as in michael@0: // libCstd on Solaris), make_pair call would fail to compile as no michael@0: // implicit conversion is found. Thus explicit typename is used michael@0: // here instead. michael@0: map1.insert(pair(true, 0)); michael@0: map1.insert(pair(true, 1)); michael@0: map1.insert(pair(false, 2)); michael@0: EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, Set) { michael@0: const unsigned int a[] = { 3, 0, 5 }; michael@0: set set1(a, a + 3); michael@0: EXPECT_EQ("{ 0, 3, 5 }", Print(set1)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, MultiSet) { michael@0: const int a[] = { 1, 1, 2, 5, 1 }; michael@0: multiset set1(a, a + 5); michael@0: EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, Pair) { michael@0: pair p(true, 5); michael@0: EXPECT_EQ("(true, 5)", Print(p)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, Vector) { michael@0: vector v; michael@0: v.push_back(1); michael@0: v.push_back(2); michael@0: EXPECT_EQ("{ 1, 2 }", Print(v)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, LongSequence) { michael@0: const int a[100] = { 1, 2, 3 }; michael@0: const vector v(a, a + 100); michael@0: EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " michael@0: "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, NestedContainer) { michael@0: const int a1[] = { 1, 2 }; michael@0: const int a2[] = { 3, 4, 5 }; michael@0: const list l1(a1, a1 + 2); michael@0: const list l2(a2, a2 + 3); michael@0: michael@0: vector > v; michael@0: v.push_back(l1); michael@0: v.push_back(l2); michael@0: EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, OneDimensionalNativeArray) { michael@0: const int a[3] = { 1, 2, 3 }; michael@0: NativeArray b(a, 3, kReference); michael@0: EXPECT_EQ("{ 1, 2, 3 }", Print(b)); michael@0: } michael@0: michael@0: TEST(PrintStlContainerTest, TwoDimensionalNativeArray) { michael@0: const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; michael@0: NativeArray b(a, 2, kReference); michael@0: EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b)); michael@0: } michael@0: michael@0: // Tests that a class named iterator isn't treated as a container. michael@0: michael@0: struct iterator { michael@0: char x; michael@0: }; michael@0: michael@0: TEST(PrintStlContainerTest, Iterator) { michael@0: iterator it = {}; michael@0: EXPECT_EQ("1-byte object <00>", Print(it)); michael@0: } michael@0: michael@0: // Tests that a class named const_iterator isn't treated as a container. michael@0: michael@0: struct const_iterator { michael@0: char x; michael@0: }; michael@0: michael@0: TEST(PrintStlContainerTest, ConstIterator) { michael@0: const_iterator it = {}; michael@0: EXPECT_EQ("1-byte object <00>", Print(it)); michael@0: } michael@0: michael@0: #if GTEST_HAS_TR1_TUPLE michael@0: // Tests printing tuples. michael@0: michael@0: // Tuples of various arities. michael@0: TEST(PrintTupleTest, VariousSizes) { michael@0: tuple<> t0; michael@0: EXPECT_EQ("()", Print(t0)); michael@0: michael@0: tuple t1(5); michael@0: EXPECT_EQ("(5)", Print(t1)); michael@0: michael@0: tuple t2('a', true); michael@0: EXPECT_EQ("('a' (97, 0x61), true)", Print(t2)); michael@0: michael@0: tuple t3(false, 2, 3); michael@0: EXPECT_EQ("(false, 2, 3)", Print(t3)); michael@0: michael@0: tuple t4(false, 2, 3, 4); michael@0: EXPECT_EQ("(false, 2, 3, 4)", Print(t4)); michael@0: michael@0: tuple t5(false, 2, 3, 4, true); michael@0: EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5)); michael@0: michael@0: tuple t6(false, 2, 3, 4, true, 6); michael@0: EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6)); michael@0: michael@0: tuple t7(false, 2, 3, 4, true, 6, 7); michael@0: EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7)); michael@0: michael@0: tuple t8( michael@0: false, 2, 3, 4, true, 6, 7, true); michael@0: EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8)); michael@0: michael@0: tuple t9( michael@0: false, 2, 3, 4, true, 6, 7, true, 9); michael@0: EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9)); michael@0: michael@0: const char* const str = "8"; michael@0: // VC++ 2010's implementation of tuple of C++0x is deficient, requiring michael@0: // an explicit type cast of NULL to be used. michael@0: tuple michael@0: t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, michael@0: ImplicitCast_(NULL), "10"); michael@0: EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) + michael@0: " pointing to \"8\", NULL, \"10\")", michael@0: Print(t10)); michael@0: } michael@0: michael@0: // Nested tuples. michael@0: TEST(PrintTupleTest, NestedTuple) { michael@0: tuple, char> nested(make_tuple(5, true), 'a'); michael@0: EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested)); michael@0: } michael@0: michael@0: #endif // GTEST_HAS_TR1_TUPLE michael@0: michael@0: // Tests printing user-defined unprintable types. michael@0: michael@0: // Unprintable types in the global namespace. michael@0: TEST(PrintUnprintableTypeTest, InGlobalNamespace) { michael@0: EXPECT_EQ("1-byte object <00>", michael@0: Print(UnprintableTemplateInGlobal())); michael@0: } michael@0: michael@0: // Unprintable types in a user namespace. michael@0: TEST(PrintUnprintableTypeTest, InUserNamespace) { michael@0: EXPECT_EQ("16-byte object ", michael@0: Print(::foo::UnprintableInFoo())); michael@0: } michael@0: michael@0: // Unprintable types are that too big to be printed completely. michael@0: michael@0: struct Big { michael@0: Big() { memset(array, 0, sizeof(array)); } michael@0: char array[257]; michael@0: }; michael@0: michael@0: TEST(PrintUnpritableTypeTest, BigObject) { michael@0: EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 " michael@0: "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " michael@0: "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " michael@0: "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 " michael@0: "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " michael@0: "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " michael@0: "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>", michael@0: Print(Big())); michael@0: } michael@0: michael@0: // Tests printing user-defined streamable types. michael@0: michael@0: // Streamable types in the global namespace. michael@0: TEST(PrintStreamableTypeTest, InGlobalNamespace) { michael@0: StreamableInGlobal x; michael@0: EXPECT_EQ("StreamableInGlobal", Print(x)); michael@0: EXPECT_EQ("StreamableInGlobal*", Print(&x)); michael@0: } michael@0: michael@0: // Printable template types in a user namespace. michael@0: TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) { michael@0: EXPECT_EQ("StreamableTemplateInFoo: 0", michael@0: Print(::foo::StreamableTemplateInFoo())); michael@0: } michael@0: michael@0: // Tests printing user-defined types that have a PrintTo() function. michael@0: TEST(PrintPrintableTypeTest, InUserNamespace) { michael@0: EXPECT_EQ("PrintableViaPrintTo: 0", michael@0: Print(::foo::PrintableViaPrintTo())); michael@0: } michael@0: michael@0: // Tests printing a pointer to a user-defined type that has a << michael@0: // operator for its pointer. michael@0: TEST(PrintPrintableTypeTest, PointerInUserNamespace) { michael@0: ::foo::PointerPrintable x; michael@0: EXPECT_EQ("PointerPrintable*", Print(&x)); michael@0: } michael@0: michael@0: // Tests printing user-defined class template that have a PrintTo() function. michael@0: TEST(PrintPrintableTypeTest, TemplateInUserNamespace) { michael@0: EXPECT_EQ("PrintableViaPrintToTemplate: 5", michael@0: Print(::foo::PrintableViaPrintToTemplate(5))); michael@0: } michael@0: michael@0: #if GTEST_HAS_PROTOBUF_ michael@0: michael@0: // Tests printing a protocol message. michael@0: TEST(PrintProtocolMessageTest, PrintsShortDebugString) { michael@0: testing::internal::TestMessage msg; michael@0: msg.set_member("yes"); michael@0: EXPECT_EQ("", Print(msg)); michael@0: } michael@0: michael@0: // Tests printing a short proto2 message. michael@0: TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) { michael@0: testing::internal::FooMessage msg; michael@0: msg.set_int_field(2); michael@0: msg.set_string_field("hello"); michael@0: EXPECT_PRED2(RE::FullMatch, Print(msg), michael@0: ""); michael@0: } michael@0: michael@0: // Tests printing a long proto2 message. michael@0: TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) { michael@0: testing::internal::FooMessage msg; michael@0: msg.set_int_field(2); michael@0: msg.set_string_field("hello"); michael@0: msg.add_names("peter"); michael@0: msg.add_names("paul"); michael@0: msg.add_names("mary"); michael@0: EXPECT_PRED2(RE::FullMatch, Print(msg), michael@0: "<\n" michael@0: "int_field:\\s*2\n" michael@0: "string_field:\\s*\"hello\"\n" michael@0: "names:\\s*\"peter\"\n" michael@0: "names:\\s*\"paul\"\n" michael@0: "names:\\s*\"mary\"\n" michael@0: ">"); michael@0: } michael@0: michael@0: #endif // GTEST_HAS_PROTOBUF_ michael@0: michael@0: // Tests that the universal printer prints both the address and the michael@0: // value of a reference. michael@0: TEST(PrintReferenceTest, PrintsAddressAndValue) { michael@0: int n = 5; michael@0: EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n)); michael@0: michael@0: int a[2][3] = { michael@0: { 0, 1, 2 }, michael@0: { 3, 4, 5 } michael@0: }; michael@0: EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }", michael@0: PrintByRef(a)); michael@0: michael@0: const ::foo::UnprintableInFoo x; michael@0: EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object " michael@0: "", michael@0: PrintByRef(x)); michael@0: } michael@0: michael@0: // Tests that the universal printer prints a function pointer passed by michael@0: // reference. michael@0: TEST(PrintReferenceTest, HandlesFunctionPointer) { michael@0: void (*fp)(int n) = &MyFunction; michael@0: const string fp_pointer_string = michael@0: PrintPointer(reinterpret_cast(&fp)); michael@0: // We cannot directly cast &MyFunction to const void* because the michael@0: // standard disallows casting between pointers to functions and michael@0: // pointers to objects, and some compilers (e.g. GCC 3.4) enforce michael@0: // this limitation. michael@0: const string fp_string = PrintPointer(reinterpret_cast( michael@0: reinterpret_cast(fp))); michael@0: EXPECT_EQ("@" + fp_pointer_string + " " + fp_string, michael@0: PrintByRef(fp)); michael@0: } michael@0: michael@0: // Tests that the universal printer prints a member function pointer michael@0: // passed by reference. michael@0: TEST(PrintReferenceTest, HandlesMemberFunctionPointer) { michael@0: int (Foo::*p)(char ch) = &Foo::MyMethod; michael@0: EXPECT_TRUE(HasPrefix( michael@0: PrintByRef(p), michael@0: "@" + PrintPointer(reinterpret_cast(&p)) + " " + michael@0: Print(sizeof(p)) + "-byte object ")); michael@0: michael@0: char (Foo::*p2)(int n) = &Foo::MyVirtualMethod; michael@0: EXPECT_TRUE(HasPrefix( michael@0: PrintByRef(p2), michael@0: "@" + PrintPointer(reinterpret_cast(&p2)) + " " + michael@0: Print(sizeof(p2)) + "-byte object ")); michael@0: } michael@0: michael@0: // Tests that the universal printer prints a member variable pointer michael@0: // passed by reference. michael@0: TEST(PrintReferenceTest, HandlesMemberVariablePointer) { michael@0: int (Foo::*p) = &Foo::value; // NOLINT michael@0: EXPECT_TRUE(HasPrefix( michael@0: PrintByRef(p), michael@0: "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object ")); michael@0: } michael@0: michael@0: // Tests that FormatForComparisonFailureMessage(), which is used to print michael@0: // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion michael@0: // fails, formats the operand in the desired way. michael@0: michael@0: // scalar michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForScalar) { michael@0: EXPECT_STREQ("123", michael@0: FormatForComparisonFailureMessage(123, 124).c_str()); michael@0: } michael@0: michael@0: // non-char pointer michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) { michael@0: int n = 0; michael@0: EXPECT_EQ(PrintPointer(&n), michael@0: FormatForComparisonFailureMessage(&n, &n).c_str()); michael@0: } michael@0: michael@0: // non-char array michael@0: TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) { michael@0: // In expression 'array == x', 'array' is compared by pointer. michael@0: // Therefore we want to print an array operand as a pointer. michael@0: int n[] = { 1, 2, 3 }; michael@0: EXPECT_EQ(PrintPointer(n), michael@0: FormatForComparisonFailureMessage(n, n).c_str()); michael@0: } michael@0: michael@0: // Tests formatting a char pointer when it's compared with another pointer. michael@0: // In this case we want to print it as a raw pointer, as the comparision is by michael@0: // pointer. michael@0: michael@0: // char pointer vs pointer michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) { michael@0: // In expression 'p == x', where 'p' and 'x' are (const or not) char michael@0: // pointers, the operands are compared by pointer. Therefore we michael@0: // want to print 'p' as a pointer instead of a C string (we don't michael@0: // even know if it's supposed to point to a valid C string). michael@0: michael@0: // const char* michael@0: const char* s = "hello"; michael@0: EXPECT_EQ(PrintPointer(s), michael@0: FormatForComparisonFailureMessage(s, s).c_str()); michael@0: michael@0: // char* michael@0: char ch = 'a'; michael@0: EXPECT_EQ(PrintPointer(&ch), michael@0: FormatForComparisonFailureMessage(&ch, &ch).c_str()); michael@0: } michael@0: michael@0: // wchar_t pointer vs pointer michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) { michael@0: // In expression 'p == x', where 'p' and 'x' are (const or not) char michael@0: // pointers, the operands are compared by pointer. Therefore we michael@0: // want to print 'p' as a pointer instead of a wide C string (we don't michael@0: // even know if it's supposed to point to a valid wide C string). michael@0: michael@0: // const wchar_t* michael@0: const wchar_t* s = L"hello"; michael@0: EXPECT_EQ(PrintPointer(s), michael@0: FormatForComparisonFailureMessage(s, s).c_str()); michael@0: michael@0: // wchar_t* michael@0: wchar_t ch = L'a'; michael@0: EXPECT_EQ(PrintPointer(&ch), michael@0: FormatForComparisonFailureMessage(&ch, &ch).c_str()); michael@0: } michael@0: michael@0: // Tests formatting a char pointer when it's compared to a string object. michael@0: // In this case we want to print the char pointer as a C string. michael@0: michael@0: #if GTEST_HAS_GLOBAL_STRING michael@0: // char pointer vs ::string michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) { michael@0: const char* s = "hello \"world"; michael@0: EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped. michael@0: FormatForComparisonFailureMessage(s, ::string()).c_str()); michael@0: michael@0: // char* michael@0: char str[] = "hi\1"; michael@0: char* p = str; michael@0: EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped. michael@0: FormatForComparisonFailureMessage(p, ::string()).c_str()); michael@0: } michael@0: #endif michael@0: michael@0: // char pointer vs std::string michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) { michael@0: const char* s = "hello \"world"; michael@0: EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped. michael@0: FormatForComparisonFailureMessage(s, ::std::string()).c_str()); michael@0: michael@0: // char* michael@0: char str[] = "hi\1"; michael@0: char* p = str; michael@0: EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped. michael@0: FormatForComparisonFailureMessage(p, ::std::string()).c_str()); michael@0: } michael@0: michael@0: #if GTEST_HAS_GLOBAL_WSTRING michael@0: // wchar_t pointer vs ::wstring michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) { michael@0: const wchar_t* s = L"hi \"world"; michael@0: EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped. michael@0: FormatForComparisonFailureMessage(s, ::wstring()).c_str()); michael@0: michael@0: // wchar_t* michael@0: wchar_t str[] = L"hi\1"; michael@0: wchar_t* p = str; michael@0: EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped. michael@0: FormatForComparisonFailureMessage(p, ::wstring()).c_str()); michael@0: } michael@0: #endif michael@0: michael@0: #if GTEST_HAS_STD_WSTRING michael@0: // wchar_t pointer vs std::wstring michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) { michael@0: const wchar_t* s = L"hi \"world"; michael@0: EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped. michael@0: FormatForComparisonFailureMessage(s, ::std::wstring()).c_str()); michael@0: michael@0: // wchar_t* michael@0: wchar_t str[] = L"hi\1"; michael@0: wchar_t* p = str; michael@0: EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped. michael@0: FormatForComparisonFailureMessage(p, ::std::wstring()).c_str()); michael@0: } michael@0: #endif michael@0: michael@0: // Tests formatting a char array when it's compared with a pointer or array. michael@0: // In this case we want to print the array as a row pointer, as the comparison michael@0: // is by pointer. michael@0: michael@0: // char array vs pointer michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) { michael@0: char str[] = "hi \"world\""; michael@0: char* p = NULL; michael@0: EXPECT_EQ(PrintPointer(str), michael@0: FormatForComparisonFailureMessage(str, p).c_str()); michael@0: } michael@0: michael@0: // char array vs char array michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) { michael@0: const char str[] = "hi \"world\""; michael@0: EXPECT_EQ(PrintPointer(str), michael@0: FormatForComparisonFailureMessage(str, str).c_str()); michael@0: } michael@0: michael@0: // wchar_t array vs pointer michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) { michael@0: wchar_t str[] = L"hi \"world\""; michael@0: wchar_t* p = NULL; michael@0: EXPECT_EQ(PrintPointer(str), michael@0: FormatForComparisonFailureMessage(str, p).c_str()); michael@0: } michael@0: michael@0: // wchar_t array vs wchar_t array michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) { michael@0: const wchar_t str[] = L"hi \"world\""; michael@0: EXPECT_EQ(PrintPointer(str), michael@0: FormatForComparisonFailureMessage(str, str).c_str()); michael@0: } michael@0: michael@0: // Tests formatting a char array when it's compared with a string object. michael@0: // In this case we want to print the array as a C string. michael@0: michael@0: #if GTEST_HAS_GLOBAL_STRING michael@0: // char array vs string michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) { michael@0: const char str[] = "hi \"w\0rld\""; michael@0: EXPECT_STREQ("\"hi \\\"w\"", // The content should be escaped. michael@0: // Embedded NUL terminates the string. michael@0: FormatForComparisonFailureMessage(str, ::string()).c_str()); michael@0: } michael@0: #endif michael@0: michael@0: // char array vs std::string michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) { michael@0: const char str[] = "hi \"world\""; michael@0: EXPECT_STREQ("\"hi \\\"world\\\"\"", // The content should be escaped. michael@0: FormatForComparisonFailureMessage(str, ::std::string()).c_str()); michael@0: } michael@0: michael@0: #if GTEST_HAS_GLOBAL_WSTRING michael@0: // wchar_t array vs wstring michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) { michael@0: const wchar_t str[] = L"hi \"world\""; michael@0: EXPECT_STREQ("L\"hi \\\"world\\\"\"", // The content should be escaped. michael@0: FormatForComparisonFailureMessage(str, ::wstring()).c_str()); michael@0: } michael@0: #endif michael@0: michael@0: #if GTEST_HAS_STD_WSTRING michael@0: // wchar_t array vs std::wstring michael@0: TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) { michael@0: const wchar_t str[] = L"hi \"w\0rld\""; michael@0: EXPECT_STREQ( michael@0: "L\"hi \\\"w\"", // The content should be escaped. michael@0: // Embedded NUL terminates the string. michael@0: FormatForComparisonFailureMessage(str, ::std::wstring()).c_str()); michael@0: } michael@0: #endif michael@0: michael@0: // Useful for testing PrintToString(). We cannot use EXPECT_EQ() michael@0: // there as its implementation uses PrintToString(). The caller must michael@0: // ensure that 'value' has no side effect. michael@0: #define EXPECT_PRINT_TO_STRING_(value, expected_string) \ michael@0: EXPECT_TRUE(PrintToString(value) == (expected_string)) \ michael@0: << " where " #value " prints as " << (PrintToString(value)) michael@0: michael@0: TEST(PrintToStringTest, WorksForScalar) { michael@0: EXPECT_PRINT_TO_STRING_(123, "123"); michael@0: } michael@0: michael@0: TEST(PrintToStringTest, WorksForPointerToConstChar) { michael@0: const char* p = "hello"; michael@0: EXPECT_PRINT_TO_STRING_(p, "\"hello\""); michael@0: } michael@0: michael@0: TEST(PrintToStringTest, WorksForPointerToNonConstChar) { michael@0: char s[] = "hello"; michael@0: char* p = s; michael@0: EXPECT_PRINT_TO_STRING_(p, "\"hello\""); michael@0: } michael@0: michael@0: TEST(PrintToStringTest, EscapesForPointerToConstChar) { michael@0: const char* p = "hello\n"; michael@0: EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\""); michael@0: } michael@0: michael@0: TEST(PrintToStringTest, EscapesForPointerToNonConstChar) { michael@0: char s[] = "hello\1"; michael@0: char* p = s; michael@0: EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\""); michael@0: } michael@0: michael@0: TEST(PrintToStringTest, WorksForArray) { michael@0: int n[3] = { 1, 2, 3 }; michael@0: EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }"); michael@0: } michael@0: michael@0: TEST(PrintToStringTest, WorksForCharArray) { michael@0: char s[] = "hello"; michael@0: EXPECT_PRINT_TO_STRING_(s, "\"hello\""); michael@0: } michael@0: michael@0: TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) { michael@0: const char str_with_nul[] = "hello\0 world"; michael@0: EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\""); michael@0: michael@0: char mutable_str_with_nul[] = "hello\0 world"; michael@0: EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\""); michael@0: } michael@0: michael@0: #undef EXPECT_PRINT_TO_STRING_ michael@0: michael@0: TEST(UniversalTersePrintTest, WorksForNonReference) { michael@0: ::std::stringstream ss; michael@0: UniversalTersePrint(123, &ss); michael@0: EXPECT_EQ("123", ss.str()); michael@0: } michael@0: michael@0: TEST(UniversalTersePrintTest, WorksForReference) { michael@0: const int& n = 123; michael@0: ::std::stringstream ss; michael@0: UniversalTersePrint(n, &ss); michael@0: EXPECT_EQ("123", ss.str()); michael@0: } michael@0: michael@0: TEST(UniversalTersePrintTest, WorksForCString) { michael@0: const char* s1 = "abc"; michael@0: ::std::stringstream ss1; michael@0: UniversalTersePrint(s1, &ss1); michael@0: EXPECT_EQ("\"abc\"", ss1.str()); michael@0: michael@0: char* s2 = const_cast(s1); michael@0: ::std::stringstream ss2; michael@0: UniversalTersePrint(s2, &ss2); michael@0: EXPECT_EQ("\"abc\"", ss2.str()); michael@0: michael@0: const char* s3 = NULL; michael@0: ::std::stringstream ss3; michael@0: UniversalTersePrint(s3, &ss3); michael@0: EXPECT_EQ("NULL", ss3.str()); michael@0: } michael@0: michael@0: TEST(UniversalPrintTest, WorksForNonReference) { michael@0: ::std::stringstream ss; michael@0: UniversalPrint(123, &ss); michael@0: EXPECT_EQ("123", ss.str()); michael@0: } michael@0: michael@0: TEST(UniversalPrintTest, WorksForReference) { michael@0: const int& n = 123; michael@0: ::std::stringstream ss; michael@0: UniversalPrint(n, &ss); michael@0: EXPECT_EQ("123", ss.str()); michael@0: } michael@0: michael@0: TEST(UniversalPrintTest, WorksForCString) { michael@0: const char* s1 = "abc"; michael@0: ::std::stringstream ss1; michael@0: UniversalPrint(s1, &ss1); michael@0: EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str())); michael@0: michael@0: char* s2 = const_cast(s1); michael@0: ::std::stringstream ss2; michael@0: UniversalPrint(s2, &ss2); michael@0: EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str())); michael@0: michael@0: const char* s3 = NULL; michael@0: ::std::stringstream ss3; michael@0: UniversalPrint(s3, &ss3); michael@0: EXPECT_EQ("NULL", ss3.str()); michael@0: } michael@0: michael@0: TEST(UniversalPrintTest, WorksForCharArray) { michael@0: const char str[] = "\"Line\0 1\"\nLine 2"; michael@0: ::std::stringstream ss1; michael@0: UniversalPrint(str, &ss1); michael@0: EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str()); michael@0: michael@0: const char mutable_str[] = "\"Line\0 1\"\nLine 2"; michael@0: ::std::stringstream ss2; michael@0: UniversalPrint(mutable_str, &ss2); michael@0: EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str()); michael@0: } michael@0: michael@0: #if GTEST_HAS_TR1_TUPLE michael@0: michael@0: TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) { michael@0: Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple()); michael@0: EXPECT_EQ(0u, result.size()); michael@0: } michael@0: michael@0: TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) { michael@0: Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1)); michael@0: ASSERT_EQ(1u, result.size()); michael@0: EXPECT_EQ("1", result[0]); michael@0: } michael@0: michael@0: TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) { michael@0: Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a')); michael@0: ASSERT_EQ(2u, result.size()); michael@0: EXPECT_EQ("1", result[0]); michael@0: EXPECT_EQ("'a' (97, 0x61)", result[1]); michael@0: } michael@0: michael@0: TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) { michael@0: const int n = 1; michael@0: Strings result = UniversalTersePrintTupleFieldsToStrings( michael@0: tuple(n, "a")); michael@0: ASSERT_EQ(2u, result.size()); michael@0: EXPECT_EQ("1", result[0]); michael@0: EXPECT_EQ("\"a\"", result[1]); michael@0: } michael@0: michael@0: #endif // GTEST_HAS_TR1_TUPLE michael@0: michael@0: } // namespace gtest_printers_test michael@0: } // namespace testing