1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Google Test - The Google C++ Testing Framework
34 // This file implements a universal value printer that can print a
35 // value of any type T:
37 // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
39 // It uses the << operator when possible, and prints the bytes in the
40 // object otherwise. A user can override its behavior for a class
41 // type Foo by defining either operator<<(::std::ostream&, const Foo&)
42 // or void PrintTo(const Foo&, ::std::ostream*) in the namespace that
45 #include "gtest/gtest-printers.h"
48 #include <ostream> // NOLINT
50 #include "gtest/internal/gtest-port.h"
58 // Prints a segment of bytes in the given object.
59 void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,
60 size_t count, ostream* os) {
62 for (size_t i = 0; i != count; i++) {
63 const size_t j = start + i;
65 // Organizes the bytes into groups of 2 for easy parsing by
72 GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]);
77 // Prints the bytes in the given value to the given ostream.
78 void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,
80 // Tells the user how big the object is.
81 *os << count << "-byte object <";
83 const size_t kThreshold = 132;
84 const size_t kChunkSize = 64;
85 // If the object size is bigger than kThreshold, we'll have to omit
86 // some details by printing only the first and the last kChunkSize
88 // TODO(wan): let the user control the threshold using a flag.
89 if (count < kThreshold) {
90 PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
92 PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
94 // Rounds up to 2-byte boundary.
95 const size_t resume_pos = (count - kChunkSize + 1)/2*2;
96 PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
103 namespace internal2 {
105 // Delegates to PrintBytesInObjectToImpl() to print the bytes in the
106 // given object. The delegation simplifies the implementation, which
107 // uses the << operator and thus is easier done outside of the
108 // ::testing::internal namespace, which contains a << operator that
109 // sometimes conflicts with the one in STL.
110 void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,
112 PrintBytesInObjectToImpl(obj_bytes, count, os);
115 } // namespace internal2
119 // Depending on the value of a char (or wchar_t), we print it in one
121 // - as is if it's a printable ASCII (e.g. 'a', '2', ' '),
122 // - as a hexidecimal escape sequence (e.g. '\x7F'), or
123 // - as a special escape sequence (e.g. '\r', '\n').
130 // Returns true if c is a printable ASCII character. We test the
131 // value of c directly instead of calling isprint(), which is buggy on
133 inline bool IsPrintableAscii(wchar_t c) {
134 return 0x20 <= c && c <= 0x7E;
137 // Prints a wide or narrow char c as a character literal without the
138 // quotes, escaping it when necessary; returns how c was formatted.
139 // The template argument UnsignedChar is the unsigned version of Char,
140 // which is the type of c.
141 template <typename UnsignedChar, typename Char>
142 static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) {
143 switch (static_cast<wchar_t>(c)) {
175 if (IsPrintableAscii(c)) {
176 *os << static_cast<char>(c);
179 *os << "\\x" + String::FormatHexInt(static_cast<UnsignedChar>(c));
183 return kSpecialEscape;
186 // Prints a wchar_t c as if it's part of a string literal, escaping it when
187 // necessary; returns how c was formatted.
188 static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) {
195 return kSpecialEscape;
197 return PrintAsCharLiteralTo<wchar_t>(c, os);
201 // Prints a char c as if it's part of a string literal, escaping it when
202 // necessary; returns how c was formatted.
203 static CharFormat PrintAsStringLiteralTo(char c, ostream* os) {
204 return PrintAsStringLiteralTo(
205 static_cast<wchar_t>(static_cast<unsigned char>(c)), os);
208 // Prints a wide or narrow character c and its code. '\0' is printed
209 // as "'\\0'", other unprintable characters are also properly escaped
210 // using the standard C++ escape sequence. The template argument
211 // UnsignedChar is the unsigned version of Char, which is the type of c.
212 template <typename UnsignedChar, typename Char>
213 void PrintCharAndCodeTo(Char c, ostream* os) {
214 // First, print c as a literal in the most readable form we can find.
215 *os << ((sizeof(c) > 1) ? "L'" : "'");
216 const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os);
219 // To aid user debugging, we also print c's code in decimal, unless
220 // it's 0 (in which case c was printed as '\\0', making the code
224 *os << " (" << static_cast<int>(c);
226 // For more convenience, we print c's code again in hexidecimal,
227 // unless c was already printed in the form '\x##' or the code is in
229 if (format == kHexEscape || (1 <= c && c <= 9)) {
232 *os << ", 0x" << String::FormatHexInt(static_cast<UnsignedChar>(c));
237 void PrintTo(unsigned char c, ::std::ostream* os) {
238 PrintCharAndCodeTo<unsigned char>(c, os);
240 void PrintTo(signed char c, ::std::ostream* os) {
241 PrintCharAndCodeTo<unsigned char>(c, os);
244 // Prints a wchar_t as a symbol if it is printable or as its internal
245 // code otherwise and also as its code. L'\0' is printed as "L'\\0'".
246 void PrintTo(wchar_t wc, ostream* os) {
247 PrintCharAndCodeTo<wchar_t>(wc, os);
250 // Prints the given array of characters to the ostream. CharType must be either
252 // The array starts at begin, the length is len, it may include '\0' characters
253 // and may not be NUL-terminated.
254 template <typename CharType>
255 static void PrintCharsAsStringTo(
256 const CharType* begin, size_t len, ostream* os) {
257 const char* const kQuoteBegin = sizeof(CharType) == 1 ? "\"" : "L\"";
259 bool is_previous_hex = false;
260 for (size_t index = 0; index < len; ++index) {
261 const CharType cur = begin[index];
262 if (is_previous_hex && IsXDigit(cur)) {
263 // Previous character is of '\x..' form and this character can be
264 // interpreted as another hexadecimal digit in its number. Break string to
266 *os << "\" " << kQuoteBegin;
268 is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape;
273 // Prints a (const) char/wchar_t array of 'len' elements, starting at address
274 // 'begin'. CharType must be either char or wchar_t.
275 template <typename CharType>
276 static void UniversalPrintCharArray(
277 const CharType* begin, size_t len, ostream* os) {
279 // const char kFoo[] = "foo";
280 // generates an array of 4, not 3, elements, with the last one being '\0'.
282 // Therefore when printing a char array, we don't print the last element if
283 // it's '\0', such that the output matches the string literal as it's
284 // written in the source code.
285 if (len > 0 && begin[len - 1] == '\0') {
286 PrintCharsAsStringTo(begin, len - 1, os);
290 // If, however, the last element in the array is not '\0', e.g.
291 // const char kFoo[] = { 'f', 'o', 'o' };
292 // we must print the entire array. We also print a message to indicate
293 // that the array is not NUL-terminated.
294 PrintCharsAsStringTo(begin, len, os);
295 *os << " (no terminating NUL)";
298 // Prints a (const) char array of 'len' elements, starting at address 'begin'.
299 void UniversalPrintArray(const char* begin, size_t len, ostream* os) {
300 UniversalPrintCharArray(begin, len, os);
303 // Prints a (const) wchar_t array of 'len' elements, starting at address
305 void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) {
306 UniversalPrintCharArray(begin, len, os);
309 // Prints the given C string to the ostream.
310 void PrintTo(const char* s, ostream* os) {
314 *os << ImplicitCast_<const void*>(s) << " pointing to ";
315 PrintCharsAsStringTo(s, strlen(s), os);
319 // MSVC compiler can be configured to define whar_t as a typedef
320 // of unsigned short. Defining an overload for const wchar_t* in that case
321 // would cause pointers to unsigned shorts be printed as wide strings,
322 // possibly accessing more memory than intended and causing invalid
323 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
324 // wchar_t is implemented as a native type.
325 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
326 // Prints the given wide C string to the ostream.
327 void PrintTo(const wchar_t* s, ostream* os) {
331 *os << ImplicitCast_<const void*>(s) << " pointing to ";
332 PrintCharsAsStringTo(s, wcslen(s), os);
335 #endif // wchar_t is native
337 // Prints a ::string object.
338 #if GTEST_HAS_GLOBAL_STRING
339 void PrintStringTo(const ::string& s, ostream* os) {
340 PrintCharsAsStringTo(s.data(), s.size(), os);
342 #endif // GTEST_HAS_GLOBAL_STRING
344 void PrintStringTo(const ::std::string& s, ostream* os) {
345 PrintCharsAsStringTo(s.data(), s.size(), os);
348 // Prints a ::wstring object.
349 #if GTEST_HAS_GLOBAL_WSTRING
350 void PrintWideStringTo(const ::wstring& s, ostream* os) {
351 PrintCharsAsStringTo(s.data(), s.size(), os);
353 #endif // GTEST_HAS_GLOBAL_WSTRING
355 #if GTEST_HAS_STD_WSTRING
356 void PrintWideStringTo(const ::std::wstring& s, ostream* os) {
357 PrintCharsAsStringTo(s.data(), s.size(), os);
359 #endif // GTEST_HAS_STD_WSTRING
361 } // namespace internal
363 } // namespace testing