00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "gtest/gtest-printers.h"
00037
00038 #include <ctype.h>
00039 #include <limits.h>
00040 #include <string.h>
00041 #include <algorithm>
00042 #include <deque>
00043 #include <list>
00044 #include <map>
00045 #include <set>
00046 #include <sstream>
00047 #include <string>
00048 #include <utility>
00049 #include <vector>
00050
00051 #include "gtest/gtest.h"
00052
00053
00054 #if _MSC_VER
00055 # define GTEST_HAS_HASH_MAP_ 1 // Indicates that hash_map is available.
00056 # include <hash_map>
00057 # define GTEST_HAS_HASH_SET_ 1 // Indicates that hash_set is available.
00058 # include <hash_set>
00059 #endif // GTEST_OS_WINDOWS
00060
00061
00062
00063
00064 enum AnonymousEnum {
00065 kAE1 = -1,
00066 kAE2 = 1
00067 };
00068
00069
00070 enum EnumWithoutPrinter {
00071 kEWP1 = -2,
00072 kEWP2 = 42
00073 };
00074
00075
00076 enum EnumWithStreaming {
00077 kEWS1 = 10
00078 };
00079
00080 std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) {
00081 return os << (e == kEWS1 ? "kEWS1" : "invalid");
00082 }
00083
00084
00085 enum EnumWithPrintTo {
00086 kEWPT1 = 1
00087 };
00088
00089 void PrintTo(EnumWithPrintTo e, std::ostream* os) {
00090 *os << (e == kEWPT1 ? "kEWPT1" : "invalid");
00091 }
00092
00093
00094 class BiggestIntConvertible {
00095 public:
00096 operator ::testing::internal::BiggestInt() const { return 42; }
00097 };
00098
00099
00100 template <typename T>
00101 class UnprintableTemplateInGlobal {
00102 public:
00103 UnprintableTemplateInGlobal() : value_() {}
00104 private:
00105 T value_;
00106 };
00107
00108
00109 class StreamableInGlobal {
00110 public:
00111 virtual ~StreamableInGlobal() {}
00112 };
00113
00114 inline void operator<<(::std::ostream& os, const StreamableInGlobal& ) {
00115 os << "StreamableInGlobal";
00116 }
00117
00118 void operator<<(::std::ostream& os, const StreamableInGlobal* ) {
00119 os << "StreamableInGlobal*";
00120 }
00121
00122 namespace foo {
00123
00124
00125 class UnprintableInFoo {
00126 public:
00127 UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); }
00128 private:
00129 char xy_[8];
00130 double z_;
00131 };
00132
00133
00134 struct PrintableViaPrintTo {
00135 PrintableViaPrintTo() : value() {}
00136 int value;
00137 };
00138
00139 void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
00140 *os << "PrintableViaPrintTo: " << x.value;
00141 }
00142
00143
00144 struct PointerPrintable {
00145 };
00146
00147 ::std::ostream& operator<<(::std::ostream& os,
00148 const PointerPrintable* ) {
00149 return os << "PointerPrintable*";
00150 }
00151
00152
00153 template <typename T>
00154 class PrintableViaPrintToTemplate {
00155 public:
00156 explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
00157
00158 const T& value() const { return value_; }
00159 private:
00160 T value_;
00161 };
00162
00163 template <typename T>
00164 void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
00165 *os << "PrintableViaPrintToTemplate: " << x.value();
00166 }
00167
00168
00169 template <typename T>
00170 class StreamableTemplateInFoo {
00171 public:
00172 StreamableTemplateInFoo() : value_() {}
00173
00174 const T& value() const { return value_; }
00175 private:
00176 T value_;
00177 };
00178
00179 template <typename T>
00180 inline ::std::ostream& operator<<(::std::ostream& os,
00181 const StreamableTemplateInFoo<T>& x) {
00182 return os << "StreamableTemplateInFoo: " << x.value();
00183 }
00184
00185 }
00186
00187 namespace testing {
00188 namespace gtest_printers_test {
00189
00190 using ::std::deque;
00191 using ::std::list;
00192 using ::std::make_pair;
00193 using ::std::map;
00194 using ::std::multimap;
00195 using ::std::multiset;
00196 using ::std::pair;
00197 using ::std::set;
00198 using ::std::vector;
00199 using ::testing::PrintToString;
00200 using ::testing::internal::FormatForComparisonFailureMessage;
00201 using ::testing::internal::ImplicitCast_;
00202 using ::testing::internal::NativeArray;
00203 using ::testing::internal::RE;
00204 using ::testing::internal::Strings;
00205 using ::testing::internal::UniversalPrint;
00206 using ::testing::internal::UniversalPrinter;
00207 using ::testing::internal::UniversalTersePrint;
00208 using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
00209 using ::testing::internal::kReference;
00210 using ::testing::internal::string;
00211
00212 #if GTEST_HAS_TR1_TUPLE
00213 using ::std::tr1::make_tuple;
00214 using ::std::tr1::tuple;
00215 #endif
00216
00217
00218
00219
00220 #ifdef _STLP_HASH_MAP // We got <hash_map> from STLport.
00221 using ::std::hash_map;
00222 using ::std::hash_set;
00223 using ::std::hash_multimap;
00224 using ::std::hash_multiset;
00225 #elif _MSC_VER
00226 using ::stdext::hash_map;
00227 using ::stdext::hash_set;
00228 using ::stdext::hash_multimap;
00229 using ::stdext::hash_multiset;
00230 #endif
00231
00232
00233
00234 template <typename T>
00235 string Print(const T& value) {
00236 ::std::stringstream ss;
00237 UniversalPrinter<T>::Print(value, &ss);
00238 return ss.str();
00239 }
00240
00241
00242
00243
00244 template <typename T>
00245 string PrintByRef(const T& value) {
00246 ::std::stringstream ss;
00247 UniversalPrinter<T&>::Print(value, &ss);
00248 return ss.str();
00249 }
00250
00251
00252
00253 TEST(PrintEnumTest, AnonymousEnum) {
00254 EXPECT_EQ("-1", Print(kAE1));
00255 EXPECT_EQ("1", Print(kAE2));
00256 }
00257
00258 TEST(PrintEnumTest, EnumWithoutPrinter) {
00259 EXPECT_EQ("-2", Print(kEWP1));
00260 EXPECT_EQ("42", Print(kEWP2));
00261 }
00262
00263 TEST(PrintEnumTest, EnumWithStreaming) {
00264 EXPECT_EQ("kEWS1", Print(kEWS1));
00265 EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
00266 }
00267
00268 TEST(PrintEnumTest, EnumWithPrintTo) {
00269 EXPECT_EQ("kEWPT1", Print(kEWPT1));
00270 EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
00271 }
00272
00273
00274
00275 TEST(PrintClassTest, BiggestIntConvertible) {
00276 EXPECT_EQ("42", Print(BiggestIntConvertible()));
00277 }
00278
00279
00280
00281
00282 TEST(PrintCharTest, PlainChar) {
00283 EXPECT_EQ("'\\0'", Print('\0'));
00284 EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
00285 EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
00286 EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
00287 EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
00288 EXPECT_EQ("'\\a' (7)", Print('\a'));
00289 EXPECT_EQ("'\\b' (8)", Print('\b'));
00290 EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
00291 EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
00292 EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
00293 EXPECT_EQ("'\\t' (9)", Print('\t'));
00294 EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
00295 EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
00296 EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
00297 EXPECT_EQ("' ' (32, 0x20)", Print(' '));
00298 EXPECT_EQ("'a' (97, 0x61)", Print('a'));
00299 }
00300
00301
00302 TEST(PrintCharTest, SignedChar) {
00303 EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
00304 EXPECT_EQ("'\\xCE' (-50)",
00305 Print(static_cast<signed char>(-50)));
00306 }
00307
00308
00309 TEST(PrintCharTest, UnsignedChar) {
00310 EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
00311 EXPECT_EQ("'b' (98, 0x62)",
00312 Print(static_cast<unsigned char>('b')));
00313 }
00314
00315
00316
00317
00318 TEST(PrintBuiltInTypeTest, Bool) {
00319 EXPECT_EQ("false", Print(false));
00320 EXPECT_EQ("true", Print(true));
00321 }
00322
00323
00324 TEST(PrintBuiltInTypeTest, Wchar_t) {
00325 EXPECT_EQ("L'\\0'", Print(L'\0'));
00326 EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
00327 EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
00328 EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
00329 EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
00330 EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
00331 EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
00332 EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
00333 EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
00334 EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
00335 EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
00336 EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
00337 EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
00338 EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
00339 EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
00340 EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
00341 EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
00342 EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
00343 }
00344
00345
00346 TEST(PrintTypeSizeTest, Wchar_t) {
00347 EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
00348 }
00349
00350
00351 TEST(PrintBuiltInTypeTest, Integer) {
00352 EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255)));
00353 EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128)));
00354 EXPECT_EQ("65535", Print(USHRT_MAX));
00355 EXPECT_EQ("-32768", Print(SHRT_MIN));
00356 EXPECT_EQ("4294967295", Print(UINT_MAX));
00357 EXPECT_EQ("-2147483648", Print(INT_MIN));
00358 EXPECT_EQ("18446744073709551615",
00359 Print(static_cast<testing::internal::UInt64>(-1)));
00360 EXPECT_EQ("-9223372036854775808",
00361 Print(static_cast<testing::internal::Int64>(1) << 63));
00362 }
00363
00364
00365 TEST(PrintBuiltInTypeTest, Size_t) {
00366 EXPECT_EQ("1", Print(sizeof('a')));
00367 #if !GTEST_OS_WINDOWS
00368
00369 EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2)));
00370 #endif // !GTEST_OS_WINDOWS
00371 }
00372
00373
00374 TEST(PrintBuiltInTypeTest, FloatingPoints) {
00375 EXPECT_EQ("1.5", Print(1.5f));
00376 EXPECT_EQ("-2.5", Print(-2.5));
00377 }
00378
00379
00380
00381
00382 static string PrintPointer(const void *p) {
00383 ::std::stringstream expected_result_stream;
00384 expected_result_stream << p;
00385 return expected_result_stream.str();
00386 }
00387
00388
00389
00390
00391 TEST(PrintCStringTest, Const) {
00392 const char* p = "World";
00393 EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
00394 }
00395
00396
00397 TEST(PrintCStringTest, NonConst) {
00398 char p[] = "Hi";
00399 EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
00400 Print(static_cast<char*>(p)));
00401 }
00402
00403
00404 TEST(PrintCStringTest, Null) {
00405 const char* p = NULL;
00406 EXPECT_EQ("NULL", Print(p));
00407 }
00408
00409
00410 TEST(PrintCStringTest, EscapesProperly) {
00411 const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
00412 EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
00413 "\\n\\r\\t\\v\\x7F\\xFF a\"",
00414 Print(p));
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
00426
00427
00428 TEST(PrintWideCStringTest, Const) {
00429 const wchar_t* p = L"World";
00430 EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
00431 }
00432
00433
00434 TEST(PrintWideCStringTest, NonConst) {
00435 wchar_t p[] = L"Hi";
00436 EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
00437 Print(static_cast<wchar_t*>(p)));
00438 }
00439
00440
00441 TEST(PrintWideCStringTest, Null) {
00442 const wchar_t* p = NULL;
00443 EXPECT_EQ("NULL", Print(p));
00444 }
00445
00446
00447 TEST(PrintWideCStringTest, EscapesProperly) {
00448 const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r',
00449 '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
00450 EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f"
00451 "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
00452 Print(static_cast<const wchar_t*>(s)));
00453 }
00454 #endif // native wchar_t
00455
00456
00457
00458
00459 TEST(PrintCharPointerTest, SignedChar) {
00460 signed char* p = reinterpret_cast<signed char*>(0x1234);
00461 EXPECT_EQ(PrintPointer(p), Print(p));
00462 p = NULL;
00463 EXPECT_EQ("NULL", Print(p));
00464 }
00465
00466
00467 TEST(PrintCharPointerTest, ConstSignedChar) {
00468 signed char* p = reinterpret_cast<signed char*>(0x1234);
00469 EXPECT_EQ(PrintPointer(p), Print(p));
00470 p = NULL;
00471 EXPECT_EQ("NULL", Print(p));
00472 }
00473
00474
00475 TEST(PrintCharPointerTest, UnsignedChar) {
00476 unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
00477 EXPECT_EQ(PrintPointer(p), Print(p));
00478 p = NULL;
00479 EXPECT_EQ("NULL", Print(p));
00480 }
00481
00482
00483 TEST(PrintCharPointerTest, ConstUnsignedChar) {
00484 const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
00485 EXPECT_EQ(PrintPointer(p), Print(p));
00486 p = NULL;
00487 EXPECT_EQ("NULL", Print(p));
00488 }
00489
00490
00491
00492
00493 TEST(PrintPointerToBuiltInTypeTest, Bool) {
00494 bool* p = reinterpret_cast<bool*>(0xABCD);
00495 EXPECT_EQ(PrintPointer(p), Print(p));
00496 p = NULL;
00497 EXPECT_EQ("NULL", Print(p));
00498 }
00499
00500
00501 TEST(PrintPointerToBuiltInTypeTest, Void) {
00502 void* p = reinterpret_cast<void*>(0xABCD);
00503 EXPECT_EQ(PrintPointer(p), Print(p));
00504 p = NULL;
00505 EXPECT_EQ("NULL", Print(p));
00506 }
00507
00508
00509 TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
00510 const void* p = reinterpret_cast<const void*>(0xABCD);
00511 EXPECT_EQ(PrintPointer(p), Print(p));
00512 p = NULL;
00513 EXPECT_EQ("NULL", Print(p));
00514 }
00515
00516
00517 TEST(PrintPointerToPointerTest, IntPointerPointer) {
00518 int** p = reinterpret_cast<int**>(0xABCD);
00519 EXPECT_EQ(PrintPointer(p), Print(p));
00520 p = NULL;
00521 EXPECT_EQ("NULL", Print(p));
00522 }
00523
00524
00525
00526 void MyFunction(int ) {}
00527
00528 TEST(PrintPointerTest, NonMemberFunctionPointer) {
00529
00530
00531
00532
00533 EXPECT_EQ(
00534 PrintPointer(reinterpret_cast<const void*>(
00535 reinterpret_cast<internal::BiggestInt>(&MyFunction))),
00536 Print(&MyFunction));
00537 int (*p)(bool) = NULL;
00538 EXPECT_EQ("NULL", Print(p));
00539 }
00540
00541
00542
00543 template <typename StringType>
00544 AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
00545 if (str.find(prefix, 0) == 0)
00546 return AssertionSuccess();
00547
00548 const bool is_wide_string = sizeof(prefix[0]) > 1;
00549 const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
00550 return AssertionFailure()
00551 << begin_string_quote << prefix << "\" is not a prefix of "
00552 << begin_string_quote << str << "\"\n";
00553 }
00554
00555
00556
00557
00558
00559
00560 struct Foo {
00561 public:
00562 virtual ~Foo() {}
00563 int MyMethod(char x) { return x + 1; }
00564 virtual char MyVirtualMethod(int ) { return 'a'; }
00565
00566 int value;
00567 };
00568
00569 TEST(PrintPointerTest, MemberVariablePointer) {
00570 EXPECT_TRUE(HasPrefix(Print(&Foo::value),
00571 Print(sizeof(&Foo::value)) + "-byte object "));
00572 int (Foo::*p) = NULL;
00573 EXPECT_TRUE(HasPrefix(Print(p),
00574 Print(sizeof(p)) + "-byte object "));
00575 }
00576
00577
00578
00579
00580
00581 TEST(PrintPointerTest, MemberFunctionPointer) {
00582 EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod),
00583 Print(sizeof(&Foo::MyMethod)) + "-byte object "));
00584 EXPECT_TRUE(
00585 HasPrefix(Print(&Foo::MyVirtualMethod),
00586 Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
00587 int (Foo::*p)(char) = NULL;
00588 EXPECT_TRUE(HasPrefix(Print(p),
00589 Print(sizeof(p)) + "-byte object "));
00590 }
00591
00592
00593
00594
00595
00596 template <typename T, size_t N>
00597 string PrintArrayHelper(T (&a)[N]) {
00598 return Print(a);
00599 }
00600
00601
00602 TEST(PrintArrayTest, OneDimensionalArray) {
00603 int a[5] = { 1, 2, 3, 4, 5 };
00604 EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
00605 }
00606
00607
00608 TEST(PrintArrayTest, TwoDimensionalArray) {
00609 int a[2][5] = {
00610 { 1, 2, 3, 4, 5 },
00611 { 6, 7, 8, 9, 0 }
00612 };
00613 EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
00614 }
00615
00616
00617 TEST(PrintArrayTest, ConstArray) {
00618 const bool a[1] = { false };
00619 EXPECT_EQ("{ false }", PrintArrayHelper(a));
00620 }
00621
00622
00623 TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
00624
00625 char a[] = { 'H', '\0', 'i' };
00626 EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
00627 }
00628
00629
00630 TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) {
00631 const char a[] = "\0Hi";
00632 EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a));
00633 }
00634
00635
00636 TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
00637
00638 const wchar_t a[] = { L'H', L'\0', L'i' };
00639 EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
00640 }
00641
00642
00643 TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
00644 const wchar_t a[] = L"\0Hi";
00645 EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a));
00646 }
00647
00648
00649 TEST(PrintArrayTest, ObjectArray) {
00650 string a[3] = { "Hi", "Hello", "Ni hao" };
00651 EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
00652 }
00653
00654
00655 TEST(PrintArrayTest, BigArray) {
00656 int a[100] = { 1, 2, 3 };
00657 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
00658 PrintArrayHelper(a));
00659 }
00660
00661
00662
00663 #if GTEST_HAS_GLOBAL_STRING
00664
00665 TEST(PrintStringTest, StringInGlobalNamespace) {
00666 const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
00667 const ::string str(s, sizeof(s));
00668 EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
00669 Print(str));
00670 }
00671 #endif // GTEST_HAS_GLOBAL_STRING
00672
00673
00674 TEST(PrintStringTest, StringInStdNamespace) {
00675 const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
00676 const ::std::string str(s, sizeof(s));
00677 EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
00678 Print(str));
00679 }
00680
00681 TEST(PrintStringTest, StringAmbiguousHex) {
00682
00683
00684
00685
00686 EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
00687
00688 EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
00689
00690 EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
00691
00692 EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
00693 }
00694
00695
00696
00697 #if GTEST_HAS_GLOBAL_WSTRING
00698
00699 TEST(PrintWideStringTest, StringInGlobalNamespace) {
00700 const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
00701 const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
00702 EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
00703 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
00704 Print(str));
00705 }
00706 #endif // GTEST_HAS_GLOBAL_WSTRING
00707
00708 #if GTEST_HAS_STD_WSTRING
00709
00710 TEST(PrintWideStringTest, StringInStdNamespace) {
00711 const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
00712 const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
00713 EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
00714 "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
00715 Print(str));
00716 }
00717
00718 TEST(PrintWideStringTest, StringAmbiguousHex) {
00719
00720 EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3")));
00721 EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
00722 Print(::std::wstring(L"mm\x6" L"bananas")));
00723 EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
00724 Print(::std::wstring(L"NOM\x6" L"BANANA")));
00725 EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
00726 }
00727 #endif // GTEST_HAS_STD_WSTRING
00728
00729
00730
00731
00732
00733
00734
00735 class AllowsGenericStreaming {};
00736
00737 template <typename Char, typename CharTraits>
00738 std::basic_ostream<Char, CharTraits>& operator<<(
00739 std::basic_ostream<Char, CharTraits>& os,
00740 const AllowsGenericStreaming& ) {
00741 return os << "AllowsGenericStreaming";
00742 }
00743
00744 TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
00745 AllowsGenericStreaming a;
00746 EXPECT_EQ("AllowsGenericStreaming", Print(a));
00747 }
00748
00749
00750
00751 template <typename T>
00752 class AllowsGenericStreamingTemplate {};
00753
00754 template <typename Char, typename CharTraits, typename T>
00755 std::basic_ostream<Char, CharTraits>& operator<<(
00756 std::basic_ostream<Char, CharTraits>& os,
00757 const AllowsGenericStreamingTemplate<T>& ) {
00758 return os << "AllowsGenericStreamingTemplate";
00759 }
00760
00761 TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
00762 AllowsGenericStreamingTemplate<int> a;
00763 EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
00764 }
00765
00766
00767
00768
00769 template <typename T>
00770 class AllowsGenericStreamingAndImplicitConversionTemplate {
00771 public:
00772 operator bool() const { return false; }
00773 };
00774
00775 template <typename Char, typename CharTraits, typename T>
00776 std::basic_ostream<Char, CharTraits>& operator<<(
00777 std::basic_ostream<Char, CharTraits>& os,
00778 const AllowsGenericStreamingAndImplicitConversionTemplate<T>& ) {
00779 return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
00780 }
00781
00782 TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
00783 AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
00784 EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
00785 }
00786
00787 #if GTEST_HAS_STRING_PIECE_
00788
00789
00790
00791 TEST(PrintStringPieceTest, SimpleStringPiece) {
00792 const StringPiece sp = "Hello";
00793 EXPECT_EQ("\"Hello\"", Print(sp));
00794 }
00795
00796 TEST(PrintStringPieceTest, UnprintableCharacters) {
00797 const char str[] = "NUL (\0) and \r\t";
00798 const StringPiece sp(str, sizeof(str) - 1);
00799 EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
00800 }
00801
00802 #endif // GTEST_HAS_STRING_PIECE_
00803
00804
00805
00806 TEST(PrintStlContainerTest, EmptyDeque) {
00807 deque<char> empty;
00808 EXPECT_EQ("{}", Print(empty));
00809 }
00810
00811 TEST(PrintStlContainerTest, NonEmptyDeque) {
00812 deque<int> non_empty;
00813 non_empty.push_back(1);
00814 non_empty.push_back(3);
00815 EXPECT_EQ("{ 1, 3 }", Print(non_empty));
00816 }
00817
00818 #if GTEST_HAS_HASH_MAP_
00819
00820 TEST(PrintStlContainerTest, OneElementHashMap) {
00821 hash_map<int, char> map1;
00822 map1[1] = 'a';
00823 EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1));
00824 }
00825
00826 TEST(PrintStlContainerTest, HashMultiMap) {
00827 hash_multimap<int, bool> map1;
00828 map1.insert(make_pair(5, true));
00829 map1.insert(make_pair(5, false));
00830
00831
00832 const string result = Print(map1);
00833 EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
00834 result == "{ (5, false), (5, true) }")
00835 << " where Print(map1) returns \"" << result << "\".";
00836 }
00837
00838 #endif // GTEST_HAS_HASH_MAP_
00839
00840 #if GTEST_HAS_HASH_SET_
00841
00842 TEST(PrintStlContainerTest, HashSet) {
00843 hash_set<string> set1;
00844 set1.insert("hello");
00845 EXPECT_EQ("{ \"hello\" }", Print(set1));
00846 }
00847
00848 TEST(PrintStlContainerTest, HashMultiSet) {
00849 const int kSize = 5;
00850 int a[kSize] = { 1, 1, 2, 5, 1 };
00851 hash_multiset<int> set1(a, a + kSize);
00852
00853
00854 const string result = Print(set1);
00855 const string expected_pattern = "{ d, d, d, d, d }";
00856
00857
00858
00859 ASSERT_EQ(expected_pattern.length(), result.length());
00860 std::vector<int> numbers;
00861 for (size_t i = 0; i != result.length(); i++) {
00862 if (expected_pattern[i] == 'd') {
00863 ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0);
00864 numbers.push_back(result[i] - '0');
00865 } else {
00866 EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
00867 << result;
00868 }
00869 }
00870
00871
00872 std::sort(numbers.begin(), numbers.end());
00873 std::sort(a, a + kSize);
00874 EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
00875 }
00876
00877 #endif // GTEST_HAS_HASH_SET_
00878
00879 TEST(PrintStlContainerTest, List) {
00880 const string a[] = {
00881 "hello",
00882 "world"
00883 };
00884 const list<string> strings(a, a + 2);
00885 EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
00886 }
00887
00888 TEST(PrintStlContainerTest, Map) {
00889 map<int, bool> map1;
00890 map1[1] = true;
00891 map1[5] = false;
00892 map1[3] = true;
00893 EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
00894 }
00895
00896 TEST(PrintStlContainerTest, MultiMap) {
00897 multimap<bool, int> map1;
00898
00899
00900
00901
00902
00903
00904 map1.insert(pair<const bool, int>(true, 0));
00905 map1.insert(pair<const bool, int>(true, 1));
00906 map1.insert(pair<const bool, int>(false, 2));
00907 EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
00908 }
00909
00910 TEST(PrintStlContainerTest, Set) {
00911 const unsigned int a[] = { 3, 0, 5 };
00912 set<unsigned int> set1(a, a + 3);
00913 EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
00914 }
00915
00916 TEST(PrintStlContainerTest, MultiSet) {
00917 const int a[] = { 1, 1, 2, 5, 1 };
00918 multiset<int> set1(a, a + 5);
00919 EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
00920 }
00921
00922 TEST(PrintStlContainerTest, Pair) {
00923 pair<const bool, int> p(true, 5);
00924 EXPECT_EQ("(true, 5)", Print(p));
00925 }
00926
00927 TEST(PrintStlContainerTest, Vector) {
00928 vector<int> v;
00929 v.push_back(1);
00930 v.push_back(2);
00931 EXPECT_EQ("{ 1, 2 }", Print(v));
00932 }
00933
00934 TEST(PrintStlContainerTest, LongSequence) {
00935 const int a[100] = { 1, 2, 3 };
00936 const vector<int> v(a, a + 100);
00937 EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
00938 "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
00939 }
00940
00941 TEST(PrintStlContainerTest, NestedContainer) {
00942 const int a1[] = { 1, 2 };
00943 const int a2[] = { 3, 4, 5 };
00944 const list<int> l1(a1, a1 + 2);
00945 const list<int> l2(a2, a2 + 3);
00946
00947 vector<list<int> > v;
00948 v.push_back(l1);
00949 v.push_back(l2);
00950 EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
00951 }
00952
00953 TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
00954 const int a[3] = { 1, 2, 3 };
00955 NativeArray<int> b(a, 3, kReference);
00956 EXPECT_EQ("{ 1, 2, 3 }", Print(b));
00957 }
00958
00959 TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
00960 const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
00961 NativeArray<int[3]> b(a, 2, kReference);
00962 EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
00963 }
00964
00965
00966
00967 struct iterator {
00968 char x;
00969 };
00970
00971 TEST(PrintStlContainerTest, Iterator) {
00972 iterator it = {};
00973 EXPECT_EQ("1-byte object <00>", Print(it));
00974 }
00975
00976
00977
00978 struct const_iterator {
00979 char x;
00980 };
00981
00982 TEST(PrintStlContainerTest, ConstIterator) {
00983 const_iterator it = {};
00984 EXPECT_EQ("1-byte object <00>", Print(it));
00985 }
00986
00987 #if GTEST_HAS_TR1_TUPLE
00988
00989
00990
00991 TEST(PrintTupleTest, VariousSizes) {
00992 tuple<> t0;
00993 EXPECT_EQ("()", Print(t0));
00994
00995 tuple<int> t1(5);
00996 EXPECT_EQ("(5)", Print(t1));
00997
00998 tuple<char, bool> t2('a', true);
00999 EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
01000
01001 tuple<bool, int, int> t3(false, 2, 3);
01002 EXPECT_EQ("(false, 2, 3)", Print(t3));
01003
01004 tuple<bool, int, int, int> t4(false, 2, 3, 4);
01005 EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
01006
01007 tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
01008 EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
01009
01010 tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
01011 EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
01012
01013 tuple<bool, int, int, int, bool, int, int> t7(false, 2, 3, 4, true, 6, 7);
01014 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
01015
01016 tuple<bool, int, int, int, bool, int, int, bool> t8(
01017 false, 2, 3, 4, true, 6, 7, true);
01018 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
01019
01020 tuple<bool, int, int, int, bool, int, int, bool, int> t9(
01021 false, 2, 3, 4, true, 6, 7, true, 9);
01022 EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
01023
01024 const char* const str = "8";
01025
01026
01027 tuple<bool, char, short, testing::internal::Int32,
01028 testing::internal::Int64, float, double, const char*, void*, string>
01029 t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str,
01030 ImplicitCast_<void*>(NULL), "10");
01031 EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
01032 " pointing to \"8\", NULL, \"10\")",
01033 Print(t10));
01034 }
01035
01036
01037 TEST(PrintTupleTest, NestedTuple) {
01038 tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a');
01039 EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
01040 }
01041
01042 #endif // GTEST_HAS_TR1_TUPLE
01043
01044
01045
01046
01047 TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
01048 EXPECT_EQ("1-byte object <00>",
01049 Print(UnprintableTemplateInGlobal<char>()));
01050 }
01051
01052
01053 TEST(PrintUnprintableTypeTest, InUserNamespace) {
01054 EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
01055 Print(::foo::UnprintableInFoo()));
01056 }
01057
01058
01059
01060 struct Big {
01061 Big() { memset(array, 0, sizeof(array)); }
01062 char array[257];
01063 };
01064
01065 TEST(PrintUnpritableTypeTest, BigObject) {
01066 EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
01067 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
01068 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
01069 "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
01070 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
01071 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
01072 "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
01073 Print(Big()));
01074 }
01075
01076
01077
01078
01079 TEST(PrintStreamableTypeTest, InGlobalNamespace) {
01080 StreamableInGlobal x;
01081 EXPECT_EQ("StreamableInGlobal", Print(x));
01082 EXPECT_EQ("StreamableInGlobal*", Print(&x));
01083 }
01084
01085
01086 TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
01087 EXPECT_EQ("StreamableTemplateInFoo: 0",
01088 Print(::foo::StreamableTemplateInFoo<int>()));
01089 }
01090
01091
01092 TEST(PrintPrintableTypeTest, InUserNamespace) {
01093 EXPECT_EQ("PrintableViaPrintTo: 0",
01094 Print(::foo::PrintableViaPrintTo()));
01095 }
01096
01097
01098
01099 TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
01100 ::foo::PointerPrintable x;
01101 EXPECT_EQ("PointerPrintable*", Print(&x));
01102 }
01103
01104
01105 TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
01106 EXPECT_EQ("PrintableViaPrintToTemplate: 5",
01107 Print(::foo::PrintableViaPrintToTemplate<int>(5)));
01108 }
01109
01110 #if GTEST_HAS_PROTOBUF_
01111
01112
01113 TEST(PrintProtocolMessageTest, PrintsShortDebugString) {
01114 testing::internal::TestMessage msg;
01115 msg.set_member("yes");
01116 EXPECT_EQ("<member:\"yes\">", Print(msg));
01117 }
01118
01119
01120 TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) {
01121 testing::internal::FooMessage msg;
01122 msg.set_int_field(2);
01123 msg.set_string_field("hello");
01124 EXPECT_PRED2(RE::FullMatch, Print(msg),
01125 "<int_field:\\s*2\\s+string_field:\\s*\"hello\">");
01126 }
01127
01128
01129 TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) {
01130 testing::internal::FooMessage msg;
01131 msg.set_int_field(2);
01132 msg.set_string_field("hello");
01133 msg.add_names("peter");
01134 msg.add_names("paul");
01135 msg.add_names("mary");
01136 EXPECT_PRED2(RE::FullMatch, Print(msg),
01137 "<\n"
01138 "int_field:\\s*2\n"
01139 "string_field:\\s*\"hello\"\n"
01140 "names:\\s*\"peter\"\n"
01141 "names:\\s*\"paul\"\n"
01142 "names:\\s*\"mary\"\n"
01143 ">");
01144 }
01145
01146 #endif // GTEST_HAS_PROTOBUF_
01147
01148
01149
01150 TEST(PrintReferenceTest, PrintsAddressAndValue) {
01151 int n = 5;
01152 EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
01153
01154 int a[2][3] = {
01155 { 0, 1, 2 },
01156 { 3, 4, 5 }
01157 };
01158 EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
01159 PrintByRef(a));
01160
01161 const ::foo::UnprintableInFoo x;
01162 EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
01163 "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
01164 PrintByRef(x));
01165 }
01166
01167
01168
01169 TEST(PrintReferenceTest, HandlesFunctionPointer) {
01170 void (*fp)(int n) = &MyFunction;
01171 const string fp_pointer_string =
01172 PrintPointer(reinterpret_cast<const void*>(&fp));
01173
01174
01175
01176
01177 const string fp_string = PrintPointer(reinterpret_cast<const void*>(
01178 reinterpret_cast<internal::BiggestInt>(fp)));
01179 EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
01180 PrintByRef(fp));
01181 }
01182
01183
01184
01185 TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
01186 int (Foo::*p)(char ch) = &Foo::MyMethod;
01187 EXPECT_TRUE(HasPrefix(
01188 PrintByRef(p),
01189 "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
01190 Print(sizeof(p)) + "-byte object "));
01191
01192 char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
01193 EXPECT_TRUE(HasPrefix(
01194 PrintByRef(p2),
01195 "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
01196 Print(sizeof(p2)) + "-byte object "));
01197 }
01198
01199
01200
01201 TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
01202 int (Foo::*p) = &Foo::value;
01203 EXPECT_TRUE(HasPrefix(
01204 PrintByRef(p),
01205 "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
01206 }
01207
01208
01209
01210
01211
01212
01213 TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
01214 EXPECT_STREQ("123",
01215 FormatForComparisonFailureMessage(123, 124).c_str());
01216 }
01217
01218
01219 TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
01220 int n = 0;
01221 EXPECT_EQ(PrintPointer(&n),
01222 FormatForComparisonFailureMessage(&n, &n).c_str());
01223 }
01224
01225
01226 TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
01227
01228
01229 int n[] = { 1, 2, 3 };
01230 EXPECT_EQ(PrintPointer(n),
01231 FormatForComparisonFailureMessage(n, n).c_str());
01232 }
01233
01234
01235
01236
01237
01238
01239 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
01240
01241
01242
01243
01244
01245
01246 const char* s = "hello";
01247 EXPECT_EQ(PrintPointer(s),
01248 FormatForComparisonFailureMessage(s, s).c_str());
01249
01250
01251 char ch = 'a';
01252 EXPECT_EQ(PrintPointer(&ch),
01253 FormatForComparisonFailureMessage(&ch, &ch).c_str());
01254 }
01255
01256
01257 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
01258
01259
01260
01261
01262
01263
01264 const wchar_t* s = L"hello";
01265 EXPECT_EQ(PrintPointer(s),
01266 FormatForComparisonFailureMessage(s, s).c_str());
01267
01268
01269 wchar_t ch = L'a';
01270 EXPECT_EQ(PrintPointer(&ch),
01271 FormatForComparisonFailureMessage(&ch, &ch).c_str());
01272 }
01273
01274
01275
01276
01277 #if GTEST_HAS_GLOBAL_STRING
01278
01279 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) {
01280 const char* s = "hello \"world";
01281 EXPECT_STREQ("\"hello \\\"world\"",
01282 FormatForComparisonFailureMessage(s, ::string()).c_str());
01283
01284
01285 char str[] = "hi\1";
01286 char* p = str;
01287 EXPECT_STREQ("\"hi\\x1\"",
01288 FormatForComparisonFailureMessage(p, ::string()).c_str());
01289 }
01290 #endif
01291
01292
01293 TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
01294 const char* s = "hello \"world";
01295 EXPECT_STREQ("\"hello \\\"world\"",
01296 FormatForComparisonFailureMessage(s, ::std::string()).c_str());
01297
01298
01299 char str[] = "hi\1";
01300 char* p = str;
01301 EXPECT_STREQ("\"hi\\x1\"",
01302 FormatForComparisonFailureMessage(p, ::std::string()).c_str());
01303 }
01304
01305 #if GTEST_HAS_GLOBAL_WSTRING
01306
01307 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) {
01308 const wchar_t* s = L"hi \"world";
01309 EXPECT_STREQ("L\"hi \\\"world\"",
01310 FormatForComparisonFailureMessage(s, ::wstring()).c_str());
01311
01312
01313 wchar_t str[] = L"hi\1";
01314 wchar_t* p = str;
01315 EXPECT_STREQ("L\"hi\\x1\"",
01316 FormatForComparisonFailureMessage(p, ::wstring()).c_str());
01317 }
01318 #endif
01319
01320 #if GTEST_HAS_STD_WSTRING
01321
01322 TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
01323 const wchar_t* s = L"hi \"world";
01324 EXPECT_STREQ("L\"hi \\\"world\"",
01325 FormatForComparisonFailureMessage(s, ::std::wstring()).c_str());
01326
01327
01328 wchar_t str[] = L"hi\1";
01329 wchar_t* p = str;
01330 EXPECT_STREQ("L\"hi\\x1\"",
01331 FormatForComparisonFailureMessage(p, ::std::wstring()).c_str());
01332 }
01333 #endif
01334
01335
01336
01337
01338
01339
01340 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
01341 char str[] = "hi \"world\"";
01342 char* p = NULL;
01343 EXPECT_EQ(PrintPointer(str),
01344 FormatForComparisonFailureMessage(str, p).c_str());
01345 }
01346
01347
01348 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
01349 const char str[] = "hi \"world\"";
01350 EXPECT_EQ(PrintPointer(str),
01351 FormatForComparisonFailureMessage(str, str).c_str());
01352 }
01353
01354
01355 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
01356 wchar_t str[] = L"hi \"world\"";
01357 wchar_t* p = NULL;
01358 EXPECT_EQ(PrintPointer(str),
01359 FormatForComparisonFailureMessage(str, p).c_str());
01360 }
01361
01362
01363 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
01364 const wchar_t str[] = L"hi \"world\"";
01365 EXPECT_EQ(PrintPointer(str),
01366 FormatForComparisonFailureMessage(str, str).c_str());
01367 }
01368
01369
01370
01371
01372 #if GTEST_HAS_GLOBAL_STRING
01373
01374 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) {
01375 const char str[] = "hi \"w\0rld\"";
01376 EXPECT_STREQ("\"hi \\\"w\"",
01377
01378 FormatForComparisonFailureMessage(str, ::string()).c_str());
01379 }
01380 #endif
01381
01382
01383 TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
01384 const char str[] = "hi \"world\"";
01385 EXPECT_STREQ("\"hi \\\"world\\\"\"",
01386 FormatForComparisonFailureMessage(str, ::std::string()).c_str());
01387 }
01388
01389 #if GTEST_HAS_GLOBAL_WSTRING
01390
01391 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) {
01392 const wchar_t str[] = L"hi \"world\"";
01393 EXPECT_STREQ("L\"hi \\\"world\\\"\"",
01394 FormatForComparisonFailureMessage(str, ::wstring()).c_str());
01395 }
01396 #endif
01397
01398 #if GTEST_HAS_STD_WSTRING
01399
01400 TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
01401 const wchar_t str[] = L"hi \"w\0rld\"";
01402 EXPECT_STREQ(
01403 "L\"hi \\\"w\"",
01404
01405 FormatForComparisonFailureMessage(str, ::std::wstring()).c_str());
01406 }
01407 #endif
01408
01409
01410
01411
01412 #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
01413 EXPECT_TRUE(PrintToString(value) == (expected_string)) \
01414 << " where " #value " prints as " << (PrintToString(value))
01415
01416 TEST(PrintToStringTest, WorksForScalar) {
01417 EXPECT_PRINT_TO_STRING_(123, "123");
01418 }
01419
01420 TEST(PrintToStringTest, WorksForPointerToConstChar) {
01421 const char* p = "hello";
01422 EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
01423 }
01424
01425 TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
01426 char s[] = "hello";
01427 char* p = s;
01428 EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
01429 }
01430
01431 TEST(PrintToStringTest, EscapesForPointerToConstChar) {
01432 const char* p = "hello\n";
01433 EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\"");
01434 }
01435
01436 TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
01437 char s[] = "hello\1";
01438 char* p = s;
01439 EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\"");
01440 }
01441
01442 TEST(PrintToStringTest, WorksForArray) {
01443 int n[3] = { 1, 2, 3 };
01444 EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
01445 }
01446
01447 TEST(PrintToStringTest, WorksForCharArray) {
01448 char s[] = "hello";
01449 EXPECT_PRINT_TO_STRING_(s, "\"hello\"");
01450 }
01451
01452 TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
01453 const char str_with_nul[] = "hello\0 world";
01454 EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\"");
01455
01456 char mutable_str_with_nul[] = "hello\0 world";
01457 EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\"");
01458 }
01459
01460 #undef EXPECT_PRINT_TO_STRING_
01461
01462 TEST(UniversalTersePrintTest, WorksForNonReference) {
01463 ::std::stringstream ss;
01464 UniversalTersePrint(123, &ss);
01465 EXPECT_EQ("123", ss.str());
01466 }
01467
01468 TEST(UniversalTersePrintTest, WorksForReference) {
01469 const int& n = 123;
01470 ::std::stringstream ss;
01471 UniversalTersePrint(n, &ss);
01472 EXPECT_EQ("123", ss.str());
01473 }
01474
01475 TEST(UniversalTersePrintTest, WorksForCString) {
01476 const char* s1 = "abc";
01477 ::std::stringstream ss1;
01478 UniversalTersePrint(s1, &ss1);
01479 EXPECT_EQ("\"abc\"", ss1.str());
01480
01481 char* s2 = const_cast<char*>(s1);
01482 ::std::stringstream ss2;
01483 UniversalTersePrint(s2, &ss2);
01484 EXPECT_EQ("\"abc\"", ss2.str());
01485
01486 const char* s3 = NULL;
01487 ::std::stringstream ss3;
01488 UniversalTersePrint(s3, &ss3);
01489 EXPECT_EQ("NULL", ss3.str());
01490 }
01491
01492 TEST(UniversalPrintTest, WorksForNonReference) {
01493 ::std::stringstream ss;
01494 UniversalPrint(123, &ss);
01495 EXPECT_EQ("123", ss.str());
01496 }
01497
01498 TEST(UniversalPrintTest, WorksForReference) {
01499 const int& n = 123;
01500 ::std::stringstream ss;
01501 UniversalPrint(n, &ss);
01502 EXPECT_EQ("123", ss.str());
01503 }
01504
01505 TEST(UniversalPrintTest, WorksForCString) {
01506 const char* s1 = "abc";
01507 ::std::stringstream ss1;
01508 UniversalPrint(s1, &ss1);
01509 EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", string(ss1.str()));
01510
01511 char* s2 = const_cast<char*>(s1);
01512 ::std::stringstream ss2;
01513 UniversalPrint(s2, &ss2);
01514 EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", string(ss2.str()));
01515
01516 const char* s3 = NULL;
01517 ::std::stringstream ss3;
01518 UniversalPrint(s3, &ss3);
01519 EXPECT_EQ("NULL", ss3.str());
01520 }
01521
01522 TEST(UniversalPrintTest, WorksForCharArray) {
01523 const char str[] = "\"Line\0 1\"\nLine 2";
01524 ::std::stringstream ss1;
01525 UniversalPrint(str, &ss1);
01526 EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
01527
01528 const char mutable_str[] = "\"Line\0 1\"\nLine 2";
01529 ::std::stringstream ss2;
01530 UniversalPrint(mutable_str, &ss2);
01531 EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
01532 }
01533
01534 #if GTEST_HAS_TR1_TUPLE
01535
01536 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsEmptyTuple) {
01537 Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple());
01538 EXPECT_EQ(0u, result.size());
01539 }
01540
01541 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsOneTuple) {
01542 Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1));
01543 ASSERT_EQ(1u, result.size());
01544 EXPECT_EQ("1", result[0]);
01545 }
01546
01547 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTwoTuple) {
01548 Strings result = UniversalTersePrintTupleFieldsToStrings(make_tuple(1, 'a'));
01549 ASSERT_EQ(2u, result.size());
01550 EXPECT_EQ("1", result[0]);
01551 EXPECT_EQ("'a' (97, 0x61)", result[1]);
01552 }
01553
01554 TEST(UniversalTersePrintTupleFieldsToStringsTest, PrintsTersely) {
01555 const int n = 1;
01556 Strings result = UniversalTersePrintTupleFieldsToStrings(
01557 tuple<const int&, const char*>(n, "a"));
01558 ASSERT_EQ(2u, result.size());
01559 EXPECT_EQ("1", result[0]);
01560 EXPECT_EQ("\"a\"", result[1]);
01561 }
01562
01563 #endif // GTEST_HAS_TR1_TUPLE
01564
01565 }
01566 }