Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gmock-matchers-arithmetic_test.cc
Go to the documentation of this file.
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 "gmock/gmock.h"
41 #include "gtest/gtest.h"
42 
43 // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
44 // possible loss of data and C4100, unreferenced local parameter
46 
47 namespace testing {
48 namespace gmock_matchers_test {
49 namespace {
50 
51 typedef ::std::tuple<long, int> Tuple2; // NOLINT
52 
53 // Tests that Eq() matches a 2-tuple where the first field == the
54 // second field.
55 TEST(Eq2Test, MatchesEqualArguments) {
56  Matcher<const Tuple2&> m = Eq();
57  EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
58  EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
59 }
60 
61 // Tests that Eq() describes itself properly.
62 TEST(Eq2Test, CanDescribeSelf) {
63  Matcher<const Tuple2&> m = Eq();
64  EXPECT_EQ("are an equal pair", Describe(m));
65 }
66 
67 // Tests that Ge() matches a 2-tuple where the first field >= the
68 // second field.
69 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
70  Matcher<const Tuple2&> m = Ge();
71  EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
72  EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
73  EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
74 }
75 
76 // Tests that Ge() describes itself properly.
77 TEST(Ge2Test, CanDescribeSelf) {
78  Matcher<const Tuple2&> m = Ge();
79  EXPECT_EQ("are a pair where the first >= the second", Describe(m));
80 }
81 
82 // Tests that Gt() matches a 2-tuple where the first field > the
83 // second field.
84 TEST(Gt2Test, MatchesGreaterThanArguments) {
85  Matcher<const Tuple2&> m = Gt();
86  EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
87  EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
88  EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
89 }
90 
91 // Tests that Gt() describes itself properly.
92 TEST(Gt2Test, CanDescribeSelf) {
93  Matcher<const Tuple2&> m = Gt();
94  EXPECT_EQ("are a pair where the first > the second", Describe(m));
95 }
96 
97 // Tests that Le() matches a 2-tuple where the first field <= the
98 // second field.
99 TEST(Le2Test, MatchesLessThanOrEqualArguments) {
100  Matcher<const Tuple2&> m = Le();
101  EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
102  EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
103  EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
104 }
105 
106 // Tests that Le() describes itself properly.
107 TEST(Le2Test, CanDescribeSelf) {
108  Matcher<const Tuple2&> m = Le();
109  EXPECT_EQ("are a pair where the first <= the second", Describe(m));
110 }
111 
112 // Tests that Lt() matches a 2-tuple where the first field < the
113 // second field.
114 TEST(Lt2Test, MatchesLessThanArguments) {
115  Matcher<const Tuple2&> m = Lt();
116  EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
117  EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
118  EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
119 }
120 
121 // Tests that Lt() describes itself properly.
122 TEST(Lt2Test, CanDescribeSelf) {
123  Matcher<const Tuple2&> m = Lt();
124  EXPECT_EQ("are a pair where the first < the second", Describe(m));
125 }
126 
127 // Tests that Ne() matches a 2-tuple where the first field != the
128 // second field.
129 TEST(Ne2Test, MatchesUnequalArguments) {
130  Matcher<const Tuple2&> m = Ne();
131  EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
132  EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
133  EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
134 }
135 
136 // Tests that Ne() describes itself properly.
137 TEST(Ne2Test, CanDescribeSelf) {
138  Matcher<const Tuple2&> m = Ne();
139  EXPECT_EQ("are an unequal pair", Describe(m));
140 }
141 
142 TEST(PairMatchBaseTest, WorksWithMoveOnly) {
143  using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
144  Matcher<Pointers> matcher = Eq();
145  Pointers pointers;
146  // Tested values don't matter; the point is that matcher does not copy the
147  // matched values.
148  EXPECT_TRUE(matcher.Matches(pointers));
149 }
150 
151 // Tests that IsNan() matches a NaN, with float.
152 TEST(IsNan, FloatMatchesNan) {
153  float quiet_nan = std::numeric_limits<float>::quiet_NaN();
154  float other_nan = std::nanf("1");
155  float real_value = 1.0f;
156 
157  Matcher<float> m = IsNan();
158  EXPECT_TRUE(m.Matches(quiet_nan));
159  EXPECT_TRUE(m.Matches(other_nan));
160  EXPECT_FALSE(m.Matches(real_value));
161 
162  Matcher<float&> m_ref = IsNan();
163  EXPECT_TRUE(m_ref.Matches(quiet_nan));
164  EXPECT_TRUE(m_ref.Matches(other_nan));
165  EXPECT_FALSE(m_ref.Matches(real_value));
166 
167  Matcher<const float&> m_cref = IsNan();
168  EXPECT_TRUE(m_cref.Matches(quiet_nan));
169  EXPECT_TRUE(m_cref.Matches(other_nan));
170  EXPECT_FALSE(m_cref.Matches(real_value));
171 }
172 
173 // Tests that IsNan() matches a NaN, with double.
174 TEST(IsNan, DoubleMatchesNan) {
175  double quiet_nan = std::numeric_limits<double>::quiet_NaN();
176  double other_nan = std::nan("1");
177  double real_value = 1.0;
178 
179  Matcher<double> m = IsNan();
180  EXPECT_TRUE(m.Matches(quiet_nan));
181  EXPECT_TRUE(m.Matches(other_nan));
182  EXPECT_FALSE(m.Matches(real_value));
183 
184  Matcher<double&> m_ref = IsNan();
185  EXPECT_TRUE(m_ref.Matches(quiet_nan));
186  EXPECT_TRUE(m_ref.Matches(other_nan));
187  EXPECT_FALSE(m_ref.Matches(real_value));
188 
189  Matcher<const double&> m_cref = IsNan();
190  EXPECT_TRUE(m_cref.Matches(quiet_nan));
191  EXPECT_TRUE(m_cref.Matches(other_nan));
192  EXPECT_FALSE(m_cref.Matches(real_value));
193 }
194 
195 // Tests that IsNan() matches a NaN, with long double.
196 TEST(IsNan, LongDoubleMatchesNan) {
197  long double quiet_nan = std::numeric_limits<long double>::quiet_NaN();
198  long double other_nan = std::nan("1");
199  long double real_value = 1.0;
200 
201  Matcher<long double> m = IsNan();
202  EXPECT_TRUE(m.Matches(quiet_nan));
203  EXPECT_TRUE(m.Matches(other_nan));
204  EXPECT_FALSE(m.Matches(real_value));
205 
206  Matcher<long double&> m_ref = IsNan();
207  EXPECT_TRUE(m_ref.Matches(quiet_nan));
208  EXPECT_TRUE(m_ref.Matches(other_nan));
209  EXPECT_FALSE(m_ref.Matches(real_value));
210 
211  Matcher<const long double&> m_cref = IsNan();
212  EXPECT_TRUE(m_cref.Matches(quiet_nan));
213  EXPECT_TRUE(m_cref.Matches(other_nan));
214  EXPECT_FALSE(m_cref.Matches(real_value));
215 }
216 
217 // Tests that IsNan() works with Not.
218 TEST(IsNan, NotMatchesNan) {
219  Matcher<float> mf = Not(IsNan());
220  EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN()));
221  EXPECT_FALSE(mf.Matches(std::nanf("1")));
222  EXPECT_TRUE(mf.Matches(1.0));
223 
224  Matcher<double> md = Not(IsNan());
225  EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN()));
226  EXPECT_FALSE(md.Matches(std::nan("1")));
227  EXPECT_TRUE(md.Matches(1.0));
228 
229  Matcher<long double> mld = Not(IsNan());
230  EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN()));
231  EXPECT_FALSE(mld.Matches(std::nanl("1")));
232  EXPECT_TRUE(mld.Matches(1.0));
233 }
234 
235 // Tests that IsNan() can describe itself.
236 TEST(IsNan, CanDescribeSelf) {
237  Matcher<float> mf = IsNan();
238  EXPECT_EQ("is NaN", Describe(mf));
239 
240  Matcher<double> md = IsNan();
241  EXPECT_EQ("is NaN", Describe(md));
242 
243  Matcher<long double> mld = IsNan();
244  EXPECT_EQ("is NaN", Describe(mld));
245 }
246 
247 // Tests that IsNan() can describe itself with Not.
248 TEST(IsNan, CanDescribeSelfWithNot) {
249  Matcher<float> mf = Not(IsNan());
250  EXPECT_EQ("isn't NaN", Describe(mf));
251 
252  Matcher<double> md = Not(IsNan());
253  EXPECT_EQ("isn't NaN", Describe(md));
254 
255  Matcher<long double> mld = Not(IsNan());
256  EXPECT_EQ("isn't NaN", Describe(mld));
257 }
258 
259 // Tests that FloatEq() matches a 2-tuple where
260 // FloatEq(first field) matches the second field.
261 TEST(FloatEq2Test, MatchesEqualArguments) {
262  typedef ::std::tuple<float, float> Tpl;
263  Matcher<const Tpl&> m = FloatEq();
264  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
265  EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f)));
266  EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
267 }
268 
269 // Tests that FloatEq() describes itself properly.
270 TEST(FloatEq2Test, CanDescribeSelf) {
272  EXPECT_EQ("are an almost-equal pair", Describe(m));
273 }
274 
275 // Tests that NanSensitiveFloatEq() matches a 2-tuple where
276 // NanSensitiveFloatEq(first field) matches the second field.
277 TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {
278  typedef ::std::tuple<float, float> Tpl;
279  Matcher<const Tpl&> m = NanSensitiveFloatEq();
280  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
281  EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
282  std::numeric_limits<float>::quiet_NaN())));
283  EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
284  EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
285  EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
286 }
287 
288 // Tests that NanSensitiveFloatEq() describes itself properly.
289 TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
290  Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq();
291  EXPECT_EQ("are an almost-equal pair", Describe(m));
292 }
293 
294 // Tests that DoubleEq() matches a 2-tuple where
295 // DoubleEq(first field) matches the second field.
296 TEST(DoubleEq2Test, MatchesEqualArguments) {
297  typedef ::std::tuple<double, double> Tpl;
298  Matcher<const Tpl&> m = DoubleEq();
299  EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));
300  EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1)));
301  EXPECT_FALSE(m.Matches(Tpl(1.1, 1.0)));
302 }
303 
304 // Tests that DoubleEq() describes itself properly.
305 TEST(DoubleEq2Test, CanDescribeSelf) {
307  EXPECT_EQ("are an almost-equal pair", Describe(m));
308 }
309 
310 // Tests that NanSensitiveDoubleEq() matches a 2-tuple where
311 // NanSensitiveDoubleEq(first field) matches the second field.
312 TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {
313  typedef ::std::tuple<double, double> Tpl;
314  Matcher<const Tpl&> m = NanSensitiveDoubleEq();
315  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
316  EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
317  std::numeric_limits<double>::quiet_NaN())));
318  EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
319  EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
320  EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
321 }
322 
323 // Tests that DoubleEq() describes itself properly.
324 TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
325  Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq();
326  EXPECT_EQ("are an almost-equal pair", Describe(m));
327 }
328 
329 // Tests that FloatEq() matches a 2-tuple where
330 // FloatNear(first field, max_abs_error) matches the second field.
331 TEST(FloatNear2Test, MatchesEqualArguments) {
332  typedef ::std::tuple<float, float> Tpl;
333  Matcher<const Tpl&> m = FloatNear(0.5f);
334  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
335  EXPECT_TRUE(m.Matches(Tpl(1.3f, 1.0f)));
336  EXPECT_FALSE(m.Matches(Tpl(1.8f, 1.0f)));
337 }
338 
339 // Tests that FloatNear() describes itself properly.
340 TEST(FloatNear2Test, CanDescribeSelf) {
341  Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f);
342  EXPECT_EQ("are an almost-equal pair", Describe(m));
343 }
344 
345 // Tests that NanSensitiveFloatNear() matches a 2-tuple where
346 // NanSensitiveFloatNear(first field) matches the second field.
347 TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {
348  typedef ::std::tuple<float, float> Tpl;
349  Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f);
350  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
351  EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));
352  EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
353  std::numeric_limits<float>::quiet_NaN())));
354  EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));
355  EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
356  EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
357 }
358 
359 // Tests that NanSensitiveFloatNear() describes itself properly.
360 TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {
361  Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f);
362  EXPECT_EQ("are an almost-equal pair", Describe(m));
363 }
364 
365 // Tests that FloatEq() matches a 2-tuple where
366 // DoubleNear(first field, max_abs_error) matches the second field.
367 TEST(DoubleNear2Test, MatchesEqualArguments) {
368  typedef ::std::tuple<double, double> Tpl;
369  Matcher<const Tpl&> m = DoubleNear(0.5);
370  EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));
371  EXPECT_TRUE(m.Matches(Tpl(1.3, 1.0)));
372  EXPECT_FALSE(m.Matches(Tpl(1.8, 1.0)));
373 }
374 
375 // Tests that DoubleNear() describes itself properly.
376 TEST(DoubleNear2Test, CanDescribeSelf) {
377  Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5);
378  EXPECT_EQ("are an almost-equal pair", Describe(m));
379 }
380 
381 // Tests that NanSensitiveDoubleNear() matches a 2-tuple where
382 // NanSensitiveDoubleNear(first field) matches the second field.
383 TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {
384  typedef ::std::tuple<double, double> Tpl;
385  Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f);
386  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
387  EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));
388  EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
389  std::numeric_limits<double>::quiet_NaN())));
390  EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));
391  EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
392  EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
393 }
394 
395 // Tests that NanSensitiveDoubleNear() describes itself properly.
396 TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
397  Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f);
398  EXPECT_EQ("are an almost-equal pair", Describe(m));
399 }
400 
401 // Tests that Not(m) matches any value that doesn't match m.
402 TEST(NotTest, NegatesMatcher) {
403  Matcher<int> m;
404  m = Not(Eq(2));
405  EXPECT_TRUE(m.Matches(3));
406  EXPECT_FALSE(m.Matches(2));
407 }
408 
409 // Tests that Not(m) describes itself properly.
410 TEST(NotTest, CanDescribeSelf) {
411  Matcher<int> m = Not(Eq(5));
412  EXPECT_EQ("isn't equal to 5", Describe(m));
413 }
414 
415 // Tests that monomorphic matchers are safely cast by the Not matcher.
416 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
417  // greater_than_5 is a monomorphic matcher.
418  Matcher<int> greater_than_5 = Gt(5);
419 
420  Matcher<const int&> m = Not(greater_than_5);
421  Matcher<int&> m2 = Not(greater_than_5);
422  Matcher<int&> m3 = Not(m);
423 }
424 
425 // Helper to allow easy testing of AllOf matchers with num parameters.
426 void AllOfMatches(int num, const Matcher<int>& m) {
428  EXPECT_TRUE(m.Matches(0));
429  for (int i = 1; i <= num; ++i) {
430  EXPECT_FALSE(m.Matches(i));
431  }
432  EXPECT_TRUE(m.Matches(num + 1));
433 }
434 
436 
437 // Tests that AllOf(m1, ..., mn) matches any value that matches all of
438 // the given matchers.
439 TEST(AllOfTest, MatchesWhenAllMatch) {
440  Matcher<int> m;
441  m = AllOf(Le(2), Ge(1));
442  EXPECT_TRUE(m.Matches(1));
443  EXPECT_TRUE(m.Matches(2));
444  EXPECT_FALSE(m.Matches(0));
445  EXPECT_FALSE(m.Matches(3));
446 
447  m = AllOf(Gt(0), Ne(1), Ne(2));
448  EXPECT_TRUE(m.Matches(3));
449  EXPECT_FALSE(m.Matches(2));
450  EXPECT_FALSE(m.Matches(1));
451  EXPECT_FALSE(m.Matches(0));
452 
453  m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
454  EXPECT_TRUE(m.Matches(4));
455  EXPECT_FALSE(m.Matches(3));
456  EXPECT_FALSE(m.Matches(2));
457  EXPECT_FALSE(m.Matches(1));
458  EXPECT_FALSE(m.Matches(0));
459 
460  m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
461  EXPECT_TRUE(m.Matches(0));
462  EXPECT_TRUE(m.Matches(1));
463  EXPECT_FALSE(m.Matches(3));
464 
465  // The following tests for varying number of sub-matchers. Due to the way
466  // the sub-matchers are handled it is enough to test every sub-matcher once
467  // with sub-matchers using the same matcher type. Varying matcher types are
468  // checked for above.
469  AllOfMatches(2, AllOf(Ne(1), Ne(2)));
470  AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
471  AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
472  AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
473  AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
474  AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
475  AllOfMatches(8,
476  AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8)));
477  AllOfMatches(
478  9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9)));
479  AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
480  Ne(9), Ne(10)));
481  AllOfMatches(
482  50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
483  Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17),
484  Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25),
485  Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33),
486  Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41),
487  Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),
488  Ne(50)));
489 }
490 
491 // Tests that AllOf(m1, ..., mn) describes itself properly.
492 TEST(AllOfTest, CanDescribeSelf) {
493  Matcher<int> m;
494  m = AllOf(Le(2), Ge(1));
495  EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
496 
497  m = AllOf(Gt(0), Ne(1), Ne(2));
498  std::string expected_descr1 =
499  "(is > 0) and (isn't equal to 1) and (isn't equal to 2)";
500  EXPECT_EQ(expected_descr1, Describe(m));
501 
502  m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
503  std::string expected_descr2 =
504  "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "
505  "to 3)";
506  EXPECT_EQ(expected_descr2, Describe(m));
507 
508  m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
509  std::string expected_descr3 =
510  "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "
511  "and (isn't equal to 7)";
512  EXPECT_EQ(expected_descr3, Describe(m));
513 }
514 
515 // Tests that AllOf(m1, ..., mn) describes its negation properly.
516 TEST(AllOfTest, CanDescribeNegation) {
517  Matcher<int> m;
518  m = AllOf(Le(2), Ge(1));
519  std::string expected_descr4 = "(isn't <= 2) or (isn't >= 1)";
520  EXPECT_EQ(expected_descr4, DescribeNegation(m));
521 
522  m = AllOf(Gt(0), Ne(1), Ne(2));
523  std::string expected_descr5 =
524  "(isn't > 0) or (is equal to 1) or (is equal to 2)";
525  EXPECT_EQ(expected_descr5, DescribeNegation(m));
526 
527  m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
528  std::string expected_descr6 =
529  "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";
530  EXPECT_EQ(expected_descr6, DescribeNegation(m));
531 
532  m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
533  std::string expected_desr7 =
534  "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "
535  "(is equal to 7)";
536  EXPECT_EQ(expected_desr7, DescribeNegation(m));
537 
538  m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
539  Ne(10), Ne(11));
540  AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
541  EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11)"));
542  AllOfMatches(11, m);
543 }
544 
545 // Tests that monomorphic matchers are safely cast by the AllOf matcher.
546 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
547  // greater_than_5 and less_than_10 are monomorphic matchers.
548  Matcher<int> greater_than_5 = Gt(5);
549  Matcher<int> less_than_10 = Lt(10);
550 
551  Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
552  Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
553  Matcher<int&> m3 = AllOf(greater_than_5, m2);
554 
555  // Tests that BothOf works when composing itself.
556  Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
557  Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
558 }
559 
560 TEST_P(AllOfTestP, ExplainsResult) {
561  Matcher<int> m;
562 
563  // Successful match. Both matchers need to explain. The second
564  // matcher doesn't give an explanation, so the matcher description is used.
565  m = AllOf(GreaterThan(10), Lt(30));
566  EXPECT_EQ("which is 15 more than 10, and is < 30", Explain(m, 25));
567 
568  // Successful match. Both matchers need to explain.
569  m = AllOf(GreaterThan(10), GreaterThan(20));
570  EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
571  Explain(m, 30));
572 
573  // Successful match. All matchers need to explain. The second
574  // matcher doesn't given an explanation.
575  m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
576  EXPECT_EQ(
577  "which is 15 more than 10, and is < 30, and which is 5 more than 20",
578  Explain(m, 25));
579 
580  // Successful match. All matchers need to explain.
581  m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
582  EXPECT_EQ(
583  "which is 30 more than 10, and which is 20 more than 20, "
584  "and which is 10 more than 30",
585  Explain(m, 40));
586 
587  // Failed match. The first matcher, which failed, needs to
588  // explain.
589  m = AllOf(GreaterThan(10), GreaterThan(20));
590  EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
591 
592  // Failed match. The second matcher, which failed, needs to
593  // explain. Since it doesn't given an explanation, the matcher text is
594  // printed.
595  m = AllOf(GreaterThan(10), Lt(30));
596  EXPECT_EQ("which doesn't match (is < 30)", Explain(m, 40));
597 
598  // Failed match. The second matcher, which failed, needs to
599  // explain.
600  m = AllOf(GreaterThan(10), GreaterThan(20));
601  EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
602 }
603 
604 // Helper to allow easy testing of AnyOf matchers with num parameters.
605 static void AnyOfMatches(int num, const Matcher<int>& m) {
607  EXPECT_FALSE(m.Matches(0));
608  for (int i = 1; i <= num; ++i) {
609  EXPECT_TRUE(m.Matches(i));
610  }
611  EXPECT_FALSE(m.Matches(num + 1));
612 }
613 
614 static void AnyOfStringMatches(int num, const Matcher<std::string>& m) {
616  EXPECT_FALSE(m.Matches(std::to_string(0)));
617 
618  for (int i = 1; i <= num; ++i) {
619  EXPECT_TRUE(m.Matches(std::to_string(i)));
620  }
621  EXPECT_FALSE(m.Matches(std::to_string(num + 1)));
622 }
623 
625 
626 // Tests that AnyOf(m1, ..., mn) matches any value that matches at
627 // least one of the given matchers.
628 TEST(AnyOfTest, MatchesWhenAnyMatches) {
629  Matcher<int> m;
630  m = AnyOf(Le(1), Ge(3));
631  EXPECT_TRUE(m.Matches(1));
632  EXPECT_TRUE(m.Matches(4));
633  EXPECT_FALSE(m.Matches(2));
634 
635  m = AnyOf(Lt(0), Eq(1), Eq(2));
636  EXPECT_TRUE(m.Matches(-1));
637  EXPECT_TRUE(m.Matches(1));
638  EXPECT_TRUE(m.Matches(2));
639  EXPECT_FALSE(m.Matches(0));
640 
641  m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
642  EXPECT_TRUE(m.Matches(-1));
643  EXPECT_TRUE(m.Matches(1));
644  EXPECT_TRUE(m.Matches(2));
645  EXPECT_TRUE(m.Matches(3));
646  EXPECT_FALSE(m.Matches(0));
647 
648  m = AnyOf(Le(0), Gt(10), 3, 5, 7);
649  EXPECT_TRUE(m.Matches(0));
650  EXPECT_TRUE(m.Matches(11));
651  EXPECT_TRUE(m.Matches(3));
652  EXPECT_FALSE(m.Matches(2));
653 
654  // The following tests for varying number of sub-matchers. Due to the way
655  // the sub-matchers are handled it is enough to test every sub-matcher once
656  // with sub-matchers using the same matcher type. Varying matcher types are
657  // checked for above.
658  AnyOfMatches(2, AnyOf(1, 2));
659  AnyOfMatches(3, AnyOf(1, 2, 3));
660  AnyOfMatches(4, AnyOf(1, 2, 3, 4));
661  AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
662  AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
663  AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
664  AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
665  AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
666  AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
667 }
668 
669 // Tests the variadic version of the AnyOfMatcher.
670 TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
671  // Also make sure AnyOf is defined in the right namespace and does not depend
672  // on ADL.
673  Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
674 
675  EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)"));
676  AnyOfMatches(11, m);
677  AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
678  17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
679  31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
680  45, 46, 47, 48, 49, 50));
681  AnyOfStringMatches(
682  50, AnyOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
683  "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
684  "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
685  "33", "34", "35", "36", "37", "38", "39", "40", "41", "42",
686  "43", "44", "45", "46", "47", "48", "49", "50"));
687 }
688 
689 TEST(ConditionalTest, MatchesFirstIfCondition) {
690  Matcher<std::string> eq_red = Eq("red");
691  Matcher<std::string> ne_red = Ne("red");
692  Matcher<std::string> m = Conditional(true, eq_red, ne_red);
693  EXPECT_TRUE(m.Matches("red"));
694  EXPECT_FALSE(m.Matches("green"));
695 
696  StringMatchResultListener listener;
697  StringMatchResultListener expected;
698  EXPECT_FALSE(m.MatchAndExplain("green", &listener));
699  EXPECT_FALSE(eq_red.MatchAndExplain("green", &expected));
700  EXPECT_THAT(listener.str(), Eq(expected.str()));
701 }
702 
703 TEST(ConditionalTest, MatchesSecondIfCondition) {
704  Matcher<std::string> eq_red = Eq("red");
705  Matcher<std::string> ne_red = Ne("red");
706  Matcher<std::string> m = Conditional(false, eq_red, ne_red);
707  EXPECT_FALSE(m.Matches("red"));
708  EXPECT_TRUE(m.Matches("green"));
709 
710  StringMatchResultListener listener;
711  StringMatchResultListener expected;
712  EXPECT_FALSE(m.MatchAndExplain("red", &listener));
713  EXPECT_FALSE(ne_red.MatchAndExplain("red", &expected));
714  EXPECT_THAT(listener.str(), Eq(expected.str()));
715 }
716 
717 // Tests that AnyOf(m1, ..., mn) describes itself properly.
718 TEST(AnyOfTest, CanDescribeSelf) {
719  Matcher<int> m;
720  m = AnyOf(Le(1), Ge(3));
721 
722  EXPECT_EQ("(is <= 1) or (is >= 3)", Describe(m));
723 
724  m = AnyOf(Lt(0), Eq(1), Eq(2));
725  EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m));
726 
727  m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
728  EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",
729  Describe(m));
730 
731  m = AnyOf(Le(0), Gt(10), 3, 5, 7);
732  EXPECT_EQ(
733  "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
734  "equal to 7)",
735  Describe(m));
736 }
737 
738 // Tests that AnyOf(m1, ..., mn) describes its negation properly.
739 TEST(AnyOfTest, CanDescribeNegation) {
740  Matcher<int> m;
741  m = AnyOf(Le(1), Ge(3));
742  EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", DescribeNegation(m));
743 
744  m = AnyOf(Lt(0), Eq(1), Eq(2));
745  EXPECT_EQ("(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",
746  DescribeNegation(m));
747 
748  m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
749  EXPECT_EQ(
750  "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
751  "equal to 3)",
752  DescribeNegation(m));
753 
754  m = AnyOf(Le(0), Gt(10), 3, 5, 7);
755  EXPECT_EQ(
756  "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "
757  "to 5) and (isn't equal to 7)",
758  DescribeNegation(m));
759 }
760 
761 // Tests that monomorphic matchers are safely cast by the AnyOf matcher.
762 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
763  // greater_than_5 and less_than_10 are monomorphic matchers.
764  Matcher<int> greater_than_5 = Gt(5);
765  Matcher<int> less_than_10 = Lt(10);
766 
767  Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
768  Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
769  Matcher<int&> m3 = AnyOf(greater_than_5, m2);
770 
771  // Tests that EitherOf works when composing itself.
772  Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
773  Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
774 }
775 
776 TEST_P(AnyOfTestP, ExplainsResult) {
777  Matcher<int> m;
778 
779  // Failed match. The second matcher have no explanation (description is used).
780  m = AnyOf(GreaterThan(10), Lt(0));
781  EXPECT_EQ("which is 5 less than 10, and isn't < 0", Explain(m, 5));
782 
783  // Failed match. Both matchers have explanations.
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. The middle matcher have no explanation.
789  m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
790  EXPECT_EQ(
791  "which is 5 less than 10, and isn't > 20, and which is 25 less than 30",
792  Explain(m, 5));
793 
794  // Failed match. All three matchers have explanations.
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 macher succeeded and has explanation.
802  m = AnyOf(GreaterThan(10), GreaterThan(20));
803  EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
804 
805  // Successful match. The second matcher succeeded and has explanation.
806  m = AnyOf(GreaterThan(30), GreaterThan(20));
807  EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
808 
809  // Successful match. The first matcher succeeded and has no explanation.
810  m = AnyOf(Gt(10), Lt(20));
811  EXPECT_EQ("which matches (is > 10)", Explain(m, 15));
812 
813  // Successful match. The second matcher succeeded and has no explanation.
814  m = AnyOf(Gt(30), Gt(20));
815  EXPECT_EQ("which matches (is > 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.
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:
831  explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
832 
833  bool operator()(int n) const { return n > threshold_; }
834 
835  private:
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.
844 bool ReferencesFooAndIsZero(const int& n) { return (&n == &foo) && (n == 0); }
845 
846 // Tests that Truly(predicate) matches what satisfies the given
847 // predicate.
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.
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:
864  explicit ConvertibleToBool(int number) : number_(number) {}
865  operator bool() const { return number_ != 0; }
866 
867  private:
868  int number_;
869 };
870 
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!().
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.
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.
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.
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.
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.
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().
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().
930 TEST(ValueTest, WorksWithPolymorphicMatcher) {
931  EXPECT_TRUE(Value("hi", StartsWith("h")));
932  EXPECT_FALSE(Value(5, Gt(10)));
933 }
934 
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 
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 
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 
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 
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:
1021  typedef typename Floating::Bits Bits;
1022 
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()),
1029  Floating::ReinterpretBits(zero_bits_ + max_ulps_ / 2)),
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()),
1038  Floating::ReinterpretBits(infinity_bits_ - max_ulps_)),
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 
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.
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_);
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.
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.
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));
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));
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_));
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_));
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));
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 
1209 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { TestMatches(&FloatEq); }
1210 
1211 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
1212  TestMatches(&NanSensitiveFloatEq);
1213 }
1214 
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 
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 
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 
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 
1263 TEST_F(FloatNearTest, FloatNearMatches) { TestNearMatches(&FloatNear); }
1264 
1265 TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
1266  TestNearMatches(&NanSensitiveFloatNear);
1267 }
1268 
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 
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 
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 
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 
1320 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
1321  TestMatches(&DoubleEq);
1322 }
1323 
1324 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
1325  TestMatches(&NanSensitiveDoubleEq);
1326 }
1327 
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 
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 
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 
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 
1376 TEST_F(DoubleNearTest, DoubleNearMatches) { TestNearMatches(&DoubleNear); }
1377 
1378 TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
1379  TestNearMatches(&NanSensitiveDoubleNear);
1380 }
1381 
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 
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 
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 
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 
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 
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 
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 
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>
1479 bool AllOf(const T1& /*t1*/, const T2& /*t2*/) {
1480  return true;
1481 }
1482 
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>
1489 bool AnyOf(const T1&, const T2&) {
1490  return true;
1491 }
1492 
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 
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 
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
constexpr bool StartsWith(const char(&prefix)[N], const char(&str)[M])
const Bits one_bits_
const RawType infinity_
void f()
std::string Explain(const MatcherType &m, const Value &x)
constexpr bool EndsWith(const char(&suffix)[N], const char(&str)[M])
const RawType nan1_
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2224
#define MOCK_METHOD0(m,...)
#define TEST(test_suite_name, test_name)
Definition: gtest.h:2192
#define ON_CALL(obj, call)
#define INSTANTIATE_GTEST_MATCHER_TEST_P(TestSuite)
const RawType nan2_
const int max_
#define MATCHER(name, description)
std::string Describe(const Matcher< T > &m)
#define MOCK_METHOD2(m,...)
const RawType close_to_negative_zero_
const Bits zero_bits_
const Bits infinity_bits_
#define T2(r, f)
Definition: Sacado_rad.hpp:558
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
Definition: gtest-port.h:377
const Bits max_ulps_
const char * p
ADVar foo(double d, ADVar x, ADVar y)
#define T1(r, f)
Definition: Sacado_rad.hpp:583
#define MOCK_METHOD1(m,...)
const RawType close_to_one_
const RawType further_from_one_
void
Definition: uninit.c:105
const RawType close_to_infinity_
#define SCOPED_TRACE(message)
Definition: gtest.h:2120
#define EXPECT_THAT(value, matcher)
internal::ReturnAction< R > Return(R value)
#define EXPECT_CALL(obj, call)
std::string DescribeNegation(const Matcher< T > &m)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1884
#define TEST_P(test_suite_name, test_name)
SimpleFad< ValueT > max(const SimpleFad< ValueT > &a, const SimpleFad< ValueT > &b)
const double y
#define EXPECT_TRUE(condition)
Definition: gtest.h:1823
const RawType close_to_positive_zero_
const RawType further_from_infinity_
#define EXPECT_FALSE(condition)
Definition: gtest.h:1827
int n
const RawType further_from_negative_zero_
static ExpectedAnswer expected[4]