1 // Copyright 2007, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29 //
30 // Author: wan@google.com (Zhanyong Wan)
31
32 // Google Test - The Google C++ Testing Framework
33 //
34 // This file tests the universal value printer.
35
36 #include "gtest/gtest-printers.h"
37
38 #include <ctype.h>
39 #include <limits.h>
40 #include <string.h>
41 #include <algorithm>
42 #include <deque>
43 #include <list>
44 #include <map>
45 #include <set>
46 #include <sstream>
47 #include <string>
48 #include <utility>
49 #include <vector>
50
51 #include "gtest/gtest.h"
52
53 // hash_map and hash_set are available under Visual C++.
54 #if _MSC_VER
55 # define GTEST_HAS_HASH_MAP_ 1 // Indicates that hash_map is available.
56 # include <hash_map> // NOLINT
57 # define GTEST_HAS_HASH_SET_ 1 // Indicates that hash_set is available.
58 # include <hash_set> // NOLINT
59 #endif // GTEST_OS_WINDOWS
60
61 // Some user-defined types for testing the universal value printer.
62
63 // An anonymous enum type.
64 enum AnonymousEnum {
65 kAE1 = -1,
66 kAE2 = 1
67 };
68
69 // An enum without a user-defined printer.
70 enum EnumWithoutPrinter {
71 kEWP1 = -2,
72 kEWP2 = 42
73 };
74
75 // An enum with a << operator.
76 enum EnumWithStreaming {
77 kEWS1 = 10
78 };
79
operator <<(std::ostream & os,EnumWithStreaming e)80 std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) {
81 return os << (e == kEWS1 ? "kEWS1" : "invalid");
82 }
83
84 // An enum with a PrintTo() function.
85 enum EnumWithPrintTo {
86 kEWPT1 = 1
87 };
88
PrintTo(EnumWithPrintTo e,std::ostream * os)89 void PrintTo(EnumWithPrintTo e, std::ostream* os) {
90 *os << (e == kEWPT1 ? "kEWPT1" : "invalid");
91 }
92
93 // A class implicitly convertible to BiggestInt.
94 class BiggestIntConvertible {
95 public:
operator ::testing::internal::BiggestInt() const96 operator ::testing::internal::BiggestInt() const { return 42; }
97 };
98
99 // A user-defined unprintable class template in the global namespace.
100 template <typename T>
101 class UnprintableTemplateInGlobal {
102 public:
UnprintableTemplateInGlobal()103 UnprintableTemplateInGlobal() : value_() {}
104 private:
105 T value_;
106 };
107
108 // A user-defined streamable type in the global namespace.
109 class StreamableInGlobal {
110 public:
~StreamableInGlobal()111 virtual ~StreamableInGlobal() {}
112 };
113
operator <<(::std::ostream & os,const StreamableInGlobal &)114 inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) {
115 os << "StreamableInGlobal";
116 }
117
operator <<(::std::ostream & os,const StreamableInGlobal *)118 void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) {
119 os << "StreamableInGlobal*";
120 }
121
122 namespace foo {
123
124 // A user-defined unprintable type in a user namespace.
125 class UnprintableInFoo {
126 public:
UnprintableInFoo()127 UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); }
128 private:
129 char xy_[8];
130 double z_;
131 };
132
133 // A user-defined printable type in a user-chosen namespace.
134 struct PrintableViaPrintTo {
PrintableViaPrintTofoo::PrintableViaPrintTo135 PrintableViaPrintTo() : value() {}
136 int value;
137 };
138
PrintTo(const PrintableViaPrintTo & x,::std::ostream * os)139 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
140 *os << "PrintableViaPrintTo: " << x.value;
141 }
142
143 // A type with a user-defined << for printing its pointer.
144 struct PointerPrintable {
145 };
146
operator <<(::std::ostream & os,const PointerPrintable *)147 ::std::ostream& operator<<(::std::ostream& os,
148 const PointerPrintable* /* x */) {
149 return os << "PointerPrintable*";
150 }
151
152 // A user-defined printable class template in a user-chosen namespace.
153 template <typename T>
154 class PrintableViaPrintToTemplate {
155 public:
PrintableViaPrintToTemplate(const T & a_value)156 explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
157
value() const158 const T& value() const { return value_; }
159 private:
160 T value_;
161 };
162
163 template <typename T>
PrintTo(const PrintableViaPrintToTemplate<T> & x,::std::ostream * os)164 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
165 *os << "PrintableViaPrintToTemplate: " << x.value();
166 }
167
168 // A user-defined streamable class template in a user namespace.
169 template <typename T>
170 class StreamableTemplateInFoo {
171 public:
StreamableTemplateInFoo()172 StreamableTemplateInFoo() : value_() {}
173
value() const174 const T& value() const { return value_; }
175 private:
176 T value_;
177 };
178
179 template <typename T>
operator <<(::std::ostream & os,const StreamableTemplateInFoo<T> & x)180 inline ::std::ostream& operator<<(::std::ostream& os,
181 const StreamableTemplateInFoo<T>& x) {
182 return os << "StreamableTemplateInFoo: " << x.value();
183 }
184
185 } // namespace foo
186
187 namespace testing {
188 namespace gtest_printers_test {
189
190 using ::std::deque;
191 using ::std::list;
192 using ::std::make_pair;
193 using ::std::map;
194 using ::std::multimap;
195 using ::std::multiset;
196 using ::std::pair;
197 using ::std::set;
198 using ::std::vector;
199 using ::testing::PrintToString;
200 using ::testing::internal::FormatForComparisonFailureMessage;
201 using ::testing::internal::ImplicitCast_;
202 using ::testing::internal::NativeArray;
203 using ::testing::internal::RE;
204 using ::testing::internal::Strings;
205 using ::testing::internal::UniversalPrint;
206 using ::testing::internal::UniversalPrinter;
207 using ::testing::internal::UniversalTersePrint;
208 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
209 using ::testing::internal::kReference;
210 using ::testing::internal::string;
211
212 #if GTEST_HAS_TR1_TUPLE
213 using ::std::tr1::make_tuple;
214 using ::std::tr1::tuple;
215 #endif
216
217 // The hash_* classes are not part of the C++ standard. STLport
218 // defines them in namespace std. MSVC defines them in ::stdext. GCC
219 // defines them in ::.
220 #ifdef _STLP_HASH_MAP // We got <hash_map> from STLport.
221 using ::std::hash_map;
222 using ::std::hash_set;
223 using ::std::hash_multimap;
224 using ::std::hash_multiset;
225 #elif _MSC_VER
226 using ::stdext::hash_map;
227 using ::stdext::hash_set;
228 using ::stdext::hash_multimap;
229 using ::stdext::hash_multiset;
230 #endif
231
232 // Prints a value to a string using the universal value printer. This
233 // is a helper for testing UniversalPrinter<T>::Print() for various types.
234 template <typename T>
Print(const T & value)235 string Print(const T& value) {
236 ::std::stringstream ss;
237 UniversalPrinter<T>::Print(value, &ss);
238 return ss.str();
239 }
240
241 // Prints a value passed by reference to a string, using the universal
242 // value printer. This is a helper for testing
243 // UniversalPrinter<T&>::Print() for various types.
244 template <typename T>
PrintByRef(const T & value)245 string PrintByRef(const T& value) {
246 ::std::stringstream ss;
247 UniversalPrinter<T&>::Print(value, &ss);
248 return ss.str();
249 }
250
251 // Tests printing various enum types.
252
TEST(PrintEnumTest,AnonymousEnum)253 TEST(PrintEnumTest, AnonymousEnum) {
254 EXPECT_EQ("-1", Print(kAE1));
255 EXPECT_EQ("1", Print(kAE2));
256 }
257
TEST(PrintEnumTest,EnumWithoutPrinter)258 TEST(PrintEnumTest, EnumWithoutPrinter) {
259 EXPECT_EQ("-2", Print(kEWP1));
260 EXPECT_EQ("42", Print(kEWP2));
261 }
262
TEST(PrintEnumTest,EnumWithStreaming)263 TEST(PrintEnumTest, EnumWithStreaming) {
264 EXPECT_EQ("kEWS1", Print(kEWS1));
265 EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
266 }
267
TEST(PrintEnumTest,EnumWithPrintTo)268 TEST(PrintEnumTest, EnumWithPrintTo) {
269 EXPECT_EQ("kEWPT1", Print(kEWPT1));
270 EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
271 }
272
273 // Tests printing a class implicitly convertible to BiggestInt.
274
TEST(PrintClassTest,BiggestIntConvertible)275 TEST(PrintClassTest, BiggestIntConvertible) {
276 EXPECT_EQ("42", Print(BiggestIntConvertible()));
277 }
278
279 // Tests printing various char types.
280
281 // char.
TEST(PrintCharTest,PlainChar)282 TEST(PrintCharTest, PlainChar) {
283 EXPECT_EQ("'\\0'", Print('\0'));
284 EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
285 EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
286 EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
287 EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
288 EXPECT_EQ("'\\a' (7)", Print('\a'));
289 EXPECT_EQ("'\\b' (8)", Print('\b'));
290 EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
291 EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
292 EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
293 EXPECT_EQ("'\\t' (9)", Print('\t'));
294 EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
295 EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
296 EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
297 EXPECT_EQ("' ' (32, 0x20)", Print(' '));
298 EXPECT_EQ("'a' (97, 0x61)", Print('a'));
299 }
300
301 // signed char.
TEST(PrintCharTest,SignedChar)302 TEST(PrintCharTest, SignedChar) {
303 EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
304 EXPECT_EQ("'\\xCE' (-50)",
305 Print(static_cast<signed char>(-50)));
306 }
307
308 // unsigned char.
TEST(PrintCharTest,UnsignedChar)309 TEST(PrintCharTest, UnsignedChar) {
310 EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
311 EXPECT_EQ("'b' (98, 0x62)",
312 Print(static_cast<unsigned char>('b')));
313 }
314
315 // Tests printing other simple, built-in types.
316
317 // bool.
TEST(PrintBuiltInTypeTest,Bool)318 TEST(PrintBuiltInTypeTest, Bool) {
319 EXPECT_EQ("false", Print(false));
320 EXPECT_EQ("true", Print(true));
321 }
322
323 // wchar_t.
TEST(PrintBuiltInTypeTest,Wchar_t)324 TEST(PrintBuiltInTypeTest, Wchar_t) {
325 EXPECT_EQ("L'\\0'", Print(L'\0'));
326 EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
327 EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
328 EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
329 EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
330 EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
331 EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
332 EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
333 EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
334 EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
335 EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
336 EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
337 EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
338 EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
339 EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
340 EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
341 EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
342 EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
343 }
344
345 // Test that Int64 provides more storage than wchar_t.
TEST(PrintTypeSizeTest,Wchar_t)346 TEST(PrintTypeSizeTest, Wchar_t) {
347 EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
348 }
349
350 // Various integer types.
TEST(PrintBuiltInTypeTest,Integer)351 TEST(PrintBuiltInTypeTest, Integer) {
352 EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8
353 EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8
354 EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16
355 EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16
356 EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32
357 EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32
358 EXPECT_EQ("18446744073709551615",
359 Print(static_cast<testing::internal::UInt64>(-1))); // uint64
360 EXPECT_EQ("-9223372036854775808",
361 Print(static_cast<testing::internal::Int64>(1) << 63)); // int64
362 }
363
364 // Size types.
TEST(PrintBuiltInTypeTest,Size_t)365 TEST(PrintBuiltInTypeTest, Size_t) {
366 EXPECT_EQ("1", Print(sizeof('a'))); // size_t.
367 #if !GTEST_OS_WINDOWS
368 // Windows has no ssize_t type.
369 EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t.
370 #endif // !GTEST_OS_WINDOWS
371 }
372
373 // Floating-points.
TEST(PrintBuiltInTypeTest,FloatingPoints)374 TEST(PrintBuiltInTypeTest, FloatingPoints) {
375 EXPECT_EQ("1.5", Print(1.5f)); // float
376 EXPECT_EQ("-2.5", Print(-2.5)); // double
377 }
378
379 // Since ::std::stringstream::operator<<(const void *) formats the pointer
380 // output differently with different compilers, we have to create the expected
381 // output first and use it as our expectation.
PrintPointer(const void * p)382 static string PrintPointer(const void *p) {
383 ::std::stringstream expected_result_stream;
384 expected_result_stream << p;
385 return expected_result_stream.str();
386 }
387
388 // Tests printing C strings.
389
390 // const char*.
TEST(PrintCStringTest,Const)391 TEST(PrintCStringTest, Const) {
392 const char* p = "World";
393 EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
394 }
395
396 // char*.
TEST(PrintCStringTest,NonConst)397 TEST(PrintCStringTest, NonConst) {
398 char p[] = "Hi";
399 EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
400 Print(static_cast<char*>(p)));
401 }
402
403 // NULL C string.
TEST(PrintCStringTest,Null)404 TEST(PrintCStringTest, Null) {
405 const char* p = NULL;
406 EXPECT_EQ("NULL", Print(p));
407 }
408
409 // Tests that C strings are escaped properly.
TEST(PrintCStringTest,EscapesProperly)410 TEST(PrintCStringTest, EscapesProperly) {
411 const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
412 EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
413 "\\n\\r\\t\\v\\x7F\\xFF a\"",
414 Print(p));
415 }
416
417
418
419 // MSVC compiler can be configured to define whar_t as a typedef
420 // of unsigned short. Defining an overload for const wchar_t* in that case
421 // would cause pointers to unsigned shorts be printed as wide strings,
422 // possibly accessing more memory than intended and causing invalid
423 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
424 // wchar_t is implemented as a native type.
425 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
426
427 // const wchar_t*.
TEST(PrintWideCStringTest,Const)428 TEST(PrintWideCStringTest, Const) {
429 const wchar_t* p = L"World";
430 EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
431 }
432
433 // wchar_t*.
TEST(PrintWideCStringTest,NonConst)434 TEST(PrintWideCStringTest, NonConst) {
435 wchar_t p[] = L"Hi";
436 EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
437 Print(static_cast<wchar_t*>(p)));
438 }
439
440 // NULL wide C string.
TEST(PrintWideCStringTest,Null)441 TEST(PrintWideCStringTest, Null) {
442 const wchar_t* p = NULL;
443 EXPECT_EQ("NULL", Print(p));
444 }
445
446 // Tests that wide C strings are escaped properly.
TEST(PrintWideCStringTest,EscapesProperly)447 TEST(PrintWideCStringTest, EscapesProperly) {
448 const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r',
449 '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
450 EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f"
451 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
452 Print(static_cast<const wchar_t*>(s)));
453 }
454 #endif // native wchar_t
455
456 // Tests printing pointers to other char types.
457
458 // signed char*.
TEST(PrintCharPointerTest,SignedChar)459 TEST(PrintCharPointerTest, SignedChar) {
460 signed char* p = reinterpret_cast<signed char*>(0x1234);
461 EXPECT_EQ(PrintPointer(p), Print(p));
462 p = NULL;
463 EXPECT_EQ("NULL", Print(p));
464 }
465
466 // const signed char*.
TEST(PrintCharPointerTest,ConstSignedChar)467 TEST(PrintCharPointerTest, ConstSignedChar) {
468 signed char* p = reinterpret_cast<signed char*>(0x1234);
469 EXPECT_EQ(PrintPointer(p), Print(p));
470 p = NULL;
471 EXPECT_EQ("NULL", Print(p));
472 }
473
474 // unsigned char*.
TEST(PrintCharPointerTest,UnsignedChar)475 TEST(PrintCharPointerTest, UnsignedChar) {
476 unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
477 EXPECT_EQ(PrintPointer(p), Print(p));
478 p = NULL;
479 EXPECT_EQ("NULL", Print(p));
480 }
481
482 // const unsigned char*.
TEST(PrintCharPointerTest,ConstUnsignedChar)483 TEST(PrintCharPointerTest, ConstUnsignedChar) {
484 const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
485 EXPECT_EQ(PrintPointer(p), Print(p));
486 p = NULL;
487 EXPECT_EQ("NULL", Print(p));
488 }
489
490 // Tests printing pointers to simple, built-in types.
491
492 // bool*.
TEST(PrintPointerToBuiltInTypeTest,Bool)493 TEST(PrintPointerToBuiltInTypeTest, Bool) {
494 bool* p = reinterpret_cast<bool*>(0xABCD);
495 EXPECT_EQ(PrintPointer(p), Print(p));
496 p = NULL;
497 EXPECT_EQ("NULL", Print(p));
498 }
499
500 // void*.
TEST(PrintPointerToBuiltInTypeTest,Void)501 TEST(PrintPointerToBuiltInTypeTest, Void) {
502 void* p = reinterpret_cast<void*>(0xABCD);
503 EXPECT_EQ(PrintPointer(p), Print(p));
504 p = NULL;
505 EXPECT_EQ("NULL", Print(p));
506 }
507
508 // const void*.
TEST(PrintPointerToBuiltInTypeTest,ConstVoid)509 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
510 const void* p = reinterpret_cast<const void*>(0xABCD);
511 EXPECT_EQ(PrintPointer(p), Print(p));
512 p = NULL;
513 EXPECT_EQ("NULL", Print(p));
514 }
515
516 // Tests printing pointers to pointers.
TEST(PrintPointerToPointerTest,IntPointerPointer)517 TEST(PrintPointerToPointerTest, IntPointerPointer) {
518 int** p = reinterpret_cast<int**>(0xABCD);
519 EXPECT_EQ(PrintPointer(p), Print(p));
520 p = NULL;
521 EXPECT_EQ("NULL", Print(p));
522 }
523
524 // Tests printing (non-member) function pointers.
525
MyFunction(int)526 void MyFunction(int /* n */) {}
527
TEST(PrintPointerTest,NonMemberFunctionPointer)528 TEST(PrintPointerTest, NonMemberFunctionPointer) {
529 // We cannot directly cast &MyFunction to const void* because the
530 // standard disallows casting between pointers to functions and
531 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
532 // this limitation.
533 EXPECT_EQ(
534 PrintPointer(reinterpret_cast<const void*>(
535 reinterpret_cast<internal::BiggestInt>(&MyFunction))),
536 Print(&MyFunction));
537 int (*p)(bool) = NULL; // NOLINT
538 EXPECT_EQ("NULL", Print(p));
539 }
540
541 // An assertion predicate determining whether a one string is a prefix for
542 // another.
543 template <typename StringType>
HasPrefix(const StringType & str,const StringType & prefix)544 AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
545 if (str.find(prefix, 0) == 0)
546 return AssertionSuccess();
547
548 const bool is_wide_string = sizeof(prefix[0]) > 1;
549 const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
550 return AssertionFailure()
551 << begin_string_quote << prefix << "\" is not a prefix of "
552 << begin_string_quote << str << "\"\n";
553 }
554
555 // Tests printing member variable pointers. Although they are called
556 // pointers, they don't point to a location in the address space.
557 // Their representation is implementation-defined. Thus they will be
558 // printed as raw bytes.
559
560 struct Foo {
561 public:
~Footesting::gtest_printers_test::Foo562 virtual ~Foo() {}
MyMethodtesting::gtest_printers_test::Foo563 int MyMethod(char x) { return x + 1; }
MyVirtualMethodtesting::gtest_printers_test::Foo564 virtual char MyVirtualMethod(int /* n */) { return 'a'; }
565
566 int value;
567 };
568
TEST(PrintPointerTest,MemberVariablePointer)569 TEST(PrintPointerTest, MemberVariablePointer) {
570 EXPECT_TRUE(HasPrefix(Print(&Foo::value),
571 Print(sizeof(&Foo::value)) + "-byte object "));
572 int (Foo::*p) = NULL; // NOLINT
573 EXPECT_TRUE(HasPrefix(Print(p),
574 Print(sizeof(p)) + "-byte object "));
575 }
576
577 // Tests printing member function pointers. Although they are called
578 // pointers, they don't point to a location in the address space.
579 // Their representation is implementation-defined. Thus they will be
580 // printed as raw bytes.
TEST(PrintPointerTest,MemberFunctionPointer)581 TEST(PrintPointerTest, MemberFunctionPointer) {
582 EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod),
583 Print(sizeof(&Foo::MyMethod)) + "-byte object "));
584 EXPECT_TRUE(
585 HasPrefix(Print(&Foo::MyVirtualMethod),
586 Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
587 int (Foo::*p)(char) = NULL; // NOLINT
588 EXPECT_TRUE(HasPrefix(Print(p),
589 Print(sizeof(p)) + "-byte object "));
590 }
591
592 // Tests printing C arrays.
593
594 // The difference between this and Print() is that it ensures that the
595 // argument is a reference to an array.
596 template <typename T, size_t N>
PrintArrayHelper(T (& a)[N])597 string PrintArrayHelper(T (&a)[N]) {
598 return Print(a);
599 }
600
601 // One-dimensional array.
TEST(PrintArrayTest,OneDimensionalArray)602 TEST(PrintArrayTest, OneDimensionalArray) {
603 int a[5] = { 1, 2, 3, 4, 5 };
604 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
605 }
606
607 // Two-dimensional array.
TEST(PrintArrayTest,TwoDimensionalArray)608 TEST(PrintArrayTest, TwoDimensionalArray) {
609 int a[2][5] = {
610 { 1, 2, 3, 4, 5 },
611 { 6, 7, 8, 9, 0 }
612 };
613 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
614 }
615
616 // Array of const elements.
TEST(PrintArrayTest,ConstArray)617 TEST(PrintArrayTest, ConstArray) {
618 const bool a[1] = { false };
619 EXPECT_EQ("{ false }", PrintArrayHelper(a));
620 }
621
622 // char array without terminating NUL.
TEST(PrintArrayTest,CharArrayWithNoTerminatingNul)623 TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
624 // Array a contains '\0' in the middle and doesn't end with '\0'.
625 char a[] = { 'H', '\0', 'i' };
626 EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
627 }
628
629 // const char array with terminating NUL.
TEST(PrintArrayTest,ConstCharArrayWithTerminatingNul)630 TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) {
631 const char a[] = "\0Hi";
632 EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a));
633 }
634
635 // const wchar_t array without terminating NUL.
TEST(PrintArrayTest,WCharArrayWithNoTerminatingNul)636 TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
637 // Array a contains '\0' in the middle and doesn't end with '\0'.
638 const wchar_t a[] = { L'H', L'\0', L'i' };
639 EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
640 }
641
642 // wchar_t array with terminating NUL.
TEST(PrintArrayTest,WConstCharArrayWithTerminatingNul)643 TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
644 const wchar_t a[] = L"\0Hi";
645 EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a));
646 }
647
648 // Array of objects.
TEST(PrintArrayTest,ObjectArray)649 TEST(PrintArrayTest, ObjectArray) {
650 string a[3] = { "Hi", "Hello", "Ni hao" };
651 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
652 }
653
654 // Array with many elements.
TEST(PrintArrayTest,BigArray)655 TEST(PrintArrayTest, BigArray) {
656 int a[100] = { 1, 2, 3 };
657 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
658 PrintArrayHelper(a));
659 }
660
661 // Tests printing ::string and ::std::string.
662
663 #if GTEST_HAS_GLOBAL_STRING
664 // ::string.
TEST(PrintStringTest,StringInGlobalNamespace)665 TEST(PrintStringTest, StringInGlobalNamespace) {
666 const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
667 const ::string str(s, sizeof(s));
668 EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
669 Print(str));
670 }
671 #endif // GTEST_HAS_GLOBAL_STRING
672
673 // ::std::string.
TEST(PrintStringTest,StringInStdNamespace)674 TEST(PrintStringTest, StringInStdNamespace) {
675 const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
676 const ::std::string str(s, sizeof(s));
677 EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
678 Print(str));
679 }
680
TEST(PrintStringTest,StringAmbiguousHex)681 TEST(PrintStringTest, StringAmbiguousHex) {
682 // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
683 // '\x6', '\x6B', or '\x6BA'.
684
685 // a hex escaping sequence following by a decimal digit
686 EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
687 // a hex escaping sequence following by a hex digit (lower-case)
688 EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
689 // a hex escaping sequence following by a hex digit (upper-case)
690 EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
691 // a hex escaping sequence following by a non-xdigit
692 EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
693 }
694
695 // Tests printing ::wstring and ::std::wstring.
696
697 #if GTEST_HAS_GLOBAL_WSTRING
698 // ::wstring.
TEST(PrintWideStringTest,StringInGlobalNamespace)699 TEST(PrintWideStringTest, StringInGlobalNamespace) {
700 const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
701 const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
702 EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
703 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
704 Print(str));
705 }
706 #endif // GTEST_HAS_GLOBAL_WSTRING
707
708 #if GTEST_HAS_STD_WSTRING
709 // ::std::wstring.
TEST(PrintWideStringTest,StringInStdNamespace)710 TEST(PrintWideStringTest, StringInStdNamespace) {
711 const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
712 const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
713 EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
714 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
715 Print(str));
716 }
717
TEST(PrintWideStringTest,StringAmbiguousHex)718 TEST(PrintWideStringTest, StringAmbiguousHex) {
719 // same for wide strings.
720 EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3")));
721 EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
722 Print(::std::wstring(L"mm\x6" L"bananas")));
723 EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
724 Print(::std::wstring(L"NOM\x6" L"BANANA")));
725 EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
726 }
727 #endif // GTEST_HAS_STD_WSTRING
728
729 // Tests printing types that support generic streaming (i.e. streaming
730 // to std::basic_ostream<Char, CharTraits> for any valid Char and
731 // CharTraits types).
732
733 // Tests printing a non-template type that supports generic streaming.
734
735 class AllowsGenericStreaming {};
736
737 template <typename Char, typename CharTraits>
operator <<(std::basic_ostream<Char,CharTraits> & os,const AllowsGenericStreaming &)738 std::basic_ostream<Char, CharTraits>& operator<<(
739 std::basic_ostream<Char, CharTraits>& os,
740 const AllowsGenericStreaming& /* a */) {
741 return os << "AllowsGenericStreaming";
742 }
743
TEST(PrintTypeWithGenericStreamingTest,NonTemplateType)744 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
745 AllowsGenericStreaming a;
746 EXPECT_EQ("AllowsGenericStreaming", Print(a));
747 }
748
749 // Tests printing a template type that supports generic streaming.
750
751 template <typename T>
752 class AllowsGenericStreamingTemplate {};
753
754 template <typename Char, typename CharTraits, typename T>
operator <<(std::basic_ostream<Char,CharTraits> & os,const AllowsGenericStreamingTemplate<T> &)755 std::basic_ostream<Char, CharTraits>& operator<<(
756 std::basic_ostream<Char, CharTraits>& os,
757 const AllowsGenericStreamingTemplate<T>& /* a */) {
758 return os << "AllowsGenericStreamingTemplate";
759 }
760
TEST(PrintTypeWithGenericStreamingTest,TemplateType)761 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
762 AllowsGenericStreamingTemplate<int> a;
763 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
764 }
765
766 // Tests printing a type that supports generic streaming and can be
767 // implicitly converted to another printable type.
768
769 template <typename T>
770 class AllowsGenericStreamingAndImplicitConversionTemplate {
771 public:
operator bool() const772 operator bool() const { return false; }
773 };
774
775 template <typename Char, typename CharTraits, typename T>
operator <<(std::basic_ostream<Char,CharTraits> & os,const AllowsGenericStreamingAndImplicitConversionTemplate<T> &)776 std::basic_ostream<Char, CharTraits>& operator<<(
777 std::basic_ostream<Char, CharTraits>& os,
778 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) {
779 return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
780 }
781
TEST(PrintTypeWithGenericStreamingTest,TypeImplicitlyConvertible)782 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
783 AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
784 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
785 }
786
787 #if GTEST_HAS_STRING_PIECE_
788
789 // Tests printing StringPiece.
790
TEST(PrintStringPieceTest,SimpleStringPiece)791 TEST(PrintStringPieceTest, SimpleStringPiece) {
792 const StringPiece sp = "Hello";
793 EXPECT_EQ("\"Hello\"", Print(sp));
794 }
795
TEST(PrintStringPieceTest,UnprintableCharacters)796 TEST(PrintStringPieceTest, UnprintableCharacters) {
797 const char str[] = "NUL (\0) and \r\t";
798 const StringPiece sp(str, sizeof(str) - 1);
799 EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
800 }
801
802 #endif // GTEST_HAS_STRING_PIECE_
803
804 // Tests printing STL containers.
805
TEST(PrintStlContainerTest,EmptyDeque)806 TEST(PrintStlContainerTest, EmptyDeque) {
807 deque<char> empty;
808 EXPECT_EQ("{}", Print(empty));
809 }
810
TEST(PrintStlContainerTest,NonEmptyDeque)811 TEST(PrintStlContainerTest, NonEmptyDeque) {
812 deque<int> non_empty;
813 non_empty.push_back(1);
814 non_empty.push_back(3);
815 EXPECT_EQ("{ 1, 3 }", Print(non_empty));
816 }
817
818 #if GTEST_HAS_HASH_MAP_
819
TEST(PrintStlContainerTest,OneElementHashMap)820 TEST(PrintStlContainerTest, OneElementHashMap) {
821 hash_map<int, char> map1;
822 map1[1] = 'a';
823 EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1));
824 }
825
TEST(PrintStlContainerTest,HashMultiMap)826 TEST(PrintStlContainerTest, HashMultiMap) {
827 hash_multimap<int, bool> map1;
828 map1.insert(make_pair(5, true));
829 map1.insert(make_pair(5, false));
830
831 // Elements of hash_multimap can be printed in any order.
832 const string result = Print(map1);
833 EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
834 result == "{ (5, false), (5, true) }")
835 << " where Print(map1) returns \"" << result << "\".";
836 }
837
838 #endif // GTEST_HAS_HASH_MAP_
839
840 #if GTEST_HAS_HASH_SET_
841
TEST(PrintStlContainerTest,HashSet)842 TEST(PrintStlContainerTest, HashSet) {
843 hash_set<string> set1;
844 set1.insert("hello");
845 EXPECT_EQ("{ \"hello\" }", Print(set1));
846 }
847
TEST(PrintStlContainerTest,HashMultiSet)848 TEST(PrintStlContainerTest, HashMultiSet) {
849 const int kSize = 5;
850 int a[kSize] = { 1, 1, 2, 5, 1 };
851 hash_multiset<int> set1(a, a + kSize);
852
853 // Elements of hash_multiset can be printed in any order.
854 const string result = Print(set1);
855 const string expected_pattern = "{ d, d, d, d, d }"; // d means a digit.
856
857 // Verifies the result matches the expected pattern; also extracts
858 // the numbers in the result.
859 ASSERT_EQ(expected_pattern.length(), result.length());
860 std::vector<int> numbers;
861 for (size_t i = 0; i != result.length(); i++) {
862 if (expected_pattern[i] == 'd') {
863 ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0);
864 numbers.push_back(result[i] - '0');
865 } else {
866 EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
867 << result;
868 }
869 }
870
871 // Makes sure the result contains the right numbers.
872 std::sort(numbers.begin(), numbers.end());
873 std::sort(a, a + kSize);
874 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
875 }
876
877 #endif // GTEST_HAS_HASH_SET_
878
TEST(PrintStlContainerTest,List)879 TEST(PrintStlContainerTest, List) {
880 const string a[] = {
881 "hello",
882 "world"
883 };
884 const list<string> strings(a, a + 2);
885 EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
886 }
887
TEST(PrintStlContainerTest,Map)888 TEST(PrintStlContainerTest, Map) {
889 map<int, bool> map1;
890 map1[1] = true;
891 map1[5] = false;
892 map1[3] = true;
893 EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
894 }
895
TEST(PrintStlContainerTest,MultiMap)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
903 // here instead.
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));
908 }
909
TEST(PrintStlContainerTest,Set)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));
914 }
915
TEST(PrintStlContainerTest,MultiSet)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));
920 }
921
TEST(PrintStlContainerTest,Pair)922 TEST(PrintStlContainerTest, Pair) {
923 pair<const bool, int> p(true, 5);
924 EXPECT_EQ("(true, 5)", Print(p));
925 }
926
TEST(PrintStlContainerTest,Vector)927 TEST(PrintStlContainerTest, Vector) {
928 vector<int> v;
929 v.push_back(1);
930 v.push_back(2);
931 EXPECT_EQ("{ 1, 2 }", Print(v));
932 }
933
TEST(PrintStlContainerTest,LongSequence)934 TEST(PrintStlContainerTest, LongSequence) {
935 const int a[100] = { 1, 2, 3 };
936 const vector<int> v(a, a + 100);
937 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
938 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
939 }
940
TEST(PrintStlContainerTest,NestedContainer)941 TEST(PrintStlContainerTest, NestedContainer) {
942 const int a1[] = { 1, 2 };
943 const int a2[] = { 3, 4, 5 };
944 const list<int> l1(a1, a1 + 2);
945 const list<int> l2(a2, a2 + 3);
946
947 vector<list<int> > v;
948 v.push_back(l1);
949 v.push_back(l2);
950 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
951 }
952
TEST(PrintStlContainerTest,OneDimensionalNativeArray)953 TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
954 const int a[3] = { 1, 2, 3 };
955 NativeArray<int> b(a, 3, kReference);
956 EXPECT_EQ("{ 1, 2, 3 }", Print(b));
957 }
958
TEST(PrintStlContainerTest,TwoDimensionalNativeArray)959 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
960 const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
961 NativeArray<int[3]> b(a, 2, kReference);
962 EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
963 }
964
965 // Tests that a class named iterator isn't treated as a container.
966
967 struct iterator {
968 char x;
969 };
970
TEST(PrintStlContainerTest,Iterator)971 TEST(PrintStlContainerTest, Iterator) {
972 iterator it = {};
973 EXPECT_EQ("1-byte object <00>", Print(it));
974 }
975
976 // Tests that a class named const_iterator isn't treated as a container.
977
978 struct const_iterator {
979 char x;
980 };
981
TEST(PrintStlContainerTest,ConstIterator)982 TEST(PrintStlContainerTest, ConstIterator) {
983 const_iterator it = {};
984 EXPECT_EQ("1-byte object <00>", Print(it));
985 }
986
987 #if GTEST_HAS_TR1_TUPLE
988 // Tests printing tuples.
989
990 // Tuples of various arities.
TEST(PrintTupleTest,VariousSizes)991 TEST(PrintTupleTest, VariousSizes) {
992 tuple<> t0;
993 EXPECT_EQ("()", Print(t0));
994
995 tuple<int> t1(5);
996 EXPECT_EQ("(5)", Print(t1));
997
998 tuple<char, bool> t2('a', true);
999 EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
1000
1001 tuple<bool, int, int> t3(false, 2, 3);
1002 EXPECT_EQ("(false, 2, 3)", Print(t3));
1003
1004 tuple<bool, int, int, int> t4(false, 2, 3, 4);
1005 EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
1006
1007 tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
1008 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
1009
1010 tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
1011 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
1012
1013 tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7);
1014 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
1015
1016 tuple<bool, int, int, int, bool, int, int, bool> t8(
1017 false, 2, 3, 4, true, 6, 7, true);
1018 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
1019
1020 tuple<bool, int, int, int, bool, int, int, bool, int> t9(
1021 false, 2, 3, 4, true, 6, 7, true, 9);
1022 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
1023
1024 const char* const str = "8";
1025 // VC++ 2010's implementation of tuple of C++0x is deficient, requiring
1026 // an explicit type cast of NULL to be used.
1027 tuple<bool, char, short, testing::internal::Int32, // NOLINT
1028 testing::internal::Int64, float, double, const char*, void*, string>
1029 t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str,
1030 ImplicitCast_<void*>(NULL), "10");
1031 EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1032 " pointing to \"8\", NULL, \"10\")",
1033 Print(t10));
1034 }
1035
1036 // Nested tuples.
TEST(PrintTupleTest,NestedTuple)1037 TEST(PrintTupleTest, NestedTuple) {
1038 tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a');
1039 EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
1040 }
1041
1042 #endif // GTEST_HAS_TR1_TUPLE
1043
1044 // Tests printing user-defined unprintable types.
1045
1046 // Unprintable types in the global namespace.
TEST(PrintUnprintableTypeTest,InGlobalNamespace)1047 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
1048 EXPECT_EQ("1-byte object <00>",
1049 Print(UnprintableTemplateInGlobal<char>()));
1050 }
1051
1052 // Unprintable types in a user namespace.
TEST(PrintUnprintableTypeTest,InUserNamespace)1053 TEST(PrintUnprintableTypeTest, InUserNamespace) {
1054 EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1055 Print(::foo::UnprintableInFoo()));
1056 }
1057
1058 // Unprintable types are that too big to be printed completely.
1059
1060 struct Big {
Bigtesting::gtest_printers_test::Big1061 Big() { memset(array, 0, sizeof(array)); }
1062 char array[257];
1063 };
1064
TEST(PrintUnpritableTypeTest,BigObject)1065 TEST(PrintUnpritableTypeTest, BigObject) {
1066 EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
1067 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1068 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1069 "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
1070 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1071 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1072 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
1073 Print(Big()));
1074 }
1075
1076 // Tests printing user-defined streamable types.
1077
1078 // Streamable types in the global namespace.
TEST(PrintStreamableTypeTest,InGlobalNamespace)1079 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
1080 StreamableInGlobal x;
1081 EXPECT_EQ("StreamableInGlobal", Print(x));
1082 EXPECT_EQ("StreamableInGlobal*", Print(&x));
1083 }
1084
1085 // Printable template types in a user namespace.
TEST(PrintStreamableTypeTest,TemplateTypeInUserNamespace)1086 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
1087 EXPECT_EQ("StreamableTemplateInFoo: 0",
1088 Print(::foo::StreamableTemplateInFoo<int>()));
1089 }
1090
1091 // Tests printing user-defined types that have a PrintTo() function.
TEST(PrintPrintableTypeTest,InUserNamespace)1092 TEST(PrintPrintableTypeTest, InUserNamespace) {
1093 EXPECT_EQ("PrintableViaPrintTo: 0",
1094 Print(::foo::PrintableViaPrintTo()));
1095 }
1096
1097 // Tests printing a pointer to a user-defined type that has a <<
1098 // operator for its pointer.
TEST(PrintPrintableTypeTest,PointerInUserNamespace)1099 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
1100 ::foo::PointerPrintable x;
1101 EXPECT_EQ("PointerPrintable*", Print(&x));
1102 }
1103
1104 // Tests printing user-defined class template that have a PrintTo() function.
TEST(PrintPrintableTypeTest,TemplateInUserNamespace)1105 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
1106 EXPECT_EQ("PrintableViaPrintToTemplate: 5",
1107 Print(::foo::PrintableViaPrintToTemplate<int>(5)));
1108 }
1109
1110 #if GTEST_HAS_PROTOBUF_
1111
1112 // Tests printing a protocol message.
TEST(PrintProtocolMessageTest,PrintsShortDebugString)1113 TEST(PrintProtocolMessageTest, PrintsShortDebugString) {
1114 testing::internal::TestMessage msg;
1115 msg.set_member("yes");
1116 EXPECT_EQ("<member:\"yes\">", Print(msg));
1117 }
1118
1119 // Tests printing a short proto2 message.
TEST(PrintProto2MessageTest,PrintsShortDebugStringWhenItIsShort)1120 TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) {
1121 testing::internal::FooMessage msg;
1122 msg.set_int_field(2);
1123 msg.set_string_field("hello");
1124 EXPECT_PRED2(RE::FullMatch, Print(msg),
1125 "<int_field:\\s*2\\s+string_field:\\s*\"hello\">");
1126 }
1127
1128 // Tests printing a long proto2 message.
TEST(PrintProto2MessageTest,PrintsDebugStringWhenItIsLong)1129 TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) {
1130 testing::internal::FooMessage msg;
1131 msg.set_int_field(2);
1132 msg.set_string_field("hello");
1133 msg.add_names("peter");
1134 msg.add_names("paul");
1135 msg.add_names("mary");
1136 EXPECT_PRED2(RE::FullMatch, Print(msg),
1137 "<\n"
1138 "int_field:\\s*2\n"
1139 "string_field:\\s*\"hello\"\n"
1140 "names:\\s*\"peter\"\n"
1141 "names:\\s*\"paul\"\n"
1142 "names:\\s*\"mary\"\n"
1143 ">");
1144 }
1145
1146 #endif // GTEST_HAS_PROTOBUF_
1147
1148 // Tests that the universal printer prints both the address and the
1149 // value of a reference.
TEST(PrintReferenceTest,PrintsAddressAndValue)1150 TEST(PrintReferenceTest, PrintsAddressAndValue) {
1151 int n = 5;
1152 EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
1153
1154 int a[2][3] = {
1155 { 0, 1, 2 },
1156 { 3, 4, 5 }
1157 };
1158 EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
1159 PrintByRef(a));
1160
1161 const ::foo::UnprintableInFoo x;
1162 EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
1163 "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1164 PrintByRef(x));
1165 }
1166
1167 // Tests that the universal printer prints a function pointer passed by
1168 // reference.
TEST(PrintReferenceTest,HandlesFunctionPointer)1169 TEST(PrintReferenceTest, HandlesFunctionPointer) {
1170 void (*fp)(int n) = &MyFunction;
1171 const string fp_pointer_string =
1172 PrintPointer(reinterpret_cast<const void*>(&fp));
1173 // We cannot directly cast &MyFunction to const void* because the
1174 // standard disallows casting between pointers to functions and
1175 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
1176 // this limitation.
1177 const string fp_string = PrintPointer(reinterpret_cast<const void*>(
1178 reinterpret_cast<internal::BiggestInt>(fp)));
1179 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
1180 PrintByRef(fp));
1181 }
1182
1183 // Tests that the universal printer prints a member function pointer
1184 // passed by reference.
TEST(PrintReferenceTest,HandlesMemberFunctionPointer)1185 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
1186 int (Foo::*p)(char ch) = &Foo::MyMethod;
1187 EXPECT_TRUE(HasPrefix(
1188 PrintByRef(p),
1189 "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
1190 Print(sizeof(p)) + "-byte object "));
1191
1192 char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
1193 EXPECT_TRUE(HasPrefix(
1194 PrintByRef(p2),
1195 "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
1196 Print(sizeof(p2)) + "-byte object "));
1197 }
1198
1199 // Tests that the universal printer prints a member variable pointer
1200 // passed by reference.
TEST(PrintReferenceTest,HandlesMemberVariablePointer)1201 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
1202 int (Foo::*p) = &Foo::value; // NOLINT
1203 EXPECT_TRUE(HasPrefix(
1204 PrintByRef(p),
1205 "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
1206 }
1207
1208 // Tests that FormatForComparisonFailureMessage(), which is used to print
1209 // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion
1210 // fails, formats the operand in the desired way.
1211
1212 // scalar
TEST(FormatForComparisonFailureMessageTest,WorksForScalar)1213 TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
1214 EXPECT_STREQ("123",
1215 FormatForComparisonFailureMessage(123, 124).c_str());
1216 }
1217
1218 // non-char pointer
TEST(FormatForComparisonFailureMessageTest,WorksForNonCharPointer)1219 TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
1220 int n = 0;
1221 EXPECT_EQ(PrintPointer(&n),
1222 FormatForComparisonFailureMessage(&n, &n).c_str());
1223 }
1224
1225 // non-char array
TEST(FormatForComparisonFailureMessageTest,FormatsNonCharArrayAsPointer)1226 TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
1227 // In expression 'array == x', 'array' is compared by pointer.
1228 // Therefore we want to print an array operand as a pointer.
1229 int n[] = { 1, 2, 3 };
1230 EXPECT_EQ(PrintPointer(n),
1231 FormatForComparisonFailureMessage(n, n).c_str());
1232 }
1233
1234 // Tests formatting a char pointer when it's compared with another pointer.
1235 // In this case we want to print it as a raw pointer, as the comparision is by
1236 // pointer.
1237
1238 // char pointer vs pointer
TEST(FormatForComparisonFailureMessageTest,WorksForCharPointerVsPointer)1239 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
1240 // In expression 'p == x', where 'p' and 'x' are (const or not) char
1241 // pointers, the operands are compared by pointer. Therefore we
1242 // want to print 'p' as a pointer instead of a C string (we don't
1243 // even know if it's supposed to point to a valid C string).
1244
1245 // const char*
1246 const char* s = "hello";
1247 EXPECT_EQ(PrintPointer(s),
1248 FormatForComparisonFailureMessage(s, s).c_str());
1249
1250 // char*
1251 char ch = 'a';
1252 EXPECT_EQ(PrintPointer(&ch),
1253 FormatForComparisonFailureMessage(&ch, &ch).c_str());
1254 }
1255
1256 // wchar_t pointer vs pointer
TEST(FormatForComparisonFailureMessageTest,WorksForWCharPointerVsPointer)1257 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
1258 // In expression 'p == x', where 'p' and 'x' are (const or not) char
1259 // pointers, the operands are compared by pointer. Therefore we
1260 // want to print 'p' as a pointer instead of a wide C string (we don't
1261 // even know if it's supposed to point to a valid wide C string).
1262
1263 // const wchar_t*
1264 const wchar_t* s = L"hello";
1265 EXPECT_EQ(PrintPointer(s),
1266 FormatForComparisonFailureMessage(s, s).c_str());
1267
1268 // wchar_t*
1269 wchar_t ch = L'a';
1270 EXPECT_EQ(PrintPointer(&ch),
1271 FormatForComparisonFailureMessage(&ch, &ch).c_str());
1272 }
1273
1274 // Tests formatting a char pointer when it's compared to a string object.
1275 // In this case we want to print the char pointer as a C string.
1276
1277 #if GTEST_HAS_GLOBAL_STRING
1278 // char pointer vs ::string
TEST(FormatForComparisonFailureMessageTest,WorksForCharPointerVsString)1279 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) {
1280 const char* s = "hello \"world";
1281 EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1282 FormatForComparisonFailureMessage(s, ::string()).c_str());
1283
1284 // char*
1285 char str[] = "hi\1";
1286 char* p = str;
1287 EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1288 FormatForComparisonFailureMessage(p, ::string()).c_str());
1289 }
1290 #endif
1291
1292 // char pointer vs std::string
TEST(FormatForComparisonFailureMessageTest,WorksForCharPointerVsStdString)1293 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
1294 const char* s = "hello \"world";
1295 EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1296 FormatForComparisonFailureMessage(s, ::std::string()).c_str());
1297
1298 // char*
1299 char str[] = "hi\1";
1300 char* p = str;
1301 EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1302 FormatForComparisonFailureMessage(p, ::std::string()).c_str());
1303 }
1304
1305 #if GTEST_HAS_GLOBAL_WSTRING
1306 // wchar_t pointer vs ::wstring
TEST(FormatForComparisonFailureMessageTest,WorksForWCharPointerVsWString)1307 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) {
1308 const wchar_t* s = L"hi \"world";
1309 EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1310 FormatForComparisonFailureMessage(s, ::wstring()).c_str());
1311
1312 // wchar_t*
1313 wchar_t str[] = L"hi\1";
1314 wchar_t* p = str;
1315 EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1316 FormatForComparisonFailureMessage(p, ::wstring()).c_str());
1317 }
1318 #endif
1319
1320 #if GTEST_HAS_STD_WSTRING
1321 // wchar_t pointer vs std::wstring
TEST(FormatForComparisonFailureMessageTest,WorksForWCharPointerVsStdWString)1322 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
1323 const wchar_t* s = L"hi \"world";
1324 EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1325 FormatForComparisonFailureMessage(s, ::std::wstring()).c_str());
1326
1327 // wchar_t*
1328 wchar_t str[] = L"hi\1";
1329 wchar_t* p = str;
1330 EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1331 FormatForComparisonFailureMessage(p, ::std::wstring()).c_str());
1332 }
1333 #endif
1334
1335 // Tests formatting a char array when it's compared with a pointer or array.
1336 // In this case we want to print the array as a row pointer, as the comparison
1337 // is by pointer.
1338
1339 // char array vs pointer
TEST(FormatForComparisonFailureMessageTest,WorksForCharArrayVsPointer)1340 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
1341 char str[] = "hi \"world\"";
1342 char* p = NULL;
1343 EXPECT_EQ(PrintPointer(str),
1344 FormatForComparisonFailureMessage(str, p).c_str());
1345 }
1346
1347 // char array vs char array
TEST(FormatForComparisonFailureMessageTest,WorksForCharArrayVsCharArray)1348 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
1349 const char str[] = "hi \"world\"";
1350 EXPECT_EQ(PrintPointer(str),
1351 FormatForComparisonFailureMessage(str, str).c_str());
1352 }
1353
1354 // wchar_t array vs pointer
TEST(FormatForComparisonFailureMessageTest,WorksForWCharArrayVsPointer)1355 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
1356 wchar_t str[] = L"hi \"world\"";
1357 wchar_t* p = NULL;
1358 EXPECT_EQ(PrintPointer(str),
1359 FormatForComparisonFailureMessage(str, p).c_str());
1360 }
1361
1362 // wchar_t array vs wchar_t array
TEST(FormatForComparisonFailureMessageTest,WorksForWCharArrayVsWCharArray)1363 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
1364 const wchar_t str[] = L"hi \"world\"";
1365 EXPECT_EQ(PrintPointer(str),
1366 FormatForComparisonFailureMessage(str, str).c_str());
1367 }
1368
1369 // Tests formatting a char array when it's compared with a string object.
1370 // In this case we want to print the array as a C string.
1371
1372 #if GTEST_HAS_GLOBAL_STRING
1373 // char array vs string
TEST(FormatForComparisonFailureMessageTest,WorksForCharArrayVsString)1374 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) {
1375 const char str[] = "hi \"w\0rld\"";
1376 EXPECT_STREQ("\"hi \\\"w\"", // The content should be escaped.
1377 // Embedded NUL terminates the string.
1378 FormatForComparisonFailureMessage(str, ::string()).c_str());
1379 }
1380 #endif
1381
1382 // char array vs std::string
TEST(FormatForComparisonFailureMessageTest,WorksForCharArrayVsStdString)1383 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
1384 const char str[] = "hi \"world\"";
1385 EXPECT_STREQ("\"hi \\\"world\\\"\"", // The content should be escaped.
1386 FormatForComparisonFailureMessage(str, ::std::string()).c_str());
1387 }
1388
1389 #if GTEST_HAS_GLOBAL_WSTRING
1390 // wchar_t array vs wstring
TEST(FormatForComparisonFailureMessageTest,WorksForWCharArrayVsWString)1391 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) {
1392 const wchar_t str[] = L"hi \"world\"";
1393 EXPECT_STREQ("L\"hi \\\"world\\\"\"", // The content should be escaped.
1394 FormatForComparisonFailureMessage(str, ::wstring()).c_str());
1395 }
1396 #endif
1397
1398 #if GTEST_HAS_STD_WSTRING
1399 // wchar_t array vs std::wstring
TEST(FormatForComparisonFailureMessageTest,WorksForWCharArrayVsStdWString)1400 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
1401 const wchar_t str[] = L"hi \"w\0rld\"";
1402 EXPECT_STREQ(
1403 "L\"hi \\\"w\"", // The content should be escaped.
1404 // Embedded NUL terminates the string.
1405 FormatForComparisonFailureMessage(str, ::std::wstring()).c_str());
1406 }
1407 #endif
1408
1409 // Useful for testing PrintToString(). We cannot use EXPECT_EQ()
1410 // there as its implementation uses PrintToString(). The caller must
1411 // ensure that 'value' has no side effect.
1412 #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
1413 EXPECT_TRUE(PrintToString(value) == (expected_string)) \
1414 << " where " #value " prints as " << (PrintToString(value))
1415
TEST(PrintToStringTest,WorksForScalar)1416 TEST(PrintToStringTest, WorksForScalar) {
1417 EXPECT_PRINT_TO_STRING_(123, "123");
1418 }
1419
TEST(PrintToStringTest,WorksForPointerToConstChar)1420 TEST(PrintToStringTest, WorksForPointerToConstChar) {
1421 const char* p = "hello";
1422 EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
1423 }
1424
TEST(PrintToStringTest,WorksForPointerToNonConstChar)1425 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
1426 char s[] = "hello";
1427 char* p = s;
1428 EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
1429 }
1430
TEST(PrintToStringTest,EscapesForPointerToConstChar)1431 TEST(PrintToStringTest, EscapesForPointerToConstChar) {
1432 const char* p = "hello\n";
1433 EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\"");
1434 }
1435
TEST(PrintToStringTest,EscapesForPointerToNonConstChar)1436 TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
1437 char s[] = "hello\1";
1438 char* p = s;
1439 EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\"");
1440 }
1441
TEST(PrintToStringTest,WorksForArray)1442 TEST(PrintToStringTest, WorksForArray) {
1443 int n[3] = { 1, 2, 3 };
1444 EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
1445 }
1446
TEST(PrintToStringTest,WorksForCharArray)1447 TEST(PrintToStringTest, WorksForCharArray) {
1448 char s[] = "hello";
1449 EXPECT_PRINT_TO_STRING_(s, "\"hello\"");
1450 }
1451
TEST(PrintToStringTest,WorksForCharArrayWithEmbeddedNul)1452 TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
1453 const char str_with_nul[] = "hello\0 world";
1454 EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\"");
1455
1456 char mutable_str_with_nul[] = "hello\0 world";
1457 EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\"");
1458 }
1459
1460 #undef EXPECT_PRINT_TO_STRING_
1461
TEST(UniversalTersePrintTest,WorksForNonReference)1462 TEST(UniversalTersePrintTest, WorksForNonReference) {
1463 ::std::stringstream ss;
1464 UniversalTersePrint(123, &ss);
1465 EXPECT_EQ("123", ss.str());
1466 }
1467
TEST(UniversalTersePrintTest,WorksForReference)1468 TEST(UniversalTersePrintTest, WorksForReference) {
1469 const int& n = 123;
1470 ::std::stringstream ss;
1471 UniversalTersePrint(n, &ss);
1472 EXPECT_EQ("123", ss.str());
1473 }
1474
TEST(UniversalTersePrintTest,WorksForCString)1475 TEST(UniversalTersePrintTest, WorksForCString) {
1476 const char* s1 = "abc";
1477 ::std::stringstream ss1;
1478 UniversalTersePrint(s1, &ss1);
1479 EXPECT_EQ("\"abc\"", ss1.str());
1480
1481 char* s2 = const_cast<char*>(s1);
1482 ::std::stringstream ss2;
1483 UniversalTersePrint(s2, &ss2);
1484 EXPECT_EQ("\"abc\"", ss2.str());
1485
1486 const char* s3 = NULL;
1487 ::std::stringstream ss3;
1488 UniversalTersePrint(s3, &ss3);
1489 EXPECT_EQ("NULL", ss3.str());
1490 }
1491
TEST(UniversalPrintTest,WorksForNonReference)1492 TEST(UniversalPrintTest, WorksForNonReference) {
1493 ::std::stringstream ss;
1494 UniversalPrint(123, &ss);
1495 EXPECT_EQ("123", ss.str());
1496 }
1497
TEST(UniversalPrintTest,WorksForReference)1498 TEST(UniversalPrintTest, WorksForReference) {
1499 const int& n = 123;
1500 ::std::stringstream ss;
1501 UniversalPrint(n, &ss);
1502 EXPECT_EQ("123", ss.str());
1503 }
1504
TEST(UniversalPrintTest,WorksForCString)1505 TEST(UniversalPrintTest, WorksForCString) {
1506 const char* s1 = "abc";
1507 ::std::stringstream ss1;
1508 UniversalPrint(s1, &ss1);
1509 EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str()));
1510
1511 char* s2 = const_cast<char*>(s1);
1512 ::std::stringstream ss2;
1513 UniversalPrint(s2, &ss2);
1514 EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str()));
1515
1516 const char* s3 = NULL;
1517 ::std::stringstream ss3;
1518 UniversalPrint(s3, &ss3);
1519 EXPECT_EQ("NULL", ss3.str());
1520 }
1521
TEST(UniversalPrintTest,WorksForCharArray)1522 TEST(UniversalPrintTest, WorksForCharArray) {
1523 const char str[] = "\"Line\0 1\"\nLine 2";
1524 ::std::stringstream ss1;
1525 UniversalPrint(str, &ss1);
1526 EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
1527
1528 const char mutable_str[] = "\"Line\0 1\"\nLine 2";
1529 ::std::stringstream ss2;
1530 UniversalPrint(mutable_str, &ss2);
1531 EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
1532 }
1533
1534 #if GTEST_HAS_TR1_TUPLE
1535
TEST(UniversalTersePrintTupleFieldsToStringsTest,PrintsEmptyTuple)1536 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) {
1537 Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple());
1538 EXPECT_EQ(0u, result.size());
1539 }
1540
TEST(UniversalTersePrintTupleFieldsToStringsTest,PrintsOneTuple)1541 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) {
1542 Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1));
1543 ASSERT_EQ(1u, result.size());
1544 EXPECT_EQ("1", result[0]);
1545 }
1546
TEST(UniversalTersePrintTupleFieldsToStringsTest,PrintsTwoTuple)1547 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) {
1548 Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a'));
1549 ASSERT_EQ(2u, result.size());
1550 EXPECT_EQ("1", result[0]);
1551 EXPECT_EQ("'a' (97, 0x61)", result[1]);
1552 }
1553
TEST(UniversalTersePrintTupleFieldsToStringsTest,PrintsTersely)1554 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) {
1555 const int n = 1;
1556 Strings result = UniversalTersePrintTupleFieldsToStrings(
1557 tuple<const int&, const char*>(n, "a"));
1558 ASSERT_EQ(2u, result.size());
1559 EXPECT_EQ("1", result[0]);
1560 EXPECT_EQ("\"a\"", result[1]);
1561 }
1562
1563 #endif // GTEST_HAS_TR1_TUPLE
1564
1565 } // namespace gtest_printers_test
1566 } // namespace testing
1567