gtest-printers.cc
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Google Test - The Google C++ Testing Framework
00033 //
00034 // This file implements a universal value printer that can print a
00035 // value of any type T:
00036 //
00037 //   void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
00038 //
00039 // It uses the << operator when possible, and prints the bytes in the
00040 // object otherwise.  A user can override its behavior for a class
00041 // type Foo by defining either operator<<(::std::ostream&, const Foo&)
00042 // or void PrintTo(const Foo&, ::std::ostream*) in the namespace that
00043 // defines Foo.
00044 
00045 #include "gtest/gtest-printers.h"
00046 #include <ctype.h>
00047 #include <stdio.h>
00048 #include <cwchar>
00049 #include <ostream>  // NOLINT
00050 #include <string>
00051 #include "gtest/internal/gtest-port.h"
00052 
00053 namespace testing {
00054 
00055 namespace {
00056 
00057 using ::std::ostream;
00058 
00059 // Prints a segment of bytes in the given object.
00060 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
00061 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
00062 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
00063 void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,
00064                                 size_t count, ostream* os) {
00065   char text[5] = "";
00066   for (size_t i = 0; i != count; i++) {
00067     const size_t j = start + i;
00068     if (i != 0) {
00069       // Organizes the bytes into groups of 2 for easy parsing by
00070       // human.
00071       if ((j % 2) == 0)
00072         *os << ' ';
00073       else
00074         *os << '-';
00075     }
00076     GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]);
00077     *os << text;
00078   }
00079 }
00080 
00081 // Prints the bytes in the given value to the given ostream.
00082 void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,
00083                               ostream* os) {
00084   // Tells the user how big the object is.
00085   *os << count << "-byte object <";
00086 
00087   const size_t kThreshold = 132;
00088   const size_t kChunkSize = 64;
00089   // If the object size is bigger than kThreshold, we'll have to omit
00090   // some details by printing only the first and the last kChunkSize
00091   // bytes.
00092   // TODO(wan): let the user control the threshold using a flag.
00093   if (count < kThreshold) {
00094     PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
00095   } else {
00096     PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
00097     *os << " ... ";
00098     // Rounds up to 2-byte boundary.
00099     const size_t resume_pos = (count - kChunkSize + 1)/2*2;
00100     PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
00101   }
00102   *os << ">";
00103 }
00104 
00105 }  // namespace
00106 
00107 namespace internal2 {
00108 
00109 // Delegates to PrintBytesInObjectToImpl() to print the bytes in the
00110 // given object.  The delegation simplifies the implementation, which
00111 // uses the << operator and thus is easier done outside of the
00112 // ::testing::internal namespace, which contains a << operator that
00113 // sometimes conflicts with the one in STL.
00114 void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,
00115                           ostream* os) {
00116   PrintBytesInObjectToImpl(obj_bytes, count, os);
00117 }
00118 
00119 }  // namespace internal2
00120 
00121 namespace internal {
00122 
00123 // Depending on the value of a char (or wchar_t), we print it in one
00124 // of three formats:
00125 //   - as is if it's a printable ASCII (e.g. 'a', '2', ' '),
00126 //   - as a hexidecimal escape sequence (e.g. '\x7F'), or
00127 //   - as a special escape sequence (e.g. '\r', '\n').
00128 enum CharFormat {
00129   kAsIs,
00130   kHexEscape,
00131   kSpecialEscape
00132 };
00133 
00134 // Returns true if c is a printable ASCII character.  We test the
00135 // value of c directly instead of calling isprint(), which is buggy on
00136 // Windows Mobile.
00137 inline bool IsPrintableAscii(wchar_t c) {
00138   return 0x20 <= c && c <= 0x7E;
00139 }
00140 
00141 // Prints a wide or narrow char c as a character literal without the
00142 // quotes, escaping it when necessary; returns how c was formatted.
00143 // The template argument UnsignedChar is the unsigned version of Char,
00144 // which is the type of c.
00145 template <typename UnsignedChar, typename Char>
00146 static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) {
00147   switch (static_cast<wchar_t>(c)) {
00148     case L'\0':
00149       *os << "\\0";
00150       break;
00151     case L'\'':
00152       *os << "\\'";
00153       break;
00154     case L'\\':
00155       *os << "\\\\";
00156       break;
00157     case L'\a':
00158       *os << "\\a";
00159       break;
00160     case L'\b':
00161       *os << "\\b";
00162       break;
00163     case L'\f':
00164       *os << "\\f";
00165       break;
00166     case L'\n':
00167       *os << "\\n";
00168       break;
00169     case L'\r':
00170       *os << "\\r";
00171       break;
00172     case L'\t':
00173       *os << "\\t";
00174       break;
00175     case L'\v':
00176       *os << "\\v";
00177       break;
00178     default:
00179       if (IsPrintableAscii(c)) {
00180         *os << static_cast<char>(c);
00181         return kAsIs;
00182       } else {
00183         *os << "\\x" + String::FormatHexInt(static_cast<UnsignedChar>(c));
00184         return kHexEscape;
00185       }
00186   }
00187   return kSpecialEscape;
00188 }
00189 
00190 // Prints a wchar_t c as if it's part of a string literal, escaping it when
00191 // necessary; returns how c was formatted.
00192 static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) {
00193   switch (c) {
00194     case L'\'':
00195       *os << "'";
00196       return kAsIs;
00197     case L'"':
00198       *os << "\\\"";
00199       return kSpecialEscape;
00200     default:
00201       return PrintAsCharLiteralTo<wchar_t>(c, os);
00202   }
00203 }
00204 
00205 // Prints a char c as if it's part of a string literal, escaping it when
00206 // necessary; returns how c was formatted.
00207 static CharFormat PrintAsStringLiteralTo(char c, ostream* os) {
00208   return PrintAsStringLiteralTo(
00209       static_cast<wchar_t>(static_cast<unsigned char>(c)), os);
00210 }
00211 
00212 // Prints a wide or narrow character c and its code.  '\0' is printed
00213 // as "'\\0'", other unprintable characters are also properly escaped
00214 // using the standard C++ escape sequence.  The template argument
00215 // UnsignedChar is the unsigned version of Char, which is the type of c.
00216 template <typename UnsignedChar, typename Char>
00217 void PrintCharAndCodeTo(Char c, ostream* os) {
00218   // First, print c as a literal in the most readable form we can find.
00219   *os << ((sizeof(c) > 1) ? "L'" : "'");
00220   const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os);
00221   *os << "'";
00222 
00223   // To aid user debugging, we also print c's code in decimal, unless
00224   // it's 0 (in which case c was printed as '\\0', making the code
00225   // obvious).
00226   if (c == 0)
00227     return;
00228   *os << " (" << static_cast<int>(c);
00229 
00230   // For more convenience, we print c's code again in hexidecimal,
00231   // unless c was already printed in the form '\x##' or the code is in
00232   // [1, 9].
00233   if (format == kHexEscape || (1 <= c && c <= 9)) {
00234     // Do nothing.
00235   } else {
00236     *os << ", 0x" << String::FormatHexInt(static_cast<UnsignedChar>(c));
00237   }
00238   *os << ")";
00239 }
00240 
00241 void PrintTo(unsigned char c, ::std::ostream* os) {
00242   PrintCharAndCodeTo<unsigned char>(c, os);
00243 }
00244 void PrintTo(signed char c, ::std::ostream* os) {
00245   PrintCharAndCodeTo<unsigned char>(c, os);
00246 }
00247 
00248 // Prints a wchar_t as a symbol if it is printable or as its internal
00249 // code otherwise and also as its code.  L'\0' is printed as "L'\\0'".
00250 void PrintTo(wchar_t wc, ostream* os) {
00251   PrintCharAndCodeTo<wchar_t>(wc, os);
00252 }
00253 
00254 // Prints the given array of characters to the ostream.  CharType must be either
00255 // char or wchar_t.
00256 // The array starts at begin, the length is len, it may include '\0' characters
00257 // and may not be NUL-terminated.
00258 template <typename CharType>
00259 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
00260 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
00261 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
00262 static void PrintCharsAsStringTo(
00263     const CharType* begin, size_t len, ostream* os) {
00264   const char* const kQuoteBegin = sizeof(CharType) == 1 ? "\"" : "L\"";
00265   *os << kQuoteBegin;
00266   bool is_previous_hex = false;
00267   for (size_t index = 0; index < len; ++index) {
00268     const CharType cur = begin[index];
00269     if (is_previous_hex && IsXDigit(cur)) {
00270       // Previous character is of '\x..' form and this character can be
00271       // interpreted as another hexadecimal digit in its number. Break string to
00272       // disambiguate.
00273       *os << "\" " << kQuoteBegin;
00274     }
00275     is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape;
00276   }
00277   *os << "\"";
00278 }
00279 
00280 // Prints a (const) char/wchar_t array of 'len' elements, starting at address
00281 // 'begin'.  CharType must be either char or wchar_t.
00282 template <typename CharType>
00283 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
00284 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
00285 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
00286 static void UniversalPrintCharArray(
00287     const CharType* begin, size_t len, ostream* os) {
00288   // The code
00289   //   const char kFoo[] = "foo";
00290   // generates an array of 4, not 3, elements, with the last one being '\0'.
00291   //
00292   // Therefore when printing a char array, we don't print the last element if
00293   // it's '\0', such that the output matches the string literal as it's
00294   // written in the source code.
00295   if (len > 0 && begin[len - 1] == '\0') {
00296     PrintCharsAsStringTo(begin, len - 1, os);
00297     return;
00298   }
00299 
00300   // If, however, the last element in the array is not '\0', e.g.
00301   //    const char kFoo[] = { 'f', 'o', 'o' };
00302   // we must print the entire array.  We also print a message to indicate
00303   // that the array is not NUL-terminated.
00304   PrintCharsAsStringTo(begin, len, os);
00305   *os << " (no terminating NUL)";
00306 }
00307 
00308 // Prints a (const) char array of 'len' elements, starting at address 'begin'.
00309 void UniversalPrintArray(const char* begin, size_t len, ostream* os) {
00310   UniversalPrintCharArray(begin, len, os);
00311 }
00312 
00313 // Prints a (const) wchar_t array of 'len' elements, starting at address
00314 // 'begin'.
00315 void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) {
00316   UniversalPrintCharArray(begin, len, os);
00317 }
00318 
00319 // Prints the given C string to the ostream.
00320 void PrintTo(const char* s, ostream* os) {
00321   if (s == NULL) {
00322     *os << "NULL";
00323   } else {
00324     *os << ImplicitCast_<const void*>(s) << " pointing to ";
00325     PrintCharsAsStringTo(s, strlen(s), os);
00326   }
00327 }
00328 
00329 // MSVC compiler can be configured to define whar_t as a typedef
00330 // of unsigned short. Defining an overload for const wchar_t* in that case
00331 // would cause pointers to unsigned shorts be printed as wide strings,
00332 // possibly accessing more memory than intended and causing invalid
00333 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
00334 // wchar_t is implemented as a native type.
00335 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
00336 // Prints the given wide C string to the ostream.
00337 void PrintTo(const wchar_t* s, ostream* os) {
00338   if (s == NULL) {
00339     *os << "NULL";
00340   } else {
00341     *os << ImplicitCast_<const void*>(s) << " pointing to ";
00342     PrintCharsAsStringTo(s, std::wcslen(s), os);
00343   }
00344 }
00345 #endif  // wchar_t is native
00346 
00347 // Prints a ::string object.
00348 #if GTEST_HAS_GLOBAL_STRING
00349 void PrintStringTo(const ::string& s, ostream* os) {
00350   PrintCharsAsStringTo(s.data(), s.size(), os);
00351 }
00352 #endif  // GTEST_HAS_GLOBAL_STRING
00353 
00354 void PrintStringTo(const ::std::string& s, ostream* os) {
00355   PrintCharsAsStringTo(s.data(), s.size(), os);
00356 }
00357 
00358 // Prints a ::wstring object.
00359 #if GTEST_HAS_GLOBAL_WSTRING
00360 void PrintWideStringTo(const ::wstring& s, ostream* os) {
00361   PrintCharsAsStringTo(s.data(), s.size(), os);
00362 }
00363 #endif  // GTEST_HAS_GLOBAL_WSTRING
00364 
00365 #if GTEST_HAS_STD_WSTRING
00366 void PrintWideStringTo(const ::std::wstring& s, ostream* os) {
00367   PrintCharsAsStringTo(s.data(), s.size(), os);
00368 }
00369 #endif  // GTEST_HAS_STD_WSTRING
00370 
00371 }  // namespace internal
00372 
00373 }  // namespace testing


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:03