]> git.proxmox.com Git - ceph.git/blob - ceph/src/s3select/rapidjson/thirdparty/gtest/googletest/test/gtest-printers_test.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / s3select / rapidjson / thirdparty / gtest / googletest / test / 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 and Mocking 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 #if GTEST_HAS_UNORDERED_MAP_
54 # include <unordered_map> // NOLINT
55 #endif // GTEST_HAS_UNORDERED_MAP_
56
57 #if GTEST_HAS_UNORDERED_SET_
58 # include <unordered_set> // NOLINT
59 #endif // GTEST_HAS_UNORDERED_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.
68 enum AnonymousEnum {
69 kAE1 = -1,
70 kAE2 = 1
71 };
72
73 // An enum without a user-defined printer.
74 enum EnumWithoutPrinter {
75 kEWP1 = -2,
76 kEWP2 = 42
77 };
78
79 // An enum with a << operator.
80 enum EnumWithStreaming {
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.
89 enum EnumWithPrintTo {
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.
98 class BiggestIntConvertible {
99 public:
100 operator ::testing::internal::BiggestInt() const { return 42; }
101 };
102
103 // A user-defined unprintable class template in the global namespace.
104 template <typename T>
105 class UnprintableTemplateInGlobal {
106 public:
107 UnprintableTemplateInGlobal() : value_() {}
108 private:
109 T value_;
110 };
111
112 // A user-defined streamable type in the global namespace.
113 class StreamableInGlobal {
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.
129 class UnprintableInFoo {
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.
139 struct PrintableViaPrintTo {
140 PrintableViaPrintTo() : value() {}
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.
149 struct PointerPrintable {
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>
159 class PrintableViaPrintToTemplate {
160 public:
161 explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
162
163 const T& value() const { return value_; }
164 private:
165 T value_;
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>
175 class StreamableTemplateInFoo {
176 public:
177 StreamableTemplateInFoo() : value_() {}
178
179 const T& value() const { return value_; }
180 private:
181 T value_;
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 // A user-defined streamable but recursivly-defined container type in
191 // a user namespace, it mimics therefore std::filesystem::path or
192 // boost::filesystem::path.
193 class PathLike {
194 public:
195 struct iterator {
196 typedef PathLike value_type;
197 };
198
199 PathLike() {}
200
201 iterator begin() const { return iterator(); }
202 iterator end() const { return iterator(); }
203
204 friend ::std::ostream& operator<<(::std::ostream& os, const PathLike&) {
205 return os << "Streamable-PathLike";
206 }
207 };
208
209 } // namespace foo
210
211 namespace testing {
212 namespace gtest_printers_test {
213
214 using ::std::deque;
215 using ::std::list;
216 using ::std::make_pair;
217 using ::std::map;
218 using ::std::multimap;
219 using ::std::multiset;
220 using ::std::pair;
221 using ::std::set;
222 using ::std::vector;
223 using ::testing::PrintToString;
224 using ::testing::internal::FormatForComparisonFailureMessage;
225 using ::testing::internal::ImplicitCast_;
226 using ::testing::internal::NativeArray;
227 using ::testing::internal::RE;
228 using ::testing::internal::RelationToSourceReference;
229 using ::testing::internal::Strings;
230 using ::testing::internal::UniversalPrint;
231 using ::testing::internal::UniversalPrinter;
232 using ::testing::internal::UniversalTersePrint;
233 #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
234 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
235 #endif
236
237 // Prints a value to a string using the universal value printer. This
238 // is a helper for testing UniversalPrinter<T>::Print() for various types.
239 template <typename T>
240 std::string Print(const T& value) {
241 ::std::stringstream ss;
242 UniversalPrinter<T>::Print(value, &ss);
243 return ss.str();
244 }
245
246 // Prints a value passed by reference to a string, using the universal
247 // value printer. This is a helper for testing
248 // UniversalPrinter<T&>::Print() for various types.
249 template <typename T>
250 std::string PrintByRef(const T& value) {
251 ::std::stringstream ss;
252 UniversalPrinter<T&>::Print(value, &ss);
253 return ss.str();
254 }
255
256 // Tests printing various enum types.
257
258 TEST(PrintEnumTest, AnonymousEnum) {
259 EXPECT_EQ("-1", Print(kAE1));
260 EXPECT_EQ("1", Print(kAE2));
261 }
262
263 TEST(PrintEnumTest, EnumWithoutPrinter) {
264 EXPECT_EQ("-2", Print(kEWP1));
265 EXPECT_EQ("42", Print(kEWP2));
266 }
267
268 TEST(PrintEnumTest, EnumWithStreaming) {
269 EXPECT_EQ("kEWS1", Print(kEWS1));
270 EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
271 }
272
273 TEST(PrintEnumTest, EnumWithPrintTo) {
274 EXPECT_EQ("kEWPT1", Print(kEWPT1));
275 EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
276 }
277
278 // Tests printing a class implicitly convertible to BiggestInt.
279
280 TEST(PrintClassTest, BiggestIntConvertible) {
281 EXPECT_EQ("42", Print(BiggestIntConvertible()));
282 }
283
284 // Tests printing various char types.
285
286 // char.
287 TEST(PrintCharTest, PlainChar) {
288 EXPECT_EQ("'\\0'", Print('\0'));
289 EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
290 EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
291 EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
292 EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
293 EXPECT_EQ("'\\a' (7)", Print('\a'));
294 EXPECT_EQ("'\\b' (8)", Print('\b'));
295 EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
296 EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
297 EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
298 EXPECT_EQ("'\\t' (9)", Print('\t'));
299 EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
300 EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
301 EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
302 EXPECT_EQ("' ' (32, 0x20)", Print(' '));
303 EXPECT_EQ("'a' (97, 0x61)", Print('a'));
304 }
305
306 // signed char.
307 TEST(PrintCharTest, SignedChar) {
308 EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
309 EXPECT_EQ("'\\xCE' (-50)",
310 Print(static_cast<signed char>(-50)));
311 }
312
313 // unsigned char.
314 TEST(PrintCharTest, UnsignedChar) {
315 EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
316 EXPECT_EQ("'b' (98, 0x62)",
317 Print(static_cast<unsigned char>('b')));
318 }
319
320 // Tests printing other simple, built-in types.
321
322 // bool.
323 TEST(PrintBuiltInTypeTest, Bool) {
324 EXPECT_EQ("false", Print(false));
325 EXPECT_EQ("true", Print(true));
326 }
327
328 // wchar_t.
329 TEST(PrintBuiltInTypeTest, Wchar_t) {
330 EXPECT_EQ("L'\\0'", Print(L'\0'));
331 EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
332 EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
333 EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
334 EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
335 EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
336 EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
337 EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
338 EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
339 EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
340 EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
341 EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
342 EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
343 EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
344 EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
345 EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
346 EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
347 EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
348 }
349
350 // Test that Int64 provides more storage than wchar_t.
351 TEST(PrintTypeSizeTest, Wchar_t) {
352 EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
353 }
354
355 // Various integer types.
356 TEST(PrintBuiltInTypeTest, Integer) {
357 EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8
358 EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8
359 EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16
360 EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16
361 EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32
362 EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32
363 EXPECT_EQ("18446744073709551615",
364 Print(static_cast<testing::internal::UInt64>(-1))); // uint64
365 EXPECT_EQ("-9223372036854775808",
366 Print(static_cast<testing::internal::Int64>(1) << 63)); // int64
367 }
368
369 // Size types.
370 TEST(PrintBuiltInTypeTest, Size_t) {
371 EXPECT_EQ("1", Print(sizeof('a'))); // size_t.
372 #if !GTEST_OS_WINDOWS
373 // Windows has no ssize_t type.
374 EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t.
375 #endif // !GTEST_OS_WINDOWS
376 }
377
378 // Floating-points.
379 TEST(PrintBuiltInTypeTest, FloatingPoints) {
380 EXPECT_EQ("1.5", Print(1.5f)); // float
381 EXPECT_EQ("-2.5", Print(-2.5)); // double
382 }
383
384 // Since ::std::stringstream::operator<<(const void *) formats the pointer
385 // output differently with different compilers, we have to create the expected
386 // output first and use it as our expectation.
387 static std::string PrintPointer(const void* p) {
388 ::std::stringstream expected_result_stream;
389 expected_result_stream << p;
390 return expected_result_stream.str();
391 }
392
393 // Tests printing C strings.
394
395 // const char*.
396 TEST(PrintCStringTest, Const) {
397 const char* p = "World";
398 EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
399 }
400
401 // char*.
402 TEST(PrintCStringTest, NonConst) {
403 char p[] = "Hi";
404 EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
405 Print(static_cast<char*>(p)));
406 }
407
408 // NULL C string.
409 TEST(PrintCStringTest, Null) {
410 const char* p = NULL;
411 EXPECT_EQ("NULL", Print(p));
412 }
413
414 // Tests that C strings are escaped properly.
415 TEST(PrintCStringTest, EscapesProperly) {
416 const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
417 EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
418 "\\n\\r\\t\\v\\x7F\\xFF a\"",
419 Print(p));
420 }
421
422 // MSVC compiler can be configured to define whar_t as a typedef
423 // of unsigned short. Defining an overload for const wchar_t* in that case
424 // would cause pointers to unsigned shorts be printed as wide strings,
425 // possibly accessing more memory than intended and causing invalid
426 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
427 // wchar_t is implemented as a native type.
428 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
429
430 // const wchar_t*.
431 TEST(PrintWideCStringTest, Const) {
432 const wchar_t* p = L"World";
433 EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
434 }
435
436 // wchar_t*.
437 TEST(PrintWideCStringTest, NonConst) {
438 wchar_t p[] = L"Hi";
439 EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
440 Print(static_cast<wchar_t*>(p)));
441 }
442
443 // NULL wide C string.
444 TEST(PrintWideCStringTest, Null) {
445 const wchar_t* p = NULL;
446 EXPECT_EQ("NULL", Print(p));
447 }
448
449 // Tests that wide C strings are escaped properly.
450 TEST(PrintWideCStringTest, EscapesProperly) {
451 const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r',
452 '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
453 EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f"
454 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
455 Print(static_cast<const wchar_t*>(s)));
456 }
457 #endif // native wchar_t
458
459 // Tests printing pointers to other char types.
460
461 // signed char*.
462 TEST(PrintCharPointerTest, SignedChar) {
463 signed char* p = reinterpret_cast<signed char*>(0x1234);
464 EXPECT_EQ(PrintPointer(p), Print(p));
465 p = NULL;
466 EXPECT_EQ("NULL", Print(p));
467 }
468
469 // const signed char*.
470 TEST(PrintCharPointerTest, ConstSignedChar) {
471 signed char* p = reinterpret_cast<signed char*>(0x1234);
472 EXPECT_EQ(PrintPointer(p), Print(p));
473 p = NULL;
474 EXPECT_EQ("NULL", Print(p));
475 }
476
477 // unsigned char*.
478 TEST(PrintCharPointerTest, UnsignedChar) {
479 unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
480 EXPECT_EQ(PrintPointer(p), Print(p));
481 p = NULL;
482 EXPECT_EQ("NULL", Print(p));
483 }
484
485 // const unsigned char*.
486 TEST(PrintCharPointerTest, ConstUnsignedChar) {
487 const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
488 EXPECT_EQ(PrintPointer(p), Print(p));
489 p = NULL;
490 EXPECT_EQ("NULL", Print(p));
491 }
492
493 // Tests printing pointers to simple, built-in types.
494
495 // bool*.
496 TEST(PrintPointerToBuiltInTypeTest, Bool) {
497 bool* p = reinterpret_cast<bool*>(0xABCD);
498 EXPECT_EQ(PrintPointer(p), Print(p));
499 p = NULL;
500 EXPECT_EQ("NULL", Print(p));
501 }
502
503 // void*.
504 TEST(PrintPointerToBuiltInTypeTest, Void) {
505 void* p = reinterpret_cast<void*>(0xABCD);
506 EXPECT_EQ(PrintPointer(p), Print(p));
507 p = NULL;
508 EXPECT_EQ("NULL", Print(p));
509 }
510
511 // const void*.
512 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
513 const void* p = reinterpret_cast<const void*>(0xABCD);
514 EXPECT_EQ(PrintPointer(p), Print(p));
515 p = NULL;
516 EXPECT_EQ("NULL", Print(p));
517 }
518
519 // Tests printing pointers to pointers.
520 TEST(PrintPointerToPointerTest, IntPointerPointer) {
521 int** p = reinterpret_cast<int**>(0xABCD);
522 EXPECT_EQ(PrintPointer(p), Print(p));
523 p = NULL;
524 EXPECT_EQ("NULL", Print(p));
525 }
526
527 // Tests printing (non-member) function pointers.
528
529 void MyFunction(int /* n */) {}
530
531 TEST(PrintPointerTest, NonMemberFunctionPointer) {
532 // We cannot directly cast &MyFunction to const void* because the
533 // standard disallows casting between pointers to functions and
534 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
535 // this limitation.
536 EXPECT_EQ(
537 PrintPointer(reinterpret_cast<const void*>(
538 reinterpret_cast<internal::BiggestInt>(&MyFunction))),
539 Print(&MyFunction));
540 int (*p)(bool) = NULL; // NOLINT
541 EXPECT_EQ("NULL", Print(p));
542 }
543
544 // An assertion predicate determining whether a one string is a prefix for
545 // another.
546 template <typename StringType>
547 AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
548 if (str.find(prefix, 0) == 0)
549 return AssertionSuccess();
550
551 const bool is_wide_string = sizeof(prefix[0]) > 1;
552 const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
553 return AssertionFailure()
554 << begin_string_quote << prefix << "\" is not a prefix of "
555 << begin_string_quote << str << "\"\n";
556 }
557
558 // Tests printing member variable pointers. Although they are called
559 // pointers, they don't point to a location in the address space.
560 // Their representation is implementation-defined. Thus they will be
561 // printed as raw bytes.
562
563 struct Foo {
564 public:
565 virtual ~Foo() {}
566 int MyMethod(char x) { return x + 1; }
567 virtual char MyVirtualMethod(int /* n */) { return 'a'; }
568
569 int value;
570 };
571
572 TEST(PrintPointerTest, MemberVariablePointer) {
573 EXPECT_TRUE(HasPrefix(Print(&Foo::value),
574 Print(sizeof(&Foo::value)) + "-byte object "));
575 int (Foo::*p) = NULL; // NOLINT
576 EXPECT_TRUE(HasPrefix(Print(p),
577 Print(sizeof(p)) + "-byte object "));
578 }
579
580 // Tests printing member function pointers. Although they are called
581 // pointers, they don't point to a location in the address space.
582 // Their representation is implementation-defined. Thus they will be
583 // printed as raw bytes.
584 TEST(PrintPointerTest, MemberFunctionPointer) {
585 EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod),
586 Print(sizeof(&Foo::MyMethod)) + "-byte object "));
587 EXPECT_TRUE(
588 HasPrefix(Print(&Foo::MyVirtualMethod),
589 Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
590 int (Foo::*p)(char) = NULL; // NOLINT
591 EXPECT_TRUE(HasPrefix(Print(p),
592 Print(sizeof(p)) + "-byte object "));
593 }
594
595 // Tests printing C arrays.
596
597 // The difference between this and Print() is that it ensures that the
598 // argument is a reference to an array.
599 template <typename T, size_t N>
600 std::string PrintArrayHelper(T (&a)[N]) {
601 return Print(a);
602 }
603
604 // One-dimensional array.
605 TEST(PrintArrayTest, OneDimensionalArray) {
606 int a[5] = { 1, 2, 3, 4, 5 };
607 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
608 }
609
610 // Two-dimensional array.
611 TEST(PrintArrayTest, TwoDimensionalArray) {
612 int a[2][5] = {
613 { 1, 2, 3, 4, 5 },
614 { 6, 7, 8, 9, 0 }
615 };
616 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
617 }
618
619 // Array of const elements.
620 TEST(PrintArrayTest, ConstArray) {
621 const bool a[1] = { false };
622 EXPECT_EQ("{ false }", PrintArrayHelper(a));
623 }
624
625 // char array without terminating NUL.
626 TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
627 // Array a contains '\0' in the middle and doesn't end with '\0'.
628 char a[] = { 'H', '\0', 'i' };
629 EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
630 }
631
632 // const char array with terminating NUL.
633 TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) {
634 const char a[] = "\0Hi";
635 EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a));
636 }
637
638 // const wchar_t array without terminating NUL.
639 TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
640 // Array a contains '\0' in the middle and doesn't end with '\0'.
641 const wchar_t a[] = { L'H', L'\0', L'i' };
642 EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
643 }
644
645 // wchar_t array with terminating NUL.
646 TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
647 const wchar_t a[] = L"\0Hi";
648 EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a));
649 }
650
651 // Array of objects.
652 TEST(PrintArrayTest, ObjectArray) {
653 std::string a[3] = {"Hi", "Hello", "Ni hao"};
654 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
655 }
656
657 // Array with many elements.
658 TEST(PrintArrayTest, BigArray) {
659 int a[100] = { 1, 2, 3 };
660 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
661 PrintArrayHelper(a));
662 }
663
664 // Tests printing ::string and ::std::string.
665
666 #if GTEST_HAS_GLOBAL_STRING
667 // ::string.
668 TEST(PrintStringTest, StringInGlobalNamespace) {
669 const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
670 const ::string str(s, sizeof(s));
671 EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
672 Print(str));
673 }
674 #endif // GTEST_HAS_GLOBAL_STRING
675
676 // ::std::string.
677 TEST(PrintStringTest, StringInStdNamespace) {
678 const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
679 const ::std::string str(s, sizeof(s));
680 EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
681 Print(str));
682 }
683
684 TEST(PrintStringTest, StringAmbiguousHex) {
685 // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
686 // '\x6', '\x6B', or '\x6BA'.
687
688 // a hex escaping sequence following by a decimal digit
689 EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
690 // a hex escaping sequence following by a hex digit (lower-case)
691 EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
692 // a hex escaping sequence following by a hex digit (upper-case)
693 EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
694 // a hex escaping sequence following by a non-xdigit
695 EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
696 }
697
698 // Tests printing ::wstring and ::std::wstring.
699
700 #if GTEST_HAS_GLOBAL_WSTRING
701 // ::wstring.
702 TEST(PrintWideStringTest, StringInGlobalNamespace) {
703 const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
704 const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
705 EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
706 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
707 Print(str));
708 }
709 #endif // GTEST_HAS_GLOBAL_WSTRING
710
711 #if GTEST_HAS_STD_WSTRING
712 // ::std::wstring.
713 TEST(PrintWideStringTest, StringInStdNamespace) {
714 const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
715 const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
716 EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
717 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
718 Print(str));
719 }
720
721 TEST(PrintWideStringTest, StringAmbiguousHex) {
722 // same for wide strings.
723 EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3")));
724 EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
725 Print(::std::wstring(L"mm\x6" L"bananas")));
726 EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
727 Print(::std::wstring(L"NOM\x6" L"BANANA")));
728 EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
729 }
730 #endif // GTEST_HAS_STD_WSTRING
731
732 // Tests printing types that support generic streaming (i.e. streaming
733 // to std::basic_ostream<Char, CharTraits> for any valid Char and
734 // CharTraits types).
735
736 // Tests printing a non-template type that supports generic streaming.
737
738 class AllowsGenericStreaming {};
739
740 template <typename Char, typename CharTraits>
741 std::basic_ostream<Char, CharTraits>& operator<<(
742 std::basic_ostream<Char, CharTraits>& os,
743 const AllowsGenericStreaming& /* a */) {
744 return os << "AllowsGenericStreaming";
745 }
746
747 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
748 AllowsGenericStreaming a;
749 EXPECT_EQ("AllowsGenericStreaming", Print(a));
750 }
751
752 // Tests printing a template type that supports generic streaming.
753
754 template <typename T>
755 class AllowsGenericStreamingTemplate {};
756
757 template <typename Char, typename CharTraits, typename T>
758 std::basic_ostream<Char, CharTraits>& operator<<(
759 std::basic_ostream<Char, CharTraits>& os,
760 const AllowsGenericStreamingTemplate<T>& /* a */) {
761 return os << "AllowsGenericStreamingTemplate";
762 }
763
764 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
765 AllowsGenericStreamingTemplate<int> a;
766 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
767 }
768
769 // Tests printing a type that supports generic streaming and can be
770 // implicitly converted to another printable type.
771
772 template <typename T>
773 class AllowsGenericStreamingAndImplicitConversionTemplate {
774 public:
775 operator bool() const { return false; }
776 };
777
778 template <typename Char, typename CharTraits, typename T>
779 std::basic_ostream<Char, CharTraits>& operator<<(
780 std::basic_ostream<Char, CharTraits>& os,
781 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) {
782 return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
783 }
784
785 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
786 AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
787 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
788 }
789
790 #if GTEST_HAS_ABSL
791
792 // Tests printing ::absl::string_view.
793
794 TEST(PrintStringViewTest, SimpleStringView) {
795 const ::absl::string_view sp = "Hello";
796 EXPECT_EQ("\"Hello\"", Print(sp));
797 }
798
799 TEST(PrintStringViewTest, UnprintableCharacters) {
800 const char str[] = "NUL (\0) and \r\t";
801 const ::absl::string_view sp(str, sizeof(str) - 1);
802 EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
803 }
804
805 #endif // GTEST_HAS_ABSL
806
807 // Tests printing STL containers.
808
809 TEST(PrintStlContainerTest, EmptyDeque) {
810 deque<char> empty;
811 EXPECT_EQ("{}", Print(empty));
812 }
813
814 TEST(PrintStlContainerTest, NonEmptyDeque) {
815 deque<int> non_empty;
816 non_empty.push_back(1);
817 non_empty.push_back(3);
818 EXPECT_EQ("{ 1, 3 }", Print(non_empty));
819 }
820
821 #if GTEST_HAS_UNORDERED_MAP_
822
823 TEST(PrintStlContainerTest, OneElementHashMap) {
824 ::std::unordered_map<int, char> map1;
825 map1[1] = 'a';
826 EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1));
827 }
828
829 TEST(PrintStlContainerTest, HashMultiMap) {
830 ::std::unordered_multimap<int, bool> map1;
831 map1.insert(make_pair(5, true));
832 map1.insert(make_pair(5, false));
833
834 // Elements of hash_multimap can be printed in any order.
835 const std::string result = Print(map1);
836 EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
837 result == "{ (5, false), (5, true) }")
838 << " where Print(map1) returns \"" << result << "\".";
839 }
840
841 #endif // GTEST_HAS_UNORDERED_MAP_
842
843 #if GTEST_HAS_UNORDERED_SET_
844
845 TEST(PrintStlContainerTest, HashSet) {
846 ::std::unordered_set<int> set1;
847 set1.insert(1);
848 EXPECT_EQ("{ 1 }", Print(set1));
849 }
850
851 TEST(PrintStlContainerTest, HashMultiSet) {
852 const int kSize = 5;
853 int a[kSize] = { 1, 1, 2, 5, 1 };
854 ::std::unordered_multiset<int> set1(a, a + kSize);
855
856 // Elements of hash_multiset can be printed in any order.
857 const std::string result = Print(set1);
858 const std::string expected_pattern = "{ d, d, d, d, d }"; // d means a digit.
859
860 // Verifies the result matches the expected pattern; also extracts
861 // the numbers in the result.
862 ASSERT_EQ(expected_pattern.length(), result.length());
863 std::vector<int> numbers;
864 for (size_t i = 0; i != result.length(); i++) {
865 if (expected_pattern[i] == 'd') {
866 ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0);
867 numbers.push_back(result[i] - '0');
868 } else {
869 EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
870 << result;
871 }
872 }
873
874 // Makes sure the result contains the right numbers.
875 std::sort(numbers.begin(), numbers.end());
876 std::sort(a, a + kSize);
877 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
878 }
879
880 #endif // GTEST_HAS_UNORDERED_SET_
881
882 TEST(PrintStlContainerTest, List) {
883 const std::string a[] = {"hello", "world"};
884 const list<std::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
989 struct const_iterator {
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) {
1003 ::std::tr1::tuple<> t0;
1004 EXPECT_EQ("()", Print(t0));
1005
1006 ::std::tr1::tuple<int> t1(5);
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
1012 ::std::tr1::tuple<bool, int, int> t3(false, 2, 3);
1013 EXPECT_EQ("(false, 2, 3)", Print(t3));
1014
1015 ::std::tr1::tuple<bool, int, int, int> t4(false, 2, 3, 4);
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
1024 ::std::tr1::tuple<bool, int, int, int, bool, int, int> t7(
1025 false, 2, 3, 4, true, 6, 7);
1026 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
1027
1028 ::std::tr1::tuple<bool, int, int, int, bool, int, int, bool> t8(
1029 false, 2, 3, 4, true, 6, 7, true);
1030 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
1031
1032 ::std::tr1::tuple<bool, int, int, int, bool, int, int, bool, int> t9(
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*,
1041 std::string>
1042 t10(false, 'a', static_cast<short>(3), 4, 5, 1.5F, -2.5, str, // NOLINT
1043 ImplicitCast_<void*>(NULL), "10");
1044 EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1045 " pointing to \"8\", NULL, \"10\")",
1046 Print(t10));
1047 }
1048
1049 // Nested tuples.
1050 TEST(PrintTr1TupleTest, NestedTuple) {
1051 ::std::tr1::tuple< ::std::tr1::tuple<int, bool>, char> nested(
1052 ::std::tr1::make_tuple(5, true), 'a');
1053 EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
1054 }
1055
1056 #endif // GTEST_HAS_TR1_TUPLE
1057
1058 #if GTEST_HAS_STD_TUPLE_
1059 // Tests printing ::std::tuples.
1060
1061 // Tuples of various arities.
1062 TEST(PrintStdTupleTest, VariousSizes) {
1063 ::std::tuple<> t0;
1064 EXPECT_EQ("()", Print(t0));
1065
1066 ::std::tuple<int> t1(5);
1067 EXPECT_EQ("(5)", Print(t1));
1068
1069 ::std::tuple<char, bool> t2('a', true);
1070 EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
1071
1072 ::std::tuple<bool, int, int> t3(false, 2, 3);
1073 EXPECT_EQ("(false, 2, 3)", Print(t3));
1074
1075 ::std::tuple<bool, int, int, int> t4(false, 2, 3, 4);
1076 EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
1077
1078 ::std::tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
1079 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
1080
1081 ::std::tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
1082 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
1083
1084 ::std::tuple<bool, int, int, int, bool, int, int> t7(
1085 false, 2, 3, 4, true, 6, 7);
1086 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
1087
1088 ::std::tuple<bool, int, int, int, bool, int, int, bool> t8(
1089 false, 2, 3, 4, true, 6, 7, true);
1090 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
1091
1092 ::std::tuple<bool, int, int, int, bool, int, int, bool, int> t9(
1093 false, 2, 3, 4, true, 6, 7, true, 9);
1094 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
1095
1096 const char* const str = "8";
1097 // VC++ 2010's implementation of tuple of C++0x is deficient, requiring
1098 // an explicit type cast of NULL to be used.
1099 ::std::tuple<bool, char, short, testing::internal::Int32, // NOLINT
1100 testing::internal::Int64, float, double, const char*, void*,
1101 std::string>
1102 t10(false, 'a', static_cast<short>(3), 4, 5, 1.5F, -2.5, str, // NOLINT
1103 ImplicitCast_<void*>(NULL), "10");
1104 EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
1105 " pointing to \"8\", NULL, \"10\")",
1106 Print(t10));
1107 }
1108
1109 // Nested tuples.
1110 TEST(PrintStdTupleTest, NestedTuple) {
1111 ::std::tuple< ::std::tuple<int, bool>, char> nested(
1112 ::std::make_tuple(5, true), 'a');
1113 EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
1114 }
1115
1116 #endif // GTEST_LANG_CXX11
1117
1118 #if GTEST_LANG_CXX11
1119 TEST(PrintNullptrT, Basic) {
1120 EXPECT_EQ("(nullptr)", Print(nullptr));
1121 }
1122 #endif // GTEST_LANG_CXX11
1123
1124 // Tests printing user-defined unprintable types.
1125
1126 // Unprintable types in the global namespace.
1127 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
1128 EXPECT_EQ("1-byte object <00>",
1129 Print(UnprintableTemplateInGlobal<char>()));
1130 }
1131
1132 // Unprintable types in a user namespace.
1133 TEST(PrintUnprintableTypeTest, InUserNamespace) {
1134 EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1135 Print(::foo::UnprintableInFoo()));
1136 }
1137
1138 // Unprintable types are that too big to be printed completely.
1139
1140 struct Big {
1141 Big() { memset(array, 0, sizeof(array)); }
1142 char array[257];
1143 };
1144
1145 TEST(PrintUnpritableTypeTest, BigObject) {
1146 EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
1147 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1148 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1149 "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
1150 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1151 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
1152 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
1153 Print(Big()));
1154 }
1155
1156 // Tests printing user-defined streamable types.
1157
1158 // Streamable types in the global namespace.
1159 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
1160 StreamableInGlobal x;
1161 EXPECT_EQ("StreamableInGlobal", Print(x));
1162 EXPECT_EQ("StreamableInGlobal*", Print(&x));
1163 }
1164
1165 // Printable template types in a user namespace.
1166 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
1167 EXPECT_EQ("StreamableTemplateInFoo: 0",
1168 Print(::foo::StreamableTemplateInFoo<int>()));
1169 }
1170
1171 // Tests printing a user-defined recursive container type that has a <<
1172 // operator.
1173 TEST(PrintStreamableTypeTest, PathLikeInUserNamespace) {
1174 ::foo::PathLike x;
1175 EXPECT_EQ("Streamable-PathLike", Print(x));
1176 const ::foo::PathLike cx;
1177 EXPECT_EQ("Streamable-PathLike", Print(cx));
1178 }
1179
1180 // Tests printing user-defined types that have a PrintTo() function.
1181 TEST(PrintPrintableTypeTest, InUserNamespace) {
1182 EXPECT_EQ("PrintableViaPrintTo: 0",
1183 Print(::foo::PrintableViaPrintTo()));
1184 }
1185
1186 // Tests printing a pointer to a user-defined type that has a <<
1187 // operator for its pointer.
1188 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
1189 ::foo::PointerPrintable x;
1190 EXPECT_EQ("PointerPrintable*", Print(&x));
1191 }
1192
1193 // Tests printing user-defined class template that have a PrintTo() function.
1194 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
1195 EXPECT_EQ("PrintableViaPrintToTemplate: 5",
1196 Print(::foo::PrintableViaPrintToTemplate<int>(5)));
1197 }
1198
1199 // Tests that the universal printer prints both the address and the
1200 // value of a reference.
1201 TEST(PrintReferenceTest, PrintsAddressAndValue) {
1202 int n = 5;
1203 EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
1204
1205 int a[2][3] = {
1206 { 0, 1, 2 },
1207 { 3, 4, 5 }
1208 };
1209 EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
1210 PrintByRef(a));
1211
1212 const ::foo::UnprintableInFoo x;
1213 EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
1214 "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
1215 PrintByRef(x));
1216 }
1217
1218 // Tests that the universal printer prints a function pointer passed by
1219 // reference.
1220 TEST(PrintReferenceTest, HandlesFunctionPointer) {
1221 void (*fp)(int n) = &MyFunction;
1222 const std::string fp_pointer_string =
1223 PrintPointer(reinterpret_cast<const void*>(&fp));
1224 // We cannot directly cast &MyFunction to const void* because the
1225 // standard disallows casting between pointers to functions and
1226 // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
1227 // this limitation.
1228 const std::string fp_string = PrintPointer(reinterpret_cast<const void*>(
1229 reinterpret_cast<internal::BiggestInt>(fp)));
1230 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
1231 PrintByRef(fp));
1232 }
1233
1234 // Tests that the universal printer prints a member function pointer
1235 // passed by reference.
1236 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
1237 int (Foo::*p)(char ch) = &Foo::MyMethod;
1238 EXPECT_TRUE(HasPrefix(
1239 PrintByRef(p),
1240 "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
1241 Print(sizeof(p)) + "-byte object "));
1242
1243 char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
1244 EXPECT_TRUE(HasPrefix(
1245 PrintByRef(p2),
1246 "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
1247 Print(sizeof(p2)) + "-byte object "));
1248 }
1249
1250 // Tests that the universal printer prints a member variable pointer
1251 // passed by reference.
1252 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
1253 int (Foo::*p) = &Foo::value; // NOLINT
1254 EXPECT_TRUE(HasPrefix(
1255 PrintByRef(p),
1256 "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
1257 }
1258
1259 // Tests that FormatForComparisonFailureMessage(), which is used to print
1260 // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion
1261 // fails, formats the operand in the desired way.
1262
1263 // scalar
1264 TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
1265 EXPECT_STREQ("123",
1266 FormatForComparisonFailureMessage(123, 124).c_str());
1267 }
1268
1269 // non-char pointer
1270 TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
1271 int n = 0;
1272 EXPECT_EQ(PrintPointer(&n),
1273 FormatForComparisonFailureMessage(&n, &n).c_str());
1274 }
1275
1276 // non-char array
1277 TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
1278 // In expression 'array == x', 'array' is compared by pointer.
1279 // Therefore we want to print an array operand as a pointer.
1280 int n[] = { 1, 2, 3 };
1281 EXPECT_EQ(PrintPointer(n),
1282 FormatForComparisonFailureMessage(n, n).c_str());
1283 }
1284
1285 // Tests formatting a char pointer when it's compared with another pointer.
1286 // In this case we want to print it as a raw pointer, as the comparison is by
1287 // pointer.
1288
1289 // char pointer vs pointer
1290 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
1291 // In expression 'p == x', where 'p' and 'x' are (const or not) char
1292 // pointers, the operands are compared by pointer. Therefore we
1293 // want to print 'p' as a pointer instead of a C string (we don't
1294 // even know if it's supposed to point to a valid C string).
1295
1296 // const char*
1297 const char* s = "hello";
1298 EXPECT_EQ(PrintPointer(s),
1299 FormatForComparisonFailureMessage(s, s).c_str());
1300
1301 // char*
1302 char ch = 'a';
1303 EXPECT_EQ(PrintPointer(&ch),
1304 FormatForComparisonFailureMessage(&ch, &ch).c_str());
1305 }
1306
1307 // wchar_t pointer vs pointer
1308 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
1309 // In expression 'p == x', where 'p' and 'x' are (const or not) char
1310 // pointers, the operands are compared by pointer. Therefore we
1311 // want to print 'p' as a pointer instead of a wide C string (we don't
1312 // even know if it's supposed to point to a valid wide C string).
1313
1314 // const wchar_t*
1315 const wchar_t* s = L"hello";
1316 EXPECT_EQ(PrintPointer(s),
1317 FormatForComparisonFailureMessage(s, s).c_str());
1318
1319 // wchar_t*
1320 wchar_t ch = L'a';
1321 EXPECT_EQ(PrintPointer(&ch),
1322 FormatForComparisonFailureMessage(&ch, &ch).c_str());
1323 }
1324
1325 // Tests formatting a char pointer when it's compared to a string object.
1326 // In this case we want to print the char pointer as a C string.
1327
1328 #if GTEST_HAS_GLOBAL_STRING
1329 // char pointer vs ::string
1330 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) {
1331 const char* s = "hello \"world";
1332 EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1333 FormatForComparisonFailureMessage(s, ::string()).c_str());
1334
1335 // char*
1336 char str[] = "hi\1";
1337 char* p = str;
1338 EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1339 FormatForComparisonFailureMessage(p, ::string()).c_str());
1340 }
1341 #endif
1342
1343 // char pointer vs std::string
1344 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
1345 const char* s = "hello \"world";
1346 EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped.
1347 FormatForComparisonFailureMessage(s, ::std::string()).c_str());
1348
1349 // char*
1350 char str[] = "hi\1";
1351 char* p = str;
1352 EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped.
1353 FormatForComparisonFailureMessage(p, ::std::string()).c_str());
1354 }
1355
1356 #if GTEST_HAS_GLOBAL_WSTRING
1357 // wchar_t pointer vs ::wstring
1358 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) {
1359 const wchar_t* s = L"hi \"world";
1360 EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1361 FormatForComparisonFailureMessage(s, ::wstring()).c_str());
1362
1363 // wchar_t*
1364 wchar_t str[] = L"hi\1";
1365 wchar_t* p = str;
1366 EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1367 FormatForComparisonFailureMessage(p, ::wstring()).c_str());
1368 }
1369 #endif
1370
1371 #if GTEST_HAS_STD_WSTRING
1372 // wchar_t pointer vs std::wstring
1373 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
1374 const wchar_t* s = L"hi \"world";
1375 EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped.
1376 FormatForComparisonFailureMessage(s, ::std::wstring()).c_str());
1377
1378 // wchar_t*
1379 wchar_t str[] = L"hi\1";
1380 wchar_t* p = str;
1381 EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped.
1382 FormatForComparisonFailureMessage(p, ::std::wstring()).c_str());
1383 }
1384 #endif
1385
1386 // Tests formatting a char array when it's compared with a pointer or array.
1387 // In this case we want to print the array as a row pointer, as the comparison
1388 // is by pointer.
1389
1390 // char array vs pointer
1391 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
1392 char str[] = "hi \"world\"";
1393 char* p = NULL;
1394 EXPECT_EQ(PrintPointer(str),
1395 FormatForComparisonFailureMessage(str, p).c_str());
1396 }
1397
1398 // char array vs char array
1399 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
1400 const char str[] = "hi \"world\"";
1401 EXPECT_EQ(PrintPointer(str),
1402 FormatForComparisonFailureMessage(str, str).c_str());
1403 }
1404
1405 // wchar_t array vs pointer
1406 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
1407 wchar_t str[] = L"hi \"world\"";
1408 wchar_t* p = NULL;
1409 EXPECT_EQ(PrintPointer(str),
1410 FormatForComparisonFailureMessage(str, p).c_str());
1411 }
1412
1413 // wchar_t array vs wchar_t array
1414 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
1415 const wchar_t str[] = L"hi \"world\"";
1416 EXPECT_EQ(PrintPointer(str),
1417 FormatForComparisonFailureMessage(str, str).c_str());
1418 }
1419
1420 // Tests formatting a char array when it's compared with a string object.
1421 // In this case we want to print the array as a C string.
1422
1423 #if GTEST_HAS_GLOBAL_STRING
1424 // char array vs string
1425 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) {
1426 const char str[] = "hi \"w\0rld\"";
1427 EXPECT_STREQ("\"hi \\\"w\"", // The content should be escaped.
1428 // Embedded NUL terminates the string.
1429 FormatForComparisonFailureMessage(str, ::string()).c_str());
1430 }
1431 #endif
1432
1433 // char array vs std::string
1434 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
1435 const char str[] = "hi \"world\"";
1436 EXPECT_STREQ("\"hi \\\"world\\\"\"", // The content should be escaped.
1437 FormatForComparisonFailureMessage(str, ::std::string()).c_str());
1438 }
1439
1440 #if GTEST_HAS_GLOBAL_WSTRING
1441 // wchar_t array vs wstring
1442 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) {
1443 const wchar_t str[] = L"hi \"world\"";
1444 EXPECT_STREQ("L\"hi \\\"world\\\"\"", // The content should be escaped.
1445 FormatForComparisonFailureMessage(str, ::wstring()).c_str());
1446 }
1447 #endif
1448
1449 #if GTEST_HAS_STD_WSTRING
1450 // wchar_t array vs std::wstring
1451 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
1452 const wchar_t str[] = L"hi \"w\0rld\"";
1453 EXPECT_STREQ(
1454 "L\"hi \\\"w\"", // The content should be escaped.
1455 // Embedded NUL terminates the string.
1456 FormatForComparisonFailureMessage(str, ::std::wstring()).c_str());
1457 }
1458 #endif
1459
1460 // Useful for testing PrintToString(). We cannot use EXPECT_EQ()
1461 // there as its implementation uses PrintToString(). The caller must
1462 // ensure that 'value' has no side effect.
1463 #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
1464 EXPECT_TRUE(PrintToString(value) == (expected_string)) \
1465 << " where " #value " prints as " << (PrintToString(value))
1466
1467 TEST(PrintToStringTest, WorksForScalar) {
1468 EXPECT_PRINT_TO_STRING_(123, "123");
1469 }
1470
1471 TEST(PrintToStringTest, WorksForPointerToConstChar) {
1472 const char* p = "hello";
1473 EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
1474 }
1475
1476 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
1477 char s[] = "hello";
1478 char* p = s;
1479 EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
1480 }
1481
1482 TEST(PrintToStringTest, EscapesForPointerToConstChar) {
1483 const char* p = "hello\n";
1484 EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\"");
1485 }
1486
1487 TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
1488 char s[] = "hello\1";
1489 char* p = s;
1490 EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\"");
1491 }
1492
1493 TEST(PrintToStringTest, WorksForArray) {
1494 int n[3] = { 1, 2, 3 };
1495 EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
1496 }
1497
1498 TEST(PrintToStringTest, WorksForCharArray) {
1499 char s[] = "hello";
1500 EXPECT_PRINT_TO_STRING_(s, "\"hello\"");
1501 }
1502
1503 TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
1504 const char str_with_nul[] = "hello\0 world";
1505 EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\"");
1506
1507 char mutable_str_with_nul[] = "hello\0 world";
1508 EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\"");
1509 }
1510
1511 TEST(PrintToStringTest, ContainsNonLatin) {
1512 // Sanity test with valid UTF-8. Prints both in hex and as text.
1513 std::string non_ascii_str = ::std::string("오전 4:30");
1514 EXPECT_PRINT_TO_STRING_(non_ascii_str,
1515 "\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
1516 " As Text: \"오전 4:30\"");
1517 non_ascii_str = ::std::string("From ä — ẑ");
1518 EXPECT_PRINT_TO_STRING_(non_ascii_str,
1519 "\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
1520 "\n As Text: \"From ä — ẑ\"");
1521 }
1522
1523 TEST(IsValidUTF8Test, IllFormedUTF8) {
1524 // The following test strings are ill-formed UTF-8 and are printed
1525 // as hex only (or ASCII, in case of ASCII bytes) because IsValidUTF8() is
1526 // expected to fail, thus output does not contain "As Text:".
1527
1528 static const char *const kTestdata[][2] = {
1529 // 2-byte lead byte followed by a single-byte character.
1530 {"\xC3\x74", "\"\\xC3t\""},
1531 // Valid 2-byte character followed by an orphan trail byte.
1532 {"\xC3\x84\xA4", "\"\\xC3\\x84\\xA4\""},
1533 // Lead byte without trail byte.
1534 {"abc\xC3", "\"abc\\xC3\""},
1535 // 3-byte lead byte, single-byte character, orphan trail byte.
1536 {"x\xE2\x70\x94", "\"x\\xE2p\\x94\""},
1537 // Truncated 3-byte character.
1538 {"\xE2\x80", "\"\\xE2\\x80\""},
1539 // Truncated 3-byte character followed by valid 2-byte char.
1540 {"\xE2\x80\xC3\x84", "\"\\xE2\\x80\\xC3\\x84\""},
1541 // Truncated 3-byte character followed by a single-byte character.
1542 {"\xE2\x80\x7A", "\"\\xE2\\x80z\""},
1543 // 3-byte lead byte followed by valid 3-byte character.
1544 {"\xE2\xE2\x80\x94", "\"\\xE2\\xE2\\x80\\x94\""},
1545 // 4-byte lead byte followed by valid 3-byte character.
1546 {"\xF0\xE2\x80\x94", "\"\\xF0\\xE2\\x80\\x94\""},
1547 // Truncated 4-byte character.
1548 {"\xF0\xE2\x80", "\"\\xF0\\xE2\\x80\""},
1549 // Invalid UTF-8 byte sequences embedded in other chars.
1550 {"abc\xE2\x80\x94\xC3\x74xyc", "\"abc\\xE2\\x80\\x94\\xC3txyc\""},
1551 {"abc\xC3\x84\xE2\x80\xC3\x84xyz",
1552 "\"abc\\xC3\\x84\\xE2\\x80\\xC3\\x84xyz\""},
1553 // Non-shortest UTF-8 byte sequences are also ill-formed.
1554 // The classics: xC0, xC1 lead byte.
1555 {"\xC0\x80", "\"\\xC0\\x80\""},
1556 {"\xC1\x81", "\"\\xC1\\x81\""},
1557 // Non-shortest sequences.
1558 {"\xE0\x80\x80", "\"\\xE0\\x80\\x80\""},
1559 {"\xf0\x80\x80\x80", "\"\\xF0\\x80\\x80\\x80\""},
1560 // Last valid code point before surrogate range, should be printed as text,
1561 // too.
1562 {"\xED\x9F\xBF", "\"\\xED\\x9F\\xBF\"\n As Text: \"퟿\""},
1563 // Start of surrogate lead. Surrogates are not printed as text.
1564 {"\xED\xA0\x80", "\"\\xED\\xA0\\x80\""},
1565 // Last non-private surrogate lead.
1566 {"\xED\xAD\xBF", "\"\\xED\\xAD\\xBF\""},
1567 // First private-use surrogate lead.
1568 {"\xED\xAE\x80", "\"\\xED\\xAE\\x80\""},
1569 // Last private-use surrogate lead.
1570 {"\xED\xAF\xBF", "\"\\xED\\xAF\\xBF\""},
1571 // Mid-point of surrogate trail.
1572 {"\xED\xB3\xBF", "\"\\xED\\xB3\\xBF\""},
1573 // First valid code point after surrogate range, should be printed as text,
1574 // too.
1575 {"\xEE\x80\x80", "\"\\xEE\\x80\\x80\"\n As Text: \"\""}
1576 };
1577
1578 for (int i = 0; i < int(sizeof(kTestdata)/sizeof(kTestdata[0])); ++i) {
1579 EXPECT_PRINT_TO_STRING_(kTestdata[i][0], kTestdata[i][1]);
1580 }
1581 }
1582
1583 #undef EXPECT_PRINT_TO_STRING_
1584
1585 TEST(UniversalTersePrintTest, WorksForNonReference) {
1586 ::std::stringstream ss;
1587 UniversalTersePrint(123, &ss);
1588 EXPECT_EQ("123", ss.str());
1589 }
1590
1591 TEST(UniversalTersePrintTest, WorksForReference) {
1592 const int& n = 123;
1593 ::std::stringstream ss;
1594 UniversalTersePrint(n, &ss);
1595 EXPECT_EQ("123", ss.str());
1596 }
1597
1598 TEST(UniversalTersePrintTest, WorksForCString) {
1599 const char* s1 = "abc";
1600 ::std::stringstream ss1;
1601 UniversalTersePrint(s1, &ss1);
1602 EXPECT_EQ("\"abc\"", ss1.str());
1603
1604 char* s2 = const_cast<char*>(s1);
1605 ::std::stringstream ss2;
1606 UniversalTersePrint(s2, &ss2);
1607 EXPECT_EQ("\"abc\"", ss2.str());
1608
1609 const char* s3 = NULL;
1610 ::std::stringstream ss3;
1611 UniversalTersePrint(s3, &ss3);
1612 EXPECT_EQ("NULL", ss3.str());
1613 }
1614
1615 TEST(UniversalPrintTest, WorksForNonReference) {
1616 ::std::stringstream ss;
1617 UniversalPrint(123, &ss);
1618 EXPECT_EQ("123", ss.str());
1619 }
1620
1621 TEST(UniversalPrintTest, WorksForReference) {
1622 const int& n = 123;
1623 ::std::stringstream ss;
1624 UniversalPrint(n, &ss);
1625 EXPECT_EQ("123", ss.str());
1626 }
1627
1628 TEST(UniversalPrintTest, WorksForCString) {
1629 const char* s1 = "abc";
1630 ::std::stringstream ss1;
1631 UniversalPrint(s1, &ss1);
1632 EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", std::string(ss1.str()));
1633
1634 char* s2 = const_cast<char*>(s1);
1635 ::std::stringstream ss2;
1636 UniversalPrint(s2, &ss2);
1637 EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", std::string(ss2.str()));
1638
1639 const char* s3 = NULL;
1640 ::std::stringstream ss3;
1641 UniversalPrint(s3, &ss3);
1642 EXPECT_EQ("NULL", ss3.str());
1643 }
1644
1645 TEST(UniversalPrintTest, WorksForCharArray) {
1646 const char str[] = "\"Line\0 1\"\nLine 2";
1647 ::std::stringstream ss1;
1648 UniversalPrint(str, &ss1);
1649 EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
1650
1651 const char mutable_str[] = "\"Line\0 1\"\nLine 2";
1652 ::std::stringstream ss2;
1653 UniversalPrint(mutable_str, &ss2);
1654 EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
1655 }
1656
1657 #if GTEST_HAS_TR1_TUPLE
1658
1659 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsEmptyTuple) {
1660 Strings result = UniversalTersePrintTupleFieldsToStrings(
1661 ::std::tr1::make_tuple());
1662 EXPECT_EQ(0u, result.size());
1663 }
1664
1665 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsOneTuple) {
1666 Strings result = UniversalTersePrintTupleFieldsToStrings(
1667 ::std::tr1::make_tuple(1));
1668 ASSERT_EQ(1u, result.size());
1669 EXPECT_EQ("1", result[0]);
1670 }
1671
1672 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTwoTuple) {
1673 Strings result = UniversalTersePrintTupleFieldsToStrings(
1674 ::std::tr1::make_tuple(1, 'a'));
1675 ASSERT_EQ(2u, result.size());
1676 EXPECT_EQ("1", result[0]);
1677 EXPECT_EQ("'a' (97, 0x61)", result[1]);
1678 }
1679
1680 TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTersely) {
1681 const int n = 1;
1682 Strings result = UniversalTersePrintTupleFieldsToStrings(
1683 ::std::tr1::tuple<const int&, const char*>(n, "a"));
1684 ASSERT_EQ(2u, result.size());
1685 EXPECT_EQ("1", result[0]);
1686 EXPECT_EQ("\"a\"", result[1]);
1687 }
1688
1689 #endif // GTEST_HAS_TR1_TUPLE
1690
1691 #if GTEST_HAS_STD_TUPLE_
1692
1693 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
1694 Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
1695 EXPECT_EQ(0u, result.size());
1696 }
1697
1698 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
1699 Strings result = UniversalTersePrintTupleFieldsToStrings(
1700 ::std::make_tuple(1));
1701 ASSERT_EQ(1u, result.size());
1702 EXPECT_EQ("1", result[0]);
1703 }
1704
1705 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
1706 Strings result = UniversalTersePrintTupleFieldsToStrings(
1707 ::std::make_tuple(1, 'a'));
1708 ASSERT_EQ(2u, result.size());
1709 EXPECT_EQ("1", result[0]);
1710 EXPECT_EQ("'a' (97, 0x61)", result[1]);
1711 }
1712
1713 TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
1714 const int n = 1;
1715 Strings result = UniversalTersePrintTupleFieldsToStrings(
1716 ::std::tuple<const int&, const char*>(n, "a"));
1717 ASSERT_EQ(2u, result.size());
1718 EXPECT_EQ("1", result[0]);
1719 EXPECT_EQ("\"a\"", result[1]);
1720 }
1721
1722 #endif // GTEST_HAS_STD_TUPLE_
1723
1724 #if GTEST_HAS_ABSL
1725
1726 TEST(PrintOptionalTest, Basic) {
1727 absl::optional<int> value;
1728 EXPECT_EQ("(nullopt)", PrintToString(value));
1729 value = {7};
1730 EXPECT_EQ("(7)", PrintToString(value));
1731 EXPECT_EQ("(1.1)", PrintToString(absl::optional<double>{1.1}));
1732 EXPECT_EQ("(\"A\")", PrintToString(absl::optional<std::string>{"A"}));
1733 }
1734 #endif // GTEST_HAS_ABSL
1735
1736 } // namespace gtest_printers_test
1737 } // namespace testing