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 // Tests that AllOf(m1, ..., mn) matches any value that matches all of
433 // the given matchers.
TEST(AllOfTest,MatchesWhenAllMatch)434 TEST(AllOfTest, MatchesWhenAllMatch) {
435 Matcher<int> m;
436 m = AllOf(Le(2), Ge(1));
437 EXPECT_TRUE(m.Matches(1));
438 EXPECT_TRUE(m.Matches(2));
439 EXPECT_FALSE(m.Matches(0));
440 EXPECT_FALSE(m.Matches(3));
441
442 m = AllOf(Gt(0), Ne(1), Ne(2));
443 EXPECT_TRUE(m.Matches(3));
444 EXPECT_FALSE(m.Matches(2));
445 EXPECT_FALSE(m.Matches(1));
446 EXPECT_FALSE(m.Matches(0));
447
448 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
449 EXPECT_TRUE(m.Matches(4));
450 EXPECT_FALSE(m.Matches(3));
451 EXPECT_FALSE(m.Matches(2));
452 EXPECT_FALSE(m.Matches(1));
453 EXPECT_FALSE(m.Matches(0));
454
455 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
456 EXPECT_TRUE(m.Matches(0));
457 EXPECT_TRUE(m.Matches(1));
458 EXPECT_FALSE(m.Matches(3));
459
460 // The following tests for varying number of sub-matchers. Due to the way
461 // the sub-matchers are handled it is enough to test every sub-matcher once
462 // with sub-matchers using the same matcher type. Varying matcher types are
463 // checked for above.
464 AllOfMatches(2, AllOf(Ne(1), Ne(2)));
465 AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
466 AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
467 AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
468 AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
469 AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
470 AllOfMatches(8,
471 AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8)));
472 AllOfMatches(
473 9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9)));
474 AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
475 Ne(9), Ne(10)));
476 AllOfMatches(
477 50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
478 Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17),
479 Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25),
480 Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33),
481 Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41),
482 Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),
483 Ne(50)));
484 }
485
486 // Tests that AllOf(m1, ..., mn) describes itself properly.
TEST(AllOfTest,CanDescribeSelf)487 TEST(AllOfTest, CanDescribeSelf) {
488 Matcher<int> m;
489 m = AllOf(Le(2), Ge(1));
490 EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
491
492 m = AllOf(Gt(0), Ne(1), Ne(2));
493 std::string expected_descr1 =
494 "(is > 0) and (isn't equal to 1) and (isn't equal to 2)";
495 EXPECT_EQ(expected_descr1, Describe(m));
496
497 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
498 std::string expected_descr2 =
499 "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "
500 "to 3)";
501 EXPECT_EQ(expected_descr2, Describe(m));
502
503 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
504 std::string expected_descr3 =
505 "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "
506 "and (isn't equal to 7)";
507 EXPECT_EQ(expected_descr3, Describe(m));
508 }
509
510 // Tests that AllOf(m1, ..., mn) describes its negation properly.
TEST(AllOfTest,CanDescribeNegation)511 TEST(AllOfTest, CanDescribeNegation) {
512 Matcher<int> m;
513 m = AllOf(Le(2), Ge(1));
514 std::string expected_descr4 = "(isn't <= 2) or (isn't >= 1)";
515 EXPECT_EQ(expected_descr4, DescribeNegation(m));
516
517 m = AllOf(Gt(0), Ne(1), Ne(2));
518 std::string expected_descr5 =
519 "(isn't > 0) or (is equal to 1) or (is equal to 2)";
520 EXPECT_EQ(expected_descr5, DescribeNegation(m));
521
522 m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
523 std::string expected_descr6 =
524 "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";
525 EXPECT_EQ(expected_descr6, DescribeNegation(m));
526
527 m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
528 std::string expected_desr7 =
529 "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "
530 "(is equal to 7)";
531 EXPECT_EQ(expected_desr7, DescribeNegation(m));
532
533 m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
534 Ne(10), Ne(11));
535 AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
536 EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11)"));
537 AllOfMatches(11, m);
538 }
539
540 // Tests that monomorphic matchers are safely cast by the AllOf matcher.
TEST(AllOfTest,AllOfMatcherSafelyCastsMonomorphicMatchers)541 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
542 // greater_than_5 and less_than_10 are monomorphic matchers.
543 Matcher<int> greater_than_5 = Gt(5);
544 Matcher<int> less_than_10 = Lt(10);
545
546 Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
547 Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
548 Matcher<int&> m3 = AllOf(greater_than_5, m2);
549
550 // Tests that BothOf works when composing itself.
551 Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
552 Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
553 }
554
TEST(AllOfTest,ExplainsResult)555 TEST(AllOfTest, ExplainsResult) {
556 Matcher<int> m;
557
558 // Successful match. Both matchers need to explain. The second
559 // matcher doesn't give an explanation, so only the first matcher's
560 // explanation is printed.
561 m = AllOf(GreaterThan(10), Lt(30));
562 EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
563
564 // Successful match. Both matchers need to explain.
565 m = AllOf(GreaterThan(10), GreaterThan(20));
566 EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
567 Explain(m, 30));
568
569 // Successful match. All matchers need to explain. The second
570 // matcher doesn't given an explanation.
571 m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
572 EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
573 Explain(m, 25));
574
575 // Successful match. All matchers need to explain.
576 m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
577 EXPECT_EQ(
578 "which is 30 more than 10, and which is 20 more than 20, "
579 "and which is 10 more than 30",
580 Explain(m, 40));
581
582 // Failed match. The first matcher, which failed, needs to
583 // explain.
584 m = AllOf(GreaterThan(10), GreaterThan(20));
585 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
586
587 // Failed match. The second matcher, which failed, needs to
588 // explain. Since it doesn't given an explanation, nothing is
589 // printed.
590 m = AllOf(GreaterThan(10), Lt(30));
591 EXPECT_EQ("", Explain(m, 40));
592
593 // Failed match. The second matcher, which failed, needs to
594 // explain.
595 m = AllOf(GreaterThan(10), GreaterThan(20));
596 EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
597 }
598
599 // Helper to allow easy testing of AnyOf matchers with num parameters.
AnyOfMatches(int num,const Matcher<int> & m)600 static void AnyOfMatches(int num, const Matcher<int>& m) {
601 SCOPED_TRACE(Describe(m));
602 EXPECT_FALSE(m.Matches(0));
603 for (int i = 1; i <= num; ++i) {
604 EXPECT_TRUE(m.Matches(i));
605 }
606 EXPECT_FALSE(m.Matches(num + 1));
607 }
608
AnyOfStringMatches(int num,const Matcher<std::string> & m)609 static void AnyOfStringMatches(int num, const Matcher<std::string>& m) {
610 SCOPED_TRACE(Describe(m));
611 EXPECT_FALSE(m.Matches(std::to_string(0)));
612
613 for (int i = 1; i <= num; ++i) {
614 EXPECT_TRUE(m.Matches(std::to_string(i)));
615 }
616 EXPECT_FALSE(m.Matches(std::to_string(num + 1)));
617 }
618
619 // Tests that AnyOf(m1, ..., mn) matches any value that matches at
620 // least one of the given matchers.
TEST(AnyOfTest,MatchesWhenAnyMatches)621 TEST(AnyOfTest, MatchesWhenAnyMatches) {
622 Matcher<int> m;
623 m = AnyOf(Le(1), Ge(3));
624 EXPECT_TRUE(m.Matches(1));
625 EXPECT_TRUE(m.Matches(4));
626 EXPECT_FALSE(m.Matches(2));
627
628 m = AnyOf(Lt(0), Eq(1), Eq(2));
629 EXPECT_TRUE(m.Matches(-1));
630 EXPECT_TRUE(m.Matches(1));
631 EXPECT_TRUE(m.Matches(2));
632 EXPECT_FALSE(m.Matches(0));
633
634 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
635 EXPECT_TRUE(m.Matches(-1));
636 EXPECT_TRUE(m.Matches(1));
637 EXPECT_TRUE(m.Matches(2));
638 EXPECT_TRUE(m.Matches(3));
639 EXPECT_FALSE(m.Matches(0));
640
641 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
642 EXPECT_TRUE(m.Matches(0));
643 EXPECT_TRUE(m.Matches(11));
644 EXPECT_TRUE(m.Matches(3));
645 EXPECT_FALSE(m.Matches(2));
646
647 // The following tests for varying number of sub-matchers. Due to the way
648 // the sub-matchers are handled it is enough to test every sub-matcher once
649 // with sub-matchers using the same matcher type. Varying matcher types are
650 // checked for above.
651 AnyOfMatches(2, AnyOf(1, 2));
652 AnyOfMatches(3, AnyOf(1, 2, 3));
653 AnyOfMatches(4, AnyOf(1, 2, 3, 4));
654 AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
655 AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
656 AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
657 AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
658 AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
659 AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
660 }
661
662 // Tests the variadic version of the AnyOfMatcher.
TEST(AnyOfTest,VariadicMatchesWhenAnyMatches)663 TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
664 // Also make sure AnyOf is defined in the right namespace and does not depend
665 // on ADL.
666 Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
667
668 EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)"));
669 AnyOfMatches(11, m);
670 AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
671 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
672 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
673 45, 46, 47, 48, 49, 50));
674 AnyOfStringMatches(
675 50, AnyOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
676 "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
677 "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
678 "33", "34", "35", "36", "37", "38", "39", "40", "41", "42",
679 "43", "44", "45", "46", "47", "48", "49", "50"));
680 }
681
TEST(ConditionalTest,MatchesFirstIfCondition)682 TEST(ConditionalTest, MatchesFirstIfCondition) {
683 Matcher<std::string> eq_red = Eq("red");
684 Matcher<std::string> ne_red = Ne("red");
685 Matcher<std::string> m = Conditional(true, eq_red, ne_red);
686 EXPECT_TRUE(m.Matches("red"));
687 EXPECT_FALSE(m.Matches("green"));
688
689 StringMatchResultListener listener;
690 StringMatchResultListener expected;
691 EXPECT_FALSE(m.MatchAndExplain("green", &listener));
692 EXPECT_FALSE(eq_red.MatchAndExplain("green", &expected));
693 EXPECT_THAT(listener.str(), Eq(expected.str()));
694 }
695
TEST(ConditionalTest,MatchesSecondIfCondition)696 TEST(ConditionalTest, MatchesSecondIfCondition) {
697 Matcher<std::string> eq_red = Eq("red");
698 Matcher<std::string> ne_red = Ne("red");
699 Matcher<std::string> m = Conditional(false, eq_red, ne_red);
700 EXPECT_FALSE(m.Matches("red"));
701 EXPECT_TRUE(m.Matches("green"));
702
703 StringMatchResultListener listener;
704 StringMatchResultListener expected;
705 EXPECT_FALSE(m.MatchAndExplain("red", &listener));
706 EXPECT_FALSE(ne_red.MatchAndExplain("red", &expected));
707 EXPECT_THAT(listener.str(), Eq(expected.str()));
708 }
709
710 // Tests that AnyOf(m1, ..., mn) describes itself properly.
TEST(AnyOfTest,CanDescribeSelf)711 TEST(AnyOfTest, CanDescribeSelf) {
712 Matcher<int> m;
713 m = AnyOf(Le(1), Ge(3));
714
715 EXPECT_EQ("(is <= 1) or (is >= 3)", Describe(m));
716
717 m = AnyOf(Lt(0), Eq(1), Eq(2));
718 EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m));
719
720 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
721 EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",
722 Describe(m));
723
724 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
725 EXPECT_EQ(
726 "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
727 "equal to 7)",
728 Describe(m));
729 }
730
731 // Tests that AnyOf(m1, ..., mn) describes its negation properly.
TEST(AnyOfTest,CanDescribeNegation)732 TEST(AnyOfTest, CanDescribeNegation) {
733 Matcher<int> m;
734 m = AnyOf(Le(1), Ge(3));
735 EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", DescribeNegation(m));
736
737 m = AnyOf(Lt(0), Eq(1), Eq(2));
738 EXPECT_EQ("(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",
739 DescribeNegation(m));
740
741 m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
742 EXPECT_EQ(
743 "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
744 "equal to 3)",
745 DescribeNegation(m));
746
747 m = AnyOf(Le(0), Gt(10), 3, 5, 7);
748 EXPECT_EQ(
749 "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "
750 "to 5) and (isn't equal to 7)",
751 DescribeNegation(m));
752 }
753
754 // Tests that monomorphic matchers are safely cast by the AnyOf matcher.
TEST(AnyOfTest,AnyOfMatcherSafelyCastsMonomorphicMatchers)755 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
756 // greater_than_5 and less_than_10 are monomorphic matchers.
757 Matcher<int> greater_than_5 = Gt(5);
758 Matcher<int> less_than_10 = Lt(10);
759
760 Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
761 Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
762 Matcher<int&> m3 = AnyOf(greater_than_5, m2);
763
764 // Tests that EitherOf works when composing itself.
765 Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
766 Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
767 }
768
TEST(AnyOfTest,ExplainsResult)769 TEST(AnyOfTest, ExplainsResult) {
770 Matcher<int> m;
771
772 // Failed match. Both matchers need to explain. The second
773 // matcher doesn't give an explanation, so only the first matcher's
774 // explanation is printed.
775 m = AnyOf(GreaterThan(10), Lt(0));
776 EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
777
778 // Failed match. Both matchers need to explain.
779 m = AnyOf(GreaterThan(10), GreaterThan(20));
780 EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
781 Explain(m, 5));
782
783 // Failed match. All matchers need to explain. The second
784 // matcher doesn't given an explanation.
785 m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
786 EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
787 Explain(m, 5));
788
789 // Failed match. All matchers need to explain.
790 m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
791 EXPECT_EQ(
792 "which is 5 less than 10, and which is 15 less than 20, "
793 "and which is 25 less than 30",
794 Explain(m, 5));
795
796 // Successful match. The first matcher, which succeeded, needs to
797 // explain.
798 m = AnyOf(GreaterThan(10), GreaterThan(20));
799 EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
800
801 // Successful match. The second matcher, which succeeded, needs to
802 // explain. Since it doesn't given an explanation, nothing is
803 // printed.
804 m = AnyOf(GreaterThan(10), Lt(30));
805 EXPECT_EQ("", Explain(m, 0));
806
807 // Successful match. The second matcher, which succeeded, needs to
808 // explain.
809 m = AnyOf(GreaterThan(30), GreaterThan(20));
810 EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
811 }
812
813 // The following predicate function and predicate functor are for
814 // testing the Truly(predicate) matcher.
815
816 // Returns non-zero if the input is positive. Note that the return
817 // type of this function is not bool. It's OK as Truly() accepts any
818 // unary function or functor whose return type can be implicitly
819 // converted to bool.
IsPositive(double x)820 int IsPositive(double x) { return x > 0 ? 1 : 0; }
821
822 // This functor returns true if the input is greater than the given
823 // number.
824 class IsGreaterThan {
825 public:
IsGreaterThan(int threshold)826 explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
827
operator ()(int n) const828 bool operator()(int n) const { return n > threshold_; }
829
830 private:
831 int threshold_;
832 };
833
834 // For testing Truly().
835 const int foo = 0;
836
837 // This predicate returns true if and only if the argument references foo and
838 // has a zero value.
ReferencesFooAndIsZero(const int & n)839 bool ReferencesFooAndIsZero(const int& n) { return (&n == &foo) && (n == 0); }
840
841 // Tests that Truly(predicate) matches what satisfies the given
842 // predicate.
TEST(TrulyTest,MatchesWhatSatisfiesThePredicate)843 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
844 Matcher<double> m = Truly(IsPositive);
845 EXPECT_TRUE(m.Matches(2.0));
846 EXPECT_FALSE(m.Matches(-1.5));
847 }
848
849 // Tests that Truly(predicate_functor) works too.
TEST(TrulyTest,CanBeUsedWithFunctor)850 TEST(TrulyTest, CanBeUsedWithFunctor) {
851 Matcher<int> m = Truly(IsGreaterThan(5));
852 EXPECT_TRUE(m.Matches(6));
853 EXPECT_FALSE(m.Matches(4));
854 }
855
856 // A class that can be implicitly converted to bool.
857 class ConvertibleToBool {
858 public:
ConvertibleToBool(int number)859 explicit ConvertibleToBool(int number) : number_(number) {}
operator bool() const860 operator bool() const { return number_ != 0; }
861
862 private:
863 int number_;
864 };
865
IsNotZero(int number)866 ConvertibleToBool IsNotZero(int number) { return ConvertibleToBool(number); }
867
868 // Tests that the predicate used in Truly() may return a class that's
869 // implicitly convertible to bool, even when the class has no
870 // operator!().
TEST(TrulyTest,PredicateCanReturnAClassConvertibleToBool)871 TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
872 Matcher<int> m = Truly(IsNotZero);
873 EXPECT_TRUE(m.Matches(1));
874 EXPECT_FALSE(m.Matches(0));
875 }
876
877 // Tests that Truly(predicate) can describe itself properly.
TEST(TrulyTest,CanDescribeSelf)878 TEST(TrulyTest, CanDescribeSelf) {
879 Matcher<double> m = Truly(IsPositive);
880 EXPECT_EQ("satisfies the given predicate", Describe(m));
881 }
882
883 // Tests that Truly(predicate) works when the matcher takes its
884 // argument by reference.
TEST(TrulyTest,WorksForByRefArguments)885 TEST(TrulyTest, WorksForByRefArguments) {
886 Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
887 EXPECT_TRUE(m.Matches(foo));
888 int n = 0;
889 EXPECT_FALSE(m.Matches(n));
890 }
891
892 // Tests that Truly(predicate) provides a helpful reason when it fails.
TEST(TrulyTest,ExplainsFailures)893 TEST(TrulyTest, ExplainsFailures) {
894 StringMatchResultListener listener;
895 EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener));
896 EXPECT_EQ(listener.str(), "didn't satisfy the given predicate");
897 }
898
899 // Tests that Matches(m) is a predicate satisfied by whatever that
900 // matches matcher m.
TEST(MatchesTest,IsSatisfiedByWhatMatchesTheMatcher)901 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
902 EXPECT_TRUE(Matches(Ge(0))(1));
903 EXPECT_FALSE(Matches(Eq('a'))('b'));
904 }
905
906 // Tests that Matches(m) works when the matcher takes its argument by
907 // reference.
TEST(MatchesTest,WorksOnByRefArguments)908 TEST(MatchesTest, WorksOnByRefArguments) {
909 int m = 0, n = 0;
910 EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
911 EXPECT_FALSE(Matches(Ref(m))(n));
912 }
913
914 // Tests that a Matcher on non-reference type can be used in
915 // Matches().
TEST(MatchesTest,WorksWithMatcherOnNonRefType)916 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
917 Matcher<int> eq5 = Eq(5);
918 EXPECT_TRUE(Matches(eq5)(5));
919 EXPECT_FALSE(Matches(eq5)(2));
920 }
921
922 // Tests Value(value, matcher). Since Value() is a simple wrapper for
923 // Matches(), which has been tested already, we don't spend a lot of
924 // effort on testing Value().
TEST(ValueTest,WorksWithPolymorphicMatcher)925 TEST(ValueTest, WorksWithPolymorphicMatcher) {
926 EXPECT_TRUE(Value("hi", StartsWith("h")));
927 EXPECT_FALSE(Value(5, Gt(10)));
928 }
929
TEST(ValueTest,WorksWithMonomorphicMatcher)930 TEST(ValueTest, WorksWithMonomorphicMatcher) {
931 const Matcher<int> is_zero = Eq(0);
932 EXPECT_TRUE(Value(0, is_zero));
933 EXPECT_FALSE(Value('a', is_zero));
934
935 int n = 0;
936 const Matcher<const int&> ref_n = Ref(n);
937 EXPECT_TRUE(Value(n, ref_n));
938 EXPECT_FALSE(Value(1, ref_n));
939 }
940
TEST(AllArgsTest,WorksForTuple)941 TEST(AllArgsTest, WorksForTuple) {
942 EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt()));
943 EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt())));
944 }
945
TEST(AllArgsTest,WorksForNonTuple)946 TEST(AllArgsTest, WorksForNonTuple) {
947 EXPECT_THAT(42, AllArgs(Gt(0)));
948 EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
949 }
950
951 class AllArgsHelper {
952 public:
AllArgsHelper()953 AllArgsHelper() {}
954
955 MOCK_METHOD2(Helper, int(char x, int y));
956
957 private:
958 GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
959 };
960
TEST(AllArgsTest,WorksInWithClause)961 TEST(AllArgsTest, WorksInWithClause) {
962 AllArgsHelper helper;
963 ON_CALL(helper, Helper(_, _)).With(AllArgs(Lt())).WillByDefault(Return(1));
964 EXPECT_CALL(helper, Helper(_, _));
965 EXPECT_CALL(helper, Helper(_, _)).With(AllArgs(Gt())).WillOnce(Return(2));
966
967 EXPECT_EQ(1, helper.Helper('\1', 2));
968 EXPECT_EQ(2, helper.Helper('a', 1));
969 }
970
971 class OptionalMatchersHelper {
972 public:
OptionalMatchersHelper()973 OptionalMatchersHelper() {}
974
975 MOCK_METHOD0(NoArgs, int());
976
977 MOCK_METHOD1(OneArg, int(int y));
978
979 MOCK_METHOD2(TwoArgs, int(char x, int y));
980
981 MOCK_METHOD1(Overloaded, int(char x));
982 MOCK_METHOD2(Overloaded, int(char x, int y));
983
984 private:
985 GTEST_DISALLOW_COPY_AND_ASSIGN_(OptionalMatchersHelper);
986 };
987
TEST(AllArgsTest,WorksWithoutMatchers)988 TEST(AllArgsTest, WorksWithoutMatchers) {
989 OptionalMatchersHelper helper;
990
991 ON_CALL(helper, NoArgs).WillByDefault(Return(10));
992 ON_CALL(helper, OneArg).WillByDefault(Return(20));
993 ON_CALL(helper, TwoArgs).WillByDefault(Return(30));
994
995 EXPECT_EQ(10, helper.NoArgs());
996 EXPECT_EQ(20, helper.OneArg(1));
997 EXPECT_EQ(30, helper.TwoArgs('\1', 2));
998
999 EXPECT_CALL(helper, NoArgs).Times(1);
1000 EXPECT_CALL(helper, OneArg).WillOnce(Return(100));
1001 EXPECT_CALL(helper, OneArg(17)).WillOnce(Return(200));
1002 EXPECT_CALL(helper, TwoArgs).Times(0);
1003
1004 EXPECT_EQ(10, helper.NoArgs());
1005 EXPECT_EQ(100, helper.OneArg(1));
1006 EXPECT_EQ(200, helper.OneArg(17));
1007 }
1008
1009 // Tests floating-point matchers.
1010 template <typename RawType>
1011 class FloatingPointTest : public testing::Test {
1012 protected:
1013 typedef testing::internal::FloatingPoint<RawType> Floating;
1014 typedef typename Floating::Bits Bits;
1015
FloatingPointTest()1016 FloatingPointTest()
1017 : max_ulps_(Floating::kMaxUlps),
1018 zero_bits_(Floating(0).bits()),
1019 one_bits_(Floating(1).bits()),
1020 infinity_bits_(Floating(Floating::Infinity()).bits()),
1021 close_to_positive_zero_(
1022 Floating::ReinterpretBits(zero_bits_ + max_ulps_ / 2)),
1023 close_to_negative_zero_(
1024 -Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_ / 2)),
1025 further_from_negative_zero_(-Floating::ReinterpretBits(
1026 zero_bits_ + max_ulps_ + 1 - max_ulps_ / 2)),
1027 close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)),
1028 further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)),
1029 infinity_(Floating::Infinity()),
1030 close_to_infinity_(
1031 Floating::ReinterpretBits(infinity_bits_ - max_ulps_)),
1032 further_from_infinity_(
1033 Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)),
1034 max_(Floating::Max()),
1035 nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
1036 nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {}
1037
TestSize()1038 void TestSize() { EXPECT_EQ(sizeof(RawType), sizeof(Bits)); }
1039
1040 // A battery of tests for FloatingEqMatcher::Matches.
1041 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
TestMatches(testing::internal::FloatingEqMatcher<RawType> (* matcher_maker)(RawType))1042 void TestMatches(
1043 testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
1044 Matcher<RawType> m1 = matcher_maker(0.0);
1045 EXPECT_TRUE(m1.Matches(-0.0));
1046 EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
1047 EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
1048 EXPECT_FALSE(m1.Matches(1.0));
1049
1050 Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
1051 EXPECT_FALSE(m2.Matches(further_from_negative_zero_));
1052
1053 Matcher<RawType> m3 = matcher_maker(1.0);
1054 EXPECT_TRUE(m3.Matches(close_to_one_));
1055 EXPECT_FALSE(m3.Matches(further_from_one_));
1056
1057 // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
1058 EXPECT_FALSE(m3.Matches(0.0));
1059
1060 Matcher<RawType> m4 = matcher_maker(-infinity_);
1061 EXPECT_TRUE(m4.Matches(-close_to_infinity_));
1062
1063 Matcher<RawType> m5 = matcher_maker(infinity_);
1064 EXPECT_TRUE(m5.Matches(close_to_infinity_));
1065
1066 // This is interesting as the representations of infinity_ and nan1_
1067 // are only 1 DLP apart.
1068 EXPECT_FALSE(m5.Matches(nan1_));
1069
1070 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
1071 // some cases.
1072 Matcher<const RawType&> m6 = matcher_maker(0.0);
1073 EXPECT_TRUE(m6.Matches(-0.0));
1074 EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
1075 EXPECT_FALSE(m6.Matches(1.0));
1076
1077 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
1078 // cases.
1079 Matcher<RawType&> m7 = matcher_maker(0.0);
1080 RawType x = 0.0;
1081 EXPECT_TRUE(m7.Matches(x));
1082 x = 0.01f;
1083 EXPECT_FALSE(m7.Matches(x));
1084 }
1085
1086 // Pre-calculated numbers to be used by the tests.
1087
1088 const Bits max_ulps_;
1089
1090 const Bits zero_bits_; // The bits that represent 0.0.
1091 const Bits one_bits_; // The bits that represent 1.0.
1092 const Bits infinity_bits_; // The bits that represent +infinity.
1093
1094 // Some numbers close to 0.0.
1095 const RawType close_to_positive_zero_;
1096 const RawType close_to_negative_zero_;
1097 const RawType further_from_negative_zero_;
1098
1099 // Some numbers close to 1.0.
1100 const RawType close_to_one_;
1101 const RawType further_from_one_;
1102
1103 // Some numbers close to +infinity.
1104 const RawType infinity_;
1105 const RawType close_to_infinity_;
1106 const RawType further_from_infinity_;
1107
1108 // Maximum representable value that's not infinity.
1109 const RawType max_;
1110
1111 // Some NaNs.
1112 const RawType nan1_;
1113 const RawType nan2_;
1114 };
1115
1116 // Tests floating-point matchers with fixed epsilons.
1117 template <typename RawType>
1118 class FloatingPointNearTest : public FloatingPointTest<RawType> {
1119 protected:
1120 typedef FloatingPointTest<RawType> ParentType;
1121
1122 // A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon.
1123 // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
TestNearMatches(testing::internal::FloatingEqMatcher<RawType> (* matcher_maker)(RawType,RawType))1124 void TestNearMatches(testing::internal::FloatingEqMatcher<RawType> (
1125 *matcher_maker)(RawType, RawType)) {
1126 Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
1127 EXPECT_TRUE(m1.Matches(0.0));
1128 EXPECT_TRUE(m1.Matches(-0.0));
1129 EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_));
1130 EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_));
1131 EXPECT_FALSE(m1.Matches(1.0));
1132
1133 Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
1134 EXPECT_TRUE(m2.Matches(0.0));
1135 EXPECT_TRUE(m2.Matches(-0.0));
1136 EXPECT_TRUE(m2.Matches(1.0));
1137 EXPECT_TRUE(m2.Matches(-1.0));
1138 EXPECT_FALSE(m2.Matches(ParentType::close_to_one_));
1139 EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_));
1140
1141 // Check that inf matches inf, regardless of the of the specified max
1142 // absolute error.
1143 Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0);
1144 EXPECT_TRUE(m3.Matches(ParentType::infinity_));
1145 EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_));
1146 EXPECT_FALSE(m3.Matches(-ParentType::infinity_));
1147
1148 Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0);
1149 EXPECT_TRUE(m4.Matches(-ParentType::infinity_));
1150 EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_));
1151 EXPECT_FALSE(m4.Matches(ParentType::infinity_));
1152
1153 // Test various overflow scenarios.
1154 Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_);
1155 EXPECT_TRUE(m5.Matches(ParentType::max_));
1156 EXPECT_FALSE(m5.Matches(-ParentType::max_));
1157
1158 Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_);
1159 EXPECT_FALSE(m6.Matches(ParentType::max_));
1160 EXPECT_TRUE(m6.Matches(-ParentType::max_));
1161
1162 Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0);
1163 EXPECT_TRUE(m7.Matches(ParentType::max_));
1164 EXPECT_FALSE(m7.Matches(-ParentType::max_));
1165
1166 Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0);
1167 EXPECT_FALSE(m8.Matches(ParentType::max_));
1168 EXPECT_TRUE(m8.Matches(-ParentType::max_));
1169
1170 // The difference between max() and -max() normally overflows to infinity,
1171 // but it should still match if the max_abs_error is also infinity.
1172 Matcher<RawType> m9 =
1173 matcher_maker(ParentType::max_, ParentType::infinity_);
1174 EXPECT_TRUE(m8.Matches(-ParentType::max_));
1175
1176 // matcher_maker can produce a Matcher<const RawType&>, which is needed in
1177 // some cases.
1178 Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
1179 EXPECT_TRUE(m10.Matches(-0.0));
1180 EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_));
1181 EXPECT_FALSE(m10.Matches(ParentType::close_to_one_));
1182
1183 // matcher_maker can produce a Matcher<RawType&>, which is needed in some
1184 // cases.
1185 Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
1186 RawType x = 0.0;
1187 EXPECT_TRUE(m11.Matches(x));
1188 x = 1.0f;
1189 EXPECT_TRUE(m11.Matches(x));
1190 x = -1.0f;
1191 EXPECT_TRUE(m11.Matches(x));
1192 x = 1.1f;
1193 EXPECT_FALSE(m11.Matches(x));
1194 x = -1.1f;
1195 EXPECT_FALSE(m11.Matches(x));
1196 }
1197 };
1198
1199 // Instantiate FloatingPointTest for testing floats.
1200 typedef FloatingPointTest<float> FloatTest;
1201
TEST_F(FloatTest,FloatEqApproximatelyMatchesFloats)1202 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { TestMatches(&FloatEq); }
1203
TEST_F(FloatTest,NanSensitiveFloatEqApproximatelyMatchesFloats)1204 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
1205 TestMatches(&NanSensitiveFloatEq);
1206 }
1207
TEST_F(FloatTest,FloatEqCannotMatchNaN)1208 TEST_F(FloatTest, FloatEqCannotMatchNaN) {
1209 // FloatEq never matches NaN.
1210 Matcher<float> m = FloatEq(nan1_);
1211 EXPECT_FALSE(m.Matches(nan1_));
1212 EXPECT_FALSE(m.Matches(nan2_));
1213 EXPECT_FALSE(m.Matches(1.0));
1214 }
1215
TEST_F(FloatTest,NanSensitiveFloatEqCanMatchNaN)1216 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
1217 // NanSensitiveFloatEq will match NaN.
1218 Matcher<float> m = NanSensitiveFloatEq(nan1_);
1219 EXPECT_TRUE(m.Matches(nan1_));
1220 EXPECT_TRUE(m.Matches(nan2_));
1221 EXPECT_FALSE(m.Matches(1.0));
1222 }
1223
TEST_F(FloatTest,FloatEqCanDescribeSelf)1224 TEST_F(FloatTest, FloatEqCanDescribeSelf) {
1225 Matcher<float> m1 = FloatEq(2.0f);
1226 EXPECT_EQ("is approximately 2", Describe(m1));
1227 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
1228
1229 Matcher<float> m2 = FloatEq(0.5f);
1230 EXPECT_EQ("is approximately 0.5", Describe(m2));
1231 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
1232
1233 Matcher<float> m3 = FloatEq(nan1_);
1234 EXPECT_EQ("never matches", Describe(m3));
1235 EXPECT_EQ("is anything", DescribeNegation(m3));
1236 }
1237
TEST_F(FloatTest,NanSensitiveFloatEqCanDescribeSelf)1238 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
1239 Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
1240 EXPECT_EQ("is approximately 2", Describe(m1));
1241 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
1242
1243 Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
1244 EXPECT_EQ("is approximately 0.5", Describe(m2));
1245 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
1246
1247 Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
1248 EXPECT_EQ("is NaN", Describe(m3));
1249 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
1250 }
1251
1252 // Instantiate FloatingPointTest for testing floats with a user-specified
1253 // max absolute error.
1254 typedef FloatingPointNearTest<float> FloatNearTest;
1255
TEST_F(FloatNearTest,FloatNearMatches)1256 TEST_F(FloatNearTest, FloatNearMatches) { TestNearMatches(&FloatNear); }
1257
TEST_F(FloatNearTest,NanSensitiveFloatNearApproximatelyMatchesFloats)1258 TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
1259 TestNearMatches(&NanSensitiveFloatNear);
1260 }
1261
TEST_F(FloatNearTest,FloatNearCanDescribeSelf)1262 TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
1263 Matcher<float> m1 = FloatNear(2.0f, 0.5f);
1264 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
1265 EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
1266 DescribeNegation(m1));
1267
1268 Matcher<float> m2 = FloatNear(0.5f, 0.5f);
1269 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
1270 EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
1271 DescribeNegation(m2));
1272
1273 Matcher<float> m3 = FloatNear(nan1_, 0.0);
1274 EXPECT_EQ("never matches", Describe(m3));
1275 EXPECT_EQ("is anything", DescribeNegation(m3));
1276 }
1277
TEST_F(FloatNearTest,NanSensitiveFloatNearCanDescribeSelf)1278 TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
1279 Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f);
1280 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
1281 EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
1282 DescribeNegation(m1));
1283
1284 Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f);
1285 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
1286 EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
1287 DescribeNegation(m2));
1288
1289 Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f);
1290 EXPECT_EQ("is NaN", Describe(m3));
1291 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
1292 }
1293
TEST_F(FloatNearTest,FloatNearCannotMatchNaN)1294 TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
1295 // FloatNear never matches NaN.
1296 Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f);
1297 EXPECT_FALSE(m.Matches(nan1_));
1298 EXPECT_FALSE(m.Matches(nan2_));
1299 EXPECT_FALSE(m.Matches(1.0));
1300 }
1301
TEST_F(FloatNearTest,NanSensitiveFloatNearCanMatchNaN)1302 TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
1303 // NanSensitiveFloatNear will match NaN.
1304 Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f);
1305 EXPECT_TRUE(m.Matches(nan1_));
1306 EXPECT_TRUE(m.Matches(nan2_));
1307 EXPECT_FALSE(m.Matches(1.0));
1308 }
1309
1310 // Instantiate FloatingPointTest for testing doubles.
1311 typedef FloatingPointTest<double> DoubleTest;
1312
TEST_F(DoubleTest,DoubleEqApproximatelyMatchesDoubles)1313 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
1314 TestMatches(&DoubleEq);
1315 }
1316
TEST_F(DoubleTest,NanSensitiveDoubleEqApproximatelyMatchesDoubles)1317 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
1318 TestMatches(&NanSensitiveDoubleEq);
1319 }
1320
TEST_F(DoubleTest,DoubleEqCannotMatchNaN)1321 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
1322 // DoubleEq never matches NaN.
1323 Matcher<double> m = DoubleEq(nan1_);
1324 EXPECT_FALSE(m.Matches(nan1_));
1325 EXPECT_FALSE(m.Matches(nan2_));
1326 EXPECT_FALSE(m.Matches(1.0));
1327 }
1328
TEST_F(DoubleTest,NanSensitiveDoubleEqCanMatchNaN)1329 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
1330 // NanSensitiveDoubleEq will match NaN.
1331 Matcher<double> m = NanSensitiveDoubleEq(nan1_);
1332 EXPECT_TRUE(m.Matches(nan1_));
1333 EXPECT_TRUE(m.Matches(nan2_));
1334 EXPECT_FALSE(m.Matches(1.0));
1335 }
1336
TEST_F(DoubleTest,DoubleEqCanDescribeSelf)1337 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
1338 Matcher<double> m1 = DoubleEq(2.0);
1339 EXPECT_EQ("is approximately 2", Describe(m1));
1340 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
1341
1342 Matcher<double> m2 = DoubleEq(0.5);
1343 EXPECT_EQ("is approximately 0.5", Describe(m2));
1344 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
1345
1346 Matcher<double> m3 = DoubleEq(nan1_);
1347 EXPECT_EQ("never matches", Describe(m3));
1348 EXPECT_EQ("is anything", DescribeNegation(m3));
1349 }
1350
TEST_F(DoubleTest,NanSensitiveDoubleEqCanDescribeSelf)1351 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
1352 Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
1353 EXPECT_EQ("is approximately 2", Describe(m1));
1354 EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
1355
1356 Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
1357 EXPECT_EQ("is approximately 0.5", Describe(m2));
1358 EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
1359
1360 Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
1361 EXPECT_EQ("is NaN", Describe(m3));
1362 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
1363 }
1364
1365 // Instantiate FloatingPointTest for testing floats with a user-specified
1366 // max absolute error.
1367 typedef FloatingPointNearTest<double> DoubleNearTest;
1368
TEST_F(DoubleNearTest,DoubleNearMatches)1369 TEST_F(DoubleNearTest, DoubleNearMatches) { TestNearMatches(&DoubleNear); }
1370
TEST_F(DoubleNearTest,NanSensitiveDoubleNearApproximatelyMatchesDoubles)1371 TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
1372 TestNearMatches(&NanSensitiveDoubleNear);
1373 }
1374
TEST_F(DoubleNearTest,DoubleNearCanDescribeSelf)1375 TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
1376 Matcher<double> m1 = DoubleNear(2.0, 0.5);
1377 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
1378 EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
1379 DescribeNegation(m1));
1380
1381 Matcher<double> m2 = DoubleNear(0.5, 0.5);
1382 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
1383 EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
1384 DescribeNegation(m2));
1385
1386 Matcher<double> m3 = DoubleNear(nan1_, 0.0);
1387 EXPECT_EQ("never matches", Describe(m3));
1388 EXPECT_EQ("is anything", DescribeNegation(m3));
1389 }
1390
TEST_F(DoubleNearTest,ExplainsResultWhenMatchFails)1391 TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {
1392 EXPECT_EQ("", Explain(DoubleNear(2.0, 0.1), 2.05));
1393 EXPECT_EQ("which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2));
1394 EXPECT_EQ("which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7));
1395
1396 const std::string explanation =
1397 Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);
1398 // Different C++ implementations may print floating-point numbers
1399 // slightly differently.
1400 EXPECT_TRUE(explanation == "which is 1.2e-10 from 2.1" || // GCC
1401 explanation == "which is 1.2e-010 from 2.1") // MSVC
1402 << " where explanation is \"" << explanation << "\".";
1403 }
1404
TEST_F(DoubleNearTest,NanSensitiveDoubleNearCanDescribeSelf)1405 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
1406 Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);
1407 EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
1408 EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)",
1409 DescribeNegation(m1));
1410
1411 Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);
1412 EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
1413 EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)",
1414 DescribeNegation(m2));
1415
1416 Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1);
1417 EXPECT_EQ("is NaN", Describe(m3));
1418 EXPECT_EQ("isn't NaN", DescribeNegation(m3));
1419 }
1420
TEST_F(DoubleNearTest,DoubleNearCannotMatchNaN)1421 TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
1422 // DoubleNear never matches NaN.
1423 Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1);
1424 EXPECT_FALSE(m.Matches(nan1_));
1425 EXPECT_FALSE(m.Matches(nan2_));
1426 EXPECT_FALSE(m.Matches(1.0));
1427 }
1428
TEST_F(DoubleNearTest,NanSensitiveDoubleNearCanMatchNaN)1429 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
1430 // NanSensitiveDoubleNear will match NaN.
1431 Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1);
1432 EXPECT_TRUE(m.Matches(nan1_));
1433 EXPECT_TRUE(m.Matches(nan2_));
1434 EXPECT_FALSE(m.Matches(1.0));
1435 }
1436
TEST(NotTest,WorksOnMoveOnlyType)1437 TEST(NotTest, WorksOnMoveOnlyType) {
1438 std::unique_ptr<int> p(new int(3));
1439 EXPECT_THAT(p, Pointee(Eq(3)));
1440 EXPECT_THAT(p, Not(Pointee(Eq(2))));
1441 }
1442
TEST(AllOfTest,HugeMatcher)1443 TEST(AllOfTest, HugeMatcher) {
1444 // Verify that using AllOf with many arguments doesn't cause
1445 // the compiler to exceed template instantiation depth limit.
1446 EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
1447 testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
1448 }
1449
TEST(AnyOfTest,HugeMatcher)1450 TEST(AnyOfTest, HugeMatcher) {
1451 // Verify that using AnyOf with many arguments doesn't cause
1452 // the compiler to exceed template instantiation depth limit.
1453 EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
1454 testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
1455 }
1456
1457 namespace adl_test {
1458
1459 // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
1460 // don't issue unqualified recursive calls. If they do, the argument dependent
1461 // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
1462 // as a candidate and the compilation will break due to an ambiguous overload.
1463
1464 // The matcher must be in the same namespace as AllOf/AnyOf to make argument
1465 // dependent lookup find those.
1466 MATCHER(M, "") {
1467 (void)arg;
1468 return true;
1469 }
1470
1471 template <typename T1, typename T2>
AllOf(const T1 &,const T2 &)1472 bool AllOf(const T1& /*t1*/, const T2& /*t2*/) {
1473 return true;
1474 }
1475
TEST(AllOfTest,DoesNotCallAllOfUnqualified)1476 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1477 EXPECT_THAT(42,
1478 testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1479 }
1480
1481 template <typename T1, typename T2>
AnyOf(const T1 &,const T2 &)1482 bool AnyOf(const T1&, const T2&) {
1483 return true;
1484 }
1485
TEST(AnyOfTest,DoesNotCallAnyOfUnqualified)1486 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1487 EXPECT_THAT(42,
1488 testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1489 }
1490
1491 } // namespace adl_test
1492
TEST(AllOfTest,WorksOnMoveOnlyType)1493 TEST(AllOfTest, WorksOnMoveOnlyType) {
1494 std::unique_ptr<int> p(new int(3));
1495 EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
1496 EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
1497 }
1498
TEST(AnyOfTest,WorksOnMoveOnlyType)1499 TEST(AnyOfTest, WorksOnMoveOnlyType) {
1500 std::unique_ptr<int> p(new int(3));
1501 EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
1502 EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
1503 }
1504
1505 } // namespace
1506 } // namespace gmock_matchers_test
1507 } // namespace testing
1508
1509 #ifdef _MSC_VER
1510 #pragma warning(pop)
1511 #endif
1512