gtest-printers.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 implements a universal value printer that can print a
  32. // value of any type T:
  33. //
  34. // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
  35. //
  36. // A user can teach this function how to print a class type T by
  37. // defining either operator<<() or PrintTo() in the namespace that
  38. // defines T. More specifically, the FIRST defined function in the
  39. // following list will be used (assuming T is defined in namespace
  40. // foo):
  41. //
  42. // 1. foo::PrintTo(const T&, ostream*)
  43. // 2. operator<<(ostream&, const T&) defined in either foo or the
  44. // global namespace.
  45. // * Prefer AbslStringify(..) to operator<<(..), per https://abseil.io/tips/215.
  46. // * Define foo::PrintTo(..) if the type already has AbslStringify(..), but an
  47. // alternative presentation in test results is of interest.
  48. //
  49. // However if T is an STL-style container then it is printed element-wise
  50. // unless foo::PrintTo(const T&, ostream*) is defined. Note that
  51. // operator<<() is ignored for container types.
  52. //
  53. // If none of the above is defined, it will print the debug string of
  54. // the value if it is a protocol buffer, or print the raw bytes in the
  55. // value otherwise.
  56. //
  57. // To aid debugging: when T is a reference type, the address of the
  58. // value is also printed; when T is a (const) char pointer, both the
  59. // pointer value and the NUL-terminated string it points to are
  60. // printed.
  61. //
  62. // We also provide some convenient wrappers:
  63. //
  64. // // Prints a value to a string. For a (const or not) char
  65. // // pointer, the NUL-terminated string (but not the pointer) is
  66. // // printed.
  67. // std::string ::testing::PrintToString(const T& value);
  68. //
  69. // // Prints a value tersely: for a reference type, the referenced
  70. // // value (but not the address) is printed; for a (const or not) char
  71. // // pointer, the NUL-terminated string (but not the pointer) is
  72. // // printed.
  73. // void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
  74. //
  75. // // Prints value using the type inferred by the compiler. The difference
  76. // // from UniversalTersePrint() is that this function prints both the
  77. // // pointer and the NUL-terminated string for a (const or not) char pointer.
  78. // void ::testing::internal::UniversalPrint(const T& value, ostream*);
  79. //
  80. // // Prints the fields of a tuple tersely to a string vector, one
  81. // // element for each field. Tuple support must be enabled in
  82. // // gtest-port.h.
  83. // std::vector<string> UniversalTersePrintTupleFieldsToStrings(
  84. // const Tuple& value);
  85. //
  86. // Known limitation:
  87. //
  88. // The print primitives print the elements of an STL-style container
  89. // using the compiler-inferred type of *iter where iter is a
  90. // const_iterator of the container. When const_iterator is an input
  91. // iterator but not a forward iterator, this inferred type may not
  92. // match value_type, and the print output may be incorrect. In
  93. // practice, this is rarely a problem as for most containers
  94. // const_iterator is a forward iterator. We'll fix this if there's an
  95. // actual need for it. Note that this fix cannot rely on value_type
  96. // being defined as many user-defined container types don't have
  97. // value_type.
  98. // IWYU pragma: private, include "gtest/gtest.h"
  99. // IWYU pragma: friend gtest/.*
  100. // IWYU pragma: friend gmock/.*
  101. #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
  102. #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
  103. #include <functional>
  104. #include <memory>
  105. #include <ostream> // NOLINT
  106. #include <sstream>
  107. #include <string>
  108. #include <tuple>
  109. #include <type_traits>
  110. #include <typeinfo>
  111. #include <utility>
  112. #include <vector>
  113. #ifdef GTEST_HAS_ABSL
  114. #include "absl/strings/has_absl_stringify.h"
  115. #include "absl/strings/str_cat.h"
  116. #endif // GTEST_HAS_ABSL
  117. #include "gtest/internal/gtest-internal.h"
  118. #include "gtest/internal/gtest-port.h"
  119. #if GTEST_INTERNAL_HAS_STD_SPAN
  120. #include <span> // NOLINT
  121. #endif // GTEST_INTERNAL_HAS_STD_SPAN
  122. namespace testing {
  123. // Definitions in the internal* namespaces are subject to change without notice.
  124. // DO NOT USE THEM IN USER CODE!
  125. namespace internal {
  126. template <typename T>
  127. void UniversalPrint(const T& value, ::std::ostream* os);
  128. template <typename T>
  129. struct IsStdSpan {
  130. static constexpr bool value = false;
  131. };
  132. #if GTEST_INTERNAL_HAS_STD_SPAN
  133. template <typename E>
  134. struct IsStdSpan<std::span<E>> {
  135. static constexpr bool value = true;
  136. };
  137. #endif // GTEST_INTERNAL_HAS_STD_SPAN
  138. // Used to print an STL-style container when the user doesn't define
  139. // a PrintTo() for it.
  140. //
  141. // NOTE: Since std::span does not have const_iterator until C++23, it would
  142. // fail IsContainerTest before C++23. However, IsContainerTest only uses
  143. // the presence of const_iterator to avoid treating iterators as containers
  144. // because of iterator::iterator. Which means std::span satisfies the *intended*
  145. // condition of IsContainerTest.
  146. struct ContainerPrinter {
  147. template <typename T,
  148. typename = typename std::enable_if<
  149. ((sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
  150. !IsRecursiveContainer<T>::value) ||
  151. IsStdSpan<T>::value>::type>
  152. static void PrintValue(const T& container, std::ostream* os) {
  153. const size_t kMaxCount = 32; // The maximum number of elements to print.
  154. *os << '{';
  155. size_t count = 0;
  156. for (auto&& elem : container) {
  157. if (count > 0) {
  158. *os << ',';
  159. if (count == kMaxCount) { // Enough has been printed.
  160. *os << " ...";
  161. break;
  162. }
  163. }
  164. *os << ' ';
  165. // We cannot call PrintTo(elem, os) here as PrintTo() doesn't
  166. // handle `elem` being a native array.
  167. internal::UniversalPrint(elem, os);
  168. ++count;
  169. }
  170. if (count > 0) {
  171. *os << ' ';
  172. }
  173. *os << '}';
  174. }
  175. };
  176. // Used to print a pointer that is neither a char pointer nor a member
  177. // pointer, when the user doesn't define PrintTo() for it. (A member
  178. // variable pointer or member function pointer doesn't really point to
  179. // a location in the address space. Their representation is
  180. // implementation-defined. Therefore they will be printed as raw
  181. // bytes.)
  182. struct FunctionPointerPrinter {
  183. template <typename T, typename = typename std::enable_if<
  184. std::is_function<T>::value>::type>
  185. static void PrintValue(T* p, ::std::ostream* os) {
  186. if (p == nullptr) {
  187. *os << "NULL";
  188. } else {
  189. // T is a function type, so '*os << p' doesn't do what we want
  190. // (it just prints p as bool). We want to print p as a const
  191. // void*.
  192. *os << reinterpret_cast<const void*>(p);
  193. }
  194. }
  195. };
  196. struct PointerPrinter {
  197. template <typename T>
  198. static void PrintValue(T* p, ::std::ostream* os) {
  199. if (p == nullptr) {
  200. *os << "NULL";
  201. } else {
  202. // T is not a function type. We just call << to print p,
  203. // relying on ADL to pick up user-defined << for their pointer
  204. // types, if any.
  205. *os << p;
  206. }
  207. }
  208. };
  209. namespace internal_stream_operator_without_lexical_name_lookup {
  210. // The presence of an operator<< here will terminate lexical scope lookup
  211. // straight away (even though it cannot be a match because of its argument
  212. // types). Thus, the two operator<< calls in StreamPrinter will find only ADL
  213. // candidates.
  214. struct LookupBlocker {};
  215. void operator<<(LookupBlocker, LookupBlocker);
  216. struct StreamPrinter {
  217. template <typename T,
  218. // Don't accept member pointers here. We'd print them via implicit
  219. // conversion to bool, which isn't useful.
  220. typename = typename std::enable_if<
  221. !std::is_member_pointer<T>::value>::type>
  222. // Only accept types for which we can find a streaming operator via
  223. // ADL (possibly involving implicit conversions).
  224. // (Use SFINAE via return type, because it seems GCC < 12 doesn't handle name
  225. // lookup properly when we do it in the template parameter list.)
  226. static auto PrintValue(const T& value,
  227. ::std::ostream* os) -> decltype((void)(*os << value)) {
  228. // Call streaming operator found by ADL, possibly with implicit conversions
  229. // of the arguments.
  230. *os << value;
  231. }
  232. };
  233. } // namespace internal_stream_operator_without_lexical_name_lookup
  234. struct ProtobufPrinter {
  235. // We print a protobuf using its ShortDebugString() when the string
  236. // doesn't exceed this many characters; otherwise we print it using
  237. // DebugString() for better readability.
  238. static const size_t kProtobufOneLinerMaxLength = 50;
  239. template <typename T,
  240. typename = typename std::enable_if<
  241. internal::HasDebugStringAndShortDebugString<T>::value>::type>
  242. static void PrintValue(const T& value, ::std::ostream* os) {
  243. std::string pretty_str = value.ShortDebugString();
  244. if (pretty_str.length() > kProtobufOneLinerMaxLength) {
  245. pretty_str = "\n" + value.DebugString();
  246. }
  247. *os << ("<" + pretty_str + ">");
  248. }
  249. };
  250. struct ConvertibleToIntegerPrinter {
  251. // Since T has no << operator or PrintTo() but can be implicitly
  252. // converted to BiggestInt, we print it as a BiggestInt.
  253. //
  254. // Most likely T is an enum type (either named or unnamed), in which
  255. // case printing it as an integer is the desired behavior. In case
  256. // T is not an enum, printing it as an integer is the best we can do
  257. // given that it has no user-defined printer.
  258. static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
  259. *os << value;
  260. }
  261. };
  262. struct ConvertibleToStringViewPrinter {
  263. #if GTEST_INTERNAL_HAS_STRING_VIEW
  264. static void PrintValue(internal::StringView value, ::std::ostream* os) {
  265. internal::UniversalPrint(value, os);
  266. }
  267. #endif
  268. };
  269. #ifdef GTEST_HAS_ABSL
  270. struct ConvertibleToAbslStringifyPrinter {
  271. template <typename T,
  272. typename = typename std::enable_if<
  273. absl::HasAbslStringify<T>::value>::type> // NOLINT
  274. static void PrintValue(const T& value, ::std::ostream* os) {
  275. *os << absl::StrCat(value);
  276. }
  277. };
  278. #endif // GTEST_HAS_ABSL
  279. // Prints the given number of bytes in the given object to the given
  280. // ostream.
  281. GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
  282. size_t count, ::std::ostream* os);
  283. struct RawBytesPrinter {
  284. // SFINAE on `sizeof` to make sure we have a complete type.
  285. template <typename T, size_t = sizeof(T)>
  286. static void PrintValue(const T& value, ::std::ostream* os) {
  287. PrintBytesInObjectTo(
  288. static_cast<const unsigned char*>(
  289. // Load bearing cast to void* to support iOS
  290. reinterpret_cast<const void*>(std::addressof(value))),
  291. sizeof(value), os);
  292. }
  293. };
  294. struct FallbackPrinter {
  295. template <typename T>
  296. static void PrintValue(const T&, ::std::ostream* os) {
  297. *os << "(incomplete type)";
  298. }
  299. };
  300. // Try every printer in order and return the first one that works.
  301. template <typename T, typename E, typename Printer, typename... Printers>
  302. struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
  303. template <typename T, typename Printer, typename... Printers>
  304. struct FindFirstPrinter<
  305. T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
  306. Printer, Printers...> {
  307. using type = Printer;
  308. };
  309. // Select the best printer in the following order:
  310. // - Print containers (they have begin/end/etc).
  311. // - Print function pointers.
  312. // - Print object pointers.
  313. // - Print protocol buffers.
  314. // - Use the stream operator, if available.
  315. // - Print types convertible to BiggestInt.
  316. // - Print types convertible to StringView, if available.
  317. // - Fallback to printing the raw bytes of the object.
  318. template <typename T>
  319. void PrintWithFallback(const T& value, ::std::ostream* os) {
  320. using Printer = typename FindFirstPrinter<
  321. T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
  322. ProtobufPrinter,
  323. #ifdef GTEST_HAS_ABSL
  324. ConvertibleToAbslStringifyPrinter,
  325. #endif // GTEST_HAS_ABSL
  326. internal_stream_operator_without_lexical_name_lookup::StreamPrinter,
  327. ConvertibleToIntegerPrinter, ConvertibleToStringViewPrinter,
  328. RawBytesPrinter, FallbackPrinter>::type;
  329. Printer::PrintValue(value, os);
  330. }
  331. // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
  332. // value of type ToPrint that is an operand of a comparison assertion
  333. // (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in
  334. // the comparison, and is used to help determine the best way to
  335. // format the value. In particular, when the value is a C string
  336. // (char pointer) and the other operand is an STL string object, we
  337. // want to format the C string as a string, since we know it is
  338. // compared by value with the string object. If the value is a char
  339. // pointer but the other operand is not an STL string object, we don't
  340. // know whether the pointer is supposed to point to a NUL-terminated
  341. // string, and thus want to print it as a pointer to be safe.
  342. //
  343. // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
  344. // The default case.
  345. template <typename ToPrint, typename OtherOperand>
  346. class FormatForComparison {
  347. public:
  348. static ::std::string Format(const ToPrint& value) {
  349. return ::testing::PrintToString(value);
  350. }
  351. };
  352. // Array.
  353. template <typename ToPrint, size_t N, typename OtherOperand>
  354. class FormatForComparison<ToPrint[N], OtherOperand> {
  355. public:
  356. static ::std::string Format(const ToPrint* value) {
  357. return FormatForComparison<const ToPrint*, OtherOperand>::Format(value);
  358. }
  359. };
  360. // By default, print C string as pointers to be safe, as we don't know
  361. // whether they actually point to a NUL-terminated string.
  362. #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \
  363. template <typename OtherOperand> \
  364. class FormatForComparison<CharType*, OtherOperand> { \
  365. public: \
  366. static ::std::string Format(CharType* value) { \
  367. return ::testing::PrintToString(static_cast<const void*>(value)); \
  368. } \
  369. }
  370. GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
  371. GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
  372. GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
  373. GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
  374. #ifdef __cpp_lib_char8_t
  375. GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t);
  376. GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t);
  377. #endif
  378. GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t);
  379. GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t);
  380. GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t);
  381. GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t);
  382. #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
  383. // If a C string is compared with an STL string object, we know it's meant
  384. // to point to a NUL-terminated string, and thus can print it as a string.
  385. #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
  386. template <> \
  387. class FormatForComparison<CharType*, OtherStringType> { \
  388. public: \
  389. static ::std::string Format(CharType* value) { \
  390. return ::testing::PrintToString(value); \
  391. } \
  392. }
  393. GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
  394. GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
  395. #ifdef __cpp_lib_char8_t
  396. GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string);
  397. GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string);
  398. #endif
  399. GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string);
  400. GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string);
  401. GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string);
  402. GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string);
  403. #if GTEST_HAS_STD_WSTRING
  404. GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
  405. GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
  406. #endif
  407. #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
  408. // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
  409. // operand to be used in a failure message. The type (but not value)
  410. // of the other operand may affect the format. This allows us to
  411. // print a char* as a raw pointer when it is compared against another
  412. // char* or void*, and print it as a C string when it is compared
  413. // against an std::string object, for example.
  414. //
  415. // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
  416. template <typename T1, typename T2>
  417. std::string FormatForComparisonFailureMessage(const T1& value,
  418. const T2& /* other_operand */) {
  419. return FormatForComparison<T1, T2>::Format(value);
  420. }
  421. // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
  422. // value to the given ostream. The caller must ensure that
  423. // 'ostream_ptr' is not NULL, or the behavior is undefined.
  424. //
  425. // We define UniversalPrinter as a class template (as opposed to a
  426. // function template), as we need to partially specialize it for
  427. // reference types, which cannot be done with function templates.
  428. template <typename T>
  429. class UniversalPrinter;
  430. // Prints the given value using the << operator if it has one;
  431. // otherwise prints the bytes in it. This is what
  432. // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
  433. // or overloaded for type T.
  434. //
  435. // A user can override this behavior for a class type Foo by defining
  436. // an overload of PrintTo() in the namespace where Foo is defined. We
  437. // give the user this option as sometimes defining a << operator for
  438. // Foo is not desirable (e.g. the coding style may prevent doing it,
  439. // or there is already a << operator but it doesn't do what the user
  440. // wants).
  441. template <typename T>
  442. void PrintTo(const T& value, ::std::ostream* os) {
  443. internal::PrintWithFallback(value, os);
  444. }
  445. // The following list of PrintTo() overloads tells
  446. // UniversalPrinter<T>::Print() how to print standard types (built-in
  447. // types, strings, plain arrays, and pointers).
  448. // Overloads for various char types.
  449. GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
  450. GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
  451. inline void PrintTo(char c, ::std::ostream* os) {
  452. // When printing a plain char, we always treat it as unsigned. This
  453. // way, the output won't be affected by whether the compiler thinks
  454. // char is signed or not.
  455. PrintTo(static_cast<unsigned char>(c), os);
  456. }
  457. // Overloads for other simple built-in types.
  458. inline void PrintTo(bool x, ::std::ostream* os) {
  459. *os << (x ? "true" : "false");
  460. }
  461. // Overload for wchar_t type.
  462. // Prints a wchar_t as a symbol if it is printable or as its internal
  463. // code otherwise and also as its decimal code (except for L'\0').
  464. // The L'\0' char is printed as "L'\\0'". The decimal code is printed
  465. // as signed integer when wchar_t is implemented by the compiler
  466. // as a signed type and is printed as an unsigned integer when wchar_t
  467. // is implemented as an unsigned type.
  468. GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
  469. GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
  470. inline void PrintTo(char16_t c, ::std::ostream* os) {
  471. PrintTo(ImplicitCast_<char32_t>(c), os);
  472. }
  473. #ifdef __cpp_lib_char8_t
  474. inline void PrintTo(char8_t c, ::std::ostream* os) {
  475. PrintTo(ImplicitCast_<char32_t>(c), os);
  476. }
  477. #endif
  478. // gcc/clang __{u,}int128_t
  479. #if defined(__SIZEOF_INT128__)
  480. GTEST_API_ void PrintTo(__uint128_t v, ::std::ostream* os);
  481. GTEST_API_ void PrintTo(__int128_t v, ::std::ostream* os);
  482. #endif // __SIZEOF_INT128__
  483. // The default resolution used to print floating-point values uses only
  484. // 6 digits, which can be confusing if a test compares two values whose
  485. // difference lies in the 7th digit. So we'd like to print out numbers
  486. // in full precision.
  487. // However if the value is something simple like 1.1, full will print a
  488. // long string like 1.100000001 due to floating-point numbers not using
  489. // a base of 10. This routiune returns an appropriate resolution for a
  490. // given floating-point number, that is, 6 if it will be accurate, or a
  491. // max_digits10 value (full precision) if it won't, for values between
  492. // 0.0001 and one million.
  493. // It does this by computing what those digits would be (by multiplying
  494. // by an appropriate power of 10), then dividing by that power again to
  495. // see if gets the original value back.
  496. // A similar algorithm applies for values larger than one million; note
  497. // that for those values, we must divide to get a six-digit number, and
  498. // then multiply to possibly get the original value again.
  499. template <typename FloatType>
  500. int AppropriateResolution(FloatType val) {
  501. int full = std::numeric_limits<FloatType>::max_digits10;
  502. if (val < 0) val = -val;
  503. #ifdef __GNUC__
  504. #pragma GCC diagnostic push
  505. #pragma GCC diagnostic ignored "-Wfloat-equal"
  506. #endif
  507. if (val < 1000000) {
  508. FloatType mulfor6 = 1e10;
  509. // Without these static casts, the template instantiation for float would
  510. // fail to compile when -Wdouble-promotion is enabled, as the arithmetic and
  511. // comparison logic would promote floats to doubles.
  512. if (val >= static_cast<FloatType>(100000.0)) { // 100,000 to 999,999
  513. mulfor6 = 1.0;
  514. } else if (val >= static_cast<FloatType>(10000.0)) {
  515. mulfor6 = 1e1;
  516. } else if (val >= static_cast<FloatType>(1000.0)) {
  517. mulfor6 = 1e2;
  518. } else if (val >= static_cast<FloatType>(100.0)) {
  519. mulfor6 = 1e3;
  520. } else if (val >= static_cast<FloatType>(10.0)) {
  521. mulfor6 = 1e4;
  522. } else if (val >= static_cast<FloatType>(1.0)) {
  523. mulfor6 = 1e5;
  524. } else if (val >= static_cast<FloatType>(0.1)) {
  525. mulfor6 = 1e6;
  526. } else if (val >= static_cast<FloatType>(0.01)) {
  527. mulfor6 = 1e7;
  528. } else if (val >= static_cast<FloatType>(0.001)) {
  529. mulfor6 = 1e8;
  530. } else if (val >= static_cast<FloatType>(0.0001)) {
  531. mulfor6 = 1e9;
  532. }
  533. if (static_cast<FloatType>(static_cast<int32_t>(
  534. val * mulfor6 + (static_cast<FloatType>(0.5)))) /
  535. mulfor6 ==
  536. val)
  537. return 6;
  538. } else if (val < static_cast<FloatType>(1e10)) {
  539. FloatType divfor6 = static_cast<FloatType>(1.0);
  540. if (val >= static_cast<FloatType>(1e9)) { // 1,000,000,000 to 9,999,999,999
  541. divfor6 = 10000;
  542. } else if (val >=
  543. static_cast<FloatType>(1e8)) { // 100,000,000 to 999,999,999
  544. divfor6 = 1000;
  545. } else if (val >=
  546. static_cast<FloatType>(1e7)) { // 10,000,000 to 99,999,999
  547. divfor6 = 100;
  548. } else if (val >= static_cast<FloatType>(1e6)) { // 1,000,000 to 9,999,999
  549. divfor6 = 10;
  550. }
  551. if (static_cast<FloatType>(static_cast<int32_t>(
  552. val / divfor6 + (static_cast<FloatType>(0.5)))) *
  553. divfor6 ==
  554. val)
  555. return 6;
  556. }
  557. #ifdef __GNUC__
  558. #pragma GCC diagnostic pop
  559. #endif
  560. return full;
  561. }
  562. inline void PrintTo(float f, ::std::ostream* os) {
  563. auto old_precision = os->precision();
  564. os->precision(AppropriateResolution(f));
  565. *os << f;
  566. os->precision(old_precision);
  567. }
  568. inline void PrintTo(double d, ::std::ostream* os) {
  569. auto old_precision = os->precision();
  570. os->precision(AppropriateResolution(d));
  571. *os << d;
  572. os->precision(old_precision);
  573. }
  574. // Overloads for C strings.
  575. GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
  576. inline void PrintTo(char* s, ::std::ostream* os) {
  577. PrintTo(ImplicitCast_<const char*>(s), os);
  578. }
  579. // signed/unsigned char is often used for representing binary data, so
  580. // we print pointers to it as void* to be safe.
  581. inline void PrintTo(const signed char* s, ::std::ostream* os) {
  582. PrintTo(ImplicitCast_<const void*>(s), os);
  583. }
  584. inline void PrintTo(signed char* s, ::std::ostream* os) {
  585. PrintTo(ImplicitCast_<const void*>(s), os);
  586. }
  587. inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
  588. PrintTo(ImplicitCast_<const void*>(s), os);
  589. }
  590. inline void PrintTo(unsigned char* s, ::std::ostream* os) {
  591. PrintTo(ImplicitCast_<const void*>(s), os);
  592. }
  593. #ifdef __cpp_lib_char8_t
  594. // Overloads for u8 strings.
  595. GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os);
  596. inline void PrintTo(char8_t* s, ::std::ostream* os) {
  597. PrintTo(ImplicitCast_<const char8_t*>(s), os);
  598. }
  599. #endif
  600. // Overloads for u16 strings.
  601. GTEST_API_ void PrintTo(const char16_t* s, ::std::ostream* os);
  602. inline void PrintTo(char16_t* s, ::std::ostream* os) {
  603. PrintTo(ImplicitCast_<const char16_t*>(s), os);
  604. }
  605. // Overloads for u32 strings.
  606. GTEST_API_ void PrintTo(const char32_t* s, ::std::ostream* os);
  607. inline void PrintTo(char32_t* s, ::std::ostream* os) {
  608. PrintTo(ImplicitCast_<const char32_t*>(s), os);
  609. }
  610. // MSVC can be configured to define wchar_t as a typedef of unsigned
  611. // short. It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
  612. // type. When wchar_t is a typedef, defining an overload for const
  613. // wchar_t* would cause unsigned short* be printed as a wide string,
  614. // possibly causing invalid memory accesses.
  615. #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
  616. // Overloads for wide C strings
  617. GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
  618. inline void PrintTo(wchar_t* s, ::std::ostream* os) {
  619. PrintTo(ImplicitCast_<const wchar_t*>(s), os);
  620. }
  621. #endif
  622. // Overload for C arrays. Multi-dimensional arrays are printed
  623. // properly.
  624. // Prints the given number of elements in an array, without printing
  625. // the curly braces.
  626. template <typename T>
  627. void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
  628. UniversalPrint(a[0], os);
  629. for (size_t i = 1; i != count; i++) {
  630. *os << ", ";
  631. UniversalPrint(a[i], os);
  632. }
  633. }
  634. // Overloads for ::std::string.
  635. GTEST_API_ void PrintStringTo(const ::std::string& s, ::std::ostream* os);
  636. inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
  637. PrintStringTo(s, os);
  638. }
  639. // Overloads for ::std::u8string
  640. #ifdef __cpp_lib_char8_t
  641. GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os);
  642. inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
  643. PrintU8StringTo(s, os);
  644. }
  645. #endif
  646. // Overloads for ::std::u16string
  647. GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os);
  648. inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
  649. PrintU16StringTo(s, os);
  650. }
  651. // Overloads for ::std::u32string
  652. GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os);
  653. inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
  654. PrintU32StringTo(s, os);
  655. }
  656. // Overloads for ::std::wstring.
  657. #if GTEST_HAS_STD_WSTRING
  658. GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os);
  659. inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
  660. PrintWideStringTo(s, os);
  661. }
  662. #endif // GTEST_HAS_STD_WSTRING
  663. #if GTEST_INTERNAL_HAS_STRING_VIEW
  664. // Overload for internal::StringView.
  665. inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
  666. PrintTo(::std::string(sp), os);
  667. }
  668. #endif // GTEST_INTERNAL_HAS_STRING_VIEW
  669. inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
  670. #if GTEST_HAS_RTTI
  671. inline void PrintTo(const std::type_info& info, std::ostream* os) {
  672. *os << internal::GetTypeName(info);
  673. }
  674. #endif // GTEST_HAS_RTTI
  675. template <typename T>
  676. void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
  677. UniversalPrinter<T&>::Print(ref.get(), os);
  678. }
  679. inline const void* VoidifyPointer(const void* p) { return p; }
  680. inline const void* VoidifyPointer(volatile const void* p) {
  681. return const_cast<const void*>(p);
  682. }
  683. template <typename T, typename Ptr>
  684. void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) {
  685. if (ptr == nullptr) {
  686. *os << "(nullptr)";
  687. } else {
  688. // We can't print the value. Just print the pointer..
  689. *os << "(" << (VoidifyPointer)(ptr.get()) << ")";
  690. }
  691. }
  692. template <typename T, typename Ptr,
  693. typename = typename std::enable_if<!std::is_void<T>::value &&
  694. !std::is_array<T>::value>::type>
  695. void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) {
  696. if (ptr == nullptr) {
  697. *os << "(nullptr)";
  698. } else {
  699. *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = ";
  700. UniversalPrinter<T>::Print(*ptr, os);
  701. *os << ")";
  702. }
  703. }
  704. template <typename T, typename D>
  705. void PrintTo(const std::unique_ptr<T, D>& ptr, std::ostream* os) {
  706. (PrintSmartPointer<T>)(ptr, os, 0);
  707. }
  708. template <typename T>
  709. void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) {
  710. (PrintSmartPointer<T>)(ptr, os, 0);
  711. }
  712. // Helper function for printing a tuple. T must be instantiated with
  713. // a tuple type.
  714. template <typename T>
  715. void PrintTupleTo(const T&, std::integral_constant<size_t, 0>,
  716. ::std::ostream*) {}
  717. template <typename T, size_t I>
  718. void PrintTupleTo(const T& t, std::integral_constant<size_t, I>,
  719. ::std::ostream* os) {
  720. PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
  721. GTEST_INTENTIONAL_CONST_COND_PUSH_()
  722. if (I > 1) {
  723. GTEST_INTENTIONAL_CONST_COND_POP_()
  724. *os << ", ";
  725. }
  726. UniversalPrinter<typename std::tuple_element<I - 1, T>::type>::Print(
  727. std::get<I - 1>(t), os);
  728. }
  729. template <typename... Types>
  730. void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
  731. *os << "(";
  732. PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os);
  733. *os << ")";
  734. }
  735. // Overload for std::pair.
  736. template <typename T1, typename T2>
  737. void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
  738. *os << '(';
  739. // We cannot use UniversalPrint(value.first, os) here, as T1 may be
  740. // a reference type. The same for printing value.second.
  741. UniversalPrinter<T1>::Print(value.first, os);
  742. *os << ", ";
  743. UniversalPrinter<T2>::Print(value.second, os);
  744. *os << ')';
  745. }
  746. // Implements printing a non-reference type T by letting the compiler
  747. // pick the right overload of PrintTo() for T.
  748. template <typename T>
  749. class UniversalPrinter {
  750. public:
  751. // MSVC warns about adding const to a function type, so we want to
  752. // disable the warning.
  753. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
  754. // Note: we deliberately don't call this PrintTo(), as that name
  755. // conflicts with ::testing::internal::PrintTo in the body of the
  756. // function.
  757. static void Print(const T& value, ::std::ostream* os) {
  758. // By default, ::testing::internal::PrintTo() is used for printing
  759. // the value.
  760. //
  761. // Thanks to Koenig look-up, if T is a class and has its own
  762. // PrintTo() function defined in its namespace, that function will
  763. // be visible here. Since it is more specific than the generic ones
  764. // in ::testing::internal, it will be picked by the compiler in the
  765. // following statement - exactly what we want.
  766. PrintTo(value, os);
  767. }
  768. GTEST_DISABLE_MSC_WARNINGS_POP_()
  769. };
  770. // Remove any const-qualifiers before passing a type to UniversalPrinter.
  771. template <typename T>
  772. class UniversalPrinter<const T> : public UniversalPrinter<T> {};
  773. #if GTEST_INTERNAL_HAS_ANY
  774. // Printer for std::any / absl::any
  775. template <>
  776. class UniversalPrinter<Any> {
  777. public:
  778. static void Print(const Any& value, ::std::ostream* os) {
  779. if (value.has_value()) {
  780. *os << "value of type " << GetTypeName(value);
  781. } else {
  782. *os << "no value";
  783. }
  784. }
  785. private:
  786. static std::string GetTypeName(const Any& value) {
  787. #if GTEST_HAS_RTTI
  788. return internal::GetTypeName(value.type());
  789. #else
  790. static_cast<void>(value); // possibly unused
  791. return "<unknown_type>";
  792. #endif // GTEST_HAS_RTTI
  793. }
  794. };
  795. #endif // GTEST_INTERNAL_HAS_ANY
  796. #if GTEST_INTERNAL_HAS_OPTIONAL
  797. // Printer for std::optional / absl::optional
  798. template <typename T>
  799. class UniversalPrinter<Optional<T>> {
  800. public:
  801. static void Print(const Optional<T>& value, ::std::ostream* os) {
  802. *os << '(';
  803. if (!value) {
  804. *os << "nullopt";
  805. } else {
  806. UniversalPrint(*value, os);
  807. }
  808. *os << ')';
  809. }
  810. };
  811. template <>
  812. class UniversalPrinter<decltype(Nullopt())> {
  813. public:
  814. static void Print(decltype(Nullopt()), ::std::ostream* os) {
  815. *os << "(nullopt)";
  816. }
  817. };
  818. #endif // GTEST_INTERNAL_HAS_OPTIONAL
  819. #if GTEST_INTERNAL_HAS_VARIANT
  820. // Printer for std::variant / absl::variant
  821. template <typename... T>
  822. class UniversalPrinter<Variant<T...>> {
  823. public:
  824. static void Print(const Variant<T...>& value, ::std::ostream* os) {
  825. *os << '(';
  826. #ifdef GTEST_HAS_ABSL
  827. absl::visit(Visitor{os, value.index()}, value);
  828. #else
  829. std::visit(Visitor{os, value.index()}, value);
  830. #endif // GTEST_HAS_ABSL
  831. *os << ')';
  832. }
  833. private:
  834. struct Visitor {
  835. template <typename U>
  836. void operator()(const U& u) const {
  837. *os << "'" << GetTypeName<U>() << "(index = " << index
  838. << ")' with value ";
  839. UniversalPrint(u, os);
  840. }
  841. ::std::ostream* os;
  842. std::size_t index;
  843. };
  844. };
  845. #endif // GTEST_INTERNAL_HAS_VARIANT
  846. // UniversalPrintArray(begin, len, os) prints an array of 'len'
  847. // elements, starting at address 'begin'.
  848. template <typename T>
  849. void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
  850. if (len == 0) {
  851. *os << "{}";
  852. } else {
  853. *os << "{ ";
  854. const size_t kThreshold = 18;
  855. const size_t kChunkSize = 8;
  856. // If the array has more than kThreshold elements, we'll have to
  857. // omit some details by printing only the first and the last
  858. // kChunkSize elements.
  859. if (len <= kThreshold) {
  860. PrintRawArrayTo(begin, len, os);
  861. } else {
  862. PrintRawArrayTo(begin, kChunkSize, os);
  863. *os << ", ..., ";
  864. PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
  865. }
  866. *os << " }";
  867. }
  868. }
  869. // This overload prints a (const) char array compactly.
  870. GTEST_API_ void UniversalPrintArray(const char* begin, size_t len,
  871. ::std::ostream* os);
  872. #ifdef __cpp_lib_char8_t
  873. // This overload prints a (const) char8_t array compactly.
  874. GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len,
  875. ::std::ostream* os);
  876. #endif
  877. // This overload prints a (const) char16_t array compactly.
  878. GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len,
  879. ::std::ostream* os);
  880. // This overload prints a (const) char32_t array compactly.
  881. GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len,
  882. ::std::ostream* os);
  883. // This overload prints a (const) wchar_t array compactly.
  884. GTEST_API_ void UniversalPrintArray(const wchar_t* begin, size_t len,
  885. ::std::ostream* os);
  886. // Implements printing an array type T[N].
  887. template <typename T, size_t N>
  888. class UniversalPrinter<T[N]> {
  889. public:
  890. // Prints the given array, omitting some elements when there are too
  891. // many.
  892. static void Print(const T (&a)[N], ::std::ostream* os) {
  893. UniversalPrintArray(a, N, os);
  894. }
  895. };
  896. // Implements printing a reference type T&.
  897. template <typename T>
  898. class UniversalPrinter<T&> {
  899. public:
  900. // MSVC warns about adding const to a function type, so we want to
  901. // disable the warning.
  902. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
  903. static void Print(const T& value, ::std::ostream* os) {
  904. // Prints the address of the value. We use reinterpret_cast here
  905. // as static_cast doesn't compile when T is a function type.
  906. *os << "@" << reinterpret_cast<const void*>(&value) << " ";
  907. // Then prints the value itself.
  908. UniversalPrint(value, os);
  909. }
  910. GTEST_DISABLE_MSC_WARNINGS_POP_()
  911. };
  912. // Prints a value tersely: for a reference type, the referenced value
  913. // (but not the address) is printed; for a (const) char pointer, the
  914. // NUL-terminated string (but not the pointer) is printed.
  915. template <typename T>
  916. class UniversalTersePrinter {
  917. public:
  918. static void Print(const T& value, ::std::ostream* os) {
  919. UniversalPrint(value, os);
  920. }
  921. };
  922. template <typename T>
  923. class UniversalTersePrinter<T&> {
  924. public:
  925. static void Print(const T& value, ::std::ostream* os) {
  926. UniversalPrint(value, os);
  927. }
  928. };
  929. template <typename T>
  930. class UniversalTersePrinter<std::reference_wrapper<T>> {
  931. public:
  932. static void Print(std::reference_wrapper<T> value, ::std::ostream* os) {
  933. UniversalTersePrinter<T>::Print(value.get(), os);
  934. }
  935. };
  936. template <typename T, size_t N>
  937. class UniversalTersePrinter<T[N]> {
  938. public:
  939. static void Print(const T (&value)[N], ::std::ostream* os) {
  940. UniversalPrinter<T[N]>::Print(value, os);
  941. }
  942. };
  943. template <>
  944. class UniversalTersePrinter<const char*> {
  945. public:
  946. static void Print(const char* str, ::std::ostream* os) {
  947. if (str == nullptr) {
  948. *os << "NULL";
  949. } else {
  950. UniversalPrint(std::string(str), os);
  951. }
  952. }
  953. };
  954. template <>
  955. class UniversalTersePrinter<char*> : public UniversalTersePrinter<const char*> {
  956. };
  957. #ifdef __cpp_lib_char8_t
  958. template <>
  959. class UniversalTersePrinter<const char8_t*> {
  960. public:
  961. static void Print(const char8_t* str, ::std::ostream* os) {
  962. if (str == nullptr) {
  963. *os << "NULL";
  964. } else {
  965. UniversalPrint(::std::u8string(str), os);
  966. }
  967. }
  968. };
  969. template <>
  970. class UniversalTersePrinter<char8_t*>
  971. : public UniversalTersePrinter<const char8_t*> {};
  972. #endif
  973. template <>
  974. class UniversalTersePrinter<const char16_t*> {
  975. public:
  976. static void Print(const char16_t* str, ::std::ostream* os) {
  977. if (str == nullptr) {
  978. *os << "NULL";
  979. } else {
  980. UniversalPrint(::std::u16string(str), os);
  981. }
  982. }
  983. };
  984. template <>
  985. class UniversalTersePrinter<char16_t*>
  986. : public UniversalTersePrinter<const char16_t*> {};
  987. template <>
  988. class UniversalTersePrinter<const char32_t*> {
  989. public:
  990. static void Print(const char32_t* str, ::std::ostream* os) {
  991. if (str == nullptr) {
  992. *os << "NULL";
  993. } else {
  994. UniversalPrint(::std::u32string(str), os);
  995. }
  996. }
  997. };
  998. template <>
  999. class UniversalTersePrinter<char32_t*>
  1000. : public UniversalTersePrinter<const char32_t*> {};
  1001. #if GTEST_HAS_STD_WSTRING
  1002. template <>
  1003. class UniversalTersePrinter<const wchar_t*> {
  1004. public:
  1005. static void Print(const wchar_t* str, ::std::ostream* os) {
  1006. if (str == nullptr) {
  1007. *os << "NULL";
  1008. } else {
  1009. UniversalPrint(::std::wstring(str), os);
  1010. }
  1011. }
  1012. };
  1013. #endif
  1014. template <>
  1015. class UniversalTersePrinter<wchar_t*> {
  1016. public:
  1017. static void Print(wchar_t* str, ::std::ostream* os) {
  1018. UniversalTersePrinter<const wchar_t*>::Print(str, os);
  1019. }
  1020. };
  1021. template <typename T>
  1022. void UniversalTersePrint(const T& value, ::std::ostream* os) {
  1023. UniversalTersePrinter<T>::Print(value, os);
  1024. }
  1025. // Prints a value using the type inferred by the compiler. The
  1026. // difference between this and UniversalTersePrint() is that for a
  1027. // (const) char pointer, this prints both the pointer and the
  1028. // NUL-terminated string.
  1029. template <typename T>
  1030. void UniversalPrint(const T& value, ::std::ostream* os) {
  1031. // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
  1032. // UniversalPrinter with T directly.
  1033. typedef T T1;
  1034. UniversalPrinter<T1>::Print(value, os);
  1035. }
  1036. typedef ::std::vector<::std::string> Strings;
  1037. // Tersely prints the first N fields of a tuple to a string vector,
  1038. // one element for each field.
  1039. template <typename Tuple>
  1040. void TersePrintPrefixToStrings(const Tuple&, std::integral_constant<size_t, 0>,
  1041. Strings*) {}
  1042. template <typename Tuple, size_t I>
  1043. void TersePrintPrefixToStrings(const Tuple& t,
  1044. std::integral_constant<size_t, I>,
  1045. Strings* strings) {
  1046. TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
  1047. strings);
  1048. ::std::stringstream ss;
  1049. UniversalTersePrint(std::get<I - 1>(t), &ss);
  1050. strings->push_back(ss.str());
  1051. }
  1052. // Prints the fields of a tuple tersely to a string vector, one
  1053. // element for each field. See the comment before
  1054. // UniversalTersePrint() for how we define "tersely".
  1055. template <typename Tuple>
  1056. Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
  1057. Strings result;
  1058. TersePrintPrefixToStrings(
  1059. value, std::integral_constant<size_t, std::tuple_size<Tuple>::value>(),
  1060. &result);
  1061. return result;
  1062. }
  1063. } // namespace internal
  1064. template <typename T>
  1065. ::std::string PrintToString(const T& value) {
  1066. ::std::stringstream ss;
  1067. internal::UniversalTersePrinter<T>::Print(value, &ss);
  1068. return ss.str();
  1069. }
  1070. } // namespace testing
  1071. // Include any custom printer added by the local installation.
  1072. // We must include this header at the end to make sure it can use the
  1073. // declarations from this file.
  1074. #include "gtest/internal/custom/gtest-printers.h"
  1075. #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_