Home | History | Annotate | Download | only in test
      1 // Copyright 2007, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 //
     30 // Author: wan (at) google.com (Zhanyong Wan)
     31 
     32 // Google Mock - a framework for writing C++ mock classes.
     33 //
     34 // This file tests some commonly used argument matchers.
     35 
     36 #include "gmock/gmock-matchers.h"
     37 
     38 #include <string.h>
     39 #include <functional>
     40 #include <iostream>
     41 #include <list>
     42 #include <map>
     43 #include <set>
     44 #include <sstream>
     45 #include <string>
     46 #include <utility>
     47 #include <vector>
     48 #include "gmock/gmock.h"
     49 #include "gtest/gtest.h"
     50 #include "gtest/gtest-spi.h"
     51 
     52 namespace testing {
     53 
     54 namespace internal {
     55 string JoinAsTuple(const Strings& fields);
     56 }  // namespace internal
     57 
     58 namespace gmock_matchers_test {
     59 
     60 using std::list;
     61 using std::make_pair;
     62 using std::map;
     63 using std::multimap;
     64 using std::multiset;
     65 using std::ostream;
     66 using std::pair;
     67 using std::set;
     68 using std::stringstream;
     69 using std::tr1::get;
     70 using std::tr1::make_tuple;
     71 using std::tr1::tuple;
     72 using std::vector;
     73 using testing::A;
     74 using testing::AllArgs;
     75 using testing::AllOf;
     76 using testing::An;
     77 using testing::AnyOf;
     78 using testing::ByRef;
     79 using testing::ContainsRegex;
     80 using testing::DoubleEq;
     81 using testing::EndsWith;
     82 using testing::Eq;
     83 using testing::ExplainMatchResult;
     84 using testing::Field;
     85 using testing::FloatEq;
     86 using testing::Ge;
     87 using testing::Gt;
     88 using testing::HasSubstr;
     89 using testing::IsNull;
     90 using testing::Key;
     91 using testing::Le;
     92 using testing::Lt;
     93 using testing::MakeMatcher;
     94 using testing::MakePolymorphicMatcher;
     95 using testing::MatchResultListener;
     96 using testing::Matcher;
     97 using testing::MatcherCast;
     98 using testing::MatcherInterface;
     99 using testing::Matches;
    100 using testing::MatchesRegex;
    101 using testing::NanSensitiveDoubleEq;
    102 using testing::NanSensitiveFloatEq;
    103 using testing::Ne;
    104 using testing::Not;
    105 using testing::NotNull;
    106 using testing::Pair;
    107 using testing::Pointee;
    108 using testing::Pointwise;
    109 using testing::PolymorphicMatcher;
    110 using testing::Property;
    111 using testing::Ref;
    112 using testing::ResultOf;
    113 using testing::StartsWith;
    114 using testing::StrCaseEq;
    115 using testing::StrCaseNe;
    116 using testing::StrEq;
    117 using testing::StrNe;
    118 using testing::Truly;
    119 using testing::TypedEq;
    120 using testing::Value;
    121 using testing::_;
    122 using testing::internal::DummyMatchResultListener;
    123 using testing::internal::ExplainMatchFailureTupleTo;
    124 using testing::internal::FloatingEqMatcher;
    125 using testing::internal::FormatMatcherDescription;
    126 using testing::internal::IsReadableTypeName;
    127 using testing::internal::JoinAsTuple;
    128 using testing::internal::RE;
    129 using testing::internal::StreamMatchResultListener;
    130 using testing::internal::String;
    131 using testing::internal::StringMatchResultListener;
    132 using testing::internal::Strings;
    133 using testing::internal::linked_ptr;
    134 using testing::internal::scoped_ptr;
    135 using testing::internal::string;
    136 
    137 // For testing ExplainMatchResultTo().
    138 class GreaterThanMatcher : public MatcherInterface<int> {
    139  public:
    140   explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
    141 
    142   virtual void DescribeTo(ostream* os) const {
    143     *os << "is > " << rhs_;
    144   }
    145 
    146   virtual bool MatchAndExplain(int lhs,
    147                                MatchResultListener* listener) const {
    148     const int diff = lhs - rhs_;
    149     if (diff > 0) {
    150       *listener << "which is " << diff << " more than " << rhs_;
    151     } else if (diff == 0) {
    152       *listener << "which is the same as " << rhs_;
    153     } else {
    154       *listener << "which is " << -diff << " less than " << rhs_;
    155     }
    156 
    157     return lhs > rhs_;
    158   }
    159 
    160  private:
    161   int rhs_;
    162 };
    163 
    164 Matcher<int> GreaterThan(int n) {
    165   return MakeMatcher(new GreaterThanMatcher(n));
    166 }
    167 
    168 string OfType(const string& type_name) {
    169 #if GTEST_HAS_RTTI
    170   return " (of type " + type_name + ")";
    171 #else
    172   return "";
    173 #endif
    174 }
    175 
    176 // Returns the description of the given matcher.
    177 template <typename T>
    178 string Describe(const Matcher<T>& m) {
    179   stringstream ss;
    180   m.DescribeTo(&ss);
    181   return ss.str();
    182 }
    183 
    184 // Returns the description of the negation of the given matcher.
    185 template <typename T>
    186 string DescribeNegation(const Matcher<T>& m) {
    187   stringstream ss;
    188   m.DescribeNegationTo(&ss);
    189   return ss.str();
    190 }
    191 
    192 // Returns the reason why x matches, or doesn't match, m.
    193 template <typename MatcherType, typename Value>
    194 string Explain(const MatcherType& m, const Value& x) {
    195   StringMatchResultListener listener;
    196   ExplainMatchResult(m, x, &listener);
    197   return listener.str();
    198 }
    199 
    200 TEST(MatchResultListenerTest, StreamingWorks) {
    201   StringMatchResultListener listener;
    202   listener << "hi" << 5;
    203   EXPECT_EQ("hi5", listener.str());
    204 
    205   // Streaming shouldn't crash when the underlying ostream is NULL.
    206   DummyMatchResultListener dummy;
    207   dummy << "hi" << 5;
    208 }
    209 
    210 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
    211   EXPECT_TRUE(DummyMatchResultListener().stream() == NULL);
    212   EXPECT_TRUE(StreamMatchResultListener(NULL).stream() == NULL);
    213 
    214   EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
    215 }
    216 
    217 TEST(MatchResultListenerTest, IsInterestedWorks) {
    218   EXPECT_TRUE(StringMatchResultListener().IsInterested());
    219   EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
    220 
    221   EXPECT_FALSE(DummyMatchResultListener().IsInterested());
    222   EXPECT_FALSE(StreamMatchResultListener(NULL).IsInterested());
    223 }
    224 
    225 // Makes sure that the MatcherInterface<T> interface doesn't
    226 // change.
    227 class EvenMatcherImpl : public MatcherInterface<int> {
    228  public:
    229   virtual bool MatchAndExplain(int x,
    230                                MatchResultListener* /* listener */) const {
    231     return x % 2 == 0;
    232   }
    233 
    234   virtual void DescribeTo(ostream* os) const {
    235     *os << "is an even number";
    236   }
    237 
    238   // We deliberately don't define DescribeNegationTo() and
    239   // ExplainMatchResultTo() here, to make sure the definition of these
    240   // two methods is optional.
    241 };
    242 
    243 // Makes sure that the MatcherInterface API doesn't change.
    244 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
    245   EvenMatcherImpl m;
    246 }
    247 
    248 // Tests implementing a monomorphic matcher using MatchAndExplain().
    249 
    250 class NewEvenMatcherImpl : public MatcherInterface<int> {
    251  public:
    252   virtual bool MatchAndExplain(int x, MatchResultListener* listener) const {
    253     const bool match = x % 2 == 0;
    254     // Verifies that we can stream to a listener directly.
    255     *listener << "value % " << 2;
    256     if (listener->stream() != NULL) {
    257       // Verifies that we can stream to a listener's underlying stream
    258       // too.
    259       *listener->stream() << " == " << (x % 2);
    260     }
    261     return match;
    262   }
    263 
    264   virtual void DescribeTo(ostream* os) const {
    265     *os << "is an even number";
    266   }
    267 };
    268 
    269 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
    270   Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
    271   EXPECT_TRUE(m.Matches(2));
    272   EXPECT_FALSE(m.Matches(3));
    273   EXPECT_EQ("value % 2 == 0", Explain(m, 2));
    274   EXPECT_EQ("value % 2 == 1", Explain(m, 3));
    275 }
    276 
    277 // Tests default-constructing a matcher.
    278 TEST(MatcherTest, CanBeDefaultConstructed) {
    279   Matcher<double> m;
    280 }
    281 
    282 // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
    283 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
    284   const MatcherInterface<int>* impl = new EvenMatcherImpl;
    285   Matcher<int> m(impl);
    286   EXPECT_TRUE(m.Matches(4));
    287   EXPECT_FALSE(m.Matches(5));
    288 }
    289 
    290 // Tests that value can be used in place of Eq(value).
    291 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
    292   Matcher<int> m1 = 5;
    293   EXPECT_TRUE(m1.Matches(5));
    294   EXPECT_FALSE(m1.Matches(6));
    295 }
    296 
    297 // Tests that NULL can be used in place of Eq(NULL).
    298 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
    299   Matcher<int*> m1 = NULL;
    300   EXPECT_TRUE(m1.Matches(NULL));
    301   int n = 0;
    302   EXPECT_FALSE(m1.Matches(&n));
    303 }
    304 
    305 // Tests that matchers are copyable.
    306 TEST(MatcherTest, IsCopyable) {
    307   // Tests the copy constructor.
    308   Matcher<bool> m1 = Eq(false);
    309   EXPECT_TRUE(m1.Matches(false));
    310   EXPECT_FALSE(m1.Matches(true));
    311 
    312   // Tests the assignment operator.
    313   m1 = Eq(true);
    314   EXPECT_TRUE(m1.Matches(true));
    315   EXPECT_FALSE(m1.Matches(false));
    316 }
    317 
    318 // Tests that Matcher<T>::DescribeTo() calls
    319 // MatcherInterface<T>::DescribeTo().
    320 TEST(MatcherTest, CanDescribeItself) {
    321   EXPECT_EQ("is an even number",
    322             Describe(Matcher<int>(new EvenMatcherImpl)));
    323 }
    324 
    325 // Tests Matcher<T>::MatchAndExplain().
    326 TEST(MatcherTest, MatchAndExplain) {
    327   Matcher<int> m = GreaterThan(0);
    328   StringMatchResultListener listener1;
    329   EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
    330   EXPECT_EQ("which is 42 more than 0", listener1.str());
    331 
    332   StringMatchResultListener listener2;
    333   EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
    334   EXPECT_EQ("which is 9 less than 0", listener2.str());
    335 }
    336 
    337 // Tests that a C-string literal can be implicitly converted to a
    338 // Matcher<string> or Matcher<const string&>.
    339 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
    340   Matcher<string> m1 = "hi";
    341   EXPECT_TRUE(m1.Matches("hi"));
    342   EXPECT_FALSE(m1.Matches("hello"));
    343 
    344   Matcher<const string&> m2 = "hi";
    345   EXPECT_TRUE(m2.Matches("hi"));
    346   EXPECT_FALSE(m2.Matches("hello"));
    347 }
    348 
    349 // Tests that a string object can be implicitly converted to a
    350 // Matcher<string> or Matcher<const string&>.
    351 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
    352   Matcher<string> m1 = string("hi");
    353   EXPECT_TRUE(m1.Matches("hi"));
    354   EXPECT_FALSE(m1.Matches("hello"));
    355 
    356   Matcher<const string&> m2 = string("hi");
    357   EXPECT_TRUE(m2.Matches("hi"));
    358   EXPECT_FALSE(m2.Matches("hello"));
    359 }
    360 
    361 // Tests that MakeMatcher() constructs a Matcher<T> from a
    362 // MatcherInterface* without requiring the user to explicitly
    363 // write the type.
    364 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
    365   const MatcherInterface<int>* dummy_impl = NULL;
    366   Matcher<int> m = MakeMatcher(dummy_impl);
    367 }
    368 
    369 // Tests that MakePolymorphicMatcher() can construct a polymorphic
    370 // matcher from its implementation using the old API.
    371 const int g_bar = 1;
    372 class ReferencesBarOrIsZeroImpl {
    373  public:
    374   template <typename T>
    375   bool MatchAndExplain(const T& x,
    376                        MatchResultListener* /* listener */) const {
    377     const void* p = &x;
    378     return p == &g_bar || x == 0;
    379   }
    380 
    381   void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
    382 
    383   void DescribeNegationTo(ostream* os) const {
    384     *os << "doesn't reference g_bar and is not zero";
    385   }
    386 };
    387 
    388 // This function verifies that MakePolymorphicMatcher() returns a
    389 // PolymorphicMatcher<T> where T is the argument's type.
    390 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
    391   return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
    392 }
    393 
    394 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
    395   // Using a polymorphic matcher to match a reference type.
    396   Matcher<const int&> m1 = ReferencesBarOrIsZero();
    397   EXPECT_TRUE(m1.Matches(0));
    398   // Verifies that the identity of a by-reference argument is preserved.
    399   EXPECT_TRUE(m1.Matches(g_bar));
    400   EXPECT_FALSE(m1.Matches(1));
    401   EXPECT_EQ("g_bar or zero", Describe(m1));
    402 
    403   // Using a polymorphic matcher to match a value type.
    404   Matcher<double> m2 = ReferencesBarOrIsZero();
    405   EXPECT_TRUE(m2.Matches(0.0));
    406   EXPECT_FALSE(m2.Matches(0.1));
    407   EXPECT_EQ("g_bar or zero", Describe(m2));
    408 }
    409 
    410 // Tests implementing a polymorphic matcher using MatchAndExplain().
    411 
    412 class PolymorphicIsEvenImpl {
    413  public:
    414   void DescribeTo(ostream* os) const { *os << "is even"; }
    415 
    416   void DescribeNegationTo(ostream* os) const {
    417     *os << "is odd";
    418   }
    419 
    420   template <typename T>
    421   bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
    422     // Verifies that we can stream to the listener directly.
    423     *listener << "% " << 2;
    424     if (listener->stream() != NULL) {
    425       // Verifies that we can stream to the listener's underlying stream
    426       // too.
    427       *listener->stream() << " == " << (x % 2);
    428     }
    429     return (x % 2) == 0;
    430   }
    431 };
    432 
    433 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
    434   return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
    435 }
    436 
    437 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
    438   // Using PolymorphicIsEven() as a Matcher<int>.
    439   const Matcher<int> m1 = PolymorphicIsEven();
    440   EXPECT_TRUE(m1.Matches(42));
    441   EXPECT_FALSE(m1.Matches(43));
    442   EXPECT_EQ("is even", Describe(m1));
    443 
    444   const Matcher<int> not_m1 = Not(m1);
    445   EXPECT_EQ("is odd", Describe(not_m1));
    446 
    447   EXPECT_EQ("% 2 == 0", Explain(m1, 42));
    448 
    449   // Using PolymorphicIsEven() as a Matcher<char>.
    450   const Matcher<char> m2 = PolymorphicIsEven();
    451   EXPECT_TRUE(m2.Matches('\x42'));
    452   EXPECT_FALSE(m2.Matches('\x43'));
    453   EXPECT_EQ("is even", Describe(m2));
    454 
    455   const Matcher<char> not_m2 = Not(m2);
    456   EXPECT_EQ("is odd", Describe(not_m2));
    457 
    458   EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
    459 }
    460 
    461 // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
    462 TEST(MatcherCastTest, FromPolymorphicMatcher) {
    463   Matcher<int> m = MatcherCast<int>(Eq(5));
    464   EXPECT_TRUE(m.Matches(5));
    465   EXPECT_FALSE(m.Matches(6));
    466 }
    467 
    468 // For testing casting matchers between compatible types.
    469 class IntValue {
    470  public:
    471   // An int can be statically (although not implicitly) cast to a
    472   // IntValue.
    473   explicit IntValue(int a_value) : value_(a_value) {}
    474 
    475   int value() const { return value_; }
    476  private:
    477   int value_;
    478 };
    479 
    480 // For testing casting matchers between compatible types.
    481 bool IsPositiveIntValue(const IntValue& foo) {
    482   return foo.value() > 0;
    483 }
    484 
    485 // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
    486 // can be statically converted to U.
    487 TEST(MatcherCastTest, FromCompatibleType) {
    488   Matcher<double> m1 = Eq(2.0);
    489   Matcher<int> m2 = MatcherCast<int>(m1);
    490   EXPECT_TRUE(m2.Matches(2));
    491   EXPECT_FALSE(m2.Matches(3));
    492 
    493   Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
    494   Matcher<int> m4 = MatcherCast<int>(m3);
    495   // In the following, the arguments 1 and 0 are statically converted
    496   // to IntValue objects, and then tested by the IsPositiveIntValue()
    497   // predicate.
    498   EXPECT_TRUE(m4.Matches(1));
    499   EXPECT_FALSE(m4.Matches(0));
    500 }
    501 
    502 // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
    503 TEST(MatcherCastTest, FromConstReferenceToNonReference) {
    504   Matcher<const int&> m1 = Eq(0);
    505   Matcher<int> m2 = MatcherCast<int>(m1);
    506   EXPECT_TRUE(m2.Matches(0));
    507   EXPECT_FALSE(m2.Matches(1));
    508 }
    509 
    510 // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
    511 TEST(MatcherCastTest, FromReferenceToNonReference) {
    512   Matcher<int&> m1 = Eq(0);
    513   Matcher<int> m2 = MatcherCast<int>(m1);
    514   EXPECT_TRUE(m2.Matches(0));
    515   EXPECT_FALSE(m2.Matches(1));
    516 }
    517 
    518 // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
    519 TEST(MatcherCastTest, FromNonReferenceToConstReference) {
    520   Matcher<int> m1 = Eq(0);
    521   Matcher<const int&> m2 = MatcherCast<const int&>(m1);
    522   EXPECT_TRUE(m2.Matches(0));
    523   EXPECT_FALSE(m2.Matches(1));
    524 }
    525 
    526 // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
    527 TEST(MatcherCastTest, FromNonReferenceToReference) {
    528   Matcher<int> m1 = Eq(0);
    529   Matcher<int&> m2 = MatcherCast<int&>(m1);
    530   int n = 0;
    531   EXPECT_TRUE(m2.Matches(n));
    532   n = 1;
    533   EXPECT_FALSE(m2.Matches(n));
    534 }
    535 
    536 // Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
    537 TEST(MatcherCastTest, FromSameType) {
    538   Matcher<int> m1 = Eq(0);
    539   Matcher<int> m2 = MatcherCast<int>(m1);
    540   EXPECT_TRUE(m2.Matches(0));
    541   EXPECT_FALSE(m2.Matches(1));
    542 }
    543 
    544 class Base {};
    545 class Derived : public Base {};
    546 
    547 // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
    548 TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
    549   Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
    550   EXPECT_TRUE(m2.Matches(' '));
    551   EXPECT_FALSE(m2.Matches('\n'));
    552 }
    553 
    554 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
    555 // T and U are arithmetic types and T can be losslessly converted to
    556 // U.
    557 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
    558   Matcher<double> m1 = DoubleEq(1.0);
    559   Matcher<float> m2 = SafeMatcherCast<float>(m1);
    560   EXPECT_TRUE(m2.Matches(1.0f));
    561   EXPECT_FALSE(m2.Matches(2.0f));
    562 
    563   Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
    564   EXPECT_TRUE(m3.Matches('a'));
    565   EXPECT_FALSE(m3.Matches('b'));
    566 }
    567 
    568 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
    569 // are pointers or references to a derived and a base class, correspondingly.
    570 TEST(SafeMatcherCastTest, FromBaseClass) {
    571   Derived d, d2;
    572   Matcher<Base*> m1 = Eq(&d);
    573   Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
    574   EXPECT_TRUE(m2.Matches(&d));
    575   EXPECT_FALSE(m2.Matches(&d2));
    576 
    577   Matcher<Base&> m3 = Ref(d);
    578   Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
    579   EXPECT_TRUE(m4.Matches(d));
    580   EXPECT_FALSE(m4.Matches(d2));
    581 }
    582 
    583 // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
    584 TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
    585   int n = 0;
    586   Matcher<const int&> m1 = Ref(n);
    587   Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
    588   int n1 = 0;
    589   EXPECT_TRUE(m2.Matches(n));
    590   EXPECT_FALSE(m2.Matches(n1));
    591 }
    592 
    593 // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
    594 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
    595   Matcher<int> m1 = Eq(0);
    596   Matcher<const int&> m2 = SafeMatcherCast<const int&>(m1);
    597   EXPECT_TRUE(m2.Matches(0));
    598   EXPECT_FALSE(m2.Matches(1));
    599 }
    600 
    601 // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
    602 TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
    603   Matcher<int> m1 = Eq(0);
    604   Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
    605   int n = 0;
    606   EXPECT_TRUE(m2.Matches(n));
    607   n = 1;
    608   EXPECT_FALSE(m2.Matches(n));
    609 }
    610 
    611 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
    612 TEST(SafeMatcherCastTest, FromSameType) {
    613   Matcher<int> m1 = Eq(0);
    614   Matcher<int> m2 = SafeMatcherCast<int>(m1);
    615   EXPECT_TRUE(m2.Matches(0));
    616   EXPECT_FALSE(m2.Matches(1));
    617 }
    618 
    619 // Tests that A<T>() matches any value of type T.
    620 TEST(ATest, MatchesAnyValue) {
    621   // Tests a matcher for a value type.
    622   Matcher<double> m1 = A<double>();
    623   EXPECT_TRUE(m1.Matches(91.43));
    624   EXPECT_TRUE(m1.Matches(-15.32));
    625 
    626   // Tests a matcher for a reference type.
    627   int a = 2;
    628   int b = -6;
    629   Matcher<int&> m2 = A<int&>();
    630   EXPECT_TRUE(m2.Matches(a));
    631   EXPECT_TRUE(m2.Matches(b));
    632 }
    633 
    634 // Tests that A<T>() describes itself properly.
    635 TEST(ATest, CanDescribeSelf) {
    636   EXPECT_EQ("is anything", Describe(A<bool>()));
    637 }
    638 
    639 // Tests that An<T>() matches any value of type T.
    640 TEST(AnTest, MatchesAnyValue) {
    641   // Tests a matcher for a value type.
    642   Matcher<int> m1 = An<int>();
    643   EXPECT_TRUE(m1.Matches(9143));
    644   EXPECT_TRUE(m1.Matches(-1532));
    645 
    646   // Tests a matcher for a reference type.
    647   int a = 2;
    648   int b = -6;
    649   Matcher<int&> m2 = An<int&>();
    650   EXPECT_TRUE(m2.Matches(a));
    651   EXPECT_TRUE(m2.Matches(b));
    652 }
    653 
    654 // Tests that An<T>() describes itself properly.
    655 TEST(AnTest, CanDescribeSelf) {
    656   EXPECT_EQ("is anything", Describe(An<int>()));
    657 }
    658 
    659 // Tests that _ can be used as a matcher for any type and matches any
    660 // value of that type.
    661 TEST(UnderscoreTest, MatchesAnyValue) {
    662   // Uses _ as a matcher for a value type.
    663   Matcher<int> m1 = _;
    664   EXPECT_TRUE(m1.Matches(123));
    665   EXPECT_TRUE(m1.Matches(-242));
    666 
    667   // Uses _ as a matcher for a reference type.
    668   bool a = false;
    669   const bool b = true;
    670   Matcher<const bool&> m2 = _;
    671   EXPECT_TRUE(m2.Matches(a));
    672   EXPECT_TRUE(m2.Matches(b));
    673 }
    674 
    675 // Tests that _ describes itself properly.
    676 TEST(UnderscoreTest, CanDescribeSelf) {
    677   Matcher<int> m = _;
    678   EXPECT_EQ("is anything", Describe(m));
    679 }
    680 
    681 // Tests that Eq(x) matches any value equal to x.
    682 TEST(EqTest, MatchesEqualValue) {
    683   // 2 C-strings with same content but different addresses.
    684   const char a1[] = "hi";
    685   const char a2[] = "hi";
    686 
    687   Matcher<const char*> m1 = Eq(a1);
    688   EXPECT_TRUE(m1.Matches(a1));
    689   EXPECT_FALSE(m1.Matches(a2));
    690 }
    691 
    692 // Tests that Eq(v) describes itself properly.
    693 
    694 class Unprintable {
    695  public:
    696   Unprintable() : c_('a') {}
    697 
    698   bool operator==(const Unprintable& /* rhs */) { return true; }
    699  private:
    700   char c_;
    701 };
    702 
    703 TEST(EqTest, CanDescribeSelf) {
    704   Matcher<Unprintable> m = Eq(Unprintable());
    705   EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
    706 }
    707 
    708 // Tests that Eq(v) can be used to match any type that supports
    709 // comparing with type T, where T is v's type.
    710 TEST(EqTest, IsPolymorphic) {
    711   Matcher<int> m1 = Eq(1);
    712   EXPECT_TRUE(m1.Matches(1));
    713   EXPECT_FALSE(m1.Matches(2));
    714 
    715   Matcher<char> m2 = Eq(1);
    716   EXPECT_TRUE(m2.Matches('\1'));
    717   EXPECT_FALSE(m2.Matches('a'));
    718 }
    719 
    720 // Tests that TypedEq<T>(v) matches values of type T that's equal to v.
    721 TEST(TypedEqTest, ChecksEqualityForGivenType) {
    722   Matcher<char> m1 = TypedEq<char>('a');
    723   EXPECT_TRUE(m1.Matches('a'));
    724   EXPECT_FALSE(m1.Matches('b'));
    725 
    726   Matcher<int> m2 = TypedEq<int>(6);
    727   EXPECT_TRUE(m2.Matches(6));
    728   EXPECT_FALSE(m2.Matches(7));
    729 }
    730 
    731 // Tests that TypedEq(v) describes itself properly.
    732 TEST(TypedEqTest, CanDescribeSelf) {
    733   EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
    734 }
    735 
    736 // Tests that TypedEq<T>(v) has type Matcher<T>.
    737 
    738 // Type<T>::IsTypeOf(v) compiles iff the type of value v is T, where T
    739 // is a "bare" type (i.e. not in the form of const U or U&).  If v's
    740 // type is not T, the compiler will generate a message about
    741 // "undefined referece".
    742 template <typename T>
    743 struct Type {
    744   static bool IsTypeOf(const T& /* v */) { return true; }
    745 
    746   template <typename T2>
    747   static void IsTypeOf(T2 v);
    748 };
    749 
    750 TEST(TypedEqTest, HasSpecifiedType) {
    751   // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
    752   Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
    753   Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
    754 }
    755 
    756 // Tests that Ge(v) matches anything >= v.
    757 TEST(GeTest, ImplementsGreaterThanOrEqual) {
    758   Matcher<int> m1 = Ge(0);
    759   EXPECT_TRUE(m1.Matches(1));
    760   EXPECT_TRUE(m1.Matches(0));
    761   EXPECT_FALSE(m1.Matches(-1));
    762 }
    763 
    764 // Tests that Ge(v) describes itself properly.
    765 TEST(GeTest, CanDescribeSelf) {
    766   Matcher<int> m = Ge(5);
    767   EXPECT_EQ("is >= 5", Describe(m));
    768 }
    769 
    770 // Tests that Gt(v) matches anything > v.
    771 TEST(GtTest, ImplementsGreaterThan) {
    772   Matcher<double> m1 = Gt(0);
    773   EXPECT_TRUE(m1.Matches(1.0));
    774   EXPECT_FALSE(m1.Matches(0.0));
    775   EXPECT_FALSE(m1.Matches(-1.0));
    776 }
    777 
    778 // Tests that Gt(v) describes itself properly.
    779 TEST(GtTest, CanDescribeSelf) {
    780   Matcher<int> m = Gt(5);
    781   EXPECT_EQ("is > 5", Describe(m));
    782 }
    783 
    784 // Tests that Le(v) matches anything <= v.
    785 TEST(LeTest, ImplementsLessThanOrEqual) {
    786   Matcher<char> m1 = Le('b');
    787   EXPECT_TRUE(m1.Matches('a'));
    788   EXPECT_TRUE(m1.Matches('b'));
    789   EXPECT_FALSE(m1.Matches('c'));
    790 }
    791 
    792 // Tests that Le(v) describes itself properly.
    793 TEST(LeTest, CanDescribeSelf) {
    794   Matcher<int> m = Le(5);
    795   EXPECT_EQ("is <= 5", Describe(m));
    796 }
    797 
    798 // Tests that Lt(v) matches anything < v.
    799 TEST(LtTest, ImplementsLessThan) {
    800   Matcher<const string&> m1 = Lt("Hello");
    801   EXPECT_TRUE(m1.Matches("Abc"));
    802   EXPECT_FALSE(m1.Matches("Hello"));
    803   EXPECT_FALSE(m1.Matches("Hello, world!"));
    804 }
    805 
    806 // Tests that Lt(v) describes itself properly.
    807 TEST(LtTest, CanDescribeSelf) {
    808   Matcher<int> m = Lt(5);
    809   EXPECT_EQ("is < 5", Describe(m));
    810 }
    811 
    812 // Tests that Ne(v) matches anything != v.
    813 TEST(NeTest, ImplementsNotEqual) {
    814   Matcher<int> m1 = Ne(0);
    815   EXPECT_TRUE(m1.Matches(1));
    816   EXPECT_TRUE(m1.Matches(-1));
    817   EXPECT_FALSE(m1.Matches(0));
    818 }
    819 
    820 // Tests that Ne(v) describes itself properly.
    821 TEST(NeTest, CanDescribeSelf) {
    822   Matcher<int> m = Ne(5);
    823   EXPECT_EQ("isn't equal to 5", Describe(m));
    824 }
    825 
    826 // Tests that IsNull() matches any NULL pointer of any type.
    827 TEST(IsNullTest, MatchesNullPointer) {
    828   Matcher<int*> m1 = IsNull();
    829   int* p1 = NULL;
    830   int n = 0;
    831   EXPECT_TRUE(m1.Matches(p1));
    832   EXPECT_FALSE(m1.Matches(&n));
    833 
    834   Matcher<const char*> m2 = IsNull();
    835   const char* p2 = NULL;
    836   EXPECT_TRUE(m2.Matches(p2));
    837   EXPECT_FALSE(m2.Matches("hi"));
    838 
    839 #if !GTEST_OS_SYMBIAN
    840   // Nokia's Symbian compiler generates:
    841   // gmock-matchers.h: ambiguous access to overloaded function
    842   // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)'
    843   // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing::
    844   //     MatcherInterface<void *> *)'
    845   // gmock-matchers.h:  (point of instantiation: 'testing::
    846   //     gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()')
    847   // gmock-matchers.h:   (instantiating: 'testing::PolymorphicMatc
    848   Matcher<void*> m3 = IsNull();
    849   void* p3 = NULL;
    850   EXPECT_TRUE(m3.Matches(p3));
    851   EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
    852 #endif
    853 }
    854 
    855 TEST(IsNullTest, LinkedPtr) {
    856   const Matcher<linked_ptr<int> > m = IsNull();
    857   const linked_ptr<int> null_p;
    858   const linked_ptr<int> non_null_p(new int);
    859 
    860   EXPECT_TRUE(m.Matches(null_p));
    861   EXPECT_FALSE(m.Matches(non_null_p));
    862 }
    863 
    864 TEST(IsNullTest, ReferenceToConstLinkedPtr) {
    865   const Matcher<const linked_ptr<double>&> m = IsNull();
    866   const linked_ptr<double> null_p;
    867   const linked_ptr<double> non_null_p(new double);
    868 
    869   EXPECT_TRUE(m.Matches(null_p));
    870   EXPECT_FALSE(m.Matches(non_null_p));
    871 }
    872 
    873 TEST(IsNullTest, ReferenceToConstScopedPtr) {
    874   const Matcher<const scoped_ptr<double>&> m = IsNull();
    875   const scoped_ptr<double> null_p;
    876   const scoped_ptr<double> non_null_p(new double);
    877 
    878   EXPECT_TRUE(m.Matches(null_p));
    879   EXPECT_FALSE(m.Matches(non_null_p));
    880 }
    881 
    882 // Tests that IsNull() describes itself properly.
    883 TEST(IsNullTest, CanDescribeSelf) {
    884   Matcher<int*> m = IsNull();
    885   EXPECT_EQ("is NULL", Describe(m));
    886   EXPECT_EQ("isn't NULL", DescribeNegation(m));
    887 }
    888 
    889 // Tests that NotNull() matches any non-NULL pointer of any type.
    890 TEST(NotNullTest, MatchesNonNullPointer) {
    891   Matcher<int*> m1 = NotNull();
    892   int* p1 = NULL;
    893   int n = 0;
    894   EXPECT_FALSE(m1.Matches(p1));
    895   EXPECT_TRUE(m1.Matches(&n));
    896 
    897   Matcher<const char*> m2 = NotNull();
    898   const char* p2 = NULL;
    899   EXPECT_FALSE(m2.Matches(p2));
    900   EXPECT_TRUE(m2.Matches("hi"));
    901 }
    902 
    903 TEST(NotNullTest, LinkedPtr) {
    904   const Matcher<linked_ptr<int> > m = NotNull();
    905   const linked_ptr<int> null_p;
    906   const linked_ptr<int> non_null_p(new int);
    907 
    908   EXPECT_FALSE(m.Matches(null_p));
    909   EXPECT_TRUE(m.Matches(non_null_p));
    910 }
    911 
    912 TEST(NotNullTest, ReferenceToConstLinkedPtr) {
    913   const Matcher<const linked_ptr<double>&> m = NotNull();
    914   const linked_ptr<double> null_p;
    915   const linked_ptr<double> non_null_p(new double);
    916 
    917   EXPECT_FALSE(m.Matches(null_p));
    918   EXPECT_TRUE(m.Matches(non_null_p));
    919 }
    920 
    921 TEST(NotNullTest, ReferenceToConstScopedPtr) {
    922   const Matcher<const scoped_ptr<double>&> m = NotNull();
    923   const scoped_ptr<double> null_p;
    924   const scoped_ptr<double> non_null_p(new double);
    925 
    926   EXPECT_FALSE(m.Matches(null_p));
    927   EXPECT_TRUE(m.Matches(non_null_p));
    928 }
    929 
    930 // Tests that NotNull() describes itself properly.
    931 TEST(NotNullTest, CanDescribeSelf) {
    932   Matcher<int*> m = NotNull();
    933   EXPECT_EQ("isn't NULL", Describe(m));
    934 }
    935 
    936 // Tests that Ref(variable) matches an argument that references
    937 // 'variable'.
    938 TEST(RefTest, MatchesSameVariable) {
    939   int a = 0;
    940   int b = 0;
    941   Matcher<int&> m = Ref(a);
    942   EXPECT_TRUE(m.Matches(a));
    943   EXPECT_FALSE(m.Matches(b));
    944 }
    945 
    946 // Tests that Ref(variable) describes itself properly.
    947 TEST(RefTest, CanDescribeSelf) {
    948   int n = 5;
    949   Matcher<int&> m = Ref(n);
    950   stringstream ss;
    951   ss << "references the variable @" << &n << " 5";
    952   EXPECT_EQ(string(ss.str()), Describe(m));
    953 }
    954 
    955 // Test that Ref(non_const_varialbe) can be used as a matcher for a
    956 // const reference.
    957 TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
    958   int a = 0;
    959   int b = 0;
    960   Matcher<const int&> m = Ref(a);
    961   EXPECT_TRUE(m.Matches(a));
    962   EXPECT_FALSE(m.Matches(b));
    963 }
    964 
    965 // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
    966 // used wherever Ref(base) can be used (Ref(derived) is a sub-type
    967 // of Ref(base), but not vice versa.
    968 
    969 TEST(RefTest, IsCovariant) {
    970   Base base, base2;
    971   Derived derived;
    972   Matcher<const Base&> m1 = Ref(base);
    973   EXPECT_TRUE(m1.Matches(base));
    974   EXPECT_FALSE(m1.Matches(base2));
    975   EXPECT_FALSE(m1.Matches(derived));
    976 
    977   m1 = Ref(derived);
    978   EXPECT_TRUE(m1.Matches(derived));
    979   EXPECT_FALSE(m1.Matches(base));
    980   EXPECT_FALSE(m1.Matches(base2));
    981 }
    982 
    983 TEST(RefTest, ExplainsResult) {
    984   int n = 0;
    985   EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
    986               StartsWith("which is located @"));
    987 
    988   int m = 0;
    989   EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
    990               StartsWith("which is located @"));
    991 }
    992 
    993 // Tests string comparison matchers.
    994 
    995 TEST(StrEqTest, MatchesEqualString) {
    996   Matcher<const char*> m = StrEq(string("Hello"));
    997   EXPECT_TRUE(m.Matches("Hello"));
    998   EXPECT_FALSE(m.Matches("hello"));
    999   EXPECT_FALSE(m.Matches(NULL));
   1000 
   1001   Matcher<const string&> m2 = StrEq("Hello");
   1002   EXPECT_TRUE(m2.Matches("Hello"));
   1003   EXPECT_FALSE(m2.Matches("Hi"));
   1004 }
   1005 
   1006 TEST(StrEqTest, CanDescribeSelf) {
   1007   Matcher<string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
   1008   EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
   1009       Describe(m));
   1010 
   1011   string str("01204500800");
   1012   str[3] = '\0';
   1013   Matcher<string> m2 = StrEq(str);
   1014   EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
   1015   str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
   1016   Matcher<string> m3 = StrEq(str);
   1017   EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
   1018 }
   1019 
   1020 TEST(StrNeTest, MatchesUnequalString) {
   1021   Matcher<const char*> m = StrNe("Hello");
   1022   EXPECT_TRUE(m.Matches(""));
   1023   EXPECT_TRUE(m.Matches(NULL));
   1024   EXPECT_FALSE(m.Matches("Hello"));
   1025 
   1026   Matcher<string> m2 = StrNe(string("Hello"));
   1027   EXPECT_TRUE(m2.Matches("hello"));
   1028   EXPECT_FALSE(m2.Matches("Hello"));
   1029 }
   1030 
   1031 TEST(StrNeTest, CanDescribeSelf) {
   1032   Matcher<const char*> m = StrNe("Hi");
   1033   EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
   1034 }
   1035 
   1036 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
   1037   Matcher<const char*> m = StrCaseEq(string("Hello"));
   1038   EXPECT_TRUE(m.Matches("Hello"));
   1039   EXPECT_TRUE(m.Matches("hello"));
   1040   EXPECT_FALSE(m.Matches("Hi"));
   1041   EXPECT_FALSE(m.Matches(NULL));
   1042 
   1043   Matcher<const string&> m2 = StrCaseEq("Hello");
   1044   EXPECT_TRUE(m2.Matches("hello"));
   1045   EXPECT_FALSE(m2.Matches("Hi"));
   1046 }
   1047 
   1048 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
   1049   string str1("oabocdooeoo");
   1050   string str2("OABOCDOOEOO");
   1051   Matcher<const string&> m0 = StrCaseEq(str1);
   1052   EXPECT_FALSE(m0.Matches(str2 + string(1, '\0')));
   1053 
   1054   str1[3] = str2[3] = '\0';
   1055   Matcher<const string&> m1 = StrCaseEq(str1);
   1056   EXPECT_TRUE(m1.Matches(str2));
   1057 
   1058   str1[0] = str1[6] = str1[7] = str1[10] = '\0';
   1059   str2[0] = str2[6] = str2[7] = str2[10] = '\0';
   1060   Matcher<const string&> m2 = StrCaseEq(str1);
   1061   str1[9] = str2[9] = '\0';
   1062   EXPECT_FALSE(m2.Matches(str2));
   1063 
   1064   Matcher<const string&> m3 = StrCaseEq(str1);
   1065   EXPECT_TRUE(m3.Matches(str2));
   1066 
   1067   EXPECT_FALSE(m3.Matches(str2 + "x"));
   1068   str2.append(1, '\0');
   1069   EXPECT_FALSE(m3.Matches(str2));
   1070   EXPECT_FALSE(m3.Matches(string(str2, 0, 9)));
   1071 }
   1072 
   1073 TEST(StrCaseEqTest, CanDescribeSelf) {
   1074   Matcher<string> m = StrCaseEq("Hi");
   1075   EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
   1076 }
   1077 
   1078 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
   1079   Matcher<const char*> m = StrCaseNe("Hello");
   1080   EXPECT_TRUE(m.Matches("Hi"));
   1081   EXPECT_TRUE(m.Matches(NULL));
   1082   EXPECT_FALSE(m.Matches("Hello"));
   1083   EXPECT_FALSE(m.Matches("hello"));
   1084 
   1085   Matcher<string> m2 = StrCaseNe(string("Hello"));
   1086   EXPECT_TRUE(m2.Matches(""));
   1087   EXPECT_FALSE(m2.Matches("Hello"));
   1088 }
   1089 
   1090 TEST(StrCaseNeTest, CanDescribeSelf) {
   1091   Matcher<const char*> m = StrCaseNe("Hi");
   1092   EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
   1093 }
   1094 
   1095 // Tests that HasSubstr() works for matching string-typed values.
   1096 TEST(HasSubstrTest, WorksForStringClasses) {
   1097   const Matcher<string> m1 = HasSubstr("foo");
   1098   EXPECT_TRUE(m1.Matches(string("I love food.")));
   1099   EXPECT_FALSE(m1.Matches(string("tofo")));
   1100 
   1101   const Matcher<const std::string&> m2 = HasSubstr("foo");
   1102   EXPECT_TRUE(m2.Matches(std::string("I love food.")));
   1103   EXPECT_FALSE(m2.Matches(std::string("tofo")));
   1104 }
   1105 
   1106 // Tests that HasSubstr() works for matching C-string-typed values.
   1107 TEST(HasSubstrTest, WorksForCStrings) {
   1108   const Matcher<char*> m1 = HasSubstr("foo");
   1109   EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
   1110   EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
   1111   EXPECT_FALSE(m1.Matches(NULL));
   1112 
   1113   const Matcher<const char*> m2 = HasSubstr("foo");
   1114   EXPECT_TRUE(m2.Matches("I love food."));
   1115   EXPECT_FALSE(m2.Matches("tofo"));
   1116   EXPECT_FALSE(m2.Matches(NULL));
   1117 }
   1118 
   1119 // Tests that HasSubstr(s) describes itself properly.
   1120 TEST(HasSubstrTest, CanDescribeSelf) {
   1121   Matcher<string> m = HasSubstr("foo\n\"");
   1122   EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
   1123 }
   1124 
   1125 TEST(KeyTest, CanDescribeSelf) {
   1126   Matcher<const pair<std::string, int>&> m = Key("foo");
   1127   EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
   1128   EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
   1129 }
   1130 
   1131 TEST(KeyTest, ExplainsResult) {
   1132   Matcher<pair<int, bool> > m = Key(GreaterThan(10));
   1133   EXPECT_EQ("whose first field is a value which is 5 less than 10",
   1134             Explain(m, make_pair(5, true)));
   1135   EXPECT_EQ("whose first field is a value which is 5 more than 10",
   1136             Explain(m, make_pair(15, true)));
   1137 }
   1138 
   1139 TEST(KeyTest, MatchesCorrectly) {
   1140   pair<int, std::string> p(25, "foo");
   1141   EXPECT_THAT(p, Key(25));
   1142   EXPECT_THAT(p, Not(Key(42)));
   1143   EXPECT_THAT(p, Key(Ge(20)));
   1144   EXPECT_THAT(p, Not(Key(Lt(25))));
   1145 }
   1146 
   1147 TEST(KeyTest, SafelyCastsInnerMatcher) {
   1148   Matcher<int> is_positive = Gt(0);
   1149   Matcher<int> is_negative = Lt(0);
   1150   pair<char, bool> p('a', true);
   1151   EXPECT_THAT(p, Key(is_positive));
   1152   EXPECT_THAT(p, Not(Key(is_negative)));
   1153 }
   1154 
   1155 TEST(KeyTest, InsideContainsUsingMap) {
   1156   map<int, char> container;
   1157   container.insert(make_pair(1, 'a'));
   1158   container.insert(make_pair(2, 'b'));
   1159   container.insert(make_pair(4, 'c'));
   1160   EXPECT_THAT(container, Contains(Key(1)));
   1161   EXPECT_THAT(container, Not(Contains(Key(3))));
   1162 }
   1163 
   1164 TEST(KeyTest, InsideContainsUsingMultimap) {
   1165   multimap<int, char> container;
   1166   container.insert(make_pair(1, 'a'));
   1167   container.insert(make_pair(2, 'b'));
   1168   container.insert(make_pair(4, 'c'));
   1169 
   1170   EXPECT_THAT(container, Not(Contains(Key(25))));
   1171   container.insert(make_pair(25, 'd'));
   1172   EXPECT_THAT(container, Contains(Key(25)));
   1173   container.insert(make_pair(25, 'e'));
   1174   EXPECT_THAT(container, Contains(Key(25)));
   1175 
   1176   EXPECT_THAT(container, Contains(Key(1)));
   1177   EXPECT_THAT(container, Not(Contains(Key(3))));
   1178 }
   1179 
   1180 TEST(PairTest, Typing) {
   1181   // Test verifies the following type conversions can be compiled.
   1182   Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
   1183   Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
   1184   Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
   1185 
   1186   Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
   1187   Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
   1188 }
   1189 
   1190 TEST(PairTest, CanDescribeSelf) {
   1191   Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
   1192   EXPECT_EQ("has a first field that is equal to \"foo\""
   1193             ", and has a second field that is equal to 42",
   1194             Describe(m1));
   1195   EXPECT_EQ("has a first field that isn't equal to \"foo\""
   1196             ", or has a second field that isn't equal to 42",
   1197             DescribeNegation(m1));
   1198   // Double and triple negation (1 or 2 times not and description of negation).
   1199   Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
   1200   EXPECT_EQ("has a first field that isn't equal to 13"
   1201             ", and has a second field that is equal to 42",
   1202             DescribeNegation(m2));
   1203 }
   1204 
   1205 TEST(PairTest, CanExplainMatchResultTo) {
   1206   // If neither field matches, Pair() should explain about the first
   1207   // field.
   1208   const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
   1209   EXPECT_EQ("whose first field does not match, which is 1 less than 0",
   1210             Explain(m, make_pair(-1, -2)));
   1211 
   1212   // If the first field matches but the second doesn't, Pair() should
   1213   // explain about the second field.
   1214   EXPECT_EQ("whose second field does not match, which is 2 less than 0",
   1215             Explain(m, make_pair(1, -2)));
   1216 
   1217   // If the first field doesn't match but the second does, Pair()
   1218   // should explain about the first field.
   1219   EXPECT_EQ("whose first field does not match, which is 1 less than 0",
   1220             Explain(m, make_pair(-1, 2)));
   1221 
   1222   // If both fields match, Pair() should explain about them both.
   1223   EXPECT_EQ("whose both fields match, where the first field is a value "
   1224             "which is 1 more than 0, and the second field is a value "
   1225             "which is 2 more than 0",
   1226             Explain(m, make_pair(1, 2)));
   1227 
   1228   // If only the first match has an explanation, only this explanation should
   1229   // be printed.
   1230   const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
   1231   EXPECT_EQ("whose both fields match, where the first field is a value "
   1232             "which is 1 more than 0",
   1233             Explain(explain_first, make_pair(1, 0)));
   1234 
   1235   // If only the second match has an explanation, only this explanation should
   1236   // be printed.
   1237   const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
   1238   EXPECT_EQ("whose both fields match, where the second field is a value "
   1239             "which is 1 more than 0",
   1240             Explain(explain_second, make_pair(0, 1)));
   1241 }
   1242 
   1243 TEST(PairTest, MatchesCorrectly) {
   1244   pair<int, std::string> p(25, "foo");
   1245 
   1246   // Both fields match.
   1247   EXPECT_THAT(p, Pair(25, "foo"));
   1248   EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
   1249 
   1250   // 'first' doesnt' match, but 'second' matches.
   1251   EXPECT_THAT(p, Not(Pair(42, "foo")));
   1252   EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
   1253 
   1254   // 'first' matches, but 'second' doesn't match.
   1255   EXPECT_THAT(p, Not(Pair(25, "bar")));
   1256   EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
   1257 
   1258   // Neither field matches.
   1259   EXPECT_THAT(p, Not(Pair(13, "bar")));
   1260   EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
   1261 }
   1262 
   1263 TEST(PairTest, SafelyCastsInnerMatchers) {
   1264   Matcher<int> is_positive = Gt(0);
   1265   Matcher<int> is_negative = Lt(0);
   1266   pair<char, bool> p('a', true);
   1267   EXPECT_THAT(p, Pair(is_positive, _));
   1268   EXPECT_THAT(p, Not(Pair(is_negative, _)));
   1269   EXPECT_THAT(p, Pair(_, is_positive));
   1270   EXPECT_THAT(p, Not(Pair(_, is_negative)));
   1271 }
   1272 
   1273 TEST(PairTest, InsideContainsUsingMap) {
   1274   map<int, char> container;
   1275   container.insert(make_pair(1, 'a'));
   1276   container.insert(make_pair(2, 'b'));
   1277   container.insert(make_pair(4, 'c'));
   1278   EXPECT_THAT(container, Contains(Pair(1, 'a')));
   1279   EXPECT_THAT(container, Contains(Pair(1, _)));
   1280   EXPECT_THAT(container, Contains(Pair(_, 'a')));
   1281   EXPECT_THAT(container, Not(Contains(Pair(3, _))));
   1282 }
   1283 
   1284 // Tests StartsWith(s).
   1285 
   1286 TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
   1287   const Matcher<const char*> m1 = StartsWith(string(""));
   1288   EXPECT_TRUE(m1.Matches("Hi"));
   1289   EXPECT_TRUE(m1.Matches(""));
   1290   EXPECT_FALSE(m1.Matches(NULL));
   1291 
   1292   const Matcher<const string&> m2 = StartsWith("Hi");
   1293   EXPECT_TRUE(m2.Matches("Hi"));
   1294   EXPECT_TRUE(m2.Matches("Hi Hi!"));
   1295   EXPECT_TRUE(m2.Matches("High"));
   1296   EXPECT_FALSE(m2.Matches("H"));
   1297   EXPECT_FALSE(m2.Matches(" Hi"));
   1298 }
   1299 
   1300 TEST(StartsWithTest, CanDescribeSelf) {
   1301   Matcher<const std::string> m = StartsWith("Hi");
   1302   EXPECT_EQ("starts with \"Hi\"", Describe(m));
   1303 }
   1304 
   1305 // Tests EndsWith(s).
   1306 
   1307 TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
   1308   const Matcher<const char*> m1 = EndsWith("");
   1309   EXPECT_TRUE(m1.Matches("Hi"));
   1310   EXPECT_TRUE(m1.Matches(""));
   1311   EXPECT_FALSE(m1.Matches(NULL));
   1312 
   1313   const Matcher<const string&> m2 = EndsWith(string("Hi"));
   1314   EXPECT_TRUE(m2.Matches("Hi"));
   1315   EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
   1316   EXPECT_TRUE(m2.Matches("Super Hi"));
   1317   EXPECT_FALSE(m2.Matches("i"));
   1318   EXPECT_FALSE(m2.Matches("Hi "));
   1319 }
   1320 
   1321 TEST(EndsWithTest, CanDescribeSelf) {
   1322   Matcher<const std::string> m = EndsWith("Hi");
   1323   EXPECT_EQ("ends with \"Hi\"", Describe(m));
   1324 }
   1325 
   1326 // Tests MatchesRegex().
   1327 
   1328 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
   1329   const Matcher<const char*> m1 = MatchesRegex("a.*z");
   1330   EXPECT_TRUE(m1.Matches("az"));
   1331   EXPECT_TRUE(m1.Matches("abcz"));
   1332   EXPECT_FALSE(m1.Matches(NULL));
   1333 
   1334   const Matcher<const string&> m2 = MatchesRegex(new RE("a.*z"));
   1335   EXPECT_TRUE(m2.Matches("azbz"));
   1336   EXPECT_FALSE(m2.Matches("az1"));
   1337   EXPECT_FALSE(m2.Matches("1az"));
   1338 }
   1339 
   1340 TEST(MatchesRegexTest, CanDescribeSelf) {
   1341   Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
   1342   EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
   1343 
   1344   Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
   1345   EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
   1346 }
   1347 
   1348 // Tests ContainsRegex().
   1349 
   1350 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
   1351   const Matcher<const char*> m1 = ContainsRegex(string("a.*z"));
   1352   EXPECT_TRUE(m1.Matches("az"));
   1353   EXPECT_TRUE(m1.Matches("0abcz1"));
   1354   EXPECT_FALSE(m1.Matches(NULL));
   1355 
   1356   const Matcher<const string&> m2 = ContainsRegex(new RE("a.*z"));
   1357   EXPECT_TRUE(m2.Matches("azbz"));
   1358   EXPECT_TRUE(m2.Matches("az1"));
   1359   EXPECT_FALSE(m2.Matches("1a"));
   1360 }
   1361 
   1362 TEST(ContainsRegexTest, CanDescribeSelf) {
   1363   Matcher<const std::string> m1 = ContainsRegex("Hi.*");
   1364   EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
   1365 
   1366   Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
   1367   EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
   1368 }
   1369 
   1370 // Tests for wide strings.
   1371 #if GTEST_HAS_STD_WSTRING
   1372 TEST(StdWideStrEqTest, MatchesEqual) {
   1373   Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
   1374   EXPECT_TRUE(m.Matches(L"Hello"));
   1375   EXPECT_FALSE(m.Matches(L"hello"));
   1376   EXPECT_FALSE(m.Matches(NULL));
   1377 
   1378   Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
   1379   EXPECT_TRUE(m2.Matches(L"Hello"));
   1380   EXPECT_FALSE(m2.Matches(L"Hi"));
   1381 
   1382   Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
   1383   EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
   1384   EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
   1385 
   1386   ::std::wstring str(L"01204500800");
   1387   str[3] = L'\0';
   1388   Matcher<const ::std::wstring&> m4 = StrEq(str);
   1389   EXPECT_TRUE(m4.Matches(str));
   1390   str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
   1391   Matcher<const ::std::wstring&> m5 = StrEq(str);
   1392   EXPECT_TRUE(m5.Matches(str));
   1393 }
   1394 
   1395 TEST(StdWideStrEqTest, CanDescribeSelf) {
   1396   Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
   1397   EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
   1398     Describe(m));
   1399 
   1400   Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
   1401   EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
   1402     Describe(m2));
   1403 
   1404   ::std::wstring str(L"01204500800");
   1405   str[3] = L'\0';
   1406   Matcher<const ::std::wstring&> m4 = StrEq(str);
   1407   EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
   1408   str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
   1409   Matcher<const ::std::wstring&> m5 = StrEq(str);
   1410   EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
   1411 }
   1412 
   1413 TEST(StdWideStrNeTest, MatchesUnequalString) {
   1414   Matcher<const wchar_t*> m = StrNe(L"Hello");
   1415   EXPECT_TRUE(m.Matches(L""));
   1416   EXPECT_TRUE(m.Matches(NULL));
   1417   EXPECT_FALSE(m.Matches(L"Hello"));
   1418 
   1419   Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
   1420   EXPECT_TRUE(m2.Matches(L"hello"));
   1421   EXPECT_FALSE(m2.Matches(L"Hello"));
   1422 }
   1423 
   1424 TEST(StdWideStrNeTest, CanDescribeSelf) {
   1425   Matcher<const wchar_t*> m = StrNe(L"Hi");
   1426   EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
   1427 }
   1428 
   1429 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
   1430   Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
   1431   EXPECT_TRUE(m.Matches(L"Hello"));
   1432   EXPECT_TRUE(m.Matches(L"hello"));
   1433   EXPECT_FALSE(m.Matches(L"Hi"));
   1434   EXPECT_FALSE(m.Matches(NULL));
   1435 
   1436   Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
   1437   EXPECT_TRUE(m2.Matches(L"hello"));
   1438   EXPECT_FALSE(m2.Matches(L"Hi"));
   1439 }
   1440 
   1441 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
   1442   ::std::wstring str1(L"oabocdooeoo");
   1443   ::std::wstring str2(L"OABOCDOOEOO");
   1444   Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
   1445   EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
   1446 
   1447   str1[3] = str2[3] = L'\0';
   1448   Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
   1449   EXPECT_TRUE(m1.Matches(str2));
   1450 
   1451   str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
   1452   str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
   1453   Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
   1454   str1[9] = str2[9] = L'\0';
   1455   EXPECT_FALSE(m2.Matches(str2));
   1456 
   1457   Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
   1458   EXPECT_TRUE(m3.Matches(str2));
   1459 
   1460   EXPECT_FALSE(m3.Matches(str2 + L"x"));
   1461   str2.append(1, L'\0');
   1462   EXPECT_FALSE(m3.Matches(str2));
   1463   EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
   1464 }
   1465 
   1466 TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
   1467   Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
   1468   EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
   1469 }
   1470 
   1471 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
   1472   Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
   1473   EXPECT_TRUE(m.Matches(L"Hi"));
   1474   EXPECT_TRUE(m.Matches(NULL));
   1475   EXPECT_FALSE(m.Matches(L"Hello"));
   1476   EXPECT_FALSE(m.Matches(L"hello"));
   1477 
   1478   Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
   1479   EXPECT_TRUE(m2.Matches(L""));
   1480   EXPECT_FALSE(m2.Matches(L"Hello"));
   1481 }
   1482 
   1483 TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
   1484   Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
   1485   EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
   1486 }
   1487 
   1488 // Tests that HasSubstr() works for matching wstring-typed values.
   1489 TEST(StdWideHasSubstrTest, WorksForStringClasses) {
   1490   const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
   1491   EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
   1492   EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
   1493 
   1494   const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
   1495   EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
   1496   EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
   1497 }
   1498 
   1499 // Tests that HasSubstr() works for matching C-wide-string-typed values.
   1500 TEST(StdWideHasSubstrTest, WorksForCStrings) {
   1501   const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
   1502   EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
   1503   EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
   1504   EXPECT_FALSE(m1.Matches(NULL));
   1505 
   1506   const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
   1507   EXPECT_TRUE(m2.Matches(L"I love food."));
   1508   EXPECT_FALSE(m2.Matches(L"tofo"));
   1509   EXPECT_FALSE(m2.Matches(NULL));
   1510 }
   1511 
   1512 // Tests that HasSubstr(s) describes itself properly.
   1513 TEST(StdWideHasSubstrTest, CanDescribeSelf) {
   1514   Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
   1515   EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
   1516 }
   1517 
   1518 // Tests StartsWith(s).
   1519 
   1520 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
   1521   const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
   1522   EXPECT_TRUE(m1.Matches(L"Hi"));
   1523   EXPECT_TRUE(m1.Matches(L""));
   1524   EXPECT_FALSE(m1.Matches(NULL));
   1525 
   1526   const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
   1527   EXPECT_TRUE(m2.Matches(L"Hi"));
   1528   EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
   1529   EXPECT_TRUE(m2.Matches(L"High"));
   1530   EXPECT_FALSE(m2.Matches(L"H"));
   1531   EXPECT_FALSE(m2.Matches(L" Hi"));
   1532 }
   1533 
   1534 TEST(StdWideStartsWithTest, CanDescribeSelf) {
   1535   Matcher<const ::std::wstring> m = StartsWith(L"Hi");
   1536   EXPECT_EQ("starts with L\"Hi\"", Describe(m));
   1537 }
   1538 
   1539 // Tests EndsWith(s).
   1540 
   1541 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
   1542   const Matcher<const wchar_t*> m1 = EndsWith(L"");
   1543   EXPECT_TRUE(m1.Matches(L"Hi"));
   1544   EXPECT_TRUE(m1.Matches(L""));
   1545   EXPECT_FALSE(m1.Matches(NULL));
   1546 
   1547   const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
   1548   EXPECT_TRUE(m2.Matches(L"Hi"));
   1549   EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
   1550   EXPECT_TRUE(m2.Matches(L"Super Hi"));
   1551   EXPECT_FALSE(m2.Matches(L"i"));
   1552   EXPECT_FALSE(m2.Matches(L"Hi "));
   1553 }
   1554 
   1555 TEST(StdWideEndsWithTest, CanDescribeSelf) {
   1556   Matcher<const ::std::wstring> m = EndsWith(L"Hi");
   1557   EXPECT_EQ("ends with L\"Hi\"", Describe(m));
   1558 }
   1559 
   1560 #endif  // GTEST_HAS_STD_WSTRING
   1561 
   1562 #if GTEST_HAS_GLOBAL_WSTRING
   1563 TEST(GlobalWideStrEqTest, MatchesEqual) {
   1564   Matcher<const wchar_t*> m = StrEq(::wstring(L"Hello"));
   1565   EXPECT_TRUE(m.Matches(L"Hello"));
   1566   EXPECT_FALSE(m.Matches(L"hello"));
   1567   EXPECT_FALSE(m.Matches(NULL));
   1568 
   1569   Matcher<const ::wstring&> m2 = StrEq(L"Hello");
   1570   EXPECT_TRUE(m2.Matches(L"Hello"));
   1571   EXPECT_FALSE(m2.Matches(L"Hi"));
   1572 
   1573   Matcher<const ::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
   1574   EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
   1575   EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
   1576 
   1577   ::wstring str(L"01204500800");
   1578   str[3] = L'\0';
   1579   Matcher<const ::wstring&> m4 = StrEq(str);
   1580   EXPECT_TRUE(m4.Matches(str));
   1581   str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
   1582   Matcher<const ::wstring&> m5 = StrEq(str);
   1583   EXPECT_TRUE(m5.Matches(str));
   1584 }
   1585 
   1586 TEST(GlobalWideStrEqTest, CanDescribeSelf) {
   1587   Matcher< ::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
   1588   EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
   1589     Describe(m));
   1590 
   1591   Matcher< ::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
   1592   EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
   1593     Describe(m2));
   1594 
   1595   ::wstring str(L"01204500800");
   1596   str[3] = L'\0';
   1597   Matcher<const ::wstring&> m4 = StrEq(str);
   1598   EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
   1599   str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
   1600   Matcher<const ::wstring&> m5 = StrEq(str);
   1601   EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
   1602 }
   1603 
   1604 TEST(GlobalWideStrNeTest, MatchesUnequalString) {
   1605   Matcher<const wchar_t*> m = StrNe(L"Hello");
   1606   EXPECT_TRUE(m.Matches(L""));
   1607   EXPECT_TRUE(m.Matches(NULL));
   1608   EXPECT_FALSE(m.Matches(L"Hello"));
   1609 
   1610   Matcher< ::wstring> m2 = StrNe(::wstring(L"Hello"));
   1611   EXPECT_TRUE(m2.Matches(L"hello"));
   1612   EXPECT_FALSE(m2.Matches(L"Hello"));
   1613 }
   1614 
   1615 TEST(GlobalWideStrNeTest, CanDescribeSelf) {
   1616   Matcher<const wchar_t*> m = StrNe(L"Hi");
   1617   EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
   1618 }
   1619 
   1620 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
   1621   Matcher<const wchar_t*> m = StrCaseEq(::wstring(L"Hello"));
   1622   EXPECT_TRUE(m.Matches(L"Hello"));
   1623   EXPECT_TRUE(m.Matches(L"hello"));
   1624   EXPECT_FALSE(m.Matches(L"Hi"));
   1625   EXPECT_FALSE(m.Matches(NULL));
   1626 
   1627   Matcher<const ::wstring&> m2 = StrCaseEq(L"Hello");
   1628   EXPECT_TRUE(m2.Matches(L"hello"));
   1629   EXPECT_FALSE(m2.Matches(L"Hi"));
   1630 }
   1631 
   1632 TEST(GlobalWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
   1633   ::wstring str1(L"oabocdooeoo");
   1634   ::wstring str2(L"OABOCDOOEOO");
   1635   Matcher<const ::wstring&> m0 = StrCaseEq(str1);
   1636   EXPECT_FALSE(m0.Matches(str2 + ::wstring(1, L'\0')));
   1637 
   1638   str1[3] = str2[3] = L'\0';
   1639   Matcher<const ::wstring&> m1 = StrCaseEq(str1);
   1640   EXPECT_TRUE(m1.Matches(str2));
   1641 
   1642   str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
   1643   str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
   1644   Matcher<const ::wstring&> m2 = StrCaseEq(str1);
   1645   str1[9] = str2[9] = L'\0';
   1646   EXPECT_FALSE(m2.Matches(str2));
   1647 
   1648   Matcher<const ::wstring&> m3 = StrCaseEq(str1);
   1649   EXPECT_TRUE(m3.Matches(str2));
   1650 
   1651   EXPECT_FALSE(m3.Matches(str2 + L"x"));
   1652   str2.append(1, L'\0');
   1653   EXPECT_FALSE(m3.Matches(str2));
   1654   EXPECT_FALSE(m3.Matches(::wstring(str2, 0, 9)));
   1655 }
   1656 
   1657 TEST(GlobalWideStrCaseEqTest, CanDescribeSelf) {
   1658   Matcher< ::wstring> m = StrCaseEq(L"Hi");
   1659   EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
   1660 }
   1661 
   1662 TEST(GlobalWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
   1663   Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
   1664   EXPECT_TRUE(m.Matches(L"Hi"));
   1665   EXPECT_TRUE(m.Matches(NULL));
   1666   EXPECT_FALSE(m.Matches(L"Hello"));
   1667   EXPECT_FALSE(m.Matches(L"hello"));
   1668 
   1669   Matcher< ::wstring> m2 = StrCaseNe(::wstring(L"Hello"));
   1670   EXPECT_TRUE(m2.Matches(L""));
   1671   EXPECT_FALSE(m2.Matches(L"Hello"));
   1672 }
   1673 
   1674 TEST(GlobalWideStrCaseNeTest, CanDescribeSelf) {
   1675   Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
   1676   EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
   1677 }
   1678 
   1679 // Tests that HasSubstr() works for matching wstring-typed values.
   1680 TEST(GlobalWideHasSubstrTest, WorksForStringClasses) {
   1681   const Matcher< ::wstring> m1 = HasSubstr(L"foo");
   1682   EXPECT_TRUE(m1.Matches(::wstring(L"I love food.")));
   1683   EXPECT_FALSE(m1.Matches(::wstring(L"tofo")));
   1684 
   1685   const Matcher<const ::wstring&> m2 = HasSubstr(L"foo");
   1686   EXPECT_TRUE(m2.Matches(::wstring(L"I love food.")));
   1687   EXPECT_FALSE(m2.Matches(::wstring(L"tofo")));
   1688 }
   1689 
   1690 // Tests that HasSubstr() works for matching C-wide-string-typed values.
   1691 TEST(GlobalWideHasSubstrTest, WorksForCStrings) {
   1692   const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
   1693   EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
   1694   EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
   1695   EXPECT_FALSE(m1.Matches(NULL));
   1696 
   1697   const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
   1698   EXPECT_TRUE(m2.Matches(L"I love food."));
   1699   EXPECT_FALSE(m2.Matches(L"tofo"));
   1700   EXPECT_FALSE(m2.Matches(NULL));
   1701 }
   1702 
   1703 // Tests that HasSubstr(s) describes itself properly.
   1704 TEST(GlobalWideHasSubstrTest, CanDescribeSelf) {
   1705   Matcher< ::wstring> m = HasSubstr(L"foo\n\"");
   1706   EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
   1707 }
   1708 
   1709 // Tests StartsWith(s).
   1710 
   1711 TEST(GlobalWideStartsWithTest, MatchesStringWithGivenPrefix) {
   1712   const Matcher<const wchar_t*> m1 = StartsWith(::wstring(L""));
   1713   EXPECT_TRUE(m1.Matches(L"Hi"));
   1714   EXPECT_TRUE(m1.Matches(L""));
   1715   EXPECT_FALSE(m1.Matches(NULL));
   1716 
   1717   const Matcher<const ::wstring&> m2 = StartsWith(L"Hi");
   1718   EXPECT_TRUE(m2.Matches(L"Hi"));
   1719   EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
   1720   EXPECT_TRUE(m2.Matches(L"High"));
   1721   EXPECT_FALSE(m2.Matches(L"H"));
   1722   EXPECT_FALSE(m2.Matches(L" Hi"));
   1723 }
   1724 
   1725 TEST(GlobalWideStartsWithTest, CanDescribeSelf) {
   1726   Matcher<const ::wstring> m = StartsWith(L"Hi");
   1727   EXPECT_EQ("starts with L\"Hi\"", Describe(m));
   1728 }
   1729 
   1730 // Tests EndsWith(s).
   1731 
   1732 TEST(GlobalWideEndsWithTest, MatchesStringWithGivenSuffix) {
   1733   const Matcher<const wchar_t*> m1 = EndsWith(L"");
   1734   EXPECT_TRUE(m1.Matches(L"Hi"));
   1735   EXPECT_TRUE(m1.Matches(L""));
   1736   EXPECT_FALSE(m1.Matches(NULL));
   1737 
   1738   const Matcher<const ::wstring&> m2 = EndsWith(::wstring(L"Hi"));
   1739   EXPECT_TRUE(m2.Matches(L"Hi"));
   1740   EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
   1741   EXPECT_TRUE(m2.Matches(L"Super Hi"));
   1742   EXPECT_FALSE(m2.Matches(L"i"));
   1743   EXPECT_FALSE(m2.Matches(L"Hi "));
   1744 }
   1745 
   1746 TEST(GlobalWideEndsWithTest, CanDescribeSelf) {
   1747   Matcher<const ::wstring> m = EndsWith(L"Hi");
   1748   EXPECT_EQ("ends with L\"Hi\"", Describe(m));
   1749 }
   1750 
   1751 #endif  // GTEST_HAS_GLOBAL_WSTRING
   1752 
   1753 
   1754 typedef ::std::tr1::tuple<long, int> Tuple2;  // NOLINT
   1755 
   1756 // Tests that Eq() matches a 2-tuple where the first field == the
   1757 // second field.
   1758 TEST(Eq2Test, MatchesEqualArguments) {
   1759   Matcher<const Tuple2&> m = Eq();
   1760   EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
   1761   EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
   1762 }
   1763 
   1764 // Tests that Eq() describes itself properly.
   1765 TEST(Eq2Test, CanDescribeSelf) {
   1766   Matcher<const Tuple2&> m = Eq();
   1767   EXPECT_EQ("are an equal pair", Describe(m));
   1768 }
   1769 
   1770 // Tests that Ge() matches a 2-tuple where the first field >= the
   1771 // second field.
   1772 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
   1773   Matcher<const Tuple2&> m = Ge();
   1774   EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
   1775   EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
   1776   EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
   1777 }
   1778 
   1779 // Tests that Ge() describes itself properly.
   1780 TEST(Ge2Test, CanDescribeSelf) {
   1781   Matcher<const Tuple2&> m = Ge();
   1782   EXPECT_EQ("are a pair where the first >= the second", Describe(m));
   1783 }
   1784 
   1785 // Tests that Gt() matches a 2-tuple where the first field > the
   1786 // second field.
   1787 TEST(Gt2Test, MatchesGreaterThanArguments) {
   1788   Matcher<const Tuple2&> m = Gt();
   1789   EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
   1790   EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
   1791   EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
   1792 }
   1793 
   1794 // Tests that Gt() describes itself properly.
   1795 TEST(Gt2Test, CanDescribeSelf) {
   1796   Matcher<const Tuple2&> m = Gt();
   1797   EXPECT_EQ("are a pair where the first > the second", Describe(m));
   1798 }
   1799 
   1800 // Tests that Le() matches a 2-tuple where the first field <= the
   1801 // second field.
   1802 TEST(Le2Test, MatchesLessThanOrEqualArguments) {
   1803   Matcher<const Tuple2&> m = Le();
   1804   EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
   1805   EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
   1806   EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
   1807 }
   1808 
   1809 // Tests that Le() describes itself properly.
   1810 TEST(Le2Test, CanDescribeSelf) {
   1811   Matcher<const Tuple2&> m = Le();
   1812   EXPECT_EQ("are a pair where the first <= the second", Describe(m));
   1813 }
   1814 
   1815 // Tests that Lt() matches a 2-tuple where the first field < the
   1816 // second field.
   1817 TEST(Lt2Test, MatchesLessThanArguments) {
   1818   Matcher<const Tuple2&> m = Lt();
   1819   EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
   1820   EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
   1821   EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
   1822 }
   1823 
   1824 // Tests that Lt() describes itself properly.
   1825 TEST(Lt2Test, CanDescribeSelf) {
   1826   Matcher<const Tuple2&> m = Lt();
   1827   EXPECT_EQ("are a pair where the first < the second", Describe(m));
   1828 }
   1829 
   1830 // Tests that Ne() matches a 2-tuple where the first field != the
   1831 // second field.
   1832 TEST(Ne2Test, MatchesUnequalArguments) {
   1833   Matcher<const Tuple2&> m = Ne();
   1834   EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
   1835   EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
   1836   EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
   1837 }
   1838 
   1839 // Tests that Ne() describes itself properly.
   1840 TEST(Ne2Test, CanDescribeSelf) {
   1841   Matcher<const Tuple2&> m = Ne();
   1842   EXPECT_EQ("are an unequal pair", Describe(m));
   1843 }
   1844 
   1845 // Tests that Not(m) matches any value that doesn't match m.
   1846 TEST(NotTest, NegatesMatcher) {
   1847   Matcher<int> m;
   1848   m = Not(Eq(2));
   1849   EXPECT_TRUE(m.Matches(3));
   1850   EXPECT_FALSE(m.Matches(2));
   1851 }
   1852 
   1853 // Tests that Not(m) describes itself properly.
   1854 TEST(NotTest, CanDescribeSelf) {
   1855   Matcher<int> m = Not(Eq(5));
   1856   EXPECT_EQ("isn't equal to 5", Describe(m));
   1857 }
   1858 
   1859 // Tests that monomorphic matchers are safely cast by the Not matcher.
   1860 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
   1861   // greater_than_5 is a monomorphic matcher.
   1862   Matcher<int> greater_than_5 = Gt(5);
   1863 
   1864   Matcher<const int&> m = Not(greater_than_5);
   1865   Matcher<int&> m2 = Not(greater_than_5);
   1866   Matcher<int&> m3 = Not(m);
   1867 }
   1868 
   1869 // Helper to allow easy testing of AllOf matchers with num parameters.
   1870 void AllOfMatches(int num, const Matcher<int>& m) {
   1871   SCOPED_TRACE(Describe(m));
   1872   EXPECT_TRUE(m.Matches(0));
   1873   for (int i = 1; i <= num; ++i) {
   1874     EXPECT_FALSE(m.Matches(i));
   1875   }
   1876   EXPECT_TRUE(m.Matches(num + 1));
   1877 }
   1878 
   1879 // Tests that AllOf(m1, ..., mn) matches any value that matches all of
   1880 // the given matchers.
   1881 TEST(AllOfTest, MatchesWhenAllMatch) {
   1882   Matcher<int> m;
   1883   m = AllOf(Le(2), Ge(1));
   1884   EXPECT_TRUE(m.Matches(1));
   1885   EXPECT_TRUE(m.Matches(2));
   1886   EXPECT_FALSE(m.Matches(0));
   1887   EXPECT_FALSE(m.Matches(3));
   1888 
   1889   m = AllOf(Gt(0), Ne(1), Ne(2));
   1890   EXPECT_TRUE(m.Matches(3));
   1891   EXPECT_FALSE(m.Matches(2));
   1892   EXPECT_FALSE(m.Matches(1));
   1893   EXPECT_FALSE(m.Matches(0));
   1894 
   1895   m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
   1896   EXPECT_TRUE(m.Matches(4));
   1897   EXPECT_FALSE(m.Matches(3));
   1898   EXPECT_FALSE(m.Matches(2));
   1899   EXPECT_FALSE(m.Matches(1));
   1900   EXPECT_FALSE(m.Matches(0));
   1901 
   1902   m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
   1903   EXPECT_TRUE(m.Matches(0));
   1904   EXPECT_TRUE(m.Matches(1));
   1905   EXPECT_FALSE(m.Matches(3));
   1906 
   1907   // The following tests for varying number of sub-matchers. Due to the way
   1908   // the sub-matchers are handled it is enough to test every sub-matcher once
   1909   // with sub-matchers using the same matcher type. Varying matcher types are
   1910   // checked for above.
   1911   AllOfMatches(2, AllOf(Ne(1), Ne(2)));
   1912   AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
   1913   AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
   1914   AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
   1915   AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
   1916   AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
   1917   AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
   1918                         Ne(8)));
   1919   AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
   1920                         Ne(8), Ne(9)));
   1921   AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
   1922                          Ne(9), Ne(10)));
   1923 }
   1924 
   1925 // Tests that AllOf(m1, ..., mn) describes itself properly.
   1926 TEST(AllOfTest, CanDescribeSelf) {
   1927   Matcher<int> m;
   1928   m = AllOf(Le(2), Ge(1));
   1929   EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
   1930 
   1931   m = AllOf(Gt(0), Ne(1), Ne(2));
   1932   EXPECT_EQ("(is > 0) and "
   1933             "((isn't equal to 1) and "
   1934             "(isn't equal to 2))",
   1935             Describe(m));
   1936 
   1937 
   1938   m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
   1939   EXPECT_EQ("(is > 0) and "
   1940             "((isn't equal to 1) and "
   1941             "((isn't equal to 2) and "
   1942             "(isn't equal to 3)))",
   1943             Describe(m));
   1944 
   1945 
   1946   m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
   1947   EXPECT_EQ("(is >= 0) and "
   1948             "((is < 10) and "
   1949             "((isn't equal to 3) and "
   1950             "((isn't equal to 5) and "
   1951             "(isn't equal to 7))))",
   1952             Describe(m));
   1953 }
   1954 
   1955 // Tests that AllOf(m1, ..., mn) describes its negation properly.
   1956 TEST(AllOfTest, CanDescribeNegation) {
   1957   Matcher<int> m;
   1958   m = AllOf(Le(2), Ge(1));
   1959   EXPECT_EQ("(isn't <= 2) or "
   1960             "(isn't >= 1)",
   1961             DescribeNegation(m));
   1962 
   1963   m = AllOf(Gt(0), Ne(1), Ne(2));
   1964   EXPECT_EQ("(isn't > 0) or "
   1965             "((is equal to 1) or "
   1966             "(is equal to 2))",
   1967             DescribeNegation(m));
   1968 
   1969 
   1970   m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
   1971   EXPECT_EQ("(isn't > 0) or "
   1972             "((is equal to 1) or "
   1973             "((is equal to 2) or "
   1974             "(is equal to 3)))",
   1975             DescribeNegation(m));
   1976 
   1977 
   1978   m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
   1979   EXPECT_EQ("(isn't >= 0) or "
   1980             "((isn't < 10) or "
   1981             "((is equal to 3) or "
   1982             "((is equal to 5) or "
   1983             "(is equal to 7))))",
   1984             DescribeNegation(m));
   1985 }
   1986 
   1987 // Tests that monomorphic matchers are safely cast by the AllOf matcher.
   1988 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
   1989   // greater_than_5 and less_than_10 are monomorphic matchers.
   1990   Matcher<int> greater_than_5 = Gt(5);
   1991   Matcher<int> less_than_10 = Lt(10);
   1992 
   1993   Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
   1994   Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
   1995   Matcher<int&> m3 = AllOf(greater_than_5, m2);
   1996 
   1997   // Tests that BothOf works when composing itself.
   1998   Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
   1999   Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
   2000 }
   2001 
   2002 TEST(AllOfTest, ExplainsResult) {
   2003   Matcher<int> m;
   2004 
   2005   // Successful match.  Both matchers need to explain.  The second
   2006   // matcher doesn't give an explanation, so only the first matcher's
   2007   // explanation is printed.
   2008   m = AllOf(GreaterThan(10), Lt(30));
   2009   EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
   2010 
   2011   // Successful match.  Both matchers need to explain.
   2012   m = AllOf(GreaterThan(10), GreaterThan(20));
   2013   EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
   2014             Explain(m, 30));
   2015 
   2016   // Successful match.  All matchers need to explain.  The second
   2017   // matcher doesn't given an explanation.
   2018   m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
   2019   EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
   2020             Explain(m, 25));
   2021 
   2022   // Successful match.  All matchers need to explain.
   2023   m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
   2024   EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
   2025             "and which is 10 more than 30",
   2026             Explain(m, 40));
   2027 
   2028   // Failed match.  The first matcher, which failed, needs to
   2029   // explain.
   2030   m = AllOf(GreaterThan(10), GreaterThan(20));
   2031   EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
   2032 
   2033   // Failed match.  The second matcher, which failed, needs to
   2034   // explain.  Since it doesn't given an explanation, nothing is
   2035   // printed.
   2036   m = AllOf(GreaterThan(10), Lt(30));
   2037   EXPECT_EQ("", Explain(m, 40));
   2038 
   2039   // Failed match.  The second matcher, which failed, needs to
   2040   // explain.
   2041   m = AllOf(GreaterThan(10), GreaterThan(20));
   2042   EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
   2043 }
   2044 
   2045 // Helper to allow easy testing of AnyOf matchers with num parameters.
   2046 void AnyOfMatches(int num, const Matcher<int>& m) {
   2047   SCOPED_TRACE(Describe(m));
   2048   EXPECT_FALSE(m.Matches(0));
   2049   for (int i = 1; i <= num; ++i) {
   2050     EXPECT_TRUE(m.Matches(i));
   2051   }
   2052   EXPECT_FALSE(m.Matches(num + 1));
   2053 }
   2054 
   2055 // Tests that AnyOf(m1, ..., mn) matches any value that matches at
   2056 // least one of the given matchers.
   2057 TEST(AnyOfTest, MatchesWhenAnyMatches) {
   2058   Matcher<int> m;
   2059   m = AnyOf(Le(1), Ge(3));
   2060   EXPECT_TRUE(m.Matches(1));
   2061   EXPECT_TRUE(m.Matches(4));
   2062   EXPECT_FALSE(m.Matches(2));
   2063 
   2064   m = AnyOf(Lt(0), Eq(1), Eq(2));
   2065   EXPECT_TRUE(m.Matches(-1));
   2066   EXPECT_TRUE(m.Matches(1));
   2067   EXPECT_TRUE(m.Matches(2));
   2068   EXPECT_FALSE(m.Matches(0));
   2069 
   2070   m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
   2071   EXPECT_TRUE(m.Matches(-1));
   2072   EXPECT_TRUE(m.Matches(1));
   2073   EXPECT_TRUE(m.Matches(2));
   2074   EXPECT_TRUE(m.Matches(3));
   2075   EXPECT_FALSE(m.Matches(0));
   2076 
   2077   m = AnyOf(Le(0), Gt(10), 3, 5, 7);
   2078   EXPECT_TRUE(m.Matches(0));
   2079   EXPECT_TRUE(m.Matches(11));
   2080   EXPECT_TRUE(m.Matches(3));
   2081   EXPECT_FALSE(m.Matches(2));
   2082 
   2083   // The following tests for varying number of sub-matchers. Due to the way
   2084   // the sub-matchers are handled it is enough to test every sub-matcher once
   2085   // with sub-matchers using the same matcher type. Varying matcher types are
   2086   // checked for above.
   2087   AnyOfMatches(2, AnyOf(1, 2));
   2088   AnyOfMatches(3, AnyOf(1, 2, 3));
   2089   AnyOfMatches(4, AnyOf(1, 2, 3, 4));
   2090   AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
   2091   AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
   2092   AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
   2093   AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
   2094   AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
   2095   AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
   2096 }
   2097 
   2098 // Tests that AnyOf(m1, ..., mn) describes itself properly.
   2099 TEST(AnyOfTest, CanDescribeSelf) {
   2100   Matcher<int> m;
   2101   m = AnyOf(Le(1), Ge(3));
   2102   EXPECT_EQ("(is <= 1) or (is >= 3)",
   2103             Describe(m));
   2104 
   2105   m = AnyOf(Lt(0), Eq(1), Eq(2));
   2106   EXPECT_EQ("(is < 0) or "
   2107             "((is equal to 1) or (is equal to 2))",
   2108             Describe(m));
   2109 
   2110   m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
   2111   EXPECT_EQ("(is < 0) or "
   2112             "((is equal to 1) or "
   2113             "((is equal to 2) or "
   2114             "(is equal to 3)))",
   2115             Describe(m));
   2116 
   2117   m = AnyOf(Le(0), Gt(10), 3, 5, 7);
   2118   EXPECT_EQ("(is <= 0) or "
   2119             "((is > 10) or "
   2120             "((is equal to 3) or "
   2121             "((is equal to 5) or "
   2122             "(is equal to 7))))",
   2123             Describe(m));
   2124 }
   2125 
   2126 // Tests that AnyOf(m1, ..., mn) describes its negation properly.
   2127 TEST(AnyOfTest, CanDescribeNegation) {
   2128   Matcher<int> m;
   2129   m = AnyOf(Le(1), Ge(3));
   2130   EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
   2131             DescribeNegation(m));
   2132 
   2133   m = AnyOf(Lt(0), Eq(1), Eq(2));
   2134   EXPECT_EQ("(isn't < 0) and "
   2135             "((isn't equal to 1) and (isn't equal to 2))",
   2136             DescribeNegation(m));
   2137 
   2138   m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
   2139   EXPECT_EQ("(isn't < 0) and "
   2140             "((isn't equal to 1) and "
   2141             "((isn't equal to 2) and "
   2142             "(isn't equal to 3)))",
   2143             DescribeNegation(m));
   2144 
   2145   m = AnyOf(Le(0), Gt(10), 3, 5, 7);
   2146   EXPECT_EQ("(isn't <= 0) and "
   2147             "((isn't > 10) and "
   2148             "((isn't equal to 3) and "
   2149             "((isn't equal to 5) and "
   2150             "(isn't equal to 7))))",
   2151             DescribeNegation(m));
   2152 }
   2153 
   2154 // Tests that monomorphic matchers are safely cast by the AnyOf matcher.
   2155 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
   2156   // greater_than_5 and less_than_10 are monomorphic matchers.
   2157   Matcher<int> greater_than_5 = Gt(5);
   2158   Matcher<int> less_than_10 = Lt(10);
   2159 
   2160   Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
   2161   Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
   2162   Matcher<int&> m3 = AnyOf(greater_than_5, m2);
   2163 
   2164   // Tests that EitherOf works when composing itself.
   2165   Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
   2166   Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
   2167 }
   2168 
   2169 TEST(AnyOfTest, ExplainsResult) {
   2170   Matcher<int> m;
   2171 
   2172   // Failed match.  Both matchers need to explain.  The second
   2173   // matcher doesn't give an explanation, so only the first matcher's
   2174   // explanation is printed.
   2175   m = AnyOf(GreaterThan(10), Lt(0));
   2176   EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
   2177 
   2178   // Failed match.  Both matchers need to explain.
   2179   m = AnyOf(GreaterThan(10), GreaterThan(20));
   2180   EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
   2181             Explain(m, 5));
   2182 
   2183   // Failed match.  All matchers need to explain.  The second
   2184   // matcher doesn't given an explanation.
   2185   m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
   2186   EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
   2187             Explain(m, 5));
   2188 
   2189   // Failed match.  All matchers need to explain.
   2190   m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
   2191   EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
   2192             "and which is 25 less than 30",
   2193             Explain(m, 5));
   2194 
   2195   // Successful match.  The first matcher, which succeeded, needs to
   2196   // explain.
   2197   m = AnyOf(GreaterThan(10), GreaterThan(20));
   2198   EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
   2199 
   2200   // Successful match.  The second matcher, which succeeded, needs to
   2201   // explain.  Since it doesn't given an explanation, nothing is
   2202   // printed.
   2203   m = AnyOf(GreaterThan(10), Lt(30));
   2204   EXPECT_EQ("", Explain(m, 0));
   2205 
   2206   // Successful match.  The second matcher, which succeeded, needs to
   2207   // explain.
   2208   m = AnyOf(GreaterThan(30), GreaterThan(20));
   2209   EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
   2210 }
   2211 
   2212 // The following predicate function and predicate functor are for
   2213 // testing the Truly(predicate) matcher.
   2214 
   2215 // Returns non-zero if the input is positive.  Note that the return
   2216 // type of this function is not bool.  It's OK as Truly() accepts any
   2217 // unary function or functor whose return type can be implicitly
   2218 // converted to bool.
   2219 int IsPositive(double x) {
   2220   return x > 0 ? 1 : 0;
   2221 }
   2222 
   2223 // This functor returns true if the input is greater than the given
   2224 // number.
   2225 class IsGreaterThan {
   2226  public:
   2227   explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
   2228 
   2229   bool operator()(int n) const { return n > threshold_; }
   2230 
   2231  private:
   2232   int threshold_;
   2233 };
   2234 
   2235 // For testing Truly().
   2236 const int foo = 0;
   2237 
   2238 // This predicate returns true iff the argument references foo and has
   2239 // a zero value.
   2240 bool ReferencesFooAndIsZero(const int& n) {
   2241   return (&n == &foo) && (n == 0);
   2242 }
   2243 
   2244 // Tests that Truly(predicate) matches what satisfies the given
   2245 // predicate.
   2246 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
   2247   Matcher<double> m = Truly(IsPositive);
   2248   EXPECT_TRUE(m.Matches(2.0));
   2249   EXPECT_FALSE(m.Matches(-1.5));
   2250 }
   2251 
   2252 // Tests that Truly(predicate_functor) works too.
   2253 TEST(TrulyTest, CanBeUsedWithFunctor) {
   2254   Matcher<int> m = Truly(IsGreaterThan(5));
   2255   EXPECT_TRUE(m.Matches(6));
   2256   EXPECT_FALSE(m.Matches(4));
   2257 }
   2258 
   2259 // Tests that Truly(predicate) can describe itself properly.
   2260 TEST(TrulyTest, CanDescribeSelf) {
   2261   Matcher<double> m = Truly(IsPositive);
   2262   EXPECT_EQ("satisfies the given predicate",
   2263             Describe(m));
   2264 }
   2265 
   2266 // Tests that Truly(predicate) works when the matcher takes its
   2267 // argument by reference.
   2268 TEST(TrulyTest, WorksForByRefArguments) {
   2269   Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
   2270   EXPECT_TRUE(m.Matches(foo));
   2271   int n = 0;
   2272   EXPECT_FALSE(m.Matches(n));
   2273 }
   2274 
   2275 // Tests that Matches(m) is a predicate satisfied by whatever that
   2276 // matches matcher m.
   2277 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
   2278   EXPECT_TRUE(Matches(Ge(0))(1));
   2279   EXPECT_FALSE(Matches(Eq('a'))('b'));
   2280 }
   2281 
   2282 // Tests that Matches(m) works when the matcher takes its argument by
   2283 // reference.
   2284 TEST(MatchesTest, WorksOnByRefArguments) {
   2285   int m = 0, n = 0;
   2286   EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
   2287   EXPECT_FALSE(Matches(Ref(m))(n));
   2288 }
   2289 
   2290 // Tests that a Matcher on non-reference type can be used in
   2291 // Matches().
   2292 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
   2293   Matcher<int> eq5 = Eq(5);
   2294   EXPECT_TRUE(Matches(eq5)(5));
   2295   EXPECT_FALSE(Matches(eq5)(2));
   2296 }
   2297 
   2298 // Tests Value(value, matcher).  Since Value() is a simple wrapper for
   2299 // Matches(), which has been tested already, we don't spend a lot of
   2300 // effort on testing Value().
   2301 TEST(ValueTest, WorksWithPolymorphicMatcher) {
   2302   EXPECT_TRUE(Value("hi", StartsWith("h")));
   2303   EXPECT_FALSE(Value(5, Gt(10)));
   2304 }
   2305 
   2306 TEST(ValueTest, WorksWithMonomorphicMatcher) {
   2307   const Matcher<int> is_zero = Eq(0);
   2308   EXPECT_TRUE(Value(0, is_zero));
   2309   EXPECT_FALSE(Value('a', is_zero));
   2310 
   2311   int n = 0;
   2312   const Matcher<const int&> ref_n = Ref(n);
   2313   EXPECT_TRUE(Value(n, ref_n));
   2314   EXPECT_FALSE(Value(1, ref_n));
   2315 }
   2316 
   2317 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
   2318   StringMatchResultListener listener1;
   2319   EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
   2320   EXPECT_EQ("% 2 == 0", listener1.str());
   2321 
   2322   StringMatchResultListener listener2;
   2323   EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
   2324   EXPECT_EQ("", listener2.str());
   2325 }
   2326 
   2327 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
   2328   const Matcher<int> is_even = PolymorphicIsEven();
   2329   StringMatchResultListener listener1;
   2330   EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
   2331   EXPECT_EQ("% 2 == 0", listener1.str());
   2332 
   2333   const Matcher<const double&> is_zero = Eq(0);
   2334   StringMatchResultListener listener2;
   2335   EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
   2336   EXPECT_EQ("", listener2.str());
   2337 }
   2338 
   2339 MATCHER_P(Really, inner_matcher, "") {
   2340   return ExplainMatchResult(inner_matcher, arg, result_listener);
   2341 }
   2342 
   2343 TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
   2344   EXPECT_THAT(0, Really(Eq(0)));
   2345 }
   2346 
   2347 TEST(AllArgsTest, WorksForTuple) {
   2348   EXPECT_THAT(make_tuple(1, 2L), AllArgs(Lt()));
   2349   EXPECT_THAT(make_tuple(2L, 1), Not(AllArgs(Lt())));
   2350 }
   2351 
   2352 TEST(AllArgsTest, WorksForNonTuple) {
   2353   EXPECT_THAT(42, AllArgs(Gt(0)));
   2354   EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
   2355 }
   2356 
   2357 class AllArgsHelper {
   2358  public:
   2359   AllArgsHelper() {}
   2360 
   2361   MOCK_METHOD2(Helper, int(char x, int y));
   2362 
   2363  private:
   2364   GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
   2365 };
   2366 
   2367 TEST(AllArgsTest, WorksInWithClause) {
   2368   AllArgsHelper helper;
   2369   ON_CALL(helper, Helper(_, _))
   2370       .With(AllArgs(Lt()))
   2371       .WillByDefault(Return(1));
   2372   EXPECT_CALL(helper, Helper(_, _));
   2373   EXPECT_CALL(helper, Helper(_, _))
   2374       .With(AllArgs(Gt()))
   2375       .WillOnce(Return(2));
   2376 
   2377   EXPECT_EQ(1, helper.Helper('\1', 2));
   2378   EXPECT_EQ(2, helper.Helper('a', 1));
   2379 }
   2380 
   2381 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
   2382 // matches the matcher.
   2383 TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
   2384   ASSERT_THAT(5, Ge(2)) << "This should succeed.";
   2385   ASSERT_THAT("Foo", EndsWith("oo"));
   2386   EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
   2387   EXPECT_THAT("Hello", StartsWith("Hell"));
   2388 }
   2389 
   2390 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
   2391 // doesn't match the matcher.
   2392 TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
   2393   // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
   2394   // which cannot reference auto variables.
   2395   static unsigned short n;  // NOLINT
   2396   n = 5;
   2397 
   2398   // VC++ prior to version 8.0 SP1 has a bug where it will not see any
   2399   // functions declared in the namespace scope from within nested classes.
   2400   // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all
   2401   // namespace-level functions invoked inside them need to be explicitly
   2402   // resolved.
   2403   EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)),
   2404                        "Value of: n\n"
   2405                        "Expected: is > 10\n"
   2406                        "  Actual: 5" + OfType("unsigned short"));
   2407   n = 0;
   2408   EXPECT_NONFATAL_FAILURE(
   2409       EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))),
   2410       "Value of: n\n"
   2411       "Expected: (is <= 7) and (is >= 5)\n"
   2412       "  Actual: 0" + OfType("unsigned short"));
   2413 }
   2414 
   2415 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
   2416 // has a reference type.
   2417 TEST(MatcherAssertionTest, WorksForByRefArguments) {
   2418   // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
   2419   // reference auto variables.
   2420   static int n;
   2421   n = 0;
   2422   EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
   2423   EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
   2424                        "Value of: n\n"
   2425                        "Expected: does not reference the variable @");
   2426   // Tests the "Actual" part.
   2427   EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))),
   2428                        "Actual: 0" + OfType("int") + ", which is located @");
   2429 }
   2430 
   2431 #if !GTEST_OS_SYMBIAN
   2432 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
   2433 // monomorphic.
   2434 
   2435 // ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's
   2436 // Symbian compiler: it tries to compile
   2437 // template<T, U> class MatcherCastImpl { ...
   2438 //   virtual bool MatchAndExplain(T x, ...) const {
   2439 //     return source_matcher_.MatchAndExplain(static_cast<U>(x), ...);
   2440 // with U == string and T == const char*
   2441 // With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... )
   2442 // the compiler silently crashes with no output.
   2443 // If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x)
   2444 // the code compiles but the converted string is bogus.
   2445 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
   2446   Matcher<const char*> starts_with_he = StartsWith("he");
   2447   ASSERT_THAT("hello", starts_with_he);
   2448 
   2449   Matcher<const string&> ends_with_ok = EndsWith("ok");
   2450   ASSERT_THAT("book", ends_with_ok);
   2451   const string bad = "bad";
   2452   EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
   2453                           "Value of: bad\n"
   2454                           "Expected: ends with \"ok\"\n"
   2455                           "  Actual: \"bad\"");
   2456   Matcher<int> is_greater_than_5 = Gt(5);
   2457   EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
   2458                           "Value of: 5\n"
   2459                           "Expected: is > 5\n"
   2460                           "  Actual: 5" + OfType("int"));
   2461 }
   2462 #endif  // !GTEST_OS_SYMBIAN
   2463 
   2464 // Tests floating-point matchers.
   2465 template <typename RawType>
   2466 class FloatingPointTest : public testing::Test {
   2467  protected:
   2468   typedef typename testing::internal::FloatingPoint<RawType> Floating;
   2469   typedef typename Floating::Bits Bits;
   2470 
   2471   virtual void SetUp() {
   2472     const size_t max_ulps = Floating::kMaxUlps;
   2473 
   2474     // The bits that represent 0.0.
   2475     const Bits zero_bits = Floating(0).bits();
   2476 
   2477     // Makes some numbers close to 0.0.
   2478     close_to_positive_zero_ = Floating::ReinterpretBits(zero_bits + max_ulps/2);
   2479     close_to_negative_zero_ = -Floating::ReinterpretBits(
   2480         zero_bits + max_ulps - max_ulps/2);
   2481     further_from_negative_zero_ = -Floating::ReinterpretBits(
   2482         zero_bits + max_ulps + 1 - max_ulps/2);
   2483 
   2484     // The bits that represent 1.0.
   2485     const Bits one_bits = Floating(1).bits();
   2486 
   2487     // Makes some numbers close to 1.0.
   2488     close_to_one_ = Floating::ReinterpretBits(one_bits + max_ulps);
   2489     further_from_one_ = Floating::ReinterpretBits(one_bits + max_ulps + 1);
   2490 
   2491     // +infinity.
   2492     infinity_ = Floating::Infinity();
   2493 
   2494     // The bits that represent +infinity.
   2495     const Bits infinity_bits = Floating(infinity_).bits();
   2496 
   2497     // Makes some numbers close to infinity.
   2498     close_to_infinity_ = Floating::ReinterpretBits(infinity_bits - max_ulps);
   2499     further_from_infinity_ = Floating::ReinterpretBits(
   2500         infinity_bits - max_ulps - 1);
   2501 
   2502     // Makes some NAN's.
   2503     nan1_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 1);
   2504     nan2_ = Floating::ReinterpretBits(Floating::kExponentBitMask | 200);
   2505   }
   2506 
   2507   void TestSize() {
   2508     EXPECT_EQ(sizeof(RawType), sizeof(Bits));
   2509   }
   2510 
   2511   // A battery of tests for FloatingEqMatcher::Matches.
   2512   // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
   2513   void TestMatches(
   2514       testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
   2515     Matcher<RawType> m1 = matcher_maker(0.0);
   2516     EXPECT_TRUE(m1.Matches(-0.0));
   2517     EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
   2518     EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
   2519     EXPECT_FALSE(m1.Matches(1.0));
   2520 
   2521     Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
   2522     EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
   2523 
   2524     Matcher<RawType> m3 = matcher_maker(1.0);
   2525     EXPECT_TRUE(m3.Matches(close_to_one_));
   2526     EXPECT_FALSE(m3.Matches(further_from_one_));
   2527 
   2528     // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
   2529     EXPECT_FALSE(m3.Matches(0.0));
   2530 
   2531     Matcher<RawType> m4 = matcher_maker(-infinity_);
   2532     EXPECT_TRUE(m4.Matches(-close_to_infinity_));
   2533 
   2534     Matcher<RawType> m5 = matcher_maker(infinity_);
   2535     EXPECT_TRUE(m5.Matches(close_to_infinity_));
   2536 
   2537     // This is interesting as the representations of infinity_ and nan1_
   2538     // are only 1 DLP apart.
   2539     EXPECT_FALSE(m5.Matches(nan1_));
   2540 
   2541     // matcher_maker can produce a Matcher<const RawType&>, which is needed in
   2542     // some cases.
   2543     Matcher<const RawType&> m6 = matcher_maker(0.0);
   2544     EXPECT_TRUE(m6.Matches(-0.0));
   2545     EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
   2546     EXPECT_FALSE(m6.Matches(1.0));
   2547 
   2548     // matcher_maker can produce a Matcher<RawType&>, which is needed in some
   2549     // cases.
   2550     Matcher<RawType&> m7 = matcher_maker(0.0);
   2551     RawType x = 0.0;
   2552     EXPECT_TRUE(m7.Matches(x));
   2553     x = 0.01f;
   2554     EXPECT_FALSE(m7.Matches(x));
   2555   }
   2556 
   2557   // Pre-calculated numbers to be used by the tests.
   2558 
   2559   static RawType close_to_positive_zero_;
   2560   static RawType close_to_negative_zero_;
   2561   static RawType further_from_negative_zero_;
   2562 
   2563   static RawType close_to_one_;
   2564   static RawType further_from_one_;
   2565 
   2566   static RawType infinity_;
   2567   static RawType close_to_infinity_;
   2568   static RawType further_from_infinity_;
   2569 
   2570   static RawType nan1_;
   2571   static RawType nan2_;
   2572 };
   2573 
   2574 template <typename RawType>
   2575 RawType FloatingPointTest<RawType>::close_to_positive_zero_;
   2576 
   2577 template <typename RawType>
   2578 RawType FloatingPointTest<RawType>::close_to_negative_zero_;
   2579 
   2580 template <typename RawType>
   2581 RawType FloatingPointTest<RawType>::further_from_negative_zero_;
   2582 
   2583 template <typename RawType>
   2584 RawType FloatingPointTest<RawType>::close_to_one_;
   2585 
   2586 template <typename RawType>
   2587 RawType FloatingPointTest<RawType>::further_from_one_;
   2588 
   2589 template <typename RawType>
   2590 RawType FloatingPointTest<RawType>::infinity_;
   2591 
   2592 template <typename RawType>
   2593 RawType FloatingPointTest<RawType>::close_to_infinity_;
   2594 
   2595 template <typename RawType>
   2596 RawType FloatingPointTest<RawType>::further_from_infinity_;
   2597 
   2598 template <typename RawType>
   2599 RawType FloatingPointTest<RawType>::nan1_;
   2600 
   2601 template <typename RawType>
   2602 RawType FloatingPointTest<RawType>::nan2_;
   2603 
   2604 // Instantiate FloatingPointTest for testing floats.
   2605 typedef FloatingPointTest<float> FloatTest;
   2606 
   2607 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
   2608   TestMatches(&FloatEq);
   2609 }
   2610 
   2611 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
   2612   TestMatches(&NanSensitiveFloatEq);
   2613 }
   2614 
   2615 TEST_F(FloatTest, FloatEqCannotMatchNaN) {
   2616   // FloatEq never matches NaN.
   2617   Matcher<float> m = FloatEq(nan1_);
   2618   EXPECT_FALSE(m.Matches(nan1_));
   2619   EXPECT_FALSE(m.Matches(nan2_));
   2620   EXPECT_FALSE(m.Matches(1.0));
   2621 }
   2622 
   2623 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
   2624   // NanSensitiveFloatEq will match NaN.
   2625   Matcher<float> m = NanSensitiveFloatEq(nan1_);
   2626   EXPECT_TRUE(m.Matches(nan1_));
   2627   EXPECT_TRUE(m.Matches(nan2_));
   2628   EXPECT_FALSE(m.Matches(1.0));
   2629 }
   2630 
   2631 TEST_F(FloatTest, FloatEqCanDescribeSelf) {
   2632   Matcher<float> m1 = FloatEq(2.0f);
   2633   EXPECT_EQ("is approximately 2", Describe(m1));
   2634   EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
   2635 
   2636   Matcher<float> m2 = FloatEq(0.5f);
   2637   EXPECT_EQ("is approximately 0.5", Describe(m2));
   2638   EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
   2639 
   2640   Matcher<float> m3 = FloatEq(nan1_);
   2641   EXPECT_EQ("never matches", Describe(m3));
   2642   EXPECT_EQ("is anything", DescribeNegation(m3));
   2643 }
   2644 
   2645 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
   2646   Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
   2647   EXPECT_EQ("is approximately 2", Describe(m1));
   2648   EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
   2649 
   2650   Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
   2651   EXPECT_EQ("is approximately 0.5", Describe(m2));
   2652   EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
   2653 
   2654   Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
   2655   EXPECT_EQ("is NaN", Describe(m3));
   2656   EXPECT_EQ("isn't NaN", DescribeNegation(m3));
   2657 }
   2658 
   2659 // Instantiate FloatingPointTest for testing doubles.
   2660 typedef FloatingPointTest<double> DoubleTest;
   2661 
   2662 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
   2663   TestMatches(&DoubleEq);
   2664 }
   2665 
   2666 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
   2667   TestMatches(&NanSensitiveDoubleEq);
   2668 }
   2669 
   2670 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
   2671   // DoubleEq never matches NaN.
   2672   Matcher<double> m = DoubleEq(nan1_);
   2673   EXPECT_FALSE(m.Matches(nan1_));
   2674   EXPECT_FALSE(m.Matches(nan2_));
   2675   EXPECT_FALSE(m.Matches(1.0));
   2676 }
   2677 
   2678 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
   2679   // NanSensitiveDoubleEq will match NaN.
   2680   Matcher<double> m = NanSensitiveDoubleEq(nan1_);
   2681   EXPECT_TRUE(m.Matches(nan1_));
   2682   EXPECT_TRUE(m.Matches(nan2_));
   2683   EXPECT_FALSE(m.Matches(1.0));
   2684 }
   2685 
   2686 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
   2687   Matcher<double> m1 = DoubleEq(2.0);
   2688   EXPECT_EQ("is approximately 2", Describe(m1));
   2689   EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
   2690 
   2691   Matcher<double> m2 = DoubleEq(0.5);
   2692   EXPECT_EQ("is approximately 0.5", Describe(m2));
   2693   EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
   2694 
   2695   Matcher<double> m3 = DoubleEq(nan1_);
   2696   EXPECT_EQ("never matches", Describe(m3));
   2697   EXPECT_EQ("is anything", DescribeNegation(m3));
   2698 }
   2699 
   2700 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
   2701   Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
   2702   EXPECT_EQ("is approximately 2", Describe(m1));
   2703   EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
   2704 
   2705   Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
   2706   EXPECT_EQ("is approximately 0.5", Describe(m2));
   2707   EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
   2708 
   2709   Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
   2710   EXPECT_EQ("is NaN", Describe(m3));
   2711   EXPECT_EQ("isn't NaN", DescribeNegation(m3));
   2712 }
   2713 
   2714 TEST(PointeeTest, RawPointer) {
   2715   const Matcher<int*> m = Pointee(Ge(0));
   2716 
   2717   int n = 1;
   2718   EXPECT_TRUE(m.Matches(&n));
   2719   n = -1;
   2720   EXPECT_FALSE(m.Matches(&n));
   2721   EXPECT_FALSE(m.Matches(NULL));
   2722 }
   2723 
   2724 TEST(PointeeTest, RawPointerToConst) {
   2725   const Matcher<const double*> m = Pointee(Ge(0));
   2726 
   2727   double x = 1;
   2728   EXPECT_TRUE(m.Matches(&x));
   2729   x = -1;
   2730   EXPECT_FALSE(m.Matches(&x));
   2731   EXPECT_FALSE(m.Matches(NULL));
   2732 }
   2733 
   2734 TEST(PointeeTest, ReferenceToConstRawPointer) {
   2735   const Matcher<int* const &> m = Pointee(Ge(0));
   2736 
   2737   int n = 1;
   2738   EXPECT_TRUE(m.Matches(&n));
   2739   n = -1;
   2740   EXPECT_FALSE(m.Matches(&n));
   2741   EXPECT_FALSE(m.Matches(NULL));
   2742 }
   2743 
   2744 TEST(PointeeTest, ReferenceToNonConstRawPointer) {
   2745   const Matcher<double* &> m = Pointee(Ge(0));
   2746 
   2747   double x = 1.0;
   2748   double* p = &x;
   2749   EXPECT_TRUE(m.Matches(p));
   2750   x = -1;
   2751   EXPECT_FALSE(m.Matches(p));
   2752   p = NULL;
   2753   EXPECT_FALSE(m.Matches(p));
   2754 }
   2755 
   2756 TEST(PointeeTest, NeverMatchesNull) {
   2757   const Matcher<const char*> m = Pointee(_);
   2758   EXPECT_FALSE(m.Matches(NULL));
   2759 }
   2760 
   2761 // Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
   2762 TEST(PointeeTest, MatchesAgainstAValue) {
   2763   const Matcher<int*> m = Pointee(5);
   2764 
   2765   int n = 5;
   2766   EXPECT_TRUE(m.Matches(&n));
   2767   n = -1;
   2768   EXPECT_FALSE(m.Matches(&n));
   2769   EXPECT_FALSE(m.Matches(NULL));
   2770 }
   2771 
   2772 TEST(PointeeTest, CanDescribeSelf) {
   2773   const Matcher<int*> m = Pointee(Gt(3));
   2774   EXPECT_EQ("points to a value that is > 3", Describe(m));
   2775   EXPECT_EQ("does not point to a value that is > 3",
   2776             DescribeNegation(m));
   2777 }
   2778 
   2779 TEST(PointeeTest, CanExplainMatchResult) {
   2780   const Matcher<const string*> m = Pointee(StartsWith("Hi"));
   2781 
   2782   EXPECT_EQ("", Explain(m, static_cast<const string*>(NULL)));
   2783 
   2784   const Matcher<long*> m2 = Pointee(GreaterThan(1));  // NOLINT
   2785   long n = 3;  // NOLINT
   2786   EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
   2787             Explain(m2, &n));
   2788 }
   2789 
   2790 TEST(PointeeTest, AlwaysExplainsPointee) {
   2791   const Matcher<int*> m = Pointee(0);
   2792   int n = 42;
   2793   EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
   2794 }
   2795 
   2796 // An uncopyable class.
   2797 class Uncopyable {
   2798  public:
   2799   explicit Uncopyable(int a_value) : value_(a_value) {}
   2800 
   2801   int value() const { return value_; }
   2802  private:
   2803   const int value_;
   2804   GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
   2805 };
   2806 
   2807 // Returns true iff x.value() is positive.
   2808 bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
   2809 
   2810 // A user-defined struct for testing Field().
   2811 struct AStruct {
   2812   AStruct() : x(0), y(1.0), z(5), p(NULL) {}
   2813   AStruct(const AStruct& rhs)
   2814       : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
   2815 
   2816   int x;           // A non-const field.
   2817   const double y;  // A const field.
   2818   Uncopyable z;    // An uncopyable field.
   2819   const char* p;   // A pointer field.
   2820 
   2821  private:
   2822   GTEST_DISALLOW_ASSIGN_(AStruct);
   2823 };
   2824 
   2825 // A derived struct for testing Field().
   2826 struct DerivedStruct : public AStruct {
   2827   char ch;
   2828 
   2829  private:
   2830   GTEST_DISALLOW_ASSIGN_(DerivedStruct);
   2831 };
   2832 
   2833 // Tests that Field(&Foo::field, ...) works when field is non-const.
   2834 TEST(FieldTest, WorksForNonConstField) {
   2835   Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
   2836 
   2837   AStruct a;
   2838   EXPECT_TRUE(m.Matches(a));
   2839   a.x = -1;
   2840   EXPECT_FALSE(m.Matches(a));
   2841 }
   2842 
   2843 // Tests that Field(&Foo::field, ...) works when field is const.
   2844 TEST(FieldTest, WorksForConstField) {
   2845   AStruct a;
   2846 
   2847   Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
   2848   EXPECT_TRUE(m.Matches(a));
   2849   m = Field(&AStruct::y, Le(0.0));
   2850   EXPECT_FALSE(m.Matches(a));
   2851 }
   2852 
   2853 // Tests that Field(&Foo::field, ...) works when field is not copyable.
   2854 TEST(FieldTest, WorksForUncopyableField) {
   2855   AStruct a;
   2856 
   2857   Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
   2858   EXPECT_TRUE(m.Matches(a));
   2859   m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
   2860   EXPECT_FALSE(m.Matches(a));
   2861 }
   2862 
   2863 // Tests that Field(&Foo::field, ...) works when field is a pointer.
   2864 TEST(FieldTest, WorksForPointerField) {
   2865   // Matching against NULL.
   2866   Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(NULL));
   2867   AStruct a;
   2868   EXPECT_TRUE(m.Matches(a));
   2869   a.p = "hi";
   2870   EXPECT_FALSE(m.Matches(a));
   2871 
   2872   // Matching a pointer that is not NULL.
   2873   m = Field(&AStruct::p, StartsWith("hi"));
   2874   a.p = "hill";
   2875   EXPECT_TRUE(m.Matches(a));
   2876   a.p = "hole";
   2877   EXPECT_FALSE(m.Matches(a));
   2878 }
   2879 
   2880 // Tests that Field() works when the object is passed by reference.
   2881 TEST(FieldTest, WorksForByRefArgument) {
   2882   Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
   2883 
   2884   AStruct a;
   2885   EXPECT_TRUE(m.Matches(a));
   2886   a.x = -1;
   2887   EXPECT_FALSE(m.Matches(a));
   2888 }
   2889 
   2890 // Tests that Field(&Foo::field, ...) works when the argument's type
   2891 // is a sub-type of Foo.
   2892 TEST(FieldTest, WorksForArgumentOfSubType) {
   2893   // Note that the matcher expects DerivedStruct but we say AStruct
   2894   // inside Field().
   2895   Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
   2896 
   2897   DerivedStruct d;
   2898   EXPECT_TRUE(m.Matches(d));
   2899   d.x = -1;
   2900   EXPECT_FALSE(m.Matches(d));
   2901 }
   2902 
   2903 // Tests that Field(&Foo::field, m) works when field's type and m's
   2904 // argument type are compatible but not the same.
   2905 TEST(FieldTest, WorksForCompatibleMatcherType) {
   2906   // The field is an int, but the inner matcher expects a signed char.
   2907   Matcher<const AStruct&> m = Field(&AStruct::x,
   2908                                     Matcher<signed char>(Ge(0)));
   2909 
   2910   AStruct a;
   2911   EXPECT_TRUE(m.Matches(a));
   2912   a.x = -1;
   2913   EXPECT_FALSE(m.Matches(a));
   2914 }
   2915 
   2916 // Tests that Field() can describe itself.
   2917 TEST(FieldTest, CanDescribeSelf) {
   2918   Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
   2919 
   2920   EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
   2921   EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
   2922 }
   2923 
   2924 // Tests that Field() can explain the match result.
   2925 TEST(FieldTest, CanExplainMatchResult) {
   2926   Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
   2927 
   2928   AStruct a;
   2929   a.x = 1;
   2930   EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
   2931 
   2932   m = Field(&AStruct::x, GreaterThan(0));
   2933   EXPECT_EQ(
   2934       "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
   2935       Explain(m, a));
   2936 }
   2937 
   2938 // Tests that Field() works when the argument is a pointer to const.
   2939 TEST(FieldForPointerTest, WorksForPointerToConst) {
   2940   Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
   2941 
   2942   AStruct a;
   2943   EXPECT_TRUE(m.Matches(&a));
   2944   a.x = -1;
   2945   EXPECT_FALSE(m.Matches(&a));
   2946 }
   2947 
   2948 // Tests that Field() works when the argument is a pointer to non-const.
   2949 TEST(FieldForPointerTest, WorksForPointerToNonConst) {
   2950   Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
   2951 
   2952   AStruct a;
   2953   EXPECT_TRUE(m.Matches(&a));
   2954   a.x = -1;
   2955   EXPECT_FALSE(m.Matches(&a));
   2956 }
   2957 
   2958 // Tests that Field() works when the argument is a reference to a const pointer.
   2959 TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
   2960   Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
   2961 
   2962   AStruct a;
   2963   EXPECT_TRUE(m.Matches(&a));
   2964   a.x = -1;
   2965   EXPECT_FALSE(m.Matches(&a));
   2966 }
   2967 
   2968 // Tests that Field() does not match the NULL pointer.
   2969 TEST(FieldForPointerTest, DoesNotMatchNull) {
   2970   Matcher<const AStruct*> m = Field(&AStruct::x, _);
   2971   EXPECT_FALSE(m.Matches(NULL));
   2972 }
   2973 
   2974 // Tests that Field(&Foo::field, ...) works when the argument's type
   2975 // is a sub-type of const Foo*.
   2976 TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
   2977   // Note that the matcher expects DerivedStruct but we say AStruct
   2978   // inside Field().
   2979   Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
   2980 
   2981   DerivedStruct d;
   2982   EXPECT_TRUE(m.Matches(&d));
   2983   d.x = -1;
   2984   EXPECT_FALSE(m.Matches(&d));
   2985 }
   2986 
   2987 // Tests that Field() can describe itself when used to match a pointer.
   2988 TEST(FieldForPointerTest, CanDescribeSelf) {
   2989   Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
   2990 
   2991   EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
   2992   EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
   2993 }
   2994 
   2995 // Tests that Field() can explain the result of matching a pointer.
   2996 TEST(FieldForPointerTest, CanExplainMatchResult) {
   2997   Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
   2998 
   2999   AStruct a;
   3000   a.x = 1;
   3001   EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(NULL)));
   3002   EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
   3003             Explain(m, &a));
   3004 
   3005   m = Field(&AStruct::x, GreaterThan(0));
   3006   EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
   3007             ", which is 1 more than 0", Explain(m, &a));
   3008 }
   3009 
   3010 // A user-defined class for testing Property().
   3011 class AClass {
   3012  public:
   3013   AClass() : n_(0) {}
   3014 
   3015   // A getter that returns a non-reference.
   3016   int n() const { return n_; }
   3017 
   3018   void set_n(int new_n) { n_ = new_n; }
   3019 
   3020   // A getter that returns a reference to const.
   3021   const string& s() const { return s_; }
   3022 
   3023   void set_s(const string& new_s) { s_ = new_s; }
   3024 
   3025   // A getter that returns a reference to non-const.
   3026   double& x() const { return x_; }
   3027  private:
   3028   int n_;
   3029   string s_;
   3030 
   3031   static double x_;
   3032 };
   3033 
   3034 double AClass::x_ = 0.0;
   3035 
   3036 // A derived class for testing Property().
   3037 class DerivedClass : public AClass {
   3038  private:
   3039   int k_;
   3040 };
   3041 
   3042 // Tests that Property(&Foo::property, ...) works when property()
   3043 // returns a non-reference.
   3044 TEST(PropertyTest, WorksForNonReferenceProperty) {
   3045   Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
   3046 
   3047   AClass a;
   3048   a.set_n(1);
   3049   EXPECT_TRUE(m.Matches(a));
   3050 
   3051   a.set_n(-1);
   3052   EXPECT_FALSE(m.Matches(a));
   3053 }
   3054 
   3055 // Tests that Property(&Foo::property, ...) works when property()
   3056 // returns a reference to const.
   3057 TEST(PropertyTest, WorksForReferenceToConstProperty) {
   3058   Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
   3059 
   3060   AClass a;
   3061   a.set_s("hill");
   3062   EXPECT_TRUE(m.Matches(a));
   3063 
   3064   a.set_s("hole");
   3065   EXPECT_FALSE(m.Matches(a));
   3066 }
   3067 
   3068 // Tests that Property(&Foo::property, ...) works when property()
   3069 // returns a reference to non-const.
   3070 TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
   3071   double x = 0.0;
   3072   AClass a;
   3073 
   3074   Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
   3075   EXPECT_FALSE(m.Matches(a));
   3076 
   3077   m = Property(&AClass::x, Not(Ref(x)));
   3078   EXPECT_TRUE(m.Matches(a));
   3079 }
   3080 
   3081 // Tests that Property(&Foo::property, ...) works when the argument is
   3082 // passed by value.
   3083 TEST(PropertyTest, WorksForByValueArgument) {
   3084   Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
   3085 
   3086   AClass a;
   3087   a.set_s("hill");
   3088   EXPECT_TRUE(m.Matches(a));
   3089 
   3090   a.set_s("hole");
   3091   EXPECT_FALSE(m.Matches(a));
   3092 }
   3093 
   3094 // Tests that Property(&Foo::property, ...) works when the argument's
   3095 // type is a sub-type of Foo.
   3096 TEST(PropertyTest, WorksForArgumentOfSubType) {
   3097   // The matcher expects a DerivedClass, but inside the Property() we
   3098   // say AClass.
   3099   Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
   3100 
   3101   DerivedClass d;
   3102   d.set_n(1);
   3103   EXPECT_TRUE(m.Matches(d));
   3104 
   3105   d.set_n(-1);
   3106   EXPECT_FALSE(m.Matches(d));
   3107 }
   3108 
   3109 // Tests that Property(&Foo::property, m) works when property()'s type
   3110 // and m's argument type are compatible but different.
   3111 TEST(PropertyTest, WorksForCompatibleMatcherType) {
   3112   // n() returns an int but the inner matcher expects a signed char.
   3113   Matcher<const AClass&> m = Property(&AClass::n,
   3114                                       Matcher<signed char>(Ge(0)));
   3115 
   3116   AClass a;
   3117   EXPECT_TRUE(m.Matches(a));
   3118   a.set_n(-1);
   3119   EXPECT_FALSE(m.Matches(a));
   3120 }
   3121 
   3122 // Tests that Property() can describe itself.
   3123 TEST(PropertyTest, CanDescribeSelf) {
   3124   Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
   3125 
   3126   EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
   3127   EXPECT_EQ("is an object whose given property isn't >= 0",
   3128             DescribeNegation(m));
   3129 }
   3130 
   3131 // Tests that Property() can explain the match result.
   3132 TEST(PropertyTest, CanExplainMatchResult) {
   3133   Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
   3134 
   3135   AClass a;
   3136   a.set_n(1);
   3137   EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
   3138 
   3139   m = Property(&AClass::n, GreaterThan(0));
   3140   EXPECT_EQ(
   3141       "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
   3142       Explain(m, a));
   3143 }
   3144 
   3145 // Tests that Property() works when the argument is a pointer to const.
   3146 TEST(PropertyForPointerTest, WorksForPointerToConst) {
   3147   Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
   3148 
   3149   AClass a;
   3150   a.set_n(1);
   3151   EXPECT_TRUE(m.Matches(&a));
   3152 
   3153   a.set_n(-1);
   3154   EXPECT_FALSE(m.Matches(&a));
   3155 }
   3156 
   3157 // Tests that Property() works when the argument is a pointer to non-const.
   3158 TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
   3159   Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
   3160 
   3161   AClass a;
   3162   a.set_s("hill");
   3163   EXPECT_TRUE(m.Matches(&a));
   3164 
   3165   a.set_s("hole");
   3166   EXPECT_FALSE(m.Matches(&a));
   3167 }
   3168 
   3169 // Tests that Property() works when the argument is a reference to a
   3170 // const pointer.
   3171 TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
   3172   Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
   3173 
   3174   AClass a;
   3175   a.set_s("hill");
   3176   EXPECT_TRUE(m.Matches(&a));
   3177 
   3178   a.set_s("hole");
   3179   EXPECT_FALSE(m.Matches(&a));
   3180 }
   3181 
   3182 // Tests that Property() does not match the NULL pointer.
   3183 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
   3184   Matcher<const AClass*> m = Property(&AClass::x, _);
   3185   EXPECT_FALSE(m.Matches(NULL));
   3186 }
   3187 
   3188 // Tests that Property(&Foo::property, ...) works when the argument's
   3189 // type is a sub-type of const Foo*.
   3190 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
   3191   // The matcher expects a DerivedClass, but inside the Property() we
   3192   // say AClass.
   3193   Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
   3194 
   3195   DerivedClass d;
   3196   d.set_n(1);
   3197   EXPECT_TRUE(m.Matches(&d));
   3198 
   3199   d.set_n(-1);
   3200   EXPECT_FALSE(m.Matches(&d));
   3201 }
   3202 
   3203 // Tests that Property() can describe itself when used to match a pointer.
   3204 TEST(PropertyForPointerTest, CanDescribeSelf) {
   3205   Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
   3206 
   3207   EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
   3208   EXPECT_EQ("is an object whose given property isn't >= 0",
   3209             DescribeNegation(m));
   3210 }
   3211 
   3212 // Tests that Property() can explain the result of matching a pointer.
   3213 TEST(PropertyForPointerTest, CanExplainMatchResult) {
   3214   Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
   3215 
   3216   AClass a;
   3217   a.set_n(1);
   3218   EXPECT_EQ("", Explain(m, static_cast<const AClass*>(NULL)));
   3219   EXPECT_EQ(
   3220       "which points to an object whose given property is 1" + OfType("int"),
   3221       Explain(m, &a));
   3222 
   3223   m = Property(&AClass::n, GreaterThan(0));
   3224   EXPECT_EQ("which points to an object whose given property is 1" +
   3225             OfType("int") + ", which is 1 more than 0",
   3226             Explain(m, &a));
   3227 }
   3228 
   3229 // Tests ResultOf.
   3230 
   3231 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
   3232 // function pointer.
   3233 string IntToStringFunction(int input) { return input == 1 ? "foo" : "bar"; }
   3234 
   3235 TEST(ResultOfTest, WorksForFunctionPointers) {
   3236   Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(string("foo")));
   3237 
   3238   EXPECT_TRUE(matcher.Matches(1));
   3239   EXPECT_FALSE(matcher.Matches(2));
   3240 }
   3241 
   3242 // Tests that ResultOf() can describe itself.
   3243 TEST(ResultOfTest, CanDescribeItself) {
   3244   Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
   3245 
   3246   EXPECT_EQ("is mapped by the given callable to a value that "
   3247             "is equal to \"foo\"", Describe(matcher));
   3248   EXPECT_EQ("is mapped by the given callable to a value that "
   3249             "isn't equal to \"foo\"", DescribeNegation(matcher));
   3250 }
   3251 
   3252 // Tests that ResultOf() can explain the match result.
   3253 int IntFunction(int input) { return input == 42 ? 80 : 90; }
   3254 
   3255 TEST(ResultOfTest, CanExplainMatchResult) {
   3256   Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
   3257   EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
   3258             Explain(matcher, 36));
   3259 
   3260   matcher = ResultOf(&IntFunction, GreaterThan(85));
   3261   EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
   3262             ", which is 5 more than 85", Explain(matcher, 36));
   3263 }
   3264 
   3265 // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
   3266 // returns a non-reference.
   3267 TEST(ResultOfTest, WorksForNonReferenceResults) {
   3268   Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
   3269 
   3270   EXPECT_TRUE(matcher.Matches(42));
   3271   EXPECT_FALSE(matcher.Matches(36));
   3272 }
   3273 
   3274 // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
   3275 // returns a reference to non-const.
   3276 double& DoubleFunction(double& input) { return input; }  // NOLINT
   3277 
   3278 Uncopyable& RefUncopyableFunction(Uncopyable& obj) {  // NOLINT
   3279   return obj;
   3280 }
   3281 
   3282 TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
   3283   double x = 3.14;
   3284   double x2 = x;
   3285   Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
   3286 
   3287   EXPECT_TRUE(matcher.Matches(x));
   3288   EXPECT_FALSE(matcher.Matches(x2));
   3289 
   3290   // Test that ResultOf works with uncopyable objects
   3291   Uncopyable obj(0);
   3292   Uncopyable obj2(0);
   3293   Matcher<Uncopyable&> matcher2 =
   3294       ResultOf(&RefUncopyableFunction, Ref(obj));
   3295 
   3296   EXPECT_TRUE(matcher2.Matches(obj));
   3297   EXPECT_FALSE(matcher2.Matches(obj2));
   3298 }
   3299 
   3300 // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
   3301 // returns a reference to const.
   3302 const string& StringFunction(const string& input) { return input; }
   3303 
   3304 TEST(ResultOfTest, WorksForReferenceToConstResults) {
   3305   string s = "foo";
   3306   string s2 = s;
   3307   Matcher<const string&> matcher = ResultOf(&StringFunction, Ref(s));
   3308 
   3309   EXPECT_TRUE(matcher.Matches(s));
   3310   EXPECT_FALSE(matcher.Matches(s2));
   3311 }
   3312 
   3313 // Tests that ResultOf(f, m) works when f(x) and m's
   3314 // argument types are compatible but different.
   3315 TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
   3316   // IntFunction() returns int but the inner matcher expects a signed char.
   3317   Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
   3318 
   3319   EXPECT_TRUE(matcher.Matches(36));
   3320   EXPECT_FALSE(matcher.Matches(42));
   3321 }
   3322 
   3323 // Tests that the program aborts when ResultOf is passed
   3324 // a NULL function pointer.
   3325 TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
   3326   EXPECT_DEATH_IF_SUPPORTED(
   3327       ResultOf(static_cast<string(*)(int dummy)>(NULL), Eq(string("foo"))),
   3328                "NULL function pointer is passed into ResultOf\\(\\)\\.");
   3329 }
   3330 
   3331 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
   3332 // function reference.
   3333 TEST(ResultOfTest, WorksForFunctionReferences) {
   3334   Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
   3335   EXPECT_TRUE(matcher.Matches(1));
   3336   EXPECT_FALSE(matcher.Matches(2));
   3337 }
   3338 
   3339 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
   3340 // function object.
   3341 struct Functor : public ::std::unary_function<int, string> {
   3342   result_type operator()(argument_type input) const {
   3343     return IntToStringFunction(input);
   3344   }
   3345 };
   3346 
   3347 TEST(ResultOfTest, WorksForFunctors) {
   3348   Matcher<int> matcher = ResultOf(Functor(), Eq(string("foo")));
   3349 
   3350   EXPECT_TRUE(matcher.Matches(1));
   3351   EXPECT_FALSE(matcher.Matches(2));
   3352 }
   3353 
   3354 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
   3355 // functor with more then one operator() defined. ResultOf() must work
   3356 // for each defined operator().
   3357 struct PolymorphicFunctor {
   3358   typedef int result_type;
   3359   int operator()(int n) { return n; }
   3360   int operator()(const char* s) { return static_cast<int>(strlen(s)); }
   3361 };
   3362 
   3363 TEST(ResultOfTest, WorksForPolymorphicFunctors) {
   3364   Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
   3365 
   3366   EXPECT_TRUE(matcher_int.Matches(10));
   3367   EXPECT_FALSE(matcher_int.Matches(2));
   3368 
   3369   Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
   3370 
   3371   EXPECT_TRUE(matcher_string.Matches("long string"));
   3372   EXPECT_FALSE(matcher_string.Matches("shrt"));
   3373 }
   3374 
   3375 const int* ReferencingFunction(const int& n) { return &n; }
   3376 
   3377 struct ReferencingFunctor {
   3378   typedef const int* result_type;
   3379   result_type operator()(const int& n) { return &n; }
   3380 };
   3381 
   3382 TEST(ResultOfTest, WorksForReferencingCallables) {
   3383   const int n = 1;
   3384   const int n2 = 1;
   3385   Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
   3386   EXPECT_TRUE(matcher2.Matches(n));
   3387   EXPECT_FALSE(matcher2.Matches(n2));
   3388 
   3389   Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
   3390   EXPECT_TRUE(matcher3.Matches(n));
   3391   EXPECT_FALSE(matcher3.Matches(n2));
   3392 }
   3393 
   3394 class DivisibleByImpl {
   3395  public:
   3396   explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
   3397 
   3398   // For testing using ExplainMatchResultTo() with polymorphic matchers.
   3399   template <typename T>
   3400   bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
   3401     *listener << "which is " << (n % divider_) << " modulo "
   3402               << divider_;
   3403     return (n % divider_) == 0;
   3404   }
   3405 
   3406   void DescribeTo(ostream* os) const {
   3407     *os << "is divisible by " << divider_;
   3408   }
   3409 
   3410   void DescribeNegationTo(ostream* os) const {
   3411     *os << "is not divisible by " << divider_;
   3412   }
   3413 
   3414   void set_divider(int a_divider) { divider_ = a_divider; }
   3415   int divider() const { return divider_; }
   3416 
   3417  private:
   3418   int divider_;
   3419 };
   3420 
   3421 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
   3422   return MakePolymorphicMatcher(DivisibleByImpl(n));
   3423 }
   3424 
   3425 // Tests that when AllOf() fails, only the first failing matcher is
   3426 // asked to explain why.
   3427 TEST(ExplainMatchResultTest, AllOf_False_False) {
   3428   const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
   3429   EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
   3430 }
   3431 
   3432 // Tests that when AllOf() fails, only the first failing matcher is
   3433 // asked to explain why.
   3434 TEST(ExplainMatchResultTest, AllOf_False_True) {
   3435   const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
   3436   EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
   3437 }
   3438 
   3439 // Tests that when AllOf() fails, only the first failing matcher is
   3440 // asked to explain why.
   3441 TEST(ExplainMatchResultTest, AllOf_True_False) {
   3442   const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
   3443   EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
   3444 }
   3445 
   3446 // Tests that when AllOf() succeeds, all matchers are asked to explain
   3447 // why.
   3448 TEST(ExplainMatchResultTest, AllOf_True_True) {
   3449   const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
   3450   EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
   3451 }
   3452 
   3453 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
   3454   const Matcher<int> m = AllOf(Ge(2), Le(3));
   3455   EXPECT_EQ("", Explain(m, 2));
   3456 }
   3457 
   3458 TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
   3459   const Matcher<int> m = GreaterThan(5);
   3460   EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
   3461 }
   3462 
   3463 // The following two tests verify that values without a public copy
   3464 // ctor can be used as arguments to matchers like Eq(), Ge(), and etc
   3465 // with the help of ByRef().
   3466 
   3467 class NotCopyable {
   3468  public:
   3469   explicit NotCopyable(int a_value) : value_(a_value) {}
   3470 
   3471   int value() const { return value_; }
   3472 
   3473   bool operator==(const NotCopyable& rhs) const {
   3474     return value() == rhs.value();
   3475   }
   3476 
   3477   bool operator>=(const NotCopyable& rhs) const {
   3478     return value() >= rhs.value();
   3479   }
   3480  private:
   3481   int value_;
   3482 
   3483   GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
   3484 };
   3485 
   3486 TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
   3487   const NotCopyable const_value1(1);
   3488   const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
   3489 
   3490   const NotCopyable n1(1), n2(2);
   3491   EXPECT_TRUE(m.Matches(n1));
   3492   EXPECT_FALSE(m.Matches(n2));
   3493 }
   3494 
   3495 TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
   3496   NotCopyable value2(2);
   3497   const Matcher<NotCopyable&> m = Ge(ByRef(value2));
   3498 
   3499   NotCopyable n1(1), n2(2);
   3500   EXPECT_FALSE(m.Matches(n1));
   3501   EXPECT_TRUE(m.Matches(n2));
   3502 }
   3503 
   3504 #if GTEST_HAS_TYPED_TEST
   3505 // Tests ContainerEq with different container types, and
   3506 // different element types.
   3507 
   3508 template <typename T>
   3509 class ContainerEqTest : public testing::Test {};
   3510 
   3511 typedef testing::Types<
   3512     set<int>,
   3513     vector<size_t>,
   3514     multiset<size_t>,
   3515     list<int> >
   3516     ContainerEqTestTypes;
   3517 
   3518 TYPED_TEST_CASE(ContainerEqTest, ContainerEqTestTypes);
   3519 
   3520 // Tests that the filled container is equal to itself.
   3521 TYPED_TEST(ContainerEqTest, EqualsSelf) {
   3522   static const int vals[] = {1, 1, 2, 3, 5, 8};
   3523   TypeParam my_set(vals, vals + 6);
   3524   const Matcher<TypeParam> m = ContainerEq(my_set);
   3525   EXPECT_TRUE(m.Matches(my_set));
   3526   EXPECT_EQ("", Explain(m, my_set));
   3527 }
   3528 
   3529 // Tests that missing values are reported.
   3530 TYPED_TEST(ContainerEqTest, ValueMissing) {
   3531   static const int vals[] = {1, 1, 2, 3, 5, 8};
   3532   static const int test_vals[] = {2, 1, 8, 5};
   3533   TypeParam my_set(vals, vals + 6);
   3534   TypeParam test_set(test_vals, test_vals + 4);
   3535   const Matcher<TypeParam> m = ContainerEq(my_set);
   3536   EXPECT_FALSE(m.Matches(test_set));
   3537   EXPECT_EQ("which doesn't have these expected elements: 3",
   3538             Explain(m, test_set));
   3539 }
   3540 
   3541 // Tests that added values are reported.
   3542 TYPED_TEST(ContainerEqTest, ValueAdded) {
   3543   static const int vals[] = {1, 1, 2, 3, 5, 8};
   3544   static const int test_vals[] = {1, 2, 3, 5, 8, 46};
   3545   TypeParam my_set(vals, vals + 6);
   3546   TypeParam test_set(test_vals, test_vals + 6);
   3547   const Matcher<const TypeParam&> m = ContainerEq(my_set);
   3548   EXPECT_FALSE(m.Matches(test_set));
   3549   EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
   3550 }
   3551 
   3552 // Tests that added and missing values are reported together.
   3553 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
   3554   static const int vals[] = {1, 1, 2, 3, 5, 8};
   3555   static const int test_vals[] = {1, 2, 3, 8, 46};
   3556   TypeParam my_set(vals, vals + 6);
   3557   TypeParam test_set(test_vals, test_vals + 5);
   3558   const Matcher<TypeParam> m = ContainerEq(my_set);
   3559   EXPECT_FALSE(m.Matches(test_set));
   3560   EXPECT_EQ("which has these unexpected elements: 46,\n"
   3561             "and doesn't have these expected elements: 5",
   3562             Explain(m, test_set));
   3563 }
   3564 
   3565 // Tests duplicated value -- expect no explanation.
   3566 TYPED_TEST(ContainerEqTest, DuplicateDifference) {
   3567   static const int vals[] = {1, 1, 2, 3, 5, 8};
   3568   static const int test_vals[] = {1, 2, 3, 5, 8};
   3569   TypeParam my_set(vals, vals + 6);
   3570   TypeParam test_set(test_vals, test_vals + 5);
   3571   const Matcher<const TypeParam&> m = ContainerEq(my_set);
   3572   // Depending on the container, match may be true or false
   3573   // But in any case there should be no explanation.
   3574   EXPECT_EQ("", Explain(m, test_set));
   3575 }
   3576 #endif  // GTEST_HAS_TYPED_TEST
   3577 
   3578 // Tests that mutliple missing values are reported.
   3579 // Using just vector here, so order is predicatble.
   3580 TEST(ContainerEqExtraTest, MultipleValuesMissing) {
   3581   static const int vals[] = {1, 1, 2, 3, 5, 8};
   3582   static const int test_vals[] = {2, 1, 5};
   3583   vector<int> my_set(vals, vals + 6);
   3584   vector<int> test_set(test_vals, test_vals + 3);
   3585   const Matcher<vector<int> > m = ContainerEq(my_set);
   3586   EXPECT_FALSE(m.Matches(test_set));
   3587   EXPECT_EQ("which doesn't have these expected elements: 3, 8",
   3588             Explain(m, test_set));
   3589 }
   3590 
   3591 // Tests that added values are reported.
   3592 // Using just vector here, so order is predicatble.
   3593 TEST(ContainerEqExtraTest, MultipleValuesAdded) {
   3594   static const int vals[] = {1, 1, 2, 3, 5, 8};
   3595   static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
   3596   list<size_t> my_set(vals, vals + 6);
   3597   list<size_t> test_set(test_vals, test_vals + 7);
   3598   const Matcher<const list<size_t>&> m = ContainerEq(my_set);
   3599   EXPECT_FALSE(m.Matches(test_set));
   3600   EXPECT_EQ("which has these unexpected elements: 92, 46",
   3601             Explain(m, test_set));
   3602 }
   3603 
   3604 // Tests that added and missing values are reported together.
   3605 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
   3606   static const int vals[] = {1, 1, 2, 3, 5, 8};
   3607   static const int test_vals[] = {1, 2, 3, 92, 46};
   3608   list<size_t> my_set(vals, vals + 6);
   3609   list<size_t> test_set(test_vals, test_vals + 5);
   3610   const Matcher<const list<size_t> > m = ContainerEq(my_set);
   3611   EXPECT_FALSE(m.Matches(test_set));
   3612   EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
   3613             "and doesn't have these expected elements: 5, 8",
   3614             Explain(m, test_set));
   3615 }
   3616 
   3617 // Tests to see that duplicate elements are detected,
   3618 // but (as above) not reported in the explanation.
   3619 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
   3620   static const int vals[] = {1, 1, 2, 3, 5, 8};
   3621   static const int test_vals[] = {1, 2, 3, 5, 8};
   3622   vector<int> my_set(vals, vals + 6);
   3623   vector<int> test_set(test_vals, test_vals + 5);
   3624   const Matcher<vector<int> > m = ContainerEq(my_set);
   3625   EXPECT_TRUE(m.Matches(my_set));
   3626   EXPECT_FALSE(m.Matches(test_set));
   3627   // There is nothing to report when both sets contain all the same values.
   3628   EXPECT_EQ("", Explain(m, test_set));
   3629 }
   3630 
   3631 // Tests that ContainerEq works for non-trivial associative containers,
   3632 // like maps.
   3633 TEST(ContainerEqExtraTest, WorksForMaps) {
   3634   map<int, std::string> my_map;
   3635   my_map[0] = "a";
   3636   my_map[1] = "b";
   3637 
   3638   map<int, std::string> test_map;
   3639   test_map[0] = "aa";
   3640   test_map[1] = "b";
   3641 
   3642   const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
   3643   EXPECT_TRUE(m.Matches(my_map));
   3644   EXPECT_FALSE(m.Matches(test_map));
   3645 
   3646   EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
   3647             "and doesn't have these expected elements: (0, \"a\")",
   3648             Explain(m, test_map));
   3649 }
   3650 
   3651 TEST(ContainerEqExtraTest, WorksForNativeArray) {
   3652   int a1[] = { 1, 2, 3 };
   3653   int a2[] = { 1, 2, 3 };
   3654   int b[] = { 1, 2, 4 };
   3655 
   3656   EXPECT_THAT(a1, ContainerEq(a2));
   3657   EXPECT_THAT(a1, Not(ContainerEq(b)));
   3658 }
   3659 
   3660 TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
   3661   const char a1[][3] = { "hi", "lo" };
   3662   const char a2[][3] = { "hi", "lo" };
   3663   const char b[][3] = { "lo", "hi" };
   3664 
   3665   // Tests using ContainerEq() in the first dimension.
   3666   EXPECT_THAT(a1, ContainerEq(a2));
   3667   EXPECT_THAT(a1, Not(ContainerEq(b)));
   3668 
   3669   // Tests using ContainerEq() in the second dimension.
   3670   EXPECT_THAT(a1, ElementsAre(ContainerEq(a2[0]), ContainerEq(a2[1])));
   3671   EXPECT_THAT(a1, ElementsAre(Not(ContainerEq(b[0])), ContainerEq(a2[1])));
   3672 }
   3673 
   3674 TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
   3675   const int a1[] = { 1, 2, 3 };
   3676   const int a2[] = { 1, 2, 3 };
   3677   const int b[] = { 1, 2, 3, 4 };
   3678 
   3679   const int* const p1 = a1;
   3680   EXPECT_THAT(make_tuple(p1, 3), ContainerEq(a2));
   3681   EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(b)));
   3682 
   3683   const int c[] = { 1, 3, 2 };
   3684   EXPECT_THAT(make_tuple(p1, 3), Not(ContainerEq(c)));
   3685 }
   3686 
   3687 TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
   3688   std::string a1[][3] = {
   3689     { "hi", "hello", "ciao" },
   3690     { "bye", "see you", "ciao" }
   3691   };
   3692 
   3693   std::string a2[][3] = {
   3694     { "hi", "hello", "ciao" },
   3695     { "bye", "see you", "ciao" }
   3696   };
   3697 
   3698   const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
   3699   EXPECT_THAT(a1, m);
   3700 
   3701   a2[0][0] = "ha";
   3702   EXPECT_THAT(a1, m);
   3703 }
   3704 
   3705 // Tests IsReadableTypeName().
   3706 
   3707 TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
   3708   EXPECT_TRUE(IsReadableTypeName("int"));
   3709   EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
   3710   EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
   3711   EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
   3712 }
   3713 
   3714 TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
   3715   EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
   3716   EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
   3717   EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
   3718 }
   3719 
   3720 TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
   3721   EXPECT_FALSE(
   3722       IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
   3723   EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
   3724 }
   3725 
   3726 TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
   3727   EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
   3728 }
   3729 
   3730 // Tests JoinAsTuple().
   3731 
   3732 TEST(JoinAsTupleTest, JoinsEmptyTuple) {
   3733   EXPECT_EQ("", JoinAsTuple(Strings()));
   3734 }
   3735 
   3736 TEST(JoinAsTupleTest, JoinsOneTuple) {
   3737   const char* fields[] = { "1" };
   3738   EXPECT_EQ("1", JoinAsTuple(Strings(fields, fields + 1)));
   3739 }
   3740 
   3741 TEST(JoinAsTupleTest, JoinsTwoTuple) {
   3742   const char* fields[] = { "1", "a" };
   3743   EXPECT_EQ("(1, a)", JoinAsTuple(Strings(fields, fields + 2)));
   3744 }
   3745 
   3746 TEST(JoinAsTupleTest, JoinsTenTuple) {
   3747   const char* fields[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
   3748   EXPECT_EQ("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)",
   3749             JoinAsTuple(Strings(fields, fields + 10)));
   3750 }
   3751 
   3752 // Tests FormatMatcherDescription().
   3753 
   3754 TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
   3755   EXPECT_EQ("is even",
   3756             FormatMatcherDescription(false, "IsEven", Strings()));
   3757   EXPECT_EQ("not (is even)",
   3758             FormatMatcherDescription(true, "IsEven", Strings()));
   3759 
   3760   const char* params[] = { "5" };
   3761   EXPECT_EQ("equals 5",
   3762             FormatMatcherDescription(false, "Equals",
   3763                                      Strings(params, params + 1)));
   3764 
   3765   const char* params2[] = { "5", "8" };
   3766   EXPECT_EQ("is in range (5, 8)",
   3767             FormatMatcherDescription(false, "IsInRange",
   3768                                      Strings(params2, params2 + 2)));
   3769 }
   3770 
   3771 // Tests PolymorphicMatcher::mutable_impl().
   3772 TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
   3773   PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
   3774   DivisibleByImpl& impl = m.mutable_impl();
   3775   EXPECT_EQ(42, impl.divider());
   3776 
   3777   impl.set_divider(0);
   3778   EXPECT_EQ(0, m.mutable_impl().divider());
   3779 }
   3780 
   3781 // Tests PolymorphicMatcher::impl().
   3782 TEST(PolymorphicMatcherTest, CanAccessImpl) {
   3783   const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
   3784   const DivisibleByImpl& impl = m.impl();
   3785   EXPECT_EQ(42, impl.divider());
   3786 }
   3787 
   3788 TEST(MatcherTupleTest, ExplainsMatchFailure) {
   3789   stringstream ss1;
   3790   ExplainMatchFailureTupleTo(make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
   3791                              make_tuple('a', 10), &ss1);
   3792   EXPECT_EQ("", ss1.str());  // Successful match.
   3793 
   3794   stringstream ss2;
   3795   ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
   3796                              make_tuple(2, 'b'), &ss2);
   3797   EXPECT_EQ("  Expected arg #0: is > 5\n"
   3798             "           Actual: 2, which is 3 less than 5\n"
   3799             "  Expected arg #1: is equal to 'a' (97, 0x61)\n"
   3800             "           Actual: 'b' (98, 0x62)\n",
   3801             ss2.str());  // Failed match where both arguments need explanation.
   3802 
   3803   stringstream ss3;
   3804   ExplainMatchFailureTupleTo(make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
   3805                              make_tuple(2, 'a'), &ss3);
   3806   EXPECT_EQ("  Expected arg #0: is > 5\n"
   3807             "           Actual: 2, which is 3 less than 5\n",
   3808             ss3.str());  // Failed match where only one argument needs
   3809                          // explanation.
   3810 }
   3811 
   3812 // Tests Each().
   3813 
   3814 TEST(EachTest, ExplainsMatchResultCorrectly) {
   3815   set<int> a;  // empty
   3816 
   3817   Matcher<set<int> > m = Each(2);
   3818   EXPECT_EQ("", Explain(m, a));
   3819 
   3820   Matcher<const int(&)[1]> n = Each(1);  // NOLINT
   3821 
   3822   const int b[1] = { 1 };
   3823   EXPECT_EQ("", Explain(n, b));
   3824 
   3825   n = Each(3);
   3826   EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
   3827 
   3828   a.insert(1);
   3829   a.insert(2);
   3830   a.insert(3);
   3831   m = Each(GreaterThan(0));
   3832   EXPECT_EQ("", Explain(m, a));
   3833 
   3834   m = Each(GreaterThan(10));
   3835   EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
   3836             Explain(m, a));
   3837 }
   3838 
   3839 TEST(EachTest, DescribesItselfCorrectly) {
   3840   Matcher<vector<int> > m = Each(1);
   3841   EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
   3842 
   3843   Matcher<vector<int> > m2 = Not(m);
   3844   EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
   3845 }
   3846 
   3847 TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
   3848   vector<int> some_vector;
   3849   EXPECT_THAT(some_vector, Each(1));
   3850   some_vector.push_back(3);
   3851   EXPECT_THAT(some_vector, Not(Each(1)));
   3852   EXPECT_THAT(some_vector, Each(3));
   3853   some_vector.push_back(1);
   3854   some_vector.push_back(2);
   3855   EXPECT_THAT(some_vector, Not(Each(3)));
   3856   EXPECT_THAT(some_vector, Each(Lt(3.5)));
   3857 
   3858   vector<string> another_vector;
   3859   another_vector.push_back("fee");
   3860   EXPECT_THAT(another_vector, Each(string("fee")));
   3861   another_vector.push_back("fie");
   3862   another_vector.push_back("foe");
   3863   another_vector.push_back("fum");
   3864   EXPECT_THAT(another_vector, Not(Each(string("fee"))));
   3865 }
   3866 
   3867 TEST(EachTest, MatchesMapWhenAllElementsMatch) {
   3868   map<const char*, int> my_map;
   3869   const char* bar = "a string";
   3870   my_map[bar] = 2;
   3871   EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
   3872 
   3873   map<string, int> another_map;
   3874   EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
   3875   another_map["fee"] = 1;
   3876   EXPECT_THAT(another_map, Each(make_pair(string("fee"), 1)));
   3877   another_map["fie"] = 2;
   3878   another_map["foe"] = 3;
   3879   another_map["fum"] = 4;
   3880   EXPECT_THAT(another_map, Not(Each(make_pair(string("fee"), 1))));
   3881   EXPECT_THAT(another_map, Not(Each(make_pair(string("fum"), 1))));
   3882   EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
   3883 }
   3884 
   3885 TEST(EachTest, AcceptsMatcher) {
   3886   const int a[] = { 1, 2, 3 };
   3887   EXPECT_THAT(a, Each(Gt(0)));
   3888   EXPECT_THAT(a, Not(Each(Gt(1))));
   3889 }
   3890 
   3891 TEST(EachTest, WorksForNativeArrayAsTuple) {
   3892   const int a[] = { 1, 2 };
   3893   const int* const pointer = a;
   3894   EXPECT_THAT(make_tuple(pointer, 2), Each(Gt(0)));
   3895   EXPECT_THAT(make_tuple(pointer, 2), Not(Each(Gt(1))));
   3896 }
   3897 
   3898 // For testing Pointwise().
   3899 class IsHalfOfMatcher {
   3900  public:
   3901   template <typename T1, typename T2>
   3902   bool MatchAndExplain(const tuple<T1, T2>& a_pair,
   3903                        MatchResultListener* listener) const {
   3904     if (get<0>(a_pair) == get<1>(a_pair)/2) {
   3905       *listener << "where the second is " << get<1>(a_pair);
   3906       return true;
   3907     } else {
   3908       *listener << "where the second/2 is " << get<1>(a_pair)/2;
   3909       return false;
   3910     }
   3911   }
   3912 
   3913   void DescribeTo(ostream* os) const {
   3914     *os << "are a pair where the first is half of the second";
   3915   }
   3916 
   3917   void DescribeNegationTo(ostream* os) const {
   3918     *os << "are a pair where the first isn't half of the second";
   3919   }
   3920 };
   3921 
   3922 PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
   3923   return MakePolymorphicMatcher(IsHalfOfMatcher());
   3924 }
   3925 
   3926 TEST(PointwiseTest, DescribesSelf) {
   3927   vector<int> rhs;
   3928   rhs.push_back(1);
   3929   rhs.push_back(2);
   3930   rhs.push_back(3);
   3931   const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
   3932   EXPECT_EQ("contains 3 values, where each value and its corresponding value "
   3933             "in { 1, 2, 3 } are a pair where the first is half of the second",
   3934             Describe(m));
   3935   EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
   3936             "index i where x and the i-th value of { 1, 2, 3 } are a pair "
   3937             "where the first isn't half of the second",
   3938             DescribeNegation(m));
   3939 }
   3940 
   3941 TEST(PointwiseTest, MakesCopyOfRhs) {
   3942   list<signed char> rhs;
   3943   rhs.push_back(2);
   3944   rhs.push_back(4);
   3945 
   3946   int lhs[] = { 1, 2 };
   3947   const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
   3948   EXPECT_THAT(lhs, m);
   3949 
   3950   // Changing rhs now shouldn't affect m, which made a copy of rhs.
   3951   rhs.push_back(6);
   3952   EXPECT_THAT(lhs, m);
   3953 }
   3954 
   3955 TEST(PointwiseTest, WorksForLhsNativeArray) {
   3956   const int lhs[] = { 1, 2, 3 };
   3957   vector<int> rhs;
   3958   rhs.push_back(2);
   3959   rhs.push_back(4);
   3960   rhs.push_back(6);
   3961   EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
   3962   EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
   3963 }
   3964 
   3965 TEST(PointwiseTest, WorksForRhsNativeArray) {
   3966   const int rhs[] = { 1, 2, 3 };
   3967   vector<int> lhs;
   3968   lhs.push_back(2);
   3969   lhs.push_back(4);
   3970   lhs.push_back(6);
   3971   EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
   3972   EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
   3973 }
   3974 
   3975 TEST(PointwiseTest, RejectsWrongSize) {
   3976   const double lhs[2] = { 1, 2 };
   3977   const int rhs[1] = { 0 };
   3978   EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
   3979   EXPECT_EQ("which contains 2 values",
   3980             Explain(Pointwise(Gt(), rhs), lhs));
   3981 
   3982   const int rhs2[3] = { 0, 1, 2 };
   3983   EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
   3984 }
   3985 
   3986 TEST(PointwiseTest, RejectsWrongContent) {
   3987   const double lhs[3] = { 1, 2, 3 };
   3988   const int rhs[3] = { 2, 6, 4 };
   3989   EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
   3990   EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
   3991             "where the second/2 is 3",
   3992             Explain(Pointwise(IsHalfOf(), rhs), lhs));
   3993 }
   3994 
   3995 TEST(PointwiseTest, AcceptsCorrectContent) {
   3996   const double lhs[3] = { 1, 2, 3 };
   3997   const int rhs[3] = { 2, 4, 6 };
   3998   EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
   3999   EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
   4000 }
   4001 
   4002 TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
   4003   const double lhs[3] = { 1, 2, 3 };
   4004   const int rhs[3] = { 2, 4, 6 };
   4005   const Matcher<tuple<const double&, const int&> > m1 = IsHalfOf();
   4006   EXPECT_THAT(lhs, Pointwise(m1, rhs));
   4007   EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
   4008 
   4009   // This type works as a tuple<const double&, const int&> can be
   4010   // implicitly cast to tuple<double, int>.
   4011   const Matcher<tuple<double, int> > m2 = IsHalfOf();
   4012   EXPECT_THAT(lhs, Pointwise(m2, rhs));
   4013   EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
   4014 }
   4015 
   4016 }  // namespace gmock_matchers_test
   4017 }  // namespace testing
   4018