Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
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 tests the universal value printer. |
michael@0 | 35 | |
michael@0 | 36 | #include "gtest/gtest-printers.h" |
michael@0 | 37 | |
michael@0 | 38 | #include <ctype.h> |
michael@0 | 39 | #include <limits.h> |
michael@0 | 40 | #include <string.h> |
michael@0 | 41 | #include <algorithm> |
michael@0 | 42 | #include <deque> |
michael@0 | 43 | #include <list> |
michael@0 | 44 | #include <map> |
michael@0 | 45 | #include <set> |
michael@0 | 46 | #include <sstream> |
michael@0 | 47 | #include <string> |
michael@0 | 48 | #include <utility> |
michael@0 | 49 | #include <vector> |
michael@0 | 50 | |
michael@0 | 51 | #include "gtest/gtest.h" |
michael@0 | 52 | |
michael@0 | 53 | // hash_map and hash_set are available under Visual C++. |
michael@0 | 54 | #if _MSC_VER |
michael@0 | 55 | # define GTEST_HAS_HASH_MAP_ 1 // Indicates that hash_map is available. |
michael@0 | 56 | # include <hash_map> // NOLINT |
michael@0 | 57 | # define GTEST_HAS_HASH_SET_ 1 // Indicates that hash_set is available. |
michael@0 | 58 | # include <hash_set> // NOLINT |
michael@0 | 59 | #endif // GTEST_OS_WINDOWS |
michael@0 | 60 | |
michael@0 | 61 | // Some user-defined types for testing the universal value printer. |
michael@0 | 62 | |
michael@0 | 63 | // An anonymous enum type. |
michael@0 | 64 | enum AnonymousEnum { |
michael@0 | 65 | kAE1 = -1, |
michael@0 | 66 | kAE2 = 1 |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | // An enum without a user-defined printer. |
michael@0 | 70 | enum EnumWithoutPrinter { |
michael@0 | 71 | kEWP1 = -2, |
michael@0 | 72 | kEWP2 = 42 |
michael@0 | 73 | }; |
michael@0 | 74 | |
michael@0 | 75 | // An enum with a << operator. |
michael@0 | 76 | enum EnumWithStreaming { |
michael@0 | 77 | kEWS1 = 10 |
michael@0 | 78 | }; |
michael@0 | 79 | |
michael@0 | 80 | std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) { |
michael@0 | 81 | return os << (e == kEWS1 ? "kEWS1" : "invalid"); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | // An enum with a PrintTo() function. |
michael@0 | 85 | enum EnumWithPrintTo { |
michael@0 | 86 | kEWPT1 = 1 |
michael@0 | 87 | }; |
michael@0 | 88 | |
michael@0 | 89 | void PrintTo(EnumWithPrintTo e, std::ostream* os) { |
michael@0 | 90 | *os << (e == kEWPT1 ? "kEWPT1" : "invalid"); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | // A class implicitly convertible to BiggestInt. |
michael@0 | 94 | class BiggestIntConvertible { |
michael@0 | 95 | public: |
michael@0 | 96 | operator ::testing::internal::BiggestInt() const { return 42; } |
michael@0 | 97 | }; |
michael@0 | 98 | |
michael@0 | 99 | // A user-defined unprintable class template in the global namespace. |
michael@0 | 100 | template <typename T> |
michael@0 | 101 | class UnprintableTemplateInGlobal { |
michael@0 | 102 | public: |
michael@0 | 103 | UnprintableTemplateInGlobal() : value_() {} |
michael@0 | 104 | private: |
michael@0 | 105 | T value_; |
michael@0 | 106 | }; |
michael@0 | 107 | |
michael@0 | 108 | // A user-defined streamable type in the global namespace. |
michael@0 | 109 | class StreamableInGlobal { |
michael@0 | 110 | public: |
michael@0 | 111 | virtual ~StreamableInGlobal() {} |
michael@0 | 112 | }; |
michael@0 | 113 | |
michael@0 | 114 | inline void operator<<(::std::ostream& os, const StreamableInGlobal& /* x */) { |
michael@0 | 115 | os << "StreamableInGlobal"; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | void operator<<(::std::ostream& os, const StreamableInGlobal* /* x */) { |
michael@0 | 119 | os << "StreamableInGlobal*"; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | namespace foo { |
michael@0 | 123 | |
michael@0 | 124 | // A user-defined unprintable type in a user namespace. |
michael@0 | 125 | class UnprintableInFoo { |
michael@0 | 126 | public: |
michael@0 | 127 | UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); } |
michael@0 | 128 | private: |
michael@0 | 129 | char xy_[8]; |
michael@0 | 130 | double z_; |
michael@0 | 131 | }; |
michael@0 | 132 | |
michael@0 | 133 | // A user-defined printable type in a user-chosen namespace. |
michael@0 | 134 | struct PrintableViaPrintTo { |
michael@0 | 135 | PrintableViaPrintTo() : value() {} |
michael@0 | 136 | int value; |
michael@0 | 137 | }; |
michael@0 | 138 | |
michael@0 | 139 | void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) { |
michael@0 | 140 | *os << "PrintableViaPrintTo: " << x.value; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | // A type with a user-defined << for printing its pointer. |
michael@0 | 144 | struct PointerPrintable { |
michael@0 | 145 | }; |
michael@0 | 146 | |
michael@0 | 147 | ::std::ostream& operator<<(::std::ostream& os, |
michael@0 | 148 | const PointerPrintable* /* x */) { |
michael@0 | 149 | return os << "PointerPrintable*"; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | // A user-defined printable class template in a user-chosen namespace. |
michael@0 | 153 | template <typename T> |
michael@0 | 154 | class PrintableViaPrintToTemplate { |
michael@0 | 155 | public: |
michael@0 | 156 | explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {} |
michael@0 | 157 | |
michael@0 | 158 | const T& value() const { return value_; } |
michael@0 | 159 | private: |
michael@0 | 160 | T value_; |
michael@0 | 161 | }; |
michael@0 | 162 | |
michael@0 | 163 | template <typename T> |
michael@0 | 164 | void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) { |
michael@0 | 165 | *os << "PrintableViaPrintToTemplate: " << x.value(); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | // A user-defined streamable class template in a user namespace. |
michael@0 | 169 | template <typename T> |
michael@0 | 170 | class StreamableTemplateInFoo { |
michael@0 | 171 | public: |
michael@0 | 172 | StreamableTemplateInFoo() : value_() {} |
michael@0 | 173 | |
michael@0 | 174 | const T& value() const { return value_; } |
michael@0 | 175 | private: |
michael@0 | 176 | T value_; |
michael@0 | 177 | }; |
michael@0 | 178 | |
michael@0 | 179 | template <typename T> |
michael@0 | 180 | inline ::std::ostream& operator<<(::std::ostream& os, |
michael@0 | 181 | const StreamableTemplateInFoo<T>& x) { |
michael@0 | 182 | return os << "StreamableTemplateInFoo: " << x.value(); |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | } // namespace foo |
michael@0 | 186 | |
michael@0 | 187 | namespace testing { |
michael@0 | 188 | namespace gtest_printers_test { |
michael@0 | 189 | |
michael@0 | 190 | using ::std::deque; |
michael@0 | 191 | using ::std::list; |
michael@0 | 192 | using ::std::make_pair; |
michael@0 | 193 | using ::std::map; |
michael@0 | 194 | using ::std::multimap; |
michael@0 | 195 | using ::std::multiset; |
michael@0 | 196 | using ::std::pair; |
michael@0 | 197 | using ::std::set; |
michael@0 | 198 | using ::std::vector; |
michael@0 | 199 | using ::testing::PrintToString; |
michael@0 | 200 | using ::testing::internal::FormatForComparisonFailureMessage; |
michael@0 | 201 | using ::testing::internal::ImplicitCast_; |
michael@0 | 202 | using ::testing::internal::NativeArray; |
michael@0 | 203 | using ::testing::internal::RE; |
michael@0 | 204 | using ::testing::internal::Strings; |
michael@0 | 205 | using ::testing::internal::UniversalPrint; |
michael@0 | 206 | using ::testing::internal::UniversalPrinter; |
michael@0 | 207 | using ::testing::internal::UniversalTersePrint; |
michael@0 | 208 | using ::testing::internal::UniversalTersePrintTupleFieldsToStrings; |
michael@0 | 209 | using ::testing::internal::kReference; |
michael@0 | 210 | using ::testing::internal::string; |
michael@0 | 211 | |
michael@0 | 212 | #if GTEST_HAS_TR1_TUPLE |
michael@0 | 213 | using ::std::tr1::make_tuple; |
michael@0 | 214 | using ::std::tr1::tuple; |
michael@0 | 215 | #endif |
michael@0 | 216 | |
michael@0 | 217 | #if _MSC_VER |
michael@0 | 218 | // MSVC defines the following classes in the ::stdext namespace while |
michael@0 | 219 | // gcc defines them in the :: namespace. Note that they are not part |
michael@0 | 220 | // of the C++ standard. |
michael@0 | 221 | using ::stdext::hash_map; |
michael@0 | 222 | using ::stdext::hash_set; |
michael@0 | 223 | using ::stdext::hash_multimap; |
michael@0 | 224 | using ::stdext::hash_multiset; |
michael@0 | 225 | #endif |
michael@0 | 226 | |
michael@0 | 227 | // Prints a value to a string using the universal value printer. This |
michael@0 | 228 | // is a helper for testing UniversalPrinter<T>::Print() for various types. |
michael@0 | 229 | template <typename T> |
michael@0 | 230 | string Print(const T& value) { |
michael@0 | 231 | ::std::stringstream ss; |
michael@0 | 232 | UniversalPrinter<T>::Print(value, &ss); |
michael@0 | 233 | return ss.str(); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | // Prints a value passed by reference to a string, using the universal |
michael@0 | 237 | // value printer. This is a helper for testing |
michael@0 | 238 | // UniversalPrinter<T&>::Print() for various types. |
michael@0 | 239 | template <typename T> |
michael@0 | 240 | string PrintByRef(const T& value) { |
michael@0 | 241 | ::std::stringstream ss; |
michael@0 | 242 | UniversalPrinter<T&>::Print(value, &ss); |
michael@0 | 243 | return ss.str(); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | // Tests printing various enum types. |
michael@0 | 247 | |
michael@0 | 248 | TEST(PrintEnumTest, AnonymousEnum) { |
michael@0 | 249 | EXPECT_EQ("-1", Print(kAE1)); |
michael@0 | 250 | EXPECT_EQ("1", Print(kAE2)); |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | TEST(PrintEnumTest, EnumWithoutPrinter) { |
michael@0 | 254 | EXPECT_EQ("-2", Print(kEWP1)); |
michael@0 | 255 | EXPECT_EQ("42", Print(kEWP2)); |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | TEST(PrintEnumTest, EnumWithStreaming) { |
michael@0 | 259 | EXPECT_EQ("kEWS1", Print(kEWS1)); |
michael@0 | 260 | EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0))); |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | TEST(PrintEnumTest, EnumWithPrintTo) { |
michael@0 | 264 | EXPECT_EQ("kEWPT1", Print(kEWPT1)); |
michael@0 | 265 | EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0))); |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | // Tests printing a class implicitly convertible to BiggestInt. |
michael@0 | 269 | |
michael@0 | 270 | TEST(PrintClassTest, BiggestIntConvertible) { |
michael@0 | 271 | EXPECT_EQ("42", Print(BiggestIntConvertible())); |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | // Tests printing various char types. |
michael@0 | 275 | |
michael@0 | 276 | // char. |
michael@0 | 277 | TEST(PrintCharTest, PlainChar) { |
michael@0 | 278 | EXPECT_EQ("'\\0'", Print('\0')); |
michael@0 | 279 | EXPECT_EQ("'\\'' (39, 0x27)", Print('\'')); |
michael@0 | 280 | EXPECT_EQ("'\"' (34, 0x22)", Print('"')); |
michael@0 | 281 | EXPECT_EQ("'?' (63, 0x3F)", Print('?')); |
michael@0 | 282 | EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\')); |
michael@0 | 283 | EXPECT_EQ("'\\a' (7)", Print('\a')); |
michael@0 | 284 | EXPECT_EQ("'\\b' (8)", Print('\b')); |
michael@0 | 285 | EXPECT_EQ("'\\f' (12, 0xC)", Print('\f')); |
michael@0 | 286 | EXPECT_EQ("'\\n' (10, 0xA)", Print('\n')); |
michael@0 | 287 | EXPECT_EQ("'\\r' (13, 0xD)", Print('\r')); |
michael@0 | 288 | EXPECT_EQ("'\\t' (9)", Print('\t')); |
michael@0 | 289 | EXPECT_EQ("'\\v' (11, 0xB)", Print('\v')); |
michael@0 | 290 | EXPECT_EQ("'\\x7F' (127)", Print('\x7F')); |
michael@0 | 291 | EXPECT_EQ("'\\xFF' (255)", Print('\xFF')); |
michael@0 | 292 | EXPECT_EQ("' ' (32, 0x20)", Print(' ')); |
michael@0 | 293 | EXPECT_EQ("'a' (97, 0x61)", Print('a')); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | // signed char. |
michael@0 | 297 | TEST(PrintCharTest, SignedChar) { |
michael@0 | 298 | EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0'))); |
michael@0 | 299 | EXPECT_EQ("'\\xCE' (-50)", |
michael@0 | 300 | Print(static_cast<signed char>(-50))); |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | // unsigned char. |
michael@0 | 304 | TEST(PrintCharTest, UnsignedChar) { |
michael@0 | 305 | EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0'))); |
michael@0 | 306 | EXPECT_EQ("'b' (98, 0x62)", |
michael@0 | 307 | Print(static_cast<unsigned char>('b'))); |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | // Tests printing other simple, built-in types. |
michael@0 | 311 | |
michael@0 | 312 | // bool. |
michael@0 | 313 | TEST(PrintBuiltInTypeTest, Bool) { |
michael@0 | 314 | EXPECT_EQ("false", Print(false)); |
michael@0 | 315 | EXPECT_EQ("true", Print(true)); |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | // wchar_t. |
michael@0 | 319 | TEST(PrintBuiltInTypeTest, Wchar_t) { |
michael@0 | 320 | EXPECT_EQ("L'\\0'", Print(L'\0')); |
michael@0 | 321 | EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\'')); |
michael@0 | 322 | EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"')); |
michael@0 | 323 | EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?')); |
michael@0 | 324 | EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\')); |
michael@0 | 325 | EXPECT_EQ("L'\\a' (7)", Print(L'\a')); |
michael@0 | 326 | EXPECT_EQ("L'\\b' (8)", Print(L'\b')); |
michael@0 | 327 | EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f')); |
michael@0 | 328 | EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n')); |
michael@0 | 329 | EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r')); |
michael@0 | 330 | EXPECT_EQ("L'\\t' (9)", Print(L'\t')); |
michael@0 | 331 | EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v')); |
michael@0 | 332 | EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F')); |
michael@0 | 333 | EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF')); |
michael@0 | 334 | EXPECT_EQ("L' ' (32, 0x20)", Print(L' ')); |
michael@0 | 335 | EXPECT_EQ("L'a' (97, 0x61)", Print(L'a')); |
michael@0 | 336 | EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576))); |
michael@0 | 337 | EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D))); |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | // Test that Int64 provides more storage than wchar_t. |
michael@0 | 341 | TEST(PrintTypeSizeTest, Wchar_t) { |
michael@0 | 342 | EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64)); |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | // Various integer types. |
michael@0 | 346 | TEST(PrintBuiltInTypeTest, Integer) { |
michael@0 | 347 | EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255))); // uint8 |
michael@0 | 348 | EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128))); // int8 |
michael@0 | 349 | EXPECT_EQ("65535", Print(USHRT_MAX)); // uint16 |
michael@0 | 350 | EXPECT_EQ("-32768", Print(SHRT_MIN)); // int16 |
michael@0 | 351 | EXPECT_EQ("4294967295", Print(UINT_MAX)); // uint32 |
michael@0 | 352 | EXPECT_EQ("-2147483648", Print(INT_MIN)); // int32 |
michael@0 | 353 | EXPECT_EQ("18446744073709551615", |
michael@0 | 354 | Print(static_cast<testing::internal::UInt64>(-1))); // uint64 |
michael@0 | 355 | EXPECT_EQ("-9223372036854775808", |
michael@0 | 356 | Print(static_cast<testing::internal::Int64>(1) << 63)); // int64 |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | // Size types. |
michael@0 | 360 | TEST(PrintBuiltInTypeTest, Size_t) { |
michael@0 | 361 | EXPECT_EQ("1", Print(sizeof('a'))); // size_t. |
michael@0 | 362 | #if !GTEST_OS_WINDOWS |
michael@0 | 363 | // Windows has no ssize_t type. |
michael@0 | 364 | EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2))); // ssize_t. |
michael@0 | 365 | #endif // !GTEST_OS_WINDOWS |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | // Floating-points. |
michael@0 | 369 | TEST(PrintBuiltInTypeTest, FloatingPoints) { |
michael@0 | 370 | EXPECT_EQ("1.5", Print(1.5f)); // float |
michael@0 | 371 | EXPECT_EQ("-2.5", Print(-2.5)); // double |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | // Since ::std::stringstream::operator<<(const void *) formats the pointer |
michael@0 | 375 | // output differently with different compilers, we have to create the expected |
michael@0 | 376 | // output first and use it as our expectation. |
michael@0 | 377 | static string PrintPointer(const void *p) { |
michael@0 | 378 | ::std::stringstream expected_result_stream; |
michael@0 | 379 | expected_result_stream << p; |
michael@0 | 380 | return expected_result_stream.str(); |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | // Tests printing C strings. |
michael@0 | 384 | |
michael@0 | 385 | // const char*. |
michael@0 | 386 | TEST(PrintCStringTest, Const) { |
michael@0 | 387 | const char* p = "World"; |
michael@0 | 388 | EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p)); |
michael@0 | 389 | } |
michael@0 | 390 | |
michael@0 | 391 | // char*. |
michael@0 | 392 | TEST(PrintCStringTest, NonConst) { |
michael@0 | 393 | char p[] = "Hi"; |
michael@0 | 394 | EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"", |
michael@0 | 395 | Print(static_cast<char*>(p))); |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | // NULL C string. |
michael@0 | 399 | TEST(PrintCStringTest, Null) { |
michael@0 | 400 | const char* p = NULL; |
michael@0 | 401 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | // Tests that C strings are escaped properly. |
michael@0 | 405 | TEST(PrintCStringTest, EscapesProperly) { |
michael@0 | 406 | const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a"; |
michael@0 | 407 | EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f" |
michael@0 | 408 | "\\n\\r\\t\\v\\x7F\\xFF a\"", |
michael@0 | 409 | Print(p)); |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | |
michael@0 | 413 | |
michael@0 | 414 | // MSVC compiler can be configured to define whar_t as a typedef |
michael@0 | 415 | // of unsigned short. Defining an overload for const wchar_t* in that case |
michael@0 | 416 | // would cause pointers to unsigned shorts be printed as wide strings, |
michael@0 | 417 | // possibly accessing more memory than intended and causing invalid |
michael@0 | 418 | // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when |
michael@0 | 419 | // wchar_t is implemented as a native type. |
michael@0 | 420 | #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) |
michael@0 | 421 | |
michael@0 | 422 | // const wchar_t*. |
michael@0 | 423 | TEST(PrintWideCStringTest, Const) { |
michael@0 | 424 | const wchar_t* p = L"World"; |
michael@0 | 425 | EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p)); |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | // wchar_t*. |
michael@0 | 429 | TEST(PrintWideCStringTest, NonConst) { |
michael@0 | 430 | wchar_t p[] = L"Hi"; |
michael@0 | 431 | EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"", |
michael@0 | 432 | Print(static_cast<wchar_t*>(p))); |
michael@0 | 433 | } |
michael@0 | 434 | |
michael@0 | 435 | // NULL wide C string. |
michael@0 | 436 | TEST(PrintWideCStringTest, Null) { |
michael@0 | 437 | const wchar_t* p = NULL; |
michael@0 | 438 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 439 | } |
michael@0 | 440 | |
michael@0 | 441 | // Tests that wide C strings are escaped properly. |
michael@0 | 442 | TEST(PrintWideCStringTest, EscapesProperly) { |
michael@0 | 443 | const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r', |
michael@0 | 444 | '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'}; |
michael@0 | 445 | EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f" |
michael@0 | 446 | "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"", |
michael@0 | 447 | Print(static_cast<const wchar_t*>(s))); |
michael@0 | 448 | } |
michael@0 | 449 | #endif // native wchar_t |
michael@0 | 450 | |
michael@0 | 451 | // Tests printing pointers to other char types. |
michael@0 | 452 | |
michael@0 | 453 | // signed char*. |
michael@0 | 454 | TEST(PrintCharPointerTest, SignedChar) { |
michael@0 | 455 | signed char* p = reinterpret_cast<signed char*>(0x1234); |
michael@0 | 456 | EXPECT_EQ(PrintPointer(p), Print(p)); |
michael@0 | 457 | p = NULL; |
michael@0 | 458 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 459 | } |
michael@0 | 460 | |
michael@0 | 461 | // const signed char*. |
michael@0 | 462 | TEST(PrintCharPointerTest, ConstSignedChar) { |
michael@0 | 463 | signed char* p = reinterpret_cast<signed char*>(0x1234); |
michael@0 | 464 | EXPECT_EQ(PrintPointer(p), Print(p)); |
michael@0 | 465 | p = NULL; |
michael@0 | 466 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | // unsigned char*. |
michael@0 | 470 | TEST(PrintCharPointerTest, UnsignedChar) { |
michael@0 | 471 | unsigned char* p = reinterpret_cast<unsigned char*>(0x1234); |
michael@0 | 472 | EXPECT_EQ(PrintPointer(p), Print(p)); |
michael@0 | 473 | p = NULL; |
michael@0 | 474 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 475 | } |
michael@0 | 476 | |
michael@0 | 477 | // const unsigned char*. |
michael@0 | 478 | TEST(PrintCharPointerTest, ConstUnsignedChar) { |
michael@0 | 479 | const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234); |
michael@0 | 480 | EXPECT_EQ(PrintPointer(p), Print(p)); |
michael@0 | 481 | p = NULL; |
michael@0 | 482 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | // Tests printing pointers to simple, built-in types. |
michael@0 | 486 | |
michael@0 | 487 | // bool*. |
michael@0 | 488 | TEST(PrintPointerToBuiltInTypeTest, Bool) { |
michael@0 | 489 | bool* p = reinterpret_cast<bool*>(0xABCD); |
michael@0 | 490 | EXPECT_EQ(PrintPointer(p), Print(p)); |
michael@0 | 491 | p = NULL; |
michael@0 | 492 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 493 | } |
michael@0 | 494 | |
michael@0 | 495 | // void*. |
michael@0 | 496 | TEST(PrintPointerToBuiltInTypeTest, Void) { |
michael@0 | 497 | void* p = reinterpret_cast<void*>(0xABCD); |
michael@0 | 498 | EXPECT_EQ(PrintPointer(p), Print(p)); |
michael@0 | 499 | p = NULL; |
michael@0 | 500 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 501 | } |
michael@0 | 502 | |
michael@0 | 503 | // const void*. |
michael@0 | 504 | TEST(PrintPointerToBuiltInTypeTest, ConstVoid) { |
michael@0 | 505 | const void* p = reinterpret_cast<const void*>(0xABCD); |
michael@0 | 506 | EXPECT_EQ(PrintPointer(p), Print(p)); |
michael@0 | 507 | p = NULL; |
michael@0 | 508 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 509 | } |
michael@0 | 510 | |
michael@0 | 511 | // Tests printing pointers to pointers. |
michael@0 | 512 | TEST(PrintPointerToPointerTest, IntPointerPointer) { |
michael@0 | 513 | int** p = reinterpret_cast<int**>(0xABCD); |
michael@0 | 514 | EXPECT_EQ(PrintPointer(p), Print(p)); |
michael@0 | 515 | p = NULL; |
michael@0 | 516 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 517 | } |
michael@0 | 518 | |
michael@0 | 519 | // Tests printing (non-member) function pointers. |
michael@0 | 520 | |
michael@0 | 521 | void MyFunction(int /* n */) {} |
michael@0 | 522 | |
michael@0 | 523 | TEST(PrintPointerTest, NonMemberFunctionPointer) { |
michael@0 | 524 | // We cannot directly cast &MyFunction to const void* because the |
michael@0 | 525 | // standard disallows casting between pointers to functions and |
michael@0 | 526 | // pointers to objects, and some compilers (e.g. GCC 3.4) enforce |
michael@0 | 527 | // this limitation. |
michael@0 | 528 | EXPECT_EQ( |
michael@0 | 529 | PrintPointer(reinterpret_cast<const void*>( |
michael@0 | 530 | reinterpret_cast<internal::BiggestInt>(&MyFunction))), |
michael@0 | 531 | Print(&MyFunction)); |
michael@0 | 532 | int (*p)(bool) = NULL; // NOLINT |
michael@0 | 533 | EXPECT_EQ("NULL", Print(p)); |
michael@0 | 534 | } |
michael@0 | 535 | |
michael@0 | 536 | // An assertion predicate determining whether a one string is a prefix for |
michael@0 | 537 | // another. |
michael@0 | 538 | template <typename StringType> |
michael@0 | 539 | AssertionResult HasPrefix(const StringType& str, const StringType& prefix) { |
michael@0 | 540 | if (str.find(prefix, 0) == 0) |
michael@0 | 541 | return AssertionSuccess(); |
michael@0 | 542 | |
michael@0 | 543 | const bool is_wide_string = sizeof(prefix[0]) > 1; |
michael@0 | 544 | const char* const begin_string_quote = is_wide_string ? "L\"" : "\""; |
michael@0 | 545 | return AssertionFailure() |
michael@0 | 546 | << begin_string_quote << prefix << "\" is not a prefix of " |
michael@0 | 547 | << begin_string_quote << str << "\"\n"; |
michael@0 | 548 | } |
michael@0 | 549 | |
michael@0 | 550 | // Tests printing member variable pointers. Although they are called |
michael@0 | 551 | // pointers, they don't point to a location in the address space. |
michael@0 | 552 | // Their representation is implementation-defined. Thus they will be |
michael@0 | 553 | // printed as raw bytes. |
michael@0 | 554 | |
michael@0 | 555 | struct Foo { |
michael@0 | 556 | public: |
michael@0 | 557 | virtual ~Foo() {} |
michael@0 | 558 | int MyMethod(char x) { return x + 1; } |
michael@0 | 559 | virtual char MyVirtualMethod(int /* n */) { return 'a'; } |
michael@0 | 560 | |
michael@0 | 561 | int value; |
michael@0 | 562 | }; |
michael@0 | 563 | |
michael@0 | 564 | TEST(PrintPointerTest, MemberVariablePointer) { |
michael@0 | 565 | EXPECT_TRUE(HasPrefix(Print(&Foo::value), |
michael@0 | 566 | Print(sizeof(&Foo::value)) + "-byte object ")); |
michael@0 | 567 | int (Foo::*p) = NULL; // NOLINT |
michael@0 | 568 | EXPECT_TRUE(HasPrefix(Print(p), |
michael@0 | 569 | Print(sizeof(p)) + "-byte object ")); |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | // Tests printing member function pointers. Although they are called |
michael@0 | 573 | // pointers, they don't point to a location in the address space. |
michael@0 | 574 | // Their representation is implementation-defined. Thus they will be |
michael@0 | 575 | // printed as raw bytes. |
michael@0 | 576 | TEST(PrintPointerTest, MemberFunctionPointer) { |
michael@0 | 577 | EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod), |
michael@0 | 578 | Print(sizeof(&Foo::MyMethod)) + "-byte object ")); |
michael@0 | 579 | EXPECT_TRUE( |
michael@0 | 580 | HasPrefix(Print(&Foo::MyVirtualMethod), |
michael@0 | 581 | Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object ")); |
michael@0 | 582 | int (Foo::*p)(char) = NULL; // NOLINT |
michael@0 | 583 | EXPECT_TRUE(HasPrefix(Print(p), |
michael@0 | 584 | Print(sizeof(p)) + "-byte object ")); |
michael@0 | 585 | } |
michael@0 | 586 | |
michael@0 | 587 | // Tests printing C arrays. |
michael@0 | 588 | |
michael@0 | 589 | // The difference between this and Print() is that it ensures that the |
michael@0 | 590 | // argument is a reference to an array. |
michael@0 | 591 | template <typename T, size_t N> |
michael@0 | 592 | string PrintArrayHelper(T (&a)[N]) { |
michael@0 | 593 | return Print(a); |
michael@0 | 594 | } |
michael@0 | 595 | |
michael@0 | 596 | // One-dimensional array. |
michael@0 | 597 | TEST(PrintArrayTest, OneDimensionalArray) { |
michael@0 | 598 | int a[5] = { 1, 2, 3, 4, 5 }; |
michael@0 | 599 | EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a)); |
michael@0 | 600 | } |
michael@0 | 601 | |
michael@0 | 602 | // Two-dimensional array. |
michael@0 | 603 | TEST(PrintArrayTest, TwoDimensionalArray) { |
michael@0 | 604 | int a[2][5] = { |
michael@0 | 605 | { 1, 2, 3, 4, 5 }, |
michael@0 | 606 | { 6, 7, 8, 9, 0 } |
michael@0 | 607 | }; |
michael@0 | 608 | EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a)); |
michael@0 | 609 | } |
michael@0 | 610 | |
michael@0 | 611 | // Array of const elements. |
michael@0 | 612 | TEST(PrintArrayTest, ConstArray) { |
michael@0 | 613 | const bool a[1] = { false }; |
michael@0 | 614 | EXPECT_EQ("{ false }", PrintArrayHelper(a)); |
michael@0 | 615 | } |
michael@0 | 616 | |
michael@0 | 617 | // char array without terminating NUL. |
michael@0 | 618 | TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) { |
michael@0 | 619 | // Array a contains '\0' in the middle and doesn't end with '\0'. |
michael@0 | 620 | char a[] = { 'H', '\0', 'i' }; |
michael@0 | 621 | EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a)); |
michael@0 | 622 | } |
michael@0 | 623 | |
michael@0 | 624 | // const char array with terminating NUL. |
michael@0 | 625 | TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) { |
michael@0 | 626 | const char a[] = "\0Hi"; |
michael@0 | 627 | EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a)); |
michael@0 | 628 | } |
michael@0 | 629 | |
michael@0 | 630 | // const wchar_t array without terminating NUL. |
michael@0 | 631 | TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) { |
michael@0 | 632 | // Array a contains '\0' in the middle and doesn't end with '\0'. |
michael@0 | 633 | const wchar_t a[] = { L'H', L'\0', L'i' }; |
michael@0 | 634 | EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a)); |
michael@0 | 635 | } |
michael@0 | 636 | |
michael@0 | 637 | // wchar_t array with terminating NUL. |
michael@0 | 638 | TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) { |
michael@0 | 639 | const wchar_t a[] = L"\0Hi"; |
michael@0 | 640 | EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a)); |
michael@0 | 641 | } |
michael@0 | 642 | |
michael@0 | 643 | // Array of objects. |
michael@0 | 644 | TEST(PrintArrayTest, ObjectArray) { |
michael@0 | 645 | string a[3] = { "Hi", "Hello", "Ni hao" }; |
michael@0 | 646 | EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a)); |
michael@0 | 647 | } |
michael@0 | 648 | |
michael@0 | 649 | // Array with many elements. |
michael@0 | 650 | TEST(PrintArrayTest, BigArray) { |
michael@0 | 651 | int a[100] = { 1, 2, 3 }; |
michael@0 | 652 | EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }", |
michael@0 | 653 | PrintArrayHelper(a)); |
michael@0 | 654 | } |
michael@0 | 655 | |
michael@0 | 656 | // Tests printing ::string and ::std::string. |
michael@0 | 657 | |
michael@0 | 658 | #if GTEST_HAS_GLOBAL_STRING |
michael@0 | 659 | // ::string. |
michael@0 | 660 | TEST(PrintStringTest, StringInGlobalNamespace) { |
michael@0 | 661 | const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; |
michael@0 | 662 | const ::string str(s, sizeof(s)); |
michael@0 | 663 | EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", |
michael@0 | 664 | Print(str)); |
michael@0 | 665 | } |
michael@0 | 666 | #endif // GTEST_HAS_GLOBAL_STRING |
michael@0 | 667 | |
michael@0 | 668 | // ::std::string. |
michael@0 | 669 | TEST(PrintStringTest, StringInStdNamespace) { |
michael@0 | 670 | const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a"; |
michael@0 | 671 | const ::std::string str(s, sizeof(s)); |
michael@0 | 672 | EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"", |
michael@0 | 673 | Print(str)); |
michael@0 | 674 | } |
michael@0 | 675 | |
michael@0 | 676 | TEST(PrintStringTest, StringAmbiguousHex) { |
michael@0 | 677 | // "\x6BANANA" is ambiguous, it can be interpreted as starting with either of: |
michael@0 | 678 | // '\x6', '\x6B', or '\x6BA'. |
michael@0 | 679 | |
michael@0 | 680 | // a hex escaping sequence following by a decimal digit |
michael@0 | 681 | EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3"))); |
michael@0 | 682 | // a hex escaping sequence following by a hex digit (lower-case) |
michael@0 | 683 | EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas"))); |
michael@0 | 684 | // a hex escaping sequence following by a hex digit (upper-case) |
michael@0 | 685 | EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA"))); |
michael@0 | 686 | // a hex escaping sequence following by a non-xdigit |
michael@0 | 687 | EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!"))); |
michael@0 | 688 | } |
michael@0 | 689 | |
michael@0 | 690 | // Tests printing ::wstring and ::std::wstring. |
michael@0 | 691 | |
michael@0 | 692 | #if GTEST_HAS_GLOBAL_WSTRING |
michael@0 | 693 | // ::wstring. |
michael@0 | 694 | TEST(PrintWideStringTest, StringInGlobalNamespace) { |
michael@0 | 695 | const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; |
michael@0 | 696 | const ::wstring str(s, sizeof(s)/sizeof(wchar_t)); |
michael@0 | 697 | EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" |
michael@0 | 698 | "\\xD3\\x576\\x8D3\\xC74D a\\0\"", |
michael@0 | 699 | Print(str)); |
michael@0 | 700 | } |
michael@0 | 701 | #endif // GTEST_HAS_GLOBAL_WSTRING |
michael@0 | 702 | |
michael@0 | 703 | #if GTEST_HAS_STD_WSTRING |
michael@0 | 704 | // ::std::wstring. |
michael@0 | 705 | TEST(PrintWideStringTest, StringInStdNamespace) { |
michael@0 | 706 | const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a"; |
michael@0 | 707 | const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t)); |
michael@0 | 708 | EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v" |
michael@0 | 709 | "\\xD3\\x576\\x8D3\\xC74D a\\0\"", |
michael@0 | 710 | Print(str)); |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | TEST(PrintWideStringTest, StringAmbiguousHex) { |
michael@0 | 714 | // same for wide strings. |
michael@0 | 715 | EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3"))); |
michael@0 | 716 | EXPECT_EQ("L\"mm\\x6\" L\"bananas\"", |
michael@0 | 717 | Print(::std::wstring(L"mm\x6" L"bananas"))); |
michael@0 | 718 | EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"", |
michael@0 | 719 | Print(::std::wstring(L"NOM\x6" L"BANANA"))); |
michael@0 | 720 | EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!"))); |
michael@0 | 721 | } |
michael@0 | 722 | #endif // GTEST_HAS_STD_WSTRING |
michael@0 | 723 | |
michael@0 | 724 | // Tests printing types that support generic streaming (i.e. streaming |
michael@0 | 725 | // to std::basic_ostream<Char, CharTraits> for any valid Char and |
michael@0 | 726 | // CharTraits types). |
michael@0 | 727 | |
michael@0 | 728 | // Tests printing a non-template type that supports generic streaming. |
michael@0 | 729 | |
michael@0 | 730 | class AllowsGenericStreaming {}; |
michael@0 | 731 | |
michael@0 | 732 | template <typename Char, typename CharTraits> |
michael@0 | 733 | std::basic_ostream<Char, CharTraits>& operator<<( |
michael@0 | 734 | std::basic_ostream<Char, CharTraits>& os, |
michael@0 | 735 | const AllowsGenericStreaming& /* a */) { |
michael@0 | 736 | return os << "AllowsGenericStreaming"; |
michael@0 | 737 | } |
michael@0 | 738 | |
michael@0 | 739 | TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) { |
michael@0 | 740 | AllowsGenericStreaming a; |
michael@0 | 741 | EXPECT_EQ("AllowsGenericStreaming", Print(a)); |
michael@0 | 742 | } |
michael@0 | 743 | |
michael@0 | 744 | // Tests printing a template type that supports generic streaming. |
michael@0 | 745 | |
michael@0 | 746 | template <typename T> |
michael@0 | 747 | class AllowsGenericStreamingTemplate {}; |
michael@0 | 748 | |
michael@0 | 749 | template <typename Char, typename CharTraits, typename T> |
michael@0 | 750 | std::basic_ostream<Char, CharTraits>& operator<<( |
michael@0 | 751 | std::basic_ostream<Char, CharTraits>& os, |
michael@0 | 752 | const AllowsGenericStreamingTemplate<T>& /* a */) { |
michael@0 | 753 | return os << "AllowsGenericStreamingTemplate"; |
michael@0 | 754 | } |
michael@0 | 755 | |
michael@0 | 756 | TEST(PrintTypeWithGenericStreamingTest, TemplateType) { |
michael@0 | 757 | AllowsGenericStreamingTemplate<int> a; |
michael@0 | 758 | EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a)); |
michael@0 | 759 | } |
michael@0 | 760 | |
michael@0 | 761 | // Tests printing a type that supports generic streaming and can be |
michael@0 | 762 | // implicitly converted to another printable type. |
michael@0 | 763 | |
michael@0 | 764 | template <typename T> |
michael@0 | 765 | class AllowsGenericStreamingAndImplicitConversionTemplate { |
michael@0 | 766 | public: |
michael@0 | 767 | operator bool() const { return false; } |
michael@0 | 768 | }; |
michael@0 | 769 | |
michael@0 | 770 | template <typename Char, typename CharTraits, typename T> |
michael@0 | 771 | std::basic_ostream<Char, CharTraits>& operator<<( |
michael@0 | 772 | std::basic_ostream<Char, CharTraits>& os, |
michael@0 | 773 | const AllowsGenericStreamingAndImplicitConversionTemplate<T>& /* a */) { |
michael@0 | 774 | return os << "AllowsGenericStreamingAndImplicitConversionTemplate"; |
michael@0 | 775 | } |
michael@0 | 776 | |
michael@0 | 777 | TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) { |
michael@0 | 778 | AllowsGenericStreamingAndImplicitConversionTemplate<int> a; |
michael@0 | 779 | EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a)); |
michael@0 | 780 | } |
michael@0 | 781 | |
michael@0 | 782 | #if GTEST_HAS_STRING_PIECE_ |
michael@0 | 783 | |
michael@0 | 784 | // Tests printing StringPiece. |
michael@0 | 785 | |
michael@0 | 786 | TEST(PrintStringPieceTest, SimpleStringPiece) { |
michael@0 | 787 | const StringPiece sp = "Hello"; |
michael@0 | 788 | EXPECT_EQ("\"Hello\"", Print(sp)); |
michael@0 | 789 | } |
michael@0 | 790 | |
michael@0 | 791 | TEST(PrintStringPieceTest, UnprintableCharacters) { |
michael@0 | 792 | const char str[] = "NUL (\0) and \r\t"; |
michael@0 | 793 | const StringPiece sp(str, sizeof(str) - 1); |
michael@0 | 794 | EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp)); |
michael@0 | 795 | } |
michael@0 | 796 | |
michael@0 | 797 | #endif // GTEST_HAS_STRING_PIECE_ |
michael@0 | 798 | |
michael@0 | 799 | // Tests printing STL containers. |
michael@0 | 800 | |
michael@0 | 801 | TEST(PrintStlContainerTest, EmptyDeque) { |
michael@0 | 802 | deque<char> empty; |
michael@0 | 803 | EXPECT_EQ("{}", Print(empty)); |
michael@0 | 804 | } |
michael@0 | 805 | |
michael@0 | 806 | TEST(PrintStlContainerTest, NonEmptyDeque) { |
michael@0 | 807 | deque<int> non_empty; |
michael@0 | 808 | non_empty.push_back(1); |
michael@0 | 809 | non_empty.push_back(3); |
michael@0 | 810 | EXPECT_EQ("{ 1, 3 }", Print(non_empty)); |
michael@0 | 811 | } |
michael@0 | 812 | |
michael@0 | 813 | #if GTEST_HAS_HASH_MAP_ |
michael@0 | 814 | |
michael@0 | 815 | TEST(PrintStlContainerTest, OneElementHashMap) { |
michael@0 | 816 | hash_map<int, char> map1; |
michael@0 | 817 | map1[1] = 'a'; |
michael@0 | 818 | EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1)); |
michael@0 | 819 | } |
michael@0 | 820 | |
michael@0 | 821 | TEST(PrintStlContainerTest, HashMultiMap) { |
michael@0 | 822 | hash_multimap<int, bool> map1; |
michael@0 | 823 | map1.insert(make_pair(5, true)); |
michael@0 | 824 | map1.insert(make_pair(5, false)); |
michael@0 | 825 | |
michael@0 | 826 | // Elements of hash_multimap can be printed in any order. |
michael@0 | 827 | const string result = Print(map1); |
michael@0 | 828 | EXPECT_TRUE(result == "{ (5, true), (5, false) }" || |
michael@0 | 829 | result == "{ (5, false), (5, true) }") |
michael@0 | 830 | << " where Print(map1) returns \"" << result << "\"."; |
michael@0 | 831 | } |
michael@0 | 832 | |
michael@0 | 833 | #endif // GTEST_HAS_HASH_MAP_ |
michael@0 | 834 | |
michael@0 | 835 | #if GTEST_HAS_HASH_SET_ |
michael@0 | 836 | |
michael@0 | 837 | TEST(PrintStlContainerTest, HashSet) { |
michael@0 | 838 | hash_set<string> set1; |
michael@0 | 839 | set1.insert("hello"); |
michael@0 | 840 | EXPECT_EQ("{ \"hello\" }", Print(set1)); |
michael@0 | 841 | } |
michael@0 | 842 | |
michael@0 | 843 | TEST(PrintStlContainerTest, HashMultiSet) { |
michael@0 | 844 | const int kSize = 5; |
michael@0 | 845 | int a[kSize] = { 1, 1, 2, 5, 1 }; |
michael@0 | 846 | hash_multiset<int> set1(a, a + kSize); |
michael@0 | 847 | |
michael@0 | 848 | // Elements of hash_multiset can be printed in any order. |
michael@0 | 849 | const string result = Print(set1); |
michael@0 | 850 | const string expected_pattern = "{ d, d, d, d, d }"; // d means a digit. |
michael@0 | 851 | |
michael@0 | 852 | // Verifies the result matches the expected pattern; also extracts |
michael@0 | 853 | // the numbers in the result. |
michael@0 | 854 | ASSERT_EQ(expected_pattern.length(), result.length()); |
michael@0 | 855 | std::vector<int> numbers; |
michael@0 | 856 | for (size_t i = 0; i != result.length(); i++) { |
michael@0 | 857 | if (expected_pattern[i] == 'd') { |
michael@0 | 858 | ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0); |
michael@0 | 859 | numbers.push_back(result[i] - '0'); |
michael@0 | 860 | } else { |
michael@0 | 861 | EXPECT_EQ(expected_pattern[i], result[i]) << " where result is " |
michael@0 | 862 | << result; |
michael@0 | 863 | } |
michael@0 | 864 | } |
michael@0 | 865 | |
michael@0 | 866 | // Makes sure the result contains the right numbers. |
michael@0 | 867 | std::sort(numbers.begin(), numbers.end()); |
michael@0 | 868 | std::sort(a, a + kSize); |
michael@0 | 869 | EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin())); |
michael@0 | 870 | } |
michael@0 | 871 | |
michael@0 | 872 | #endif // GTEST_HAS_HASH_SET_ |
michael@0 | 873 | |
michael@0 | 874 | TEST(PrintStlContainerTest, List) { |
michael@0 | 875 | const string a[] = { |
michael@0 | 876 | "hello", |
michael@0 | 877 | "world" |
michael@0 | 878 | }; |
michael@0 | 879 | const list<string> strings(a, a + 2); |
michael@0 | 880 | EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings)); |
michael@0 | 881 | } |
michael@0 | 882 | |
michael@0 | 883 | TEST(PrintStlContainerTest, Map) { |
michael@0 | 884 | map<int, bool> map1; |
michael@0 | 885 | map1[1] = true; |
michael@0 | 886 | map1[5] = false; |
michael@0 | 887 | map1[3] = true; |
michael@0 | 888 | EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1)); |
michael@0 | 889 | } |
michael@0 | 890 | |
michael@0 | 891 | TEST(PrintStlContainerTest, MultiMap) { |
michael@0 | 892 | multimap<bool, int> map1; |
michael@0 | 893 | // The make_pair template function would deduce the type as |
michael@0 | 894 | // pair<bool, int> here, and since the key part in a multimap has to |
michael@0 | 895 | // be constant, without a templated ctor in the pair class (as in |
michael@0 | 896 | // libCstd on Solaris), make_pair call would fail to compile as no |
michael@0 | 897 | // implicit conversion is found. Thus explicit typename is used |
michael@0 | 898 | // here instead. |
michael@0 | 899 | map1.insert(pair<const bool, int>(true, 0)); |
michael@0 | 900 | map1.insert(pair<const bool, int>(true, 1)); |
michael@0 | 901 | map1.insert(pair<const bool, int>(false, 2)); |
michael@0 | 902 | EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1)); |
michael@0 | 903 | } |
michael@0 | 904 | |
michael@0 | 905 | TEST(PrintStlContainerTest, Set) { |
michael@0 | 906 | const unsigned int a[] = { 3, 0, 5 }; |
michael@0 | 907 | set<unsigned int> set1(a, a + 3); |
michael@0 | 908 | EXPECT_EQ("{ 0, 3, 5 }", Print(set1)); |
michael@0 | 909 | } |
michael@0 | 910 | |
michael@0 | 911 | TEST(PrintStlContainerTest, MultiSet) { |
michael@0 | 912 | const int a[] = { 1, 1, 2, 5, 1 }; |
michael@0 | 913 | multiset<int> set1(a, a + 5); |
michael@0 | 914 | EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1)); |
michael@0 | 915 | } |
michael@0 | 916 | |
michael@0 | 917 | TEST(PrintStlContainerTest, Pair) { |
michael@0 | 918 | pair<const bool, int> p(true, 5); |
michael@0 | 919 | EXPECT_EQ("(true, 5)", Print(p)); |
michael@0 | 920 | } |
michael@0 | 921 | |
michael@0 | 922 | TEST(PrintStlContainerTest, Vector) { |
michael@0 | 923 | vector<int> v; |
michael@0 | 924 | v.push_back(1); |
michael@0 | 925 | v.push_back(2); |
michael@0 | 926 | EXPECT_EQ("{ 1, 2 }", Print(v)); |
michael@0 | 927 | } |
michael@0 | 928 | |
michael@0 | 929 | TEST(PrintStlContainerTest, LongSequence) { |
michael@0 | 930 | const int a[100] = { 1, 2, 3 }; |
michael@0 | 931 | const vector<int> v(a, a + 100); |
michael@0 | 932 | EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " |
michael@0 | 933 | "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v)); |
michael@0 | 934 | } |
michael@0 | 935 | |
michael@0 | 936 | TEST(PrintStlContainerTest, NestedContainer) { |
michael@0 | 937 | const int a1[] = { 1, 2 }; |
michael@0 | 938 | const int a2[] = { 3, 4, 5 }; |
michael@0 | 939 | const list<int> l1(a1, a1 + 2); |
michael@0 | 940 | const list<int> l2(a2, a2 + 3); |
michael@0 | 941 | |
michael@0 | 942 | vector<list<int> > v; |
michael@0 | 943 | v.push_back(l1); |
michael@0 | 944 | v.push_back(l2); |
michael@0 | 945 | EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v)); |
michael@0 | 946 | } |
michael@0 | 947 | |
michael@0 | 948 | TEST(PrintStlContainerTest, OneDimensionalNativeArray) { |
michael@0 | 949 | const int a[3] = { 1, 2, 3 }; |
michael@0 | 950 | NativeArray<int> b(a, 3, kReference); |
michael@0 | 951 | EXPECT_EQ("{ 1, 2, 3 }", Print(b)); |
michael@0 | 952 | } |
michael@0 | 953 | |
michael@0 | 954 | TEST(PrintStlContainerTest, TwoDimensionalNativeArray) { |
michael@0 | 955 | const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
michael@0 | 956 | NativeArray<int[3]> b(a, 2, kReference); |
michael@0 | 957 | EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b)); |
michael@0 | 958 | } |
michael@0 | 959 | |
michael@0 | 960 | // Tests that a class named iterator isn't treated as a container. |
michael@0 | 961 | |
michael@0 | 962 | struct iterator { |
michael@0 | 963 | char x; |
michael@0 | 964 | }; |
michael@0 | 965 | |
michael@0 | 966 | TEST(PrintStlContainerTest, Iterator) { |
michael@0 | 967 | iterator it = {}; |
michael@0 | 968 | EXPECT_EQ("1-byte object <00>", Print(it)); |
michael@0 | 969 | } |
michael@0 | 970 | |
michael@0 | 971 | // Tests that a class named const_iterator isn't treated as a container. |
michael@0 | 972 | |
michael@0 | 973 | struct const_iterator { |
michael@0 | 974 | char x; |
michael@0 | 975 | }; |
michael@0 | 976 | |
michael@0 | 977 | TEST(PrintStlContainerTest, ConstIterator) { |
michael@0 | 978 | const_iterator it = {}; |
michael@0 | 979 | EXPECT_EQ("1-byte object <00>", Print(it)); |
michael@0 | 980 | } |
michael@0 | 981 | |
michael@0 | 982 | #if GTEST_HAS_TR1_TUPLE |
michael@0 | 983 | // Tests printing tuples. |
michael@0 | 984 | |
michael@0 | 985 | // Tuples of various arities. |
michael@0 | 986 | TEST(PrintTupleTest, VariousSizes) { |
michael@0 | 987 | tuple<> t0; |
michael@0 | 988 | EXPECT_EQ("()", Print(t0)); |
michael@0 | 989 | |
michael@0 | 990 | tuple<int> t1(5); |
michael@0 | 991 | EXPECT_EQ("(5)", Print(t1)); |
michael@0 | 992 | |
michael@0 | 993 | tuple<char, bool> t2('a', true); |
michael@0 | 994 | EXPECT_EQ("('a' (97, 0x61), true)", Print(t2)); |
michael@0 | 995 | |
michael@0 | 996 | tuple<bool, int, int> t3(false, 2, 3); |
michael@0 | 997 | EXPECT_EQ("(false, 2, 3)", Print(t3)); |
michael@0 | 998 | |
michael@0 | 999 | tuple<bool, int, int, int> t4(false, 2, 3, 4); |
michael@0 | 1000 | EXPECT_EQ("(false, 2, 3, 4)", Print(t4)); |
michael@0 | 1001 | |
michael@0 | 1002 | tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true); |
michael@0 | 1003 | EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5)); |
michael@0 | 1004 | |
michael@0 | 1005 | tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6); |
michael@0 | 1006 | EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6)); |
michael@0 | 1007 | |
michael@0 | 1008 | tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7); |
michael@0 | 1009 | EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7)); |
michael@0 | 1010 | |
michael@0 | 1011 | tuple<bool, int, int, int, bool, int, int, bool> t8( |
michael@0 | 1012 | false, 2, 3, 4, true, 6, 7, true); |
michael@0 | 1013 | EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8)); |
michael@0 | 1014 | |
michael@0 | 1015 | tuple<bool, int, int, int, bool, int, int, bool, int> t9( |
michael@0 | 1016 | false, 2, 3, 4, true, 6, 7, true, 9); |
michael@0 | 1017 | EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9)); |
michael@0 | 1018 | |
michael@0 | 1019 | const char* const str = "8"; |
michael@0 | 1020 | // VC++ 2010's implementation of tuple of C++0x is deficient, requiring |
michael@0 | 1021 | // an explicit type cast of NULL to be used. |
michael@0 | 1022 | tuple<bool, char, short, testing::internal::Int32, // NOLINT |
michael@0 | 1023 | testing::internal::Int64, float, double, const char*, void*, string> |
michael@0 | 1024 | t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, |
michael@0 | 1025 | ImplicitCast_<void*>(NULL), "10"); |
michael@0 | 1026 | EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) + |
michael@0 | 1027 | " pointing to \"8\", NULL, \"10\")", |
michael@0 | 1028 | Print(t10)); |
michael@0 | 1029 | } |
michael@0 | 1030 | |
michael@0 | 1031 | // Nested tuples. |
michael@0 | 1032 | TEST(PrintTupleTest, NestedTuple) { |
michael@0 | 1033 | tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a'); |
michael@0 | 1034 | EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested)); |
michael@0 | 1035 | } |
michael@0 | 1036 | |
michael@0 | 1037 | #endif // GTEST_HAS_TR1_TUPLE |
michael@0 | 1038 | |
michael@0 | 1039 | // Tests printing user-defined unprintable types. |
michael@0 | 1040 | |
michael@0 | 1041 | // Unprintable types in the global namespace. |
michael@0 | 1042 | TEST(PrintUnprintableTypeTest, InGlobalNamespace) { |
michael@0 | 1043 | EXPECT_EQ("1-byte object <00>", |
michael@0 | 1044 | Print(UnprintableTemplateInGlobal<char>())); |
michael@0 | 1045 | } |
michael@0 | 1046 | |
michael@0 | 1047 | // Unprintable types in a user namespace. |
michael@0 | 1048 | TEST(PrintUnprintableTypeTest, InUserNamespace) { |
michael@0 | 1049 | EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>", |
michael@0 | 1050 | Print(::foo::UnprintableInFoo())); |
michael@0 | 1051 | } |
michael@0 | 1052 | |
michael@0 | 1053 | // Unprintable types are that too big to be printed completely. |
michael@0 | 1054 | |
michael@0 | 1055 | struct Big { |
michael@0 | 1056 | Big() { memset(array, 0, sizeof(array)); } |
michael@0 | 1057 | char array[257]; |
michael@0 | 1058 | }; |
michael@0 | 1059 | |
michael@0 | 1060 | TEST(PrintUnpritableTypeTest, BigObject) { |
michael@0 | 1061 | EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 " |
michael@0 | 1062 | "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " |
michael@0 | 1063 | "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " |
michael@0 | 1064 | "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 " |
michael@0 | 1065 | "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " |
michael@0 | 1066 | "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 " |
michael@0 | 1067 | "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>", |
michael@0 | 1068 | Print(Big())); |
michael@0 | 1069 | } |
michael@0 | 1070 | |
michael@0 | 1071 | // Tests printing user-defined streamable types. |
michael@0 | 1072 | |
michael@0 | 1073 | // Streamable types in the global namespace. |
michael@0 | 1074 | TEST(PrintStreamableTypeTest, InGlobalNamespace) { |
michael@0 | 1075 | StreamableInGlobal x; |
michael@0 | 1076 | EXPECT_EQ("StreamableInGlobal", Print(x)); |
michael@0 | 1077 | EXPECT_EQ("StreamableInGlobal*", Print(&x)); |
michael@0 | 1078 | } |
michael@0 | 1079 | |
michael@0 | 1080 | // Printable template types in a user namespace. |
michael@0 | 1081 | TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) { |
michael@0 | 1082 | EXPECT_EQ("StreamableTemplateInFoo: 0", |
michael@0 | 1083 | Print(::foo::StreamableTemplateInFoo<int>())); |
michael@0 | 1084 | } |
michael@0 | 1085 | |
michael@0 | 1086 | // Tests printing user-defined types that have a PrintTo() function. |
michael@0 | 1087 | TEST(PrintPrintableTypeTest, InUserNamespace) { |
michael@0 | 1088 | EXPECT_EQ("PrintableViaPrintTo: 0", |
michael@0 | 1089 | Print(::foo::PrintableViaPrintTo())); |
michael@0 | 1090 | } |
michael@0 | 1091 | |
michael@0 | 1092 | // Tests printing a pointer to a user-defined type that has a << |
michael@0 | 1093 | // operator for its pointer. |
michael@0 | 1094 | TEST(PrintPrintableTypeTest, PointerInUserNamespace) { |
michael@0 | 1095 | ::foo::PointerPrintable x; |
michael@0 | 1096 | EXPECT_EQ("PointerPrintable*", Print(&x)); |
michael@0 | 1097 | } |
michael@0 | 1098 | |
michael@0 | 1099 | // Tests printing user-defined class template that have a PrintTo() function. |
michael@0 | 1100 | TEST(PrintPrintableTypeTest, TemplateInUserNamespace) { |
michael@0 | 1101 | EXPECT_EQ("PrintableViaPrintToTemplate: 5", |
michael@0 | 1102 | Print(::foo::PrintableViaPrintToTemplate<int>(5))); |
michael@0 | 1103 | } |
michael@0 | 1104 | |
michael@0 | 1105 | #if GTEST_HAS_PROTOBUF_ |
michael@0 | 1106 | |
michael@0 | 1107 | // Tests printing a protocol message. |
michael@0 | 1108 | TEST(PrintProtocolMessageTest, PrintsShortDebugString) { |
michael@0 | 1109 | testing::internal::TestMessage msg; |
michael@0 | 1110 | msg.set_member("yes"); |
michael@0 | 1111 | EXPECT_EQ("<member:\"yes\">", Print(msg)); |
michael@0 | 1112 | } |
michael@0 | 1113 | |
michael@0 | 1114 | // Tests printing a short proto2 message. |
michael@0 | 1115 | TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) { |
michael@0 | 1116 | testing::internal::FooMessage msg; |
michael@0 | 1117 | msg.set_int_field(2); |
michael@0 | 1118 | msg.set_string_field("hello"); |
michael@0 | 1119 | EXPECT_PRED2(RE::FullMatch, Print(msg), |
michael@0 | 1120 | "<int_field:\\s*2\\s+string_field:\\s*\"hello\">"); |
michael@0 | 1121 | } |
michael@0 | 1122 | |
michael@0 | 1123 | // Tests printing a long proto2 message. |
michael@0 | 1124 | TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) { |
michael@0 | 1125 | testing::internal::FooMessage msg; |
michael@0 | 1126 | msg.set_int_field(2); |
michael@0 | 1127 | msg.set_string_field("hello"); |
michael@0 | 1128 | msg.add_names("peter"); |
michael@0 | 1129 | msg.add_names("paul"); |
michael@0 | 1130 | msg.add_names("mary"); |
michael@0 | 1131 | EXPECT_PRED2(RE::FullMatch, Print(msg), |
michael@0 | 1132 | "<\n" |
michael@0 | 1133 | "int_field:\\s*2\n" |
michael@0 | 1134 | "string_field:\\s*\"hello\"\n" |
michael@0 | 1135 | "names:\\s*\"peter\"\n" |
michael@0 | 1136 | "names:\\s*\"paul\"\n" |
michael@0 | 1137 | "names:\\s*\"mary\"\n" |
michael@0 | 1138 | ">"); |
michael@0 | 1139 | } |
michael@0 | 1140 | |
michael@0 | 1141 | #endif // GTEST_HAS_PROTOBUF_ |
michael@0 | 1142 | |
michael@0 | 1143 | // Tests that the universal printer prints both the address and the |
michael@0 | 1144 | // value of a reference. |
michael@0 | 1145 | TEST(PrintReferenceTest, PrintsAddressAndValue) { |
michael@0 | 1146 | int n = 5; |
michael@0 | 1147 | EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n)); |
michael@0 | 1148 | |
michael@0 | 1149 | int a[2][3] = { |
michael@0 | 1150 | { 0, 1, 2 }, |
michael@0 | 1151 | { 3, 4, 5 } |
michael@0 | 1152 | }; |
michael@0 | 1153 | EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }", |
michael@0 | 1154 | PrintByRef(a)); |
michael@0 | 1155 | |
michael@0 | 1156 | const ::foo::UnprintableInFoo x; |
michael@0 | 1157 | EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object " |
michael@0 | 1158 | "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>", |
michael@0 | 1159 | PrintByRef(x)); |
michael@0 | 1160 | } |
michael@0 | 1161 | |
michael@0 | 1162 | // Tests that the universal printer prints a function pointer passed by |
michael@0 | 1163 | // reference. |
michael@0 | 1164 | TEST(PrintReferenceTest, HandlesFunctionPointer) { |
michael@0 | 1165 | void (*fp)(int n) = &MyFunction; |
michael@0 | 1166 | const string fp_pointer_string = |
michael@0 | 1167 | PrintPointer(reinterpret_cast<const void*>(&fp)); |
michael@0 | 1168 | // We cannot directly cast &MyFunction to const void* because the |
michael@0 | 1169 | // standard disallows casting between pointers to functions and |
michael@0 | 1170 | // pointers to objects, and some compilers (e.g. GCC 3.4) enforce |
michael@0 | 1171 | // this limitation. |
michael@0 | 1172 | const string fp_string = PrintPointer(reinterpret_cast<const void*>( |
michael@0 | 1173 | reinterpret_cast<internal::BiggestInt>(fp))); |
michael@0 | 1174 | EXPECT_EQ("@" + fp_pointer_string + " " + fp_string, |
michael@0 | 1175 | PrintByRef(fp)); |
michael@0 | 1176 | } |
michael@0 | 1177 | |
michael@0 | 1178 | // Tests that the universal printer prints a member function pointer |
michael@0 | 1179 | // passed by reference. |
michael@0 | 1180 | TEST(PrintReferenceTest, HandlesMemberFunctionPointer) { |
michael@0 | 1181 | int (Foo::*p)(char ch) = &Foo::MyMethod; |
michael@0 | 1182 | EXPECT_TRUE(HasPrefix( |
michael@0 | 1183 | PrintByRef(p), |
michael@0 | 1184 | "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " + |
michael@0 | 1185 | Print(sizeof(p)) + "-byte object ")); |
michael@0 | 1186 | |
michael@0 | 1187 | char (Foo::*p2)(int n) = &Foo::MyVirtualMethod; |
michael@0 | 1188 | EXPECT_TRUE(HasPrefix( |
michael@0 | 1189 | PrintByRef(p2), |
michael@0 | 1190 | "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " + |
michael@0 | 1191 | Print(sizeof(p2)) + "-byte object ")); |
michael@0 | 1192 | } |
michael@0 | 1193 | |
michael@0 | 1194 | // Tests that the universal printer prints a member variable pointer |
michael@0 | 1195 | // passed by reference. |
michael@0 | 1196 | TEST(PrintReferenceTest, HandlesMemberVariablePointer) { |
michael@0 | 1197 | int (Foo::*p) = &Foo::value; // NOLINT |
michael@0 | 1198 | EXPECT_TRUE(HasPrefix( |
michael@0 | 1199 | PrintByRef(p), |
michael@0 | 1200 | "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object ")); |
michael@0 | 1201 | } |
michael@0 | 1202 | |
michael@0 | 1203 | // Tests that FormatForComparisonFailureMessage(), which is used to print |
michael@0 | 1204 | // an operand in a comparison assertion (e.g. ASSERT_EQ) when the assertion |
michael@0 | 1205 | // fails, formats the operand in the desired way. |
michael@0 | 1206 | |
michael@0 | 1207 | // scalar |
michael@0 | 1208 | TEST(FormatForComparisonFailureMessageTest, WorksForScalar) { |
michael@0 | 1209 | EXPECT_STREQ("123", |
michael@0 | 1210 | FormatForComparisonFailureMessage(123, 124).c_str()); |
michael@0 | 1211 | } |
michael@0 | 1212 | |
michael@0 | 1213 | // non-char pointer |
michael@0 | 1214 | TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) { |
michael@0 | 1215 | int n = 0; |
michael@0 | 1216 | EXPECT_EQ(PrintPointer(&n), |
michael@0 | 1217 | FormatForComparisonFailureMessage(&n, &n).c_str()); |
michael@0 | 1218 | } |
michael@0 | 1219 | |
michael@0 | 1220 | // non-char array |
michael@0 | 1221 | TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) { |
michael@0 | 1222 | // In expression 'array == x', 'array' is compared by pointer. |
michael@0 | 1223 | // Therefore we want to print an array operand as a pointer. |
michael@0 | 1224 | int n[] = { 1, 2, 3 }; |
michael@0 | 1225 | EXPECT_EQ(PrintPointer(n), |
michael@0 | 1226 | FormatForComparisonFailureMessage(n, n).c_str()); |
michael@0 | 1227 | } |
michael@0 | 1228 | |
michael@0 | 1229 | // Tests formatting a char pointer when it's compared with another pointer. |
michael@0 | 1230 | // In this case we want to print it as a raw pointer, as the comparision is by |
michael@0 | 1231 | // pointer. |
michael@0 | 1232 | |
michael@0 | 1233 | // char pointer vs pointer |
michael@0 | 1234 | TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) { |
michael@0 | 1235 | // In expression 'p == x', where 'p' and 'x' are (const or not) char |
michael@0 | 1236 | // pointers, the operands are compared by pointer. Therefore we |
michael@0 | 1237 | // want to print 'p' as a pointer instead of a C string (we don't |
michael@0 | 1238 | // even know if it's supposed to point to a valid C string). |
michael@0 | 1239 | |
michael@0 | 1240 | // const char* |
michael@0 | 1241 | const char* s = "hello"; |
michael@0 | 1242 | EXPECT_EQ(PrintPointer(s), |
michael@0 | 1243 | FormatForComparisonFailureMessage(s, s).c_str()); |
michael@0 | 1244 | |
michael@0 | 1245 | // char* |
michael@0 | 1246 | char ch = 'a'; |
michael@0 | 1247 | EXPECT_EQ(PrintPointer(&ch), |
michael@0 | 1248 | FormatForComparisonFailureMessage(&ch, &ch).c_str()); |
michael@0 | 1249 | } |
michael@0 | 1250 | |
michael@0 | 1251 | // wchar_t pointer vs pointer |
michael@0 | 1252 | TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) { |
michael@0 | 1253 | // In expression 'p == x', where 'p' and 'x' are (const or not) char |
michael@0 | 1254 | // pointers, the operands are compared by pointer. Therefore we |
michael@0 | 1255 | // want to print 'p' as a pointer instead of a wide C string (we don't |
michael@0 | 1256 | // even know if it's supposed to point to a valid wide C string). |
michael@0 | 1257 | |
michael@0 | 1258 | // const wchar_t* |
michael@0 | 1259 | const wchar_t* s = L"hello"; |
michael@0 | 1260 | EXPECT_EQ(PrintPointer(s), |
michael@0 | 1261 | FormatForComparisonFailureMessage(s, s).c_str()); |
michael@0 | 1262 | |
michael@0 | 1263 | // wchar_t* |
michael@0 | 1264 | wchar_t ch = L'a'; |
michael@0 | 1265 | EXPECT_EQ(PrintPointer(&ch), |
michael@0 | 1266 | FormatForComparisonFailureMessage(&ch, &ch).c_str()); |
michael@0 | 1267 | } |
michael@0 | 1268 | |
michael@0 | 1269 | // Tests formatting a char pointer when it's compared to a string object. |
michael@0 | 1270 | // In this case we want to print the char pointer as a C string. |
michael@0 | 1271 | |
michael@0 | 1272 | #if GTEST_HAS_GLOBAL_STRING |
michael@0 | 1273 | // char pointer vs ::string |
michael@0 | 1274 | TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) { |
michael@0 | 1275 | const char* s = "hello \"world"; |
michael@0 | 1276 | EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped. |
michael@0 | 1277 | FormatForComparisonFailureMessage(s, ::string()).c_str()); |
michael@0 | 1278 | |
michael@0 | 1279 | // char* |
michael@0 | 1280 | char str[] = "hi\1"; |
michael@0 | 1281 | char* p = str; |
michael@0 | 1282 | EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped. |
michael@0 | 1283 | FormatForComparisonFailureMessage(p, ::string()).c_str()); |
michael@0 | 1284 | } |
michael@0 | 1285 | #endif |
michael@0 | 1286 | |
michael@0 | 1287 | // char pointer vs std::string |
michael@0 | 1288 | TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) { |
michael@0 | 1289 | const char* s = "hello \"world"; |
michael@0 | 1290 | EXPECT_STREQ("\"hello \\\"world\"", // The string content should be escaped. |
michael@0 | 1291 | FormatForComparisonFailureMessage(s, ::std::string()).c_str()); |
michael@0 | 1292 | |
michael@0 | 1293 | // char* |
michael@0 | 1294 | char str[] = "hi\1"; |
michael@0 | 1295 | char* p = str; |
michael@0 | 1296 | EXPECT_STREQ("\"hi\\x1\"", // The string content should be escaped. |
michael@0 | 1297 | FormatForComparisonFailureMessage(p, ::std::string()).c_str()); |
michael@0 | 1298 | } |
michael@0 | 1299 | |
michael@0 | 1300 | #if GTEST_HAS_GLOBAL_WSTRING |
michael@0 | 1301 | // wchar_t pointer vs ::wstring |
michael@0 | 1302 | TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) { |
michael@0 | 1303 | const wchar_t* s = L"hi \"world"; |
michael@0 | 1304 | EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped. |
michael@0 | 1305 | FormatForComparisonFailureMessage(s, ::wstring()).c_str()); |
michael@0 | 1306 | |
michael@0 | 1307 | // wchar_t* |
michael@0 | 1308 | wchar_t str[] = L"hi\1"; |
michael@0 | 1309 | wchar_t* p = str; |
michael@0 | 1310 | EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped. |
michael@0 | 1311 | FormatForComparisonFailureMessage(p, ::wstring()).c_str()); |
michael@0 | 1312 | } |
michael@0 | 1313 | #endif |
michael@0 | 1314 | |
michael@0 | 1315 | #if GTEST_HAS_STD_WSTRING |
michael@0 | 1316 | // wchar_t pointer vs std::wstring |
michael@0 | 1317 | TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) { |
michael@0 | 1318 | const wchar_t* s = L"hi \"world"; |
michael@0 | 1319 | EXPECT_STREQ("L\"hi \\\"world\"", // The string content should be escaped. |
michael@0 | 1320 | FormatForComparisonFailureMessage(s, ::std::wstring()).c_str()); |
michael@0 | 1321 | |
michael@0 | 1322 | // wchar_t* |
michael@0 | 1323 | wchar_t str[] = L"hi\1"; |
michael@0 | 1324 | wchar_t* p = str; |
michael@0 | 1325 | EXPECT_STREQ("L\"hi\\x1\"", // The string content should be escaped. |
michael@0 | 1326 | FormatForComparisonFailureMessage(p, ::std::wstring()).c_str()); |
michael@0 | 1327 | } |
michael@0 | 1328 | #endif |
michael@0 | 1329 | |
michael@0 | 1330 | // Tests formatting a char array when it's compared with a pointer or array. |
michael@0 | 1331 | // In this case we want to print the array as a row pointer, as the comparison |
michael@0 | 1332 | // is by pointer. |
michael@0 | 1333 | |
michael@0 | 1334 | // char array vs pointer |
michael@0 | 1335 | TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) { |
michael@0 | 1336 | char str[] = "hi \"world\""; |
michael@0 | 1337 | char* p = NULL; |
michael@0 | 1338 | EXPECT_EQ(PrintPointer(str), |
michael@0 | 1339 | FormatForComparisonFailureMessage(str, p).c_str()); |
michael@0 | 1340 | } |
michael@0 | 1341 | |
michael@0 | 1342 | // char array vs char array |
michael@0 | 1343 | TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) { |
michael@0 | 1344 | const char str[] = "hi \"world\""; |
michael@0 | 1345 | EXPECT_EQ(PrintPointer(str), |
michael@0 | 1346 | FormatForComparisonFailureMessage(str, str).c_str()); |
michael@0 | 1347 | } |
michael@0 | 1348 | |
michael@0 | 1349 | // wchar_t array vs pointer |
michael@0 | 1350 | TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) { |
michael@0 | 1351 | wchar_t str[] = L"hi \"world\""; |
michael@0 | 1352 | wchar_t* p = NULL; |
michael@0 | 1353 | EXPECT_EQ(PrintPointer(str), |
michael@0 | 1354 | FormatForComparisonFailureMessage(str, p).c_str()); |
michael@0 | 1355 | } |
michael@0 | 1356 | |
michael@0 | 1357 | // wchar_t array vs wchar_t array |
michael@0 | 1358 | TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) { |
michael@0 | 1359 | const wchar_t str[] = L"hi \"world\""; |
michael@0 | 1360 | EXPECT_EQ(PrintPointer(str), |
michael@0 | 1361 | FormatForComparisonFailureMessage(str, str).c_str()); |
michael@0 | 1362 | } |
michael@0 | 1363 | |
michael@0 | 1364 | // Tests formatting a char array when it's compared with a string object. |
michael@0 | 1365 | // In this case we want to print the array as a C string. |
michael@0 | 1366 | |
michael@0 | 1367 | #if GTEST_HAS_GLOBAL_STRING |
michael@0 | 1368 | // char array vs string |
michael@0 | 1369 | TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) { |
michael@0 | 1370 | const char str[] = "hi \"w\0rld\""; |
michael@0 | 1371 | EXPECT_STREQ("\"hi \\\"w\"", // The content should be escaped. |
michael@0 | 1372 | // Embedded NUL terminates the string. |
michael@0 | 1373 | FormatForComparisonFailureMessage(str, ::string()).c_str()); |
michael@0 | 1374 | } |
michael@0 | 1375 | #endif |
michael@0 | 1376 | |
michael@0 | 1377 | // char array vs std::string |
michael@0 | 1378 | TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) { |
michael@0 | 1379 | const char str[] = "hi \"world\""; |
michael@0 | 1380 | EXPECT_STREQ("\"hi \\\"world\\\"\"", // The content should be escaped. |
michael@0 | 1381 | FormatForComparisonFailureMessage(str, ::std::string()).c_str()); |
michael@0 | 1382 | } |
michael@0 | 1383 | |
michael@0 | 1384 | #if GTEST_HAS_GLOBAL_WSTRING |
michael@0 | 1385 | // wchar_t array vs wstring |
michael@0 | 1386 | TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) { |
michael@0 | 1387 | const wchar_t str[] = L"hi \"world\""; |
michael@0 | 1388 | EXPECT_STREQ("L\"hi \\\"world\\\"\"", // The content should be escaped. |
michael@0 | 1389 | FormatForComparisonFailureMessage(str, ::wstring()).c_str()); |
michael@0 | 1390 | } |
michael@0 | 1391 | #endif |
michael@0 | 1392 | |
michael@0 | 1393 | #if GTEST_HAS_STD_WSTRING |
michael@0 | 1394 | // wchar_t array vs std::wstring |
michael@0 | 1395 | TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) { |
michael@0 | 1396 | const wchar_t str[] = L"hi \"w\0rld\""; |
michael@0 | 1397 | EXPECT_STREQ( |
michael@0 | 1398 | "L\"hi \\\"w\"", // The content should be escaped. |
michael@0 | 1399 | // Embedded NUL terminates the string. |
michael@0 | 1400 | FormatForComparisonFailureMessage(str, ::std::wstring()).c_str()); |
michael@0 | 1401 | } |
michael@0 | 1402 | #endif |
michael@0 | 1403 | |
michael@0 | 1404 | // Useful for testing PrintToString(). We cannot use EXPECT_EQ() |
michael@0 | 1405 | // there as its implementation uses PrintToString(). The caller must |
michael@0 | 1406 | // ensure that 'value' has no side effect. |
michael@0 | 1407 | #define EXPECT_PRINT_TO_STRING_(value, expected_string) \ |
michael@0 | 1408 | EXPECT_TRUE(PrintToString(value) == (expected_string)) \ |
michael@0 | 1409 | << " where " #value " prints as " << (PrintToString(value)) |
michael@0 | 1410 | |
michael@0 | 1411 | TEST(PrintToStringTest, WorksForScalar) { |
michael@0 | 1412 | EXPECT_PRINT_TO_STRING_(123, "123"); |
michael@0 | 1413 | } |
michael@0 | 1414 | |
michael@0 | 1415 | TEST(PrintToStringTest, WorksForPointerToConstChar) { |
michael@0 | 1416 | const char* p = "hello"; |
michael@0 | 1417 | EXPECT_PRINT_TO_STRING_(p, "\"hello\""); |
michael@0 | 1418 | } |
michael@0 | 1419 | |
michael@0 | 1420 | TEST(PrintToStringTest, WorksForPointerToNonConstChar) { |
michael@0 | 1421 | char s[] = "hello"; |
michael@0 | 1422 | char* p = s; |
michael@0 | 1423 | EXPECT_PRINT_TO_STRING_(p, "\"hello\""); |
michael@0 | 1424 | } |
michael@0 | 1425 | |
michael@0 | 1426 | TEST(PrintToStringTest, EscapesForPointerToConstChar) { |
michael@0 | 1427 | const char* p = "hello\n"; |
michael@0 | 1428 | EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\""); |
michael@0 | 1429 | } |
michael@0 | 1430 | |
michael@0 | 1431 | TEST(PrintToStringTest, EscapesForPointerToNonConstChar) { |
michael@0 | 1432 | char s[] = "hello\1"; |
michael@0 | 1433 | char* p = s; |
michael@0 | 1434 | EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\""); |
michael@0 | 1435 | } |
michael@0 | 1436 | |
michael@0 | 1437 | TEST(PrintToStringTest, WorksForArray) { |
michael@0 | 1438 | int n[3] = { 1, 2, 3 }; |
michael@0 | 1439 | EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }"); |
michael@0 | 1440 | } |
michael@0 | 1441 | |
michael@0 | 1442 | TEST(PrintToStringTest, WorksForCharArray) { |
michael@0 | 1443 | char s[] = "hello"; |
michael@0 | 1444 | EXPECT_PRINT_TO_STRING_(s, "\"hello\""); |
michael@0 | 1445 | } |
michael@0 | 1446 | |
michael@0 | 1447 | TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) { |
michael@0 | 1448 | const char str_with_nul[] = "hello\0 world"; |
michael@0 | 1449 | EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\""); |
michael@0 | 1450 | |
michael@0 | 1451 | char mutable_str_with_nul[] = "hello\0 world"; |
michael@0 | 1452 | EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\""); |
michael@0 | 1453 | } |
michael@0 | 1454 | |
michael@0 | 1455 | #undef EXPECT_PRINT_TO_STRING_ |
michael@0 | 1456 | |
michael@0 | 1457 | TEST(UniversalTersePrintTest, WorksForNonReference) { |
michael@0 | 1458 | ::std::stringstream ss; |
michael@0 | 1459 | UniversalTersePrint(123, &ss); |
michael@0 | 1460 | EXPECT_EQ("123", ss.str()); |
michael@0 | 1461 | } |
michael@0 | 1462 | |
michael@0 | 1463 | TEST(UniversalTersePrintTest, WorksForReference) { |
michael@0 | 1464 | const int& n = 123; |
michael@0 | 1465 | ::std::stringstream ss; |
michael@0 | 1466 | UniversalTersePrint(n, &ss); |
michael@0 | 1467 | EXPECT_EQ("123", ss.str()); |
michael@0 | 1468 | } |
michael@0 | 1469 | |
michael@0 | 1470 | TEST(UniversalTersePrintTest, WorksForCString) { |
michael@0 | 1471 | const char* s1 = "abc"; |
michael@0 | 1472 | ::std::stringstream ss1; |
michael@0 | 1473 | UniversalTersePrint(s1, &ss1); |
michael@0 | 1474 | EXPECT_EQ("\"abc\"", ss1.str()); |
michael@0 | 1475 | |
michael@0 | 1476 | char* s2 = const_cast<char*>(s1); |
michael@0 | 1477 | ::std::stringstream ss2; |
michael@0 | 1478 | UniversalTersePrint(s2, &ss2); |
michael@0 | 1479 | EXPECT_EQ("\"abc\"", ss2.str()); |
michael@0 | 1480 | |
michael@0 | 1481 | const char* s3 = NULL; |
michael@0 | 1482 | ::std::stringstream ss3; |
michael@0 | 1483 | UniversalTersePrint(s3, &ss3); |
michael@0 | 1484 | EXPECT_EQ("NULL", ss3.str()); |
michael@0 | 1485 | } |
michael@0 | 1486 | |
michael@0 | 1487 | TEST(UniversalPrintTest, WorksForNonReference) { |
michael@0 | 1488 | ::std::stringstream ss; |
michael@0 | 1489 | UniversalPrint(123, &ss); |
michael@0 | 1490 | EXPECT_EQ("123", ss.str()); |
michael@0 | 1491 | } |
michael@0 | 1492 | |
michael@0 | 1493 | TEST(UniversalPrintTest, WorksForReference) { |
michael@0 | 1494 | const int& n = 123; |
michael@0 | 1495 | ::std::stringstream ss; |
michael@0 | 1496 | UniversalPrint(n, &ss); |
michael@0 | 1497 | EXPECT_EQ("123", ss.str()); |
michael@0 | 1498 | } |
michael@0 | 1499 | |
michael@0 | 1500 | TEST(UniversalPrintTest, WorksForCString) { |
michael@0 | 1501 | const char* s1 = "abc"; |
michael@0 | 1502 | ::std::stringstream ss1; |
michael@0 | 1503 | UniversalPrint(s1, &ss1); |
michael@0 | 1504 | EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str())); |
michael@0 | 1505 | |
michael@0 | 1506 | char* s2 = const_cast<char*>(s1); |
michael@0 | 1507 | ::std::stringstream ss2; |
michael@0 | 1508 | UniversalPrint(s2, &ss2); |
michael@0 | 1509 | EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str())); |
michael@0 | 1510 | |
michael@0 | 1511 | const char* s3 = NULL; |
michael@0 | 1512 | ::std::stringstream ss3; |
michael@0 | 1513 | UniversalPrint(s3, &ss3); |
michael@0 | 1514 | EXPECT_EQ("NULL", ss3.str()); |
michael@0 | 1515 | } |
michael@0 | 1516 | |
michael@0 | 1517 | TEST(UniversalPrintTest, WorksForCharArray) { |
michael@0 | 1518 | const char str[] = "\"Line\0 1\"\nLine 2"; |
michael@0 | 1519 | ::std::stringstream ss1; |
michael@0 | 1520 | UniversalPrint(str, &ss1); |
michael@0 | 1521 | EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str()); |
michael@0 | 1522 | |
michael@0 | 1523 | const char mutable_str[] = "\"Line\0 1\"\nLine 2"; |
michael@0 | 1524 | ::std::stringstream ss2; |
michael@0 | 1525 | UniversalPrint(mutable_str, &ss2); |
michael@0 | 1526 | EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str()); |
michael@0 | 1527 | } |
michael@0 | 1528 | |
michael@0 | 1529 | #if GTEST_HAS_TR1_TUPLE |
michael@0 | 1530 | |
michael@0 | 1531 | TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) { |
michael@0 | 1532 | Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple()); |
michael@0 | 1533 | EXPECT_EQ(0u, result.size()); |
michael@0 | 1534 | } |
michael@0 | 1535 | |
michael@0 | 1536 | TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) { |
michael@0 | 1537 | Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1)); |
michael@0 | 1538 | ASSERT_EQ(1u, result.size()); |
michael@0 | 1539 | EXPECT_EQ("1", result[0]); |
michael@0 | 1540 | } |
michael@0 | 1541 | |
michael@0 | 1542 | TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) { |
michael@0 | 1543 | Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a')); |
michael@0 | 1544 | ASSERT_EQ(2u, result.size()); |
michael@0 | 1545 | EXPECT_EQ("1", result[0]); |
michael@0 | 1546 | EXPECT_EQ("'a' (97, 0x61)", result[1]); |
michael@0 | 1547 | } |
michael@0 | 1548 | |
michael@0 | 1549 | TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) { |
michael@0 | 1550 | const int n = 1; |
michael@0 | 1551 | Strings result = UniversalTersePrintTupleFieldsToStrings( |
michael@0 | 1552 | tuple<const int&, const char*>(n, "a")); |
michael@0 | 1553 | ASSERT_EQ(2u, result.size()); |
michael@0 | 1554 | EXPECT_EQ("1", result[0]); |
michael@0 | 1555 | EXPECT_EQ("\"a\"", result[1]); |
michael@0 | 1556 | } |
michael@0 | 1557 | |
michael@0 | 1558 | #endif // GTEST_HAS_TR1_TUPLE |
michael@0 | 1559 | |
michael@0 | 1560 | } // namespace gtest_printers_test |
michael@0 | 1561 | } // namespace testing |