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.
34 // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
35 // possible loss of data and C4100, unreferenced local parameter
38 #pragma warning(disable : 4244)
39 #pragma warning(disable : 4100)
42 #include "test/gmock-matchers_test.h"
45 namespace gmock_matchers_test {
48 typedef ::std::tuple<long, int> Tuple2; // NOLINT
50 // Tests that Eq() matches a 2-tuple where the first field == the
52 TEST(Eq2Test, MatchesEqualArguments) {
53 Matcher<const Tuple2&> m = Eq();
54 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
55 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
58 // Tests that Eq() describes itself properly.
59 TEST(Eq2Test, CanDescribeSelf) {
60 Matcher<const Tuple2&> m = Eq();
61 EXPECT_EQ("are an equal pair", Describe(m));
64 // Tests that Ge() matches a 2-tuple where the first field >= the
66 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
67 Matcher<const Tuple2&> m = Ge();
68 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
69 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
70 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
73 // Tests that Ge() describes itself properly.
74 TEST(Ge2Test, CanDescribeSelf) {
75 Matcher<const Tuple2&> m = Ge();
76 EXPECT_EQ("are a pair where the first >= the second", Describe(m));
79 // Tests that Gt() matches a 2-tuple where the first field > the
81 TEST(Gt2Test, MatchesGreaterThanArguments) {
82 Matcher<const Tuple2&> m = Gt();
83 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
84 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
85 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
88 // Tests that Gt() describes itself properly.
89 TEST(Gt2Test, CanDescribeSelf) {
90 Matcher<const Tuple2&> m = Gt();
91 EXPECT_EQ("are a pair where the first > the second", Describe(m));
94 // Tests that Le() matches a 2-tuple where the first field <= the
96 TEST(Le2Test, MatchesLessThanOrEqualArguments) {
97 Matcher<const Tuple2&> m = Le();
98 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
99 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
100 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
103 // Tests that Le() describes itself properly.
104 TEST(Le2Test, CanDescribeSelf) {
105 Matcher<const Tuple2&> m = Le();
106 EXPECT_EQ("are a pair where the first <= the second", Describe(m));
109 // Tests that Lt() matches a 2-tuple where the first field < the
111 TEST(Lt2Test, MatchesLessThanArguments) {
112 Matcher<const Tuple2&> m = Lt();
113 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
114 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
115 EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
118 // Tests that Lt() describes itself properly.
119 TEST(Lt2Test, CanDescribeSelf) {
120 Matcher<const Tuple2&> m = Lt();
121 EXPECT_EQ("are a pair where the first < the second", Describe(m));
124 // Tests that Ne() matches a 2-tuple where the first field != the
126 TEST(Ne2Test, MatchesUnequalArguments) {
127 Matcher<const Tuple2&> m = Ne();
128 EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
129 EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
130 EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
133 // Tests that Ne() describes itself properly.
134 TEST(Ne2Test, CanDescribeSelf) {
135 Matcher<const Tuple2&> m = Ne();
136 EXPECT_EQ("are an unequal pair", Describe(m));
139 TEST(PairMatchBaseTest, WorksWithMoveOnly) {
140 using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
141 Matcher<Pointers> matcher = Eq();
143 // Tested values don't matter; the point is that matcher does not copy the
145 EXPECT_TRUE(matcher.Matches(pointers));
148 // Tests that IsNan() matches a NaN, with float.
149 TEST(IsNan, FloatMatchesNan) {
150 float quiet_nan = std::numeric_limits<float>::quiet_NaN();
151 float other_nan = std::nanf("1");
152 float real_value = 1.0f;
154 Matcher<float> m = IsNan();
155 EXPECT_TRUE(m.Matches(quiet_nan));
156 EXPECT_TRUE(m.Matches(other_nan));
157 EXPECT_FALSE(m.Matches(real_value));
159 Matcher<float&> m_ref = IsNan();
160 EXPECT_TRUE(m_ref.Matches(quiet_nan));
161 EXPECT_TRUE(m_ref.Matches(other_nan));
162 EXPECT_FALSE(m_ref.Matches(real_value));
164 Matcher<const float&> m_cref = IsNan();
165 EXPECT_TRUE(m_cref.Matches(quiet_nan));
166 EXPECT_TRUE(m_cref.Matches(other_nan));
167 EXPECT_FALSE(m_cref.Matches(real_value));
170 // Tests that IsNan() matches a NaN, with double.
171 TEST(IsNan, DoubleMatchesNan) {
172 double quiet_nan = std::numeric_limits<double>::quiet_NaN();
173 double other_nan = std::nan("1");
174 double real_value = 1.0;
176 Matcher<double> m = IsNan();
177 EXPECT_TRUE(m.Matches(quiet_nan));
178 EXPECT_TRUE(m.Matches(other_nan));
179 EXPECT_FALSE(m.Matches(real_value));
181 Matcher<double&> m_ref = IsNan();
182 EXPECT_TRUE(m_ref.Matches(quiet_nan));
183 EXPECT_TRUE(m_ref.Matches(other_nan));
184 EXPECT_FALSE(m_ref.Matches(real_value));
186 Matcher<const double&> m_cref = IsNan();
187 EXPECT_TRUE(m_cref.Matches(quiet_nan));
188 EXPECT_TRUE(m_cref.Matches(other_nan));
189 EXPECT_FALSE(m_cref.Matches(real_value));
192 // Tests that IsNan() matches a NaN, with long double.
193 TEST(IsNan, LongDoubleMatchesNan) {
194 long double quiet_nan = std::numeric_limits<long double>::quiet_NaN();
195 long double other_nan = std::nan("1");
196 long double real_value = 1.0;
198 Matcher<long double> m = IsNan();
199 EXPECT_TRUE(m.Matches(quiet_nan));
200 EXPECT_TRUE(m.Matches(other_nan));
201 EXPECT_FALSE(m.Matches(real_value));
203 Matcher<long double&> m_ref = IsNan();
204 EXPECT_TRUE(m_ref.Matches(quiet_nan));
205 EXPECT_TRUE(m_ref.Matches(other_nan));
206 EXPECT_FALSE(m_ref.Matches(real_value));
208 Matcher<const long double&> m_cref = IsNan();
209 EXPECT_TRUE(m_cref.Matches(quiet_nan));
210 EXPECT_TRUE(m_cref.Matches(other_nan));
211 EXPECT_FALSE(m_cref.Matches(real_value));
214 // Tests that IsNan() works with Not.
215 TEST(IsNan, NotMatchesNan) {
216 Matcher<float> mf = Not(IsNan());
217 EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN()));
218 EXPECT_FALSE(mf.Matches(std::nanf("1")));
219 EXPECT_TRUE(mf.Matches(1.0));
221 Matcher<double> md = Not(IsNan());
222 EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN()));
223 EXPECT_FALSE(md.Matches(std::nan("1")));
224 EXPECT_TRUE(md.Matches(1.0));
226 Matcher<long double> mld = Not(IsNan());
227 EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN()));
228 EXPECT_FALSE(mld.Matches(std::nanl("1")));
229 EXPECT_TRUE(mld.Matches(1.0));
232 // Tests that IsNan() can describe itself.
233 TEST(IsNan, CanDescribeSelf) {
234 Matcher<float> mf = IsNan();
235 EXPECT_EQ("is NaN", Describe(mf));
237 Matcher<double> md = IsNan();
238 EXPECT_EQ("is NaN", Describe(md));
240 Matcher<long double> mld = IsNan();
241 EXPECT_EQ("is NaN", Describe(mld));
244 // Tests that IsNan() can describe itself with Not.
245 TEST(IsNan, CanDescribeSelfWithNot) {
246 Matcher<float> mf = Not(IsNan());
247 EXPECT_EQ("isn't NaN", Describe(mf));
249 Matcher<double> md = Not(IsNan());
250 EXPECT_EQ("isn't NaN", Describe(md));
252 Matcher<long double> mld = Not(IsNan());
253 EXPECT_EQ("isn't NaN", Describe(mld));
256 // Tests that FloatEq() matches a 2-tuple where
257 // FloatEq(first field) matches the second field.
258 TEST(FloatEq2Test, MatchesEqualArguments) {
259 typedef ::std::tuple<float, float> Tpl;
260 Matcher<const Tpl&> m = FloatEq();
261 EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
262 EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f)));
263 EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
266 // Tests that FloatEq() describes itself properly.
267 TEST(FloatEq2Test, CanDescribeSelf) {
268 Matcher<const ::std::tuple<float, float>&> m = FloatEq();
269 EXPECT_EQ("are an almost-equal pair", Describe(m));
272 // Tests that NanSensitiveFloatEq() matches a 2-tuple where
273 // NanSensitiveFloatEq(first field) matches the second field.
274 TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {
275 typedef ::std::tuple<float, float> Tpl;
276 Matcher<const Tpl&> m = NanSensitiveFloatEq();
277 EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
278 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
279 std::numeric_limits<float>::quiet_NaN())));
280 EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
281 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
282 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
285 // Tests that NanSensitiveFloatEq() describes itself properly.
286 TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
287 Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq();
288 EXPECT_EQ("are an almost-equal pair", Describe(m));
291 // Tests that DoubleEq() matches a 2-tuple where
292 // DoubleEq(first field) matches the second field.
293 TEST(DoubleEq2Test, MatchesEqualArguments) {
294 typedef ::std::tuple<double, double> Tpl;
295 Matcher<const Tpl&> m = DoubleEq();
296 EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));
297 EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1)));
298 EXPECT_FALSE(m.Matches(Tpl(1.1, 1.0)));
301 // Tests that DoubleEq() describes itself properly.
302 TEST(DoubleEq2Test, CanDescribeSelf) {
303 Matcher<const ::std::tuple<double, double>&> m = DoubleEq();
304 EXPECT_EQ("are an almost-equal pair", Describe(m));
307 // Tests that NanSensitiveDoubleEq() matches a 2-tuple where
308 // NanSensitiveDoubleEq(first field) matches the second field.
309 TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {
310 typedef ::std::tuple<double, double> Tpl;
311 Matcher<const Tpl&> m = NanSensitiveDoubleEq();
312 EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
313 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
314 std::numeric_limits<double>::quiet_NaN())));
315 EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
316 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
317 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
320 // Tests that DoubleEq() describes itself properly.
321 TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
322 Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq();
323 EXPECT_EQ("are an almost-equal pair", Describe(m));
326 // Tests that FloatEq() matches a 2-tuple where
327 // FloatNear(first field, max_abs_error) matches the second field.
328 TEST(FloatNear2Test, MatchesEqualArguments) {
329 typedef ::std::tuple<float, float> Tpl;
330 Matcher<const Tpl&> m = FloatNear(0.5f);
331 EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
332 EXPECT_TRUE(m.Matches(Tpl(1.3f, 1.0f)));
333 EXPECT_FALSE(m.Matches(Tpl(1.8f, 1.0f)));
336 // Tests that FloatNear() describes itself properly.
337 TEST(FloatNear2Test, CanDescribeSelf) {
338 Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f);
339 EXPECT_EQ("are an almost-equal pair", Describe(m));
342 // Tests that NanSensitiveFloatNear() matches a 2-tuple where
343 // NanSensitiveFloatNear(first field) matches the second field.
344 TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {
345 typedef ::std::tuple<float, float> Tpl;
346 Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f);
347 EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
348 EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));
349 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
350 std::numeric_limits<float>::quiet_NaN())));
351 EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));
352 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
353 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
356 // Tests that NanSensitiveFloatNear() describes itself properly.
357 TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {
358 Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f);
359 EXPECT_EQ("are an almost-equal pair", Describe(m));
362 // Tests that FloatEq() matches a 2-tuple where
363 // DoubleNear(first field, max_abs_error) matches the second field.
364 TEST(DoubleNear2Test, MatchesEqualArguments) {
365 typedef ::std::tuple<double, double> Tpl;
366 Matcher<const Tpl&> m = DoubleNear(0.5);
367 EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));
368 EXPECT_TRUE(m.Matches(Tpl(1.3, 1.0)));
369 EXPECT_FALSE(m.Matches(Tpl(1.8, 1.0)));
372 // Tests that DoubleNear() describes itself properly.
373 TEST(DoubleNear2Test, CanDescribeSelf) {
374 Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5);
375 EXPECT_EQ("are an almost-equal pair", Describe(m));
378 // Tests that NanSensitiveDoubleNear() matches a 2-tuple where
379 // NanSensitiveDoubleNear(first field) matches the second field.
380 TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {
381 typedef ::std::tuple<double, double> Tpl;
382 Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f);
383 EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
384 EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));
385 EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
386 std::numeric_limits<double>::quiet_NaN())));
387 EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));
388 EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
389 EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
392 // Tests that NanSensitiveDoubleNear() describes itself properly.
393 TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
394 Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f);
395 EXPECT_EQ("are an almost-equal pair", Describe(m));
398 // Tests that Not(m) matches any value that doesn't match m.
399 TEST(NotTest, NegatesMatcher) {
402 EXPECT_TRUE(m.Matches(3));
403 EXPECT_FALSE(m.Matches(2));
406 // Tests that Not(m) describes itself properly.
407 TEST(NotTest, CanDescribeSelf) {
408 Matcher<int> m = Not(Eq(5));
409 EXPECT_EQ("isn't equal to 5", Describe(m));
412 // Tests that monomorphic matchers are safely cast by the Not matcher.
413 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
414 // greater_than_5 is a monomorphic matcher.
415 Matcher<int> greater_than_5 = Gt(5);
417 Matcher<const int&> m = Not(greater_than_5);
418 Matcher<int&> m2 = Not(greater_than_5);
419 Matcher<int&> m3 = Not(m);
422 // Helper to allow easy testing of AllOf matchers with num parameters.
423 void AllOfMatches(int num, const Matcher<int>& m) {
424 SCOPED_TRACE(Describe(m));
425 EXPECT_TRUE(m.Matches(0));
426 for (int i = 1; i <= num; ++i) {
427 EXPECT_FALSE(m.Matches(i));
429 EXPECT_TRUE(m.Matches(num + 1));
432 INSTANTIATE_GTEST_MATCHER_TEST_P(AllOfTest);
434 // Tests that AllOf(m1, ..., mn) matches any value that matches all of
435 // the given matchers.
436 TEST(AllOfTest, MatchesWhenAllMatch) {
438 m = AllOf(Le(2), Ge(1));
439 EXPECT_TRUE(m.Matches(1));
440 EXPECT_TRUE(m.Matches(2));
441 EXPECT_FALSE(m.Matches(0));
442 EXPECT_FALSE(m.Matches(3));
444 m = AllOf(Gt(0), Ne(1), Ne(2));
445 EXPECT_TRUE(m.Matches(3));
446 EXPECT_FALSE(m.Matches(2));
447 EXPECT_FALSE(m.Matches(1));
448 EXPECT_FALSE(m.Matches(0));
450 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
451 EXPECT_TRUE(m.Matches(4));
452 EXPECT_FALSE(m.Matches(3));
453 EXPECT_FALSE(m.Matches(2));
454 EXPECT_FALSE(m.Matches(1));
455 EXPECT_FALSE(m.Matches(0));
457 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
458 EXPECT_TRUE(m.Matches(0));
459 EXPECT_TRUE(m.Matches(1));
460 EXPECT_FALSE(m.Matches(3));
462 // The following tests for varying number of sub-matchers. Due to the way
463 // the sub-matchers are handled it is enough to test every sub-matcher once
464 // with sub-matchers using the same matcher type. Varying matcher types are
465 // checked for above.
466 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
467 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
468 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
469 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
470 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
471 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
473 AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8)));
475 9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9)));
476 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
479 50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
480 Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17),
481 Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25),
482 Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33),
483 Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41),
484 Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),
488 // Tests that AllOf(m1, ..., mn) describes itself properly.
489 TEST(AllOfTest, CanDescribeSelf) {
491 m = AllOf(Le(2), Ge(1));
492 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
494 m = AllOf(Gt(0), Ne(1), Ne(2));
495 std::string expected_descr1 =
496 "(is > 0) and (isn't equal to 1) and (isn't equal to 2)";
497 EXPECT_EQ(expected_descr1, Describe(m));
499 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
500 std::string expected_descr2 =
501 "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "
503 EXPECT_EQ(expected_descr2, Describe(m));
505 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
506 std::string expected_descr3 =
507 "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "
508 "and (isn't equal to 7)";
509 EXPECT_EQ(expected_descr3, Describe(m));
512 // Tests that AllOf(m1, ..., mn) describes its negation properly.
513 TEST(AllOfTest, CanDescribeNegation) {
515 m = AllOf(Le(2), Ge(1));
516 std::string expected_descr4 = "(isn't <= 2) or (isn't >= 1)";
517 EXPECT_EQ(expected_descr4, DescribeNegation(m));
519 m = AllOf(Gt(0), Ne(1), Ne(2));
520 std::string expected_descr5 =
521 "(isn't > 0) or (is equal to 1) or (is equal to 2)";
522 EXPECT_EQ(expected_descr5, DescribeNegation(m));
524 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
525 std::string expected_descr6 =
526 "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";
527 EXPECT_EQ(expected_descr6, DescribeNegation(m));
529 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
530 std::string expected_desr7 =
531 "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "
533 EXPECT_EQ(expected_desr7, DescribeNegation(m));
535 m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
537 AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
538 EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11)"));
542 // Tests that monomorphic matchers are safely cast by the AllOf matcher.
543 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
544 // greater_than_5 and less_than_10 are monomorphic matchers.
545 Matcher<int> greater_than_5 = Gt(5);
546 Matcher<int> less_than_10 = Lt(10);
548 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
549 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
550 Matcher<int&> m3 = AllOf(greater_than_5, m2);
552 // Tests that BothOf works when composing itself.
553 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
554 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
557 TEST_P(AllOfTestP, ExplainsResult) {
560 // Successful match. Both matchers need to explain. The second
561 // matcher doesn't give an explanation, so only the first matcher's
562 // explanation is printed.
563 m = AllOf(GreaterThan(10), Lt(30));
564 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
566 // Successful match. Both matchers need to explain.
567 m = AllOf(GreaterThan(10), GreaterThan(20));
568 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
571 // Successful match. All matchers need to explain. The second
572 // matcher doesn't given an explanation.
573 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
574 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
577 // Successful match. All matchers need to explain.
578 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
580 "which is 30 more than 10, and which is 20 more than 20, "
581 "and which is 10 more than 30",
584 // Failed match. The first matcher, which failed, needs to
586 m = AllOf(GreaterThan(10), GreaterThan(20));
587 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
589 // Failed match. The second matcher, which failed, needs to
590 // explain. Since it doesn't given an explanation, nothing is
592 m = AllOf(GreaterThan(10), Lt(30));
593 EXPECT_EQ("", Explain(m, 40));
595 // Failed match. The second matcher, which failed, needs to
597 m = AllOf(GreaterThan(10), GreaterThan(20));
598 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
601 // Helper to allow easy testing of AnyOf matchers with num parameters.
602 static void AnyOfMatches(int num, const Matcher<int>& m) {
603 SCOPED_TRACE(Describe(m));
604 EXPECT_FALSE(m.Matches(0));
605 for (int i = 1; i <= num; ++i) {
606 EXPECT_TRUE(m.Matches(i));
608 EXPECT_FALSE(m.Matches(num + 1));
611 static void AnyOfStringMatches(int num, const Matcher<std::string>& m) {
612 SCOPED_TRACE(Describe(m));
613 EXPECT_FALSE(m.Matches(std::to_string(0)));
615 for (int i = 1; i <= num; ++i) {
616 EXPECT_TRUE(m.Matches(std::to_string(i)));
618 EXPECT_FALSE(m.Matches(std::to_string(num + 1)));
621 INSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfTest);
623 // Tests that AnyOf(m1, ..., mn) matches any value that matches at
624 // least one of the given matchers.
625 TEST(AnyOfTest, MatchesWhenAnyMatches) {
627 m = AnyOf(Le(1), Ge(3));
628 EXPECT_TRUE(m.Matches(1));
629 EXPECT_TRUE(m.Matches(4));
630 EXPECT_FALSE(m.Matches(2));
632 m = AnyOf(Lt(0), Eq(1), Eq(2));
633 EXPECT_TRUE(m.Matches(-1));
634 EXPECT_TRUE(m.Matches(1));
635 EXPECT_TRUE(m.Matches(2));
636 EXPECT_FALSE(m.Matches(0));
638 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
639 EXPECT_TRUE(m.Matches(-1));
640 EXPECT_TRUE(m.Matches(1));
641 EXPECT_TRUE(m.Matches(2));
642 EXPECT_TRUE(m.Matches(3));
643 EXPECT_FALSE(m.Matches(0));
645 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
646 EXPECT_TRUE(m.Matches(0));
647 EXPECT_TRUE(m.Matches(11));
648 EXPECT_TRUE(m.Matches(3));
649 EXPECT_FALSE(m.Matches(2));
651 // The following tests for varying number of sub-matchers. Due to the way
652 // the sub-matchers are handled it is enough to test every sub-matcher once
653 // with sub-matchers using the same matcher type. Varying matcher types are
654 // checked for above.
655 AnyOfMatches(2, AnyOf(1, 2));
656 AnyOfMatches(3, AnyOf(1, 2, 3));
657 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
658 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
659 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
660 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
661 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
662 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
663 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
666 // Tests the variadic version of the AnyOfMatcher.
667 TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
668 // Also make sure AnyOf is defined in the right namespace and does not depend
670 Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
672 EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)"));
674 AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
675 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
676 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
677 45, 46, 47, 48, 49, 50));
679 50, AnyOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
680 "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
681 "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
682 "33", "34", "35", "36", "37", "38", "39", "40", "41", "42",
683 "43", "44", "45", "46", "47", "48", "49", "50"));
686 TEST(ConditionalTest, MatchesFirstIfCondition) {
687 Matcher<std::string> eq_red = Eq("red");
688 Matcher<std::string> ne_red = Ne("red");
689 Matcher<std::string> m = Conditional(true, eq_red, ne_red);
690 EXPECT_TRUE(m.Matches("red"));
691 EXPECT_FALSE(m.Matches("green"));
693 StringMatchResultListener listener;
694 StringMatchResultListener expected;
695 EXPECT_FALSE(m.MatchAndExplain("green", &listener));
696 EXPECT_FALSE(eq_red.MatchAndExplain("green", &expected));
697 EXPECT_THAT(listener.str(), Eq(expected.str()));
700 TEST(ConditionalTest, MatchesSecondIfCondition) {
701 Matcher<std::string> eq_red = Eq("red");
702 Matcher<std::string> ne_red = Ne("red");
703 Matcher<std::string> m = Conditional(false, eq_red, ne_red);
704 EXPECT_FALSE(m.Matches("red"));
705 EXPECT_TRUE(m.Matches("green"));
707 StringMatchResultListener listener;
708 StringMatchResultListener expected;
709 EXPECT_FALSE(m.MatchAndExplain("red", &listener));
710 EXPECT_FALSE(ne_red.MatchAndExplain("red", &expected));
711 EXPECT_THAT(listener.str(), Eq(expected.str()));
714 // Tests that AnyOf(m1, ..., mn) describes itself properly.
715 TEST(AnyOfTest, CanDescribeSelf) {
717 m = AnyOf(Le(1), Ge(3));
719 EXPECT_EQ("(is <= 1) or (is >= 3)", Describe(m));
721 m = AnyOf(Lt(0), Eq(1), Eq(2));
722 EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m));
724 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
725 EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",
728 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
730 "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
735 // Tests that AnyOf(m1, ..., mn) describes its negation properly.
736 TEST(AnyOfTest, CanDescribeNegation) {
738 m = AnyOf(Le(1), Ge(3));
739 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", DescribeNegation(m));
741 m = AnyOf(Lt(0), Eq(1), Eq(2));
742 EXPECT_EQ("(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",
743 DescribeNegation(m));
745 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
747 "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
749 DescribeNegation(m));
751 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
753 "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "
754 "to 5) and (isn't equal to 7)",
755 DescribeNegation(m));
758 // Tests that monomorphic matchers are safely cast by the AnyOf matcher.
759 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
760 // greater_than_5 and less_than_10 are monomorphic matchers.
761 Matcher<int> greater_than_5 = Gt(5);
762 Matcher<int> less_than_10 = Lt(10);
764 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
765 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
766 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
768 // Tests that EitherOf works when composing itself.
769 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
770 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
773 TEST_P(AnyOfTestP, ExplainsResult) {
776 // Failed match. Both matchers need to explain. The second
777 // matcher doesn't give an explanation, so only the first matcher's
778 // explanation is printed.
779 m = AnyOf(GreaterThan(10), Lt(0));
780 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
782 // Failed match. Both matchers need to explain.
783 m = AnyOf(GreaterThan(10), GreaterThan(20));
784 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
787 // Failed match. All matchers need to explain. The second
788 // matcher doesn't given an explanation.
789 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
790 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
793 // Failed match. All matchers need to explain.
794 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
796 "which is 5 less than 10, and which is 15 less than 20, "
797 "and which is 25 less than 30",
800 // Successful match. The first matcher, which succeeded, needs to
802 m = AnyOf(GreaterThan(10), GreaterThan(20));
803 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
805 // Successful match. The second matcher, which succeeded, needs to
806 // explain. Since it doesn't given an explanation, nothing is
808 m = AnyOf(GreaterThan(10), Lt(30));
809 EXPECT_EQ("", Explain(m, 0));
811 // Successful match. The second matcher, which succeeded, needs to
813 m = AnyOf(GreaterThan(30), GreaterThan(20));
814 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
817 // The following predicate function and predicate functor are for
818 // testing the Truly(predicate) matcher.
820 // Returns non-zero if the input is positive. Note that the return
821 // type of this function is not bool. It's OK as Truly() accepts any
822 // unary function or functor whose return type can be implicitly
823 // converted to bool.
824 int IsPositive(double x) { return x > 0 ? 1 : 0; }
826 // This functor returns true if the input is greater than the given
828 class IsGreaterThan {
830 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
832 bool operator()(int n) const { return n > threshold_; }
838 // For testing Truly().
841 // This predicate returns true if and only if the argument references foo and
843 bool ReferencesFooAndIsZero(const int& n) { return (&n == &foo) && (n == 0); }
845 // Tests that Truly(predicate) matches what satisfies the given
847 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
848 Matcher<double> m = Truly(IsPositive);
849 EXPECT_TRUE(m.Matches(2.0));
850 EXPECT_FALSE(m.Matches(-1.5));
853 // Tests that Truly(predicate_functor) works too.
854 TEST(TrulyTest, CanBeUsedWithFunctor) {
855 Matcher<int> m = Truly(IsGreaterThan(5));
856 EXPECT_TRUE(m.Matches(6));
857 EXPECT_FALSE(m.Matches(4));
860 // A class that can be implicitly converted to bool.
861 class ConvertibleToBool {
863 explicit ConvertibleToBool(int number) : number_(number) {}
864 operator bool() const { return number_ != 0; }
870 ConvertibleToBool IsNotZero(int number) { return ConvertibleToBool(number); }
872 // Tests that the predicate used in Truly() may return a class that's
873 // implicitly convertible to bool, even when the class has no
875 TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
876 Matcher<int> m = Truly(IsNotZero);
877 EXPECT_TRUE(m.Matches(1));
878 EXPECT_FALSE(m.Matches(0));
881 // Tests that Truly(predicate) can describe itself properly.
882 TEST(TrulyTest, CanDescribeSelf) {
883 Matcher<double> m = Truly(IsPositive);
884 EXPECT_EQ("satisfies the given predicate", Describe(m));
887 // Tests that Truly(predicate) works when the matcher takes its
888 // argument by reference.
889 TEST(TrulyTest, WorksForByRefArguments) {
890 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
891 EXPECT_TRUE(m.Matches(foo));
893 EXPECT_FALSE(m.Matches(n));
896 // Tests that Truly(predicate) provides a helpful reason when it fails.
897 TEST(TrulyTest, ExplainsFailures) {
898 StringMatchResultListener listener;
899 EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener));
900 EXPECT_EQ(listener.str(), "didn't satisfy the given predicate");
903 // Tests that Matches(m) is a predicate satisfied by whatever that
904 // matches matcher m.
905 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
906 EXPECT_TRUE(Matches(Ge(0))(1));
907 EXPECT_FALSE(Matches(Eq('a'))('b'));
910 // Tests that Matches(m) works when the matcher takes its argument by
912 TEST(MatchesTest, WorksOnByRefArguments) {
914 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
915 EXPECT_FALSE(Matches(Ref(m))(n));
918 // Tests that a Matcher on non-reference type can be used in
920 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
921 Matcher<int> eq5 = Eq(5);
922 EXPECT_TRUE(Matches(eq5)(5));
923 EXPECT_FALSE(Matches(eq5)(2));
926 // Tests Value(value, matcher). Since Value() is a simple wrapper for
927 // Matches(), which has been tested already, we don't spend a lot of
928 // effort on testing Value().
929 TEST(ValueTest, WorksWithPolymorphicMatcher) {
930 EXPECT_TRUE(Value("hi", StartsWith("h")));
931 EXPECT_FALSE(Value(5, Gt(10)));
934 TEST(ValueTest, WorksWithMonomorphicMatcher) {
935 const Matcher<int> is_zero = Eq(0);
936 EXPECT_TRUE(Value(0, is_zero));
937 EXPECT_FALSE(Value('a', is_zero));
940 const Matcher<const int&> ref_n = Ref(n);
941 EXPECT_TRUE(Value(n, ref_n));
942 EXPECT_FALSE(Value(1, ref_n));
945 TEST(AllArgsTest, WorksForTuple) {
946 EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt()));
947 EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt())));
950 TEST(AllArgsTest, WorksForNonTuple) {
951 EXPECT_THAT(42, AllArgs(Gt(0)));
952 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
955 class AllArgsHelper {
959 MOCK_METHOD2(Helper, int(char x, int y));
962 AllArgsHelper(const AllArgsHelper&) = delete;
963 AllArgsHelper& operator=(const AllArgsHelper&) = delete;
966 TEST(AllArgsTest, WorksInWithClause) {
967 AllArgsHelper helper;
968 ON_CALL(helper, Helper(_, _)).With(AllArgs(Lt())).WillByDefault(Return(1));
969 EXPECT_CALL(helper, Helper(_, _));
970 EXPECT_CALL(helper, Helper(_, _)).With(AllArgs(Gt())).WillOnce(Return(2));
972 EXPECT_EQ(1, helper.Helper('\1', 2));
973 EXPECT_EQ(2, helper.Helper('a', 1));
976 class OptionalMatchersHelper {
978 OptionalMatchersHelper() {}
980 MOCK_METHOD0(NoArgs, int());
982 MOCK_METHOD1(OneArg, int(int y));
984 MOCK_METHOD2(TwoArgs, int(char x, int y));
986 MOCK_METHOD1(Overloaded, int(char x));
987 MOCK_METHOD2(Overloaded, int(char x, int y));
990 OptionalMatchersHelper(const OptionalMatchersHelper&) = delete;
991 OptionalMatchersHelper& operator=(const OptionalMatchersHelper&) = delete;
994 TEST(AllArgsTest, WorksWithoutMatchers) {
995 OptionalMatchersHelper helper;
997 ON_CALL(helper, NoArgs).WillByDefault(Return(10));
998 ON_CALL(helper, OneArg).WillByDefault(Return(20));
999 ON_CALL(helper, TwoArgs).WillByDefault(Return(30));
1001 EXPECT_EQ(10, helper.NoArgs());
1002 EXPECT_EQ(20, helper.OneArg(1));
1003 EXPECT_EQ(30, helper.TwoArgs('\1', 2));
1005 EXPECT_CALL(helper, NoArgs).Times(1);
1006 EXPECT_CALL(helper, OneArg).WillOnce(Return(100));
1007 EXPECT_CALL(helper, OneArg(17)).WillOnce(Return(200));
1008 EXPECT_CALL(helper, TwoArgs).Times(0);
1010 EXPECT_EQ(10, helper.NoArgs());
1011 EXPECT_EQ(100, helper.OneArg(1));
1012 EXPECT_EQ(200, helper.OneArg(17));
1015 // Tests floating-point matchers.
1016 template <typename RawType>
1017 class FloatingPointTest : public testing::Test {
1019 typedef testing::internal::FloatingPoint<RawType> Floating;
1020 typedef typename Floating::Bits Bits;
1023 : max_ulps_(Floating::kMaxUlps),
1024 zero_bits_(Floating(0).bits()),
1025 one_bits_(Floating(1).bits()),
1026 infinity_bits_(Floating(Floating::Infinity()).bits()),
1027 close_to_positive_zero_(
1028 Floating::ReinterpretBits(zero_bits_ + max_ulps_ / 2)),
1029 close_to_negative_zero_(
1030 -Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_ / 2)),
1031 further_from_negative_zero_(-Floating::ReinterpretBits(
1032 zero_bits_ + max_ulps_ + 1 - max_ulps_ / 2)),
1033 close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)),
1034 further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)),
1035 infinity_(Floating::Infinity()),
1037 Floating::ReinterpretBits(infinity_bits_ - max_ulps_)),
1038 further_from_infinity_(
1039 Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)),
1040 max_(Floating::Max()),
1041 nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
1042 nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {}
1044 void TestSize() { EXPECT_EQ(sizeof(RawType), sizeof(Bits)); }
1046 // A battery of tests for FloatingEqMatcher::Matches.
1047 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
1049 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
1050 Matcher<RawType> m1 = matcher_maker(0.0);
1051 EXPECT_TRUE(m1.Matches(-0.0));
1052 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
1053 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
1054 EXPECT_FALSE(m1.Matches(1.0));
1056 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
1057 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
1059 Matcher<RawType> m3 = matcher_maker(1.0);
1060 EXPECT_TRUE(m3.Matches(close_to_one_));
1061 EXPECT_FALSE(m3.Matches(further_from_one_));
1063 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
1064 EXPECT_FALSE(m3.Matches(0.0));
1066 Matcher<RawType> m4 = matcher_maker(-infinity_);
1067 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
1069 Matcher<RawType> m5 = matcher_maker(infinity_);
1070 EXPECT_TRUE(m5.Matches(close_to_infinity_));
1072 // This is interesting as the representations of infinity_ and nan1_
1073 // are only 1 DLP apart.
1074 EXPECT_FALSE(m5.Matches(nan1_));
1076 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
1078 Matcher<const RawType&> m6 = matcher_maker(0.0);
1079 EXPECT_TRUE(m6.Matches(-0.0));
1080 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
1081 EXPECT_FALSE(m6.Matches(1.0));
1083 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
1085 Matcher<RawType&> m7 = matcher_maker(0.0);
1087 EXPECT_TRUE(m7.Matches(x));
1089 EXPECT_FALSE(m7.Matches(x));
1092 // Pre-calculated numbers to be used by the tests.
1094 const Bits max_ulps_;
1096 const Bits zero_bits_; // The bits that represent 0.0.
1097 const Bits one_bits_; // The bits that represent 1.0.
1098 const Bits infinity_bits_; // The bits that represent +infinity.
1100 // Some numbers close to 0.0.
1101 const RawType close_to_positive_zero_;
1102 const RawType close_to_negative_zero_;
1103 const RawType further_from_negative_zero_;
1105 // Some numbers close to 1.0.
1106 const RawType close_to_one_;
1107 const RawType further_from_one_;
1109 // Some numbers close to +infinity.
1110 const RawType infinity_;
1111 const RawType close_to_infinity_;
1112 const RawType further_from_infinity_;
1114 // Maximum representable value that's not infinity.
1118 const RawType nan1_;
1119 const RawType nan2_;
1122 // Tests floating-point matchers with fixed epsilons.
1123 template <typename RawType>
1124 class FloatingPointNearTest : public FloatingPointTest<RawType> {
1126 typedef FloatingPointTest<RawType> ParentType;
1128 // A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon.
1129 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
1130 void TestNearMatches(testing::internal::FloatingEqMatcher<RawType> (
1131 *matcher_maker)(RawType, RawType)) {
1132 Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
1133 EXPECT_TRUE(m1.Matches(0.0));
1134 EXPECT_TRUE(m1.Matches(-0.0));
1135 EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_));
1136 EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_));
1137 EXPECT_FALSE(m1.Matches(1.0));
1139 Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
1140 EXPECT_TRUE(m2.Matches(0.0));
1141 EXPECT_TRUE(m2.Matches(-0.0));
1142 EXPECT_TRUE(m2.Matches(1.0));
1143 EXPECT_TRUE(m2.Matches(-1.0));
1144 EXPECT_FALSE(m2.Matches(ParentType::close_to_one_));
1145 EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_));
1147 // Check that inf matches inf, regardless of the of the specified max
1149 Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0);
1150 EXPECT_TRUE(m3.Matches(ParentType::infinity_));
1151 EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_));
1152 EXPECT_FALSE(m3.Matches(-ParentType::infinity_));
1154 Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0);
1155 EXPECT_TRUE(m4.Matches(-ParentType::infinity_));
1156 EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_));
1157 EXPECT_FALSE(m4.Matches(ParentType::infinity_));
1159 // Test various overflow scenarios.
1160 Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_);
1161 EXPECT_TRUE(m5.Matches(ParentType::max_));
1162 EXPECT_FALSE(m5.Matches(-ParentType::max_));
1164 Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_);
1165 EXPECT_FALSE(m6.Matches(ParentType::max_));
1166 EXPECT_TRUE(m6.Matches(-ParentType::max_));
1168 Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0);
1169 EXPECT_TRUE(m7.Matches(ParentType::max_));
1170 EXPECT_FALSE(m7.Matches(-ParentType::max_));
1172 Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0);
1173 EXPECT_FALSE(m8.Matches(ParentType::max_));
1174 EXPECT_TRUE(m8.Matches(-ParentType::max_));
1176 // The difference between max() and -max() normally overflows to infinity,
1177 // but it should still match if the max_abs_error is also infinity.
1178 Matcher<RawType> m9 =
1179 matcher_maker(ParentType::max_, ParentType::infinity_);
1180 EXPECT_TRUE(m8.Matches(-ParentType::max_));
1182 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
1184 Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
1185 EXPECT_TRUE(m10.Matches(-0.0));
1186 EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_));
1187 EXPECT_FALSE(m10.Matches(ParentType::close_to_one_));
1189 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
1191 Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
1193 EXPECT_TRUE(m11.Matches(x));
1195 EXPECT_TRUE(m11.Matches(x));
1197 EXPECT_TRUE(m11.Matches(x));
1199 EXPECT_FALSE(m11.Matches(x));
1201 EXPECT_FALSE(m11.Matches(x));
1205 // Instantiate FloatingPointTest for testing floats.
1206 typedef FloatingPointTest<float> FloatTest;
1208 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { TestMatches(&FloatEq); }
1210 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
1211 TestMatches(&NanSensitiveFloatEq);
1214 TEST_F(FloatTest, FloatEqCannotMatchNaN) {
1215 // FloatEq never matches NaN.
1216 Matcher<float> m = FloatEq(nan1_);
1217 EXPECT_FALSE(m.Matches(nan1_));
1218 EXPECT_FALSE(m.Matches(nan2_));
1219 EXPECT_FALSE(m.Matches(1.0));
1222 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
1223 // NanSensitiveFloatEq will match NaN.
1224 Matcher<float> m = NanSensitiveFloatEq(nan1_);
1225 EXPECT_TRUE(m.Matches(nan1_));
1226 EXPECT_TRUE(m.Matches(nan2_));
1227 EXPECT_FALSE(m.Matches(1.0));
1230 TEST_F(FloatTest, FloatEqCanDescribeSelf) {
1231 Matcher<float> m1 = FloatEq(2.0f);
1232 EXPECT_EQ("is approximately 2", Describe(m1));
1233 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
1235 Matcher<float> m2 = FloatEq(0.5f);
1236 EXPECT_EQ("is approximately 0.5", Describe(m2));
1237 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
1239 Matcher<float> m3 = FloatEq(nan1_);
1240 EXPECT_EQ("never matches", Describe(m3));
1241 EXPECT_EQ("is anything", DescribeNegation(m3));
1244 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
1245 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
1246 EXPECT_EQ("is approximately 2", Describe(m1));
1247 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
1249 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
1250 EXPECT_EQ("is approximately 0.5", Describe(m2));
1251 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
1253 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
1254 EXPECT_EQ("is NaN", Describe(m3));
1255 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
1258 // Instantiate FloatingPointTest for testing floats with a user-specified
1259 // max absolute error.
1260 typedef FloatingPointNearTest<float> FloatNearTest;
1262 TEST_F(FloatNearTest, FloatNearMatches) { TestNearMatches(&FloatNear); }
1264 TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
1265 TestNearMatches(&NanSensitiveFloatNear);
1268 TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
1269 Matcher<float> m1 = FloatNear(2.0f, 0.5f);
1270 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
1271 EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
1272 DescribeNegation(m1));
1274 Matcher<float> m2 = FloatNear(0.5f, 0.5f);
1275 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
1276 EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
1277 DescribeNegation(m2));
1279 Matcher<float> m3 = FloatNear(nan1_, 0.0);
1280 EXPECT_EQ("never matches", Describe(m3));
1281 EXPECT_EQ("is anything", DescribeNegation(m3));
1284 TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
1285 Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f);
1286 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
1287 EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
1288 DescribeNegation(m1));
1290 Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f);
1291 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
1292 EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
1293 DescribeNegation(m2));
1295 Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f);
1296 EXPECT_EQ("is NaN", Describe(m3));
1297 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
1300 TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
1301 // FloatNear never matches NaN.
1302 Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f);
1303 EXPECT_FALSE(m.Matches(nan1_));
1304 EXPECT_FALSE(m.Matches(nan2_));
1305 EXPECT_FALSE(m.Matches(1.0));
1308 TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
1309 // NanSensitiveFloatNear will match NaN.
1310 Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f);
1311 EXPECT_TRUE(m.Matches(nan1_));
1312 EXPECT_TRUE(m.Matches(nan2_));
1313 EXPECT_FALSE(m.Matches(1.0));
1316 // Instantiate FloatingPointTest for testing doubles.
1317 typedef FloatingPointTest<double> DoubleTest;
1319 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
1320 TestMatches(&DoubleEq);
1323 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
1324 TestMatches(&NanSensitiveDoubleEq);
1327 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
1328 // DoubleEq never matches NaN.
1329 Matcher<double> m = DoubleEq(nan1_);
1330 EXPECT_FALSE(m.Matches(nan1_));
1331 EXPECT_FALSE(m.Matches(nan2_));
1332 EXPECT_FALSE(m.Matches(1.0));
1335 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
1336 // NanSensitiveDoubleEq will match NaN.
1337 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
1338 EXPECT_TRUE(m.Matches(nan1_));
1339 EXPECT_TRUE(m.Matches(nan2_));
1340 EXPECT_FALSE(m.Matches(1.0));
1343 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
1344 Matcher<double> m1 = DoubleEq(2.0);
1345 EXPECT_EQ("is approximately 2", Describe(m1));
1346 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
1348 Matcher<double> m2 = DoubleEq(0.5);
1349 EXPECT_EQ("is approximately 0.5", Describe(m2));
1350 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
1352 Matcher<double> m3 = DoubleEq(nan1_);
1353 EXPECT_EQ("never matches", Describe(m3));
1354 EXPECT_EQ("is anything", DescribeNegation(m3));
1357 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
1358 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
1359 EXPECT_EQ("is approximately 2", Describe(m1));
1360 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
1362 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
1363 EXPECT_EQ("is approximately 0.5", Describe(m2));
1364 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
1366 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
1367 EXPECT_EQ("is NaN", Describe(m3));
1368 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
1371 // Instantiate FloatingPointTest for testing floats with a user-specified
1372 // max absolute error.
1373 typedef FloatingPointNearTest<double> DoubleNearTest;
1375 TEST_F(DoubleNearTest, DoubleNearMatches) { TestNearMatches(&DoubleNear); }
1377 TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
1378 TestNearMatches(&NanSensitiveDoubleNear);
1381 TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
1382 Matcher<double> m1 = DoubleNear(2.0, 0.5);
1383 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
1384 EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
1385 DescribeNegation(m1));
1387 Matcher<double> m2 = DoubleNear(0.5, 0.5);
1388 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
1389 EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
1390 DescribeNegation(m2));
1392 Matcher<double> m3 = DoubleNear(nan1_, 0.0);
1393 EXPECT_EQ("never matches", Describe(m3));
1394 EXPECT_EQ("is anything", DescribeNegation(m3));
1397 TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {
1398 EXPECT_EQ("", Explain(DoubleNear(2.0, 0.1), 2.05));
1399 EXPECT_EQ("which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2));
1400 EXPECT_EQ("which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7));
1402 const std::string explanation =
1403 Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);
1404 // Different C++ implementations may print floating-point numbers
1405 // slightly differently.
1406 EXPECT_TRUE(explanation == "which is 1.2e-10 from 2.1" || // GCC
1407 explanation == "which is 1.2e-010 from 2.1") // MSVC
1408 << " where explanation is \"" << explanation << "\".";
1411 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
1412 Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);
1413 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
1414 EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
1415 DescribeNegation(m1));
1417 Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);
1418 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
1419 EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
1420 DescribeNegation(m2));
1422 Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1);
1423 EXPECT_EQ("is NaN", Describe(m3));
1424 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
1427 TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
1428 // DoubleNear never matches NaN.
1429 Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1);
1430 EXPECT_FALSE(m.Matches(nan1_));
1431 EXPECT_FALSE(m.Matches(nan2_));
1432 EXPECT_FALSE(m.Matches(1.0));
1435 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
1436 // NanSensitiveDoubleNear will match NaN.
1437 Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1);
1438 EXPECT_TRUE(m.Matches(nan1_));
1439 EXPECT_TRUE(m.Matches(nan2_));
1440 EXPECT_FALSE(m.Matches(1.0));
1443 TEST(NotTest, WorksOnMoveOnlyType) {
1444 std::unique_ptr<int> p(new int(3));
1445 EXPECT_THAT(p, Pointee(Eq(3)));
1446 EXPECT_THAT(p, Not(Pointee(Eq(2))));
1449 TEST(AllOfTest, HugeMatcher) {
1450 // Verify that using AllOf with many arguments doesn't cause
1451 // the compiler to exceed template instantiation depth limit.
1452 EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
1453 testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
1456 TEST(AnyOfTest, HugeMatcher) {
1457 // Verify that using AnyOf with many arguments doesn't cause
1458 // the compiler to exceed template instantiation depth limit.
1459 EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
1460 testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
1463 namespace adl_test {
1465 // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
1466 // don't issue unqualified recursive calls. If they do, the argument dependent
1467 // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
1468 // as a candidate and the compilation will break due to an ambiguous overload.
1470 // The matcher must be in the same namespace as AllOf/AnyOf to make argument
1471 // dependent lookup find those.
1477 template <typename T1, typename T2>
1478 bool AllOf(const T1& /*t1*/, const T2& /*t2*/) {
1482 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1484 testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1487 template <typename T1, typename T2>
1488 bool AnyOf(const T1&, const T2&) {
1492 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1494 testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1497 } // namespace adl_test
1499 TEST(AllOfTest, WorksOnMoveOnlyType) {
1500 std::unique_ptr<int> p(new int(3));
1501 EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
1502 EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
1505 TEST(AnyOfTest, WorksOnMoveOnlyType) {
1506 std::unique_ptr<int> p(new int(3));
1507 EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
1508 EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
1512 } // namespace gmock_matchers_test
1513 } // namespace testing
1516 #pragma warning(pop)