1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Google Test - The Google C++ Testing and Mocking Framework
34 // This file tests the universal value printer.
36 #include "gtest/gtest-printers.h"
51 #include "gtest/gtest.h"
53 #if GTEST_HAS_UNORDERED_MAP_
54 # include <unordered_map> // NOLINT
55 #endif // GTEST_HAS_UNORDERED_MAP_
57 #if GTEST_HAS_UNORDERED_SET_
58 # include <unordered_set> // NOLINT
59 #endif // GTEST_HAS_UNORDERED_SET_
61 #if GTEST_HAS_STD_FORWARD_LIST_
62 # include <forward_list> // NOLINT
63 #endif // GTEST_HAS_STD_FORWARD_LIST_
65 // Some user-defined types for testing the universal value printer.
67 // An anonymous enum type.
73 // An enum without a user-defined printer.
74 enum EnumWithoutPrinter
{
79 // An enum with a << operator.
80 enum EnumWithStreaming
{
84 std::ostream
& operator<<(std::ostream
& os
, EnumWithStreaming e
) {
85 return os
<< (e
== kEWS1
? "kEWS1" : "invalid");
88 // An enum with a PrintTo() function.
89 enum EnumWithPrintTo
{
93 void PrintTo(EnumWithPrintTo e
, std::ostream
* os
) {
94 *os
<< (e
== kEWPT1
? "kEWPT1" : "invalid");
97 // A class implicitly convertible to BiggestInt.
98 class BiggestIntConvertible
{
100 operator ::testing::internal::BiggestInt() const { return 42; }
103 // A user-defined unprintable class template in the global namespace.
104 template <typename T
>
105 class UnprintableTemplateInGlobal
{
107 UnprintableTemplateInGlobal() : value_() {}
112 // A user-defined streamable type in the global namespace.
113 class StreamableInGlobal
{
115 virtual ~StreamableInGlobal() {}
118 inline void operator<<(::std::ostream
& os
, const StreamableInGlobal
& /* x */) {
119 os
<< "StreamableInGlobal";
122 void operator<<(::std::ostream
& os
, const StreamableInGlobal
* /* x */) {
123 os
<< "StreamableInGlobal*";
128 // A user-defined unprintable type in a user namespace.
129 class UnprintableInFoo
{
131 UnprintableInFoo() : z_(0) { memcpy(xy_
, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); }
132 double z() const { return z_
; }
138 // A user-defined printable type in a user-chosen namespace.
139 struct PrintableViaPrintTo
{
140 PrintableViaPrintTo() : value() {}
144 void PrintTo(const PrintableViaPrintTo
& x
, ::std::ostream
* os
) {
145 *os
<< "PrintableViaPrintTo: " << x
.value
;
148 // A type with a user-defined << for printing its pointer.
149 struct PointerPrintable
{
152 ::std::ostream
& operator<<(::std::ostream
& os
,
153 const PointerPrintable
* /* x */) {
154 return os
<< "PointerPrintable*";
157 // A user-defined printable class template in a user-chosen namespace.
158 template <typename T
>
159 class PrintableViaPrintToTemplate
{
161 explicit PrintableViaPrintToTemplate(const T
& a_value
) : value_(a_value
) {}
163 const T
& value() const { return value_
; }
168 template <typename T
>
169 void PrintTo(const PrintableViaPrintToTemplate
<T
>& x
, ::std::ostream
* os
) {
170 *os
<< "PrintableViaPrintToTemplate: " << x
.value();
173 // A user-defined streamable class template in a user namespace.
174 template <typename T
>
175 class StreamableTemplateInFoo
{
177 StreamableTemplateInFoo() : value_() {}
179 const T
& value() const { return value_
; }
184 template <typename T
>
185 inline ::std::ostream
& operator<<(::std::ostream
& os
,
186 const StreamableTemplateInFoo
<T
>& x
) {
187 return os
<< "StreamableTemplateInFoo: " << x
.value();
190 // A user-defined streamable but recursivly-defined container type in
191 // a user namespace, it mimics therefore std::filesystem::path or
192 // boost::filesystem::path.
196 typedef PathLike value_type
;
201 iterator
begin() const { return iterator(); }
202 iterator
end() const { return iterator(); }
204 friend ::std::ostream
& operator<<(::std::ostream
& os
, const PathLike
&) {
205 return os
<< "Streamable-PathLike";
212 namespace gtest_printers_test
{
216 using ::std::make_pair
;
218 using ::std::multimap
;
219 using ::std::multiset
;
223 using ::testing::PrintToString
;
224 using ::testing::internal::FormatForComparisonFailureMessage
;
225 using ::testing::internal::ImplicitCast_
;
226 using ::testing::internal::NativeArray
;
227 using ::testing::internal::RE
;
228 using ::testing::internal::RelationToSourceReference
;
229 using ::testing::internal::Strings
;
230 using ::testing::internal::UniversalPrint
;
231 using ::testing::internal::UniversalPrinter
;
232 using ::testing::internal::UniversalTersePrint
;
233 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
234 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings
;
237 // Prints a value to a string using the universal value printer. This
238 // is a helper for testing UniversalPrinter<T>::Print() for various types.
239 template <typename T
>
240 std::string
Print(const T
& value
) {
241 ::std::stringstream ss
;
242 UniversalPrinter
<T
>::Print(value
, &ss
);
246 // Prints a value passed by reference to a string, using the universal
247 // value printer. This is a helper for testing
248 // UniversalPrinter<T&>::Print() for various types.
249 template <typename T
>
250 std::string
PrintByRef(const T
& value
) {
251 ::std::stringstream ss
;
252 UniversalPrinter
<T
&>::Print(value
, &ss
);
256 // Tests printing various enum types.
258 TEST(PrintEnumTest
, AnonymousEnum
) {
259 EXPECT_EQ("-1", Print(kAE1
));
260 EXPECT_EQ("1", Print(kAE2
));
263 TEST(PrintEnumTest
, EnumWithoutPrinter
) {
264 EXPECT_EQ("-2", Print(kEWP1
));
265 EXPECT_EQ("42", Print(kEWP2
));
268 TEST(PrintEnumTest
, EnumWithStreaming
) {
269 EXPECT_EQ("kEWS1", Print(kEWS1
));
270 EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming
>(0)));
273 TEST(PrintEnumTest
, EnumWithPrintTo
) {
274 EXPECT_EQ("kEWPT1", Print(kEWPT1
));
275 EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo
>(0)));
278 // Tests printing a class implicitly convertible to BiggestInt.
280 TEST(PrintClassTest
, BiggestIntConvertible
) {
281 EXPECT_EQ("42", Print(BiggestIntConvertible()));
284 // Tests printing various char types.
287 TEST(PrintCharTest
, PlainChar
) {
288 EXPECT_EQ("'\\0'", Print('\0'));
289 EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
290 EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
291 EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
292 EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
293 EXPECT_EQ("'\\a' (7)", Print('\a'));
294 EXPECT_EQ("'\\b' (8)", Print('\b'));
295 EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
296 EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
297 EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
298 EXPECT_EQ("'\\t' (9)", Print('\t'));
299 EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
300 EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
301 EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
302 EXPECT_EQ("' ' (32, 0x20)", Print(' '));
303 EXPECT_EQ("'a' (97, 0x61)", Print('a'));
307 TEST(PrintCharTest
, SignedChar
) {
308 EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
309 EXPECT_EQ("'\\xCE' (-50)",
310 Print(static_cast<signed char>(-50)));
314 TEST(PrintCharTest
, UnsignedChar
) {
315 EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
316 EXPECT_EQ("'b' (98, 0x62)",
317 Print(static_cast<unsigned char>('b')));
320 // Tests printing other simple, built-in types.
323 TEST(PrintBuiltInTypeTest
, Bool
) {
324 EXPECT_EQ("false", Print(false));
325 EXPECT_EQ("true", Print(true));
329 TEST(PrintBuiltInTypeTest
, Wchar_t
) {
330 EXPECT_EQ("L'\\0'", Print(L
'\0'));
331 EXPECT_EQ("L'\\'' (39, 0x27)", Print(L
'\''));
332 EXPECT_EQ("L'\"' (34, 0x22)", Print(L
'"'));
333 EXPECT_EQ("L'?' (63, 0x3F)", Print(L
'?'));
334 EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L
'\\'));
335 EXPECT_EQ("L'\\a' (7)", Print(L
'\a'));
336 EXPECT_EQ("L'\\b' (8)", Print(L
'\b'));
337 EXPECT_EQ("L'\\f' (12, 0xC)", Print(L
'\f'));
338 EXPECT_EQ("L'\\n' (10, 0xA)", Print(L
'\n'));
339 EXPECT_EQ("L'\\r' (13, 0xD)", Print(L
'\r'));
340 EXPECT_EQ("L'\\t' (9)", Print(L
'\t'));
341 EXPECT_EQ("L'\\v' (11, 0xB)", Print(L
'\v'));
342 EXPECT_EQ("L'\\x7F' (127)", Print(L
'\x7F'));
343 EXPECT_EQ("L'\\xFF' (255)", Print(L
'\xFF'));
344 EXPECT_EQ("L' ' (32, 0x20)", Print(L
' '));
345 EXPECT_EQ("L'a' (97, 0x61)", Print(L
'a'));
346 EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
347 EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
350 // Test that Int64 provides more storage than wchar_t.
351 TEST(PrintTypeSizeTest
, Wchar_t
) {
352 EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64
));
355 // Various integer types.
356 TEST(PrintBuiltInTypeTest
, Integer
) {
357 EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8
358 EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8
359 EXPECT_EQ("65535", Print(USHRT_MAX
)); // uint16
360 EXPECT_EQ("-32768", Print(SHRT_MIN
)); // int16
361 EXPECT_EQ("4294967295", Print(UINT_MAX
)); // uint32
362 EXPECT_EQ("-2147483648", Print(INT_MIN
)); // int32
363 EXPECT_EQ("18446744073709551615",
364 Print(static_cast<testing::internal::UInt64
>(-1))); // uint64
365 EXPECT_EQ("-9223372036854775808",
366 Print(static_cast<testing::internal::Int64
>(1) << 63)); // int64
370 TEST(PrintBuiltInTypeTest
, Size_t
) {
371 EXPECT_EQ("1", Print(sizeof('a'))); // size_t.
372 #if !GTEST_OS_WINDOWS
373 // Windows has no ssize_t type.
374 EXPECT_EQ("-2", Print(static_cast<ssize_t
>(-2))); // ssize_t.
375 #endif // !GTEST_OS_WINDOWS
379 TEST(PrintBuiltInTypeTest
, FloatingPoints
) {
380 EXPECT_EQ("1.5", Print(1.5f
)); // float
381 EXPECT_EQ("-2.5", Print(-2.5)); // double
384 // Since ::std::stringstream::operator<<(const void *) formats the pointer
385 // output differently with different compilers, we have to create the expected
386 // output first and use it as our expectation.
387 static std::string
PrintPointer(const void* p
) {
388 ::std::stringstream expected_result_stream
;
389 expected_result_stream
<< p
;
390 return expected_result_stream
.str();
393 // Tests printing C strings.
396 TEST(PrintCStringTest
, Const
) {
397 const char* p
= "World";
398 EXPECT_EQ(PrintPointer(p
) + " pointing to \"World\"", Print(p
));
402 TEST(PrintCStringTest
, NonConst
) {
404 EXPECT_EQ(PrintPointer(p
) + " pointing to \"Hi\"",
405 Print(static_cast<char*>(p
)));
409 TEST(PrintCStringTest
, Null
) {
410 const char* p
= NULL
;
411 EXPECT_EQ("NULL", Print(p
));
414 // Tests that C strings are escaped properly.
415 TEST(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\"",
422 // MSVC compiler can be configured to define whar_t as a typedef
423 // of unsigned short. Defining an overload for const wchar_t* in that case
424 // would cause pointers to unsigned shorts be printed as wide strings,
425 // possibly accessing more memory than intended and causing invalid
426 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
427 // wchar_t is implemented as a native type.
428 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
431 TEST(PrintWideCStringTest
, Const
) {
432 const wchar_t* p
= L
"World";
433 EXPECT_EQ(PrintPointer(p
) + " pointing to L\"World\"", Print(p
));
437 TEST(PrintWideCStringTest
, NonConst
) {
439 EXPECT_EQ(PrintPointer(p
) + " pointing to L\"Hi\"",
440 Print(static_cast<wchar_t*>(p
)));
443 // NULL wide C string.
444 TEST(PrintWideCStringTest
, Null
) {
445 const wchar_t* p
= NULL
;
446 EXPECT_EQ("NULL", Print(p
));
449 // Tests that wide C strings are escaped properly.
450 TEST(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
)));
457 #endif // native wchar_t
459 // Tests printing pointers to other char types.
462 TEST(PrintCharPointerTest
, SignedChar
) {
463 signed char* p
= reinterpret_cast<signed char*>(0x1234);
464 EXPECT_EQ(PrintPointer(p
), Print(p
));
466 EXPECT_EQ("NULL", Print(p
));
469 // const signed char*.
470 TEST(PrintCharPointerTest
, ConstSignedChar
) {
471 signed char* p
= reinterpret_cast<signed char*>(0x1234);
472 EXPECT_EQ(PrintPointer(p
), Print(p
));
474 EXPECT_EQ("NULL", Print(p
));
478 TEST(PrintCharPointerTest
, UnsignedChar
) {
479 unsigned char* p
= reinterpret_cast<unsigned char*>(0x1234);
480 EXPECT_EQ(PrintPointer(p
), Print(p
));
482 EXPECT_EQ("NULL", Print(p
));
485 // const unsigned char*.
486 TEST(PrintCharPointerTest
, ConstUnsignedChar
) {
487 const unsigned char* p
= reinterpret_cast<const unsigned char*>(0x1234);
488 EXPECT_EQ(PrintPointer(p
), Print(p
));
490 EXPECT_EQ("NULL", Print(p
));
493 // Tests printing pointers to simple, built-in types.
496 TEST(PrintPointerToBuiltInTypeTest
, Bool
) {
497 bool* p
= reinterpret_cast<bool*>(0xABCD);
498 EXPECT_EQ(PrintPointer(p
), Print(p
));
500 EXPECT_EQ("NULL", Print(p
));
504 TEST(PrintPointerToBuiltInTypeTest
, Void
) {
505 void* p
= reinterpret_cast<void*>(0xABCD);
506 EXPECT_EQ(PrintPointer(p
), Print(p
));
508 EXPECT_EQ("NULL", Print(p
));
512 TEST(PrintPointerToBuiltInTypeTest
, ConstVoid
) {
513 const void* p
= reinterpret_cast<const void*>(0xABCD);
514 EXPECT_EQ(PrintPointer(p
), Print(p
));
516 EXPECT_EQ("NULL", Print(p
));
519 // Tests printing pointers to pointers.
520 TEST(PrintPointerToPointerTest
, IntPointerPointer
) {
521 int** p
= reinterpret_cast<int**>(0xABCD);
522 EXPECT_EQ(PrintPointer(p
), Print(p
));
524 EXPECT_EQ("NULL", Print(p
));
527 // Tests printing (non-member) function pointers.
529 void MyFunction(int /* n */) {}
531 TEST(PrintPointerTest
, NonMemberFunctionPointer
) {
532 // We cannot directly cast &MyFunction to const void* because the
533 // standard disallows casting between pointers to functions and
534 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
537 PrintPointer(reinterpret_cast<const void*>(
538 reinterpret_cast<internal::BiggestInt
>(&MyFunction
))),
540 int (*p
)(bool) = NULL
; // NOLINT
541 EXPECT_EQ("NULL", Print(p
));
544 // An assertion predicate determining whether a one string is a prefix for
546 template <typename StringType
>
547 AssertionResult
HasPrefix(const StringType
& str
, const StringType
& prefix
) {
548 if (str
.find(prefix
, 0) == 0)
549 return AssertionSuccess();
551 const bool is_wide_string
= sizeof(prefix
[0]) > 1;
552 const char* const begin_string_quote
= is_wide_string
? "L\"" : "\"";
553 return AssertionFailure()
554 << begin_string_quote
<< prefix
<< "\" is not a prefix of "
555 << begin_string_quote
<< str
<< "\"\n";
558 // Tests printing member variable pointers. Although they are called
559 // pointers, they don't point to a location in the address space.
560 // Their representation is implementation-defined. Thus they will be
561 // printed as raw bytes.
566 int MyMethod(char x
) { return x
+ 1; }
567 virtual char MyVirtualMethod(int /* n */) { return 'a'; }
572 TEST(PrintPointerTest
, MemberVariablePointer
) {
573 EXPECT_TRUE(HasPrefix(Print(&Foo::value
),
574 Print(sizeof(&Foo::value
)) + "-byte object "));
575 int (Foo::*p
) = NULL
; // NOLINT
576 EXPECT_TRUE(HasPrefix(Print(p
),
577 Print(sizeof(p
)) + "-byte object "));
580 // Tests printing member function pointers. Although they are called
581 // pointers, they don't point to a location in the address space.
582 // Their representation is implementation-defined. Thus they will be
583 // printed as raw bytes.
584 TEST(PrintPointerTest
, MemberFunctionPointer
) {
585 EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod
),
586 Print(sizeof(&Foo::MyMethod
)) + "-byte object "));
588 HasPrefix(Print(&Foo::MyVirtualMethod
),
589 Print(sizeof((&Foo::MyVirtualMethod
))) + "-byte object "));
590 int (Foo::*p
)(char) = NULL
; // NOLINT
591 EXPECT_TRUE(HasPrefix(Print(p
),
592 Print(sizeof(p
)) + "-byte object "));
595 // Tests printing C arrays.
597 // The difference between this and Print() is that it ensures that the
598 // argument is a reference to an array.
599 template <typename T
, size_t N
>
600 std::string
PrintArrayHelper(T (&a
)[N
]) {
604 // One-dimensional array.
605 TEST(PrintArrayTest
, OneDimensionalArray
) {
606 int a
[5] = { 1, 2, 3, 4, 5 };
607 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a
));
610 // Two-dimensional array.
611 TEST(PrintArrayTest
, TwoDimensionalArray
) {
616 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a
));
619 // Array of const elements.
620 TEST(PrintArrayTest
, ConstArray
) {
621 const bool a
[1] = { false };
622 EXPECT_EQ("{ false }", PrintArrayHelper(a
));
625 // char array without terminating NUL.
626 TEST(PrintArrayTest
, CharArrayWithNoTerminatingNul
) {
627 // Array a contains '\0' in the middle and doesn't end with '\0'.
628 char a
[] = { 'H', '\0', 'i' };
629 EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a
));
632 // const char array with terminating NUL.
633 TEST(PrintArrayTest
, ConstCharArrayWithTerminatingNul
) {
634 const char a
[] = "\0Hi";
635 EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a
));
638 // const wchar_t array without terminating NUL.
639 TEST(PrintArrayTest
, WCharArrayWithNoTerminatingNul
) {
640 // Array a contains '\0' in the middle and doesn't end with '\0'.
641 const wchar_t a
[] = { L
'H', L
'\0', L
'i' };
642 EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a
));
645 // wchar_t array with terminating NUL.
646 TEST(PrintArrayTest
, WConstCharArrayWithTerminatingNul
) {
647 const wchar_t a
[] = L
"\0Hi";
648 EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a
));
652 TEST(PrintArrayTest
, ObjectArray
) {
653 std::string a
[3] = {"Hi", "Hello", "Ni hao"};
654 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a
));
657 // Array with many elements.
658 TEST(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 }",
661 PrintArrayHelper(a
));
664 // Tests printing ::string and ::std::string.
666 #if GTEST_HAS_GLOBAL_STRING
668 TEST(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\"",
674 #endif // GTEST_HAS_GLOBAL_STRING
677 TEST(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\"",
684 TEST(PrintStringTest
, StringAmbiguousHex
) {
685 // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
686 // '\x6', '\x6B', or '\x6BA'.
688 // a hex escaping sequence following by a decimal digit
689 EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
690 // a hex escaping sequence following by a hex digit (lower-case)
691 EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
692 // a hex escaping sequence following by a hex digit (upper-case)
693 EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
694 // a hex escaping sequence following by a non-xdigit
695 EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
698 // Tests printing ::wstring and ::std::wstring.
700 #if GTEST_HAS_GLOBAL_WSTRING
702 TEST(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\"",
709 #endif // GTEST_HAS_GLOBAL_WSTRING
711 #if GTEST_HAS_STD_WSTRING
713 TEST(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\"",
721 TEST(PrintWideStringTest
, StringAmbiguousHex
) {
722 // same for wide strings.
723 EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L
"0\x12" L
"3")));
724 EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
725 Print(::std::wstring(L
"mm\x6" L
"bananas")));
726 EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
727 Print(::std::wstring(L
"NOM\x6" L
"BANANA")));
728 EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L
"!\x5-!")));
730 #endif // GTEST_HAS_STD_WSTRING
732 // Tests printing types that support generic streaming (i.e. streaming
733 // to std::basic_ostream<Char, CharTraits> for any valid Char and
734 // CharTraits types).
736 // Tests printing a non-template type that supports generic streaming.
738 class AllowsGenericStreaming
{};
740 template <typename Char
, typename CharTraits
>
741 std::basic_ostream
<Char
, CharTraits
>& operator<<(
742 std::basic_ostream
<Char
, CharTraits
>& os
,
743 const AllowsGenericStreaming
& /* a */) {
744 return os
<< "AllowsGenericStreaming";
747 TEST(PrintTypeWithGenericStreamingTest
, NonTemplateType
) {
748 AllowsGenericStreaming a
;
749 EXPECT_EQ("AllowsGenericStreaming", Print(a
));
752 // Tests printing a template type that supports generic streaming.
754 template <typename T
>
755 class AllowsGenericStreamingTemplate
{};
757 template <typename Char
, typename CharTraits
, typename T
>
758 std::basic_ostream
<Char
, CharTraits
>& operator<<(
759 std::basic_ostream
<Char
, CharTraits
>& os
,
760 const AllowsGenericStreamingTemplate
<T
>& /* a */) {
761 return os
<< "AllowsGenericStreamingTemplate";
764 TEST(PrintTypeWithGenericStreamingTest
, TemplateType
) {
765 AllowsGenericStreamingTemplate
<int> a
;
766 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a
));
769 // Tests printing a type that supports generic streaming and can be
770 // implicitly converted to another printable type.
772 template <typename T
>
773 class AllowsGenericStreamingAndImplicitConversionTemplate
{
775 operator bool() const { return false; }
778 template <typename Char
, typename CharTraits
, typename T
>
779 std::basic_ostream
<Char
, CharTraits
>& operator<<(
780 std::basic_ostream
<Char
, CharTraits
>& os
,
781 const AllowsGenericStreamingAndImplicitConversionTemplate
<T
>& /* a */) {
782 return os
<< "AllowsGenericStreamingAndImplicitConversionTemplate";
785 TEST(PrintTypeWithGenericStreamingTest
, TypeImplicitlyConvertible
) {
786 AllowsGenericStreamingAndImplicitConversionTemplate
<int> a
;
787 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a
));
792 // Tests printing ::absl::string_view.
794 TEST(PrintStringViewTest
, SimpleStringView
) {
795 const ::absl::string_view sp
= "Hello";
796 EXPECT_EQ("\"Hello\"", Print(sp
));
799 TEST(PrintStringViewTest
, UnprintableCharacters
) {
800 const char str
[] = "NUL (\0) and \r\t";
801 const ::absl::string_view
sp(str
, sizeof(str
) - 1);
802 EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp
));
805 #endif // GTEST_HAS_ABSL
807 // Tests printing STL containers.
809 TEST(PrintStlContainerTest
, EmptyDeque
) {
811 EXPECT_EQ("{}", Print(empty
));
814 TEST(PrintStlContainerTest
, NonEmptyDeque
) {
815 deque
<int> non_empty
;
816 non_empty
.push_back(1);
817 non_empty
.push_back(3);
818 EXPECT_EQ("{ 1, 3 }", Print(non_empty
));
821 #if GTEST_HAS_UNORDERED_MAP_
823 TEST(PrintStlContainerTest
, OneElementHashMap
) {
824 ::std::unordered_map
<int, char> map1
;
826 EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1
));
829 TEST(PrintStlContainerTest
, HashMultiMap
) {
830 ::std::unordered_multimap
<int, bool> map1
;
831 map1
.insert(make_pair(5, true));
832 map1
.insert(make_pair(5, false));
834 // Elements of hash_multimap can be printed in any order.
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
<< "\".";
841 #endif // GTEST_HAS_UNORDERED_MAP_
843 #if GTEST_HAS_UNORDERED_SET_
845 TEST(PrintStlContainerTest
, HashSet
) {
846 ::std::unordered_set
<int> set1
;
848 EXPECT_EQ("{ 1 }", Print(set1
));
851 TEST(PrintStlContainerTest
, HashMultiSet
) {
853 int a
[kSize
] = { 1, 1, 2, 5, 1 };
854 ::std::unordered_multiset
<int> set1(a
, a
+ kSize
);
856 // Elements of hash_multiset can be printed in any order.
857 const std::string result
= Print(set1
);
858 const std::string expected_pattern
= "{ d, d, d, d, d }"; // d means a digit.
860 // Verifies the result matches the expected pattern; also extracts
861 // the numbers in the result.
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 "
874 // Makes sure the result contains the right numbers.
875 std::sort(numbers
.begin(), numbers
.end());
876 std::sort(a
, a
+ kSize
);
877 EXPECT_TRUE(std::equal(a
, a
+ kSize
, numbers
.begin()));
880 #endif // GTEST_HAS_UNORDERED_SET_
882 TEST(PrintStlContainerTest
, List
) {
883 const std::string a
[] = {"hello", "world"};
884 const list
<std::string
> strings(a
, a
+ 2);
885 EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings
));
888 TEST(PrintStlContainerTest
, Map
) {
893 EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1
));
896 TEST(PrintStlContainerTest
, MultiMap
) {
897 multimap
<bool, int> map1
;
898 // The make_pair template function would deduce the type as
899 // pair<bool, int> here, and since the key part in a multimap has to
900 // be constant, without a templated ctor in the pair class (as in
901 // libCstd on Solaris), make_pair call would fail to compile as no
902 // implicit conversion is found. Thus explicit typename is used
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));
907 EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1
));
910 TEST(PrintStlContainerTest
, Set
) {
911 const unsigned int a
[] = { 3, 0, 5 };
912 set
<unsigned int> set1(a
, a
+ 3);
913 EXPECT_EQ("{ 0, 3, 5 }", Print(set1
));
916 TEST(PrintStlContainerTest
, MultiSet
) {
917 const int a
[] = { 1, 1, 2, 5, 1 };
918 multiset
<int> set1(a
, a
+ 5);
919 EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1
));
922 #if GTEST_HAS_STD_FORWARD_LIST_
923 // <slist> is available on Linux in the google3 mode, but not on
924 // Windows or Mac OS X.
926 TEST(PrintStlContainerTest
, SinglyLinkedList
) {
927 int a
[] = { 9, 2, 8 };
928 const std::forward_list
<int> ints(a
, a
+ 3);
929 EXPECT_EQ("{ 9, 2, 8 }", Print(ints
));
931 #endif // GTEST_HAS_STD_FORWARD_LIST_
933 TEST(PrintStlContainerTest
, Pair
) {
934 pair
<const bool, int> p(true, 5);
935 EXPECT_EQ("(true, 5)", Print(p
));
938 TEST(PrintStlContainerTest
, Vector
) {
942 EXPECT_EQ("{ 1, 2 }", Print(v
));
945 TEST(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
));
952 TEST(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
;
961 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v
));
964 TEST(PrintStlContainerTest
, OneDimensionalNativeArray
) {
965 const int a
[3] = { 1, 2, 3 };
966 NativeArray
<int> b(a
, 3, RelationToSourceReference());
967 EXPECT_EQ("{ 1, 2, 3 }", Print(b
));
970 TEST(PrintStlContainerTest
, TwoDimensionalNativeArray
) {
971 const int a
[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
972 NativeArray
<int[3]> b(a
, 2, RelationToSourceReference());
973 EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b
));
976 // Tests that a class named iterator isn't treated as a container.
982 TEST(PrintStlContainerTest
, Iterator
) {
984 EXPECT_EQ("1-byte object <00>", Print(it
));
987 // Tests that a class named const_iterator isn't treated as a container.
989 struct const_iterator
{
993 TEST(PrintStlContainerTest
, ConstIterator
) {
994 const_iterator it
= {};
995 EXPECT_EQ("1-byte object <00>", Print(it
));
998 #if GTEST_HAS_TR1_TUPLE
999 // Tests printing ::std::tr1::tuples.
1001 // Tuples of various arities.
1002 TEST(PrintTr1TupleTest
, VariousSizes
) {
1003 ::std::tr1::tuple
<> t0
;
1004 EXPECT_EQ("()", Print(t0
));
1006 ::std::tr1::tuple
<int> t1(5);
1007 EXPECT_EQ("(5)", Print(t1
));
1009 ::std::tr1::tuple
<char, bool> t2('a', true);
1010 EXPECT_EQ("('a' (97, 0x61), true)", Print(t2
));
1012 ::std::tr1::tuple
<bool, int, int> t3(false, 2, 3);
1013 EXPECT_EQ("(false, 2, 3)", Print(t3
));
1015 ::std::tr1::tuple
<bool, int, int, int> t4(false, 2, 3, 4);
1016 EXPECT_EQ("(false, 2, 3, 4)", Print(t4
));
1018 ::std::tr1::tuple
<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
1019 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5
));
1021 ::std::tr1::tuple
<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
1022 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6
));
1024 ::std::tr1::tuple
<bool, int, int, int, bool, int, int> t7(
1025 false, 2, 3, 4, true, 6, 7);
1026 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7
));
1028 ::std::tr1::tuple
<bool, int, int, int, bool, int, int, bool> t8(
1029 false, 2, 3, 4, true, 6, 7, true);
1030 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8
));
1032 ::std::tr1::tuple
<bool, int, int, int, bool, int, int, bool, int> t9(
1033 false, 2, 3, 4, true, 6, 7, true, 9);
1034 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9
));
1036 const char* const str
= "8";
1037 // VC++ 2010's implementation of tuple of C++0x is deficient, requiring
1038 // an explicit type cast of NULL to be used.
1039 ::std::tr1::tuple
<bool, char, short, testing::internal::Int32
, // NOLINT
1040 testing::internal::Int64
, float, double, const char*, void*,
1042 t10(false, 'a', static_cast<short>(3), 4, 5, 1.5F
, -2.5, str
, // NOLINT
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\")",
1050 TEST(PrintTr1TupleTest
, NestedTuple
) {
1051 ::std::tr1::tuple
< ::std::tr1::tuple
<int, bool>, char> nested(
1052 ::std::tr1::make_tuple(5, true), 'a');
1053 EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested
));
1056 #endif // GTEST_HAS_TR1_TUPLE
1058 #if GTEST_HAS_STD_TUPLE_
1059 // Tests printing ::std::tuples.
1061 // Tuples of various arities.
1062 TEST(PrintStdTupleTest
, VariousSizes
) {
1064 EXPECT_EQ("()", Print(t0
));
1066 ::std::tuple
<int> t1(5);
1067 EXPECT_EQ("(5)", Print(t1
));
1069 ::std::tuple
<char, bool> t2('a', true);
1070 EXPECT_EQ("('a' (97, 0x61), true)", Print(t2
));
1072 ::std::tuple
<bool, int, int> t3(false, 2, 3);
1073 EXPECT_EQ("(false, 2, 3)", Print(t3
));
1075 ::std::tuple
<bool, int, int, int> t4(false, 2, 3, 4);
1076 EXPECT_EQ("(false, 2, 3, 4)", Print(t4
));
1078 ::std::tuple
<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
1079 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5
));
1081 ::std::tuple
<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
1082 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6
));
1084 ::std::tuple
<bool, int, int, int, bool, int, int> t7(
1085 false, 2, 3, 4, true, 6, 7);
1086 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7
));
1088 ::std::tuple
<bool, int, int, int, bool, int, int, bool> t8(
1089 false, 2, 3, 4, true, 6, 7, true);
1090 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8
));
1092 ::std::tuple
<bool, int, int, int, bool, int, int, bool, int> t9(
1093 false, 2, 3, 4, true, 6, 7, true, 9);
1094 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9
));
1096 const char* const str
= "8";
1097 // VC++ 2010's implementation of tuple of C++0x is deficient, requiring
1098 // an explicit type cast of NULL to be used.
1099 ::std::tuple
<bool, char, short, testing::internal::Int32
, // NOLINT
1100 testing::internal::Int64
, float, double, const char*, void*,
1102 t10(false, 'a', static_cast<short>(3), 4, 5, 1.5F
, -2.5, str
, // NOLINT
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\")",
1110 TEST(PrintStdTupleTest
, NestedTuple
) {
1111 ::std::tuple
< ::std::tuple
<int, bool>, char> nested(
1112 ::std::make_tuple(5, true), 'a');
1113 EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested
));
1116 #endif // GTEST_LANG_CXX11
1118 #if GTEST_LANG_CXX11
1119 TEST(PrintNullptrT
, Basic
) {
1120 EXPECT_EQ("(nullptr)", Print(nullptr));
1122 #endif // GTEST_LANG_CXX11
1124 // Tests printing user-defined unprintable types.
1126 // Unprintable types in the global namespace.
1127 TEST(PrintUnprintableTypeTest
, InGlobalNamespace
) {
1128 EXPECT_EQ("1-byte object <00>",
1129 Print(UnprintableTemplateInGlobal
<char>()));
1132 // Unprintable types in a user namespace.
1133 TEST(PrintUnprintableTypeTest
, InUserNamespace
) {
1134 EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1135 Print(::foo::UnprintableInFoo()));
1138 // Unprintable types are that too big to be printed completely.
1141 Big() { memset(array
, 0, sizeof(array
)); }
1145 TEST(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>",
1156 // Tests printing user-defined streamable types.
1158 // Streamable types in the global namespace.
1159 TEST(PrintStreamableTypeTest
, InGlobalNamespace
) {
1160 StreamableInGlobal x
;
1161 EXPECT_EQ("StreamableInGlobal", Print(x
));
1162 EXPECT_EQ("StreamableInGlobal*", Print(&x
));
1165 // Printable template types in a user namespace.
1166 TEST(PrintStreamableTypeTest
, TemplateTypeInUserNamespace
) {
1167 EXPECT_EQ("StreamableTemplateInFoo: 0",
1168 Print(::foo::StreamableTemplateInFoo
<int>()));
1171 // Tests printing a user-defined recursive container type that has a <<
1173 TEST(PrintStreamableTypeTest
, PathLikeInUserNamespace
) {
1175 EXPECT_EQ("Streamable-PathLike", Print(x
));
1176 const ::foo::PathLike cx
;
1177 EXPECT_EQ("Streamable-PathLike", Print(cx
));
1180 // Tests printing user-defined types that have a PrintTo() function.
1181 TEST(PrintPrintableTypeTest
, InUserNamespace
) {
1182 EXPECT_EQ("PrintableViaPrintTo: 0",
1183 Print(::foo::PrintableViaPrintTo()));
1186 // Tests printing a pointer to a user-defined type that has a <<
1187 // operator for its pointer.
1188 TEST(PrintPrintableTypeTest
, PointerInUserNamespace
) {
1189 ::foo::PointerPrintable x
;
1190 EXPECT_EQ("PointerPrintable*", Print(&x
));
1193 // Tests printing user-defined class template that have a PrintTo() function.
1194 TEST(PrintPrintableTypeTest
, TemplateInUserNamespace
) {
1195 EXPECT_EQ("PrintableViaPrintToTemplate: 5",
1196 Print(::foo::PrintableViaPrintToTemplate
<int>(5)));
1199 // Tests that the universal printer prints both the address and the
1200 // value of a reference.
1201 TEST(PrintReferenceTest
, PrintsAddressAndValue
) {
1203 EXPECT_EQ("@" + PrintPointer(&n
) + " 5", PrintByRef(n
));
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>",
1218 // Tests that the universal printer prints a function pointer passed by
1220 TEST(PrintReferenceTest
, HandlesFunctionPointer
) {
1221 void (*fp
)(int n
) = &MyFunction
;
1222 const std::string fp_pointer_string
=
1223 PrintPointer(reinterpret_cast<const void*>(&fp
));
1224 // We cannot directly cast &MyFunction to const void* because the
1225 // standard disallows casting between pointers to functions and
1226 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
1228 const std::string fp_string
= PrintPointer(reinterpret_cast<const void*>(
1229 reinterpret_cast<internal::BiggestInt
>(fp
)));
1230 EXPECT_EQ("@" + fp_pointer_string
+ " " + fp_string
,
1234 // Tests that the universal printer prints a member function pointer
1235 // passed by reference.
1236 TEST(PrintReferenceTest
, HandlesMemberFunctionPointer
) {
1237 int (Foo::*p
)(char ch
) = &Foo::MyMethod
;
1238 EXPECT_TRUE(HasPrefix(
1240 "@" + PrintPointer(reinterpret_cast<const void*>(&p
)) + " " +
1241 Print(sizeof(p
)) + "-byte object "));
1243 char (Foo::*p2
)(int n
) = &Foo::MyVirtualMethod
;
1244 EXPECT_TRUE(HasPrefix(
1246 "@" + PrintPointer(reinterpret_cast<const void*>(&p2
)) + " " +
1247 Print(sizeof(p2
)) + "-byte object "));
1250 // Tests that the universal printer prints a member variable pointer
1251 // passed by reference.
1252 TEST(PrintReferenceTest
, HandlesMemberVariablePointer
) {
1253 int (Foo::*p
) = &Foo::value
; // NOLINT
1254 EXPECT_TRUE(HasPrefix(
1256 "@" + PrintPointer(&p
) + " " + Print(sizeof(p
)) + "-byte object "));
1259 // Tests that FormatForComparisonFailureMessage(), which is used to print
1260 // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion
1261 // fails, formats the operand in the desired way.
1264 TEST(FormatForComparisonFailureMessageTest
, WorksForScalar
) {
1266 FormatForComparisonFailureMessage(123, 124).c_str());
1270 TEST(FormatForComparisonFailureMessageTest
, WorksForNonCharPointer
) {
1272 EXPECT_EQ(PrintPointer(&n
),
1273 FormatForComparisonFailureMessage(&n
, &n
).c_str());
1277 TEST(FormatForComparisonFailureMessageTest
, FormatsNonCharArrayAsPointer
) {
1278 // In expression 'array == x', 'array' is compared by pointer.
1279 // Therefore we want to print an array operand as a pointer.
1280 int n
[] = { 1, 2, 3 };
1281 EXPECT_EQ(PrintPointer(n
),
1282 FormatForComparisonFailureMessage(n
, n
).c_str());
1285 // Tests formatting a char pointer when it's compared with another pointer.
1286 // In this case we want to print it as a raw pointer, as the comparison is by
1289 // char pointer vs pointer
1290 TEST(FormatForComparisonFailureMessageTest
, WorksForCharPointerVsPointer
) {
1291 // In expression 'p == x', where 'p' and 'x' are (const or not) char
1292 // pointers, the operands are compared by pointer. Therefore we
1293 // want to print 'p' as a pointer instead of a C string (we don't
1294 // even know if it's supposed to point to a valid C string).
1297 const char* s
= "hello";
1298 EXPECT_EQ(PrintPointer(s
),
1299 FormatForComparisonFailureMessage(s
, s
).c_str());
1303 EXPECT_EQ(PrintPointer(&ch
),
1304 FormatForComparisonFailureMessage(&ch
, &ch
).c_str());
1307 // wchar_t pointer vs pointer
1308 TEST(FormatForComparisonFailureMessageTest
, WorksForWCharPointerVsPointer
) {
1309 // In expression 'p == x', where 'p' and 'x' are (const or not) char
1310 // pointers, the operands are compared by pointer. Therefore we
1311 // want to print 'p' as a pointer instead of a wide C string (we don't
1312 // even know if it's supposed to point to a valid wide C string).
1315 const wchar_t* s
= L
"hello";
1316 EXPECT_EQ(PrintPointer(s
),
1317 FormatForComparisonFailureMessage(s
, s
).c_str());
1321 EXPECT_EQ(PrintPointer(&ch
),
1322 FormatForComparisonFailureMessage(&ch
, &ch
).c_str());
1325 // Tests formatting a char pointer when it's compared to a string object.
1326 // In this case we want to print the char pointer as a C string.
1328 #if GTEST_HAS_GLOBAL_STRING
1329 // char pointer vs ::string
1330 TEST(FormatForComparisonFailureMessageTest
, WorksForCharPointerVsString
) {
1331 const char* s
= "hello \"world";
1332 EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1333 FormatForComparisonFailureMessage(s
, ::string()).c_str());
1336 char str
[] = "hi\1";
1338 EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1339 FormatForComparisonFailureMessage(p
, ::string()).c_str());
1343 // char pointer vs std::string
1344 TEST(FormatForComparisonFailureMessageTest
, WorksForCharPointerVsStdString
) {
1345 const char* s
= "hello \"world";
1346 EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1347 FormatForComparisonFailureMessage(s
, ::std::string()).c_str());
1350 char str
[] = "hi\1";
1352 EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1353 FormatForComparisonFailureMessage(p
, ::std::string()).c_str());
1356 #if GTEST_HAS_GLOBAL_WSTRING
1357 // wchar_t pointer vs ::wstring
1358 TEST(FormatForComparisonFailureMessageTest
, WorksForWCharPointerVsWString
) {
1359 const wchar_t* s
= L
"hi \"world";
1360 EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1361 FormatForComparisonFailureMessage(s
, ::wstring()).c_str());
1364 wchar_t str
[] = L
"hi\1";
1366 EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1367 FormatForComparisonFailureMessage(p
, ::wstring()).c_str());
1371 #if GTEST_HAS_STD_WSTRING
1372 // wchar_t pointer vs std::wstring
1373 TEST(FormatForComparisonFailureMessageTest
, WorksForWCharPointerVsStdWString
) {
1374 const wchar_t* s
= L
"hi \"world";
1375 EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1376 FormatForComparisonFailureMessage(s
, ::std::wstring()).c_str());
1379 wchar_t str
[] = L
"hi\1";
1381 EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1382 FormatForComparisonFailureMessage(p
, ::std::wstring()).c_str());
1386 // Tests formatting a char array when it's compared with a pointer or array.
1387 // In this case we want to print the array as a row pointer, as the comparison
1390 // char array vs pointer
1391 TEST(FormatForComparisonFailureMessageTest
, WorksForCharArrayVsPointer
) {
1392 char str
[] = "hi \"world\"";
1394 EXPECT_EQ(PrintPointer(str
),
1395 FormatForComparisonFailureMessage(str
, p
).c_str());
1398 // char array vs char array
1399 TEST(FormatForComparisonFailureMessageTest
, WorksForCharArrayVsCharArray
) {
1400 const char str
[] = "hi \"world\"";
1401 EXPECT_EQ(PrintPointer(str
),
1402 FormatForComparisonFailureMessage(str
, str
).c_str());
1405 // wchar_t array vs pointer
1406 TEST(FormatForComparisonFailureMessageTest
, WorksForWCharArrayVsPointer
) {
1407 wchar_t str
[] = L
"hi \"world\"";
1409 EXPECT_EQ(PrintPointer(str
),
1410 FormatForComparisonFailureMessage(str
, p
).c_str());
1413 // wchar_t array vs wchar_t array
1414 TEST(FormatForComparisonFailureMessageTest
, WorksForWCharArrayVsWCharArray
) {
1415 const wchar_t str
[] = L
"hi \"world\"";
1416 EXPECT_EQ(PrintPointer(str
),
1417 FormatForComparisonFailureMessage(str
, str
).c_str());
1420 // Tests formatting a char array when it's compared with a string object.
1421 // In this case we want to print the array as a C string.
1423 #if GTEST_HAS_GLOBAL_STRING
1424 // char array vs string
1425 TEST(FormatForComparisonFailureMessageTest
, WorksForCharArrayVsString
) {
1426 const char str
[] = "hi \"w\0rld\"";
1427 EXPECT_STREQ("\"hi \\\"w\"", // The content should be escaped.
1428 // Embedded NUL terminates the string.
1429 FormatForComparisonFailureMessage(str
, ::string()).c_str());
1433 // char array vs std::string
1434 TEST(FormatForComparisonFailureMessageTest
, WorksForCharArrayVsStdString
) {
1435 const char str
[] = "hi \"world\"";
1436 EXPECT_STREQ("\"hi \\\"world\\\"\"", // The content should be escaped.
1437 FormatForComparisonFailureMessage(str
, ::std::string()).c_str());
1440 #if GTEST_HAS_GLOBAL_WSTRING
1441 // wchar_t array vs wstring
1442 TEST(FormatForComparisonFailureMessageTest
, WorksForWCharArrayVsWString
) {
1443 const wchar_t str
[] = L
"hi \"world\"";
1444 EXPECT_STREQ("L\"hi \\\"world\\\"\"", // The content should be escaped.
1445 FormatForComparisonFailureMessage(str
, ::wstring()).c_str());
1449 #if GTEST_HAS_STD_WSTRING
1450 // wchar_t array vs std::wstring
1451 TEST(FormatForComparisonFailureMessageTest
, WorksForWCharArrayVsStdWString
) {
1452 const wchar_t str
[] = L
"hi \"w\0rld\"";
1454 "L\"hi \\\"w\"", // The content should be escaped.
1455 // Embedded NUL terminates the string.
1456 FormatForComparisonFailureMessage(str
, ::std::wstring()).c_str());
1460 // Useful for testing PrintToString(). We cannot use EXPECT_EQ()
1461 // there as its implementation uses PrintToString(). The caller must
1462 // ensure that 'value' has no side effect.
1463 #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
1464 EXPECT_TRUE(PrintToString(value) == (expected_string)) \
1465 << " where " #value " prints as " << (PrintToString(value))
1467 TEST(PrintToStringTest
, WorksForScalar
) {
1468 EXPECT_PRINT_TO_STRING_(123, "123");
1471 TEST(PrintToStringTest
, WorksForPointerToConstChar
) {
1472 const char* p
= "hello";
1473 EXPECT_PRINT_TO_STRING_(p
, "\"hello\"");
1476 TEST(PrintToStringTest
, WorksForPointerToNonConstChar
) {
1479 EXPECT_PRINT_TO_STRING_(p
, "\"hello\"");
1482 TEST(PrintToStringTest
, EscapesForPointerToConstChar
) {
1483 const char* p
= "hello\n";
1484 EXPECT_PRINT_TO_STRING_(p
, "\"hello\\n\"");
1487 TEST(PrintToStringTest
, EscapesForPointerToNonConstChar
) {
1488 char s
[] = "hello\1";
1490 EXPECT_PRINT_TO_STRING_(p
, "\"hello\\x1\"");
1493 TEST(PrintToStringTest
, WorksForArray
) {
1494 int n
[3] = { 1, 2, 3 };
1495 EXPECT_PRINT_TO_STRING_(n
, "{ 1, 2, 3 }");
1498 TEST(PrintToStringTest
, WorksForCharArray
) {
1500 EXPECT_PRINT_TO_STRING_(s
, "\"hello\"");
1503 TEST(PrintToStringTest
, WorksForCharArrayWithEmbeddedNul
) {
1504 const char str_with_nul
[] = "hello\0 world";
1505 EXPECT_PRINT_TO_STRING_(str_with_nul
, "\"hello\\0 world\"");
1507 char mutable_str_with_nul
[] = "hello\0 world";
1508 EXPECT_PRINT_TO_STRING_(mutable_str_with_nul
, "\"hello\\0 world\"");
1511 TEST(PrintToStringTest
, ContainsNonLatin
) {
1512 // Sanity test with valid UTF-8. Prints both in hex and as text.
1513 std::string non_ascii_str
= ::std::string("ì˜¤ì „ 4:30");
1514 EXPECT_PRINT_TO_STRING_(non_ascii_str
,
1515 "\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
1516 " As Text: \"ì˜¤ì „ 4:30\"");
1517 non_ascii_str
= ::std::string("From ä — ẑ");
1518 EXPECT_PRINT_TO_STRING_(non_ascii_str
,
1519 "\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
1520 "\n As Text: \"From ä — ẑ\"");
1523 TEST(IsValidUTF8Test
, IllFormedUTF8
) {
1524 // The following test strings are ill-formed UTF-8 and are printed
1525 // as hex only (or ASCII, in case of ASCII bytes) because IsValidUTF8() is
1526 // expected to fail, thus output does not contain "As Text:".
1528 static const char *const kTestdata
[][2] = {
1529 // 2-byte lead byte followed by a single-byte character.
1530 {"\xC3\x74", "\"\\xC3t\""},
1531 // Valid 2-byte character followed by an orphan trail byte.
1532 {"\xC3\x84\xA4", "\"\\xC3\\x84\\xA4\""},
1533 // Lead byte without trail byte.
1534 {"abc\xC3", "\"abc\\xC3\""},
1535 // 3-byte lead byte, single-byte character, orphan trail byte.
1536 {"x\xE2\x70\x94", "\"x\\xE2p\\x94\""},
1537 // Truncated 3-byte character.
1538 {"\xE2\x80", "\"\\xE2\\x80\""},
1539 // Truncated 3-byte character followed by valid 2-byte char.
1540 {"\xE2\x80\xC3\x84", "\"\\xE2\\x80\\xC3\\x84\""},
1541 // Truncated 3-byte character followed by a single-byte character.
1542 {"\xE2\x80\x7A", "\"\\xE2\\x80z\""},
1543 // 3-byte lead byte followed by valid 3-byte character.
1544 {"\xE2\xE2\x80\x94", "\"\\xE2\\xE2\\x80\\x94\""},
1545 // 4-byte lead byte followed by valid 3-byte character.
1546 {"\xF0\xE2\x80\x94", "\"\\xF0\\xE2\\x80\\x94\""},
1547 // Truncated 4-byte character.
1548 {"\xF0\xE2\x80", "\"\\xF0\\xE2\\x80\""},
1549 // Invalid UTF-8 byte sequences embedded in other chars.
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\""},
1553 // Non-shortest UTF-8 byte sequences are also ill-formed.
1554 // The classics: xC0, xC1 lead byte.
1555 {"\xC0\x80", "\"\\xC0\\x80\""},
1556 {"\xC1\x81", "\"\\xC1\\x81\""},
1557 // Non-shortest sequences.
1558 {"\xE0\x80\x80", "\"\\xE0\\x80\\x80\""},
1559 {"\xf0\x80\x80\x80", "\"\\xF0\\x80\\x80\\x80\""},
1560 // Last valid code point before surrogate range, should be printed as text,
1562 {"\xED\x9F\xBF", "\"\\xED\\x9F\\xBF\"\n As Text: \"퟿\""},
1563 // Start of surrogate lead. Surrogates are not printed as text.
1564 {"\xED\xA0\x80", "\"\\xED\\xA0\\x80\""},
1565 // Last non-private surrogate lead.
1566 {"\xED\xAD\xBF", "\"\\xED\\xAD\\xBF\""},
1567 // First private-use surrogate lead.
1568 {"\xED\xAE\x80", "\"\\xED\\xAE\\x80\""},
1569 // Last private-use surrogate lead.
1570 {"\xED\xAF\xBF", "\"\\xED\\xAF\\xBF\""},
1571 // Mid-point of surrogate trail.
1572 {"\xED\xB3\xBF", "\"\\xED\\xB3\\xBF\""},
1573 // First valid code point after surrogate range, should be printed as text,
1575 {"\xEE\x80\x80", "\"\\xEE\\x80\\x80\"\n As Text: \"\""}
1578 for (int i
= 0; i
< int(sizeof(kTestdata
)/sizeof(kTestdata
[0])); ++i
) {
1579 EXPECT_PRINT_TO_STRING_(kTestdata
[i
][0], kTestdata
[i
][1]);
1583 #undef EXPECT_PRINT_TO_STRING_
1585 TEST(UniversalTersePrintTest
, WorksForNonReference
) {
1586 ::std::stringstream ss
;
1587 UniversalTersePrint(123, &ss
);
1588 EXPECT_EQ("123", ss
.str());
1591 TEST(UniversalTersePrintTest
, WorksForReference
) {
1593 ::std::stringstream ss
;
1594 UniversalTersePrint(n
, &ss
);
1595 EXPECT_EQ("123", ss
.str());
1598 TEST(UniversalTersePrintTest
, WorksForCString
) {
1599 const char* s1
= "abc";
1600 ::std::stringstream ss1
;
1601 UniversalTersePrint(s1
, &ss1
);
1602 EXPECT_EQ("\"abc\"", ss1
.str());
1604 char* s2
= const_cast<char*>(s1
);
1605 ::std::stringstream ss2
;
1606 UniversalTersePrint(s2
, &ss2
);
1607 EXPECT_EQ("\"abc\"", ss2
.str());
1609 const char* s3
= NULL
;
1610 ::std::stringstream ss3
;
1611 UniversalTersePrint(s3
, &ss3
);
1612 EXPECT_EQ("NULL", ss3
.str());
1615 TEST(UniversalPrintTest
, WorksForNonReference
) {
1616 ::std::stringstream ss
;
1617 UniversalPrint(123, &ss
);
1618 EXPECT_EQ("123", ss
.str());
1621 TEST(UniversalPrintTest
, WorksForReference
) {
1623 ::std::stringstream ss
;
1624 UniversalPrint(n
, &ss
);
1625 EXPECT_EQ("123", ss
.str());
1628 TEST(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
);
1642 EXPECT_EQ("NULL", ss3
.str());
1645 TEST(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
1659 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1
, PrintsEmptyTuple
) {
1660 Strings result
= UniversalTersePrintTupleFieldsToStrings(
1661 ::std::tr1::make_tuple());
1662 EXPECT_EQ(0u, result
.size());
1665 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1
, PrintsOneTuple
) {
1666 Strings result
= UniversalTersePrintTupleFieldsToStrings(
1667 ::std::tr1::make_tuple(1));
1668 ASSERT_EQ(1u, result
.size());
1669 EXPECT_EQ("1", result
[0]);
1672 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1
, PrintsTwoTuple
) {
1673 Strings result
= UniversalTersePrintTupleFieldsToStrings(
1674 ::std::tr1::make_tuple(1, 'a'));
1675 ASSERT_EQ(2u, result
.size());
1676 EXPECT_EQ("1", result
[0]);
1677 EXPECT_EQ("'a' (97, 0x61)", result
[1]);
1680 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1
, PrintsTersely
) {
1682 Strings result
= UniversalTersePrintTupleFieldsToStrings(
1683 ::std::tr1::tuple
<const int&, const char*>(n
, "a"));
1684 ASSERT_EQ(2u, result
.size());
1685 EXPECT_EQ("1", result
[0]);
1686 EXPECT_EQ("\"a\"", result
[1]);
1689 #endif // GTEST_HAS_TR1_TUPLE
1691 #if GTEST_HAS_STD_TUPLE_
1693 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd
, PrintsEmptyTuple
) {
1694 Strings result
= UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
1695 EXPECT_EQ(0u, result
.size());
1698 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd
, PrintsOneTuple
) {
1699 Strings result
= UniversalTersePrintTupleFieldsToStrings(
1700 ::std::make_tuple(1));
1701 ASSERT_EQ(1u, result
.size());
1702 EXPECT_EQ("1", result
[0]);
1705 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd
, PrintsTwoTuple
) {
1706 Strings result
= UniversalTersePrintTupleFieldsToStrings(
1707 ::std::make_tuple(1, 'a'));
1708 ASSERT_EQ(2u, result
.size());
1709 EXPECT_EQ("1", result
[0]);
1710 EXPECT_EQ("'a' (97, 0x61)", result
[1]);
1713 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd
, PrintsTersely
) {
1715 Strings result
= UniversalTersePrintTupleFieldsToStrings(
1716 ::std::tuple
<const int&, const char*>(n
, "a"));
1717 ASSERT_EQ(2u, result
.size());
1718 EXPECT_EQ("1", result
[0]);
1719 EXPECT_EQ("\"a\"", result
[1]);
1722 #endif // GTEST_HAS_STD_TUPLE_
1726 TEST(PrintOptionalTest
, Basic
) {
1727 absl::optional
<int> value
;
1728 EXPECT_EQ("(nullopt)", PrintToString(value
));
1730 EXPECT_EQ("(7)", PrintToString(value
));
1731 EXPECT_EQ("(1.1)", PrintToString(absl::optional
<double>{1.1}));
1732 EXPECT_EQ("(\"A\")", PrintToString(absl::optional
<std::string
>{"A"}));
1734 #endif // GTEST_HAS_ABSL
1736 } // namespace gtest_printers_test
1737 } // namespace testing