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