Counter Strike : Global Offensive Source Code
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.

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