Path: blob/main/contrib/googletest/googlemock/test/gmock-matchers-arithmetic_test.cc
101206 views
// Copyright 2007, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Google Mock - a framework for writing C++ mock classes.30//31// This file tests some commonly used argument matchers.3233#include <cmath>34#include <limits>35#include <memory>36#include <ostream>37#include <string>3839#include "gmock/gmock.h"40#include "test/gmock-matchers_test.h"41#include "gtest/gtest.h"4243// Silence warning C4244: 'initializing': conversion from 'int' to 'short',44// possible loss of data and C4100, unreferenced local parameter45GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)4647namespace testing {48namespace gmock_matchers_test {49namespace {5051typedef ::std::tuple<long, int> Tuple2; // NOLINT5253// Tests that Eq() matches a 2-tuple where the first field == the54// second field.55TEST(Eq2Test, MatchesEqualArguments) {56Matcher<const Tuple2&> m = Eq();57EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));58EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));59}6061// Tests that Eq() describes itself properly.62TEST(Eq2Test, CanDescribeSelf) {63Matcher<const Tuple2&> m = Eq();64EXPECT_EQ("are an equal pair", Describe(m));65}6667// Tests that Ge() matches a 2-tuple where the first field >= the68// second field.69TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {70Matcher<const Tuple2&> m = Ge();71EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));72EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));73EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));74}7576// Tests that Ge() describes itself properly.77TEST(Ge2Test, CanDescribeSelf) {78Matcher<const Tuple2&> m = Ge();79EXPECT_EQ("are a pair where the first >= the second", Describe(m));80}8182// Tests that Gt() matches a 2-tuple where the first field > the83// second field.84TEST(Gt2Test, MatchesGreaterThanArguments) {85Matcher<const Tuple2&> m = Gt();86EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));87EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));88EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));89}9091// Tests that Gt() describes itself properly.92TEST(Gt2Test, CanDescribeSelf) {93Matcher<const Tuple2&> m = Gt();94EXPECT_EQ("are a pair where the first > the second", Describe(m));95}9697// Tests that Le() matches a 2-tuple where the first field <= the98// second field.99TEST(Le2Test, MatchesLessThanOrEqualArguments) {100Matcher<const Tuple2&> m = Le();101EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));102EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));103EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));104}105106// Tests that Le() describes itself properly.107TEST(Le2Test, CanDescribeSelf) {108Matcher<const Tuple2&> m = Le();109EXPECT_EQ("are a pair where the first <= the second", Describe(m));110}111112// Tests that Lt() matches a 2-tuple where the first field < the113// second field.114TEST(Lt2Test, MatchesLessThanArguments) {115Matcher<const Tuple2&> m = Lt();116EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));117EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));118EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));119}120121// Tests that Lt() describes itself properly.122TEST(Lt2Test, CanDescribeSelf) {123Matcher<const Tuple2&> m = Lt();124EXPECT_EQ("are a pair where the first < the second", Describe(m));125}126127// Tests that Ne() matches a 2-tuple where the first field != the128// second field.129TEST(Ne2Test, MatchesUnequalArguments) {130Matcher<const Tuple2&> m = Ne();131EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));132EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));133EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));134}135136// Tests that Ne() describes itself properly.137TEST(Ne2Test, CanDescribeSelf) {138Matcher<const Tuple2&> m = Ne();139EXPECT_EQ("are an unequal pair", Describe(m));140}141142TEST(PairMatchBaseTest, WorksWithMoveOnly) {143using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;144Matcher<Pointers> matcher = Eq();145Pointers pointers;146// Tested values don't matter; the point is that matcher does not copy the147// matched values.148EXPECT_TRUE(matcher.Matches(pointers));149}150151// Tests that IsNan() matches a NaN, with float.152TEST(IsNan, FloatMatchesNan) {153float quiet_nan = std::numeric_limits<float>::quiet_NaN();154float other_nan = std::nanf("1");155float real_value = 1.0f;156157Matcher<float> m = IsNan();158EXPECT_TRUE(m.Matches(quiet_nan));159EXPECT_TRUE(m.Matches(other_nan));160EXPECT_FALSE(m.Matches(real_value));161162Matcher<float&> m_ref = IsNan();163EXPECT_TRUE(m_ref.Matches(quiet_nan));164EXPECT_TRUE(m_ref.Matches(other_nan));165EXPECT_FALSE(m_ref.Matches(real_value));166167Matcher<const float&> m_cref = IsNan();168EXPECT_TRUE(m_cref.Matches(quiet_nan));169EXPECT_TRUE(m_cref.Matches(other_nan));170EXPECT_FALSE(m_cref.Matches(real_value));171}172173// Tests that IsNan() matches a NaN, with double.174TEST(IsNan, DoubleMatchesNan) {175double quiet_nan = std::numeric_limits<double>::quiet_NaN();176double other_nan = std::nan("1");177double real_value = 1.0;178179Matcher<double> m = IsNan();180EXPECT_TRUE(m.Matches(quiet_nan));181EXPECT_TRUE(m.Matches(other_nan));182EXPECT_FALSE(m.Matches(real_value));183184Matcher<double&> m_ref = IsNan();185EXPECT_TRUE(m_ref.Matches(quiet_nan));186EXPECT_TRUE(m_ref.Matches(other_nan));187EXPECT_FALSE(m_ref.Matches(real_value));188189Matcher<const double&> m_cref = IsNan();190EXPECT_TRUE(m_cref.Matches(quiet_nan));191EXPECT_TRUE(m_cref.Matches(other_nan));192EXPECT_FALSE(m_cref.Matches(real_value));193}194195// Tests that IsNan() matches a NaN, with long double.196TEST(IsNan, LongDoubleMatchesNan) {197long double quiet_nan = std::numeric_limits<long double>::quiet_NaN();198long double other_nan = std::nan("1");199long double real_value = 1.0;200201Matcher<long double> m = IsNan();202EXPECT_TRUE(m.Matches(quiet_nan));203EXPECT_TRUE(m.Matches(other_nan));204EXPECT_FALSE(m.Matches(real_value));205206Matcher<long double&> m_ref = IsNan();207EXPECT_TRUE(m_ref.Matches(quiet_nan));208EXPECT_TRUE(m_ref.Matches(other_nan));209EXPECT_FALSE(m_ref.Matches(real_value));210211Matcher<const long double&> m_cref = IsNan();212EXPECT_TRUE(m_cref.Matches(quiet_nan));213EXPECT_TRUE(m_cref.Matches(other_nan));214EXPECT_FALSE(m_cref.Matches(real_value));215}216217// Tests that IsNan() works with Not.218TEST(IsNan, NotMatchesNan) {219Matcher<float> mf = Not(IsNan());220EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN()));221EXPECT_FALSE(mf.Matches(std::nanf("1")));222EXPECT_TRUE(mf.Matches(1.0));223224Matcher<double> md = Not(IsNan());225EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN()));226EXPECT_FALSE(md.Matches(std::nan("1")));227EXPECT_TRUE(md.Matches(1.0));228229Matcher<long double> mld = Not(IsNan());230EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN()));231EXPECT_FALSE(mld.Matches(std::nanl("1")));232EXPECT_TRUE(mld.Matches(1.0));233}234235// Tests that IsNan() can describe itself.236TEST(IsNan, CanDescribeSelf) {237Matcher<float> mf = IsNan();238EXPECT_EQ("is NaN", Describe(mf));239240Matcher<double> md = IsNan();241EXPECT_EQ("is NaN", Describe(md));242243Matcher<long double> mld = IsNan();244EXPECT_EQ("is NaN", Describe(mld));245}246247// Tests that IsNan() can describe itself with Not.248TEST(IsNan, CanDescribeSelfWithNot) {249Matcher<float> mf = Not(IsNan());250EXPECT_EQ("isn't NaN", Describe(mf));251252Matcher<double> md = Not(IsNan());253EXPECT_EQ("isn't NaN", Describe(md));254255Matcher<long double> mld = Not(IsNan());256EXPECT_EQ("isn't NaN", Describe(mld));257}258259// Tests that FloatEq() matches a 2-tuple where260// FloatEq(first field) matches the second field.261TEST(FloatEq2Test, MatchesEqualArguments) {262typedef ::std::tuple<float, float> Tpl;263Matcher<const Tpl&> m = FloatEq();264EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));265EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f)));266EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));267}268269// Tests that FloatEq() describes itself properly.270TEST(FloatEq2Test, CanDescribeSelf) {271Matcher<const ::std::tuple<float, float>&> m = FloatEq();272EXPECT_EQ("are an almost-equal pair", Describe(m));273}274275// Tests that NanSensitiveFloatEq() matches a 2-tuple where276// NanSensitiveFloatEq(first field) matches the second field.277TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {278typedef ::std::tuple<float, float> Tpl;279Matcher<const Tpl&> m = NanSensitiveFloatEq();280EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));281EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),282std::numeric_limits<float>::quiet_NaN())));283EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));284EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));285EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));286}287288// Tests that NanSensitiveFloatEq() describes itself properly.289TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {290Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq();291EXPECT_EQ("are an almost-equal pair", Describe(m));292}293294// Tests that DoubleEq() matches a 2-tuple where295// DoubleEq(first field) matches the second field.296TEST(DoubleEq2Test, MatchesEqualArguments) {297typedef ::std::tuple<double, double> Tpl;298Matcher<const Tpl&> m = DoubleEq();299EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));300EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1)));301EXPECT_FALSE(m.Matches(Tpl(1.1, 1.0)));302}303304// Tests that DoubleEq() describes itself properly.305TEST(DoubleEq2Test, CanDescribeSelf) {306Matcher<const ::std::tuple<double, double>&> m = DoubleEq();307EXPECT_EQ("are an almost-equal pair", Describe(m));308}309310// Tests that NanSensitiveDoubleEq() matches a 2-tuple where311// NanSensitiveDoubleEq(first field) matches the second field.312TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {313typedef ::std::tuple<double, double> Tpl;314Matcher<const Tpl&> m = NanSensitiveDoubleEq();315EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));316EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),317std::numeric_limits<double>::quiet_NaN())));318EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));319EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));320EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));321}322323// Tests that DoubleEq() describes itself properly.324TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {325Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq();326EXPECT_EQ("are an almost-equal pair", Describe(m));327}328329// Tests that FloatEq() matches a 2-tuple where330// FloatNear(first field, max_abs_error) matches the second field.331TEST(FloatNear2Test, MatchesEqualArguments) {332typedef ::std::tuple<float, float> Tpl;333Matcher<const Tpl&> m = FloatNear(0.5f);334EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));335EXPECT_TRUE(m.Matches(Tpl(1.3f, 1.0f)));336EXPECT_FALSE(m.Matches(Tpl(1.8f, 1.0f)));337}338339// Tests that FloatNear() describes itself properly.340TEST(FloatNear2Test, CanDescribeSelf) {341Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f);342EXPECT_EQ("are an almost-equal pair", Describe(m));343}344345// Tests that NanSensitiveFloatNear() matches a 2-tuple where346// NanSensitiveFloatNear(first field) matches the second field.347TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {348typedef ::std::tuple<float, float> Tpl;349Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f);350EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));351EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));352EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),353std::numeric_limits<float>::quiet_NaN())));354EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));355EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));356EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));357}358359// Tests that NanSensitiveFloatNear() describes itself properly.360TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {361Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f);362EXPECT_EQ("are an almost-equal pair", Describe(m));363}364365// Tests that FloatEq() matches a 2-tuple where366// DoubleNear(first field, max_abs_error) matches the second field.367TEST(DoubleNear2Test, MatchesEqualArguments) {368typedef ::std::tuple<double, double> Tpl;369Matcher<const Tpl&> m = DoubleNear(0.5);370EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));371EXPECT_TRUE(m.Matches(Tpl(1.3, 1.0)));372EXPECT_FALSE(m.Matches(Tpl(1.8, 1.0)));373}374375// Tests that DoubleNear() describes itself properly.376TEST(DoubleNear2Test, CanDescribeSelf) {377Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5);378EXPECT_EQ("are an almost-equal pair", Describe(m));379}380381// Tests that NanSensitiveDoubleNear() matches a 2-tuple where382// NanSensitiveDoubleNear(first field) matches the second field.383TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {384typedef ::std::tuple<double, double> Tpl;385Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f);386EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));387EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));388EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),389std::numeric_limits<double>::quiet_NaN())));390EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));391EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));392EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));393}394395// Tests that NanSensitiveDoubleNear() describes itself properly.396TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {397Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f);398EXPECT_EQ("are an almost-equal pair", Describe(m));399}400401// Tests that DistanceFrom() can describe itself properly.402TEST(DistanceFrom, CanDescribeSelf) {403Matcher<double> m = DistanceFrom(1.5, Lt(0.1));404EXPECT_EQ(Describe(m), "is < 0.1 away from 1.5");405406m = DistanceFrom(2.5, Gt(0.2));407EXPECT_EQ(Describe(m), "is > 0.2 away from 2.5");408}409410// Tests that DistanceFrom() can explain match failure.411TEST(DistanceFrom, CanExplainMatchFailure) {412Matcher<double> m = DistanceFrom(1.5, Lt(0.1));413EXPECT_EQ(Explain(m, 2.0), "which is 0.5 away from 1.5");414}415416// Tests that DistanceFrom() matches a double that is within the given range of417// the given value.418TEST(DistanceFrom, MatchesDoubleWithinRange) {419const Matcher<double> m = DistanceFrom(0.5, Le(0.1));420EXPECT_TRUE(m.Matches(0.45));421EXPECT_TRUE(m.Matches(0.5));422EXPECT_TRUE(m.Matches(0.55));423EXPECT_FALSE(m.Matches(0.39));424EXPECT_FALSE(m.Matches(0.61));425}426427// Tests that DistanceFrom() matches a double reference that is within the given428// range of the given value.429TEST(DistanceFrom, MatchesDoubleRefWithinRange) {430const Matcher<const double&> m = DistanceFrom(0.5, Le(0.1));431EXPECT_TRUE(m.Matches(0.45));432EXPECT_TRUE(m.Matches(0.5));433EXPECT_TRUE(m.Matches(0.55));434EXPECT_FALSE(m.Matches(0.39));435EXPECT_FALSE(m.Matches(0.61));436}437438// Tests that DistanceFrom() can be implicitly converted to a matcher depending439// on the type of the argument.440TEST(DistanceFrom, CanBeImplicitlyConvertedToMatcher) {441EXPECT_THAT(0.58, DistanceFrom(0.5, Le(0.1)));442EXPECT_THAT(0.2, Not(DistanceFrom(0.5, Le(0.1))));443444EXPECT_THAT(0.58f, DistanceFrom(0.5f, Le(0.1f)));445EXPECT_THAT(0.7f, Not(DistanceFrom(0.5f, Le(0.1f))));446}447448// Tests that DistanceFrom() can be used on compatible types (i.e. not449// everything has to be of the same type).450TEST(DistanceFrom, CanBeUsedOnCompatibleTypes) {451EXPECT_THAT(0.58, DistanceFrom(0.5, Le(0.1f)));452EXPECT_THAT(0.2, Not(DistanceFrom(0.5, Le(0.1f))));453454EXPECT_THAT(0.58, DistanceFrom(0.5f, Le(0.1)));455EXPECT_THAT(0.2, Not(DistanceFrom(0.5f, Le(0.1))));456457EXPECT_THAT(0.58, DistanceFrom(0.5f, Le(0.1f)));458EXPECT_THAT(0.2, Not(DistanceFrom(0.5f, Le(0.1f))));459460EXPECT_THAT(0.58f, DistanceFrom(0.5, Le(0.1)));461EXPECT_THAT(0.2f, Not(DistanceFrom(0.5, Le(0.1))));462463EXPECT_THAT(0.58f, DistanceFrom(0.5, Le(0.1f)));464EXPECT_THAT(0.2f, Not(DistanceFrom(0.5, Le(0.1f))));465466EXPECT_THAT(0.58f, DistanceFrom(0.5f, Le(0.1)));467EXPECT_THAT(0.2f, Not(DistanceFrom(0.5f, Le(0.1))));468}469470// A 2-dimensional point. For testing using DistanceFrom() with a custom type471// that doesn't have a built-in distance function.472class Point {473public:474Point(double x, double y) : x_(x), y_(y) {}475double x() const { return x_; }476double y() const { return y_; }477478private:479double x_;480double y_;481};482483// Returns the distance between two points.484double PointDistance(const Point& lhs, const Point& rhs) {485return std::sqrt(std::pow(lhs.x() - rhs.x(), 2) +486std::pow(lhs.y() - rhs.y(), 2));487}488489// Tests that DistanceFrom() can be used on a type with a custom distance490// function.491TEST(DistanceFrom, CanBeUsedOnTypeWithCustomDistanceFunction) {492const Matcher<Point> m =493DistanceFrom(Point(0.5, 0.5), PointDistance, Le(0.1));494EXPECT_THAT(Point(0.45, 0.45), m);495EXPECT_THAT(Point(0.2, 0.45), Not(m));496}497498// A wrapper around a double value. For testing using DistanceFrom() with a499// custom type that has neither a built-in distance function nor a built-in500// distance comparator.501class Double {502public:503explicit Double(double value) : value_(value) {}504Double(const Double& other) = default;505double value() const { return value_; }506507// Defines how to print a Double value. We don't use the AbslStringify API508// because googletest doesn't require absl yet.509friend void PrintTo(const Double& value, std::ostream* os) {510*os << "Double(" << value.value() << ")";511}512513private:514double value_;515};516517// Returns the distance between two Double values.518Double DoubleDistance(Double lhs, Double rhs) {519return Double(std::abs(lhs.value() - rhs.value()));520}521522MATCHER_P(DoubleLe, rhs, (negation ? "is > " : "is <= ") + PrintToString(rhs)) {523return arg.value() <= rhs.value();524}525526// Tests that DistanceFrom() can describe itself properly for a type with a527// custom printer.528TEST(DistanceFrom, CanDescribeWithCustomPrinter) {529const Matcher<Double> m =530DistanceFrom(Double(0.5), DoubleDistance, DoubleLe(Double(0.1)));531EXPECT_EQ(Describe(m), "is <= Double(0.1) away from Double(0.5)");532EXPECT_EQ(DescribeNegation(m), "is > Double(0.1) away from Double(0.5)");533}534535// Tests that DistanceFrom() can be used with a custom distance function and536// comparator.537TEST(DistanceFrom, CanCustomizeDistanceAndComparator) {538const Matcher<Double> m =539DistanceFrom(Double(0.5), DoubleDistance, DoubleLe(Double(0.1)));540EXPECT_TRUE(m.Matches(Double(0.45)));541EXPECT_TRUE(m.Matches(Double(0.5)));542EXPECT_FALSE(m.Matches(Double(0.39)));543EXPECT_FALSE(m.Matches(Double(0.61)));544}545546// For testing using DistanceFrom() with a type that supports both - and abs.547class Float {548public:549explicit Float(float value) : value_(value) {}550Float(const Float& other) = default;551float value() const { return value_; }552553private:554float value_ = 0.0f;555};556557// Returns the difference between two Float values. This must be defined in the558// same namespace as Float.559Float operator-(const Float& lhs, const Float& rhs) {560return Float(lhs.value() - rhs.value());561}562563// Returns the absolute value of a Float value. This must be defined in the564// same namespace as Float.565Float abs(Float value) { return Float(std::abs(value.value())); }566567// Returns true if and only if the first Float value is less than the second568// Float value. This must be defined in the same namespace as Float.569bool operator<(const Float& lhs, const Float& rhs) {570return lhs.value() < rhs.value();571}572573// Tests that DistanceFrom() can be used with a type that supports both - and574// abs.575TEST(DistanceFrom, CanBeUsedWithTypeThatSupportsBothMinusAndAbs) {576const Matcher<Float> m = DistanceFrom(Float(0.5f), Lt(Float(0.1f)));577EXPECT_TRUE(m.Matches(Float(0.45f)));578EXPECT_TRUE(m.Matches(Float(0.55f)));579EXPECT_FALSE(m.Matches(Float(0.39f)));580EXPECT_FALSE(m.Matches(Float(0.61f)));581}582583// Tests that Not(m) matches any value that doesn't match m.584TEST(NotTest, NegatesMatcher) {585Matcher<int> m;586m = Not(Eq(2));587EXPECT_TRUE(m.Matches(3));588EXPECT_FALSE(m.Matches(2));589}590591// Tests that Not(m) describes itself properly.592TEST(NotTest, CanDescribeSelf) {593Matcher<int> m = Not(Eq(5));594EXPECT_EQ("isn't equal to 5", Describe(m));595}596597// Tests that monomorphic matchers are safely cast by the Not matcher.598TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {599// greater_than_5 is a monomorphic matcher.600Matcher<int> greater_than_5 = Gt(5);601602Matcher<const int&> m = Not(greater_than_5);603Matcher<int&> m2 = Not(greater_than_5);604Matcher<int&> m3 = Not(m);605}606607// Helper to allow easy testing of AllOf matchers with num parameters.608void AllOfMatches(int num, const Matcher<int>& m) {609SCOPED_TRACE(Describe(m));610EXPECT_TRUE(m.Matches(0));611for (int i = 1; i <= num; ++i) {612EXPECT_FALSE(m.Matches(i));613}614EXPECT_TRUE(m.Matches(num + 1));615}616617INSTANTIATE_GTEST_MATCHER_TEST_P(AllOfTest);618619// Tests that AllOf(m1, ..., mn) matches any value that matches all of620// the given matchers.621TEST(AllOfTest, MatchesWhenAllMatch) {622Matcher<int> m;623m = AllOf(Le(2), Ge(1));624EXPECT_TRUE(m.Matches(1));625EXPECT_TRUE(m.Matches(2));626EXPECT_FALSE(m.Matches(0));627EXPECT_FALSE(m.Matches(3));628629m = AllOf(Gt(0), Ne(1), Ne(2));630EXPECT_TRUE(m.Matches(3));631EXPECT_FALSE(m.Matches(2));632EXPECT_FALSE(m.Matches(1));633EXPECT_FALSE(m.Matches(0));634635m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));636EXPECT_TRUE(m.Matches(4));637EXPECT_FALSE(m.Matches(3));638EXPECT_FALSE(m.Matches(2));639EXPECT_FALSE(m.Matches(1));640EXPECT_FALSE(m.Matches(0));641642m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));643EXPECT_TRUE(m.Matches(0));644EXPECT_TRUE(m.Matches(1));645EXPECT_FALSE(m.Matches(3));646647// The following tests for varying number of sub-matchers. Due to the way648// the sub-matchers are handled it is enough to test every sub-matcher once649// with sub-matchers using the same matcher type. Varying matcher types are650// checked for above.651AllOfMatches(2, AllOf(Ne(1), Ne(2)));652AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));653AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));654AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));655AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));656AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));657AllOfMatches(8,658AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8)));659AllOfMatches(6609, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9)));661AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),662Ne(9), Ne(10)));663AllOfMatches(66450, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),665Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17),666Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25),667Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33),668Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41),669Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),670Ne(50)));671}672673// Tests that AllOf(m1, ..., mn) describes itself properly.674TEST(AllOfTest, CanDescribeSelf) {675Matcher<int> m;676m = AllOf(Le(2), Ge(1));677EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));678679m = AllOf(Gt(0), Ne(1), Ne(2));680std::string expected_descr1 =681"(is > 0) and (isn't equal to 1) and (isn't equal to 2)";682EXPECT_EQ(expected_descr1, Describe(m));683684m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));685std::string expected_descr2 =686"(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "687"to 3)";688EXPECT_EQ(expected_descr2, Describe(m));689690m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));691std::string expected_descr3 =692"(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "693"and (isn't equal to 7)";694EXPECT_EQ(expected_descr3, Describe(m));695}696697// Tests that AllOf(m1, ..., mn) describes its negation properly.698TEST(AllOfTest, CanDescribeNegation) {699Matcher<int> m;700m = AllOf(Le(2), Ge(1));701std::string expected_descr4 = "(isn't <= 2) or (isn't >= 1)";702EXPECT_EQ(expected_descr4, DescribeNegation(m));703704m = AllOf(Gt(0), Ne(1), Ne(2));705std::string expected_descr5 =706"(isn't > 0) or (is equal to 1) or (is equal to 2)";707EXPECT_EQ(expected_descr5, DescribeNegation(m));708709m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));710std::string expected_descr6 =711"(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";712EXPECT_EQ(expected_descr6, DescribeNegation(m));713714m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));715std::string expected_desr7 =716"(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "717"(is equal to 7)";718EXPECT_EQ(expected_desr7, DescribeNegation(m));719720m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),721Ne(10), Ne(11));722AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);723EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11)"));724AllOfMatches(11, m);725}726727// Tests that monomorphic matchers are safely cast by the AllOf matcher.728TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {729// greater_than_5 and less_than_10 are monomorphic matchers.730Matcher<int> greater_than_5 = Gt(5);731Matcher<int> less_than_10 = Lt(10);732733Matcher<const int&> m = AllOf(greater_than_5, less_than_10);734Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);735Matcher<int&> m3 = AllOf(greater_than_5, m2);736737// Tests that BothOf works when composing itself.738Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);739Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);740}741742TEST_P(AllOfTestP, ExplainsResult) {743Matcher<int> m;744745// Successful match. Both matchers need to explain. The second746// matcher doesn't give an explanation, so the matcher description is used.747m = AllOf(GreaterThan(10), Lt(30));748EXPECT_EQ("which is 15 more than 10, and is < 30", Explain(m, 25));749750// Successful match. Both matchers need to explain.751m = AllOf(GreaterThan(10), GreaterThan(20));752EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",753Explain(m, 30));754755// Successful match. All matchers need to explain. The second756// matcher doesn't given an explanation.757m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));758EXPECT_EQ(759"which is 15 more than 10, and is < 30, and which is 5 more than 20",760Explain(m, 25));761762// Successful match. All matchers need to explain.763m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));764EXPECT_EQ(765"which is 30 more than 10, and which is 20 more than 20, "766"and which is 10 more than 30",767Explain(m, 40));768769// Failed match. The first matcher, which failed, needs to770// explain.771m = AllOf(GreaterThan(10), GreaterThan(20));772EXPECT_EQ("which is 5 less than 10", Explain(m, 5));773774// Failed match. The second matcher, which failed, needs to775// explain. Since it doesn't given an explanation, the matcher text is776// printed.777m = AllOf(GreaterThan(10), Lt(30));778EXPECT_EQ("which doesn't match (is < 30)", Explain(m, 40));779780// Failed match. The second matcher, which failed, needs to781// explain.782m = AllOf(GreaterThan(10), GreaterThan(20));783EXPECT_EQ("which is 5 less than 20", Explain(m, 15));784}785786// Helper to allow easy testing of AnyOf matchers with num parameters.787static void AnyOfMatches(int num, const Matcher<int>& m) {788SCOPED_TRACE(Describe(m));789EXPECT_FALSE(m.Matches(0));790for (int i = 1; i <= num; ++i) {791EXPECT_TRUE(m.Matches(i));792}793EXPECT_FALSE(m.Matches(num + 1));794}795796static void AnyOfStringMatches(int num, const Matcher<std::string>& m) {797SCOPED_TRACE(Describe(m));798EXPECT_FALSE(m.Matches(std::to_string(0)));799800for (int i = 1; i <= num; ++i) {801EXPECT_TRUE(m.Matches(std::to_string(i)));802}803EXPECT_FALSE(m.Matches(std::to_string(num + 1)));804}805806INSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfTest);807808// Tests that AnyOf(m1, ..., mn) matches any value that matches at809// least one of the given matchers.810TEST(AnyOfTest, MatchesWhenAnyMatches) {811Matcher<int> m;812m = AnyOf(Le(1), Ge(3));813EXPECT_TRUE(m.Matches(1));814EXPECT_TRUE(m.Matches(4));815EXPECT_FALSE(m.Matches(2));816817m = AnyOf(Lt(0), Eq(1), Eq(2));818EXPECT_TRUE(m.Matches(-1));819EXPECT_TRUE(m.Matches(1));820EXPECT_TRUE(m.Matches(2));821EXPECT_FALSE(m.Matches(0));822823m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));824EXPECT_TRUE(m.Matches(-1));825EXPECT_TRUE(m.Matches(1));826EXPECT_TRUE(m.Matches(2));827EXPECT_TRUE(m.Matches(3));828EXPECT_FALSE(m.Matches(0));829830m = AnyOf(Le(0), Gt(10), 3, 5, 7);831EXPECT_TRUE(m.Matches(0));832EXPECT_TRUE(m.Matches(11));833EXPECT_TRUE(m.Matches(3));834EXPECT_FALSE(m.Matches(2));835836// The following tests for varying number of sub-matchers. Due to the way837// the sub-matchers are handled it is enough to test every sub-matcher once838// with sub-matchers using the same matcher type. Varying matcher types are839// checked for above.840AnyOfMatches(2, AnyOf(1, 2));841AnyOfMatches(3, AnyOf(1, 2, 3));842AnyOfMatches(4, AnyOf(1, 2, 3, 4));843AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));844AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));845AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));846AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));847AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));848AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));849}850851// Tests the variadic version of the AnyOfMatcher.852TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {853// Also make sure AnyOf is defined in the right namespace and does not depend854// on ADL.855Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);856857EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)"));858AnyOfMatches(11, m);859AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,86017, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,86131, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,86245, 46, 47, 48, 49, 50));863AnyOfStringMatches(86450, AnyOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",865"13", "14", "15", "16", "17", "18", "19", "20", "21", "22",866"23", "24", "25", "26", "27", "28", "29", "30", "31", "32",867"33", "34", "35", "36", "37", "38", "39", "40", "41", "42",868"43", "44", "45", "46", "47", "48", "49", "50"));869}870871TEST(ConditionalTest, MatchesFirstIfCondition) {872Matcher<std::string> eq_red = Eq("red");873Matcher<std::string> ne_red = Ne("red");874Matcher<std::string> m = Conditional(true, eq_red, ne_red);875EXPECT_TRUE(m.Matches("red"));876EXPECT_FALSE(m.Matches("green"));877878StringMatchResultListener listener;879StringMatchResultListener expected;880EXPECT_FALSE(m.MatchAndExplain("green", &listener));881EXPECT_FALSE(eq_red.MatchAndExplain("green", &expected));882EXPECT_THAT(listener.str(), Eq(expected.str()));883}884885TEST(ConditionalTest, MatchesSecondIfCondition) {886Matcher<std::string> eq_red = Eq("red");887Matcher<std::string> ne_red = Ne("red");888Matcher<std::string> m = Conditional(false, eq_red, ne_red);889EXPECT_FALSE(m.Matches("red"));890EXPECT_TRUE(m.Matches("green"));891892StringMatchResultListener listener;893StringMatchResultListener expected;894EXPECT_FALSE(m.MatchAndExplain("red", &listener));895EXPECT_FALSE(ne_red.MatchAndExplain("red", &expected));896EXPECT_THAT(listener.str(), Eq(expected.str()));897}898899// Tests that AnyOf(m1, ..., mn) describes itself properly.900TEST(AnyOfTest, CanDescribeSelf) {901Matcher<int> m;902m = AnyOf(Le(1), Ge(3));903904EXPECT_EQ("(is <= 1) or (is >= 3)", Describe(m));905906m = AnyOf(Lt(0), Eq(1), Eq(2));907EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m));908909m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));910EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",911Describe(m));912913m = AnyOf(Le(0), Gt(10), 3, 5, 7);914EXPECT_EQ(915"(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "916"equal to 7)",917Describe(m));918}919920// Tests that AnyOf(m1, ..., mn) describes its negation properly.921TEST(AnyOfTest, CanDescribeNegation) {922Matcher<int> m;923m = AnyOf(Le(1), Ge(3));924EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", DescribeNegation(m));925926m = AnyOf(Lt(0), Eq(1), Eq(2));927EXPECT_EQ("(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",928DescribeNegation(m));929930m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));931EXPECT_EQ(932"(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "933"equal to 3)",934DescribeNegation(m));935936m = AnyOf(Le(0), Gt(10), 3, 5, 7);937EXPECT_EQ(938"(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "939"to 5) and (isn't equal to 7)",940DescribeNegation(m));941}942943// Tests that monomorphic matchers are safely cast by the AnyOf matcher.944TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {945// greater_than_5 and less_than_10 are monomorphic matchers.946Matcher<int> greater_than_5 = Gt(5);947Matcher<int> less_than_10 = Lt(10);948949Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);950Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);951Matcher<int&> m3 = AnyOf(greater_than_5, m2);952953// Tests that EitherOf works when composing itself.954Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);955Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);956}957958TEST_P(AnyOfTestP, ExplainsResult) {959Matcher<int> m;960961// Failed match. The second matcher have no explanation (description is used).962m = AnyOf(GreaterThan(10), Lt(0));963EXPECT_EQ("which is 5 less than 10, and isn't < 0", Explain(m, 5));964965// Failed match. Both matchers have explanations.966m = AnyOf(GreaterThan(10), GreaterThan(20));967EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",968Explain(m, 5));969970// Failed match. The middle matcher have no explanation.971m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));972EXPECT_EQ(973"which is 5 less than 10, and isn't > 20, and which is 25 less than 30",974Explain(m, 5));975976// Failed match. All three matchers have explanations.977m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));978EXPECT_EQ(979"which is 5 less than 10, and which is 15 less than 20, "980"and which is 25 less than 30",981Explain(m, 5));982983// Successful match. The first macher succeeded and has explanation.984m = AnyOf(GreaterThan(10), GreaterThan(20));985EXPECT_EQ("which is 5 more than 10", Explain(m, 15));986987// Successful match. The second matcher succeeded and has explanation.988m = AnyOf(GreaterThan(30), GreaterThan(20));989EXPECT_EQ("which is 5 more than 20", Explain(m, 25));990991// Successful match. The first matcher succeeded and has no explanation.992m = AnyOf(Gt(10), Lt(20));993EXPECT_EQ("which matches (is > 10)", Explain(m, 15));994995// Successful match. The second matcher succeeded and has no explanation.996m = AnyOf(Gt(30), Gt(20));997EXPECT_EQ("which matches (is > 20)", Explain(m, 25));998}9991000// The following predicate function and predicate functor are for1001// testing the Truly(predicate) matcher.10021003// Returns non-zero if the input is positive. Note that the return1004// type of this function is not bool. It's OK as Truly() accepts any1005// unary function or functor whose return type can be implicitly1006// converted to bool.1007int IsPositive(double x) { return x > 0 ? 1 : 0; }10081009// This functor returns true if the input is greater than the given1010// number.1011class IsGreaterThan {1012public:1013explicit IsGreaterThan(int threshold) : threshold_(threshold) {}10141015bool operator()(int n) const { return n > threshold_; }10161017private:1018int threshold_;1019};10201021// For testing Truly().1022const int foo = 0;10231024// This predicate returns true if and only if the argument references foo and1025// has a zero value.1026bool ReferencesFooAndIsZero(const int& n) { return (&n == &foo) && (n == 0); }10271028// Tests that Truly(predicate) matches what satisfies the given1029// predicate.1030TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {1031Matcher<double> m = Truly(IsPositive);1032EXPECT_TRUE(m.Matches(2.0));1033EXPECT_FALSE(m.Matches(-1.5));1034}10351036// Tests that Truly(predicate_functor) works too.1037TEST(TrulyTest, CanBeUsedWithFunctor) {1038Matcher<int> m = Truly(IsGreaterThan(5));1039EXPECT_TRUE(m.Matches(6));1040EXPECT_FALSE(m.Matches(4));1041}10421043// A class that can be implicitly converted to bool.1044class ConvertibleToBool {1045public:1046explicit ConvertibleToBool(int number) : number_(number) {}1047operator bool() const { return number_ != 0; }10481049private:1050int number_;1051};10521053ConvertibleToBool IsNotZero(int number) { return ConvertibleToBool(number); }10541055// Tests that the predicate used in Truly() may return a class that's1056// implicitly convertible to bool, even when the class has no1057// operator!().1058TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {1059Matcher<int> m = Truly(IsNotZero);1060EXPECT_TRUE(m.Matches(1));1061EXPECT_FALSE(m.Matches(0));1062}10631064// Tests that Truly(predicate) can describe itself properly.1065TEST(TrulyTest, CanDescribeSelf) {1066Matcher<double> m = Truly(IsPositive);1067EXPECT_EQ("satisfies the given predicate", Describe(m));1068}10691070// Tests that Truly(predicate) works when the matcher takes its1071// argument by reference.1072TEST(TrulyTest, WorksForByRefArguments) {1073Matcher<const int&> m = Truly(ReferencesFooAndIsZero);1074EXPECT_TRUE(m.Matches(foo));1075int n = 0;1076EXPECT_FALSE(m.Matches(n));1077}10781079// Tests that Truly(predicate) provides a helpful reason when it fails.1080TEST(TrulyTest, ExplainsFailures) {1081StringMatchResultListener listener;1082EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener));1083EXPECT_EQ(listener.str(), "didn't satisfy the given predicate");1084}10851086// Tests that Matches(m) is a predicate satisfied by whatever that1087// matches matcher m.1088TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {1089EXPECT_TRUE(Matches(Ge(0))(1));1090EXPECT_FALSE(Matches(Eq('a'))('b'));1091}10921093// Tests that Matches(m) works when the matcher takes its argument by1094// reference.1095TEST(MatchesTest, WorksOnByRefArguments) {1096int m = 0, n = 0;1097EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));1098EXPECT_FALSE(Matches(Ref(m))(n));1099}11001101// Tests that a Matcher on non-reference type can be used in1102// Matches().1103TEST(MatchesTest, WorksWithMatcherOnNonRefType) {1104Matcher<int> eq5 = Eq(5);1105EXPECT_TRUE(Matches(eq5)(5));1106EXPECT_FALSE(Matches(eq5)(2));1107}11081109// Tests Value(value, matcher). Since Value() is a simple wrapper for1110// Matches(), which has been tested already, we don't spend a lot of1111// effort on testing Value().1112TEST(ValueTest, WorksWithPolymorphicMatcher) {1113EXPECT_TRUE(Value("hi", StartsWith("h")));1114EXPECT_FALSE(Value(5, Gt(10)));1115}11161117TEST(ValueTest, WorksWithMonomorphicMatcher) {1118const Matcher<int> is_zero = Eq(0);1119EXPECT_TRUE(Value(0, is_zero));1120EXPECT_FALSE(Value('a', is_zero));11211122int n = 0;1123const Matcher<const int&> ref_n = Ref(n);1124EXPECT_TRUE(Value(n, ref_n));1125EXPECT_FALSE(Value(1, ref_n));1126}11271128TEST(AllArgsTest, WorksForTuple) {1129EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt()));1130EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt())));1131}11321133TEST(AllArgsTest, WorksForNonTuple) {1134EXPECT_THAT(42, AllArgs(Gt(0)));1135EXPECT_THAT('a', Not(AllArgs(Eq('b'))));1136}11371138class AllArgsHelper {1139public:1140AllArgsHelper() = default;11411142MOCK_METHOD2(Helper, int(char x, int y));11431144private:1145AllArgsHelper(const AllArgsHelper&) = delete;1146AllArgsHelper& operator=(const AllArgsHelper&) = delete;1147};11481149TEST(AllArgsTest, WorksInWithClause) {1150AllArgsHelper helper;1151ON_CALL(helper, Helper(_, _)).With(AllArgs(Lt())).WillByDefault(Return(1));1152EXPECT_CALL(helper, Helper(_, _));1153EXPECT_CALL(helper, Helper(_, _)).With(AllArgs(Gt())).WillOnce(Return(2));11541155EXPECT_EQ(1, helper.Helper('\1', 2));1156EXPECT_EQ(2, helper.Helper('a', 1));1157}11581159class OptionalMatchersHelper {1160public:1161OptionalMatchersHelper() = default;11621163MOCK_METHOD0(NoArgs, int());11641165MOCK_METHOD1(OneArg, int(int y));11661167MOCK_METHOD2(TwoArgs, int(char x, int y));11681169MOCK_METHOD1(Overloaded, int(char x));1170MOCK_METHOD2(Overloaded, int(char x, int y));11711172private:1173OptionalMatchersHelper(const OptionalMatchersHelper&) = delete;1174OptionalMatchersHelper& operator=(const OptionalMatchersHelper&) = delete;1175};11761177TEST(AllArgsTest, WorksWithoutMatchers) {1178OptionalMatchersHelper helper;11791180ON_CALL(helper, NoArgs).WillByDefault(Return(10));1181ON_CALL(helper, OneArg).WillByDefault(Return(20));1182ON_CALL(helper, TwoArgs).WillByDefault(Return(30));11831184EXPECT_EQ(10, helper.NoArgs());1185EXPECT_EQ(20, helper.OneArg(1));1186EXPECT_EQ(30, helper.TwoArgs('\1', 2));11871188EXPECT_CALL(helper, NoArgs).Times(1);1189EXPECT_CALL(helper, OneArg).WillOnce(Return(100));1190EXPECT_CALL(helper, OneArg(17)).WillOnce(Return(200));1191EXPECT_CALL(helper, TwoArgs).Times(0);11921193EXPECT_EQ(10, helper.NoArgs());1194EXPECT_EQ(100, helper.OneArg(1));1195EXPECT_EQ(200, helper.OneArg(17));1196}11971198// Tests floating-point matchers.1199template <typename RawType>1200class FloatingPointTest : public testing::Test {1201protected:1202typedef testing::internal::FloatingPoint<RawType> Floating;1203typedef typename Floating::Bits Bits;12041205FloatingPointTest()1206: max_ulps_(Floating::kMaxUlps),1207zero_bits_(Floating(0).bits()),1208one_bits_(Floating(1).bits()),1209infinity_bits_(Floating(Floating::Infinity()).bits()),1210close_to_positive_zero_(1211Floating::ReinterpretBits(zero_bits_ + max_ulps_ / 2)),1212close_to_negative_zero_(1213-Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_ / 2)),1214further_from_negative_zero_(-Floating::ReinterpretBits(1215zero_bits_ + max_ulps_ + 1 - max_ulps_ / 2)),1216close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)),1217further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)),1218infinity_(Floating::Infinity()),1219close_to_infinity_(1220Floating::ReinterpretBits(infinity_bits_ - max_ulps_)),1221further_from_infinity_(1222Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)),1223max_(std::numeric_limits<RawType>::max()),1224nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),1225nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {}12261227void TestSize() { EXPECT_EQ(sizeof(RawType), sizeof(Bits)); }12281229// A battery of tests for FloatingEqMatcher::Matches.1230// matcher_maker is a pointer to a function which creates a FloatingEqMatcher.1231void TestMatches(1232testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {1233Matcher<RawType> m1 = matcher_maker(0.0);1234EXPECT_TRUE(m1.Matches(-0.0));1235EXPECT_TRUE(m1.Matches(close_to_positive_zero_));1236EXPECT_TRUE(m1.Matches(close_to_negative_zero_));1237EXPECT_FALSE(m1.Matches(1.0));12381239Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);1240EXPECT_FALSE(m2.Matches(further_from_negative_zero_));12411242Matcher<RawType> m3 = matcher_maker(1.0);1243EXPECT_TRUE(m3.Matches(close_to_one_));1244EXPECT_FALSE(m3.Matches(further_from_one_));12451246// Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.1247EXPECT_FALSE(m3.Matches(0.0));12481249Matcher<RawType> m4 = matcher_maker(-infinity_);1250EXPECT_TRUE(m4.Matches(-close_to_infinity_));12511252Matcher<RawType> m5 = matcher_maker(infinity_);1253EXPECT_TRUE(m5.Matches(close_to_infinity_));12541255// This is interesting as the representations of infinity_ and nan1_1256// are only 1 DLP apart.1257EXPECT_FALSE(m5.Matches(nan1_));12581259// matcher_maker can produce a Matcher<const RawType&>, which is needed in1260// some cases.1261Matcher<const RawType&> m6 = matcher_maker(0.0);1262EXPECT_TRUE(m6.Matches(-0.0));1263EXPECT_TRUE(m6.Matches(close_to_positive_zero_));1264EXPECT_FALSE(m6.Matches(1.0));12651266// matcher_maker can produce a Matcher<RawType&>, which is needed in some1267// cases.1268Matcher<RawType&> m7 = matcher_maker(0.0);1269RawType x = 0.0;1270EXPECT_TRUE(m7.Matches(x));1271x = 0.01f;1272EXPECT_FALSE(m7.Matches(x));1273}12741275// Pre-calculated numbers to be used by the tests.12761277const Bits max_ulps_;12781279const Bits zero_bits_; // The bits that represent 0.0.1280const Bits one_bits_; // The bits that represent 1.0.1281const Bits infinity_bits_; // The bits that represent +infinity.12821283// Some numbers close to 0.0.1284const RawType close_to_positive_zero_;1285const RawType close_to_negative_zero_;1286const RawType further_from_negative_zero_;12871288// Some numbers close to 1.0.1289const RawType close_to_one_;1290const RawType further_from_one_;12911292// Some numbers close to +infinity.1293const RawType infinity_;1294const RawType close_to_infinity_;1295const RawType further_from_infinity_;12961297// Maximum representable value that's not infinity.1298const RawType max_;12991300// Some NaNs.1301const RawType nan1_;1302const RawType nan2_;1303};13041305// Tests floating-point matchers with fixed epsilons.1306template <typename RawType>1307class FloatingPointNearTest : public FloatingPointTest<RawType> {1308protected:1309typedef FloatingPointTest<RawType> ParentType;13101311// A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon.1312// matcher_maker is a pointer to a function which creates a FloatingEqMatcher.1313void TestNearMatches(testing::internal::FloatingEqMatcher<RawType> (1314*matcher_maker)(RawType, RawType)) {1315Matcher<RawType> m1 = matcher_maker(0.0, 0.0);1316EXPECT_TRUE(m1.Matches(0.0));1317EXPECT_TRUE(m1.Matches(-0.0));1318EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_));1319EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_));1320EXPECT_FALSE(m1.Matches(1.0));13211322Matcher<RawType> m2 = matcher_maker(0.0, 1.0);1323EXPECT_TRUE(m2.Matches(0.0));1324EXPECT_TRUE(m2.Matches(-0.0));1325EXPECT_TRUE(m2.Matches(1.0));1326EXPECT_TRUE(m2.Matches(-1.0));1327EXPECT_FALSE(m2.Matches(ParentType::close_to_one_));1328EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_));13291330// Check that inf matches inf, regardless of the of the specified max1331// absolute error.1332Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0);1333EXPECT_TRUE(m3.Matches(ParentType::infinity_));1334EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_));1335EXPECT_FALSE(m3.Matches(-ParentType::infinity_));13361337Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0);1338EXPECT_TRUE(m4.Matches(-ParentType::infinity_));1339EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_));1340EXPECT_FALSE(m4.Matches(ParentType::infinity_));13411342// Test various overflow scenarios.1343Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_);1344EXPECT_TRUE(m5.Matches(ParentType::max_));1345EXPECT_FALSE(m5.Matches(-ParentType::max_));13461347Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_);1348EXPECT_FALSE(m6.Matches(ParentType::max_));1349EXPECT_TRUE(m6.Matches(-ParentType::max_));13501351Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0);1352EXPECT_TRUE(m7.Matches(ParentType::max_));1353EXPECT_FALSE(m7.Matches(-ParentType::max_));13541355Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0);1356EXPECT_FALSE(m8.Matches(ParentType::max_));1357EXPECT_TRUE(m8.Matches(-ParentType::max_));13581359// The difference between max() and -max() normally overflows to infinity,1360// but it should still match if the max_abs_error is also infinity.1361Matcher<RawType> m9 =1362matcher_maker(ParentType::max_, ParentType::infinity_);1363EXPECT_TRUE(m8.Matches(-ParentType::max_));13641365// matcher_maker can produce a Matcher<const RawType&>, which is needed in1366// some cases.1367Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);1368EXPECT_TRUE(m10.Matches(-0.0));1369EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_));1370EXPECT_FALSE(m10.Matches(ParentType::close_to_one_));13711372// matcher_maker can produce a Matcher<RawType&>, which is needed in some1373// cases.1374Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);1375RawType x = 0.0;1376EXPECT_TRUE(m11.Matches(x));1377x = 1.0f;1378EXPECT_TRUE(m11.Matches(x));1379x = -1.0f;1380EXPECT_TRUE(m11.Matches(x));1381x = 1.1f;1382EXPECT_FALSE(m11.Matches(x));1383x = -1.1f;1384EXPECT_FALSE(m11.Matches(x));1385}1386};13871388// Instantiate FloatingPointTest for testing floats.1389typedef FloatingPointTest<float> FloatTest;13901391TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { TestMatches(&FloatEq); }13921393TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {1394TestMatches(&NanSensitiveFloatEq);1395}13961397TEST_F(FloatTest, FloatEqCannotMatchNaN) {1398// FloatEq never matches NaN.1399Matcher<float> m = FloatEq(nan1_);1400EXPECT_FALSE(m.Matches(nan1_));1401EXPECT_FALSE(m.Matches(nan2_));1402EXPECT_FALSE(m.Matches(1.0));1403}14041405TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {1406// NanSensitiveFloatEq will match NaN.1407Matcher<float> m = NanSensitiveFloatEq(nan1_);1408EXPECT_TRUE(m.Matches(nan1_));1409EXPECT_TRUE(m.Matches(nan2_));1410EXPECT_FALSE(m.Matches(1.0));1411}14121413TEST_F(FloatTest, FloatEqCanDescribeSelf) {1414Matcher<float> m1 = FloatEq(2.0f);1415EXPECT_EQ("is approximately 2", Describe(m1));1416EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));14171418Matcher<float> m2 = FloatEq(0.5f);1419EXPECT_EQ("is approximately 0.5", Describe(m2));1420EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));14211422Matcher<float> m3 = FloatEq(nan1_);1423EXPECT_EQ("never matches", Describe(m3));1424EXPECT_EQ("is anything", DescribeNegation(m3));1425}14261427TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {1428Matcher<float> m1 = NanSensitiveFloatEq(2.0f);1429EXPECT_EQ("is approximately 2", Describe(m1));1430EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));14311432Matcher<float> m2 = NanSensitiveFloatEq(0.5f);1433EXPECT_EQ("is approximately 0.5", Describe(m2));1434EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));14351436Matcher<float> m3 = NanSensitiveFloatEq(nan1_);1437EXPECT_EQ("is NaN", Describe(m3));1438EXPECT_EQ("isn't NaN", DescribeNegation(m3));1439}14401441// Instantiate FloatingPointTest for testing floats with a user-specified1442// max absolute error.1443typedef FloatingPointNearTest<float> FloatNearTest;14441445TEST_F(FloatNearTest, FloatNearMatches) { TestNearMatches(&FloatNear); }14461447TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {1448TestNearMatches(&NanSensitiveFloatNear);1449}14501451TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {1452Matcher<float> m1 = FloatNear(2.0f, 0.5f);1453EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));1454EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",1455DescribeNegation(m1));14561457Matcher<float> m2 = FloatNear(0.5f, 0.5f);1458EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));1459EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",1460DescribeNegation(m2));14611462Matcher<float> m3 = FloatNear(nan1_, 0.0);1463EXPECT_EQ("never matches", Describe(m3));1464EXPECT_EQ("is anything", DescribeNegation(m3));1465}14661467TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {1468Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f);1469EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));1470EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",1471DescribeNegation(m1));14721473Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f);1474EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));1475EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",1476DescribeNegation(m2));14771478Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f);1479EXPECT_EQ("is NaN", Describe(m3));1480EXPECT_EQ("isn't NaN", DescribeNegation(m3));1481}14821483TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {1484// FloatNear never matches NaN.1485Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f);1486EXPECT_FALSE(m.Matches(nan1_));1487EXPECT_FALSE(m.Matches(nan2_));1488EXPECT_FALSE(m.Matches(1.0));1489}14901491TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {1492// NanSensitiveFloatNear will match NaN.1493Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f);1494EXPECT_TRUE(m.Matches(nan1_));1495EXPECT_TRUE(m.Matches(nan2_));1496EXPECT_FALSE(m.Matches(1.0));1497}14981499// Instantiate FloatingPointTest for testing doubles.1500typedef FloatingPointTest<double> DoubleTest;15011502TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {1503TestMatches(&DoubleEq);1504}15051506TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {1507TestMatches(&NanSensitiveDoubleEq);1508}15091510TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {1511// DoubleEq never matches NaN.1512Matcher<double> m = DoubleEq(nan1_);1513EXPECT_FALSE(m.Matches(nan1_));1514EXPECT_FALSE(m.Matches(nan2_));1515EXPECT_FALSE(m.Matches(1.0));1516}15171518TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {1519// NanSensitiveDoubleEq will match NaN.1520Matcher<double> m = NanSensitiveDoubleEq(nan1_);1521EXPECT_TRUE(m.Matches(nan1_));1522EXPECT_TRUE(m.Matches(nan2_));1523EXPECT_FALSE(m.Matches(1.0));1524}15251526TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {1527Matcher<double> m1 = DoubleEq(2.0);1528EXPECT_EQ("is approximately 2", Describe(m1));1529EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));15301531Matcher<double> m2 = DoubleEq(0.5);1532EXPECT_EQ("is approximately 0.5", Describe(m2));1533EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));15341535Matcher<double> m3 = DoubleEq(nan1_);1536EXPECT_EQ("never matches", Describe(m3));1537EXPECT_EQ("is anything", DescribeNegation(m3));1538}15391540TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {1541Matcher<double> m1 = NanSensitiveDoubleEq(2.0);1542EXPECT_EQ("is approximately 2", Describe(m1));1543EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));15441545Matcher<double> m2 = NanSensitiveDoubleEq(0.5);1546EXPECT_EQ("is approximately 0.5", Describe(m2));1547EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));15481549Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);1550EXPECT_EQ("is NaN", Describe(m3));1551EXPECT_EQ("isn't NaN", DescribeNegation(m3));1552}15531554// Instantiate FloatingPointTest for testing floats with a user-specified1555// max absolute error.1556typedef FloatingPointNearTest<double> DoubleNearTest;15571558TEST_F(DoubleNearTest, DoubleNearMatches) { TestNearMatches(&DoubleNear); }15591560TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {1561TestNearMatches(&NanSensitiveDoubleNear);1562}15631564TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {1565Matcher<double> m1 = DoubleNear(2.0, 0.5);1566EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));1567EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",1568DescribeNegation(m1));15691570Matcher<double> m2 = DoubleNear(0.5, 0.5);1571EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));1572EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",1573DescribeNegation(m2));15741575Matcher<double> m3 = DoubleNear(nan1_, 0.0);1576EXPECT_EQ("never matches", Describe(m3));1577EXPECT_EQ("is anything", DescribeNegation(m3));1578}15791580TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {1581EXPECT_EQ("", Explain(DoubleNear(2.0, 0.1), 2.05));1582EXPECT_EQ("which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2));1583EXPECT_EQ("which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7));15841585const std::string explanation =1586Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);1587// Different C++ implementations may print floating-point numbers1588// slightly differently.1589EXPECT_TRUE(explanation == "which is 1.2e-10 from 2.1" || // GCC1590explanation == "which is 1.2e-010 from 2.1") // MSVC1591<< " where explanation is \"" << explanation << "\".";1592}15931594TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {1595Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);1596EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));1597EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",1598DescribeNegation(m1));15991600Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);1601EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));1602EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",1603DescribeNegation(m2));16041605Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1);1606EXPECT_EQ("is NaN", Describe(m3));1607EXPECT_EQ("isn't NaN", DescribeNegation(m3));1608}16091610TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {1611// DoubleNear never matches NaN.1612Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1);1613EXPECT_FALSE(m.Matches(nan1_));1614EXPECT_FALSE(m.Matches(nan2_));1615EXPECT_FALSE(m.Matches(1.0));1616}16171618TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {1619// NanSensitiveDoubleNear will match NaN.1620Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1);1621EXPECT_TRUE(m.Matches(nan1_));1622EXPECT_TRUE(m.Matches(nan2_));1623EXPECT_FALSE(m.Matches(1.0));1624}16251626TEST(NotTest, WorksOnMoveOnlyType) {1627std::unique_ptr<int> p(new int(3));1628EXPECT_THAT(p, Pointee(Eq(3)));1629EXPECT_THAT(p, Not(Pointee(Eq(2))));1630}16311632TEST(AllOfTest, HugeMatcher) {1633// Verify that using AllOf with many arguments doesn't cause1634// the compiler to exceed template instantiation depth limit.1635EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,1636testing::AllOf(_, _, _, _, _, _, _, _, _, _)));1637}16381639TEST(AnyOfTest, HugeMatcher) {1640// Verify that using AnyOf with many arguments doesn't cause1641// the compiler to exceed template instantiation depth limit.1642EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,1643testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));1644}16451646namespace adl_test {16471648// Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf1649// don't issue unqualified recursive calls. If they do, the argument dependent1650// name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found1651// as a candidate and the compilation will break due to an ambiguous overload.16521653// The matcher must be in the same namespace as AllOf/AnyOf to make argument1654// dependent lookup find those.1655MATCHER(M, "") {1656(void)arg;1657return true;1658}16591660template <typename T1, typename T2>1661bool AllOf(const T1& /*t1*/, const T2& /*t2*/) {1662return true;1663}16641665TEST(AllOfTest, DoesNotCallAllOfUnqualified) {1666EXPECT_THAT(42,1667testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));1668}16691670template <typename T1, typename T2>1671bool AnyOf(const T1&, const T2&) {1672return true;1673}16741675TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {1676EXPECT_THAT(42,1677testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));1678}16791680} // namespace adl_test16811682TEST(AllOfTest, WorksOnMoveOnlyType) {1683std::unique_ptr<int> p(new int(3));1684EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));1685EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));1686}16871688TEST(AnyOfTest, WorksOnMoveOnlyType) {1689std::unique_ptr<int> p(new int(3));1690EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));1691EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));1692}16931694} // namespace1695} // namespace gmock_matchers_test1696} // namespace testing16971698GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100169917001701