12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748 |
- #include <ctype.h>
- #include <limits.h>
- #include <string.h>
- #include <algorithm>
- #include <deque>
- #include <list>
- #include <map>
- #include <set>
- #include <sstream>
- #include <string>
- #include <utility>
- #include <vector>
- #include "gtest/gtest-printers.h"
- #include "gtest/gtest.h"
- #if GTEST_HAS_UNORDERED_MAP_
- # include <unordered_map> // NOLINT
- #endif
- #if GTEST_HAS_UNORDERED_SET_
- # include <unordered_set> // NOLINT
- #endif
- #if GTEST_HAS_STD_FORWARD_LIST_
- # include <forward_list> // NOLINT
- #endif
- enum AnonymousEnum {
- kAE1 = -1,
- kAE2 = 1
- };
- enum EnumWithoutPrinter {
- kEWP1 = -2,
- kEWP2 = 42
- };
- enum EnumWithStreaming {
- kEWS1 = 10
- };
- std::ostream& operator<<(std::ostream& os, EnumWithStreaming e) {
- return os << (e == kEWS1 ? "kEWS1" : "invalid");
- }
- enum EnumWithPrintTo {
- kEWPT1 = 1
- };
- void PrintTo(EnumWithPrintTo e, std::ostream* os) {
- *os << (e == kEWPT1 ? "kEWPT1" : "invalid");
- }
- class BiggestIntConvertible {
- public:
- operator ::testing::internal::BiggestInt() const { return 42; }
- };
- template <typename T>
- class UnprintableTemplateInGlobal {
- public:
- UnprintableTemplateInGlobal() : value_() {}
- private:
- T value_;
- };
- class StreamableInGlobal {
- public:
- virtual ~StreamableInGlobal() {}
- };
- inline void operator<<(::std::ostream& os, const StreamableInGlobal& ) {
- os << "StreamableInGlobal";
- }
- void operator<<(::std::ostream& os, const StreamableInGlobal* ) {
- os << "StreamableInGlobal*";
- }
- namespace foo {
- class UnprintableInFoo {
- public:
- UnprintableInFoo() : z_(0) { memcpy(xy_, "\xEF\x12\x0\x0\x34\xAB\x0\x0", 8); }
- double z() const { return z_; }
- private:
- char xy_[8];
- double z_;
- };
- struct PrintableViaPrintTo {
- PrintableViaPrintTo() : value() {}
- int value;
- };
- void PrintTo(const PrintableViaPrintTo& x, ::std::ostream* os) {
- *os << "PrintableViaPrintTo: " << x.value;
- }
- struct PointerPrintable {
- };
- ::std::ostream& operator<<(::std::ostream& os,
- const PointerPrintable* ) {
- return os << "PointerPrintable*";
- }
- template <typename T>
- class PrintableViaPrintToTemplate {
- public:
- explicit PrintableViaPrintToTemplate(const T& a_value) : value_(a_value) {}
- const T& value() const { return value_; }
- private:
- T value_;
- };
- template <typename T>
- void PrintTo(const PrintableViaPrintToTemplate<T>& x, ::std::ostream* os) {
- *os << "PrintableViaPrintToTemplate: " << x.value();
- }
- template <typename T>
- class StreamableTemplateInFoo {
- public:
- StreamableTemplateInFoo() : value_() {}
- const T& value() const { return value_; }
- private:
- T value_;
- };
- template <typename T>
- inline ::std::ostream& operator<<(::std::ostream& os,
- const StreamableTemplateInFoo<T>& x) {
- return os << "StreamableTemplateInFoo: " << x.value();
- }
- class PathLike {
- public:
- struct iterator {
- typedef PathLike value_type;
- };
- PathLike() {}
- iterator begin() const { return iterator(); }
- iterator end() const { return iterator(); }
- friend ::std::ostream& operator<<(::std::ostream& os, const PathLike&) {
- return os << "Streamable-PathLike";
- }
- };
- }
- namespace testing {
- namespace gtest_printers_test {
- using ::std::deque;
- using ::std::list;
- using ::std::make_pair;
- using ::std::map;
- using ::std::multimap;
- using ::std::multiset;
- using ::std::pair;
- using ::std::set;
- using ::std::vector;
- using ::testing::PrintToString;
- using ::testing::internal::FormatForComparisonFailureMessage;
- using ::testing::internal::ImplicitCast_;
- using ::testing::internal::NativeArray;
- using ::testing::internal::RE;
- using ::testing::internal::RelationToSourceReference;
- using ::testing::internal::Strings;
- using ::testing::internal::UniversalPrint;
- using ::testing::internal::UniversalPrinter;
- using ::testing::internal::UniversalTersePrint;
- #if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_
- using ::testing::internal::UniversalTersePrintTupleFieldsToStrings;
- #endif
- template <typename T>
- std::string Print(const T& value) {
- ::std::stringstream ss;
- UniversalPrinter<T>::Print(value, &ss);
- return ss.str();
- }
- template <typename T>
- std::string PrintByRef(const T& value) {
- ::std::stringstream ss;
- UniversalPrinter<T&>::Print(value, &ss);
- return ss.str();
- }
- TEST(PrintEnumTest, AnonymousEnum) {
- EXPECT_EQ("-1", Print(kAE1));
- EXPECT_EQ("1", Print(kAE2));
- }
- TEST(PrintEnumTest, EnumWithoutPrinter) {
- EXPECT_EQ("-2", Print(kEWP1));
- EXPECT_EQ("42", Print(kEWP2));
- }
- TEST(PrintEnumTest, EnumWithStreaming) {
- EXPECT_EQ("kEWS1", Print(kEWS1));
- EXPECT_EQ("invalid", Print(static_cast<EnumWithStreaming>(0)));
- }
- TEST(PrintEnumTest, EnumWithPrintTo) {
- EXPECT_EQ("kEWPT1", Print(kEWPT1));
- EXPECT_EQ("invalid", Print(static_cast<EnumWithPrintTo>(0)));
- }
- TEST(PrintClassTest, BiggestIntConvertible) {
- EXPECT_EQ("42", Print(BiggestIntConvertible()));
- }
- TEST(PrintCharTest, PlainChar) {
- EXPECT_EQ("'\\0'", Print('\0'));
- EXPECT_EQ("'\\'' (39, 0x27)", Print('\''));
- EXPECT_EQ("'\"' (34, 0x22)", Print('"'));
- EXPECT_EQ("'?' (63, 0x3F)", Print('?'));
- EXPECT_EQ("'\\\\' (92, 0x5C)", Print('\\'));
- EXPECT_EQ("'\\a' (7)", Print('\a'));
- EXPECT_EQ("'\\b' (8)", Print('\b'));
- EXPECT_EQ("'\\f' (12, 0xC)", Print('\f'));
- EXPECT_EQ("'\\n' (10, 0xA)", Print('\n'));
- EXPECT_EQ("'\\r' (13, 0xD)", Print('\r'));
- EXPECT_EQ("'\\t' (9)", Print('\t'));
- EXPECT_EQ("'\\v' (11, 0xB)", Print('\v'));
- EXPECT_EQ("'\\x7F' (127)", Print('\x7F'));
- EXPECT_EQ("'\\xFF' (255)", Print('\xFF'));
- EXPECT_EQ("' ' (32, 0x20)", Print(' '));
- EXPECT_EQ("'a' (97, 0x61)", Print('a'));
- }
- TEST(PrintCharTest, SignedChar) {
- EXPECT_EQ("'\\0'", Print(static_cast<signed char>('\0')));
- EXPECT_EQ("'\\xCE' (-50)",
- Print(static_cast<signed char>(-50)));
- }
- TEST(PrintCharTest, UnsignedChar) {
- EXPECT_EQ("'\\0'", Print(static_cast<unsigned char>('\0')));
- EXPECT_EQ("'b' (98, 0x62)",
- Print(static_cast<unsigned char>('b')));
- }
- TEST(PrintBuiltInTypeTest, Bool) {
- EXPECT_EQ("false", Print(false));
- EXPECT_EQ("true", Print(true));
- }
- TEST(PrintBuiltInTypeTest, Wchar_t) {
- EXPECT_EQ("L'\\0'", Print(L'\0'));
- EXPECT_EQ("L'\\'' (39, 0x27)", Print(L'\''));
- EXPECT_EQ("L'\"' (34, 0x22)", Print(L'"'));
- EXPECT_EQ("L'?' (63, 0x3F)", Print(L'?'));
- EXPECT_EQ("L'\\\\' (92, 0x5C)", Print(L'\\'));
- EXPECT_EQ("L'\\a' (7)", Print(L'\a'));
- EXPECT_EQ("L'\\b' (8)", Print(L'\b'));
- EXPECT_EQ("L'\\f' (12, 0xC)", Print(L'\f'));
- EXPECT_EQ("L'\\n' (10, 0xA)", Print(L'\n'));
- EXPECT_EQ("L'\\r' (13, 0xD)", Print(L'\r'));
- EXPECT_EQ("L'\\t' (9)", Print(L'\t'));
- EXPECT_EQ("L'\\v' (11, 0xB)", Print(L'\v'));
- EXPECT_EQ("L'\\x7F' (127)", Print(L'\x7F'));
- EXPECT_EQ("L'\\xFF' (255)", Print(L'\xFF'));
- EXPECT_EQ("L' ' (32, 0x20)", Print(L' '));
- EXPECT_EQ("L'a' (97, 0x61)", Print(L'a'));
- EXPECT_EQ("L'\\x576' (1398)", Print(static_cast<wchar_t>(0x576)));
- EXPECT_EQ("L'\\xC74D' (51021)", Print(static_cast<wchar_t>(0xC74D)));
- }
- TEST(PrintTypeSizeTest, Wchar_t) {
- EXPECT_LT(sizeof(wchar_t), sizeof(testing::internal::Int64));
- }
- TEST(PrintBuiltInTypeTest, Integer) {
- EXPECT_EQ("'\\xFF' (255)", Print(static_cast<unsigned char>(255)));
- EXPECT_EQ("'\\x80' (-128)", Print(static_cast<signed char>(-128)));
- EXPECT_EQ("65535", Print(USHRT_MAX));
- EXPECT_EQ("-32768", Print(SHRT_MIN));
- EXPECT_EQ("4294967295", Print(UINT_MAX));
- EXPECT_EQ("-2147483648", Print(INT_MIN));
- EXPECT_EQ("18446744073709551615",
- Print(static_cast<testing::internal::UInt64>(-1)));
- EXPECT_EQ("-9223372036854775808",
- Print(static_cast<testing::internal::Int64>(1) << 63));
- }
- TEST(PrintBuiltInTypeTest, Size_t) {
- EXPECT_EQ("1", Print(sizeof('a')));
- #if !GTEST_OS_WINDOWS
-
- EXPECT_EQ("-2", Print(static_cast<ssize_t>(-2)));
- #endif
- }
- TEST(PrintBuiltInTypeTest, FloatingPoints) {
- EXPECT_EQ("1.5", Print(1.5f));
- EXPECT_EQ("-2.5", Print(-2.5));
- }
- static std::string PrintPointer(const void* p) {
- ::std::stringstream expected_result_stream;
- expected_result_stream << p;
- return expected_result_stream.str();
- }
- TEST(PrintCStringTest, Const) {
- const char* p = "World";
- EXPECT_EQ(PrintPointer(p) + " pointing to \"World\"", Print(p));
- }
- TEST(PrintCStringTest, NonConst) {
- char p[] = "Hi";
- EXPECT_EQ(PrintPointer(p) + " pointing to \"Hi\"",
- Print(static_cast<char*>(p)));
- }
- TEST(PrintCStringTest, Null) {
- const char* p = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- TEST(PrintCStringTest, EscapesProperly) {
- const char* p = "'\"?\\\a\b\f\n\r\t\v\x7F\xFF a";
- EXPECT_EQ(PrintPointer(p) + " pointing to \"'\\\"?\\\\\\a\\b\\f"
- "\\n\\r\\t\\v\\x7F\\xFF a\"",
- Print(p));
- }
- #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
- TEST(PrintWideCStringTest, Const) {
- const wchar_t* p = L"World";
- EXPECT_EQ(PrintPointer(p) + " pointing to L\"World\"", Print(p));
- }
- TEST(PrintWideCStringTest, NonConst) {
- wchar_t p[] = L"Hi";
- EXPECT_EQ(PrintPointer(p) + " pointing to L\"Hi\"",
- Print(static_cast<wchar_t*>(p)));
- }
- TEST(PrintWideCStringTest, Null) {
- const wchar_t* p = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- TEST(PrintWideCStringTest, EscapesProperly) {
- const wchar_t s[] = {'\'', '"', '?', '\\', '\a', '\b', '\f', '\n', '\r',
- '\t', '\v', 0xD3, 0x576, 0x8D3, 0xC74D, ' ', 'a', '\0'};
- EXPECT_EQ(PrintPointer(s) + " pointing to L\"'\\\"?\\\\\\a\\b\\f"
- "\\n\\r\\t\\v\\xD3\\x576\\x8D3\\xC74D a\"",
- Print(static_cast<const wchar_t*>(s)));
- }
- #endif
- TEST(PrintCharPointerTest, SignedChar) {
- signed char* p = reinterpret_cast<signed char*>(0x1234);
- EXPECT_EQ(PrintPointer(p), Print(p));
- p = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- TEST(PrintCharPointerTest, ConstSignedChar) {
- signed char* p = reinterpret_cast<signed char*>(0x1234);
- EXPECT_EQ(PrintPointer(p), Print(p));
- p = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- TEST(PrintCharPointerTest, UnsignedChar) {
- unsigned char* p = reinterpret_cast<unsigned char*>(0x1234);
- EXPECT_EQ(PrintPointer(p), Print(p));
- p = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- TEST(PrintCharPointerTest, ConstUnsignedChar) {
- const unsigned char* p = reinterpret_cast<const unsigned char*>(0x1234);
- EXPECT_EQ(PrintPointer(p), Print(p));
- p = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- TEST(PrintPointerToBuiltInTypeTest, Bool) {
- bool* p = reinterpret_cast<bool*>(0xABCD);
- EXPECT_EQ(PrintPointer(p), Print(p));
- p = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- TEST(PrintPointerToBuiltInTypeTest, Void) {
- void* p = reinterpret_cast<void*>(0xABCD);
- EXPECT_EQ(PrintPointer(p), Print(p));
- p = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- TEST(PrintPointerToBuiltInTypeTest, ConstVoid) {
- const void* p = reinterpret_cast<const void*>(0xABCD);
- EXPECT_EQ(PrintPointer(p), Print(p));
- p = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- TEST(PrintPointerToPointerTest, IntPointerPointer) {
- int** p = reinterpret_cast<int**>(0xABCD);
- EXPECT_EQ(PrintPointer(p), Print(p));
- p = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- void MyFunction(int ) {}
- TEST(PrintPointerTest, NonMemberFunctionPointer) {
-
-
-
-
- EXPECT_EQ(
- PrintPointer(reinterpret_cast<const void*>(
- reinterpret_cast<internal::BiggestInt>(&MyFunction))),
- Print(&MyFunction));
- int (*p)(bool) = NULL;
- EXPECT_EQ("NULL", Print(p));
- }
- template <typename StringType>
- AssertionResult HasPrefix(const StringType& str, const StringType& prefix) {
- if (str.find(prefix, 0) == 0)
- return AssertionSuccess();
- const bool is_wide_string = sizeof(prefix[0]) > 1;
- const char* const begin_string_quote = is_wide_string ? "L\"" : "\"";
- return AssertionFailure()
- << begin_string_quote << prefix << "\" is not a prefix of "
- << begin_string_quote << str << "\"\n";
- }
- struct Foo {
- public:
- virtual ~Foo() {}
- int MyMethod(char x) { return x + 1; }
- virtual char MyVirtualMethod(int ) { return 'a'; }
- int value;
- };
- TEST(PrintPointerTest, MemberVariablePointer) {
- EXPECT_TRUE(HasPrefix(Print(&Foo::value),
- Print(sizeof(&Foo::value)) + "-byte object "));
- int Foo::*p = NULL;
- EXPECT_TRUE(HasPrefix(Print(p),
- Print(sizeof(p)) + "-byte object "));
- }
- TEST(PrintPointerTest, MemberFunctionPointer) {
- EXPECT_TRUE(HasPrefix(Print(&Foo::MyMethod),
- Print(sizeof(&Foo::MyMethod)) + "-byte object "));
- EXPECT_TRUE(
- HasPrefix(Print(&Foo::MyVirtualMethod),
- Print(sizeof((&Foo::MyVirtualMethod))) + "-byte object "));
- int (Foo::*p)(char) = NULL;
- EXPECT_TRUE(HasPrefix(Print(p),
- Print(sizeof(p)) + "-byte object "));
- }
- template <typename T, size_t N>
- std::string PrintArrayHelper(T (&a)[N]) {
- return Print(a);
- }
- TEST(PrintArrayTest, OneDimensionalArray) {
- int a[5] = { 1, 2, 3, 4, 5 };
- EXPECT_EQ("{ 1, 2, 3, 4, 5 }", PrintArrayHelper(a));
- }
- TEST(PrintArrayTest, TwoDimensionalArray) {
- int a[2][5] = {
- { 1, 2, 3, 4, 5 },
- { 6, 7, 8, 9, 0 }
- };
- EXPECT_EQ("{ { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 0 } }", PrintArrayHelper(a));
- }
- TEST(PrintArrayTest, ConstArray) {
- const bool a[1] = { false };
- EXPECT_EQ("{ false }", PrintArrayHelper(a));
- }
- TEST(PrintArrayTest, CharArrayWithNoTerminatingNul) {
-
- char a[] = { 'H', '\0', 'i' };
- EXPECT_EQ("\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
- }
- TEST(PrintArrayTest, ConstCharArrayWithTerminatingNul) {
- const char a[] = "\0Hi";
- EXPECT_EQ("\"\\0Hi\"", PrintArrayHelper(a));
- }
- TEST(PrintArrayTest, WCharArrayWithNoTerminatingNul) {
-
- const wchar_t a[] = { L'H', L'\0', L'i' };
- EXPECT_EQ("L\"H\\0i\" (no terminating NUL)", PrintArrayHelper(a));
- }
- TEST(PrintArrayTest, WConstCharArrayWithTerminatingNul) {
- const wchar_t a[] = L"\0Hi";
- EXPECT_EQ("L\"\\0Hi\"", PrintArrayHelper(a));
- }
- TEST(PrintArrayTest, ObjectArray) {
- std::string a[3] = {"Hi", "Hello", "Ni hao"};
- EXPECT_EQ("{ \"Hi\", \"Hello\", \"Ni hao\" }", PrintArrayHelper(a));
- }
- TEST(PrintArrayTest, BigArray) {
- int a[100] = { 1, 2, 3 };
- EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0 }",
- PrintArrayHelper(a));
- }
- #if GTEST_HAS_GLOBAL_STRING
- TEST(PrintStringTest, StringInGlobalNamespace) {
- const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
- const ::string str(s, sizeof(s));
- EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
- Print(str));
- }
- #endif
- TEST(PrintStringTest, StringInStdNamespace) {
- const char s[] = "'\"?\\\a\b\f\n\0\r\t\v\x7F\xFF a";
- const ::std::string str(s, sizeof(s));
- EXPECT_EQ("\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v\\x7F\\xFF a\\0\"",
- Print(str));
- }
- TEST(PrintStringTest, StringAmbiguousHex) {
-
-
-
- EXPECT_EQ("\"0\\x12\" \"3\"", Print(::std::string("0\x12" "3")));
-
- EXPECT_EQ("\"mm\\x6\" \"bananas\"", Print(::std::string("mm\x6" "bananas")));
-
- EXPECT_EQ("\"NOM\\x6\" \"BANANA\"", Print(::std::string("NOM\x6" "BANANA")));
-
- EXPECT_EQ("\"!\\x5-!\"", Print(::std::string("!\x5-!")));
- }
- #if GTEST_HAS_GLOBAL_WSTRING
- TEST(PrintWideStringTest, StringInGlobalNamespace) {
- const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
- const ::wstring str(s, sizeof(s)/sizeof(wchar_t));
- EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
- "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
- Print(str));
- }
- #endif
- #if GTEST_HAS_STD_WSTRING
- TEST(PrintWideStringTest, StringInStdNamespace) {
- const wchar_t s[] = L"'\"?\\\a\b\f\n\0\r\t\v\xD3\x576\x8D3\xC74D a";
- const ::std::wstring str(s, sizeof(s)/sizeof(wchar_t));
- EXPECT_EQ("L\"'\\\"?\\\\\\a\\b\\f\\n\\0\\r\\t\\v"
- "\\xD3\\x576\\x8D3\\xC74D a\\0\"",
- Print(str));
- }
- TEST(PrintWideStringTest, StringAmbiguousHex) {
-
- EXPECT_EQ("L\"0\\x12\" L\"3\"", Print(::std::wstring(L"0\x12" L"3")));
- EXPECT_EQ("L\"mm\\x6\" L\"bananas\"",
- Print(::std::wstring(L"mm\x6" L"bananas")));
- EXPECT_EQ("L\"NOM\\x6\" L\"BANANA\"",
- Print(::std::wstring(L"NOM\x6" L"BANANA")));
- EXPECT_EQ("L\"!\\x5-!\"", Print(::std::wstring(L"!\x5-!")));
- }
- #endif
- class AllowsGenericStreaming {};
- template <typename Char, typename CharTraits>
- std::basic_ostream<Char, CharTraits>& operator<<(
- std::basic_ostream<Char, CharTraits>& os,
- const AllowsGenericStreaming& ) {
- return os << "AllowsGenericStreaming";
- }
- TEST(PrintTypeWithGenericStreamingTest, NonTemplateType) {
- AllowsGenericStreaming a;
- EXPECT_EQ("AllowsGenericStreaming", Print(a));
- }
- template <typename T>
- class AllowsGenericStreamingTemplate {};
- template <typename Char, typename CharTraits, typename T>
- std::basic_ostream<Char, CharTraits>& operator<<(
- std::basic_ostream<Char, CharTraits>& os,
- const AllowsGenericStreamingTemplate<T>& ) {
- return os << "AllowsGenericStreamingTemplate";
- }
- TEST(PrintTypeWithGenericStreamingTest, TemplateType) {
- AllowsGenericStreamingTemplate<int> a;
- EXPECT_EQ("AllowsGenericStreamingTemplate", Print(a));
- }
- template <typename T>
- class AllowsGenericStreamingAndImplicitConversionTemplate {
- public:
- operator bool() const { return false; }
- };
- template <typename Char, typename CharTraits, typename T>
- std::basic_ostream<Char, CharTraits>& operator<<(
- std::basic_ostream<Char, CharTraits>& os,
- const AllowsGenericStreamingAndImplicitConversionTemplate<T>& ) {
- return os << "AllowsGenericStreamingAndImplicitConversionTemplate";
- }
- TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
- AllowsGenericStreamingAndImplicitConversionTemplate<int> a;
- EXPECT_EQ("AllowsGenericStreamingAndImplicitConversionTemplate", Print(a));
- }
- #if GTEST_HAS_ABSL
- TEST(PrintStringViewTest, SimpleStringView) {
- const ::absl::string_view sp = "Hello";
- EXPECT_EQ("\"Hello\"", Print(sp));
- }
- TEST(PrintStringViewTest, UnprintableCharacters) {
- const char str[] = "NUL (\0) and \r\t";
- const ::absl::string_view sp(str, sizeof(str) - 1);
- EXPECT_EQ("\"NUL (\\0) and \\r\\t\"", Print(sp));
- }
- #endif
- TEST(PrintStlContainerTest, EmptyDeque) {
- deque<char> empty;
- EXPECT_EQ("{}", Print(empty));
- }
- TEST(PrintStlContainerTest, NonEmptyDeque) {
- deque<int> non_empty;
- non_empty.push_back(1);
- non_empty.push_back(3);
- EXPECT_EQ("{ 1, 3 }", Print(non_empty));
- }
- #if GTEST_HAS_UNORDERED_MAP_
- TEST(PrintStlContainerTest, OneElementHashMap) {
- ::std::unordered_map<int, char> map1;
- map1[1] = 'a';
- EXPECT_EQ("{ (1, 'a' (97, 0x61)) }", Print(map1));
- }
- TEST(PrintStlContainerTest, HashMultiMap) {
- ::std::unordered_multimap<int, bool> map1;
- map1.insert(make_pair(5, true));
- map1.insert(make_pair(5, false));
-
- const std::string result = Print(map1);
- EXPECT_TRUE(result == "{ (5, true), (5, false) }" ||
- result == "{ (5, false), (5, true) }")
- << " where Print(map1) returns \"" << result << "\".";
- }
- #endif
- #if GTEST_HAS_UNORDERED_SET_
- TEST(PrintStlContainerTest, HashSet) {
- ::std::unordered_set<int> set1;
- set1.insert(1);
- EXPECT_EQ("{ 1 }", Print(set1));
- }
- TEST(PrintStlContainerTest, HashMultiSet) {
- const int kSize = 5;
- int a[kSize] = { 1, 1, 2, 5, 1 };
- ::std::unordered_multiset<int> set1(a, a + kSize);
-
- const std::string result = Print(set1);
- const std::string expected_pattern = "{ d, d, d, d, d }";
-
-
- ASSERT_EQ(expected_pattern.length(), result.length());
- std::vector<int> numbers;
- for (size_t i = 0; i != result.length(); i++) {
- if (expected_pattern[i] == 'd') {
- ASSERT_NE(isdigit(static_cast<unsigned char>(result[i])), 0);
- numbers.push_back(result[i] - '0');
- } else {
- EXPECT_EQ(expected_pattern[i], result[i]) << " where result is "
- << result;
- }
- }
-
- std::sort(numbers.begin(), numbers.end());
- std::sort(a, a + kSize);
- EXPECT_TRUE(std::equal(a, a + kSize, numbers.begin()));
- }
- #endif
- TEST(PrintStlContainerTest, List) {
- const std::string a[] = {"hello", "world"};
- const list<std::string> strings(a, a + 2);
- EXPECT_EQ("{ \"hello\", \"world\" }", Print(strings));
- }
- TEST(PrintStlContainerTest, Map) {
- map<int, bool> map1;
- map1[1] = true;
- map1[5] = false;
- map1[3] = true;
- EXPECT_EQ("{ (1, true), (3, true), (5, false) }", Print(map1));
- }
- TEST(PrintStlContainerTest, MultiMap) {
- multimap<bool, int> map1;
-
-
-
-
-
-
- map1.insert(pair<const bool, int>(true, 0));
- map1.insert(pair<const bool, int>(true, 1));
- map1.insert(pair<const bool, int>(false, 2));
- EXPECT_EQ("{ (false, 2), (true, 0), (true, 1) }", Print(map1));
- }
- TEST(PrintStlContainerTest, Set) {
- const unsigned int a[] = { 3, 0, 5 };
- set<unsigned int> set1(a, a + 3);
- EXPECT_EQ("{ 0, 3, 5 }", Print(set1));
- }
- TEST(PrintStlContainerTest, MultiSet) {
- const int a[] = { 1, 1, 2, 5, 1 };
- multiset<int> set1(a, a + 5);
- EXPECT_EQ("{ 1, 1, 1, 2, 5 }", Print(set1));
- }
- #if GTEST_HAS_STD_FORWARD_LIST_
- TEST(PrintStlContainerTest, SinglyLinkedList) {
- int a[] = { 9, 2, 8 };
- const std::forward_list<int> ints(a, a + 3);
- EXPECT_EQ("{ 9, 2, 8 }", Print(ints));
- }
- #endif
- TEST(PrintStlContainerTest, Pair) {
- pair<const bool, int> p(true, 5);
- EXPECT_EQ("(true, 5)", Print(p));
- }
- TEST(PrintStlContainerTest, Vector) {
- vector<int> v;
- v.push_back(1);
- v.push_back(2);
- EXPECT_EQ("{ 1, 2 }", Print(v));
- }
- TEST(PrintStlContainerTest, LongSequence) {
- const int a[100] = { 1, 2, 3 };
- const vector<int> v(a, a + 100);
- EXPECT_EQ("{ 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "
- "0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }", Print(v));
- }
- TEST(PrintStlContainerTest, NestedContainer) {
- const int a1[] = { 1, 2 };
- const int a2[] = { 3, 4, 5 };
- const list<int> l1(a1, a1 + 2);
- const list<int> l2(a2, a2 + 3);
- vector<list<int> > v;
- v.push_back(l1);
- v.push_back(l2);
- EXPECT_EQ("{ { 1, 2 }, { 3, 4, 5 } }", Print(v));
- }
- TEST(PrintStlContainerTest, OneDimensionalNativeArray) {
- const int a[3] = { 1, 2, 3 };
- NativeArray<int> b(a, 3, RelationToSourceReference());
- EXPECT_EQ("{ 1, 2, 3 }", Print(b));
- }
- TEST(PrintStlContainerTest, TwoDimensionalNativeArray) {
- const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
- NativeArray<int[3]> b(a, 2, RelationToSourceReference());
- EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b));
- }
- struct iterator {
- char x;
- };
- TEST(PrintStlContainerTest, Iterator) {
- iterator it = {};
- EXPECT_EQ("1-byte object <00>", Print(it));
- }
- struct const_iterator {
- char x;
- };
- TEST(PrintStlContainerTest, ConstIterator) {
- const_iterator it = {};
- EXPECT_EQ("1-byte object <00>", Print(it));
- }
- #if GTEST_HAS_TR1_TUPLE
- TEST(PrintTr1TupleTest, VariousSizes) {
- ::std::tr1::tuple<> t0;
- EXPECT_EQ("()", Print(t0));
- ::std::tr1::tuple<int> t1(5);
- EXPECT_EQ("(5)", Print(t1));
- ::std::tr1::tuple<char, bool> t2('a', true);
- EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
- ::std::tr1::tuple<bool, int, int> t3(false, 2, 3);
- EXPECT_EQ("(false, 2, 3)", Print(t3));
- ::std::tr1::tuple<bool, int, int, int> t4(false, 2, 3, 4);
- EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
- ::std::tr1::tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
- EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
- ::std::tr1::tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
- EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
- ::std::tr1::tuple<bool, int, int, int, bool, int, int> t7(
- false, 2, 3, 4, true, 6, 7);
- EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
- ::std::tr1::tuple<bool, int, int, int, bool, int, int, bool> t8(
- false, 2, 3, 4, true, 6, 7, true);
- EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
- ::std::tr1::tuple<bool, int, int, int, bool, int, int, bool, int> t9(
- false, 2, 3, 4, true, 6, 7, true, 9);
- EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
- const char* const str = "8";
-
-
- ::std::tr1::tuple<bool, char, short, testing::internal::Int32,
- testing::internal::Int64, float, double, const char*, void*,
- std::string>
- t10(false, 'a', static_cast<short>(3), 4, 5, 1.5F, -2.5, str,
- ImplicitCast_<void*>(NULL), "10");
- EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
- " pointing to \"8\", NULL, \"10\")",
- Print(t10));
- }
- TEST(PrintTr1TupleTest, NestedTuple) {
- ::std::tr1::tuple< ::std::tr1::tuple<int, bool>, char> nested(
- ::std::tr1::make_tuple(5, true), 'a');
- EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
- }
- #endif
- #if GTEST_HAS_STD_TUPLE_
- TEST(PrintStdTupleTest, VariousSizes) {
- ::std::tuple<> t0;
- EXPECT_EQ("()", Print(t0));
- ::std::tuple<int> t1(5);
- EXPECT_EQ("(5)", Print(t1));
- ::std::tuple<char, bool> t2('a', true);
- EXPECT_EQ("('a' (97, 0x61), true)", Print(t2));
- ::std::tuple<bool, int, int> t3(false, 2, 3);
- EXPECT_EQ("(false, 2, 3)", Print(t3));
- ::std::tuple<bool, int, int, int> t4(false, 2, 3, 4);
- EXPECT_EQ("(false, 2, 3, 4)", Print(t4));
- ::std::tuple<bool, int, int, int, bool> t5(false, 2, 3, 4, true);
- EXPECT_EQ("(false, 2, 3, 4, true)", Print(t5));
- ::std::tuple<bool, int, int, int, bool, int> t6(false, 2, 3, 4, true, 6);
- EXPECT_EQ("(false, 2, 3, 4, true, 6)", Print(t6));
- ::std::tuple<bool, int, int, int, bool, int, int> t7(
- false, 2, 3, 4, true, 6, 7);
- EXPECT_EQ("(false, 2, 3, 4, true, 6, 7)", Print(t7));
- ::std::tuple<bool, int, int, int, bool, int, int, bool> t8(
- false, 2, 3, 4, true, 6, 7, true);
- EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true)", Print(t8));
- ::std::tuple<bool, int, int, int, bool, int, int, bool, int> t9(
- false, 2, 3, 4, true, 6, 7, true, 9);
- EXPECT_EQ("(false, 2, 3, 4, true, 6, 7, true, 9)", Print(t9));
- const char* const str = "8";
-
-
- ::std::tuple<bool, char, short, testing::internal::Int32,
- testing::internal::Int64, float, double, const char*, void*,
- std::string>
- t10(false, 'a', static_cast<short>(3), 4, 5, 1.5F, -2.5, str,
- ImplicitCast_<void*>(NULL), "10");
- EXPECT_EQ("(false, 'a' (97, 0x61), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) +
- " pointing to \"8\", NULL, \"10\")",
- Print(t10));
- }
- TEST(PrintStdTupleTest, NestedTuple) {
- ::std::tuple< ::std::tuple<int, bool>, char> nested(
- ::std::make_tuple(5, true), 'a');
- EXPECT_EQ("((5, true), 'a' (97, 0x61))", Print(nested));
- }
- #endif
- #if GTEST_LANG_CXX11
- TEST(PrintNullptrT, Basic) {
- EXPECT_EQ("(nullptr)", Print(nullptr));
- }
- #endif
- TEST(PrintUnprintableTypeTest, InGlobalNamespace) {
- EXPECT_EQ("1-byte object <00>",
- Print(UnprintableTemplateInGlobal<char>()));
- }
- TEST(PrintUnprintableTypeTest, InUserNamespace) {
- EXPECT_EQ("16-byte object <EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
- Print(::foo::UnprintableInFoo()));
- }
- struct Big {
- Big() { memset(array, 0, sizeof(array)); }
- char array[257];
- };
- TEST(PrintUnpritableTypeTest, BigObject) {
- EXPECT_EQ("257-byte object <00-00 00-00 00-00 00-00 00-00 00-00 "
- "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
- "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
- "00-00 00-00 00-00 00-00 00-00 00-00 ... 00-00 00-00 00-00 "
- "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
- "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 "
- "00-00 00-00 00-00 00-00 00-00 00-00 00-00 00-00 00>",
- Print(Big()));
- }
- TEST(PrintStreamableTypeTest, InGlobalNamespace) {
- StreamableInGlobal x;
- EXPECT_EQ("StreamableInGlobal", Print(x));
- EXPECT_EQ("StreamableInGlobal*", Print(&x));
- }
- TEST(PrintStreamableTypeTest, TemplateTypeInUserNamespace) {
- EXPECT_EQ("StreamableTemplateInFoo: 0",
- Print(::foo::StreamableTemplateInFoo<int>()));
- }
- TEST(PrintStreamableTypeTest, PathLikeInUserNamespace) {
- ::foo::PathLike x;
- EXPECT_EQ("Streamable-PathLike", Print(x));
- const ::foo::PathLike cx;
- EXPECT_EQ("Streamable-PathLike", Print(cx));
- }
- TEST(PrintPrintableTypeTest, InUserNamespace) {
- EXPECT_EQ("PrintableViaPrintTo: 0",
- Print(::foo::PrintableViaPrintTo()));
- }
- TEST(PrintPrintableTypeTest, PointerInUserNamespace) {
- ::foo::PointerPrintable x;
- EXPECT_EQ("PointerPrintable*", Print(&x));
- }
- TEST(PrintPrintableTypeTest, TemplateInUserNamespace) {
- EXPECT_EQ("PrintableViaPrintToTemplate: 5",
- Print(::foo::PrintableViaPrintToTemplate<int>(5)));
- }
- TEST(PrintReferenceTest, PrintsAddressAndValue) {
- int n = 5;
- EXPECT_EQ("@" + PrintPointer(&n) + " 5", PrintByRef(n));
- int a[2][3] = {
- { 0, 1, 2 },
- { 3, 4, 5 }
- };
- EXPECT_EQ("@" + PrintPointer(a) + " { { 0, 1, 2 }, { 3, 4, 5 } }",
- PrintByRef(a));
- const ::foo::UnprintableInFoo x;
- EXPECT_EQ("@" + PrintPointer(&x) + " 16-byte object "
- "<EF-12 00-00 34-AB 00-00 00-00 00-00 00-00 00-00>",
- PrintByRef(x));
- }
- TEST(PrintReferenceTest, HandlesFunctionPointer) {
- void (*fp)(int n) = &MyFunction;
- const std::string fp_pointer_string =
- PrintPointer(reinterpret_cast<const void*>(&fp));
-
-
-
-
- const std::string fp_string = PrintPointer(reinterpret_cast<const void*>(
- reinterpret_cast<internal::BiggestInt>(fp)));
- EXPECT_EQ("@" + fp_pointer_string + " " + fp_string,
- PrintByRef(fp));
- }
- TEST(PrintReferenceTest, HandlesMemberFunctionPointer) {
- int (Foo::*p)(char ch) = &Foo::MyMethod;
- EXPECT_TRUE(HasPrefix(
- PrintByRef(p),
- "@" + PrintPointer(reinterpret_cast<const void*>(&p)) + " " +
- Print(sizeof(p)) + "-byte object "));
- char (Foo::*p2)(int n) = &Foo::MyVirtualMethod;
- EXPECT_TRUE(HasPrefix(
- PrintByRef(p2),
- "@" + PrintPointer(reinterpret_cast<const void*>(&p2)) + " " +
- Print(sizeof(p2)) + "-byte object "));
- }
- TEST(PrintReferenceTest, HandlesMemberVariablePointer) {
- int Foo::*p = &Foo::value;
- EXPECT_TRUE(HasPrefix(
- PrintByRef(p),
- "@" + PrintPointer(&p) + " " + Print(sizeof(p)) + "-byte object "));
- }
- TEST(FormatForComparisonFailureMessageTest, WorksForScalar) {
- EXPECT_STREQ("123",
- FormatForComparisonFailureMessage(123, 124).c_str());
- }
- TEST(FormatForComparisonFailureMessageTest, WorksForNonCharPointer) {
- int n = 0;
- EXPECT_EQ(PrintPointer(&n),
- FormatForComparisonFailureMessage(&n, &n).c_str());
- }
- TEST(FormatForComparisonFailureMessageTest, FormatsNonCharArrayAsPointer) {
-
-
- int n[] = { 1, 2, 3 };
- EXPECT_EQ(PrintPointer(n),
- FormatForComparisonFailureMessage(n, n).c_str());
- }
- TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsPointer) {
-
-
-
-
-
- const char* s = "hello";
- EXPECT_EQ(PrintPointer(s),
- FormatForComparisonFailureMessage(s, s).c_str());
-
- char ch = 'a';
- EXPECT_EQ(PrintPointer(&ch),
- FormatForComparisonFailureMessage(&ch, &ch).c_str());
- }
- TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsPointer) {
-
-
-
-
-
- const wchar_t* s = L"hello";
- EXPECT_EQ(PrintPointer(s),
- FormatForComparisonFailureMessage(s, s).c_str());
-
- wchar_t ch = L'a';
- EXPECT_EQ(PrintPointer(&ch),
- FormatForComparisonFailureMessage(&ch, &ch).c_str());
- }
- #if GTEST_HAS_GLOBAL_STRING
- TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsString) {
- const char* s = "hello \"world";
- EXPECT_STREQ("\"hello \\\"world\"",
- FormatForComparisonFailureMessage(s, ::string()).c_str());
-
- char str[] = "hi\1";
- char* p = str;
- EXPECT_STREQ("\"hi\\x1\"",
- FormatForComparisonFailureMessage(p, ::string()).c_str());
- }
- #endif
- TEST(FormatForComparisonFailureMessageTest, WorksForCharPointerVsStdString) {
- const char* s = "hello \"world";
- EXPECT_STREQ("\"hello \\\"world\"",
- FormatForComparisonFailureMessage(s, ::std::string()).c_str());
-
- char str[] = "hi\1";
- char* p = str;
- EXPECT_STREQ("\"hi\\x1\"",
- FormatForComparisonFailureMessage(p, ::std::string()).c_str());
- }
- #if GTEST_HAS_GLOBAL_WSTRING
- TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsWString) {
- const wchar_t* s = L"hi \"world";
- EXPECT_STREQ("L\"hi \\\"world\"",
- FormatForComparisonFailureMessage(s, ::wstring()).c_str());
-
- wchar_t str[] = L"hi\1";
- wchar_t* p = str;
- EXPECT_STREQ("L\"hi\\x1\"",
- FormatForComparisonFailureMessage(p, ::wstring()).c_str());
- }
- #endif
- #if GTEST_HAS_STD_WSTRING
- TEST(FormatForComparisonFailureMessageTest, WorksForWCharPointerVsStdWString) {
- const wchar_t* s = L"hi \"world";
- EXPECT_STREQ("L\"hi \\\"world\"",
- FormatForComparisonFailureMessage(s, ::std::wstring()).c_str());
-
- wchar_t str[] = L"hi\1";
- wchar_t* p = str;
- EXPECT_STREQ("L\"hi\\x1\"",
- FormatForComparisonFailureMessage(p, ::std::wstring()).c_str());
- }
- #endif
- TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsPointer) {
- char str[] = "hi \"world\"";
- char* p = NULL;
- EXPECT_EQ(PrintPointer(str),
- FormatForComparisonFailureMessage(str, p).c_str());
- }
- TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsCharArray) {
- const char str[] = "hi \"world\"";
- EXPECT_EQ(PrintPointer(str),
- FormatForComparisonFailureMessage(str, str).c_str());
- }
- TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsPointer) {
- wchar_t str[] = L"hi \"world\"";
- wchar_t* p = NULL;
- EXPECT_EQ(PrintPointer(str),
- FormatForComparisonFailureMessage(str, p).c_str());
- }
- TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWCharArray) {
- const wchar_t str[] = L"hi \"world\"";
- EXPECT_EQ(PrintPointer(str),
- FormatForComparisonFailureMessage(str, str).c_str());
- }
- #if GTEST_HAS_GLOBAL_STRING
- TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsString) {
- const char str[] = "hi \"w\0rld\"";
- EXPECT_STREQ("\"hi \\\"w\"",
-
- FormatForComparisonFailureMessage(str, ::string()).c_str());
- }
- #endif
- TEST(FormatForComparisonFailureMessageTest, WorksForCharArrayVsStdString) {
- const char str[] = "hi \"world\"";
- EXPECT_STREQ("\"hi \\\"world\\\"\"",
- FormatForComparisonFailureMessage(str, ::std::string()).c_str());
- }
- #if GTEST_HAS_GLOBAL_WSTRING
- TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsWString) {
- const wchar_t str[] = L"hi \"world\"";
- EXPECT_STREQ("L\"hi \\\"world\\\"\"",
- FormatForComparisonFailureMessage(str, ::wstring()).c_str());
- }
- #endif
- #if GTEST_HAS_STD_WSTRING
- TEST(FormatForComparisonFailureMessageTest, WorksForWCharArrayVsStdWString) {
- const wchar_t str[] = L"hi \"w\0rld\"";
- EXPECT_STREQ(
- "L\"hi \\\"w\"",
-
- FormatForComparisonFailureMessage(str, ::std::wstring()).c_str());
- }
- #endif
- #define EXPECT_PRINT_TO_STRING_(value, expected_string) \
- EXPECT_TRUE(PrintToString(value) == (expected_string)) \
- << " where " #value " prints as " << (PrintToString(value))
- TEST(PrintToStringTest, WorksForScalar) {
- EXPECT_PRINT_TO_STRING_(123, "123");
- }
- TEST(PrintToStringTest, WorksForPointerToConstChar) {
- const char* p = "hello";
- EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
- }
- TEST(PrintToStringTest, WorksForPointerToNonConstChar) {
- char s[] = "hello";
- char* p = s;
- EXPECT_PRINT_TO_STRING_(p, "\"hello\"");
- }
- TEST(PrintToStringTest, EscapesForPointerToConstChar) {
- const char* p = "hello\n";
- EXPECT_PRINT_TO_STRING_(p, "\"hello\\n\"");
- }
- TEST(PrintToStringTest, EscapesForPointerToNonConstChar) {
- char s[] = "hello\1";
- char* p = s;
- EXPECT_PRINT_TO_STRING_(p, "\"hello\\x1\"");
- }
- TEST(PrintToStringTest, WorksForArray) {
- int n[3] = { 1, 2, 3 };
- EXPECT_PRINT_TO_STRING_(n, "{ 1, 2, 3 }");
- }
- TEST(PrintToStringTest, WorksForCharArray) {
- char s[] = "hello";
- EXPECT_PRINT_TO_STRING_(s, "\"hello\"");
- }
- TEST(PrintToStringTest, WorksForCharArrayWithEmbeddedNul) {
- const char str_with_nul[] = "hello\0 world";
- EXPECT_PRINT_TO_STRING_(str_with_nul, "\"hello\\0 world\"");
- char mutable_str_with_nul[] = "hello\0 world";
- EXPECT_PRINT_TO_STRING_(mutable_str_with_nul, "\"hello\\0 world\"");
- }
- TEST(PrintToStringTest, ContainsNonLatin) {
-
- std::string non_ascii_str = ::std::string("오전 4:30");
- EXPECT_PRINT_TO_STRING_(non_ascii_str,
- "\"\\xEC\\x98\\xA4\\xEC\\xA0\\x84 4:30\"\n"
- " As Text: \"오전 4:30\"");
- non_ascii_str = ::std::string("From ä — ẑ");
- EXPECT_PRINT_TO_STRING_(non_ascii_str,
- "\"From \\xC3\\xA4 \\xE2\\x80\\x94 \\xE1\\xBA\\x91\""
- "\n As Text: \"From ä — ẑ\"");
- }
- TEST(IsValidUTF8Test, IllFormedUTF8) {
-
-
-
- static const char *const kTestdata[][2] = {
-
- {"\xC3\x74", "\"\\xC3t\""},
-
- {"\xC3\x84\xA4", "\"\\xC3\\x84\\xA4\""},
-
- {"abc\xC3", "\"abc\\xC3\""},
-
- {"x\xE2\x70\x94", "\"x\\xE2p\\x94\""},
-
- {"\xE2\x80", "\"\\xE2\\x80\""},
-
- {"\xE2\x80\xC3\x84", "\"\\xE2\\x80\\xC3\\x84\""},
-
- {"\xE2\x80\x7A", "\"\\xE2\\x80z\""},
-
- {"\xE2\xE2\x80\x94", "\"\\xE2\\xE2\\x80\\x94\""},
-
- {"\xF0\xE2\x80\x94", "\"\\xF0\\xE2\\x80\\x94\""},
-
- {"\xF0\xE2\x80", "\"\\xF0\\xE2\\x80\""},
-
- {"abc\xE2\x80\x94\xC3\x74xyc", "\"abc\\xE2\\x80\\x94\\xC3txyc\""},
- {"abc\xC3\x84\xE2\x80\xC3\x84xyz",
- "\"abc\\xC3\\x84\\xE2\\x80\\xC3\\x84xyz\""},
-
-
- {"\xC0\x80", "\"\\xC0\\x80\""},
- {"\xC1\x81", "\"\\xC1\\x81\""},
-
- {"\xE0\x80\x80", "\"\\xE0\\x80\\x80\""},
- {"\xf0\x80\x80\x80", "\"\\xF0\\x80\\x80\\x80\""},
-
-
- {"\xED\x9F\xBF", "\"\\xED\\x9F\\xBF\"\n As Text: \"\""},
-
- {"\xED\xA0\x80", "\"\\xED\\xA0\\x80\""},
-
- {"\xED\xAD\xBF", "\"\\xED\\xAD\\xBF\""},
-
- {"\xED\xAE\x80", "\"\\xED\\xAE\\x80\""},
-
- {"\xED\xAF\xBF", "\"\\xED\\xAF\\xBF\""},
-
- {"\xED\xB3\xBF", "\"\\xED\\xB3\\xBF\""},
-
-
- {"\xEE\x80\x80", "\"\\xEE\\x80\\x80\"\n As Text: \"\""}
- };
- for (int i = 0; i < int(sizeof(kTestdata)/sizeof(kTestdata[0])); ++i) {
- EXPECT_PRINT_TO_STRING_(kTestdata[i][0], kTestdata[i][1]);
- }
- }
- #undef EXPECT_PRINT_TO_STRING_
- TEST(UniversalTersePrintTest, WorksForNonReference) {
- ::std::stringstream ss;
- UniversalTersePrint(123, &ss);
- EXPECT_EQ("123", ss.str());
- }
- TEST(UniversalTersePrintTest, WorksForReference) {
- const int& n = 123;
- ::std::stringstream ss;
- UniversalTersePrint(n, &ss);
- EXPECT_EQ("123", ss.str());
- }
- TEST(UniversalTersePrintTest, WorksForCString) {
- const char* s1 = "abc";
- ::std::stringstream ss1;
- UniversalTersePrint(s1, &ss1);
- EXPECT_EQ("\"abc\"", ss1.str());
- char* s2 = const_cast<char*>(s1);
- ::std::stringstream ss2;
- UniversalTersePrint(s2, &ss2);
- EXPECT_EQ("\"abc\"", ss2.str());
- const char* s3 = NULL;
- ::std::stringstream ss3;
- UniversalTersePrint(s3, &ss3);
- EXPECT_EQ("NULL", ss3.str());
- }
- TEST(UniversalPrintTest, WorksForNonReference) {
- ::std::stringstream ss;
- UniversalPrint(123, &ss);
- EXPECT_EQ("123", ss.str());
- }
- TEST(UniversalPrintTest, WorksForReference) {
- const int& n = 123;
- ::std::stringstream ss;
- UniversalPrint(n, &ss);
- EXPECT_EQ("123", ss.str());
- }
- TEST(UniversalPrintTest, WorksForCString) {
- const char* s1 = "abc";
- ::std::stringstream ss1;
- UniversalPrint(s1, &ss1);
- EXPECT_EQ(PrintPointer(s1) + " pointing to \"abc\"", std::string(ss1.str()));
- char* s2 = const_cast<char*>(s1);
- ::std::stringstream ss2;
- UniversalPrint(s2, &ss2);
- EXPECT_EQ(PrintPointer(s2) + " pointing to \"abc\"", std::string(ss2.str()));
- const char* s3 = NULL;
- ::std::stringstream ss3;
- UniversalPrint(s3, &ss3);
- EXPECT_EQ("NULL", ss3.str());
- }
- TEST(UniversalPrintTest, WorksForCharArray) {
- const char str[] = "\"Line\0 1\"\nLine 2";
- ::std::stringstream ss1;
- UniversalPrint(str, &ss1);
- EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss1.str());
- const char mutable_str[] = "\"Line\0 1\"\nLine 2";
- ::std::stringstream ss2;
- UniversalPrint(mutable_str, &ss2);
- EXPECT_EQ("\"\\\"Line\\0 1\\\"\\nLine 2\"", ss2.str());
- }
- #if GTEST_HAS_TR1_TUPLE
- TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsEmptyTuple) {
- Strings result = UniversalTersePrintTupleFieldsToStrings(
- ::std::tr1::make_tuple());
- EXPECT_EQ(0u, result.size());
- }
- TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsOneTuple) {
- Strings result = UniversalTersePrintTupleFieldsToStrings(
- ::std::tr1::make_tuple(1));
- ASSERT_EQ(1u, result.size());
- EXPECT_EQ("1", result[0]);
- }
- TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTwoTuple) {
- Strings result = UniversalTersePrintTupleFieldsToStrings(
- ::std::tr1::make_tuple(1, 'a'));
- ASSERT_EQ(2u, result.size());
- EXPECT_EQ("1", result[0]);
- EXPECT_EQ("'a' (97, 0x61)", result[1]);
- }
- TEST(UniversalTersePrintTupleFieldsToStringsTestWithTr1, PrintsTersely) {
- const int n = 1;
- Strings result = UniversalTersePrintTupleFieldsToStrings(
- ::std::tr1::tuple<const int&, const char*>(n, "a"));
- ASSERT_EQ(2u, result.size());
- EXPECT_EQ("1", result[0]);
- EXPECT_EQ("\"a\"", result[1]);
- }
- #endif
- #if GTEST_HAS_STD_TUPLE_
- TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsEmptyTuple) {
- Strings result = UniversalTersePrintTupleFieldsToStrings(::std::make_tuple());
- EXPECT_EQ(0u, result.size());
- }
- TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsOneTuple) {
- Strings result = UniversalTersePrintTupleFieldsToStrings(
- ::std::make_tuple(1));
- ASSERT_EQ(1u, result.size());
- EXPECT_EQ("1", result[0]);
- }
- TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTwoTuple) {
- Strings result = UniversalTersePrintTupleFieldsToStrings(
- ::std::make_tuple(1, 'a'));
- ASSERT_EQ(2u, result.size());
- EXPECT_EQ("1", result[0]);
- EXPECT_EQ("'a' (97, 0x61)", result[1]);
- }
- TEST(UniversalTersePrintTupleFieldsToStringsTestWithStd, PrintsTersely) {
- const int n = 1;
- Strings result = UniversalTersePrintTupleFieldsToStrings(
- ::std::tuple<const int&, const char*>(n, "a"));
- ASSERT_EQ(2u, result.size());
- EXPECT_EQ("1", result[0]);
- EXPECT_EQ("\"a\"", result[1]);
- }
- #endif
- #if GTEST_HAS_ABSL
- TEST(PrintOptionalTest, Basic) {
- absl::optional<int> value;
- EXPECT_EQ("(nullopt)", PrintToString(value));
- value = {7};
- EXPECT_EQ("(7)", PrintToString(value));
- EXPECT_EQ("(1.1)", PrintToString(absl::optional<double>{1.1}));
- EXPECT_EQ("(\"A\")", PrintToString(absl::optional<std::string>{"A"}));
- }
- struct NonPrintable {
- unsigned char contents = 17;
- };
- TEST(PrintOneofTest, Basic) {
- using Type = absl::variant<int, StreamableInGlobal, NonPrintable>;
- EXPECT_EQ("('int' with value 7)", PrintToString(Type(7)));
- EXPECT_EQ("('StreamableInGlobal' with value StreamableInGlobal)",
- PrintToString(Type(StreamableInGlobal{})));
- EXPECT_EQ(
- "('testing::gtest_printers_test::NonPrintable' with value 1-byte object "
- "<11>)",
- PrintToString(Type(NonPrintable{})));
- }
- #endif
- }
- }
|