Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gtest-printers_test.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file gtest-printers_test.cc
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++, or on Linux.
54 #if GTEST_HAS_HASH_MAP_
55 # include <hash_map> // NOLINT
56 #endif // GTEST_HAS_HASH_MAP_
57 #if GTEST_HAS_HASH_SET_
58 # include <hash_set> // NOLINT
59 #endif // GTEST_HAS_HASH_SET_
60 
61 #if GTEST_HAS_STD_FORWARD_LIST_
62 # include <forward_list> // NOLINT
63 #endif // GTEST_HAS_STD_FORWARD_LIST_
64 
65 // Some user-defined types for testing the universal value printer.
66 
67 // An anonymous enum type.
69  kAE1 = -1,
70  kAE2 = 1
71 };
72 
73 // An enum without a user-defined printer.
75  kEWP1 = -2,
76  kEWP2 = 42
77 };
78 
79 // An enum with a << operator.
81  kEWS1 = 10
82 };
83 
84 std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) {
85  return os << (e == kEWS1 ? "kEWS1" : "invalid");
86 }
87 
88 // An enum with a PrintTo() function.
90  kEWPT1 = 1
91 };
92 
93 void PrintTo(EnumWithPrintTo e, std::ostream* os) {
94  *os << (e == kEWPT1 ? "kEWPT1" : "invalid");
95 }
96 
97 // A class implicitly convertible to BiggestInt.
99  public:
101 };
102 
103 // A user-defined unprintable class template in the global namespace.
104 template <typename T>
106  public:
108  private:
110 };
111 
112 // A user-defined streamable type in the global namespace.
114  public:
115  virtual ~StreamableInGlobal() {}
116 };
117 
118 inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) {
119  os << "StreamableInGlobal";
120 }
121 
122 void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) {
123  os << "StreamableInGlobal*";
124 }
125 
126 namespace foo {
127 
128 // A user-defined unprintable type in a user namespace.
130  public:
131  UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); }
132  double z() const { return z_; }
133  private:
134  char xy_[8];
135  double z_;
136 };
137 
138 // A user-defined printable type in a user-chosen namespace.
141  int value;
142 };
143 
144 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
145  *os << "PrintableViaPrintTo: " << x.value;
146 }
147 
148 // A type with a user-defined << for printing its pointer.
150 };
151 
152 ::std::ostream& operator<<(::std::ostream& os,
153  const PointerPrintable* /* x */) {
154  return os << "PointerPrintable*";
155 }
156 
157 // A user-defined printable class template in a user-chosen namespace.
158 template <typename T>
160  public:
161  explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
162 
163  const T& value() const { return value_; }
164  private:
166 };
167 
168 template <typename T>
169 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
170  *os << "PrintableViaPrintToTemplate: " << x.value();
171 }
172 
173 // A user-defined streamable class template in a user namespace.
174 template <typename T>
176  public:
178 
179  const T& value() const { return value_; }
180  private:
182 };
183 
184 template <typename T>
185 inline ::std::ostream& operator<<(::std::ostream& os,
186  const StreamableTemplateInFoo<T>& x) {
187  return os << "StreamableTemplateInFoo: " << x.value();
188 }
189 
190 } // namespace foo
191 
192 namespace testing {
193 namespace gtest_printers_test {
194 
195 using ::std::deque;
196 using ::std::list;
197 using ::std::make_pair;
198 using ::std::map;
199 using ::std::multimap;
200 using ::std::multiset;
201 using ::std::pair;
203 using ::std::vector;
207 using ::testing::internal::NativeArray;
208 using ::testing::internal::RE;
209 using ::testing::internal::RelationToSourceReference;
212 using ::testing::internal::UniversalPrinter;
214 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
215 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
216 #endif
218 
219 // The hash_* classes are not part of the C++ standard. STLport
220 // defines them in namespace std. MSVC defines them in ::stdext. GCC
221 // defines them in ::.
222 #ifdef _STLP_HASH_MAP // We got <hash_map> from STLport.
223 using ::std::hash_map;
224 using ::std::hash_set;
225 using ::std::hash_multimap;
226 using ::std::hash_multiset;
227 #elif _MSC_VER
228 using ::stdext::hash_map;
229 using ::stdext::hash_set;
230 using ::stdext::hash_multimap;
231 using ::stdext::hash_multiset;
232 #endif
233 
234 // Prints a value to a string using the universal value printer. This
235 // is a helper for testing UniversalPrinter<T>::Print() for various types.
236 template <typename T>
237 string Print(const T& value) {
238  ::std::stringstream ss;
239  UniversalPrinter<T>::Print(value, &ss);
240  return ss.str();
241 }
242 
243 // Prints a value passed by reference to a string, using the universal
244 // value printer. This is a helper for testing
245 // UniversalPrinter<T&>::Print() for various types.
246 template <typename T>
247 string PrintByRef(const T& value) {
248  ::std::stringstream ss;
249  UniversalPrinter<T&>::Print(value, &ss);
250  return ss.str();
251 }
252 
253 // Tests printing various enum types.
254 
255 TEST(PrintEnumTest, AnonymousEnum) {
256  EXPECT_EQ("-1", Print(kAE1));
257  EXPECT_EQ("1", Print(kAE2));
258 }
259 
260 TEST(PrintEnumTest, EnumWithoutPrinter) {
261  EXPECT_EQ("-2", Print(kEWP1));
262  EXPECT_EQ("42", Print(kEWP2));
263 }
264 
265 TEST(PrintEnumTest, EnumWithStreaming) {
266  EXPECT_EQ("kEWS1", Print(kEWS1));
267  EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
268 }
269 
270 TEST(PrintEnumTest, EnumWithPrintTo) {
271  EXPECT_EQ("kEWPT1", Print(kEWPT1));
272  EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
273 }
274 
275 // Tests printing a class implicitly convertible to BiggestInt.
276 
277 TEST(PrintClassTest, BiggestIntConvertible) {
279 }
280 
281 // Tests printing various char types.
282 
283 // char.
284 TEST(PrintCharTest, PlainChar) {
285  EXPECT_EQ("'\\0'", Print('\0'));
286  EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
287  EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
288  EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
289  EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
290  EXPECT_EQ("'\\a' (7)", Print('\a'));
291  EXPECT_EQ("'\\b' (8)", Print('\b'));
292  EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
293  EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
294  EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
295  EXPECT_EQ("'\\t' (9)", Print('\t'));
296  EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
297  EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
298  EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
299  EXPECT_EQ("' ' (32, 0x20)", Print(' '));
300  EXPECT_EQ("'a' (97, 0x61)", Print('a'));
301 }
302 
303 // signed char.
304 TEST(PrintCharTest, SignedChar) {
305  EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
306  EXPECT_EQ("'\\xCE' (-50)",
307  Print(static_cast<signed char>(-50)));
308 }
309 
310 // unsigned char.
311 TEST(PrintCharTest, UnsignedChar) {
312  EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
313  EXPECT_EQ("'b' (98, 0x62)",
314  Print(static_cast<unsigned char>('b')));
315 }
316 
317 // Tests printing other simple, built-in types.
318 
319 // bool.
320 TEST(PrintBuiltInTypeTest, Bool) {
321  EXPECT_EQ("false", Print(false));
322  EXPECT_EQ("true", Print(true));
323 }
324 
325 // wchar_t.
326 TEST(PrintBuiltInTypeTest, Wchar_t) {
327  EXPECT_EQ("L'\\0'", Print(L'\0'));
328  EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
329  EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
330  EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
331  EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
332  EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
333  EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
334  EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
335  EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
336  EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
337  EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
338  EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
339  EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
340  EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
341  EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
342  EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
343  EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
344  EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
345 }
346 
347 // Test that Int64 provides more storage than wchar_t.
348 TEST(PrintTypeSizeTest, Wchar_t) {
349  EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
350 }
351 
352 // Various integer types.
353 TEST(PrintBuiltInTypeTest, Integer) {
354  EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8
355  EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8
356  EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16
357  EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16
358  EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32
359  EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32
360  EXPECT_EQ("18446744073709551615",
361  Print(static_cast<testing::internal::UInt64>(-1))); // uint64
362  EXPECT_EQ("-9223372036854775808",
363  Print(static_cast<testing::internal::Int64>(1) << 63)); // int64
364 }
365 
366 // Size types.
367 TEST(PrintBuiltInTypeTest, Size_t) {
368  EXPECT_EQ("1", Print(sizeof('a'))); // size_t.
369 #if !GTEST_OS_WINDOWS
370  // Windows has no ssize_t type.
371  EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t.
372 #endif // !GTEST_OS_WINDOWS
373 }
374 
375 // Floating-points.
376 TEST(PrintBuiltInTypeTest, FloatingPoints) {
377  EXPECT_EQ("1.5", Print(1.5f)); // float
378  EXPECT_EQ("-2.5", Print(-2.5)); // double
379 }
380 
381 // Since ::std::stringstream::operator<<(const void *) formats the pointer
382 // output differently with different compilers, we have to create the expected
383 // output first and use it as our expectation.
384 static string PrintPointer(const void *p) {
385  ::std::stringstream expected_result_stream;
386  expected_result_stream << p;
387  return expected_result_stream.str();
388 }
389 
390 // Tests printing C strings.
391 
392 // const char*.
393 TEST(PrintCStringTest, Const) {
394  const char* p = "World";
395  EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
396 }
397 
398 // char*.
399 TEST(PrintCStringTest, NonConst) {
400  char p[] = "Hi";
401  EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
402  Print(static_cast<char*>(p)));
403 }
404 
405 // NULL C string.
406 TEST(PrintCStringTest, Null) {
407  const char* p = NULL;
408  EXPECT_EQ("NULL", Print(p));
409 }
410 
411 // Tests that C strings are escaped properly.
412 TEST(PrintCStringTest, EscapesProperly) {
413  const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
414  EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
415  "\\n\\r\\t\\v\\x7F\\xFF a\"",
416  Print(p));
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*.
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*.
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.
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.
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*.
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*.
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*.
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*.
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*.
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*.
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*.
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.
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 
526 void MyFunction(int /* n */) {}
527 
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>
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:
562  virtual ~Foo() {}
563  int MyMethod(char x) { return x + 1; }
564  virtual char MyVirtualMethod(int /* n */) { return 'a'; }
565 
566  int value;
567 };
568 
569 TEST(PrintPointerTest, MemberVariablePointer) {
571  Print(sizeof(&Foo::value)) + "-byte object "));
572  int (Foo::*p) = NULL; // NOLINT
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.
581 TEST(PrintPointerTest, MemberFunctionPointer) {
583  Print(sizeof(&Foo::MyMethod)) + "-byte object "));
584  EXPECT_TRUE(
586  Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
587  int (Foo::*p)(char) = NULL; // NOLINT
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>
597 string PrintArrayHelper(T (&a)[N]) {
598  return Print(a);
599 }
600 
601 // One-dimensional array.
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.
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.
617 TEST(PrintArrayTest, ConstArray) {
618  const bool a[1] = { false };
619  EXPECT_EQ("{ false }", PrintArrayHelper(a));
620 }
621 
622 // char array without terminating NUL.
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.
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.
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.
643 TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
644  const wchar_t a[] = L"\0Hi";
645  EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a));
646 }
647 
648 // Array of objects.
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.
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.
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.
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 
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.
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.
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 
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 
736 
737 template <typename Char, typename CharTraits>
738 std::basic_ostream<Char, CharTraits>& operator<<(
739  std::basic_ostream<Char, CharTraits>& os,
740  const AllowsGenericStreaming& /* a */) {
741  return os << "AllowsGenericStreaming";
742 }
743 
744 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
746  EXPECT_EQ("AllowsGenericStreaming", Print(a));
747 }
748 
749 // Tests printing a template type that supports generic streaming.
750 
751 template <typename T>
753 
754 template <typename Char, typename CharTraits, typename 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 
761 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
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>
771  public:
772  operator bool() const { return false; }
773 };
774 
775 template <typename Char, typename CharTraits, typename T>
776 std::basic_ostream<Char, CharTraits>& operator<<(
777  std::basic_ostream<Char, CharTraits>& os,
779  return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
780 }
781 
782 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
784  EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
785 }
786 
787 #if GTEST_HAS_STRING_PIECE_
788 
789 // Tests printing StringPiece.
790 
791 TEST(PrintStringPieceTest, SimpleStringPiece) {
792  const StringPiece sp = "Hello";
793  EXPECT_EQ("\"Hello\"", Print(sp));
794 }
795 
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 
806 TEST(PrintStlContainerTest, EmptyDeque) {
807  deque<char> empty;
808  EXPECT_EQ("{}", Print(empty));
809 }
810 
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 
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 
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 
842 TEST(PrintStlContainerTest, HashSet) {
843  hash_set<string> set1;
844  set1.insert("hello");
845  EXPECT_EQ("{ \"hello\" }", Print(set1));
846 }
847 
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 
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 
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 
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 
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 
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 
922 #if GTEST_HAS_STD_FORWARD_LIST_
923 // <slist> is available on Linux in the google3 mode, but not on
924 // Windows or Mac OS X.
925 
926 TEST(PrintStlContainerTest, SinglyLinkedList) {
927  int a[] = { 9, 2, 8 };
928  const std::forward_list<int> ints(a, a + 3);
929  EXPECT_EQ("{ 9, 2, 8 }", Print(ints));
930 }
931 #endif // GTEST_HAS_STD_FORWARD_LIST_
932 
933 TEST(PrintStlContainerTest, Pair) {
934  pair<const bool, int> p(true, 5);
935  EXPECT_EQ("(true, 5)", Print(p));
936 }
937 
938 TEST(PrintStlContainerTest, Vector) {
939  vector<int> v;
940  v.push_back(1);
941  v.push_back(2);
942  EXPECT_EQ("{ 1, 2 }", Print(v));
943 }
944 
945 TEST(PrintStlContainerTest, LongSequence) {
946  const int a[100] = { 1, 2, 3 };
947  const vector<int> v(a, a + 100);
948  EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
949  "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
950 }
951 
952 TEST(PrintStlContainerTest, NestedContainer) {
953  const int a1[] = { 1, 2 };
954  const int a2[] = { 3, 4, 5 };
955  const list<int> l1(a1, a1 + 2);
956  const list<int> l2(a2, a2 + 3);
957 
958  vector<list<int> > v;
959  v.push_back(l1);
960  v.push_back(l2);
961  EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
962 }
963 
964 TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
965  const int a[3] = { 1, 2, 3 };
966  NativeArray<int> b(a, 3, RelationToSourceReference());
967  EXPECT_EQ("{ 1, 2, 3 }", Print(b));
968 }
969 
970 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
971  const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
972  NativeArray<int[3]> b(a, 2, RelationToSourceReference());
973  EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
974 }
975 
976 // Tests that a class named iterator isn't treated as a container.
977 
978 struct iterator {
979  char x;
980 };
981 
982 TEST(PrintStlContainerTest, Iterator) {
983  iterator it = {};
984  EXPECT_EQ("1-byte object <00>", Print(it));
985 }
986 
987 // Tests that a class named const_iterator isn't treated as a container.
988 
990  char x;
991 };
992 
993 TEST(PrintStlContainerTest, ConstIterator) {
994  const_iterator it = {};
995  EXPECT_EQ("1-byte object <00>", Print(it));
996 }
997 
998 #if GTEST_HAS_TR1_TUPLE
999 // Tests printing ::std::tr1::tuples.
1000 
1001 // Tuples of various arities.
1002 TEST(PrintTr1TupleTest, VariousSizes) {
1004  EXPECT_EQ("()", Print(t0));
1005 
1007  EXPECT_EQ("(5)", Print(t1));
1008 
1009  ::std::tr1::tuple<char, bool> t2('a', true);
1010  EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
1011 
1013  EXPECT_EQ("(false, 2, 3)", Print(t3));
1014 
1016  EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
1017 
1018  ::std::tr1::tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
1019  EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
1020 
1021  ::std::tr1::tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
1022  EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
1023 
1025  false, 2, 3, 4, true, 6, 7);
1026  EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
1027 
1029  false, 2, 3, 4, true, 6, 7, true);
1030  EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
1031 
1033  false, 2, 3, 4, true, 6, 7, true, 9);
1034  EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
1035 
1036  const char* const str = "8";
1037  // VC++ 2010's implementation of tuple of C++0x is deficient, requiring
1038  // an explicit type cast of NULL to be used.
1039  ::std::tr1::tuple<bool, char, short, testing::internal::Int32, // NOLINT
1040  testing::internal::Int64, float, double, const char*, void*, string>
1041  t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str,
1042  ImplicitCast_<void*>(NULL), "10");
1043  EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1044  " pointing to \"8\", NULL, \"10\")",
1045  Print(t10));
1046 }
1047 
1048 // Nested tuples.
1049 TEST(PrintTr1TupleTest, NestedTuple) {
1051  ::std::tr1::make_tuple(5, true), 'a');
1052  EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
1053 }
1054 
1055 #endif // GTEST_HAS_TR1_TUPLE
1056 
1057 #if GTEST_HAS_STD_TUPLE_
1058 // Tests printing ::std::tuples.
1059 
1060 // Tuples of various arities.
1061 TEST(PrintStdTupleTest, VariousSizes) {
1062  ::std::tuple<> t0;
1063  EXPECT_EQ("()", Print(t0));
1064 
1065  ::std::tuple<int> t1(5);
1066  EXPECT_EQ("(5)", Print(t1));
1067 
1068  ::std::tuple<char, bool> t2('a', true);
1069  EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
1070 
1071  ::std::tuple<bool, int, int> t3(false, 2, 3);
1072  EXPECT_EQ("(false, 2, 3)", Print(t3));
1073 
1074  ::std::tuple<bool, int, int, int> t4(false, 2, 3, 4);
1075  EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
1076 
1077  ::std::tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
1078  EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
1079 
1080  ::std::tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
1081  EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
1082 
1083  ::std::tuple<bool, int, int, int, bool, int, int> t7(
1084  false, 2, 3, 4, true, 6, 7);
1085  EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
1086 
1087  ::std::tuple<bool, int, int, int, bool, int, int, bool> t8(
1088  false, 2, 3, 4, true, 6, 7, true);
1089  EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
1090 
1091  ::std::tuple<bool, int, int, int, bool, int, int, bool, int> t9(
1092  false, 2, 3, 4, true, 6, 7, true, 9);
1093  EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
1094 
1095  const char* const str = "8";
1096  // VC++ 2010's implementation of tuple of C++0x is deficient, requiring
1097  // an explicit type cast of NULL to be used.
1098  ::std::tuple<bool, char, short, testing::internal::Int32, // NOLINT
1099  testing::internal::Int64, float, double, const char*, void*, string>
1100  t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str,
1101  ImplicitCast_<void*>(NULL), "10");
1102  EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1103  " pointing to \"8\", NULL, \"10\")",
1104  Print(t10));
1105 }
1106 
1107 // Nested tuples.
1108 TEST(PrintStdTupleTest, NestedTuple) {
1109  ::std::tuple< ::std::tuple<int, bool>, char> nested(
1110  ::std::make_tuple(5, true), 'a');
1111  EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
1112 }
1113 
1114 #endif // GTEST_LANG_CXX11
1115 
1116 // Tests printing user-defined unprintable types.
1117 
1118 // Unprintable types in the global namespace.
1119 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
1120  EXPECT_EQ("1-byte object <00>",
1122 }
1123 
1124 // Unprintable types in a user namespace.
1125 TEST(PrintUnprintableTypeTest, InUserNamespace) {
1126  EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1128 }
1129 
1130 // Unprintable types are that too big to be printed completely.
1131 
1132 struct Big {
1133  Big() { memset(array, 0, sizeof(array)); }
1134  char array[257];
1135 };
1136 
1137 TEST(PrintUnpritableTypeTest, BigObject) {
1138  EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
1139  "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1140  "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1141  "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
1142  "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1143  "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1144  "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
1145  Print(Big()));
1146 }
1147 
1148 // Tests printing user-defined streamable types.
1149 
1150 // Streamable types in the global namespace.
1151 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
1153  EXPECT_EQ("StreamableInGlobal", Print(x));
1154  EXPECT_EQ("StreamableInGlobal*", Print(&x));
1155 }
1156 
1157 // Printable template types in a user namespace.
1158 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
1159  EXPECT_EQ("StreamableTemplateInFoo: 0",
1161 }
1162 
1163 // Tests printing user-defined types that have a PrintTo() function.
1164 TEST(PrintPrintableTypeTest, InUserNamespace) {
1165  EXPECT_EQ("PrintableViaPrintTo: 0",
1167 }
1168 
1169 // Tests printing a pointer to a user-defined type that has a <<
1170 // operator for its pointer.
1171 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
1173  EXPECT_EQ("PointerPrintable*", Print(&x));
1174 }
1175 
1176 // Tests printing user-defined class template that have a PrintTo() function.
1177 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
1178  EXPECT_EQ("PrintableViaPrintToTemplate: 5",
1180 }
1181 
1182 // Tests that the universal printer prints both the address and the
1183 // value of a reference.
1184 TEST(PrintReferenceTest, PrintsAddressAndValue) {
1185  int n = 5;
1186  EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
1187 
1188  int a[2][3] = {
1189  { 0, 1, 2 },
1190  { 3, 4, 5 }
1191  };
1192  EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
1193  PrintByRef(a));
1194 
1195  const ::foo::UnprintableInFoo x;
1196  EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
1197  "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1198  PrintByRef(x));
1199 }
1200 
1201 // Tests that the universal printer prints a function pointer passed by
1202 // reference.
1203 TEST(PrintReferenceTest, HandlesFunctionPointer) {
1204  void (*fp)(int n) = &MyFunction;
1205  const string fp_pointer_string =
1206  PrintPointer(reinterpret_cast<const void*>(&fp));
1207  // We cannot directly cast &MyFunction to const void* because the
1208  // standard disallows casting between pointers to functions and
1209  // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
1210  // this limitation.
1211  const string fp_string = PrintPointer(reinterpret_cast<const void*>(
1212  reinterpret_cast<internal::BiggestInt>(fp)));
1213  EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
1214  PrintByRef(fp));
1215 }
1216 
1217 // Tests that the universal printer prints a member function pointer
1218 // passed by reference.
1219 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
1220  int (Foo::*p)(char ch) = &Foo::MyMethod;
1222  PrintByRef(p),
1223  "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
1224  Print(sizeof(p)) + "-byte object "));
1225 
1226  char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
1228  PrintByRef(p2),
1229  "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
1230  Print(sizeof(p2)) + "-byte object "));
1231 }
1232 
1233 // Tests that the universal printer prints a member variable pointer
1234 // passed by reference.
1235 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
1236  int (Foo::*p) = &Foo::value; // NOLINT
1238  PrintByRef(p),
1239  "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
1240 }
1241 
1242 // Tests that FormatForComparisonFailureMessage(), which is used to print
1243 // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion
1244 // fails, formats the operand in the desired way.
1245 
1246 // scalar
1247 TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
1248  EXPECT_STREQ("123",
1249  FormatForComparisonFailureMessage(123, 124).c_str());
1250 }
1251 
1252 // non-char pointer
1253 TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
1254  int n = 0;
1255  EXPECT_EQ(PrintPointer(&n),
1256  FormatForComparisonFailureMessage(&n, &n).c_str());
1257 }
1258 
1259 // non-char array
1260 TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
1261  // In expression 'array == x', 'array' is compared by pointer.
1262  // Therefore we want to print an array operand as a pointer.
1263  int n[] = { 1, 2, 3 };
1265  FormatForComparisonFailureMessage(n, n).c_str());
1266 }
1267 
1268 // Tests formatting a char pointer when it's compared with another pointer.
1269 // In this case we want to print it as a raw pointer, as the comparision is by
1270 // pointer.
1271 
1272 // char pointer vs pointer
1273 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
1274  // In expression 'p == x', where 'p' and 'x' are (const or not) char
1275  // pointers, the operands are compared by pointer. Therefore we
1276  // want to print 'p' as a pointer instead of a C string (we don't
1277  // even know if it's supposed to point to a valid C string).
1278 
1279  // const char*
1280  const char* s = "hello";
1282  FormatForComparisonFailureMessage(s, s).c_str());
1283 
1284  // char*
1285  char ch = 'a';
1286  EXPECT_EQ(PrintPointer(&ch),
1287  FormatForComparisonFailureMessage(&ch, &ch).c_str());
1288 }
1289 
1290 // wchar_t pointer vs pointer
1291 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
1292  // In expression 'p == x', where 'p' and 'x' are (const or not) char
1293  // pointers, the operands are compared by pointer. Therefore we
1294  // want to print 'p' as a pointer instead of a wide C string (we don't
1295  // even know if it's supposed to point to a valid wide C string).
1296 
1297  // const wchar_t*
1298  const wchar_t* s = L"hello";
1300  FormatForComparisonFailureMessage(s, s).c_str());
1301 
1302  // wchar_t*
1303  wchar_t ch = L'a';
1304  EXPECT_EQ(PrintPointer(&ch),
1305  FormatForComparisonFailureMessage(&ch, &ch).c_str());
1306 }
1307 
1308 // Tests formatting a char pointer when it's compared to a string object.
1309 // In this case we want to print the char pointer as a C string.
1310 
1311 #if GTEST_HAS_GLOBAL_STRING
1312 // char pointer vs ::string
1313 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) {
1314  const char* s = "hello \"world";
1315  EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1316  FormatForComparisonFailureMessage(s, ::string()).c_str());
1317 
1318  // char*
1319  char str[] = "hi\1";
1320  char* p = str;
1321  EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1322  FormatForComparisonFailureMessage(p, ::string()).c_str());
1323 }
1324 #endif
1325 
1326 // char pointer vs std::string
1327 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
1328  const char* s = "hello \"world";
1329  EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1331 
1332  // char*
1333  char str[] = "hi\1";
1334  char* p = str;
1335  EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1337 }
1338 
1339 #if GTEST_HAS_GLOBAL_WSTRING
1340 // wchar_t pointer vs ::wstring
1341 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) {
1342  const wchar_t* s = L"hi \"world";
1343  EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1344  FormatForComparisonFailureMessage(s, ::wstring()).c_str());
1345 
1346  // wchar_t*
1347  wchar_t str[] = L"hi\1";
1348  wchar_t* p = str;
1349  EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1350  FormatForComparisonFailureMessage(p, ::wstring()).c_str());
1351 }
1352 #endif
1353 
1354 #if GTEST_HAS_STD_WSTRING
1355 // wchar_t pointer vs std::wstring
1356 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
1357  const wchar_t* s = L"hi \"world";
1358  EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1360 
1361  // wchar_t*
1362  wchar_t str[] = L"hi\1";
1363  wchar_t* p = str;
1364  EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1366 }
1367 #endif
1368 
1369 // Tests formatting a char array when it's compared with a pointer or array.
1370 // In this case we want to print the array as a row pointer, as the comparison
1371 // is by pointer.
1372 
1373 // char array vs pointer
1374 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
1375  char str[] = "hi \"world\"";
1376  char* p = NULL;
1377  EXPECT_EQ(PrintPointer(str),
1378  FormatForComparisonFailureMessage(str, p).c_str());
1379 }
1380 
1381 // char array vs char array
1382 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
1383  const char str[] = "hi \"world\"";
1384  EXPECT_EQ(PrintPointer(str),
1385  FormatForComparisonFailureMessage(str, str).c_str());
1386 }
1387 
1388 // wchar_t array vs pointer
1389 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
1390  wchar_t str[] = L"hi \"world\"";
1391  wchar_t* p = NULL;
1392  EXPECT_EQ(PrintPointer(str),
1393  FormatForComparisonFailureMessage(str, p).c_str());
1394 }
1395 
1396 // wchar_t array vs wchar_t array
1397 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
1398  const wchar_t str[] = L"hi \"world\"";
1399  EXPECT_EQ(PrintPointer(str),
1400  FormatForComparisonFailureMessage(str, str).c_str());
1401 }
1402 
1403 // Tests formatting a char array when it's compared with a string object.
1404 // In this case we want to print the array as a C string.
1405 
1406 #if GTEST_HAS_GLOBAL_STRING
1407 // char array vs string
1408 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) {
1409  const char str[] = "hi \"w\0rld\"";
1410  EXPECT_STREQ("\"hi \\\"w\"", // The content should be escaped.
1411  // Embedded NUL terminates the string.
1412  FormatForComparisonFailureMessage(str, ::string()).c_str());
1413 }
1414 #endif
1415 
1416 // char array vs std::string
1417 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
1418  const char str[] = "hi \"world\"";
1419  EXPECT_STREQ("\"hi \\\"world\\\"\"", // The content should be escaped.
1421 }
1422 
1423 #if GTEST_HAS_GLOBAL_WSTRING
1424 // wchar_t array vs wstring
1425 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) {
1426  const wchar_t str[] = L"hi \"world\"";
1427  EXPECT_STREQ("L\"hi \\\"world\\\"\"", // The content should be escaped.
1428  FormatForComparisonFailureMessage(str, ::wstring()).c_str());
1429 }
1430 #endif
1431 
1432 #if GTEST_HAS_STD_WSTRING
1433 // wchar_t array vs std::wstring
1434 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
1435  const wchar_t str[] = L"hi \"w\0rld\"";
1436  EXPECT_STREQ(
1437  "L\"hi \\\"w\"", // The content should be escaped.
1438  // Embedded NUL terminates the string.
1440 }
1441 #endif
1442 
1443 // Useful for testing PrintToString(). We cannot use EXPECT_EQ()
1444 // there as its implementation uses PrintToString(). The caller must
1445 // ensure that 'value' has no side effect.
1446 #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
1447  EXPECT_TRUE(PrintToString(value) == (expected_string)) \
1448  << " where " #value " prints as " << (PrintToString(value))
1449 
1450 TEST(PrintToStringTest, WorksForScalar) {
1451  EXPECT_PRINT_TO_STRING_(123, "123");
1452 }
1453 
1454 TEST(PrintToStringTest, WorksForPointerToConstChar) {
1455  const char* p = "hello";
1456  EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
1457 }
1458 
1459 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
1460  char s[] = "hello";
1461  char* p = s;
1462  EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
1463 }
1464 
1465 TEST(PrintToStringTest, EscapesForPointerToConstChar) {
1466  const char* p = "hello\n";
1467  EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\"");
1468 }
1469 
1470 TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
1471  char s[] = "hello\1";
1472  char* p = s;
1473  EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\"");
1474 }
1475 
1476 TEST(PrintToStringTest, WorksForArray) {
1477  int n[3] = { 1, 2, 3 };
1478  EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
1479 }
1480 
1481 TEST(PrintToStringTest, WorksForCharArray) {
1482  char s[] = "hello";
1483  EXPECT_PRINT_TO_STRING_(s, "\"hello\"");
1484 }
1485 
1486 TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
1487  const char str_with_nul[] = "hello\0 world";
1488  EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\"");
1489 
1490  char mutable_str_with_nul[] = "hello\0 world";
1491  EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\"");
1492 }
1493 
1494 #undef EXPECT_PRINT_TO_STRING_
1495 
1496 TEST(UniversalTersePrintTest, WorksForNonReference) {
1497  ::std::stringstream ss;
1498  UniversalTersePrint(123, &ss);
1499  EXPECT_EQ("123", ss.str());
1500 }
1501 
1502 TEST(UniversalTersePrintTest, WorksForReference) {
1503  const int& n = 123;
1504  ::std::stringstream ss;
1505  UniversalTersePrint(n, &ss);
1506  EXPECT_EQ("123", ss.str());
1507 }
1508 
1509 TEST(UniversalTersePrintTest, WorksForCString) {
1510  const char* s1 = "abc";
1511  ::std::stringstream ss1;
1512  UniversalTersePrint(s1, &ss1);
1513  EXPECT_EQ("\"abc\"", ss1.str());
1514 
1515  char* s2 = const_cast<char*>(s1);
1516  ::std::stringstream ss2;
1517  UniversalTersePrint(s2, &ss2);
1518  EXPECT_EQ("\"abc\"", ss2.str());
1519 
1520  const char* s3 = NULL;
1521  ::std::stringstream ss3;
1522  UniversalTersePrint(s3, &ss3);
1523  EXPECT_EQ("NULL", ss3.str());
1524 }
1525 
1526 TEST(UniversalPrintTest, WorksForNonReference) {
1527  ::std::stringstream ss;
1528  UniversalPrint(123, &ss);
1529  EXPECT_EQ("123", ss.str());
1530 }
1531 
1532 TEST(UniversalPrintTest, WorksForReference) {
1533  const int& n = 123;
1534  ::std::stringstream ss;
1535  UniversalPrint(n, &ss);
1536  EXPECT_EQ("123", ss.str());
1537 }
1538 
1539 TEST(UniversalPrintTest, WorksForCString) {
1540  const char* s1 = "abc";
1541  ::std::stringstream ss1;
1542  UniversalPrint(s1, &ss1);
1543  EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str()));
1544 
1545  char* s2 = const_cast<char*>(s1);
1546  ::std::stringstream ss2;
1547  UniversalPrint(s2, &ss2);
1548  EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str()));
1549 
1550  const char* s3 = NULL;
1551  ::std::stringstream ss3;
1552  UniversalPrint(s3, &ss3);
1553  EXPECT_EQ("NULL", ss3.str());
1554 }
1555 
1556 TEST(UniversalPrintTest, WorksForCharArray) {
1557  const char str[] = "\"Line\0 1\"\nLine 2";
1558  ::std::stringstream ss1;
1559  UniversalPrint(str, &ss1);
1560  EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
1561 
1562  const char mutable_str[] = "\"Line\0 1\"\nLine 2";
1563  ::std::stringstream ss2;
1564  UniversalPrint(mutable_str, &ss2);
1565  EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
1566 }
1567 
1568 #if GTEST_HAS_TR1_TUPLE
1569 
1570 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsEmptyTuple) {
1571  Strings result = UniversalTersePrintTupleFieldsToStrings(
1572  ::std::tr1::make_tuple());
1573  EXPECT_EQ(0u, result.size());
1574 }
1575 
1576 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsOneTuple) {
1577  Strings result = UniversalTersePrintTupleFieldsToStrings(
1578  ::std::tr1::make_tuple(1));
1579  ASSERT_EQ(1u, result.size());
1580  EXPECT_EQ("1", result[0]);
1581 }
1582 
1583 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTwoTuple) {
1584  Strings result = UniversalTersePrintTupleFieldsToStrings(
1585  ::std::tr1::make_tuple(1, 'a'));
1586  ASSERT_EQ(2u, result.size());
1587  EXPECT_EQ("1", result[0]);
1588  EXPECT_EQ("'a' (97, 0x61)", result[1]);
1589 }
1590 
1591 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTersely) {
1592  const int n = 1;
1593  Strings result = UniversalTersePrintTupleFieldsToStrings(
1595  ASSERT_EQ(2u, result.size());
1596  EXPECT_EQ("1", result[0]);
1597  EXPECT_EQ("\"a\"", result[1]);
1598 }
1599 
1600 #endif // GTEST_HAS_TR1_TUPLE
1601 
1602 #if GTEST_HAS_STD_TUPLE_
1603 
1604 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
1605  Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
1606  EXPECT_EQ(0u, result.size());
1607 }
1608 
1609 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
1610  Strings result = UniversalTersePrintTupleFieldsToStrings(
1611  ::std::make_tuple(1));
1612  ASSERT_EQ(1u, result.size());
1613  EXPECT_EQ("1", result[0]);
1614 }
1615 
1616 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
1617  Strings result = UniversalTersePrintTupleFieldsToStrings(
1618  ::std::make_tuple(1, 'a'));
1619  ASSERT_EQ(2u, result.size());
1620  EXPECT_EQ("1", result[0]);
1621  EXPECT_EQ("'a' (97, 0x61)", result[1]);
1622 }
1623 
1624 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
1625  const int n = 1;
1626  Strings result = UniversalTersePrintTupleFieldsToStrings(
1627  ::std::tuple<const int&, const char*>(n, "a"));
1628  ASSERT_EQ(2u, result.size());
1629  EXPECT_EQ("1", result[0]);
1630  EXPECT_EQ("\"a\"", result[1]);
1631 }
1632 
1633 #endif // GTEST_HAS_STD_TUPLE_
1634 
1635 } // namespace gtest_printers_test
1636 } // namespace testing
1637