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 Framework
34 // This file tests the universal value printer.
36 #include "gtest/gtest-printers.h"
51 #include "gtest/gtest.h"
53 // hash_map and hash_set are available under Visual C++, or on Linux.
54 #if GTEST_HAS_HASH_MAP_
55 # include <hash_map> // NOLINT
56 #endif // GTEST_HAS_HASH_MAP_
57 #if GTEST_HAS_HASH_SET_
58 # include <hash_set> // NOLINT
59 #endif // GTEST_HAS_HASH_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.
197 typedef PathLike value_type;
199 typedef iterator const_iterator;
203 iterator begin() const { return iterator(); }
204 iterator end() const { return iterator(); }
207 ::std::ostream& operator<<(::std::ostream& os, const PathLike&)
209 return os << "Streamable-PathLike";
216 namespace gtest_printers_test {
220 using ::std::make_pair;
222 using ::std::multimap;
223 using ::std::multiset;
227 using ::testing::PrintToString;
228 using ::testing::internal::FormatForComparisonFailureMessage;
229 using ::testing::internal::ImplicitCast_;
230 using ::testing::internal::NativeArray;
231 using ::testing::internal::RE;
232 using ::testing::internal::RelationToSourceReference;
233 using ::testing::internal::Strings;
234 using ::testing::internal::UniversalPrint;
235 using ::testing::internal::UniversalPrinter;
236 using ::testing::internal::UniversalTersePrint;
237 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
238 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
240 using ::testing::internal::string;
242 #if GTEST_HAS_HASH_MAP_
243 // The hash_* classes are not part of the C++ standard. STLport
244 // defines them in namespace std. MSVC defines them in ::stdext. GCC
245 // defines them in ::.
246 #ifdef _STLP_HASH_MAP // We got <hash_map> from STLport.
247 using ::std::hash_map;
248 using ::std::hash_set;
249 using ::std::hash_multimap;
250 using ::std::hash_multiset;
252 using ::stdext::hash_map;
253 using ::stdext::hash_set;
254 using ::stdext::hash_multimap;
255 using ::stdext::hash_multiset;
259 // Prints a value to a string using the universal value printer. This
260 // is a helper for testing UniversalPrinter<T>::Print() for various types.
261 template <typename T>
262 std::string Print(const T& value) {
263 ::std::stringstream ss;
264 UniversalPrinter<T>::Print(value, &ss);
268 // Prints a value passed by reference to a string, using the universal
269 // value printer. This is a helper for testing
270 // UniversalPrinter<T&>::Print() for various types.
271 template <typename T>
272 std::string PrintByRef(const T& value) {
273 ::std::stringstream ss;
274 UniversalPrinter<T&>::Print(value, &ss);
278 // Tests printing various enum types.
280 TEST(PrintEnumTest, AnonymousEnum) {
281 EXPECT_EQ("-1", Print(kAE1));
282 EXPECT_EQ("1", Print(kAE2));
285 TEST(PrintEnumTest, EnumWithoutPrinter) {
286 EXPECT_EQ("-2", Print(kEWP1));
287 EXPECT_EQ("42", Print(kEWP2));
290 TEST(PrintEnumTest, EnumWithStreaming) {
291 EXPECT_EQ("kEWS1", Print(kEWS1));
292 EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
295 TEST(PrintEnumTest, EnumWithPrintTo) {
296 EXPECT_EQ("kEWPT1", Print(kEWPT1));
297 EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
300 // Tests printing a class implicitly convertible to BiggestInt.
302 TEST(PrintClassTest, BiggestIntConvertible) {
303 EXPECT_EQ("42", Print(BiggestIntConvertible()));
306 // Tests printing various char types.
309 TEST(PrintCharTest, PlainChar) {
310 EXPECT_EQ("'\\0'", Print('\0'));
311 EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
312 EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
313 EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
314 EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
315 EXPECT_EQ("'\\a' (7)", Print('\a'));
316 EXPECT_EQ("'\\b' (8)", Print('\b'));
317 EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
318 EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
319 EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
320 EXPECT_EQ("'\\t' (9)", Print('\t'));
321 EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
322 EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
323 EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
324 EXPECT_EQ("' ' (32, 0x20)", Print(' '));
325 EXPECT_EQ("'a' (97, 0x61)", Print('a'));
329 TEST(PrintCharTest, SignedChar) {
330 EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
331 EXPECT_EQ("'\\xCE' (-50)",
332 Print(static_cast<signed char>(-50)));
336 TEST(PrintCharTest, UnsignedChar) {
337 EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
338 EXPECT_EQ("'b' (98, 0x62)",
339 Print(static_cast<unsigned char>('b')));
342 // Tests printing other simple, built-in types.
345 TEST(PrintBuiltInTypeTest, Bool) {
346 EXPECT_EQ("false", Print(false));
347 EXPECT_EQ("true", Print(true));
351 TEST(PrintBuiltInTypeTest, Wchar_t) {
352 EXPECT_EQ("L'\\0'", Print(L'\0'));
353 EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
354 EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
355 EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
356 EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
357 EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
358 EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
359 EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
360 EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
361 EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
362 EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
363 EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
364 EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
365 EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
366 EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
367 EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
368 EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
369 EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
372 // Test that Int64 provides more storage than wchar_t.
373 TEST(PrintTypeSizeTest, Wchar_t) {
374 EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
377 // Various integer types.
378 TEST(PrintBuiltInTypeTest, Integer) {
379 EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8
380 EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8
381 EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16
382 EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16
383 EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32
384 EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32
385 EXPECT_EQ("18446744073709551615",
386 Print(static_cast<testing::internal::UInt64>(-1))); // uint64
387 EXPECT_EQ("-9223372036854775808",
388 Print(static_cast<testing::internal::Int64>(1) << 63)); // int64
392 TEST(PrintBuiltInTypeTest, Size_t) {
393 EXPECT_EQ("1", Print(sizeof('a'))); // size_t.
394 #if !GTEST_OS_WINDOWS
395 // Windows has no ssize_t type.
396 EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t.
397 #endif // !GTEST_OS_WINDOWS
401 TEST(PrintBuiltInTypeTest, FloatingPoints) {
402 EXPECT_EQ("1.5", Print(1.5f)); // float
403 EXPECT_EQ("-2.5", Print(-2.5)); // double
406 // Since ::std::stringstream::operator<<(const void *) formats the pointer
407 // output differently with different compilers, we have to create the expected
408 // output first and use it as our expectation.
409 static std::string PrintPointer(const void* p) {
410 ::std::stringstream expected_result_stream;
411 expected_result_stream << p;
412 return expected_result_stream.str();
415 // Tests printing C strings.
418 TEST(PrintCStringTest, Const) {
419 const char* p = "World";
420 EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
424 TEST(PrintCStringTest, NonConst) {
426 EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
427 Print(static_cast<char*>(p)));
431 TEST(PrintCStringTest, Null) {
432 const char* p = NULL;
433 EXPECT_EQ("NULL", Print(p));
436 // Tests that C strings are escaped properly.
437 TEST(PrintCStringTest, EscapesProperly) {
438 const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
439 EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
440 "\\n\\r\\t\\v\\x7F\\xFF a\"",
444 // MSVC compiler can be configured to define whar_t as a typedef
445 // of unsigned short. Defining an overload for const wchar_t* in that case
446 // would cause pointers to unsigned shorts be printed as wide strings,
447 // possibly accessing more memory than intended and causing invalid
448 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
449 // wchar_t is implemented as a native type.
450 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
453 TEST(PrintWideCStringTest, Const) {
454 const wchar_t* p = L"World";
455 EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
459 TEST(PrintWideCStringTest, NonConst) {
461 EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
462 Print(static_cast<wchar_t*>(p)));
465 // NULL wide C string.
466 TEST(PrintWideCStringTest, Null) {
467 const wchar_t* p = NULL;
468 EXPECT_EQ("NULL", Print(p));
471 // Tests that wide C strings are escaped properly.
472 TEST(PrintWideCStringTest, EscapesProperly) {
473 const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r',
474 '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
475 EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f"
476 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
477 Print(static_cast<const wchar_t*>(s)));
479 #endif // native wchar_t
481 // Tests printing pointers to other char types.
484 TEST(PrintCharPointerTest, SignedChar) {
485 signed char* p = reinterpret_cast<signed char*>(0x1234);
486 EXPECT_EQ(PrintPointer(p), Print(p));
488 EXPECT_EQ("NULL", Print(p));
491 // const signed char*.
492 TEST(PrintCharPointerTest, ConstSignedChar) {
493 signed char* p = reinterpret_cast<signed char*>(0x1234);
494 EXPECT_EQ(PrintPointer(p), Print(p));
496 EXPECT_EQ("NULL", Print(p));
500 TEST(PrintCharPointerTest, UnsignedChar) {
501 unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
502 EXPECT_EQ(PrintPointer(p), Print(p));
504 EXPECT_EQ("NULL", Print(p));
507 // const unsigned char*.
508 TEST(PrintCharPointerTest, ConstUnsignedChar) {
509 const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
510 EXPECT_EQ(PrintPointer(p), Print(p));
512 EXPECT_EQ("NULL", Print(p));
515 // Tests printing pointers to simple, built-in types.
518 TEST(PrintPointerToBuiltInTypeTest, Bool) {
519 bool* p = reinterpret_cast<bool*>(0xABCD);
520 EXPECT_EQ(PrintPointer(p), Print(p));
522 EXPECT_EQ("NULL", Print(p));
526 TEST(PrintPointerToBuiltInTypeTest, Void) {
527 void* p = reinterpret_cast<void*>(0xABCD);
528 EXPECT_EQ(PrintPointer(p), Print(p));
530 EXPECT_EQ("NULL", Print(p));
534 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
535 const void* p = reinterpret_cast<const void*>(0xABCD);
536 EXPECT_EQ(PrintPointer(p), Print(p));
538 EXPECT_EQ("NULL", Print(p));
541 // Tests printing pointers to pointers.
542 TEST(PrintPointerToPointerTest, IntPointerPointer) {
543 int** p = reinterpret_cast<int**>(0xABCD);
544 EXPECT_EQ(PrintPointer(p), Print(p));
546 EXPECT_EQ("NULL", Print(p));
549 // Tests printing (non-member) function pointers.
551 void MyFunction(int /* n */) {}
553 TEST(PrintPointerTest, NonMemberFunctionPointer) {
554 // We cannot directly cast &MyFunction to const void* because the
555 // standard disallows casting between pointers to functions and
556 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
559 PrintPointer(reinterpret_cast<const void*>(
560 reinterpret_cast<internal::BiggestInt>(&MyFunction))),
562 int (*p)(bool) = NULL; // NOLINT
563 EXPECT_EQ("NULL", Print(p));
566 // An assertion predicate determining whether a one string is a prefix for
568 template <typename StringType>
569 AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
570 if (str.find(prefix, 0) == 0)
571 return AssertionSuccess();
573 const bool is_wide_string = sizeof(prefix[0]) > 1;
574 const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
575 return AssertionFailure()
576 << begin_string_quote << prefix << "\" is not a prefix of "
577 << begin_string_quote << str << "\"\n";
580 // Tests printing member variable 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.
588 int MyMethod(char x) { return x + 1; }
589 virtual char MyVirtualMethod(int /* n */) { return 'a'; }
594 TEST(PrintPointerTest, MemberVariablePointer) {
595 EXPECT_TRUE(HasPrefix(Print(&Foo::value),
596 Print(sizeof(&Foo::value)) + "-byte object "));
597 int (Foo::*p) = NULL; // NOLINT
598 EXPECT_TRUE(HasPrefix(Print(p),
599 Print(sizeof(p)) + "-byte object "));
602 // Tests printing member function pointers. Although they are called
603 // pointers, they don't point to a location in the address space.
604 // Their representation is implementation-defined. Thus they will be
605 // printed as raw bytes.
606 TEST(PrintPointerTest, MemberFunctionPointer) {
607 EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod),
608 Print(sizeof(&Foo::MyMethod)) + "-byte object "));
610 HasPrefix(Print(&Foo::MyVirtualMethod),
611 Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
612 int (Foo::*p)(char) = NULL; // NOLINT
613 EXPECT_TRUE(HasPrefix(Print(p),
614 Print(sizeof(p)) + "-byte object "));
617 // Tests printing C arrays.
619 // The difference between this and Print() is that it ensures that the
620 // argument is a reference to an array.
621 template <typename T, size_t N>
622 std::string PrintArrayHelper(T (&a)[N]) {
626 // One-dimensional array.
627 TEST(PrintArrayTest, OneDimensionalArray) {
628 int a[5] = { 1, 2, 3, 4, 5 };
629 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
632 // Two-dimensional array.
633 TEST(PrintArrayTest, TwoDimensionalArray) {
638 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
641 // Array of const elements.
642 TEST(PrintArrayTest, ConstArray) {
643 const bool a[1] = { false };
644 EXPECT_EQ("{ false }", PrintArrayHelper(a));
647 // char array without terminating NUL.
648 TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
649 // Array a contains '\0' in the middle and doesn't end with '\0'.
650 char a[] = { 'H', '\0', 'i' };
651 EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
654 // const char array with terminating NUL.
655 TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) {
656 const char a[] = "\0Hi";
657 EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a));
660 // const wchar_t array without terminating NUL.
661 TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
662 // Array a contains '\0' in the middle and doesn't end with '\0'.
663 const wchar_t a[] = { L'H', L'\0', L'i' };
664 EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
667 // wchar_t array with terminating NUL.
668 TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
669 const wchar_t a[] = L"\0Hi";
670 EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a));
674 TEST(PrintArrayTest, ObjectArray) {
675 std::string a[3] = {"Hi", "Hello", "Ni hao"};
676 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
679 // Array with many elements.
680 TEST(PrintArrayTest, BigArray) {
681 int a[100] = { 1, 2, 3 };
682 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
683 PrintArrayHelper(a));
686 // Tests printing ::string and ::std::string.
688 #if GTEST_HAS_GLOBAL_STRING
690 TEST(PrintStringTest, StringInGlobalNamespace) {
691 const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
692 const ::string str(s, sizeof(s));
693 EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
696 #endif // GTEST_HAS_GLOBAL_STRING
699 TEST(PrintStringTest, StringInStdNamespace) {
700 const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
701 const ::std::string str(s, sizeof(s));
702 EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
706 TEST(PrintStringTest, StringAmbiguousHex) {
707 // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
708 // '\x6', '\x6B', or '\x6BA'.
710 // a hex escaping sequence following by a decimal digit
711 EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
712 // a hex escaping sequence following by a hex digit (lower-case)
713 EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
714 // a hex escaping sequence following by a hex digit (upper-case)
715 EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
716 // a hex escaping sequence following by a non-xdigit
717 EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
720 // Tests printing ::wstring and ::std::wstring.
722 #if GTEST_HAS_GLOBAL_WSTRING
724 TEST(PrintWideStringTest, StringInGlobalNamespace) {
725 const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
726 const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
727 EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
728 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
731 #endif // GTEST_HAS_GLOBAL_WSTRING
733 #if GTEST_HAS_STD_WSTRING
735 TEST(PrintWideStringTest, StringInStdNamespace) {
736 const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
737 const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
738 EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
739 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
743 TEST(PrintWideStringTest, StringAmbiguousHex) {
744 // same for wide strings.
745 EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3")));
746 EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
747 Print(::std::wstring(L"mm\x6" L"bananas")));
748 EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
749 Print(::std::wstring(L"NOM\x6" L"BANANA")));
750 EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
752 #endif // GTEST_HAS_STD_WSTRING
754 // Tests printing types that support generic streaming (i.e. streaming
755 // to std::basic_ostream<Char, CharTraits> for any valid Char and
756 // CharTraits types).
758 // Tests printing a non-template type that supports generic streaming.
760 class AllowsGenericStreaming {};
762 template <typename Char, typename CharTraits>
763 std::basic_ostream<Char, CharTraits>& operator<<(
764 std::basic_ostream<Char, CharTraits>& os,
765 const AllowsGenericStreaming& /* a */) {
766 return os << "AllowsGenericStreaming";
769 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
770 AllowsGenericStreaming a;
771 EXPECT_EQ("AllowsGenericStreaming", Print(a));
774 // Tests printing a template type that supports generic streaming.
776 template <typename T>
777 class AllowsGenericStreamingTemplate {};
779 template <typename Char, typename CharTraits, typename T>
780 std::basic_ostream<Char, CharTraits>& operator<<(
781 std::basic_ostream<Char, CharTraits>& os,
782 const AllowsGenericStreamingTemplate<T>& /* a */) {
783 return os << "AllowsGenericStreamingTemplate";
786 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
787 AllowsGenericStreamingTemplate<int> a;
788 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
791 // Tests printing a type that supports generic streaming and can be
792 // implicitly converted to another printable type.
794 template <typename T>
795 class AllowsGenericStreamingAndImplicitConversionTemplate {
797 operator bool() const { return false; }
800 template <typename Char, typename CharTraits, typename T>
801 std::basic_ostream<Char, CharTraits>& operator<<(
802 std::basic_ostream<Char, CharTraits>& os,
803 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) {
804 return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
807 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
808 AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
809 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
812 #if GTEST_HAS_STRING_PIECE_
814 // Tests printing StringPiece.
816 TEST(PrintStringPieceTest, SimpleStringPiece) {
817 const StringPiece sp = "Hello";
818 EXPECT_EQ("\"Hello\"", Print(sp));
821 TEST(PrintStringPieceTest, UnprintableCharacters) {
822 const char str[] = "NUL (\0) and \r\t";
823 const StringPiece sp(str, sizeof(str) - 1);
824 EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
827 #endif // GTEST_HAS_STRING_PIECE_
829 // Tests printing STL containers.
831 TEST(PrintStlContainerTest, EmptyDeque) {
833 EXPECT_EQ("{}", Print(empty));
836 TEST(PrintStlContainerTest, NonEmptyDeque) {
837 deque<int> non_empty;
838 non_empty.push_back(1);
839 non_empty.push_back(3);
840 EXPECT_EQ("{ 1, 3 }", Print(non_empty));
843 #if GTEST_HAS_HASH_MAP_
845 TEST(PrintStlContainerTest, OneElementHashMap) {
846 hash_map<int, char> map1;
848 EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1));
851 TEST(PrintStlContainerTest, HashMultiMap) {
852 hash_multimap<int, bool> map1;
853 map1.insert(make_pair(5, true));
854 map1.insert(make_pair(5, false));
856 // Elements of hash_multimap can be printed in any order.
857 const std::string result = Print(map1);
858 EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
859 result == "{ (5, false), (5, true) }")
860 << " where Print(map1) returns \"" << result << "\".";
863 #endif // GTEST_HAS_HASH_MAP_
865 #if GTEST_HAS_HASH_SET_
867 TEST(PrintStlContainerTest, HashSet) {
870 EXPECT_EQ("{ 1 }", Print(set1));
873 TEST(PrintStlContainerTest, HashMultiSet) {
875 int a[kSize] = { 1, 1, 2, 5, 1 };
876 hash_multiset<int> set1(a, a + kSize);
878 // Elements of hash_multiset can be printed in any order.
879 const std::string result = Print(set1);
880 const std::string expected_pattern = "{ d, d, d, d, d }"; // d means a digit.
882 // Verifies the result matches the expected pattern; also extracts
883 // the numbers in the result.
884 ASSERT_EQ(expected_pattern.length(), result.length());
885 std::vector<int> numbers;
886 for (size_t i = 0; i != result.length(); i++) {
887 if (expected_pattern[i] == 'd') {
888 ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0);
889 numbers.push_back(result[i] - '0');
891 EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
896 // Makes sure the result contains the right numbers.
897 std::sort(numbers.begin(), numbers.end());
898 std::sort(a, a + kSize);
899 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
902 #endif // GTEST_HAS_HASH_SET_
904 TEST(PrintStlContainerTest, List) {
905 const std::string a[] = {"hello", "world"};
906 const list<std::string> strings(a, a + 2);
907 EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
910 TEST(PrintStlContainerTest, Map) {
915 EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
918 TEST(PrintStlContainerTest, MultiMap) {
919 multimap<bool, int> map1;
920 // The make_pair template function would deduce the type as
921 // pair<bool, int> here, and since the key part in a multimap has to
922 // be constant, without a templated ctor in the pair class (as in
923 // libCstd on Solaris), make_pair call would fail to compile as no
924 // implicit conversion is found. Thus explicit typename is used
926 map1.insert(pair<const bool, int>(true, 0));
927 map1.insert(pair<const bool, int>(true, 1));
928 map1.insert(pair<const bool, int>(false, 2));
929 EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
932 TEST(PrintStlContainerTest, Set) {
933 const unsigned int a[] = { 3, 0, 5 };
934 set<unsigned int> set1(a, a + 3);
935 EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
938 TEST(PrintStlContainerTest, MultiSet) {
939 const int a[] = { 1, 1, 2, 5, 1 };
940 multiset<int> set1(a, a + 5);
941 EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
944 #if GTEST_HAS_STD_FORWARD_LIST_
945 // <slist> is available on Linux in the google3 mode, but not on
946 // Windows or Mac OS X.
948 TEST(PrintStlContainerTest, SinglyLinkedList) {
949 int a[] = { 9, 2, 8 };
950 const std::forward_list<int> ints(a, a + 3);
951 EXPECT_EQ("{ 9, 2, 8 }", Print(ints));
953 #endif // GTEST_HAS_STD_FORWARD_LIST_
955 TEST(PrintStlContainerTest, Pair) {
956 pair<const bool, int> p(true, 5);
957 EXPECT_EQ("(true, 5)", Print(p));
960 TEST(PrintStlContainerTest, Vector) {
964 EXPECT_EQ("{ 1, 2 }", Print(v));
967 TEST(PrintStlContainerTest, LongSequence) {
968 const int a[100] = { 1, 2, 3 };
969 const vector<int> v(a, a + 100);
970 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
971 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
974 TEST(PrintStlContainerTest, NestedContainer) {
975 const int a1[] = { 1, 2 };
976 const int a2[] = { 3, 4, 5 };
977 const list<int> l1(a1, a1 + 2);
978 const list<int> l2(a2, a2 + 3);
980 vector<list<int> > v;
983 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
986 TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
987 const int a[3] = { 1, 2, 3 };
988 NativeArray<int> b(a, 3, RelationToSourceReference());
989 EXPECT_EQ("{ 1, 2, 3 }", Print(b));
992 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
993 const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
994 NativeArray<int[3]> b(a, 2, RelationToSourceReference());
995 EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
998 // Tests that a class named iterator isn't treated as a container.
1004 TEST(PrintStlContainerTest, Iterator) {
1006 EXPECT_EQ("1-byte object <00>", Print(it));
1009 // Tests that a class named const_iterator isn't treated as a container.
1011 struct const_iterator {
1015 TEST(PrintStlContainerTest, ConstIterator) {
1016 const_iterator it = {};
1017 EXPECT_EQ("1-byte object <00>", Print(it));
1020 #if GTEST_HAS_TR1_TUPLE
1021 // Tests printing ::std::tr1::tuples.
1023 // Tuples of various arities.
1024 TEST(PrintTr1TupleTest, VariousSizes) {
1025 ::std::tr1::tuple<> t0;
1026 EXPECT_EQ("()", Print(t0));
1028 ::std::tr1::tuple<int> t1(5);
1029 EXPECT_EQ("(5)", Print(t1));
1031 ::std::tr1::tuple<char, bool> t2('a', true);
1032 EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
1034 ::std::tr1::tuple<bool, int, int> t3(false, 2, 3);
1035 EXPECT_EQ("(false, 2, 3)", Print(t3));
1037 ::std::tr1::tuple<bool, int, int, int> t4(false, 2, 3, 4);
1038 EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
1040 ::std::tr1::tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
1041 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
1043 ::std::tr1::tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
1044 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
1046 ::std::tr1::tuple<bool, int, int, int, bool, int, int> t7(
1047 false, 2, 3, 4, true, 6, 7);
1048 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
1050 ::std::tr1::tuple<bool, int, int, int, bool, int, int, bool> t8(
1051 false, 2, 3, 4, true, 6, 7, true);
1052 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
1054 ::std::tr1::tuple<bool, int, int, int, bool, int, int, bool, int> t9(
1055 false, 2, 3, 4, true, 6, 7, true, 9);
1056 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
1058 const char* const str = "8";
1059 // VC++ 2010's implementation of tuple of C++0x is deficient, requiring
1060 // an explicit type cast of NULL to be used.
1061 ::std::tr1::tuple<bool, char, short, testing::internal::Int32, // NOLINT
1062 testing::internal::Int64, float, double, const char*, void*,
1064 t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, ImplicitCast_<void*>(NULL),
1066 EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1067 " pointing to \"8\", NULL, \"10\")",
1072 TEST(PrintTr1TupleTest, NestedTuple) {
1073 ::std::tr1::tuple< ::std::tr1::tuple<int, bool>, char> nested(
1074 ::std::tr1::make_tuple(5, true), 'a');
1075 EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
1078 #endif // GTEST_HAS_TR1_TUPLE
1080 #if GTEST_HAS_STD_TUPLE_
1081 // Tests printing ::std::tuples.
1083 // Tuples of various arities.
1084 TEST(PrintStdTupleTest, VariousSizes) {
1086 EXPECT_EQ("()", Print(t0));
1088 ::std::tuple<int> t1(5);
1089 EXPECT_EQ("(5)", Print(t1));
1091 ::std::tuple<char, bool> t2('a', true);
1092 EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
1094 ::std::tuple<bool, int, int> t3(false, 2, 3);
1095 EXPECT_EQ("(false, 2, 3)", Print(t3));
1097 ::std::tuple<bool, int, int, int> t4(false, 2, 3, 4);
1098 EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
1100 ::std::tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
1101 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
1103 ::std::tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
1104 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
1106 ::std::tuple<bool, int, int, int, bool, int, int> t7(
1107 false, 2, 3, 4, true, 6, 7);
1108 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
1110 ::std::tuple<bool, int, int, int, bool, int, int, bool> t8(
1111 false, 2, 3, 4, true, 6, 7, true);
1112 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
1114 ::std::tuple<bool, int, int, int, bool, int, int, bool, int> t9(
1115 false, 2, 3, 4, true, 6, 7, true, 9);
1116 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
1118 const char* const str = "8";
1119 // VC++ 2010's implementation of tuple of C++0x is deficient, requiring
1120 // an explicit type cast of NULL to be used.
1121 ::std::tuple<bool, char, short, testing::internal::Int32, // NOLINT
1122 testing::internal::Int64, float, double, const char*, void*,
1124 t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, ImplicitCast_<void*>(NULL),
1126 EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1127 " pointing to \"8\", NULL, \"10\")",
1132 TEST(PrintStdTupleTest, NestedTuple) {
1133 ::std::tuple< ::std::tuple<int, bool>, char> nested(
1134 ::std::make_tuple(5, true), 'a');
1135 EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
1138 #endif // GTEST_LANG_CXX11
1140 // Tests printing user-defined unprintable types.
1142 // Unprintable types in the global namespace.
1143 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
1144 EXPECT_EQ("1-byte object <00>",
1145 Print(UnprintableTemplateInGlobal<char>()));
1148 // Unprintable types in a user namespace.
1149 TEST(PrintUnprintableTypeTest, InUserNamespace) {
1150 EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1151 Print(::foo::UnprintableInFoo()));
1154 // Unprintable types are that too big to be printed completely.
1157 Big() { memset(array, 0, sizeof(array)); }
1161 TEST(PrintUnpritableTypeTest, BigObject) {
1162 EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
1163 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1164 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1165 "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
1166 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1167 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1168 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
1172 // Tests printing user-defined streamable types.
1174 // Streamable types in the global namespace.
1175 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
1176 StreamableInGlobal x;
1177 EXPECT_EQ("StreamableInGlobal", Print(x));
1178 EXPECT_EQ("StreamableInGlobal*", Print(&x));
1181 // Printable template types in a user namespace.
1182 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
1183 EXPECT_EQ("StreamableTemplateInFoo: 0",
1184 Print(::foo::StreamableTemplateInFoo<int>()));
1187 // Tests printing a user-defined recursive container type that has a <<
1189 TEST(PrintStreamableTypeTest, PathLikeInUserNamespace) {
1191 EXPECT_EQ("Streamable-PathLike", Print(x));
1192 const ::foo::PathLike cx;
1193 EXPECT_EQ("Streamable-PathLike", Print(cx));
1196 // Tests printing user-defined types that have a PrintTo() function.
1197 TEST(PrintPrintableTypeTest, InUserNamespace) {
1198 EXPECT_EQ("PrintableViaPrintTo: 0",
1199 Print(::foo::PrintableViaPrintTo()));
1202 // Tests printing a pointer to a user-defined type that has a <<
1203 // operator for its pointer.
1204 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
1205 ::foo::PointerPrintable x;
1206 EXPECT_EQ("PointerPrintable*", Print(&x));
1209 // Tests printing user-defined class template that have a PrintTo() function.
1210 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
1211 EXPECT_EQ("PrintableViaPrintToTemplate: 5",
1212 Print(::foo::PrintableViaPrintToTemplate<int>(5)));
1215 // Tests that the universal printer prints both the address and the
1216 // value of a reference.
1217 TEST(PrintReferenceTest, PrintsAddressAndValue) {
1219 EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
1225 EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
1228 const ::foo::UnprintableInFoo x;
1229 EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
1230 "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1234 // Tests that the universal printer prints a function pointer passed by
1236 TEST(PrintReferenceTest, HandlesFunctionPointer) {
1237 void (*fp)(int n) = &MyFunction;
1238 const std::string fp_pointer_string =
1239 PrintPointer(reinterpret_cast<const void*>(&fp));
1240 // We cannot directly cast &MyFunction to const void* because the
1241 // standard disallows casting between pointers to functions and
1242 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
1244 const std::string fp_string = PrintPointer(reinterpret_cast<const void*>(
1245 reinterpret_cast<internal::BiggestInt>(fp)));
1246 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
1250 // Tests that the universal printer prints a member function pointer
1251 // passed by reference.
1252 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
1253 int (Foo::*p)(char ch) = &Foo::MyMethod;
1254 EXPECT_TRUE(HasPrefix(
1256 "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
1257 Print(sizeof(p)) + "-byte object "));
1259 char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
1260 EXPECT_TRUE(HasPrefix(
1262 "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
1263 Print(sizeof(p2)) + "-byte object "));
1266 // Tests that the universal printer prints a member variable pointer
1267 // passed by reference.
1268 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
1269 int (Foo::*p) = &Foo::value; // NOLINT
1270 EXPECT_TRUE(HasPrefix(
1272 "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
1275 // Tests that FormatForComparisonFailureMessage(), which is used to print
1276 // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion
1277 // fails, formats the operand in the desired way.
1280 TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
1282 FormatForComparisonFailureMessage(123, 124).c_str());
1286 TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
1288 EXPECT_EQ(PrintPointer(&n),
1289 FormatForComparisonFailureMessage(&n, &n).c_str());
1293 TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
1294 // In expression 'array == x', 'array' is compared by pointer.
1295 // Therefore we want to print an array operand as a pointer.
1296 int n[] = { 1, 2, 3 };
1297 EXPECT_EQ(PrintPointer(n),
1298 FormatForComparisonFailureMessage(n, n).c_str());
1301 // Tests formatting a char pointer when it's compared with another pointer.
1302 // In this case we want to print it as a raw pointer, as the comparision is by
1305 // char pointer vs pointer
1306 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
1307 // In expression 'p == x', where 'p' and 'x' are (const or not) char
1308 // pointers, the operands are compared by pointer. Therefore we
1309 // want to print 'p' as a pointer instead of a C string (we don't
1310 // even know if it's supposed to point to a valid C string).
1313 const char* s = "hello";
1314 EXPECT_EQ(PrintPointer(s),
1315 FormatForComparisonFailureMessage(s, s).c_str());
1319 EXPECT_EQ(PrintPointer(&ch),
1320 FormatForComparisonFailureMessage(&ch, &ch).c_str());
1323 // wchar_t pointer vs pointer
1324 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
1325 // In expression 'p == x', where 'p' and 'x' are (const or not) char
1326 // pointers, the operands are compared by pointer. Therefore we
1327 // want to print 'p' as a pointer instead of a wide C string (we don't
1328 // even know if it's supposed to point to a valid wide C string).
1331 const wchar_t* s = L"hello";
1332 EXPECT_EQ(PrintPointer(s),
1333 FormatForComparisonFailureMessage(s, s).c_str());
1337 EXPECT_EQ(PrintPointer(&ch),
1338 FormatForComparisonFailureMessage(&ch, &ch).c_str());
1341 // Tests formatting a char pointer when it's compared to a string object.
1342 // In this case we want to print the char pointer as a C string.
1344 #if GTEST_HAS_GLOBAL_STRING
1345 // char pointer vs ::string
1346 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) {
1347 const char* s = "hello \"world";
1348 EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1349 FormatForComparisonFailureMessage(s, ::string()).c_str());
1352 char str[] = "hi\1";
1354 EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1355 FormatForComparisonFailureMessage(p, ::string()).c_str());
1359 // char pointer vs std::string
1360 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
1361 const char* s = "hello \"world";
1362 EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1363 FormatForComparisonFailureMessage(s, ::std::string()).c_str());
1366 char str[] = "hi\1";
1368 EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1369 FormatForComparisonFailureMessage(p, ::std::string()).c_str());
1372 #if GTEST_HAS_GLOBAL_WSTRING
1373 // wchar_t pointer vs ::wstring
1374 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) {
1375 const wchar_t* s = L"hi \"world";
1376 EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1377 FormatForComparisonFailureMessage(s, ::wstring()).c_str());
1380 wchar_t str[] = L"hi\1";
1382 EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1383 FormatForComparisonFailureMessage(p, ::wstring()).c_str());
1387 #if GTEST_HAS_STD_WSTRING
1388 // wchar_t pointer vs std::wstring
1389 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
1390 const wchar_t* s = L"hi \"world";
1391 EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1392 FormatForComparisonFailureMessage(s, ::std::wstring()).c_str());
1395 wchar_t str[] = L"hi\1";
1397 EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1398 FormatForComparisonFailureMessage(p, ::std::wstring()).c_str());
1402 // Tests formatting a char array when it's compared with a pointer or array.
1403 // In this case we want to print the array as a row pointer, as the comparison
1406 // char array vs pointer
1407 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
1408 char str[] = "hi \"world\"";
1410 EXPECT_EQ(PrintPointer(str),
1411 FormatForComparisonFailureMessage(str, p).c_str());
1414 // char array vs char array
1415 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
1416 const char str[] = "hi \"world\"";
1417 EXPECT_EQ(PrintPointer(str),
1418 FormatForComparisonFailureMessage(str, str).c_str());
1421 // wchar_t array vs pointer
1422 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
1423 wchar_t str[] = L"hi \"world\"";
1425 EXPECT_EQ(PrintPointer(str),
1426 FormatForComparisonFailureMessage(str, p).c_str());
1429 // wchar_t array vs wchar_t array
1430 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
1431 const wchar_t str[] = L"hi \"world\"";
1432 EXPECT_EQ(PrintPointer(str),
1433 FormatForComparisonFailureMessage(str, str).c_str());
1436 // Tests formatting a char array when it's compared with a string object.
1437 // In this case we want to print the array as a C string.
1439 #if GTEST_HAS_GLOBAL_STRING
1440 // char array vs string
1441 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) {
1442 const char str[] = "hi \"w\0rld\"";
1443 EXPECT_STREQ("\"hi \\\"w\"", // The content should be escaped.
1444 // Embedded NUL terminates the string.
1445 FormatForComparisonFailureMessage(str, ::string()).c_str());
1449 // char array vs std::string
1450 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
1451 const char str[] = "hi \"world\"";
1452 EXPECT_STREQ("\"hi \\\"world\\\"\"", // The content should be escaped.
1453 FormatForComparisonFailureMessage(str, ::std::string()).c_str());
1456 #if GTEST_HAS_GLOBAL_WSTRING
1457 // wchar_t array vs wstring
1458 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) {
1459 const wchar_t str[] = L"hi \"world\"";
1460 EXPECT_STREQ("L\"hi \\\"world\\\"\"", // The content should be escaped.
1461 FormatForComparisonFailureMessage(str, ::wstring()).c_str());
1465 #if GTEST_HAS_STD_WSTRING
1466 // wchar_t array vs std::wstring
1467 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
1468 const wchar_t str[] = L"hi \"w\0rld\"";
1470 "L\"hi \\\"w\"", // The content should be escaped.
1471 // Embedded NUL terminates the string.
1472 FormatForComparisonFailureMessage(str, ::std::wstring()).c_str());
1476 // Useful for testing PrintToString(). We cannot use EXPECT_EQ()
1477 // there as its implementation uses PrintToString(). The caller must
1478 // ensure that 'value' has no side effect.
1479 #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
1480 EXPECT_TRUE(PrintToString(value) == (expected_string)) \
1481 << " where " #value " prints as " << (PrintToString(value))
1483 TEST(PrintToStringTest, WorksForScalar) {
1484 EXPECT_PRINT_TO_STRING_(123, "123");
1487 TEST(PrintToStringTest, WorksForPointerToConstChar) {
1488 const char* p = "hello";
1489 EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
1492 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
1495 EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
1498 TEST(PrintToStringTest, EscapesForPointerToConstChar) {
1499 const char* p = "hello\n";
1500 EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\"");
1503 TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
1504 char s[] = "hello\1";
1506 EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\"");
1509 TEST(PrintToStringTest, WorksForArray) {
1510 int n[3] = { 1, 2, 3 };
1511 EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
1514 TEST(PrintToStringTest, WorksForCharArray) {
1516 EXPECT_PRINT_TO_STRING_(s, "\"hello\"");
1519 TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
1520 const char str_with_nul[] = "hello\0 world";
1521 EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\"");
1523 char mutable_str_with_nul[] = "hello\0 world";
1524 EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\"");
1527 #undef EXPECT_PRINT_TO_STRING_
1529 TEST(UniversalTersePrintTest, WorksForNonReference) {
1530 ::std::stringstream ss;
1531 UniversalTersePrint(123, &ss);
1532 EXPECT_EQ("123", ss.str());
1535 TEST(UniversalTersePrintTest, WorksForReference) {
1537 ::std::stringstream ss;
1538 UniversalTersePrint(n, &ss);
1539 EXPECT_EQ("123", ss.str());
1542 TEST(UniversalTersePrintTest, WorksForCString) {
1543 const char* s1 = "abc";
1544 ::std::stringstream ss1;
1545 UniversalTersePrint(s1, &ss1);
1546 EXPECT_EQ("\"abc\"", ss1.str());
1548 char* s2 = const_cast<char*>(s1);
1549 ::std::stringstream ss2;
1550 UniversalTersePrint(s2, &ss2);
1551 EXPECT_EQ("\"abc\"", ss2.str());
1553 const char* s3 = NULL;
1554 ::std::stringstream ss3;
1555 UniversalTersePrint(s3, &ss3);
1556 EXPECT_EQ("NULL", ss3.str());
1559 TEST(UniversalPrintTest, WorksForNonReference) {
1560 ::std::stringstream ss;
1561 UniversalPrint(123, &ss);
1562 EXPECT_EQ("123", ss.str());
1565 TEST(UniversalPrintTest, WorksForReference) {
1567 ::std::stringstream ss;
1568 UniversalPrint(n, &ss);
1569 EXPECT_EQ("123", ss.str());
1572 TEST(UniversalPrintTest, WorksForCString) {
1573 const char* s1 = "abc";
1574 ::std::stringstream ss1;
1575 UniversalPrint(s1, &ss1);
1576 EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", std::string(ss1.str()));
1578 char* s2 = const_cast<char*>(s1);
1579 ::std::stringstream ss2;
1580 UniversalPrint(s2, &ss2);
1581 EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", std::string(ss2.str()));
1583 const char* s3 = NULL;
1584 ::std::stringstream ss3;
1585 UniversalPrint(s3, &ss3);
1586 EXPECT_EQ("NULL", ss3.str());
1589 TEST(UniversalPrintTest, WorksForCharArray) {
1590 const char str[] = "\"Line\0 1\"\nLine 2";
1591 ::std::stringstream ss1;
1592 UniversalPrint(str, &ss1);
1593 EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
1595 const char mutable_str[] = "\"Line\0 1\"\nLine 2";
1596 ::std::stringstream ss2;
1597 UniversalPrint(mutable_str, &ss2);
1598 EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
1601 #if GTEST_HAS_TR1_TUPLE
1603 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsEmptyTuple) {
1604 Strings result = UniversalTersePrintTupleFieldsToStrings(
1605 ::std::tr1::make_tuple());
1606 EXPECT_EQ(0u, result.size());
1609 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsOneTuple) {
1610 Strings result = UniversalTersePrintTupleFieldsToStrings(
1611 ::std::tr1::make_tuple(1));
1612 ASSERT_EQ(1u, result.size());
1613 EXPECT_EQ("1", result[0]);
1616 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTwoTuple) {
1617 Strings result = UniversalTersePrintTupleFieldsToStrings(
1618 ::std::tr1::make_tuple(1, 'a'));
1619 ASSERT_EQ(2u, result.size());
1620 EXPECT_EQ("1", result[0]);
1621 EXPECT_EQ("'a' (97, 0x61)", result[1]);
1624 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTersely) {
1626 Strings result = UniversalTersePrintTupleFieldsToStrings(
1627 ::std::tr1::tuple<const int&, const char*>(n, "a"));
1628 ASSERT_EQ(2u, result.size());
1629 EXPECT_EQ("1", result[0]);
1630 EXPECT_EQ("\"a\"", result[1]);
1633 #endif // GTEST_HAS_TR1_TUPLE
1635 #if GTEST_HAS_STD_TUPLE_
1637 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
1638 Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
1639 EXPECT_EQ(0u, result.size());
1642 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
1643 Strings result = UniversalTersePrintTupleFieldsToStrings(
1644 ::std::make_tuple(1));
1645 ASSERT_EQ(1u, result.size());
1646 EXPECT_EQ("1", result[0]);
1649 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
1650 Strings result = UniversalTersePrintTupleFieldsToStrings(
1651 ::std::make_tuple(1, 'a'));
1652 ASSERT_EQ(2u, result.size());
1653 EXPECT_EQ("1", result[0]);
1654 EXPECT_EQ("'a' (97, 0x61)", result[1]);
1657 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
1659 Strings result = UniversalTersePrintTupleFieldsToStrings(
1660 ::std::tuple<const int&, const char*>(n, "a"));
1661 ASSERT_EQ(2u, result.size());
1662 EXPECT_EQ("1", result[0]);
1663 EXPECT_EQ("\"a\"", result[1]);
1666 #endif // GTEST_HAS_STD_TUPLE_
1668 } // namespace gtest_printers_test
1669 } // namespace testing