40 #include <forward_list>
48 #include <unordered_map>
49 #include <unordered_set>
53 #include "gtest/gtest-printers.h"
54 #include "gtest/gtest.h"
76 return os << (e ==
kEWS1 ?
"kEWS1" :
"invalid");
85 *os << (e ==
kEWPT1 ?
"kEWPT1" :
"invalid");
103 os <<
"ChildClassWithStreamOperator";
107 template <
typename T>
122 os <<
"StreamableInGlobal";
126 os <<
"StreamableInGlobal*";
132 class UnprintableInFoo {
135 double z()
const {
return z_; }
142 struct PrintableViaPrintTo {
147 void PrintTo(
const PrintableViaPrintTo& x, ::std::ostream* os) {
148 *os <<
"PrintableViaPrintTo: " <<
x.value;
152 struct PointerPrintable {
155 ::std::ostream&
operator<<(::std::ostream& os,
156 const PointerPrintable* ) {
157 return os <<
"PointerPrintable*";
161 template <
typename T>
162 class PrintableViaPrintToTemplate {
171 template <
typename T>
172 void PrintTo(
const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
173 *os <<
"PrintableViaPrintToTemplate: " <<
x.value();
177 template <
typename T>
178 class StreamableTemplateInFoo {
187 template <
typename T>
188 inline ::std::ostream&
operator<<(::std::ostream& os,
189 const StreamableTemplateInFoo<T>& x) {
190 return os <<
"StreamableTemplateInFoo: " <<
x.value();
195 struct TemplatedStreamableInFoo {};
197 template <
typename OutputStream>
199 const TemplatedStreamableInFoo& ) {
200 os <<
"TemplatedStreamableInFoo";
225 return os <<
"Streamable-PathLike";
233 template <
typename T>
247 template <
typename T>
250 static void Print(
const Wrapper<T>& w, ::std::ostream* os) {
259 namespace gtest_printers_test {
263 using ::std::make_pair;
265 using ::std::multimap;
266 using ::std::multiset;
273 using ::testing::internal::NativeArray;
274 using ::testing::internal::RelationToSourceReference;
277 using ::testing::internal::UniversalPrinter;
283 template <
typename T>
285 ::std::stringstream ss;
293 template <
typename T>
295 ::std::stringstream ss;
331 TEST(PrintCharTest, PlainChar) {
351 TEST(PrintCharTest, SignedChar) {
354 Print(
static_cast<signed char>(-50)));
358 TEST(PrintCharTest, UnsignedChar) {
361 Print(
static_cast<unsigned char>(
'b')));
364 TEST(PrintCharTest, Char16) {
368 TEST(PrintCharTest, Char32) {
373 TEST(PrintCharTest, Char8) {
387 TEST(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)));
409 TEST(PrintTypeSizeTest, Wchar_t) {
414 TEST(PrintBuiltInTypeTest, Integer) {
415 EXPECT_EQ(
"'\\xFF' (255)",
Print(
static_cast<unsigned char>(255)));
416 EXPECT_EQ(
"'\\x80' (-128)",
Print(
static_cast<signed char>(-128)));
444 TEST(PrintBuiltInTypeTest, Size_t) {
446 #if !GTEST_OS_WINDOWS
449 #endif // !GTEST_OS_WINDOWS
453 #if defined(__SIZEOF_INT128__)
454 TEST(PrintBuiltInTypeTest, Int128) {
463 EXPECT_EQ(
"340282366920938463463374607431768211455",
Print(~__uint128_t{}));
464 __int128_t max_128 =
static_cast<__int128_t
>(~__uint128_t{} / 2);
465 EXPECT_EQ(
"-170141183460469231731687303715884105728",
Print(~max_128));
466 EXPECT_EQ(
"170141183460469231731687303715884105727",
Print(max_128));
468 #endif // __SIZEOF_INT128__
471 TEST(PrintBuiltInTypeTest, FloatingPoints) {
479 auto res =
Print(
typeid(MyStruct{}));
482 EXPECT_NE(res.find(
"MyStruct"), res.npos) << res;
484 #endif // GTEST_HAS_RTTI
490 ::std::stringstream expected_result_stream;
491 expected_result_stream << p;
492 return expected_result_stream.str();
499 const char* p =
"World";
504 TEST(PrintCStringTest, NonConst) {
507 Print(
static_cast<char*
>(p)));
511 TEST(PrintCStringTest, Null) {
512 const char*
p =
nullptr;
517 TEST(PrintCStringTest, EscapesProperly) {
518 const char*
p =
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
520 "\\n\\r\\t\\v\\x7F\\xFF a\"",
527 const char8_t*
p = u8
"界";
532 TEST(PrintU8StringTest, NonConst) {
535 Print(
static_cast<char8_t*
>(p)));
539 TEST(PrintU8StringTest, Null) {
540 const char8_t*
p =
nullptr;
545 TEST(PrintU8StringTest, EscapesProperly) {
546 const char8_t*
p = u8
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 世界";
548 " pointing to u8\"'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\x7F\\xFF "
549 "hello \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
556 const char16_t* p =
u"界";
561 TEST(PrintU16StringTest, NonConst) {
564 Print(
static_cast<char16_t*
>(p)));
568 TEST(PrintU16StringTest, Null) {
569 const char16_t* p =
nullptr;
574 TEST(PrintU16StringTest, EscapesProperly) {
575 const char16_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 \\x4E16\\x754C\"",
584 const char32_t* p = U
"🗺️";
589 TEST(PrintU32StringTest, NonConst) {
592 Print(
static_cast<char32_t*
>(p)));
596 TEST(PrintU32StringTest, Null) {
597 const char32_t* p =
nullptr;
602 TEST(PrintU32StringTest, EscapesProperly) {
603 const char32_t* p = U
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 🗺️";
605 " pointing to U\"'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\x7F\\xFF "
606 "hello \\x1F5FA\\xFE0F\"",
616 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
620 const wchar_t* p =
L"World";
625 TEST(PrintWideCStringTest, NonConst) {
628 Print(
static_cast<wchar_t*
>(p)));
632 TEST(PrintWideCStringTest, Null) {
633 const wchar_t*
p =
nullptr;
638 TEST(PrintWideCStringTest, EscapesProperly) {
639 const wchar_t s[] = {
'\'',
'"',
'?',
'\\',
'\a',
'\b',
'\f',
'\n',
'\r',
640 '\t',
'\v', 0xD3, 0x576, 0x8D3, 0xC74D,
' ',
'a',
'\0'};
642 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
643 Print(
static_cast<const wchar_t*
>(s)));
645 #endif // native wchar_t
650 TEST(PrintCharPointerTest, SignedChar) {
651 signed char*
p =
reinterpret_cast<signed char*
>(0x1234);
658 TEST(PrintCharPointerTest, ConstSignedChar) {
659 signed char*
p =
reinterpret_cast<signed char*
>(0x1234);
666 TEST(PrintCharPointerTest, UnsignedChar) {
667 unsigned char*
p =
reinterpret_cast<unsigned char*
>(0x1234);
674 TEST(PrintCharPointerTest, ConstUnsignedChar) {
675 const unsigned char*
p =
reinterpret_cast<const unsigned char*
>(0x1234);
684 TEST(PrintPointerToBuiltInTypeTest,
Bool) {
685 bool*
p =
reinterpret_cast<bool*
>(0xABCD);
692 TEST(PrintPointerToBuiltInTypeTest, Void) {
693 void*
p =
reinterpret_cast<void*
>(0xABCD);
700 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
701 const void*
p =
reinterpret_cast<const void*
>(0xABCD);
708 TEST(PrintPointerToPointerTest, IntPointerPointer) {
709 int**
p =
reinterpret_cast<int**
>(0xABCD);
719 TEST(PrintPointerTest, NonMemberFunctionPointer) {
734 template <
typename StringType>
739 const bool is_wide_string =
sizeof(
prefix[0]) > 1;
740 const char*
const begin_string_quote = is_wide_string ?
"L\"" :
"\"";
742 << begin_string_quote <<
prefix <<
"\" is not a prefix of "
743 << begin_string_quote <<
str <<
"\"\n";
760 TEST(PrintPointerTest, MemberVariablePointer) {
765 Print(
sizeof(p)) +
"-byte object "));
772 TEST(PrintPointerTest, MemberFunctionPointer) {
778 int (
Foo::*p)(char) = NULL;
780 Print(
sizeof(p)) +
"-byte object "));
787 template <
typename T,
size_t N>
793 TEST(PrintArrayTest, OneDimensionalArray) {
794 int a[5] = { 1, 2, 3, 4, 5 };
799 TEST(PrintArrayTest, TwoDimensionalArray) {
804 EXPECT_EQ(
"{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }",
PrintArrayHelper(a));
808 TEST(PrintArrayTest, ConstArray) {
809 const bool a[1] = {
false };
814 TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
816 char a[] = {
'H',
'\0',
'i' };
821 TEST(PrintArrayTest, CharArrayWithTerminatingNul) {
822 const char a[] =
"\0Hi";
828 TEST(PrintArrayTest, Char8ArrayWithNoTerminatingNul) {
830 const char8_t
a[] = {u8
'H', u8
'\0', u8
'i'};
835 TEST(PrintArrayTest, Char8ArrayWithTerminatingNul) {
836 const char8_t
a[] = u8
"\0世界";
838 "u8\"\\0\\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
844 TEST(PrintArrayTest, Char16ArrayWithNoTerminatingNul) {
846 const char16_t
a[] = {
u'こ',
u'\0',
u'ん',
u'に',
u'ち',
u'は'};
847 EXPECT_EQ(
"u\"\\x3053\\0\\x3093\\x306B\\x3061\\x306F\" (no terminating NUL)",
852 TEST(PrintArrayTest, Char16ArrayWithTerminatingNul) {
853 const char16_t
a[] =
u"\0こんにちは";
858 TEST(PrintArrayTest, Char32ArrayWithNoTerminatingNul) {
860 const char32_t
a[] = {U
'👋', U
'\0', U
'🌌'};
861 EXPECT_EQ(
"U\"\\x1F44B\\0\\x1F30C\" (no terminating NUL)",
866 TEST(PrintArrayTest, Char32ArrayWithTerminatingNul) {
867 const char32_t
a[] = U
"\0👋🌌";
872 TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
874 const wchar_t a[] = {
L'H',
L'\0',
L'i'};
879 TEST(PrintArrayTest, WCharArrayWithTerminatingNul) {
880 const wchar_t a[] =
L"\0Hi";
885 TEST(PrintArrayTest, ObjectArray) {
891 TEST(PrintArrayTest, BigArray) {
892 int a[100] = { 1, 2, 3 };
893 EXPECT_EQ(
"{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
900 TEST(PrintStringTest, StringInStdNamespace) {
901 const char s[] =
"'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
903 EXPECT_EQ(
"\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
907 TEST(PrintStringTest, StringAmbiguousHex) {
922 #if GTEST_HAS_STD_WSTRING
924 TEST(PrintWideStringTest, StringInStdNamespace) {
925 const wchar_t s[] =
L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
927 EXPECT_EQ(
"L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
928 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
932 TEST(PrintWideStringTest, StringAmbiguousHex) {
941 #endif // GTEST_HAS_STD_WSTRING
944 TEST(PrintStringTest, U8String) {
945 std::u8string
str = u8
"Hello, 世界";
951 TEST(PrintStringTest, U16String) {
952 std::u16string
str =
u"Hello, 世界";
957 TEST(PrintStringTest, U32String) {
958 std::u32string
str = U
"Hello, 🗺️";
969 class AllowsGenericStreaming {};
971 template <
typename Char,
typename CharTraits>
972 std::basic_ostream<Char, CharTraits>&
operator<<(
973 std::basic_ostream<Char, CharTraits>& os,
974 const AllowsGenericStreaming& ) {
975 return os <<
"AllowsGenericStreaming";
978 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
979 AllowsGenericStreaming
a;
985 template <
typename T>
986 class AllowsGenericStreamingTemplate {};
988 template <
typename Char,
typename CharTraits,
typename T>
989 std::basic_ostream<Char, CharTraits>&
operator<<(
990 std::basic_ostream<Char, CharTraits>& os,
991 const AllowsGenericStreamingTemplate<T>& ) {
992 return os <<
"AllowsGenericStreamingTemplate";
995 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
996 AllowsGenericStreamingTemplate<int>
a;
1003 template <
typename T>
1004 class AllowsGenericStreamingAndImplicitConversionTemplate {
1006 operator bool()
const {
return false; }
1009 template <
typename Char,
typename CharTraits,
typename T>
1010 std::basic_ostream<Char, CharTraits>&
operator<<(
1011 std::basic_ostream<Char, CharTraits>& os,
1012 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& ) {
1013 return os <<
"AllowsGenericStreamingAndImplicitConversionTemplate";
1016 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
1017 AllowsGenericStreamingAndImplicitConversionTemplate<int>
a;
1018 EXPECT_EQ(
"AllowsGenericStreamingAndImplicitConversionTemplate",
Print(a));
1021 #if GTEST_INTERNAL_HAS_STRING_VIEW
1025 TEST(PrintStringViewTest, SimpleStringView) {
1026 const internal::StringView sp =
"Hello";
1030 TEST(PrintStringViewTest, UnprintableCharacters) {
1031 const char str[] =
"NUL (\0) and \r\t";
1032 const internal::StringView sp(
str,
sizeof(
str) - 1);
1036 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1040 TEST(PrintStlContainerTest, EmptyDeque) {
1045 TEST(PrintStlContainerTest, NonEmptyDeque) {
1046 deque<int> non_empty;
1047 non_empty.push_back(1);
1048 non_empty.push_back(3);
1053 TEST(PrintStlContainerTest, OneElementHashMap) {
1054 ::std::unordered_map<int, char> map1;
1059 TEST(PrintStlContainerTest, HashMultiMap) {
1060 ::std::unordered_multimap<int, bool> map1;
1061 map1.insert(make_pair(5,
true));
1062 map1.insert(make_pair(5,
false));
1067 result ==
"{ (5, false), (5, true) }")
1068 <<
" where Print(map1) returns \"" <<
result <<
"\".";
1073 TEST(PrintStlContainerTest, HashSet) {
1074 ::std::unordered_set<int> set1;
1079 TEST(PrintStlContainerTest, HashMultiSet) {
1080 const int kSize = 5;
1081 int a[
kSize] = { 1, 1, 2, 5, 1 };
1082 ::std::unordered_multiset<int> set1(a, a + kSize);
1086 const std::string expected_pattern =
"{ d, d, d, d, d }";
1091 std::vector<int> numbers;
1092 for (
size_t i = 0;
i !=
result.length();
i++) {
1093 if (expected_pattern[i] ==
'd') {
1095 numbers.push_back(
result[i] -
'0');
1103 std::sort(numbers.begin(), numbers.end());
1104 std::sort(a, a + kSize);
1105 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
1109 TEST(PrintStlContainerTest, List) {
1111 const list<std::string> strings(a, a + 2);
1115 TEST(PrintStlContainerTest,
Map) {
1116 map<int, bool> map1;
1123 TEST(PrintStlContainerTest, MultiMap) {
1124 multimap<bool, int> map1;
1131 map1.insert(pair<const bool, int>(
true, 0));
1132 map1.insert(pair<const bool, int>(
true, 1));
1133 map1.insert(pair<const bool, int>(
false, 2));
1137 TEST(PrintStlContainerTest, Set) {
1138 const unsigned int a[] = { 3, 0, 5 };
1139 set<unsigned int> set1(a, a + 3);
1143 TEST(PrintStlContainerTest, MultiSet) {
1144 const int a[] = { 1, 1, 2, 5, 1 };
1145 multiset<int> set1(a, a + 5);
1150 TEST(PrintStlContainerTest, SinglyLinkedList) {
1151 int a[] = { 9, 2, 8 };
1152 const std::forward_list<int> ints(a, a + 3);
1156 TEST(PrintStlContainerTest,
Pair) {
1157 pair<const bool, int>
p(
true, 5);
1168 TEST(PrintStlContainerTest, LongSequence) {
1169 const int a[100] = { 1, 2, 3 };
1170 const vector<int>
v(a, a + 100);
1171 EXPECT_EQ(
"{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
1172 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }",
Print(
v));
1175 TEST(PrintStlContainerTest, NestedContainer) {
1176 const int a1[] = { 1, 2 };
1177 const int a2[] = { 3, 4, 5 };
1178 const list<int> l1(
a1,
a1 + 2);
1179 const list<int> l2(
a2,
a2 + 3);
1181 vector<list<int> >
v;
1187 TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
1188 const int a[3] = { 1, 2, 3 };
1189 NativeArray<int>
b(a, 3, RelationToSourceReference());
1193 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
1194 const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1195 NativeArray<int[3]>
b(a, 2, RelationToSourceReference());
1212 struct const_iterator {
1217 const_iterator
it = {};
1224 TEST(PrintStdTupleTest, VariousSizes) {
1228 ::std::tuple<int>
t1(5);
1231 ::std::tuple<char, bool> t2(
'a',
true);
1234 ::std::tuple<bool, int, int> t3(
false, 2, 3);
1237 ::std::tuple<bool, int, int, int> t4(
false, 2, 3, 4);
1240 const char*
const str =
"8";
1243 t10(
false,
'a',
static_cast<short>(3), 4, 5, 1.5F, -2.5,
str,
1246 " pointing to \"8\", NULL, \"10\")",
1251 TEST(PrintStdTupleTest, NestedTuple) {
1252 ::std::tuple< ::std::tuple<int, bool>,
char>
nested(
1257 TEST(PrintNullptrT, Basic) {
1261 TEST(PrintReferenceWrapper, Printable) {
1267 TEST(PrintReferenceWrapper, Unprintable) {
1271 " 16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1275 " 16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1276 Print(std::cref(up)));
1282 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
1288 TEST(PrintUnprintableTypeTest, InUserNamespace) {
1289 EXPECT_EQ(
"16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1300 TEST(PrintUnpritableTypeTest, BigObject) {
1301 EXPECT_EQ(
"257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
1302 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1303 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1304 "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
1305 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1306 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1307 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
1314 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
1321 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
1326 TEST(PrintStreamableTypeTest, TypeInUserNamespaceWithTemplatedStreamOperator) {
1331 TEST(PrintStreamableTypeTest, SubclassUsesSuperclassStreamOperator) {
1342 TEST(PrintStreamableTypeTest, PathLikeInUserNamespace) {
1345 const ::foo::PathLike cx;
1350 TEST(PrintPrintableTypeTest, InUserNamespace) {
1357 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
1363 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
1364 EXPECT_EQ(
"PrintableViaPrintToTemplate: 5",
1370 TEST(PrintReferenceTest, PrintsAddressAndValue) {
1381 const ::foo::UnprintableInFoo
x;
1383 "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1389 TEST(PrintReferenceTest, HandlesFunctionPointer) {
1399 EXPECT_EQ(
"@" + fp_pointer_string +
" " + fp_string,
1405 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
1409 "@" +
PrintPointer(
reinterpret_cast<const void*
>(&p)) +
" " +
1410 Print(
sizeof(p)) +
"-byte object "));
1415 "@" +
PrintPointer(
reinterpret_cast<const void*
>(&p2)) +
" " +
1416 Print(
sizeof(p2)) +
"-byte object "));
1421 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
1433 TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
1439 TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
1446 TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
1449 int n[] = { 1, 2, 3 };
1459 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
1466 const char*
s =
"hello";
1477 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
1484 const wchar_t*
s =
L"hello";
1498 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
1499 const char*
s =
"hello \"world";
1504 char str[] =
"hi\1";
1510 #if GTEST_HAS_STD_WSTRING
1512 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
1513 const wchar_t*
s =
L"hi \"world";
1518 wchar_t str[] =
L"hi\1";
1530 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
1531 char str[] =
"hi \"world\"";
1538 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
1539 const char str[] =
"hi \"world\"";
1545 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
1546 wchar_t str[] =
L"hi \"world\"";
1547 wchar_t*
p =
nullptr;
1553 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
1554 const wchar_t str[] =
L"hi \"world\"";
1563 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
1564 const char str[] =
"hi \"world\"";
1569 #if GTEST_HAS_STD_WSTRING
1571 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
1572 const wchar_t str[] =
L"hi \"w\0rld\"";
1583 #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
1584 EXPECT_TRUE(PrintToString(value) == (expected_string)) \
1585 << " where " #value " prints as " << (PrintToString(value))
1587 TEST(PrintToStringTest, WorksForScalar) {
1591 TEST(PrintToStringTest, WorksForPointerToConstChar) {
1592 const char*
p =
"hello";
1596 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
1602 TEST(PrintToStringTest, EscapesForPointerToConstChar) {
1603 const char*
p =
"hello\n";
1607 TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
1608 char s[] =
"hello\1";
1613 TEST(PrintToStringTest, WorksForArray) {
1614 int n[3] = { 1, 2, 3 };
1618 TEST(PrintToStringTest, WorksForCharArray) {
1623 TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
1624 const char str_with_nul[] =
"hello\0 world";
1627 char mutable_str_with_nul[] =
"hello\0 world";
1631 TEST(PrintToStringTest, ContainsNonLatin) {
1635 "\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
1636 " As Text: \"오전 4:30\"");
1639 "\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
1640 "\n As Text: \"From ä — ẑ\"");
1643 TEST(IsValidUTF8Test, IllFormedUTF8) {
1648 static const char *
const kTestdata[][2] = {
1650 {
"\xC3\x74",
"\"\\xC3t\""},
1652 {
"\xC3\x84\xA4",
"\"\\xC3\\x84\\xA4\""},
1654 {
"abc\xC3",
"\"abc\\xC3\""},
1656 {
"x\xE2\x70\x94",
"\"x\\xE2p\\x94\""},
1658 {
"\xE2\x80",
"\"\\xE2\\x80\""},
1660 {
"\xE2\x80\xC3\x84",
"\"\\xE2\\x80\\xC3\\x84\""},
1662 {
"\xE2\x80\x7A",
"\"\\xE2\\x80z\""},
1664 {
"\xE2\xE2\x80\x94",
"\"\\xE2\\xE2\\x80\\x94\""},
1666 {
"\xF0\xE2\x80\x94",
"\"\\xF0\\xE2\\x80\\x94\""},
1668 {
"\xF0\xE2\x80",
"\"\\xF0\\xE2\\x80\""},
1670 {
"abc\xE2\x80\x94\xC3\x74xyc",
"\"abc\\xE2\\x80\\x94\\xC3txyc\""},
1671 {
"abc\xC3\x84\xE2\x80\xC3\x84xyz",
1672 "\"abc\\xC3\\x84\\xE2\\x80\\xC3\\x84xyz\""},
1675 {
"\xC0\x80",
"\"\\xC0\\x80\""},
1676 {
"\xC1\x81",
"\"\\xC1\\x81\""},
1678 {
"\xE0\x80\x80",
"\"\\xE0\\x80\\x80\""},
1679 {
"\xf0\x80\x80\x80",
"\"\\xF0\\x80\\x80\\x80\""},
1682 {
"\xED\x9F\xBF",
"\"\\xED\\x9F\\xBF\"\n As Text: \"\""},
1684 {
"\xED\xA0\x80",
"\"\\xED\\xA0\\x80\""},
1686 {
"\xED\xAD\xBF",
"\"\\xED\\xAD\\xBF\""},
1688 {
"\xED\xAE\x80",
"\"\\xED\\xAE\\x80\""},
1690 {
"\xED\xAF\xBF",
"\"\\xED\\xAF\\xBF\""},
1692 {
"\xED\xB3\xBF",
"\"\\xED\\xB3\\xBF\""},
1695 {
"\xEE\x80\x80",
"\"\\xEE\\x80\\x80\"\n As Text: \"\""}
1698 for (
int i = 0;
i <
int(
sizeof(kTestdata)/
sizeof(kTestdata[0])); ++
i) {
1703 #undef EXPECT_PRINT_TO_STRING_
1705 TEST(UniversalTersePrintTest, WorksForNonReference) {
1706 ::std::stringstream ss;
1711 TEST(UniversalTersePrintTest, WorksForReference) {
1713 ::std::stringstream ss;
1718 TEST(UniversalTersePrintTest, WorksForCString) {
1719 const char* s1 =
"abc";
1720 ::std::stringstream ss1;
1724 char* s2 =
const_cast<char*
>(s1);
1725 ::std::stringstream ss2;
1729 const char* s3 =
nullptr;
1730 ::std::stringstream ss3;
1735 TEST(UniversalPrintTest, WorksForNonReference) {
1736 ::std::stringstream ss;
1741 TEST(UniversalPrintTest, WorksForReference) {
1743 ::std::stringstream ss;
1748 TEST(UniversalPrintTest, WorksForPairWithConst) {
1749 std::pair<const Wrapper<std::string>,
int> p(Wrapper<std::string>(
"abc"), 1);
1750 ::std::stringstream ss;
1752 EXPECT_EQ(
"(Wrapper(\"abc\"), 1)", ss.str());
1755 TEST(UniversalPrintTest, WorksForCString) {
1756 const char* s1 =
"abc";
1757 ::std::stringstream ss1;
1761 char* s2 =
const_cast<char*
>(s1);
1762 ::std::stringstream ss2;
1766 const char* s3 =
nullptr;
1767 ::std::stringstream ss3;
1772 TEST(UniversalPrintTest, WorksForCharArray) {
1773 const char str[] =
"\"Line\0 1\"\nLine 2";
1774 ::std::stringstream ss1;
1776 EXPECT_EQ(
"\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
1778 const char mutable_str[] =
"\"Line\0 1\"\nLine 2";
1779 ::std::stringstream ss2;
1781 EXPECT_EQ(
"\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
1784 TEST(UniversalPrintTest, IncompleteType) {
1786 char some_object = 0;
1791 TEST(UniversalPrintTest, SmartPointers) {
1793 std::unique_ptr<int>
p(
new int(17));
1796 std::unique_ptr<int[]> p2(
new int[2]);
1800 std::shared_ptr<int> p3(
new int(1979));
1803 #if __cpp_lib_shared_ptr_arrays >= 201611L
1804 std::shared_ptr<int[]> p4(
new int[2]);
1822 #if __cpp_lib_shared_ptr_arrays >= 201611L
1832 nullptr,
nullptr)));
1835 std::unique_ptr<
void,
void (*)(
void*)>(
p.get(), [](
void*) {})));
1841 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
1846 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
1853 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
1861 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
1864 ::std::tuple<const int&, const char*>(
n,
"a"));
1870 #if GTEST_INTERNAL_HAS_ANY
1873 template <
typename T>
1876 return internal::GetTypeName<T>();
1878 return "<unknown_type>";
1879 #endif // GTEST_HAS_RTTI
1888 TEST_F(PrintAnyTest, NonEmpty) {
1890 constexpr
int val1 = 10;
1897 EXPECT_EQ(
"value of type " + ExpectedTypeName<std::string>(),
1900 #endif // GTEST_INTERNAL_HAS_ANY
1902 #if GTEST_INTERNAL_HAS_OPTIONAL
1903 TEST(PrintOptionalTest, Basic) {
1905 internal::Optional<int>
value;
1912 #endif // GTEST_INTERNAL_HAS_OPTIONAL
1914 #if GTEST_INTERNAL_HAS_VARIANT
1915 struct NonPrintable {
1919 TEST(PrintOneofTest, Basic) {
1920 using Type = internal::Variant<int, StreamableInGlobal, NonPrintable>;
1922 EXPECT_EQ(
"('StreamableInGlobal(index = 1)' with value StreamableInGlobal)",
1925 "('testing::gtest_printers_test::NonPrintable(index = 2)' with value "
1926 "1-byte object <11>)",
1929 #endif // GTEST_INTERNAL_HAS_VARIANT
1961 bool operator==(
const char* s)
const noexcept {
1977 const char*
s =
"alex\0davidjohn\0";
1978 string_ptr
ptr(s, 5);