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