Path: blob/main/contrib/googletest/googlemock/test/gmock-matchers-arithmetic_test.cc
48255 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 <string>3738#include "test/gmock-matchers_test.h"3940// Silence warning C4244: 'initializing': conversion from 'int' to 'short',41// possible loss of data and C4100, unreferenced local parameter42GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100)4344namespace testing {45namespace gmock_matchers_test {46namespace {4748typedef ::std::tuple<long, int> Tuple2; // NOLINT4950// Tests that Eq() matches a 2-tuple where the first field == the51// second field.52TEST(Eq2Test, MatchesEqualArguments) {53Matcher<const Tuple2&> m = Eq();54EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));55EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));56}5758// Tests that Eq() describes itself properly.59TEST(Eq2Test, CanDescribeSelf) {60Matcher<const Tuple2&> m = Eq();61EXPECT_EQ("are an equal pair", Describe(m));62}6364// Tests that Ge() matches a 2-tuple where the first field >= the65// second field.66TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {67Matcher<const Tuple2&> m = Ge();68EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));69EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));70EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));71}7273// Tests that Ge() describes itself properly.74TEST(Ge2Test, CanDescribeSelf) {75Matcher<const Tuple2&> m = Ge();76EXPECT_EQ("are a pair where the first >= the second", Describe(m));77}7879// Tests that Gt() matches a 2-tuple where the first field > the80// second field.81TEST(Gt2Test, MatchesGreaterThanArguments) {82Matcher<const Tuple2&> m = Gt();83EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));84EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));85EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));86}8788// Tests that Gt() describes itself properly.89TEST(Gt2Test, CanDescribeSelf) {90Matcher<const Tuple2&> m = Gt();91EXPECT_EQ("are a pair where the first > the second", Describe(m));92}9394// Tests that Le() matches a 2-tuple where the first field <= the95// second field.96TEST(Le2Test, MatchesLessThanOrEqualArguments) {97Matcher<const Tuple2&> m = Le();98EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));99EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));100EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));101}102103// Tests that Le() describes itself properly.104TEST(Le2Test, CanDescribeSelf) {105Matcher<const Tuple2&> m = Le();106EXPECT_EQ("are a pair where the first <= the second", Describe(m));107}108109// Tests that Lt() matches a 2-tuple where the first field < the110// second field.111TEST(Lt2Test, MatchesLessThanArguments) {112Matcher<const Tuple2&> m = Lt();113EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));114EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));115EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));116}117118// Tests that Lt() describes itself properly.119TEST(Lt2Test, CanDescribeSelf) {120Matcher<const Tuple2&> m = Lt();121EXPECT_EQ("are a pair where the first < the second", Describe(m));122}123124// Tests that Ne() matches a 2-tuple where the first field != the125// second field.126TEST(Ne2Test, MatchesUnequalArguments) {127Matcher<const Tuple2&> m = Ne();128EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));129EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));130EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));131}132133// Tests that Ne() describes itself properly.134TEST(Ne2Test, CanDescribeSelf) {135Matcher<const Tuple2&> m = Ne();136EXPECT_EQ("are an unequal pair", Describe(m));137}138139TEST(PairMatchBaseTest, WorksWithMoveOnly) {140using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;141Matcher<Pointers> matcher = Eq();142Pointers pointers;143// Tested values don't matter; the point is that matcher does not copy the144// matched values.145EXPECT_TRUE(matcher.Matches(pointers));146}147148// Tests that IsNan() matches a NaN, with float.149TEST(IsNan, FloatMatchesNan) {150float quiet_nan = std::numeric_limits<float>::quiet_NaN();151float other_nan = std::nanf("1");152float real_value = 1.0f;153154Matcher<float> m = IsNan();155EXPECT_TRUE(m.Matches(quiet_nan));156EXPECT_TRUE(m.Matches(other_nan));157EXPECT_FALSE(m.Matches(real_value));158159Matcher<float&> m_ref = IsNan();160EXPECT_TRUE(m_ref.Matches(quiet_nan));161EXPECT_TRUE(m_ref.Matches(other_nan));162EXPECT_FALSE(m_ref.Matches(real_value));163164Matcher<const float&> m_cref = IsNan();165EXPECT_TRUE(m_cref.Matches(quiet_nan));166EXPECT_TRUE(m_cref.Matches(other_nan));167EXPECT_FALSE(m_cref.Matches(real_value));168}169170// Tests that IsNan() matches a NaN, with double.171TEST(IsNan, DoubleMatchesNan) {172double quiet_nan = std::numeric_limits<double>::quiet_NaN();173double other_nan = std::nan("1");174double real_value = 1.0;175176Matcher<double> m = IsNan();177EXPECT_TRUE(m.Matches(quiet_nan));178EXPECT_TRUE(m.Matches(other_nan));179EXPECT_FALSE(m.Matches(real_value));180181Matcher<double&> m_ref = IsNan();182EXPECT_TRUE(m_ref.Matches(quiet_nan));183EXPECT_TRUE(m_ref.Matches(other_nan));184EXPECT_FALSE(m_ref.Matches(real_value));185186Matcher<const double&> m_cref = IsNan();187EXPECT_TRUE(m_cref.Matches(quiet_nan));188EXPECT_TRUE(m_cref.Matches(other_nan));189EXPECT_FALSE(m_cref.Matches(real_value));190}191192// Tests that IsNan() matches a NaN, with long double.193TEST(IsNan, LongDoubleMatchesNan) {194long double quiet_nan = std::numeric_limits<long double>::quiet_NaN();195long double other_nan = std::nan("1");196long double real_value = 1.0;197198Matcher<long double> m = IsNan();199EXPECT_TRUE(m.Matches(quiet_nan));200EXPECT_TRUE(m.Matches(other_nan));201EXPECT_FALSE(m.Matches(real_value));202203Matcher<long double&> m_ref = IsNan();204EXPECT_TRUE(m_ref.Matches(quiet_nan));205EXPECT_TRUE(m_ref.Matches(other_nan));206EXPECT_FALSE(m_ref.Matches(real_value));207208Matcher<const long double&> m_cref = IsNan();209EXPECT_TRUE(m_cref.Matches(quiet_nan));210EXPECT_TRUE(m_cref.Matches(other_nan));211EXPECT_FALSE(m_cref.Matches(real_value));212}213214// Tests that IsNan() works with Not.215TEST(IsNan, NotMatchesNan) {216Matcher<float> mf = Not(IsNan());217EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN()));218EXPECT_FALSE(mf.Matches(std::nanf("1")));219EXPECT_TRUE(mf.Matches(1.0));220221Matcher<double> md = Not(IsNan());222EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN()));223EXPECT_FALSE(md.Matches(std::nan("1")));224EXPECT_TRUE(md.Matches(1.0));225226Matcher<long double> mld = Not(IsNan());227EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN()));228EXPECT_FALSE(mld.Matches(std::nanl("1")));229EXPECT_TRUE(mld.Matches(1.0));230}231232// Tests that IsNan() can describe itself.233TEST(IsNan, CanDescribeSelf) {234Matcher<float> mf = IsNan();235EXPECT_EQ("is NaN", Describe(mf));236237Matcher<double> md = IsNan();238EXPECT_EQ("is NaN", Describe(md));239240Matcher<long double> mld = IsNan();241EXPECT_EQ("is NaN", Describe(mld));242}243244// Tests that IsNan() can describe itself with Not.245TEST(IsNan, CanDescribeSelfWithNot) {246Matcher<float> mf = Not(IsNan());247EXPECT_EQ("isn't NaN", Describe(mf));248249Matcher<double> md = Not(IsNan());250EXPECT_EQ("isn't NaN", Describe(md));251252Matcher<long double> mld = Not(IsNan());253EXPECT_EQ("isn't NaN", Describe(mld));254}255256// Tests that FloatEq() matches a 2-tuple where257// FloatEq(first field) matches the second field.258TEST(FloatEq2Test, MatchesEqualArguments) {259typedef ::std::tuple<float, float> Tpl;260Matcher<const Tpl&> m = FloatEq();261EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));262EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f)));263EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));264}265266// Tests that FloatEq() describes itself properly.267TEST(FloatEq2Test, CanDescribeSelf) {268Matcher<const ::std::tuple<float, float>&> m = FloatEq();269EXPECT_EQ("are an almost-equal pair", Describe(m));270}271272// Tests that NanSensitiveFloatEq() matches a 2-tuple where273// NanSensitiveFloatEq(first field) matches the second field.274TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {275typedef ::std::tuple<float, float> Tpl;276Matcher<const Tpl&> m = NanSensitiveFloatEq();277EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));278EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),279std::numeric_limits<float>::quiet_NaN())));280EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));281EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));282EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));283}284285// Tests that NanSensitiveFloatEq() describes itself properly.286TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {287Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq();288EXPECT_EQ("are an almost-equal pair", Describe(m));289}290291// Tests that DoubleEq() matches a 2-tuple where292// DoubleEq(first field) matches the second field.293TEST(DoubleEq2Test, MatchesEqualArguments) {294typedef ::std::tuple<double, double> Tpl;295Matcher<const Tpl&> m = DoubleEq();296EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));297EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1)));298EXPECT_FALSE(m.Matches(Tpl(1.1, 1.0)));299}300301// Tests that DoubleEq() describes itself properly.302TEST(DoubleEq2Test, CanDescribeSelf) {303Matcher<const ::std::tuple<double, double>&> m = DoubleEq();304EXPECT_EQ("are an almost-equal pair", Describe(m));305}306307// Tests that NanSensitiveDoubleEq() matches a 2-tuple where308// NanSensitiveDoubleEq(first field) matches the second field.309TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {310typedef ::std::tuple<double, double> Tpl;311Matcher<const Tpl&> m = NanSensitiveDoubleEq();312EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));313EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),314std::numeric_limits<double>::quiet_NaN())));315EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));316EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));317EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));318}319320// Tests that DoubleEq() describes itself properly.321TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {322Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq();323EXPECT_EQ("are an almost-equal pair", Describe(m));324}325326// Tests that FloatEq() matches a 2-tuple where327// FloatNear(first field, max_abs_error) matches the second field.328TEST(FloatNear2Test, MatchesEqualArguments) {329typedef ::std::tuple<float, float> Tpl;330Matcher<const Tpl&> m = FloatNear(0.5f);331EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));332EXPECT_TRUE(m.Matches(Tpl(1.3f, 1.0f)));333EXPECT_FALSE(m.Matches(Tpl(1.8f, 1.0f)));334}335336// Tests that FloatNear() describes itself properly.337TEST(FloatNear2Test, CanDescribeSelf) {338Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f);339EXPECT_EQ("are an almost-equal pair", Describe(m));340}341342// Tests that NanSensitiveFloatNear() matches a 2-tuple where343// NanSensitiveFloatNear(first field) matches the second field.344TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {345typedef ::std::tuple<float, float> Tpl;346Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f);347EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));348EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));349EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),350std::numeric_limits<float>::quiet_NaN())));351EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));352EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));353EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));354}355356// Tests that NanSensitiveFloatNear() describes itself properly.357TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {358Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f);359EXPECT_EQ("are an almost-equal pair", Describe(m));360}361362// Tests that FloatEq() matches a 2-tuple where363// DoubleNear(first field, max_abs_error) matches the second field.364TEST(DoubleNear2Test, MatchesEqualArguments) {365typedef ::std::tuple<double, double> Tpl;366Matcher<const Tpl&> m = DoubleNear(0.5);367EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));368EXPECT_TRUE(m.Matches(Tpl(1.3, 1.0)));369EXPECT_FALSE(m.Matches(Tpl(1.8, 1.0)));370}371372// Tests that DoubleNear() describes itself properly.373TEST(DoubleNear2Test, CanDescribeSelf) {374Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5);375EXPECT_EQ("are an almost-equal pair", Describe(m));376}377378// Tests that NanSensitiveDoubleNear() matches a 2-tuple where379// NanSensitiveDoubleNear(first field) matches the second field.380TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {381typedef ::std::tuple<double, double> Tpl;382Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f);383EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));384EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));385EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),386std::numeric_limits<double>::quiet_NaN())));387EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));388EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));389EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));390}391392// Tests that NanSensitiveDoubleNear() describes itself properly.393TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {394Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f);395EXPECT_EQ("are an almost-equal pair", Describe(m));396}397398// Tests that Not(m) matches any value that doesn't match m.399TEST(NotTest, NegatesMatcher) {400Matcher<int> m;401m = Not(Eq(2));402EXPECT_TRUE(m.Matches(3));403EXPECT_FALSE(m.Matches(2));404}405406// Tests that Not(m) describes itself properly.407TEST(NotTest, CanDescribeSelf) {408Matcher<int> m = Not(Eq(5));409EXPECT_EQ("isn't equal to 5", Describe(m));410}411412// Tests that monomorphic matchers are safely cast by the Not matcher.413TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {414// greater_than_5 is a monomorphic matcher.415Matcher<int> greater_than_5 = Gt(5);416417Matcher<const int&> m = Not(greater_than_5);418Matcher<int&> m2 = Not(greater_than_5);419Matcher<int&> m3 = Not(m);420}421422// Helper to allow easy testing of AllOf matchers with num parameters.423void AllOfMatches(int num, const Matcher<int>& m) {424SCOPED_TRACE(Describe(m));425EXPECT_TRUE(m.Matches(0));426for (int i = 1; i <= num; ++i) {427EXPECT_FALSE(m.Matches(i));428}429EXPECT_TRUE(m.Matches(num + 1));430}431432INSTANTIATE_GTEST_MATCHER_TEST_P(AllOfTest);433434// Tests that AllOf(m1, ..., mn) matches any value that matches all of435// the given matchers.436TEST(AllOfTest, MatchesWhenAllMatch) {437Matcher<int> m;438m = AllOf(Le(2), Ge(1));439EXPECT_TRUE(m.Matches(1));440EXPECT_TRUE(m.Matches(2));441EXPECT_FALSE(m.Matches(0));442EXPECT_FALSE(m.Matches(3));443444m = AllOf(Gt(0), Ne(1), Ne(2));445EXPECT_TRUE(m.Matches(3));446EXPECT_FALSE(m.Matches(2));447EXPECT_FALSE(m.Matches(1));448EXPECT_FALSE(m.Matches(0));449450m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));451EXPECT_TRUE(m.Matches(4));452EXPECT_FALSE(m.Matches(3));453EXPECT_FALSE(m.Matches(2));454EXPECT_FALSE(m.Matches(1));455EXPECT_FALSE(m.Matches(0));456457m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));458EXPECT_TRUE(m.Matches(0));459EXPECT_TRUE(m.Matches(1));460EXPECT_FALSE(m.Matches(3));461462// The following tests for varying number of sub-matchers. Due to the way463// the sub-matchers are handled it is enough to test every sub-matcher once464// with sub-matchers using the same matcher type. Varying matcher types are465// checked for above.466AllOfMatches(2, AllOf(Ne(1), Ne(2)));467AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));468AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));469AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));470AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));471AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));472AllOfMatches(8,473AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8)));474AllOfMatches(4759, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9)));476AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),477Ne(9), Ne(10)));478AllOfMatches(47950, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),480Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17),481Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25),482Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33),483Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41),484Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),485Ne(50)));486}487488// Tests that AllOf(m1, ..., mn) describes itself properly.489TEST(AllOfTest, CanDescribeSelf) {490Matcher<int> m;491m = AllOf(Le(2), Ge(1));492EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));493494m = AllOf(Gt(0), Ne(1), Ne(2));495std::string expected_descr1 =496"(is > 0) and (isn't equal to 1) and (isn't equal to 2)";497EXPECT_EQ(expected_descr1, Describe(m));498499m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));500std::string expected_descr2 =501"(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "502"to 3)";503EXPECT_EQ(expected_descr2, Describe(m));504505m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));506std::string expected_descr3 =507"(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "508"and (isn't equal to 7)";509EXPECT_EQ(expected_descr3, Describe(m));510}511512// Tests that AllOf(m1, ..., mn) describes its negation properly.513TEST(AllOfTest, CanDescribeNegation) {514Matcher<int> m;515m = AllOf(Le(2), Ge(1));516std::string expected_descr4 = "(isn't <= 2) or (isn't >= 1)";517EXPECT_EQ(expected_descr4, DescribeNegation(m));518519m = AllOf(Gt(0), Ne(1), Ne(2));520std::string expected_descr5 =521"(isn't > 0) or (is equal to 1) or (is equal to 2)";522EXPECT_EQ(expected_descr5, DescribeNegation(m));523524m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));525std::string expected_descr6 =526"(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";527EXPECT_EQ(expected_descr6, DescribeNegation(m));528529m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));530std::string expected_desr7 =531"(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "532"(is equal to 7)";533EXPECT_EQ(expected_desr7, DescribeNegation(m));534535m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),536Ne(10), Ne(11));537AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);538EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11)"));539AllOfMatches(11, m);540}541542// Tests that monomorphic matchers are safely cast by the AllOf matcher.543TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {544// greater_than_5 and less_than_10 are monomorphic matchers.545Matcher<int> greater_than_5 = Gt(5);546Matcher<int> less_than_10 = Lt(10);547548Matcher<const int&> m = AllOf(greater_than_5, less_than_10);549Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);550Matcher<int&> m3 = AllOf(greater_than_5, m2);551552// Tests that BothOf works when composing itself.553Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);554Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);555}556557TEST_P(AllOfTestP, ExplainsResult) {558Matcher<int> m;559560// Successful match. Both matchers need to explain. The second561// matcher doesn't give an explanation, so only the first matcher's562// explanation is printed.563m = AllOf(GreaterThan(10), Lt(30));564EXPECT_EQ("which is 15 more than 10", Explain(m, 25));565566// Successful match. Both matchers need to explain.567m = AllOf(GreaterThan(10), GreaterThan(20));568EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",569Explain(m, 30));570571// Successful match. All matchers need to explain. The second572// matcher doesn't given an explanation.573m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));574EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",575Explain(m, 25));576577// Successful match. All matchers need to explain.578m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));579EXPECT_EQ(580"which is 30 more than 10, and which is 20 more than 20, "581"and which is 10 more than 30",582Explain(m, 40));583584// Failed match. The first matcher, which failed, needs to585// explain.586m = AllOf(GreaterThan(10), GreaterThan(20));587EXPECT_EQ("which is 5 less than 10", Explain(m, 5));588589// Failed match. The second matcher, which failed, needs to590// explain. Since it doesn't given an explanation, nothing is591// printed.592m = AllOf(GreaterThan(10), Lt(30));593EXPECT_EQ("", Explain(m, 40));594595// Failed match. The second matcher, which failed, needs to596// explain.597m = AllOf(GreaterThan(10), GreaterThan(20));598EXPECT_EQ("which is 5 less than 20", Explain(m, 15));599}600601// Helper to allow easy testing of AnyOf matchers with num parameters.602static void AnyOfMatches(int num, const Matcher<int>& m) {603SCOPED_TRACE(Describe(m));604EXPECT_FALSE(m.Matches(0));605for (int i = 1; i <= num; ++i) {606EXPECT_TRUE(m.Matches(i));607}608EXPECT_FALSE(m.Matches(num + 1));609}610611static void AnyOfStringMatches(int num, const Matcher<std::string>& m) {612SCOPED_TRACE(Describe(m));613EXPECT_FALSE(m.Matches(std::to_string(0)));614615for (int i = 1; i <= num; ++i) {616EXPECT_TRUE(m.Matches(std::to_string(i)));617}618EXPECT_FALSE(m.Matches(std::to_string(num + 1)));619}620621INSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfTest);622623// Tests that AnyOf(m1, ..., mn) matches any value that matches at624// least one of the given matchers.625TEST(AnyOfTest, MatchesWhenAnyMatches) {626Matcher<int> m;627m = AnyOf(Le(1), Ge(3));628EXPECT_TRUE(m.Matches(1));629EXPECT_TRUE(m.Matches(4));630EXPECT_FALSE(m.Matches(2));631632m = AnyOf(Lt(0), Eq(1), Eq(2));633EXPECT_TRUE(m.Matches(-1));634EXPECT_TRUE(m.Matches(1));635EXPECT_TRUE(m.Matches(2));636EXPECT_FALSE(m.Matches(0));637638m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));639EXPECT_TRUE(m.Matches(-1));640EXPECT_TRUE(m.Matches(1));641EXPECT_TRUE(m.Matches(2));642EXPECT_TRUE(m.Matches(3));643EXPECT_FALSE(m.Matches(0));644645m = AnyOf(Le(0), Gt(10), 3, 5, 7);646EXPECT_TRUE(m.Matches(0));647EXPECT_TRUE(m.Matches(11));648EXPECT_TRUE(m.Matches(3));649EXPECT_FALSE(m.Matches(2));650651// The following tests for varying number of sub-matchers. Due to the way652// the sub-matchers are handled it is enough to test every sub-matcher once653// with sub-matchers using the same matcher type. Varying matcher types are654// checked for above.655AnyOfMatches(2, AnyOf(1, 2));656AnyOfMatches(3, AnyOf(1, 2, 3));657AnyOfMatches(4, AnyOf(1, 2, 3, 4));658AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));659AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));660AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));661AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));662AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));663AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));664}665666// Tests the variadic version of the AnyOfMatcher.667TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {668// Also make sure AnyOf is defined in the right namespace and does not depend669// on ADL.670Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);671672EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)"));673AnyOfMatches(11, m);674AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,67517, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,67631, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,67745, 46, 47, 48, 49, 50));678AnyOfStringMatches(67950, AnyOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",680"13", "14", "15", "16", "17", "18", "19", "20", "21", "22",681"23", "24", "25", "26", "27", "28", "29", "30", "31", "32",682"33", "34", "35", "36", "37", "38", "39", "40", "41", "42",683"43", "44", "45", "46", "47", "48", "49", "50"));684}685686TEST(ConditionalTest, MatchesFirstIfCondition) {687Matcher<std::string> eq_red = Eq("red");688Matcher<std::string> ne_red = Ne("red");689Matcher<std::string> m = Conditional(true, eq_red, ne_red);690EXPECT_TRUE(m.Matches("red"));691EXPECT_FALSE(m.Matches("green"));692693StringMatchResultListener listener;694StringMatchResultListener expected;695EXPECT_FALSE(m.MatchAndExplain("green", &listener));696EXPECT_FALSE(eq_red.MatchAndExplain("green", &expected));697EXPECT_THAT(listener.str(), Eq(expected.str()));698}699700TEST(ConditionalTest, MatchesSecondIfCondition) {701Matcher<std::string> eq_red = Eq("red");702Matcher<std::string> ne_red = Ne("red");703Matcher<std::string> m = Conditional(false, eq_red, ne_red);704EXPECT_FALSE(m.Matches("red"));705EXPECT_TRUE(m.Matches("green"));706707StringMatchResultListener listener;708StringMatchResultListener expected;709EXPECT_FALSE(m.MatchAndExplain("red", &listener));710EXPECT_FALSE(ne_red.MatchAndExplain("red", &expected));711EXPECT_THAT(listener.str(), Eq(expected.str()));712}713714// Tests that AnyOf(m1, ..., mn) describes itself properly.715TEST(AnyOfTest, CanDescribeSelf) {716Matcher<int> m;717m = AnyOf(Le(1), Ge(3));718719EXPECT_EQ("(is <= 1) or (is >= 3)", Describe(m));720721m = AnyOf(Lt(0), Eq(1), Eq(2));722EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m));723724m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));725EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",726Describe(m));727728m = AnyOf(Le(0), Gt(10), 3, 5, 7);729EXPECT_EQ(730"(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "731"equal to 7)",732Describe(m));733}734735// Tests that AnyOf(m1, ..., mn) describes its negation properly.736TEST(AnyOfTest, CanDescribeNegation) {737Matcher<int> m;738m = AnyOf(Le(1), Ge(3));739EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", DescribeNegation(m));740741m = AnyOf(Lt(0), Eq(1), Eq(2));742EXPECT_EQ("(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",743DescribeNegation(m));744745m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));746EXPECT_EQ(747"(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "748"equal to 3)",749DescribeNegation(m));750751m = AnyOf(Le(0), Gt(10), 3, 5, 7);752EXPECT_EQ(753"(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "754"to 5) and (isn't equal to 7)",755DescribeNegation(m));756}757758// Tests that monomorphic matchers are safely cast by the AnyOf matcher.759TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {760// greater_than_5 and less_than_10 are monomorphic matchers.761Matcher<int> greater_than_5 = Gt(5);762Matcher<int> less_than_10 = Lt(10);763764Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);765Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);766Matcher<int&> m3 = AnyOf(greater_than_5, m2);767768// Tests that EitherOf works when composing itself.769Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);770Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);771}772773TEST_P(AnyOfTestP, ExplainsResult) {774Matcher<int> m;775776// Failed match. Both matchers need to explain. The second777// matcher doesn't give an explanation, so only the first matcher's778// explanation is printed.779m = AnyOf(GreaterThan(10), Lt(0));780EXPECT_EQ("which is 5 less than 10", Explain(m, 5));781782// Failed match. Both matchers need to explain.783m = AnyOf(GreaterThan(10), GreaterThan(20));784EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",785Explain(m, 5));786787// Failed match. All matchers need to explain. The second788// matcher doesn't given an explanation.789m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));790EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",791Explain(m, 5));792793// Failed match. All matchers need to explain.794m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));795EXPECT_EQ(796"which is 5 less than 10, and which is 15 less than 20, "797"and which is 25 less than 30",798Explain(m, 5));799800// Successful match. The first matcher, which succeeded, needs to801// explain.802m = AnyOf(GreaterThan(10), GreaterThan(20));803EXPECT_EQ("which is 5 more than 10", Explain(m, 15));804805// Successful match. The second matcher, which succeeded, needs to806// explain. Since it doesn't given an explanation, nothing is807// printed.808m = AnyOf(GreaterThan(10), Lt(30));809EXPECT_EQ("", Explain(m, 0));810811// Successful match. The second matcher, which succeeded, needs to812// explain.813m = AnyOf(GreaterThan(30), GreaterThan(20));814EXPECT_EQ("which is 5 more than 20", Explain(m, 25));815}816817// The following predicate function and predicate functor are for818// testing the Truly(predicate) matcher.819820// Returns non-zero if the input is positive. Note that the return821// type of this function is not bool. It's OK as Truly() accepts any822// unary function or functor whose return type can be implicitly823// converted to bool.824int IsPositive(double x) { return x > 0 ? 1 : 0; }825826// This functor returns true if the input is greater than the given827// number.828class IsGreaterThan {829public:830explicit IsGreaterThan(int threshold) : threshold_(threshold) {}831832bool operator()(int n) const { return n > threshold_; }833834private:835int threshold_;836};837838// For testing Truly().839const int foo = 0;840841// This predicate returns true if and only if the argument references foo and842// has a zero value.843bool ReferencesFooAndIsZero(const int& n) { return (&n == &foo) && (n == 0); }844845// Tests that Truly(predicate) matches what satisfies the given846// predicate.847TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {848Matcher<double> m = Truly(IsPositive);849EXPECT_TRUE(m.Matches(2.0));850EXPECT_FALSE(m.Matches(-1.5));851}852853// Tests that Truly(predicate_functor) works too.854TEST(TrulyTest, CanBeUsedWithFunctor) {855Matcher<int> m = Truly(IsGreaterThan(5));856EXPECT_TRUE(m.Matches(6));857EXPECT_FALSE(m.Matches(4));858}859860// A class that can be implicitly converted to bool.861class ConvertibleToBool {862public:863explicit ConvertibleToBool(int number) : number_(number) {}864operator bool() const { return number_ != 0; }865866private:867int number_;868};869870ConvertibleToBool IsNotZero(int number) { return ConvertibleToBool(number); }871872// Tests that the predicate used in Truly() may return a class that's873// implicitly convertible to bool, even when the class has no874// operator!().875TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {876Matcher<int> m = Truly(IsNotZero);877EXPECT_TRUE(m.Matches(1));878EXPECT_FALSE(m.Matches(0));879}880881// Tests that Truly(predicate) can describe itself properly.882TEST(TrulyTest, CanDescribeSelf) {883Matcher<double> m = Truly(IsPositive);884EXPECT_EQ("satisfies the given predicate", Describe(m));885}886887// Tests that Truly(predicate) works when the matcher takes its888// argument by reference.889TEST(TrulyTest, WorksForByRefArguments) {890Matcher<const int&> m = Truly(ReferencesFooAndIsZero);891EXPECT_TRUE(m.Matches(foo));892int n = 0;893EXPECT_FALSE(m.Matches(n));894}895896// Tests that Truly(predicate) provides a helpful reason when it fails.897TEST(TrulyTest, ExplainsFailures) {898StringMatchResultListener listener;899EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener));900EXPECT_EQ(listener.str(), "didn't satisfy the given predicate");901}902903// Tests that Matches(m) is a predicate satisfied by whatever that904// matches matcher m.905TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {906EXPECT_TRUE(Matches(Ge(0))(1));907EXPECT_FALSE(Matches(Eq('a'))('b'));908}909910// Tests that Matches(m) works when the matcher takes its argument by911// reference.912TEST(MatchesTest, WorksOnByRefArguments) {913int m = 0, n = 0;914EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));915EXPECT_FALSE(Matches(Ref(m))(n));916}917918// Tests that a Matcher on non-reference type can be used in919// Matches().920TEST(MatchesTest, WorksWithMatcherOnNonRefType) {921Matcher<int> eq5 = Eq(5);922EXPECT_TRUE(Matches(eq5)(5));923EXPECT_FALSE(Matches(eq5)(2));924}925926// Tests Value(value, matcher). Since Value() is a simple wrapper for927// Matches(), which has been tested already, we don't spend a lot of928// effort on testing Value().929TEST(ValueTest, WorksWithPolymorphicMatcher) {930EXPECT_TRUE(Value("hi", StartsWith("h")));931EXPECT_FALSE(Value(5, Gt(10)));932}933934TEST(ValueTest, WorksWithMonomorphicMatcher) {935const Matcher<int> is_zero = Eq(0);936EXPECT_TRUE(Value(0, is_zero));937EXPECT_FALSE(Value('a', is_zero));938939int n = 0;940const Matcher<const int&> ref_n = Ref(n);941EXPECT_TRUE(Value(n, ref_n));942EXPECT_FALSE(Value(1, ref_n));943}944945TEST(AllArgsTest, WorksForTuple) {946EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt()));947EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt())));948}949950TEST(AllArgsTest, WorksForNonTuple) {951EXPECT_THAT(42, AllArgs(Gt(0)));952EXPECT_THAT('a', Not(AllArgs(Eq('b'))));953}954955class AllArgsHelper {956public:957AllArgsHelper() = default;958959MOCK_METHOD2(Helper, int(char x, int y));960961private:962AllArgsHelper(const AllArgsHelper&) = delete;963AllArgsHelper& operator=(const AllArgsHelper&) = delete;964};965966TEST(AllArgsTest, WorksInWithClause) {967AllArgsHelper helper;968ON_CALL(helper, Helper(_, _)).With(AllArgs(Lt())).WillByDefault(Return(1));969EXPECT_CALL(helper, Helper(_, _));970EXPECT_CALL(helper, Helper(_, _)).With(AllArgs(Gt())).WillOnce(Return(2));971972EXPECT_EQ(1, helper.Helper('\1', 2));973EXPECT_EQ(2, helper.Helper('a', 1));974}975976class OptionalMatchersHelper {977public:978OptionalMatchersHelper() = default;979980MOCK_METHOD0(NoArgs, int());981982MOCK_METHOD1(OneArg, int(int y));983984MOCK_METHOD2(TwoArgs, int(char x, int y));985986MOCK_METHOD1(Overloaded, int(char x));987MOCK_METHOD2(Overloaded, int(char x, int y));988989private:990OptionalMatchersHelper(const OptionalMatchersHelper&) = delete;991OptionalMatchersHelper& operator=(const OptionalMatchersHelper&) = delete;992};993994TEST(AllArgsTest, WorksWithoutMatchers) {995OptionalMatchersHelper helper;996997ON_CALL(helper, NoArgs).WillByDefault(Return(10));998ON_CALL(helper, OneArg).WillByDefault(Return(20));999ON_CALL(helper, TwoArgs).WillByDefault(Return(30));10001001EXPECT_EQ(10, helper.NoArgs());1002EXPECT_EQ(20, helper.OneArg(1));1003EXPECT_EQ(30, helper.TwoArgs('\1', 2));10041005EXPECT_CALL(helper, NoArgs).Times(1);1006EXPECT_CALL(helper, OneArg).WillOnce(Return(100));1007EXPECT_CALL(helper, OneArg(17)).WillOnce(Return(200));1008EXPECT_CALL(helper, TwoArgs).Times(0);10091010EXPECT_EQ(10, helper.NoArgs());1011EXPECT_EQ(100, helper.OneArg(1));1012EXPECT_EQ(200, helper.OneArg(17));1013}10141015// Tests floating-point matchers.1016template <typename RawType>1017class FloatingPointTest : public testing::Test {1018protected:1019typedef testing::internal::FloatingPoint<RawType> Floating;1020typedef typename Floating::Bits Bits;10211022FloatingPointTest()1023: max_ulps_(Floating::kMaxUlps),1024zero_bits_(Floating(0).bits()),1025one_bits_(Floating(1).bits()),1026infinity_bits_(Floating(Floating::Infinity()).bits()),1027close_to_positive_zero_(1028Floating::ReinterpretBits(zero_bits_ + max_ulps_ / 2)),1029close_to_negative_zero_(1030-Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_ / 2)),1031further_from_negative_zero_(-Floating::ReinterpretBits(1032zero_bits_ + max_ulps_ + 1 - max_ulps_ / 2)),1033close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)),1034further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)),1035infinity_(Floating::Infinity()),1036close_to_infinity_(1037Floating::ReinterpretBits(infinity_bits_ - max_ulps_)),1038further_from_infinity_(1039Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)),1040max_(std::numeric_limits<RawType>::max()),1041nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),1042nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {}10431044void TestSize() { EXPECT_EQ(sizeof(RawType), sizeof(Bits)); }10451046// A battery of tests for FloatingEqMatcher::Matches.1047// matcher_maker is a pointer to a function which creates a FloatingEqMatcher.1048void TestMatches(1049testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {1050Matcher<RawType> m1 = matcher_maker(0.0);1051EXPECT_TRUE(m1.Matches(-0.0));1052EXPECT_TRUE(m1.Matches(close_to_positive_zero_));1053EXPECT_TRUE(m1.Matches(close_to_negative_zero_));1054EXPECT_FALSE(m1.Matches(1.0));10551056Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);1057EXPECT_FALSE(m2.Matches(further_from_negative_zero_));10581059Matcher<RawType> m3 = matcher_maker(1.0);1060EXPECT_TRUE(m3.Matches(close_to_one_));1061EXPECT_FALSE(m3.Matches(further_from_one_));10621063// Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.1064EXPECT_FALSE(m3.Matches(0.0));10651066Matcher<RawType> m4 = matcher_maker(-infinity_);1067EXPECT_TRUE(m4.Matches(-close_to_infinity_));10681069Matcher<RawType> m5 = matcher_maker(infinity_);1070EXPECT_TRUE(m5.Matches(close_to_infinity_));10711072// This is interesting as the representations of infinity_ and nan1_1073// are only 1 DLP apart.1074EXPECT_FALSE(m5.Matches(nan1_));10751076// matcher_maker can produce a Matcher<const RawType&>, which is needed in1077// some cases.1078Matcher<const RawType&> m6 = matcher_maker(0.0);1079EXPECT_TRUE(m6.Matches(-0.0));1080EXPECT_TRUE(m6.Matches(close_to_positive_zero_));1081EXPECT_FALSE(m6.Matches(1.0));10821083// matcher_maker can produce a Matcher<RawType&>, which is needed in some1084// cases.1085Matcher<RawType&> m7 = matcher_maker(0.0);1086RawType x = 0.0;1087EXPECT_TRUE(m7.Matches(x));1088x = 0.01f;1089EXPECT_FALSE(m7.Matches(x));1090}10911092// Pre-calculated numbers to be used by the tests.10931094const Bits max_ulps_;10951096const Bits zero_bits_; // The bits that represent 0.0.1097const Bits one_bits_; // The bits that represent 1.0.1098const Bits infinity_bits_; // The bits that represent +infinity.10991100// Some numbers close to 0.0.1101const RawType close_to_positive_zero_;1102const RawType close_to_negative_zero_;1103const RawType further_from_negative_zero_;11041105// Some numbers close to 1.0.1106const RawType close_to_one_;1107const RawType further_from_one_;11081109// Some numbers close to +infinity.1110const RawType infinity_;1111const RawType close_to_infinity_;1112const RawType further_from_infinity_;11131114// Maximum representable value that's not infinity.1115const RawType max_;11161117// Some NaNs.1118const RawType nan1_;1119const RawType nan2_;1120};11211122// Tests floating-point matchers with fixed epsilons.1123template <typename RawType>1124class FloatingPointNearTest : public FloatingPointTest<RawType> {1125protected:1126typedef FloatingPointTest<RawType> ParentType;11271128// A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon.1129// matcher_maker is a pointer to a function which creates a FloatingEqMatcher.1130void TestNearMatches(testing::internal::FloatingEqMatcher<RawType> (1131*matcher_maker)(RawType, RawType)) {1132Matcher<RawType> m1 = matcher_maker(0.0, 0.0);1133EXPECT_TRUE(m1.Matches(0.0));1134EXPECT_TRUE(m1.Matches(-0.0));1135EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_));1136EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_));1137EXPECT_FALSE(m1.Matches(1.0));11381139Matcher<RawType> m2 = matcher_maker(0.0, 1.0);1140EXPECT_TRUE(m2.Matches(0.0));1141EXPECT_TRUE(m2.Matches(-0.0));1142EXPECT_TRUE(m2.Matches(1.0));1143EXPECT_TRUE(m2.Matches(-1.0));1144EXPECT_FALSE(m2.Matches(ParentType::close_to_one_));1145EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_));11461147// Check that inf matches inf, regardless of the of the specified max1148// absolute error.1149Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0);1150EXPECT_TRUE(m3.Matches(ParentType::infinity_));1151EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_));1152EXPECT_FALSE(m3.Matches(-ParentType::infinity_));11531154Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0);1155EXPECT_TRUE(m4.Matches(-ParentType::infinity_));1156EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_));1157EXPECT_FALSE(m4.Matches(ParentType::infinity_));11581159// Test various overflow scenarios.1160Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_);1161EXPECT_TRUE(m5.Matches(ParentType::max_));1162EXPECT_FALSE(m5.Matches(-ParentType::max_));11631164Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_);1165EXPECT_FALSE(m6.Matches(ParentType::max_));1166EXPECT_TRUE(m6.Matches(-ParentType::max_));11671168Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0);1169EXPECT_TRUE(m7.Matches(ParentType::max_));1170EXPECT_FALSE(m7.Matches(-ParentType::max_));11711172Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0);1173EXPECT_FALSE(m8.Matches(ParentType::max_));1174EXPECT_TRUE(m8.Matches(-ParentType::max_));11751176// The difference between max() and -max() normally overflows to infinity,1177// but it should still match if the max_abs_error is also infinity.1178Matcher<RawType> m9 =1179matcher_maker(ParentType::max_, ParentType::infinity_);1180EXPECT_TRUE(m8.Matches(-ParentType::max_));11811182// matcher_maker can produce a Matcher<const RawType&>, which is needed in1183// some cases.1184Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);1185EXPECT_TRUE(m10.Matches(-0.0));1186EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_));1187EXPECT_FALSE(m10.Matches(ParentType::close_to_one_));11881189// matcher_maker can produce a Matcher<RawType&>, which is needed in some1190// cases.1191Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);1192RawType x = 0.0;1193EXPECT_TRUE(m11.Matches(x));1194x = 1.0f;1195EXPECT_TRUE(m11.Matches(x));1196x = -1.0f;1197EXPECT_TRUE(m11.Matches(x));1198x = 1.1f;1199EXPECT_FALSE(m11.Matches(x));1200x = -1.1f;1201EXPECT_FALSE(m11.Matches(x));1202}1203};12041205// Instantiate FloatingPointTest for testing floats.1206typedef FloatingPointTest<float> FloatTest;12071208TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { TestMatches(&FloatEq); }12091210TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {1211TestMatches(&NanSensitiveFloatEq);1212}12131214TEST_F(FloatTest, FloatEqCannotMatchNaN) {1215// FloatEq never matches NaN.1216Matcher<float> m = FloatEq(nan1_);1217EXPECT_FALSE(m.Matches(nan1_));1218EXPECT_FALSE(m.Matches(nan2_));1219EXPECT_FALSE(m.Matches(1.0));1220}12211222TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {1223// NanSensitiveFloatEq will match NaN.1224Matcher<float> m = NanSensitiveFloatEq(nan1_);1225EXPECT_TRUE(m.Matches(nan1_));1226EXPECT_TRUE(m.Matches(nan2_));1227EXPECT_FALSE(m.Matches(1.0));1228}12291230TEST_F(FloatTest, FloatEqCanDescribeSelf) {1231Matcher<float> m1 = FloatEq(2.0f);1232EXPECT_EQ("is approximately 2", Describe(m1));1233EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));12341235Matcher<float> m2 = FloatEq(0.5f);1236EXPECT_EQ("is approximately 0.5", Describe(m2));1237EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));12381239Matcher<float> m3 = FloatEq(nan1_);1240EXPECT_EQ("never matches", Describe(m3));1241EXPECT_EQ("is anything", DescribeNegation(m3));1242}12431244TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {1245Matcher<float> m1 = NanSensitiveFloatEq(2.0f);1246EXPECT_EQ("is approximately 2", Describe(m1));1247EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));12481249Matcher<float> m2 = NanSensitiveFloatEq(0.5f);1250EXPECT_EQ("is approximately 0.5", Describe(m2));1251EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));12521253Matcher<float> m3 = NanSensitiveFloatEq(nan1_);1254EXPECT_EQ("is NaN", Describe(m3));1255EXPECT_EQ("isn't NaN", DescribeNegation(m3));1256}12571258// Instantiate FloatingPointTest for testing floats with a user-specified1259// max absolute error.1260typedef FloatingPointNearTest<float> FloatNearTest;12611262TEST_F(FloatNearTest, FloatNearMatches) { TestNearMatches(&FloatNear); }12631264TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {1265TestNearMatches(&NanSensitiveFloatNear);1266}12671268TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {1269Matcher<float> m1 = FloatNear(2.0f, 0.5f);1270EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));1271EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",1272DescribeNegation(m1));12731274Matcher<float> m2 = FloatNear(0.5f, 0.5f);1275EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));1276EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",1277DescribeNegation(m2));12781279Matcher<float> m3 = FloatNear(nan1_, 0.0);1280EXPECT_EQ("never matches", Describe(m3));1281EXPECT_EQ("is anything", DescribeNegation(m3));1282}12831284TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {1285Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f);1286EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));1287EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",1288DescribeNegation(m1));12891290Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f);1291EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));1292EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",1293DescribeNegation(m2));12941295Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f);1296EXPECT_EQ("is NaN", Describe(m3));1297EXPECT_EQ("isn't NaN", DescribeNegation(m3));1298}12991300TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {1301// FloatNear never matches NaN.1302Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f);1303EXPECT_FALSE(m.Matches(nan1_));1304EXPECT_FALSE(m.Matches(nan2_));1305EXPECT_FALSE(m.Matches(1.0));1306}13071308TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {1309// NanSensitiveFloatNear will match NaN.1310Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f);1311EXPECT_TRUE(m.Matches(nan1_));1312EXPECT_TRUE(m.Matches(nan2_));1313EXPECT_FALSE(m.Matches(1.0));1314}13151316// Instantiate FloatingPointTest for testing doubles.1317typedef FloatingPointTest<double> DoubleTest;13181319TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {1320TestMatches(&DoubleEq);1321}13221323TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {1324TestMatches(&NanSensitiveDoubleEq);1325}13261327TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {1328// DoubleEq never matches NaN.1329Matcher<double> m = DoubleEq(nan1_);1330EXPECT_FALSE(m.Matches(nan1_));1331EXPECT_FALSE(m.Matches(nan2_));1332EXPECT_FALSE(m.Matches(1.0));1333}13341335TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {1336// NanSensitiveDoubleEq will match NaN.1337Matcher<double> m = NanSensitiveDoubleEq(nan1_);1338EXPECT_TRUE(m.Matches(nan1_));1339EXPECT_TRUE(m.Matches(nan2_));1340EXPECT_FALSE(m.Matches(1.0));1341}13421343TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {1344Matcher<double> m1 = DoubleEq(2.0);1345EXPECT_EQ("is approximately 2", Describe(m1));1346EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));13471348Matcher<double> m2 = DoubleEq(0.5);1349EXPECT_EQ("is approximately 0.5", Describe(m2));1350EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));13511352Matcher<double> m3 = DoubleEq(nan1_);1353EXPECT_EQ("never matches", Describe(m3));1354EXPECT_EQ("is anything", DescribeNegation(m3));1355}13561357TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {1358Matcher<double> m1 = NanSensitiveDoubleEq(2.0);1359EXPECT_EQ("is approximately 2", Describe(m1));1360EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));13611362Matcher<double> m2 = NanSensitiveDoubleEq(0.5);1363EXPECT_EQ("is approximately 0.5", Describe(m2));1364EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));13651366Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);1367EXPECT_EQ("is NaN", Describe(m3));1368EXPECT_EQ("isn't NaN", DescribeNegation(m3));1369}13701371// Instantiate FloatingPointTest for testing floats with a user-specified1372// max absolute error.1373typedef FloatingPointNearTest<double> DoubleNearTest;13741375TEST_F(DoubleNearTest, DoubleNearMatches) { TestNearMatches(&DoubleNear); }13761377TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {1378TestNearMatches(&NanSensitiveDoubleNear);1379}13801381TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {1382Matcher<double> m1 = DoubleNear(2.0, 0.5);1383EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));1384EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",1385DescribeNegation(m1));13861387Matcher<double> m2 = DoubleNear(0.5, 0.5);1388EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));1389EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",1390DescribeNegation(m2));13911392Matcher<double> m3 = DoubleNear(nan1_, 0.0);1393EXPECT_EQ("never matches", Describe(m3));1394EXPECT_EQ("is anything", DescribeNegation(m3));1395}13961397TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {1398EXPECT_EQ("", Explain(DoubleNear(2.0, 0.1), 2.05));1399EXPECT_EQ("which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2));1400EXPECT_EQ("which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7));14011402const std::string explanation =1403Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);1404// Different C++ implementations may print floating-point numbers1405// slightly differently.1406EXPECT_TRUE(explanation == "which is 1.2e-10 from 2.1" || // GCC1407explanation == "which is 1.2e-010 from 2.1") // MSVC1408<< " where explanation is \"" << explanation << "\".";1409}14101411TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {1412Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);1413EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));1414EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",1415DescribeNegation(m1));14161417Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);1418EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));1419EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",1420DescribeNegation(m2));14211422Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1);1423EXPECT_EQ("is NaN", Describe(m3));1424EXPECT_EQ("isn't NaN", DescribeNegation(m3));1425}14261427TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {1428// DoubleNear never matches NaN.1429Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1);1430EXPECT_FALSE(m.Matches(nan1_));1431EXPECT_FALSE(m.Matches(nan2_));1432EXPECT_FALSE(m.Matches(1.0));1433}14341435TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {1436// NanSensitiveDoubleNear will match NaN.1437Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1);1438EXPECT_TRUE(m.Matches(nan1_));1439EXPECT_TRUE(m.Matches(nan2_));1440EXPECT_FALSE(m.Matches(1.0));1441}14421443TEST(NotTest, WorksOnMoveOnlyType) {1444std::unique_ptr<int> p(new int(3));1445EXPECT_THAT(p, Pointee(Eq(3)));1446EXPECT_THAT(p, Not(Pointee(Eq(2))));1447}14481449TEST(AllOfTest, HugeMatcher) {1450// Verify that using AllOf with many arguments doesn't cause1451// the compiler to exceed template instantiation depth limit.1452EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,1453testing::AllOf(_, _, _, _, _, _, _, _, _, _)));1454}14551456TEST(AnyOfTest, HugeMatcher) {1457// Verify that using AnyOf with many arguments doesn't cause1458// the compiler to exceed template instantiation depth limit.1459EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,1460testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));1461}14621463namespace adl_test {14641465// Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf1466// don't issue unqualified recursive calls. If they do, the argument dependent1467// name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found1468// as a candidate and the compilation will break due to an ambiguous overload.14691470// The matcher must be in the same namespace as AllOf/AnyOf to make argument1471// dependent lookup find those.1472MATCHER(M, "") {1473(void)arg;1474return true;1475}14761477template <typename T1, typename T2>1478bool AllOf(const T1& /*t1*/, const T2& /*t2*/) {1479return true;1480}14811482TEST(AllOfTest, DoesNotCallAllOfUnqualified) {1483EXPECT_THAT(42,1484testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));1485}14861487template <typename T1, typename T2>1488bool AnyOf(const T1&, const T2&) {1489return true;1490}14911492TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {1493EXPECT_THAT(42,1494testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));1495}14961497} // namespace adl_test14981499TEST(AllOfTest, WorksOnMoveOnlyType) {1500std::unique_ptr<int> p(new int(3));1501EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));1502EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));1503}15041505TEST(AnyOfTest, WorksOnMoveOnlyType) {1506std::unique_ptr<int> p(new int(3));1507EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));1508EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));1509}15101511} // namespace1512} // namespace gmock_matchers_test1513} // namespace testing15141515GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100151615171518