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 "gmock/gmock-matchers.h"
00037 #include "gmock/gmock-more-matchers.h"
00038
00039 #include <string.h>
00040 #include <time.h>
00041 #include <deque>
00042 #include <functional>
00043 #include <iostream>
00044 #include <iterator>
00045 #include <limits>
00046 #include <list>
00047 #include <map>
00048 #include <set>
00049 #include <sstream>
00050 #include <string>
00051 #include <utility>
00052 #include <vector>
00053 #include "gmock/gmock.h"
00054 #include "gtest/gtest.h"
00055 #include "gtest/gtest-spi.h"
00056
00057 namespace testing {
00058
00059 namespace internal {
00060 GTEST_API_ string JoinAsTuple(const Strings& fields);
00061 }
00062
00063 namespace gmock_matchers_test {
00064
00065 using std::greater;
00066 using std::less;
00067 using std::list;
00068 using std::make_pair;
00069 using std::map;
00070 using std::multimap;
00071 using std::multiset;
00072 using std::ostream;
00073 using std::pair;
00074 using std::set;
00075 using std::stringstream;
00076 using std::tr1::get;
00077 using std::tr1::make_tuple;
00078 using std::tr1::tuple;
00079 using std::vector;
00080 using testing::A;
00081 using testing::AllArgs;
00082 using testing::AllOf;
00083 using testing::An;
00084 using testing::AnyOf;
00085 using testing::ByRef;
00086 using testing::ContainsRegex;
00087 using testing::DoubleEq;
00088 using testing::DoubleNear;
00089 using testing::EndsWith;
00090 using testing::Eq;
00091 using testing::ExplainMatchResult;
00092 using testing::Field;
00093 using testing::FloatEq;
00094 using testing::FloatNear;
00095 using testing::Ge;
00096 using testing::Gt;
00097 using testing::HasSubstr;
00098 using testing::IsEmpty;
00099 using testing::IsNull;
00100 using testing::Key;
00101 using testing::Le;
00102 using testing::Lt;
00103 using testing::MakeMatcher;
00104 using testing::MakePolymorphicMatcher;
00105 using testing::MatchResultListener;
00106 using testing::Matcher;
00107 using testing::MatcherCast;
00108 using testing::MatcherInterface;
00109 using testing::Matches;
00110 using testing::MatchesRegex;
00111 using testing::NanSensitiveDoubleEq;
00112 using testing::NanSensitiveDoubleNear;
00113 using testing::NanSensitiveFloatEq;
00114 using testing::NanSensitiveFloatNear;
00115 using testing::Ne;
00116 using testing::Not;
00117 using testing::NotNull;
00118 using testing::Pair;
00119 using testing::Pointee;
00120 using testing::Pointwise;
00121 using testing::PolymorphicMatcher;
00122 using testing::Property;
00123 using testing::Ref;
00124 using testing::ResultOf;
00125 using testing::SizeIs;
00126 using testing::StartsWith;
00127 using testing::StringMatchResultListener;
00128 using testing::StrCaseEq;
00129 using testing::StrCaseNe;
00130 using testing::StrEq;
00131 using testing::StrNe;
00132 using testing::Truly;
00133 using testing::TypedEq;
00134 using testing::Value;
00135 using testing::WhenSorted;
00136 using testing::WhenSortedBy;
00137 using testing::_;
00138 using testing::internal::DummyMatchResultListener;
00139 using testing::internal::ElementMatcherPair;
00140 using testing::internal::ElementMatcherPairs;
00141 using testing::internal::ExplainMatchFailureTupleTo;
00142 using testing::internal::FloatingEqMatcher;
00143 using testing::internal::FormatMatcherDescription;
00144 using testing::internal::IsReadableTypeName;
00145 using testing::internal::JoinAsTuple;
00146 using testing::internal::MatchMatrix;
00147 using testing::internal::RE;
00148 using testing::internal::StreamMatchResultListener;
00149 using testing::internal::Strings;
00150 using testing::internal::linked_ptr;
00151 using testing::internal::scoped_ptr;
00152 using testing::internal::string;
00153
00154
00155 #define GMOCK_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))
00156
00157
00158 class GreaterThanMatcher : public MatcherInterface<int> {
00159 public:
00160 explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
00161
00162 virtual void DescribeTo(ostream* os) const {
00163 *os << "is > " << rhs_;
00164 }
00165
00166 virtual bool MatchAndExplain(int lhs,
00167 MatchResultListener* listener) const {
00168 const int diff = lhs - rhs_;
00169 if (diff > 0) {
00170 *listener << "which is " << diff << " more than " << rhs_;
00171 } else if (diff == 0) {
00172 *listener << "which is the same as " << rhs_;
00173 } else {
00174 *listener << "which is " << -diff << " less than " << rhs_;
00175 }
00176
00177 return lhs > rhs_;
00178 }
00179
00180 private:
00181 int rhs_;
00182 };
00183
00184 Matcher<int> GreaterThan(int n) {
00185 return MakeMatcher(new GreaterThanMatcher(n));
00186 }
00187
00188 string OfType(const string& type_name) {
00189 #if GTEST_HAS_RTTI
00190 return " (of type " + type_name + ")";
00191 #else
00192 return "";
00193 #endif
00194 }
00195
00196
00197 template <typename T>
00198 string Describe(const Matcher<T>& m) {
00199 stringstream ss;
00200 m.DescribeTo(&ss);
00201 return ss.str();
00202 }
00203
00204
00205 template <typename T>
00206 string DescribeNegation(const Matcher<T>& m) {
00207 stringstream ss;
00208 m.DescribeNegationTo(&ss);
00209 return ss.str();
00210 }
00211
00212
00213 template <typename MatcherType, typename Value>
00214 string Explain(const MatcherType& m, const Value& x) {
00215 StringMatchResultListener listener;
00216 ExplainMatchResult(m, x, &listener);
00217 return listener.str();
00218 }
00219
00220 TEST(MatchResultListenerTest, StreamingWorks) {
00221 StringMatchResultListener listener;
00222 listener << "hi" << 5;
00223 EXPECT_EQ("hi5", listener.str());
00224
00225 listener.Clear();
00226 EXPECT_EQ("", listener.str());
00227
00228 listener << 42;
00229 EXPECT_EQ("42", listener.str());
00230
00231
00232 DummyMatchResultListener dummy;
00233 dummy << "hi" << 5;
00234 }
00235
00236 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
00237 EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
00238 EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
00239
00240 EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
00241 }
00242
00243 TEST(MatchResultListenerTest, IsInterestedWorks) {
00244 EXPECT_TRUE(StringMatchResultListener().IsInterested());
00245 EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
00246
00247 EXPECT_FALSE(DummyMatchResultListener().IsInterested());
00248 EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
00249 }
00250
00251
00252
00253 class EvenMatcherImpl : public MatcherInterface<int> {
00254 public:
00255 virtual bool MatchAndExplain(int x,
00256 MatchResultListener* ) const {
00257 return x % 2 == 0;
00258 }
00259
00260 virtual void DescribeTo(ostream* os) const {
00261 *os << "is an even number";
00262 }
00263
00264
00265
00266
00267 };
00268
00269
00270 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
00271 EvenMatcherImpl m;
00272 }
00273
00274
00275
00276 class NewEvenMatcherImpl : public MatcherInterface<int> {
00277 public:
00278 virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
00279 const bool match = x % 2 == 0;
00280
00281 *listener << "value % " << 2;
00282 if (listener->stream() != NULL) {
00283
00284
00285 *listener->stream() << " == " << (x % 2);
00286 }
00287 return match;
00288 }
00289
00290 virtual void DescribeTo(ostream* os) const {
00291 *os << "is an even number";
00292 }
00293 };
00294
00295 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
00296 Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
00297 EXPECT_TRUE(m.Matches(2));
00298 EXPECT_FALSE(m.Matches(3));
00299 EXPECT_EQ("value % 2 == 0", Explain(m, 2));
00300 EXPECT_EQ("value % 2 == 1", Explain(m, 3));
00301 }
00302
00303
00304 TEST(MatcherTest, CanBeDefaultConstructed) {
00305 Matcher<double> m;
00306 }
00307
00308
00309 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
00310 const MatcherInterface<int>* impl = new EvenMatcherImpl;
00311 Matcher<int> m(impl);
00312 EXPECT_TRUE(m.Matches(4));
00313 EXPECT_FALSE(m.Matches(5));
00314 }
00315
00316
00317 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
00318 Matcher<int> m1 = 5;
00319 EXPECT_TRUE(m1.Matches(5));
00320 EXPECT_FALSE(m1.Matches(6));
00321 }
00322
00323
00324 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
00325 Matcher<int*> m1 = NULL;
00326 EXPECT_TRUE(m1.Matches(NULL));
00327 int n = 0;
00328 EXPECT_FALSE(m1.Matches(&n));
00329 }
00330
00331
00332 TEST(MatcherTest, IsCopyable) {
00333
00334 Matcher<bool> m1 = Eq(false);
00335 EXPECT_TRUE(m1.Matches(false));
00336 EXPECT_FALSE(m1.Matches(true));
00337
00338
00339 m1 = Eq(true);
00340 EXPECT_TRUE(m1.Matches(true));
00341 EXPECT_FALSE(m1.Matches(false));
00342 }
00343
00344
00345
00346 TEST(MatcherTest, CanDescribeItself) {
00347 EXPECT_EQ("is an even number",
00348 Describe(Matcher<int>(new EvenMatcherImpl)));
00349 }
00350
00351
00352 TEST(MatcherTest, MatchAndExplain) {
00353 Matcher<int> m = GreaterThan(0);
00354 StringMatchResultListener listener1;
00355 EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
00356 EXPECT_EQ("which is 42 more than 0", listener1.str());
00357
00358 StringMatchResultListener listener2;
00359 EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
00360 EXPECT_EQ("which is 9 less than 0", listener2.str());
00361 }
00362
00363
00364
00365 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
00366 Matcher<string> m1 = "hi";
00367 EXPECT_TRUE(m1.Matches("hi"));
00368 EXPECT_FALSE(m1.Matches("hello"));
00369
00370 Matcher<const string&> m2 = "hi";
00371 EXPECT_TRUE(m2.Matches("hi"));
00372 EXPECT_FALSE(m2.Matches("hello"));
00373 }
00374
00375
00376
00377 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
00378 Matcher<string> m1 = string("hi");
00379 EXPECT_TRUE(m1.Matches("hi"));
00380 EXPECT_FALSE(m1.Matches("hello"));
00381
00382 Matcher<const string&> m2 = string("hi");
00383 EXPECT_TRUE(m2.Matches("hi"));
00384 EXPECT_FALSE(m2.Matches("hello"));
00385 }
00386
00387 #if GTEST_HAS_STRING_PIECE_
00388
00389
00390 TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
00391 Matcher<StringPiece> m1 = "cats";
00392 EXPECT_TRUE(m1.Matches("cats"));
00393 EXPECT_FALSE(m1.Matches("dogs"));
00394
00395 Matcher<const StringPiece&> m2 = "cats";
00396 EXPECT_TRUE(m2.Matches("cats"));
00397 EXPECT_FALSE(m2.Matches("dogs"));
00398 }
00399
00400
00401
00402 TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromString) {
00403 Matcher<StringPiece> m1 = string("cats");
00404 EXPECT_TRUE(m1.Matches("cats"));
00405 EXPECT_FALSE(m1.Matches("dogs"));
00406
00407 Matcher<const StringPiece&> m2 = string("cats");
00408 EXPECT_TRUE(m2.Matches("cats"));
00409 EXPECT_FALSE(m2.Matches("dogs"));
00410 }
00411
00412
00413
00414 TEST(StringPieceMatcherTest, CanBeImplicitlyConstructedFromStringPiece) {
00415 Matcher<StringPiece> m1 = StringPiece("cats");
00416 EXPECT_TRUE(m1.Matches("cats"));
00417 EXPECT_FALSE(m1.Matches("dogs"));
00418
00419 Matcher<const StringPiece&> m2 = StringPiece("cats");
00420 EXPECT_TRUE(m2.Matches("cats"));
00421 EXPECT_FALSE(m2.Matches("dogs"));
00422 }
00423 #endif // GTEST_HAS_STRING_PIECE_
00424
00425
00426
00427
00428 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
00429 const MatcherInterface<int>* dummy_impl = NULL;
00430 Matcher<int> m = MakeMatcher(dummy_impl);
00431 }
00432
00433
00434
00435 const int g_bar = 1;
00436 class ReferencesBarOrIsZeroImpl {
00437 public:
00438 template <typename T>
00439 bool MatchAndExplain(const T& x,
00440 MatchResultListener* ) const {
00441 const void* p = &x;
00442 return p == &g_bar || x == 0;
00443 }
00444
00445 void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
00446
00447 void DescribeNegationTo(ostream* os) const {
00448 *os << "doesn't reference g_bar and is not zero";
00449 }
00450 };
00451
00452
00453
00454 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
00455 return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
00456 }
00457
00458 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
00459
00460 Matcher<const int&> m1 = ReferencesBarOrIsZero();
00461 EXPECT_TRUE(m1.Matches(0));
00462
00463 EXPECT_TRUE(m1.Matches(g_bar));
00464 EXPECT_FALSE(m1.Matches(1));
00465 EXPECT_EQ("g_bar or zero", Describe(m1));
00466
00467
00468 Matcher<double> m2 = ReferencesBarOrIsZero();
00469 EXPECT_TRUE(m2.Matches(0.0));
00470 EXPECT_FALSE(m2.Matches(0.1));
00471 EXPECT_EQ("g_bar or zero", Describe(m2));
00472 }
00473
00474
00475
00476 class PolymorphicIsEvenImpl {
00477 public:
00478 void DescribeTo(ostream* os) const { *os << "is even"; }
00479
00480 void DescribeNegationTo(ostream* os) const {
00481 *os << "is odd";
00482 }
00483
00484 template <typename T>
00485 bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
00486
00487 *listener << "% " << 2;
00488 if (listener->stream() != NULL) {
00489
00490
00491 *listener->stream() << " == " << (x % 2);
00492 }
00493 return (x % 2) == 0;
00494 }
00495 };
00496
00497 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
00498 return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
00499 }
00500
00501 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
00502
00503 const Matcher<int> m1 = PolymorphicIsEven();
00504 EXPECT_TRUE(m1.Matches(42));
00505 EXPECT_FALSE(m1.Matches(43));
00506 EXPECT_EQ("is even", Describe(m1));
00507
00508 const Matcher<int> not_m1 = Not(m1);
00509 EXPECT_EQ("is odd", Describe(not_m1));
00510
00511 EXPECT_EQ("% 2 == 0", Explain(m1, 42));
00512
00513
00514 const Matcher<char> m2 = PolymorphicIsEven();
00515 EXPECT_TRUE(m2.Matches('\x42'));
00516 EXPECT_FALSE(m2.Matches('\x43'));
00517 EXPECT_EQ("is even", Describe(m2));
00518
00519 const Matcher<char> not_m2 = Not(m2);
00520 EXPECT_EQ("is odd", Describe(not_m2));
00521
00522 EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
00523 }
00524
00525
00526 TEST(MatcherCastTest, FromPolymorphicMatcher) {
00527 Matcher<int> m = MatcherCast<int>(Eq(5));
00528 EXPECT_TRUE(m.Matches(5));
00529 EXPECT_FALSE(m.Matches(6));
00530 }
00531
00532
00533 class IntValue {
00534 public:
00535
00536
00537 explicit IntValue(int a_value) : value_(a_value) {}
00538
00539 int value() const { return value_; }
00540 private:
00541 int value_;
00542 };
00543
00544
00545 bool IsPositiveIntValue(const IntValue& foo) {
00546 return foo.value() > 0;
00547 }
00548
00549
00550
00551 TEST(MatcherCastTest, FromCompatibleType) {
00552 Matcher<double> m1 = Eq(2.0);
00553 Matcher<int> m2 = MatcherCast<int>(m1);
00554 EXPECT_TRUE(m2.Matches(2));
00555 EXPECT_FALSE(m2.Matches(3));
00556
00557 Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
00558 Matcher<int> m4 = MatcherCast<int>(m3);
00559
00560
00561
00562 EXPECT_TRUE(m4.Matches(1));
00563 EXPECT_FALSE(m4.Matches(0));
00564 }
00565
00566
00567 TEST(MatcherCastTest, FromConstReferenceToNonReference) {
00568 Matcher<const int&> m1 = Eq(0);
00569 Matcher<int> m2 = MatcherCast<int>(m1);
00570 EXPECT_TRUE(m2.Matches(0));
00571 EXPECT_FALSE(m2.Matches(1));
00572 }
00573
00574
00575 TEST(MatcherCastTest, FromReferenceToNonReference) {
00576 Matcher<int&> m1 = Eq(0);
00577 Matcher<int> m2 = MatcherCast<int>(m1);
00578 EXPECT_TRUE(m2.Matches(0));
00579 EXPECT_FALSE(m2.Matches(1));
00580 }
00581
00582
00583 TEST(MatcherCastTest, FromNonReferenceToConstReference) {
00584 Matcher<int> m1 = Eq(0);
00585 Matcher<const int&> m2 = MatcherCast<const int&>(m1);
00586 EXPECT_TRUE(m2.Matches(0));
00587 EXPECT_FALSE(m2.Matches(1));
00588 }
00589
00590
00591 TEST(MatcherCastTest, FromNonReferenceToReference) {
00592 Matcher<int> m1 = Eq(0);
00593 Matcher<int&> m2 = MatcherCast<int&>(m1);
00594 int n = 0;
00595 EXPECT_TRUE(m2.Matches(n));
00596 n = 1;
00597 EXPECT_FALSE(m2.Matches(n));
00598 }
00599
00600
00601 TEST(MatcherCastTest, FromSameType) {
00602 Matcher<int> m1 = Eq(0);
00603 Matcher<int> m2 = MatcherCast<int>(m1);
00604 EXPECT_TRUE(m2.Matches(0));
00605 EXPECT_FALSE(m2.Matches(1));
00606 }
00607
00608
00609 struct ConvertibleFromAny {
00610 ConvertibleFromAny(int a_value) : value(a_value) {}
00611 template <typename T>
00612 ConvertibleFromAny(const T& a_value) : value(-1) {
00613 ADD_FAILURE() << "Conversion constructor called";
00614 }
00615 int value;
00616 };
00617
00618 bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
00619 return a.value == b.value;
00620 }
00621
00622 ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
00623 return os << a.value;
00624 }
00625
00626 TEST(MatcherCastTest, ConversionConstructorIsUsed) {
00627 Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
00628 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
00629 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
00630 }
00631
00632 TEST(MatcherCastTest, FromConvertibleFromAny) {
00633 Matcher<ConvertibleFromAny> m =
00634 MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
00635 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
00636 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
00637 }
00638
00639 class Base {};
00640 class Derived : public Base {};
00641
00642
00643 TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
00644 Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
00645 EXPECT_TRUE(m2.Matches(' '));
00646 EXPECT_FALSE(m2.Matches('\n'));
00647 }
00648
00649
00650
00651
00652 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
00653 Matcher<double> m1 = DoubleEq(1.0);
00654 Matcher<float> m2 = SafeMatcherCast<float>(m1);
00655 EXPECT_TRUE(m2.Matches(1.0f));
00656 EXPECT_FALSE(m2.Matches(2.0f));
00657
00658 Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
00659 EXPECT_TRUE(m3.Matches('a'));
00660 EXPECT_FALSE(m3.Matches('b'));
00661 }
00662
00663
00664
00665 TEST(SafeMatcherCastTest, FromBaseClass) {
00666 Derived d, d2;
00667 Matcher<Base*> m1 = Eq(&d);
00668 Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
00669 EXPECT_TRUE(m2.Matches(&d));
00670 EXPECT_FALSE(m2.Matches(&d2));
00671
00672 Matcher<Base&> m3 = Ref(d);
00673 Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
00674 EXPECT_TRUE(m4.Matches(d));
00675 EXPECT_FALSE(m4.Matches(d2));
00676 }
00677
00678
00679 TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
00680 int n = 0;
00681 Matcher<const int&> m1 = Ref(n);
00682 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
00683 int n1 = 0;
00684 EXPECT_TRUE(m2.Matches(n));
00685 EXPECT_FALSE(m2.Matches(n1));
00686 }
00687
00688
00689 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
00690 Matcher<int> m1 = Eq(0);
00691 Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
00692 EXPECT_TRUE(m2.Matches(0));
00693 EXPECT_FALSE(m2.Matches(1));
00694 }
00695
00696
00697 TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
00698 Matcher<int> m1 = Eq(0);
00699 Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
00700 int n = 0;
00701 EXPECT_TRUE(m2.Matches(n));
00702 n = 1;
00703 EXPECT_FALSE(m2.Matches(n));
00704 }
00705
00706
00707 TEST(SafeMatcherCastTest, FromSameType) {
00708 Matcher<int> m1 = Eq(0);
00709 Matcher<int> m2 = SafeMatcherCast<int>(m1);
00710 EXPECT_TRUE(m2.Matches(0));
00711 EXPECT_FALSE(m2.Matches(1));
00712 }
00713
00714 TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
00715 Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
00716 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
00717 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
00718 }
00719
00720 TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
00721 Matcher<ConvertibleFromAny> m =
00722 SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
00723 EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
00724 EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
00725 }
00726
00727
00728 TEST(ATest, MatchesAnyValue) {
00729
00730 Matcher<double> m1 = A<double>();
00731 EXPECT_TRUE(m1.Matches(91.43));
00732 EXPECT_TRUE(m1.Matches(-15.32));
00733
00734
00735 int a = 2;
00736 int b = -6;
00737 Matcher<int&> m2 = A<int&>();
00738 EXPECT_TRUE(m2.Matches(a));
00739 EXPECT_TRUE(m2.Matches(b));
00740 }
00741
00742 TEST(ATest, WorksForDerivedClass) {
00743 Base base;
00744 Derived derived;
00745 EXPECT_THAT(&base, A<Base*>());
00746
00747 EXPECT_THAT(&derived, A<Base*>());
00748 EXPECT_THAT(&derived, A<Derived*>());
00749 }
00750
00751
00752 TEST(ATest, CanDescribeSelf) {
00753 EXPECT_EQ("is anything", Describe(A<bool>()));
00754 }
00755
00756
00757 TEST(AnTest, MatchesAnyValue) {
00758
00759 Matcher<int> m1 = An<int>();
00760 EXPECT_TRUE(m1.Matches(9143));
00761 EXPECT_TRUE(m1.Matches(-1532));
00762
00763
00764 int a = 2;
00765 int b = -6;
00766 Matcher<int&> m2 = An<int&>();
00767 EXPECT_TRUE(m2.Matches(a));
00768 EXPECT_TRUE(m2.Matches(b));
00769 }
00770
00771
00772 TEST(AnTest, CanDescribeSelf) {
00773 EXPECT_EQ("is anything", Describe(An<int>()));
00774 }
00775
00776
00777
00778 TEST(UnderscoreTest, MatchesAnyValue) {
00779
00780 Matcher<int> m1 = _;
00781 EXPECT_TRUE(m1.Matches(123));
00782 EXPECT_TRUE(m1.Matches(-242));
00783
00784
00785 bool a = false;
00786 const bool b = true;
00787 Matcher<const bool&> m2 = _;
00788 EXPECT_TRUE(m2.Matches(a));
00789 EXPECT_TRUE(m2.Matches(b));
00790 }
00791
00792
00793 TEST(UnderscoreTest, CanDescribeSelf) {
00794 Matcher<int> m = _;
00795 EXPECT_EQ("is anything", Describe(m));
00796 }
00797
00798
00799 TEST(EqTest, MatchesEqualValue) {
00800
00801 const char a1[] = "hi";
00802 const char a2[] = "hi";
00803
00804 Matcher<const char*> m1 = Eq(a1);
00805 EXPECT_TRUE(m1.Matches(a1));
00806 EXPECT_FALSE(m1.Matches(a2));
00807 }
00808
00809
00810
00811 class Unprintable {
00812 public:
00813 Unprintable() : c_('a') {}
00814
00815 bool operator==(const Unprintable& ) { return true; }
00816 private:
00817 char c_;
00818 };
00819
00820 TEST(EqTest, CanDescribeSelf) {
00821 Matcher<Unprintable> m = Eq(Unprintable());
00822 EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
00823 }
00824
00825
00826
00827 TEST(EqTest, IsPolymorphic) {
00828 Matcher<int> m1 = Eq(1);
00829 EXPECT_TRUE(m1.Matches(1));
00830 EXPECT_FALSE(m1.Matches(2));
00831
00832 Matcher<char> m2 = Eq(1);
00833 EXPECT_TRUE(m2.Matches('\1'));
00834 EXPECT_FALSE(m2.Matches('a'));
00835 }
00836
00837
00838 TEST(TypedEqTest, ChecksEqualityForGivenType) {
00839 Matcher<char> m1 = TypedEq<char>('a');
00840 EXPECT_TRUE(m1.Matches('a'));
00841 EXPECT_FALSE(m1.Matches('b'));
00842
00843 Matcher<int> m2 = TypedEq<int>(6);
00844 EXPECT_TRUE(m2.Matches(6));
00845 EXPECT_FALSE(m2.Matches(7));
00846 }
00847
00848
00849 TEST(TypedEqTest, CanDescribeSelf) {
00850 EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
00851 }
00852
00853
00854
00855
00856
00857
00858
00859 template <typename T>
00860 struct Type {
00861 static bool IsTypeOf(const T& ) { return true; }
00862
00863 template <typename T2>
00864 static void IsTypeOf(T2 v);
00865 };
00866
00867 TEST(TypedEqTest, HasSpecifiedType) {
00868
00869 Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
00870 Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
00871 }
00872
00873
00874 TEST(GeTest, ImplementsGreaterThanOrEqual) {
00875 Matcher<int> m1 = Ge(0);
00876 EXPECT_TRUE(m1.Matches(1));
00877 EXPECT_TRUE(m1.Matches(0));
00878 EXPECT_FALSE(m1.Matches(-1));
00879 }
00880
00881
00882 TEST(GeTest, CanDescribeSelf) {
00883 Matcher<int> m = Ge(5);
00884 EXPECT_EQ("is >= 5", Describe(m));
00885 }
00886
00887
00888 TEST(GtTest, ImplementsGreaterThan) {
00889 Matcher<double> m1 = Gt(0);
00890 EXPECT_TRUE(m1.Matches(1.0));
00891 EXPECT_FALSE(m1.Matches(0.0));
00892 EXPECT_FALSE(m1.Matches(-1.0));
00893 }
00894
00895
00896 TEST(GtTest, CanDescribeSelf) {
00897 Matcher<int> m = Gt(5);
00898 EXPECT_EQ("is > 5", Describe(m));
00899 }
00900
00901
00902 TEST(LeTest, ImplementsLessThanOrEqual) {
00903 Matcher<char> m1 = Le('b');
00904 EXPECT_TRUE(m1.Matches('a'));
00905 EXPECT_TRUE(m1.Matches('b'));
00906 EXPECT_FALSE(m1.Matches('c'));
00907 }
00908
00909
00910 TEST(LeTest, CanDescribeSelf) {
00911 Matcher<int> m = Le(5);
00912 EXPECT_EQ("is <= 5", Describe(m));
00913 }
00914
00915
00916 TEST(LtTest, ImplementsLessThan) {
00917 Matcher<const string&> m1 = Lt("Hello");
00918 EXPECT_TRUE(m1.Matches("Abc"));
00919 EXPECT_FALSE(m1.Matches("Hello"));
00920 EXPECT_FALSE(m1.Matches("Hello, world!"));
00921 }
00922
00923
00924 TEST(LtTest, CanDescribeSelf) {
00925 Matcher<int> m = Lt(5);
00926 EXPECT_EQ("is < 5", Describe(m));
00927 }
00928
00929
00930 TEST(NeTest, ImplementsNotEqual) {
00931 Matcher<int> m1 = Ne(0);
00932 EXPECT_TRUE(m1.Matches(1));
00933 EXPECT_TRUE(m1.Matches(-1));
00934 EXPECT_FALSE(m1.Matches(0));
00935 }
00936
00937
00938 TEST(NeTest, CanDescribeSelf) {
00939 Matcher<int> m = Ne(5);
00940 EXPECT_EQ("isn't equal to 5", Describe(m));
00941 }
00942
00943
00944 TEST(IsNullTest, MatchesNullPointer) {
00945 Matcher<int*> m1 = IsNull();
00946 int* p1 = NULL;
00947 int n = 0;
00948 EXPECT_TRUE(m1.Matches(p1));
00949 EXPECT_FALSE(m1.Matches(&n));
00950
00951 Matcher<const char*> m2 = IsNull();
00952 const char* p2 = NULL;
00953 EXPECT_TRUE(m2.Matches(p2));
00954 EXPECT_FALSE(m2.Matches("hi"));
00955
00956 #if !GTEST_OS_SYMBIAN
00957
00958
00959
00960
00961
00962
00963
00964
00965 Matcher<void*> m3 = IsNull();
00966 void* p3 = NULL;
00967 EXPECT_TRUE(m3.Matches(p3));
00968 EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
00969 #endif
00970 }
00971
00972 TEST(IsNullTest, LinkedPtr) {
00973 const Matcher<linked_ptr<int> > m = IsNull();
00974 const linked_ptr<int> null_p;
00975 const linked_ptr<int> non_null_p(new int);
00976
00977 EXPECT_TRUE(m.Matches(null_p));
00978 EXPECT_FALSE(m.Matches(non_null_p));
00979 }
00980
00981 TEST(IsNullTest, ReferenceToConstLinkedPtr) {
00982 const Matcher<const linked_ptr<double>&> m = IsNull();
00983 const linked_ptr<double> null_p;
00984 const linked_ptr<double> non_null_p(new double);
00985
00986 EXPECT_TRUE(m.Matches(null_p));
00987 EXPECT_FALSE(m.Matches(non_null_p));
00988 }
00989
00990 TEST(IsNullTest, ReferenceToConstScopedPtr) {
00991 const Matcher<const scoped_ptr<double>&> m = IsNull();
00992 const scoped_ptr<double> null_p;
00993 const scoped_ptr<double> non_null_p(new double);
00994
00995 EXPECT_TRUE(m.Matches(null_p));
00996 EXPECT_FALSE(m.Matches(non_null_p));
00997 }
00998
00999
01000 TEST(IsNullTest, CanDescribeSelf) {
01001 Matcher<int*> m = IsNull();
01002 EXPECT_EQ("is NULL", Describe(m));
01003 EXPECT_EQ("isn't NULL", DescribeNegation(m));
01004 }
01005
01006
01007 TEST(NotNullTest, MatchesNonNullPointer) {
01008 Matcher<int*> m1 = NotNull();
01009 int* p1 = NULL;
01010 int n = 0;
01011 EXPECT_FALSE(m1.Matches(p1));
01012 EXPECT_TRUE(m1.Matches(&n));
01013
01014 Matcher<const char*> m2 = NotNull();
01015 const char* p2 = NULL;
01016 EXPECT_FALSE(m2.Matches(p2));
01017 EXPECT_TRUE(m2.Matches("hi"));
01018 }
01019
01020 TEST(NotNullTest, LinkedPtr) {
01021 const Matcher<linked_ptr<int> > m = NotNull();
01022 const linked_ptr<int> null_p;
01023 const linked_ptr<int> non_null_p(new int);
01024
01025 EXPECT_FALSE(m.Matches(null_p));
01026 EXPECT_TRUE(m.Matches(non_null_p));
01027 }
01028
01029 TEST(NotNullTest, ReferenceToConstLinkedPtr) {
01030 const Matcher<const linked_ptr<double>&> m = NotNull();
01031 const linked_ptr<double> null_p;
01032 const linked_ptr<double> non_null_p(new double);
01033
01034 EXPECT_FALSE(m.Matches(null_p));
01035 EXPECT_TRUE(m.Matches(non_null_p));
01036 }
01037
01038 TEST(NotNullTest, ReferenceToConstScopedPtr) {
01039 const Matcher<const scoped_ptr<double>&> m = NotNull();
01040 const scoped_ptr<double> null_p;
01041 const scoped_ptr<double> non_null_p(new double);
01042
01043 EXPECT_FALSE(m.Matches(null_p));
01044 EXPECT_TRUE(m.Matches(non_null_p));
01045 }
01046
01047
01048 TEST(NotNullTest, CanDescribeSelf) {
01049 Matcher<int*> m = NotNull();
01050 EXPECT_EQ("isn't NULL", Describe(m));
01051 }
01052
01053
01054
01055 TEST(RefTest, MatchesSameVariable) {
01056 int a = 0;
01057 int b = 0;
01058 Matcher<int&> m = Ref(a);
01059 EXPECT_TRUE(m.Matches(a));
01060 EXPECT_FALSE(m.Matches(b));
01061 }
01062
01063
01064 TEST(RefTest, CanDescribeSelf) {
01065 int n = 5;
01066 Matcher<int&> m = Ref(n);
01067 stringstream ss;
01068 ss << "references the variable @" << &n << " 5";
01069 EXPECT_EQ(string(ss.str()), Describe(m));
01070 }
01071
01072
01073
01074 TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
01075 int a = 0;
01076 int b = 0;
01077 Matcher<const int&> m = Ref(a);
01078 EXPECT_TRUE(m.Matches(a));
01079 EXPECT_FALSE(m.Matches(b));
01080 }
01081
01082
01083
01084
01085
01086 TEST(RefTest, IsCovariant) {
01087 Base base, base2;
01088 Derived derived;
01089 Matcher<const Base&> m1 = Ref(base);
01090 EXPECT_TRUE(m1.Matches(base));
01091 EXPECT_FALSE(m1.Matches(base2));
01092 EXPECT_FALSE(m1.Matches(derived));
01093
01094 m1 = Ref(derived);
01095 EXPECT_TRUE(m1.Matches(derived));
01096 EXPECT_FALSE(m1.Matches(base));
01097 EXPECT_FALSE(m1.Matches(base2));
01098 }
01099
01100 TEST(RefTest, ExplainsResult) {
01101 int n = 0;
01102 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
01103 StartsWith("which is located @"));
01104
01105 int m = 0;
01106 EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
01107 StartsWith("which is located @"));
01108 }
01109
01110
01111
01112 TEST(StrEqTest, MatchesEqualString) {
01113 Matcher<const char*> m = StrEq(string("Hello"));
01114 EXPECT_TRUE(m.Matches("Hello"));
01115 EXPECT_FALSE(m.Matches("hello"));
01116 EXPECT_FALSE(m.Matches(NULL));
01117
01118 Matcher<const string&> m2 = StrEq("Hello");
01119 EXPECT_TRUE(m2.Matches("Hello"));
01120 EXPECT_FALSE(m2.Matches("Hi"));
01121 }
01122
01123 TEST(StrEqTest, CanDescribeSelf) {
01124 Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
01125 EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
01126 Describe(m));
01127
01128 string str("01204500800");
01129 str[3] = '\0';
01130 Matcher<string> m2 = StrEq(str);
01131 EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
01132 str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
01133 Matcher<string> m3 = StrEq(str);
01134 EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
01135 }
01136
01137 TEST(StrNeTest, MatchesUnequalString) {
01138 Matcher<const char*> m = StrNe("Hello");
01139 EXPECT_TRUE(m.Matches(""));
01140 EXPECT_TRUE(m.Matches(NULL));
01141 EXPECT_FALSE(m.Matches("Hello"));
01142
01143 Matcher<string> m2 = StrNe(string("Hello"));
01144 EXPECT_TRUE(m2.Matches("hello"));
01145 EXPECT_FALSE(m2.Matches("Hello"));
01146 }
01147
01148 TEST(StrNeTest, CanDescribeSelf) {
01149 Matcher<const char*> m = StrNe("Hi");
01150 EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
01151 }
01152
01153 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
01154 Matcher<const char*> m = StrCaseEq(string("Hello"));
01155 EXPECT_TRUE(m.Matches("Hello"));
01156 EXPECT_TRUE(m.Matches("hello"));
01157 EXPECT_FALSE(m.Matches("Hi"));
01158 EXPECT_FALSE(m.Matches(NULL));
01159
01160 Matcher<const string&> m2 = StrCaseEq("Hello");
01161 EXPECT_TRUE(m2.Matches("hello"));
01162 EXPECT_FALSE(m2.Matches("Hi"));
01163 }
01164
01165 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
01166 string str1("oabocdooeoo");
01167 string str2("OABOCDOOEOO");
01168 Matcher<const string&> m0 = StrCaseEq(str1);
01169 EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
01170
01171 str1[3] = str2[3] = '\0';
01172 Matcher<const string&> m1 = StrCaseEq(str1);
01173 EXPECT_TRUE(m1.Matches(str2));
01174
01175 str1[0] = str1[6] = str1[7] = str1[10] = '\0';
01176 str2[0] = str2[6] = str2[7] = str2[10] = '\0';
01177 Matcher<const string&> m2 = StrCaseEq(str1);
01178 str1[9] = str2[9] = '\0';
01179 EXPECT_FALSE(m2.Matches(str2));
01180
01181 Matcher<const string&> m3 = StrCaseEq(str1);
01182 EXPECT_TRUE(m3.Matches(str2));
01183
01184 EXPECT_FALSE(m3.Matches(str2 + "x"));
01185 str2.append(1, '\0');
01186 EXPECT_FALSE(m3.Matches(str2));
01187 EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
01188 }
01189
01190 TEST(StrCaseEqTest, CanDescribeSelf) {
01191 Matcher<string> m = StrCaseEq("Hi");
01192 EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
01193 }
01194
01195 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
01196 Matcher<const char*> m = StrCaseNe("Hello");
01197 EXPECT_TRUE(m.Matches("Hi"));
01198 EXPECT_TRUE(m.Matches(NULL));
01199 EXPECT_FALSE(m.Matches("Hello"));
01200 EXPECT_FALSE(m.Matches("hello"));
01201
01202 Matcher<string> m2 = StrCaseNe(string("Hello"));
01203 EXPECT_TRUE(m2.Matches(""));
01204 EXPECT_FALSE(m2.Matches("Hello"));
01205 }
01206
01207 TEST(StrCaseNeTest, CanDescribeSelf) {
01208 Matcher<const char*> m = StrCaseNe("Hi");
01209 EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
01210 }
01211
01212
01213 TEST(HasSubstrTest, WorksForStringClasses) {
01214 const Matcher<string> m1 = HasSubstr("foo");
01215 EXPECT_TRUE(m1.Matches(string("I love food.")));
01216 EXPECT_FALSE(m1.Matches(string("tofo")));
01217
01218 const Matcher<const std::string&> m2 = HasSubstr("foo");
01219 EXPECT_TRUE(m2.Matches(std::string("I love food.")));
01220 EXPECT_FALSE(m2.Matches(std::string("tofo")));
01221 }
01222
01223
01224 TEST(HasSubstrTest, WorksForCStrings) {
01225 const Matcher<char*> m1 = HasSubstr("foo");
01226 EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
01227 EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
01228 EXPECT_FALSE(m1.Matches(NULL));
01229
01230 const Matcher<const char*> m2 = HasSubstr("foo");
01231 EXPECT_TRUE(m2.Matches("I love food."));
01232 EXPECT_FALSE(m2.Matches("tofo"));
01233 EXPECT_FALSE(m2.Matches(NULL));
01234 }
01235
01236
01237 TEST(HasSubstrTest, CanDescribeSelf) {
01238 Matcher<string> m = HasSubstr("foo\n\"");
01239 EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
01240 }
01241
01242 TEST(KeyTest, CanDescribeSelf) {
01243 Matcher<const pair<std::string, int>&> m = Key("foo");
01244 EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
01245 EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
01246 }
01247
01248 TEST(KeyTest, ExplainsResult) {
01249 Matcher<pair<int, bool> > m = Key(GreaterThan(10));
01250 EXPECT_EQ("whose first field is a value which is 5 less than 10",
01251 Explain(m, make_pair(5, true)));
01252 EXPECT_EQ("whose first field is a value which is 5 more than 10",
01253 Explain(m, make_pair(15, true)));
01254 }
01255
01256 TEST(KeyTest, MatchesCorrectly) {
01257 pair<int, std::string> p(25, "foo");
01258 EXPECT_THAT(p, Key(25));
01259 EXPECT_THAT(p, Not(Key(42)));
01260 EXPECT_THAT(p, Key(Ge(20)));
01261 EXPECT_THAT(p, Not(Key(Lt(25))));
01262 }
01263
01264 TEST(KeyTest, SafelyCastsInnerMatcher) {
01265 Matcher<int> is_positive = Gt(0);
01266 Matcher<int> is_negative = Lt(0);
01267 pair<char, bool> p('a', true);
01268 EXPECT_THAT(p, Key(is_positive));
01269 EXPECT_THAT(p, Not(Key(is_negative)));
01270 }
01271
01272 TEST(KeyTest, InsideContainsUsingMap) {
01273 map<int, char> container;
01274 container.insert(make_pair(1, 'a'));
01275 container.insert(make_pair(2, 'b'));
01276 container.insert(make_pair(4, 'c'));
01277 EXPECT_THAT(container, Contains(Key(1)));
01278 EXPECT_THAT(container, Not(Contains(Key(3))));
01279 }
01280
01281 TEST(KeyTest, InsideContainsUsingMultimap) {
01282 multimap<int, char> container;
01283 container.insert(make_pair(1, 'a'));
01284 container.insert(make_pair(2, 'b'));
01285 container.insert(make_pair(4, 'c'));
01286
01287 EXPECT_THAT(container, Not(Contains(Key(25))));
01288 container.insert(make_pair(25, 'd'));
01289 EXPECT_THAT(container, Contains(Key(25)));
01290 container.insert(make_pair(25, 'e'));
01291 EXPECT_THAT(container, Contains(Key(25)));
01292
01293 EXPECT_THAT(container, Contains(Key(1)));
01294 EXPECT_THAT(container, Not(Contains(Key(3))));
01295 }
01296
01297 TEST(PairTest, Typing) {
01298
01299 Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
01300 Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
01301 Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
01302
01303 Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
01304 Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
01305 }
01306
01307 TEST(PairTest, CanDescribeSelf) {
01308 Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
01309 EXPECT_EQ("has a first field that is equal to \"foo\""
01310 ", and has a second field that is equal to 42",
01311 Describe(m1));
01312 EXPECT_EQ("has a first field that isn't equal to \"foo\""
01313 ", or has a second field that isn't equal to 42",
01314 DescribeNegation(m1));
01315
01316 Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
01317 EXPECT_EQ("has a first field that isn't equal to 13"
01318 ", and has a second field that is equal to 42",
01319 DescribeNegation(m2));
01320 }
01321
01322 TEST(PairTest, CanExplainMatchResultTo) {
01323
01324
01325 const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
01326 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
01327 Explain(m, make_pair(-1, -2)));
01328
01329
01330
01331 EXPECT_EQ("whose second field does not match, which is 2 less than 0",
01332 Explain(m, make_pair(1, -2)));
01333
01334
01335
01336 EXPECT_EQ("whose first field does not match, which is 1 less than 0",
01337 Explain(m, make_pair(-1, 2)));
01338
01339
01340 EXPECT_EQ("whose both fields match, where the first field is a value "
01341 "which is 1 more than 0, and the second field is a value "
01342 "which is 2 more than 0",
01343 Explain(m, make_pair(1, 2)));
01344
01345
01346
01347 const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
01348 EXPECT_EQ("whose both fields match, where the first field is a value "
01349 "which is 1 more than 0",
01350 Explain(explain_first, make_pair(1, 0)));
01351
01352
01353
01354 const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
01355 EXPECT_EQ("whose both fields match, where the second field is a value "
01356 "which is 1 more than 0",
01357 Explain(explain_second, make_pair(0, 1)));
01358 }
01359
01360 TEST(PairTest, MatchesCorrectly) {
01361 pair<int, std::string> p(25, "foo");
01362
01363
01364 EXPECT_THAT(p, Pair(25, "foo"));
01365 EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
01366
01367
01368 EXPECT_THAT(p, Not(Pair(42, "foo")));
01369 EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
01370
01371
01372 EXPECT_THAT(p, Not(Pair(25, "bar")));
01373 EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
01374
01375
01376 EXPECT_THAT(p, Not(Pair(13, "bar")));
01377 EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
01378 }
01379
01380 TEST(PairTest, SafelyCastsInnerMatchers) {
01381 Matcher<int> is_positive = Gt(0);
01382 Matcher<int> is_negative = Lt(0);
01383 pair<char, bool> p('a', true);
01384 EXPECT_THAT(p, Pair(is_positive, _));
01385 EXPECT_THAT(p, Not(Pair(is_negative, _)));
01386 EXPECT_THAT(p, Pair(_, is_positive));
01387 EXPECT_THAT(p, Not(Pair(_, is_negative)));
01388 }
01389
01390 TEST(PairTest, InsideContainsUsingMap) {
01391 map<int, char> container;
01392 container.insert(make_pair(1, 'a'));
01393 container.insert(make_pair(2, 'b'));
01394 container.insert(make_pair(4, 'c'));
01395 EXPECT_THAT(container, Contains(Pair(1, 'a')));
01396 EXPECT_THAT(container, Contains(Pair(1, _)));
01397 EXPECT_THAT(container, Contains(Pair(_, 'a')));
01398 EXPECT_THAT(container, Not(Contains(Pair(3, _))));
01399 }
01400
01401
01402
01403 TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
01404 const Matcher<const char*> m1 = StartsWith(string(""));
01405 EXPECT_TRUE(m1.Matches("Hi"));
01406 EXPECT_TRUE(m1.Matches(""));
01407 EXPECT_FALSE(m1.Matches(NULL));
01408
01409 const Matcher<const string&> m2 = StartsWith("Hi");
01410 EXPECT_TRUE(m2.Matches("Hi"));
01411 EXPECT_TRUE(m2.Matches("Hi Hi!"));
01412 EXPECT_TRUE(m2.Matches("High"));
01413 EXPECT_FALSE(m2.Matches("H"));
01414 EXPECT_FALSE(m2.Matches(" Hi"));
01415 }
01416
01417 TEST(StartsWithTest, CanDescribeSelf) {
01418 Matcher<const std::string> m = StartsWith("Hi");
01419 EXPECT_EQ("starts with \"Hi\"", Describe(m));
01420 }
01421
01422
01423
01424 TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
01425 const Matcher<const char*> m1 = EndsWith("");
01426 EXPECT_TRUE(m1.Matches("Hi"));
01427 EXPECT_TRUE(m1.Matches(""));
01428 EXPECT_FALSE(m1.Matches(NULL));
01429
01430 const Matcher<const string&> m2 = EndsWith(string("Hi"));
01431 EXPECT_TRUE(m2.Matches("Hi"));
01432 EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
01433 EXPECT_TRUE(m2.Matches("Super Hi"));
01434 EXPECT_FALSE(m2.Matches("i"));
01435 EXPECT_FALSE(m2.Matches("Hi "));
01436 }
01437
01438 TEST(EndsWithTest, CanDescribeSelf) {
01439 Matcher<const std::string> m = EndsWith("Hi");
01440 EXPECT_EQ("ends with \"Hi\"", Describe(m));
01441 }
01442
01443
01444
01445 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
01446 const Matcher<const char*> m1 = MatchesRegex("a.*z");
01447 EXPECT_TRUE(m1.Matches("az"));
01448 EXPECT_TRUE(m1.Matches("abcz"));
01449 EXPECT_FALSE(m1.Matches(NULL));
01450
01451 const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
01452 EXPECT_TRUE(m2.Matches("azbz"));
01453 EXPECT_FALSE(m2.Matches("az1"));
01454 EXPECT_FALSE(m2.Matches("1az"));
01455 }
01456
01457 TEST(MatchesRegexTest, CanDescribeSelf) {
01458 Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
01459 EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
01460
01461 Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
01462 EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
01463 }
01464
01465
01466
01467 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
01468 const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
01469 EXPECT_TRUE(m1.Matches("az"));
01470 EXPECT_TRUE(m1.Matches("0abcz1"));
01471 EXPECT_FALSE(m1.Matches(NULL));
01472
01473 const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
01474 EXPECT_TRUE(m2.Matches("azbz"));
01475 EXPECT_TRUE(m2.Matches("az1"));
01476 EXPECT_FALSE(m2.Matches("1a"));
01477 }
01478
01479 TEST(ContainsRegexTest, CanDescribeSelf) {
01480 Matcher<const std::string> m1 = ContainsRegex("Hi.*");
01481 EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
01482
01483 Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
01484 EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
01485 }
01486
01487
01488 #if GTEST_HAS_STD_WSTRING
01489 TEST(StdWideStrEqTest, MatchesEqual) {
01490 Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
01491 EXPECT_TRUE(m.Matches(L"Hello"));
01492 EXPECT_FALSE(m.Matches(L"hello"));
01493 EXPECT_FALSE(m.Matches(NULL));
01494
01495 Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
01496 EXPECT_TRUE(m2.Matches(L"Hello"));
01497 EXPECT_FALSE(m2.Matches(L"Hi"));
01498
01499 Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
01500 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
01501 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
01502
01503 ::std::wstring str(L"01204500800");
01504 str[3] = L'\0';
01505 Matcher<const ::std::wstring&> m4 = StrEq(str);
01506 EXPECT_TRUE(m4.Matches(str));
01507 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
01508 Matcher<const ::std::wstring&> m5 = StrEq(str);
01509 EXPECT_TRUE(m5.Matches(str));
01510 }
01511
01512 TEST(StdWideStrEqTest, CanDescribeSelf) {
01513 Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
01514 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
01515 Describe(m));
01516
01517 Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
01518 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
01519 Describe(m2));
01520
01521 ::std::wstring str(L"01204500800");
01522 str[3] = L'\0';
01523 Matcher<const ::std::wstring&> m4 = StrEq(str);
01524 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
01525 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
01526 Matcher<const ::std::wstring&> m5 = StrEq(str);
01527 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
01528 }
01529
01530 TEST(StdWideStrNeTest, MatchesUnequalString) {
01531 Matcher<const wchar_t*> m = StrNe(L"Hello");
01532 EXPECT_TRUE(m.Matches(L""));
01533 EXPECT_TRUE(m.Matches(NULL));
01534 EXPECT_FALSE(m.Matches(L"Hello"));
01535
01536 Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
01537 EXPECT_TRUE(m2.Matches(L"hello"));
01538 EXPECT_FALSE(m2.Matches(L"Hello"));
01539 }
01540
01541 TEST(StdWideStrNeTest, CanDescribeSelf) {
01542 Matcher<const wchar_t*> m = StrNe(L"Hi");
01543 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
01544 }
01545
01546 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
01547 Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
01548 EXPECT_TRUE(m.Matches(L"Hello"));
01549 EXPECT_TRUE(m.Matches(L"hello"));
01550 EXPECT_FALSE(m.Matches(L"Hi"));
01551 EXPECT_FALSE(m.Matches(NULL));
01552
01553 Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
01554 EXPECT_TRUE(m2.Matches(L"hello"));
01555 EXPECT_FALSE(m2.Matches(L"Hi"));
01556 }
01557
01558 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
01559 ::std::wstring str1(L"oabocdooeoo");
01560 ::std::wstring str2(L"OABOCDOOEOO");
01561 Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
01562 EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
01563
01564 str1[3] = str2[3] = L'\0';
01565 Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
01566 EXPECT_TRUE(m1.Matches(str2));
01567
01568 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
01569 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
01570 Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
01571 str1[9] = str2[9] = L'\0';
01572 EXPECT_FALSE(m2.Matches(str2));
01573
01574 Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
01575 EXPECT_TRUE(m3.Matches(str2));
01576
01577 EXPECT_FALSE(m3.Matches(str2 + L"x"));
01578 str2.append(1, L'\0');
01579 EXPECT_FALSE(m3.Matches(str2));
01580 EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
01581 }
01582
01583 TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
01584 Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
01585 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
01586 }
01587
01588 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
01589 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
01590 EXPECT_TRUE(m.Matches(L"Hi"));
01591 EXPECT_TRUE(m.Matches(NULL));
01592 EXPECT_FALSE(m.Matches(L"Hello"));
01593 EXPECT_FALSE(m.Matches(L"hello"));
01594
01595 Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
01596 EXPECT_TRUE(m2.Matches(L""));
01597 EXPECT_FALSE(m2.Matches(L"Hello"));
01598 }
01599
01600 TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
01601 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
01602 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
01603 }
01604
01605
01606 TEST(StdWideHasSubstrTest, WorksForStringClasses) {
01607 const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
01608 EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
01609 EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
01610
01611 const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
01612 EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
01613 EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
01614 }
01615
01616
01617 TEST(StdWideHasSubstrTest, WorksForCStrings) {
01618 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
01619 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
01620 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
01621 EXPECT_FALSE(m1.Matches(NULL));
01622
01623 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
01624 EXPECT_TRUE(m2.Matches(L"I love food."));
01625 EXPECT_FALSE(m2.Matches(L"tofo"));
01626 EXPECT_FALSE(m2.Matches(NULL));
01627 }
01628
01629
01630 TEST(StdWideHasSubstrTest, CanDescribeSelf) {
01631 Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
01632 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
01633 }
01634
01635
01636
01637 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
01638 const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
01639 EXPECT_TRUE(m1.Matches(L"Hi"));
01640 EXPECT_TRUE(m1.Matches(L""));
01641 EXPECT_FALSE(m1.Matches(NULL));
01642
01643 const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
01644 EXPECT_TRUE(m2.Matches(L"Hi"));
01645 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
01646 EXPECT_TRUE(m2.Matches(L"High"));
01647 EXPECT_FALSE(m2.Matches(L"H"));
01648 EXPECT_FALSE(m2.Matches(L" Hi"));
01649 }
01650
01651 TEST(StdWideStartsWithTest, CanDescribeSelf) {
01652 Matcher<const ::std::wstring> m = StartsWith(L"Hi");
01653 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
01654 }
01655
01656
01657
01658 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
01659 const Matcher<const wchar_t*> m1 = EndsWith(L"");
01660 EXPECT_TRUE(m1.Matches(L"Hi"));
01661 EXPECT_TRUE(m1.Matches(L""));
01662 EXPECT_FALSE(m1.Matches(NULL));
01663
01664 const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
01665 EXPECT_TRUE(m2.Matches(L"Hi"));
01666 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
01667 EXPECT_TRUE(m2.Matches(L"Super Hi"));
01668 EXPECT_FALSE(m2.Matches(L"i"));
01669 EXPECT_FALSE(m2.Matches(L"Hi "));
01670 }
01671
01672 TEST(StdWideEndsWithTest, CanDescribeSelf) {
01673 Matcher<const ::std::wstring> m = EndsWith(L"Hi");
01674 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
01675 }
01676
01677 #endif // GTEST_HAS_STD_WSTRING
01678
01679 #if GTEST_HAS_GLOBAL_WSTRING
01680 TEST(GlobalWideStrEqTest, MatchesEqual) {
01681 Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
01682 EXPECT_TRUE(m.Matches(L"Hello"));
01683 EXPECT_FALSE(m.Matches(L"hello"));
01684 EXPECT_FALSE(m.Matches(NULL));
01685
01686 Matcher<const ::wstring&> m2 = StrEq(L"Hello");
01687 EXPECT_TRUE(m2.Matches(L"Hello"));
01688 EXPECT_FALSE(m2.Matches(L"Hi"));
01689
01690 Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
01691 EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
01692 EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
01693
01694 ::wstring str(L"01204500800");
01695 str[3] = L'\0';
01696 Matcher<const ::wstring&> m4 = StrEq(str);
01697 EXPECT_TRUE(m4.Matches(str));
01698 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
01699 Matcher<const ::wstring&> m5 = StrEq(str);
01700 EXPECT_TRUE(m5.Matches(str));
01701 }
01702
01703 TEST(GlobalWideStrEqTest, CanDescribeSelf) {
01704 Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
01705 EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
01706 Describe(m));
01707
01708 Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
01709 EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
01710 Describe(m2));
01711
01712 ::wstring str(L"01204500800");
01713 str[3] = L'\0';
01714 Matcher<const ::wstring&> m4 = StrEq(str);
01715 EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
01716 str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
01717 Matcher<const ::wstring&> m5 = StrEq(str);
01718 EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
01719 }
01720
01721 TEST(GlobalWideStrNeTest, MatchesUnequalString) {
01722 Matcher<const wchar_t*> m = StrNe(L"Hello");
01723 EXPECT_TRUE(m.Matches(L""));
01724 EXPECT_TRUE(m.Matches(NULL));
01725 EXPECT_FALSE(m.Matches(L"Hello"));
01726
01727 Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
01728 EXPECT_TRUE(m2.Matches(L"hello"));
01729 EXPECT_FALSE(m2.Matches(L"Hello"));
01730 }
01731
01732 TEST(GlobalWideStrNeTest, CanDescribeSelf) {
01733 Matcher<const wchar_t*> m = StrNe(L"Hi");
01734 EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
01735 }
01736
01737 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
01738 Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
01739 EXPECT_TRUE(m.Matches(L"Hello"));
01740 EXPECT_TRUE(m.Matches(L"hello"));
01741 EXPECT_FALSE(m.Matches(L"Hi"));
01742 EXPECT_FALSE(m.Matches(NULL));
01743
01744 Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
01745 EXPECT_TRUE(m2.Matches(L"hello"));
01746 EXPECT_FALSE(m2.Matches(L"Hi"));
01747 }
01748
01749 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
01750 ::wstring str1(L"oabocdooeoo");
01751 ::wstring str2(L"OABOCDOOEOO");
01752 Matcher<const ::wstring&> m0 = StrCaseEq(str1);
01753 EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
01754
01755 str1[3] = str2[3] = L'\0';
01756 Matcher<const ::wstring&> m1 = StrCaseEq(str1);
01757 EXPECT_TRUE(m1.Matches(str2));
01758
01759 str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
01760 str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
01761 Matcher<const ::wstring&> m2 = StrCaseEq(str1);
01762 str1[9] = str2[9] = L'\0';
01763 EXPECT_FALSE(m2.Matches(str2));
01764
01765 Matcher<const ::wstring&> m3 = StrCaseEq(str1);
01766 EXPECT_TRUE(m3.Matches(str2));
01767
01768 EXPECT_FALSE(m3.Matches(str2 + L"x"));
01769 str2.append(1, L'\0');
01770 EXPECT_FALSE(m3.Matches(str2));
01771 EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
01772 }
01773
01774 TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
01775 Matcher< ::wstring> m = StrCaseEq(L"Hi");
01776 EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
01777 }
01778
01779 TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
01780 Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
01781 EXPECT_TRUE(m.Matches(L"Hi"));
01782 EXPECT_TRUE(m.Matches(NULL));
01783 EXPECT_FALSE(m.Matches(L"Hello"));
01784 EXPECT_FALSE(m.Matches(L"hello"));
01785
01786 Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
01787 EXPECT_TRUE(m2.Matches(L""));
01788 EXPECT_FALSE(m2.Matches(L"Hello"));
01789 }
01790
01791 TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
01792 Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
01793 EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
01794 }
01795
01796
01797 TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
01798 const Matcher< ::wstring> m1 = HasSubstr(L"foo");
01799 EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
01800 EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
01801
01802 const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
01803 EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
01804 EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
01805 }
01806
01807
01808 TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
01809 const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
01810 EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
01811 EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
01812 EXPECT_FALSE(m1.Matches(NULL));
01813
01814 const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
01815 EXPECT_TRUE(m2.Matches(L"I love food."));
01816 EXPECT_FALSE(m2.Matches(L"tofo"));
01817 EXPECT_FALSE(m2.Matches(NULL));
01818 }
01819
01820
01821 TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
01822 Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
01823 EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
01824 }
01825
01826
01827
01828 TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
01829 const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
01830 EXPECT_TRUE(m1.Matches(L"Hi"));
01831 EXPECT_TRUE(m1.Matches(L""));
01832 EXPECT_FALSE(m1.Matches(NULL));
01833
01834 const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
01835 EXPECT_TRUE(m2.Matches(L"Hi"));
01836 EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
01837 EXPECT_TRUE(m2.Matches(L"High"));
01838 EXPECT_FALSE(m2.Matches(L"H"));
01839 EXPECT_FALSE(m2.Matches(L" Hi"));
01840 }
01841
01842 TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
01843 Matcher<const ::wstring> m = StartsWith(L"Hi");
01844 EXPECT_EQ("starts with L\"Hi\"", Describe(m));
01845 }
01846
01847
01848
01849 TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
01850 const Matcher<const wchar_t*> m1 = EndsWith(L"");
01851 EXPECT_TRUE(m1.Matches(L"Hi"));
01852 EXPECT_TRUE(m1.Matches(L""));
01853 EXPECT_FALSE(m1.Matches(NULL));
01854
01855 const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
01856 EXPECT_TRUE(m2.Matches(L"Hi"));
01857 EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
01858 EXPECT_TRUE(m2.Matches(L"Super Hi"));
01859 EXPECT_FALSE(m2.Matches(L"i"));
01860 EXPECT_FALSE(m2.Matches(L"Hi "));
01861 }
01862
01863 TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
01864 Matcher<const ::wstring> m = EndsWith(L"Hi");
01865 EXPECT_EQ("ends with L\"Hi\"", Describe(m));
01866 }
01867
01868 #endif // GTEST_HAS_GLOBAL_WSTRING
01869
01870
01871 typedef ::std::tr1::tuple<long, int> Tuple2;
01872
01873
01874
01875 TEST(Eq2Test, MatchesEqualArguments) {
01876 Matcher<const Tuple2&> m = Eq();
01877 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
01878 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
01879 }
01880
01881
01882 TEST(Eq2Test, CanDescribeSelf) {
01883 Matcher<const Tuple2&> m = Eq();
01884 EXPECT_EQ("are an equal pair", Describe(m));
01885 }
01886
01887
01888
01889 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
01890 Matcher<const Tuple2&> m = Ge();
01891 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
01892 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
01893 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
01894 }
01895
01896
01897 TEST(Ge2Test, CanDescribeSelf) {
01898 Matcher<const Tuple2&> m = Ge();
01899 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
01900 }
01901
01902
01903
01904 TEST(Gt2Test, MatchesGreaterThanArguments) {
01905 Matcher<const Tuple2&> m = Gt();
01906 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
01907 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
01908 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
01909 }
01910
01911
01912 TEST(Gt2Test, CanDescribeSelf) {
01913 Matcher<const Tuple2&> m = Gt();
01914 EXPECT_EQ("are a pair where the first > the second", Describe(m));
01915 }
01916
01917
01918
01919 TEST(Le2Test, MatchesLessThanOrEqualArguments) {
01920 Matcher<const Tuple2&> m = Le();
01921 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
01922 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
01923 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
01924 }
01925
01926
01927 TEST(Le2Test, CanDescribeSelf) {
01928 Matcher<const Tuple2&> m = Le();
01929 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
01930 }
01931
01932
01933
01934 TEST(Lt2Test, MatchesLessThanArguments) {
01935 Matcher<const Tuple2&> m = Lt();
01936 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
01937 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
01938 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
01939 }
01940
01941
01942 TEST(Lt2Test, CanDescribeSelf) {
01943 Matcher<const Tuple2&> m = Lt();
01944 EXPECT_EQ("are a pair where the first < the second", Describe(m));
01945 }
01946
01947
01948
01949 TEST(Ne2Test, MatchesUnequalArguments) {
01950 Matcher<const Tuple2&> m = Ne();
01951 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
01952 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
01953 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
01954 }
01955
01956
01957 TEST(Ne2Test, CanDescribeSelf) {
01958 Matcher<const Tuple2&> m = Ne();
01959 EXPECT_EQ("are an unequal pair", Describe(m));
01960 }
01961
01962
01963 TEST(NotTest, NegatesMatcher) {
01964 Matcher<int> m;
01965 m = Not(Eq(2));
01966 EXPECT_TRUE(m.Matches(3));
01967 EXPECT_FALSE(m.Matches(2));
01968 }
01969
01970
01971 TEST(NotTest, CanDescribeSelf) {
01972 Matcher<int> m = Not(Eq(5));
01973 EXPECT_EQ("isn't equal to 5", Describe(m));
01974 }
01975
01976
01977 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
01978
01979 Matcher<int> greater_than_5 = Gt(5);
01980
01981 Matcher<const int&> m = Not(greater_than_5);
01982 Matcher<int&> m2 = Not(greater_than_5);
01983 Matcher<int&> m3 = Not(m);
01984 }
01985
01986
01987 void AllOfMatches(int num, const Matcher<int>& m) {
01988 SCOPED_TRACE(Describe(m));
01989 EXPECT_TRUE(m.Matches(0));
01990 for (int i = 1; i <= num; ++i) {
01991 EXPECT_FALSE(m.Matches(i));
01992 }
01993 EXPECT_TRUE(m.Matches(num + 1));
01994 }
01995
01996
01997
01998 TEST(AllOfTest, MatchesWhenAllMatch) {
01999 Matcher<int> m;
02000 m = AllOf(Le(2), Ge(1));
02001 EXPECT_TRUE(m.Matches(1));
02002 EXPECT_TRUE(m.Matches(2));
02003 EXPECT_FALSE(m.Matches(0));
02004 EXPECT_FALSE(m.Matches(3));
02005
02006 m = AllOf(Gt(0), Ne(1), Ne(2));
02007 EXPECT_TRUE(m.Matches(3));
02008 EXPECT_FALSE(m.Matches(2));
02009 EXPECT_FALSE(m.Matches(1));
02010 EXPECT_FALSE(m.Matches(0));
02011
02012 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
02013 EXPECT_TRUE(m.Matches(4));
02014 EXPECT_FALSE(m.Matches(3));
02015 EXPECT_FALSE(m.Matches(2));
02016 EXPECT_FALSE(m.Matches(1));
02017 EXPECT_FALSE(m.Matches(0));
02018
02019 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
02020 EXPECT_TRUE(m.Matches(0));
02021 EXPECT_TRUE(m.Matches(1));
02022 EXPECT_FALSE(m.Matches(3));
02023
02024
02025
02026
02027
02028 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
02029 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
02030 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
02031 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
02032 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
02033 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
02034 AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
02035 Ne(8)));
02036 AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
02037 Ne(8), Ne(9)));
02038 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
02039 Ne(9), Ne(10)));
02040 }
02041
02042 #if GTEST_LANG_CXX11
02043
02044 TEST(AllOfTest, VariadicMatchesWhenAllMatch) {
02045
02046
02047 ::testing::AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
02048 Matcher<int> m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
02049 Ne(9), Ne(10), Ne(11));
02050 EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11))))))))))"));
02051 AllOfMatches(11, m);
02052 AllOfMatches(50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
02053 Ne(9), Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15),
02054 Ne(16), Ne(17), Ne(18), Ne(19), Ne(20), Ne(21), Ne(22),
02055 Ne(23), Ne(24), Ne(25), Ne(26), Ne(27), Ne(28), Ne(29),
02056 Ne(30), Ne(31), Ne(32), Ne(33), Ne(34), Ne(35), Ne(36),
02057 Ne(37), Ne(38), Ne(39), Ne(40), Ne(41), Ne(42), Ne(43),
02058 Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),
02059 Ne(50)));
02060 }
02061
02062 #endif // GTEST_LANG_CXX11
02063
02064
02065 TEST(AllOfTest, CanDescribeSelf) {
02066 Matcher<int> m;
02067 m = AllOf(Le(2), Ge(1));
02068 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
02069
02070 m = AllOf(Gt(0), Ne(1), Ne(2));
02071 EXPECT_EQ("(is > 0) and "
02072 "((isn't equal to 1) and "
02073 "(isn't equal to 2))",
02074 Describe(m));
02075
02076
02077 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
02078 EXPECT_EQ("((is > 0) and "
02079 "(isn't equal to 1)) and "
02080 "((isn't equal to 2) and "
02081 "(isn't equal to 3))",
02082 Describe(m));
02083
02084
02085 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
02086 EXPECT_EQ("((is >= 0) and "
02087 "(is < 10)) and "
02088 "((isn't equal to 3) and "
02089 "((isn't equal to 5) and "
02090 "(isn't equal to 7)))",
02091 Describe(m));
02092 }
02093
02094
02095 TEST(AllOfTest, CanDescribeNegation) {
02096 Matcher<int> m;
02097 m = AllOf(Le(2), Ge(1));
02098 EXPECT_EQ("(isn't <= 2) or "
02099 "(isn't >= 1)",
02100 DescribeNegation(m));
02101
02102 m = AllOf(Gt(0), Ne(1), Ne(2));
02103 EXPECT_EQ("(isn't > 0) or "
02104 "((is equal to 1) or "
02105 "(is equal to 2))",
02106 DescribeNegation(m));
02107
02108
02109 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
02110 EXPECT_EQ("((isn't > 0) or "
02111 "(is equal to 1)) or "
02112 "((is equal to 2) or "
02113 "(is equal to 3))",
02114 DescribeNegation(m));
02115
02116
02117 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
02118 EXPECT_EQ("((isn't >= 0) or "
02119 "(isn't < 10)) or "
02120 "((is equal to 3) or "
02121 "((is equal to 5) or "
02122 "(is equal to 7)))",
02123 DescribeNegation(m));
02124 }
02125
02126
02127 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
02128
02129 Matcher<int> greater_than_5 = Gt(5);
02130 Matcher<int> less_than_10 = Lt(10);
02131
02132 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
02133 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
02134 Matcher<int&> m3 = AllOf(greater_than_5, m2);
02135
02136
02137 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
02138 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
02139 }
02140
02141 TEST(AllOfTest, ExplainsResult) {
02142 Matcher<int> m;
02143
02144
02145
02146
02147 m = AllOf(GreaterThan(10), Lt(30));
02148 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
02149
02150
02151 m = AllOf(GreaterThan(10), GreaterThan(20));
02152 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
02153 Explain(m, 30));
02154
02155
02156
02157 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
02158 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
02159 Explain(m, 25));
02160
02161
02162 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
02163 EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
02164 "and which is 10 more than 30",
02165 Explain(m, 40));
02166
02167
02168
02169 m = AllOf(GreaterThan(10), GreaterThan(20));
02170 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
02171
02172
02173
02174
02175 m = AllOf(GreaterThan(10), Lt(30));
02176 EXPECT_EQ("", Explain(m, 40));
02177
02178
02179
02180 m = AllOf(GreaterThan(10), GreaterThan(20));
02181 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
02182 }
02183
02184
02185 void AnyOfMatches(int num, const Matcher<int>& m) {
02186 SCOPED_TRACE(Describe(m));
02187 EXPECT_FALSE(m.Matches(0));
02188 for (int i = 1; i <= num; ++i) {
02189 EXPECT_TRUE(m.Matches(i));
02190 }
02191 EXPECT_FALSE(m.Matches(num + 1));
02192 }
02193
02194
02195
02196 TEST(AnyOfTest, MatchesWhenAnyMatches) {
02197 Matcher<int> m;
02198 m = AnyOf(Le(1), Ge(3));
02199 EXPECT_TRUE(m.Matches(1));
02200 EXPECT_TRUE(m.Matches(4));
02201 EXPECT_FALSE(m.Matches(2));
02202
02203 m = AnyOf(Lt(0), Eq(1), Eq(2));
02204 EXPECT_TRUE(m.Matches(-1));
02205 EXPECT_TRUE(m.Matches(1));
02206 EXPECT_TRUE(m.Matches(2));
02207 EXPECT_FALSE(m.Matches(0));
02208
02209 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
02210 EXPECT_TRUE(m.Matches(-1));
02211 EXPECT_TRUE(m.Matches(1));
02212 EXPECT_TRUE(m.Matches(2));
02213 EXPECT_TRUE(m.Matches(3));
02214 EXPECT_FALSE(m.Matches(0));
02215
02216 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
02217 EXPECT_TRUE(m.Matches(0));
02218 EXPECT_TRUE(m.Matches(11));
02219 EXPECT_TRUE(m.Matches(3));
02220 EXPECT_FALSE(m.Matches(2));
02221
02222
02223
02224
02225
02226 AnyOfMatches(2, AnyOf(1, 2));
02227 AnyOfMatches(3, AnyOf(1, 2, 3));
02228 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
02229 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
02230 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
02231 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
02232 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
02233 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
02234 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
02235 }
02236
02237 #if GTEST_LANG_CXX11
02238
02239 TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
02240
02241
02242 Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
02243
02244 EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11))))))))))"));
02245 AnyOfMatches(11, m);
02246 AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
02247 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
02248 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
02249 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
02250 41, 42, 43, 44, 45, 46, 47, 48, 49, 50));
02251 }
02252
02253 #endif // GTEST_LANG_CXX11
02254
02255
02256 TEST(AnyOfTest, CanDescribeSelf) {
02257 Matcher<int> m;
02258 m = AnyOf(Le(1), Ge(3));
02259 EXPECT_EQ("(is <= 1) or (is >= 3)",
02260 Describe(m));
02261
02262 m = AnyOf(Lt(0), Eq(1), Eq(2));
02263 EXPECT_EQ("(is < 0) or "
02264 "((is equal to 1) or (is equal to 2))",
02265 Describe(m));
02266
02267 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
02268 EXPECT_EQ("((is < 0) or "
02269 "(is equal to 1)) or "
02270 "((is equal to 2) or "
02271 "(is equal to 3))",
02272 Describe(m));
02273
02274 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
02275 EXPECT_EQ("((is <= 0) or "
02276 "(is > 10)) or "
02277 "((is equal to 3) or "
02278 "((is equal to 5) or "
02279 "(is equal to 7)))",
02280 Describe(m));
02281 }
02282
02283
02284 TEST(AnyOfTest, CanDescribeNegation) {
02285 Matcher<int> m;
02286 m = AnyOf(Le(1), Ge(3));
02287 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
02288 DescribeNegation(m));
02289
02290 m = AnyOf(Lt(0), Eq(1), Eq(2));
02291 EXPECT_EQ("(isn't < 0) and "
02292 "((isn't equal to 1) and (isn't equal to 2))",
02293 DescribeNegation(m));
02294
02295 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
02296 EXPECT_EQ("((isn't < 0) and "
02297 "(isn't equal to 1)) and "
02298 "((isn't equal to 2) and "
02299 "(isn't equal to 3))",
02300 DescribeNegation(m));
02301
02302 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
02303 EXPECT_EQ("((isn't <= 0) and "
02304 "(isn't > 10)) and "
02305 "((isn't equal to 3) and "
02306 "((isn't equal to 5) and "
02307 "(isn't equal to 7)))",
02308 DescribeNegation(m));
02309 }
02310
02311
02312 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
02313
02314 Matcher<int> greater_than_5 = Gt(5);
02315 Matcher<int> less_than_10 = Lt(10);
02316
02317 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
02318 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
02319 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
02320
02321
02322 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
02323 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
02324 }
02325
02326 TEST(AnyOfTest, ExplainsResult) {
02327 Matcher<int> m;
02328
02329
02330
02331
02332 m = AnyOf(GreaterThan(10), Lt(0));
02333 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
02334
02335
02336 m = AnyOf(GreaterThan(10), GreaterThan(20));
02337 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
02338 Explain(m, 5));
02339
02340
02341
02342 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
02343 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
02344 Explain(m, 5));
02345
02346
02347 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
02348 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
02349 "and which is 25 less than 30",
02350 Explain(m, 5));
02351
02352
02353
02354 m = AnyOf(GreaterThan(10), GreaterThan(20));
02355 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
02356
02357
02358
02359
02360 m = AnyOf(GreaterThan(10), Lt(30));
02361 EXPECT_EQ("", Explain(m, 0));
02362
02363
02364
02365 m = AnyOf(GreaterThan(30), GreaterThan(20));
02366 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
02367 }
02368
02369
02370
02371
02372
02373
02374
02375
02376 int IsPositive(double x) {
02377 return x > 0 ? 1 : 0;
02378 }
02379
02380
02381
02382 class IsGreaterThan {
02383 public:
02384 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
02385
02386 bool operator()(int n) const { return n > threshold_; }
02387
02388 private:
02389 int threshold_;
02390 };
02391
02392
02393 const int foo = 0;
02394
02395
02396
02397 bool ReferencesFooAndIsZero(const int& n) {
02398 return (&n == &foo) && (n == 0);
02399 }
02400
02401
02402
02403 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
02404 Matcher<double> m = Truly(IsPositive);
02405 EXPECT_TRUE(m.Matches(2.0));
02406 EXPECT_FALSE(m.Matches(-1.5));
02407 }
02408
02409
02410 TEST(TrulyTest, CanBeUsedWithFunctor) {
02411 Matcher<int> m = Truly(IsGreaterThan(5));
02412 EXPECT_TRUE(m.Matches(6));
02413 EXPECT_FALSE(m.Matches(4));
02414 }
02415
02416
02417 class ConvertibleToBool {
02418 public:
02419 explicit ConvertibleToBool(int number) : number_(number) {}
02420 operator bool() const { return number_ != 0; }
02421
02422 private:
02423 int number_;
02424 };
02425
02426 ConvertibleToBool IsNotZero(int number) {
02427 return ConvertibleToBool(number);
02428 }
02429
02430
02431
02432
02433 TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
02434 Matcher<int> m = Truly(IsNotZero);
02435 EXPECT_TRUE(m.Matches(1));
02436 EXPECT_FALSE(m.Matches(0));
02437 }
02438
02439
02440 TEST(TrulyTest, CanDescribeSelf) {
02441 Matcher<double> m = Truly(IsPositive);
02442 EXPECT_EQ("satisfies the given predicate",
02443 Describe(m));
02444 }
02445
02446
02447
02448 TEST(TrulyTest, WorksForByRefArguments) {
02449 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
02450 EXPECT_TRUE(m.Matches(foo));
02451 int n = 0;
02452 EXPECT_FALSE(m.Matches(n));
02453 }
02454
02455
02456
02457 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
02458 EXPECT_TRUE(Matches(Ge(0))(1));
02459 EXPECT_FALSE(Matches(Eq('a'))('b'));
02460 }
02461
02462
02463
02464 TEST(MatchesTest, WorksOnByRefArguments) {
02465 int m = 0, n = 0;
02466 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
02467 EXPECT_FALSE(Matches(Ref(m))(n));
02468 }
02469
02470
02471
02472 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
02473 Matcher<int> eq5 = Eq(5);
02474 EXPECT_TRUE(Matches(eq5)(5));
02475 EXPECT_FALSE(Matches(eq5)(2));
02476 }
02477
02478
02479
02480
02481 TEST(ValueTest, WorksWithPolymorphicMatcher) {
02482 EXPECT_TRUE(Value("hi", StartsWith("h")));
02483 EXPECT_FALSE(Value(5, Gt(10)));
02484 }
02485
02486 TEST(ValueTest, WorksWithMonomorphicMatcher) {
02487 const Matcher<int> is_zero = Eq(0);
02488 EXPECT_TRUE(Value(0, is_zero));
02489 EXPECT_FALSE(Value('a', is_zero));
02490
02491 int n = 0;
02492 const Matcher<const int&> ref_n = Ref(n);
02493 EXPECT_TRUE(Value(n, ref_n));
02494 EXPECT_FALSE(Value(1, ref_n));
02495 }
02496
02497 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
02498 StringMatchResultListener listener1;
02499 EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
02500 EXPECT_EQ("% 2 == 0", listener1.str());
02501
02502 StringMatchResultListener listener2;
02503 EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
02504 EXPECT_EQ("", listener2.str());
02505 }
02506
02507 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
02508 const Matcher<int> is_even = PolymorphicIsEven();
02509 StringMatchResultListener listener1;
02510 EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
02511 EXPECT_EQ("% 2 == 0", listener1.str());
02512
02513 const Matcher<const double&> is_zero = Eq(0);
02514 StringMatchResultListener listener2;
02515 EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
02516 EXPECT_EQ("", listener2.str());
02517 }
02518
02519 MATCHER_P(Really, inner_matcher, "") {
02520 return ExplainMatchResult(inner_matcher, arg, result_listener);
02521 }
02522
02523 TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
02524 EXPECT_THAT(0, Really(Eq(0)));
02525 }
02526
02527 TEST(AllArgsTest, WorksForTuple) {
02528 EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
02529 EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
02530 }
02531
02532 TEST(AllArgsTest, WorksForNonTuple) {
02533 EXPECT_THAT(42, AllArgs(Gt(0)));
02534 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
02535 }
02536
02537 class AllArgsHelper {
02538 public:
02539 AllArgsHelper() {}
02540
02541 MOCK_METHOD2(Helper, int(char x, int y));
02542
02543 private:
02544 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
02545 };
02546
02547 TEST(AllArgsTest, WorksInWithClause) {
02548 AllArgsHelper helper;
02549 ON_CALL(helper, Helper(_, _))
02550 .With(AllArgs(Lt()))
02551 .WillByDefault(Return(1));
02552 EXPECT_CALL(helper, Helper(_, _));
02553 EXPECT_CALL(helper, Helper(_, _))
02554 .With(AllArgs(Gt()))
02555 .WillOnce(Return(2));
02556
02557 EXPECT_EQ(1, helper.Helper('\1', 2));
02558 EXPECT_EQ(2, helper.Helper('a', 1));
02559 }
02560
02561
02562
02563 TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
02564 ASSERT_THAT(5, Ge(2)) << "This should succeed.";
02565 ASSERT_THAT("Foo", EndsWith("oo"));
02566 EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
02567 EXPECT_THAT("Hello", StartsWith("Hell"));
02568 }
02569
02570
02571
02572 TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
02573
02574
02575 static unsigned short n;
02576 n = 5;
02577
02578
02579
02580
02581
02582
02583 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
02584 "Value of: n\n"
02585 "Expected: is > 10\n"
02586 " Actual: 5" + OfType("unsigned short"));
02587 n = 0;
02588 EXPECT_NONFATAL_FAILURE(
02589 EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
02590 "Value of: n\n"
02591 "Expected: (is <= 7) and (is >= 5)\n"
02592 " Actual: 0" + OfType("unsigned short"));
02593 }
02594
02595
02596
02597 TEST(MatcherAssertionTest, WorksForByRefArguments) {
02598
02599
02600 static int n;
02601 n = 0;
02602 EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
02603 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
02604 "Value of: n\n"
02605 "Expected: does not reference the variable @");
02606
02607 EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
02608 "Actual: 0" + OfType("int") + ", which is located @");
02609 }
02610
02611 #if !GTEST_OS_SYMBIAN
02612
02613
02614
02615
02616
02617
02618
02619
02620
02621
02622
02623
02624
02625 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
02626 Matcher<const char*> starts_with_he = StartsWith("he");
02627 ASSERT_THAT("hello", starts_with_he);
02628
02629 Matcher<const string&> ends_with_ok = EndsWith("ok");
02630 ASSERT_THAT("book", ends_with_ok);
02631 const string bad = "bad";
02632 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
02633 "Value of: bad\n"
02634 "Expected: ends with \"ok\"\n"
02635 " Actual: \"bad\"");
02636 Matcher<int> is_greater_than_5 = Gt(5);
02637 EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
02638 "Value of: 5\n"
02639 "Expected: is > 5\n"
02640 " Actual: 5" + OfType("int"));
02641 }
02642 #endif // !GTEST_OS_SYMBIAN
02643
02644
02645 template <typename RawType>
02646 class FloatingPointTest : public testing::Test {
02647 protected:
02648 typedef testing::internal::FloatingPoint<RawType> Floating;
02649 typedef typename Floating::Bits Bits;
02650
02651 FloatingPointTest()
02652 : max_ulps_(Floating::kMaxUlps),
02653 zero_bits_(Floating(0).bits()),
02654 one_bits_(Floating(1).bits()),
02655 infinity_bits_(Floating(Floating::Infinity()).bits()),
02656 close_to_positive_zero_(
02657 Floating::ReinterpretBits(zero_bits_ + max_ulps_/2)),
02658 close_to_negative_zero_(
02659 -Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_/2)),
02660 further_from_negative_zero_(-Floating::ReinterpretBits(
02661 zero_bits_ + max_ulps_ + 1 - max_ulps_/2)),
02662 close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)),
02663 further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)),
02664 infinity_(Floating::Infinity()),
02665 close_to_infinity_(
02666 Floating::ReinterpretBits(infinity_bits_ - max_ulps_)),
02667 further_from_infinity_(
02668 Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)),
02669 max_(Floating::Max()),
02670 nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
02671 nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {
02672 }
02673
02674 void TestSize() {
02675 EXPECT_EQ(sizeof(RawType), sizeof(Bits));
02676 }
02677
02678
02679
02680 void TestMatches(
02681 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
02682 Matcher<RawType> m1 = matcher_maker(0.0);
02683 EXPECT_TRUE(m1.Matches(-0.0));
02684 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
02685 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
02686 EXPECT_FALSE(m1.Matches(1.0));
02687
02688 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
02689 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
02690
02691 Matcher<RawType> m3 = matcher_maker(1.0);
02692 EXPECT_TRUE(m3.Matches(close_to_one_));
02693 EXPECT_FALSE(m3.Matches(further_from_one_));
02694
02695
02696 EXPECT_FALSE(m3.Matches(0.0));
02697
02698 Matcher<RawType> m4 = matcher_maker(-infinity_);
02699 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
02700
02701 Matcher<RawType> m5 = matcher_maker(infinity_);
02702 EXPECT_TRUE(m5.Matches(close_to_infinity_));
02703
02704
02705
02706 EXPECT_FALSE(m5.Matches(nan1_));
02707
02708
02709
02710 Matcher<const RawType&> m6 = matcher_maker(0.0);
02711 EXPECT_TRUE(m6.Matches(-0.0));
02712 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
02713 EXPECT_FALSE(m6.Matches(1.0));
02714
02715
02716
02717 Matcher<RawType&> m7 = matcher_maker(0.0);
02718 RawType x = 0.0;
02719 EXPECT_TRUE(m7.Matches(x));
02720 x = 0.01f;
02721 EXPECT_FALSE(m7.Matches(x));
02722 }
02723
02724
02725
02726 const size_t max_ulps_;
02727
02728 const Bits zero_bits_;
02729 const Bits one_bits_;
02730 const Bits infinity_bits_;
02731
02732
02733 const RawType close_to_positive_zero_;
02734 const RawType close_to_negative_zero_;
02735 const RawType further_from_negative_zero_;
02736
02737
02738 const RawType close_to_one_;
02739 const RawType further_from_one_;
02740
02741
02742 const RawType infinity_;
02743 const RawType close_to_infinity_;
02744 const RawType further_from_infinity_;
02745
02746
02747 const RawType max_;
02748
02749
02750 const RawType nan1_;
02751 const RawType nan2_;
02752 };
02753
02754
02755 template <typename RawType>
02756 class FloatingPointNearTest : public FloatingPointTest<RawType> {
02757 protected:
02758 typedef FloatingPointTest<RawType> ParentType;
02759
02760
02761
02762 void TestNearMatches(
02763 testing::internal::FloatingEqMatcher<RawType>
02764 (*matcher_maker)(RawType, RawType)) {
02765 Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
02766 EXPECT_TRUE(m1.Matches(0.0));
02767 EXPECT_TRUE(m1.Matches(-0.0));
02768 EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_));
02769 EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_));
02770 EXPECT_FALSE(m1.Matches(1.0));
02771
02772 Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
02773 EXPECT_TRUE(m2.Matches(0.0));
02774 EXPECT_TRUE(m2.Matches(-0.0));
02775 EXPECT_TRUE(m2.Matches(1.0));
02776 EXPECT_TRUE(m2.Matches(-1.0));
02777 EXPECT_FALSE(m2.Matches(ParentType::close_to_one_));
02778 EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_));
02779
02780
02781
02782 Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0);
02783 EXPECT_TRUE(m3.Matches(ParentType::infinity_));
02784 EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_));
02785 EXPECT_FALSE(m3.Matches(-ParentType::infinity_));
02786
02787 Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0);
02788 EXPECT_TRUE(m4.Matches(-ParentType::infinity_));
02789 EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_));
02790 EXPECT_FALSE(m4.Matches(ParentType::infinity_));
02791
02792
02793 Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_);
02794 EXPECT_TRUE(m5.Matches(ParentType::max_));
02795 EXPECT_FALSE(m5.Matches(-ParentType::max_));
02796
02797 Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_);
02798 EXPECT_FALSE(m6.Matches(ParentType::max_));
02799 EXPECT_TRUE(m6.Matches(-ParentType::max_));
02800
02801 Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0);
02802 EXPECT_TRUE(m7.Matches(ParentType::max_));
02803 EXPECT_FALSE(m7.Matches(-ParentType::max_));
02804
02805 Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0);
02806 EXPECT_FALSE(m8.Matches(ParentType::max_));
02807 EXPECT_TRUE(m8.Matches(-ParentType::max_));
02808
02809
02810
02811 Matcher<RawType> m9 = matcher_maker(
02812 ParentType::max_, ParentType::infinity_);
02813 EXPECT_TRUE(m8.Matches(-ParentType::max_));
02814
02815
02816
02817 Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
02818 EXPECT_TRUE(m10.Matches(-0.0));
02819 EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_));
02820 EXPECT_FALSE(m10.Matches(ParentType::close_to_one_));
02821
02822
02823
02824 Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
02825 RawType x = 0.0;
02826 EXPECT_TRUE(m11.Matches(x));
02827 x = 1.0f;
02828 EXPECT_TRUE(m11.Matches(x));
02829 x = -1.0f;
02830 EXPECT_TRUE(m11.Matches(x));
02831 x = 1.1f;
02832 EXPECT_FALSE(m11.Matches(x));
02833 x = -1.1f;
02834 EXPECT_FALSE(m11.Matches(x));
02835 }
02836 };
02837
02838
02839 typedef FloatingPointTest<float> FloatTest;
02840
02841 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
02842 TestMatches(&FloatEq);
02843 }
02844
02845 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
02846 TestMatches(&NanSensitiveFloatEq);
02847 }
02848
02849 TEST_F(FloatTest, FloatEqCannotMatchNaN) {
02850
02851 Matcher<float> m = FloatEq(nan1_);
02852 EXPECT_FALSE(m.Matches(nan1_));
02853 EXPECT_FALSE(m.Matches(nan2_));
02854 EXPECT_FALSE(m.Matches(1.0));
02855 }
02856
02857 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
02858
02859 Matcher<float> m = NanSensitiveFloatEq(nan1_);
02860 EXPECT_TRUE(m.Matches(nan1_));
02861 EXPECT_TRUE(m.Matches(nan2_));
02862 EXPECT_FALSE(m.Matches(1.0));
02863 }
02864
02865 TEST_F(FloatTest, FloatEqCanDescribeSelf) {
02866 Matcher<float> m1 = FloatEq(2.0f);
02867 EXPECT_EQ("is approximately 2", Describe(m1));
02868 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
02869
02870 Matcher<float> m2 = FloatEq(0.5f);
02871 EXPECT_EQ("is approximately 0.5", Describe(m2));
02872 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
02873
02874 Matcher<float> m3 = FloatEq(nan1_);
02875 EXPECT_EQ("never matches", Describe(m3));
02876 EXPECT_EQ("is anything", DescribeNegation(m3));
02877 }
02878
02879 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
02880 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
02881 EXPECT_EQ("is approximately 2", Describe(m1));
02882 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
02883
02884 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
02885 EXPECT_EQ("is approximately 0.5", Describe(m2));
02886 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
02887
02888 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
02889 EXPECT_EQ("is NaN", Describe(m3));
02890 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
02891 }
02892
02893
02894
02895 typedef FloatingPointNearTest<float> FloatNearTest;
02896
02897 TEST_F(FloatNearTest, FloatNearMatches) {
02898 TestNearMatches(&FloatNear);
02899 }
02900
02901 TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
02902 TestNearMatches(&NanSensitiveFloatNear);
02903 }
02904
02905 TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
02906 Matcher<float> m1 = FloatNear(2.0f, 0.5f);
02907 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
02908 EXPECT_EQ(
02909 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
02910
02911 Matcher<float> m2 = FloatNear(0.5f, 0.5f);
02912 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
02913 EXPECT_EQ(
02914 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
02915
02916 Matcher<float> m3 = FloatNear(nan1_, 0.0);
02917 EXPECT_EQ("never matches", Describe(m3));
02918 EXPECT_EQ("is anything", DescribeNegation(m3));
02919 }
02920
02921 TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
02922 Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f);
02923 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
02924 EXPECT_EQ(
02925 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
02926
02927 Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f);
02928 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
02929 EXPECT_EQ(
02930 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
02931
02932 Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f);
02933 EXPECT_EQ("is NaN", Describe(m3));
02934 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
02935 }
02936
02937 TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
02938
02939 Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f);
02940 EXPECT_FALSE(m.Matches(nan1_));
02941 EXPECT_FALSE(m.Matches(nan2_));
02942 EXPECT_FALSE(m.Matches(1.0));
02943 }
02944
02945 TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
02946
02947 Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f);
02948 EXPECT_TRUE(m.Matches(nan1_));
02949 EXPECT_TRUE(m.Matches(nan2_));
02950 EXPECT_FALSE(m.Matches(1.0));
02951 }
02952
02953
02954 typedef FloatingPointTest<double> DoubleTest;
02955
02956 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
02957 TestMatches(&DoubleEq);
02958 }
02959
02960 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
02961 TestMatches(&NanSensitiveDoubleEq);
02962 }
02963
02964 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
02965
02966 Matcher<double> m = DoubleEq(nan1_);
02967 EXPECT_FALSE(m.Matches(nan1_));
02968 EXPECT_FALSE(m.Matches(nan2_));
02969 EXPECT_FALSE(m.Matches(1.0));
02970 }
02971
02972 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
02973
02974 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
02975 EXPECT_TRUE(m.Matches(nan1_));
02976 EXPECT_TRUE(m.Matches(nan2_));
02977 EXPECT_FALSE(m.Matches(1.0));
02978 }
02979
02980 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
02981 Matcher<double> m1 = DoubleEq(2.0);
02982 EXPECT_EQ("is approximately 2", Describe(m1));
02983 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
02984
02985 Matcher<double> m2 = DoubleEq(0.5);
02986 EXPECT_EQ("is approximately 0.5", Describe(m2));
02987 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
02988
02989 Matcher<double> m3 = DoubleEq(nan1_);
02990 EXPECT_EQ("never matches", Describe(m3));
02991 EXPECT_EQ("is anything", DescribeNegation(m3));
02992 }
02993
02994 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
02995 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
02996 EXPECT_EQ("is approximately 2", Describe(m1));
02997 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
02998
02999 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
03000 EXPECT_EQ("is approximately 0.5", Describe(m2));
03001 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
03002
03003 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
03004 EXPECT_EQ("is NaN", Describe(m3));
03005 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
03006 }
03007
03008
03009
03010 typedef FloatingPointNearTest<double> DoubleNearTest;
03011
03012 TEST_F(DoubleNearTest, DoubleNearMatches) {
03013 TestNearMatches(&DoubleNear);
03014 }
03015
03016 TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
03017 TestNearMatches(&NanSensitiveDoubleNear);
03018 }
03019
03020 TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
03021 Matcher<double> m1 = DoubleNear(2.0, 0.5);
03022 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
03023 EXPECT_EQ(
03024 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
03025
03026 Matcher<double> m2 = DoubleNear(0.5, 0.5);
03027 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
03028 EXPECT_EQ(
03029 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
03030
03031 Matcher<double> m3 = DoubleNear(nan1_, 0.0);
03032 EXPECT_EQ("never matches", Describe(m3));
03033 EXPECT_EQ("is anything", DescribeNegation(m3));
03034 }
03035
03036 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
03037 Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);
03038 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
03039 EXPECT_EQ(
03040 "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
03041
03042 Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);
03043 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
03044 EXPECT_EQ(
03045 "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
03046
03047 Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1);
03048 EXPECT_EQ("is NaN", Describe(m3));
03049 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
03050 }
03051
03052 TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
03053
03054 Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1);
03055 EXPECT_FALSE(m.Matches(nan1_));
03056 EXPECT_FALSE(m.Matches(nan2_));
03057 EXPECT_FALSE(m.Matches(1.0));
03058 }
03059
03060 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
03061
03062 Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1);
03063 EXPECT_TRUE(m.Matches(nan1_));
03064 EXPECT_TRUE(m.Matches(nan2_));
03065 EXPECT_FALSE(m.Matches(1.0));
03066 }
03067
03068 TEST(PointeeTest, RawPointer) {
03069 const Matcher<int*> m = Pointee(Ge(0));
03070
03071 int n = 1;
03072 EXPECT_TRUE(m.Matches(&n));
03073 n = -1;
03074 EXPECT_FALSE(m.Matches(&n));
03075 EXPECT_FALSE(m.Matches(NULL));
03076 }
03077
03078 TEST(PointeeTest, RawPointerToConst) {
03079 const Matcher<const double*> m = Pointee(Ge(0));
03080
03081 double x = 1;
03082 EXPECT_TRUE(m.Matches(&x));
03083 x = -1;
03084 EXPECT_FALSE(m.Matches(&x));
03085 EXPECT_FALSE(m.Matches(NULL));
03086 }
03087
03088 TEST(PointeeTest, ReferenceToConstRawPointer) {
03089 const Matcher<int* const &> m = Pointee(Ge(0));
03090
03091 int n = 1;
03092 EXPECT_TRUE(m.Matches(&n));
03093 n = -1;
03094 EXPECT_FALSE(m.Matches(&n));
03095 EXPECT_FALSE(m.Matches(NULL));
03096 }
03097
03098 TEST(PointeeTest, ReferenceToNonConstRawPointer) {
03099 const Matcher<double* &> m = Pointee(Ge(0));
03100
03101 double x = 1.0;
03102 double* p = &x;
03103 EXPECT_TRUE(m.Matches(p));
03104 x = -1;
03105 EXPECT_FALSE(m.Matches(p));
03106 p = NULL;
03107 EXPECT_FALSE(m.Matches(p));
03108 }
03109
03110
03111 template <typename T>
03112 class ConstPropagatingPtr {
03113 public:
03114 typedef T element_type;
03115
03116 ConstPropagatingPtr() : val_() {}
03117 explicit ConstPropagatingPtr(T* t) : val_(t) {}
03118 ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}
03119
03120 T* get() { return val_; }
03121 T& operator*() { return *val_; }
03122
03123 const T* get() const { return val_; }
03124 const T& operator*() const { return *val_; }
03125
03126 private:
03127 T* val_;
03128 };
03129
03130 TEST(PointeeTest, WorksWithConstPropagatingPointers) {
03131 const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
03132 int three = 3;
03133 const ConstPropagatingPtr<int> co(&three);
03134 ConstPropagatingPtr<int> o(&three);
03135 EXPECT_TRUE(m.Matches(o));
03136 EXPECT_TRUE(m.Matches(co));
03137 *o = 6;
03138 EXPECT_FALSE(m.Matches(o));
03139 EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
03140 }
03141
03142 TEST(PointeeTest, NeverMatchesNull) {
03143 const Matcher<const char*> m = Pointee(_);
03144 EXPECT_FALSE(m.Matches(NULL));
03145 }
03146
03147
03148 TEST(PointeeTest, MatchesAgainstAValue) {
03149 const Matcher<int*> m = Pointee(5);
03150
03151 int n = 5;
03152 EXPECT_TRUE(m.Matches(&n));
03153 n = -1;
03154 EXPECT_FALSE(m.Matches(&n));
03155 EXPECT_FALSE(m.Matches(NULL));
03156 }
03157
03158 TEST(PointeeTest, CanDescribeSelf) {
03159 const Matcher<int*> m = Pointee(Gt(3));
03160 EXPECT_EQ("points to a value that is > 3", Describe(m));
03161 EXPECT_EQ("does not point to a value that is > 3",
03162 DescribeNegation(m));
03163 }
03164
03165 TEST(PointeeTest, CanExplainMatchResult) {
03166 const Matcher<const string*> m = Pointee(StartsWith("Hi"));
03167
03168 EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
03169
03170 const Matcher<long*> m2 = Pointee(GreaterThan(1));
03171 long n = 3;
03172 EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
03173 Explain(m2, &n));
03174 }
03175
03176 TEST(PointeeTest, AlwaysExplainsPointee) {
03177 const Matcher<int*> m = Pointee(0);
03178 int n = 42;
03179 EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
03180 }
03181
03182
03183 class Uncopyable {
03184 public:
03185 explicit Uncopyable(int a_value) : value_(a_value) {}
03186
03187 int value() const { return value_; }
03188 private:
03189 const int value_;
03190 GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
03191 };
03192
03193
03194 bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
03195
03196
03197 struct AStruct {
03198 AStruct() : x(0), y(1.0), z(5), p(NULL) {}
03199 AStruct(const AStruct& rhs)
03200 : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
03201
03202 int x;
03203 const double y;
03204 Uncopyable z;
03205 const char* p;
03206
03207 private:
03208 GTEST_DISALLOW_ASSIGN_(AStruct);
03209 };
03210
03211
03212 struct DerivedStruct : public AStruct {
03213 char ch;
03214
03215 private:
03216 GTEST_DISALLOW_ASSIGN_(DerivedStruct);
03217 };
03218
03219
03220 TEST(FieldTest, WorksForNonConstField) {
03221 Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
03222
03223 AStruct a;
03224 EXPECT_TRUE(m.Matches(a));
03225 a.x = -1;
03226 EXPECT_FALSE(m.Matches(a));
03227 }
03228
03229
03230 TEST(FieldTest, WorksForConstField) {
03231 AStruct a;
03232
03233 Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
03234 EXPECT_TRUE(m.Matches(a));
03235 m = Field(&AStruct::y, Le(0.0));
03236 EXPECT_FALSE(m.Matches(a));
03237 }
03238
03239
03240 TEST(FieldTest, WorksForUncopyableField) {
03241 AStruct a;
03242
03243 Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
03244 EXPECT_TRUE(m.Matches(a));
03245 m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
03246 EXPECT_FALSE(m.Matches(a));
03247 }
03248
03249
03250 TEST(FieldTest, WorksForPointerField) {
03251
03252 Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
03253 AStruct a;
03254 EXPECT_TRUE(m.Matches(a));
03255 a.p = "hi";
03256 EXPECT_FALSE(m.Matches(a));
03257
03258
03259 m = Field(&AStruct::p, StartsWith("hi"));
03260 a.p = "hill";
03261 EXPECT_TRUE(m.Matches(a));
03262 a.p = "hole";
03263 EXPECT_FALSE(m.Matches(a));
03264 }
03265
03266
03267 TEST(FieldTest, WorksForByRefArgument) {
03268 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
03269
03270 AStruct a;
03271 EXPECT_TRUE(m.Matches(a));
03272 a.x = -1;
03273 EXPECT_FALSE(m.Matches(a));
03274 }
03275
03276
03277
03278 TEST(FieldTest, WorksForArgumentOfSubType) {
03279
03280
03281 Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
03282
03283 DerivedStruct d;
03284 EXPECT_TRUE(m.Matches(d));
03285 d.x = -1;
03286 EXPECT_FALSE(m.Matches(d));
03287 }
03288
03289
03290
03291 TEST(FieldTest, WorksForCompatibleMatcherType) {
03292
03293 Matcher<const AStruct&> m = Field(&AStruct::x,
03294 Matcher<signed char>(Ge(0)));
03295
03296 AStruct a;
03297 EXPECT_TRUE(m.Matches(a));
03298 a.x = -1;
03299 EXPECT_FALSE(m.Matches(a));
03300 }
03301
03302
03303 TEST(FieldTest, CanDescribeSelf) {
03304 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
03305
03306 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
03307 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
03308 }
03309
03310
03311 TEST(FieldTest, CanExplainMatchResult) {
03312 Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
03313
03314 AStruct a;
03315 a.x = 1;
03316 EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
03317
03318 m = Field(&AStruct::x, GreaterThan(0));
03319 EXPECT_EQ(
03320 "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
03321 Explain(m, a));
03322 }
03323
03324
03325 TEST(FieldForPointerTest, WorksForPointerToConst) {
03326 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
03327
03328 AStruct a;
03329 EXPECT_TRUE(m.Matches(&a));
03330 a.x = -1;
03331 EXPECT_FALSE(m.Matches(&a));
03332 }
03333
03334
03335 TEST(FieldForPointerTest, WorksForPointerToNonConst) {
03336 Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
03337
03338 AStruct a;
03339 EXPECT_TRUE(m.Matches(&a));
03340 a.x = -1;
03341 EXPECT_FALSE(m.Matches(&a));
03342 }
03343
03344
03345 TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
03346 Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
03347
03348 AStruct a;
03349 EXPECT_TRUE(m.Matches(&a));
03350 a.x = -1;
03351 EXPECT_FALSE(m.Matches(&a));
03352 }
03353
03354
03355 TEST(FieldForPointerTest, DoesNotMatchNull) {
03356 Matcher<const AStruct*> m = Field(&AStruct::x, _);
03357 EXPECT_FALSE(m.Matches(NULL));
03358 }
03359
03360
03361
03362 TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
03363
03364
03365 Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
03366
03367 DerivedStruct d;
03368 EXPECT_TRUE(m.Matches(&d));
03369 d.x = -1;
03370 EXPECT_FALSE(m.Matches(&d));
03371 }
03372
03373
03374 TEST(FieldForPointerTest, CanDescribeSelf) {
03375 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
03376
03377 EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
03378 EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
03379 }
03380
03381
03382 TEST(FieldForPointerTest, CanExplainMatchResult) {
03383 Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
03384
03385 AStruct a;
03386 a.x = 1;
03387 EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
03388 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
03389 Explain(m, &a));
03390
03391 m = Field(&AStruct::x, GreaterThan(0));
03392 EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
03393 ", which is 1 more than 0", Explain(m, &a));
03394 }
03395
03396
03397 class AClass {
03398 public:
03399 AClass() : n_(0) {}
03400
03401
03402 int n() const { return n_; }
03403
03404 void set_n(int new_n) { n_ = new_n; }
03405
03406
03407 const string& s() const { return s_; }
03408
03409 void set_s(const string& new_s) { s_ = new_s; }
03410
03411
03412 double& x() const { return x_; }
03413 private:
03414 int n_;
03415 string s_;
03416
03417 static double x_;
03418 };
03419
03420 double AClass::x_ = 0.0;
03421
03422
03423 class DerivedClass : public AClass {
03424 private:
03425 int k_;
03426 };
03427
03428
03429
03430 TEST(PropertyTest, WorksForNonReferenceProperty) {
03431 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
03432
03433 AClass a;
03434 a.set_n(1);
03435 EXPECT_TRUE(m.Matches(a));
03436
03437 a.set_n(-1);
03438 EXPECT_FALSE(m.Matches(a));
03439 }
03440
03441
03442
03443 TEST(PropertyTest, WorksForReferenceToConstProperty) {
03444 Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
03445
03446 AClass a;
03447 a.set_s("hill");
03448 EXPECT_TRUE(m.Matches(a));
03449
03450 a.set_s("hole");
03451 EXPECT_FALSE(m.Matches(a));
03452 }
03453
03454
03455
03456 TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
03457 double x = 0.0;
03458 AClass a;
03459
03460 Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
03461 EXPECT_FALSE(m.Matches(a));
03462
03463 m = Property(&AClass::x, Not(Ref(x)));
03464 EXPECT_TRUE(m.Matches(a));
03465 }
03466
03467
03468
03469 TEST(PropertyTest, WorksForByValueArgument) {
03470 Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
03471
03472 AClass a;
03473 a.set_s("hill");
03474 EXPECT_TRUE(m.Matches(a));
03475
03476 a.set_s("hole");
03477 EXPECT_FALSE(m.Matches(a));
03478 }
03479
03480
03481
03482 TEST(PropertyTest, WorksForArgumentOfSubType) {
03483
03484
03485 Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
03486
03487 DerivedClass d;
03488 d.set_n(1);
03489 EXPECT_TRUE(m.Matches(d));
03490
03491 d.set_n(-1);
03492 EXPECT_FALSE(m.Matches(d));
03493 }
03494
03495
03496
03497 TEST(PropertyTest, WorksForCompatibleMatcherType) {
03498
03499 Matcher<const AClass&> m = Property(&AClass::n,
03500 Matcher<signed char>(Ge(0)));
03501
03502 AClass a;
03503 EXPECT_TRUE(m.Matches(a));
03504 a.set_n(-1);
03505 EXPECT_FALSE(m.Matches(a));
03506 }
03507
03508
03509 TEST(PropertyTest, CanDescribeSelf) {
03510 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
03511
03512 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
03513 EXPECT_EQ("is an object whose given property isn't >= 0",
03514 DescribeNegation(m));
03515 }
03516
03517
03518 TEST(PropertyTest, CanExplainMatchResult) {
03519 Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
03520
03521 AClass a;
03522 a.set_n(1);
03523 EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
03524
03525 m = Property(&AClass::n, GreaterThan(0));
03526 EXPECT_EQ(
03527 "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
03528 Explain(m, a));
03529 }
03530
03531
03532 TEST(PropertyForPointerTest, WorksForPointerToConst) {
03533 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
03534
03535 AClass a;
03536 a.set_n(1);
03537 EXPECT_TRUE(m.Matches(&a));
03538
03539 a.set_n(-1);
03540 EXPECT_FALSE(m.Matches(&a));
03541 }
03542
03543
03544 TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
03545 Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
03546
03547 AClass a;
03548 a.set_s("hill");
03549 EXPECT_TRUE(m.Matches(&a));
03550
03551 a.set_s("hole");
03552 EXPECT_FALSE(m.Matches(&a));
03553 }
03554
03555
03556
03557 TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
03558 Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
03559
03560 AClass a;
03561 a.set_s("hill");
03562 EXPECT_TRUE(m.Matches(&a));
03563
03564 a.set_s("hole");
03565 EXPECT_FALSE(m.Matches(&a));
03566 }
03567
03568
03569 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
03570 Matcher<const AClass*> m = Property(&AClass::x, _);
03571 EXPECT_FALSE(m.Matches(NULL));
03572 }
03573
03574
03575
03576 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
03577
03578
03579 Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
03580
03581 DerivedClass d;
03582 d.set_n(1);
03583 EXPECT_TRUE(m.Matches(&d));
03584
03585 d.set_n(-1);
03586 EXPECT_FALSE(m.Matches(&d));
03587 }
03588
03589
03590 TEST(PropertyForPointerTest, CanDescribeSelf) {
03591 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
03592
03593 EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
03594 EXPECT_EQ("is an object whose given property isn't >= 0",
03595 DescribeNegation(m));
03596 }
03597
03598
03599 TEST(PropertyForPointerTest, CanExplainMatchResult) {
03600 Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
03601
03602 AClass a;
03603 a.set_n(1);
03604 EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
03605 EXPECT_EQ(
03606 "which points to an object whose given property is 1" + OfType("int"),
03607 Explain(m, &a));
03608
03609 m = Property(&AClass::n, GreaterThan(0));
03610 EXPECT_EQ("which points to an object whose given property is 1" +
03611 OfType("int") + ", which is 1 more than 0",
03612 Explain(m, &a));
03613 }
03614
03615
03616
03617
03618
03619 string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
03620
03621 TEST(ResultOfTest, WorksForFunctionPointers) {
03622 Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
03623
03624 EXPECT_TRUE(matcher.Matches(1));
03625 EXPECT_FALSE(matcher.Matches(2));
03626 }
03627
03628
03629 TEST(ResultOfTest, CanDescribeItself) {
03630 Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
03631
03632 EXPECT_EQ("is mapped by the given callable to a value that "
03633 "is equal to \"foo\"", Describe(matcher));
03634 EXPECT_EQ("is mapped by the given callable to a value that "
03635 "isn't equal to \"foo\"", DescribeNegation(matcher));
03636 }
03637
03638
03639 int IntFunction(int input) { return input == 42 ? 80 : 90; }
03640
03641 TEST(ResultOfTest, CanExplainMatchResult) {
03642 Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
03643 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
03644 Explain(matcher, 36));
03645
03646 matcher = ResultOf(&IntFunction, GreaterThan(85));
03647 EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
03648 ", which is 5 more than 85", Explain(matcher, 36));
03649 }
03650
03651
03652
03653 TEST(ResultOfTest, WorksForNonReferenceResults) {
03654 Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
03655
03656 EXPECT_TRUE(matcher.Matches(42));
03657 EXPECT_FALSE(matcher.Matches(36));
03658 }
03659
03660
03661
03662 double& DoubleFunction(double& input) { return input; }
03663
03664 Uncopyable& RefUncopyableFunction(Uncopyable& obj) {
03665 return obj;
03666 }
03667
03668 TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
03669 double x = 3.14;
03670 double x2 = x;
03671 Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
03672
03673 EXPECT_TRUE(matcher.Matches(x));
03674 EXPECT_FALSE(matcher.Matches(x2));
03675
03676
03677 Uncopyable obj(0);
03678 Uncopyable obj2(0);
03679 Matcher<Uncopyable&> matcher2 =
03680 ResultOf(&RefUncopyableFunction, Ref(obj));
03681
03682 EXPECT_TRUE(matcher2.Matches(obj));
03683 EXPECT_FALSE(matcher2.Matches(obj2));
03684 }
03685
03686
03687
03688 const string& StringFunction(const string& input) { return input; }
03689
03690 TEST(ResultOfTest, WorksForReferenceToConstResults) {
03691 string s = "foo";
03692 string s2 = s;
03693 Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
03694
03695 EXPECT_TRUE(matcher.Matches(s));
03696 EXPECT_FALSE(matcher.Matches(s2));
03697 }
03698
03699
03700
03701 TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
03702
03703 Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
03704
03705 EXPECT_TRUE(matcher.Matches(36));
03706 EXPECT_FALSE(matcher.Matches(42));
03707 }
03708
03709
03710
03711 TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
03712 EXPECT_DEATH_IF_SUPPORTED(
03713 ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
03714 "NULL function pointer is passed into ResultOf\\(\\)\\.");
03715 }
03716
03717
03718
03719 TEST(ResultOfTest, WorksForFunctionReferences) {
03720 Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
03721 EXPECT_TRUE(matcher.Matches(1));
03722 EXPECT_FALSE(matcher.Matches(2));
03723 }
03724
03725
03726
03727 struct Functor : public ::std::unary_function<int, string> {
03728 result_type operator()(argument_type input) const {
03729 return IntToStringFunction(input);
03730 }
03731 };
03732
03733 TEST(ResultOfTest, WorksForFunctors) {
03734 Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
03735
03736 EXPECT_TRUE(matcher.Matches(1));
03737 EXPECT_FALSE(matcher.Matches(2));
03738 }
03739
03740
03741
03742
03743 struct PolymorphicFunctor {
03744 typedef int result_type;
03745 int operator()(int n) { return n; }
03746 int operator()(const char* s) { return static_cast<int>(strlen(s)); }
03747 };
03748
03749 TEST(ResultOfTest, WorksForPolymorphicFunctors) {
03750 Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
03751
03752 EXPECT_TRUE(matcher_int.Matches(10));
03753 EXPECT_FALSE(matcher_int.Matches(2));
03754
03755 Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
03756
03757 EXPECT_TRUE(matcher_string.Matches("long string"));
03758 EXPECT_FALSE(matcher_string.Matches("shrt"));
03759 }
03760
03761 const int* ReferencingFunction(const int& n) { return &n; }
03762
03763 struct ReferencingFunctor {
03764 typedef const int* result_type;
03765 result_type operator()(const int& n) { return &n; }
03766 };
03767
03768 TEST(ResultOfTest, WorksForReferencingCallables) {
03769 const int n = 1;
03770 const int n2 = 1;
03771 Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
03772 EXPECT_TRUE(matcher2.Matches(n));
03773 EXPECT_FALSE(matcher2.Matches(n2));
03774
03775 Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
03776 EXPECT_TRUE(matcher3.Matches(n));
03777 EXPECT_FALSE(matcher3.Matches(n2));
03778 }
03779
03780 class DivisibleByImpl {
03781 public:
03782 explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
03783
03784
03785 template <typename T>
03786 bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
03787 *listener << "which is " << (n % divider_) << " modulo "
03788 << divider_;
03789 return (n % divider_) == 0;
03790 }
03791
03792 void DescribeTo(ostream* os) const {
03793 *os << "is divisible by " << divider_;
03794 }
03795
03796 void DescribeNegationTo(ostream* os) const {
03797 *os << "is not divisible by " << divider_;
03798 }
03799
03800 void set_divider(int a_divider) { divider_ = a_divider; }
03801 int divider() const { return divider_; }
03802
03803 private:
03804 int divider_;
03805 };
03806
03807 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
03808 return MakePolymorphicMatcher(DivisibleByImpl(n));
03809 }
03810
03811
03812
03813 TEST(ExplainMatchResultTest, AllOf_False_False) {
03814 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
03815 EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
03816 }
03817
03818
03819
03820 TEST(ExplainMatchResultTest, AllOf_False_True) {
03821 const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
03822 EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
03823 }
03824
03825
03826
03827 TEST(ExplainMatchResultTest, AllOf_True_False) {
03828 const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
03829 EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
03830 }
03831
03832
03833
03834 TEST(ExplainMatchResultTest, AllOf_True_True) {
03835 const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
03836 EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
03837 }
03838
03839 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
03840 const Matcher<int> m = AllOf(Ge(2), Le(3));
03841 EXPECT_EQ("", Explain(m, 2));
03842 }
03843
03844 TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
03845 const Matcher<int> m = GreaterThan(5);
03846 EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
03847 }
03848
03849
03850
03851
03852
03853 class NotCopyable {
03854 public:
03855 explicit NotCopyable(int a_value) : value_(a_value) {}
03856
03857 int value() const { return value_; }
03858
03859 bool operator==(const NotCopyable& rhs) const {
03860 return value() == rhs.value();
03861 }
03862
03863 bool operator>=(const NotCopyable& rhs) const {
03864 return value() >= rhs.value();
03865 }
03866 private:
03867 int value_;
03868
03869 GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
03870 };
03871
03872 TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
03873 const NotCopyable const_value1(1);
03874 const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
03875
03876 const NotCopyable n1(1), n2(2);
03877 EXPECT_TRUE(m.Matches(n1));
03878 EXPECT_FALSE(m.Matches(n2));
03879 }
03880
03881 TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
03882 NotCopyable value2(2);
03883 const Matcher<NotCopyable&> m = Ge(ByRef(value2));
03884
03885 NotCopyable n1(1), n2(2);
03886 EXPECT_FALSE(m.Matches(n1));
03887 EXPECT_TRUE(m.Matches(n2));
03888 }
03889
03890 TEST(IsEmptyTest, ImplementsIsEmpty) {
03891 vector<int> container;
03892 EXPECT_THAT(container, IsEmpty());
03893 container.push_back(0);
03894 EXPECT_THAT(container, Not(IsEmpty()));
03895 container.push_back(1);
03896 EXPECT_THAT(container, Not(IsEmpty()));
03897 }
03898
03899 TEST(IsEmptyTest, WorksWithString) {
03900 string text;
03901 EXPECT_THAT(text, IsEmpty());
03902 text = "foo";
03903 EXPECT_THAT(text, Not(IsEmpty()));
03904 text = string("\0", 1);
03905 EXPECT_THAT(text, Not(IsEmpty()));
03906 }
03907
03908 TEST(IsEmptyTest, CanDescribeSelf) {
03909 Matcher<vector<int> > m = IsEmpty();
03910 EXPECT_EQ("is empty", Describe(m));
03911 EXPECT_EQ("isn't empty", DescribeNegation(m));
03912 }
03913
03914 TEST(IsEmptyTest, ExplainsResult) {
03915 Matcher<vector<int> > m = IsEmpty();
03916 vector<int> container;
03917 EXPECT_EQ("", Explain(m, container));
03918 container.push_back(0);
03919 EXPECT_EQ("whose size is 1", Explain(m, container));
03920 }
03921
03922 TEST(SizeIsTest, ImplementsSizeIs) {
03923 vector<int> container;
03924 EXPECT_THAT(container, SizeIs(0));
03925 EXPECT_THAT(container, Not(SizeIs(1)));
03926 container.push_back(0);
03927 EXPECT_THAT(container, Not(SizeIs(0)));
03928 EXPECT_THAT(container, SizeIs(1));
03929 container.push_back(0);
03930 EXPECT_THAT(container, Not(SizeIs(0)));
03931 EXPECT_THAT(container, SizeIs(2));
03932 }
03933
03934 TEST(SizeIsTest, WorksWithMap) {
03935 map<string, int> container;
03936 EXPECT_THAT(container, SizeIs(0));
03937 EXPECT_THAT(container, Not(SizeIs(1)));
03938 container.insert(make_pair("foo", 1));
03939 EXPECT_THAT(container, Not(SizeIs(0)));
03940 EXPECT_THAT(container, SizeIs(1));
03941 container.insert(make_pair("bar", 2));
03942 EXPECT_THAT(container, Not(SizeIs(0)));
03943 EXPECT_THAT(container, SizeIs(2));
03944 }
03945
03946 TEST(SizeIsTest, WorksWithReferences) {
03947 vector<int> container;
03948 Matcher<const vector<int>&> m = SizeIs(1);
03949 EXPECT_THAT(container, Not(m));
03950 container.push_back(0);
03951 EXPECT_THAT(container, m);
03952 }
03953
03954 TEST(SizeIsTest, CanDescribeSelf) {
03955 Matcher<vector<int> > m = SizeIs(2);
03956 EXPECT_EQ("size is equal to 2", Describe(m));
03957 EXPECT_EQ("size isn't equal to 2", DescribeNegation(m));
03958 }
03959
03960 TEST(SizeIsTest, ExplainsResult) {
03961 Matcher<vector<int> > m1 = SizeIs(2);
03962 Matcher<vector<int> > m2 = SizeIs(Lt(2u));
03963 Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3));
03964 Matcher<vector<int> > m4 = SizeIs(GreaterThan(1));
03965 vector<int> container;
03966 EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container));
03967 EXPECT_EQ("whose size 0 matches", Explain(m2, container));
03968 EXPECT_EQ("whose size 0 matches", Explain(m3, container));
03969 EXPECT_EQ("whose size 0 doesn't match, which is 1 less than 1",
03970 Explain(m4, container));
03971 container.push_back(0);
03972 container.push_back(0);
03973 EXPECT_EQ("whose size 2 matches", Explain(m1, container));
03974 EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container));
03975 EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container));
03976 EXPECT_EQ("whose size 2 matches, which is 1 more than 1",
03977 Explain(m4, container));
03978 }
03979
03980 #if GTEST_HAS_TYPED_TEST
03981
03982
03983
03984 template <typename T>
03985 class ContainerEqTest : public testing::Test {};
03986
03987 typedef testing::Types<
03988 set<int>,
03989 vector<size_t>,
03990 multiset<size_t>,
03991 list<int> >
03992 ContainerEqTestTypes;
03993
03994 TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
03995
03996
03997 TYPED_TEST(ContainerEqTest, EqualsSelf) {
03998 static const int vals[] = {1, 1, 2, 3, 5, 8};
03999 TypeParam my_set(vals, vals + 6);
04000 const Matcher<TypeParam> m = ContainerEq(my_set);
04001 EXPECT_TRUE(m.Matches(my_set));
04002 EXPECT_EQ("", Explain(m, my_set));
04003 }
04004
04005
04006 TYPED_TEST(ContainerEqTest, ValueMissing) {
04007 static const int vals[] = {1, 1, 2, 3, 5, 8};
04008 static const int test_vals[] = {2, 1, 8, 5};
04009 TypeParam my_set(vals, vals + 6);
04010 TypeParam test_set(test_vals, test_vals + 4);
04011 const Matcher<TypeParam> m = ContainerEq(my_set);
04012 EXPECT_FALSE(m.Matches(test_set));
04013 EXPECT_EQ("which doesn't have these expected elements: 3",
04014 Explain(m, test_set));
04015 }
04016
04017
04018 TYPED_TEST(ContainerEqTest, ValueAdded) {
04019 static const int vals[] = {1, 1, 2, 3, 5, 8};
04020 static const int test_vals[] = {1, 2, 3, 5, 8, 46};
04021 TypeParam my_set(vals, vals + 6);
04022 TypeParam test_set(test_vals, test_vals + 6);
04023 const Matcher<const TypeParam&> m = ContainerEq(my_set);
04024 EXPECT_FALSE(m.Matches(test_set));
04025 EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
04026 }
04027
04028
04029 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
04030 static const int vals[] = {1, 1, 2, 3, 5, 8};
04031 static const int test_vals[] = {1, 2, 3, 8, 46};
04032 TypeParam my_set(vals, vals + 6);
04033 TypeParam test_set(test_vals, test_vals + 5);
04034 const Matcher<TypeParam> m = ContainerEq(my_set);
04035 EXPECT_FALSE(m.Matches(test_set));
04036 EXPECT_EQ("which has these unexpected elements: 46,\n"
04037 "and doesn't have these expected elements: 5",
04038 Explain(m, test_set));
04039 }
04040
04041
04042 TYPED_TEST(ContainerEqTest, DuplicateDifference) {
04043 static const int vals[] = {1, 1, 2, 3, 5, 8};
04044 static const int test_vals[] = {1, 2, 3, 5, 8};
04045 TypeParam my_set(vals, vals + 6);
04046 TypeParam test_set(test_vals, test_vals + 5);
04047 const Matcher<const TypeParam&> m = ContainerEq(my_set);
04048
04049
04050 EXPECT_EQ("", Explain(m, test_set));
04051 }
04052 #endif // GTEST_HAS_TYPED_TEST
04053
04054
04055
04056 TEST(ContainerEqExtraTest, MultipleValuesMissing) {
04057 static const int vals[] = {1, 1, 2, 3, 5, 8};
04058 static const int test_vals[] = {2, 1, 5};
04059 vector<int> my_set(vals, vals + 6);
04060 vector<int> test_set(test_vals, test_vals + 3);
04061 const Matcher<vector<int> > m = ContainerEq(my_set);
04062 EXPECT_FALSE(m.Matches(test_set));
04063 EXPECT_EQ("which doesn't have these expected elements: 3, 8",
04064 Explain(m, test_set));
04065 }
04066
04067
04068
04069 TEST(ContainerEqExtraTest, MultipleValuesAdded) {
04070 static const int vals[] = {1, 1, 2, 3, 5, 8};
04071 static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
04072 list<size_t> my_set(vals, vals + 6);
04073 list<size_t> test_set(test_vals, test_vals + 7);
04074 const Matcher<const list<size_t>&> m = ContainerEq(my_set);
04075 EXPECT_FALSE(m.Matches(test_set));
04076 EXPECT_EQ("which has these unexpected elements: 92, 46",
04077 Explain(m, test_set));
04078 }
04079
04080
04081 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
04082 static const int vals[] = {1, 1, 2, 3, 5, 8};
04083 static const int test_vals[] = {1, 2, 3, 92, 46};
04084 list<size_t> my_set(vals, vals + 6);
04085 list<size_t> test_set(test_vals, test_vals + 5);
04086 const Matcher<const list<size_t> > m = ContainerEq(my_set);
04087 EXPECT_FALSE(m.Matches(test_set));
04088 EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
04089 "and doesn't have these expected elements: 5, 8",
04090 Explain(m, test_set));
04091 }
04092
04093
04094
04095 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
04096 static const int vals[] = {1, 1, 2, 3, 5, 8};
04097 static const int test_vals[] = {1, 2, 3, 5, 8};
04098 vector<int> my_set(vals, vals + 6);
04099 vector<int> test_set(test_vals, test_vals + 5);
04100 const Matcher<vector<int> > m = ContainerEq(my_set);
04101 EXPECT_TRUE(m.Matches(my_set));
04102 EXPECT_FALSE(m.Matches(test_set));
04103
04104 EXPECT_EQ("", Explain(m, test_set));
04105 }
04106
04107
04108
04109 TEST(ContainerEqExtraTest, WorksForMaps) {
04110 map<int, std::string> my_map;
04111 my_map[0] = "a";
04112 my_map[1] = "b";
04113
04114 map<int, std::string> test_map;
04115 test_map[0] = "aa";
04116 test_map[1] = "b";
04117
04118 const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
04119 EXPECT_TRUE(m.Matches(my_map));
04120 EXPECT_FALSE(m.Matches(test_map));
04121
04122 EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
04123 "and doesn't have these expected elements: (0, \"a\")",
04124 Explain(m, test_map));
04125 }
04126
04127 TEST(ContainerEqExtraTest, WorksForNativeArray) {
04128 int a1[] = { 1, 2, 3 };
04129 int a2[] = { 1, 2, 3 };
04130 int b[] = { 1, 2, 4 };
04131
04132 EXPECT_THAT(a1, ContainerEq(a2));
04133 EXPECT_THAT(a1, Not(ContainerEq(b)));
04134 }
04135
04136 TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
04137 const char a1[][3] = { "hi", "lo" };
04138 const char a2[][3] = { "hi", "lo" };
04139 const char b[][3] = { "lo", "hi" };
04140
04141
04142 EXPECT_THAT(a1, ContainerEq(a2));
04143 EXPECT_THAT(a1, Not(ContainerEq(b)));
04144
04145
04146 EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
04147 EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
04148 }
04149
04150 TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
04151 const int a1[] = { 1, 2, 3 };
04152 const int a2[] = { 1, 2, 3 };
04153 const int b[] = { 1, 2, 3, 4 };
04154
04155 const int* const p1 = a1;
04156 EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
04157 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
04158
04159 const int c[] = { 1, 3, 2 };
04160 EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
04161 }
04162
04163 TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
04164 std::string a1[][3] = {
04165 { "hi", "hello", "ciao" },
04166 { "bye", "see you", "ciao" }
04167 };
04168
04169 std::string a2[][3] = {
04170 { "hi", "hello", "ciao" },
04171 { "bye", "see you", "ciao" }
04172 };
04173
04174 const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
04175 EXPECT_THAT(a1, m);
04176
04177 a2[0][0] = "ha";
04178 EXPECT_THAT(a1, m);
04179 }
04180
04181 TEST(WhenSortedByTest, WorksForEmptyContainer) {
04182 const vector<int> numbers;
04183 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
04184 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
04185 }
04186
04187 TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
04188 vector<unsigned> numbers;
04189 numbers.push_back(3);
04190 numbers.push_back(1);
04191 numbers.push_back(2);
04192 numbers.push_back(2);
04193 EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
04194 ElementsAre(3, 2, 2, 1)));
04195 EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
04196 ElementsAre(1, 2, 2, 3))));
04197 }
04198
04199 TEST(WhenSortedByTest, WorksForNonVectorContainer) {
04200 list<string> words;
04201 words.push_back("say");
04202 words.push_back("hello");
04203 words.push_back("world");
04204 EXPECT_THAT(words, WhenSortedBy(less<string>(),
04205 ElementsAre("hello", "say", "world")));
04206 EXPECT_THAT(words, Not(WhenSortedBy(less<string>(),
04207 ElementsAre("say", "hello", "world"))));
04208 }
04209
04210 TEST(WhenSortedByTest, WorksForNativeArray) {
04211 const int numbers[] = { 1, 3, 2, 4 };
04212 const int sorted_numbers[] = { 1, 2, 3, 4 };
04213 EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
04214 EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
04215 ElementsAreArray(sorted_numbers)));
04216 EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
04217 }
04218
04219 TEST(WhenSortedByTest, CanDescribeSelf) {
04220 const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
04221 EXPECT_EQ("(when sorted) has 2 elements where\n"
04222 "element #0 is equal to 1,\n"
04223 "element #1 is equal to 2",
04224 Describe(m));
04225 EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
04226 "element #0 isn't equal to 1, or\n"
04227 "element #1 isn't equal to 2",
04228 DescribeNegation(m));
04229 }
04230
04231 TEST(WhenSortedByTest, ExplainsMatchResult) {
04232 const int a[] = { 2, 1 };
04233 EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
04234 Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
04235 EXPECT_EQ("which is { 1, 2 } when sorted",
04236 Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
04237 }
04238
04239
04240
04241
04242 TEST(WhenSortedTest, WorksForEmptyContainer) {
04243 const vector<int> numbers;
04244 EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
04245 EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
04246 }
04247
04248 TEST(WhenSortedTest, WorksForNonEmptyContainer) {
04249 list<string> words;
04250 words.push_back("3");
04251 words.push_back("1");
04252 words.push_back("2");
04253 words.push_back("2");
04254 EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
04255 EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
04256 }
04257
04258 TEST(WhenSortedTest, WorksForMapTypes) {
04259 map<string, int> word_counts;
04260 word_counts["and"] = 1;
04261 word_counts["the"] = 1;
04262 word_counts["buffalo"] = 2;
04263 EXPECT_THAT(word_counts, WhenSorted(ElementsAre(
04264 Pair("and", 1), Pair("buffalo", 2), Pair("the", 1))));
04265 EXPECT_THAT(word_counts, Not(WhenSorted(ElementsAre(
04266 Pair("and", 1), Pair("the", 1), Pair("buffalo", 2)))));
04267 }
04268
04269 TEST(WhenSortedTest, WorksForMultiMapTypes) {
04270 multimap<int, int> ifib;
04271 ifib.insert(make_pair(8, 6));
04272 ifib.insert(make_pair(2, 3));
04273 ifib.insert(make_pair(1, 1));
04274 ifib.insert(make_pair(3, 4));
04275 ifib.insert(make_pair(1, 2));
04276 ifib.insert(make_pair(5, 5));
04277 EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1),
04278 Pair(1, 2),
04279 Pair(2, 3),
04280 Pair(3, 4),
04281 Pair(5, 5),
04282 Pair(8, 6))));
04283 EXPECT_THAT(ifib, Not(WhenSorted(ElementsAre(Pair(8, 6),
04284 Pair(2, 3),
04285 Pair(1, 1),
04286 Pair(3, 4),
04287 Pair(1, 2),
04288 Pair(5, 5)))));
04289 }
04290
04291 TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
04292 std::deque<int> d;
04293 d.push_back(2);
04294 d.push_back(1);
04295 EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2)));
04296 EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
04297 }
04298
04299 TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
04300 std::deque<int> d;
04301 d.push_back(2);
04302 d.push_back(1);
04303 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
04304 EXPECT_THAT(d, WhenSorted(vector_match));
04305 Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
04306 EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
04307 }
04308
04309
04310
04311 template <typename T>
04312 class Streamlike {
04313 private:
04314 class ConstIter;
04315 public:
04316 typedef ConstIter const_iterator;
04317 typedef T value_type;
04318
04319 template <typename InIter>
04320 Streamlike(InIter first, InIter last) : remainder_(first, last) {}
04321
04322 const_iterator begin() const {
04323 return const_iterator(this, remainder_.begin());
04324 }
04325 const_iterator end() const {
04326 return const_iterator(this, remainder_.end());
04327 }
04328
04329 private:
04330 class ConstIter : public std::iterator<std::input_iterator_tag,
04331 value_type,
04332 ptrdiff_t,
04333 const value_type&,
04334 const value_type*> {
04335 public:
04336 ConstIter(const Streamlike* s,
04337 typename std::list<value_type>::iterator pos)
04338 : s_(s), pos_(pos) {}
04339
04340 const value_type& operator*() const { return *pos_; }
04341 const value_type* operator->() const { return &*pos_; }
04342 ConstIter& operator++() {
04343 s_->remainder_.erase(pos_++);
04344 return *this;
04345 }
04346
04347
04348
04349 class PostIncrProxy {
04350 public:
04351 explicit PostIncrProxy(const value_type& value) : value_(value) {}
04352 value_type operator*() const { return value_; }
04353 private:
04354 value_type value_;
04355 };
04356 PostIncrProxy operator++(int) {
04357 PostIncrProxy proxy(**this);
04358 ++(*this);
04359 return proxy;
04360 }
04361
04362 friend bool operator==(const ConstIter& a, const ConstIter& b) {
04363 return a.s_ == b.s_ && a.pos_ == b.pos_;
04364 }
04365 friend bool operator!=(const ConstIter& a, const ConstIter& b) {
04366 return !(a == b);
04367 }
04368
04369 private:
04370 const Streamlike* s_;
04371 typename std::list<value_type>::iterator pos_;
04372 };
04373
04374 friend std::ostream& operator<<(std::ostream& os, const Streamlike& s) {
04375 os << "[";
04376 typedef typename std::list<value_type>::const_iterator Iter;
04377 const char* sep = "";
04378 for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
04379 os << sep << *it;
04380 sep = ",";
04381 }
04382 os << "]";
04383 return os;
04384 }
04385
04386 mutable std::list<value_type> remainder_;
04387 };
04388
04389 TEST(StreamlikeTest, Iteration) {
04390 const int a[5] = { 2, 1, 4, 5, 3 };
04391 Streamlike<int> s(a, a + 5);
04392 Streamlike<int>::const_iterator it = s.begin();
04393 const int* ip = a;
04394 while (it != s.end()) {
04395 SCOPED_TRACE(ip - a);
04396 EXPECT_EQ(*ip++, *it++);
04397 }
04398 }
04399
04400 TEST(WhenSortedTest, WorksForStreamlike) {
04401
04402
04403 const int a[5] = { 2, 1, 4, 5, 3 };
04404 Streamlike<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
04405 EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
04406 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
04407 }
04408
04409 TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
04410 const int a[] = { 2, 1, 4, 5, 3 };
04411 Streamlike<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
04412 Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
04413 EXPECT_THAT(s, WhenSorted(vector_match));
04414 EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
04415 }
04416
04417
04418
04419
04420 TEST(ElemensAreStreamTest, WorksForStreamlike) {
04421 const int a[5] = { 1, 2, 3, 4, 5 };
04422 Streamlike<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
04423 EXPECT_THAT(s, ElementsAre(1, 2, 3, 4, 5));
04424 EXPECT_THAT(s, Not(ElementsAre(2, 1, 4, 5, 3)));
04425 }
04426
04427 TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
04428 const int a[5] = { 1, 2, 3, 4, 5 };
04429 Streamlike<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
04430
04431 vector<int> expected;
04432 expected.push_back(1);
04433 expected.push_back(2);
04434 expected.push_back(3);
04435 expected.push_back(4);
04436 expected.push_back(5);
04437 EXPECT_THAT(s, ElementsAreArray(expected));
04438
04439 expected[3] = 0;
04440 EXPECT_THAT(s, Not(ElementsAreArray(expected)));
04441 }
04442
04443
04444
04445 TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
04446 const int a[] = { 0, 1, 2, 3, 4 };
04447 std::vector<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
04448 do {
04449 StringMatchResultListener listener;
04450 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a),
04451 s, &listener)) << listener.str();
04452 } while (std::next_permutation(s.begin(), s.end()));
04453 }
04454
04455 TEST(UnorderedElementsAreArrayTest, VectorBool) {
04456 const bool a[] = { 0, 1, 0, 1, 1 };
04457 const bool b[] = { 1, 0, 1, 1, 0 };
04458 std::vector<bool> expected(a, a + GMOCK_ARRAY_SIZE_(a));
04459 std::vector<bool> actual(b, b + GMOCK_ARRAY_SIZE_(b));
04460 StringMatchResultListener listener;
04461 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected),
04462 actual, &listener)) << listener.str();
04463 }
04464
04465 TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
04466
04467
04468
04469 const int a[5] = { 2, 1, 4, 5, 3 };
04470 Streamlike<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
04471
04472 ::std::vector<int> expected;
04473 expected.push_back(1);
04474 expected.push_back(2);
04475 expected.push_back(3);
04476 expected.push_back(4);
04477 expected.push_back(5);
04478 EXPECT_THAT(s, UnorderedElementsAreArray(expected));
04479
04480 expected.push_back(6);
04481 EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected)));
04482 }
04483
04484 #if GTEST_LANG_CXX11
04485
04486 TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
04487 const int a[5] = { 2, 1, 4, 5, 3 };
04488 EXPECT_THAT(a, UnorderedElementsAreArray({ 1, 2, 3, 4, 5 }));
04489 EXPECT_THAT(a, Not(UnorderedElementsAreArray({ 1, 2, 3, 4, 6 })));
04490 }
04491
04492 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
04493 const string a[5] = { "a", "b", "c", "d", "e" };
04494 EXPECT_THAT(a, UnorderedElementsAreArray({ "a", "b", "c", "d", "e" }));
04495 EXPECT_THAT(a, Not(UnorderedElementsAreArray({ "a", "b", "c", "d", "ef" })));
04496 }
04497
04498 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
04499 const int a[5] = { 2, 1, 4, 5, 3 };
04500 EXPECT_THAT(a, UnorderedElementsAreArray(
04501 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
04502 EXPECT_THAT(a, Not(UnorderedElementsAreArray(
04503 { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
04504 }
04505
04506 TEST(UnorderedElementsAreArrayTest,
04507 TakesInitializerListOfDifferentTypedMatchers) {
04508 const int a[5] = { 2, 1, 4, 5, 3 };
04509
04510
04511
04512 EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int> >(
04513 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
04514 EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int> >(
04515 { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
04516 }
04517
04518 #endif // GTEST_LANG_CXX11
04519
04520 class UnorderedElementsAreTest : public testing::Test {
04521 protected:
04522 typedef std::vector<int> IntVec;
04523 };
04524
04525 TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
04526 const int a[] = { 1, 2, 3 };
04527 std::vector<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
04528 do {
04529 StringMatchResultListener listener;
04530 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
04531 s, &listener)) << listener.str();
04532 } while (std::next_permutation(s.begin(), s.end()));
04533 }
04534
04535 TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
04536 const int a[] = { 1, 2, 3 };
04537 std::vector<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
04538 std::vector<Matcher<int> > mv;
04539 mv.push_back(1);
04540 mv.push_back(2);
04541 mv.push_back(2);
04542
04543 StringMatchResultListener listener;
04544 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAreArray(mv),
04545 s, &listener)) << listener.str();
04546 }
04547
04548 TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
04549
04550
04551
04552 const int a[5] = { 2, 1, 4, 5, 3 };
04553 Streamlike<int> s(a, a + GMOCK_ARRAY_SIZE_(a));
04554
04555 EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
04556 EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));
04557 }
04558
04559
04560
04561
04562
04563
04564
04565 TEST_F(UnorderedElementsAreTest, Performance) {
04566 std::vector<int> s;
04567 std::vector<Matcher<int> > mv;
04568 for (int i = 0; i < 100; ++i) {
04569 s.push_back(i);
04570 mv.push_back(_);
04571 }
04572 mv[50] = Eq(0);
04573 StringMatchResultListener listener;
04574 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv),
04575 s, &listener)) << listener.str();
04576 }
04577
04578
04579
04580
04581 TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
04582 std::vector<int> s;
04583 std::vector<Matcher<int> > mv;
04584 for (int i = 0; i < 100; ++i) {
04585 s.push_back(i);
04586 if (i & 1) {
04587 mv.push_back(_);
04588 } else {
04589 mv.push_back(i);
04590 }
04591 }
04592 StringMatchResultListener listener;
04593 EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(mv),
04594 s, &listener)) << listener.str();
04595 }
04596
04597 TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
04598 std::vector<int> v;
04599 v.push_back(4);
04600 StringMatchResultListener listener;
04601 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
04602 v, &listener)) << listener.str();
04603 EXPECT_THAT(listener.str(), Eq("which has 1 element"));
04604 }
04605
04606 TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
04607 std::vector<int> v;
04608 StringMatchResultListener listener;
04609 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3),
04610 v, &listener)) << listener.str();
04611 EXPECT_THAT(listener.str(), Eq(""));
04612 }
04613
04614 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
04615 std::vector<int> v;
04616 v.push_back(1);
04617 v.push_back(1);
04618 StringMatchResultListener listener;
04619 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2),
04620 v, &listener)) << listener.str();
04621 EXPECT_THAT(
04622 listener.str(),
04623 Eq("where the following matchers don't match any elements:\n"
04624 "matcher #1: is equal to 2"));
04625 }
04626
04627 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
04628 std::vector<int> v;
04629 v.push_back(1);
04630 v.push_back(2);
04631 StringMatchResultListener listener;
04632 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 1),
04633 v, &listener)) << listener.str();
04634 EXPECT_THAT(
04635 listener.str(),
04636 Eq("where the following elements don't match any matchers:\n"
04637 "element #1: 2"));
04638 }
04639
04640 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
04641 std::vector<int> v;
04642 v.push_back(2);
04643 v.push_back(3);
04644 StringMatchResultListener listener;
04645 EXPECT_FALSE(ExplainMatchResult(UnorderedElementsAre(1, 2),
04646 v, &listener)) << listener.str();
04647 EXPECT_THAT(
04648 listener.str(),
04649 Eq("where"
04650 " the following matchers don't match any elements:\n"
04651 "matcher #0: is equal to 1\n"
04652 "and"
04653 " where"
04654 " the following elements don't match any matchers:\n"
04655 "element #1: 3"));
04656 }
04657
04658
04659 static string EMString(int element, int matcher) {
04660 stringstream ss;
04661 ss << "(element #" << element << ", matcher #" << matcher << ")";
04662 return ss.str();
04663 }
04664
04665 TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
04666
04667
04668 std::vector<string> v;
04669 v.push_back("a");
04670 v.push_back("b");
04671 v.push_back("c");
04672 StringMatchResultListener listener;
04673 EXPECT_FALSE(ExplainMatchResult(
04674 UnorderedElementsAre("a", "a", AnyOf("b", "c")), v, &listener))
04675 << listener.str();
04676
04677 string prefix =
04678 "where no permutation of the elements can satisfy all matchers, "
04679 "and the closest match is 2 of 3 matchers with the "
04680 "pairings:\n";
04681
04682
04683 EXPECT_THAT(
04684 listener.str(),
04685 AnyOf(prefix + "{\n " + EMString(0, 0) +
04686 ",\n " + EMString(1, 2) + "\n}",
04687 prefix + "{\n " + EMString(0, 1) +
04688 ",\n " + EMString(1, 2) + "\n}",
04689 prefix + "{\n " + EMString(0, 0) +
04690 ",\n " + EMString(2, 2) + "\n}",
04691 prefix + "{\n " + EMString(0, 1) +
04692 ",\n " + EMString(2, 2) + "\n}"));
04693 }
04694
04695 TEST_F(UnorderedElementsAreTest, Describe) {
04696 EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()),
04697 Eq("is empty"));
04698 EXPECT_THAT(
04699 Describe<IntVec>(UnorderedElementsAre(345)),
04700 Eq("has 1 element and that element is equal to 345"));
04701 EXPECT_THAT(
04702 Describe<IntVec>(UnorderedElementsAre(111, 222, 333)),
04703 Eq("has 3 elements and there exists some permutation "
04704 "of elements such that:\n"
04705 " - element #0 is equal to 111, and\n"
04706 " - element #1 is equal to 222, and\n"
04707 " - element #2 is equal to 333"));
04708 }
04709
04710 TEST_F(UnorderedElementsAreTest, DescribeNegation) {
04711 EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()),
04712 Eq("isn't empty"));
04713 EXPECT_THAT(
04714 DescribeNegation<IntVec>(UnorderedElementsAre(345)),
04715 Eq("doesn't have 1 element, or has 1 element that isn't equal to 345"));
04716 EXPECT_THAT(
04717 DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)),
04718 Eq("doesn't have 3 elements, or there exists no permutation "
04719 "of elements such that:\n"
04720 " - element #0 is equal to 123, and\n"
04721 " - element #1 is equal to 234, and\n"
04722 " - element #2 is equal to 345"));
04723 }
04724
04725 namespace {
04726
04727
04728
04729
04730
04731 template <typename Graph>
04732 class BacktrackingMaxBPMState {
04733 public:
04734
04735 explicit BacktrackingMaxBPMState(const Graph* g) : graph_(g) { }
04736
04737 ElementMatcherPairs Compute() {
04738 if (graph_->LhsSize() == 0 || graph_->RhsSize() == 0) {
04739 return best_so_far_;
04740 }
04741 lhs_used_.assign(graph_->LhsSize(), kUnused);
04742 rhs_used_.assign(graph_->RhsSize(), kUnused);
04743 for (size_t irhs = 0; irhs < graph_->RhsSize(); ++irhs) {
04744 matches_.clear();
04745 RecurseInto(irhs);
04746 if (best_so_far_.size() == graph_->RhsSize())
04747 break;
04748 }
04749 return best_so_far_;
04750 }
04751
04752 private:
04753 static const size_t kUnused = static_cast<size_t>(-1);
04754
04755 void PushMatch(size_t lhs, size_t rhs) {
04756 matches_.push_back(ElementMatcherPair(lhs, rhs));
04757 lhs_used_[lhs] = rhs;
04758 rhs_used_[rhs] = lhs;
04759 if (matches_.size() > best_so_far_.size()) {
04760 best_so_far_ = matches_;
04761 }
04762 }
04763
04764 void PopMatch() {
04765 const ElementMatcherPair& back = matches_.back();
04766 lhs_used_[back.first] = kUnused;
04767 rhs_used_[back.second] = kUnused;
04768 matches_.pop_back();
04769 }
04770
04771 bool RecurseInto(size_t irhs) {
04772 if (rhs_used_[irhs] != kUnused) {
04773 return true;
04774 }
04775 for (size_t ilhs = 0; ilhs < graph_->LhsSize(); ++ilhs) {
04776 if (lhs_used_[ilhs] != kUnused) {
04777 continue;
04778 }
04779 if (!graph_->HasEdge(ilhs, irhs)) {
04780 continue;
04781 }
04782 PushMatch(ilhs, irhs);
04783 if (best_so_far_.size() == graph_->RhsSize()) {
04784 return false;
04785 }
04786 for (size_t mi = irhs + 1; mi < graph_->RhsSize(); ++mi) {
04787 if (!RecurseInto(mi)) return false;
04788 }
04789 PopMatch();
04790 }
04791 return true;
04792 }
04793
04794 const Graph* graph_;
04795 std::vector<size_t> lhs_used_;
04796 std::vector<size_t> rhs_used_;
04797 ElementMatcherPairs matches_;
04798 ElementMatcherPairs best_so_far_;
04799 };
04800
04801 template <typename Graph>
04802 const size_t BacktrackingMaxBPMState<Graph>::kUnused;
04803
04804 }
04805
04806
04807
04808 template <typename Graph>
04809 ElementMatcherPairs
04810 FindBacktrackingMaxBPM(const Graph& g) {
04811 return BacktrackingMaxBPMState<Graph>(&g).Compute();
04812 }
04813
04814 class BacktrackingBPMTest : public ::testing::Test { };
04815
04816
04817
04818 class BipartiteTest : public ::testing::TestWithParam<int> { };
04819
04820
04821 TEST_P(BipartiteTest, Exhaustive) {
04822 int nodes = GetParam();
04823 MatchMatrix graph(nodes, nodes);
04824 do {
04825 ElementMatcherPairs matches =
04826 internal::FindMaxBipartiteMatching(graph);
04827 EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), matches.size())
04828 << "graph: " << graph.DebugString();
04829
04830
04831 std::vector<bool> seen_element(graph.LhsSize());
04832 std::vector<bool> seen_matcher(graph.RhsSize());
04833 SCOPED_TRACE(PrintToString(matches));
04834 for (size_t i = 0; i < matches.size(); ++i) {
04835 size_t ilhs = matches[i].first;
04836 size_t irhs = matches[i].second;
04837 EXPECT_TRUE(graph.HasEdge(ilhs, irhs));
04838 EXPECT_FALSE(seen_element[ilhs]);
04839 EXPECT_FALSE(seen_matcher[irhs]);
04840 seen_element[ilhs] = true;
04841 seen_matcher[irhs] = true;
04842 }
04843 } while (graph.NextGraph());
04844 }
04845
04846 INSTANTIATE_TEST_CASE_P(AllGraphs, BipartiteTest,
04847 ::testing::Range(0, 5));
04848
04849
04850 class BipartiteNonSquareTest
04851 : public ::testing::TestWithParam<std::pair<size_t, size_t> > {
04852 };
04853
04854 TEST_F(BipartiteNonSquareTest, SimpleBacktracking) {
04855
04856
04857
04858
04859
04860
04861
04862 MatchMatrix g(4, 3);
04863 static const int kEdges[][2] = { {0, 2}, {1, 1}, {2, 1}, {3, 0} };
04864 for (size_t i = 0; i < GMOCK_ARRAY_SIZE_(kEdges); ++i) {
04865 g.SetEdge(kEdges[i][0], kEdges[i][1], true);
04866 }
04867 EXPECT_THAT(FindBacktrackingMaxBPM(g),
04868 ElementsAre(Pair(3, 0),
04869 Pair(AnyOf(1, 2), 1),
04870 Pair(0, 2))) << g.DebugString();
04871 }
04872
04873
04874 TEST_P(BipartiteNonSquareTest, Exhaustive) {
04875 size_t nlhs = GetParam().first;
04876 size_t nrhs = GetParam().second;
04877 MatchMatrix graph(nlhs, nrhs);
04878 do {
04879 EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(),
04880 internal::FindMaxBipartiteMatching(graph).size())
04881 << "graph: " << graph.DebugString()
04882 << "\nbacktracking: "
04883 << PrintToString(FindBacktrackingMaxBPM(graph))
04884 << "\nmax flow: "
04885 << PrintToString(internal::FindMaxBipartiteMatching(graph));
04886 } while (graph.NextGraph());
04887 }
04888
04889 INSTANTIATE_TEST_CASE_P(AllGraphs, BipartiteNonSquareTest,
04890 testing::Values(
04891 std::make_pair(1, 2),
04892 std::make_pair(2, 1),
04893 std::make_pair(3, 2),
04894 std::make_pair(2, 3),
04895 std::make_pair(4, 1),
04896 std::make_pair(1, 4),
04897 std::make_pair(4, 3),
04898 std::make_pair(3, 4)));
04899
04900 class BipartiteRandomTest
04901 : public ::testing::TestWithParam<std::pair<int, int> > {
04902 };
04903
04904
04905 TEST_P(BipartiteRandomTest, LargerNets) {
04906 int nodes = GetParam().first;
04907 int iters = GetParam().second;
04908 MatchMatrix graph(nodes, nodes);
04909
04910 testing::internal::Int32 seed = GTEST_FLAG(random_seed);
04911 if (seed == 0) {
04912 seed = static_cast<testing::internal::Int32>(time(NULL));
04913 }
04914
04915 for (; iters > 0; --iters, ++seed) {
04916 srand(static_cast<int>(seed));
04917 graph.Randomize();
04918 EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(),
04919 internal::FindMaxBipartiteMatching(graph).size())
04920 << " graph: " << graph.DebugString()
04921 << "\nTo reproduce the failure, rerun the test with the flag"
04922 " --" << GTEST_FLAG_PREFIX_ << "random_seed=" << seed;
04923 }
04924 }
04925
04926
04927 INSTANTIATE_TEST_CASE_P(Samples, BipartiteRandomTest,
04928 testing::Values(
04929 std::make_pair(5, 10000),
04930 std::make_pair(6, 5000),
04931 std::make_pair(7, 2000),
04932 std::make_pair(8, 500),
04933 std::make_pair(9, 100)));
04934
04935
04936
04937 TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
04938 EXPECT_TRUE(IsReadableTypeName("int"));
04939 EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
04940 EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
04941 EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
04942 }
04943
04944 TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
04945 EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
04946 EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
04947 EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
04948 }
04949
04950 TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
04951 EXPECT_FALSE(
04952 IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
04953 EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
04954 }
04955
04956 TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
04957 EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
04958 }
04959
04960
04961
04962 TEST(JoinAsTupleTest, JoinsEmptyTuple) {
04963 EXPECT_EQ("", JoinAsTuple(Strings()));
04964 }
04965
04966 TEST(JoinAsTupleTest, JoinsOneTuple) {
04967 const char* fields[] = { "1" };
04968 EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
04969 }
04970
04971 TEST(JoinAsTupleTest, JoinsTwoTuple) {
04972 const char* fields[] = { "1", "a" };
04973 EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
04974 }
04975
04976 TEST(JoinAsTupleTest, JoinsTenTuple) {
04977 const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
04978 EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
04979 JoinAsTuple(Strings(fields, fields + 10)));
04980 }
04981
04982
04983
04984 TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
04985 EXPECT_EQ("is even",
04986 FormatMatcherDescription(false, "IsEven", Strings()));
04987 EXPECT_EQ("not (is even)",
04988 FormatMatcherDescription(true, "IsEven", Strings()));
04989
04990 const char* params[] = { "5" };
04991 EXPECT_EQ("equals 5",
04992 FormatMatcherDescription(false, "Equals",
04993 Strings(params, params + 1)));
04994
04995 const char* params2[] = { "5", "8" };
04996 EXPECT_EQ("is in range (5, 8)",
04997 FormatMatcherDescription(false, "IsInRange",
04998 Strings(params2, params2 + 2)));
04999 }
05000
05001
05002 TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
05003 PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
05004 DivisibleByImpl& impl = m.mutable_impl();
05005 EXPECT_EQ(42, impl.divider());
05006
05007 impl.set_divider(0);
05008 EXPECT_EQ(0, m.mutable_impl().divider());
05009 }
05010
05011
05012 TEST(PolymorphicMatcherTest, CanAccessImpl) {
05013 const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
05014 const DivisibleByImpl& impl = m.impl();
05015 EXPECT_EQ(42, impl.divider());
05016 }
05017
05018 TEST(MatcherTupleTest, ExplainsMatchFailure) {
05019 stringstream ss1;
05020 ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
05021 make_tuple('a', 10), &ss1);
05022 EXPECT_EQ("", ss1.str());
05023
05024 stringstream ss2;
05025 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
05026 make_tuple(2, 'b'), &ss2);
05027 EXPECT_EQ(" Expected arg #0: is > 5\n"
05028 " Actual: 2, which is 3 less than 5\n"
05029 " Expected arg #1: is equal to 'a' (97, 0x61)\n"
05030 " Actual: 'b' (98, 0x62)\n",
05031 ss2.str());
05032
05033 stringstream ss3;
05034 ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
05035 make_tuple(2, 'a'), &ss3);
05036 EXPECT_EQ(" Expected arg #0: is > 5\n"
05037 " Actual: 2, which is 3 less than 5\n",
05038 ss3.str());
05039
05040 }
05041
05042
05043
05044 TEST(EachTest, ExplainsMatchResultCorrectly) {
05045 set<int> a;
05046
05047 Matcher<set<int> > m = Each(2);
05048 EXPECT_EQ("", Explain(m, a));
05049
05050 Matcher<const int(&)[1]> n = Each(1);
05051
05052 const int b[1] = { 1 };
05053 EXPECT_EQ("", Explain(n, b));
05054
05055 n = Each(3);
05056 EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
05057
05058 a.insert(1);
05059 a.insert(2);
05060 a.insert(3);
05061 m = Each(GreaterThan(0));
05062 EXPECT_EQ("", Explain(m, a));
05063
05064 m = Each(GreaterThan(10));
05065 EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
05066 Explain(m, a));
05067 }
05068
05069 TEST(EachTest, DescribesItselfCorrectly) {
05070 Matcher<vector<int> > m = Each(1);
05071 EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
05072
05073 Matcher<vector<int> > m2 = Not(m);
05074 EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
05075 }
05076
05077 TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
05078 vector<int> some_vector;
05079 EXPECT_THAT(some_vector, Each(1));
05080 some_vector.push_back(3);
05081 EXPECT_THAT(some_vector, Not(Each(1)));
05082 EXPECT_THAT(some_vector, Each(3));
05083 some_vector.push_back(1);
05084 some_vector.push_back(2);
05085 EXPECT_THAT(some_vector, Not(Each(3)));
05086 EXPECT_THAT(some_vector, Each(Lt(3.5)));
05087
05088 vector<string> another_vector;
05089 another_vector.push_back("fee");
05090 EXPECT_THAT(another_vector, Each(string("fee")));
05091 another_vector.push_back("fie");
05092 another_vector.push_back("foe");
05093 another_vector.push_back("fum");
05094 EXPECT_THAT(another_vector, Not(Each(string("fee"))));
05095 }
05096
05097 TEST(EachTest, MatchesMapWhenAllElementsMatch) {
05098 map<const char*, int> my_map;
05099 const char* bar = "a string";
05100 my_map[bar] = 2;
05101 EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
05102
05103 map<string, int> another_map;
05104 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
05105 another_map["fee"] = 1;
05106 EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
05107 another_map["fie"] = 2;
05108 another_map["foe"] = 3;
05109 another_map["fum"] = 4;
05110 EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
05111 EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
05112 EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
05113 }
05114
05115 TEST(EachTest, AcceptsMatcher) {
05116 const int a[] = { 1, 2, 3 };
05117 EXPECT_THAT(a, Each(Gt(0)));
05118 EXPECT_THAT(a, Not(Each(Gt(1))));
05119 }
05120
05121 TEST(EachTest, WorksForNativeArrayAsTuple) {
05122 const int a[] = { 1, 2 };
05123 const int* const pointer = a;
05124 EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
05125 EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
05126 }
05127
05128
05129 class IsHalfOfMatcher {
05130 public:
05131 template <typename T1, typename T2>
05132 bool MatchAndExplain(const tuple<T1, T2>& a_pair,
05133 MatchResultListener* listener) const {
05134 if (get<0>(a_pair) == get<1>(a_pair)/2) {
05135 *listener << "where the second is " << get<1>(a_pair);
05136 return true;
05137 } else {
05138 *listener << "where the second/2 is " << get<1>(a_pair)/2;
05139 return false;
05140 }
05141 }
05142
05143 void DescribeTo(ostream* os) const {
05144 *os << "are a pair where the first is half of the second";
05145 }
05146
05147 void DescribeNegationTo(ostream* os) const {
05148 *os << "are a pair where the first isn't half of the second";
05149 }
05150 };
05151
05152 PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
05153 return MakePolymorphicMatcher(IsHalfOfMatcher());
05154 }
05155
05156 TEST(PointwiseTest, DescribesSelf) {
05157 vector<int> rhs;
05158 rhs.push_back(1);
05159 rhs.push_back(2);
05160 rhs.push_back(3);
05161 const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
05162 EXPECT_EQ("contains 3 values, where each value and its corresponding value "
05163 "in { 1, 2, 3 } are a pair where the first is half of the second",
05164 Describe(m));
05165 EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
05166 "index i where x and the i-th value of { 1, 2, 3 } are a pair "
05167 "where the first isn't half of the second",
05168 DescribeNegation(m));
05169 }
05170
05171 TEST(PointwiseTest, MakesCopyOfRhs) {
05172 list<signed char> rhs;
05173 rhs.push_back(2);
05174 rhs.push_back(4);
05175
05176 int lhs[] = { 1, 2 };
05177 const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
05178 EXPECT_THAT(lhs, m);
05179
05180
05181 rhs.push_back(6);
05182 EXPECT_THAT(lhs, m);
05183 }
05184
05185 TEST(PointwiseTest, WorksForLhsNativeArray) {
05186 const int lhs[] = { 1, 2, 3 };
05187 vector<int> rhs;
05188 rhs.push_back(2);
05189 rhs.push_back(4);
05190 rhs.push_back(6);
05191 EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
05192 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
05193 }
05194
05195 TEST(PointwiseTest, WorksForRhsNativeArray) {
05196 const int rhs[] = { 1, 2, 3 };
05197 vector<int> lhs;
05198 lhs.push_back(2);
05199 lhs.push_back(4);
05200 lhs.push_back(6);
05201 EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
05202 EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
05203 }
05204
05205 TEST(PointwiseTest, RejectsWrongSize) {
05206 const double lhs[2] = { 1, 2 };
05207 const int rhs[1] = { 0 };
05208 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
05209 EXPECT_EQ("which contains 2 values",
05210 Explain(Pointwise(Gt(), rhs), lhs));
05211
05212 const int rhs2[3] = { 0, 1, 2 };
05213 EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
05214 }
05215
05216 TEST(PointwiseTest, RejectsWrongContent) {
05217 const double lhs[3] = { 1, 2, 3 };
05218 const int rhs[3] = { 2, 6, 4 };
05219 EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
05220 EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
05221 "where the second/2 is 3",
05222 Explain(Pointwise(IsHalfOf(), rhs), lhs));
05223 }
05224
05225 TEST(PointwiseTest, AcceptsCorrectContent) {
05226 const double lhs[3] = { 1, 2, 3 };
05227 const int rhs[3] = { 2, 4, 6 };
05228 EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
05229 EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
05230 }
05231
05232 TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
05233 const double lhs[3] = { 1, 2, 3 };
05234 const int rhs[3] = { 2, 4, 6 };
05235 const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
05236 EXPECT_THAT(lhs, Pointwise(m1, rhs));
05237 EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
05238
05239
05240
05241 const Matcher<tuple<double, int> > m2 = IsHalfOf();
05242 EXPECT_THAT(lhs, Pointwise(m2, rhs));
05243 EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
05244 }
05245
05246 }
05247 }