39 #include <forward_list>
50 #include <unordered_map>
51 #include <unordered_set>
60 #include "absl/strings/str_format.h"
63 #if GTEST_INTERNAL_HAS_STD_SPAN
65 #endif // GTEST_INTERNAL_HAS_STD_SPAN
67 #if GTEST_INTERNAL_HAS_COMPARE_LIB
69 #endif // GTEST_INTERNAL_HAS_COMPARE_LIB
83 return os << (e ==
kEWS1 ?
"kEWS1" :
"invalid");
90 *os << (e ==
kEWPT1 ?
"kEWPT1" :
"invalid");
108 os <<
"ChildClassWithStreamOperator";
112 template <
typename T>
128 os <<
"StreamableInGlobal";
132 os <<
"StreamableInGlobal*";
135 #ifdef GTEST_HAS_ABSL
138 template <
typename Sink>
139 friend void AbslStringify(Sink& sink,
const Point&
p) {
140 absl::Format(&sink,
"(%d, %d)", p.x, p.y);
154 double z()
const {
return z_; }
168 *os <<
"PrintableViaPrintTo: " << x.
value;
176 return os <<
"PointerPrintable*";
180 template <
typename T>
191 template <
typename T>
193 *os <<
"PrintableViaPrintToTemplate: " << x.
value();
197 template <
typename T>
208 template <
typename T>
211 return os <<
"StreamableTemplateInFoo: " << x.
value();
218 template <
typename OutputStream>
221 os <<
"TemplatedStreamableInFoo";
227 os <<
"StreamableInLocal";
251 return os <<
"Streamable-PathLike";
259 template <
typename T>
273 template <
typename T>
276 static void Print(
const Wrapper<T>& w, ::std::ostream* os) {
284 namespace gtest_printers_test {
288 using ::std::make_pair;
290 using ::std::multimap;
291 using ::std::multiset;
297 using ::testing::internal::NativeArray;
298 using ::testing::internal::RelationToSourceReference;
301 using ::testing::internal::UniversalPrinter;
307 template <
typename T>
309 ::std::stringstream ss;
317 template <
typename T>
319 ::std::stringstream ss;
346 #ifdef GTEST_HAS_ABSL
360 TEST(PrintCharTest, PlainChar) {
380 TEST(PrintCharTest, SignedChar) {
386 TEST(PrintCharTest, UnsignedChar) {
388 EXPECT_EQ(
"'b' (98, 0x62)",
Print(static_cast<unsigned char>(
'b')));
395 #ifdef __cpp_lib_char8_t
408 TEST(PrintBuiltInTypeTest, Wchar_t) {
425 EXPECT_EQ(
"L'\\x576' (1398)",
Print(static_cast<wchar_t>(0x576)));
426 EXPECT_EQ(
"L'\\xC74D' (51021)",
Print(static_cast<wchar_t>(0xC74D)));
430 TEST(PrintTypeSizeTest, Wchar_t) {
431 EXPECT_LT(
sizeof(
wchar_t),
sizeof(int64_t));
435 TEST(PrintBuiltInTypeTest, Integer) {
436 EXPECT_EQ(
"'\\xFF' (255)",
Print(static_cast<unsigned char>(255)));
437 EXPECT_EQ(
"'\\x80' (-128)",
Print(static_cast<signed char>(-128)));
448 #ifdef __cpp_lib_char8_t
465 TEST(PrintBuiltInTypeTest, Size_t) {
467 #ifndef GTEST_OS_WINDOWS
470 #endif // !GTEST_OS_WINDOWS
474 #if defined(__SIZEOF_INT128__)
475 TEST(PrintBuiltInTypeTest, Int128) {
484 EXPECT_EQ(
"340282366920938463463374607431768211455",
Print(~__uint128_t{}));
485 __int128_t max_128 =
static_cast<__int128_t
>(~__uint128_t{} / 2);
486 EXPECT_EQ(
"-170141183460469231731687303715884105728",
Print(~max_128));
487 EXPECT_EQ(
"170141183460469231731687303715884105727",
Print(max_128));
489 #endif // __SIZEOF_INT128__
492 TEST(PrintBuiltInTypeTest, FloatingPoints) {
506 TEST(PrintBuiltInTypeTest, TypeInfo) {
508 auto res =
Print(
typeid(MyStruct{}));
511 EXPECT_NE(res.find(
"MyStruct"), res.npos) << res;
513 #endif // GTEST_HAS_RTTI
519 ::std::stringstream expected_result_stream;
520 expected_result_stream <<
p;
521 return expected_result_stream.str();
527 TEST(PrintCStringTest, Const) {
528 const char*
p =
"World";
533 TEST(PrintCStringTest, NonConst) {
536 Print(static_cast<char*>(p)));
541 const char*
p =
nullptr;
546 TEST(PrintCStringTest, EscapesProperly) {
547 const char*
p =
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
549 " pointing to \"'\\\"?\\\\\\a\\b\\f"
550 "\\n\\r\\t\\v\\x7F\\xFF a\"",
554 #ifdef __cpp_lib_char8_t
556 TEST(PrintU8StringTest, Const) {
557 const char8_t*
p = u8
"界";
562 TEST(PrintU8StringTest, NonConst) {
565 Print(static_cast<char8_t*>(p)));
569 TEST(PrintU8StringTest, Null) {
570 const char8_t* p =
nullptr;
575 TEST(PrintU8StringTest, EscapesProperly) {
576 const char8_t* p = u8
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 世界";
578 " pointing to u8\"'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\x7F\\xFF "
579 "hello \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
585 TEST(PrintU16StringTest, Const) {
586 const char16_t* p = u
"界";
591 TEST(PrintU16StringTest, NonConst) {
594 Print(static_cast<char16_t*>(p)));
598 TEST(PrintU16StringTest, Null) {
599 const char16_t* p =
nullptr;
604 TEST(PrintU16StringTest, EscapesProperly) {
605 const char16_t* p = u
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 世界";
607 " pointing to u\"'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\x7F\\xFF "
608 "hello \\x4E16\\x754C\"",
613 TEST(PrintU32StringTest, Const) {
614 const char32_t* p = U
"🗺️";
619 TEST(PrintU32StringTest, NonConst) {
622 Print(static_cast<char32_t*>(p)));
626 TEST(PrintU32StringTest, Null) {
627 const char32_t* p =
nullptr;
632 TEST(PrintU32StringTest, EscapesProperly) {
633 const char32_t* p = U
"'\"?\\\a\b\f\n\r\t\v\x7F\xFF hello 🗺️";
635 " pointing to U\"'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\x7F\\xFF "
636 "hello \\x1F5FA\\xFE0F\"",
646 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
649 TEST(PrintWideCStringTest, Const) {
650 const wchar_t* p = L
"World";
655 TEST(PrintWideCStringTest, NonConst) {
658 Print(static_cast<wchar_t*>(p)));
662 TEST(PrintWideCStringTest, Null) {
663 const wchar_t* p =
nullptr;
668 TEST(PrintWideCStringTest, EscapesProperly) {
669 const wchar_t s[] = {
'\'',
'"',
'?',
'\\',
'\a',
'\b',
670 '\f',
'\n',
'\r',
'\t',
'\v', 0xD3,
671 0x576, 0x8D3, 0xC74D,
' ',
'a',
'\0'};
673 " pointing to L\"'\\\"?\\\\\\a\\b\\f"
674 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
675 Print(static_cast<const wchar_t*>(s)));
677 #endif // native wchar_t
682 TEST(PrintCharPointerTest, SignedChar) {
683 signed char* p =
reinterpret_cast<signed char*
>(0x1234);
690 TEST(PrintCharPointerTest, ConstSignedChar) {
691 signed char* p =
reinterpret_cast<signed char*
>(0x1234);
698 TEST(PrintCharPointerTest, UnsignedChar) {
699 unsigned char* p =
reinterpret_cast<unsigned char*
>(0x1234);
706 TEST(PrintCharPointerTest, ConstUnsignedChar) {
707 const unsigned char* p =
reinterpret_cast<const unsigned char*
>(0x1234);
717 bool* p =
reinterpret_cast<bool*
>(0xABCD);
725 void* p =
reinterpret_cast<void*
>(0xABCD);
732 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
733 const void* p =
reinterpret_cast<const void*
>(0xABCD);
740 TEST(PrintPointerToPointerTest, IntPointerPointer) {
741 int** p =
reinterpret_cast<int**
>(0xABCD);
751 TEST(PrintPointerTest, NonMemberFunctionPointer) {
757 reinterpret_cast<internal::BiggestInt>(&
MyFunction))),
759 int (*p)(bool) = NULL;
765 template <
typename StringType>
766 AssertionResult
HasPrefix(
const StringType& str,
const StringType& prefix) {
769 const bool is_wide_string =
sizeof(prefix[0]) > 1;
770 const char*
const begin_string_quote = is_wide_string ?
"L\"" :
"\"";
772 << begin_string_quote << prefix <<
"\" is not a prefix of "
773 << begin_string_quote << str <<
"\"\n";
783 virtual ~Foo() =
default;
790 TEST(PrintPointerTest, MemberVariablePointer) {
801 TEST(PrintPointerTest, MemberFunctionPointer) {
807 int (
Foo::*p)(char) = NULL;
815 template <
typename T,
size_t N>
821 TEST(PrintArrayTest, OneDimensionalArray) {
822 int a[5] = {1, 2, 3, 4, 5};
827 TEST(PrintArrayTest, TwoDimensionalArray) {
828 int a[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 0}};
829 EXPECT_EQ(
"{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }",
PrintArrayHelper(a));
833 TEST(PrintArrayTest, ConstArray) {
834 const bool a[1] = {
false};
839 TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
841 char a[] = {
'H',
'\0',
'i'};
846 TEST(PrintArrayTest, CharArrayWithTerminatingNul) {
847 const char a[] =
"\0Hi";
851 #ifdef __cpp_lib_char8_t
853 TEST(PrintArrayTest, Char8ArrayWithNoTerminatingNul) {
855 const char8_t
a[] = {u8
'H', u8
'\0', u8
'i'};
860 TEST(PrintArrayTest, Char8ArrayWithTerminatingNul) {
861 const char8_t a[] = u8
"\0世界";
867 TEST(PrintArrayTest, Char16ArrayWithNoTerminatingNul) {
869 const char16_t a[] = {u
'こ', u
'\0', u
'ん', u
'に', u
'ち', u
'は'};
870 EXPECT_EQ(
"u\"\\x3053\\0\\x3093\\x306B\\x3061\\x306F\" (no terminating NUL)",
875 TEST(PrintArrayTest, Char16ArrayWithTerminatingNul) {
876 const char16_t a[] = u
"\0こんにちは";
881 TEST(PrintArrayTest, Char32ArrayWithNoTerminatingNul) {
883 const char32_t a[] = {U
'👋', U
'\0', U
'🌌'};
884 EXPECT_EQ(
"U\"\\x1F44B\\0\\x1F30C\" (no terminating NUL)",
889 TEST(PrintArrayTest, Char32ArrayWithTerminatingNul) {
890 const char32_t a[] = U
"\0👋🌌";
895 TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
897 const wchar_t a[] = {L
'H', L
'\0', L
'i'};
902 TEST(PrintArrayTest, WCharArrayWithTerminatingNul) {
903 const wchar_t a[] = L
"\0Hi";
908 TEST(PrintArrayTest, ObjectArray) {
909 std::string a[3] = {
"Hi",
"Hello",
"Ni hao"};
914 TEST(PrintArrayTest, BigArray) {
915 int a[100] = {1, 2, 3};
916 EXPECT_EQ(
"{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
923 TEST(PrintStringTest, StringInStdNamespace) {
924 const char s[] =
"'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
925 const ::std::string str(s,
sizeof(s));
926 EXPECT_EQ(
"\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
930 TEST(PrintStringTest, StringAmbiguousHex) {
948 #if GTEST_HAS_STD_WSTRING
950 TEST(PrintWideStringTest, StringInStdNamespace) {
951 const wchar_t s[] = L
"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
952 const ::std::wstring str(s,
sizeof(s) /
sizeof(
wchar_t));
954 "L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
955 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
959 TEST(PrintWideStringTest, StringAmbiguousHex) {
963 EXPECT_EQ(
"L\"mm\\x6\" L\"bananas\"",
Print(::std::wstring(L
"mm\x6"
965 EXPECT_EQ(
"L\"NOM\\x6\" L\"BANANA\"",
Print(::std::wstring(L
"NOM\x6"
969 #endif // GTEST_HAS_STD_WSTRING
971 #ifdef __cpp_lib_char8_t
972 TEST(PrintStringTest, U8String) {
973 std::u8string str = u8
"Hello, 世界";
975 EXPECT_EQ(
"u8\"Hello, \\xE4\\xB8\\x96\\xE7\\x95\\x8C\"",
Print(str));
979 TEST(PrintStringTest, U16String) {
980 std::u16string str = u
"Hello, 世界";
985 TEST(PrintStringTest, U32String) {
986 std::u32string str = U
"Hello, 🗺️";
999 template <
typename Char,
typename CharTraits>
1001 std::basic_ostream<Char, CharTraits>& os,
1003 return os <<
"AllowsGenericStreaming";
1006 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
1013 template <
typename T>
1016 template <
typename Char,
typename CharTraits,
typename T>
1018 std::basic_ostream<Char, CharTraits>& os,
1020 return os <<
"AllowsGenericStreamingTemplate";
1023 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
1031 template <
typename T>
1034 operator bool()
const {
return false; }
1037 template <
typename Char,
typename CharTraits,
typename T>
1039 std::basic_ostream<Char, CharTraits>& os,
1041 return os <<
"AllowsGenericStreamingAndImplicitConversionTemplate";
1044 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
1046 EXPECT_EQ(
"AllowsGenericStreamingAndImplicitConversionTemplate",
Print(a));
1049 #if GTEST_INTERNAL_HAS_STRING_VIEW
1053 TEST(PrintStringViewTest, SimpleStringView) {
1054 const internal::StringView sp =
"Hello";
1058 TEST(PrintStringViewTest, UnprintableCharacters) {
1059 const char str[] =
"NUL (\0) and \r\t";
1060 const internal::StringView sp(str,
sizeof(str) - 1);
1064 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1068 TEST(PrintStlContainerTest, EmptyDeque) {
1073 TEST(PrintStlContainerTest, NonEmptyDeque) {
1074 deque<int> non_empty;
1075 non_empty.push_back(1);
1076 non_empty.push_back(3);
1080 TEST(PrintStlContainerTest, OneElementHashMap) {
1081 ::std::unordered_map<int, char> map1;
1086 TEST(PrintStlContainerTest, HashMultiMap) {
1087 ::std::unordered_multimap<int, bool> map1;
1088 map1.insert(make_pair(5,
true));
1089 map1.insert(make_pair(5,
false));
1092 const std::string result =
Print(map1);
1093 EXPECT_TRUE(result ==
"{ (5, true), (5, false) }" ||
1094 result ==
"{ (5, false), (5, true) }")
1095 <<
" where Print(map1) returns \"" << result <<
"\".";
1098 TEST(PrintStlContainerTest, HashSet) {
1099 ::std::unordered_set<int> set1;
1104 TEST(PrintStlContainerTest, HashMultiSet) {
1105 const int kSize = 5;
1106 int a[kSize] = {1, 1, 2, 5, 1};
1107 ::std::unordered_multiset<int> set1(a, a + kSize);
1110 const std::string result =
Print(set1);
1111 const std::string expected_pattern =
"{ d, d, d, d, d }";
1115 ASSERT_EQ(expected_pattern.length(), result.length());
1116 std::vector<int> numbers;
1117 for (
size_t i = 0;
i != result.length();
i++) {
1118 if (expected_pattern[
i] ==
'd') {
1119 ASSERT_NE(isdigit(static_cast<unsigned char>(result[
i])), 0);
1120 numbers.push_back(result[i] -
'0');
1123 <<
" where result is " << result;
1128 std::sort(numbers.begin(), numbers.end());
1129 std::sort(a, a + kSize);
1130 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
1133 TEST(PrintStlContainerTest, List) {
1134 const std::string a[] = {
"hello",
"world"};
1135 const list<std::string> strings(a, a + 2);
1140 map<int, bool> map1;
1147 TEST(PrintStlContainerTest, MultiMap) {
1148 multimap<bool, int> map1;
1155 map1.insert(pair<const bool, int>(
true, 0));
1156 map1.insert(pair<const bool, int>(
true, 1));
1157 map1.insert(pair<const bool, int>(
false, 2));
1162 const unsigned int a[] = {3, 0, 5};
1163 set<unsigned int> set1(a, a + 3);
1167 TEST(PrintStlContainerTest, MultiSet) {
1168 const int a[] = {1, 1, 2, 5, 1};
1169 multiset<int> set1(a, a + 5);
1173 TEST(PrintStlContainerTest, SinglyLinkedList) {
1174 int a[] = {9, 2, 8};
1175 const std::forward_list<int> ints(a, a + 3);
1179 TEST(PrintStlContainerTest, Pair) {
1180 pair<const bool, int>
p(
true, 5);
1184 TEST(PrintStlContainerTest, Vector) {
1191 TEST(PrintStlContainerTest, StdSpan) {
1192 #if GTEST_INTERNAL_HAS_STD_SPAN
1193 int a[] = {3, 6, 5};
1194 std::span<int> s =
a;
1199 #endif // GTEST_INTERNAL_HAS_STD_SPAN
1202 TEST(PrintStlContainerTest, LongSequence) {
1203 const int a[100] = {1, 2, 3};
1204 const vector<int> v(a, a + 100);
1206 "{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
1207 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }",
1211 TEST(PrintStlContainerTest, NestedContainer) {
1212 const int a1[] = {1, 2};
1213 const int a2[] = {3, 4, 5};
1214 const list<int> l1(a1, a1 + 2);
1215 const list<int> l2(a2, a2 + 3);
1217 vector<list<int>> v;
1223 TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
1224 const int a[3] = {1, 2, 3};
1225 NativeArray<int> b(a, 3, RelationToSourceReference());
1229 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
1230 const int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
1231 NativeArray<int[3]> b(a, 2, RelationToSourceReference());
1241 TEST(PrintStlContainerTest, Iterator) {
1252 TEST(PrintStlContainerTest, ConstIterator) {
1260 TEST(PrintStdTupleTest, VariousSizes) {
1264 ::std::tuple<int> t1(5);
1267 ::std::tuple<char, bool> t2(
'a',
true);
1270 ::std::tuple<bool, int, int> t3(
false, 2, 3);
1273 ::std::tuple<bool, int, int, int> t4(
false, 2, 3, 4);
1276 const char*
const str =
"8";
1277 ::std::tuple<bool, char, short, int32_t, int64_t, float, double,
1278 const char*,
void*, std::string>
1279 t10(
false,
'a', static_cast<short>(3), 4, 5, 1.5
F, -2.5, str,
1282 " pointing to \"8\", NULL, \"10\")",
1287 TEST(PrintStdTupleTest, NestedTuple) {
1288 ::std::tuple<::std::tuple<int, bool>,
char> nested(::std::make_tuple(5,
true),
1295 TEST(PrintReferenceWrapper, Printable) {
1301 TEST(PrintReferenceWrapper, Unprintable) {
1305 " 16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1306 Print(std::ref(up)));
1309 " 16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1310 Print(std::cref(up)));
1316 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
1321 TEST(PrintUnprintableTypeTest, InUserNamespace) {
1322 EXPECT_EQ(
"16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1333 TEST(PrintUnpritableTypeTest, BigObject) {
1335 "257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
1336 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1337 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1338 "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
1339 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1340 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1341 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
1348 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
1355 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
1360 TEST(PrintStreamableTypeTest, TypeInUserNamespaceWithTemplatedStreamOperator) {
1365 TEST(PrintStreamableTypeTest, SubclassUsesSuperclassStreamOperator) {
1376 TEST(PrintStreamableTypeTest, PathLikeInUserNamespace) {
1379 const ::foo::PathLike cx;
1384 TEST(PrintPrintableTypeTest, InUserNamespace) {
1390 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
1396 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
1397 EXPECT_EQ(
"PrintableViaPrintToTemplate: 5",
1403 TEST(PrintReferenceTest, PrintsAddressAndValue) {
1407 int a[2][3] = {{0, 1, 2}, {3, 4, 5}};
1411 const ::foo::UnprintableInFoo
x;
1414 "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1420 TEST(PrintReferenceTest, HandlesFunctionPointer) {
1422 const std::string fp_pointer_string =
1428 const std::string fp_string =
PrintPointer(reinterpret_cast<const void*>(
1429 reinterpret_cast<internal::BiggestInt>(fp)));
1435 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
1438 "@" +
PrintPointer(reinterpret_cast<const void*>(&p)) +
1439 " " +
Print(
sizeof(p)) +
"-byte object "));
1443 "@" +
PrintPointer(reinterpret_cast<const void*>(&p2)) +
1444 " " +
Print(
sizeof(p2)) +
"-byte object "));
1449 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
1452 Print(
sizeof(p)) +
"-byte object "));
1460 TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
1465 TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
1472 TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
1475 int n[] = {1, 2, 3};
1484 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
1491 const char* s =
"hello";
1501 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
1508 const wchar_t* s = L
"hello";
1521 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
1522 const char* s =
"hello \"world";
1527 char str[] =
"hi\1";
1533 #if GTEST_HAS_STD_WSTRING
1535 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
1536 const wchar_t* s = L
"hi \"world";
1541 wchar_t str[] = L
"hi\1";
1553 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
1554 char str[] =
"hi \"world\"";
1561 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
1562 const char str[] =
"hi \"world\"";
1568 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
1569 wchar_t str[] = L
"hi \"world\"";
1570 wchar_t* p =
nullptr;
1576 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
1577 const wchar_t str[] = L
"hi \"world\"";
1586 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
1587 const char str[] =
"hi \"world\"";
1592 #if GTEST_HAS_STD_WSTRING
1594 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
1595 const wchar_t str[] = L
"hi \"w\0rld\"";
1606 #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
1607 EXPECT_TRUE(PrintToString(value) == (expected_string)) \
1608 << " where " #value " prints as " << (PrintToString(value))
1612 TEST(PrintToStringTest, WorksForPointerToConstChar) {
1613 const char* p =
"hello";
1617 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
1623 TEST(PrintToStringTest, EscapesForPointerToConstChar) {
1624 const char* p =
"hello\n";
1628 TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
1629 char s[] =
"hello\1";
1634 TEST(PrintToStringTest, WorksForArray) {
1635 int n[3] = {1, 2, 3};
1639 TEST(PrintToStringTest, WorksForCharArray) {
1644 TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
1645 const char str_with_nul[] =
"hello\0 world";
1648 char mutable_str_with_nul[] =
"hello\0 world";
1652 TEST(PrintToStringTest, ContainsNonLatin) {
1654 std::string non_ascii_str = ::std::string(
"오전 4:30");
1656 "\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
1657 " As Text: \"오전 4:30\"");
1658 non_ascii_str = ::std::string(
"From ä — ẑ");
1660 "\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
1661 "\n As Text: \"From ä — ẑ\"");
1664 TEST(PrintToStringTest, PrintStreamableInLocal) {
1669 TEST(PrintToStringTest, PrintReferenceToStreamableInLocal) {
1671 std::reference_wrapper<foo::StreamableInLocal> r(s);
1675 TEST(PrintToStringTest, PrintReferenceToStreamableInGlobal) {
1677 std::reference_wrapper<StreamableInGlobal> r(s);
1681 #ifdef GTEST_HAS_ABSL
1682 TEST(PrintToStringTest, AbslStringify) {
1687 TEST(IsValidUTF8Test, IllFormedUTF8) {
1692 static const char*
const kTestdata[][2] = {
1694 {
"\xC3\x74",
"\"\\xC3t\""},
1696 {
"\xC3\x84\xA4",
"\"\\xC3\\x84\\xA4\""},
1698 {
"abc\xC3",
"\"abc\\xC3\""},
1700 {
"x\xE2\x70\x94",
"\"x\\xE2p\\x94\""},
1702 {
"\xE2\x80",
"\"\\xE2\\x80\""},
1704 {
"\xE2\x80\xC3\x84",
"\"\\xE2\\x80\\xC3\\x84\""},
1706 {
"\xE2\x80\x7A",
"\"\\xE2\\x80z\""},
1708 {
"\xE2\xE2\x80\x94",
"\"\\xE2\\xE2\\x80\\x94\""},
1710 {
"\xF0\xE2\x80\x94",
"\"\\xF0\\xE2\\x80\\x94\""},
1712 {
"\xF0\xE2\x80",
"\"\\xF0\\xE2\\x80\""},
1714 {
"abc\xE2\x80\x94\xC3\x74xyc",
"\"abc\\xE2\\x80\\x94\\xC3txyc\""},
1715 {
"abc\xC3\x84\xE2\x80\xC3\x84xyz",
1716 "\"abc\\xC3\\x84\\xE2\\x80\\xC3\\x84xyz\""},
1719 {
"\xC0\x80",
"\"\\xC0\\x80\""},
1720 {
"\xC1\x81",
"\"\\xC1\\x81\""},
1722 {
"\xE0\x80\x80",
"\"\\xE0\\x80\\x80\""},
1723 {
"\xf0\x80\x80\x80",
"\"\\xF0\\x80\\x80\\x80\""},
1727 {
"\xED\x9F\xBF",
"\"\\xED\\x9F\\xBF\"\n As Text: \"\""},
1729 {
"\xED\xA0\x80",
"\"\\xED\\xA0\\x80\""},
1731 {
"\xED\xAD\xBF",
"\"\\xED\\xAD\\xBF\""},
1733 {
"\xED\xAE\x80",
"\"\\xED\\xAE\\x80\""},
1735 {
"\xED\xAF\xBF",
"\"\\xED\\xAF\\xBF\""},
1737 {
"\xED\xB3\xBF",
"\"\\xED\\xB3\\xBF\""},
1741 {
"\xEE\x80\x80",
"\"\\xEE\\x80\\x80\"\n As Text: \"\""}};
1743 for (
int i = 0;
i < int(
sizeof(kTestdata) /
sizeof(kTestdata[0])); ++
i) {
1748 #undef EXPECT_PRINT_TO_STRING_
1750 TEST(UniversalTersePrintTest, WorksForNonReference) {
1751 ::std::stringstream ss;
1756 TEST(UniversalTersePrintTest, WorksForReference) {
1758 ::std::stringstream ss;
1763 TEST(UniversalTersePrintTest, WorksForCString) {
1764 const char* s1 =
"abc";
1765 ::std::stringstream ss1;
1769 char* s2 =
const_cast<char*
>(s1);
1770 ::std::stringstream ss2;
1774 const char* s3 =
nullptr;
1775 ::std::stringstream ss3;
1780 TEST(UniversalPrintTest, WorksForNonReference) {
1781 ::std::stringstream ss;
1786 TEST(UniversalPrintTest, WorksForReference) {
1788 ::std::stringstream ss;
1793 TEST(UniversalPrintTest, WorksForPairWithConst) {
1794 std::pair<const Wrapper<std::string>,
int>
p(Wrapper<std::string>(
"abc"), 1);
1795 ::std::stringstream ss;
1797 EXPECT_EQ(
"(Wrapper(\"abc\"), 1)", ss.str());
1800 TEST(UniversalPrintTest, WorksForCString) {
1801 const char* s1 =
"abc";
1802 ::std::stringstream ss1;
1806 char* s2 =
const_cast<char*
>(s1);
1807 ::std::stringstream ss2;
1811 const char* s3 =
nullptr;
1812 ::std::stringstream ss3;
1817 TEST(UniversalPrintTest, WorksForCharArray) {
1818 const char str[] =
"\"Line\0 1\"\nLine 2";
1819 ::std::stringstream ss1;
1821 EXPECT_EQ(
"\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
1823 const char mutable_str[] =
"\"Line\0 1\"\nLine 2";
1824 ::std::stringstream ss2;
1826 EXPECT_EQ(
"\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
1829 TEST(UniversalPrintTest, IncompleteType) {
1831 char some_object = 0;
1836 TEST(UniversalPrintTest, SmartPointers) {
1838 std::unique_ptr<int>
p(
new int(17));
1841 std::unique_ptr<int[]> p2(
new int[2]);
1845 std::shared_ptr<int> p3(
new int(1979));
1848 #if defined(__cpp_lib_shared_ptr_arrays) && \
1849 (__cpp_lib_shared_ptr_arrays >= 201611L)
1850 std::shared_ptr<int[]> p4(
new int[2]);
1868 #if defined(__cpp_lib_shared_ptr_arrays) && \
1869 (__cpp_lib_shared_ptr_arrays >= 201611L)
1879 nullptr,
nullptr)));
1882 std::unique_ptr<
void,
void (*)(
void*)>(p.get(), [](
void*) {})));
1885 PrintToString(std::shared_ptr<void>(p.get(), [](
void*) {})));
1888 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
1893 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
1900 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
1908 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
1911 ::std::tuple<const int&, const char*>(n,
"a"));
1917 #if GTEST_INTERNAL_HAS_ANY
1920 template <
typename T>
1921 static std::string ExpectedTypeName() {
1923 return internal::GetTypeName<T>();
1925 return "<unknown_type>";
1926 #endif // GTEST_HAS_RTTI
1930 TEST_F(PrintAnyTest, Empty) {
1935 TEST_F(PrintAnyTest, NonEmpty) {
1937 constexpr
int val1 = 10;
1938 const std::string val2 =
"content";
1944 EXPECT_EQ(
"value of type " + ExpectedTypeName<std::string>(),
1947 #endif // GTEST_INTERNAL_HAS_ANY
1949 #if GTEST_INTERNAL_HAS_OPTIONAL
1950 TEST(PrintOptionalTest, Basic) {
1952 internal::Optional<int>
value;
1959 #endif // GTEST_INTERNAL_HAS_OPTIONAL
1961 #if GTEST_INTERNAL_HAS_VARIANT
1962 struct NonPrintable {
1963 unsigned char contents = 17;
1966 TEST(PrintOneofTest, Basic) {
1967 using Type = internal::Variant<int, StreamableInGlobal, NonPrintable>;
1969 EXPECT_EQ(
"('StreamableInGlobal(index = 1)' with value StreamableInGlobal)",
1972 "('testing::gtest_printers_test::NonPrintable(index = 2)' with value "
1973 "1-byte object <11>)",
1976 #endif // GTEST_INTERNAL_HAS_VARIANT
1978 #if GTEST_INTERNAL_HAS_COMPARE_LIB
1979 TEST(PrintOrderingTest, Basic) {
2005 string_ptr(
const char* data,
size_t size) :
data_(data),
size_(size) {}
2007 string_ptr& operator++() noexcept {
2024 string_ref(
const char* data,
size_t size) :
data_(data),
size_(size) {}
2026 string_ptr operator&()
const noexcept {
return {
data_, size_}; }
2028 bool operator==(
const char* s)
const noexcept {
2029 if (size_ > 0 && data_[size_ - 1] != 0) {
2030 return std::string(data_, size_) == std::string(s);
2032 return std::string(data_) == std::string(s);
2041 string_ref string_ptr::operator*() const noexcept {
return {
data_,
size_}; }
2043 TEST(string_ref, compare) {
2044 const char* s =
"alex\0davidjohn\0";
2045 string_ptr ptr(s, 5);
TEST(PrintEnumTest, AnonymousEnum)
AssertionResult AssertionFailure()
#define EXPECT_PRINT_TO_STRING_(value, expected_string)
#define ASSERT_NE(val1, val2)
::std::string PrintToString(const T &value)
TEST_F(TestInfoTest, Names)
#define EXPECT_NE(val1, val2)
UnprintableTemplateInGlobal()
void UniversalTersePrint(const T &value,::std::ostream *os)
std::basic_ostream< Char, CharTraits > & operator<<(std::basic_ostream< Char, CharTraits > &os, const AllowsGenericStreaming &)
std::string Print(const T &value)
#define ASSERT_EQ(val1, val2)
::std::ostream & operator<<(::std::ostream &os, const PointerPrintable *)
::std::vector<::std::string > Strings
std::string FormatForComparisonFailureMessage(const T1 &value, const T2 &)
std::string PrintArrayHelper(T(&a)[N])
BigUInt< n > operator*(BigUInt< n > const &a, BigUInt< n > const &b)
#define EXPECT_LT(val1, val2)
std::ostream & operator<<(std::ostream &os, const Expr< T > &xx)
AssertionResult AssertionSuccess()
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
void UniversalPrint(const T &value,::std::ostream *os)
ADVar foo(double d, ADVar x, ADVar y)
PrintableViaPrintToTemplate(const T &a_value)
#define EXPECT_STREQ(s1, s2)
void PrintTo(const PrintableViaPrintTo &x,::std::ostream *os)
void PrintTo(const T &value,::std::ostream *os)
virtual ~StreamableInGlobal()=default
Strings UniversalTersePrintTupleFieldsToStrings(const Tuple &value)
AssertionResult HasPrefix(const StringType &str, const StringType &prefix)
bool operator==(const Handle< T > &h1, const Handle< T > &h2)
Compare two handles.
virtual char MyVirtualMethod(int)
#define EXPECT_EQ(val1, val2)
static std::string PrintPointer(const void *p)
StreamableTemplateInFoo()
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
#define EXPECT_TRUE(condition)
std::string PrintByRef(const T &value)
friend::std::ostream & operator<<(::std::ostream &os, const PathLike &)
static void Print(const Wrapper< T > &w,::std::ostream *os)