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.

1307 lines
39 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++.
  49. #if _MSC_VER
  50. # define GTEST_HAS_HASH_MAP_ 1 // Indicates that hash_map is available.
  51. # include <hash_map> // NOLINT
  52. # define GTEST_HAS_HASH_SET_ 1 // Indicates that hash_set is available.
  53. # include <hash_set> // NOLINT
  54. #endif // GTEST_OS_WINDOWS
  55. // Some user-defined types for testing the universal value printer.
  56. // An anonymous enum type.
  57. enum AnonymousEnum {
  58. kAE1 = -1,
  59. kAE2 = 1
  60. };
  61. // An enum without a user-defined printer.
  62. enum EnumWithoutPrinter {
  63. kEWP1 = -2,
  64. kEWP2 = 42
  65. };
  66. // An enum with a << operator.
  67. enum EnumWithStreaming {
  68. kEWS1 = 10
  69. };
  70. std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) {
  71. return os << (e == kEWS1 ? "kEWS1" : "invalid");
  72. }
  73. // An enum with a PrintTo() function.
  74. enum EnumWithPrintTo {
  75. kEWPT1 = 1
  76. };
  77. void PrintTo(EnumWithPrintTo e, std::ostream* os) {
  78. *os << (e == kEWPT1 ? "kEWPT1" : "invalid");
  79. }
  80. // A class implicitly convertible to BiggestInt.
  81. class BiggestIntConvertible {
  82. public:
  83. operator ::testing::internal::BiggestInt() const { return 42; }
  84. };
  85. // A user-defined unprintable class template in the global namespace.
  86. template <typename T>
  87. class UnprintableTemplateInGlobal {
  88. public:
  89. UnprintableTemplateInGlobal() : value_() {}
  90. private:
  91. T value_;
  92. };
  93. // A user-defined streamable type in the global namespace.
  94. class StreamableInGlobal {
  95. public:
  96. virtual ~StreamableInGlobal() {}
  97. };
  98. inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) {
  99. os << "StreamableInGlobal";
  100. }
  101. void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) {
  102. os << "StreamableInGlobal*";
  103. }
  104. namespace foo {
  105. // A user-defined unprintable type in a user namespace.
  106. class UnprintableInFoo {
  107. public:
  108. UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); }
  109. private:
  110. char xy_[8];
  111. double z_;
  112. };
  113. // A user-defined printable type in a user-chosen namespace.
  114. struct PrintableViaPrintTo {
  115. PrintableViaPrintTo() : value() {}
  116. int value;
  117. };
  118. void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
  119. *os << "PrintableViaPrintTo: " << x.value;
  120. }
  121. // A type with a user-defined << for printing its pointer.
  122. struct PointerPrintable {
  123. };
  124. ::std::ostream& operator<<(::std::ostream& os,
  125. const PointerPrintable* /* x */) {
  126. return os << "PointerPrintable*";
  127. }
  128. // A user-defined printable class template in a user-chosen namespace.
  129. template <typename T>
  130. class PrintableViaPrintToTemplate {
  131. public:
  132. explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
  133. const T& value() const { return value_; }
  134. private:
  135. T value_;
  136. };
  137. template <typename T>
  138. void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
  139. *os << "PrintableViaPrintToTemplate: " << x.value();
  140. }
  141. // A user-defined streamable class template in a user namespace.
  142. template <typename T>
  143. class StreamableTemplateInFoo {
  144. public:
  145. StreamableTemplateInFoo() : value_() {}
  146. const T& value() const { return value_; }
  147. private:
  148. T value_;
  149. };
  150. template <typename T>
  151. inline ::std::ostream& operator<<(::std::ostream& os,
  152. const StreamableTemplateInFoo<T>& x) {
  153. return os << "StreamableTemplateInFoo: " << x.value();
  154. }
  155. } // namespace foo
  156. namespace testing {
  157. namespace gtest_printers_test {
  158. using ::std::deque;
  159. using ::std::list;
  160. using ::std::make_pair;
  161. using ::std::map;
  162. using ::std::multimap;
  163. using ::std::multiset;
  164. using ::std::pair;
  165. using ::std::set;
  166. using ::std::vector;
  167. using ::testing::PrintToString;
  168. using ::testing::internal::NativeArray;
  169. using ::testing::internal::RE;
  170. using ::testing::internal::Strings;
  171. using ::testing::internal::UniversalTersePrint;
  172. using ::testing::internal::UniversalPrint;
  173. using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
  174. using ::testing::internal::UniversalPrinter;
  175. using ::testing::internal::kReference;
  176. using ::testing::internal::string;
  177. #if GTEST_HAS_TR1_TUPLE
  178. using ::std::tr1::make_tuple;
  179. using ::std::tr1::tuple;
  180. #endif
  181. #if _MSC_VER
  182. // MSVC defines the following classes in the ::stdext namespace while
  183. // gcc defines them in the :: namespace. Note that they are not part
  184. // of the C++ standard.
  185. using ::stdext::hash_map;
  186. using ::stdext::hash_set;
  187. using ::stdext::hash_multimap;
  188. using ::stdext::hash_multiset;
  189. #endif
  190. // Prints a value to a string using the universal value printer. This
  191. // is a helper for testing UniversalPrinter<T>::Print() for various types.
  192. template <typename T>
  193. string Print(const T& value) {
  194. ::std::stringstream ss;
  195. UniversalPrinter<T>::Print(value, &ss);
  196. return ss.str();
  197. }
  198. // Prints a value passed by reference to a string, using the universal
  199. // value printer. This is a helper for testing
  200. // UniversalPrinter<T&>::Print() for various types.
  201. template <typename T>
  202. string PrintByRef(const T& value) {
  203. ::std::stringstream ss;
  204. UniversalPrinter<T&>::Print(value, &ss);
  205. return ss.str();
  206. }
  207. // Tests printing various enum types.
  208. TEST(PrintEnumTest, AnonymousEnum) {
  209. EXPECT_EQ("-1", Print(kAE1));
  210. EXPECT_EQ("1", Print(kAE2));
  211. }
  212. TEST(PrintEnumTest, EnumWithoutPrinter) {
  213. EXPECT_EQ("-2", Print(kEWP1));
  214. EXPECT_EQ("42", Print(kEWP2));
  215. }
  216. TEST(PrintEnumTest, EnumWithStreaming) {
  217. EXPECT_EQ("kEWS1", Print(kEWS1));
  218. EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
  219. }
  220. TEST(PrintEnumTest, EnumWithPrintTo) {
  221. EXPECT_EQ("kEWPT1", Print(kEWPT1));
  222. EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
  223. }
  224. // Tests printing a class implicitly convertible to BiggestInt.
  225. TEST(PrintClassTest, BiggestIntConvertible) {
  226. EXPECT_EQ("42", Print(BiggestIntConvertible()));
  227. }
  228. // Tests printing various char types.
  229. // char.
  230. TEST(PrintCharTest, PlainChar) {
  231. EXPECT_EQ("'\\0'", Print('\0'));
  232. EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
  233. EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
  234. EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
  235. EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
  236. EXPECT_EQ("'\\a' (7)", Print('\a'));
  237. EXPECT_EQ("'\\b' (8)", Print('\b'));
  238. EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
  239. EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
  240. EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
  241. EXPECT_EQ("'\\t' (9)", Print('\t'));
  242. EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
  243. EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
  244. EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
  245. EXPECT_EQ("' ' (32, 0x20)", Print(' '));
  246. EXPECT_EQ("'a' (97, 0x61)", Print('a'));
  247. }
  248. // signed char.
  249. TEST(PrintCharTest, SignedChar) {
  250. EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
  251. EXPECT_EQ("'\\xCE' (-50)",
  252. Print(static_cast<signed char>(-50)));
  253. }
  254. // unsigned char.
  255. TEST(PrintCharTest, UnsignedChar) {
  256. EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
  257. EXPECT_EQ("'b' (98, 0x62)",
  258. Print(static_cast<unsigned char>('b')));
  259. }
  260. // Tests printing other simple, built-in types.
  261. // bool.
  262. TEST(PrintBuiltInTypeTest, Bool) {
  263. EXPECT_EQ("false", Print(false));
  264. EXPECT_EQ("true", Print(true));
  265. }
  266. // wchar_t.
  267. TEST(PrintBuiltInTypeTest, Wchar_t) {
  268. EXPECT_EQ("L'\\0'", Print(L'\0'));
  269. EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
  270. EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
  271. EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
  272. EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
  273. EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
  274. EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
  275. EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
  276. EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
  277. EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
  278. EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
  279. EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
  280. EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
  281. EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
  282. EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
  283. EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
  284. EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
  285. EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
  286. }
  287. // Test that Int64 provides more storage than wchar_t.
  288. TEST(PrintTypeSizeTest, Wchar_t) {
  289. EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
  290. }
  291. // Various integer types.
  292. TEST(PrintBuiltInTypeTest, Integer) {
  293. EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8
  294. EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8
  295. EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16
  296. EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16
  297. EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32
  298. EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32
  299. EXPECT_EQ("18446744073709551615",
  300. Print(static_cast<testing::internal::UInt64>(-1))); // uint64
  301. EXPECT_EQ("-9223372036854775808",
  302. Print(static_cast<testing::internal::Int64>(1) << 63)); // int64
  303. }
  304. // Size types.
  305. TEST(PrintBuiltInTypeTest, Size_t) {
  306. EXPECT_EQ("1", Print(sizeof('a'))); // size_t.
  307. #if !GTEST_OS_WINDOWS
  308. // Windows has no ssize_t type.
  309. EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t.
  310. #endif // !GTEST_OS_WINDOWS
  311. }
  312. // Floating-points.
  313. TEST(PrintBuiltInTypeTest, FloatingPoints) {
  314. EXPECT_EQ("1.5", Print(1.5f)); // float
  315. EXPECT_EQ("-2.5", Print(-2.5)); // double
  316. }
  317. // Since ::std::stringstream::operator<<(const void *) formats the pointer
  318. // output differently with different compilers, we have to create the expected
  319. // output first and use it as our expectation.
  320. static string PrintPointer(const void *p) {
  321. ::std::stringstream expected_result_stream;
  322. expected_result_stream << p;
  323. return expected_result_stream.str();
  324. }
  325. // Tests printing C strings.
  326. // const char*.
  327. TEST(PrintCStringTest, Const) {
  328. const char* p = "World";
  329. EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
  330. }
  331. // char*.
  332. TEST(PrintCStringTest, NonConst) {
  333. char p[] = "Hi";
  334. EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
  335. Print(static_cast<char*>(p)));
  336. }
  337. // NULL C string.
  338. TEST(PrintCStringTest, Null) {
  339. const char* p = NULL;
  340. EXPECT_EQ("NULL", Print(p));
  341. }
  342. // Tests that C strings are escaped properly.
  343. TEST(PrintCStringTest, EscapesProperly) {
  344. const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
  345. EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
  346. "\\n\\r\\t\\v\\x7F\\xFF a\"",
  347. Print(p));
  348. }
  349. // MSVC compiler can be configured to define whar_t as a typedef
  350. // of unsigned short. Defining an overload for const wchar_t* in that case
  351. // would cause pointers to unsigned shorts be printed as wide strings,
  352. // possibly accessing more memory than intended and causing invalid
  353. // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
  354. // wchar_t is implemented as a native type.
  355. #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
  356. // const wchar_t*.
  357. TEST(PrintWideCStringTest, Const) {
  358. const wchar_t* p = L"World";
  359. EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
  360. }
  361. // wchar_t*.
  362. TEST(PrintWideCStringTest, NonConst) {
  363. wchar_t p[] = L"Hi";
  364. EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
  365. Print(static_cast<wchar_t*>(p)));
  366. }
  367. // NULL wide C string.
  368. TEST(PrintWideCStringTest, Null) {
  369. const wchar_t* p = NULL;
  370. EXPECT_EQ("NULL", Print(p));
  371. }
  372. // Tests that wide C strings are escaped properly.
  373. TEST(PrintWideCStringTest, EscapesProperly) {
  374. const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r',
  375. '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
  376. EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f"
  377. "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
  378. Print(static_cast<const wchar_t*>(s)));
  379. }
  380. #endif // native wchar_t
  381. // Tests printing pointers to other char types.
  382. // signed char*.
  383. TEST(PrintCharPointerTest, SignedChar) {
  384. signed char* p = reinterpret_cast<signed char*>(0x1234);
  385. EXPECT_EQ(PrintPointer(p), Print(p));
  386. p = NULL;
  387. EXPECT_EQ("NULL", Print(p));
  388. }
  389. // const signed char*.
  390. TEST(PrintCharPointerTest, ConstSignedChar) {
  391. signed char* p = reinterpret_cast<signed char*>(0x1234);
  392. EXPECT_EQ(PrintPointer(p), Print(p));
  393. p = NULL;
  394. EXPECT_EQ("NULL", Print(p));
  395. }
  396. // unsigned char*.
  397. TEST(PrintCharPointerTest, UnsignedChar) {
  398. unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
  399. EXPECT_EQ(PrintPointer(p), Print(p));
  400. p = NULL;
  401. EXPECT_EQ("NULL", Print(p));
  402. }
  403. // const unsigned char*.
  404. TEST(PrintCharPointerTest, ConstUnsignedChar) {
  405. const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
  406. EXPECT_EQ(PrintPointer(p), Print(p));
  407. p = NULL;
  408. EXPECT_EQ("NULL", Print(p));
  409. }
  410. // Tests printing pointers to simple, built-in types.
  411. // bool*.
  412. TEST(PrintPointerToBuiltInTypeTest, Bool) {
  413. bool* p = reinterpret_cast<bool*>(0xABCD);
  414. EXPECT_EQ(PrintPointer(p), Print(p));
  415. p = NULL;
  416. EXPECT_EQ("NULL", Print(p));
  417. }
  418. // void*.
  419. TEST(PrintPointerToBuiltInTypeTest, Void) {
  420. void* p = reinterpret_cast<void*>(0xABCD);
  421. EXPECT_EQ(PrintPointer(p), Print(p));
  422. p = NULL;
  423. EXPECT_EQ("NULL", Print(p));
  424. }
  425. // const void*.
  426. TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
  427. const void* p = reinterpret_cast<const void*>(0xABCD);
  428. EXPECT_EQ(PrintPointer(p), Print(p));
  429. p = NULL;
  430. EXPECT_EQ("NULL", Print(p));
  431. }
  432. // Tests printing pointers to pointers.
  433. TEST(PrintPointerToPointerTest, IntPointerPointer) {
  434. int** p = reinterpret_cast<int**>(0xABCD);
  435. EXPECT_EQ(PrintPointer(p), Print(p));
  436. p = NULL;
  437. EXPECT_EQ("NULL", Print(p));
  438. }
  439. // Tests printing (non-member) function pointers.
  440. void MyFunction(int /* n */) {}
  441. TEST(PrintPointerTest, NonMemberFunctionPointer) {
  442. // We cannot directly cast &MyFunction to const void* because the
  443. // standard disallows casting between pointers to functions and
  444. // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
  445. // this limitation.
  446. EXPECT_EQ(
  447. PrintPointer(reinterpret_cast<const void*>(
  448. reinterpret_cast<internal::BiggestInt>(&MyFunction))),
  449. Print(&MyFunction));
  450. int (*p)(bool) = NULL; // NOLINT
  451. EXPECT_EQ("NULL", Print(p));
  452. }
  453. // An assertion predicate determining whether a one string is a prefix for
  454. // another.
  455. template <typename StringType>
  456. AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
  457. if (str.find(prefix, 0) == 0)
  458. return AssertionSuccess();
  459. const bool is_wide_string = sizeof(prefix[0]) > 1;
  460. const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
  461. return AssertionFailure()
  462. << begin_string_quote << prefix << "\" is not a prefix of "
  463. << begin_string_quote << str << "\"\n";
  464. }
  465. // Tests printing member variable pointers. Although they are called
  466. // pointers, they don't point to a location in the address space.
  467. // Their representation is implementation-defined. Thus they will be
  468. // printed as raw bytes.
  469. struct Foo {
  470. public:
  471. virtual ~Foo() {}
  472. int MyMethod(char x) { return x + 1; }
  473. virtual char MyVirtualMethod(int /* n */) { return 'a'; }
  474. int value;
  475. };
  476. TEST(PrintPointerTest, MemberVariablePointer) {
  477. EXPECT_TRUE(HasPrefix(Print(&Foo::value),
  478. Print(sizeof(&Foo::value)) + "-byte object "));
  479. int (Foo::*p) = NULL; // NOLINT
  480. EXPECT_TRUE(HasPrefix(Print(p),
  481. Print(sizeof(p)) + "-byte object "));
  482. }
  483. // Tests printing member function pointers. Although they are called
  484. // pointers, they don't point to a location in the address space.
  485. // Their representation is implementation-defined. Thus they will be
  486. // printed as raw bytes.
  487. TEST(PrintPointerTest, MemberFunctionPointer) {
  488. EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod),
  489. Print(sizeof(&Foo::MyMethod)) + "-byte object "));
  490. EXPECT_TRUE(
  491. HasPrefix(Print(&Foo::MyVirtualMethod),
  492. Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
  493. int (Foo::*p)(char) = NULL; // NOLINT
  494. EXPECT_TRUE(HasPrefix(Print(p),
  495. Print(sizeof(p)) + "-byte object "));
  496. }
  497. // Tests printing C arrays.
  498. // The difference between this and Print() is that it ensures that the
  499. // argument is a reference to an array.
  500. template <typename T, size_t N>
  501. string PrintArrayHelper(T (&a)[N]) {
  502. return Print(a);
  503. }
  504. // One-dimensional array.
  505. TEST(PrintArrayTest, OneDimensionalArray) {
  506. int a[5] = { 1, 2, 3, 4, 5 };
  507. EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
  508. }
  509. // Two-dimensional array.
  510. TEST(PrintArrayTest, TwoDimensionalArray) {
  511. int a[2][5] = {
  512. { 1, 2, 3, 4, 5 },
  513. { 6, 7, 8, 9, 0 }
  514. };
  515. EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
  516. }
  517. // Array of const elements.
  518. TEST(PrintArrayTest, ConstArray) {
  519. const bool a[1] = { false };
  520. EXPECT_EQ("{ false }", PrintArrayHelper(a));
  521. }
  522. // Char array.
  523. TEST(PrintArrayTest, CharArray) {
  524. // Array a contains '\0' in the middle and doesn't end with '\0'.
  525. char a[3] = { 'H', '\0', 'i' };
  526. EXPECT_EQ("\"H\\0i\"", PrintArrayHelper(a));
  527. }
  528. // Const char array.
  529. TEST(PrintArrayTest, ConstCharArray) {
  530. const char a[4] = "\0Hi";
  531. EXPECT_EQ("\"\\0Hi\\0\"", PrintArrayHelper(a));
  532. }
  533. // Array of objects.
  534. TEST(PrintArrayTest, ObjectArray) {
  535. string a[3] = { "Hi", "Hello", "Ni hao" };
  536. EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
  537. }
  538. // Array with many elements.
  539. TEST(PrintArrayTest, BigArray) {
  540. int a[100] = { 1, 2, 3 };
  541. EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
  542. PrintArrayHelper(a));
  543. }
  544. // Tests printing ::string and ::std::string.
  545. #if GTEST_HAS_GLOBAL_STRING
  546. // ::string.
  547. TEST(PrintStringTest, StringInGlobalNamespace) {
  548. const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
  549. const ::string str(s, sizeof(s));
  550. EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
  551. Print(str));
  552. }
  553. #endif // GTEST_HAS_GLOBAL_STRING
  554. // ::std::string.
  555. TEST(PrintStringTest, StringInStdNamespace) {
  556. const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
  557. const ::std::string str(s, sizeof(s));
  558. EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
  559. Print(str));
  560. }
  561. TEST(PrintStringTest, StringAmbiguousHex) {
  562. // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of:
  563. // '\x6', '\x6B', or '\x6BA'.
  564. // a hex escaping sequence following by a decimal digit
  565. EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
  566. // a hex escaping sequence following by a hex digit (lower-case)
  567. EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
  568. // a hex escaping sequence following by a hex digit (upper-case)
  569. EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
  570. // a hex escaping sequence following by a non-xdigit
  571. EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
  572. }
  573. // Tests printing ::wstring and ::std::wstring.
  574. #if GTEST_HAS_GLOBAL_WSTRING
  575. // ::wstring.
  576. TEST(PrintWideStringTest, StringInGlobalNamespace) {
  577. const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
  578. const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
  579. EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
  580. "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
  581. Print(str));
  582. }
  583. #endif // GTEST_HAS_GLOBAL_WSTRING
  584. #if GTEST_HAS_STD_WSTRING
  585. // ::std::wstring.
  586. TEST(PrintWideStringTest, StringInStdNamespace) {
  587. const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
  588. const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
  589. EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
  590. "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
  591. Print(str));
  592. }
  593. TEST(PrintWideStringTest, StringAmbiguousHex) {
  594. // same for wide strings.
  595. EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3")));
  596. EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
  597. Print(::std::wstring(L"mm\x6" L"bananas")));
  598. EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
  599. Print(::std::wstring(L"NOM\x6" L"BANANA")));
  600. EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
  601. }
  602. #endif // GTEST_HAS_STD_WSTRING
  603. // Tests printing types that support generic streaming (i.e. streaming
  604. // to std::basic_ostream<Char, CharTraits> for any valid Char and
  605. // CharTraits types).
  606. // Tests printing a non-template type that supports generic streaming.
  607. class AllowsGenericStreaming {};
  608. template <typename Char, typename CharTraits>
  609. std::basic_ostream<Char, CharTraits>& operator<<(
  610. std::basic_ostream<Char, CharTraits>& os,
  611. const AllowsGenericStreaming& /* a */) {
  612. return os << "AllowsGenericStreaming";
  613. }
  614. TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
  615. AllowsGenericStreaming a;
  616. EXPECT_EQ("AllowsGenericStreaming", Print(a));
  617. }
  618. // Tests printing a template type that supports generic streaming.
  619. template <typename T>
  620. class AllowsGenericStreamingTemplate {};
  621. template <typename Char, typename CharTraits, typename T>
  622. std::basic_ostream<Char, CharTraits>& operator<<(
  623. std::basic_ostream<Char, CharTraits>& os,
  624. const AllowsGenericStreamingTemplate<T>& /* a */) {
  625. return os << "AllowsGenericStreamingTemplate";
  626. }
  627. TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
  628. AllowsGenericStreamingTemplate<int> a;
  629. EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
  630. }
  631. // Tests printing a type that supports generic streaming and can be
  632. // implicitly converted to another printable type.
  633. template <typename T>
  634. class AllowsGenericStreamingAndImplicitConversionTemplate {
  635. public:
  636. operator bool() const { return false; }
  637. };
  638. template <typename Char, typename CharTraits, typename T>
  639. std::basic_ostream<Char, CharTraits>& operator<<(
  640. std::basic_ostream<Char, CharTraits>& os,
  641. const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) {
  642. return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
  643. }
  644. TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
  645. AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
  646. EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
  647. }
  648. #if GTEST_HAS_STRING_PIECE_
  649. // Tests printing StringPiece.
  650. TEST(PrintStringPieceTest, SimpleStringPiece) {
  651. const StringPiece sp = "Hello";
  652. EXPECT_EQ("\"Hello\"", Print(sp));
  653. }
  654. TEST(PrintStringPieceTest, UnprintableCharacters) {
  655. const char str[] = "NUL (\0) and \r\t";
  656. const StringPiece sp(str, sizeof(str) - 1);
  657. EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
  658. }
  659. #endif // GTEST_HAS_STRING_PIECE_
  660. // Tests printing STL containers.
  661. TEST(PrintStlContainerTest, EmptyDeque) {
  662. deque<char> empty;
  663. EXPECT_EQ("{}", Print(empty));
  664. }
  665. TEST(PrintStlContainerTest, NonEmptyDeque) {
  666. deque<int> non_empty;
  667. non_empty.push_back(1);
  668. non_empty.push_back(3);
  669. EXPECT_EQ("{ 1, 3 }", Print(non_empty));
  670. }
  671. #if GTEST_HAS_HASH_MAP_
  672. TEST(PrintStlContainerTest, OneElementHashMap) {
  673. hash_map<int, char> map1;
  674. map1[1] = 'a';
  675. EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1));
  676. }
  677. TEST(PrintStlContainerTest, HashMultiMap) {
  678. hash_multimap<int, bool> map1;
  679. map1.insert(make_pair(5, true));
  680. map1.insert(make_pair(5, false));
  681. // Elements of hash_multimap can be printed in any order.
  682. const string result = Print(map1);
  683. EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
  684. result == "{ (5, false), (5, true) }")
  685. << " where Print(map1) returns \"" << result << "\".";
  686. }
  687. #endif // GTEST_HAS_HASH_MAP_
  688. #if GTEST_HAS_HASH_SET_
  689. TEST(PrintStlContainerTest, HashSet) {
  690. hash_set<string> set1;
  691. set1.insert("hello");
  692. EXPECT_EQ("{ \"hello\" }", Print(set1));
  693. }
  694. TEST(PrintStlContainerTest, HashMultiSet) {
  695. const int kSize = 5;
  696. int a[kSize] = { 1, 1, 2, 5, 1 };
  697. hash_multiset<int> set1(a, a + kSize);
  698. // Elements of hash_multiset can be printed in any order.
  699. const string result = Print(set1);
  700. const string expected_pattern = "{ d, d, d, d, d }"; // d means a digit.
  701. // Verifies the result matches the expected pattern; also extracts
  702. // the numbers in the result.
  703. ASSERT_EQ(expected_pattern.length(), result.length());
  704. std::vector<int> numbers;
  705. for (size_t i = 0; i != result.length(); i++) {
  706. if (expected_pattern[i] == 'd') {
  707. ASSERT_TRUE(isdigit(static_cast<unsigned char>(result[i])) != 0);
  708. numbers.push_back(result[i] - '0');
  709. } else {
  710. EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
  711. << result;
  712. }
  713. }
  714. // Makes sure the result contains the right numbers.
  715. std::sort(numbers.begin(), numbers.end());
  716. std::sort(a, a + kSize);
  717. EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
  718. }
  719. #endif // GTEST_HAS_HASH_SET_
  720. TEST(PrintStlContainerTest, List) {
  721. const string a[] = {
  722. "hello",
  723. "world"
  724. };
  725. const list<string> strings(a, a + 2);
  726. EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
  727. }
  728. TEST(PrintStlContainerTest, Map) {
  729. map<int, bool> map1;
  730. map1[1] = true;
  731. map1[5] = false;
  732. map1[3] = true;
  733. EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
  734. }
  735. TEST(PrintStlContainerTest, MultiMap) {
  736. multimap<bool, int> map1;
  737. // The make_pair template function would deduce the type as
  738. // pair<bool, int> here, and since the key part in a multimap has to
  739. // be constant, without a templated ctor in the pair class (as in
  740. // libCstd on Solaris), make_pair call would fail to compile as no
  741. // implicit conversion is found. Thus explicit typename is used
  742. // here instead.
  743. map1.insert(pair<const bool, int>(true, 0));
  744. map1.insert(pair<const bool, int>(true, 1));
  745. map1.insert(pair<const bool, int>(false, 2));
  746. EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
  747. }
  748. TEST(PrintStlContainerTest, Set) {
  749. const unsigned int a[] = { 3, 0, 5 };
  750. set<unsigned int> set1(a, a + 3);
  751. EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
  752. }
  753. TEST(PrintStlContainerTest, MultiSet) {
  754. const int a[] = { 1, 1, 2, 5, 1 };
  755. multiset<int> set1(a, a + 5);
  756. EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
  757. }
  758. TEST(PrintStlContainerTest, Pair) {
  759. pair<const bool, int> p(true, 5);
  760. EXPECT_EQ("(true, 5)", Print(p));
  761. }
  762. TEST(PrintStlContainerTest, Vector) {
  763. vector<int> v;
  764. v.push_back(1);
  765. v.push_back(2);
  766. EXPECT_EQ("{ 1, 2 }", Print(v));
  767. }
  768. TEST(PrintStlContainerTest, LongSequence) {
  769. const int a[100] = { 1, 2, 3 };
  770. const vector<int> v(a, a + 100);
  771. EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
  772. "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
  773. }
  774. TEST(PrintStlContainerTest, NestedContainer) {
  775. const int a1[] = { 1, 2 };
  776. const int a2[] = { 3, 4, 5 };
  777. const list<int> l1(a1, a1 + 2);
  778. const list<int> l2(a2, a2 + 3);
  779. vector<list<int> > v;
  780. v.push_back(l1);
  781. v.push_back(l2);
  782. EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
  783. }
  784. TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
  785. const int a[3] = { 1, 2, 3 };
  786. NativeArray<int> b(a, 3, kReference);
  787. EXPECT_EQ("{ 1, 2, 3 }", Print(b));
  788. }
  789. TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
  790. const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
  791. NativeArray<int[3]> b(a, 2, kReference);
  792. EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
  793. }
  794. // Tests that a class named iterator isn't treated as a container.
  795. struct iterator {
  796. char x;
  797. };
  798. TEST(PrintStlContainerTest, Iterator) {
  799. iterator it = {};
  800. EXPECT_EQ("1-byte object <00>", Print(it));
  801. }
  802. // Tests that a class named const_iterator isn't treated as a container.
  803. struct const_iterator {
  804. char x;
  805. };
  806. TEST(PrintStlContainerTest, ConstIterator) {
  807. const_iterator it = {};
  808. EXPECT_EQ("1-byte object <00>", Print(it));
  809. }
  810. #if GTEST_HAS_TR1_TUPLE
  811. // Tests printing tuples.
  812. // Tuples of various arities.
  813. TEST(PrintTupleTest, VariousSizes) {
  814. tuple<> t0;
  815. EXPECT_EQ("()", Print(t0));
  816. tuple<int> t1(5);
  817. EXPECT_EQ("(5)", Print(t1));
  818. tuple<char, bool> t2('a', true);
  819. EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
  820. tuple<bool, int, int> t3(false, 2, 3);
  821. EXPECT_EQ("(false, 2, 3)", Print(t3));
  822. tuple<bool, int, int, int> t4(false, 2, 3, 4);
  823. EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
  824. tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
  825. EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
  826. tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
  827. EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
  828. tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7);
  829. EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
  830. tuple<bool, int, int, int, bool, int, int, bool> t8(
  831. false, 2, 3, 4, true, 6, 7, true);
  832. EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
  833. tuple<bool, int, int, int, bool, int, int, bool, int> t9(
  834. false, 2, 3, 4, true, 6, 7, true, 9);
  835. EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
  836. const char* const str = "8";
  837. tuple<bool, char, short, testing::internal::Int32, // NOLINT
  838. testing::internal::Int64, float, double, const char*, void*, string>
  839. t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, NULL, "10");
  840. EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
  841. " pointing to \"8\", NULL, \"10\")",
  842. Print(t10));
  843. }
  844. // Nested tuples.
  845. TEST(PrintTupleTest, NestedTuple) {
  846. tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a');
  847. EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
  848. }
  849. #endif // GTEST_HAS_TR1_TUPLE
  850. // Tests printing user-defined unprintable types.
  851. // Unprintable types in the global namespace.
  852. TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
  853. EXPECT_EQ("1-byte object <00>",
  854. Print(UnprintableTemplateInGlobal<char>()));
  855. }
  856. // Unprintable types in a user namespace.
  857. TEST(PrintUnprintableTypeTest, InUserNamespace) {
  858. EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
  859. Print(::foo::UnprintableInFoo()));
  860. }
  861. // Unprintable types are that too big to be printed completely.
  862. struct Big {
  863. Big() { memset(array, 0, sizeof(array)); }
  864. char array[257];
  865. };
  866. TEST(PrintUnpritableTypeTest, BigObject) {
  867. EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
  868. "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
  869. "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
  870. "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
  871. "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
  872. "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
  873. "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
  874. Print(Big()));
  875. }
  876. // Tests printing user-defined streamable types.
  877. // Streamable types in the global namespace.
  878. TEST(PrintStreamableTypeTest, InGlobalNamespace) {
  879. StreamableInGlobal x;
  880. EXPECT_EQ("StreamableInGlobal", Print(x));
  881. EXPECT_EQ("StreamableInGlobal*", Print(&x));
  882. }
  883. // Printable template types in a user namespace.
  884. TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
  885. EXPECT_EQ("StreamableTemplateInFoo: 0",
  886. Print(::foo::StreamableTemplateInFoo<int>()));
  887. }
  888. // Tests printing user-defined types that have a PrintTo() function.
  889. TEST(PrintPrintableTypeTest, InUserNamespace) {
  890. EXPECT_EQ("PrintableViaPrintTo: 0",
  891. Print(::foo::PrintableViaPrintTo()));
  892. }
  893. // Tests printing a pointer to a user-defined type that has a <<
  894. // operator for its pointer.
  895. TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
  896. ::foo::PointerPrintable x;
  897. EXPECT_EQ("PointerPrintable*", Print(&x));
  898. }
  899. // Tests printing user-defined class template that have a PrintTo() function.
  900. TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
  901. EXPECT_EQ("PrintableViaPrintToTemplate: 5",
  902. Print(::foo::PrintableViaPrintToTemplate<int>(5)));
  903. }
  904. #if GTEST_HAS_PROTOBUF_
  905. // Tests printing a protocol message.
  906. TEST(PrintProtocolMessageTest, PrintsShortDebugString) {
  907. testing::internal::TestMessage msg;
  908. msg.set_member("yes");
  909. EXPECT_EQ("<member:\"yes\">", Print(msg));
  910. }
  911. // Tests printing a short proto2 message.
  912. TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) {
  913. testing::internal::FooMessage msg;
  914. msg.set_int_field(2);
  915. msg.set_string_field("hello");
  916. EXPECT_PRED2(RE::FullMatch, Print(msg),
  917. "<int_field:\\s*2\\s+string_field:\\s*\"hello\">");
  918. }
  919. // Tests printing a long proto2 message.
  920. TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) {
  921. testing::internal::FooMessage msg;
  922. msg.set_int_field(2);
  923. msg.set_string_field("hello");
  924. msg.add_names("peter");
  925. msg.add_names("paul");
  926. msg.add_names("mary");
  927. EXPECT_PRED2(RE::FullMatch, Print(msg),
  928. "<\n"
  929. "int_field:\\s*2\n"
  930. "string_field:\\s*\"hello\"\n"
  931. "names:\\s*\"peter\"\n"
  932. "names:\\s*\"paul\"\n"
  933. "names:\\s*\"mary\"\n"
  934. ">");
  935. }
  936. #endif // GTEST_HAS_PROTOBUF_
  937. // Tests that the universal printer prints both the address and the
  938. // value of a reference.
  939. TEST(PrintReferenceTest, PrintsAddressAndValue) {
  940. int n = 5;
  941. EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
  942. int a[2][3] = {
  943. { 0, 1, 2 },
  944. { 3, 4, 5 }
  945. };
  946. EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
  947. PrintByRef(a));
  948. const ::foo::UnprintableInFoo x;
  949. EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
  950. "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
  951. PrintByRef(x));
  952. }
  953. // Tests that the universal printer prints a function pointer passed by
  954. // reference.
  955. TEST(PrintReferenceTest, HandlesFunctionPointer) {
  956. void (*fp)(int n) = &MyFunction;
  957. const string fp_pointer_string =
  958. PrintPointer(reinterpret_cast<const void*>(&fp));
  959. // We cannot directly cast &MyFunction to const void* because the
  960. // standard disallows casting between pointers to functions and
  961. // pointers to objects, and some compilers (e.g. GCC 3.4) enforce
  962. // this limitation.
  963. const string fp_string = PrintPointer(reinterpret_cast<const void*>(
  964. reinterpret_cast<internal::BiggestInt>(fp)));
  965. EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
  966. PrintByRef(fp));
  967. }
  968. // Tests that the universal printer prints a member function pointer
  969. // passed by reference.
  970. TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
  971. int (Foo::*p)(char ch) = &Foo::MyMethod;
  972. EXPECT_TRUE(HasPrefix(
  973. PrintByRef(p),
  974. "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
  975. Print(sizeof(p)) + "-byte object "));
  976. char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
  977. EXPECT_TRUE(HasPrefix(
  978. PrintByRef(p2),
  979. "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
  980. Print(sizeof(p2)) + "-byte object "));
  981. }
  982. // Tests that the universal printer prints a member variable pointer
  983. // passed by reference.
  984. TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
  985. int (Foo::*p) = &Foo::value; // NOLINT
  986. EXPECT_TRUE(HasPrefix(
  987. PrintByRef(p),
  988. "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
  989. }
  990. // Useful for testing PrintToString(). We cannot use EXPECT_EQ()
  991. // there as its implementation uses PrintToString(). The caller must
  992. // ensure that 'value' has no side effect.
  993. #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
  994. EXPECT_TRUE(PrintToString(value) == (expected_string)) \
  995. << " where " #value " prints as " << (PrintToString(value))
  996. TEST(PrintToStringTest, WorksForScalar) {
  997. EXPECT_PRINT_TO_STRING_(123, "123");
  998. }
  999. TEST(PrintToStringTest, WorksForPointerToConstChar) {
  1000. const char* p = "hello";
  1001. EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
  1002. }
  1003. TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
  1004. char s[] = "hello";
  1005. char* p = s;
  1006. EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
  1007. }
  1008. TEST(PrintToStringTest, WorksForArray) {
  1009. int n[3] = { 1, 2, 3 };
  1010. EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
  1011. }
  1012. #undef EXPECT_PRINT_TO_STRING_
  1013. TEST(UniversalTersePrintTest, WorksForNonReference) {
  1014. ::std::stringstream ss;
  1015. UniversalTersePrint(123, &ss);
  1016. EXPECT_EQ("123", ss.str());
  1017. }
  1018. TEST(UniversalTersePrintTest, WorksForReference) {
  1019. const int& n = 123;
  1020. ::std::stringstream ss;
  1021. UniversalTersePrint(n, &ss);
  1022. EXPECT_EQ("123", ss.str());
  1023. }
  1024. TEST(UniversalTersePrintTest, WorksForCString) {
  1025. const char* s1 = "abc";
  1026. ::std::stringstream ss1;
  1027. UniversalTersePrint(s1, &ss1);
  1028. EXPECT_EQ("\"abc\"", ss1.str());
  1029. char* s2 = const_cast<char*>(s1);
  1030. ::std::stringstream ss2;
  1031. UniversalTersePrint(s2, &ss2);
  1032. EXPECT_EQ("\"abc\"", ss2.str());
  1033. const char* s3 = NULL;
  1034. ::std::stringstream ss3;
  1035. UniversalTersePrint(s3, &ss3);
  1036. EXPECT_EQ("NULL", ss3.str());
  1037. }
  1038. TEST(UniversalPrintTest, WorksForNonReference) {
  1039. ::std::stringstream ss;
  1040. UniversalPrint(123, &ss);
  1041. EXPECT_EQ("123", ss.str());
  1042. }
  1043. TEST(UniversalPrintTest, WorksForReference) {
  1044. const int& n = 123;
  1045. ::std::stringstream ss;
  1046. UniversalPrint(n, &ss);
  1047. EXPECT_EQ("123", ss.str());
  1048. }
  1049. TEST(UniversalPrintTest, WorksForCString) {
  1050. const char* s1 = "abc";
  1051. ::std::stringstream ss1;
  1052. UniversalPrint(s1, &ss1);
  1053. EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str()));
  1054. char* s2 = const_cast<char*>(s1);
  1055. ::std::stringstream ss2;
  1056. UniversalPrint(s2, &ss2);
  1057. EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str()));
  1058. const char* s3 = NULL;
  1059. ::std::stringstream ss3;
  1060. UniversalPrint(s3, &ss3);
  1061. EXPECT_EQ("NULL", ss3.str());
  1062. }
  1063. #if GTEST_HAS_TR1_TUPLE
  1064. TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) {
  1065. Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple());
  1066. EXPECT_EQ(0u, result.size());
  1067. }
  1068. TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) {
  1069. Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1));
  1070. ASSERT_EQ(1u, result.size());
  1071. EXPECT_EQ("1", result[0]);
  1072. }
  1073. TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) {
  1074. Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a'));
  1075. ASSERT_EQ(2u, result.size());
  1076. EXPECT_EQ("1", result[0]);
  1077. EXPECT_EQ("'a' (97, 0x61)", result[1]);
  1078. }
  1079. TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) {
  1080. const int n = 1;
  1081. Strings result = UniversalTersePrintTupleFieldsToStrings(
  1082. tuple<const int&, const char*>(n, "a"));
  1083. ASSERT_EQ(2u, result.size());
  1084. EXPECT_EQ("1", result[0]);
  1085. EXPECT_EQ("\"a\"", result[1]);
  1086. }
  1087. #endif // GTEST_HAS_TR1_TUPLE
  1088. } // namespace gtest_printers_test
  1089. } // namespace testing