212namespace gtest_printers_test {
216using ::std::make_pair;
218using ::std::multimap;
219using ::std::multiset;
223using ::testing::PrintToString;
224using ::testing::internal::FormatForComparisonFailureMessage;
225using ::testing::internal::ImplicitCast_;
226using ::testing::internal::NativeArray;
227using ::testing::internal::RE;
228using ::testing::internal::RelationToSourceReference;
229using ::testing::internal::Strings;
230using ::testing::internal::UniversalPrint;
231using ::testing::internal::UniversalPrinter;
232using ::testing::internal::UniversalTersePrint;
233#if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
234using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
241 ::std::stringstream ss;
242 UniversalPrinter<T>::Print(
value, &ss);
251 ::std::stringstream ss;
252 UniversalPrinter<T&>::Print(
value, &ss);
287TEST(PrintCharTest, PlainChar) {
307TEST(PrintCharTest, SignedChar) {
310 Print(
static_cast<signed char>(-50)));
314TEST(PrintCharTest, UnsignedChar) {
317 Print(
static_cast<unsigned char>(
'b')));
329TEST(PrintBuiltInTypeTest, Wchar_t) {
346 EXPECT_EQ(
"L'\\x576' (1398)",
Print(
static_cast<wchar_t>(0x576)));
347 EXPECT_EQ(
"L'\\xC74D' (51021)",
Print(
static_cast<wchar_t>(0xC74D)));
351TEST(PrintTypeSizeTest, Wchar_t) {
356TEST(PrintBuiltInTypeTest, Integer) {
357 EXPECT_EQ(
"'\\xFF' (255)",
Print(
static_cast<unsigned char>(255)));
358 EXPECT_EQ(
"'\\x80' (-128)",
Print(
static_cast<signed char>(-128)));
370TEST(PrintBuiltInTypeTest, Size_t) {
379TEST(PrintBuiltInTypeTest, FloatingPoints) {
387static std::string PrintPointer(
const void*
p) {
388 ::std::stringstream expected_result_stream;
389 expected_result_stream <<
p;
390 return expected_result_stream.str();
397 const char*
p =
"World";
402TEST(PrintCStringTest, NonConst) {
404 EXPECT_EQ(PrintPointer(
p) +
" pointing to \"Hi\"",
405 Print(
static_cast<char*
>(
p)));
410 const char*
p = NULL;
415TEST(PrintCStringTest, EscapesProperly) {
416 const char*
p =
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
417 EXPECT_EQ(PrintPointer(
p) +
" pointing to \"'\\\"?\\\\\\a\\b\\f"
418 "\\n\\r\\t\\v\\x7F\\xFF a\"",
428#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
431TEST(PrintWideCStringTest, Const) {
432 const wchar_t*
p = L
"World";
437TEST(PrintWideCStringTest, NonConst) {
439 EXPECT_EQ(PrintPointer(
p) +
" pointing to L\"Hi\"",
440 Print(
static_cast<wchar_t*
>(
p)));
444TEST(PrintWideCStringTest, Null) {
445 const wchar_t*
p = NULL;
450TEST(PrintWideCStringTest, EscapesProperly) {
451 const wchar_t s[] = {
'\'',
'"',
'?',
'\\',
'\a',
'\b',
'\f',
'\n',
'\r',
452 '\t',
'\v', 0xD3, 0x576, 0x8D3, 0xC74D,
' ',
'a',
'\0'};
453 EXPECT_EQ(PrintPointer(
s) +
" pointing to L\"'\\\"?\\\\\\a\\b\\f"
454 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
455 Print(
static_cast<const wchar_t*
>(
s)));
462TEST(PrintCharPointerTest, SignedChar) {
463 signed char*
p =
reinterpret_cast<signed char*
>(0x1234);
470TEST(PrintCharPointerTest, ConstSignedChar) {
471 signed char*
p =
reinterpret_cast<signed char*
>(0x1234);
478TEST(PrintCharPointerTest, UnsignedChar) {
479 unsigned char*
p =
reinterpret_cast<unsigned char*
>(0x1234);
486TEST(PrintCharPointerTest, ConstUnsignedChar) {
487 const unsigned char*
p =
reinterpret_cast<const unsigned char*
>(0x1234);
497 bool*
p =
reinterpret_cast<bool*
>(0xABCD);
504TEST(PrintPointerToBuiltInTypeTest, Void) {
505 void*
p =
reinterpret_cast<void*
>(0xABCD);
512TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
513 const void*
p =
reinterpret_cast<const void*
>(0xABCD);
520TEST(PrintPointerToPointerTest, IntPointerPointer) {
521 int**
p =
reinterpret_cast<int**
>(0xABCD);
531TEST(PrintPointerTest, NonMemberFunctionPointer) {
537 PrintPointer(
reinterpret_cast<const void*
>(
540 int (*
p)(bool) = NULL;
546template <
typename StringType>
548 if (str.find(prefix, 0) == 0)
551 const bool is_wide_string =
sizeof(prefix[0]) > 1;
552 const char*
const begin_string_quote = is_wide_string ?
"L\"" :
"\"";
554 << begin_string_quote << prefix <<
"\" is not a prefix of "
555 << begin_string_quote << str <<
"\"\n";
572TEST(PrintPointerTest, MemberVariablePointer) {
575 int (
Foo::*
p) = NULL;
577 Print(
sizeof(
p)) +
"-byte object "));
584TEST(PrintPointerTest, MemberFunctionPointer) {
590 int (
Foo::*
p)(char) = NULL;
592 Print(
sizeof(
p)) +
"-byte object "));
599template <
typename T,
size_t N>
605TEST(PrintArrayTest, OneDimensionalArray) {
606 int a[5] = { 1, 2, 3, 4, 5 };
611TEST(PrintArrayTest, TwoDimensionalArray) {
616 EXPECT_EQ(
"{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }",
PrintArrayHelper(
a));
620TEST(PrintArrayTest, ConstArray) {
621 const bool a[1] = {
false };
626TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
628 char a[] = {
'H',
'\0',
'i' };
633TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) {
634 const char a[] =
"\0Hi";
639TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
641 const wchar_t a[] = { L
'H', L
'\0', L
'i' };
646TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
647 const wchar_t a[] = L
"\0Hi";
652TEST(PrintArrayTest, ObjectArray) {
653 std::string
a[3] = {
"Hi",
"Hello",
"Ni hao"};
658TEST(PrintArrayTest, BigArray) {
659 int a[100] = { 1, 2, 3 };
660 EXPECT_EQ(
"{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
666#if GTEST_HAS_GLOBAL_STRING
668TEST(PrintStringTest, StringInGlobalNamespace) {
669 const char s[] =
"'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
670 const ::string str(
s,
sizeof(
s));
671 EXPECT_EQ(
"\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
677TEST(PrintStringTest, StringInStdNamespace) {
678 const char s[] =
"'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
679 const ::std::string str(
s,
sizeof(
s));
680 EXPECT_EQ(
"\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
684TEST(PrintStringTest, StringAmbiguousHex) {
691 EXPECT_EQ(
"\"mm\\x6\" \"bananas\"",
Print(::std::string(
"mm\x6" "bananas")));
693 EXPECT_EQ(
"\"NOM\\x6\" \"BANANA\"",
Print(::std::string(
"NOM\x6" "BANANA")));
700#if GTEST_HAS_GLOBAL_WSTRING
702TEST(PrintWideStringTest, StringInGlobalNamespace) {
703 const wchar_t s[] = L
"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
704 const ::wstring str(
s,
sizeof(
s)/
sizeof(
wchar_t));
705 EXPECT_EQ(
"L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
706 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
711#if GTEST_HAS_STD_WSTRING
713TEST(PrintWideStringTest, StringInStdNamespace) {
714 const wchar_t s[] = L
"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
715 const ::std::wstring str(
s,
sizeof(
s)/
sizeof(
wchar_t));
716 EXPECT_EQ(
"L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
717 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
721TEST(PrintWideStringTest, StringAmbiguousHex) {
723 EXPECT_EQ(
"L\"0\\x12\" L\"3\"",
Print(::std::wstring(L
"0\x12" L
"3")));
725 Print(::std::wstring(L
"mm\x6" L
"bananas")));
727 Print(::std::wstring(L
"NOM\x6" L
"BANANA")));
740template <
typename Char,
typename CharTraits>
741std::basic_ostream<Char, CharTraits>&
operator<<(
742 std::basic_ostream<Char, CharTraits>&
os,
744 return os <<
"AllowsGenericStreaming";
747TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
757template <
typename Char,
typename CharTraits,
typename T>
758std::basic_ostream<Char, CharTraits>&
operator<<(
759 std::basic_ostream<Char, CharTraits>&
os,
761 return os <<
"AllowsGenericStreamingTemplate";
764TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
775 operator bool()
const {
return false; }
778template <
typename Char,
typename CharTraits,
typename T>
779std::basic_ostream<Char, CharTraits>&
operator<<(
780 std::basic_ostream<Char, CharTraits>&
os,
781 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& ) {
782 return os <<
"AllowsGenericStreamingAndImplicitConversionTemplate";
785TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
787 EXPECT_EQ(
"AllowsGenericStreamingAndImplicitConversionTemplate",
Print(
a));
794TEST(PrintStringViewTest, SimpleStringView) {
795 const ::absl::string_view sp =
"Hello";
799TEST(PrintStringViewTest, UnprintableCharacters) {
800 const char str[] =
"NUL (\0) and \r\t";
801 const ::absl::string_view sp(str,
sizeof(str) - 1);
809TEST(PrintStlContainerTest, EmptyDeque) {
814TEST(PrintStlContainerTest, NonEmptyDeque) {
815 deque<int> non_empty;
816 non_empty.push_back(1);
817 non_empty.push_back(3);
821#if GTEST_HAS_UNORDERED_MAP_
823TEST(PrintStlContainerTest, OneElementHashMap) {
824 ::std::unordered_map<int, char> map1;
829TEST(PrintStlContainerTest, HashMultiMap) {
830 ::std::unordered_multimap<int, bool> map1;
831 map1.insert(make_pair(5,
true));
832 map1.insert(make_pair(5,
false));
835 const std::string result =
Print(map1);
836 EXPECT_TRUE(result ==
"{ (5, true), (5, false) }" ||
837 result ==
"{ (5, false), (5, true) }")
838 <<
" where Print(map1) returns \"" << result <<
"\".";
843#if GTEST_HAS_UNORDERED_SET_
845TEST(PrintStlContainerTest, HashSet) {
846 ::std::unordered_set<int> set1;
851TEST(PrintStlContainerTest, HashMultiSet) {
853 int a[kSize] = { 1, 1, 2, 5, 1 };
854 ::std::unordered_multiset<int> set1(
a,
a + kSize);
857 const std::string result =
Print(set1);
858 const std::string expected_pattern =
"{ d, d, d, d, d }";
862 ASSERT_EQ(expected_pattern.length(), result.length());
863 std::vector<int> numbers;
864 for (
size_t i = 0; i != result.length(); i++) {
865 if (expected_pattern[i] ==
'd') {
866 ASSERT_NE(isdigit(
static_cast<unsigned char>(result[i])), 0);
867 numbers.push_back(result[i] -
'0');
869 EXPECT_EQ(expected_pattern[i], result[i]) <<
" where result is "
875 std::sort(numbers.begin(), numbers.end());
876 std::sort(
a,
a + kSize);
883 const std::string
a[] = {
"hello",
"world"};
884 const list<std::string> strings(
a,
a + 2);
888TEST(PrintStlContainerTest, Map) {
896TEST(PrintStlContainerTest, MultiMap) {
897 multimap<bool, int> map1;
904 map1.insert(pair<const bool, int>(
true, 0));
905 map1.insert(pair<const bool, int>(
true, 1));
906 map1.insert(pair<const bool, int>(
false, 2));
910TEST(PrintStlContainerTest, Set) {
911 const unsigned int a[] = { 3, 0, 5 };
912 set<unsigned int> set1(
a,
a + 3);
916TEST(PrintStlContainerTest, MultiSet) {
917 const int a[] = { 1, 1, 2, 5, 1 };
918 multiset<int> set1(
a,
a + 5);
922#if GTEST_HAS_STD_FORWARD_LIST_
926TEST(PrintStlContainerTest, SinglyLinkedList) {
927 int a[] = { 9, 2, 8 };
928 const std::forward_list<int> ints(
a,
a + 3);
933TEST(PrintStlContainerTest, Pair) {
934 pair<const bool, int>
p(
true, 5);
938TEST(PrintStlContainerTest, Vector) {
945TEST(PrintStlContainerTest, LongSequence) {
946 const int a[100] = { 1, 2, 3 };
947 const vector<int> v(
a,
a + 100);
948 EXPECT_EQ(
"{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
949 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }",
Print(v));
952TEST(PrintStlContainerTest, NestedContainer) {
953 const int a1[] = { 1, 2 };
954 const int a2[] = { 3, 4, 5 };
955 const list<int> l1(a1, a1 + 2);
956 const list<int> l2(a2, a2 + 3);
958 vector<list<int> > v;
964TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
965 const int a[3] = { 1, 2, 3 };
966 NativeArray<int> b(
a, 3, RelationToSourceReference());
970TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
971 const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
972 NativeArray<int[3]> b(
a, 2, RelationToSourceReference());
982TEST(PrintStlContainerTest, Iterator) {
993TEST(PrintStlContainerTest, ConstIterator) {
998#if GTEST_HAS_TR1_TUPLE
1002TEST(PrintTr1TupleTest, VariousSizes) {
1021 ::std::tr1::tuple<bool, int, int, int, bool, int> t6(
false, 2, 3, 4,
true, 6);
1025 false, 2, 3, 4,
true, 6, 7);
1029 false, 2, 3, 4,
true, 6, 7,
true);
1033 false, 2, 3, 4,
true, 6, 7,
true, 9);
1036 const char*
const str =
"8";
1042 t10(
false,
'a',
static_cast<short>(3), 4, 5, 1.5F, -2.5, str,
1043 ImplicitCast_<void*>(NULL),
"10");
1044 EXPECT_EQ(
"(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1045 " pointing to \"8\", NULL, \"10\")",
1050TEST(PrintTr1TupleTest, NestedTuple) {
1058#if GTEST_HAS_STD_TUPLE_
1062TEST(PrintStdTupleTest, VariousSizes) {
1066 ::std::tuple<int> t1(5);
1069 ::std::tuple<char, bool> t2(
'a',
true);
1072 ::std::tuple<bool, int, int> t3(
false, 2, 3);
1075 ::std::tuple<bool, int, int, int> t4(
false, 2, 3, 4);
1078 ::std::tuple<bool, int, int, int, bool> t5(
false, 2, 3, 4,
true);
1081 ::std::tuple<bool, int, int, int, bool, int> t6(
false, 2, 3, 4,
true, 6);
1084 ::std::tuple<bool, int, int, int, bool, int, int> t7(
1085 false, 2, 3, 4,
true, 6, 7);
1088 ::std::tuple<bool, int, int, int, bool, int, int, bool> t8(
1089 false, 2, 3, 4,
true, 6, 7,
true);
1092 ::std::tuple<bool, int, int, int, bool, int, int, bool, int> t9(
1093 false, 2, 3, 4,
true, 6, 7,
true, 9);
1096 const char*
const str =
"8";
1102 t10(
false,
'a',
static_cast<short>(3), 4, 5, 1.5F, -2.5, str,
1103 ImplicitCast_<void*>(NULL),
"10");
1104 EXPECT_EQ(
"(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1105 " pointing to \"8\", NULL, \"10\")",
1110TEST(PrintStdTupleTest, NestedTuple) {
1111 ::std::tuple< ::std::tuple<int, bool>,
char> nested(
1112 ::std::make_tuple(5,
true),
'a');
1119TEST(PrintNullptrT, Basic) {
1127TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
1133TEST(PrintUnprintableTypeTest, InUserNamespace) {
1134 EXPECT_EQ(
"16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1145TEST(PrintUnpritableTypeTest, BigObject) {
1146 EXPECT_EQ(
"257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
1147 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1148 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1149 "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
1150 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1151 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1152 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
1159TEST(PrintStreamableTypeTest, InGlobalNamespace) {
1166TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
1173TEST(PrintStreamableTypeTest, PathLikeInUserNamespace) {
1176 const ::foo::PathLike cx;
1181TEST(PrintPrintableTypeTest, InUserNamespace) {
1188TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
1194TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
1195 EXPECT_EQ(
"PrintableViaPrintToTemplate: 5",
1201TEST(PrintReferenceTest, PrintsAddressAndValue) {
1209 EXPECT_EQ(
"@" + PrintPointer(
a) +
" { { 0, 1, 2 }, { 3, 4, 5 } }",
1212 const ::foo::UnprintableInFoo x;
1213 EXPECT_EQ(
"@" + PrintPointer(&x) +
" 16-byte object "
1214 "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1220TEST(PrintReferenceTest, HandlesFunctionPointer) {
1222 const std::string fp_pointer_string =
1223 PrintPointer(
reinterpret_cast<const void*
>(&fp));
1228 const std::string fp_string = PrintPointer(
reinterpret_cast<const void*
>(
1230 EXPECT_EQ(
"@" + fp_pointer_string +
" " + fp_string,
1236TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
1240 "@" + PrintPointer(
reinterpret_cast<const void*
>(&
p)) +
" " +
1241 Print(
sizeof(
p)) +
"-byte object "));
1246 "@" + PrintPointer(
reinterpret_cast<const void*
>(&p2)) +
" " +
1247 Print(
sizeof(p2)) +
"-byte object "));
1252TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
1256 "@" + PrintPointer(&
p) +
" " +
Print(
sizeof(
p)) +
"-byte object "));
1264TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
1266 FormatForComparisonFailureMessage(123, 124).c_str());
1270TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
1273 FormatForComparisonFailureMessage(&n, &n).c_str());
1277TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
1280 int n[] = { 1, 2, 3 };
1282 FormatForComparisonFailureMessage(n, n).c_str());
1290TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
1297 const char*
s =
"hello";
1299 FormatForComparisonFailureMessage(
s,
s).c_str());
1304 FormatForComparisonFailureMessage(&ch, &ch).c_str());
1308TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
1315 const wchar_t*
s = L
"hello";
1317 FormatForComparisonFailureMessage(
s,
s).c_str());
1322 FormatForComparisonFailureMessage(&ch, &ch).c_str());
1328#if GTEST_HAS_GLOBAL_STRING
1330TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) {
1331 const char*
s =
"hello \"world";
1333 FormatForComparisonFailureMessage(
s, ::string()).c_str());
1336 char str[] =
"hi\1";
1339 FormatForComparisonFailureMessage(
p, ::string()).c_str());
1344TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
1345 const char*
s =
"hello \"world";
1347 FormatForComparisonFailureMessage(
s, ::std::string()).c_str());
1350 char str[] =
"hi\1";
1353 FormatForComparisonFailureMessage(
p, ::std::string()).c_str());
1356#if GTEST_HAS_GLOBAL_WSTRING
1358TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) {
1359 const wchar_t*
s = L
"hi \"world";
1361 FormatForComparisonFailureMessage(
s, ::wstring()).c_str());
1364 wchar_t str[] = L
"hi\1";
1367 FormatForComparisonFailureMessage(
p, ::wstring()).c_str());
1371#if GTEST_HAS_STD_WSTRING
1373TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
1374 const wchar_t*
s = L
"hi \"world";
1376 FormatForComparisonFailureMessage(
s, ::std::wstring()).c_str());
1379 wchar_t str[] = L
"hi\1";
1382 FormatForComparisonFailureMessage(
p, ::std::wstring()).c_str());
1391TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
1392 char str[] =
"hi \"world\"";
1395 FormatForComparisonFailureMessage(str,
p).c_str());
1399TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
1400 const char str[] =
"hi \"world\"";
1402 FormatForComparisonFailureMessage(str, str).c_str());
1406TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
1407 wchar_t str[] = L
"hi \"world\"";
1410 FormatForComparisonFailureMessage(str,
p).c_str());
1414TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
1415 const wchar_t str[] = L
"hi \"world\"";
1417 FormatForComparisonFailureMessage(str, str).c_str());
1423#if GTEST_HAS_GLOBAL_STRING
1425TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) {
1426 const char str[] =
"hi \"w\0rld\"";
1429 FormatForComparisonFailureMessage(str, ::string()).c_str());
1434TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
1435 const char str[] =
"hi \"world\"";
1437 FormatForComparisonFailureMessage(str, ::std::string()).c_str());
1440#if GTEST_HAS_GLOBAL_WSTRING
1442TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) {
1443 const wchar_t str[] = L
"hi \"world\"";
1445 FormatForComparisonFailureMessage(str, ::wstring()).c_str());
1449#if GTEST_HAS_STD_WSTRING
1451TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
1452 const wchar_t str[] = L
"hi \"w\0rld\"";
1456 FormatForComparisonFailureMessage(str, ::std::wstring()).c_str());
1463#define EXPECT_PRINT_TO_STRING_(value, expected_string) \
1464 EXPECT_TRUE(PrintToString(value) == (expected_string)) \
1465 << " where " #value " prints as " << (PrintToString(value))
1467TEST(PrintToStringTest, WorksForScalar) {
1471TEST(PrintToStringTest, WorksForPointerToConstChar) {
1472 const char*
p =
"hello";
1476TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
1482TEST(PrintToStringTest, EscapesForPointerToConstChar) {
1483 const char*
p =
"hello\n";
1487TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
1488 char s[] =
"hello\1";
1493TEST(PrintToStringTest, WorksForArray) {
1494 int n[3] = { 1, 2, 3 };
1498TEST(PrintToStringTest, WorksForCharArray) {
1503TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
1504 const char str_with_nul[] =
"hello\0 world";
1507 char mutable_str_with_nul[] =
"hello\0 world";
1511 TEST(PrintToStringTest, ContainsNonLatin) {
1513 std::string non_ascii_str = ::std::string(
"오전 4:30");
1515 "\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
1516 " As Text: \"오전 4:30\"");
1517 non_ascii_str = ::std::string(
"From ä — ẑ");
1519 "\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
1520 "\n As Text: \"From ä — ẑ\"");
1523TEST(IsValidUTF8Test, IllFormedUTF8) {
1528 static const char *
const kTestdata[][2] = {
1530 {
"\xC3\x74",
"\"\\xC3t\""},
1532 {
"\xC3\x84\xA4",
"\"\\xC3\\x84\\xA4\""},
1534 {
"abc\xC3",
"\"abc\\xC3\""},
1536 {
"x\xE2\x70\x94",
"\"x\\xE2p\\x94\""},
1538 {
"\xE2\x80",
"\"\\xE2\\x80\""},
1540 {
"\xE2\x80\xC3\x84",
"\"\\xE2\\x80\\xC3\\x84\""},
1542 {
"\xE2\x80\x7A",
"\"\\xE2\\x80z\""},
1544 {
"\xE2\xE2\x80\x94",
"\"\\xE2\\xE2\\x80\\x94\""},
1546 {
"\xF0\xE2\x80\x94",
"\"\\xF0\\xE2\\x80\\x94\""},
1548 {
"\xF0\xE2\x80",
"\"\\xF0\\xE2\\x80\""},
1550 {
"abc\xE2\x80\x94\xC3\x74xyc",
"\"abc\\xE2\\x80\\x94\\xC3txyc\""},
1551 {
"abc\xC3\x84\xE2\x80\xC3\x84xyz",
1552 "\"abc\\xC3\\x84\\xE2\\x80\\xC3\\x84xyz\""},
1555 {
"\xC0\x80",
"\"\\xC0\\x80\""},
1556 {
"\xC1\x81",
"\"\\xC1\\x81\""},
1558 {
"\xE0\x80\x80",
"\"\\xE0\\x80\\x80\""},
1559 {
"\xf0\x80\x80\x80",
"\"\\xF0\\x80\\x80\\x80\""},
1562 {
"\xED\x9F\xBF",
"\"\\xED\\x9F\\xBF\"\n As Text: \"\""},
1564 {
"\xED\xA0\x80",
"\"\\xED\\xA0\\x80\""},
1566 {
"\xED\xAD\xBF",
"\"\\xED\\xAD\\xBF\""},
1568 {
"\xED\xAE\x80",
"\"\\xED\\xAE\\x80\""},
1570 {
"\xED\xAF\xBF",
"\"\\xED\\xAF\\xBF\""},
1572 {
"\xED\xB3\xBF",
"\"\\xED\\xB3\\xBF\""},
1575 {
"\xEE\x80\x80",
"\"\\xEE\\x80\\x80\"\n As Text: \"\""}
1578 for (
int i = 0; i < int(
sizeof(kTestdata)/
sizeof(kTestdata[0])); ++i) {
1583#undef EXPECT_PRINT_TO_STRING_
1585TEST(UniversalTersePrintTest, WorksForNonReference) {
1586 ::std::stringstream ss;
1587 UniversalTersePrint(123, &ss);
1591TEST(UniversalTersePrintTest, WorksForReference) {
1593 ::std::stringstream ss;
1594 UniversalTersePrint(n, &ss);
1598TEST(UniversalTersePrintTest, WorksForCString) {
1599 const char* s1 =
"abc";
1600 ::std::stringstream ss1;
1601 UniversalTersePrint(s1, &ss1);
1604 char* s2 =
const_cast<char*
>(s1);
1605 ::std::stringstream ss2;
1606 UniversalTersePrint(s2, &ss2);
1609 const char* s3 = NULL;
1610 ::std::stringstream ss3;
1611 UniversalTersePrint(s3, &ss3);
1615TEST(UniversalPrintTest, WorksForNonReference) {
1616 ::std::stringstream ss;
1617 UniversalPrint(123, &ss);
1621TEST(UniversalPrintTest, WorksForReference) {
1623 ::std::stringstream ss;
1624 UniversalPrint(n, &ss);
1628TEST(UniversalPrintTest, WorksForCString) {
1629 const char* s1 =
"abc";
1630 ::std::stringstream ss1;
1631 UniversalPrint(s1, &ss1);
1632 EXPECT_EQ(PrintPointer(s1) +
" pointing to \"abc\"", std::string(ss1.str()));
1634 char* s2 =
const_cast<char*
>(s1);
1635 ::std::stringstream ss2;
1636 UniversalPrint(s2, &ss2);
1637 EXPECT_EQ(PrintPointer(s2) +
" pointing to \"abc\"", std::string(ss2.str()));
1639 const char* s3 = NULL;
1640 ::std::stringstream ss3;
1641 UniversalPrint(s3, &ss3);
1645TEST(UniversalPrintTest, WorksForCharArray) {
1646 const char str[] =
"\"Line\0 1\"\nLine 2";
1647 ::std::stringstream ss1;
1648 UniversalPrint(str, &ss1);
1649 EXPECT_EQ(
"\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
1651 const char mutable_str[] =
"\"Line\0 1\"\nLine 2";
1652 ::std::stringstream ss2;
1653 UniversalPrint(mutable_str, &ss2);
1654 EXPECT_EQ(
"\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
1657#if GTEST_HAS_TR1_TUPLE
1659TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsEmptyTuple) {
1660 Strings result = UniversalTersePrintTupleFieldsToStrings(
1665TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsOneTuple) {
1666 Strings result = UniversalTersePrintTupleFieldsToStrings(
1672TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTwoTuple) {
1673 Strings result = UniversalTersePrintTupleFieldsToStrings(
1680TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTersely) {
1682 Strings result = UniversalTersePrintTupleFieldsToStrings(
1691#if GTEST_HAS_STD_TUPLE_
1693TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
1694 Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
1698TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
1699 Strings result = UniversalTersePrintTupleFieldsToStrings(
1700 ::std::make_tuple(1));
1705TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
1706 Strings result = UniversalTersePrintTupleFieldsToStrings(
1707 ::std::make_tuple(1,
'a'));
1713TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
1715 Strings result = UniversalTersePrintTupleFieldsToStrings(
1716 ::std::tuple<const int&, const char*>(n,
"a"));
1726TEST(PrintOptionalTest, Basic) {
1727 absl::optional<int>
value;