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 <ostream>  // NOLINT
00049 #include <string>
00050 #include "gtest/internal/gtest-port.h"
00051 
00052 namespace testing {
00053 
00054 namespace {
00055 
00056 using ::std::ostream;
00057 
00058 #if GTEST_OS_WINDOWS_MOBILE  // Windows CE does not define _snprintf_s.
00059 # define snprintf _snprintf
00060 #elif _MSC_VER >= 1400  // VC 8.0 and later deprecate snprintf and _snprintf.
00061 # define snprintf _snprintf_s
00062 #elif _MSC_VER
00063 # define snprintf _snprintf
00064 #endif  // GTEST_OS_WINDOWS_MOBILE
00065 
00066 // Prints a segment of bytes in the given object.
00067 void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,
00068                                 size_t count, ostream* os) {
00069   char text[5] = "";
00070   for (size_t i = 0; i != count; i++) {
00071     const size_t j = start + i;
00072     if (i != 0) {
00073       // Organizes the bytes into groups of 2 for easy parsing by
00074       // human.
00075       if ((j % 2) == 0)
00076         *os << ' ';
00077       else
00078         *os << '-';
00079     }
00080     snprintf(text, sizeof(text), "%02X", obj_bytes[j]);
00081     *os << text;
00082   }
00083 }
00084 
00085 // Prints the bytes in the given value to the given ostream.
00086 void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,
00087                               ostream* os) {
00088   // Tells the user how big the object is.
00089   *os << count << "-byte object <";
00090 
00091   const size_t kThreshold = 132;
00092   const size_t kChunkSize = 64;
00093   // If the object size is bigger than kThreshold, we'll have to omit
00094   // some details by printing only the first and the last kChunkSize
00095   // bytes.
00096   // TODO(wan): let the user control the threshold using a flag.
00097   if (count < kThreshold) {
00098     PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
00099   } else {
00100     PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
00101     *os << " ... ";
00102     // Rounds up to 2-byte boundary.
00103     const size_t resume_pos = (count - kChunkSize + 1)/2*2;
00104     PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
00105   }
00106   *os << ">";
00107 }
00108 
00109 }  // namespace
00110 
00111 namespace internal2 {
00112 
00113 // Delegates to PrintBytesInObjectToImpl() to print the bytes in the
00114 // given object.  The delegation simplifies the implementation, which
00115 // uses the << operator and thus is easier done outside of the
00116 // ::testing::internal namespace, which contains a << operator that
00117 // sometimes conflicts with the one in STL.
00118 void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,
00119                           ostream* os) {
00120   PrintBytesInObjectToImpl(obj_bytes, count, os);
00121 }
00122 
00123 }  // namespace internal2
00124 
00125 namespace internal {
00126 
00127 // Depending on the value of a char (or wchar_t), we print it in one
00128 // of three formats:
00129 //   - as is if it's a printable ASCII (e.g. 'a', '2', ' '),
00130 //   - as a hexidecimal escape sequence (e.g. '\x7F'), or
00131 //   - as a special escape sequence (e.g. '\r', '\n').
00132 enum CharFormat {
00133   kAsIs,
00134   kHexEscape,
00135   kSpecialEscape
00136 };
00137 
00138 // Returns true if c is a printable ASCII character.  We test the
00139 // value of c directly instead of calling isprint(), which is buggy on
00140 // Windows Mobile.
00141 inline bool IsPrintableAscii(wchar_t c) {
00142   return 0x20 <= c && c <= 0x7E;
00143 }
00144 
00145 // Prints a wide or narrow char c as a character literal without the
00146 // quotes, escaping it when necessary; returns how c was formatted.
00147 // The template argument UnsignedChar is the unsigned version of Char,
00148 // which is the type of c.
00149 template <typename UnsignedChar, typename Char>
00150 static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) {
00151   switch (static_cast<wchar_t>(c)) {
00152     case L'\0':
00153       *os << "\\0";
00154       break;
00155     case L'\'':
00156       *os << "\\'";
00157       break;
00158     case L'\\':
00159       *os << "\\\\";
00160       break;
00161     case L'\a':
00162       *os << "\\a";
00163       break;
00164     case L'\b':
00165       *os << "\\b";
00166       break;
00167     case L'\f':
00168       *os << "\\f";
00169       break;
00170     case L'\n':
00171       *os << "\\n";
00172       break;
00173     case L'\r':
00174       *os << "\\r";
00175       break;
00176     case L'\t':
00177       *os << "\\t";
00178       break;
00179     case L'\v':
00180       *os << "\\v";
00181       break;
00182     default:
00183       if (IsPrintableAscii(c)) {
00184         *os << static_cast<char>(c);
00185         return kAsIs;
00186       } else {
00187         *os << String::Format("\\x%X", static_cast<UnsignedChar>(c));
00188         return kHexEscape;
00189       }
00190   }
00191   return kSpecialEscape;
00192 }
00193 
00194 // Prints a char c as if it's part of a string literal, escaping it when
00195 // necessary; returns how c was formatted.
00196 static CharFormat PrintAsWideStringLiteralTo(wchar_t c, ostream* os) {
00197   switch (c) {
00198     case L'\'':
00199       *os << "'";
00200       return kAsIs;
00201     case L'"':
00202       *os << "\\\"";
00203       return kSpecialEscape;
00204     default:
00205       return PrintAsCharLiteralTo<wchar_t>(c, os);
00206   }
00207 }
00208 
00209 // Prints a char c as if it's part of a string literal, escaping it when
00210 // necessary; returns how c was formatted.
00211 static CharFormat PrintAsNarrowStringLiteralTo(char c, ostream* os) {
00212   return PrintAsWideStringLiteralTo(static_cast<unsigned char>(c), os);
00213 }
00214 
00215 // Prints a wide or narrow character c and its code.  '\0' is printed
00216 // as "'\\0'", other unprintable characters are also properly escaped
00217 // using the standard C++ escape sequence.  The template argument
00218 // UnsignedChar is the unsigned version of Char, which is the type of c.
00219 template <typename UnsignedChar, typename Char>
00220 void PrintCharAndCodeTo(Char c, ostream* os) {
00221   // First, print c as a literal in the most readable form we can find.
00222   *os << ((sizeof(c) > 1) ? "L'" : "'");
00223   const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os);
00224   *os << "'";
00225 
00226   // To aid user debugging, we also print c's code in decimal, unless
00227   // it's 0 (in which case c was printed as '\\0', making the code
00228   // obvious).
00229   if (c == 0)
00230     return;
00231   *os << " (" << String::Format("%d", c).c_str();
00232 
00233   // For more convenience, we print c's code again in hexidecimal,
00234   // unless c was already printed in the form '\x##' or the code is in
00235   // [1, 9].
00236   if (format == kHexEscape || (1 <= c && c <= 9)) {
00237     // Do nothing.
00238   } else {
00239     *os << String::Format(", 0x%X",
00240                           static_cast<UnsignedChar>(c)).c_str();
00241   }
00242   *os << ")";
00243 }
00244 
00245 void PrintTo(unsigned char c, ::std::ostream* os) {
00246   PrintCharAndCodeTo<unsigned char>(c, os);
00247 }
00248 void PrintTo(signed char c, ::std::ostream* os) {
00249   PrintCharAndCodeTo<unsigned char>(c, os);
00250 }
00251 
00252 // Prints a wchar_t as a symbol if it is printable or as its internal
00253 // code otherwise and also as its code.  L'\0' is printed as "L'\\0'".
00254 void PrintTo(wchar_t wc, ostream* os) {
00255   PrintCharAndCodeTo<wchar_t>(wc, os);
00256 }
00257 
00258 // Prints the given array of characters to the ostream.
00259 // The array starts at *begin, the length is len, it may include '\0' characters
00260 // and may not be null-terminated.
00261 static void PrintCharsAsStringTo(const char* begin, size_t len, ostream* os) {
00262   *os << "\"";
00263   bool is_previous_hex = false;
00264   for (size_t index = 0; index < len; ++index) {
00265     const char cur = begin[index];
00266     if (is_previous_hex && IsXDigit(cur)) {
00267       // Previous character is of '\x..' form and this character can be
00268       // interpreted as another hexadecimal digit in its number. Break string to
00269       // disambiguate.
00270       *os << "\" \"";
00271     }
00272     is_previous_hex = PrintAsNarrowStringLiteralTo(cur, os) == kHexEscape;
00273   }
00274   *os << "\"";
00275 }
00276 
00277 // Prints a (const) char array of 'len' elements, starting at address 'begin'.
00278 void UniversalPrintArray(const char* begin, size_t len, ostream* os) {
00279   PrintCharsAsStringTo(begin, len, os);
00280 }
00281 
00282 // Prints the given array of wide characters to the ostream.
00283 // The array starts at *begin, the length is len, it may include L'\0'
00284 // characters and may not be null-terminated.
00285 static void PrintWideCharsAsStringTo(const wchar_t* begin, size_t len,
00286                                      ostream* os) {
00287   *os << "L\"";
00288   bool is_previous_hex = false;
00289   for (size_t index = 0; index < len; ++index) {
00290     const wchar_t cur = begin[index];
00291     if (is_previous_hex && isascii(cur) && IsXDigit(static_cast<char>(cur))) {
00292       // Previous character is of '\x..' form and this character can be
00293       // interpreted as another hexadecimal digit in its number. Break string to
00294       // disambiguate.
00295       *os << "\" L\"";
00296     }
00297     is_previous_hex = PrintAsWideStringLiteralTo(cur, os) == kHexEscape;
00298   }
00299   *os << "\"";
00300 }
00301 
00302 // Prints the given C string to the ostream.
00303 void PrintTo(const char* s, ostream* os) {
00304   if (s == NULL) {
00305     *os << "NULL";
00306   } else {
00307     *os << ImplicitCast_<const void*>(s) << " pointing to ";
00308     PrintCharsAsStringTo(s, strlen(s), os);
00309   }
00310 }
00311 
00312 // MSVC compiler can be configured to define whar_t as a typedef
00313 // of unsigned short. Defining an overload for const wchar_t* in that case
00314 // would cause pointers to unsigned shorts be printed as wide strings,
00315 // possibly accessing more memory than intended and causing invalid
00316 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
00317 // wchar_t is implemented as a native type.
00318 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
00319 // Prints the given wide C string to the ostream.
00320 void PrintTo(const wchar_t* s, ostream* os) {
00321   if (s == NULL) {
00322     *os << "NULL";
00323   } else {
00324     *os << ImplicitCast_<const void*>(s) << " pointing to ";
00325     PrintWideCharsAsStringTo(s, wcslen(s), os);
00326   }
00327 }
00328 #endif  // wchar_t is native
00329 
00330 // Prints a ::string object.
00331 #if GTEST_HAS_GLOBAL_STRING
00332 void PrintStringTo(const ::string& s, ostream* os) {
00333   PrintCharsAsStringTo(s.data(), s.size(), os);
00334 }
00335 #endif  // GTEST_HAS_GLOBAL_STRING
00336 
00337 void PrintStringTo(const ::std::string& s, ostream* os) {
00338   PrintCharsAsStringTo(s.data(), s.size(), os);
00339 }
00340 
00341 // Prints a ::wstring object.
00342 #if GTEST_HAS_GLOBAL_WSTRING
00343 void PrintWideStringTo(const ::wstring& s, ostream* os) {
00344   PrintWideCharsAsStringTo(s.data(), s.size(), os);
00345 }
00346 #endif  // GTEST_HAS_GLOBAL_WSTRING
00347 
00348 #if GTEST_HAS_STD_WSTRING
00349 void PrintWideStringTo(const ::std::wstring& s, ostream* os) {
00350   PrintWideCharsAsStringTo(s.data(), s.size(), os);
00351 }
00352 #endif  // GTEST_HAS_STD_WSTRING
00353 
00354 }  // namespace internal
00355 
00356 }  // namespace testing


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:24:39