1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest-printers_test.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1561 @@ 1.4 +// Copyright 2007, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// Author: wan@google.com (Zhanyong Wan) 1.34 + 1.35 +// Google Test - The Google C++ Testing Framework 1.36 +// 1.37 +// This file tests the universal value printer. 1.38 + 1.39 +#include "gtest/gtest-printers.h" 1.40 + 1.41 +#include <ctype.h> 1.42 +#include <limits.h> 1.43 +#include <string.h> 1.44 +#include <algorithm> 1.45 +#include <deque> 1.46 +#include <list> 1.47 +#include <map> 1.48 +#include <set> 1.49 +#include <sstream> 1.50 +#include <string> 1.51 +#include <utility> 1.52 +#include <vector> 1.53 + 1.54 +#include "gtest/gtest.h" 1.55 + 1.56 +// hash_map and hash_set are available under Visual C++. 1.57 +#if _MSC_VER 1.58 +# define GTEST_HAS_HASH_MAP_ 1 // Indicates that hash_map is available. 1.59 +# include <hash_map> // NOLINT 1.60 +# define GTEST_HAS_HASH_SET_ 1 // Indicates that hash_set is available. 1.61 +# include <hash_set> // NOLINT 1.62 +#endif // GTEST_OS_WINDOWS 1.63 + 1.64 +// Some user-defined types for testing the universal value printer. 1.65 + 1.66 +// An anonymous enum type. 1.67 +enum AnonymousEnum { 1.68 + kAE1 = -1, 1.69 + kAE2 = 1 1.70 +}; 1.71 + 1.72 +// An enum without a user-defined printer. 1.73 +enum EnumWithoutPrinter { 1.74 + kEWP1 = -2, 1.75 + kEWP2 = 42 1.76 +}; 1.77 + 1.78 +// An enum with a << operator. 1.79 +enum EnumWithStreaming { 1.80 + kEWS1 = 10 1.81 +}; 1.82 + 1.83 +std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) { 1.84 + return os << (e == kEWS1 ? "kEWS1" : "invalid"); 1.85 +} 1.86 + 1.87 +// An enum with a PrintTo() function. 1.88 +enum EnumWithPrintTo { 1.89 + kEWPT1 = 1 1.90 +}; 1.91 + 1.92 +void PrintTo(EnumWithPrintTo e, std::ostream* os) { 1.93 + *os << (e == kEWPT1 ? "kEWPT1" : "invalid"); 1.94 +} 1.95 + 1.96 +// A class implicitly convertible to BiggestInt. 1.97 +class BiggestIntConvertible { 1.98 + public: 1.99 + operator ::testing::internal::BiggestInt() const { return 42; } 1.100 +}; 1.101 + 1.102 +// A user-defined unprintable class template in the global namespace. 1.103 +template <typename T> 1.104 +class UnprintableTemplateInGlobal { 1.105 + public: 1.106 + UnprintableTemplateInGlobal() : value_() {} 1.107 + private: 1.108 + T value_; 1.109 +}; 1.110 + 1.111 +// A user-defined streamable type in the global namespace. 1.112 +class StreamableInGlobal { 1.113 + public: 1.114 + virtual ~StreamableInGlobal() {} 1.115 +}; 1.116 + 1.117 +inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) { 1.118 + os << "StreamableInGlobal"; 1.119 +} 1.120 + 1.121 +void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) { 1.122 + os << "StreamableInGlobal*"; 1.123 +} 1.124 + 1.125 +namespace foo { 1.126 + 1.127 +// A user-defined unprintable type in a user namespace. 1.128 +class UnprintableInFoo { 1.129 + public: 1.130 + UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); } 1.131 + private: 1.132 + char xy_[8]; 1.133 + double z_; 1.134 +}; 1.135 + 1.136 +// A user-defined printable type in a user-chosen namespace. 1.137 +struct PrintableViaPrintTo { 1.138 + PrintableViaPrintTo() : value() {} 1.139 + int value; 1.140 +}; 1.141 + 1.142 +void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) { 1.143 + *os << "PrintableViaPrintTo: " << x.value; 1.144 +} 1.145 + 1.146 +// A type with a user-defined << for printing its pointer. 1.147 +struct PointerPrintable { 1.148 +}; 1.149 + 1.150 +::std::ostream& operator<<(::std::ostream& os, 1.151 + const PointerPrintable* /* x */) { 1.152 + return os << "PointerPrintable*"; 1.153 +} 1.154 + 1.155 +// A user-defined printable class template in a user-chosen namespace. 1.156 +template <typename T> 1.157 +class PrintableViaPrintToTemplate { 1.158 + public: 1.159 + explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {} 1.160 + 1.161 + const T& value() const { return value_; } 1.162 + private: 1.163 + T value_; 1.164 +}; 1.165 + 1.166 +template <typename T> 1.167 +void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) { 1.168 + *os << "PrintableViaPrintToTemplate: " << x.value(); 1.169 +} 1.170 + 1.171 +// A user-defined streamable class template in a user namespace. 1.172 +template <typename T> 1.173 +class StreamableTemplateInFoo { 1.174 + public: 1.175 + StreamableTemplateInFoo() : value_() {} 1.176 + 1.177 + const T& value() const { return value_; } 1.178 + private: 1.179 + T value_; 1.180 +}; 1.181 + 1.182 +template <typename T> 1.183 +inline ::std::ostream& operator<<(::std::ostream& os, 1.184 + const StreamableTemplateInFoo<T>& x) { 1.185 + return os << "StreamableTemplateInFoo: " << x.value(); 1.186 +} 1.187 + 1.188 +} // namespace foo 1.189 + 1.190 +namespace testing { 1.191 +namespace gtest_printers_test { 1.192 + 1.193 +using ::std::deque; 1.194 +using ::std::list; 1.195 +using ::std::make_pair; 1.196 +using ::std::map; 1.197 +using ::std::multimap; 1.198 +using ::std::multiset; 1.199 +using ::std::pair; 1.200 +using ::std::set; 1.201 +using ::std::vector; 1.202 +using ::testing::PrintToString; 1.203 +using ::testing::internal::FormatForComparisonFailureMessage; 1.204 +using ::testing::internal::ImplicitCast_; 1.205 +using ::testing::internal::NativeArray; 1.206 +using ::testing::internal::RE; 1.207 +using ::testing::internal::Strings; 1.208 +using ::testing::internal::UniversalPrint; 1.209 +using ::testing::internal::UniversalPrinter; 1.210 +using ::testing::internal::UniversalTersePrint; 1.211 +using ::testing::internal::UniversalTersePrintTupleFieldsToStrings; 1.212 +using ::testing::internal::kReference; 1.213 +using ::testing::internal::string; 1.214 + 1.215 +#if GTEST_HAS_TR1_TUPLE 1.216 +using ::std::tr1::make_tuple; 1.217 +using ::std::tr1::tuple; 1.218 +#endif 1.219 + 1.220 +#if _MSC_VER 1.221 +// MSVC defines the following classes in the ::stdext namespace while 1.222 +// gcc defines them in the :: namespace. Note that they are not part 1.223 +// of the C++ standard. 1.224 +using ::stdext::hash_map; 1.225 +using ::stdext::hash_set; 1.226 +using ::stdext::hash_multimap; 1.227 +using ::stdext::hash_multiset; 1.228 +#endif 1.229 + 1.230 +// Prints a value to a string using the universal value printer. This 1.231 +// is a helper for testing UniversalPrinter<T>::Print() for various types. 1.232 +template <typename T> 1.233 +string Print(const T& value) { 1.234 + ::std::stringstream ss; 1.235 + UniversalPrinter<T>::Print(value, &ss); 1.236 + return ss.str(); 1.237 +} 1.238 + 1.239 +// Prints a value passed by reference to a string, using the universal 1.240 +// value printer. This is a helper for testing 1.241 +// UniversalPrinter<T&>::Print() for various types. 1.242 +template <typename T> 1.243 +string PrintByRef(const T& value) { 1.244 + ::std::stringstream ss; 1.245 + UniversalPrinter<T&>::Print(value, &ss); 1.246 + return ss.str(); 1.247 +} 1.248 + 1.249 +// Tests printing various enum types. 1.250 + 1.251 +TEST(PrintEnumTest, AnonymousEnum) { 1.252 + EXPECT_EQ("-1", Print(kAE1)); 1.253 + EXPECT_EQ("1", Print(kAE2)); 1.254 +} 1.255 + 1.256 +TEST(PrintEnumTest, EnumWithoutPrinter) { 1.257 + EXPECT_EQ("-2", Print(kEWP1)); 1.258 + EXPECT_EQ("42", Print(kEWP2)); 1.259 +} 1.260 + 1.261 +TEST(PrintEnumTest, EnumWithStreaming) { 1.262 + EXPECT_EQ("kEWS1", Print(kEWS1)); 1.263 + EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0))); 1.264 +} 1.265 + 1.266 +TEST(PrintEnumTest, EnumWithPrintTo) { 1.267 + EXPECT_EQ("kEWPT1", Print(kEWPT1)); 1.268 + EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0))); 1.269 +} 1.270 + 1.271 +// Tests printing a class implicitly convertible to BiggestInt. 1.272 + 1.273 +TEST(PrintClassTest, BiggestIntConvertible) { 1.274 + EXPECT_EQ("42", Print(BiggestIntConvertible())); 1.275 +} 1.276 + 1.277 +// Tests printing various char types. 1.278 + 1.279 +// char. 1.280 +TEST(PrintCharTest, PlainChar) { 1.281 + EXPECT_EQ("'\\0'", Print('\0')); 1.282 + EXPECT_EQ("'\\'' (39, 0x27)", Print('\'')); 1.283 + EXPECT_EQ("'\"' (34, 0x22)", Print('"')); 1.284 + EXPECT_EQ("'?' (63, 0x3F)", Print('?')); 1.285 + EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\')); 1.286 + EXPECT_EQ("'\\a' (7)", Print('\a')); 1.287 + EXPECT_EQ("'\\b' (8)", Print('\b')); 1.288 + EXPECT_EQ("'\\f' (12, 0xC)", Print('\f')); 1.289 + EXPECT_EQ("'\\n' (10, 0xA)", Print('\n')); 1.290 + EXPECT_EQ("'\\r' (13, 0xD)", Print('\r')); 1.291 + EXPECT_EQ("'\\t' (9)", Print('\t')); 1.292 + EXPECT_EQ("'\\v' (11, 0xB)", Print('\v')); 1.293 + EXPECT_EQ("'\\x7F' (127)", Print('\x7F')); 1.294 + EXPECT_EQ("'\\xFF' (255)", Print('\xFF')); 1.295 + EXPECT_EQ("' ' (32, 0x20)", Print(' ')); 1.296 + EXPECT_EQ("'a' (97, 0x61)", Print('a')); 1.297 +} 1.298 + 1.299 +// signed char. 1.300 +TEST(PrintCharTest, SignedChar) { 1.301 + EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0'))); 1.302 + EXPECT_EQ("'\\xCE' (-50)", 1.303 + Print(static_cast<signed char>(-50))); 1.304 +} 1.305 + 1.306 +// unsigned char. 1.307 +TEST(PrintCharTest, UnsignedChar) { 1.308 + EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0'))); 1.309 + EXPECT_EQ("'b' (98, 0x62)", 1.310 + Print(static_cast<unsigned char>('b'))); 1.311 +} 1.312 + 1.313 +// Tests printing other simple, built-in types. 1.314 + 1.315 +// bool. 1.316 +TEST(PrintBuiltInTypeTest, Bool) { 1.317 + EXPECT_EQ("false", Print(false)); 1.318 + EXPECT_EQ("true", Print(true)); 1.319 +} 1.320 + 1.321 +// wchar_t. 1.322 +TEST(PrintBuiltInTypeTest, Wchar_t) { 1.323 + EXPECT_EQ("L'\\0'", Print(L'\0')); 1.324 + EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\'')); 1.325 + EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"')); 1.326 + EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?')); 1.327 + EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\')); 1.328 + EXPECT_EQ("L'\\a' (7)", Print(L'\a')); 1.329 + EXPECT_EQ("L'\\b' (8)", Print(L'\b')); 1.330 + EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f')); 1.331 + EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n')); 1.332 + EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r')); 1.333 + EXPECT_EQ("L'\\t' (9)", Print(L'\t')); 1.334 + EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v')); 1.335 + EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F')); 1.336 + EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF')); 1.337 + EXPECT_EQ("L' ' (32, 0x20)", Print(L' ')); 1.338 + EXPECT_EQ("L'a' (97, 0x61)", Print(L'a')); 1.339 + EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576))); 1.340 + EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D))); 1.341 +} 1.342 + 1.343 +// Test that Int64 provides more storage than wchar_t. 1.344 +TEST(PrintTypeSizeTest, Wchar_t) { 1.345 + EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64)); 1.346 +} 1.347 + 1.348 +// Various integer types. 1.349 +TEST(PrintBuiltInTypeTest, Integer) { 1.350 + EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8 1.351 + EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8 1.352 + EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16 1.353 + EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16 1.354 + EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32 1.355 + EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32 1.356 + EXPECT_EQ("18446744073709551615", 1.357 + Print(static_cast<testing::internal::UInt64>(-1))); // uint64 1.358 + EXPECT_EQ("-9223372036854775808", 1.359 + Print(static_cast<testing::internal::Int64>(1) << 63)); // int64 1.360 +} 1.361 + 1.362 +// Size types. 1.363 +TEST(PrintBuiltInTypeTest, Size_t) { 1.364 + EXPECT_EQ("1", Print(sizeof('a'))); // size_t. 1.365 +#if !GTEST_OS_WINDOWS 1.366 + // Windows has no ssize_t type. 1.367 + EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t. 1.368 +#endif // !GTEST_OS_WINDOWS 1.369 +} 1.370 + 1.371 +// Floating-points. 1.372 +TEST(PrintBuiltInTypeTest, FloatingPoints) { 1.373 + EXPECT_EQ("1.5", Print(1.5f)); // float 1.374 + EXPECT_EQ("-2.5", Print(-2.5)); // double 1.375 +} 1.376 + 1.377 +// Since ::std::stringstream::operator<<(const void *) formats the pointer 1.378 +// output differently with different compilers, we have to create the expected 1.379 +// output first and use it as our expectation. 1.380 +static string PrintPointer(const void *p) { 1.381 + ::std::stringstream expected_result_stream; 1.382 + expected_result_stream << p; 1.383 + return expected_result_stream.str(); 1.384 +} 1.385 + 1.386 +// Tests printing C strings. 1.387 + 1.388 +// const char*. 1.389 +TEST(PrintCStringTest, Const) { 1.390 + const char* p = "World"; 1.391 + EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p)); 1.392 +} 1.393 + 1.394 +// char*. 1.395 +TEST(PrintCStringTest, NonConst) { 1.396 + char p[] = "Hi"; 1.397 + EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"", 1.398 + Print(static_cast<char*>(p))); 1.399 +} 1.400 + 1.401 +// NULL C string. 1.402 +TEST(PrintCStringTest, Null) { 1.403 + const char* p = NULL; 1.404 + EXPECT_EQ("NULL", Print(p)); 1.405 +} 1.406 + 1.407 +// Tests that C strings are escaped properly. 1.408 +TEST(PrintCStringTest, EscapesProperly) { 1.409 + const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a"; 1.410 + EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f" 1.411 + "\\n\\r\\t\\v\\x7F\\xFF a\"", 1.412 + Print(p)); 1.413 +} 1.414 + 1.415 + 1.416 + 1.417 +// MSVC compiler can be configured to define whar_t as a typedef 1.418 +// of unsigned short. Defining an overload for const wchar_t* in that case 1.419 +// would cause pointers to unsigned shorts be printed as wide strings, 1.420 +// possibly accessing more memory than intended and causing invalid 1.421 +// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when 1.422 +// wchar_t is implemented as a native type. 1.423 +#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) 1.424 + 1.425 +// const wchar_t*. 1.426 +TEST(PrintWideCStringTest, Const) { 1.427 + const wchar_t* p = L"World"; 1.428 + EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p)); 1.429 +} 1.430 + 1.431 +// wchar_t*. 1.432 +TEST(PrintWideCStringTest, NonConst) { 1.433 + wchar_t p[] = L"Hi"; 1.434 + EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"", 1.435 + Print(static_cast<wchar_t*>(p))); 1.436 +} 1.437 + 1.438 +// NULL wide C string. 1.439 +TEST(PrintWideCStringTest, Null) { 1.440 + const wchar_t* p = NULL; 1.441 + EXPECT_EQ("NULL", Print(p)); 1.442 +} 1.443 + 1.444 +// Tests that wide C strings are escaped properly. 1.445 +TEST(PrintWideCStringTest, EscapesProperly) { 1.446 + const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r', 1.447 + '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'}; 1.448 + EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f" 1.449 + "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"", 1.450 + Print(static_cast<const wchar_t*>(s))); 1.451 +} 1.452 +#endif // native wchar_t 1.453 + 1.454 +// Tests printing pointers to other char types. 1.455 + 1.456 +// signed char*. 1.457 +TEST(PrintCharPointerTest, SignedChar) { 1.458 + signed char* p = reinterpret_cast<signed char*>(0x1234); 1.459 + EXPECT_EQ(PrintPointer(p), Print(p)); 1.460 + p = NULL; 1.461 + EXPECT_EQ("NULL", Print(p)); 1.462 +} 1.463 + 1.464 +// const signed char*. 1.465 +TEST(PrintCharPointerTest, ConstSignedChar) { 1.466 + signed char* p = reinterpret_cast<signed char*>(0x1234); 1.467 + EXPECT_EQ(PrintPointer(p), Print(p)); 1.468 + p = NULL; 1.469 + EXPECT_EQ("NULL", Print(p)); 1.470 +} 1.471 + 1.472 +// unsigned char*. 1.473 +TEST(PrintCharPointerTest, UnsignedChar) { 1.474 + unsigned char* p = reinterpret_cast<unsigned char*>(0x1234); 1.475 + EXPECT_EQ(PrintPointer(p), Print(p)); 1.476 + p = NULL; 1.477 + EXPECT_EQ("NULL", Print(p)); 1.478 +} 1.479 + 1.480 +// const unsigned char*. 1.481 +TEST(PrintCharPointerTest, ConstUnsignedChar) { 1.482 + const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234); 1.483 + EXPECT_EQ(PrintPointer(p), Print(p)); 1.484 + p = NULL; 1.485 + EXPECT_EQ("NULL", Print(p)); 1.486 +} 1.487 + 1.488 +// Tests printing pointers to simple, built-in types. 1.489 + 1.490 +// bool*. 1.491 +TEST(PrintPointerToBuiltInTypeTest, Bool) { 1.492 + bool* p = reinterpret_cast<bool*>(0xABCD); 1.493 + EXPECT_EQ(PrintPointer(p), Print(p)); 1.494 + p = NULL; 1.495 + EXPECT_EQ("NULL", Print(p)); 1.496 +} 1.497 + 1.498 +// void*. 1.499 +TEST(PrintPointerToBuiltInTypeTest, Void) { 1.500 + void* p = reinterpret_cast<void*>(0xABCD); 1.501 + EXPECT_EQ(PrintPointer(p), Print(p)); 1.502 + p = NULL; 1.503 + EXPECT_EQ("NULL", Print(p)); 1.504 +} 1.505 + 1.506 +// const void*. 1.507 +TEST(PrintPointerToBuiltInTypeTest, ConstVoid) { 1.508 + const void* p = reinterpret_cast<const void*>(0xABCD); 1.509 + EXPECT_EQ(PrintPointer(p), Print(p)); 1.510 + p = NULL; 1.511 + EXPECT_EQ("NULL", Print(p)); 1.512 +} 1.513 + 1.514 +// Tests printing pointers to pointers. 1.515 +TEST(PrintPointerToPointerTest, IntPointerPointer) { 1.516 + int** p = reinterpret_cast<int**>(0xABCD); 1.517 + EXPECT_EQ(PrintPointer(p), Print(p)); 1.518 + p = NULL; 1.519 + EXPECT_EQ("NULL", Print(p)); 1.520 +} 1.521 + 1.522 +// Tests printing (non-member) function pointers. 1.523 + 1.524 +void MyFunction(int /* n */) {} 1.525 + 1.526 +TEST(PrintPointerTest, NonMemberFunctionPointer) { 1.527 + // We cannot directly cast &MyFunction to const void* because the 1.528 + // standard disallows casting between pointers to functions and 1.529 + // pointers to objects, and some compilers (e.g. GCC 3.4) enforce 1.530 + // this limitation. 1.531 + EXPECT_EQ( 1.532 + PrintPointer(reinterpret_cast<const void*>( 1.533 + reinterpret_cast<internal::BiggestInt>(&MyFunction))), 1.534 + Print(&MyFunction)); 1.535 + int (*p)(bool) = NULL; // NOLINT 1.536 + EXPECT_EQ("NULL", Print(p)); 1.537 +} 1.538 + 1.539 +// An assertion predicate determining whether a one string is a prefix for 1.540 +// another. 1.541 +template <typename StringType> 1.542 +AssertionResult HasPrefix(const StringType& str, const StringType& prefix) { 1.543 + if (str.find(prefix, 0) == 0) 1.544 + return AssertionSuccess(); 1.545 + 1.546 + const bool is_wide_string = sizeof(prefix[0]) > 1; 1.547 + const char* const begin_string_quote = is_wide_string ? "L\"" : "\""; 1.548 + return AssertionFailure() 1.549 + << begin_string_quote << prefix << "\" is not a prefix of " 1.550 + << begin_string_quote << str << "\"\n"; 1.551 +} 1.552 + 1.553 +// Tests printing member variable pointers. Although they are called 1.554 +// pointers, they don't point to a location in the address space. 1.555 +// Their representation is implementation-defined. Thus they will be 1.556 +// printed as raw bytes. 1.557 + 1.558 +struct Foo { 1.559 + public: 1.560 + virtual ~Foo() {} 1.561 + int MyMethod(char x) { return x + 1; } 1.562 + virtual char MyVirtualMethod(int /* n */) { return 'a'; } 1.563 + 1.564 + int value; 1.565 +}; 1.566 + 1.567 +TEST(PrintPointerTest, MemberVariablePointer) { 1.568 + EXPECT_TRUE(HasPrefix(Print(&Foo::value), 1.569 + Print(sizeof(&Foo::value)) + "-byte object ")); 1.570 + int (Foo::*p) = NULL; // NOLINT 1.571 + EXPECT_TRUE(HasPrefix(Print(p), 1.572 + Print(sizeof(p)) + "-byte object ")); 1.573 +} 1.574 + 1.575 +// Tests printing member function pointers. Although they are called 1.576 +// pointers, they don't point to a location in the address space. 1.577 +// Their representation is implementation-defined. Thus they will be 1.578 +// printed as raw bytes. 1.579 +TEST(PrintPointerTest, MemberFunctionPointer) { 1.580 + EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod), 1.581 + Print(sizeof(&Foo::MyMethod)) + "-byte object ")); 1.582 + EXPECT_TRUE( 1.583 + HasPrefix(Print(&Foo::MyVirtualMethod), 1.584 + Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object ")); 1.585 + int (Foo::*p)(char) = NULL; // NOLINT 1.586 + EXPECT_TRUE(HasPrefix(Print(p), 1.587 + Print(sizeof(p)) + "-byte object ")); 1.588 +} 1.589 + 1.590 +// Tests printing C arrays. 1.591 + 1.592 +// The difference between this and Print() is that it ensures that the 1.593 +// argument is a reference to an array. 1.594 +template <typename T, size_t N> 1.595 +string PrintArrayHelper(T (&a)[N]) { 1.596 + return Print(a); 1.597 +} 1.598 + 1.599 +// One-dimensional array. 1.600 +TEST(PrintArrayTest, OneDimensionalArray) { 1.601 + int a[5] = { 1, 2, 3, 4, 5 }; 1.602 + EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a)); 1.603 +} 1.604 + 1.605 +// Two-dimensional array. 1.606 +TEST(PrintArrayTest, TwoDimensionalArray) { 1.607 + int a[2][5] = { 1.608 + { 1, 2, 3, 4, 5 }, 1.609 + { 6, 7, 8, 9, 0 } 1.610 + }; 1.611 + EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a)); 1.612 +} 1.613 + 1.614 +// Array of const elements. 1.615 +TEST(PrintArrayTest, ConstArray) { 1.616 + const bool a[1] = { false }; 1.617 + EXPECT_EQ("{ false }", PrintArrayHelper(a)); 1.618 +} 1.619 + 1.620 +// char array without terminating NUL. 1.621 +TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) { 1.622 + // Array a contains '\0' in the middle and doesn't end with '\0'. 1.623 + char a[] = { 'H', '\0', 'i' }; 1.624 + EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a)); 1.625 +} 1.626 + 1.627 +// const char array with terminating NUL. 1.628 +TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) { 1.629 + const char a[] = "\0Hi"; 1.630 + EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a)); 1.631 +} 1.632 + 1.633 +// const wchar_t array without terminating NUL. 1.634 +TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) { 1.635 + // Array a contains '\0' in the middle and doesn't end with '\0'. 1.636 + const wchar_t a[] = { L'H', L'\0', L'i' }; 1.637 + EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a)); 1.638 +} 1.639 + 1.640 +// wchar_t array with terminating NUL. 1.641 +TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) { 1.642 + const wchar_t a[] = L"\0Hi"; 1.643 + EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a)); 1.644 +} 1.645 + 1.646 +// Array of objects. 1.647 +TEST(PrintArrayTest, ObjectArray) { 1.648 + string a[3] = { "Hi", "Hello", "Ni hao" }; 1.649 + EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a)); 1.650 +} 1.651 + 1.652 +// Array with many elements. 1.653 +TEST(PrintArrayTest, BigArray) { 1.654 + int a[100] = { 1, 2, 3 }; 1.655 + EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }", 1.656 + PrintArrayHelper(a)); 1.657 +} 1.658 + 1.659 +// Tests printing ::string and ::std::string. 1.660 + 1.661 +#if GTEST_HAS_GLOBAL_STRING 1.662 +// ::string. 1.663 +TEST(PrintStringTest, StringInGlobalNamespace) { 1.664 + const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; 1.665 + const ::string str(s, sizeof(s)); 1.666 + EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", 1.667 + Print(str)); 1.668 +} 1.669 +#endif // GTEST_HAS_GLOBAL_STRING 1.670 + 1.671 +// ::std::string. 1.672 +TEST(PrintStringTest, StringInStdNamespace) { 1.673 + const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; 1.674 + const ::std::string str(s, sizeof(s)); 1.675 + EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", 1.676 + Print(str)); 1.677 +} 1.678 + 1.679 +TEST(PrintStringTest, StringAmbiguousHex) { 1.680 + // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of: 1.681 + // '\x6', '\x6B', or '\x6BA'. 1.682 + 1.683 + // a hex escaping sequence following by a decimal digit 1.684 + EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3"))); 1.685 + // a hex escaping sequence following by a hex digit (lower-case) 1.686 + EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas"))); 1.687 + // a hex escaping sequence following by a hex digit (upper-case) 1.688 + EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA"))); 1.689 + // a hex escaping sequence following by a non-xdigit 1.690 + EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!"))); 1.691 +} 1.692 + 1.693 +// Tests printing ::wstring and ::std::wstring. 1.694 + 1.695 +#if GTEST_HAS_GLOBAL_WSTRING 1.696 +// ::wstring. 1.697 +TEST(PrintWideStringTest, StringInGlobalNamespace) { 1.698 + const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; 1.699 + const ::wstring str(s, sizeof(s)/sizeof(wchar_t)); 1.700 + EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" 1.701 + "\\xD3\\x576\\x8D3\\xC74D a\\0\"", 1.702 + Print(str)); 1.703 +} 1.704 +#endif // GTEST_HAS_GLOBAL_WSTRING 1.705 + 1.706 +#if GTEST_HAS_STD_WSTRING 1.707 +// ::std::wstring. 1.708 +TEST(PrintWideStringTest, StringInStdNamespace) { 1.709 + const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; 1.710 + const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t)); 1.711 + EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" 1.712 + "\\xD3\\x576\\x8D3\\xC74D a\\0\"", 1.713 + Print(str)); 1.714 +} 1.715 + 1.716 +TEST(PrintWideStringTest, StringAmbiguousHex) { 1.717 + // same for wide strings. 1.718 + EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3"))); 1.719 + EXPECT_EQ("L\"mm\\x6\" L\"bananas\"", 1.720 + Print(::std::wstring(L"mm\x6" L"bananas"))); 1.721 + EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"", 1.722 + Print(::std::wstring(L"NOM\x6" L"BANANA"))); 1.723 + EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!"))); 1.724 +} 1.725 +#endif // GTEST_HAS_STD_WSTRING 1.726 + 1.727 +// Tests printing types that support generic streaming (i.e. streaming 1.728 +// to std::basic_ostream<Char, CharTraits> for any valid Char and 1.729 +// CharTraits types). 1.730 + 1.731 +// Tests printing a non-template type that supports generic streaming. 1.732 + 1.733 +class AllowsGenericStreaming {}; 1.734 + 1.735 +template <typename Char, typename CharTraits> 1.736 +std::basic_ostream<Char, CharTraits>& operator<<( 1.737 + std::basic_ostream<Char, CharTraits>& os, 1.738 + const AllowsGenericStreaming& /* a */) { 1.739 + return os << "AllowsGenericStreaming"; 1.740 +} 1.741 + 1.742 +TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) { 1.743 + AllowsGenericStreaming a; 1.744 + EXPECT_EQ("AllowsGenericStreaming", Print(a)); 1.745 +} 1.746 + 1.747 +// Tests printing a template type that supports generic streaming. 1.748 + 1.749 +template <typename T> 1.750 +class AllowsGenericStreamingTemplate {}; 1.751 + 1.752 +template <typename Char, typename CharTraits, typename T> 1.753 +std::basic_ostream<Char, CharTraits>& operator<<( 1.754 + std::basic_ostream<Char, CharTraits>& os, 1.755 + const AllowsGenericStreamingTemplate<T>& /* a */) { 1.756 + return os << "AllowsGenericStreamingTemplate"; 1.757 +} 1.758 + 1.759 +TEST(PrintTypeWithGenericStreamingTest, TemplateType) { 1.760 + AllowsGenericStreamingTemplate<int> a; 1.761 + EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a)); 1.762 +} 1.763 + 1.764 +// Tests printing a type that supports generic streaming and can be 1.765 +// implicitly converted to another printable type. 1.766 + 1.767 +template <typename T> 1.768 +class AllowsGenericStreamingAndImplicitConversionTemplate { 1.769 + public: 1.770 + operator bool() const { return false; } 1.771 +}; 1.772 + 1.773 +template <typename Char, typename CharTraits, typename T> 1.774 +std::basic_ostream<Char, CharTraits>& operator<<( 1.775 + std::basic_ostream<Char, CharTraits>& os, 1.776 + const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) { 1.777 + return os << "AllowsGenericStreamingAndImplicitConversionTemplate"; 1.778 +} 1.779 + 1.780 +TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) { 1.781 + AllowsGenericStreamingAndImplicitConversionTemplate<int> a; 1.782 + EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a)); 1.783 +} 1.784 + 1.785 +#if GTEST_HAS_STRING_PIECE_ 1.786 + 1.787 +// Tests printing StringPiece. 1.788 + 1.789 +TEST(PrintStringPieceTest, SimpleStringPiece) { 1.790 + const StringPiece sp = "Hello"; 1.791 + EXPECT_EQ("\"Hello\"", Print(sp)); 1.792 +} 1.793 + 1.794 +TEST(PrintStringPieceTest, UnprintableCharacters) { 1.795 + const char str[] = "NUL (\0) and \r\t"; 1.796 + const StringPiece sp(str, sizeof(str) - 1); 1.797 + EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp)); 1.798 +} 1.799 + 1.800 +#endif // GTEST_HAS_STRING_PIECE_ 1.801 + 1.802 +// Tests printing STL containers. 1.803 + 1.804 +TEST(PrintStlContainerTest, EmptyDeque) { 1.805 + deque<char> empty; 1.806 + EXPECT_EQ("{}", Print(empty)); 1.807 +} 1.808 + 1.809 +TEST(PrintStlContainerTest, NonEmptyDeque) { 1.810 + deque<int> non_empty; 1.811 + non_empty.push_back(1); 1.812 + non_empty.push_back(3); 1.813 + EXPECT_EQ("{ 1, 3 }", Print(non_empty)); 1.814 +} 1.815 + 1.816 +#if GTEST_HAS_HASH_MAP_ 1.817 + 1.818 +TEST(PrintStlContainerTest, OneElementHashMap) { 1.819 + hash_map<int, char> map1; 1.820 + map1[1] = 'a'; 1.821 + EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1)); 1.822 +} 1.823 + 1.824 +TEST(PrintStlContainerTest, HashMultiMap) { 1.825 + hash_multimap<int, bool> map1; 1.826 + map1.insert(make_pair(5, true)); 1.827 + map1.insert(make_pair(5, false)); 1.828 + 1.829 + // Elements of hash_multimap can be printed in any order. 1.830 + const string result = Print(map1); 1.831 + EXPECT_TRUE(result == "{ (5, true), (5, false) }" || 1.832 + result == "{ (5, false), (5, true) }") 1.833 + << " where Print(map1) returns \"" << result << "\"."; 1.834 +} 1.835 + 1.836 +#endif // GTEST_HAS_HASH_MAP_ 1.837 + 1.838 +#if GTEST_HAS_HASH_SET_ 1.839 + 1.840 +TEST(PrintStlContainerTest, HashSet) { 1.841 + hash_set<string> set1; 1.842 + set1.insert("hello"); 1.843 + EXPECT_EQ("{ \"hello\" }", Print(set1)); 1.844 +} 1.845 + 1.846 +TEST(PrintStlContainerTest, HashMultiSet) { 1.847 + const int kSize = 5; 1.848 + int a[kSize] = { 1, 1, 2, 5, 1 }; 1.849 + hash_multiset<int> set1(a, a + kSize); 1.850 + 1.851 + // Elements of hash_multiset can be printed in any order. 1.852 + const string result = Print(set1); 1.853 + const string expected_pattern = "{ d, d, d, d, d }"; // d means a digit. 1.854 + 1.855 + // Verifies the result matches the expected pattern; also extracts 1.856 + // the numbers in the result. 1.857 + ASSERT_EQ(expected_pattern.length(), result.length()); 1.858 + std::vector<int> numbers; 1.859 + for (size_t i = 0; i != result.length(); i++) { 1.860 + if (expected_pattern[i] == 'd') { 1.861 + ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0); 1.862 + numbers.push_back(result[i] - '0'); 1.863 + } else { 1.864 + EXPECT_EQ(expected_pattern[i], result[i]) << " where result is " 1.865 + << result; 1.866 + } 1.867 + } 1.868 + 1.869 + // Makes sure the result contains the right numbers. 1.870 + std::sort(numbers.begin(), numbers.end()); 1.871 + std::sort(a, a + kSize); 1.872 + EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin())); 1.873 +} 1.874 + 1.875 +#endif // GTEST_HAS_HASH_SET_ 1.876 + 1.877 +TEST(PrintStlContainerTest, List) { 1.878 + const string a[] = { 1.879 + "hello", 1.880 + "world" 1.881 + }; 1.882 + const list<string> strings(a, a + 2); 1.883 + EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings)); 1.884 +} 1.885 + 1.886 +TEST(PrintStlContainerTest, Map) { 1.887 + map<int, bool> map1; 1.888 + map1[1] = true; 1.889 + map1[5] = false; 1.890 + map1[3] = true; 1.891 + EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1)); 1.892 +} 1.893 + 1.894 +TEST(PrintStlContainerTest, MultiMap) { 1.895 + multimap<bool, int> map1; 1.896 + // The make_pair template function would deduce the type as 1.897 + // pair<bool, int> here, and since the key part in a multimap has to 1.898 + // be constant, without a templated ctor in the pair class (as in 1.899 + // libCstd on Solaris), make_pair call would fail to compile as no 1.900 + // implicit conversion is found. Thus explicit typename is used 1.901 + // here instead. 1.902 + map1.insert(pair<const bool, int>(true, 0)); 1.903 + map1.insert(pair<const bool, int>(true, 1)); 1.904 + map1.insert(pair<const bool, int>(false, 2)); 1.905 + EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1)); 1.906 +} 1.907 + 1.908 +TEST(PrintStlContainerTest, Set) { 1.909 + const unsigned int a[] = { 3, 0, 5 }; 1.910 + set<unsigned int> set1(a, a + 3); 1.911 + EXPECT_EQ("{ 0, 3, 5 }", Print(set1)); 1.912 +} 1.913 + 1.914 +TEST(PrintStlContainerTest, MultiSet) { 1.915 + const int a[] = { 1, 1, 2, 5, 1 }; 1.916 + multiset<int> set1(a, a + 5); 1.917 + EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1)); 1.918 +} 1.919 + 1.920 +TEST(PrintStlContainerTest, Pair) { 1.921 + pair<const bool, int> p(true, 5); 1.922 + EXPECT_EQ("(true, 5)", Print(p)); 1.923 +} 1.924 + 1.925 +TEST(PrintStlContainerTest, Vector) { 1.926 + vector<int> v; 1.927 + v.push_back(1); 1.928 + v.push_back(2); 1.929 + EXPECT_EQ("{ 1, 2 }", Print(v)); 1.930 +} 1.931 + 1.932 +TEST(PrintStlContainerTest, LongSequence) { 1.933 + const int a[100] = { 1, 2, 3 }; 1.934 + const vector<int> v(a, a + 100); 1.935 + EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " 1.936 + "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v)); 1.937 +} 1.938 + 1.939 +TEST(PrintStlContainerTest, NestedContainer) { 1.940 + const int a1[] = { 1, 2 }; 1.941 + const int a2[] = { 3, 4, 5 }; 1.942 + const list<int> l1(a1, a1 + 2); 1.943 + const list<int> l2(a2, a2 + 3); 1.944 + 1.945 + vector<list<int> > v; 1.946 + v.push_back(l1); 1.947 + v.push_back(l2); 1.948 + EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v)); 1.949 +} 1.950 + 1.951 +TEST(PrintStlContainerTest, OneDimensionalNativeArray) { 1.952 + const int a[3] = { 1, 2, 3 }; 1.953 + NativeArray<int> b(a, 3, kReference); 1.954 + EXPECT_EQ("{ 1, 2, 3 }", Print(b)); 1.955 +} 1.956 + 1.957 +TEST(PrintStlContainerTest, TwoDimensionalNativeArray) { 1.958 + const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; 1.959 + NativeArray<int[3]> b(a, 2, kReference); 1.960 + EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b)); 1.961 +} 1.962 + 1.963 +// Tests that a class named iterator isn't treated as a container. 1.964 + 1.965 +struct iterator { 1.966 + char x; 1.967 +}; 1.968 + 1.969 +TEST(PrintStlContainerTest, Iterator) { 1.970 + iterator it = {}; 1.971 + EXPECT_EQ("1-byte object <00>", Print(it)); 1.972 +} 1.973 + 1.974 +// Tests that a class named const_iterator isn't treated as a container. 1.975 + 1.976 +struct const_iterator { 1.977 + char x; 1.978 +}; 1.979 + 1.980 +TEST(PrintStlContainerTest, ConstIterator) { 1.981 + const_iterator it = {}; 1.982 + EXPECT_EQ("1-byte object <00>", Print(it)); 1.983 +} 1.984 + 1.985 +#if GTEST_HAS_TR1_TUPLE 1.986 +// Tests printing tuples. 1.987 + 1.988 +// Tuples of various arities. 1.989 +TEST(PrintTupleTest, VariousSizes) { 1.990 + tuple<> t0; 1.991 + EXPECT_EQ("()", Print(t0)); 1.992 + 1.993 + tuple<int> t1(5); 1.994 + EXPECT_EQ("(5)", Print(t1)); 1.995 + 1.996 + tuple<char, bool> t2('a', true); 1.997 + EXPECT_EQ("('a' (97, 0x61), true)", Print(t2)); 1.998 + 1.999 + tuple<bool, int, int> t3(false, 2, 3); 1.1000 + EXPECT_EQ("(false, 2, 3)", Print(t3)); 1.1001 + 1.1002 + tuple<bool, int, int, int> t4(false, 2, 3, 4); 1.1003 + EXPECT_EQ("(false, 2, 3, 4)", Print(t4)); 1.1004 + 1.1005 + tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true); 1.1006 + EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5)); 1.1007 + 1.1008 + tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6); 1.1009 + EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6)); 1.1010 + 1.1011 + tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7); 1.1012 + EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7)); 1.1013 + 1.1014 + tuple<bool, int, int, int, bool, int, int, bool> t8( 1.1015 + false, 2, 3, 4, true, 6, 7, true); 1.1016 + EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8)); 1.1017 + 1.1018 + tuple<bool, int, int, int, bool, int, int, bool, int> t9( 1.1019 + false, 2, 3, 4, true, 6, 7, true, 9); 1.1020 + EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9)); 1.1021 + 1.1022 + const char* const str = "8"; 1.1023 + // VC++ 2010's implementation of tuple of C++0x is deficient, requiring 1.1024 + // an explicit type cast of NULL to be used. 1.1025 + tuple<bool, char, short, testing::internal::Int32, // NOLINT 1.1026 + testing::internal::Int64, float, double, const char*, void*, string> 1.1027 + t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, 1.1028 + ImplicitCast_<void*>(NULL), "10"); 1.1029 + EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) + 1.1030 + " pointing to \"8\", NULL, \"10\")", 1.1031 + Print(t10)); 1.1032 +} 1.1033 + 1.1034 +// Nested tuples. 1.1035 +TEST(PrintTupleTest, NestedTuple) { 1.1036 + tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a'); 1.1037 + EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested)); 1.1038 +} 1.1039 + 1.1040 +#endif // GTEST_HAS_TR1_TUPLE 1.1041 + 1.1042 +// Tests printing user-defined unprintable types. 1.1043 + 1.1044 +// Unprintable types in the global namespace. 1.1045 +TEST(PrintUnprintableTypeTest, InGlobalNamespace) { 1.1046 + EXPECT_EQ("1-byte object <00>", 1.1047 + Print(UnprintableTemplateInGlobal<char>())); 1.1048 +} 1.1049 + 1.1050 +// Unprintable types in a user namespace. 1.1051 +TEST(PrintUnprintableTypeTest, InUserNamespace) { 1.1052 + EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>", 1.1053 + Print(::foo::UnprintableInFoo())); 1.1054 +} 1.1055 + 1.1056 +// Unprintable types are that too big to be printed completely. 1.1057 + 1.1058 +struct Big { 1.1059 + Big() { memset(array, 0, sizeof(array)); } 1.1060 + char array[257]; 1.1061 +}; 1.1062 + 1.1063 +TEST(PrintUnpritableTypeTest, BigObject) { 1.1064 + EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 " 1.1065 + "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " 1.1066 + "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " 1.1067 + "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 " 1.1068 + "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " 1.1069 + "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " 1.1070 + "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>", 1.1071 + Print(Big())); 1.1072 +} 1.1073 + 1.1074 +// Tests printing user-defined streamable types. 1.1075 + 1.1076 +// Streamable types in the global namespace. 1.1077 +TEST(PrintStreamableTypeTest, InGlobalNamespace) { 1.1078 + StreamableInGlobal x; 1.1079 + EXPECT_EQ("StreamableInGlobal", Print(x)); 1.1080 + EXPECT_EQ("StreamableInGlobal*", Print(&x)); 1.1081 +} 1.1082 + 1.1083 +// Printable template types in a user namespace. 1.1084 +TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) { 1.1085 + EXPECT_EQ("StreamableTemplateInFoo: 0", 1.1086 + Print(::foo::StreamableTemplateInFoo<int>())); 1.1087 +} 1.1088 + 1.1089 +// Tests printing user-defined types that have a PrintTo() function. 1.1090 +TEST(PrintPrintableTypeTest, InUserNamespace) { 1.1091 + EXPECT_EQ("PrintableViaPrintTo: 0", 1.1092 + Print(::foo::PrintableViaPrintTo())); 1.1093 +} 1.1094 + 1.1095 +// Tests printing a pointer to a user-defined type that has a << 1.1096 +// operator for its pointer. 1.1097 +TEST(PrintPrintableTypeTest, PointerInUserNamespace) { 1.1098 + ::foo::PointerPrintable x; 1.1099 + EXPECT_EQ("PointerPrintable*", Print(&x)); 1.1100 +} 1.1101 + 1.1102 +// Tests printing user-defined class template that have a PrintTo() function. 1.1103 +TEST(PrintPrintableTypeTest, TemplateInUserNamespace) { 1.1104 + EXPECT_EQ("PrintableViaPrintToTemplate: 5", 1.1105 + Print(::foo::PrintableViaPrintToTemplate<int>(5))); 1.1106 +} 1.1107 + 1.1108 +#if GTEST_HAS_PROTOBUF_ 1.1109 + 1.1110 +// Tests printing a protocol message. 1.1111 +TEST(PrintProtocolMessageTest, PrintsShortDebugString) { 1.1112 + testing::internal::TestMessage msg; 1.1113 + msg.set_member("yes"); 1.1114 + EXPECT_EQ("<member:\"yes\">", Print(msg)); 1.1115 +} 1.1116 + 1.1117 +// Tests printing a short proto2 message. 1.1118 +TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) { 1.1119 + testing::internal::FooMessage msg; 1.1120 + msg.set_int_field(2); 1.1121 + msg.set_string_field("hello"); 1.1122 + EXPECT_PRED2(RE::FullMatch, Print(msg), 1.1123 + "<int_field:\\s*2\\s+string_field:\\s*\"hello\">"); 1.1124 +} 1.1125 + 1.1126 +// Tests printing a long proto2 message. 1.1127 +TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) { 1.1128 + testing::internal::FooMessage msg; 1.1129 + msg.set_int_field(2); 1.1130 + msg.set_string_field("hello"); 1.1131 + msg.add_names("peter"); 1.1132 + msg.add_names("paul"); 1.1133 + msg.add_names("mary"); 1.1134 + EXPECT_PRED2(RE::FullMatch, Print(msg), 1.1135 + "<\n" 1.1136 + "int_field:\\s*2\n" 1.1137 + "string_field:\\s*\"hello\"\n" 1.1138 + "names:\\s*\"peter\"\n" 1.1139 + "names:\\s*\"paul\"\n" 1.1140 + "names:\\s*\"mary\"\n" 1.1141 + ">"); 1.1142 +} 1.1143 + 1.1144 +#endif // GTEST_HAS_PROTOBUF_ 1.1145 + 1.1146 +// Tests that the universal printer prints both the address and the 1.1147 +// value of a reference. 1.1148 +TEST(PrintReferenceTest, PrintsAddressAndValue) { 1.1149 + int n = 5; 1.1150 + EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n)); 1.1151 + 1.1152 + int a[2][3] = { 1.1153 + { 0, 1, 2 }, 1.1154 + { 3, 4, 5 } 1.1155 + }; 1.1156 + EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }", 1.1157 + PrintByRef(a)); 1.1158 + 1.1159 + const ::foo::UnprintableInFoo x; 1.1160 + EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object " 1.1161 + "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>", 1.1162 + PrintByRef(x)); 1.1163 +} 1.1164 + 1.1165 +// Tests that the universal printer prints a function pointer passed by 1.1166 +// reference. 1.1167 +TEST(PrintReferenceTest, HandlesFunctionPointer) { 1.1168 + void (*fp)(int n) = &MyFunction; 1.1169 + const string fp_pointer_string = 1.1170 + PrintPointer(reinterpret_cast<const void*>(&fp)); 1.1171 + // We cannot directly cast &MyFunction to const void* because the 1.1172 + // standard disallows casting between pointers to functions and 1.1173 + // pointers to objects, and some compilers (e.g. GCC 3.4) enforce 1.1174 + // this limitation. 1.1175 + const string fp_string = PrintPointer(reinterpret_cast<const void*>( 1.1176 + reinterpret_cast<internal::BiggestInt>(fp))); 1.1177 + EXPECT_EQ("@" + fp_pointer_string + " " + fp_string, 1.1178 + PrintByRef(fp)); 1.1179 +} 1.1180 + 1.1181 +// Tests that the universal printer prints a member function pointer 1.1182 +// passed by reference. 1.1183 +TEST(PrintReferenceTest, HandlesMemberFunctionPointer) { 1.1184 + int (Foo::*p)(char ch) = &Foo::MyMethod; 1.1185 + EXPECT_TRUE(HasPrefix( 1.1186 + PrintByRef(p), 1.1187 + "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " + 1.1188 + Print(sizeof(p)) + "-byte object ")); 1.1189 + 1.1190 + char (Foo::*p2)(int n) = &Foo::MyVirtualMethod; 1.1191 + EXPECT_TRUE(HasPrefix( 1.1192 + PrintByRef(p2), 1.1193 + "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " + 1.1194 + Print(sizeof(p2)) + "-byte object ")); 1.1195 +} 1.1196 + 1.1197 +// Tests that the universal printer prints a member variable pointer 1.1198 +// passed by reference. 1.1199 +TEST(PrintReferenceTest, HandlesMemberVariablePointer) { 1.1200 + int (Foo::*p) = &Foo::value; // NOLINT 1.1201 + EXPECT_TRUE(HasPrefix( 1.1202 + PrintByRef(p), 1.1203 + "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object ")); 1.1204 +} 1.1205 + 1.1206 +// Tests that FormatForComparisonFailureMessage(), which is used to print 1.1207 +// an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion 1.1208 +// fails, formats the operand in the desired way. 1.1209 + 1.1210 +// scalar 1.1211 +TEST(FormatForComparisonFailureMessageTest, WorksForScalar) { 1.1212 + EXPECT_STREQ("123", 1.1213 + FormatForComparisonFailureMessage(123, 124).c_str()); 1.1214 +} 1.1215 + 1.1216 +// non-char pointer 1.1217 +TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) { 1.1218 + int n = 0; 1.1219 + EXPECT_EQ(PrintPointer(&n), 1.1220 + FormatForComparisonFailureMessage(&n, &n).c_str()); 1.1221 +} 1.1222 + 1.1223 +// non-char array 1.1224 +TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) { 1.1225 + // In expression 'array == x', 'array' is compared by pointer. 1.1226 + // Therefore we want to print an array operand as a pointer. 1.1227 + int n[] = { 1, 2, 3 }; 1.1228 + EXPECT_EQ(PrintPointer(n), 1.1229 + FormatForComparisonFailureMessage(n, n).c_str()); 1.1230 +} 1.1231 + 1.1232 +// Tests formatting a char pointer when it's compared with another pointer. 1.1233 +// In this case we want to print it as a raw pointer, as the comparision is by 1.1234 +// pointer. 1.1235 + 1.1236 +// char pointer vs pointer 1.1237 +TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) { 1.1238 + // In expression 'p == x', where 'p' and 'x' are (const or not) char 1.1239 + // pointers, the operands are compared by pointer. Therefore we 1.1240 + // want to print 'p' as a pointer instead of a C string (we don't 1.1241 + // even know if it's supposed to point to a valid C string). 1.1242 + 1.1243 + // const char* 1.1244 + const char* s = "hello"; 1.1245 + EXPECT_EQ(PrintPointer(s), 1.1246 + FormatForComparisonFailureMessage(s, s).c_str()); 1.1247 + 1.1248 + // char* 1.1249 + char ch = 'a'; 1.1250 + EXPECT_EQ(PrintPointer(&ch), 1.1251 + FormatForComparisonFailureMessage(&ch, &ch).c_str()); 1.1252 +} 1.1253 + 1.1254 +// wchar_t pointer vs pointer 1.1255 +TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) { 1.1256 + // In expression 'p == x', where 'p' and 'x' are (const or not) char 1.1257 + // pointers, the operands are compared by pointer. Therefore we 1.1258 + // want to print 'p' as a pointer instead of a wide C string (we don't 1.1259 + // even know if it's supposed to point to a valid wide C string). 1.1260 + 1.1261 + // const wchar_t* 1.1262 + const wchar_t* s = L"hello"; 1.1263 + EXPECT_EQ(PrintPointer(s), 1.1264 + FormatForComparisonFailureMessage(s, s).c_str()); 1.1265 + 1.1266 + // wchar_t* 1.1267 + wchar_t ch = L'a'; 1.1268 + EXPECT_EQ(PrintPointer(&ch), 1.1269 + FormatForComparisonFailureMessage(&ch, &ch).c_str()); 1.1270 +} 1.1271 + 1.1272 +// Tests formatting a char pointer when it's compared to a string object. 1.1273 +// In this case we want to print the char pointer as a C string. 1.1274 + 1.1275 +#if GTEST_HAS_GLOBAL_STRING 1.1276 +// char pointer vs ::string 1.1277 +TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) { 1.1278 + const char* s = "hello \"world"; 1.1279 + EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped. 1.1280 + FormatForComparisonFailureMessage(s, ::string()).c_str()); 1.1281 + 1.1282 + // char* 1.1283 + char str[] = "hi\1"; 1.1284 + char* p = str; 1.1285 + EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped. 1.1286 + FormatForComparisonFailureMessage(p, ::string()).c_str()); 1.1287 +} 1.1288 +#endif 1.1289 + 1.1290 +// char pointer vs std::string 1.1291 +TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) { 1.1292 + const char* s = "hello \"world"; 1.1293 + EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped. 1.1294 + FormatForComparisonFailureMessage(s, ::std::string()).c_str()); 1.1295 + 1.1296 + // char* 1.1297 + char str[] = "hi\1"; 1.1298 + char* p = str; 1.1299 + EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped. 1.1300 + FormatForComparisonFailureMessage(p, ::std::string()).c_str()); 1.1301 +} 1.1302 + 1.1303 +#if GTEST_HAS_GLOBAL_WSTRING 1.1304 +// wchar_t pointer vs ::wstring 1.1305 +TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) { 1.1306 + const wchar_t* s = L"hi \"world"; 1.1307 + EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped. 1.1308 + FormatForComparisonFailureMessage(s, ::wstring()).c_str()); 1.1309 + 1.1310 + // wchar_t* 1.1311 + wchar_t str[] = L"hi\1"; 1.1312 + wchar_t* p = str; 1.1313 + EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped. 1.1314 + FormatForComparisonFailureMessage(p, ::wstring()).c_str()); 1.1315 +} 1.1316 +#endif 1.1317 + 1.1318 +#if GTEST_HAS_STD_WSTRING 1.1319 +// wchar_t pointer vs std::wstring 1.1320 +TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) { 1.1321 + const wchar_t* s = L"hi \"world"; 1.1322 + EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped. 1.1323 + FormatForComparisonFailureMessage(s, ::std::wstring()).c_str()); 1.1324 + 1.1325 + // wchar_t* 1.1326 + wchar_t str[] = L"hi\1"; 1.1327 + wchar_t* p = str; 1.1328 + EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped. 1.1329 + FormatForComparisonFailureMessage(p, ::std::wstring()).c_str()); 1.1330 +} 1.1331 +#endif 1.1332 + 1.1333 +// Tests formatting a char array when it's compared with a pointer or array. 1.1334 +// In this case we want to print the array as a row pointer, as the comparison 1.1335 +// is by pointer. 1.1336 + 1.1337 +// char array vs pointer 1.1338 +TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) { 1.1339 + char str[] = "hi \"world\""; 1.1340 + char* p = NULL; 1.1341 + EXPECT_EQ(PrintPointer(str), 1.1342 + FormatForComparisonFailureMessage(str, p).c_str()); 1.1343 +} 1.1344 + 1.1345 +// char array vs char array 1.1346 +TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) { 1.1347 + const char str[] = "hi \"world\""; 1.1348 + EXPECT_EQ(PrintPointer(str), 1.1349 + FormatForComparisonFailureMessage(str, str).c_str()); 1.1350 +} 1.1351 + 1.1352 +// wchar_t array vs pointer 1.1353 +TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) { 1.1354 + wchar_t str[] = L"hi \"world\""; 1.1355 + wchar_t* p = NULL; 1.1356 + EXPECT_EQ(PrintPointer(str), 1.1357 + FormatForComparisonFailureMessage(str, p).c_str()); 1.1358 +} 1.1359 + 1.1360 +// wchar_t array vs wchar_t array 1.1361 +TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) { 1.1362 + const wchar_t str[] = L"hi \"world\""; 1.1363 + EXPECT_EQ(PrintPointer(str), 1.1364 + FormatForComparisonFailureMessage(str, str).c_str()); 1.1365 +} 1.1366 + 1.1367 +// Tests formatting a char array when it's compared with a string object. 1.1368 +// In this case we want to print the array as a C string. 1.1369 + 1.1370 +#if GTEST_HAS_GLOBAL_STRING 1.1371 +// char array vs string 1.1372 +TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) { 1.1373 + const char str[] = "hi \"w\0rld\""; 1.1374 + EXPECT_STREQ("\"hi \\\"w\"", // The content should be escaped. 1.1375 + // Embedded NUL terminates the string. 1.1376 + FormatForComparisonFailureMessage(str, ::string()).c_str()); 1.1377 +} 1.1378 +#endif 1.1379 + 1.1380 +// char array vs std::string 1.1381 +TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) { 1.1382 + const char str[] = "hi \"world\""; 1.1383 + EXPECT_STREQ("\"hi \\\"world\\\"\"", // The content should be escaped. 1.1384 + FormatForComparisonFailureMessage(str, ::std::string()).c_str()); 1.1385 +} 1.1386 + 1.1387 +#if GTEST_HAS_GLOBAL_WSTRING 1.1388 +// wchar_t array vs wstring 1.1389 +TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) { 1.1390 + const wchar_t str[] = L"hi \"world\""; 1.1391 + EXPECT_STREQ("L\"hi \\\"world\\\"\"", // The content should be escaped. 1.1392 + FormatForComparisonFailureMessage(str, ::wstring()).c_str()); 1.1393 +} 1.1394 +#endif 1.1395 + 1.1396 +#if GTEST_HAS_STD_WSTRING 1.1397 +// wchar_t array vs std::wstring 1.1398 +TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) { 1.1399 + const wchar_t str[] = L"hi \"w\0rld\""; 1.1400 + EXPECT_STREQ( 1.1401 + "L\"hi \\\"w\"", // The content should be escaped. 1.1402 + // Embedded NUL terminates the string. 1.1403 + FormatForComparisonFailureMessage(str, ::std::wstring()).c_str()); 1.1404 +} 1.1405 +#endif 1.1406 + 1.1407 +// Useful for testing PrintToString(). We cannot use EXPECT_EQ() 1.1408 +// there as its implementation uses PrintToString(). The caller must 1.1409 +// ensure that 'value' has no side effect. 1.1410 +#define EXPECT_PRINT_TO_STRING_(value, expected_string) \ 1.1411 + EXPECT_TRUE(PrintToString(value) == (expected_string)) \ 1.1412 + << " where " #value " prints as " << (PrintToString(value)) 1.1413 + 1.1414 +TEST(PrintToStringTest, WorksForScalar) { 1.1415 + EXPECT_PRINT_TO_STRING_(123, "123"); 1.1416 +} 1.1417 + 1.1418 +TEST(PrintToStringTest, WorksForPointerToConstChar) { 1.1419 + const char* p = "hello"; 1.1420 + EXPECT_PRINT_TO_STRING_(p, "\"hello\""); 1.1421 +} 1.1422 + 1.1423 +TEST(PrintToStringTest, WorksForPointerToNonConstChar) { 1.1424 + char s[] = "hello"; 1.1425 + char* p = s; 1.1426 + EXPECT_PRINT_TO_STRING_(p, "\"hello\""); 1.1427 +} 1.1428 + 1.1429 +TEST(PrintToStringTest, EscapesForPointerToConstChar) { 1.1430 + const char* p = "hello\n"; 1.1431 + EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\""); 1.1432 +} 1.1433 + 1.1434 +TEST(PrintToStringTest, EscapesForPointerToNonConstChar) { 1.1435 + char s[] = "hello\1"; 1.1436 + char* p = s; 1.1437 + EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\""); 1.1438 +} 1.1439 + 1.1440 +TEST(PrintToStringTest, WorksForArray) { 1.1441 + int n[3] = { 1, 2, 3 }; 1.1442 + EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }"); 1.1443 +} 1.1444 + 1.1445 +TEST(PrintToStringTest, WorksForCharArray) { 1.1446 + char s[] = "hello"; 1.1447 + EXPECT_PRINT_TO_STRING_(s, "\"hello\""); 1.1448 +} 1.1449 + 1.1450 +TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) { 1.1451 + const char str_with_nul[] = "hello\0 world"; 1.1452 + EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\""); 1.1453 + 1.1454 + char mutable_str_with_nul[] = "hello\0 world"; 1.1455 + EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\""); 1.1456 +} 1.1457 + 1.1458 +#undef EXPECT_PRINT_TO_STRING_ 1.1459 + 1.1460 +TEST(UniversalTersePrintTest, WorksForNonReference) { 1.1461 + ::std::stringstream ss; 1.1462 + UniversalTersePrint(123, &ss); 1.1463 + EXPECT_EQ("123", ss.str()); 1.1464 +} 1.1465 + 1.1466 +TEST(UniversalTersePrintTest, WorksForReference) { 1.1467 + const int& n = 123; 1.1468 + ::std::stringstream ss; 1.1469 + UniversalTersePrint(n, &ss); 1.1470 + EXPECT_EQ("123", ss.str()); 1.1471 +} 1.1472 + 1.1473 +TEST(UniversalTersePrintTest, WorksForCString) { 1.1474 + const char* s1 = "abc"; 1.1475 + ::std::stringstream ss1; 1.1476 + UniversalTersePrint(s1, &ss1); 1.1477 + EXPECT_EQ("\"abc\"", ss1.str()); 1.1478 + 1.1479 + char* s2 = const_cast<char*>(s1); 1.1480 + ::std::stringstream ss2; 1.1481 + UniversalTersePrint(s2, &ss2); 1.1482 + EXPECT_EQ("\"abc\"", ss2.str()); 1.1483 + 1.1484 + const char* s3 = NULL; 1.1485 + ::std::stringstream ss3; 1.1486 + UniversalTersePrint(s3, &ss3); 1.1487 + EXPECT_EQ("NULL", ss3.str()); 1.1488 +} 1.1489 + 1.1490 +TEST(UniversalPrintTest, WorksForNonReference) { 1.1491 + ::std::stringstream ss; 1.1492 + UniversalPrint(123, &ss); 1.1493 + EXPECT_EQ("123", ss.str()); 1.1494 +} 1.1495 + 1.1496 +TEST(UniversalPrintTest, WorksForReference) { 1.1497 + const int& n = 123; 1.1498 + ::std::stringstream ss; 1.1499 + UniversalPrint(n, &ss); 1.1500 + EXPECT_EQ("123", ss.str()); 1.1501 +} 1.1502 + 1.1503 +TEST(UniversalPrintTest, WorksForCString) { 1.1504 + const char* s1 = "abc"; 1.1505 + ::std::stringstream ss1; 1.1506 + UniversalPrint(s1, &ss1); 1.1507 + EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str())); 1.1508 + 1.1509 + char* s2 = const_cast<char*>(s1); 1.1510 + ::std::stringstream ss2; 1.1511 + UniversalPrint(s2, &ss2); 1.1512 + EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str())); 1.1513 + 1.1514 + const char* s3 = NULL; 1.1515 + ::std::stringstream ss3; 1.1516 + UniversalPrint(s3, &ss3); 1.1517 + EXPECT_EQ("NULL", ss3.str()); 1.1518 +} 1.1519 + 1.1520 +TEST(UniversalPrintTest, WorksForCharArray) { 1.1521 + const char str[] = "\"Line\0 1\"\nLine 2"; 1.1522 + ::std::stringstream ss1; 1.1523 + UniversalPrint(str, &ss1); 1.1524 + EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str()); 1.1525 + 1.1526 + const char mutable_str[] = "\"Line\0 1\"\nLine 2"; 1.1527 + ::std::stringstream ss2; 1.1528 + UniversalPrint(mutable_str, &ss2); 1.1529 + EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str()); 1.1530 +} 1.1531 + 1.1532 +#if GTEST_HAS_TR1_TUPLE 1.1533 + 1.1534 +TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) { 1.1535 + Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple()); 1.1536 + EXPECT_EQ(0u, result.size()); 1.1537 +} 1.1538 + 1.1539 +TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) { 1.1540 + Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1)); 1.1541 + ASSERT_EQ(1u, result.size()); 1.1542 + EXPECT_EQ("1", result[0]); 1.1543 +} 1.1544 + 1.1545 +TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) { 1.1546 + Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a')); 1.1547 + ASSERT_EQ(2u, result.size()); 1.1548 + EXPECT_EQ("1", result[0]); 1.1549 + EXPECT_EQ("'a' (97, 0x61)", result[1]); 1.1550 +} 1.1551 + 1.1552 +TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) { 1.1553 + const int n = 1; 1.1554 + Strings result = UniversalTersePrintTupleFieldsToStrings( 1.1555 + tuple<const int&, const char*>(n, "a")); 1.1556 + ASSERT_EQ(2u, result.size()); 1.1557 + EXPECT_EQ("1", result[0]); 1.1558 + EXPECT_EQ("\"a\"", result[1]); 1.1559 +} 1.1560 + 1.1561 +#endif // GTEST_HAS_TR1_TUPLE 1.1562 + 1.1563 +} // namespace gtest_printers_test 1.1564 +} // namespace testing