"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "googletest/test/googletest-printers-test.cc" between
googletest-release-1.11.0.tar.gz and googletest-release-1.12.0.tar.gz

About: GoogleTest is Google's (unit) testing and mocking framework for C++ tests.

googletest-printers-test.cc  (googletest-release-1.11.0):googletest-printers-test.cc  (googletest-release-1.12.0)
skipping to change at line 58 skipping to change at line 58
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "gtest/gtest-printers.h" #include "gtest/gtest-printers.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
// Some user-defined types for testing the universal value printer. // Some user-defined types for testing the universal value printer.
// An anonymous enum type. // An anonymous enum type.
enum AnonymousEnum { enum AnonymousEnum { kAE1 = -1, kAE2 = 1 };
kAE1 = -1,
kAE2 = 1
};
// An enum without a user-defined printer. // An enum without a user-defined printer.
enum EnumWithoutPrinter { enum EnumWithoutPrinter { kEWP1 = -2, kEWP2 = 42 };
kEWP1 = -2,
kEWP2 = 42
};
// An enum with a << operator. // An enum with a << operator.
enum EnumWithStreaming { enum EnumWithStreaming { kEWS1 = 10 };
kEWS1 = 10
};
std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) { std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) {
return os << (e == kEWS1 ? "kEWS1" : "invalid"); return os << (e == kEWS1 ? "kEWS1" : "invalid");
} }
// An enum with a PrintTo() function. // An enum with a PrintTo() function.
enum EnumWithPrintTo { enum EnumWithPrintTo { kEWPT1 = 1 };
kEWPT1 = 1
};
void PrintTo(EnumWithPrintTo e, std::ostream* os) { void PrintTo(EnumWithPrintTo e, std::ostream* os) {
*os << (e == kEWPT1 ? "kEWPT1" : "invalid"); *os << (e == kEWPT1 ? "kEWPT1" : "invalid");
} }
// A class implicitly convertible to BiggestInt. // A class implicitly convertible to BiggestInt.
class BiggestIntConvertible { class BiggestIntConvertible {
public: public:
operator ::testing::internal::BiggestInt() const { return 42; } operator ::testing::internal::BiggestInt() const { return 42; }
}; };
skipping to change at line 110 skipping to change at line 100
} }
static void operator<<(std::ostream& os, const ChildClassWithStreamOperator&) { static void operator<<(std::ostream& os, const ChildClassWithStreamOperator&) {
os << "ChildClassWithStreamOperator"; os << "ChildClassWithStreamOperator";
} }
// A user-defined unprintable class template in the global namespace. // A user-defined unprintable class template in the global namespace.
template <typename T> template <typename T>
class UnprintableTemplateInGlobal { class UnprintableTemplateInGlobal {
public: public:
UnprintableTemplateInGlobal() : value_() {} UnprintableTemplateInGlobal() : value_() {}
private: private:
T value_; T value_;
}; };
// A user-defined streamable type in the global namespace. // A user-defined streamable type in the global namespace.
class StreamableInGlobal { class StreamableInGlobal {
public: public:
virtual ~StreamableInGlobal() {} virtual ~StreamableInGlobal() {}
}; };
skipping to change at line 135 skipping to change at line 126
os << "StreamableInGlobal*"; os << "StreamableInGlobal*";
} }
namespace foo { namespace foo {
// A user-defined unprintable type in a user namespace. // A user-defined unprintable type in a user namespace.
class UnprintableInFoo { class UnprintableInFoo {
public: public:
UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); } UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); }
double z() const { return z_; } double z() const { return z_; }
private: private:
char xy_[8]; char xy_[8];
double z_; double z_;
}; };
// A user-defined printable type in a user-chosen namespace. // A user-defined printable type in a user-chosen namespace.
struct PrintableViaPrintTo { struct PrintableViaPrintTo {
PrintableViaPrintTo() : value() {} PrintableViaPrintTo() : value() {}
int value; int value;
}; };
void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) { void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
*os << "PrintableViaPrintTo: " << x.value; *os << "PrintableViaPrintTo: " << x.value;
} }
// A type with a user-defined << for printing its pointer. // A type with a user-defined << for printing its pointer.
struct PointerPrintable { struct PointerPrintable {};
};
::std::ostream& operator<<(::std::ostream& os, ::std::ostream& operator<<(::std::ostream& os,
const PointerPrintable* /* x */) { const PointerPrintable* /* x */) {
return os << "PointerPrintable*"; return os << "PointerPrintable*";
} }
// A user-defined printable class template in a user-chosen namespace. // A user-defined printable class template in a user-chosen namespace.
template <typename T> template <typename T>
class PrintableViaPrintToTemplate { class PrintableViaPrintToTemplate {
public: public:
explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {} explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
const T& value() const { return value_; } const T& value() const { return value_; }
private: private:
T value_; T value_;
}; };
template <typename T> template <typename T>
void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) { void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
*os << "PrintableViaPrintToTemplate: " << x.value(); *os << "PrintableViaPrintToTemplate: " << x.value();
} }
// A user-defined streamable class template in a user namespace. // A user-defined streamable class template in a user namespace.
template <typename T> template <typename T>
class StreamableTemplateInFoo { class StreamableTemplateInFoo {
public: public:
StreamableTemplateInFoo() : value_() {} StreamableTemplateInFoo() : value_() {}
const T& value() const { return value_; } const T& value() const { return value_; }
private: private:
T value_; T value_;
}; };
template <typename T> template <typename T>
inline ::std::ostream& operator<<(::std::ostream& os, inline ::std::ostream& operator<<(::std::ostream& os,
const StreamableTemplateInFoo<T>& x) { const StreamableTemplateInFoo<T>& x) {
return os << "StreamableTemplateInFoo: " << x.value(); return os << "StreamableTemplateInFoo: " << x.value();
} }
skipping to change at line 351 skipping to change at line 344
EXPECT_EQ("'\\v' (11, 0xB)", Print('\v')); EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
EXPECT_EQ("'\\x7F' (127)", Print('\x7F')); EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
EXPECT_EQ("'\\xFF' (255)", Print('\xFF')); EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
EXPECT_EQ("' ' (32, 0x20)", Print(' ')); EXPECT_EQ("' ' (32, 0x20)", Print(' '));
EXPECT_EQ("'a' (97, 0x61)", Print('a')); EXPECT_EQ("'a' (97, 0x61)", Print('a'));
} }
// signed char. // signed char.
TEST(PrintCharTest, SignedChar) { TEST(PrintCharTest, SignedChar) {
EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0'))); EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
EXPECT_EQ("'\\xCE' (-50)", EXPECT_EQ("'\\xCE' (-50)", Print(static_cast<signed char>(-50)));
Print(static_cast<signed char>(-50)));
} }
// unsigned char. // unsigned char.
TEST(PrintCharTest, UnsignedChar) { TEST(PrintCharTest, UnsignedChar) {
EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0'))); EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
EXPECT_EQ("'b' (98, 0x62)", EXPECT_EQ("'b' (98, 0x62)", Print(static_cast<unsigned char>('b')));
Print(static_cast<unsigned char>('b')));
} }
TEST(PrintCharTest, Char16) { TEST(PrintCharTest, Char16) { EXPECT_EQ("U+0041", Print(u'A')); }
EXPECT_EQ("U+0041", Print(u'A'));
}
TEST(PrintCharTest, Char32) { TEST(PrintCharTest, Char32) { EXPECT_EQ("U+0041", Print(U'A')); }
EXPECT_EQ("U+0041", Print(U'A'));
}
#ifdef __cpp_char8_t #ifdef __cpp_char8_t
TEST(PrintCharTest, Char8) { TEST(PrintCharTest, Char8) { EXPECT_EQ("U+0041", Print(u8'A')); }
EXPECT_EQ("U+0041", Print(u8'A'));
}
#endif #endif
// Tests printing other simple, built-in types. // Tests printing other simple, built-in types.
// bool. // bool.
TEST(PrintBuiltInTypeTest, Bool) { TEST(PrintBuiltInTypeTest, Bool) {
EXPECT_EQ("false", Print(false)); EXPECT_EQ("false", Print(false));
EXPECT_EQ("true", Print(true)); EXPECT_EQ("true", Print(true));
} }
skipping to change at line 415 skipping to change at line 400
// Test that int64_t provides more storage than wchar_t. // Test that int64_t provides more storage than wchar_t.
TEST(PrintTypeSizeTest, Wchar_t) { TEST(PrintTypeSizeTest, Wchar_t) {
EXPECT_LT(sizeof(wchar_t), sizeof(int64_t)); EXPECT_LT(sizeof(wchar_t), sizeof(int64_t));
} }
// Various integer types. // Various integer types.
TEST(PrintBuiltInTypeTest, Integer) { TEST(PrintBuiltInTypeTest, Integer) {
EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8 EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8
EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8 EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8
EXPECT_EQ("65535", Print(std::numeric_limits<uint16_t>::max())); // uint16 EXPECT_EQ("65535", Print(std::numeric_limits<uint16_t>::max())); // uint16
EXPECT_EQ("-32768", Print(std::numeric_limits<int16_t>::min())); // int16 EXPECT_EQ("-32768", Print(std::numeric_limits<int16_t>::min())); // int16
EXPECT_EQ("4294967295", EXPECT_EQ("4294967295",
Print(std::numeric_limits<uint32_t>::max())); // uint32 Print(std::numeric_limits<uint32_t>::max())); // uint32
EXPECT_EQ("-2147483648", EXPECT_EQ("-2147483648",
Print(std::numeric_limits<int32_t>::min())); // int32 Print(std::numeric_limits<int32_t>::min())); // int32
EXPECT_EQ("18446744073709551615", EXPECT_EQ("18446744073709551615",
Print(std::numeric_limits<uint64_t>::max())); // uint64 Print(std::numeric_limits<uint64_t>::max())); // uint64
EXPECT_EQ("-9223372036854775808", EXPECT_EQ("-9223372036854775808",
Print(std::numeric_limits<int64_t>::min())); // int64 Print(std::numeric_limits<int64_t>::min())); // int64
#ifdef __cpp_char8_t #ifdef __cpp_char8_t
EXPECT_EQ("U+0000", EXPECT_EQ("U+0000",
skipping to change at line 447 skipping to change at line 432
EXPECT_EQ("U+FFFFFFFF", EXPECT_EQ("U+FFFFFFFF",
Print(std::numeric_limits<char32_t>::max())); // char32_t Print(std::numeric_limits<char32_t>::max())); // char32_t
} }
// Size types. // Size types.
TEST(PrintBuiltInTypeTest, Size_t) { TEST(PrintBuiltInTypeTest, Size_t) {
EXPECT_EQ("1", Print(sizeof('a'))); // size_t. EXPECT_EQ("1", Print(sizeof('a'))); // size_t.
#if !GTEST_OS_WINDOWS #if !GTEST_OS_WINDOWS
// Windows has no ssize_t type. // Windows has no ssize_t type.
EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t. EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t.
#endif // !GTEST_OS_WINDOWS #endif // !GTEST_OS_WINDOWS
} }
// gcc/clang __{u,}int128_t values.
#if defined(__SIZEOF_INT128__)
TEST(PrintBuiltInTypeTest, Int128) {
// Small ones
EXPECT_EQ("0", Print(__int128_t{0}));
EXPECT_EQ("0", Print(__uint128_t{0}));
EXPECT_EQ("12345", Print(__int128_t{12345}));
EXPECT_EQ("12345", Print(__uint128_t{12345}));
EXPECT_EQ("-12345", Print(__int128_t{-12345}));
// Large ones
EXPECT_EQ("340282366920938463463374607431768211455", Print(~__uint128_t{}));
__int128_t max_128 = static_cast<__int128_t>(~__uint128_t{} / 2);
EXPECT_EQ("-170141183460469231731687303715884105728", Print(~max_128));
EXPECT_EQ("170141183460469231731687303715884105727", Print(max_128));
}
#endif // __SIZEOF_INT128__
// Floating-points. // Floating-points.
TEST(PrintBuiltInTypeTest, FloatingPoints) { TEST(PrintBuiltInTypeTest, FloatingPoints) {
EXPECT_EQ("1.5", Print(1.5f)); // float EXPECT_EQ("1.5", Print(1.5f)); // float
EXPECT_EQ("-2.5", Print(-2.5)); // double EXPECT_EQ("-2.5", Print(-2.5)); // double
} }
#if GTEST_HAS_RTTI
TEST(PrintBuiltInTypeTest, TypeInfo) {
struct MyStruct {};
auto res = Print(typeid(MyStruct{}));
// We can't guarantee that we can demangle the name, but either name should
// contain the substring "MyStruct".
EXPECT_NE(res.find("MyStruct"), res.npos) << res;
}
#endif // GTEST_HAS_RTTI
// Since ::std::stringstream::operator<<(const void *) formats the pointer // Since ::std::stringstream::operator<<(const void *) formats the pointer
// output differently with different compilers, we have to create the expected // output differently with different compilers, we have to create the expected
// output first and use it as our expectation. // output first and use it as our expectation.
static std::string PrintPointer(const void* p) { static std::string PrintPointer(const void* p) {
::std::stringstream expected_result_stream; ::std::stringstream expected_result_stream;
expected_result_stream << p; expected_result_stream << p;
return expected_result_stream.str(); return expected_result_stream.str();
} }
// Tests printing C strings. // Tests printing C strings.
skipping to change at line 489 skipping to change at line 502
// NULL C string. // NULL C string.
TEST(PrintCStringTest, Null) { TEST(PrintCStringTest, Null) {
const char* p = nullptr; const char* p = nullptr;
EXPECT_EQ("NULL", Print(p)); EXPECT_EQ("NULL", Print(p));
} }
// Tests that C strings are escaped properly. // Tests that C strings are escaped properly.
TEST(PrintCStringTest, EscapesProperly) { TEST(PrintCStringTest, EscapesProperly) {
const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a"; const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f" EXPECT_EQ(PrintPointer(p) +
"\\n\\r\\t\\v\\x7F\\xFF a\"", " pointing to \"'\\\"?\\\\\\a\\b\\f"
"\\n\\r\\t\\v\\x7F\\xFF a\"",
Print(p)); Print(p));
} }
#ifdef __cpp_char8_t #ifdef __cpp_char8_t
// const char8_t*. // const char8_t*.
TEST(PrintU8StringTest, Const) { TEST(PrintU8StringTest, Const) {
const char8_t* p = u8"界"; const char8_t* p = u8"界";
EXPECT_EQ(PrintPointer(p) + " pointing to u8\"\\xE7\\x95\\x8C\"", Print(p)); EXPECT_EQ(PrintPointer(p) + " pointing to u8\"\\xE7\\x95\\x8C\"", Print(p));
} }
skipping to change at line 609 skipping to change at line 623
} }
// NULL wide C string. // NULL wide C string.
TEST(PrintWideCStringTest, Null) { TEST(PrintWideCStringTest, Null) {
const wchar_t* p = nullptr; const wchar_t* p = nullptr;
EXPECT_EQ("NULL", Print(p)); EXPECT_EQ("NULL", Print(p));
} }
// Tests that wide C strings are escaped properly. // Tests that wide C strings are escaped properly.
TEST(PrintWideCStringTest, EscapesProperly) { TEST(PrintWideCStringTest, EscapesProperly) {
const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r', const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b',
'\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'}; '\f', '\n', '\r', '\t', '\v', 0xD3,
EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f" 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
"\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"", EXPECT_EQ(PrintPointer(s) +
" pointing to L\"'\\\"?\\\\\\a\\b\\f"
"\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
Print(static_cast<const wchar_t*>(s))); Print(static_cast<const wchar_t*>(s)));
} }
#endif // native wchar_t #endif // native wchar_t
// Tests printing pointers to other char types. // Tests printing pointers to other char types.
// signed char*. // signed char*.
TEST(PrintCharPointerTest, SignedChar) { TEST(PrintCharPointerTest, SignedChar) {
signed char* p = reinterpret_cast<signed char*>(0x1234); signed char* p = reinterpret_cast<signed char*>(0x1234);
EXPECT_EQ(PrintPointer(p), Print(p)); EXPECT_EQ(PrintPointer(p), Print(p));
skipping to change at line 694 skipping to change at line 710
// Tests printing (non-member) function pointers. // Tests printing (non-member) function pointers.
void MyFunction(int /* n */) {} void MyFunction(int /* n */) {}
TEST(PrintPointerTest, NonMemberFunctionPointer) { TEST(PrintPointerTest, NonMemberFunctionPointer) {
// We cannot directly cast &MyFunction to const void* because the // We cannot directly cast &MyFunction to const void* because the
// standard disallows casting between pointers to functions and // standard disallows casting between pointers to functions and
// pointers to objects, and some compilers (e.g. GCC 3.4) enforce // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
// this limitation. // this limitation.
EXPECT_EQ( EXPECT_EQ(PrintPointer(reinterpret_cast<const void*>(
PrintPointer(reinterpret_cast<const void*>( reinterpret_cast<internal::BiggestInt>(&MyFunction))),
reinterpret_cast<internal::BiggestInt>(&MyFunction))), Print(&MyFunction));
Print(&MyFunction));
int (*p)(bool) = NULL; // NOLINT int (*p)(bool) = NULL; // NOLINT
EXPECT_EQ("NULL", Print(p)); EXPECT_EQ("NULL", Print(p));
} }
// An assertion predicate determining whether a one string is a prefix for // An assertion predicate determining whether a one string is a prefix for
// another. // another.
template <typename StringType> template <typename StringType>
AssertionResult HasPrefix(const StringType& str, const StringType& prefix) { AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
if (str.find(prefix, 0) == 0) if (str.find(prefix, 0) == 0) return AssertionSuccess();
return AssertionSuccess();
const bool is_wide_string = sizeof(prefix[0]) > 1; const bool is_wide_string = sizeof(prefix[0]) > 1;
const char* const begin_string_quote = is_wide_string ? "L\"" : "\""; const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
return AssertionFailure() return AssertionFailure()
<< begin_string_quote << prefix << "\" is not a prefix of " << begin_string_quote << prefix << "\" is not a prefix of "
<< begin_string_quote << str << "\"\n"; << begin_string_quote << str << "\"\n";
} }
// Tests printing member variable pointers. Although they are called // Tests printing member variable pointers. Although they are called
// pointers, they don't point to a location in the address space. // pointers, they don't point to a location in the address space.
// Their representation is implementation-defined. Thus they will be // Their representation is implementation-defined. Thus they will be
// printed as raw bytes. // printed as raw bytes.
struct Foo { struct Foo {
public: public:
virtual ~Foo() {} virtual ~Foo() {}
int MyMethod(char x) { return x + 1; } int MyMethod(char x) { return x + 1; }
virtual char MyVirtualMethod(int /* n */) { return 'a'; } virtual char MyVirtualMethod(int /* n */) { return 'a'; }
int value; int value;
}; };
TEST(PrintPointerTest, MemberVariablePointer) { TEST(PrintPointerTest, MemberVariablePointer) {
EXPECT_TRUE(HasPrefix(Print(&Foo::value), EXPECT_TRUE(HasPrefix(Print(&Foo::value),
Print(sizeof(&Foo::value)) + "-byte object ")); Print(sizeof(&Foo::value)) + "-byte object "));
int Foo::*p = NULL; // NOLINT int Foo::*p = NULL; // NOLINT
EXPECT_TRUE(HasPrefix(Print(p), EXPECT_TRUE(HasPrefix(Print(p), Print(sizeof(p)) + "-byte object "));
Print(sizeof(p)) + "-byte object "));
} }
// Tests printing member function pointers. Although they are called // Tests printing member function pointers. Although they are called
// pointers, they don't point to a location in the address space. // pointers, they don't point to a location in the address space.
// Their representation is implementation-defined. Thus they will be // Their representation is implementation-defined. Thus they will be
// printed as raw bytes. // printed as raw bytes.
TEST(PrintPointerTest, MemberFunctionPointer) { TEST(PrintPointerTest, MemberFunctionPointer) {
EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod), EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod),
Print(sizeof(&Foo::MyMethod)) + "-byte object ")); Print(sizeof(&Foo::MyMethod)) + "-byte object "));
EXPECT_TRUE( EXPECT_TRUE(
HasPrefix(Print(&Foo::MyVirtualMethod), HasPrefix(Print(&Foo::MyVirtualMethod),
Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object ")); Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
int (Foo::*p)(char) = NULL; // NOLINT int (Foo::*p)(char) = NULL; // NOLINT
EXPECT_TRUE(HasPrefix(Print(p), EXPECT_TRUE(HasPrefix(Print(p), Print(sizeof(p)) + "-byte object "));
Print(sizeof(p)) + "-byte object "));
} }
// Tests printing C arrays. // Tests printing C arrays.
// The difference between this and Print() is that it ensures that the // The difference between this and Print() is that it ensures that the
// argument is a reference to an array. // argument is a reference to an array.
template <typename T, size_t N> template <typename T, size_t N>
std::string PrintArrayHelper(T (&a)[N]) { std::string PrintArrayHelper(T (&a)[N]) {
return Print(a); return Print(a);
} }
// One-dimensional array. // One-dimensional array.
TEST(PrintArrayTest, OneDimensionalArray) { TEST(PrintArrayTest, OneDimensionalArray) {
int a[5] = { 1, 2, 3, 4, 5 }; int a[5] = {1, 2, 3, 4, 5};
EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a)); EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
} }
// Two-dimensional array. // Two-dimensional array.
TEST(PrintArrayTest, TwoDimensionalArray) { TEST(PrintArrayTest, TwoDimensionalArray) {
int a[2][5] = { int a[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 0}};
{ 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 0 }
};
EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a)); EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
} }
// Array of const elements. // Array of const elements.
TEST(PrintArrayTest, ConstArray) { TEST(PrintArrayTest, ConstArray) {
const bool a[1] = { false }; const bool a[1] = {false};
EXPECT_EQ("{ false }", PrintArrayHelper(a)); EXPECT_EQ("{ false }", PrintArrayHelper(a));
} }
// char array without terminating NUL. // char array without terminating NUL.
TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) { TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
// Array a contains '\0' in the middle and doesn't end with '\0'. // Array a contains '\0' in the middle and doesn't end with '\0'.
char a[] = { 'H', '\0', 'i' }; char a[] = {'H', '\0', 'i'};
EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a)); EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
} }
// char array with terminating NUL. // char array with terminating NUL.
TEST(PrintArrayTest, CharArrayWithTerminatingNul) { TEST(PrintArrayTest, CharArrayWithTerminatingNul) {
const char a[] = "\0Hi"; const char a[] = "\0Hi";
EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a)); EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a));
} }
#ifdef __cpp_char8_t #ifdef __cpp_char8_t
// char_t array without terminating NUL. // char_t array without terminating NUL.
TEST(PrintArrayTest, Char8ArrayWithNoTerminatingNul) { TEST(PrintArrayTest, Char8ArrayWithNoTerminatingNul) {
// Array a contains '\0' in the middle and doesn't end with '\0'. // Array a contains '\0' in the middle and doesn't end with '\0'.
const char8_t a[] = {u8'H', u8'\0', u8'i'}; const char8_t a[] = {u8'H', u8'\0', u8'i'};
EXPECT_EQ("u8\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a)); EXPECT_EQ("u8\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
} }
// char8_t array with terminating NUL. // char8_t array with terminating NUL.
TEST(PrintArrayTest, Char8ArrayWithTerminatingNul) { TEST(PrintArrayTest, Char8ArrayWithTerminatingNul) {
const char8_t a[] = u8"\0世界"; const char8_t a[] = u8"\0世界";
EXPECT_EQ( EXPECT_EQ("u8\"\\0\\xE4\\xB8\\x96\\xE7\\x95\\x8C\"", PrintArrayHelper(a));
"u8\"\\0\\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
PrintArrayHelper(a));
} }
#endif #endif
// const char16_t array without terminating NUL. // const char16_t array without terminating NUL.
TEST(PrintArrayTest, Char16ArrayWithNoTerminatingNul) { TEST(PrintArrayTest, Char16ArrayWithNoTerminatingNul) {
// Array a contains '\0' in the middle and doesn't end with '\0'. // Array a contains '\0' in the middle and doesn't end with '\0'.
const char16_t a[] = {u'こ', u'\0', u'ん', u'に', u'ち', u'は'}; const char16_t a[] = {u'こ', u'\0', u'ん', u'に', u'ち', u'は'};
EXPECT_EQ("u\"\\x3053\\0\\x3093\\x306B\\x3061\\x306F\" (no terminating NUL)", EXPECT_EQ("u\"\\x3053\\0\\x3093\\x306B\\x3061\\x306F\" (no terminating NUL)",
PrintArrayHelper(a)); PrintArrayHelper(a));
} }
skipping to change at line 862 skipping to change at line 869
} }
// Array of objects. // Array of objects.
TEST(PrintArrayTest, ObjectArray) { TEST(PrintArrayTest, ObjectArray) {
std::string a[3] = {"Hi", "Hello", "Ni hao"}; std::string a[3] = {"Hi", "Hello", "Ni hao"};
EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a)); EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
} }
// Array with many elements. // Array with many elements.
TEST(PrintArrayTest, BigArray) { TEST(PrintArrayTest, BigArray) {
int a[100] = { 1, 2, 3 }; int a[100] = {1, 2, 3};
EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }", EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
PrintArrayHelper(a)); PrintArrayHelper(a));
} }
// Tests printing ::string and ::std::string. // Tests printing ::string and ::std::string.
// ::std::string. // ::std::string.
TEST(PrintStringTest, StringInStdNamespace) { TEST(PrintStringTest, StringInStdNamespace) {
const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
const ::std::string str(s, sizeof(s)); const ::std::string str(s, sizeof(s));
EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
Print(str)); Print(str));
} }
TEST(PrintStringTest, StringAmbiguousHex) { TEST(PrintStringTest, StringAmbiguousHex) {
// "\x6BANANA" is ambiguous, it can be interpreted as starting with either of: // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
// '\x6', '\x6B', or '\x6BA'. // '\x6', '\x6B', or '\x6BA'.
// a hex escaping sequence following by a decimal digit // a hex escaping sequence following by a decimal digit
EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3"))); EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12"
"3")));
// a hex escaping sequence following by a hex digit (lower-case) // a hex escaping sequence following by a hex digit (lower-case)
EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas"))); EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6"
"bananas")));
// a hex escaping sequence following by a hex digit (upper-case) // a hex escaping sequence following by a hex digit (upper-case)
EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA"))); EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6"
"BANANA")));
// a hex escaping sequence following by a non-xdigit // a hex escaping sequence following by a non-xdigit
EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!"))); EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
} }
// Tests printing ::std::wstring. // Tests printing ::std::wstring.
#if GTEST_HAS_STD_WSTRING #if GTEST_HAS_STD_WSTRING
// ::std::wstring. // ::std::wstring.
TEST(PrintWideStringTest, StringInStdNamespace) { TEST(PrintWideStringTest, StringInStdNamespace) {
const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t)); const ::std::wstring str(s, sizeof(s) / sizeof(wchar_t));
EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" EXPECT_EQ(
"\\xD3\\x576\\x8D3\\xC74D a\\0\"", "L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
Print(str)); "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
Print(str));
} }
TEST(PrintWideStringTest, StringAmbiguousHex) { TEST(PrintWideStringTest, StringAmbiguousHex) {
// same for wide strings. // same for wide strings.
EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3"))); EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12"
EXPECT_EQ("L\"mm\\x6\" L\"bananas\"", L"3")));
Print(::std::wstring(L"mm\x6" L"bananas"))); EXPECT_EQ("L\"mm\\x6\" L\"bananas\"", Print(::std::wstring(L"mm\x6"
EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"", L"bananas")));
Print(::std::wstring(L"NOM\x6" L"BANANA"))); EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"", Print(::std::wstring(L"NOM\x6"
L"BANANA")));
EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!"))); EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
} }
#endif // GTEST_HAS_STD_WSTRING #endif // GTEST_HAS_STD_WSTRING
#ifdef __cpp_char8_t #ifdef __cpp_char8_t
TEST(PrintStringTest, U8String) { TEST(PrintStringTest, U8String) {
std::u8string str = u8"Hello, 世界"; std::u8string str = u8"Hello, 世界";
EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type. EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
EXPECT_EQ("u8\"Hello, \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"", Print(str)); EXPECT_EQ("u8\"Hello, \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"", Print(str));
} }
skipping to change at line 1037 skipping to change at line 1049
TEST(PrintStlContainerTest, HashMultiMap) { TEST(PrintStlContainerTest, HashMultiMap) {
::std::unordered_multimap<int, bool> map1; ::std::unordered_multimap<int, bool> map1;
map1.insert(make_pair(5, true)); map1.insert(make_pair(5, true));
map1.insert(make_pair(5, false)); map1.insert(make_pair(5, false));
// Elements of hash_multimap can be printed in any order. // Elements of hash_multimap can be printed in any order.
const std::string result = Print(map1); const std::string result = Print(map1);
EXPECT_TRUE(result == "{ (5, true), (5, false) }" || EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
result == "{ (5, false), (5, true) }") result == "{ (5, false), (5, true) }")
<< " where Print(map1) returns \"" << result << "\"."; << " where Print(map1) returns \"" << result << "\".";
} }
TEST(PrintStlContainerTest, HashSet) { TEST(PrintStlContainerTest, HashSet) {
::std::unordered_set<int> set1; ::std::unordered_set<int> set1;
set1.insert(1); set1.insert(1);
EXPECT_EQ("{ 1 }", Print(set1)); EXPECT_EQ("{ 1 }", Print(set1));
} }
TEST(PrintStlContainerTest, HashMultiSet) { TEST(PrintStlContainerTest, HashMultiSet) {
const int kSize = 5; const int kSize = 5;
int a[kSize] = { 1, 1, 2, 5, 1 }; int a[kSize] = {1, 1, 2, 5, 1};
::std::unordered_multiset<int> set1(a, a + kSize); ::std::unordered_multiset<int> set1(a, a + kSize);
// Elements of hash_multiset can be printed in any order. // Elements of hash_multiset can be printed in any order.
const std::string result = Print(set1); const std::string result = Print(set1);
const std::string expected_pattern = "{ d, d, d, d, d }"; // d means a digit. const std::string expected_pattern = "{ d, d, d, d, d }"; // d means a digit.
// Verifies the result matches the expected pattern; also extracts // Verifies the result matches the expected pattern; also extracts
// the numbers in the result. // the numbers in the result.
ASSERT_EQ(expected_pattern.length(), result.length()); ASSERT_EQ(expected_pattern.length(), result.length());
std::vector<int> numbers; std::vector<int> numbers;
for (size_t i = 0; i != result.length(); i++) { for (size_t i = 0; i != result.length(); i++) {
if (expected_pattern[i] == 'd') { if (expected_pattern[i] == 'd') {
ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0); ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0);
numbers.push_back(result[i] - '0'); numbers.push_back(result[i] - '0');
} else { } else {
EXPECT_EQ(expected_pattern[i], result[i]) << " where result is " EXPECT_EQ(expected_pattern[i], result[i])
<< result; << " where result is " << result;
} }
} }
// Makes sure the result contains the right numbers. // Makes sure the result contains the right numbers.
std::sort(numbers.begin(), numbers.end()); std::sort(numbers.begin(), numbers.end());
std::sort(a, a + kSize); std::sort(a, a + kSize);
EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin())); EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
} }
TEST(PrintStlContainerTest, List) { TEST(PrintStlContainerTest, List) {
skipping to change at line 1104 skipping to change at line 1116
// libCstd on Solaris), make_pair call would fail to compile as no // libCstd on Solaris), make_pair call would fail to compile as no
// implicit conversion is found. Thus explicit typename is used // implicit conversion is found. Thus explicit typename is used
// here instead. // here instead.
map1.insert(pair<const bool, int>(true, 0)); map1.insert(pair<const bool, int>(true, 0));
map1.insert(pair<const bool, int>(true, 1)); map1.insert(pair<const bool, int>(true, 1));
map1.insert(pair<const bool, int>(false, 2)); map1.insert(pair<const bool, int>(false, 2));
EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1)); EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
} }
TEST(PrintStlContainerTest, Set) { TEST(PrintStlContainerTest, Set) {
const unsigned int a[] = { 3, 0, 5 }; const unsigned int a[] = {3, 0, 5};
set<unsigned int> set1(a, a + 3); set<unsigned int> set1(a, a + 3);
EXPECT_EQ("{ 0, 3, 5 }", Print(set1)); EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
} }
TEST(PrintStlContainerTest, MultiSet) { TEST(PrintStlContainerTest, MultiSet) {
const int a[] = { 1, 1, 2, 5, 1 }; const int a[] = {1, 1, 2, 5, 1};
multiset<int> set1(a, a + 5); multiset<int> set1(a, a + 5);
EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1)); EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
} }
TEST(PrintStlContainerTest, SinglyLinkedList) { TEST(PrintStlContainerTest, SinglyLinkedList) {
int a[] = { 9, 2, 8 }; int a[] = {9, 2, 8};
const std::forward_list<int> ints(a, a + 3); const std::forward_list<int> ints(a, a + 3);
EXPECT_EQ("{ 9, 2, 8 }", Print(ints)); EXPECT_EQ("{ 9, 2, 8 }", Print(ints));
} }
TEST(PrintStlContainerTest, Pair) { TEST(PrintStlContainerTest, Pair) {
pair<const bool, int> p(true, 5); pair<const bool, int> p(true, 5);
EXPECT_EQ("(true, 5)", Print(p)); EXPECT_EQ("(true, 5)", Print(p));
} }
TEST(PrintStlContainerTest, Vector) { TEST(PrintStlContainerTest, Vector) {
vector<int> v; vector<int> v;
v.push_back(1); v.push_back(1);
v.push_back(2); v.push_back(2);
EXPECT_EQ("{ 1, 2 }", Print(v)); EXPECT_EQ("{ 1, 2 }", Print(v));
} }
TEST(PrintStlContainerTest, LongSequence) { TEST(PrintStlContainerTest, LongSequence) {
const int a[100] = { 1, 2, 3 }; const int a[100] = {1, 2, 3};
const vector<int> v(a, a + 100); const vector<int> v(a, a + 100);
EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " EXPECT_EQ(
"0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v)); "{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
"0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }",
Print(v));
} }
TEST(PrintStlContainerTest, NestedContainer) { TEST(PrintStlContainerTest, NestedContainer) {
const int a1[] = { 1, 2 }; const int a1[] = {1, 2};
const int a2[] = { 3, 4, 5 }; const int a2[] = {3, 4, 5};
const list<int> l1(a1, a1 + 2); const list<int> l1(a1, a1 + 2);
const list<int> l2(a2, a2 + 3); const list<int> l2(a2, a2 + 3);
vector<list<int> > v; vector<list<int>> v;
v.push_back(l1); v.push_back(l1);
v.push_back(l2); v.push_back(l2);
EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v)); EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
} }
TEST(PrintStlContainerTest, OneDimensionalNativeArray) { TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
const int a[3] = { 1, 2, 3 }; const int a[3] = {1, 2, 3};
NativeArray<int> b(a, 3, RelationToSourceReference()); NativeArray<int> b(a, 3, RelationToSourceReference());
EXPECT_EQ("{ 1, 2, 3 }", Print(b)); EXPECT_EQ("{ 1, 2, 3 }", Print(b));
} }
TEST(PrintStlContainerTest, TwoDimensionalNativeArray) { TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; const int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
NativeArray<int[3]> b(a, 2, RelationToSourceReference()); NativeArray<int[3]> b(a, 2, RelationToSourceReference());
EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b)); EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
} }
// Tests that a class named iterator isn't treated as a container. // Tests that a class named iterator isn't treated as a container.
struct iterator { struct iterator {
char x; char x;
}; };
skipping to change at line 1211 skipping to change at line 1225
::std::tuple<bool, int, int, int> t4(false, 2, 3, 4); ::std::tuple<bool, int, int, int> t4(false, 2, 3, 4);
EXPECT_EQ("(false, 2, 3, 4)", Print(t4)); EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
const char* const str = "8"; const char* const str = "8";
::std::tuple<bool, char, short, int32_t, int64_t, float, double, // NOLINT ::std::tuple<bool, char, short, int32_t, int64_t, float, double, // NOLINT
const char*, void*, std::string> const char*, void*, std::string>
t10(false, 'a', static_cast<short>(3), 4, 5, 1.5F, -2.5, str, // NOLINT t10(false, 'a', static_cast<short>(3), 4, 5, 1.5F, -2.5, str, // NOLINT
nullptr, "10"); nullptr, "10");
EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) + EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
" pointing to \"8\", NULL, \"10\")", " pointing to \"8\", NULL, \"10\")",
Print(t10)); Print(t10));
} }
// Nested tuples. // Nested tuples.
TEST(PrintStdTupleTest, NestedTuple) { TEST(PrintStdTupleTest, NestedTuple) {
::std::tuple< ::std::tuple<int, bool>, char> nested( ::std::tuple<::std::tuple<int, bool>, char> nested(::std::make_tuple(5, true),
::std::make_tuple(5, true), 'a'); 'a');
EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested)); EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
} }
TEST(PrintNullptrT, Basic) { TEST(PrintNullptrT, Basic) { EXPECT_EQ("(nullptr)", Print(nullptr)); }
EXPECT_EQ("(nullptr)", Print(nullptr));
}
TEST(PrintReferenceWrapper, Printable) { TEST(PrintReferenceWrapper, Printable) {
int x = 5; int x = 5;
EXPECT_EQ("@" + PrintPointer(&x) + " 5", Print(std::ref(x))); EXPECT_EQ("@" + PrintPointer(&x) + " 5", Print(std::ref(x)));
EXPECT_EQ("@" + PrintPointer(&x) + " 5", Print(std::cref(x))); EXPECT_EQ("@" + PrintPointer(&x) + " 5", Print(std::cref(x)));
} }
TEST(PrintReferenceWrapper, Unprintable) { TEST(PrintReferenceWrapper, Unprintable) {
::foo::UnprintableInFoo up; ::foo::UnprintableInFoo up;
EXPECT_EQ( EXPECT_EQ(
skipping to change at line 1248 skipping to change at line 1260
EXPECT_EQ( EXPECT_EQ(
"@" + PrintPointer(&up) + "@" + PrintPointer(&up) +
" 16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>", " 16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
Print(std::cref(up))); Print(std::cref(up)));
} }
// Tests printing user-defined unprintable types. // Tests printing user-defined unprintable types.
// Unprintable types in the global namespace. // Unprintable types in the global namespace.
TEST(PrintUnprintableTypeTest, InGlobalNamespace) { TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
EXPECT_EQ("1-byte object <00>", EXPECT_EQ("1-byte object <00>", Print(UnprintableTemplateInGlobal<char>()));
Print(UnprintableTemplateInGlobal<char>()));
} }
// Unprintable types in a user namespace. // Unprintable types in a user namespace.
TEST(PrintUnprintableTypeTest, InUserNamespace) { TEST(PrintUnprintableTypeTest, InUserNamespace) {
EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>", EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
Print(::foo::UnprintableInFoo())); Print(::foo::UnprintableInFoo()));
} }
// Unprintable types are that too big to be printed completely. // Unprintable types are that too big to be printed completely.
struct Big { struct Big {
Big() { memset(array, 0, sizeof(array)); } Big() { memset(array, 0, sizeof(array)); }
char array[257]; char array[257];
}; };
TEST(PrintUnpritableTypeTest, BigObject) { TEST(PrintUnpritableTypeTest, BigObject) {
EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 " EXPECT_EQ(
"00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " "257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
"00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
"00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 " "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
"00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
"00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
"00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>", "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
Print(Big())); "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
Print(Big()));
} }
// Tests printing user-defined streamable types. // Tests printing user-defined streamable types.
// Streamable types in the global namespace. // Streamable types in the global namespace.
TEST(PrintStreamableTypeTest, InGlobalNamespace) { TEST(PrintStreamableTypeTest, InGlobalNamespace) {
StreamableInGlobal x; StreamableInGlobal x;
EXPECT_EQ("StreamableInGlobal", Print(x)); EXPECT_EQ("StreamableInGlobal", Print(x));
EXPECT_EQ("StreamableInGlobal*", Print(&x)); EXPECT_EQ("StreamableInGlobal*", Print(&x));
} }
skipping to change at line 1316 skipping to change at line 1328
// operator. // operator.
TEST(PrintStreamableTypeTest, PathLikeInUserNamespace) { TEST(PrintStreamableTypeTest, PathLikeInUserNamespace) {
::foo::PathLike x; ::foo::PathLike x;
EXPECT_EQ("Streamable-PathLike", Print(x)); EXPECT_EQ("Streamable-PathLike", Print(x));
const ::foo::PathLike cx; const ::foo::PathLike cx;
EXPECT_EQ("Streamable-PathLike", Print(cx)); EXPECT_EQ("Streamable-PathLike", Print(cx));
} }
// Tests printing user-defined types that have a PrintTo() function. // Tests printing user-defined types that have a PrintTo() function.
TEST(PrintPrintableTypeTest, InUserNamespace) { TEST(PrintPrintableTypeTest, InUserNamespace) {
EXPECT_EQ("PrintableViaPrintTo: 0", EXPECT_EQ("PrintableViaPrintTo: 0", Print(::foo::PrintableViaPrintTo()));
Print(::foo::PrintableViaPrintTo()));
} }
// Tests printing a pointer to a user-defined type that has a << // Tests printing a pointer to a user-defined type that has a <<
// operator for its pointer. // operator for its pointer.
TEST(PrintPrintableTypeTest, PointerInUserNamespace) { TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
::foo::PointerPrintable x; ::foo::PointerPrintable x;
EXPECT_EQ("PointerPrintable*", Print(&x)); EXPECT_EQ("PointerPrintable*", Print(&x));
} }
// Tests printing user-defined class template that have a PrintTo() function. // Tests printing user-defined class template that have a PrintTo() function.
skipping to change at line 1339 skipping to change at line 1350
EXPECT_EQ("PrintableViaPrintToTemplate: 5", EXPECT_EQ("PrintableViaPrintToTemplate: 5",
Print(::foo::PrintableViaPrintToTemplate<int>(5))); Print(::foo::PrintableViaPrintToTemplate<int>(5)));
} }
// Tests that the universal printer prints both the address and the // Tests that the universal printer prints both the address and the
// value of a reference. // value of a reference.
TEST(PrintReferenceTest, PrintsAddressAndValue) { TEST(PrintReferenceTest, PrintsAddressAndValue) {
int n = 5; int n = 5;
EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n)); EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
int a[2][3] = { int a[2][3] = {{0, 1, 2}, {3, 4, 5}};
{ 0, 1, 2 },
{ 3, 4, 5 }
};
EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }", EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
PrintByRef(a)); PrintByRef(a));
const ::foo::UnprintableInFoo x; const ::foo::UnprintableInFoo x;
EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object " EXPECT_EQ("@" + PrintPointer(&x) +
"<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>", " 16-byte object "
"<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
PrintByRef(x)); PrintByRef(x));
} }
// Tests that the universal printer prints a function pointer passed by // Tests that the universal printer prints a function pointer passed by
// reference. // reference.
TEST(PrintReferenceTest, HandlesFunctionPointer) { TEST(PrintReferenceTest, HandlesFunctionPointer) {
void (*fp)(int n) = &MyFunction; void (*fp)(int n) = &MyFunction;
const std::string fp_pointer_string = const std::string fp_pointer_string =
PrintPointer(reinterpret_cast<const void*>(&fp)); PrintPointer(reinterpret_cast<const void*>(&fp));
// We cannot directly cast &MyFunction to const void* because the // We cannot directly cast &MyFunction to const void* because the
// standard disallows casting between pointers to functions and // standard disallows casting between pointers to functions and
// pointers to objects, and some compilers (e.g. GCC 3.4) enforce // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
// this limitation. // this limitation.
const std::string fp_string = PrintPointer(reinterpret_cast<const void*>( const std::string fp_string = PrintPointer(reinterpret_cast<const void*>(
reinterpret_cast<internal::BiggestInt>(fp))); reinterpret_cast<internal::BiggestInt>(fp)));
EXPECT_EQ("@" + fp_pointer_string + " " + fp_string, EXPECT_EQ("@" + fp_pointer_string + " " + fp_string, PrintByRef(fp));
PrintByRef(fp));
} }
// Tests that the universal printer prints a member function pointer // Tests that the universal printer prints a member function pointer
// passed by reference. // passed by reference.
TEST(PrintReferenceTest, HandlesMemberFunctionPointer) { TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
int (Foo::*p)(char ch) = &Foo::MyMethod; int (Foo::*p)(char ch) = &Foo::MyMethod;
EXPECT_TRUE(HasPrefix( EXPECT_TRUE(HasPrefix(PrintByRef(p),
PrintByRef(p), "@" + PrintPointer(reinterpret_cast<const void*>(&p)) +
"@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " + " " + Print(sizeof(p)) + "-byte object "));
Print(sizeof(p)) + "-byte object "));
char (Foo::*p2)(int n) = &Foo::MyVirtualMethod; char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
EXPECT_TRUE(HasPrefix( EXPECT_TRUE(HasPrefix(PrintByRef(p2),
PrintByRef(p2), "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) +
"@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " + " " + Print(sizeof(p2)) + "-byte object "));
Print(sizeof(p2)) + "-byte object "));
} }
// Tests that the universal printer prints a member variable pointer // Tests that the universal printer prints a member variable pointer
// passed by reference. // passed by reference.
TEST(PrintReferenceTest, HandlesMemberVariablePointer) { TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
int Foo::*p = &Foo::value; // NOLINT int Foo::*p = &Foo::value; // NOLINT
EXPECT_TRUE(HasPrefix( EXPECT_TRUE(HasPrefix(PrintByRef(p), "@" + PrintPointer(&p) + " " +
PrintByRef(p), Print(sizeof(p)) + "-byte object "));
"@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
} }
// Tests that FormatForComparisonFailureMessage(), which is used to print // Tests that FormatForComparisonFailureMessage(), which is used to print
// an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion
// fails, formats the operand in the desired way. // fails, formats the operand in the desired way.
// scalar // scalar
TEST(FormatForComparisonFailureMessageTest, WorksForScalar) { TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
EXPECT_STREQ("123", EXPECT_STREQ("123", FormatForComparisonFailureMessage(123, 124).c_str());
FormatForComparisonFailureMessage(123, 124).c_str());
} }
// non-char pointer // non-char pointer
TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) { TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
int n = 0; int n = 0;
EXPECT_EQ(PrintPointer(&n), EXPECT_EQ(PrintPointer(&n),
FormatForComparisonFailureMessage(&n, &n).c_str()); FormatForComparisonFailureMessage(&n, &n).c_str());
} }
// non-char array // non-char array
TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) { TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
// In expression 'array == x', 'array' is compared by pointer. // In expression 'array == x', 'array' is compared by pointer.
// Therefore we want to print an array operand as a pointer. // Therefore we want to print an array operand as a pointer.
int n[] = { 1, 2, 3 }; int n[] = {1, 2, 3};
EXPECT_EQ(PrintPointer(n), EXPECT_EQ(PrintPointer(n), FormatForComparisonFailureMessage(n, n).c_str());
FormatForComparisonFailureMessage(n, n).c_str());
} }
// Tests formatting a char pointer when it's compared with another pointer. // Tests formatting a char pointer when it's compared with another pointer.
// In this case we want to print it as a raw pointer, as the comparison is by // In this case we want to print it as a raw pointer, as the comparison is by
// pointer. // pointer.
// char pointer vs pointer // char pointer vs pointer
TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) { TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
// In expression 'p == x', where 'p' and 'x' are (const or not) char // In expression 'p == x', where 'p' and 'x' are (const or not) char
// pointers, the operands are compared by pointer. Therefore we // pointers, the operands are compared by pointer. Therefore we
// want to print 'p' as a pointer instead of a C string (we don't // want to print 'p' as a pointer instead of a C string (we don't
// even know if it's supposed to point to a valid C string). // even know if it's supposed to point to a valid C string).
// const char* // const char*
const char* s = "hello"; const char* s = "hello";
EXPECT_EQ(PrintPointer(s), EXPECT_EQ(PrintPointer(s), FormatForComparisonFailureMessage(s, s).c_str());
FormatForComparisonFailureMessage(s, s).c_str());
// char* // char*
char ch = 'a'; char ch = 'a';
EXPECT_EQ(PrintPointer(&ch), EXPECT_EQ(PrintPointer(&ch),
FormatForComparisonFailureMessage(&ch, &ch).c_str()); FormatForComparisonFailureMessage(&ch, &ch).c_str());
} }
// wchar_t pointer vs pointer // wchar_t pointer vs pointer
TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) { TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
// In expression 'p == x', where 'p' and 'x' are (const or not) char // In expression 'p == x', where 'p' and 'x' are (const or not) char
// pointers, the operands are compared by pointer. Therefore we // pointers, the operands are compared by pointer. Therefore we
// want to print 'p' as a pointer instead of a wide C string (we don't // want to print 'p' as a pointer instead of a wide C string (we don't
// even know if it's supposed to point to a valid wide C string). // even know if it's supposed to point to a valid wide C string).
// const wchar_t* // const wchar_t*
const wchar_t* s = L"hello"; const wchar_t* s = L"hello";
EXPECT_EQ(PrintPointer(s), EXPECT_EQ(PrintPointer(s), FormatForComparisonFailureMessage(s, s).c_str());
FormatForComparisonFailureMessage(s, s).c_str());
// wchar_t* // wchar_t*
wchar_t ch = L'a'; wchar_t ch = L'a';
EXPECT_EQ(PrintPointer(&ch), EXPECT_EQ(PrintPointer(&ch),
FormatForComparisonFailureMessage(&ch, &ch).c_str()); FormatForComparisonFailureMessage(&ch, &ch).c_str());
} }
// Tests formatting a char pointer when it's compared to a string object. // Tests formatting a char pointer when it's compared to a string object.
// In this case we want to print the char pointer as a C string. // In this case we want to print the char pointer as a C string.
skipping to change at line 1548 skipping to change at line 1549
EXPECT_STREQ( EXPECT_STREQ(
"L\"hi \\\"w\"", // The content should be escaped. "L\"hi \\\"w\"", // The content should be escaped.
// Embedded NUL terminates the string. // Embedded NUL terminates the string.
FormatForComparisonFailureMessage(str, ::std::wstring()).c_str()); FormatForComparisonFailureMessage(str, ::std::wstring()).c_str());
} }
#endif #endif
// Useful for testing PrintToString(). We cannot use EXPECT_EQ() // Useful for testing PrintToString(). We cannot use EXPECT_EQ()
// there as its implementation uses PrintToString(). The caller must // there as its implementation uses PrintToString(). The caller must
// ensure that 'value' has no side effect. // ensure that 'value' has no side effect.
#define EXPECT_PRINT_TO_STRING_(value, expected_string) \ #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
EXPECT_TRUE(PrintToString(value) == (expected_string)) \ EXPECT_TRUE(PrintToString(value) == (expected_string)) \
<< " where " #value " prints as " << (PrintToString(value)) << " where " #value " prints as " << (PrintToString(value))
TEST(PrintToStringTest, WorksForScalar) { TEST(PrintToStringTest, WorksForScalar) { EXPECT_PRINT_TO_STRING_(123, "123"); }
EXPECT_PRINT_TO_STRING_(123, "123");
}
TEST(PrintToStringTest, WorksForPointerToConstChar) { TEST(PrintToStringTest, WorksForPointerToConstChar) {
const char* p = "hello"; const char* p = "hello";
EXPECT_PRINT_TO_STRING_(p, "\"hello\""); EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
} }
TEST(PrintToStringTest, WorksForPointerToNonConstChar) { TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
char s[] = "hello"; char s[] = "hello";
char* p = s; char* p = s;
EXPECT_PRINT_TO_STRING_(p, "\"hello\""); EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
skipping to change at line 1579 skipping to change at line 1578
EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\""); EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\"");
} }
TEST(PrintToStringTest, EscapesForPointerToNonConstChar) { TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
char s[] = "hello\1"; char s[] = "hello\1";
char* p = s; char* p = s;
EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\""); EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\"");
} }
TEST(PrintToStringTest, WorksForArray) { TEST(PrintToStringTest, WorksForArray) {
int n[3] = { 1, 2, 3 }; int n[3] = {1, 2, 3};
EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }"); EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
} }
TEST(PrintToStringTest, WorksForCharArray) { TEST(PrintToStringTest, WorksForCharArray) {
char s[] = "hello"; char s[] = "hello";
EXPECT_PRINT_TO_STRING_(s, "\"hello\""); EXPECT_PRINT_TO_STRING_(s, "\"hello\"");
} }
TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) { TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
const char str_with_nul[] = "hello\0 world"; const char str_with_nul[] = "hello\0 world";
EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\""); EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\"");
char mutable_str_with_nul[] = "hello\0 world"; char mutable_str_with_nul[] = "hello\0 world";
EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\""); EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\"");
} }
TEST(PrintToStringTest, ContainsNonLatin) { TEST(PrintToStringTest, ContainsNonLatin) {
// Sanity test with valid UTF-8. Prints both in hex and as text. // Test with valid UTF-8. Prints both in hex and as text.
std::string non_ascii_str = ::std::string("오전 4:30"); std::string non_ascii_str = ::std::string("오전 4:30");
EXPECT_PRINT_TO_STRING_(non_ascii_str, EXPECT_PRINT_TO_STRING_(non_ascii_str,
"\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n" "\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
" As Text: \"오전 4:30\""); " As Text: \"오전 4:30\"");
non_ascii_str = ::std::string("From ä — ẑ"); non_ascii_str = ::std::string("From ä — ẑ");
EXPECT_PRINT_TO_STRING_(non_ascii_str, EXPECT_PRINT_TO_STRING_(non_ascii_str,
"\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\"" "\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
"\n As Text: \"From ä — ẑ\""); "\n As Text: \"From ä — ẑ\"");
} }
TEST(IsValidUTF8Test, IllFormedUTF8) { TEST(IsValidUTF8Test, IllFormedUTF8) {
// The following test strings are ill-formed UTF-8 and are printed // The following test strings are ill-formed UTF-8 and are printed
// as hex only (or ASCII, in case of ASCII bytes) because IsValidUTF8() is // as hex only (or ASCII, in case of ASCII bytes) because IsValidUTF8() is
// expected to fail, thus output does not contain "As Text:". // expected to fail, thus output does not contain "As Text:".
static const char *const kTestdata[][2] = { static const char* const kTestdata[][2] = {
// 2-byte lead byte followed by a single-byte character. // 2-byte lead byte followed by a single-byte character.
{"\xC3\x74", "\"\\xC3t\""}, {"\xC3\x74", "\"\\xC3t\""},
// Valid 2-byte character followed by an orphan trail byte. // Valid 2-byte character followed by an orphan trail byte.
{"\xC3\x84\xA4", "\"\\xC3\\x84\\xA4\""}, {"\xC3\x84\xA4", "\"\\xC3\\x84\\xA4\""},
// Lead byte without trail byte. // Lead byte without trail byte.
{"abc\xC3", "\"abc\\xC3\""}, {"abc\xC3", "\"abc\\xC3\""},
// 3-byte lead byte, single-byte character, orphan trail byte. // 3-byte lead byte, single-byte character, orphan trail byte.
{"x\xE2\x70\x94", "\"x\\xE2p\\x94\""}, {"x\xE2\x70\x94", "\"x\\xE2p\\x94\""},
// Truncated 3-byte character. // Truncated 3-byte character.
{"\xE2\x80", "\"\\xE2\\x80\""}, {"\xE2\x80", "\"\\xE2\\x80\""},
// Truncated 3-byte character followed by valid 2-byte char. // Truncated 3-byte character followed by valid 2-byte char.
{"\xE2\x80\xC3\x84", "\"\\xE2\\x80\\xC3\\x84\""}, {"\xE2\x80\xC3\x84", "\"\\xE2\\x80\\xC3\\x84\""},
// Truncated 3-byte character followed by a single-byte character. // Truncated 3-byte character followed by a single-byte character.
{"\xE2\x80\x7A", "\"\\xE2\\x80z\""}, {"\xE2\x80\x7A", "\"\\xE2\\x80z\""},
// 3-byte lead byte followed by valid 3-byte character. // 3-byte lead byte followed by valid 3-byte character.
{"\xE2\xE2\x80\x94", "\"\\xE2\\xE2\\x80\\x94\""}, {"\xE2\xE2\x80\x94", "\"\\xE2\\xE2\\x80\\x94\""},
// 4-byte lead byte followed by valid 3-byte character. // 4-byte lead byte followed by valid 3-byte character.
{"\xF0\xE2\x80\x94", "\"\\xF0\\xE2\\x80\\x94\""}, {"\xF0\xE2\x80\x94", "\"\\xF0\\xE2\\x80\\x94\""},
// Truncated 4-byte character. // Truncated 4-byte character.
{"\xF0\xE2\x80", "\"\\xF0\\xE2\\x80\""}, {"\xF0\xE2\x80", "\"\\xF0\\xE2\\x80\""},
// Invalid UTF-8 byte sequences embedded in other chars. // Invalid UTF-8 byte sequences embedded in other chars.
{"abc\xE2\x80\x94\xC3\x74xyc", "\"abc\\xE2\\x80\\x94\\xC3txyc\""}, {"abc\xE2\x80\x94\xC3\x74xyc", "\"abc\\xE2\\x80\\x94\\xC3txyc\""},
{"abc\xC3\x84\xE2\x80\xC3\x84xyz", {"abc\xC3\x84\xE2\x80\xC3\x84xyz",
"\"abc\\xC3\\x84\\xE2\\x80\\xC3\\x84xyz\""}, "\"abc\\xC3\\x84\\xE2\\x80\\xC3\\x84xyz\""},
// Non-shortest UTF-8 byte sequences are also ill-formed. // Non-shortest UTF-8 byte sequences are also ill-formed.
// The classics: xC0, xC1 lead byte. // The classics: xC0, xC1 lead byte.
{"\xC0\x80", "\"\\xC0\\x80\""}, {"\xC0\x80", "\"\\xC0\\x80\""},
{"\xC1\x81", "\"\\xC1\\x81\""}, {"\xC1\x81", "\"\\xC1\\x81\""},
// Non-shortest sequences. // Non-shortest sequences.
{"\xE0\x80\x80", "\"\\xE0\\x80\\x80\""}, {"\xE0\x80\x80", "\"\\xE0\\x80\\x80\""},
{"\xf0\x80\x80\x80", "\"\\xF0\\x80\\x80\\x80\""}, {"\xf0\x80\x80\x80", "\"\\xF0\\x80\\x80\\x80\""},
// Last valid code point before surrogate range, should be printed as text, // Last valid code point before surrogate range, should be printed as
// too. // text,
{"\xED\x9F\xBF", "\"\\xED\\x9F\\xBF\"\n As Text: \"퟿\""}, // too.
// Start of surrogate lead. Surrogates are not printed as text. {"\xED\x9F\xBF", "\"\\xED\\x9F\\xBF\"\n As Text: \"퟿\""},
{"\xED\xA0\x80", "\"\\xED\\xA0\\x80\""}, // Start of surrogate lead. Surrogates are not printed as text.
// Last non-private surrogate lead. {"\xED\xA0\x80", "\"\\xED\\xA0\\x80\""},
{"\xED\xAD\xBF", "\"\\xED\\xAD\\xBF\""}, // Last non-private surrogate lead.
// First private-use surrogate lead. {"\xED\xAD\xBF", "\"\\xED\\xAD\\xBF\""},
{"\xED\xAE\x80", "\"\\xED\\xAE\\x80\""}, // First private-use surrogate lead.
// Last private-use surrogate lead. {"\xED\xAE\x80", "\"\\xED\\xAE\\x80\""},
{"\xED\xAF\xBF", "\"\\xED\\xAF\\xBF\""}, // Last private-use surrogate lead.
// Mid-point of surrogate trail. {"\xED\xAF\xBF", "\"\\xED\\xAF\\xBF\""},
{"\xED\xB3\xBF", "\"\\xED\\xB3\\xBF\""}, // Mid-point of surrogate trail.
// First valid code point after surrogate range, should be printed as text, {"\xED\xB3\xBF", "\"\\xED\\xB3\\xBF\""},
// too. // First valid code point after surrogate range, should be printed as
{"\xEE\x80\x80", "\"\\xEE\\x80\\x80\"\n As Text: \"\""} // text,
}; // too.
{"\xEE\x80\x80", "\"\\xEE\\x80\\x80\"\n As Text: \"\""}};
for (int i = 0; i < int(sizeof(kTestdata)/sizeof(kTestdata[0])); ++i) { for (int i = 0; i < int(sizeof(kTestdata) / sizeof(kTestdata[0])); ++i) {
EXPECT_PRINT_TO_STRING_(kTestdata[i][0], kTestdata[i][1]); EXPECT_PRINT_TO_STRING_(kTestdata[i][0], kTestdata[i][1]);
} }
} }
#undef EXPECT_PRINT_TO_STRING_ #undef EXPECT_PRINT_TO_STRING_
TEST(UniversalTersePrintTest, WorksForNonReference) { TEST(UniversalTersePrintTest, WorksForNonReference) {
::std::stringstream ss; ::std::stringstream ss;
UniversalTersePrint(123, &ss); UniversalTersePrint(123, &ss);
EXPECT_EQ("123", ss.str()); EXPECT_EQ("123", ss.str());
skipping to change at line 1812 skipping to change at line 1812
EXPECT_EQ("(" + PrintPointer(p.get()) + ")", EXPECT_EQ("(" + PrintPointer(p.get()) + ")",
PrintToString(std::shared_ptr<void>(p.get(), [](void*) {}))); PrintToString(std::shared_ptr<void>(p.get(), [](void*) {})));
} }
TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) { TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple()); Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
EXPECT_EQ(0u, result.size()); EXPECT_EQ(0u, result.size());
} }
TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) { TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
Strings result = UniversalTersePrintTupleFieldsToStrings( Strings result =
::std::make_tuple(1)); UniversalTersePrintTupleFieldsToStrings(::std::make_tuple(1));
ASSERT_EQ(1u, result.size()); ASSERT_EQ(1u, result.size());
EXPECT_EQ("1", result[0]); EXPECT_EQ("1", result[0]);
} }
TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) { TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
Strings result = UniversalTersePrintTupleFieldsToStrings( Strings result =
::std::make_tuple(1, 'a')); UniversalTersePrintTupleFieldsToStrings(::std::make_tuple(1, 'a'));
ASSERT_EQ(2u, result.size()); ASSERT_EQ(2u, result.size());
EXPECT_EQ("1", result[0]); EXPECT_EQ("1", result[0]);
EXPECT_EQ("'a' (97, 0x61)", result[1]); EXPECT_EQ("'a' (97, 0x61)", result[1]);
} }
TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) { TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
const int n = 1; const int n = 1;
Strings result = UniversalTersePrintTupleFieldsToStrings( Strings result = UniversalTersePrintTupleFieldsToStrings(
::std::tuple<const int&, const char*>(n, "a")); ::std::tuple<const int&, const char*>(n, "a"));
ASSERT_EQ(2u, result.size()); ASSERT_EQ(2u, result.size());
skipping to change at line 1869 skipping to change at line 1869
EXPECT_EQ("value of type " + ExpectedTypeName<int>(), PrintToString(any)); EXPECT_EQ("value of type " + ExpectedTypeName<int>(), PrintToString(any));
any = val2; any = val2;
EXPECT_EQ("value of type " + ExpectedTypeName<std::string>(), EXPECT_EQ("value of type " + ExpectedTypeName<std::string>(),
PrintToString(any)); PrintToString(any));
} }
#endif // GTEST_INTERNAL_HAS_ANY #endif // GTEST_INTERNAL_HAS_ANY
#if GTEST_INTERNAL_HAS_OPTIONAL #if GTEST_INTERNAL_HAS_OPTIONAL
TEST(PrintOptionalTest, Basic) { TEST(PrintOptionalTest, Basic) {
EXPECT_EQ("(nullopt)", PrintToString(internal::Nullopt()));
internal::Optional<int> value; internal::Optional<int> value;
EXPECT_EQ("(nullopt)", PrintToString(value)); EXPECT_EQ("(nullopt)", PrintToString(value));
value = {7}; value = {7};
EXPECT_EQ("(7)", PrintToString(value)); EXPECT_EQ("(7)", PrintToString(value));
EXPECT_EQ("(1.1)", PrintToString(internal::Optional<double>{1.1})); EXPECT_EQ("(1.1)", PrintToString(internal::Optional<double>{1.1}));
EXPECT_EQ("(\"A\")", PrintToString(internal::Optional<std::string>{"A"})); EXPECT_EQ("(\"A\")", PrintToString(internal::Optional<std::string>{"A"}));
} }
#endif // GTEST_INTERNAL_HAS_OPTIONAL #endif // GTEST_INTERNAL_HAS_OPTIONAL
#if GTEST_INTERNAL_HAS_VARIANT #if GTEST_INTERNAL_HAS_VARIANT
 End of changes. 73 change blocks. 
196 lines changed or deleted 197 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)