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
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 #ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
00096 #define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
00097
00098 #include <ostream>
00099 #include <sstream>
00100 #include <string>
00101 #include <utility>
00102 #include <vector>
00103 #include "gtest/internal/gtest-port.h"
00104 #include "gtest/internal/gtest-internal.h"
00105
00106 namespace testing {
00107
00108
00109
00110 namespace internal2 {
00111
00112
00113
00114 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
00115 size_t count,
00116 ::std::ostream* os);
00117
00118
00119
00120 enum TypeKind {
00121 kProtobuf,
00122 kConvertibleToInteger,
00123
00124 kOtherType
00125 };
00126
00127
00128
00129
00130
00131 template <typename T, TypeKind kTypeKind>
00132 class TypeWithoutFormatter {
00133 public:
00134
00135 static void PrintValue(const T& value, ::std::ostream* os) {
00136 PrintBytesInObjectTo(reinterpret_cast<const unsigned char*>(&value),
00137 sizeof(value), os);
00138 }
00139 };
00140
00141
00142
00143
00144 const size_t kProtobufOneLinerMaxLength = 50;
00145
00146 template <typename T>
00147 class TypeWithoutFormatter<T, kProtobuf> {
00148 public:
00149 static void PrintValue(const T& value, ::std::ostream* os) {
00150 const ::testing::internal::string short_str = value.ShortDebugString();
00151 const ::testing::internal::string pretty_str =
00152 short_str.length() <= kProtobufOneLinerMaxLength ?
00153 short_str : ("\n" + value.DebugString());
00154 *os << ("<" + pretty_str + ">");
00155 }
00156 };
00157
00158 template <typename T>
00159 class TypeWithoutFormatter<T, kConvertibleToInteger> {
00160 public:
00161
00162
00163
00164
00165
00166
00167
00168 static void PrintValue(const T& value, ::std::ostream* os) {
00169 const internal::BiggestInt kBigInt = value;
00170 *os << kBigInt;
00171 }
00172 };
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 template <typename Char, typename CharTraits, typename T>
00199 ::std::basic_ostream<Char, CharTraits>& operator<<(
00200 ::std::basic_ostream<Char, CharTraits>& os, const T& x) {
00201 TypeWithoutFormatter<T,
00202 (internal::IsAProtocolMessage<T>::value ? kProtobuf :
00203 internal::ImplicitlyConvertible<const T&, internal::BiggestInt>::value ?
00204 kConvertibleToInteger : kOtherType)>::PrintValue(x, &os);
00205 return os;
00206 }
00207
00208 }
00209 }
00210
00211
00212
00213 namespace testing_internal {
00214
00215
00216
00217 template <typename T>
00218 void DefaultPrintNonContainerTo(const T& value, ::std::ostream* os) {
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 using namespace ::testing::internal2;
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 *os << value;
00246 }
00247
00248 }
00249
00250 namespace testing {
00251 namespace internal {
00252
00253
00254
00255
00256
00257
00258
00259
00260 template <typename T>
00261 class UniversalPrinter;
00262
00263 template <typename T>
00264 void UniversalPrint(const T& value, ::std::ostream* os);
00265
00266
00267
00268 template <typename C>
00269 void DefaultPrintTo(IsContainer ,
00270 false_type ,
00271 const C& container, ::std::ostream* os) {
00272 const size_t kMaxCount = 32;
00273 *os << '{';
00274 size_t count = 0;
00275 for (typename C::const_iterator it = container.begin();
00276 it != container.end(); ++it, ++count) {
00277 if (count > 0) {
00278 *os << ',';
00279 if (count == kMaxCount) {
00280 *os << " ...";
00281 break;
00282 }
00283 }
00284 *os << ' ';
00285
00286
00287 internal::UniversalPrint(*it, os);
00288 }
00289
00290 if (count > 0) {
00291 *os << ' ';
00292 }
00293 *os << '}';
00294 }
00295
00296
00297
00298
00299
00300
00301
00302 template <typename T>
00303 void DefaultPrintTo(IsNotContainer ,
00304 true_type ,
00305 T* p, ::std::ostream* os) {
00306 if (p == NULL) {
00307 *os << "NULL";
00308 } else {
00309
00310
00311
00312
00313
00314 if (IsTrue(ImplicitlyConvertible<T*, const void*>::value)) {
00315
00316
00317
00318 *os << p;
00319 } else {
00320
00321
00322
00323
00324
00325
00326 *os << reinterpret_cast<const void*>(
00327 reinterpret_cast<internal::UInt64>(p));
00328 }
00329 }
00330 }
00331
00332
00333
00334 template <typename T>
00335 void DefaultPrintTo(IsNotContainer ,
00336 false_type ,
00337 const T& value, ::std::ostream* os) {
00338 ::testing_internal::DefaultPrintNonContainerTo(value, os);
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 template <typename T>
00353 void PrintTo(const T& value, ::std::ostream* os) {
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 DefaultPrintTo(IsContainerTest<T>(0), is_pointer<T>(), value, os);
00377 }
00378
00379
00380
00381
00382
00383
00384 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
00385 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
00386 inline void PrintTo(char c, ::std::ostream* os) {
00387
00388
00389
00390 PrintTo(static_cast<unsigned char>(c), os);
00391 }
00392
00393
00394 inline void PrintTo(bool x, ::std::ostream* os) {
00395 *os << (x ? "true" : "false");
00396 }
00397
00398
00399
00400
00401
00402
00403
00404
00405 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
00406
00407
00408 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
00409 inline void PrintTo(char* s, ::std::ostream* os) {
00410 PrintTo(ImplicitCast_<const char*>(s), os);
00411 }
00412
00413
00414
00415 inline void PrintTo(const signed char* s, ::std::ostream* os) {
00416 PrintTo(ImplicitCast_<const void*>(s), os);
00417 }
00418 inline void PrintTo(signed char* s, ::std::ostream* os) {
00419 PrintTo(ImplicitCast_<const void*>(s), os);
00420 }
00421 inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
00422 PrintTo(ImplicitCast_<const void*>(s), os);
00423 }
00424 inline void PrintTo(unsigned char* s, ::std::ostream* os) {
00425 PrintTo(ImplicitCast_<const void*>(s), os);
00426 }
00427
00428
00429
00430
00431
00432
00433 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
00434
00435 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
00436 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
00437 PrintTo(ImplicitCast_<const wchar_t*>(s), os);
00438 }
00439 #endif
00440
00441
00442
00443
00444
00445
00446 template <typename T>
00447 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
00448 UniversalPrint(a[0], os);
00449 for (size_t i = 1; i != count; i++) {
00450 *os << ", ";
00451 UniversalPrint(a[i], os);
00452 }
00453 }
00454
00455
00456 #if GTEST_HAS_GLOBAL_STRING
00457 GTEST_API_ void PrintStringTo(const ::string&s, ::std::ostream* os);
00458 inline void PrintTo(const ::string& s, ::std::ostream* os) {
00459 PrintStringTo(s, os);
00460 }
00461 #endif // GTEST_HAS_GLOBAL_STRING
00462
00463 GTEST_API_ void PrintStringTo(const ::std::string&s, ::std::ostream* os);
00464 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
00465 PrintStringTo(s, os);
00466 }
00467
00468
00469 #if GTEST_HAS_GLOBAL_WSTRING
00470 GTEST_API_ void PrintWideStringTo(const ::wstring&s, ::std::ostream* os);
00471 inline void PrintTo(const ::wstring& s, ::std::ostream* os) {
00472 PrintWideStringTo(s, os);
00473 }
00474 #endif // GTEST_HAS_GLOBAL_WSTRING
00475
00476 #if GTEST_HAS_STD_WSTRING
00477 GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os);
00478 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
00479 PrintWideStringTo(s, os);
00480 }
00481 #endif // GTEST_HAS_STD_WSTRING
00482
00483 #if GTEST_HAS_TR1_TUPLE
00484
00485
00486
00487
00488
00489 template <typename T>
00490 void PrintTupleTo(const T& t, ::std::ostream* os);
00491
00492
00493
00494
00495
00496
00497 inline void PrintTo(const ::std::tr1::tuple<>& t, ::std::ostream* os) {
00498 PrintTupleTo(t, os);
00499 }
00500
00501 template <typename T1>
00502 void PrintTo(const ::std::tr1::tuple<T1>& t, ::std::ostream* os) {
00503 PrintTupleTo(t, os);
00504 }
00505
00506 template <typename T1, typename T2>
00507 void PrintTo(const ::std::tr1::tuple<T1, T2>& t, ::std::ostream* os) {
00508 PrintTupleTo(t, os);
00509 }
00510
00511 template <typename T1, typename T2, typename T3>
00512 void PrintTo(const ::std::tr1::tuple<T1, T2, T3>& t, ::std::ostream* os) {
00513 PrintTupleTo(t, os);
00514 }
00515
00516 template <typename T1, typename T2, typename T3, typename T4>
00517 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4>& t, ::std::ostream* os) {
00518 PrintTupleTo(t, os);
00519 }
00520
00521 template <typename T1, typename T2, typename T3, typename T4, typename T5>
00522 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5>& t,
00523 ::std::ostream* os) {
00524 PrintTupleTo(t, os);
00525 }
00526
00527 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00528 typename T6>
00529 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6>& t,
00530 ::std::ostream* os) {
00531 PrintTupleTo(t, os);
00532 }
00533
00534 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00535 typename T6, typename T7>
00536 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7>& t,
00537 ::std::ostream* os) {
00538 PrintTupleTo(t, os);
00539 }
00540
00541 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00542 typename T6, typename T7, typename T8>
00543 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8>& t,
00544 ::std::ostream* os) {
00545 PrintTupleTo(t, os);
00546 }
00547
00548 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00549 typename T6, typename T7, typename T8, typename T9>
00550 void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>& t,
00551 ::std::ostream* os) {
00552 PrintTupleTo(t, os);
00553 }
00554
00555 template <typename T1, typename T2, typename T3, typename T4, typename T5,
00556 typename T6, typename T7, typename T8, typename T9, typename T10>
00557 void PrintTo(
00558 const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& t,
00559 ::std::ostream* os) {
00560 PrintTupleTo(t, os);
00561 }
00562 #endif // GTEST_HAS_TR1_TUPLE
00563
00564
00565 template <typename T1, typename T2>
00566 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
00567 *os << '(';
00568
00569
00570 UniversalPrinter<T1>::Print(value.first, os);
00571 *os << ", ";
00572 UniversalPrinter<T2>::Print(value.second, os);
00573 *os << ')';
00574 }
00575
00576
00577
00578 template <typename T>
00579 class UniversalPrinter {
00580 public:
00581
00582
00583 #ifdef _MSC_VER
00584 # pragma warning(push) // Saves the current warning state.
00585 # pragma warning(disable:4180) // Temporarily disables warning 4180.
00586 #endif // _MSC_VER
00587
00588
00589
00590
00591 static void Print(const T& value, ::std::ostream* os) {
00592
00593
00594
00595
00596
00597
00598
00599
00600 PrintTo(value, os);
00601 }
00602
00603 #ifdef _MSC_VER
00604 # pragma warning(pop) // Restores the warning state.
00605 #endif // _MSC_VER
00606 };
00607
00608
00609
00610 template <typename T>
00611 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
00612 if (len == 0) {
00613 *os << "{}";
00614 } else {
00615 *os << "{ ";
00616 const size_t kThreshold = 18;
00617 const size_t kChunkSize = 8;
00618
00619
00620
00621
00622 if (len <= kThreshold) {
00623 PrintRawArrayTo(begin, len, os);
00624 } else {
00625 PrintRawArrayTo(begin, kChunkSize, os);
00626 *os << ", ..., ";
00627 PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
00628 }
00629 *os << " }";
00630 }
00631 }
00632
00633 GTEST_API_ void UniversalPrintArray(
00634 const char* begin, size_t len, ::std::ostream* os);
00635
00636
00637 GTEST_API_ void UniversalPrintArray(
00638 const wchar_t* begin, size_t len, ::std::ostream* os);
00639
00640
00641 template <typename T, size_t N>
00642 class UniversalPrinter<T[N]> {
00643 public:
00644
00645
00646 static void Print(const T (&a)[N], ::std::ostream* os) {
00647 UniversalPrintArray(a, N, os);
00648 }
00649 };
00650
00651
00652 template <typename T>
00653 class UniversalPrinter<T&> {
00654 public:
00655
00656
00657 #ifdef _MSC_VER
00658 # pragma warning(push) // Saves the current warning state.
00659 # pragma warning(disable:4180) // Temporarily disables warning 4180.
00660 #endif // _MSC_VER
00661
00662 static void Print(const T& value, ::std::ostream* os) {
00663
00664
00665 *os << "@" << reinterpret_cast<const void*>(&value) << " ";
00666
00667
00668 UniversalPrint(value, os);
00669 }
00670
00671 #ifdef _MSC_VER
00672 # pragma warning(pop) // Restores the warning state.
00673 #endif // _MSC_VER
00674 };
00675
00676
00677
00678
00679
00680 template <typename T>
00681 class UniversalTersePrinter {
00682 public:
00683 static void Print(const T& value, ::std::ostream* os) {
00684 UniversalPrint(value, os);
00685 }
00686 };
00687 template <typename T>
00688 class UniversalTersePrinter<T&> {
00689 public:
00690 static void Print(const T& value, ::std::ostream* os) {
00691 UniversalPrint(value, os);
00692 }
00693 };
00694 template <typename T, size_t N>
00695 class UniversalTersePrinter<T[N]> {
00696 public:
00697 static void Print(const T (&value)[N], ::std::ostream* os) {
00698 UniversalPrinter<T[N]>::Print(value, os);
00699 }
00700 };
00701 template <>
00702 class UniversalTersePrinter<const char*> {
00703 public:
00704 static void Print(const char* str, ::std::ostream* os) {
00705 if (str == NULL) {
00706 *os << "NULL";
00707 } else {
00708 UniversalPrint(string(str), os);
00709 }
00710 }
00711 };
00712 template <>
00713 class UniversalTersePrinter<char*> {
00714 public:
00715 static void Print(char* str, ::std::ostream* os) {
00716 UniversalTersePrinter<const char*>::Print(str, os);
00717 }
00718 };
00719
00720 #if GTEST_HAS_STD_WSTRING
00721 template <>
00722 class UniversalTersePrinter<const wchar_t*> {
00723 public:
00724 static void Print(const wchar_t* str, ::std::ostream* os) {
00725 if (str == NULL) {
00726 *os << "NULL";
00727 } else {
00728 UniversalPrint(::std::wstring(str), os);
00729 }
00730 }
00731 };
00732 #endif
00733
00734 template <>
00735 class UniversalTersePrinter<wchar_t*> {
00736 public:
00737 static void Print(wchar_t* str, ::std::ostream* os) {
00738 UniversalTersePrinter<const wchar_t*>::Print(str, os);
00739 }
00740 };
00741
00742 template <typename T>
00743 void UniversalTersePrint(const T& value, ::std::ostream* os) {
00744 UniversalTersePrinter<T>::Print(value, os);
00745 }
00746
00747
00748
00749
00750
00751 template <typename T>
00752 void UniversalPrint(const T& value, ::std::ostream* os) {
00753
00754
00755 typedef T T1;
00756 UniversalPrinter<T1>::Print(value, os);
00757 }
00758
00759 #if GTEST_HAS_TR1_TUPLE
00760 typedef ::std::vector<string> Strings;
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770 template <size_t N>
00771 struct TuplePrefixPrinter {
00772
00773 template <typename Tuple>
00774 static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
00775 TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);
00776 *os << ", ";
00777 UniversalPrinter<typename ::std::tr1::tuple_element<N - 1, Tuple>::type>
00778 ::Print(::std::tr1::get<N - 1>(t), os);
00779 }
00780
00781
00782
00783 template <typename Tuple>
00784 static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
00785 TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);
00786 ::std::stringstream ss;
00787 UniversalTersePrint(::std::tr1::get<N - 1>(t), &ss);
00788 strings->push_back(ss.str());
00789 }
00790 };
00791
00792
00793 template <>
00794 struct TuplePrefixPrinter<0> {
00795 template <typename Tuple>
00796 static void PrintPrefixTo(const Tuple&, ::std::ostream*) {}
00797
00798 template <typename Tuple>
00799 static void TersePrintPrefixToStrings(const Tuple&, Strings*) {}
00800 };
00801
00802
00803
00804
00805
00806 template <>
00807 struct TuplePrefixPrinter<1> {
00808 template <typename Tuple>
00809 static void PrintPrefixTo(const Tuple& t, ::std::ostream* os) {
00810 UniversalPrinter<typename ::std::tr1::tuple_element<0, Tuple>::type>::
00811 Print(::std::tr1::get<0>(t), os);
00812 }
00813
00814 template <typename Tuple>
00815 static void TersePrintPrefixToStrings(const Tuple& t, Strings* strings) {
00816 ::std::stringstream ss;
00817 UniversalTersePrint(::std::tr1::get<0>(t), &ss);
00818 strings->push_back(ss.str());
00819 }
00820 };
00821
00822
00823
00824 template <typename T>
00825 void PrintTupleTo(const T& t, ::std::ostream* os) {
00826 *os << "(";
00827 TuplePrefixPrinter< ::std::tr1::tuple_size<T>::value>::
00828 PrintPrefixTo(t, os);
00829 *os << ")";
00830 }
00831
00832
00833
00834
00835 template <typename Tuple>
00836 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
00837 Strings result;
00838 TuplePrefixPrinter< ::std::tr1::tuple_size<Tuple>::value>::
00839 TersePrintPrefixToStrings(value, &result);
00840 return result;
00841 }
00842 #endif // GTEST_HAS_TR1_TUPLE
00843
00844 }
00845
00846 template <typename T>
00847 ::std::string PrintToString(const T& value) {
00848 ::std::stringstream ss;
00849 internal::UniversalTersePrinter<T>::Print(value, &ss);
00850 return ss.str();
00851 }
00852
00853 }
00854
00855 #endif // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_