Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | // Copyright 2007, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | // |
michael@0 | 30 | // Author: wan@google.com (Zhanyong Wan) |
michael@0 | 31 | |
michael@0 | 32 | // Google Test - The Google C++ Testing Framework |
michael@0 | 33 | // |
michael@0 | 34 | // This file implements a universal value printer that can print a |
michael@0 | 35 | // value of any type T: |
michael@0 | 36 | // |
michael@0 | 37 | // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr); |
michael@0 | 38 | // |
michael@0 | 39 | // It uses the << operator when possible, and prints the bytes in the |
michael@0 | 40 | // object otherwise. A user can override its behavior for a class |
michael@0 | 41 | // type Foo by defining either operator<<(::std::ostream&, const Foo&) |
michael@0 | 42 | // or void PrintTo(const Foo&, ::std::ostream*) in the namespace that |
michael@0 | 43 | // defines Foo. |
michael@0 | 44 | |
michael@0 | 45 | #include "gtest/gtest-printers.h" |
michael@0 | 46 | #include <ctype.h> |
michael@0 | 47 | #include <stdio.h> |
michael@0 | 48 | #include <ostream> // NOLINT |
michael@0 | 49 | #include <string> |
michael@0 | 50 | #include "gtest/internal/gtest-port.h" |
michael@0 | 51 | |
michael@0 | 52 | namespace testing { |
michael@0 | 53 | |
michael@0 | 54 | namespace { |
michael@0 | 55 | |
michael@0 | 56 | using ::std::ostream; |
michael@0 | 57 | |
michael@0 | 58 | // Prints a segment of bytes in the given object. |
michael@0 | 59 | void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start, |
michael@0 | 60 | size_t count, ostream* os) { |
michael@0 | 61 | char text[5] = ""; |
michael@0 | 62 | for (size_t i = 0; i != count; i++) { |
michael@0 | 63 | const size_t j = start + i; |
michael@0 | 64 | if (i != 0) { |
michael@0 | 65 | // Organizes the bytes into groups of 2 for easy parsing by |
michael@0 | 66 | // human. |
michael@0 | 67 | if ((j % 2) == 0) |
michael@0 | 68 | *os << ' '; |
michael@0 | 69 | else |
michael@0 | 70 | *os << '-'; |
michael@0 | 71 | } |
michael@0 | 72 | GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]); |
michael@0 | 73 | *os << text; |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | // Prints the bytes in the given value to the given ostream. |
michael@0 | 78 | void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count, |
michael@0 | 79 | ostream* os) { |
michael@0 | 80 | // Tells the user how big the object is. |
michael@0 | 81 | *os << count << "-byte object <"; |
michael@0 | 82 | |
michael@0 | 83 | const size_t kThreshold = 132; |
michael@0 | 84 | const size_t kChunkSize = 64; |
michael@0 | 85 | // If the object size is bigger than kThreshold, we'll have to omit |
michael@0 | 86 | // some details by printing only the first and the last kChunkSize |
michael@0 | 87 | // bytes. |
michael@0 | 88 | // TODO(wan): let the user control the threshold using a flag. |
michael@0 | 89 | if (count < kThreshold) { |
michael@0 | 90 | PrintByteSegmentInObjectTo(obj_bytes, 0, count, os); |
michael@0 | 91 | } else { |
michael@0 | 92 | PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os); |
michael@0 | 93 | *os << " ... "; |
michael@0 | 94 | // Rounds up to 2-byte boundary. |
michael@0 | 95 | const size_t resume_pos = (count - kChunkSize + 1)/2*2; |
michael@0 | 96 | PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os); |
michael@0 | 97 | } |
michael@0 | 98 | *os << ">"; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | } // namespace |
michael@0 | 102 | |
michael@0 | 103 | namespace internal2 { |
michael@0 | 104 | |
michael@0 | 105 | // Delegates to PrintBytesInObjectToImpl() to print the bytes in the |
michael@0 | 106 | // given object. The delegation simplifies the implementation, which |
michael@0 | 107 | // uses the << operator and thus is easier done outside of the |
michael@0 | 108 | // ::testing::internal namespace, which contains a << operator that |
michael@0 | 109 | // sometimes conflicts with the one in STL. |
michael@0 | 110 | void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count, |
michael@0 | 111 | ostream* os) { |
michael@0 | 112 | PrintBytesInObjectToImpl(obj_bytes, count, os); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | } // namespace internal2 |
michael@0 | 116 | |
michael@0 | 117 | namespace internal { |
michael@0 | 118 | |
michael@0 | 119 | // Depending on the value of a char (or wchar_t), we print it in one |
michael@0 | 120 | // of three formats: |
michael@0 | 121 | // - as is if it's a printable ASCII (e.g. 'a', '2', ' '), |
michael@0 | 122 | // - as a hexidecimal escape sequence (e.g. '\x7F'), or |
michael@0 | 123 | // - as a special escape sequence (e.g. '\r', '\n'). |
michael@0 | 124 | enum CharFormat { |
michael@0 | 125 | kAsIs, |
michael@0 | 126 | kHexEscape, |
michael@0 | 127 | kSpecialEscape |
michael@0 | 128 | }; |
michael@0 | 129 | |
michael@0 | 130 | // Returns true if c is a printable ASCII character. We test the |
michael@0 | 131 | // value of c directly instead of calling isprint(), which is buggy on |
michael@0 | 132 | // Windows Mobile. |
michael@0 | 133 | inline bool IsPrintableAscii(wchar_t c) { |
michael@0 | 134 | return 0x20 <= c && c <= 0x7E; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | // Prints a wide or narrow char c as a character literal without the |
michael@0 | 138 | // quotes, escaping it when necessary; returns how c was formatted. |
michael@0 | 139 | // The template argument UnsignedChar is the unsigned version of Char, |
michael@0 | 140 | // which is the type of c. |
michael@0 | 141 | template <typename UnsignedChar, typename Char> |
michael@0 | 142 | static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) { |
michael@0 | 143 | switch (static_cast<wchar_t>(c)) { |
michael@0 | 144 | case L'\0': |
michael@0 | 145 | *os << "\\0"; |
michael@0 | 146 | break; |
michael@0 | 147 | case L'\'': |
michael@0 | 148 | *os << "\\'"; |
michael@0 | 149 | break; |
michael@0 | 150 | case L'\\': |
michael@0 | 151 | *os << "\\\\"; |
michael@0 | 152 | break; |
michael@0 | 153 | case L'\a': |
michael@0 | 154 | *os << "\\a"; |
michael@0 | 155 | break; |
michael@0 | 156 | case L'\b': |
michael@0 | 157 | *os << "\\b"; |
michael@0 | 158 | break; |
michael@0 | 159 | case L'\f': |
michael@0 | 160 | *os << "\\f"; |
michael@0 | 161 | break; |
michael@0 | 162 | case L'\n': |
michael@0 | 163 | *os << "\\n"; |
michael@0 | 164 | break; |
michael@0 | 165 | case L'\r': |
michael@0 | 166 | *os << "\\r"; |
michael@0 | 167 | break; |
michael@0 | 168 | case L'\t': |
michael@0 | 169 | *os << "\\t"; |
michael@0 | 170 | break; |
michael@0 | 171 | case L'\v': |
michael@0 | 172 | *os << "\\v"; |
michael@0 | 173 | break; |
michael@0 | 174 | default: |
michael@0 | 175 | if (IsPrintableAscii(c)) { |
michael@0 | 176 | *os << static_cast<char>(c); |
michael@0 | 177 | return kAsIs; |
michael@0 | 178 | } else { |
michael@0 | 179 | *os << String::Format("\\x%X", static_cast<UnsignedChar>(c)); |
michael@0 | 180 | return kHexEscape; |
michael@0 | 181 | } |
michael@0 | 182 | } |
michael@0 | 183 | return kSpecialEscape; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | // Prints a wchar_t c as if it's part of a string literal, escaping it when |
michael@0 | 187 | // necessary; returns how c was formatted. |
michael@0 | 188 | static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) { |
michael@0 | 189 | switch (c) { |
michael@0 | 190 | case L'\'': |
michael@0 | 191 | *os << "'"; |
michael@0 | 192 | return kAsIs; |
michael@0 | 193 | case L'"': |
michael@0 | 194 | *os << "\\\""; |
michael@0 | 195 | return kSpecialEscape; |
michael@0 | 196 | default: |
michael@0 | 197 | return PrintAsCharLiteralTo<wchar_t>(c, os); |
michael@0 | 198 | } |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | // Prints a char c as if it's part of a string literal, escaping it when |
michael@0 | 202 | // necessary; returns how c was formatted. |
michael@0 | 203 | static CharFormat PrintAsStringLiteralTo(char c, ostream* os) { |
michael@0 | 204 | return PrintAsStringLiteralTo( |
michael@0 | 205 | static_cast<wchar_t>(static_cast<unsigned char>(c)), os); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | // Prints a wide or narrow character c and its code. '\0' is printed |
michael@0 | 209 | // as "'\\0'", other unprintable characters are also properly escaped |
michael@0 | 210 | // using the standard C++ escape sequence. The template argument |
michael@0 | 211 | // UnsignedChar is the unsigned version of Char, which is the type of c. |
michael@0 | 212 | template <typename UnsignedChar, typename Char> |
michael@0 | 213 | void PrintCharAndCodeTo(Char c, ostream* os) { |
michael@0 | 214 | // First, print c as a literal in the most readable form we can find. |
michael@0 | 215 | *os << ((sizeof(c) > 1) ? "L'" : "'"); |
michael@0 | 216 | const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os); |
michael@0 | 217 | *os << "'"; |
michael@0 | 218 | |
michael@0 | 219 | // To aid user debugging, we also print c's code in decimal, unless |
michael@0 | 220 | // it's 0 (in which case c was printed as '\\0', making the code |
michael@0 | 221 | // obvious). |
michael@0 | 222 | if (c == 0) |
michael@0 | 223 | return; |
michael@0 | 224 | *os << " (" << String::Format("%d", c).c_str(); |
michael@0 | 225 | |
michael@0 | 226 | // For more convenience, we print c's code again in hexidecimal, |
michael@0 | 227 | // unless c was already printed in the form '\x##' or the code is in |
michael@0 | 228 | // [1, 9]. |
michael@0 | 229 | if (format == kHexEscape || (1 <= c && c <= 9)) { |
michael@0 | 230 | // Do nothing. |
michael@0 | 231 | } else { |
michael@0 | 232 | *os << String::Format(", 0x%X", |
michael@0 | 233 | static_cast<UnsignedChar>(c)).c_str(); |
michael@0 | 234 | } |
michael@0 | 235 | *os << ")"; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | void PrintTo(unsigned char c, ::std::ostream* os) { |
michael@0 | 239 | PrintCharAndCodeTo<unsigned char>(c, os); |
michael@0 | 240 | } |
michael@0 | 241 | void PrintTo(signed char c, ::std::ostream* os) { |
michael@0 | 242 | PrintCharAndCodeTo<unsigned char>(c, os); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | // Prints a wchar_t as a symbol if it is printable or as its internal |
michael@0 | 246 | // code otherwise and also as its code. L'\0' is printed as "L'\\0'". |
michael@0 | 247 | void PrintTo(wchar_t wc, ostream* os) { |
michael@0 | 248 | PrintCharAndCodeTo<wchar_t>(wc, os); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | // Prints the given array of characters to the ostream. CharType must be either |
michael@0 | 252 | // char or wchar_t. |
michael@0 | 253 | // The array starts at begin, the length is len, it may include '\0' characters |
michael@0 | 254 | // and may not be NUL-terminated. |
michael@0 | 255 | template <typename CharType> |
michael@0 | 256 | static void PrintCharsAsStringTo( |
michael@0 | 257 | const CharType* begin, size_t len, ostream* os) { |
michael@0 | 258 | const char* const kQuoteBegin = sizeof(CharType) == 1 ? "\"" : "L\""; |
michael@0 | 259 | *os << kQuoteBegin; |
michael@0 | 260 | bool is_previous_hex = false; |
michael@0 | 261 | for (size_t index = 0; index < len; ++index) { |
michael@0 | 262 | const CharType cur = begin[index]; |
michael@0 | 263 | if (is_previous_hex && IsXDigit(cur)) { |
michael@0 | 264 | // Previous character is of '\x..' form and this character can be |
michael@0 | 265 | // interpreted as another hexadecimal digit in its number. Break string to |
michael@0 | 266 | // disambiguate. |
michael@0 | 267 | *os << "\" " << kQuoteBegin; |
michael@0 | 268 | } |
michael@0 | 269 | is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape; |
michael@0 | 270 | } |
michael@0 | 271 | *os << "\""; |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | // Prints a (const) char/wchar_t array of 'len' elements, starting at address |
michael@0 | 275 | // 'begin'. CharType must be either char or wchar_t. |
michael@0 | 276 | template <typename CharType> |
michael@0 | 277 | static void UniversalPrintCharArray( |
michael@0 | 278 | const CharType* begin, size_t len, ostream* os) { |
michael@0 | 279 | // The code |
michael@0 | 280 | // const char kFoo[] = "foo"; |
michael@0 | 281 | // generates an array of 4, not 3, elements, with the last one being '\0'. |
michael@0 | 282 | // |
michael@0 | 283 | // Therefore when printing a char array, we don't print the last element if |
michael@0 | 284 | // it's '\0', such that the output matches the string literal as it's |
michael@0 | 285 | // written in the source code. |
michael@0 | 286 | if (len > 0 && begin[len - 1] == '\0') { |
michael@0 | 287 | PrintCharsAsStringTo(begin, len - 1, os); |
michael@0 | 288 | return; |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | // If, however, the last element in the array is not '\0', e.g. |
michael@0 | 292 | // const char kFoo[] = { 'f', 'o', 'o' }; |
michael@0 | 293 | // we must print the entire array. We also print a message to indicate |
michael@0 | 294 | // that the array is not NUL-terminated. |
michael@0 | 295 | PrintCharsAsStringTo(begin, len, os); |
michael@0 | 296 | *os << " (no terminating NUL)"; |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | // Prints a (const) char array of 'len' elements, starting at address 'begin'. |
michael@0 | 300 | void UniversalPrintArray(const char* begin, size_t len, ostream* os) { |
michael@0 | 301 | UniversalPrintCharArray(begin, len, os); |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | // Prints a (const) wchar_t array of 'len' elements, starting at address |
michael@0 | 305 | // 'begin'. |
michael@0 | 306 | void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) { |
michael@0 | 307 | UniversalPrintCharArray(begin, len, os); |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | // Prints the given C string to the ostream. |
michael@0 | 311 | void PrintTo(const char* s, ostream* os) { |
michael@0 | 312 | if (s == NULL) { |
michael@0 | 313 | *os << "NULL"; |
michael@0 | 314 | } else { |
michael@0 | 315 | *os << ImplicitCast_<const void*>(s) << " pointing to "; |
michael@0 | 316 | PrintCharsAsStringTo(s, strlen(s), os); |
michael@0 | 317 | } |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | // MSVC compiler can be configured to define whar_t as a typedef |
michael@0 | 321 | // of unsigned short. Defining an overload for const wchar_t* in that case |
michael@0 | 322 | // would cause pointers to unsigned shorts be printed as wide strings, |
michael@0 | 323 | // possibly accessing more memory than intended and causing invalid |
michael@0 | 324 | // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when |
michael@0 | 325 | // wchar_t is implemented as a native type. |
michael@0 | 326 | #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) |
michael@0 | 327 | // Prints the given wide C string to the ostream. |
michael@0 | 328 | void PrintTo(const wchar_t* s, ostream* os) { |
michael@0 | 329 | if (s == NULL) { |
michael@0 | 330 | *os << "NULL"; |
michael@0 | 331 | } else { |
michael@0 | 332 | *os << ImplicitCast_<const void*>(s) << " pointing to "; |
michael@0 | 333 | PrintCharsAsStringTo(s, wcslen(s), os); |
michael@0 | 334 | } |
michael@0 | 335 | } |
michael@0 | 336 | #endif // wchar_t is native |
michael@0 | 337 | |
michael@0 | 338 | // Prints a ::string object. |
michael@0 | 339 | #if GTEST_HAS_GLOBAL_STRING |
michael@0 | 340 | void PrintStringTo(const ::string& s, ostream* os) { |
michael@0 | 341 | PrintCharsAsStringTo(s.data(), s.size(), os); |
michael@0 | 342 | } |
michael@0 | 343 | #endif // GTEST_HAS_GLOBAL_STRING |
michael@0 | 344 | |
michael@0 | 345 | void PrintStringTo(const ::std::string& s, ostream* os) { |
michael@0 | 346 | PrintCharsAsStringTo(s.data(), s.size(), os); |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | // Prints a ::wstring object. |
michael@0 | 350 | #if GTEST_HAS_GLOBAL_WSTRING |
michael@0 | 351 | void PrintWideStringTo(const ::wstring& s, ostream* os) { |
michael@0 | 352 | PrintCharsAsStringTo(s.data(), s.size(), os); |
michael@0 | 353 | } |
michael@0 | 354 | #endif // GTEST_HAS_GLOBAL_WSTRING |
michael@0 | 355 | |
michael@0 | 356 | #if GTEST_HAS_STD_WSTRING |
michael@0 | 357 | void PrintWideStringTo(const ::std::wstring& s, ostream* os) { |
michael@0 | 358 | PrintCharsAsStringTo(s.data(), s.size(), os); |
michael@0 | 359 | } |
michael@0 | 360 | #endif // GTEST_HAS_STD_WSTRING |
michael@0 | 361 | |
michael@0 | 362 | } // namespace internal |
michael@0 | 363 | |
michael@0 | 364 | } // namespace testing |