Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
googletest-printers-test.cc
Go to the documentation of this file.
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 
31 // Google Test - The Google C++ Testing and Mocking Framework
32 //
33 // This file tests the universal value printer.
34 
35 #include <ctype.h>
36 #include <string.h>
37 #include <algorithm>
38 #include <cstdint>
39 #include <deque>
40 #include <forward_list>
41 #include <limits>
42 #include <list>
43 #include <map>
44 #include <set>
45 #include <sstream>
46 #include <string>
47 #include <unordered_map>
48 #include <unordered_set>
49 #include <utility>
50 #include <vector>
51 
52 #include "gtest/gtest-printers.h"
53 #include "gtest/gtest.h"
54 
55 // Some user-defined types for testing the universal value printer.
56 
57 // An anonymous enum type.
59  kAE1 = -1,
60  kAE2 = 1
61 };
62 
63 // An enum without a user-defined printer.
65  kEWP1 = -2,
66  kEWP2 = 42
67 };
68 
69 // An enum with a << operator.
71  kEWS1 = 10
72 };
73 
74 std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) {
75  return os << (e == kEWS1 ? "kEWS1" : "invalid");
76 }
77 
78 // An enum with a PrintTo() function.
80  kEWPT1 = 1
81 };
82 
83 void PrintTo(EnumWithPrintTo e, std::ostream* os) {
84  *os << (e == kEWPT1 ? "kEWPT1" : "invalid");
85 }
86 
87 // A class implicitly convertible to BiggestInt.
89  public:
91 };
92 
93 // A user-defined unprintable class template in the global namespace.
94 template <typename T>
96  public:
98  private:
100 };
101 
102 // A user-defined streamable type in the global namespace.
104  public:
105  virtual ~StreamableInGlobal() {}
106 };
107 
108 inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) {
109  os << "StreamableInGlobal";
110 }
111 
112 void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) {
113  os << "StreamableInGlobal*";
114 }
115 
116 namespace foo {
117 
118 // A user-defined unprintable type in a user namespace.
120  public:
121  UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); }
122  double z() const { return z_; }
123  private:
124  char xy_[8];
125  double z_;
126 };
127 
128 // A user-defined printable type in a user-chosen namespace.
131  int value;
132 };
133 
134 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
135  *os << "PrintableViaPrintTo: " << x.value;
136 }
137 
138 // A type with a user-defined << for printing its pointer.
140 };
141 
142 ::std::ostream& operator<<(::std::ostream& os,
143  const PointerPrintable* /* x */) {
144  return os << "PointerPrintable*";
145 }
146 
147 // A user-defined printable class template in a user-chosen namespace.
148 template <typename T>
150  public:
151  explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
152 
153  const T& value() const { return value_; }
154  private:
156 };
157 
158 template <typename T>
159 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
160  *os << "PrintableViaPrintToTemplate: " << x.value();
161 }
162 
163 // A user-defined streamable class template in a user namespace.
164 template <typename T>
166  public:
168 
169  const T& value() const { return value_; }
170  private:
172 };
173 
174 template <typename T>
175 inline ::std::ostream& operator<<(::std::ostream& os,
176  const StreamableTemplateInFoo<T>& x) {
177  return os << "StreamableTemplateInFoo: " << x.value();
178 }
179 
180 // A user-defined streamable but recursivly-defined container type in
181 // a user namespace, it mimics therefore std::filesystem::path or
182 // boost::filesystem::path.
183 class PathLike {
184  public:
185  struct iterator {
187 
188  iterator& operator++();
189  PathLike& operator*();
190  };
191 
192  using value_type = char;
194 
195  PathLike() {}
196 
197  iterator begin() const { return iterator(); }
198  iterator end() const { return iterator(); }
199 
200  friend ::std::ostream& operator<<(::std::ostream& os, const PathLike&) {
201  return os << "Streamable-PathLike";
202  }
203 };
204 
205 } // namespace foo
206 
207 namespace testing {
208 namespace gtest_printers_test {
209 
210 using ::std::deque;
211 using ::std::list;
212 using ::std::make_pair;
213 using ::std::map;
214 using ::std::multimap;
215 using ::std::multiset;
216 using ::std::pair;
217 using ::std::set;
218 using ::std::vector;
222 using ::testing::internal::NativeArray;
223 using ::testing::internal::RelationToSourceReference;
226 using ::testing::internal::UniversalPrinter;
229 
230 // Prints a value to a string using the universal value printer. This
231 // is a helper for testing UniversalPrinter<T>::Print() for various types.
232 template <typename T>
233 std::string Print(const T& value) {
234  ::std::stringstream ss;
235  UniversalPrinter<T>::Print(value, &ss);
236  return ss.str();
237 }
238 
239 // Prints a value passed by reference to a string, using the universal
240 // value printer. This is a helper for testing
241 // UniversalPrinter<T&>::Print() for various types.
242 template <typename T>
243 std::string PrintByRef(const T& value) {
244  ::std::stringstream ss;
245  UniversalPrinter<T&>::Print(value, &ss);
246  return ss.str();
247 }
248 
249 // Tests printing various enum types.
250 
251 TEST(PrintEnumTest, AnonymousEnum) {
252  EXPECT_EQ("-1", Print(kAE1));
253  EXPECT_EQ("1", Print(kAE2));
254 }
255 
256 TEST(PrintEnumTest, EnumWithoutPrinter) {
257  EXPECT_EQ("-2", Print(kEWP1));
258  EXPECT_EQ("42", Print(kEWP2));
259 }
260 
261 TEST(PrintEnumTest, EnumWithStreaming) {
262  EXPECT_EQ("kEWS1", Print(kEWS1));
263  EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
264 }
265 
266 TEST(PrintEnumTest, EnumWithPrintTo) {
267  EXPECT_EQ("kEWPT1", Print(kEWPT1));
268  EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
269 }
270 
271 // Tests printing a class implicitly convertible to BiggestInt.
272 
273 TEST(PrintClassTest, BiggestIntConvertible) {
275 }
276 
277 // Tests printing various char types.
278 
279 // char.
280 TEST(PrintCharTest, PlainChar) {
281  EXPECT_EQ("'\\0'", Print('\0'));
282  EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
283  EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
284  EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
285  EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
286  EXPECT_EQ("'\\a' (7)", Print('\a'));
287  EXPECT_EQ("'\\b' (8)", Print('\b'));
288  EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
289  EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
290  EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
291  EXPECT_EQ("'\\t' (9)", Print('\t'));
292  EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
293  EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
294  EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
295  EXPECT_EQ("' ' (32, 0x20)", Print(' '));
296  EXPECT_EQ("'a' (97, 0x61)", Print('a'));
297 }
298 
299 // signed char.
300 TEST(PrintCharTest, SignedChar) {
301  EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
302  EXPECT_EQ("'\\xCE' (-50)",
303  Print(static_cast<signed char>(-50)));
304 }
305 
306 // unsigned char.
307 TEST(PrintCharTest, UnsignedChar) {
308  EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
309  EXPECT_EQ("'b' (98, 0x62)",
310  Print(static_cast<unsigned char>('b')));
311 }
312 
313 TEST(PrintCharTest, Char16) {
314  EXPECT_EQ("U+0041", Print(u'A'));
315 }
316 
317 TEST(PrintCharTest, Char32) {
318  EXPECT_EQ("U+0041", Print(U'A'));
319 }
320 
321 #ifdef __cpp_char8_t
322 TEST(PrintCharTest, Char8) {
323  EXPECT_EQ("U+0041", Print(u8'A'));
324 }
325 #endif
326 
327 // Tests printing other simple, built-in types.
328 
329 // bool.
330 TEST(PrintBuiltInTypeTest, Bool) {
331  EXPECT_EQ("false", Print(false));
332  EXPECT_EQ("true", Print(true));
333 }
334 
335 // wchar_t.
336 TEST(PrintBuiltInTypeTest, Wchar_t) {
337  EXPECT_EQ("L'\\0'", Print(L'\0'));
338  EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
339  EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
340  EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
341  EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
342  EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
343  EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
344  EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
345  EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
346  EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
347  EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
348  EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
349  EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
350  EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
351  EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
352  EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
353  EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
354  EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
355 }
356 
357 // Test that int64_t provides more storage than wchar_t.
358 TEST(PrintTypeSizeTest, Wchar_t) {
359  EXPECT_LT(sizeof(wchar_t), sizeof(int64_t));
360 }
361 
362 // Various integer types.
363 TEST(PrintBuiltInTypeTest, Integer) {
364  EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8
365  EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8
366  EXPECT_EQ("65535", Print(std::numeric_limits<uint16_t>::max())); // uint16
367  EXPECT_EQ("-32768", Print(std::numeric_limits<int16_t>::min())); // int16
368  EXPECT_EQ("4294967295",
370  EXPECT_EQ("-2147483648",
372  EXPECT_EQ("18446744073709551615",
374  EXPECT_EQ("-9223372036854775808",
376 #ifdef __cpp_char8_t
377  EXPECT_EQ("U+0000",
379  EXPECT_EQ("U+00FF",
381 #endif
382  EXPECT_EQ("U+0000",
384  EXPECT_EQ("U+FFFF",
386  EXPECT_EQ("U+0000",
388  EXPECT_EQ("U+FFFFFFFF",
390 }
391 
392 // Size types.
393 TEST(PrintBuiltInTypeTest, Size_t) {
394  EXPECT_EQ("1", Print(sizeof('a'))); // size_t.
395 #if !GTEST_OS_WINDOWS
396  // Windows has no ssize_t type.
397  EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t.
398 #endif // !GTEST_OS_WINDOWS
399 }
400 
401 // Floating-points.
402 TEST(PrintBuiltInTypeTest, FloatingPoints) {
403  EXPECT_EQ("1.5", Print(1.5f)); // float
404  EXPECT_EQ("-2.5", Print(-2.5)); // double
405 }
406 
407 // Since ::std::stringstream::operator<<(const void *) formats the pointer
408 // output differently with different compilers, we have to create the expected
409 // output first and use it as our expectation.
410 static std::string PrintPointer(const void* p) {
411  ::std::stringstream expected_result_stream;
412  expected_result_stream << p;
413  return expected_result_stream.str();
414 }
415 
416 // Tests printing C strings.
417 
418 // const char*.
419 TEST(PrintCStringTest, Const) {
420  const char* p = "World";
421  EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
422 }
423 
424 // char*.
425 TEST(PrintCStringTest, NonConst) {
426  char p[] = "Hi";
427  EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
428  Print(static_cast<char*>(p)));
429 }
430 
431 // NULL C string.
432 TEST(PrintCStringTest, Null) {
433  const char* p = nullptr;
434  EXPECT_EQ("NULL", Print(p));
435 }
436 
437 // Tests that C strings are escaped properly.
438 TEST(PrintCStringTest, EscapesProperly) {
439  const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
440  EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
441  "\\n\\r\\t\\v\\x7F\\xFF a\"",
442  Print(p));
443 }
444 
445 // MSVC compiler can be configured to define whar_t as a typedef
446 // of unsigned short. Defining an overload for const wchar_t* in that case
447 // would cause pointers to unsigned shorts be printed as wide strings,
448 // possibly accessing more memory than intended and causing invalid
449 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
450 // wchar_t is implemented as a native type.
451 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
452 
453 // const wchar_t*.
454 TEST(PrintWideCStringTest, Const) {
455  const wchar_t* p = L"World";
456  EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
457 }
458 
459 // wchar_t*.
460 TEST(PrintWideCStringTest, NonConst) {
461  wchar_t p[] = L"Hi";
462  EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
463  Print(static_cast<wchar_t*>(p)));
464 }
465 
466 // NULL wide C string.
467 TEST(PrintWideCStringTest, Null) {
468  const wchar_t* p = nullptr;
469  EXPECT_EQ("NULL", Print(p));
470 }
471 
472 // Tests that wide C strings are escaped properly.
473 TEST(PrintWideCStringTest, EscapesProperly) {
474  const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r',
475  '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
476  EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f"
477  "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
478  Print(static_cast<const wchar_t*>(s)));
479 }
480 #endif // native wchar_t
481 
482 // Tests printing pointers to other char types.
483 
484 // signed char*.
485 TEST(PrintCharPointerTest, SignedChar) {
486  signed char* p = reinterpret_cast<signed char*>(0x1234);
487  EXPECT_EQ(PrintPointer(p), Print(p));
488  p = nullptr;
489  EXPECT_EQ("NULL", Print(p));
490 }
491 
492 // const signed char*.
493 TEST(PrintCharPointerTest, ConstSignedChar) {
494  signed char* p = reinterpret_cast<signed char*>(0x1234);
495  EXPECT_EQ(PrintPointer(p), Print(p));
496  p = nullptr;
497  EXPECT_EQ("NULL", Print(p));
498 }
499 
500 // unsigned char*.
501 TEST(PrintCharPointerTest, UnsignedChar) {
502  unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
503  EXPECT_EQ(PrintPointer(p), Print(p));
504  p = nullptr;
505  EXPECT_EQ("NULL", Print(p));
506 }
507 
508 // const unsigned char*.
509 TEST(PrintCharPointerTest, ConstUnsignedChar) {
510  const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
511  EXPECT_EQ(PrintPointer(p), Print(p));
512  p = nullptr;
513  EXPECT_EQ("NULL", Print(p));
514 }
515 
516 #ifdef __cpp_char8_t
517 // char8_t*
518 TEST(PrintCharPointerTest, Char8) {
519  char8_t* p = reinterpret_cast<char8_t*>(0x1234);
520  EXPECT_EQ(PrintPointer(p), Print(p));
521  p = nullptr;
522  EXPECT_EQ("NULL", Print(p));
523 }
524 
525 // const char8_t*
526 TEST(PrintCharPointerTest, ConstChar8) {
527  const char8_t* p = reinterpret_cast<const char8_t*>(0x1234);
528  EXPECT_EQ(PrintPointer(p), Print(p));
529  p = nullptr;
530  EXPECT_EQ("NULL", Print(p));
531 }
532 #endif
533 
534 // char16_t*
535 TEST(PrintCharPointerTest, Char16) {
536  char16_t* p = reinterpret_cast<char16_t*>(0x1234);
537  EXPECT_EQ(PrintPointer(p), Print(p));
538  p = nullptr;
539  EXPECT_EQ("NULL", Print(p));
540 }
541 
542 // const char16_t*
543 TEST(PrintCharPointerTest, ConstChar16) {
544  const char16_t* p = reinterpret_cast<const char16_t*>(0x1234);
545  EXPECT_EQ(PrintPointer(p), Print(p));
546  p = nullptr;
547  EXPECT_EQ("NULL", Print(p));
548 }
549 
550 // char32_t*
551 TEST(PrintCharPointerTest, Char32) {
552  char32_t* p = reinterpret_cast<char32_t*>(0x1234);
553  EXPECT_EQ(PrintPointer(p), Print(p));
554  p = nullptr;
555  EXPECT_EQ("NULL", Print(p));
556 }
557 
558 // const char32_t*
559 TEST(PrintCharPointerTest, ConstChar32) {
560  const char32_t* p = reinterpret_cast<const char32_t*>(0x1234);
561  EXPECT_EQ(PrintPointer(p), Print(p));
562  p = nullptr;
563  EXPECT_EQ("NULL", Print(p));
564 }
565 
566 // Tests printing pointers to simple, built-in types.
567 
568 // bool*.
569 TEST(PrintPointerToBuiltInTypeTest, Bool) {
570  bool* p = reinterpret_cast<bool*>(0xABCD);
571  EXPECT_EQ(PrintPointer(p), Print(p));
572  p = nullptr;
573  EXPECT_EQ("NULL", Print(p));
574 }
575 
576 // void*.
577 TEST(PrintPointerToBuiltInTypeTest, Void) {
578  void* p = reinterpret_cast<void*>(0xABCD);
579  EXPECT_EQ(PrintPointer(p), Print(p));
580  p = nullptr;
581  EXPECT_EQ("NULL", Print(p));
582 }
583 
584 // const void*.
585 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
586  const void* p = reinterpret_cast<const void*>(0xABCD);
587  EXPECT_EQ(PrintPointer(p), Print(p));
588  p = nullptr;
589  EXPECT_EQ("NULL", Print(p));
590 }
591 
592 // Tests printing pointers to pointers.
593 TEST(PrintPointerToPointerTest, IntPointerPointer) {
594  int** p = reinterpret_cast<int**>(0xABCD);
595  EXPECT_EQ(PrintPointer(p), Print(p));
596  p = nullptr;
597  EXPECT_EQ("NULL", Print(p));
598 }
599 
600 // Tests printing (non-member) function pointers.
601 
602 void MyFunction(int /* n */) {}
603 
604 TEST(PrintPointerTest, NonMemberFunctionPointer) {
605  // We cannot directly cast &MyFunction to const void* because the
606  // standard disallows casting between pointers to functions and
607  // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
608  // this limitation.
609  EXPECT_EQ(
610  PrintPointer(reinterpret_cast<const void*>(
611  reinterpret_cast<internal::BiggestInt>(&MyFunction))),
612  Print(&MyFunction));
613  int (*p)(bool) = NULL; // NOLINT
614  EXPECT_EQ("NULL", Print(p));
615 }
616 
617 // An assertion predicate determining whether a one string is a prefix for
618 // another.
619 template <typename StringType>
620 AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
621  if (str.find(prefix, 0) == 0)
622  return AssertionSuccess();
623 
624  const bool is_wide_string = sizeof(prefix[0]) > 1;
625  const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
626  return AssertionFailure()
627  << begin_string_quote << prefix << "\" is not a prefix of "
628  << begin_string_quote << str << "\"\n";
629 }
630 
631 // Tests printing member variable pointers. Although they are called
632 // pointers, they don't point to a location in the address space.
633 // Their representation is implementation-defined. Thus they will be
634 // printed as raw bytes.
635 
636 struct Foo {
637  public:
638  virtual ~Foo() {}
639  int MyMethod(char x) { return x + 1; }
640  virtual char MyVirtualMethod(int /* n */) { return 'a'; }
641 
642  int value;
643 };
644 
645 TEST(PrintPointerTest, MemberVariablePointer) {
647  Print(sizeof(&Foo::value)) + "-byte object "));
648  int Foo::*p = NULL; // NOLINT
650  Print(sizeof(p)) + "-byte object "));
651 }
652 
653 // Tests printing member function pointers. Although they are called
654 // pointers, they don't point to a location in the address space.
655 // Their representation is implementation-defined. Thus they will be
656 // printed as raw bytes.
657 TEST(PrintPointerTest, MemberFunctionPointer) {
659  Print(sizeof(&Foo::MyMethod)) + "-byte object "));
660  EXPECT_TRUE(
662  Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
663  int (Foo::*p)(char) = NULL; // NOLINT
665  Print(sizeof(p)) + "-byte object "));
666 }
667 
668 // Tests printing C arrays.
669 
670 // The difference between this and Print() is that it ensures that the
671 // argument is a reference to an array.
672 template <typename T, size_t N>
673 std::string PrintArrayHelper(T (&a)[N]) {
674  return Print(a);
675 }
676 
677 // One-dimensional array.
678 TEST(PrintArrayTest, OneDimensionalArray) {
679  int a[5] = { 1, 2, 3, 4, 5 };
680  EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
681 }
682 
683 // Two-dimensional array.
684 TEST(PrintArrayTest, TwoDimensionalArray) {
685  int a[2][5] = {
686  { 1, 2, 3, 4, 5 },
687  { 6, 7, 8, 9, 0 }
688  };
689  EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
690 }
691 
692 // Array of const elements.
693 TEST(PrintArrayTest, ConstArray) {
694  const bool a[1] = { false };
695  EXPECT_EQ("{ false }", PrintArrayHelper(a));
696 }
697 
698 // char array without terminating NUL.
699 TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
700  // Array a contains '\0' in the middle and doesn't end with '\0'.
701  char a[] = { 'H', '\0', 'i' };
702  EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
703 }
704 
705 // const char array with terminating NUL.
706 TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) {
707  const char a[] = "\0Hi";
708  EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a));
709 }
710 
711 // const wchar_t array without terminating NUL.
712 TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
713  // Array a contains '\0' in the middle and doesn't end with '\0'.
714  const wchar_t a[] = { L'H', L'\0', L'i' };
715  EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
716 }
717 
718 // wchar_t array with terminating NUL.
719 TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
720  const wchar_t a[] = L"\0Hi";
721  EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a));
722 }
723 
724 #ifdef __cpp_char8_t
725 // char8_t array.
726 TEST(PrintArrayTest, Char8Array) {
727  const char8_t a[] = u8"Hello, world!";
728  EXPECT_EQ(
729  "{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+0077, "
730  "U+006F, U+0072, U+006C, U+0064, U+0021, U+0000 }",
731  PrintArrayHelper(a));
732 }
733 #endif
734 
735 // char16_t array.
736 TEST(PrintArrayTest, Char16Array) {
737  const char16_t a[] = u"Hello, 世界";
738  EXPECT_EQ(
739  "{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+4E16, "
740  "U+754C, U+0000 }",
741  PrintArrayHelper(a));
742 }
743 
744 // char32_t array.
745 TEST(PrintArrayTest, Char32Array) {
746  const char32_t a[] = U"Hello, 世界";
747  EXPECT_EQ(
748  "{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+4E16, "
749  "U+754C, U+0000 }",
750  PrintArrayHelper(a));
751 }
752 
753 // Array of objects.
754 TEST(PrintArrayTest, ObjectArray) {
755  std::string a[3] = {"Hi", "Hello", "Ni hao"};
756  EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
757 }
758 
759 // Array with many elements.
760 TEST(PrintArrayTest, BigArray) {
761  int a[100] = { 1, 2, 3 };
762  EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
763  PrintArrayHelper(a));
764 }
765 
766 // Tests printing ::string and ::std::string.
767 
768 // ::std::string.
769 TEST(PrintStringTest, StringInStdNamespace) {
770  const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
771  const ::std::string str(s, sizeof(s));
772  EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
773  Print(str));
774 }
775 
776 TEST(PrintStringTest, StringAmbiguousHex) {
777  // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
778  // '\x6', '\x6B', or '\x6BA'.
779 
780  // a hex escaping sequence following by a decimal digit
781  EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
782  // a hex escaping sequence following by a hex digit (lower-case)
783  EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
784  // a hex escaping sequence following by a hex digit (upper-case)
785  EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
786  // a hex escaping sequence following by a non-xdigit
787  EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
788 }
789 
790 // Tests printing ::std::wstring.
791 #if GTEST_HAS_STD_WSTRING
792 // ::std::wstring.
793 TEST(PrintWideStringTest, StringInStdNamespace) {
794  const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
795  const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
796  EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
797  "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
798  Print(str));
799 }
800 
801 TEST(PrintWideStringTest, StringAmbiguousHex) {
802  // same for wide strings.
803  EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3")));
804  EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
805  Print(::std::wstring(L"mm\x6" L"bananas")));
806  EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
807  Print(::std::wstring(L"NOM\x6" L"BANANA")));
808  EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
809 }
810 #endif // GTEST_HAS_STD_WSTRING
811 
812 #ifdef __cpp_char8_t
813 TEST(PrintStringTest, U8String) {
814  std::u8string str = u8"Hello, world!";
815  EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
816  EXPECT_EQ(
817  "{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+0077, "
818  "U+006F, U+0072, U+006C, U+0064, U+0021 }",
819  Print(str));
820 }
821 #endif
822 
823 TEST(PrintStringTest, U16String) {
824  std::u16string str = u"Hello, 世界";
825  EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
826  EXPECT_EQ(
827  "{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+4E16, "
828  "U+754C }",
829  Print(str));
830 }
831 
832 TEST(PrintStringTest, U32String) {
833  std::u32string str = U"Hello, 世界";
834  EXPECT_EQ(str, str); // Verify EXPECT_EQ compiles with this type.
835  EXPECT_EQ(
836  "{ U+0048, U+0065, U+006C, U+006C, U+006F, U+002C, U+0020, U+4E16, "
837  "U+754C }",
838  Print(str));
839 }
840 
841 // Tests printing types that support generic streaming (i.e. streaming
842 // to std::basic_ostream<Char, CharTraits> for any valid Char and
843 // CharTraits types).
844 
845 // Tests printing a non-template type that supports generic streaming.
846 
848 
849 template <typename Char, typename CharTraits>
850 std::basic_ostream<Char, CharTraits>& operator<<(
851  std::basic_ostream<Char, CharTraits>& os,
852  const AllowsGenericStreaming& /* a */) {
853  return os << "AllowsGenericStreaming";
854 }
855 
856 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
858  EXPECT_EQ("AllowsGenericStreaming", Print(a));
859 }
860 
861 // Tests printing a template type that supports generic streaming.
862 
863 template <typename T>
865 
866 template <typename Char, typename CharTraits, typename T>
867 std::basic_ostream<Char, CharTraits>& operator<<(
868  std::basic_ostream<Char, CharTraits>& os,
869  const AllowsGenericStreamingTemplate<T>& /* a */) {
870  return os << "AllowsGenericStreamingTemplate";
871 }
872 
873 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
875  EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
876 }
877 
878 // Tests printing a type that supports generic streaming and can be
879 // implicitly converted to another printable type.
880 
881 template <typename T>
883  public:
884  operator bool() const { return false; }
885 };
886 
887 template <typename Char, typename CharTraits, typename T>
888 std::basic_ostream<Char, CharTraits>& operator<<(
889  std::basic_ostream<Char, CharTraits>& os,
891  return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
892 }
893 
894 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
896  EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
897 }
898 
899 #if GTEST_INTERNAL_HAS_STRING_VIEW
900 
901 // Tests printing internal::StringView.
902 
903 TEST(PrintStringViewTest, SimpleStringView) {
904  const internal::StringView sp = "Hello";
905  EXPECT_EQ("\"Hello\"", Print(sp));
906 }
907 
908 TEST(PrintStringViewTest, UnprintableCharacters) {
909  const char str[] = "NUL (\0) and \r\t";
910  const internal::StringView sp(str, sizeof(str) - 1);
911  EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
912 }
913 
914 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
915 
916 // Tests printing STL containers.
917 
918 TEST(PrintStlContainerTest, EmptyDeque) {
919  deque<char> empty;
920  EXPECT_EQ("{}", Print(empty));
921 }
922 
923 TEST(PrintStlContainerTest, NonEmptyDeque) {
924  deque<int> non_empty;
925  non_empty.push_back(1);
926  non_empty.push_back(3);
927  EXPECT_EQ("{ 1, 3 }", Print(non_empty));
928 }
929 
930 
931 TEST(PrintStlContainerTest, OneElementHashMap) {
932  ::std::unordered_map<int, char> map1;
933  map1[1] = 'a';
934  EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1));
935 }
936 
937 TEST(PrintStlContainerTest, HashMultiMap) {
938  ::std::unordered_multimap<int, bool> map1;
939  map1.insert(make_pair(5, true));
940  map1.insert(make_pair(5, false));
941 
942  // Elements of hash_multimap can be printed in any order.
943  const std::string result = Print(map1);
944  EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
945  result == "{ (5, false), (5, true) }")
946  << " where Print(map1) returns \"" << result << "\".";
947 }
948 
949 
950 
951 TEST(PrintStlContainerTest, HashSet) {
952  ::std::unordered_set<int> set1;
953  set1.insert(1);
954  EXPECT_EQ("{ 1 }", Print(set1));
955 }
956 
957 TEST(PrintStlContainerTest, HashMultiSet) {
958  const int kSize = 5;
959  int a[kSize] = { 1, 1, 2, 5, 1 };
960  ::std::unordered_multiset<int> set1(a, a + kSize);
961 
962  // Elements of hash_multiset can be printed in any order.
963  const std::string result = Print(set1);
964  const std::string expected_pattern = "{ d, d, d, d, d }"; // d means a digit.
965 
966  // Verifies the result matches the expected pattern; also extracts
967  // the numbers in the result.
968  ASSERT_EQ(expected_pattern.length(), result.length());
969  std::vector<int> numbers;
970  for (size_t i = 0; i != result.length(); i++) {
971  if (expected_pattern[i] == 'd') {
972  ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0);
973  numbers.push_back(result[i] - '0');
974  } else {
975  EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
976  << result;
977  }
978  }
979 
980  // Makes sure the result contains the right numbers.
981  std::sort(numbers.begin(), numbers.end());
982  std::sort(a, a + kSize);
983  EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
984 }
985 
986 
987 TEST(PrintStlContainerTest, List) {
988  const std::string a[] = {"hello", "world"};
989  const list<std::string> strings(a, a + 2);
990  EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
991 }
992 
993 TEST(PrintStlContainerTest, Map) {
994  map<int, bool> map1;
995  map1[1] = true;
996  map1[5] = false;
997  map1[3] = true;
998  EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
999 }
1000 
1001 TEST(PrintStlContainerTest, MultiMap) {
1002  multimap<bool, int> map1;
1003  // The make_pair template function would deduce the type as
1004  // pair<bool, int> here, and since the key part in a multimap has to
1005  // be constant, without a templated ctor in the pair class (as in
1006  // libCstd on Solaris), make_pair call would fail to compile as no
1007  // implicit conversion is found. Thus explicit typename is used
1008  // here instead.
1009  map1.insert(pair<const bool, int>(true, 0));
1010  map1.insert(pair<const bool, int>(true, 1));
1011  map1.insert(pair<const bool, int>(false, 2));
1012  EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
1013 }
1014 
1015 TEST(PrintStlContainerTest, Set) {
1016  const unsigned int a[] = { 3, 0, 5 };
1017  set<unsigned int> set1(a, a + 3);
1018  EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
1019 }
1020 
1021 TEST(PrintStlContainerTest, MultiSet) {
1022  const int a[] = { 1, 1, 2, 5, 1 };
1023  multiset<int> set1(a, a + 5);
1024  EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
1025 }
1026 
1027 
1028 TEST(PrintStlContainerTest, SinglyLinkedList) {
1029  int a[] = { 9, 2, 8 };
1030  const std::forward_list<int> ints(a, a + 3);
1031  EXPECT_EQ("{ 9, 2, 8 }", Print(ints));
1032 }
1033 
1034 TEST(PrintStlContainerTest, Pair) {
1035  pair<const bool, int> p(true, 5);
1036  EXPECT_EQ("(true, 5)", Print(p));
1037 }
1038 
1039 TEST(PrintStlContainerTest, Vector) {
1040  vector<int> v;
1041  v.push_back(1);
1042  v.push_back(2);
1043  EXPECT_EQ("{ 1, 2 }", Print(v));
1044 }
1045 
1046 TEST(PrintStlContainerTest, LongSequence) {
1047  const int a[100] = { 1, 2, 3 };
1048  const vector<int> v(a, a + 100);
1049  EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
1050  "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
1051 }
1052 
1053 TEST(PrintStlContainerTest, NestedContainer) {
1054  const int a1[] = { 1, 2 };
1055  const int a2[] = { 3, 4, 5 };
1056  const list<int> l1(a1, a1 + 2);
1057  const list<int> l2(a2, a2 + 3);
1058 
1059  vector<list<int> > v;
1060  v.push_back(l1);
1061  v.push_back(l2);
1062  EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
1063 }
1064 
1065 TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
1066  const int a[3] = { 1, 2, 3 };
1067  NativeArray<int> b(a, 3, RelationToSourceReference());
1068  EXPECT_EQ("{ 1, 2, 3 }", Print(b));
1069 }
1070 
1071 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
1072  const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1073  NativeArray<int[3]> b(a, 2, RelationToSourceReference());
1074  EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
1075 }
1076 
1077 // Tests that a class named iterator isn't treated as a container.
1078 
1079 struct iterator {
1080  char x;
1081 };
1082 
1083 TEST(PrintStlContainerTest, Iterator) {
1084  iterator it = {};
1085  EXPECT_EQ("1-byte object <00>", Print(it));
1086 }
1087 
1088 // Tests that a class named const_iterator isn't treated as a container.
1089 
1091  char x;
1092 };
1093 
1094 TEST(PrintStlContainerTest, ConstIterator) {
1095  const_iterator it = {};
1096  EXPECT_EQ("1-byte object <00>", Print(it));
1097 }
1098 
1099 // Tests printing ::std::tuples.
1100 
1101 // Tuples of various arities.
1102 TEST(PrintStdTupleTest, VariousSizes) {
1103  ::std::tuple<> t0;
1104  EXPECT_EQ("()", Print(t0));
1105 
1106  ::std::tuple<int> t1(5);
1107  EXPECT_EQ("(5)", Print(t1));
1108 
1109  ::std::tuple<char, bool> t2('a', true);
1110  EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
1111 
1112  ::std::tuple<bool, int, int> t3(false, 2, 3);
1113  EXPECT_EQ("(false, 2, 3)", Print(t3));
1114 
1115  ::std::tuple<bool, int, int, int> t4(false, 2, 3, 4);
1116  EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
1117 
1118  const char* const str = "8";
1119  ::std::tuple<bool, char, short, int32_t, int64_t, float, double, // NOLINT
1120  const char*, void*, std::string>
1121  t10(false, 'a', static_cast<short>(3), 4, 5, 1.5F, -2.5, str, // NOLINT
1122  nullptr, "10");
1123  EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1124  " pointing to \"8\", NULL, \"10\")",
1125  Print(t10));
1126 }
1127 
1128 // Nested tuples.
1129 TEST(PrintStdTupleTest, NestedTuple) {
1130  ::std::tuple< ::std::tuple<int, bool>, char> nested(
1131  ::std::make_tuple(5, true), 'a');
1132  EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
1133 }
1134 
1135 TEST(PrintNullptrT, Basic) {
1136  EXPECT_EQ("(nullptr)", Print(nullptr));
1137 }
1138 
1139 TEST(PrintReferenceWrapper, Printable) {
1140  int x = 5;
1141  EXPECT_EQ("@" + PrintPointer(&x) + " 5", Print(std::ref(x)));
1142  EXPECT_EQ("@" + PrintPointer(&x) + " 5", Print(std::cref(x)));
1143 }
1144 
1145 TEST(PrintReferenceWrapper, Unprintable) {
1147  EXPECT_EQ(
1148  "@" + PrintPointer(&up) +
1149  " 16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1150  Print(std::ref(up)));
1151  EXPECT_EQ(
1152  "@" + PrintPointer(&up) +
1153  " 16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1154  Print(std::cref(up)));
1155 }
1156 
1157 // Tests printing user-defined unprintable types.
1158 
1159 // Unprintable types in the global namespace.
1160 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
1161  EXPECT_EQ("1-byte object <00>",
1163 }
1164 
1165 // Unprintable types in a user namespace.
1166 TEST(PrintUnprintableTypeTest, InUserNamespace) {
1167  EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1169 }
1170 
1171 // Unprintable types are that too big to be printed completely.
1172 
1173 struct Big {
1174  Big() { memset(array, 0, sizeof(array)); }
1175  char array[257];
1176 };
1177 
1178 TEST(PrintUnpritableTypeTest, BigObject) {
1179  EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
1180  "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1181  "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1182  "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
1183  "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1184  "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1185  "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
1186  Print(Big()));
1187 }
1188 
1189 // Tests printing user-defined streamable types.
1190 
1191 // Streamable types in the global namespace.
1192 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
1194  EXPECT_EQ("StreamableInGlobal", Print(x));
1195  EXPECT_EQ("StreamableInGlobal*", Print(&x));
1196 }
1197 
1198 // Printable template types in a user namespace.
1199 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
1200  EXPECT_EQ("StreamableTemplateInFoo: 0",
1202 }
1203 
1204 // Tests printing a user-defined recursive container type that has a <<
1205 // operator.
1206 TEST(PrintStreamableTypeTest, PathLikeInUserNamespace) {
1208  EXPECT_EQ("Streamable-PathLike", Print(x));
1209  const ::foo::PathLike cx;
1210  EXPECT_EQ("Streamable-PathLike", Print(cx));
1211 }
1212 
1213 // Tests printing user-defined types that have a PrintTo() function.
1214 TEST(PrintPrintableTypeTest, InUserNamespace) {
1215  EXPECT_EQ("PrintableViaPrintTo: 0",
1217 }
1218 
1219 // Tests printing a pointer to a user-defined type that has a <<
1220 // operator for its pointer.
1221 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
1223  EXPECT_EQ("PointerPrintable*", Print(&x));
1224 }
1225 
1226 // Tests printing user-defined class template that have a PrintTo() function.
1227 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
1228  EXPECT_EQ("PrintableViaPrintToTemplate: 5",
1230 }
1231 
1232 // Tests that the universal printer prints both the address and the
1233 // value of a reference.
1234 TEST(PrintReferenceTest, PrintsAddressAndValue) {
1235  int n = 5;
1236  EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
1237 
1238  int a[2][3] = {
1239  { 0, 1, 2 },
1240  { 3, 4, 5 }
1241  };
1242  EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
1243  PrintByRef(a));
1244 
1245  const ::foo::UnprintableInFoo x;
1246  EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
1247  "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1248  PrintByRef(x));
1249 }
1250 
1251 // Tests that the universal printer prints a function pointer passed by
1252 // reference.
1253 TEST(PrintReferenceTest, HandlesFunctionPointer) {
1254  void (*fp)(int n) = &MyFunction;
1255  const std::string fp_pointer_string =
1256  PrintPointer(reinterpret_cast<const void*>(&fp));
1257  // We cannot directly cast &MyFunction to const void* because the
1258  // standard disallows casting between pointers to functions and
1259  // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
1260  // this limitation.
1261  const std::string fp_string = PrintPointer(reinterpret_cast<const void*>(
1262  reinterpret_cast<internal::BiggestInt>(fp)));
1263  EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
1264  PrintByRef(fp));
1265 }
1266 
1267 // Tests that the universal printer prints a member function pointer
1268 // passed by reference.
1269 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
1270  int (Foo::*p)(char ch) = &Foo::MyMethod;
1272  PrintByRef(p),
1273  "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
1274  Print(sizeof(p)) + "-byte object "));
1275 
1276  char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
1278  PrintByRef(p2),
1279  "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
1280  Print(sizeof(p2)) + "-byte object "));
1281 }
1282 
1283 // Tests that the universal printer prints a member variable pointer
1284 // passed by reference.
1285 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
1286  int Foo::*p = &Foo::value; // NOLINT
1288  PrintByRef(p),
1289  "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
1290 }
1291 
1292 // Tests that FormatForComparisonFailureMessage(), which is used to print
1293 // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion
1294 // fails, formats the operand in the desired way.
1295 
1296 // scalar
1297 TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
1298  EXPECT_STREQ("123",
1299  FormatForComparisonFailureMessage(123, 124).c_str());
1300 }
1301 
1302 // non-char pointer
1303 TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
1304  int n = 0;
1305  EXPECT_EQ(PrintPointer(&n),
1306  FormatForComparisonFailureMessage(&n, &n).c_str());
1307 }
1308 
1309 // non-char array
1310 TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
1311  // In expression 'array == x', 'array' is compared by pointer.
1312  // Therefore we want to print an array operand as a pointer.
1313  int n[] = { 1, 2, 3 };
1315  FormatForComparisonFailureMessage(n, n).c_str());
1316 }
1317 
1318 // Tests formatting a char pointer when it's compared with another pointer.
1319 // In this case we want to print it as a raw pointer, as the comparison is by
1320 // pointer.
1321 
1322 // char pointer vs pointer
1323 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
1324  // In expression 'p == x', where 'p' and 'x' are (const or not) char
1325  // pointers, the operands are compared by pointer. Therefore we
1326  // want to print 'p' as a pointer instead of a C string (we don't
1327  // even know if it's supposed to point to a valid C string).
1328 
1329  // const char*
1330  const char* s = "hello";
1332  FormatForComparisonFailureMessage(s, s).c_str());
1333 
1334  // char*
1335  char ch = 'a';
1336  EXPECT_EQ(PrintPointer(&ch),
1337  FormatForComparisonFailureMessage(&ch, &ch).c_str());
1338 }
1339 
1340 // wchar_t pointer vs pointer
1341 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
1342  // In expression 'p == x', where 'p' and 'x' are (const or not) char
1343  // pointers, the operands are compared by pointer. Therefore we
1344  // want to print 'p' as a pointer instead of a wide C string (we don't
1345  // even know if it's supposed to point to a valid wide C string).
1346 
1347  // const wchar_t*
1348  const wchar_t* s = L"hello";
1350  FormatForComparisonFailureMessage(s, s).c_str());
1351 
1352  // wchar_t*
1353  wchar_t ch = L'a';
1354  EXPECT_EQ(PrintPointer(&ch),
1355  FormatForComparisonFailureMessage(&ch, &ch).c_str());
1356 }
1357 
1358 // Tests formatting a char pointer when it's compared to a string object.
1359 // In this case we want to print the char pointer as a C string.
1360 
1361 // char pointer vs std::string
1362 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
1363  const char* s = "hello \"world";
1364  EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1365  FormatForComparisonFailureMessage(s, ::std::string()).c_str());
1366 
1367  // char*
1368  char str[] = "hi\1";
1369  char* p = str;
1370  EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1371  FormatForComparisonFailureMessage(p, ::std::string()).c_str());
1372 }
1373 
1374 #if GTEST_HAS_STD_WSTRING
1375 // wchar_t pointer vs std::wstring
1376 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
1377  const wchar_t* s = L"hi \"world";
1378  EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1379  FormatForComparisonFailureMessage(s, ::std::wstring()).c_str());
1380 
1381  // wchar_t*
1382  wchar_t str[] = L"hi\1";
1383  wchar_t* p = str;
1384  EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1385  FormatForComparisonFailureMessage(p, ::std::wstring()).c_str());
1386 }
1387 #endif
1388 
1389 // Tests formatting a char array when it's compared with a pointer or array.
1390 // In this case we want to print the array as a row pointer, as the comparison
1391 // is by pointer.
1392 
1393 // char array vs pointer
1394 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
1395  char str[] = "hi \"world\"";
1396  char* p = nullptr;
1397  EXPECT_EQ(PrintPointer(str),
1398  FormatForComparisonFailureMessage(str, p).c_str());
1399 }
1400 
1401 // char array vs char array
1402 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
1403  const char str[] = "hi \"world\"";
1404  EXPECT_EQ(PrintPointer(str),
1405  FormatForComparisonFailureMessage(str, str).c_str());
1406 }
1407 
1408 // wchar_t array vs pointer
1409 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
1410  wchar_t str[] = L"hi \"world\"";
1411  wchar_t* p = nullptr;
1412  EXPECT_EQ(PrintPointer(str),
1413  FormatForComparisonFailureMessage(str, p).c_str());
1414 }
1415 
1416 // wchar_t array vs wchar_t array
1417 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
1418  const wchar_t str[] = L"hi \"world\"";
1419  EXPECT_EQ(PrintPointer(str),
1420  FormatForComparisonFailureMessage(str, str).c_str());
1421 }
1422 
1423 // Tests formatting a char array when it's compared with a string object.
1424 // In this case we want to print the array as a C string.
1425 
1426 // char array vs std::string
1427 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
1428  const char str[] = "hi \"world\"";
1429  EXPECT_STREQ("\"hi \\\"world\\\"\"", // The content should be escaped.
1430  FormatForComparisonFailureMessage(str, ::std::string()).c_str());
1431 }
1432 
1433 #if GTEST_HAS_STD_WSTRING
1434 // wchar_t array vs std::wstring
1435 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
1436  const wchar_t str[] = L"hi \"w\0rld\"";
1437  EXPECT_STREQ(
1438  "L\"hi \\\"w\"", // The content should be escaped.
1439  // Embedded NUL terminates the string.
1440  FormatForComparisonFailureMessage(str, ::std::wstring()).c_str());
1441 }
1442 #endif
1443 
1444 // Useful for testing PrintToString(). We cannot use EXPECT_EQ()
1445 // there as its implementation uses PrintToString(). The caller must
1446 // ensure that 'value' has no side effect.
1447 #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
1448  EXPECT_TRUE(PrintToString(value) == (expected_string)) \
1449  << " where " #value " prints as " << (PrintToString(value))
1450 
1451 TEST(PrintToStringTest, WorksForScalar) {
1452  EXPECT_PRINT_TO_STRING_(123, "123");
1453 }
1454 
1455 TEST(PrintToStringTest, WorksForPointerToConstChar) {
1456  const char* p = "hello";
1457  EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
1458 }
1459 
1460 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
1461  char s[] = "hello";
1462  char* p = s;
1463  EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
1464 }
1465 
1466 TEST(PrintToStringTest, EscapesForPointerToConstChar) {
1467  const char* p = "hello\n";
1468  EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\"");
1469 }
1470 
1471 TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
1472  char s[] = "hello\1";
1473  char* p = s;
1474  EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\"");
1475 }
1476 
1477 TEST(PrintToStringTest, WorksForArray) {
1478  int n[3] = { 1, 2, 3 };
1479  EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
1480 }
1481 
1482 TEST(PrintToStringTest, WorksForCharArray) {
1483  char s[] = "hello";
1484  EXPECT_PRINT_TO_STRING_(s, "\"hello\"");
1485 }
1486 
1487 TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
1488  const char str_with_nul[] = "hello\0 world";
1489  EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\"");
1490 
1491  char mutable_str_with_nul[] = "hello\0 world";
1492  EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\"");
1493 }
1494 
1495  TEST(PrintToStringTest, ContainsNonLatin) {
1496  // Sanity test with valid UTF-8. Prints both in hex and as text.
1497  std::string non_ascii_str = ::std::string("오전 4:30");
1498  EXPECT_PRINT_TO_STRING_(non_ascii_str,
1499  "\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
1500  " As Text: \"오전 4:30\"");
1501  non_ascii_str = ::std::string("From ä — ẑ");
1502  EXPECT_PRINT_TO_STRING_(non_ascii_str,
1503  "\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
1504  "\n As Text: \"From ä — ẑ\"");
1505 }
1506 
1507 TEST(IsValidUTF8Test, IllFormedUTF8) {
1508  // The following test strings are ill-formed UTF-8 and are printed
1509  // as hex only (or ASCII, in case of ASCII bytes) because IsValidUTF8() is
1510  // expected to fail, thus output does not contain "As Text:".
1511 
1512  static const char *const kTestdata[][2] = {
1513  // 2-byte lead byte followed by a single-byte character.
1514  {"\xC3\x74", "\"\\xC3t\""},
1515  // Valid 2-byte character followed by an orphan trail byte.
1516  {"\xC3\x84\xA4", "\"\\xC3\\x84\\xA4\""},
1517  // Lead byte without trail byte.
1518  {"abc\xC3", "\"abc\\xC3\""},
1519  // 3-byte lead byte, single-byte character, orphan trail byte.
1520  {"x\xE2\x70\x94", "\"x\\xE2p\\x94\""},
1521  // Truncated 3-byte character.
1522  {"\xE2\x80", "\"\\xE2\\x80\""},
1523  // Truncated 3-byte character followed by valid 2-byte char.
1524  {"\xE2\x80\xC3\x84", "\"\\xE2\\x80\\xC3\\x84\""},
1525  // Truncated 3-byte character followed by a single-byte character.
1526  {"\xE2\x80\x7A", "\"\\xE2\\x80z\""},
1527  // 3-byte lead byte followed by valid 3-byte character.
1528  {"\xE2\xE2\x80\x94", "\"\\xE2\\xE2\\x80\\x94\""},
1529  // 4-byte lead byte followed by valid 3-byte character.
1530  {"\xF0\xE2\x80\x94", "\"\\xF0\\xE2\\x80\\x94\""},
1531  // Truncated 4-byte character.
1532  {"\xF0\xE2\x80", "\"\\xF0\\xE2\\x80\""},
1533  // Invalid UTF-8 byte sequences embedded in other chars.
1534  {"abc\xE2\x80\x94\xC3\x74xyc", "\"abc\\xE2\\x80\\x94\\xC3txyc\""},
1535  {"abc\xC3\x84\xE2\x80\xC3\x84xyz",
1536  "\"abc\\xC3\\x84\\xE2\\x80\\xC3\\x84xyz\""},
1537  // Non-shortest UTF-8 byte sequences are also ill-formed.
1538  // The classics: xC0, xC1 lead byte.
1539  {"\xC0\x80", "\"\\xC0\\x80\""},
1540  {"\xC1\x81", "\"\\xC1\\x81\""},
1541  // Non-shortest sequences.
1542  {"\xE0\x80\x80", "\"\\xE0\\x80\\x80\""},
1543  {"\xf0\x80\x80\x80", "\"\\xF0\\x80\\x80\\x80\""},
1544  // Last valid code point before surrogate range, should be printed as text,
1545  // too.
1546  {"\xED\x9F\xBF", "\"\\xED\\x9F\\xBF\"\n As Text: \"퟿\""},
1547  // Start of surrogate lead. Surrogates are not printed as text.
1548  {"\xED\xA0\x80", "\"\\xED\\xA0\\x80\""},
1549  // Last non-private surrogate lead.
1550  {"\xED\xAD\xBF", "\"\\xED\\xAD\\xBF\""},
1551  // First private-use surrogate lead.
1552  {"\xED\xAE\x80", "\"\\xED\\xAE\\x80\""},
1553  // Last private-use surrogate lead.
1554  {"\xED\xAF\xBF", "\"\\xED\\xAF\\xBF\""},
1555  // Mid-point of surrogate trail.
1556  {"\xED\xB3\xBF", "\"\\xED\\xB3\\xBF\""},
1557  // First valid code point after surrogate range, should be printed as text,
1558  // too.
1559  {"\xEE\x80\x80", "\"\\xEE\\x80\\x80\"\n As Text: \"\""}
1560  };
1561 
1562  for (int i = 0; i < int(sizeof(kTestdata)/sizeof(kTestdata[0])); ++i) {
1563  EXPECT_PRINT_TO_STRING_(kTestdata[i][0], kTestdata[i][1]);
1564  }
1565 }
1566 
1567 #undef EXPECT_PRINT_TO_STRING_
1568 
1569 TEST(UniversalTersePrintTest, WorksForNonReference) {
1570  ::std::stringstream ss;
1571  UniversalTersePrint(123, &ss);
1572  EXPECT_EQ("123", ss.str());
1573 }
1574 
1575 TEST(UniversalTersePrintTest, WorksForReference) {
1576  const int& n = 123;
1577  ::std::stringstream ss;
1578  UniversalTersePrint(n, &ss);
1579  EXPECT_EQ("123", ss.str());
1580 }
1581 
1582 TEST(UniversalTersePrintTest, WorksForCString) {
1583  const char* s1 = "abc";
1584  ::std::stringstream ss1;
1585  UniversalTersePrint(s1, &ss1);
1586  EXPECT_EQ("\"abc\"", ss1.str());
1587 
1588  char* s2 = const_cast<char*>(s1);
1589  ::std::stringstream ss2;
1590  UniversalTersePrint(s2, &ss2);
1591  EXPECT_EQ("\"abc\"", ss2.str());
1592 
1593  const char* s3 = nullptr;
1594  ::std::stringstream ss3;
1595  UniversalTersePrint(s3, &ss3);
1596  EXPECT_EQ("NULL", ss3.str());
1597 }
1598 
1599 TEST(UniversalPrintTest, WorksForNonReference) {
1600  ::std::stringstream ss;
1601  UniversalPrint(123, &ss);
1602  EXPECT_EQ("123", ss.str());
1603 }
1604 
1605 TEST(UniversalPrintTest, WorksForReference) {
1606  const int& n = 123;
1607  ::std::stringstream ss;
1608  UniversalPrint(n, &ss);
1609  EXPECT_EQ("123", ss.str());
1610 }
1611 
1612 TEST(UniversalPrintTest, WorksForCString) {
1613  const char* s1 = "abc";
1614  ::std::stringstream ss1;
1615  UniversalPrint(s1, &ss1);
1616  EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", std::string(ss1.str()));
1617 
1618  char* s2 = const_cast<char*>(s1);
1619  ::std::stringstream ss2;
1620  UniversalPrint(s2, &ss2);
1621  EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", std::string(ss2.str()));
1622 
1623  const char* s3 = nullptr;
1624  ::std::stringstream ss3;
1625  UniversalPrint(s3, &ss3);
1626  EXPECT_EQ("NULL", ss3.str());
1627 }
1628 
1629 TEST(UniversalPrintTest, WorksForCharArray) {
1630  const char str[] = "\"Line\0 1\"\nLine 2";
1631  ::std::stringstream ss1;
1632  UniversalPrint(str, &ss1);
1633  EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
1634 
1635  const char mutable_str[] = "\"Line\0 1\"\nLine 2";
1636  ::std::stringstream ss2;
1637  UniversalPrint(mutable_str, &ss2);
1638  EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
1639 }
1640 
1641 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
1642  Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
1643  EXPECT_EQ(0u, result.size());
1644 }
1645 
1646 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
1648  ::std::make_tuple(1));
1649  ASSERT_EQ(1u, result.size());
1650  EXPECT_EQ("1", result[0]);
1651 }
1652 
1653 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
1655  ::std::make_tuple(1, 'a'));
1656  ASSERT_EQ(2u, result.size());
1657  EXPECT_EQ("1", result[0]);
1658  EXPECT_EQ("'a' (97, 0x61)", result[1]);
1659 }
1660 
1661 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
1662  const int n = 1;
1664  ::std::tuple<const int&, const char*>(n, "a"));
1665  ASSERT_EQ(2u, result.size());
1666  EXPECT_EQ("1", result[0]);
1667  EXPECT_EQ("\"a\"", result[1]);
1668 }
1669 
1670 #if GTEST_INTERNAL_HAS_ANY
1671 class PrintAnyTest : public ::testing::Test {
1672  protected:
1673  template <typename T>
1674  static std::string ExpectedTypeName() {
1675 #if GTEST_HAS_RTTI
1676  return internal::GetTypeName<T>();
1677 #else
1678  return "<unknown_type>";
1679 #endif // GTEST_HAS_RTTI
1680  }
1681 };
1682 
1683 TEST_F(PrintAnyTest, Empty) {
1684  internal::Any any;
1685  EXPECT_EQ("no value", PrintToString(any));
1686 }
1687 
1688 TEST_F(PrintAnyTest, NonEmpty) {
1689  internal::Any any;
1690  constexpr int val1 = 10;
1691  const std::string val2 = "content";
1692 
1693  any = val1;
1694  EXPECT_EQ("value of type " + ExpectedTypeName<int>(), PrintToString(any));
1695 
1696  any = val2;
1697  EXPECT_EQ("value of type " + ExpectedTypeName<std::string>(),
1698  PrintToString(any));
1699 }
1700 #endif // GTEST_INTERNAL_HAS_ANY
1701 
1702 #if GTEST_INTERNAL_HAS_OPTIONAL
1703 TEST(PrintOptionalTest, Basic) {
1704  internal::Optional<int> value;
1705  EXPECT_EQ("(nullopt)", PrintToString(value));
1706  value = {7};
1707  EXPECT_EQ("(7)", PrintToString(value));
1708  EXPECT_EQ("(1.1)", PrintToString(internal::Optional<double>{1.1}));
1709  EXPECT_EQ("(\"A\")", PrintToString(internal::Optional<std::string>{"A"}));
1710 }
1711 #endif // GTEST_INTERNAL_HAS_OPTIONAL
1712 
1713 #if GTEST_INTERNAL_HAS_VARIANT
1714 struct NonPrintable {
1715  unsigned char contents = 17;
1716 };
1717 
1718 TEST(PrintOneofTest, Basic) {
1719  using Type = internal::Variant<int, StreamableInGlobal, NonPrintable>;
1720  EXPECT_EQ("('int(index = 0)' with value 7)", PrintToString(Type(7)));
1721  EXPECT_EQ("('StreamableInGlobal(index = 1)' with value StreamableInGlobal)",
1723  EXPECT_EQ(
1724  "('testing::gtest_printers_test::NonPrintable(index = 2)' with value "
1725  "1-byte object <11>)",
1726  PrintToString(Type(NonPrintable{})));
1727 }
1728 #endif // GTEST_INTERNAL_HAS_VARIANT
1729 namespace {
1730 class string_ref;
1731 
1735 class string_ptr {
1736  public:
1737  string_ptr(const char* data, size_t size) : data_(data), size_(size) {}
1738 
1739  string_ptr& operator++() noexcept {
1740  data_ += size_;
1741  return *this;
1742  }
1743 
1744  string_ref operator*() const noexcept;
1745 
1746  private:
1747  const char* data_;
1748  size_t size_;
1749 };
1750 
1754 class string_ref {
1755  public:
1756  string_ref(const char* data, size_t size) : data_(data), size_(size) {}
1757 
1758  string_ptr operator&() const noexcept { return {data_, size_}; } // NOLINT
1759 
1760  bool operator==(const char* s) const noexcept {
1761  if (size_ > 0 && data_[size_ - 1] != 0) {
1762  return std::string(data_, size_) == std::string(s);
1763  } else {
1764  return std::string(data_) == std::string(s);
1765  }
1766  }
1767 
1768  private:
1769  const char* data_;
1770  size_t size_;
1771 };
1772 
1773 string_ref string_ptr::operator*() const noexcept { return {data_, size_}; }
1774 
1775 TEST(string_ref, compare) {
1776  const char* s = "alex\0davidjohn\0";
1777  string_ptr ptr(s, 5);
1778  EXPECT_EQ(*ptr, "alex");
1779  EXPECT_TRUE(*ptr == "alex");
1780  ++ptr;
1781  EXPECT_EQ(*ptr, "david");
1782  EXPECT_TRUE(*ptr == "david");
1783  ++ptr;
1784  EXPECT_EQ(*ptr, "john");
1785 }
1786 
1787 } // namespace
1788 
1789 } // namespace gtest_printers_test
1790 } // namespace testing
const char * p
const char * data_
TEST(PrintEnumTest, AnonymousEnum)
To ImplicitCast_(To x)
Definition: gtest-port.h:1063
void f()
AssertionResult AssertionFailure()
Definition: gtest.cc:1200
#define EXPECT_PRINT_TO_STRING_(value, expected_string)
#define ASSERT_NE(val1, val2)
Definition: gtest.h:2072
::std::string PrintToString(const T &value)
TEST_F(TestInfoTest, Names)
void UniversalTersePrint(const T &value,::std::ostream *os)
std::basic_ostream< Char, CharTraits > & operator<<(std::basic_ostream< Char, CharTraits > &os, const AllowsGenericStreaming &)
std::string Print(const T &value)
::std::vector< ::std::string > Strings
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:2068
::std::ostream & operator<<(::std::ostream &os, const PointerPrintable *)
iterator end() const
std::string FormatForComparisonFailureMessage(const T1 &value, const T2 &)
BigUInt< n > operator*(BigUInt< n > const &a, BigUInt< n > const &b)
#define EXPECT_LT(val1, val2)
Definition: gtest.h:2044
std::ostream & operator<<(std::ostream &os, const Expr< T > &xx)
#define T
Definition: Sacado_rad.hpp:573
AssertionResult AssertionSuccess()
Definition: gtest.cc:1195
SimpleFad< ValueT > min(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
void UniversalPrint(const T &value,::std::ostream *os)
ADVar foo(double d, ADVar x, ADVar y)
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:2107
const int N
void PrintTo(const PrintableViaPrintTo &x,::std::ostream *os)
#define Void
Definition: uninit.c:74
void
Definition: uninit.c:96
void PrintTo(const T &value,::std::ostream *os)
int value
long long BiggestInt
Definition: gtest-port.h:2133
iterator begin() const
Strings UniversalTersePrintTupleFieldsToStrings(const Tuple &value)
AssertionResult HasPrefix(const StringType &str, const StringType &prefix)
bool operator==(const Handle< T > &h1, const Handle< T > &h2)
Compare two handles.
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2038
static std::string PrintPointer(const void *p)
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1979
std::string PrintByRef(const T &value)
friend::std::ostream & operator<<(::std::ostream &os, const PathLike &)
int n