gtest-printers.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 implements a universal value printer that can print a
  34. // value of any type T:
  35. //
  36. // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
  37. //
  38. // A user can teach this function how to print a class type T by
  39. // defining either operator<<() or PrintTo() in the namespace that
  40. // defines T. More specifically, the FIRST defined function in the
  41. // following list will be used (assuming T is defined in namespace
  42. // foo):
  43. //
  44. // 1. foo::PrintTo(const T&, ostream*)
  45. // 2. operator<<(ostream&, const T&) defined in either foo or the
  46. // global namespace.
  47. //
  48. // If none of the above is defined, it will print the debug string of
  49. // the value if it is a protocol buffer, or print the raw bytes in the
  50. // value otherwise.
  51. //
  52. // To aid debugging: when T is a reference type, the address of the
  53. // value is also printed; when T is a (const) char pointer, both the
  54. // pointer value and the NUL-terminated string it points to are
  55. // printed.
  56. //
  57. // We also provide some convenient wrappers:
  58. //
  59. // // Prints a value to a string. For a (const or not) char
  60. // // pointer, the NUL-terminated string (but not the pointer) is
  61. // // printed.
  62. // std::string ::testing::PrintToString(const T& value);
  63. //
  64. // // Prints a value tersely: for a reference type, the referenced
  65. // // value (but not the address) is printed; for a (const or not) char
  66. // // pointer, the NUL-terminated string (but not the pointer) is
  67. // // printed.
  68. // void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
  69. //
  70. // // Prints value using the type inferred by the compiler. The difference
  71. // // from UniversalTersePrint() is that this function prints both the
  72. // // pointer and the NUL-terminated string for a (const or not) char pointer.
  73. // void ::testing::internal::UniversalPrint(const T& value, ostream*);
  74. //
  75. // // Prints the fields of a tuple tersely to a string vector, one
  76. // // element for each field. Tuple support must be enabled in
  77. // // gtest-port.h.
  78. // std::vector<string> UniversalTersePrintTupleFieldsToStrings(
  79. // const Tuple& value);
  80. //
  81. // Known limitation:
  82. //
  83. // The print primitives print the elements of an STL-style container
  84. // using the compiler-inferred type of *iter where iter is a
  85. // const_iterator of the container. When const_iterator is an input
  86. // iterator but not a forward iterator, this inferred type may not
  87. // match value_type, and the print output may be incorrect. In
  88. // practice, this is rarely a problem as for most containers
  89. // const_iterator is a forward iterator. We'll fix this if there's an
  90. // actual need for it. Note that this fix cannot rely on value_type
  91. // being defined as many user-defined container types don't have
  92. // value_type.
  93. #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
  94. #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
  95. #include <ostream> // NOLINT
  96. #include <sstream>
  97. #include <string>
  98. #include <utility>
  99. #include <vector>
  100. #include "gtest/internal/gtest-port.h"
  101. #include "gtest/internal/gtest-internal.h"
  102. namespace testing {
  103. // Definitions in the 'internal' and 'internal2' name spaces are
  104. // subject to change without notice. DO NOT USE THEM IN USER CODE!
  105. namespace internal2 {
  106. // Prints the given number of bytes in the given object to the given
  107. // ostream.
  108. GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
  109. size_t count,
  110. ::std::ostream* os);
  111. // For selecting which printer to use when a given type has neither <<
  112. // nor PrintTo().
  113. enum TypeKind {
  114. kProtobuf, // a protobuf type
  115. kConvertibleToInteger, // a type implicitly convertible to BiggestInt
  116. // (e.g. a named or unnamed enum type)
  117. kOtherType // anything else
  118. };
  119. // TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called
  120. // by the universal printer to print a value of type T when neither
  121. // operator<< nor PrintTo() is defined for T, where kTypeKind is the
  122. // "kind" of T as defined by enum TypeKind.
  123. template <typename T, TypeKind kTypeKind>
  124. class TypeWithoutFormatter {
  125. public:
  126. // This default version is called when kTypeKind is kOtherType.
  127. static void PrintValue(const T& value, ::std::ostream* os) {
  128. PrintBytesInObjectTo(reinterpret_cast<const unsigned char*>(&value),
  129. sizeof(value), os);
  130. }
  131. };
  132. // We print a protobuf using its ShortDebugString() when the string
  133. // doesn't exceed this many characters; otherwise we print it using
  134. // DebugString() for better readability.
  135. const size_t kProtobufOneLinerMaxLength = 50;
  136. template <typename T>
  137. class TypeWithoutFormatter<T, kProtobuf> {
  138. public:
  139. static void PrintValue(const T& value, ::std::ostream* os) {
  140. const ::testing::internal::string short_str = value.ShortDebugString();
  141. const ::testing::internal::string pretty_str =
  142. short_str.length() <= kProtobufOneLinerMaxLength ?
  143. short_str : ("\n" + value.DebugString());
  144. *os << ("<" + pretty_str + ">");
  145. }
  146. };
  147. template <typename T>
  148. class TypeWithoutFormatter<T, kConvertibleToInteger> {
  149. public:
  150. // Since T has no << operator or PrintTo() but can be implicitly
  151. // converted to BiggestInt, we print it as a BiggestInt.
  152. //
  153. // Most likely T is an enum type (either named or unnamed), in which
  154. // case printing it as an integer is the desired behavior. In case
  155. // T is not an enum, printing it as an integer is the best we can do
  156. // given that it has no user-defined printer.
  157. static void PrintValue(const T& value, ::std::ostream* os) {
  158. const internal::BiggestInt kBigInt = value;
  159. *os << kBigInt;
  160. }
  161. };
  162. // Prints the given value to the given ostream. If the value is a
  163. // protocol message, its debug string is printed; if it's an enum or
  164. // of a type implicitly convertible to BiggestInt, it's printed as an
  165. // integer; otherwise the bytes in the value are printed. This is
  166. // what UniversalPrinter<T>::Print() does when it knows nothing about
  167. // type T and T has neither << operator nor PrintTo().
  168. //
  169. // A user can override this behavior for a class type Foo by defining
  170. // a << operator in the namespace where Foo is defined.
  171. //
  172. // We put this operator in namespace 'internal2' instead of 'internal'
  173. // to simplify the implementation, as much code in 'internal' needs to
  174. // use << in STL, which would conflict with our own << were it defined
  175. // in 'internal'.
  176. //
  177. // Note that this operator<< takes a generic std::basic_ostream<Char,
  178. // CharTraits> type instead of the more restricted std::ostream. If
  179. // we define it to take an std::ostream instead, we'll get an
  180. // "ambiguous overloads" compiler error when trying to print a type
  181. // Foo that supports streaming to std::basic_ostream<Char,
  182. // CharTraits>, as the compiler cannot tell whether
  183. // operator<<(std::ostream&, const T&) or
  184. // operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more
  185. // specific.
  186. template <typename Char, typename CharTraits, typename T>
  187. ::std::basic_ostream<Char, CharTraits>& operator<<(
  188. ::std::basic_ostream<Char, CharTraits>& os, const T& x) {
  189. TypeWithoutFormatter<T,
  190. (internal::IsAProtocolMessage<T>::value ? kProtobuf :
  191. internal::ImplicitlyConvertible<const T&, internal::BiggestInt>::value ?
  192. kConvertibleToInteger : kOtherType)>::PrintValue(x, &os);
  193. return os;
  194. }
  195. } // namespace internal2
  196. } // namespace testing
  197. // This namespace MUST NOT BE NESTED IN ::testing, or the name look-up
  198. // magic needed for implementing UniversalPrinter won't work.
  199. namespace testing_internal {
  200. // Used to print a value that is not an STL-style container when the
  201. // user doesn't define PrintTo() for it.
  202. template <typename T>
  203. void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
  204. // With the following statement, during unqualified name lookup,
  205. // testing::internal2::operator<< appears as if it was declared in
  206. // the nearest enclosing namespace that contains both
  207. // ::testing_internal and ::testing::internal2, i.e. the global
  208. // namespace. For more details, refer to the C++ Standard section
  209. // 7.3.4-1 [namespace.udir]. This allows us to fall back onto
  210. // testing::internal2::operator<< in case T doesn't come with a <<
  211. // operator.
  212. //
  213. // We cannot write 'using ::testing::internal2::operator<<;', which
  214. // gcc 3.3 fails to compile due to a compiler bug.
  215. using namespace ::testing::internal2; // NOLINT
  216. // Assuming T is defined in namespace foo, in the next statement,
  217. // the compiler will consider all of:
  218. //
  219. // 1. foo::operator<< (thanks to Koenig look-up),
  220. // 2. ::operator<< (as the current namespace is enclosed in ::),
  221. // 3. testing::internal2::operator<< (thanks to the using statement above).
  222. //
  223. // The operator<< whose type matches T best will be picked.
  224. //
  225. // We deliberately allow #2 to be a candidate, as sometimes it's
  226. // impossible to define #1 (e.g. when foo is ::std, defining
  227. // anything in it is undefined behavior unless you are a compiler
  228. // vendor.).
  229. *os << value;
  230. }
  231. } // namespace testing_internal
  232. namespace testing {
  233. namespace internal {
  234. // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
  235. // value to the given ostream. The caller must ensure that
  236. // 'ostream_ptr' is not NULL, or the behavior is undefined.
  237. //
  238. // We define UniversalPrinter as a class template (as opposed to a
  239. // function template), as we need to partially specialize it for
  240. // reference types, which cannot be done with function templates.
  241. template <typename T>
  242. class UniversalPrinter;
  243. template <typename T>
  244. void UniversalPrint(const T& value, ::std::ostream* os);
  245. // Used to print an STL-style container when the user doesn't define
  246. // a PrintTo() for it.
  247. template <typename C>
  248. void DefaultPrintTo(IsContainer /* dummy */,
  249. false_type /* is not a pointer */,
  250. const C& container, ::std::ostream* os) {
  251. const size_t kMaxCount = 32; // The maximum number of elements to print.
  252. *os << '{';
  253. size_t count = 0;
  254. for (typename C::const_iterator it = container.begin();
  255. it != container.end(); ++it, ++count) {
  256. if (count > 0) {
  257. *os << ',';
  258. if (count == kMaxCount) { // Enough has been printed.
  259. *os << " ...";
  260. break;
  261. }
  262. }
  263. *os << ' ';
  264. // We cannot call PrintTo(*it, os) here as PrintTo() doesn't
  265. // handle *it being a native array.
  266. internal::UniversalPrint(*it, os);
  267. }
  268. if (count > 0) {
  269. *os << ' ';
  270. }
  271. *os << '}';
  272. }
  273. // Used to print a pointer that is neither a char pointer nor a member
  274. // pointer, when the user doesn't define PrintTo() for it. (A member
  275. // variable pointer or member function pointer doesn't really point to
  276. // a location in the address space. Their representation is
  277. // implementation-defined. Therefore they will be printed as raw
  278. // bytes.)
  279. template <typename T>
  280. void DefaultPrintTo(IsNotContainer /* dummy */,
  281. true_type /* is a pointer */,
  282. T* p, ::std::ostream* os) {
  283. if (p == NULL) {
  284. *os << "NULL";
  285. } else {
  286. // C++ doesn't allow casting from a function pointer to any object
  287. // pointer.
  288. //
  289. // IsTrue() silences warnings: "Condition is always true",
  290. // "unreachable code".
  291. if (IsTrue(ImplicitlyConvertible<T*, const void*>::value)) {
  292. // T is not a function type. We just call << to print p,
  293. // relying on ADL to pick up user-defined << for their pointer
  294. // types, if any.
  295. *os << p;
  296. } else {
  297. // T is a function type, so '*os << p' doesn't do what we want
  298. // (it just prints p as bool). We want to print p as a const
  299. // void*. However, we cannot cast it to const void* directly,
  300. // even using reinterpret_cast, as earlier versions of gcc
  301. // (e.g. 3.4.5) cannot compile the cast when p is a function
  302. // pointer. Casting to UInt64 first solves the problem.
  303. *os << reinterpret_cast<const void*>(
  304. reinterpret_cast<internal::UInt64>(p));
  305. }
  306. }
  307. }
  308. // Used to print a non-container, non-pointer value when the user
  309. // doesn't define PrintTo() for it.
  310. template <typename T>
  311. void DefaultPrintTo(IsNotContainer /* dummy */,
  312. false_type /* is not a pointer */,
  313. const T& value, ::std::ostream* os) {
  314. ::testing_internal::DefaultPrintNonContainerTo(value, os);
  315. }
  316. // Prints the given value using the << operator if it has one;
  317. // otherwise prints the bytes in it. This is what
  318. // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
  319. // or overloaded for type T.
  320. //
  321. // A user can override this behavior for a class type Foo by defining
  322. // an overload of PrintTo() in the namespace where Foo is defined. We
  323. // give the user this option as sometimes defining a << operator for
  324. // Foo is not desirable (e.g. the coding style may prevent doing it,
  325. // or there is already a << operator but it doesn't do what the user
  326. // wants).
  327. template <typename T>
  328. void PrintTo(const T& value, ::std::ostream* os) {
  329. // DefaultPrintTo() is overloaded. The type of its first two
  330. // arguments determine which version will be picked. If T is an
  331. // STL-style container, the version for container will be called; if
  332. // T is a pointer, the pointer version will be called; otherwise the
  333. // generic version will be called.
  334. //
  335. // Note that we check for container types here, prior to we check
  336. // for protocol message types in our operator<<. The rationale is:
  337. //
  338. // For protocol messages, we want to give people a chance to
  339. // override Google Mock's format by defining a PrintTo() or
  340. // operator<<. For STL containers, other formats can be
  341. // incompatible with Google Mock's format for the container
  342. // elements; therefore we check for container types here to ensure
  343. // that our format is used.
  344. //
  345. // The second argument of DefaultPrintTo() is needed to bypass a bug
  346. // in Symbian's C++ compiler that prevents it from picking the right
  347. // overload between:
  348. //
  349. // PrintTo(const T& x, ...);
  350. // PrintTo(T* x, ...);
  351. DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
  352. }
  353. // The following list of PrintTo() overloads tells
  354. // UniversalPrinter<T>::Print() how to print standard types (built-in
  355. // types, strings, plain arrays, and pointers).
  356. // Overloads for various char types.
  357. GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
  358. GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
  359. inline void PrintTo(char c, ::std::ostream* os) {
  360. // When printing a plain char, we always treat it as unsigned. This
  361. // way, the output won't be affected by whether the compiler thinks
  362. // char is signed or not.
  363. PrintTo(static_cast<unsigned char>(c), os);
  364. }
  365. // Overloads for other simple built-in types.
  366. inline void PrintTo(bool x, ::std::ostream* os) {
  367. *os << (x ? "true" : "false");
  368. }
  369. // Overload for wchar_t type.
  370. // Prints a wchar_t as a symbol if it is printable or as its internal
  371. // code otherwise and also as its decimal code (except for L'\0').
  372. // The L'\0' char is printed as "L'\\0'". The decimal code is printed
  373. // as signed integer when wchar_t is implemented by the compiler
  374. // as a signed type and is printed as an unsigned integer when wchar_t
  375. // is implemented as an unsigned type.
  376. GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
  377. // Overloads for C strings.
  378. GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
  379. inline void PrintTo(char* s, ::std::ostream* os) {
  380. PrintTo(ImplicitCast_<const char*>(s), os);
  381. }
  382. // signed/unsigned char is often used for representing binary data, so
  383. // we print pointers to it as void* to be safe.
  384. inline void PrintTo(const signed char* s, ::std::ostream* os) {
  385. PrintTo(ImplicitCast_<const void*>(s), os);
  386. }
  387. inline void PrintTo(signed char* s, ::std::ostream* os) {
  388. PrintTo(ImplicitCast_<const void*>(s), os);
  389. }
  390. inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
  391. PrintTo(ImplicitCast_<const void*>(s), os);
  392. }
  393. inline void PrintTo(unsigned char* s, ::std::ostream* os) {
  394. PrintTo(ImplicitCast_<const void*>(s), os);
  395. }
  396. // MSVC can be configured to define wchar_t as a typedef of unsigned
  397. // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
  398. // type. When wchar_t is a typedef, defining an overload for const
  399. // wchar_t* would cause unsigned short* be printed as a wide string,
  400. // possibly causing invalid memory accesses.
  401. #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
  402. // Overloads for wide C strings
  403. GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
  404. inline void PrintTo(wchar_t* s, ::std::ostream* os) {
  405. PrintTo(ImplicitCast_<const wchar_t*>(s), os);
  406. }
  407. #endif
  408. // Overload for C arrays. Multi-dimensional arrays are printed
  409. // properly.
  410. // Prints the given number of elements in an array, without printing
  411. // the curly braces.
  412. template <typename T>
  413. void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
  414. UniversalPrint(a[0], os);
  415. for (size_t i = 1; i != count; i++) {
  416. *os << ", ";
  417. UniversalPrint(a[i], os);
  418. }
  419. }
  420. // Overloads for ::string and ::std::string.
  421. #if GTEST_HAS_GLOBAL_STRING
  422. GTEST_API_ void PrintStringTo(const ::string&s, ::std::ostream* os);
  423. inline void PrintTo(const ::string& s, ::std::ostream* os) {
  424. PrintStringTo(s, os);
  425. }
  426. #endif // GTEST_HAS_GLOBAL_STRING
  427. GTEST_API_ void PrintStringTo(const ::std::string&s, ::std::ostream* os);
  428. inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
  429. PrintStringTo(s, os);
  430. }
  431. // Overloads for ::wstring and ::std::wstring.
  432. #if GTEST_HAS_GLOBAL_WSTRING
  433. GTEST_API_ void PrintWideStringTo(const ::wstring&s, ::std::ostream* os);
  434. inline void PrintTo(const ::wstring& s, ::std::ostream* os) {
  435. PrintWideStringTo(s, os);
  436. }
  437. #endif // GTEST_HAS_GLOBAL_WSTRING
  438. #if GTEST_HAS_STD_WSTRING
  439. GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
  440. inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
  441. PrintWideStringTo(s, os);
  442. }
  443. #endif // GTEST_HAS_STD_WSTRING
  444. #if GTEST_HAS_TR1_TUPLE
  445. // Overload for ::std::tr1::tuple. Needed for printing function arguments,
  446. // which are packed as tuples.
  447. // Helper function for printing a tuple. T must be instantiated with
  448. // a tuple type.
  449. template <typename T>
  450. void PrintTupleTo(const T& t, ::std::ostream* os);
  451. // Overloaded PrintTo() for tuples of various arities. We support
  452. // tuples of up-to 10 fields. The following implementation works
  453. // regardless of whether tr1::tuple is implemented using the
  454. // non-standard variadic template feature or not.
  455. inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
  456. PrintTupleTo(t, os);
  457. }
  458. template <typename T1>
  459. void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
  460. PrintTupleTo(t, os);
  461. }
  462. template <typename T1, typename T2>
  463. void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
  464. PrintTupleTo(t, os);
  465. }
  466. template <typename T1, typename T2, typename T3>
  467. void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
  468. PrintTupleTo(t, os);
  469. }
  470. template <typename T1, typename T2, typename T3, typename T4>
  471. void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
  472. PrintTupleTo(t, os);
  473. }
  474. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  475. void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
  476. ::std::ostream* os) {
  477. PrintTupleTo(t, os);
  478. }
  479. template <typename T1, typename T2, typename T3, typename T4, typename T5,
  480. typename T6>
  481. void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
  482. ::std::ostream* os) {
  483. PrintTupleTo(t, os);
  484. }
  485. template <typename T1, typename T2, typename T3, typename T4, typename T5,
  486. typename T6, typename T7>
  487. void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
  488. ::std::ostream* os) {
  489. PrintTupleTo(t, os);
  490. }
  491. template <typename T1, typename T2, typename T3, typename T4, typename T5,
  492. typename T6, typename T7, typename T8>
  493. void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
  494. ::std::ostream* os) {
  495. PrintTupleTo(t, os);
  496. }
  497. template <typename T1, typename T2, typename T3, typename T4, typename T5,
  498. typename T6, typename T7, typename T8, typename T9>
  499. void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t,
  500. ::std::ostream* os) {
  501. PrintTupleTo(t, os);
  502. }
  503. template <typename T1, typename T2, typename T3, typename T4, typename T5,
  504. typename T6, typename T7, typename T8, typename T9, typename T10>
  505. void PrintTo(
  506. const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
  507. ::std::ostream* os) {
  508. PrintTupleTo(t, os);
  509. }
  510. #endif // GTEST_HAS_TR1_TUPLE
  511. // Overload for std::pair.
  512. template <typename T1, typename T2>
  513. void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
  514. *os << '(';
  515. // We cannot use UniversalPrint(value.first, os) here, as T1 may be
  516. // a reference type. The same for printing value.second.
  517. UniversalPrinter<T1>::Print(value.first, os);
  518. *os << ", ";
  519. UniversalPrinter<T2>::Print(value.second, os);
  520. *os << ')';
  521. }
  522. // Implements printing a non-reference type T by letting the compiler
  523. // pick the right overload of PrintTo() for T.
  524. template <typename T>
  525. class UniversalPrinter {
  526. public:
  527. // MSVC warns about adding const to a function type, so we want to
  528. // disable the warning.
  529. #ifdef _MSC_VER
  530. # pragma warning(push) // Saves the current warning state.
  531. # pragma warning(disable:4180) // Temporarily disables warning 4180.
  532. #endif // _MSC_VER
  533. // Note: we deliberately don't call this PrintTo(), as that name
  534. // conflicts with ::testing::internal::PrintTo in the body of the
  535. // function.
  536. static void Print(const T& value, ::std::ostream* os) {
  537. // By default, ::testing::internal::PrintTo() is used for printing
  538. // the value.
  539. //
  540. // Thanks to Koenig look-up, if T is a class and has its own
  541. // PrintTo() function defined in its namespace, that function will
  542. // be visible here. Since it is more specific than the generic ones
  543. // in ::testing::internal, it will be picked by the compiler in the
  544. // following statement - exactly what we want.
  545. PrintTo(value, os);
  546. }
  547. #ifdef _MSC_VER
  548. # pragma warning(pop) // Restores the warning state.
  549. #endif // _MSC_VER
  550. };
  551. // UniversalPrintArray(begin, len, os) prints an array of 'len'
  552. // elements, starting at address 'begin'.
  553. template <typename T>
  554. void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
  555. if (len == 0) {
  556. *os << "{}";
  557. } else {
  558. *os << "{ ";
  559. const size_t kThreshold = 18;
  560. const size_t kChunkSize = 8;
  561. // If the array has more than kThreshold elements, we'll have to
  562. // omit some details by printing only the first and the last
  563. // kChunkSize elements.
  564. // TODO(wan@google.com): let the user control the threshold using a flag.
  565. if (len <= kThreshold) {
  566. PrintRawArrayTo(begin, len, os);
  567. } else {
  568. PrintRawArrayTo(begin, kChunkSize, os);
  569. *os << ", ..., ";
  570. PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
  571. }
  572. *os << " }";
  573. }
  574. }
  575. // This overload prints a (const) char array compactly.
  576. GTEST_API_ void UniversalPrintArray(
  577. const char* begin, size_t len, ::std::ostream* os);
  578. // This overload prints a (const) wchar_t array compactly.
  579. GTEST_API_ void UniversalPrintArray(
  580. const wchar_t* begin, size_t len, ::std::ostream* os);
  581. // Implements printing an array type T[N].
  582. template <typename T, size_t N>
  583. class UniversalPrinter<T[N]> {
  584. public:
  585. // Prints the given array, omitting some elements when there are too
  586. // many.
  587. static void Print(const T (&a)[N], ::std::ostream* os) {
  588. UniversalPrintArray(a, N, os);
  589. }
  590. };
  591. // Implements printing a reference type T&.
  592. template <typename T>
  593. class UniversalPrinter<T&> {
  594. public:
  595. // MSVC warns about adding const to a function type, so we want to
  596. // disable the warning.
  597. #ifdef _MSC_VER
  598. # pragma warning(push) // Saves the current warning state.
  599. # pragma warning(disable:4180) // Temporarily disables warning 4180.
  600. #endif // _MSC_VER
  601. static void Print(const T& value, ::std::ostream* os) {
  602. // Prints the address of the value. We use reinterpret_cast here
  603. // as static_cast doesn't compile when T is a function type.
  604. *os << "@" << reinterpret_cast<const void*>(&value) << " ";
  605. // Then prints the value itself.
  606. UniversalPrint(value, os);
  607. }
  608. #ifdef _MSC_VER
  609. # pragma warning(pop) // Restores the warning state.
  610. #endif // _MSC_VER
  611. };
  612. // Prints a value tersely: for a reference type, the referenced value
  613. // (but not the address) is printed; for a (const) char pointer, the
  614. // NUL-terminated string (but not the pointer) is printed.
  615. template <typename T>
  616. class UniversalTersePrinter {
  617. public:
  618. static void Print(const T& value, ::std::ostream* os) {
  619. UniversalPrint(value, os);
  620. }
  621. };
  622. template <typename T>
  623. class UniversalTersePrinter<T&> {
  624. public:
  625. static void Print(const T& value, ::std::ostream* os) {
  626. UniversalPrint(value, os);
  627. }
  628. };
  629. template <typename T, size_t N>
  630. class UniversalTersePrinter<T[N]> {
  631. public:
  632. static void Print(const T (&value)[N], ::std::ostream* os) {
  633. UniversalPrinter<T[N]>::Print(value, os);
  634. }
  635. };
  636. template <>
  637. class UniversalTersePrinter<const char*> {
  638. public:
  639. static void Print(const char* str, ::std::ostream* os) {
  640. if (str == NULL) {
  641. *os << "NULL";
  642. } else {
  643. UniversalPrint(string(str), os);
  644. }
  645. }
  646. };
  647. template <>
  648. class UniversalTersePrinter<char*> {
  649. public:
  650. static void Print(char* str, ::std::ostream* os) {
  651. UniversalTersePrinter<const char*>::Print(str, os);
  652. }
  653. };
  654. #if GTEST_HAS_STD_WSTRING
  655. template <>
  656. class UniversalTersePrinter<const wchar_t*> {
  657. public:
  658. static void Print(const wchar_t* str, ::std::ostream* os) {
  659. if (str == NULL) {
  660. *os << "NULL";
  661. } else {
  662. UniversalPrint(::std::wstring(str), os);
  663. }
  664. }
  665. };
  666. #endif
  667. template <>
  668. class UniversalTersePrinter<wchar_t*> {
  669. public:
  670. static void Print(wchar_t* str, ::std::ostream* os) {
  671. UniversalTersePrinter<const wchar_t*>::Print(str, os);
  672. }
  673. };
  674. template <typename T>
  675. void UniversalTersePrint(const T& value, ::std::ostream* os) {
  676. UniversalTersePrinter<T>::Print(value, os);
  677. }
  678. // Prints a value using the type inferred by the compiler. The
  679. // difference between this and UniversalTersePrint() is that for a
  680. // (const) char pointer, this prints both the pointer and the
  681. // NUL-terminated string.
  682. template <typename T>
  683. void UniversalPrint(const T& value, ::std::ostream* os) {
  684. // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
  685. // UniversalPrinter with T directly.
  686. typedef T T1;
  687. UniversalPrinter<T1>::Print(value, os);
  688. }
  689. #if GTEST_HAS_TR1_TUPLE
  690. typedef ::std::vector<string> Strings;
  691. // This helper template allows PrintTo() for tuples and
  692. // UniversalTersePrintTupleFieldsToStrings() to be defined by
  693. // induction on the number of tuple fields. The idea is that
  694. // TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N
  695. // fields in tuple t, and can be defined in terms of
  696. // TuplePrefixPrinter<N - 1>.
  697. // The inductive case.
  698. template <size_t N>
  699. struct TuplePrefixPrinter {
  700. // Prints the first N fields of a tuple.
  701. template <typename Tuple>
  702. static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
  703. TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
  704. *os << ", ";
  705. UniversalPrinter<typename ::std::tr1::tuple_element<N - 1, Tuple>::type>
  706. ::Print(::std::tr1::get<N - 1>(t), os);
  707. }
  708. // Tersely prints the first N fields of a tuple to a string vector,
  709. // one element for each field.
  710. template <typename Tuple>
  711. static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
  712. TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
  713. ::std::stringstream ss;
  714. UniversalTersePrint(::std::tr1::get<N - 1>(t), &ss);
  715. strings->push_back(ss.str());
  716. }
  717. };
  718. // Base cases.
  719. template <>
  720. struct TuplePrefixPrinter<0> {
  721. template <typename Tuple>
  722. static void PrintPrefixTo(const Tuple&, ::std::ostream*) {}
  723. template <typename Tuple>
  724. static void TersePrintPrefixToStrings(const Tuple&, Strings*) {}
  725. };
  726. // We have to specialize the entire TuplePrefixPrinter<> class
  727. // template here, even though the definition of
  728. // TersePrintPrefixToStrings() is the same as the generic version, as
  729. // Embarcadero (formerly CodeGear, formerly Borland) C++ doesn't
  730. // support specializing a method template of a class template.
  731. template <>
  732. struct TuplePrefixPrinter<1> {
  733. template <typename Tuple>
  734. static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
  735. UniversalPrinter<typename ::std::tr1::tuple_element<0, Tuple>::type>::
  736. Print(::std::tr1::get<0>(t), os);
  737. }
  738. template <typename Tuple>
  739. static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
  740. ::std::stringstream ss;
  741. UniversalTersePrint(::std::tr1::get<0>(t), &ss);
  742. strings->push_back(ss.str());
  743. }
  744. };
  745. // Helper function for printing a tuple. T must be instantiated with
  746. // a tuple type.
  747. template <typename T>
  748. void PrintTupleTo(const T& t, ::std::ostream* os) {
  749. *os << "(";
  750. TuplePrefixPrinter< ::std::tr1::tuple_size<T>::value>::
  751. PrintPrefixTo(t, os);
  752. *os << ")";
  753. }
  754. // Prints the fields of a tuple tersely to a string vector, one
  755. // element for each field. See the comment before
  756. // UniversalTersePrint() for how we define "tersely".
  757. template <typename Tuple>
  758. Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
  759. Strings result;
  760. TuplePrefixPrinter< ::std::tr1::tuple_size<Tuple>::value>::
  761. TersePrintPrefixToStrings(value, &result);
  762. return result;
  763. }
  764. #endif // GTEST_HAS_TR1_TUPLE
  765. } // namespace internal
  766. template <typename T>
  767. ::std::string PrintToString(const T& value) {
  768. ::std::stringstream ss;
  769. internal::UniversalTersePrinter<T>::Print(value, &ss);
  770. return ss.str();
  771. }
  772. } // namespace testing
  773. #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_