gtest-printers.h 36 KB

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