gtest-printers_test.cc 51 KB

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