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