1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Google Mock - a framework for writing C++ mock classes.
32 // This file tests some commonly used argument matchers.
39 #include "test/gmock-matchers_test.h"
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)
46 namespace gmock_matchers_test {
49 typedef ::std::tuple<long, int> Tuple2; // NOLINT
51 // Tests that Eq() matches a 2-tuple where the first field == the
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)));
59 // Tests that Eq() describes itself properly.
60 TEST(Eq2Test, CanDescribeSelf) {
61 Matcher<const Tuple2&> m = Eq();
62 EXPECT_EQ("are an equal pair", Describe(m));
65 // Tests that Ge() matches a 2-tuple where the first field >= the
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)));
74 // Tests that Ge() describes itself properly.
75 TEST(Ge2Test, CanDescribeSelf) {
76 Matcher<const Tuple2&> m = Ge();
77 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
80 // Tests that Gt() matches a 2-tuple where the first field > the
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)));
89 // Tests that Gt() describes itself properly.
90 TEST(Gt2Test, CanDescribeSelf) {
91 Matcher<const Tuple2&> m = Gt();
92 EXPECT_EQ("are a pair where the first > the second", Describe(m));
95 // Tests that Le() matches a 2-tuple where the first field <= the
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)));
104 // Tests that Le() describes itself properly.
105 TEST(Le2Test, CanDescribeSelf) {
106 Matcher<const Tuple2&> m = Le();
107 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
110 // Tests that Lt() matches a 2-tuple where the first field < the
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)));
119 // Tests that Lt() describes itself properly.
120 TEST(Lt2Test, CanDescribeSelf) {
121 Matcher<const Tuple2&> m = Lt();
122 EXPECT_EQ("are a pair where the first < the second", Describe(m));
125 // Tests that Ne() matches a 2-tuple where the first field != the
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)));
134 // Tests that Ne() describes itself properly.
135 TEST(Ne2Test, CanDescribeSelf) {
136 Matcher<const Tuple2&> m = Ne();
137 EXPECT_EQ("are an unequal pair", Describe(m));
140 TEST(PairMatchBaseTest, WorksWithMoveOnly) {
141 using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
142 Matcher<Pointers> matcher = Eq();
144 // Tested values don't matter; the point is that matcher does not copy the
146 EXPECT_TRUE(matcher.Matches(pointers));
149 // Tests that IsNan() matches a NaN, with float.
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;
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));
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));
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));
171 // Tests that IsNan() matches a NaN, with double.
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;
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));
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));
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));
193 // Tests that IsNan() matches a NaN, with long double.
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;
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));
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));
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));
215 // Tests that IsNan() works with Not.
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));
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));
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));
233 // Tests that IsNan() can describe itself.
234 TEST(IsNan, CanDescribeSelf) {
235 Matcher<float> mf = IsNan();
236 EXPECT_EQ("is NaN", Describe(mf));
238 Matcher<double> md = IsNan();
239 EXPECT_EQ("is NaN", Describe(md));
241 Matcher<long double> mld = IsNan();
242 EXPECT_EQ("is NaN", Describe(mld));
245 // Tests that IsNan() can describe itself with Not.
246 TEST(IsNan, CanDescribeSelfWithNot) {
247 Matcher<float> mf = Not(IsNan());
248 EXPECT_EQ("isn't NaN", Describe(mf));
250 Matcher<double> md = Not(IsNan());
251 EXPECT_EQ("isn't NaN", Describe(md));
253 Matcher<long double> mld = Not(IsNan());
254 EXPECT_EQ("isn't NaN", Describe(mld));
257 // Tests that FloatEq() matches a 2-tuple where
258 // FloatEq(first field) matches the second field.
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)));
267 // Tests that FloatEq() describes itself properly.
268 TEST(FloatEq2Test, CanDescribeSelf) {
269 Matcher<const ::std::tuple<float, float>&> m = FloatEq();
270 EXPECT_EQ("are an almost-equal pair", Describe(m));
273 // Tests that NanSensitiveFloatEq() matches a 2-tuple where
274 // NanSensitiveFloatEq(first field) matches the second field.
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)));
286 // Tests that NanSensitiveFloatEq() describes itself properly.
287 TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
288 Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq();
289 EXPECT_EQ("are an almost-equal pair", Describe(m));
292 // Tests that DoubleEq() matches a 2-tuple where
293 // DoubleEq(first field) matches the second field.
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)));
302 // Tests that DoubleEq() describes itself properly.
303 TEST(DoubleEq2Test, CanDescribeSelf) {
304 Matcher<const ::std::tuple<double, double>&> m = DoubleEq();
305 EXPECT_EQ("are an almost-equal pair", Describe(m));
308 // Tests that NanSensitiveDoubleEq() matches a 2-tuple where
309 // NanSensitiveDoubleEq(first field) matches the second field.
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)));
321 // Tests that DoubleEq() describes itself properly.
322 TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
323 Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq();
324 EXPECT_EQ("are an almost-equal pair", Describe(m));
327 // Tests that FloatEq() matches a 2-tuple where
328 // FloatNear(first field, max_abs_error) matches the second field.
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)));
337 // Tests that FloatNear() describes itself properly.
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));
343 // Tests that NanSensitiveFloatNear() matches a 2-tuple where
344 // NanSensitiveFloatNear(first field) matches the second field.
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)));
357 // Tests that NanSensitiveFloatNear() describes itself properly.
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));
363 // Tests that FloatEq() matches a 2-tuple where
364 // DoubleNear(first field, max_abs_error) matches the second field.
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)));
373 // Tests that DoubleNear() describes itself properly.
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));
379 // Tests that NanSensitiveDoubleNear() matches a 2-tuple where
380 // NanSensitiveDoubleNear(first field) matches the second field.
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)));
393 // Tests that NanSensitiveDoubleNear() describes itself properly.
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));
399 // Tests that Not(m) matches any value that doesn't match m.
400 TEST(NotTest, NegatesMatcher) {
403 EXPECT_TRUE(m.Matches(3));
404 EXPECT_FALSE(m.Matches(2));
407 // Tests that Not(m) describes itself properly.
408 TEST(NotTest, CanDescribeSelf) {
409 Matcher<int> m = Not(Eq(5));
410 EXPECT_EQ("isn't equal to 5", Describe(m));
413 // Tests that monomorphic matchers are safely cast by the Not matcher.
414 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
415 // greater_than_5 is a monomorphic matcher.
416 Matcher<int> greater_than_5 = Gt(5);
418 Matcher<const int&> m = Not(greater_than_5);
419 Matcher<int&> m2 = Not(greater_than_5);
420 Matcher<int&> m3 = Not(m);
423 // Helper to allow easy testing of AllOf matchers with num parameters.
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));
430 EXPECT_TRUE(m.Matches(num + 1));
433 INSTANTIATE_GTEST_MATCHER_TEST_P(AllOfTest);
435 // Tests that AllOf(m1, ..., mn) matches any value that matches all of
436 // the given matchers.
437 TEST(AllOfTest, MatchesWhenAllMatch) {
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));
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));
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));
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));
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)));
474 AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8)));
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),
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),
489 // Tests that AllOf(m1, ..., mn) describes itself properly.
490 TEST(AllOfTest, CanDescribeSelf) {
492 m = AllOf(Le(2), Ge(1));
493 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
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));
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 "
504 EXPECT_EQ(expected_descr2, Describe(m));
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));
513 // Tests that AllOf(m1, ..., mn) describes its negation properly.
514 TEST(AllOfTest, CanDescribeNegation) {
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));
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));
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));
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 "
534 EXPECT_EQ(expected_desr7, DescribeNegation(m));
536 m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
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)"));
543 // Tests that monomorphic matchers are safely cast by the AllOf matcher.
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);
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);
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);
558 TEST_P(AllOfTestP, ExplainsResult) {
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));
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",
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",
578 // Successful match. All matchers need to explain.
579 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
581 "which is 30 more than 10, and which is 20 more than 20, "
582 "and which is 10 more than 30",
585 // Failed match. The first matcher, which failed, needs to
587 m = AllOf(GreaterThan(10), GreaterThan(20));
588 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
590 // Failed match. The second matcher, which failed, needs to
591 // explain. Since it doesn't given an explanation, nothing is
593 m = AllOf(GreaterThan(10), Lt(30));
594 EXPECT_EQ("", Explain(m, 40));
596 // Failed match. The second matcher, which failed, needs to
598 m = AllOf(GreaterThan(10), GreaterThan(20));
599 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
602 // Helper to allow easy testing of AnyOf matchers with num parameters.
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));
609 EXPECT_FALSE(m.Matches(num + 1));
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)));
616 for (int i = 1; i <= num; ++i) {
617 EXPECT_TRUE(m.Matches(std::to_string(i)));
619 EXPECT_FALSE(m.Matches(std::to_string(num + 1)));
622 INSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfTest);
624 // Tests that AnyOf(m1, ..., mn) matches any value that matches at
625 // least one of the given matchers.
626 TEST(AnyOfTest, MatchesWhenAnyMatches) {
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));
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));
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));
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));
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));
667 // Tests the variadic version of the AnyOfMatcher.
668 TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
669 // Also make sure AnyOf is defined in the right namespace and does not depend
671 Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
673 EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)"));
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));
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"));
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"));
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()));
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"));
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()));
715 // Tests that AnyOf(m1, ..., mn) describes itself properly.
716 TEST(AnyOfTest, CanDescribeSelf) {
718 m = AnyOf(Le(1), Ge(3));
720 EXPECT_EQ("(is <= 1) or (is >= 3)", Describe(m));
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));
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)",
729 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
731 "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
736 // Tests that AnyOf(m1, ..., mn) describes its negation properly.
737 TEST(AnyOfTest, CanDescribeNegation) {
739 m = AnyOf(Le(1), Ge(3));
740 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", DescribeNegation(m));
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));
746 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
748 "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
750 DescribeNegation(m));
752 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
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));
759 // Tests that monomorphic matchers are safely cast by the AnyOf matcher.
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);
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);
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);
774 TEST_P(AnyOfTestP, ExplainsResult) {
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));
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",
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",
794 // Failed match. All matchers need to explain.
795 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
797 "which is 5 less than 10, and which is 15 less than 20, "
798 "and which is 25 less than 30",
801 // Successful match. The first matcher, which succeeded, needs to
803 m = AnyOf(GreaterThan(10), GreaterThan(20));
804 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
806 // Successful match. The second matcher, which succeeded, needs to
807 // explain. Since it doesn't given an explanation, nothing is
809 m = AnyOf(GreaterThan(10), Lt(30));
810 EXPECT_EQ("", Explain(m, 0));
812 // Successful match. The second matcher, which succeeded, needs to
814 m = AnyOf(GreaterThan(30), GreaterThan(20));
815 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
818 // The following predicate function and predicate functor are for
819 // testing the Truly(predicate) matcher.
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; }
827 // This functor returns true if the input is greater than the given
829 class IsGreaterThan {
831 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
833 bool operator()(int n) const { return n > threshold_; }
839 // For testing Truly().
842 // This predicate returns true if and only if the argument references foo and
844 bool ReferencesFooAndIsZero(const int& n) { return (&n == &foo) && (n == 0); }
846 // Tests that Truly(predicate) matches what satisfies the given
848 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
849 Matcher<double> m = Truly(IsPositive);
850 EXPECT_TRUE(m.Matches(2.0));
851 EXPECT_FALSE(m.Matches(-1.5));
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));
861 // A class that can be implicitly converted to bool.
862 class ConvertibleToBool {
864 explicit ConvertibleToBool(int number) : number_(number) {}
865 operator bool() const { return number_ != 0; }
871 ConvertibleToBool IsNotZero(int number) { return ConvertibleToBool(number); }
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
876 TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
877 Matcher<int> m = Truly(IsNotZero);
878 EXPECT_TRUE(m.Matches(1));
879 EXPECT_FALSE(m.Matches(0));
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));
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));
894 EXPECT_FALSE(m.Matches(n));
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");
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'));
911 // Tests that Matches(m) works when the matcher takes its argument by
913 TEST(MatchesTest, WorksOnByRefArguments) {
915 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
916 EXPECT_FALSE(Matches(Ref(m))(n));
919 // Tests that a Matcher on non-reference type can be used in
921 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
922 Matcher<int> eq5 = Eq(5);
923 EXPECT_TRUE(Matches(eq5)(5));
924 EXPECT_FALSE(Matches(eq5)(2));
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)));
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));
941 const Matcher<const int&> ref_n = Ref(n);
942 EXPECT_TRUE(Value(n, ref_n));
943 EXPECT_FALSE(Value(1, ref_n));
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())));
951 TEST(AllArgsTest, WorksForNonTuple) {
952 EXPECT_THAT(42, AllArgs(Gt(0)));
953 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
956 class AllArgsHelper {
958 AllArgsHelper() = default;
960 MOCK_METHOD2(Helper, int(char x, int y));
963 AllArgsHelper(const AllArgsHelper&) = delete;
964 AllArgsHelper& operator=(const AllArgsHelper&) = delete;
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));
973 EXPECT_EQ(1, helper.Helper('\1', 2));
974 EXPECT_EQ(2, helper.Helper('a', 1));
977 class OptionalMatchersHelper {
979 OptionalMatchersHelper() = default;
981 MOCK_METHOD0(NoArgs, int());
983 MOCK_METHOD1(OneArg, int(int y));
985 MOCK_METHOD2(TwoArgs, int(char x, int y));
987 MOCK_METHOD1(Overloaded, int(char x));
988 MOCK_METHOD2(Overloaded, int(char x, int y));
991 OptionalMatchersHelper(const OptionalMatchersHelper&) = delete;
992 OptionalMatchersHelper& operator=(const OptionalMatchersHelper&) = delete;
995 TEST(AllArgsTest, WorksWithoutMatchers) {
996 OptionalMatchersHelper helper;
998 ON_CALL(helper, NoArgs).WillByDefault(Return(10));
999 ON_CALL(helper, OneArg).WillByDefault(Return(20));
1000 ON_CALL(helper, TwoArgs).WillByDefault(Return(30));
1002 EXPECT_EQ(10, helper.NoArgs());
1003 EXPECT_EQ(20, helper.OneArg(1));
1004 EXPECT_EQ(30, helper.TwoArgs('\1', 2));
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);
1011 EXPECT_EQ(10, helper.NoArgs());
1012 EXPECT_EQ(100, helper.OneArg(1));
1013 EXPECT_EQ(200, helper.OneArg(17));
1016 // Tests floating-point matchers.
1017 template <typename RawType>
1018 class FloatingPointTest : public testing::Test {
1020 typedef testing::internal::FloatingPoint<RawType> Floating;
1021 typedef typename Floating::Bits Bits;
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()),
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)) {}
1045 void TestSize() { EXPECT_EQ(sizeof(RawType), sizeof(Bits)); }
1047 // A battery of tests for FloatingEqMatcher::Matches.
1048 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
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));
1057 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
1058 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
1060 Matcher<RawType> m3 = matcher_maker(1.0);
1061 EXPECT_TRUE(m3.Matches(close_to_one_));
1062 EXPECT_FALSE(m3.Matches(further_from_one_));
1064 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
1065 EXPECT_FALSE(m3.Matches(0.0));
1067 Matcher<RawType> m4 = matcher_maker(-infinity_);
1068 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
1070 Matcher<RawType> m5 = matcher_maker(infinity_);
1071 EXPECT_TRUE(m5.Matches(close_to_infinity_));
1073 // This is interesting as the representations of infinity_ and nan1_
1074 // are only 1 DLP apart.
1075 EXPECT_FALSE(m5.Matches(nan1_));
1077 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
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));
1084 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
1086 Matcher<RawType&> m7 = matcher_maker(0.0);
1088 EXPECT_TRUE(m7.Matches(x));
1090 EXPECT_FALSE(m7.Matches(x));
1093 // Pre-calculated numbers to be used by the tests.
1095 const Bits max_ulps_;
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.
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_;
1106 // Some numbers close to 1.0.
1107 const RawType close_to_one_;
1108 const RawType further_from_one_;
1110 // Some numbers close to +infinity.
1111 const RawType infinity_;
1112 const RawType close_to_infinity_;
1113 const RawType further_from_infinity_;
1115 // Maximum representable value that's not infinity.
1119 const RawType nan1_;
1120 const RawType nan2_;
1123 // Tests floating-point matchers with fixed epsilons.
1124 template <typename RawType>
1125 class FloatingPointNearTest : public FloatingPointTest<RawType> {
1127 typedef FloatingPointTest<RawType> ParentType;
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));
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));
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_));
1148 // Check that inf matches inf, regardless of the of the specified max
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_));
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_));
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_));
1165 Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_);
1166 EXPECT_FALSE(m6.Matches(ParentType::max_));
1167 EXPECT_TRUE(m6.Matches(-ParentType::max_));
1169 Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0);
1170 EXPECT_TRUE(m7.Matches(ParentType::max_));
1171 EXPECT_FALSE(m7.Matches(-ParentType::max_));
1173 Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0);
1174 EXPECT_FALSE(m8.Matches(ParentType::max_));
1175 EXPECT_TRUE(m8.Matches(-ParentType::max_));
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_));
1183 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
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_));
1190 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
1192 Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
1194 EXPECT_TRUE(m11.Matches(x));
1196 EXPECT_TRUE(m11.Matches(x));
1198 EXPECT_TRUE(m11.Matches(x));
1200 EXPECT_FALSE(m11.Matches(x));
1202 EXPECT_FALSE(m11.Matches(x));
1206 // Instantiate FloatingPointTest for testing floats.
1207 typedef FloatingPointTest<float> FloatTest;
1209 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { TestMatches(&FloatEq); }
1211 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
1212 TestMatches(&NanSensitiveFloatEq);
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));
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));
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));
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));
1240 Matcher<float> m3 = FloatEq(nan1_);
1241 EXPECT_EQ("never matches", Describe(m3));
1242 EXPECT_EQ("is anything", DescribeNegation(m3));
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));
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));
1254 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
1255 EXPECT_EQ("is NaN", Describe(m3));
1256 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
1259 // Instantiate FloatingPointTest for testing floats with a user-specified
1260 // max absolute error.
1261 typedef FloatingPointNearTest<float> FloatNearTest;
1263 TEST_F(FloatNearTest, FloatNearMatches) { TestNearMatches(&FloatNear); }
1265 TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
1266 TestNearMatches(&NanSensitiveFloatNear);
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));
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));
1280 Matcher<float> m3 = FloatNear(nan1_, 0.0);
1281 EXPECT_EQ("never matches", Describe(m3));
1282 EXPECT_EQ("is anything", DescribeNegation(m3));
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));
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));
1296 Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f);
1297 EXPECT_EQ("is NaN", Describe(m3));
1298 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
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));
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));
1317 // Instantiate FloatingPointTest for testing doubles.
1318 typedef FloatingPointTest<double> DoubleTest;
1320 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
1321 TestMatches(&DoubleEq);
1324 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
1325 TestMatches(&NanSensitiveDoubleEq);
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));
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));
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));
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));
1353 Matcher<double> m3 = DoubleEq(nan1_);
1354 EXPECT_EQ("never matches", Describe(m3));
1355 EXPECT_EQ("is anything", DescribeNegation(m3));
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));
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));
1367 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
1368 EXPECT_EQ("is NaN", Describe(m3));
1369 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
1372 // Instantiate FloatingPointTest for testing floats with a user-specified
1373 // max absolute error.
1374 typedef FloatingPointNearTest<double> DoubleNearTest;
1376 TEST_F(DoubleNearTest, DoubleNearMatches) { TestNearMatches(&DoubleNear); }
1378 TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
1379 TestNearMatches(&NanSensitiveDoubleNear);
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));
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));
1393 Matcher<double> m3 = DoubleNear(nan1_, 0.0);
1394 EXPECT_EQ("never matches", Describe(m3));
1395 EXPECT_EQ("is anything", DescribeNegation(m3));
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));
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 << "\".";
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));
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));
1423 Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1);
1424 EXPECT_EQ("is NaN", Describe(m3));
1425 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
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));
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));
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))));
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(_, _, _, _, _, _, _, _, _, _)));
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(_, _, _, _, _, _, _, _, _, _)));
1464 namespace adl_test {
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.
1471 // The matcher must be in the same namespace as AllOf/AnyOf to make argument
1472 // dependent lookup find those.
1478 template <typename T1, typename T2>
1479 bool AllOf(const T1& /*t1*/, const T2& /*t2*/) {
1483 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1485 testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1488 template <typename T1, typename T2>
1489 bool AnyOf(const T1&, const T2&) {
1493 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1495 testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1498 } // namespace adl_test
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)))));
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)))));
1513 } // namespace gmock_matchers_test
1514 } // namespace testing
1516 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100