38#pragma warning(disable : 4244)
39#pragma warning(disable : 4100)
42#include "test/gmock-matchers_test.h"
45namespace gmock_matchers_test {
48typedef ::std::tuple<long, int> Tuple2;
52TEST(Eq2Test, MatchesEqualArguments) {
53 Matcher<const Tuple2&> m = Eq();
54 EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
55 EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
59TEST(Eq2Test, CanDescribeSelf) {
60 Matcher<const Tuple2&> m = Eq();
61 EXPECT_EQ(
"are an equal pair", Describe(m));
66TEST(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)));
74TEST(Ge2Test, CanDescribeSelf) {
75 Matcher<const Tuple2&> m = Ge();
76 EXPECT_EQ(
"are a pair where the first >= the second", Describe(m));
81TEST(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)));
89TEST(Gt2Test, CanDescribeSelf) {
90 Matcher<const Tuple2&> m = Gt();
91 EXPECT_EQ(
"are a pair where the first > the second", Describe(m));
96TEST(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)));
104TEST(Le2Test, CanDescribeSelf) {
105 Matcher<const Tuple2&> m = Le();
106 EXPECT_EQ(
"are a pair where the first <= the second", Describe(m));
111TEST(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)));
119TEST(Lt2Test, CanDescribeSelf) {
120 Matcher<const Tuple2&> m = Lt();
121 EXPECT_EQ(
"are a pair where the first < the second", Describe(m));
126TEST(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)));
134TEST(Ne2Test, CanDescribeSelf) {
135 Matcher<const Tuple2&> m = Ne();
136 EXPECT_EQ(
"are an unequal pair", Describe(m));
139TEST(PairMatchBaseTest, WorksWithMoveOnly) {
140 using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
141 Matcher<Pointers> matcher = Eq();
145 EXPECT_TRUE(matcher.Matches(pointers));
149TEST(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));
171TEST(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));
193TEST(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));
215TEST(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));
233TEST(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));
245TEST(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));
258TEST(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)));
267TEST(FloatEq2Test, CanDescribeSelf) {
268 Matcher<const ::std::tuple<float, float>&> m = FloatEq();
269 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
274TEST(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)));
286TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
287 Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq();
288 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
293TEST(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)));
302TEST(DoubleEq2Test, CanDescribeSelf) {
303 Matcher<const ::std::tuple<double, double>&> m = DoubleEq();
304 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
309TEST(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)));
321TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
322 Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq();
323 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
328TEST(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)));
337TEST(FloatNear2Test, CanDescribeSelf) {
338 Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f);
339 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
344TEST(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)));
357TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {
358 Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f);
359 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
364TEST(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)));
373TEST(DoubleNear2Test, CanDescribeSelf) {
374 Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5);
375 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
380TEST(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)));
393TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
394 Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f);
395 EXPECT_EQ(
"are an almost-equal pair", Describe(m));
399TEST(NotTest, NegatesMatcher) {
402 EXPECT_TRUE(m.Matches(3));
403 EXPECT_FALSE(m.Matches(2));
407TEST(NotTest, CanDescribeSelf) {
408 Matcher<int> m = Not(Eq(5));
409 EXPECT_EQ(
"isn't equal to 5", Describe(m));
413TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
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);
423void 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));
432INSTANTIATE_GTEST_MATCHER_TEST_P(AllOfTest);
436TEST(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));
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),
489TEST(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));
513TEST(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)"));
543TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
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);
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);
557TEST_P(AllOfTestP, ExplainsResult) {
563 m = AllOf(GreaterThan(10), Lt(30));
564 EXPECT_EQ(
"which is 15 more than 10", Explain(m, 25));
567 m = AllOf(GreaterThan(10), GreaterThan(20));
568 EXPECT_EQ(
"which is 20 more than 10, and which is 10 more than 20",
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",
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",
586 m = AllOf(GreaterThan(10), GreaterThan(20));
587 EXPECT_EQ(
"which is 5 less than 10", Explain(m, 5));
592 m = AllOf(GreaterThan(10), Lt(30));
593 EXPECT_EQ(
"", Explain(m, 40));
597 m = AllOf(GreaterThan(10), GreaterThan(20));
598 EXPECT_EQ(
"which is 5 less than 20", Explain(m, 15));
602static 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));
611static 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)));
621INSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfTest);
625TEST(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));
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));
667TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
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"));
686TEST(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()));
700TEST(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()));
715TEST(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 "
736TEST(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));
759TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
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);
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);
773TEST_P(AnyOfTestP, ExplainsResult) {
779 m = AnyOf(GreaterThan(10), Lt(0));
780 EXPECT_EQ(
"which is 5 less than 10", Explain(m, 5));
783 m = AnyOf(GreaterThan(10), GreaterThan(20));
784 EXPECT_EQ(
"which is 5 less than 10, and which is 15 less than 20",
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",
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",
802 m = AnyOf(GreaterThan(10), GreaterThan(20));
803 EXPECT_EQ(
"which is 5 more than 10", Explain(m, 15));
808 m = AnyOf(GreaterThan(10), Lt(30));
809 EXPECT_EQ(
"", Explain(m, 0));
813 m = AnyOf(GreaterThan(30), GreaterThan(20));
814 EXPECT_EQ(
"which is 5 more than 20", Explain(m, 25));
824int IsPositive(
double x) {
return x > 0 ? 1 : 0; }
830 explicit IsGreaterThan(
int threshold) : threshold_(threshold) {}
832 bool operator()(
int n)
const {
return n > threshold_; }
843bool ReferencesFooAndIsZero(
const int& n) {
return (&n == &foo) && (n == 0); }
847TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
848 Matcher<double> m = Truly(IsPositive);
849 EXPECT_TRUE(m.Matches(2.0));
850 EXPECT_FALSE(m.Matches(-1.5));
854TEST(TrulyTest, CanBeUsedWithFunctor) {
855 Matcher<int> m = Truly(IsGreaterThan(5));
856 EXPECT_TRUE(m.Matches(6));
857 EXPECT_FALSE(m.Matches(4));
861class ConvertibleToBool {
863 explicit ConvertibleToBool(
int number) : number_(number) {}
864 operator bool()
const {
return number_ != 0; }
870ConvertibleToBool IsNotZero(
int number) {
return ConvertibleToBool(number); }
875TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
876 Matcher<int> m = Truly(IsNotZero);
877 EXPECT_TRUE(m.Matches(1));
878 EXPECT_FALSE(m.Matches(0));
882TEST(TrulyTest, CanDescribeSelf) {
883 Matcher<double> m = Truly(IsPositive);
884 EXPECT_EQ(
"satisfies the given predicate", Describe(m));
889TEST(TrulyTest, WorksForByRefArguments) {
890 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
891 EXPECT_TRUE(m.Matches(foo));
893 EXPECT_FALSE(m.Matches(n));
897TEST(TrulyTest, ExplainsFailures) {
898 StringMatchResultListener listener;
899 EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener));
900 EXPECT_EQ(listener.str(),
"didn't satisfy the given predicate");
905TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
906 EXPECT_TRUE(Matches(Ge(0))(1));
907 EXPECT_FALSE(Matches(Eq(
'a'))(
'b'));
912TEST(MatchesTest, WorksOnByRefArguments) {
914 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
915 EXPECT_FALSE(Matches(Ref(m))(n));
920TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
921 Matcher<int> eq5 = Eq(5);
922 EXPECT_TRUE(Matches(eq5)(5));
923 EXPECT_FALSE(Matches(eq5)(2));
929TEST(ValueTest, WorksWithPolymorphicMatcher) {
930 EXPECT_TRUE(Value(
"hi", StartsWith(
"h")));
931 EXPECT_FALSE(Value(5, Gt(10)));
934TEST(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));
945TEST(AllArgsTest, WorksForTuple) {
946 EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt()));
947 EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt())));
950TEST(AllArgsTest, WorksForNonTuple) {
951 EXPECT_THAT(42, AllArgs(Gt(0)));
952 EXPECT_THAT(
'a', Not(AllArgs(Eq(
'b'))));
959 MOCK_METHOD2(Helper,
int(
char x,
int y));
962 AllArgsHelper(
const AllArgsHelper&) =
delete;
963 AllArgsHelper& operator=(
const AllArgsHelper&) =
delete;
966TEST(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));
976class 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;
994TEST(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));
1016template <
typename RawType>
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)); }
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_));
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_));
1074 EXPECT_FALSE(m5.Matches(nan1_));
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));
1085 Matcher<RawType&> m7 = matcher_maker(0.0);
1087 EXPECT_TRUE(m7.Matches(x));
1089 EXPECT_FALSE(m7.Matches(x));
1094 const Bits max_ulps_;
1096 const Bits zero_bits_;
1097 const Bits one_bits_;
1098 const Bits infinity_bits_;
1101 const RawType close_to_positive_zero_;
1102 const RawType close_to_negative_zero_;
1103 const RawType further_from_negative_zero_;
1106 const RawType close_to_one_;
1107 const RawType further_from_one_;
1110 const RawType infinity_;
1111 const RawType close_to_infinity_;
1112 const RawType further_from_infinity_;
1118 const RawType nan1_;
1119 const RawType nan2_;
1123template <
typename RawType>
1124class FloatingPointNearTest :
public FloatingPointTest<RawType> {
1126 typedef FloatingPointTest<RawType> ParentType;
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_));
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_));
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_));
1178 Matcher<RawType> m9 =
1179 matcher_maker(ParentType::max_, ParentType::infinity_);
1180 EXPECT_TRUE(m8.Matches(-ParentType::max_));
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_));
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));
1206typedef FloatingPointTest<float> FloatTest;
1208TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { TestMatches(&FloatEq); }
1210TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
1211 TestMatches(&NanSensitiveFloatEq);
1214TEST_F(FloatTest, FloatEqCannotMatchNaN) {
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));
1222TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
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));
1230TEST_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));
1244TEST_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));
1260typedef FloatingPointNearTest<float> FloatNearTest;
1262TEST_F(FloatNearTest, FloatNearMatches) { TestNearMatches(&FloatNear); }
1264TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
1265 TestNearMatches(&NanSensitiveFloatNear);
1268TEST_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));
1284TEST_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));
1300TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
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));
1308TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
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));
1317typedef FloatingPointTest<double> DoubleTest;
1319TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
1320 TestMatches(&DoubleEq);
1323TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
1324 TestMatches(&NanSensitiveDoubleEq);
1327TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
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));
1335TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
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));
1343TEST_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));
1357TEST_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));
1373typedef FloatingPointNearTest<double> DoubleNearTest;
1375TEST_F(DoubleNearTest, DoubleNearMatches) { TestNearMatches(&DoubleNear); }
1377TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
1378 TestNearMatches(&NanSensitiveDoubleNear);
1381TEST_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));
1397TEST_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);
1406 EXPECT_TRUE(explanation ==
"which is 1.2e-10 from 2.1" ||
1407 explanation ==
"which is 1.2e-010 from 2.1")
1408 <<
" where explanation is \"" << explanation <<
"\".";
1411TEST_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));
1427TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
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));
1435TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
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));
1443TEST(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))));
1449TEST(AllOfTest, HugeMatcher) {
1452 EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
1453 testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
1456TEST(AnyOfTest, HugeMatcher) {
1459 EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
1460 testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
1477template <
typename T1,
typename T2>
1478bool AllOf(
const T1& ,
const T2& ) {
1482TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1484 testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1487template <
typename T1,
typename T2>
1488bool AnyOf(
const T1&,
const T2&) {
1492TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1494 testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1499TEST(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)))));
1505TEST(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)))));
Definition gtest-internal.h:245