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


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:47