40#include <forward_list>
48#include <unordered_map>
49#include <unordered_set>
76 return os << (e ==
kEWS1 ?
"kEWS1" :
"invalid");
85 *os << (e ==
kEWPT1 ?
"kEWPT1" :
"invalid");
103 os <<
"ChildClassWithStreamOperator";
122 os <<
"StreamableInGlobal";
126 os <<
"StreamableInGlobal*";
135 double z()
const {
return z_; }
148 *os <<
"PrintableViaPrintTo: " <<
x.value;
156 const PointerPrintable* ) {
157 return os <<
"PointerPrintable*";
166 const T&
value()
const {
return value_; }
173 *os <<
"PrintableViaPrintToTemplate: " <<
x.value();
182 const T&
value()
const {
return value_; }
190 return os <<
"StreamableTemplateInFoo: " <<
x.value();
197template <
typename OutputStream>
200 os <<
"TemplatedStreamableInFoo";
225 return os <<
"Streamable-PathLike";
236 explicit Wrapper(T&&
value) : value_(
std::forward<T>(
value)) {}
238 const T&
value()
const {
return value_; }
250 static void Print(
const Wrapper<T>& w, ::std::ostream* os) {
259namespace gtest_printers_test {
263using ::std::make_pair;
265using ::std::multimap;
266using ::std::multiset;
273using ::testing::internal::NativeArray;
274using ::testing::internal::RelationToSourceReference;
277using ::testing::internal::UniversalPrinter;
285 ::std::stringstream ss;
295 ::std::stringstream ss;
331TEST(PrintCharTest, PlainChar) {
351TEST(PrintCharTest, SignedChar) {
354 Print(
static_cast<signed char>(-50)));
358TEST(PrintCharTest, UnsignedChar) {
361 Print(
static_cast<unsigned char>(
'b')));
373TEST(PrintCharTest, Char8) {
387TEST(PrintBuiltInTypeTest, Wchar_t) {
404 EXPECT_EQ(
"L'\\x576' (1398)",
Print(
static_cast<wchar_t>(0x576)));
405 EXPECT_EQ(
"L'\\xC74D' (51021)",
Print(
static_cast<wchar_t>(0xC74D)));
409TEST(PrintTypeSizeTest, Wchar_t) {
410 EXPECT_LT(
sizeof(
wchar_t),
sizeof(int64_t));
414TEST(PrintBuiltInTypeTest, Integer) {
415 EXPECT_EQ(
"'\\xFF' (255)",
Print(
static_cast<unsigned char>(255)));
416 EXPECT_EQ(
"'\\x80' (-128)",
Print(
static_cast<signed char>(-128)));
420 Print(std::numeric_limits<uint32_t>::max()));
422 Print(std::numeric_limits<int32_t>::min()));
424 Print(std::numeric_limits<uint64_t>::max()));
426 Print(std::numeric_limits<int64_t>::min()));
429 Print(std::numeric_limits<char8_t>::min()));
431 Print(std::numeric_limits<char8_t>::max()));
434 Print(std::numeric_limits<char16_t>::min()));
436 Print(std::numeric_limits<char16_t>::max()));
438 Print(std::numeric_limits<char32_t>::min()));
440 Print(std::numeric_limits<char32_t>::max()));
444TEST(PrintBuiltInTypeTest, Size_t) {
453TEST(PrintBuiltInTypeTest, FloatingPoints) {
461static std::string PrintPointer(
const void*
p) {
462 ::std::stringstream expected_result_stream;
463 expected_result_stream <<
p;
464 return expected_result_stream.str();
471 const char*
p =
"World";
476TEST(PrintCStringTest, NonConst) {
478 EXPECT_EQ(PrintPointer(
p) +
" pointing to \"Hi\"",
479 Print(
static_cast<char*
>(
p)));
484 const char*
p =
nullptr;
489TEST(PrintCStringTest, EscapesProperly) {
490 const char*
p =
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
491 EXPECT_EQ(PrintPointer(
p) +
" pointing to \"'\\\"?\\\\\\a\\b\\f"
492 "\\n\\r\\t\\v\\x7F\\xFF a\"",
498TEST(PrintU8StringTest, Const) {
499 const char8_t*
p = u8
"界";
500 EXPECT_EQ(PrintPointer(
p) +
" pointing to u8\"\\xE7\\x95\\x8C\"",
Print(
p));
504TEST(PrintU8StringTest, NonConst) {
506 EXPECT_EQ(PrintPointer(
p) +
" pointing to u8\"\\xE4\\xB8\\x96\"",
507 Print(
static_cast<char8_t*
>(
p)));
511TEST(PrintU8StringTest, Null) {
512 const char8_t*
p =
nullptr;
517TEST(PrintU8StringTest, EscapesProperly) {
518 const char8_t*
p = u8
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 世界";
520 " pointing to u8\"'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\x7F\\xFF "
521 "hello \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
527TEST(PrintU16StringTest, Const) {
528 const char16_t*
p = u
"界";
533TEST(PrintU16StringTest, NonConst) {
535 EXPECT_EQ(PrintPointer(
p) +
" pointing to u\"\\x4E16\"",
536 Print(
static_cast<char16_t*
>(
p)));
540TEST(PrintU16StringTest, Null) {
541 const char16_t*
p =
nullptr;
546TEST(PrintU16StringTest, EscapesProperly) {
547 const char16_t*
p = u
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 世界";
549 " pointing to u\"'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\x7F\\xFF "
550 "hello \\x4E16\\x754C\"",
555TEST(PrintU32StringTest, Const) {
556 const char32_t*
p = U
"🗺️";
561TEST(PrintU32StringTest, NonConst) {
563 EXPECT_EQ(PrintPointer(
p) +
" pointing to U\"\\x1F30C\"",
564 Print(
static_cast<char32_t*
>(
p)));
568TEST(PrintU32StringTest, Null) {
569 const char32_t*
p =
nullptr;
574TEST(PrintU32StringTest, EscapesProperly) {
575 const char32_t*
p = U
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 🗺️";
577 " pointing to U\"'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\x7F\\xFF "
578 "hello \\x1F5FA\\xFE0F\"",
588#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
591TEST(PrintWideCStringTest, Const) {
592 const wchar_t*
p = L
"World";
597TEST(PrintWideCStringTest, NonConst) {
599 EXPECT_EQ(PrintPointer(
p) +
" pointing to L\"Hi\"",
600 Print(
static_cast<wchar_t*
>(
p)));
604TEST(PrintWideCStringTest, Null) {
605 const wchar_t*
p =
nullptr;
610TEST(PrintWideCStringTest, EscapesProperly) {
611 const wchar_t s[] = {
'\'',
'"',
'?',
'\\',
'\a',
'\b',
'\f',
'\n',
'\r',
612 '\t',
'\v', 0xD3, 0x576, 0x8D3, 0xC74D,
' ',
'a',
'\0'};
613 EXPECT_EQ(PrintPointer(s) +
" pointing to L\"'\\\"?\\\\\\a\\b\\f"
614 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
615 Print(
static_cast<const wchar_t*
>(s)));
622TEST(PrintCharPointerTest, SignedChar) {
623 signed char*
p =
reinterpret_cast<signed char*
>(0x1234);
630TEST(PrintCharPointerTest, ConstSignedChar) {
631 signed char*
p =
reinterpret_cast<signed char*
>(0x1234);
638TEST(PrintCharPointerTest, UnsignedChar) {
639 unsigned char*
p =
reinterpret_cast<unsigned char*
>(0x1234);
646TEST(PrintCharPointerTest, ConstUnsignedChar) {
647 const unsigned char*
p =
reinterpret_cast<const unsigned char*
>(0x1234);
657 bool*
p =
reinterpret_cast<bool*
>(0xABCD);
664TEST(PrintPointerToBuiltInTypeTest, Void) {
665 void*
p =
reinterpret_cast<void*
>(0xABCD);
672TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
673 const void*
p =
reinterpret_cast<const void*
>(0xABCD);
680TEST(PrintPointerToPointerTest, IntPointerPointer) {
681 int**
p =
reinterpret_cast<int**
>(0xABCD);
691TEST(PrintPointerTest, NonMemberFunctionPointer) {
697 PrintPointer(
reinterpret_cast<const void*
>(
700 int (*
p)(bool) = NULL;
706template <
typename StringType>
707AssertionResult
HasPrefix(
const StringType& str,
const StringType& prefix) {
708 if (str.find(prefix, 0) == 0)
711 const bool is_wide_string =
sizeof(prefix[0]) > 1;
712 const char*
const begin_string_quote = is_wide_string ?
"L\"" :
"\"";
714 << begin_string_quote << prefix <<
"\" is not a prefix of "
715 << begin_string_quote << str <<
"\"\n";
732TEST(PrintPointerTest, MemberVariablePointer) {
737 Print(
sizeof(
p)) +
"-byte object "));
744TEST(PrintPointerTest, MemberFunctionPointer) {
750 int (
Foo::*
p)(char) = NULL;
752 Print(
sizeof(
p)) +
"-byte object "));
759template <
typename T,
size_t N>
765TEST(PrintArrayTest, OneDimensionalArray) {
766 int a[5] = { 1, 2, 3, 4, 5 };
771TEST(PrintArrayTest, TwoDimensionalArray) {
776 EXPECT_EQ(
"{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }",
PrintArrayHelper(a));
780TEST(PrintArrayTest, ConstArray) {
781 const bool a[1] = {
false };
786TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
788 char a[] = {
'H',
'\0',
'i' };
793TEST(PrintArrayTest, CharArrayWithTerminatingNul) {
794 const char a[] =
"\0Hi";
800TEST(PrintArrayTest, Char8ArrayWithNoTerminatingNul) {
802 const char8_t a[] = {u8
'H', u8
'\0', u8
'i'};
807TEST(PrintArrayTest, Char8ArrayWithTerminatingNul) {
808 const char8_t a[] = u8
"\0世界";
810 "u8\"\\0\\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
816TEST(PrintArrayTest, Char16ArrayWithNoTerminatingNul) {
818 const char16_t a[] = {u
'こ', u
'\0', u
'ん', u
'に', u
'ち', u
'は'};
819 EXPECT_EQ(
"u\"\\x3053\\0\\x3093\\x306B\\x3061\\x306F\" (no terminating NUL)",
824TEST(PrintArrayTest, Char16ArrayWithTerminatingNul) {
825 const char16_t a[] = u
"\0こんにちは";
830TEST(PrintArrayTest, Char32ArrayWithNoTerminatingNul) {
832 const char32_t a[] = {U
'👋', U
'\0', U
'🌌'};
833 EXPECT_EQ(
"U\"\\x1F44B\\0\\x1F30C\" (no terminating NUL)",
838TEST(PrintArrayTest, Char32ArrayWithTerminatingNul) {
839 const char32_t a[] = U
"\0👋🌌";
844TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
846 const wchar_t a[] = {L
'H', L
'\0', L
'i'};
851TEST(PrintArrayTest, WCharArrayWithTerminatingNul) {
852 const wchar_t a[] = L
"\0Hi";
857TEST(PrintArrayTest, ObjectArray) {
858 std::string a[3] = {
"Hi",
"Hello",
"Ni hao"};
863TEST(PrintArrayTest, BigArray) {
864 int a[100] = { 1, 2, 3 };
865 EXPECT_EQ(
"{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
872TEST(PrintStringTest, StringInStdNamespace) {
873 const char s[] =
"'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
874 const ::std::string str(s,
sizeof(s));
875 EXPECT_EQ(
"\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
879TEST(PrintStringTest, StringAmbiguousHex) {
886 EXPECT_EQ(
"\"mm\\x6\" \"bananas\"",
Print(::std::string(
"mm\x6" "bananas")));
888 EXPECT_EQ(
"\"NOM\\x6\" \"BANANA\"",
Print(::std::string(
"NOM\x6" "BANANA")));
894#if GTEST_HAS_STD_WSTRING
896TEST(PrintWideStringTest, StringInStdNamespace) {
897 const wchar_t s[] = L
"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
898 const ::std::wstring str(s,
sizeof(s)/
sizeof(
wchar_t));
899 EXPECT_EQ(
"L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
900 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
904TEST(PrintWideStringTest, StringAmbiguousHex) {
906 EXPECT_EQ(
"L\"0\\x12\" L\"3\"",
Print(::std::wstring(L
"0\x12" L
"3")));
908 Print(::std::wstring(L
"mm\x6" L
"bananas")));
910 Print(::std::wstring(L
"NOM\x6" L
"BANANA")));
916TEST(PrintStringTest, U8String) {
917 std::u8string str = u8
"Hello, 世界";
919 EXPECT_EQ(
"u8\"Hello, \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
Print(str));
923TEST(PrintStringTest, U16String) {
924 std::u16string str = u
"Hello, 世界";
929TEST(PrintStringTest, U32String) {
930 std::u32string str = U
"Hello, 🗺️";
943template <
typename Char,
typename CharTraits>
944std::basic_ostream<Char, CharTraits>&
operator<<(
945 std::basic_ostream<Char, CharTraits>& os,
947 return os <<
"AllowsGenericStreaming";
950TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
960template <
typename Char,
typename CharTraits,
typename T>
961std::basic_ostream<Char, CharTraits>&
operator<<(
962 std::basic_ostream<Char, CharTraits>& os,
964 return os <<
"AllowsGenericStreamingTemplate";
967TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
978 operator bool()
const {
return false; }
981template <
typename Char,
typename CharTraits,
typename T>
982std::basic_ostream<Char, CharTraits>&
operator<<(
983 std::basic_ostream<Char, CharTraits>& os,
984 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& ) {
985 return os <<
"AllowsGenericStreamingAndImplicitConversionTemplate";
988TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
990 EXPECT_EQ(
"AllowsGenericStreamingAndImplicitConversionTemplate",
Print(a));
993#if GTEST_INTERNAL_HAS_STRING_VIEW
997TEST(PrintStringViewTest, SimpleStringView) {
998 const internal::StringView sp =
"Hello";
1002TEST(PrintStringViewTest, UnprintableCharacters) {
1003 const char str[] =
"NUL (\0) and \r\t";
1004 const internal::StringView sp(str,
sizeof(str) - 1);
1012TEST(PrintStlContainerTest, EmptyDeque) {
1017TEST(PrintStlContainerTest, NonEmptyDeque) {
1018 deque<int> non_empty;
1019 non_empty.push_back(1);
1020 non_empty.push_back(3);
1025TEST(PrintStlContainerTest, OneElementHashMap) {
1026 ::std::unordered_map<int, char> map1;
1031TEST(PrintStlContainerTest, HashMultiMap) {
1032 ::std::unordered_multimap<int, bool> map1;
1033 map1.insert(make_pair(5,
true));
1034 map1.insert(make_pair(5,
false));
1037 const std::string result =
Print(map1);
1038 EXPECT_TRUE(result ==
"{ (5, true), (5, false) }" ||
1039 result ==
"{ (5, false), (5, true) }")
1040 <<
" where Print(map1) returns \"" << result <<
"\".";
1045TEST(PrintStlContainerTest, HashSet) {
1046 ::std::unordered_set<int> set1;
1051TEST(PrintStlContainerTest, HashMultiSet) {
1052 const int kSize = 5;
1053 int a[kSize] = { 1, 1, 2, 5, 1 };
1054 ::std::unordered_multiset<int> set1(a, a + kSize);
1057 const std::string result =
Print(set1);
1058 const std::string expected_pattern =
"{ d, d, d, d, d }";
1062 ASSERT_EQ(expected_pattern.length(), result.length());
1063 std::vector<int> numbers;
1064 for (
size_t i = 0;
i != result.length();
i++) {
1065 if (expected_pattern[
i] ==
'd') {
1066 ASSERT_NE(isdigit(
static_cast<unsigned char>(result[
i])), 0);
1067 numbers.push_back(result[
i] -
'0');
1069 EXPECT_EQ(expected_pattern[
i], result[
i]) <<
" where result is "
1075 std::sort(numbers.begin(), numbers.end());
1076 std::sort(a, a + kSize);
1077 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
1082 const std::string a[] = {
"hello",
"world"};
1083 const list<std::string> strings(a, a + 2);
1088 map<int, bool> map1;
1095TEST(PrintStlContainerTest, MultiMap) {
1096 multimap<bool, int> map1;
1103 map1.insert(pair<const bool, int>(
true, 0));
1104 map1.insert(pair<const bool, int>(
true, 1));
1105 map1.insert(pair<const bool, int>(
false, 2));
1110 const unsigned int a[] = { 3, 0, 5 };
1111 set<unsigned int> set1(a, a + 3);
1115TEST(PrintStlContainerTest, MultiSet) {
1116 const int a[] = { 1, 1, 2, 5, 1 };
1117 multiset<int> set1(a, a + 5);
1122TEST(PrintStlContainerTest, SinglyLinkedList) {
1123 int a[] = { 9, 2, 8 };
1124 const std::forward_list<int> ints(a, a + 3);
1129 pair<const bool, int>
p(
true, 5);
1133TEST(PrintStlContainerTest, Vector) {
1140TEST(PrintStlContainerTest, LongSequence) {
1141 const int a[100] = { 1, 2, 3 };
1142 const vector<int> v(a, a + 100);
1143 EXPECT_EQ(
"{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
1144 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }",
Print(v));
1147TEST(PrintStlContainerTest, NestedContainer) {
1148 const int a1[] = { 1, 2 };
1149 const int a2[] = { 3, 4, 5 };
1150 const list<int> l1(a1, a1 + 2);
1151 const list<int> l2(a2, a2 + 3);
1153 vector<list<int> > v;
1159TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
1160 const int a[3] = { 1, 2, 3 };
1161 NativeArray<int> b(a, 3, RelationToSourceReference());
1165TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
1166 const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1167 NativeArray<int[3]> b(a, 2, RelationToSourceReference());
1177TEST(PrintStlContainerTest, Iterator) {
1188TEST(PrintStlContainerTest, ConstIterator) {
1196TEST(PrintStdTupleTest, VariousSizes) {
1200 ::std::tuple<int> t1(5);
1203 ::std::tuple<char, bool> t2(
'a',
true);
1206 ::std::tuple<bool, int, int> t3(
false, 2, 3);
1209 ::std::tuple<bool, int, int, int> t4(
false, 2, 3, 4);
1212 const char*
const str =
"8";
1213 ::std::tuple<bool, char, short, int32_t, int64_t, float, double,
1214 const char*,
void*, std::string>
1215 t10(
false,
'a',
static_cast<short>(3), 4, 5, 1.5F, -2.5, str,
1217 EXPECT_EQ(
"(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1218 " pointing to \"8\", NULL, \"10\")",
1223TEST(PrintStdTupleTest, NestedTuple) {
1224 ::std::tuple< ::std::tuple<int, bool>,
char> nested(
1225 ::std::make_tuple(5,
true),
'a');
1233TEST(PrintReferenceWrapper, Printable) {
1239TEST(PrintReferenceWrapper, Unprintable) {
1242 "@" + PrintPointer(&up) +
1243 " 16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1244 Print(std::ref(up)));
1246 "@" + PrintPointer(&up) +
1247 " 16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1248 Print(std::cref(up)));
1254TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
1260TEST(PrintUnprintableTypeTest, InUserNamespace) {
1261 EXPECT_EQ(
"16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1272TEST(PrintUnpritableTypeTest, BigObject) {
1273 EXPECT_EQ(
"257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
1274 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1275 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1276 "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
1277 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1278 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1279 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
1286TEST(PrintStreamableTypeTest, InGlobalNamespace) {
1293TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
1298TEST(PrintStreamableTypeTest, TypeInUserNamespaceWithTemplatedStreamOperator) {
1303TEST(PrintStreamableTypeTest, SubclassUsesSuperclassStreamOperator) {
1314TEST(PrintStreamableTypeTest, PathLikeInUserNamespace) {
1317 const ::foo::PathLike cx;
1322TEST(PrintPrintableTypeTest, InUserNamespace) {
1329TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
1335TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
1336 EXPECT_EQ(
"PrintableViaPrintToTemplate: 5",
1342TEST(PrintReferenceTest, PrintsAddressAndValue) {
1350 EXPECT_EQ(
"@" + PrintPointer(a) +
" { { 0, 1, 2 }, { 3, 4, 5 } }",
1353 const ::foo::UnprintableInFoo
x;
1354 EXPECT_EQ(
"@" + PrintPointer(&
x) +
" 16-byte object "
1355 "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1361TEST(PrintReferenceTest, HandlesFunctionPointer) {
1363 const std::string fp_pointer_string =
1364 PrintPointer(
reinterpret_cast<const void*
>(&fp));
1369 const std::string fp_string = PrintPointer(
reinterpret_cast<const void*
>(
1371 EXPECT_EQ(
"@" + fp_pointer_string +
" " + fp_string,
1377TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
1381 "@" + PrintPointer(
reinterpret_cast<const void*
>(&
p)) +
" " +
1382 Print(
sizeof(
p)) +
"-byte object "));
1387 "@" + PrintPointer(
reinterpret_cast<const void*
>(&p2)) +
" " +
1388 Print(
sizeof(p2)) +
"-byte object "));
1393TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
1397 "@" + PrintPointer(&
p) +
" " +
Print(
sizeof(
p)) +
"-byte object "));
1405TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
1411TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
1418TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
1421 int n[] = { 1, 2, 3 };
1431TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
1438 const char* s =
"hello";
1449TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
1456 const wchar_t* s = L
"hello";
1470TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
1471 const char* s =
"hello \"world";
1476 char str[] =
"hi\1";
1482#if GTEST_HAS_STD_WSTRING
1484TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
1485 const wchar_t* s = L
"hi \"world";
1490 wchar_t str[] = L
"hi\1";
1502TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
1503 char str[] =
"hi \"world\"";
1510TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
1511 const char str[] =
"hi \"world\"";
1517TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
1518 wchar_t str[] = L
"hi \"world\"";
1519 wchar_t*
p =
nullptr;
1525TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
1526 const wchar_t str[] = L
"hi \"world\"";
1535TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
1536 const char str[] =
"hi \"world\"";
1541#if GTEST_HAS_STD_WSTRING
1543TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
1544 const wchar_t str[] = L
"hi \"w\0rld\"";
1555#define EXPECT_PRINT_TO_STRING_(value, expected_string) \
1556 EXPECT_TRUE(PrintToString(value) == (expected_string)) \
1557 << " where " #value " prints as " << (PrintToString(value))
1559TEST(PrintToStringTest, WorksForScalar) {
1563TEST(PrintToStringTest, WorksForPointerToConstChar) {
1564 const char*
p =
"hello";
1568TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
1574TEST(PrintToStringTest, EscapesForPointerToConstChar) {
1575 const char*
p =
"hello\n";
1579TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
1580 char s[] =
"hello\1";
1585TEST(PrintToStringTest, WorksForArray) {
1586 int n[3] = { 1, 2, 3 };
1590TEST(PrintToStringTest, WorksForCharArray) {
1595TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
1596 const char str_with_nul[] =
"hello\0 world";
1599 char mutable_str_with_nul[] =
"hello\0 world";
1603 TEST(PrintToStringTest, ContainsNonLatin) {
1605 std::string non_ascii_str = ::std::string(
"오전 4:30");
1607 "\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
1608 " As Text: \"오전 4:30\"");
1609 non_ascii_str = ::std::string(
"From ä — ẑ");
1611 "\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
1612 "\n As Text: \"From ä — ẑ\"");
1615TEST(IsValidUTF8Test, IllFormedUTF8) {
1620 static const char *
const kTestdata[][2] = {
1622 {
"\xC3\x74",
"\"\\xC3t\""},
1624 {
"\xC3\x84\xA4",
"\"\\xC3\\x84\\xA4\""},
1626 {
"abc\xC3",
"\"abc\\xC3\""},
1628 {
"x\xE2\x70\x94",
"\"x\\xE2p\\x94\""},
1630 {
"\xE2\x80",
"\"\\xE2\\x80\""},
1632 {
"\xE2\x80\xC3\x84",
"\"\\xE2\\x80\\xC3\\x84\""},
1634 {
"\xE2\x80\x7A",
"\"\\xE2\\x80z\""},
1636 {
"\xE2\xE2\x80\x94",
"\"\\xE2\\xE2\\x80\\x94\""},
1638 {
"\xF0\xE2\x80\x94",
"\"\\xF0\\xE2\\x80\\x94\""},
1640 {
"\xF0\xE2\x80",
"\"\\xF0\\xE2\\x80\""},
1642 {
"abc\xE2\x80\x94\xC3\x74xyc",
"\"abc\\xE2\\x80\\x94\\xC3txyc\""},
1643 {
"abc\xC3\x84\xE2\x80\xC3\x84xyz",
1644 "\"abc\\xC3\\x84\\xE2\\x80\\xC3\\x84xyz\""},
1647 {
"\xC0\x80",
"\"\\xC0\\x80\""},
1648 {
"\xC1\x81",
"\"\\xC1\\x81\""},
1650 {
"\xE0\x80\x80",
"\"\\xE0\\x80\\x80\""},
1651 {
"\xf0\x80\x80\x80",
"\"\\xF0\\x80\\x80\\x80\""},
1654 {
"\xED\x9F\xBF",
"\"\\xED\\x9F\\xBF\"\n As Text: \"\""},
1656 {
"\xED\xA0\x80",
"\"\\xED\\xA0\\x80\""},
1658 {
"\xED\xAD\xBF",
"\"\\xED\\xAD\\xBF\""},
1660 {
"\xED\xAE\x80",
"\"\\xED\\xAE\\x80\""},
1662 {
"\xED\xAF\xBF",
"\"\\xED\\xAF\\xBF\""},
1664 {
"\xED\xB3\xBF",
"\"\\xED\\xB3\\xBF\""},
1667 {
"\xEE\x80\x80",
"\"\\xEE\\x80\\x80\"\n As Text: \"\""}
1670 for (
int i = 0;
i < int(
sizeof(kTestdata)/
sizeof(kTestdata[0])); ++
i) {
1675#undef EXPECT_PRINT_TO_STRING_
1677TEST(UniversalTersePrintTest, WorksForNonReference) {
1678 ::std::stringstream ss;
1683TEST(UniversalTersePrintTest, WorksForReference) {
1685 ::std::stringstream ss;
1690TEST(UniversalTersePrintTest, WorksForCString) {
1691 const char* s1 =
"abc";
1692 ::std::stringstream ss1;
1696 char* s2 =
const_cast<char*
>(s1);
1697 ::std::stringstream ss2;
1701 const char* s3 =
nullptr;
1702 ::std::stringstream ss3;
1707TEST(UniversalPrintTest, WorksForNonReference) {
1708 ::std::stringstream ss;
1713TEST(UniversalPrintTest, WorksForReference) {
1715 ::std::stringstream ss;
1720TEST(UniversalPrintTest, WorksForPairWithConst) {
1721 std::pair<const Wrapper<std::string>,
int>
p(Wrapper<std::string>(
"abc"), 1);
1722 ::std::stringstream ss;
1724 EXPECT_EQ(
"(Wrapper(\"abc\"), 1)", ss.str());
1727TEST(UniversalPrintTest, WorksForCString) {
1728 const char* s1 =
"abc";
1729 ::std::stringstream ss1;
1731 EXPECT_EQ(PrintPointer(s1) +
" pointing to \"abc\"", std::string(ss1.str()));
1733 char* s2 =
const_cast<char*
>(s1);
1734 ::std::stringstream ss2;
1736 EXPECT_EQ(PrintPointer(s2) +
" pointing to \"abc\"", std::string(ss2.str()));
1738 const char* s3 =
nullptr;
1739 ::std::stringstream ss3;
1744TEST(UniversalPrintTest, WorksForCharArray) {
1745 const char str[] =
"\"Line\0 1\"\nLine 2";
1746 ::std::stringstream ss1;
1748 EXPECT_EQ(
"\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
1750 const char mutable_str[] =
"\"Line\0 1\"\nLine 2";
1751 ::std::stringstream ss2;
1753 EXPECT_EQ(
"\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
1756TEST(UniversalPrintTest, IncompleteType) {
1758 char some_object = 0;
1763TEST(UniversalPrintTest, SmartPointers) {
1765 std::unique_ptr<int>
p(
new int(17));
1766 EXPECT_EQ(
"(ptr = " + PrintPointer(
p.get()) +
", value = 17)",
1768 std::unique_ptr<int[]> p2(
new int[2]);
1772 std::shared_ptr<int> p3(
new int(1979));
1773 EXPECT_EQ(
"(ptr = " + PrintPointer(p3.get()) +
", value = 1979)",
1775#if __cpp_lib_shared_ptr_arrays >= 201611L
1776 std::shared_ptr<int[]> p4(
new int[2]);
1794#if __cpp_lib_shared_ptr_arrays >= 201611L
1804 nullptr,
nullptr)));
1807 std::unique_ptr<
void,
void (*)(
void*)>(
p.get(), [](
void*) {})));
1813TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
1818TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
1820 ::std::make_tuple(1));
1825TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
1827 ::std::make_tuple(1,
'a'));
1833TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
1836 ::std::tuple<const int&, const char*>(n,
"a"));
1842#if GTEST_INTERNAL_HAS_ANY
1845 template <
typename T>
1846 static std::string ExpectedTypeName() {
1848 return internal::GetTypeName<T>();
1850 return "<unknown_type>";
1855TEST_F(PrintAnyTest, Empty) {
1860TEST_F(PrintAnyTest, NonEmpty) {
1862 constexpr int val1 = 10;
1863 const std::string val2 =
"content";
1869 EXPECT_EQ(
"value of type " + ExpectedTypeName<std::string>(),
1874#if GTEST_INTERNAL_HAS_OPTIONAL
1875TEST(PrintOptionalTest, Basic) {
1876 internal::Optional<int>
value;
1885#if GTEST_INTERNAL_HAS_VARIANT
1886struct NonPrintable {
1887 unsigned char contents = 17;
1890TEST(PrintOneofTest, Basic) {
1891 using Type = internal::Variant<int, StreamableInGlobal, NonPrintable>;
1893 EXPECT_EQ(
"('StreamableInGlobal(index = 1)' with value StreamableInGlobal)",
1896 "('testing::gtest_printers_test::NonPrintable(index = 2)' with value "
1897 "1-byte object <11>)",
1909 string_ptr(
const char* data,
size_t size) : data_(data), size_(size) {}
1911 string_ptr& operator++() noexcept {
1928 string_ref(
const char* data,
size_t size) : data_(data), size_(size) {}
1930 string_ptr operator&()
const noexcept {
return {data_, size_}; }
1932 bool operator==(
const char* s)
const noexcept {
1933 if (size_ > 0 && data_[size_ - 1] != 0) {
1934 return std::string(data_, size_) == std::string(s);
1936 return std::string(data_) == std::string(s);
1947TEST(string_ref, compare) {
1948 const char* s =
"alex\0davidjohn\0";
1949 string_ptr ptr(s, 5);
#define ASSERT_EQ(val1, val2)
#define EXPECT_EQ(val1, val2)
#define ASSERT_NE(val1, val2)
#define EXPECT_TRUE(condition)
#define EXPECT_STREQ(s1, s2)
#define EXPECT_LT(val1, val2)
std::ostream & operator<<(std::ostream &os, EnumWithStreaming e)
#define EXPECT_PRINT_TO_STRING_(value, expected_string)
void PrintTo(EnumWithPrintTo e, std::ostream *os)
int32_t operator*(const ICOORD &op1, const ICOORD &op2)
AssertionResult AssertionFailure()
::std::string PrintToString(const T &value)
AssertionResult AssertionSuccess()
TEST_F(TestInfoTest, Names)
void UniversalPrint(const T &value, ::std::ostream *os)
::std::vector< ::std::string > Strings
bool operator==(faketype, faketype)
Strings UniversalTersePrintTupleFieldsToStrings(const Tuple &value)
std::string FormatForComparisonFailureMessage(const T1 &value, const T2 &)
void UniversalTersePrint(const T &value, ::std::ostream *os)
::std::ostream & operator<<(::std::ostream &os, const PointerPrintable *)
void PrintTo(const PrintableViaPrintTo &x, ::std::ostream *os)
std::basic_ostream< Char, CharTraits > & operator<<(std::basic_ostream< Char, CharTraits > &os, const AllowsGenericStreaming &)
std::string PrintByRef(const T &value)
std::string PrintArrayHelper(T(&a)[N])
std::string Print(const T &value)
AssertionResult HasPrefix(const StringType &str, const StringType &prefix)
TEST(PrintEnumTest, AnonymousEnum)
UnprintableTemplateInGlobal()
virtual ~StreamableInGlobal()
PrintableViaPrintToTemplate(const T &a_value)
StreamableTemplateInFoo()
friend::std::ostream & operator<<(::std::ostream &os, const PathLike &)
static void Print(const Wrapper< T > &w, ::std::ostream *os)
virtual char MyVirtualMethod(int)