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