googletest-printers-test.cc 56 KB

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