1f92157deSopenharmony_ci// Copyright 2007, Google Inc. 2f92157deSopenharmony_ci// All rights reserved. 3f92157deSopenharmony_ci// 4f92157deSopenharmony_ci// Redistribution and use in source and binary forms, with or without 5f92157deSopenharmony_ci// modification, are permitted provided that the following conditions are 6f92157deSopenharmony_ci// met: 7f92157deSopenharmony_ci// 8f92157deSopenharmony_ci// * Redistributions of source code must retain the above copyright 9f92157deSopenharmony_ci// notice, this list of conditions and the following disclaimer. 10f92157deSopenharmony_ci// * Redistributions in binary form must reproduce the above 11f92157deSopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 12f92157deSopenharmony_ci// in the documentation and/or other materials provided with the 13f92157deSopenharmony_ci// distribution. 14f92157deSopenharmony_ci// * Neither the name of Google Inc. nor the names of its 15f92157deSopenharmony_ci// contributors may be used to endorse or promote products derived from 16f92157deSopenharmony_ci// this software without specific prior written permission. 17f92157deSopenharmony_ci// 18f92157deSopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19f92157deSopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20f92157deSopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21f92157deSopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22f92157deSopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23f92157deSopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24f92157deSopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25f92157deSopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26f92157deSopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27f92157deSopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28f92157deSopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29f92157deSopenharmony_ci 30f92157deSopenharmony_ci// Google Mock - a framework for writing C++ mock classes. 31f92157deSopenharmony_ci// 32f92157deSopenharmony_ci// This file tests some commonly used argument matchers. 33f92157deSopenharmony_ci 34f92157deSopenharmony_ci// Silence warning C4244: 'initializing': conversion from 'int' to 'short', 35f92157deSopenharmony_ci// possible loss of data and C4100, unreferenced local parameter 36f92157deSopenharmony_ci#ifdef _MSC_VER 37f92157deSopenharmony_ci#pragma warning(push) 38f92157deSopenharmony_ci#pragma warning(disable : 4244) 39f92157deSopenharmony_ci#pragma warning(disable : 4100) 40f92157deSopenharmony_ci#endif 41f92157deSopenharmony_ci 42f92157deSopenharmony_ci#include "test/gmock-matchers_test.h" 43f92157deSopenharmony_ci 44f92157deSopenharmony_cinamespace testing { 45f92157deSopenharmony_cinamespace gmock_matchers_test { 46f92157deSopenharmony_cinamespace { 47f92157deSopenharmony_ci 48f92157deSopenharmony_citypedef ::std::tuple<long, int> Tuple2; // NOLINT 49f92157deSopenharmony_ci 50f92157deSopenharmony_ci// Tests that Eq() matches a 2-tuple where the first field == the 51f92157deSopenharmony_ci// second field. 52f92157deSopenharmony_ciTEST(Eq2Test, MatchesEqualArguments) { 53f92157deSopenharmony_ci Matcher<const Tuple2&> m = Eq(); 54f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); 55f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); 56f92157deSopenharmony_ci} 57f92157deSopenharmony_ci 58f92157deSopenharmony_ci// Tests that Eq() describes itself properly. 59f92157deSopenharmony_ciTEST(Eq2Test, CanDescribeSelf) { 60f92157deSopenharmony_ci Matcher<const Tuple2&> m = Eq(); 61f92157deSopenharmony_ci EXPECT_EQ("are an equal pair", Describe(m)); 62f92157deSopenharmony_ci} 63f92157deSopenharmony_ci 64f92157deSopenharmony_ci// Tests that Ge() matches a 2-tuple where the first field >= the 65f92157deSopenharmony_ci// second field. 66f92157deSopenharmony_ciTEST(Ge2Test, MatchesGreaterThanOrEqualArguments) { 67f92157deSopenharmony_ci Matcher<const Tuple2&> m = Ge(); 68f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); 69f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); 70f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); 71f92157deSopenharmony_ci} 72f92157deSopenharmony_ci 73f92157deSopenharmony_ci// Tests that Ge() describes itself properly. 74f92157deSopenharmony_ciTEST(Ge2Test, CanDescribeSelf) { 75f92157deSopenharmony_ci Matcher<const Tuple2&> m = Ge(); 76f92157deSopenharmony_ci EXPECT_EQ("are a pair where the first >= the second", Describe(m)); 77f92157deSopenharmony_ci} 78f92157deSopenharmony_ci 79f92157deSopenharmony_ci// Tests that Gt() matches a 2-tuple where the first field > the 80f92157deSopenharmony_ci// second field. 81f92157deSopenharmony_ciTEST(Gt2Test, MatchesGreaterThanArguments) { 82f92157deSopenharmony_ci Matcher<const Tuple2&> m = Gt(); 83f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); 84f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); 85f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); 86f92157deSopenharmony_ci} 87f92157deSopenharmony_ci 88f92157deSopenharmony_ci// Tests that Gt() describes itself properly. 89f92157deSopenharmony_ciTEST(Gt2Test, CanDescribeSelf) { 90f92157deSopenharmony_ci Matcher<const Tuple2&> m = Gt(); 91f92157deSopenharmony_ci EXPECT_EQ("are a pair where the first > the second", Describe(m)); 92f92157deSopenharmony_ci} 93f92157deSopenharmony_ci 94f92157deSopenharmony_ci// Tests that Le() matches a 2-tuple where the first field <= the 95f92157deSopenharmony_ci// second field. 96f92157deSopenharmony_ciTEST(Le2Test, MatchesLessThanOrEqualArguments) { 97f92157deSopenharmony_ci Matcher<const Tuple2&> m = Le(); 98f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); 99f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); 100f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); 101f92157deSopenharmony_ci} 102f92157deSopenharmony_ci 103f92157deSopenharmony_ci// Tests that Le() describes itself properly. 104f92157deSopenharmony_ciTEST(Le2Test, CanDescribeSelf) { 105f92157deSopenharmony_ci Matcher<const Tuple2&> m = Le(); 106f92157deSopenharmony_ci EXPECT_EQ("are a pair where the first <= the second", Describe(m)); 107f92157deSopenharmony_ci} 108f92157deSopenharmony_ci 109f92157deSopenharmony_ci// Tests that Lt() matches a 2-tuple where the first field < the 110f92157deSopenharmony_ci// second field. 111f92157deSopenharmony_ciTEST(Lt2Test, MatchesLessThanArguments) { 112f92157deSopenharmony_ci Matcher<const Tuple2&> m = Lt(); 113f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); 114f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); 115f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); 116f92157deSopenharmony_ci} 117f92157deSopenharmony_ci 118f92157deSopenharmony_ci// Tests that Lt() describes itself properly. 119f92157deSopenharmony_ciTEST(Lt2Test, CanDescribeSelf) { 120f92157deSopenharmony_ci Matcher<const Tuple2&> m = Lt(); 121f92157deSopenharmony_ci EXPECT_EQ("are a pair where the first < the second", Describe(m)); 122f92157deSopenharmony_ci} 123f92157deSopenharmony_ci 124f92157deSopenharmony_ci// Tests that Ne() matches a 2-tuple where the first field != the 125f92157deSopenharmony_ci// second field. 126f92157deSopenharmony_ciTEST(Ne2Test, MatchesUnequalArguments) { 127f92157deSopenharmony_ci Matcher<const Tuple2&> m = Ne(); 128f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); 129f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); 130f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); 131f92157deSopenharmony_ci} 132f92157deSopenharmony_ci 133f92157deSopenharmony_ci// Tests that Ne() describes itself properly. 134f92157deSopenharmony_ciTEST(Ne2Test, CanDescribeSelf) { 135f92157deSopenharmony_ci Matcher<const Tuple2&> m = Ne(); 136f92157deSopenharmony_ci EXPECT_EQ("are an unequal pair", Describe(m)); 137f92157deSopenharmony_ci} 138f92157deSopenharmony_ci 139f92157deSopenharmony_ciTEST(PairMatchBaseTest, WorksWithMoveOnly) { 140f92157deSopenharmony_ci using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>; 141f92157deSopenharmony_ci Matcher<Pointers> matcher = Eq(); 142f92157deSopenharmony_ci Pointers pointers; 143f92157deSopenharmony_ci // Tested values don't matter; the point is that matcher does not copy the 144f92157deSopenharmony_ci // matched values. 145f92157deSopenharmony_ci EXPECT_TRUE(matcher.Matches(pointers)); 146f92157deSopenharmony_ci} 147f92157deSopenharmony_ci 148f92157deSopenharmony_ci// Tests that IsNan() matches a NaN, with float. 149f92157deSopenharmony_ciTEST(IsNan, FloatMatchesNan) { 150f92157deSopenharmony_ci float quiet_nan = std::numeric_limits<float>::quiet_NaN(); 151f92157deSopenharmony_ci float other_nan = std::nanf("1"); 152f92157deSopenharmony_ci float real_value = 1.0f; 153f92157deSopenharmony_ci 154f92157deSopenharmony_ci Matcher<float> m = IsNan(); 155f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(quiet_nan)); 156f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(other_nan)); 157f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(real_value)); 158f92157deSopenharmony_ci 159f92157deSopenharmony_ci Matcher<float&> m_ref = IsNan(); 160f92157deSopenharmony_ci EXPECT_TRUE(m_ref.Matches(quiet_nan)); 161f92157deSopenharmony_ci EXPECT_TRUE(m_ref.Matches(other_nan)); 162f92157deSopenharmony_ci EXPECT_FALSE(m_ref.Matches(real_value)); 163f92157deSopenharmony_ci 164f92157deSopenharmony_ci Matcher<const float&> m_cref = IsNan(); 165f92157deSopenharmony_ci EXPECT_TRUE(m_cref.Matches(quiet_nan)); 166f92157deSopenharmony_ci EXPECT_TRUE(m_cref.Matches(other_nan)); 167f92157deSopenharmony_ci EXPECT_FALSE(m_cref.Matches(real_value)); 168f92157deSopenharmony_ci} 169f92157deSopenharmony_ci 170f92157deSopenharmony_ci// Tests that IsNan() matches a NaN, with double. 171f92157deSopenharmony_ciTEST(IsNan, DoubleMatchesNan) { 172f92157deSopenharmony_ci double quiet_nan = std::numeric_limits<double>::quiet_NaN(); 173f92157deSopenharmony_ci double other_nan = std::nan("1"); 174f92157deSopenharmony_ci double real_value = 1.0; 175f92157deSopenharmony_ci 176f92157deSopenharmony_ci Matcher<double> m = IsNan(); 177f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(quiet_nan)); 178f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(other_nan)); 179f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(real_value)); 180f92157deSopenharmony_ci 181f92157deSopenharmony_ci Matcher<double&> m_ref = IsNan(); 182f92157deSopenharmony_ci EXPECT_TRUE(m_ref.Matches(quiet_nan)); 183f92157deSopenharmony_ci EXPECT_TRUE(m_ref.Matches(other_nan)); 184f92157deSopenharmony_ci EXPECT_FALSE(m_ref.Matches(real_value)); 185f92157deSopenharmony_ci 186f92157deSopenharmony_ci Matcher<const double&> m_cref = IsNan(); 187f92157deSopenharmony_ci EXPECT_TRUE(m_cref.Matches(quiet_nan)); 188f92157deSopenharmony_ci EXPECT_TRUE(m_cref.Matches(other_nan)); 189f92157deSopenharmony_ci EXPECT_FALSE(m_cref.Matches(real_value)); 190f92157deSopenharmony_ci} 191f92157deSopenharmony_ci 192f92157deSopenharmony_ci// Tests that IsNan() matches a NaN, with long double. 193f92157deSopenharmony_ciTEST(IsNan, LongDoubleMatchesNan) { 194f92157deSopenharmony_ci long double quiet_nan = std::numeric_limits<long double>::quiet_NaN(); 195f92157deSopenharmony_ci long double other_nan = std::nan("1"); 196f92157deSopenharmony_ci long double real_value = 1.0; 197f92157deSopenharmony_ci 198f92157deSopenharmony_ci Matcher<long double> m = IsNan(); 199f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(quiet_nan)); 200f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(other_nan)); 201f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(real_value)); 202f92157deSopenharmony_ci 203f92157deSopenharmony_ci Matcher<long double&> m_ref = IsNan(); 204f92157deSopenharmony_ci EXPECT_TRUE(m_ref.Matches(quiet_nan)); 205f92157deSopenharmony_ci EXPECT_TRUE(m_ref.Matches(other_nan)); 206f92157deSopenharmony_ci EXPECT_FALSE(m_ref.Matches(real_value)); 207f92157deSopenharmony_ci 208f92157deSopenharmony_ci Matcher<const long double&> m_cref = IsNan(); 209f92157deSopenharmony_ci EXPECT_TRUE(m_cref.Matches(quiet_nan)); 210f92157deSopenharmony_ci EXPECT_TRUE(m_cref.Matches(other_nan)); 211f92157deSopenharmony_ci EXPECT_FALSE(m_cref.Matches(real_value)); 212f92157deSopenharmony_ci} 213f92157deSopenharmony_ci 214f92157deSopenharmony_ci// Tests that IsNan() works with Not. 215f92157deSopenharmony_ciTEST(IsNan, NotMatchesNan) { 216f92157deSopenharmony_ci Matcher<float> mf = Not(IsNan()); 217f92157deSopenharmony_ci EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN())); 218f92157deSopenharmony_ci EXPECT_FALSE(mf.Matches(std::nanf("1"))); 219f92157deSopenharmony_ci EXPECT_TRUE(mf.Matches(1.0)); 220f92157deSopenharmony_ci 221f92157deSopenharmony_ci Matcher<double> md = Not(IsNan()); 222f92157deSopenharmony_ci EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN())); 223f92157deSopenharmony_ci EXPECT_FALSE(md.Matches(std::nan("1"))); 224f92157deSopenharmony_ci EXPECT_TRUE(md.Matches(1.0)); 225f92157deSopenharmony_ci 226f92157deSopenharmony_ci Matcher<long double> mld = Not(IsNan()); 227f92157deSopenharmony_ci EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN())); 228f92157deSopenharmony_ci EXPECT_FALSE(mld.Matches(std::nanl("1"))); 229f92157deSopenharmony_ci EXPECT_TRUE(mld.Matches(1.0)); 230f92157deSopenharmony_ci} 231f92157deSopenharmony_ci 232f92157deSopenharmony_ci// Tests that IsNan() can describe itself. 233f92157deSopenharmony_ciTEST(IsNan, CanDescribeSelf) { 234f92157deSopenharmony_ci Matcher<float> mf = IsNan(); 235f92157deSopenharmony_ci EXPECT_EQ("is NaN", Describe(mf)); 236f92157deSopenharmony_ci 237f92157deSopenharmony_ci Matcher<double> md = IsNan(); 238f92157deSopenharmony_ci EXPECT_EQ("is NaN", Describe(md)); 239f92157deSopenharmony_ci 240f92157deSopenharmony_ci Matcher<long double> mld = IsNan(); 241f92157deSopenharmony_ci EXPECT_EQ("is NaN", Describe(mld)); 242f92157deSopenharmony_ci} 243f92157deSopenharmony_ci 244f92157deSopenharmony_ci// Tests that IsNan() can describe itself with Not. 245f92157deSopenharmony_ciTEST(IsNan, CanDescribeSelfWithNot) { 246f92157deSopenharmony_ci Matcher<float> mf = Not(IsNan()); 247f92157deSopenharmony_ci EXPECT_EQ("isn't NaN", Describe(mf)); 248f92157deSopenharmony_ci 249f92157deSopenharmony_ci Matcher<double> md = Not(IsNan()); 250f92157deSopenharmony_ci EXPECT_EQ("isn't NaN", Describe(md)); 251f92157deSopenharmony_ci 252f92157deSopenharmony_ci Matcher<long double> mld = Not(IsNan()); 253f92157deSopenharmony_ci EXPECT_EQ("isn't NaN", Describe(mld)); 254f92157deSopenharmony_ci} 255f92157deSopenharmony_ci 256f92157deSopenharmony_ci// Tests that FloatEq() matches a 2-tuple where 257f92157deSopenharmony_ci// FloatEq(first field) matches the second field. 258f92157deSopenharmony_ciTEST(FloatEq2Test, MatchesEqualArguments) { 259f92157deSopenharmony_ci typedef ::std::tuple<float, float> Tpl; 260f92157deSopenharmony_ci Matcher<const Tpl&> m = FloatEq(); 261f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); 262f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f))); 263f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f))); 264f92157deSopenharmony_ci} 265f92157deSopenharmony_ci 266f92157deSopenharmony_ci// Tests that FloatEq() describes itself properly. 267f92157deSopenharmony_ciTEST(FloatEq2Test, CanDescribeSelf) { 268f92157deSopenharmony_ci Matcher<const ::std::tuple<float, float>&> m = FloatEq(); 269f92157deSopenharmony_ci EXPECT_EQ("are an almost-equal pair", Describe(m)); 270f92157deSopenharmony_ci} 271f92157deSopenharmony_ci 272f92157deSopenharmony_ci// Tests that NanSensitiveFloatEq() matches a 2-tuple where 273f92157deSopenharmony_ci// NanSensitiveFloatEq(first field) matches the second field. 274f92157deSopenharmony_ciTEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) { 275f92157deSopenharmony_ci typedef ::std::tuple<float, float> Tpl; 276f92157deSopenharmony_ci Matcher<const Tpl&> m = NanSensitiveFloatEq(); 277f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); 278f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 279f92157deSopenharmony_ci std::numeric_limits<float>::quiet_NaN()))); 280f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f))); 281f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN()))); 282f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f))); 283f92157deSopenharmony_ci} 284f92157deSopenharmony_ci 285f92157deSopenharmony_ci// Tests that NanSensitiveFloatEq() describes itself properly. 286f92157deSopenharmony_ciTEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) { 287f92157deSopenharmony_ci Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq(); 288f92157deSopenharmony_ci EXPECT_EQ("are an almost-equal pair", Describe(m)); 289f92157deSopenharmony_ci} 290f92157deSopenharmony_ci 291f92157deSopenharmony_ci// Tests that DoubleEq() matches a 2-tuple where 292f92157deSopenharmony_ci// DoubleEq(first field) matches the second field. 293f92157deSopenharmony_ciTEST(DoubleEq2Test, MatchesEqualArguments) { 294f92157deSopenharmony_ci typedef ::std::tuple<double, double> Tpl; 295f92157deSopenharmony_ci Matcher<const Tpl&> m = DoubleEq(); 296f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0))); 297f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1))); 298f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.1, 1.0))); 299f92157deSopenharmony_ci} 300f92157deSopenharmony_ci 301f92157deSopenharmony_ci// Tests that DoubleEq() describes itself properly. 302f92157deSopenharmony_ciTEST(DoubleEq2Test, CanDescribeSelf) { 303f92157deSopenharmony_ci Matcher<const ::std::tuple<double, double>&> m = DoubleEq(); 304f92157deSopenharmony_ci EXPECT_EQ("are an almost-equal pair", Describe(m)); 305f92157deSopenharmony_ci} 306f92157deSopenharmony_ci 307f92157deSopenharmony_ci// Tests that NanSensitiveDoubleEq() matches a 2-tuple where 308f92157deSopenharmony_ci// NanSensitiveDoubleEq(first field) matches the second field. 309f92157deSopenharmony_ciTEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) { 310f92157deSopenharmony_ci typedef ::std::tuple<double, double> Tpl; 311f92157deSopenharmony_ci Matcher<const Tpl&> m = NanSensitiveDoubleEq(); 312f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); 313f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 314f92157deSopenharmony_ci std::numeric_limits<double>::quiet_NaN()))); 315f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f))); 316f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN()))); 317f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f))); 318f92157deSopenharmony_ci} 319f92157deSopenharmony_ci 320f92157deSopenharmony_ci// Tests that DoubleEq() describes itself properly. 321f92157deSopenharmony_ciTEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) { 322f92157deSopenharmony_ci Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq(); 323f92157deSopenharmony_ci EXPECT_EQ("are an almost-equal pair", Describe(m)); 324f92157deSopenharmony_ci} 325f92157deSopenharmony_ci 326f92157deSopenharmony_ci// Tests that FloatEq() matches a 2-tuple where 327f92157deSopenharmony_ci// FloatNear(first field, max_abs_error) matches the second field. 328f92157deSopenharmony_ciTEST(FloatNear2Test, MatchesEqualArguments) { 329f92157deSopenharmony_ci typedef ::std::tuple<float, float> Tpl; 330f92157deSopenharmony_ci Matcher<const Tpl&> m = FloatNear(0.5f); 331f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); 332f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.3f, 1.0f))); 333f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.8f, 1.0f))); 334f92157deSopenharmony_ci} 335f92157deSopenharmony_ci 336f92157deSopenharmony_ci// Tests that FloatNear() describes itself properly. 337f92157deSopenharmony_ciTEST(FloatNear2Test, CanDescribeSelf) { 338f92157deSopenharmony_ci Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f); 339f92157deSopenharmony_ci EXPECT_EQ("are an almost-equal pair", Describe(m)); 340f92157deSopenharmony_ci} 341f92157deSopenharmony_ci 342f92157deSopenharmony_ci// Tests that NanSensitiveFloatNear() matches a 2-tuple where 343f92157deSopenharmony_ci// NanSensitiveFloatNear(first field) matches the second field. 344f92157deSopenharmony_ciTEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) { 345f92157deSopenharmony_ci typedef ::std::tuple<float, float> Tpl; 346f92157deSopenharmony_ci Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f); 347f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); 348f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f))); 349f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 350f92157deSopenharmony_ci std::numeric_limits<float>::quiet_NaN()))); 351f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f))); 352f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN()))); 353f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f))); 354f92157deSopenharmony_ci} 355f92157deSopenharmony_ci 356f92157deSopenharmony_ci// Tests that NanSensitiveFloatNear() describes itself properly. 357f92157deSopenharmony_ciTEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) { 358f92157deSopenharmony_ci Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f); 359f92157deSopenharmony_ci EXPECT_EQ("are an almost-equal pair", Describe(m)); 360f92157deSopenharmony_ci} 361f92157deSopenharmony_ci 362f92157deSopenharmony_ci// Tests that FloatEq() matches a 2-tuple where 363f92157deSopenharmony_ci// DoubleNear(first field, max_abs_error) matches the second field. 364f92157deSopenharmony_ciTEST(DoubleNear2Test, MatchesEqualArguments) { 365f92157deSopenharmony_ci typedef ::std::tuple<double, double> Tpl; 366f92157deSopenharmony_ci Matcher<const Tpl&> m = DoubleNear(0.5); 367f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0))); 368f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.3, 1.0))); 369f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.8, 1.0))); 370f92157deSopenharmony_ci} 371f92157deSopenharmony_ci 372f92157deSopenharmony_ci// Tests that DoubleNear() describes itself properly. 373f92157deSopenharmony_ciTEST(DoubleNear2Test, CanDescribeSelf) { 374f92157deSopenharmony_ci Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5); 375f92157deSopenharmony_ci EXPECT_EQ("are an almost-equal pair", Describe(m)); 376f92157deSopenharmony_ci} 377f92157deSopenharmony_ci 378f92157deSopenharmony_ci// Tests that NanSensitiveDoubleNear() matches a 2-tuple where 379f92157deSopenharmony_ci// NanSensitiveDoubleNear(first field) matches the second field. 380f92157deSopenharmony_ciTEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) { 381f92157deSopenharmony_ci typedef ::std::tuple<double, double> Tpl; 382f92157deSopenharmony_ci Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f); 383f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); 384f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f))); 385f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 386f92157deSopenharmony_ci std::numeric_limits<double>::quiet_NaN()))); 387f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f))); 388f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN()))); 389f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f))); 390f92157deSopenharmony_ci} 391f92157deSopenharmony_ci 392f92157deSopenharmony_ci// Tests that NanSensitiveDoubleNear() describes itself properly. 393f92157deSopenharmony_ciTEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) { 394f92157deSopenharmony_ci Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f); 395f92157deSopenharmony_ci EXPECT_EQ("are an almost-equal pair", Describe(m)); 396f92157deSopenharmony_ci} 397f92157deSopenharmony_ci 398f92157deSopenharmony_ci// Tests that Not(m) matches any value that doesn't match m. 399f92157deSopenharmony_ciTEST(NotTest, NegatesMatcher) { 400f92157deSopenharmony_ci Matcher<int> m; 401f92157deSopenharmony_ci m = Not(Eq(2)); 402f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(3)); 403f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(2)); 404f92157deSopenharmony_ci} 405f92157deSopenharmony_ci 406f92157deSopenharmony_ci// Tests that Not(m) describes itself properly. 407f92157deSopenharmony_ciTEST(NotTest, CanDescribeSelf) { 408f92157deSopenharmony_ci Matcher<int> m = Not(Eq(5)); 409f92157deSopenharmony_ci EXPECT_EQ("isn't equal to 5", Describe(m)); 410f92157deSopenharmony_ci} 411f92157deSopenharmony_ci 412f92157deSopenharmony_ci// Tests that monomorphic matchers are safely cast by the Not matcher. 413f92157deSopenharmony_ciTEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) { 414f92157deSopenharmony_ci // greater_than_5 is a monomorphic matcher. 415f92157deSopenharmony_ci Matcher<int> greater_than_5 = Gt(5); 416f92157deSopenharmony_ci 417f92157deSopenharmony_ci Matcher<const int&> m = Not(greater_than_5); 418f92157deSopenharmony_ci Matcher<int&> m2 = Not(greater_than_5); 419f92157deSopenharmony_ci Matcher<int&> m3 = Not(m); 420f92157deSopenharmony_ci} 421f92157deSopenharmony_ci 422f92157deSopenharmony_ci// Helper to allow easy testing of AllOf matchers with num parameters. 423f92157deSopenharmony_civoid AllOfMatches(int num, const Matcher<int>& m) { 424f92157deSopenharmony_ci SCOPED_TRACE(Describe(m)); 425f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(0)); 426f92157deSopenharmony_ci for (int i = 1; i <= num; ++i) { 427f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(i)); 428f92157deSopenharmony_ci } 429f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(num + 1)); 430f92157deSopenharmony_ci} 431f92157deSopenharmony_ci 432f92157deSopenharmony_ciINSTANTIATE_GTEST_MATCHER_TEST_P(AllOfTest); 433f92157deSopenharmony_ci 434f92157deSopenharmony_ci// Tests that AllOf(m1, ..., mn) matches any value that matches all of 435f92157deSopenharmony_ci// the given matchers. 436f92157deSopenharmony_ciTEST(AllOfTest, MatchesWhenAllMatch) { 437f92157deSopenharmony_ci Matcher<int> m; 438f92157deSopenharmony_ci m = AllOf(Le(2), Ge(1)); 439f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(1)); 440f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(2)); 441f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(0)); 442f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(3)); 443f92157deSopenharmony_ci 444f92157deSopenharmony_ci m = AllOf(Gt(0), Ne(1), Ne(2)); 445f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(3)); 446f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(2)); 447f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(1)); 448f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(0)); 449f92157deSopenharmony_ci 450f92157deSopenharmony_ci m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); 451f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(4)); 452f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(3)); 453f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(2)); 454f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(1)); 455f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(0)); 456f92157deSopenharmony_ci 457f92157deSopenharmony_ci m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); 458f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(0)); 459f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(1)); 460f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(3)); 461f92157deSopenharmony_ci 462f92157deSopenharmony_ci // The following tests for varying number of sub-matchers. Due to the way 463f92157deSopenharmony_ci // the sub-matchers are handled it is enough to test every sub-matcher once 464f92157deSopenharmony_ci // with sub-matchers using the same matcher type. Varying matcher types are 465f92157deSopenharmony_ci // checked for above. 466f92157deSopenharmony_ci AllOfMatches(2, AllOf(Ne(1), Ne(2))); 467f92157deSopenharmony_ci AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3))); 468f92157deSopenharmony_ci AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4))); 469f92157deSopenharmony_ci AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5))); 470f92157deSopenharmony_ci AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6))); 471f92157deSopenharmony_ci AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7))); 472f92157deSopenharmony_ci AllOfMatches(8, 473f92157deSopenharmony_ci AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8))); 474f92157deSopenharmony_ci AllOfMatches( 475f92157deSopenharmony_ci 9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9))); 476f92157deSopenharmony_ci AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), 477f92157deSopenharmony_ci Ne(9), Ne(10))); 478f92157deSopenharmony_ci AllOfMatches( 479f92157deSopenharmony_ci 50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9), 480f92157deSopenharmony_ci Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17), 481f92157deSopenharmony_ci Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25), 482f92157deSopenharmony_ci Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33), 483f92157deSopenharmony_ci Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41), 484f92157deSopenharmony_ci Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49), 485f92157deSopenharmony_ci Ne(50))); 486f92157deSopenharmony_ci} 487f92157deSopenharmony_ci 488f92157deSopenharmony_ci// Tests that AllOf(m1, ..., mn) describes itself properly. 489f92157deSopenharmony_ciTEST(AllOfTest, CanDescribeSelf) { 490f92157deSopenharmony_ci Matcher<int> m; 491f92157deSopenharmony_ci m = AllOf(Le(2), Ge(1)); 492f92157deSopenharmony_ci EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m)); 493f92157deSopenharmony_ci 494f92157deSopenharmony_ci m = AllOf(Gt(0), Ne(1), Ne(2)); 495f92157deSopenharmony_ci std::string expected_descr1 = 496f92157deSopenharmony_ci "(is > 0) and (isn't equal to 1) and (isn't equal to 2)"; 497f92157deSopenharmony_ci EXPECT_EQ(expected_descr1, Describe(m)); 498f92157deSopenharmony_ci 499f92157deSopenharmony_ci m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); 500f92157deSopenharmony_ci std::string expected_descr2 = 501f92157deSopenharmony_ci "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal " 502f92157deSopenharmony_ci "to 3)"; 503f92157deSopenharmony_ci EXPECT_EQ(expected_descr2, Describe(m)); 504f92157deSopenharmony_ci 505f92157deSopenharmony_ci m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); 506f92157deSopenharmony_ci std::string expected_descr3 = 507f92157deSopenharmony_ci "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) " 508f92157deSopenharmony_ci "and (isn't equal to 7)"; 509f92157deSopenharmony_ci EXPECT_EQ(expected_descr3, Describe(m)); 510f92157deSopenharmony_ci} 511f92157deSopenharmony_ci 512f92157deSopenharmony_ci// Tests that AllOf(m1, ..., mn) describes its negation properly. 513f92157deSopenharmony_ciTEST(AllOfTest, CanDescribeNegation) { 514f92157deSopenharmony_ci Matcher<int> m; 515f92157deSopenharmony_ci m = AllOf(Le(2), Ge(1)); 516f92157deSopenharmony_ci std::string expected_descr4 = "(isn't <= 2) or (isn't >= 1)"; 517f92157deSopenharmony_ci EXPECT_EQ(expected_descr4, DescribeNegation(m)); 518f92157deSopenharmony_ci 519f92157deSopenharmony_ci m = AllOf(Gt(0), Ne(1), Ne(2)); 520f92157deSopenharmony_ci std::string expected_descr5 = 521f92157deSopenharmony_ci "(isn't > 0) or (is equal to 1) or (is equal to 2)"; 522f92157deSopenharmony_ci EXPECT_EQ(expected_descr5, DescribeNegation(m)); 523f92157deSopenharmony_ci 524f92157deSopenharmony_ci m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); 525f92157deSopenharmony_ci std::string expected_descr6 = 526f92157deSopenharmony_ci "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)"; 527f92157deSopenharmony_ci EXPECT_EQ(expected_descr6, DescribeNegation(m)); 528f92157deSopenharmony_ci 529f92157deSopenharmony_ci m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); 530f92157deSopenharmony_ci std::string expected_desr7 = 531f92157deSopenharmony_ci "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or " 532f92157deSopenharmony_ci "(is equal to 7)"; 533f92157deSopenharmony_ci EXPECT_EQ(expected_desr7, DescribeNegation(m)); 534f92157deSopenharmony_ci 535f92157deSopenharmony_ci m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9), 536f92157deSopenharmony_ci Ne(10), Ne(11)); 537f92157deSopenharmony_ci AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); 538f92157deSopenharmony_ci EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11)")); 539f92157deSopenharmony_ci AllOfMatches(11, m); 540f92157deSopenharmony_ci} 541f92157deSopenharmony_ci 542f92157deSopenharmony_ci// Tests that monomorphic matchers are safely cast by the AllOf matcher. 543f92157deSopenharmony_ciTEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) { 544f92157deSopenharmony_ci // greater_than_5 and less_than_10 are monomorphic matchers. 545f92157deSopenharmony_ci Matcher<int> greater_than_5 = Gt(5); 546f92157deSopenharmony_ci Matcher<int> less_than_10 = Lt(10); 547f92157deSopenharmony_ci 548f92157deSopenharmony_ci Matcher<const int&> m = AllOf(greater_than_5, less_than_10); 549f92157deSopenharmony_ci Matcher<int&> m2 = AllOf(greater_than_5, less_than_10); 550f92157deSopenharmony_ci Matcher<int&> m3 = AllOf(greater_than_5, m2); 551f92157deSopenharmony_ci 552f92157deSopenharmony_ci // Tests that BothOf works when composing itself. 553f92157deSopenharmony_ci Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10); 554f92157deSopenharmony_ci Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10); 555f92157deSopenharmony_ci} 556f92157deSopenharmony_ci 557f92157deSopenharmony_ciTEST_P(AllOfTestP, ExplainsResult) { 558f92157deSopenharmony_ci Matcher<int> m; 559f92157deSopenharmony_ci 560f92157deSopenharmony_ci // Successful match. Both matchers need to explain. The second 561f92157deSopenharmony_ci // matcher doesn't give an explanation, so only the first matcher's 562f92157deSopenharmony_ci // explanation is printed. 563f92157deSopenharmony_ci m = AllOf(GreaterThan(10), Lt(30)); 564f92157deSopenharmony_ci EXPECT_EQ("which is 15 more than 10", Explain(m, 25)); 565f92157deSopenharmony_ci 566f92157deSopenharmony_ci // Successful match. Both matchers need to explain. 567f92157deSopenharmony_ci m = AllOf(GreaterThan(10), GreaterThan(20)); 568f92157deSopenharmony_ci EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20", 569f92157deSopenharmony_ci Explain(m, 30)); 570f92157deSopenharmony_ci 571f92157deSopenharmony_ci // Successful match. All matchers need to explain. The second 572f92157deSopenharmony_ci // matcher doesn't given an explanation. 573f92157deSopenharmony_ci m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20)); 574f92157deSopenharmony_ci EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20", 575f92157deSopenharmony_ci Explain(m, 25)); 576f92157deSopenharmony_ci 577f92157deSopenharmony_ci // Successful match. All matchers need to explain. 578f92157deSopenharmony_ci m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); 579f92157deSopenharmony_ci EXPECT_EQ( 580f92157deSopenharmony_ci "which is 30 more than 10, and which is 20 more than 20, " 581f92157deSopenharmony_ci "and which is 10 more than 30", 582f92157deSopenharmony_ci Explain(m, 40)); 583f92157deSopenharmony_ci 584f92157deSopenharmony_ci // Failed match. The first matcher, which failed, needs to 585f92157deSopenharmony_ci // explain. 586f92157deSopenharmony_ci m = AllOf(GreaterThan(10), GreaterThan(20)); 587f92157deSopenharmony_ci EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); 588f92157deSopenharmony_ci 589f92157deSopenharmony_ci // Failed match. The second matcher, which failed, needs to 590f92157deSopenharmony_ci // explain. Since it doesn't given an explanation, nothing is 591f92157deSopenharmony_ci // printed. 592f92157deSopenharmony_ci m = AllOf(GreaterThan(10), Lt(30)); 593f92157deSopenharmony_ci EXPECT_EQ("", Explain(m, 40)); 594f92157deSopenharmony_ci 595f92157deSopenharmony_ci // Failed match. The second matcher, which failed, needs to 596f92157deSopenharmony_ci // explain. 597f92157deSopenharmony_ci m = AllOf(GreaterThan(10), GreaterThan(20)); 598f92157deSopenharmony_ci EXPECT_EQ("which is 5 less than 20", Explain(m, 15)); 599f92157deSopenharmony_ci} 600f92157deSopenharmony_ci 601f92157deSopenharmony_ci// Helper to allow easy testing of AnyOf matchers with num parameters. 602f92157deSopenharmony_cistatic void AnyOfMatches(int num, const Matcher<int>& m) { 603f92157deSopenharmony_ci SCOPED_TRACE(Describe(m)); 604f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(0)); 605f92157deSopenharmony_ci for (int i = 1; i <= num; ++i) { 606f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(i)); 607f92157deSopenharmony_ci } 608f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(num + 1)); 609f92157deSopenharmony_ci} 610f92157deSopenharmony_ci 611f92157deSopenharmony_cistatic void AnyOfStringMatches(int num, const Matcher<std::string>& m) { 612f92157deSopenharmony_ci SCOPED_TRACE(Describe(m)); 613f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(std::to_string(0))); 614f92157deSopenharmony_ci 615f92157deSopenharmony_ci for (int i = 1; i <= num; ++i) { 616f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(std::to_string(i))); 617f92157deSopenharmony_ci } 618f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(std::to_string(num + 1))); 619f92157deSopenharmony_ci} 620f92157deSopenharmony_ci 621f92157deSopenharmony_ciINSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfTest); 622f92157deSopenharmony_ci 623f92157deSopenharmony_ci// Tests that AnyOf(m1, ..., mn) matches any value that matches at 624f92157deSopenharmony_ci// least one of the given matchers. 625f92157deSopenharmony_ciTEST(AnyOfTest, MatchesWhenAnyMatches) { 626f92157deSopenharmony_ci Matcher<int> m; 627f92157deSopenharmony_ci m = AnyOf(Le(1), Ge(3)); 628f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(1)); 629f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(4)); 630f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(2)); 631f92157deSopenharmony_ci 632f92157deSopenharmony_ci m = AnyOf(Lt(0), Eq(1), Eq(2)); 633f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(-1)); 634f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(1)); 635f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(2)); 636f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(0)); 637f92157deSopenharmony_ci 638f92157deSopenharmony_ci m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); 639f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(-1)); 640f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(1)); 641f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(2)); 642f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(3)); 643f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(0)); 644f92157deSopenharmony_ci 645f92157deSopenharmony_ci m = AnyOf(Le(0), Gt(10), 3, 5, 7); 646f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(0)); 647f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(11)); 648f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(3)); 649f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(2)); 650f92157deSopenharmony_ci 651f92157deSopenharmony_ci // The following tests for varying number of sub-matchers. Due to the way 652f92157deSopenharmony_ci // the sub-matchers are handled it is enough to test every sub-matcher once 653f92157deSopenharmony_ci // with sub-matchers using the same matcher type. Varying matcher types are 654f92157deSopenharmony_ci // checked for above. 655f92157deSopenharmony_ci AnyOfMatches(2, AnyOf(1, 2)); 656f92157deSopenharmony_ci AnyOfMatches(3, AnyOf(1, 2, 3)); 657f92157deSopenharmony_ci AnyOfMatches(4, AnyOf(1, 2, 3, 4)); 658f92157deSopenharmony_ci AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5)); 659f92157deSopenharmony_ci AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6)); 660f92157deSopenharmony_ci AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7)); 661f92157deSopenharmony_ci AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8)); 662f92157deSopenharmony_ci AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9)); 663f92157deSopenharmony_ci AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); 664f92157deSopenharmony_ci} 665f92157deSopenharmony_ci 666f92157deSopenharmony_ci// Tests the variadic version of the AnyOfMatcher. 667f92157deSopenharmony_ciTEST(AnyOfTest, VariadicMatchesWhenAnyMatches) { 668f92157deSopenharmony_ci // Also make sure AnyOf is defined in the right namespace and does not depend 669f92157deSopenharmony_ci // on ADL. 670f92157deSopenharmony_ci Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); 671f92157deSopenharmony_ci 672f92157deSopenharmony_ci EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)")); 673f92157deSopenharmony_ci AnyOfMatches(11, m); 674f92157deSopenharmony_ci AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 675f92157deSopenharmony_ci 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 676f92157deSopenharmony_ci 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 677f92157deSopenharmony_ci 45, 46, 47, 48, 49, 50)); 678f92157deSopenharmony_ci AnyOfStringMatches( 679f92157deSopenharmony_ci 50, AnyOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 680f92157deSopenharmony_ci "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", 681f92157deSopenharmony_ci "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", 682f92157deSopenharmony_ci "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", 683f92157deSopenharmony_ci "43", "44", "45", "46", "47", "48", "49", "50")); 684f92157deSopenharmony_ci} 685f92157deSopenharmony_ci 686f92157deSopenharmony_ciTEST(ConditionalTest, MatchesFirstIfCondition) { 687f92157deSopenharmony_ci Matcher<std::string> eq_red = Eq("red"); 688f92157deSopenharmony_ci Matcher<std::string> ne_red = Ne("red"); 689f92157deSopenharmony_ci Matcher<std::string> m = Conditional(true, eq_red, ne_red); 690f92157deSopenharmony_ci EXPECT_TRUE(m.Matches("red")); 691f92157deSopenharmony_ci EXPECT_FALSE(m.Matches("green")); 692f92157deSopenharmony_ci 693f92157deSopenharmony_ci StringMatchResultListener listener; 694f92157deSopenharmony_ci StringMatchResultListener expected; 695f92157deSopenharmony_ci EXPECT_FALSE(m.MatchAndExplain("green", &listener)); 696f92157deSopenharmony_ci EXPECT_FALSE(eq_red.MatchAndExplain("green", &expected)); 697f92157deSopenharmony_ci EXPECT_THAT(listener.str(), Eq(expected.str())); 698f92157deSopenharmony_ci} 699f92157deSopenharmony_ci 700f92157deSopenharmony_ciTEST(ConditionalTest, MatchesSecondIfCondition) { 701f92157deSopenharmony_ci Matcher<std::string> eq_red = Eq("red"); 702f92157deSopenharmony_ci Matcher<std::string> ne_red = Ne("red"); 703f92157deSopenharmony_ci Matcher<std::string> m = Conditional(false, eq_red, ne_red); 704f92157deSopenharmony_ci EXPECT_FALSE(m.Matches("red")); 705f92157deSopenharmony_ci EXPECT_TRUE(m.Matches("green")); 706f92157deSopenharmony_ci 707f92157deSopenharmony_ci StringMatchResultListener listener; 708f92157deSopenharmony_ci StringMatchResultListener expected; 709f92157deSopenharmony_ci EXPECT_FALSE(m.MatchAndExplain("red", &listener)); 710f92157deSopenharmony_ci EXPECT_FALSE(ne_red.MatchAndExplain("red", &expected)); 711f92157deSopenharmony_ci EXPECT_THAT(listener.str(), Eq(expected.str())); 712f92157deSopenharmony_ci} 713f92157deSopenharmony_ci 714f92157deSopenharmony_ci// Tests that AnyOf(m1, ..., mn) describes itself properly. 715f92157deSopenharmony_ciTEST(AnyOfTest, CanDescribeSelf) { 716f92157deSopenharmony_ci Matcher<int> m; 717f92157deSopenharmony_ci m = AnyOf(Le(1), Ge(3)); 718f92157deSopenharmony_ci 719f92157deSopenharmony_ci EXPECT_EQ("(is <= 1) or (is >= 3)", Describe(m)); 720f92157deSopenharmony_ci 721f92157deSopenharmony_ci m = AnyOf(Lt(0), Eq(1), Eq(2)); 722f92157deSopenharmony_ci EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m)); 723f92157deSopenharmony_ci 724f92157deSopenharmony_ci m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); 725f92157deSopenharmony_ci EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)", 726f92157deSopenharmony_ci Describe(m)); 727f92157deSopenharmony_ci 728f92157deSopenharmony_ci m = AnyOf(Le(0), Gt(10), 3, 5, 7); 729f92157deSopenharmony_ci EXPECT_EQ( 730f92157deSopenharmony_ci "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is " 731f92157deSopenharmony_ci "equal to 7)", 732f92157deSopenharmony_ci Describe(m)); 733f92157deSopenharmony_ci} 734f92157deSopenharmony_ci 735f92157deSopenharmony_ci// Tests that AnyOf(m1, ..., mn) describes its negation properly. 736f92157deSopenharmony_ciTEST(AnyOfTest, CanDescribeNegation) { 737f92157deSopenharmony_ci Matcher<int> m; 738f92157deSopenharmony_ci m = AnyOf(Le(1), Ge(3)); 739f92157deSopenharmony_ci EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", DescribeNegation(m)); 740f92157deSopenharmony_ci 741f92157deSopenharmony_ci m = AnyOf(Lt(0), Eq(1), Eq(2)); 742f92157deSopenharmony_ci EXPECT_EQ("(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)", 743f92157deSopenharmony_ci DescribeNegation(m)); 744f92157deSopenharmony_ci 745f92157deSopenharmony_ci m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); 746f92157deSopenharmony_ci EXPECT_EQ( 747f92157deSopenharmony_ci "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't " 748f92157deSopenharmony_ci "equal to 3)", 749f92157deSopenharmony_ci DescribeNegation(m)); 750f92157deSopenharmony_ci 751f92157deSopenharmony_ci m = AnyOf(Le(0), Gt(10), 3, 5, 7); 752f92157deSopenharmony_ci EXPECT_EQ( 753f92157deSopenharmony_ci "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal " 754f92157deSopenharmony_ci "to 5) and (isn't equal to 7)", 755f92157deSopenharmony_ci DescribeNegation(m)); 756f92157deSopenharmony_ci} 757f92157deSopenharmony_ci 758f92157deSopenharmony_ci// Tests that monomorphic matchers are safely cast by the AnyOf matcher. 759f92157deSopenharmony_ciTEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) { 760f92157deSopenharmony_ci // greater_than_5 and less_than_10 are monomorphic matchers. 761f92157deSopenharmony_ci Matcher<int> greater_than_5 = Gt(5); 762f92157deSopenharmony_ci Matcher<int> less_than_10 = Lt(10); 763f92157deSopenharmony_ci 764f92157deSopenharmony_ci Matcher<const int&> m = AnyOf(greater_than_5, less_than_10); 765f92157deSopenharmony_ci Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10); 766f92157deSopenharmony_ci Matcher<int&> m3 = AnyOf(greater_than_5, m2); 767f92157deSopenharmony_ci 768f92157deSopenharmony_ci // Tests that EitherOf works when composing itself. 769f92157deSopenharmony_ci Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10); 770f92157deSopenharmony_ci Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10); 771f92157deSopenharmony_ci} 772f92157deSopenharmony_ci 773f92157deSopenharmony_ciTEST_P(AnyOfTestP, ExplainsResult) { 774f92157deSopenharmony_ci Matcher<int> m; 775f92157deSopenharmony_ci 776f92157deSopenharmony_ci // Failed match. Both matchers need to explain. The second 777f92157deSopenharmony_ci // matcher doesn't give an explanation, so only the first matcher's 778f92157deSopenharmony_ci // explanation is printed. 779f92157deSopenharmony_ci m = AnyOf(GreaterThan(10), Lt(0)); 780f92157deSopenharmony_ci EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); 781f92157deSopenharmony_ci 782f92157deSopenharmony_ci // Failed match. Both matchers need to explain. 783f92157deSopenharmony_ci m = AnyOf(GreaterThan(10), GreaterThan(20)); 784f92157deSopenharmony_ci EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20", 785f92157deSopenharmony_ci Explain(m, 5)); 786f92157deSopenharmony_ci 787f92157deSopenharmony_ci // Failed match. All matchers need to explain. The second 788f92157deSopenharmony_ci // matcher doesn't given an explanation. 789f92157deSopenharmony_ci m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30)); 790f92157deSopenharmony_ci EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30", 791f92157deSopenharmony_ci Explain(m, 5)); 792f92157deSopenharmony_ci 793f92157deSopenharmony_ci // Failed match. All matchers need to explain. 794f92157deSopenharmony_ci m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); 795f92157deSopenharmony_ci EXPECT_EQ( 796f92157deSopenharmony_ci "which is 5 less than 10, and which is 15 less than 20, " 797f92157deSopenharmony_ci "and which is 25 less than 30", 798f92157deSopenharmony_ci Explain(m, 5)); 799f92157deSopenharmony_ci 800f92157deSopenharmony_ci // Successful match. The first matcher, which succeeded, needs to 801f92157deSopenharmony_ci // explain. 802f92157deSopenharmony_ci m = AnyOf(GreaterThan(10), GreaterThan(20)); 803f92157deSopenharmony_ci EXPECT_EQ("which is 5 more than 10", Explain(m, 15)); 804f92157deSopenharmony_ci 805f92157deSopenharmony_ci // Successful match. The second matcher, which succeeded, needs to 806f92157deSopenharmony_ci // explain. Since it doesn't given an explanation, nothing is 807f92157deSopenharmony_ci // printed. 808f92157deSopenharmony_ci m = AnyOf(GreaterThan(10), Lt(30)); 809f92157deSopenharmony_ci EXPECT_EQ("", Explain(m, 0)); 810f92157deSopenharmony_ci 811f92157deSopenharmony_ci // Successful match. The second matcher, which succeeded, needs to 812f92157deSopenharmony_ci // explain. 813f92157deSopenharmony_ci m = AnyOf(GreaterThan(30), GreaterThan(20)); 814f92157deSopenharmony_ci EXPECT_EQ("which is 5 more than 20", Explain(m, 25)); 815f92157deSopenharmony_ci} 816f92157deSopenharmony_ci 817f92157deSopenharmony_ci// The following predicate function and predicate functor are for 818f92157deSopenharmony_ci// testing the Truly(predicate) matcher. 819f92157deSopenharmony_ci 820f92157deSopenharmony_ci// Returns non-zero if the input is positive. Note that the return 821f92157deSopenharmony_ci// type of this function is not bool. It's OK as Truly() accepts any 822f92157deSopenharmony_ci// unary function or functor whose return type can be implicitly 823f92157deSopenharmony_ci// converted to bool. 824f92157deSopenharmony_ciint IsPositive(double x) { return x > 0 ? 1 : 0; } 825f92157deSopenharmony_ci 826f92157deSopenharmony_ci// This functor returns true if the input is greater than the given 827f92157deSopenharmony_ci// number. 828f92157deSopenharmony_ciclass IsGreaterThan { 829f92157deSopenharmony_ci public: 830f92157deSopenharmony_ci explicit IsGreaterThan(int threshold) : threshold_(threshold) {} 831f92157deSopenharmony_ci 832f92157deSopenharmony_ci bool operator()(int n) const { return n > threshold_; } 833f92157deSopenharmony_ci 834f92157deSopenharmony_ci private: 835f92157deSopenharmony_ci int threshold_; 836f92157deSopenharmony_ci}; 837f92157deSopenharmony_ci 838f92157deSopenharmony_ci// For testing Truly(). 839f92157deSopenharmony_ciconst int foo = 0; 840f92157deSopenharmony_ci 841f92157deSopenharmony_ci// This predicate returns true if and only if the argument references foo and 842f92157deSopenharmony_ci// has a zero value. 843f92157deSopenharmony_cibool ReferencesFooAndIsZero(const int& n) { return (&n == &foo) && (n == 0); } 844f92157deSopenharmony_ci 845f92157deSopenharmony_ci// Tests that Truly(predicate) matches what satisfies the given 846f92157deSopenharmony_ci// predicate. 847f92157deSopenharmony_ciTEST(TrulyTest, MatchesWhatSatisfiesThePredicate) { 848f92157deSopenharmony_ci Matcher<double> m = Truly(IsPositive); 849f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(2.0)); 850f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(-1.5)); 851f92157deSopenharmony_ci} 852f92157deSopenharmony_ci 853f92157deSopenharmony_ci// Tests that Truly(predicate_functor) works too. 854f92157deSopenharmony_ciTEST(TrulyTest, CanBeUsedWithFunctor) { 855f92157deSopenharmony_ci Matcher<int> m = Truly(IsGreaterThan(5)); 856f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(6)); 857f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(4)); 858f92157deSopenharmony_ci} 859f92157deSopenharmony_ci 860f92157deSopenharmony_ci// A class that can be implicitly converted to bool. 861f92157deSopenharmony_ciclass ConvertibleToBool { 862f92157deSopenharmony_ci public: 863f92157deSopenharmony_ci explicit ConvertibleToBool(int number) : number_(number) {} 864f92157deSopenharmony_ci operator bool() const { return number_ != 0; } 865f92157deSopenharmony_ci 866f92157deSopenharmony_ci private: 867f92157deSopenharmony_ci int number_; 868f92157deSopenharmony_ci}; 869f92157deSopenharmony_ci 870f92157deSopenharmony_ciConvertibleToBool IsNotZero(int number) { return ConvertibleToBool(number); } 871f92157deSopenharmony_ci 872f92157deSopenharmony_ci// Tests that the predicate used in Truly() may return a class that's 873f92157deSopenharmony_ci// implicitly convertible to bool, even when the class has no 874f92157deSopenharmony_ci// operator!(). 875f92157deSopenharmony_ciTEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) { 876f92157deSopenharmony_ci Matcher<int> m = Truly(IsNotZero); 877f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(1)); 878f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(0)); 879f92157deSopenharmony_ci} 880f92157deSopenharmony_ci 881f92157deSopenharmony_ci// Tests that Truly(predicate) can describe itself properly. 882f92157deSopenharmony_ciTEST(TrulyTest, CanDescribeSelf) { 883f92157deSopenharmony_ci Matcher<double> m = Truly(IsPositive); 884f92157deSopenharmony_ci EXPECT_EQ("satisfies the given predicate", Describe(m)); 885f92157deSopenharmony_ci} 886f92157deSopenharmony_ci 887f92157deSopenharmony_ci// Tests that Truly(predicate) works when the matcher takes its 888f92157deSopenharmony_ci// argument by reference. 889f92157deSopenharmony_ciTEST(TrulyTest, WorksForByRefArguments) { 890f92157deSopenharmony_ci Matcher<const int&> m = Truly(ReferencesFooAndIsZero); 891f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(foo)); 892f92157deSopenharmony_ci int n = 0; 893f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(n)); 894f92157deSopenharmony_ci} 895f92157deSopenharmony_ci 896f92157deSopenharmony_ci// Tests that Truly(predicate) provides a helpful reason when it fails. 897f92157deSopenharmony_ciTEST(TrulyTest, ExplainsFailures) { 898f92157deSopenharmony_ci StringMatchResultListener listener; 899f92157deSopenharmony_ci EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener)); 900f92157deSopenharmony_ci EXPECT_EQ(listener.str(), "didn't satisfy the given predicate"); 901f92157deSopenharmony_ci} 902f92157deSopenharmony_ci 903f92157deSopenharmony_ci// Tests that Matches(m) is a predicate satisfied by whatever that 904f92157deSopenharmony_ci// matches matcher m. 905f92157deSopenharmony_ciTEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) { 906f92157deSopenharmony_ci EXPECT_TRUE(Matches(Ge(0))(1)); 907f92157deSopenharmony_ci EXPECT_FALSE(Matches(Eq('a'))('b')); 908f92157deSopenharmony_ci} 909f92157deSopenharmony_ci 910f92157deSopenharmony_ci// Tests that Matches(m) works when the matcher takes its argument by 911f92157deSopenharmony_ci// reference. 912f92157deSopenharmony_ciTEST(MatchesTest, WorksOnByRefArguments) { 913f92157deSopenharmony_ci int m = 0, n = 0; 914f92157deSopenharmony_ci EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n)); 915f92157deSopenharmony_ci EXPECT_FALSE(Matches(Ref(m))(n)); 916f92157deSopenharmony_ci} 917f92157deSopenharmony_ci 918f92157deSopenharmony_ci// Tests that a Matcher on non-reference type can be used in 919f92157deSopenharmony_ci// Matches(). 920f92157deSopenharmony_ciTEST(MatchesTest, WorksWithMatcherOnNonRefType) { 921f92157deSopenharmony_ci Matcher<int> eq5 = Eq(5); 922f92157deSopenharmony_ci EXPECT_TRUE(Matches(eq5)(5)); 923f92157deSopenharmony_ci EXPECT_FALSE(Matches(eq5)(2)); 924f92157deSopenharmony_ci} 925f92157deSopenharmony_ci 926f92157deSopenharmony_ci// Tests Value(value, matcher). Since Value() is a simple wrapper for 927f92157deSopenharmony_ci// Matches(), which has been tested already, we don't spend a lot of 928f92157deSopenharmony_ci// effort on testing Value(). 929f92157deSopenharmony_ciTEST(ValueTest, WorksWithPolymorphicMatcher) { 930f92157deSopenharmony_ci EXPECT_TRUE(Value("hi", StartsWith("h"))); 931f92157deSopenharmony_ci EXPECT_FALSE(Value(5, Gt(10))); 932f92157deSopenharmony_ci} 933f92157deSopenharmony_ci 934f92157deSopenharmony_ciTEST(ValueTest, WorksWithMonomorphicMatcher) { 935f92157deSopenharmony_ci const Matcher<int> is_zero = Eq(0); 936f92157deSopenharmony_ci EXPECT_TRUE(Value(0, is_zero)); 937f92157deSopenharmony_ci EXPECT_FALSE(Value('a', is_zero)); 938f92157deSopenharmony_ci 939f92157deSopenharmony_ci int n = 0; 940f92157deSopenharmony_ci const Matcher<const int&> ref_n = Ref(n); 941f92157deSopenharmony_ci EXPECT_TRUE(Value(n, ref_n)); 942f92157deSopenharmony_ci EXPECT_FALSE(Value(1, ref_n)); 943f92157deSopenharmony_ci} 944f92157deSopenharmony_ci 945f92157deSopenharmony_ciTEST(AllArgsTest, WorksForTuple) { 946f92157deSopenharmony_ci EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt())); 947f92157deSopenharmony_ci EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt()))); 948f92157deSopenharmony_ci} 949f92157deSopenharmony_ci 950f92157deSopenharmony_ciTEST(AllArgsTest, WorksForNonTuple) { 951f92157deSopenharmony_ci EXPECT_THAT(42, AllArgs(Gt(0))); 952f92157deSopenharmony_ci EXPECT_THAT('a', Not(AllArgs(Eq('b')))); 953f92157deSopenharmony_ci} 954f92157deSopenharmony_ci 955f92157deSopenharmony_ciclass AllArgsHelper { 956f92157deSopenharmony_ci public: 957f92157deSopenharmony_ci AllArgsHelper() {} 958f92157deSopenharmony_ci 959f92157deSopenharmony_ci MOCK_METHOD2(Helper, int(char x, int y)); 960f92157deSopenharmony_ci 961f92157deSopenharmony_ci private: 962f92157deSopenharmony_ci AllArgsHelper(const AllArgsHelper&) = delete; 963f92157deSopenharmony_ci AllArgsHelper& operator=(const AllArgsHelper&) = delete; 964f92157deSopenharmony_ci}; 965f92157deSopenharmony_ci 966f92157deSopenharmony_ciTEST(AllArgsTest, WorksInWithClause) { 967f92157deSopenharmony_ci AllArgsHelper helper; 968f92157deSopenharmony_ci ON_CALL(helper, Helper(_, _)).With(AllArgs(Lt())).WillByDefault(Return(1)); 969f92157deSopenharmony_ci EXPECT_CALL(helper, Helper(_, _)); 970f92157deSopenharmony_ci EXPECT_CALL(helper, Helper(_, _)).With(AllArgs(Gt())).WillOnce(Return(2)); 971f92157deSopenharmony_ci 972f92157deSopenharmony_ci EXPECT_EQ(1, helper.Helper('\1', 2)); 973f92157deSopenharmony_ci EXPECT_EQ(2, helper.Helper('a', 1)); 974f92157deSopenharmony_ci} 975f92157deSopenharmony_ci 976f92157deSopenharmony_ciclass OptionalMatchersHelper { 977f92157deSopenharmony_ci public: 978f92157deSopenharmony_ci OptionalMatchersHelper() {} 979f92157deSopenharmony_ci 980f92157deSopenharmony_ci MOCK_METHOD0(NoArgs, int()); 981f92157deSopenharmony_ci 982f92157deSopenharmony_ci MOCK_METHOD1(OneArg, int(int y)); 983f92157deSopenharmony_ci 984f92157deSopenharmony_ci MOCK_METHOD2(TwoArgs, int(char x, int y)); 985f92157deSopenharmony_ci 986f92157deSopenharmony_ci MOCK_METHOD1(Overloaded, int(char x)); 987f92157deSopenharmony_ci MOCK_METHOD2(Overloaded, int(char x, int y)); 988f92157deSopenharmony_ci 989f92157deSopenharmony_ci private: 990f92157deSopenharmony_ci OptionalMatchersHelper(const OptionalMatchersHelper&) = delete; 991f92157deSopenharmony_ci OptionalMatchersHelper& operator=(const OptionalMatchersHelper&) = delete; 992f92157deSopenharmony_ci}; 993f92157deSopenharmony_ci 994f92157deSopenharmony_ciTEST(AllArgsTest, WorksWithoutMatchers) { 995f92157deSopenharmony_ci OptionalMatchersHelper helper; 996f92157deSopenharmony_ci 997f92157deSopenharmony_ci ON_CALL(helper, NoArgs).WillByDefault(Return(10)); 998f92157deSopenharmony_ci ON_CALL(helper, OneArg).WillByDefault(Return(20)); 999f92157deSopenharmony_ci ON_CALL(helper, TwoArgs).WillByDefault(Return(30)); 1000f92157deSopenharmony_ci 1001f92157deSopenharmony_ci EXPECT_EQ(10, helper.NoArgs()); 1002f92157deSopenharmony_ci EXPECT_EQ(20, helper.OneArg(1)); 1003f92157deSopenharmony_ci EXPECT_EQ(30, helper.TwoArgs('\1', 2)); 1004f92157deSopenharmony_ci 1005f92157deSopenharmony_ci EXPECT_CALL(helper, NoArgs).Times(1); 1006f92157deSopenharmony_ci EXPECT_CALL(helper, OneArg).WillOnce(Return(100)); 1007f92157deSopenharmony_ci EXPECT_CALL(helper, OneArg(17)).WillOnce(Return(200)); 1008f92157deSopenharmony_ci EXPECT_CALL(helper, TwoArgs).Times(0); 1009f92157deSopenharmony_ci 1010f92157deSopenharmony_ci EXPECT_EQ(10, helper.NoArgs()); 1011f92157deSopenharmony_ci EXPECT_EQ(100, helper.OneArg(1)); 1012f92157deSopenharmony_ci EXPECT_EQ(200, helper.OneArg(17)); 1013f92157deSopenharmony_ci} 1014f92157deSopenharmony_ci 1015f92157deSopenharmony_ci// Tests floating-point matchers. 1016f92157deSopenharmony_citemplate <typename RawType> 1017f92157deSopenharmony_ciclass FloatingPointTest : public testing::Test { 1018f92157deSopenharmony_ci protected: 1019f92157deSopenharmony_ci typedef testing::internal::FloatingPoint<RawType> Floating; 1020f92157deSopenharmony_ci typedef typename Floating::Bits Bits; 1021f92157deSopenharmony_ci 1022f92157deSopenharmony_ci FloatingPointTest() 1023f92157deSopenharmony_ci : max_ulps_(Floating::kMaxUlps), 1024f92157deSopenharmony_ci zero_bits_(Floating(0).bits()), 1025f92157deSopenharmony_ci one_bits_(Floating(1).bits()), 1026f92157deSopenharmony_ci infinity_bits_(Floating(Floating::Infinity()).bits()), 1027f92157deSopenharmony_ci close_to_positive_zero_( 1028f92157deSopenharmony_ci Floating::ReinterpretBits(zero_bits_ + max_ulps_ / 2)), 1029f92157deSopenharmony_ci close_to_negative_zero_( 1030f92157deSopenharmony_ci -Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_ / 2)), 1031f92157deSopenharmony_ci further_from_negative_zero_(-Floating::ReinterpretBits( 1032f92157deSopenharmony_ci zero_bits_ + max_ulps_ + 1 - max_ulps_ / 2)), 1033f92157deSopenharmony_ci close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)), 1034f92157deSopenharmony_ci further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)), 1035f92157deSopenharmony_ci infinity_(Floating::Infinity()), 1036f92157deSopenharmony_ci close_to_infinity_( 1037f92157deSopenharmony_ci Floating::ReinterpretBits(infinity_bits_ - max_ulps_)), 1038f92157deSopenharmony_ci further_from_infinity_( 1039f92157deSopenharmony_ci Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)), 1040f92157deSopenharmony_ci max_(Floating::Max()), 1041f92157deSopenharmony_ci nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)), 1042f92157deSopenharmony_ci nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {} 1043f92157deSopenharmony_ci 1044f92157deSopenharmony_ci void TestSize() { EXPECT_EQ(sizeof(RawType), sizeof(Bits)); } 1045f92157deSopenharmony_ci 1046f92157deSopenharmony_ci // A battery of tests for FloatingEqMatcher::Matches. 1047f92157deSopenharmony_ci // matcher_maker is a pointer to a function which creates a FloatingEqMatcher. 1048f92157deSopenharmony_ci void TestMatches( 1049f92157deSopenharmony_ci testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) { 1050f92157deSopenharmony_ci Matcher<RawType> m1 = matcher_maker(0.0); 1051f92157deSopenharmony_ci EXPECT_TRUE(m1.Matches(-0.0)); 1052f92157deSopenharmony_ci EXPECT_TRUE(m1.Matches(close_to_positive_zero_)); 1053f92157deSopenharmony_ci EXPECT_TRUE(m1.Matches(close_to_negative_zero_)); 1054f92157deSopenharmony_ci EXPECT_FALSE(m1.Matches(1.0)); 1055f92157deSopenharmony_ci 1056f92157deSopenharmony_ci Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_); 1057f92157deSopenharmony_ci EXPECT_FALSE(m2.Matches(further_from_negative_zero_)); 1058f92157deSopenharmony_ci 1059f92157deSopenharmony_ci Matcher<RawType> m3 = matcher_maker(1.0); 1060f92157deSopenharmony_ci EXPECT_TRUE(m3.Matches(close_to_one_)); 1061f92157deSopenharmony_ci EXPECT_FALSE(m3.Matches(further_from_one_)); 1062f92157deSopenharmony_ci 1063f92157deSopenharmony_ci // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above. 1064f92157deSopenharmony_ci EXPECT_FALSE(m3.Matches(0.0)); 1065f92157deSopenharmony_ci 1066f92157deSopenharmony_ci Matcher<RawType> m4 = matcher_maker(-infinity_); 1067f92157deSopenharmony_ci EXPECT_TRUE(m4.Matches(-close_to_infinity_)); 1068f92157deSopenharmony_ci 1069f92157deSopenharmony_ci Matcher<RawType> m5 = matcher_maker(infinity_); 1070f92157deSopenharmony_ci EXPECT_TRUE(m5.Matches(close_to_infinity_)); 1071f92157deSopenharmony_ci 1072f92157deSopenharmony_ci // This is interesting as the representations of infinity_ and nan1_ 1073f92157deSopenharmony_ci // are only 1 DLP apart. 1074f92157deSopenharmony_ci EXPECT_FALSE(m5.Matches(nan1_)); 1075f92157deSopenharmony_ci 1076f92157deSopenharmony_ci // matcher_maker can produce a Matcher<const RawType&>, which is needed in 1077f92157deSopenharmony_ci // some cases. 1078f92157deSopenharmony_ci Matcher<const RawType&> m6 = matcher_maker(0.0); 1079f92157deSopenharmony_ci EXPECT_TRUE(m6.Matches(-0.0)); 1080f92157deSopenharmony_ci EXPECT_TRUE(m6.Matches(close_to_positive_zero_)); 1081f92157deSopenharmony_ci EXPECT_FALSE(m6.Matches(1.0)); 1082f92157deSopenharmony_ci 1083f92157deSopenharmony_ci // matcher_maker can produce a Matcher<RawType&>, which is needed in some 1084f92157deSopenharmony_ci // cases. 1085f92157deSopenharmony_ci Matcher<RawType&> m7 = matcher_maker(0.0); 1086f92157deSopenharmony_ci RawType x = 0.0; 1087f92157deSopenharmony_ci EXPECT_TRUE(m7.Matches(x)); 1088f92157deSopenharmony_ci x = 0.01f; 1089f92157deSopenharmony_ci EXPECT_FALSE(m7.Matches(x)); 1090f92157deSopenharmony_ci } 1091f92157deSopenharmony_ci 1092f92157deSopenharmony_ci // Pre-calculated numbers to be used by the tests. 1093f92157deSopenharmony_ci 1094f92157deSopenharmony_ci const Bits max_ulps_; 1095f92157deSopenharmony_ci 1096f92157deSopenharmony_ci const Bits zero_bits_; // The bits that represent 0.0. 1097f92157deSopenharmony_ci const Bits one_bits_; // The bits that represent 1.0. 1098f92157deSopenharmony_ci const Bits infinity_bits_; // The bits that represent +infinity. 1099f92157deSopenharmony_ci 1100f92157deSopenharmony_ci // Some numbers close to 0.0. 1101f92157deSopenharmony_ci const RawType close_to_positive_zero_; 1102f92157deSopenharmony_ci const RawType close_to_negative_zero_; 1103f92157deSopenharmony_ci const RawType further_from_negative_zero_; 1104f92157deSopenharmony_ci 1105f92157deSopenharmony_ci // Some numbers close to 1.0. 1106f92157deSopenharmony_ci const RawType close_to_one_; 1107f92157deSopenharmony_ci const RawType further_from_one_; 1108f92157deSopenharmony_ci 1109f92157deSopenharmony_ci // Some numbers close to +infinity. 1110f92157deSopenharmony_ci const RawType infinity_; 1111f92157deSopenharmony_ci const RawType close_to_infinity_; 1112f92157deSopenharmony_ci const RawType further_from_infinity_; 1113f92157deSopenharmony_ci 1114f92157deSopenharmony_ci // Maximum representable value that's not infinity. 1115f92157deSopenharmony_ci const RawType max_; 1116f92157deSopenharmony_ci 1117f92157deSopenharmony_ci // Some NaNs. 1118f92157deSopenharmony_ci const RawType nan1_; 1119f92157deSopenharmony_ci const RawType nan2_; 1120f92157deSopenharmony_ci}; 1121f92157deSopenharmony_ci 1122f92157deSopenharmony_ci// Tests floating-point matchers with fixed epsilons. 1123f92157deSopenharmony_citemplate <typename RawType> 1124f92157deSopenharmony_ciclass FloatingPointNearTest : public FloatingPointTest<RawType> { 1125f92157deSopenharmony_ci protected: 1126f92157deSopenharmony_ci typedef FloatingPointTest<RawType> ParentType; 1127f92157deSopenharmony_ci 1128f92157deSopenharmony_ci // A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon. 1129f92157deSopenharmony_ci // matcher_maker is a pointer to a function which creates a FloatingEqMatcher. 1130f92157deSopenharmony_ci void TestNearMatches(testing::internal::FloatingEqMatcher<RawType> ( 1131f92157deSopenharmony_ci *matcher_maker)(RawType, RawType)) { 1132f92157deSopenharmony_ci Matcher<RawType> m1 = matcher_maker(0.0, 0.0); 1133f92157deSopenharmony_ci EXPECT_TRUE(m1.Matches(0.0)); 1134f92157deSopenharmony_ci EXPECT_TRUE(m1.Matches(-0.0)); 1135f92157deSopenharmony_ci EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_)); 1136f92157deSopenharmony_ci EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_)); 1137f92157deSopenharmony_ci EXPECT_FALSE(m1.Matches(1.0)); 1138f92157deSopenharmony_ci 1139f92157deSopenharmony_ci Matcher<RawType> m2 = matcher_maker(0.0, 1.0); 1140f92157deSopenharmony_ci EXPECT_TRUE(m2.Matches(0.0)); 1141f92157deSopenharmony_ci EXPECT_TRUE(m2.Matches(-0.0)); 1142f92157deSopenharmony_ci EXPECT_TRUE(m2.Matches(1.0)); 1143f92157deSopenharmony_ci EXPECT_TRUE(m2.Matches(-1.0)); 1144f92157deSopenharmony_ci EXPECT_FALSE(m2.Matches(ParentType::close_to_one_)); 1145f92157deSopenharmony_ci EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_)); 1146f92157deSopenharmony_ci 1147f92157deSopenharmony_ci // Check that inf matches inf, regardless of the of the specified max 1148f92157deSopenharmony_ci // absolute error. 1149f92157deSopenharmony_ci Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0); 1150f92157deSopenharmony_ci EXPECT_TRUE(m3.Matches(ParentType::infinity_)); 1151f92157deSopenharmony_ci EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_)); 1152f92157deSopenharmony_ci EXPECT_FALSE(m3.Matches(-ParentType::infinity_)); 1153f92157deSopenharmony_ci 1154f92157deSopenharmony_ci Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0); 1155f92157deSopenharmony_ci EXPECT_TRUE(m4.Matches(-ParentType::infinity_)); 1156f92157deSopenharmony_ci EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_)); 1157f92157deSopenharmony_ci EXPECT_FALSE(m4.Matches(ParentType::infinity_)); 1158f92157deSopenharmony_ci 1159f92157deSopenharmony_ci // Test various overflow scenarios. 1160f92157deSopenharmony_ci Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_); 1161f92157deSopenharmony_ci EXPECT_TRUE(m5.Matches(ParentType::max_)); 1162f92157deSopenharmony_ci EXPECT_FALSE(m5.Matches(-ParentType::max_)); 1163f92157deSopenharmony_ci 1164f92157deSopenharmony_ci Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_); 1165f92157deSopenharmony_ci EXPECT_FALSE(m6.Matches(ParentType::max_)); 1166f92157deSopenharmony_ci EXPECT_TRUE(m6.Matches(-ParentType::max_)); 1167f92157deSopenharmony_ci 1168f92157deSopenharmony_ci Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0); 1169f92157deSopenharmony_ci EXPECT_TRUE(m7.Matches(ParentType::max_)); 1170f92157deSopenharmony_ci EXPECT_FALSE(m7.Matches(-ParentType::max_)); 1171f92157deSopenharmony_ci 1172f92157deSopenharmony_ci Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0); 1173f92157deSopenharmony_ci EXPECT_FALSE(m8.Matches(ParentType::max_)); 1174f92157deSopenharmony_ci EXPECT_TRUE(m8.Matches(-ParentType::max_)); 1175f92157deSopenharmony_ci 1176f92157deSopenharmony_ci // The difference between max() and -max() normally overflows to infinity, 1177f92157deSopenharmony_ci // but it should still match if the max_abs_error is also infinity. 1178f92157deSopenharmony_ci Matcher<RawType> m9 = 1179f92157deSopenharmony_ci matcher_maker(ParentType::max_, ParentType::infinity_); 1180f92157deSopenharmony_ci EXPECT_TRUE(m8.Matches(-ParentType::max_)); 1181f92157deSopenharmony_ci 1182f92157deSopenharmony_ci // matcher_maker can produce a Matcher<const RawType&>, which is needed in 1183f92157deSopenharmony_ci // some cases. 1184f92157deSopenharmony_ci Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0); 1185f92157deSopenharmony_ci EXPECT_TRUE(m10.Matches(-0.0)); 1186f92157deSopenharmony_ci EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_)); 1187f92157deSopenharmony_ci EXPECT_FALSE(m10.Matches(ParentType::close_to_one_)); 1188f92157deSopenharmony_ci 1189f92157deSopenharmony_ci // matcher_maker can produce a Matcher<RawType&>, which is needed in some 1190f92157deSopenharmony_ci // cases. 1191f92157deSopenharmony_ci Matcher<RawType&> m11 = matcher_maker(0.0, 1.0); 1192f92157deSopenharmony_ci RawType x = 0.0; 1193f92157deSopenharmony_ci EXPECT_TRUE(m11.Matches(x)); 1194f92157deSopenharmony_ci x = 1.0f; 1195f92157deSopenharmony_ci EXPECT_TRUE(m11.Matches(x)); 1196f92157deSopenharmony_ci x = -1.0f; 1197f92157deSopenharmony_ci EXPECT_TRUE(m11.Matches(x)); 1198f92157deSopenharmony_ci x = 1.1f; 1199f92157deSopenharmony_ci EXPECT_FALSE(m11.Matches(x)); 1200f92157deSopenharmony_ci x = -1.1f; 1201f92157deSopenharmony_ci EXPECT_FALSE(m11.Matches(x)); 1202f92157deSopenharmony_ci } 1203f92157deSopenharmony_ci}; 1204f92157deSopenharmony_ci 1205f92157deSopenharmony_ci// Instantiate FloatingPointTest for testing floats. 1206f92157deSopenharmony_citypedef FloatingPointTest<float> FloatTest; 1207f92157deSopenharmony_ci 1208f92157deSopenharmony_ciTEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { TestMatches(&FloatEq); } 1209f92157deSopenharmony_ci 1210f92157deSopenharmony_ciTEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) { 1211f92157deSopenharmony_ci TestMatches(&NanSensitiveFloatEq); 1212f92157deSopenharmony_ci} 1213f92157deSopenharmony_ci 1214f92157deSopenharmony_ciTEST_F(FloatTest, FloatEqCannotMatchNaN) { 1215f92157deSopenharmony_ci // FloatEq never matches NaN. 1216f92157deSopenharmony_ci Matcher<float> m = FloatEq(nan1_); 1217f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(nan1_)); 1218f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(nan2_)); 1219f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(1.0)); 1220f92157deSopenharmony_ci} 1221f92157deSopenharmony_ci 1222f92157deSopenharmony_ciTEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) { 1223f92157deSopenharmony_ci // NanSensitiveFloatEq will match NaN. 1224f92157deSopenharmony_ci Matcher<float> m = NanSensitiveFloatEq(nan1_); 1225f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(nan1_)); 1226f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(nan2_)); 1227f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(1.0)); 1228f92157deSopenharmony_ci} 1229f92157deSopenharmony_ci 1230f92157deSopenharmony_ciTEST_F(FloatTest, FloatEqCanDescribeSelf) { 1231f92157deSopenharmony_ci Matcher<float> m1 = FloatEq(2.0f); 1232f92157deSopenharmony_ci EXPECT_EQ("is approximately 2", Describe(m1)); 1233f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); 1234f92157deSopenharmony_ci 1235f92157deSopenharmony_ci Matcher<float> m2 = FloatEq(0.5f); 1236f92157deSopenharmony_ci EXPECT_EQ("is approximately 0.5", Describe(m2)); 1237f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); 1238f92157deSopenharmony_ci 1239f92157deSopenharmony_ci Matcher<float> m3 = FloatEq(nan1_); 1240f92157deSopenharmony_ci EXPECT_EQ("never matches", Describe(m3)); 1241f92157deSopenharmony_ci EXPECT_EQ("is anything", DescribeNegation(m3)); 1242f92157deSopenharmony_ci} 1243f92157deSopenharmony_ci 1244f92157deSopenharmony_ciTEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) { 1245f92157deSopenharmony_ci Matcher<float> m1 = NanSensitiveFloatEq(2.0f); 1246f92157deSopenharmony_ci EXPECT_EQ("is approximately 2", Describe(m1)); 1247f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); 1248f92157deSopenharmony_ci 1249f92157deSopenharmony_ci Matcher<float> m2 = NanSensitiveFloatEq(0.5f); 1250f92157deSopenharmony_ci EXPECT_EQ("is approximately 0.5", Describe(m2)); 1251f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); 1252f92157deSopenharmony_ci 1253f92157deSopenharmony_ci Matcher<float> m3 = NanSensitiveFloatEq(nan1_); 1254f92157deSopenharmony_ci EXPECT_EQ("is NaN", Describe(m3)); 1255f92157deSopenharmony_ci EXPECT_EQ("isn't NaN", DescribeNegation(m3)); 1256f92157deSopenharmony_ci} 1257f92157deSopenharmony_ci 1258f92157deSopenharmony_ci// Instantiate FloatingPointTest for testing floats with a user-specified 1259f92157deSopenharmony_ci// max absolute error. 1260f92157deSopenharmony_citypedef FloatingPointNearTest<float> FloatNearTest; 1261f92157deSopenharmony_ci 1262f92157deSopenharmony_ciTEST_F(FloatNearTest, FloatNearMatches) { TestNearMatches(&FloatNear); } 1263f92157deSopenharmony_ci 1264f92157deSopenharmony_ciTEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) { 1265f92157deSopenharmony_ci TestNearMatches(&NanSensitiveFloatNear); 1266f92157deSopenharmony_ci} 1267f92157deSopenharmony_ci 1268f92157deSopenharmony_ciTEST_F(FloatNearTest, FloatNearCanDescribeSelf) { 1269f92157deSopenharmony_ci Matcher<float> m1 = FloatNear(2.0f, 0.5f); 1270f92157deSopenharmony_ci EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); 1271f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)", 1272f92157deSopenharmony_ci DescribeNegation(m1)); 1273f92157deSopenharmony_ci 1274f92157deSopenharmony_ci Matcher<float> m2 = FloatNear(0.5f, 0.5f); 1275f92157deSopenharmony_ci EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); 1276f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)", 1277f92157deSopenharmony_ci DescribeNegation(m2)); 1278f92157deSopenharmony_ci 1279f92157deSopenharmony_ci Matcher<float> m3 = FloatNear(nan1_, 0.0); 1280f92157deSopenharmony_ci EXPECT_EQ("never matches", Describe(m3)); 1281f92157deSopenharmony_ci EXPECT_EQ("is anything", DescribeNegation(m3)); 1282f92157deSopenharmony_ci} 1283f92157deSopenharmony_ci 1284f92157deSopenharmony_ciTEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) { 1285f92157deSopenharmony_ci Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f); 1286f92157deSopenharmony_ci EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); 1287f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)", 1288f92157deSopenharmony_ci DescribeNegation(m1)); 1289f92157deSopenharmony_ci 1290f92157deSopenharmony_ci Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f); 1291f92157deSopenharmony_ci EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); 1292f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)", 1293f92157deSopenharmony_ci DescribeNegation(m2)); 1294f92157deSopenharmony_ci 1295f92157deSopenharmony_ci Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f); 1296f92157deSopenharmony_ci EXPECT_EQ("is NaN", Describe(m3)); 1297f92157deSopenharmony_ci EXPECT_EQ("isn't NaN", DescribeNegation(m3)); 1298f92157deSopenharmony_ci} 1299f92157deSopenharmony_ci 1300f92157deSopenharmony_ciTEST_F(FloatNearTest, FloatNearCannotMatchNaN) { 1301f92157deSopenharmony_ci // FloatNear never matches NaN. 1302f92157deSopenharmony_ci Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f); 1303f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(nan1_)); 1304f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(nan2_)); 1305f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(1.0)); 1306f92157deSopenharmony_ci} 1307f92157deSopenharmony_ci 1308f92157deSopenharmony_ciTEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) { 1309f92157deSopenharmony_ci // NanSensitiveFloatNear will match NaN. 1310f92157deSopenharmony_ci Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f); 1311f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(nan1_)); 1312f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(nan2_)); 1313f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(1.0)); 1314f92157deSopenharmony_ci} 1315f92157deSopenharmony_ci 1316f92157deSopenharmony_ci// Instantiate FloatingPointTest for testing doubles. 1317f92157deSopenharmony_citypedef FloatingPointTest<double> DoubleTest; 1318f92157deSopenharmony_ci 1319f92157deSopenharmony_ciTEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) { 1320f92157deSopenharmony_ci TestMatches(&DoubleEq); 1321f92157deSopenharmony_ci} 1322f92157deSopenharmony_ci 1323f92157deSopenharmony_ciTEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) { 1324f92157deSopenharmony_ci TestMatches(&NanSensitiveDoubleEq); 1325f92157deSopenharmony_ci} 1326f92157deSopenharmony_ci 1327f92157deSopenharmony_ciTEST_F(DoubleTest, DoubleEqCannotMatchNaN) { 1328f92157deSopenharmony_ci // DoubleEq never matches NaN. 1329f92157deSopenharmony_ci Matcher<double> m = DoubleEq(nan1_); 1330f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(nan1_)); 1331f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(nan2_)); 1332f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(1.0)); 1333f92157deSopenharmony_ci} 1334f92157deSopenharmony_ci 1335f92157deSopenharmony_ciTEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) { 1336f92157deSopenharmony_ci // NanSensitiveDoubleEq will match NaN. 1337f92157deSopenharmony_ci Matcher<double> m = NanSensitiveDoubleEq(nan1_); 1338f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(nan1_)); 1339f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(nan2_)); 1340f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(1.0)); 1341f92157deSopenharmony_ci} 1342f92157deSopenharmony_ci 1343f92157deSopenharmony_ciTEST_F(DoubleTest, DoubleEqCanDescribeSelf) { 1344f92157deSopenharmony_ci Matcher<double> m1 = DoubleEq(2.0); 1345f92157deSopenharmony_ci EXPECT_EQ("is approximately 2", Describe(m1)); 1346f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); 1347f92157deSopenharmony_ci 1348f92157deSopenharmony_ci Matcher<double> m2 = DoubleEq(0.5); 1349f92157deSopenharmony_ci EXPECT_EQ("is approximately 0.5", Describe(m2)); 1350f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); 1351f92157deSopenharmony_ci 1352f92157deSopenharmony_ci Matcher<double> m3 = DoubleEq(nan1_); 1353f92157deSopenharmony_ci EXPECT_EQ("never matches", Describe(m3)); 1354f92157deSopenharmony_ci EXPECT_EQ("is anything", DescribeNegation(m3)); 1355f92157deSopenharmony_ci} 1356f92157deSopenharmony_ci 1357f92157deSopenharmony_ciTEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) { 1358f92157deSopenharmony_ci Matcher<double> m1 = NanSensitiveDoubleEq(2.0); 1359f92157deSopenharmony_ci EXPECT_EQ("is approximately 2", Describe(m1)); 1360f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); 1361f92157deSopenharmony_ci 1362f92157deSopenharmony_ci Matcher<double> m2 = NanSensitiveDoubleEq(0.5); 1363f92157deSopenharmony_ci EXPECT_EQ("is approximately 0.5", Describe(m2)); 1364f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); 1365f92157deSopenharmony_ci 1366f92157deSopenharmony_ci Matcher<double> m3 = NanSensitiveDoubleEq(nan1_); 1367f92157deSopenharmony_ci EXPECT_EQ("is NaN", Describe(m3)); 1368f92157deSopenharmony_ci EXPECT_EQ("isn't NaN", DescribeNegation(m3)); 1369f92157deSopenharmony_ci} 1370f92157deSopenharmony_ci 1371f92157deSopenharmony_ci// Instantiate FloatingPointTest for testing floats with a user-specified 1372f92157deSopenharmony_ci// max absolute error. 1373f92157deSopenharmony_citypedef FloatingPointNearTest<double> DoubleNearTest; 1374f92157deSopenharmony_ci 1375f92157deSopenharmony_ciTEST_F(DoubleNearTest, DoubleNearMatches) { TestNearMatches(&DoubleNear); } 1376f92157deSopenharmony_ci 1377f92157deSopenharmony_ciTEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) { 1378f92157deSopenharmony_ci TestNearMatches(&NanSensitiveDoubleNear); 1379f92157deSopenharmony_ci} 1380f92157deSopenharmony_ci 1381f92157deSopenharmony_ciTEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) { 1382f92157deSopenharmony_ci Matcher<double> m1 = DoubleNear(2.0, 0.5); 1383f92157deSopenharmony_ci EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); 1384f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)", 1385f92157deSopenharmony_ci DescribeNegation(m1)); 1386f92157deSopenharmony_ci 1387f92157deSopenharmony_ci Matcher<double> m2 = DoubleNear(0.5, 0.5); 1388f92157deSopenharmony_ci EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); 1389f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)", 1390f92157deSopenharmony_ci DescribeNegation(m2)); 1391f92157deSopenharmony_ci 1392f92157deSopenharmony_ci Matcher<double> m3 = DoubleNear(nan1_, 0.0); 1393f92157deSopenharmony_ci EXPECT_EQ("never matches", Describe(m3)); 1394f92157deSopenharmony_ci EXPECT_EQ("is anything", DescribeNegation(m3)); 1395f92157deSopenharmony_ci} 1396f92157deSopenharmony_ci 1397f92157deSopenharmony_ciTEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) { 1398f92157deSopenharmony_ci EXPECT_EQ("", Explain(DoubleNear(2.0, 0.1), 2.05)); 1399f92157deSopenharmony_ci EXPECT_EQ("which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2)); 1400f92157deSopenharmony_ci EXPECT_EQ("which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7)); 1401f92157deSopenharmony_ci 1402f92157deSopenharmony_ci const std::string explanation = 1403f92157deSopenharmony_ci Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10); 1404f92157deSopenharmony_ci // Different C++ implementations may print floating-point numbers 1405f92157deSopenharmony_ci // slightly differently. 1406f92157deSopenharmony_ci EXPECT_TRUE(explanation == "which is 1.2e-10 from 2.1" || // GCC 1407f92157deSopenharmony_ci explanation == "which is 1.2e-010 from 2.1") // MSVC 1408f92157deSopenharmony_ci << " where explanation is \"" << explanation << "\"."; 1409f92157deSopenharmony_ci} 1410f92157deSopenharmony_ci 1411f92157deSopenharmony_ciTEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) { 1412f92157deSopenharmony_ci Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5); 1413f92157deSopenharmony_ci EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); 1414f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)", 1415f92157deSopenharmony_ci DescribeNegation(m1)); 1416f92157deSopenharmony_ci 1417f92157deSopenharmony_ci Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5); 1418f92157deSopenharmony_ci EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); 1419f92157deSopenharmony_ci EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)", 1420f92157deSopenharmony_ci DescribeNegation(m2)); 1421f92157deSopenharmony_ci 1422f92157deSopenharmony_ci Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1); 1423f92157deSopenharmony_ci EXPECT_EQ("is NaN", Describe(m3)); 1424f92157deSopenharmony_ci EXPECT_EQ("isn't NaN", DescribeNegation(m3)); 1425f92157deSopenharmony_ci} 1426f92157deSopenharmony_ci 1427f92157deSopenharmony_ciTEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) { 1428f92157deSopenharmony_ci // DoubleNear never matches NaN. 1429f92157deSopenharmony_ci Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1); 1430f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(nan1_)); 1431f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(nan2_)); 1432f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(1.0)); 1433f92157deSopenharmony_ci} 1434f92157deSopenharmony_ci 1435f92157deSopenharmony_ciTEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) { 1436f92157deSopenharmony_ci // NanSensitiveDoubleNear will match NaN. 1437f92157deSopenharmony_ci Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1); 1438f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(nan1_)); 1439f92157deSopenharmony_ci EXPECT_TRUE(m.Matches(nan2_)); 1440f92157deSopenharmony_ci EXPECT_FALSE(m.Matches(1.0)); 1441f92157deSopenharmony_ci} 1442f92157deSopenharmony_ci 1443f92157deSopenharmony_ciTEST(NotTest, WorksOnMoveOnlyType) { 1444f92157deSopenharmony_ci std::unique_ptr<int> p(new int(3)); 1445f92157deSopenharmony_ci EXPECT_THAT(p, Pointee(Eq(3))); 1446f92157deSopenharmony_ci EXPECT_THAT(p, Not(Pointee(Eq(2)))); 1447f92157deSopenharmony_ci} 1448f92157deSopenharmony_ci 1449f92157deSopenharmony_ciTEST(AllOfTest, HugeMatcher) { 1450f92157deSopenharmony_ci // Verify that using AllOf with many arguments doesn't cause 1451f92157deSopenharmony_ci // the compiler to exceed template instantiation depth limit. 1452f92157deSopenharmony_ci EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _, 1453f92157deSopenharmony_ci testing::AllOf(_, _, _, _, _, _, _, _, _, _))); 1454f92157deSopenharmony_ci} 1455f92157deSopenharmony_ci 1456f92157deSopenharmony_ciTEST(AnyOfTest, HugeMatcher) { 1457f92157deSopenharmony_ci // Verify that using AnyOf with many arguments doesn't cause 1458f92157deSopenharmony_ci // the compiler to exceed template instantiation depth limit. 1459f92157deSopenharmony_ci EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _, 1460f92157deSopenharmony_ci testing::AnyOf(_, _, _, _, _, _, _, _, _, _))); 1461f92157deSopenharmony_ci} 1462f92157deSopenharmony_ci 1463f92157deSopenharmony_cinamespace adl_test { 1464f92157deSopenharmony_ci 1465f92157deSopenharmony_ci// Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf 1466f92157deSopenharmony_ci// don't issue unqualified recursive calls. If they do, the argument dependent 1467f92157deSopenharmony_ci// name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found 1468f92157deSopenharmony_ci// as a candidate and the compilation will break due to an ambiguous overload. 1469f92157deSopenharmony_ci 1470f92157deSopenharmony_ci// The matcher must be in the same namespace as AllOf/AnyOf to make argument 1471f92157deSopenharmony_ci// dependent lookup find those. 1472f92157deSopenharmony_ciMATCHER(M, "") { 1473f92157deSopenharmony_ci (void)arg; 1474f92157deSopenharmony_ci return true; 1475f92157deSopenharmony_ci} 1476f92157deSopenharmony_ci 1477f92157deSopenharmony_citemplate <typename T1, typename T2> 1478f92157deSopenharmony_cibool AllOf(const T1& /*t1*/, const T2& /*t2*/) { 1479f92157deSopenharmony_ci return true; 1480f92157deSopenharmony_ci} 1481f92157deSopenharmony_ci 1482f92157deSopenharmony_ciTEST(AllOfTest, DoesNotCallAllOfUnqualified) { 1483f92157deSopenharmony_ci EXPECT_THAT(42, 1484f92157deSopenharmony_ci testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M())); 1485f92157deSopenharmony_ci} 1486f92157deSopenharmony_ci 1487f92157deSopenharmony_citemplate <typename T1, typename T2> 1488f92157deSopenharmony_cibool AnyOf(const T1&, const T2&) { 1489f92157deSopenharmony_ci return true; 1490f92157deSopenharmony_ci} 1491f92157deSopenharmony_ci 1492f92157deSopenharmony_ciTEST(AnyOfTest, DoesNotCallAnyOfUnqualified) { 1493f92157deSopenharmony_ci EXPECT_THAT(42, 1494f92157deSopenharmony_ci testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M())); 1495f92157deSopenharmony_ci} 1496f92157deSopenharmony_ci 1497f92157deSopenharmony_ci} // namespace adl_test 1498f92157deSopenharmony_ci 1499f92157deSopenharmony_ciTEST(AllOfTest, WorksOnMoveOnlyType) { 1500f92157deSopenharmony_ci std::unique_ptr<int> p(new int(3)); 1501f92157deSopenharmony_ci EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5)))); 1502f92157deSopenharmony_ci EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3))))); 1503f92157deSopenharmony_ci} 1504f92157deSopenharmony_ci 1505f92157deSopenharmony_ciTEST(AnyOfTest, WorksOnMoveOnlyType) { 1506f92157deSopenharmony_ci std::unique_ptr<int> p(new int(3)); 1507f92157deSopenharmony_ci EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5)))); 1508f92157deSopenharmony_ci EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5))))); 1509f92157deSopenharmony_ci} 1510f92157deSopenharmony_ci 1511f92157deSopenharmony_ci} // namespace 1512f92157deSopenharmony_ci} // namespace gmock_matchers_test 1513f92157deSopenharmony_ci} // namespace testing 1514f92157deSopenharmony_ci 1515f92157deSopenharmony_ci#ifdef _MSC_VER 1516f92157deSopenharmony_ci#pragma warning(pop) 1517f92157deSopenharmony_ci#endif 1518