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