You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1638 lines
51 KiB

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