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