googletest/googlemock/test/gmock-matchers_test.cc
Go to the documentation of this file.
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 
31 // Google Mock - a framework for writing C++ mock classes.
32 //
33 // This file tests some commonly used argument matchers.
34 
35 // Silence warning C4244: 'initializing': conversion from 'int' to 'short',
36 // possible loss of data and C4100, unreferenced local parameter
37 #ifdef _MSC_VER
38 # pragma warning(push)
39 # pragma warning(disable:4244)
40 # pragma warning(disable:4100)
41 #endif
42 
43 #include "gmock/gmock-matchers.h"
44 
45 #include <string.h>
46 #include <time.h>
47 
48 #include <array>
49 #include <cstdint>
50 #include <deque>
51 #include <forward_list>
52 #include <functional>
53 #include <iostream>
54 #include <iterator>
55 #include <limits>
56 #include <list>
57 #include <map>
58 #include <memory>
59 #include <set>
60 #include <sstream>
61 #include <string>
62 #include <type_traits>
63 #include <unordered_map>
64 #include <unordered_set>
65 #include <utility>
66 #include <vector>
67 
68 #include "gmock/gmock-more-matchers.h"
69 #include "gmock/gmock.h"
70 #include "gtest/gtest-spi.h"
71 #include "gtest/gtest.h"
72 
73 namespace testing {
74 namespace gmock_matchers_test {
75 namespace {
76 
77 using std::greater;
78 using std::less;
79 using std::list;
80 using std::make_pair;
81 using std::map;
82 using std::multimap;
83 using std::multiset;
84 using std::ostream;
85 using std::pair;
86 using std::set;
87 using std::stringstream;
88 using std::vector;
102 
103 // Helper for testing container-valued matchers in mock method context. It is
104 // important to test matchers in this context, since it requires additional type
105 // deduction beyond what EXPECT_THAT does, thus making it more restrictive.
106 struct ContainerHelper {
107  MOCK_METHOD1(Call, void(std::vector<std::unique_ptr<int>>));
108 };
109 
110 std::vector<std::unique_ptr<int>> MakeUniquePtrs(const std::vector<int>& ints) {
111  std::vector<std::unique_ptr<int>> pointers;
112  for (int i : ints) pointers.emplace_back(new int(i));
113  return pointers;
114 }
115 
116 // For testing ExplainMatchResultTo().
117 template <typename T = int>
118 class GreaterThanMatcher : public MatcherInterface<T> {
119  public:
120  explicit GreaterThanMatcher(T rhs) : rhs_(rhs) {}
121 
122  void DescribeTo(ostream* os) const override { *os << "is > " << rhs_; }
123 
124  bool MatchAndExplain(T lhs, MatchResultListener* listener) const override {
125  if (lhs > rhs_) {
126  *listener << "which is " << (lhs - rhs_) << " more than " << rhs_;
127  } else if (lhs == rhs_) {
128  *listener << "which is the same as " << rhs_;
129  } else {
130  *listener << "which is " << (rhs_ - lhs) << " less than " << rhs_;
131  }
132 
133  return lhs > rhs_;
134  }
135 
136  private:
137  const T rhs_;
138 };
139 
140 template <typename T>
141 Matcher<T> GreaterThan(T n) {
142  return MakeMatcher(new GreaterThanMatcher<T>(n));
143 }
144 
145 std::string OfType(const std::string& type_name) {
146 #if GTEST_HAS_RTTI
147  return IsReadableTypeName(type_name) ? " (of type " + type_name + ")" : "";
148 #else
149  return "";
150 #endif
151 }
152 
153 // Returns the description of the given matcher.
154 template <typename T>
155 std::string Describe(const Matcher<T>& m) {
156  return DescribeMatcher<T>(m);
157 }
158 
159 // Returns the description of the negation of the given matcher.
160 template <typename T>
161 std::string DescribeNegation(const Matcher<T>& m) {
162  return DescribeMatcher<T>(m, true);
163 }
164 
165 // Returns the reason why x matches, or doesn't match, m.
166 template <typename MatcherType, typename Value>
167 std::string Explain(const MatcherType& m, const Value& x) {
168  StringMatchResultListener listener;
169  ExplainMatchResult(m, x, &listener);
170  return listener.str();
171 }
172 
173 TEST(MonotonicMatcherTest, IsPrintable) {
174  stringstream ss;
175  ss << GreaterThan(5);
176  EXPECT_EQ("is > 5", ss.str());
177 }
178 
179 TEST(MatchResultListenerTest, StreamingWorks) {
180  StringMatchResultListener listener;
181  listener << "hi" << 5;
182  EXPECT_EQ("hi5", listener.str());
183 
184  listener.Clear();
185  EXPECT_EQ("", listener.str());
186 
187  listener << 42;
188  EXPECT_EQ("42", listener.str());
189 
190  // Streaming shouldn't crash when the underlying ostream is NULL.
191  DummyMatchResultListener dummy;
192  dummy << "hi" << 5;
193 }
194 
195 TEST(MatchResultListenerTest, CanAccessUnderlyingStream) {
196  EXPECT_TRUE(DummyMatchResultListener().stream() == nullptr);
197  EXPECT_TRUE(StreamMatchResultListener(nullptr).stream() == nullptr);
198 
199  EXPECT_EQ(&std::cout, StreamMatchResultListener(&std::cout).stream());
200 }
201 
202 TEST(MatchResultListenerTest, IsInterestedWorks) {
203  EXPECT_TRUE(StringMatchResultListener().IsInterested());
204  EXPECT_TRUE(StreamMatchResultListener(&std::cout).IsInterested());
205 
206  EXPECT_FALSE(DummyMatchResultListener().IsInterested());
207  EXPECT_FALSE(StreamMatchResultListener(nullptr).IsInterested());
208 }
209 
210 // Makes sure that the MatcherInterface<T> interface doesn't
211 // change.
212 class EvenMatcherImpl : public MatcherInterface<int> {
213  public:
214  bool MatchAndExplain(int x,
215  MatchResultListener* /* listener */) const override {
216  return x % 2 == 0;
217  }
218 
219  void DescribeTo(ostream* os) const override { *os << "is an even number"; }
220 
221  // We deliberately don't define DescribeNegationTo() and
222  // ExplainMatchResultTo() here, to make sure the definition of these
223  // two methods is optional.
224 };
225 
226 // Makes sure that the MatcherInterface API doesn't change.
227 TEST(MatcherInterfaceTest, CanBeImplementedUsingPublishedAPI) {
228  EvenMatcherImpl m;
229 }
230 
231 // Tests implementing a monomorphic matcher using MatchAndExplain().
232 
233 class NewEvenMatcherImpl : public MatcherInterface<int> {
234  public:
235  bool MatchAndExplain(int x, MatchResultListener* listener) const override {
236  const bool match = x % 2 == 0;
237  // Verifies that we can stream to a listener directly.
238  *listener << "value % " << 2;
239  if (listener->stream() != nullptr) {
240  // Verifies that we can stream to a listener's underlying stream
241  // too.
242  *listener->stream() << " == " << (x % 2);
243  }
244  return match;
245  }
246 
247  void DescribeTo(ostream* os) const override { *os << "is an even number"; }
248 };
249 
250 TEST(MatcherInterfaceTest, CanBeImplementedUsingNewAPI) {
251  Matcher<int> m = MakeMatcher(new NewEvenMatcherImpl);
252  EXPECT_TRUE(m.Matches(2));
253  EXPECT_FALSE(m.Matches(3));
254  EXPECT_EQ("value % 2 == 0", Explain(m, 2));
255  EXPECT_EQ("value % 2 == 1", Explain(m, 3));
256 }
257 
258 // Tests default-constructing a matcher.
259 TEST(MatcherTest, CanBeDefaultConstructed) {
260  Matcher<double> m;
261 }
262 
263 // Tests that Matcher<T> can be constructed from a MatcherInterface<T>*.
264 TEST(MatcherTest, CanBeConstructedFromMatcherInterface) {
265  const MatcherInterface<int>* impl = new EvenMatcherImpl;
266  Matcher<int> m(impl);
267  EXPECT_TRUE(m.Matches(4));
268  EXPECT_FALSE(m.Matches(5));
269 }
270 
271 // Tests that value can be used in place of Eq(value).
272 TEST(MatcherTest, CanBeImplicitlyConstructedFromValue) {
273  Matcher<int> m1 = 5;
274  EXPECT_TRUE(m1.Matches(5));
275  EXPECT_FALSE(m1.Matches(6));
276 }
277 
278 // Tests that NULL can be used in place of Eq(NULL).
279 TEST(MatcherTest, CanBeImplicitlyConstructedFromNULL) {
280  Matcher<int*> m1 = nullptr;
281  EXPECT_TRUE(m1.Matches(nullptr));
282  int n = 0;
283  EXPECT_FALSE(m1.Matches(&n));
284 }
285 
286 // Tests that matchers can be constructed from a variable that is not properly
287 // defined. This should be illegal, but many users rely on this accidentally.
288 struct Undefined {
289  virtual ~Undefined() = 0;
290  static const int kInt = 1;
291 };
292 
293 TEST(MatcherTest, CanBeConstructedFromUndefinedVariable) {
294  Matcher<int> m1 = Undefined::kInt;
295  EXPECT_TRUE(m1.Matches(1));
296  EXPECT_FALSE(m1.Matches(2));
297 }
298 
299 // Test that a matcher parameterized with an abstract class compiles.
300 TEST(MatcherTest, CanAcceptAbstractClass) { Matcher<const Undefined&> m = _; }
301 
302 // Tests that matchers are copyable.
303 TEST(MatcherTest, IsCopyable) {
304  // Tests the copy constructor.
305  Matcher<bool> m1 = Eq(false);
306  EXPECT_TRUE(m1.Matches(false));
307  EXPECT_FALSE(m1.Matches(true));
308 
309  // Tests the assignment operator.
310  m1 = Eq(true);
311  EXPECT_TRUE(m1.Matches(true));
312  EXPECT_FALSE(m1.Matches(false));
313 }
314 
315 // Tests that Matcher<T>::DescribeTo() calls
316 // MatcherInterface<T>::DescribeTo().
317 TEST(MatcherTest, CanDescribeItself) {
318  EXPECT_EQ("is an even number",
319  Describe(Matcher<int>(new EvenMatcherImpl)));
320 }
321 
322 // Tests Matcher<T>::MatchAndExplain().
323 TEST(MatcherTest, MatchAndExplain) {
324  Matcher<int> m = GreaterThan(0);
325  StringMatchResultListener listener1;
326  EXPECT_TRUE(m.MatchAndExplain(42, &listener1));
327  EXPECT_EQ("which is 42 more than 0", listener1.str());
328 
329  StringMatchResultListener listener2;
330  EXPECT_FALSE(m.MatchAndExplain(-9, &listener2));
331  EXPECT_EQ("which is 9 less than 0", listener2.str());
332 }
333 
334 // Tests that a C-string literal can be implicitly converted to a
335 // Matcher<std::string> or Matcher<const std::string&>.
336 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
337  Matcher<std::string> m1 = "hi";
338  EXPECT_TRUE(m1.Matches("hi"));
339  EXPECT_FALSE(m1.Matches("hello"));
340 
341  Matcher<const std::string&> m2 = "hi";
342  EXPECT_TRUE(m2.Matches("hi"));
343  EXPECT_FALSE(m2.Matches("hello"));
344 }
345 
346 // Tests that a string object can be implicitly converted to a
347 // Matcher<std::string> or Matcher<const std::string&>.
348 TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
349  Matcher<std::string> m1 = std::string("hi");
350  EXPECT_TRUE(m1.Matches("hi"));
351  EXPECT_FALSE(m1.Matches("hello"));
352 
353  Matcher<const std::string&> m2 = std::string("hi");
354  EXPECT_TRUE(m2.Matches("hi"));
355  EXPECT_FALSE(m2.Matches("hello"));
356 }
357 
358 #if GTEST_INTERNAL_HAS_STRING_VIEW
359 // Tests that a C-string literal can be implicitly converted to a
360 // Matcher<StringView> or Matcher<const StringView&>.
361 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromCStringLiteral) {
362  Matcher<internal::StringView> m1 = "cats";
363  EXPECT_TRUE(m1.Matches("cats"));
364  EXPECT_FALSE(m1.Matches("dogs"));
365 
366  Matcher<const internal::StringView&> m2 = "cats";
367  EXPECT_TRUE(m2.Matches("cats"));
368  EXPECT_FALSE(m2.Matches("dogs"));
369 }
370 
371 // Tests that a std::string object can be implicitly converted to a
372 // Matcher<StringView> or Matcher<const StringView&>.
373 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromString) {
374  Matcher<internal::StringView> m1 = std::string("cats");
375  EXPECT_TRUE(m1.Matches("cats"));
376  EXPECT_FALSE(m1.Matches("dogs"));
377 
378  Matcher<const internal::StringView&> m2 = std::string("cats");
379  EXPECT_TRUE(m2.Matches("cats"));
380  EXPECT_FALSE(m2.Matches("dogs"));
381 }
382 
383 // Tests that a StringView object can be implicitly converted to a
384 // Matcher<StringView> or Matcher<const StringView&>.
385 TEST(StringViewMatcherTest, CanBeImplicitlyConstructedFromStringView) {
386  Matcher<internal::StringView> m1 = internal::StringView("cats");
387  EXPECT_TRUE(m1.Matches("cats"));
388  EXPECT_FALSE(m1.Matches("dogs"));
389 
390  Matcher<const internal::StringView&> m2 = internal::StringView("cats");
391  EXPECT_TRUE(m2.Matches("cats"));
392  EXPECT_FALSE(m2.Matches("dogs"));
393 }
394 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
395 
396 // Tests that a std::reference_wrapper<std::string> object can be implicitly
397 // converted to a Matcher<std::string> or Matcher<const std::string&> via Eq().
398 TEST(StringMatcherTest,
399  CanBeImplicitlyConstructedFromEqReferenceWrapperString) {
400  std::string value = "cats";
401  Matcher<std::string> m1 = Eq(std::ref(value));
402  EXPECT_TRUE(m1.Matches("cats"));
403  EXPECT_FALSE(m1.Matches("dogs"));
404 
405  Matcher<const std::string&> m2 = Eq(std::ref(value));
406  EXPECT_TRUE(m2.Matches("cats"));
407  EXPECT_FALSE(m2.Matches("dogs"));
408 }
409 
410 // Tests that MakeMatcher() constructs a Matcher<T> from a
411 // MatcherInterface* without requiring the user to explicitly
412 // write the type.
413 TEST(MakeMatcherTest, ConstructsMatcherFromMatcherInterface) {
414  const MatcherInterface<int>* dummy_impl = new EvenMatcherImpl;
415  Matcher<int> m = MakeMatcher(dummy_impl);
416 }
417 
418 // Tests that MakePolymorphicMatcher() can construct a polymorphic
419 // matcher from its implementation using the old API.
420 const int g_bar = 1;
421 class ReferencesBarOrIsZeroImpl {
422  public:
423  template <typename T>
424  bool MatchAndExplain(const T& x,
425  MatchResultListener* /* listener */) const {
426  const void* p = &x;
427  return p == &g_bar || x == 0;
428  }
429 
430  void DescribeTo(ostream* os) const { *os << "g_bar or zero"; }
431 
432  void DescribeNegationTo(ostream* os) const {
433  *os << "doesn't reference g_bar and is not zero";
434  }
435 };
436 
437 // This function verifies that MakePolymorphicMatcher() returns a
438 // PolymorphicMatcher<T> where T is the argument's type.
439 PolymorphicMatcher<ReferencesBarOrIsZeroImpl> ReferencesBarOrIsZero() {
440  return MakePolymorphicMatcher(ReferencesBarOrIsZeroImpl());
441 }
442 
443 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingOldAPI) {
444  // Using a polymorphic matcher to match a reference type.
445  Matcher<const int&> m1 = ReferencesBarOrIsZero();
446  EXPECT_TRUE(m1.Matches(0));
447  // Verifies that the identity of a by-reference argument is preserved.
448  EXPECT_TRUE(m1.Matches(g_bar));
449  EXPECT_FALSE(m1.Matches(1));
450  EXPECT_EQ("g_bar or zero", Describe(m1));
451 
452  // Using a polymorphic matcher to match a value type.
453  Matcher<double> m2 = ReferencesBarOrIsZero();
454  EXPECT_TRUE(m2.Matches(0.0));
455  EXPECT_FALSE(m2.Matches(0.1));
456  EXPECT_EQ("g_bar or zero", Describe(m2));
457 }
458 
459 // Tests implementing a polymorphic matcher using MatchAndExplain().
460 
461 class PolymorphicIsEvenImpl {
462  public:
463  void DescribeTo(ostream* os) const { *os << "is even"; }
464 
465  void DescribeNegationTo(ostream* os) const {
466  *os << "is odd";
467  }
468 
469  template <typename T>
470  bool MatchAndExplain(const T& x, MatchResultListener* listener) const {
471  // Verifies that we can stream to the listener directly.
472  *listener << "% " << 2;
473  if (listener->stream() != nullptr) {
474  // Verifies that we can stream to the listener's underlying stream
475  // too.
476  *listener->stream() << " == " << (x % 2);
477  }
478  return (x % 2) == 0;
479  }
480 };
481 
482 PolymorphicMatcher<PolymorphicIsEvenImpl> PolymorphicIsEven() {
483  return MakePolymorphicMatcher(PolymorphicIsEvenImpl());
484 }
485 
486 TEST(MakePolymorphicMatcherTest, ConstructsMatcherUsingNewAPI) {
487  // Using PolymorphicIsEven() as a Matcher<int>.
488  const Matcher<int> m1 = PolymorphicIsEven();
489  EXPECT_TRUE(m1.Matches(42));
490  EXPECT_FALSE(m1.Matches(43));
491  EXPECT_EQ("is even", Describe(m1));
492 
493  const Matcher<int> not_m1 = Not(m1);
494  EXPECT_EQ("is odd", Describe(not_m1));
495 
496  EXPECT_EQ("% 2 == 0", Explain(m1, 42));
497 
498  // Using PolymorphicIsEven() as a Matcher<char>.
499  const Matcher<char> m2 = PolymorphicIsEven();
500  EXPECT_TRUE(m2.Matches('\x42'));
501  EXPECT_FALSE(m2.Matches('\x43'));
502  EXPECT_EQ("is even", Describe(m2));
503 
504  const Matcher<char> not_m2 = Not(m2);
505  EXPECT_EQ("is odd", Describe(not_m2));
506 
507  EXPECT_EQ("% 2 == 0", Explain(m2, '\x42'));
508 }
509 
510 // Tests that MatcherCast<T>(m) works when m is a polymorphic matcher.
511 TEST(MatcherCastTest, FromPolymorphicMatcher) {
512  Matcher<int> m = MatcherCast<int>(Eq(5));
513  EXPECT_TRUE(m.Matches(5));
514  EXPECT_FALSE(m.Matches(6));
515 }
516 
517 // For testing casting matchers between compatible types.
518 class IntValue {
519  public:
520  // An int can be statically (although not implicitly) cast to a
521  // IntValue.
522  explicit IntValue(int a_value) : value_(a_value) {}
523 
524  int value() const { return value_; }
525  private:
526  int value_;
527 };
528 
529 // For testing casting matchers between compatible types.
530 bool IsPositiveIntValue(const IntValue& foo) {
531  return foo.value() > 0;
532 }
533 
534 // Tests that MatcherCast<T>(m) works when m is a Matcher<U> where T
535 // can be statically converted to U.
536 TEST(MatcherCastTest, FromCompatibleType) {
537  Matcher<double> m1 = Eq(2.0);
538  Matcher<int> m2 = MatcherCast<int>(m1);
539  EXPECT_TRUE(m2.Matches(2));
540  EXPECT_FALSE(m2.Matches(3));
541 
542  Matcher<IntValue> m3 = Truly(IsPositiveIntValue);
543  Matcher<int> m4 = MatcherCast<int>(m3);
544  // In the following, the arguments 1 and 0 are statically converted
545  // to IntValue objects, and then tested by the IsPositiveIntValue()
546  // predicate.
547  EXPECT_TRUE(m4.Matches(1));
548  EXPECT_FALSE(m4.Matches(0));
549 }
550 
551 // Tests that MatcherCast<T>(m) works when m is a Matcher<const T&>.
552 TEST(MatcherCastTest, FromConstReferenceToNonReference) {
553  Matcher<const int&> m1 = Eq(0);
554  Matcher<int> m2 = MatcherCast<int>(m1);
555  EXPECT_TRUE(m2.Matches(0));
556  EXPECT_FALSE(m2.Matches(1));
557 }
558 
559 // Tests that MatcherCast<T>(m) works when m is a Matcher<T&>.
560 TEST(MatcherCastTest, FromReferenceToNonReference) {
561  Matcher<int&> m1 = Eq(0);
562  Matcher<int> m2 = MatcherCast<int>(m1);
563  EXPECT_TRUE(m2.Matches(0));
564  EXPECT_FALSE(m2.Matches(1));
565 }
566 
567 // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
568 TEST(MatcherCastTest, FromNonReferenceToConstReference) {
569  Matcher<int> m1 = Eq(0);
570  Matcher<const int&> m2 = MatcherCast<const int&>(m1);
571  EXPECT_TRUE(m2.Matches(0));
572  EXPECT_FALSE(m2.Matches(1));
573 }
574 
575 // Tests that MatcherCast<T&>(m) works when m is a Matcher<T>.
576 TEST(MatcherCastTest, FromNonReferenceToReference) {
577  Matcher<int> m1 = Eq(0);
578  Matcher<int&> m2 = MatcherCast<int&>(m1);
579  int n = 0;
580  EXPECT_TRUE(m2.Matches(n));
581  n = 1;
582  EXPECT_FALSE(m2.Matches(n));
583 }
584 
585 // Tests that MatcherCast<T>(m) works when m is a Matcher<T>.
586 TEST(MatcherCastTest, FromSameType) {
587  Matcher<int> m1 = Eq(0);
588  Matcher<int> m2 = MatcherCast<int>(m1);
589  EXPECT_TRUE(m2.Matches(0));
590  EXPECT_FALSE(m2.Matches(1));
591 }
592 
593 // Tests that MatcherCast<T>(m) works when m is a value of the same type as the
594 // value type of the Matcher.
595 TEST(MatcherCastTest, FromAValue) {
596  Matcher<int> m = MatcherCast<int>(42);
597  EXPECT_TRUE(m.Matches(42));
598  EXPECT_FALSE(m.Matches(239));
599 }
600 
601 // Tests that MatcherCast<T>(m) works when m is a value of the type implicitly
602 // convertible to the value type of the Matcher.
603 TEST(MatcherCastTest, FromAnImplicitlyConvertibleValue) {
604  const int kExpected = 'c';
605  Matcher<int> m = MatcherCast<int>('c');
606  EXPECT_TRUE(m.Matches(kExpected));
607  EXPECT_FALSE(m.Matches(kExpected + 1));
608 }
609 
610 struct NonImplicitlyConstructibleTypeWithOperatorEq {
611  friend bool operator==(
612  const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */,
613  int rhs) {
614  return 42 == rhs;
615  }
616  friend bool operator==(
617  int lhs,
618  const NonImplicitlyConstructibleTypeWithOperatorEq& /* ignored */) {
619  return lhs == 42;
620  }
621 };
622 
623 // Tests that MatcherCast<T>(m) works when m is a neither a matcher nor
624 // implicitly convertible to the value type of the Matcher, but the value type
625 // of the matcher has operator==() overload accepting m.
626 TEST(MatcherCastTest, NonImplicitlyConstructibleTypeWithOperatorEq) {
627  Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m1 =
628  MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(42);
629  EXPECT_TRUE(m1.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
630 
631  Matcher<NonImplicitlyConstructibleTypeWithOperatorEq> m2 =
632  MatcherCast<NonImplicitlyConstructibleTypeWithOperatorEq>(239);
633  EXPECT_FALSE(m2.Matches(NonImplicitlyConstructibleTypeWithOperatorEq()));
634 
635  // When updating the following lines please also change the comment to
636  // namespace convertible_from_any.
637  Matcher<int> m3 =
638  MatcherCast<int>(NonImplicitlyConstructibleTypeWithOperatorEq());
639  EXPECT_TRUE(m3.Matches(42));
640  EXPECT_FALSE(m3.Matches(239));
641 }
642 
643 // ConvertibleFromAny does not work with MSVC. resulting in
644 // error C2440: 'initializing': cannot convert from 'Eq' to 'M'
645 // No constructor could take the source type, or constructor overload
646 // resolution was ambiguous
647 
648 #if !defined _MSC_VER
649 
650 // The below ConvertibleFromAny struct is implicitly constructible from anything
651 // and when in the same namespace can interact with other tests. In particular,
652 // if it is in the same namespace as other tests and one removes
653 // NonImplicitlyConstructibleTypeWithOperatorEq::operator==(int lhs, ...);
654 // then the corresponding test still compiles (and it should not!) by implicitly
655 // converting NonImplicitlyConstructibleTypeWithOperatorEq to ConvertibleFromAny
656 // in m3.Matcher().
657 namespace convertible_from_any {
658 // Implicitly convertible from any type.
659 struct ConvertibleFromAny {
660  ConvertibleFromAny(int a_value) : value(a_value) {}
661  template <typename T>
662  ConvertibleFromAny(const T& /*a_value*/) : value(-1) {
663  ADD_FAILURE() << "Conversion constructor called";
664  }
665  int value;
666 };
667 
668 bool operator==(const ConvertibleFromAny& a, const ConvertibleFromAny& b) {
669  return a.value == b.value;
670 }
671 
672 ostream& operator<<(ostream& os, const ConvertibleFromAny& a) {
673  return os << a.value;
674 }
675 
676 TEST(MatcherCastTest, ConversionConstructorIsUsed) {
677  Matcher<ConvertibleFromAny> m = MatcherCast<ConvertibleFromAny>(1);
678  EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
679  EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
680 }
681 
682 TEST(MatcherCastTest, FromConvertibleFromAny) {
683  Matcher<ConvertibleFromAny> m =
684  MatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
685  EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
686  EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
687 }
688 } // namespace convertible_from_any
689 
690 #endif // !defined _MSC_VER
691 
692 struct IntReferenceWrapper {
693  IntReferenceWrapper(const int& a_value) : value(&a_value) {}
694  const int* value;
695 };
696 
697 bool operator==(const IntReferenceWrapper& a, const IntReferenceWrapper& b) {
698  return a.value == b.value;
699 }
700 
701 TEST(MatcherCastTest, ValueIsNotCopied) {
702  int n = 42;
703  Matcher<IntReferenceWrapper> m = MatcherCast<IntReferenceWrapper>(n);
704  // Verify that the matcher holds a reference to n, not to its temporary copy.
705  EXPECT_TRUE(m.Matches(n));
706 }
707 
708 class Base {
709  public:
710  virtual ~Base() {}
711  Base() {}
712  private:
714 };
715 
716 class Derived : public Base {
717  public:
718  Derived() : Base() {}
719  int i;
720 };
721 
722 class OtherDerived : public Base {};
723 
724 // Tests that SafeMatcherCast<T>(m) works when m is a polymorphic matcher.
725 TEST(SafeMatcherCastTest, FromPolymorphicMatcher) {
726  Matcher<char> m2 = SafeMatcherCast<char>(Eq(32));
727  EXPECT_TRUE(m2.Matches(' '));
728  EXPECT_FALSE(m2.Matches('\n'));
729 }
730 
731 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where
732 // T and U are arithmetic types and T can be losslessly converted to
733 // U.
734 TEST(SafeMatcherCastTest, FromLosslesslyConvertibleArithmeticType) {
735  Matcher<double> m1 = DoubleEq(1.0);
736  Matcher<float> m2 = SafeMatcherCast<float>(m1);
737  EXPECT_TRUE(m2.Matches(1.0f));
738  EXPECT_FALSE(m2.Matches(2.0f));
739 
740  Matcher<char> m3 = SafeMatcherCast<char>(TypedEq<int>('a'));
741  EXPECT_TRUE(m3.Matches('a'));
742  EXPECT_FALSE(m3.Matches('b'));
743 }
744 
745 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<U> where T and U
746 // are pointers or references to a derived and a base class, correspondingly.
747 TEST(SafeMatcherCastTest, FromBaseClass) {
748  Derived d, d2;
749  Matcher<Base*> m1 = Eq(&d);
750  Matcher<Derived*> m2 = SafeMatcherCast<Derived*>(m1);
751  EXPECT_TRUE(m2.Matches(&d));
752  EXPECT_FALSE(m2.Matches(&d2));
753 
754  Matcher<Base&> m3 = Ref(d);
755  Matcher<Derived&> m4 = SafeMatcherCast<Derived&>(m3);
756  EXPECT_TRUE(m4.Matches(d));
757  EXPECT_FALSE(m4.Matches(d2));
758 }
759 
760 // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<const T&>.
761 TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
762  int n = 0;
763  Matcher<const int&> m1 = Ref(n);
764  Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
765  int n1 = 0;
766  EXPECT_TRUE(m2.Matches(n));
767  EXPECT_FALSE(m2.Matches(n1));
768 }
769 
770 // Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
771 TEST(SafeMatcherCastTest, FromNonReferenceToConstReference) {
772  Matcher<std::unique_ptr<int>> m1 = IsNull();
773  Matcher<const std::unique_ptr<int>&> m2 =
774  SafeMatcherCast<const std::unique_ptr<int>&>(m1);
775  EXPECT_TRUE(m2.Matches(std::unique_ptr<int>()));
776  EXPECT_FALSE(m2.Matches(std::unique_ptr<int>(new int)));
777 }
778 
779 // Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
780 TEST(SafeMatcherCastTest, FromNonReferenceToReference) {
781  Matcher<int> m1 = Eq(0);
782  Matcher<int&> m2 = SafeMatcherCast<int&>(m1);
783  int n = 0;
784  EXPECT_TRUE(m2.Matches(n));
785  n = 1;
786  EXPECT_FALSE(m2.Matches(n));
787 }
788 
789 // Tests that SafeMatcherCast<T>(m) works when m is a Matcher<T>.
790 TEST(SafeMatcherCastTest, FromSameType) {
791  Matcher<int> m1 = Eq(0);
792  Matcher<int> m2 = SafeMatcherCast<int>(m1);
793  EXPECT_TRUE(m2.Matches(0));
794  EXPECT_FALSE(m2.Matches(1));
795 }
796 
797 #if !defined _MSC_VER
798 
799 namespace convertible_from_any {
800 TEST(SafeMatcherCastTest, ConversionConstructorIsUsed) {
801  Matcher<ConvertibleFromAny> m = SafeMatcherCast<ConvertibleFromAny>(1);
802  EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
803  EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
804 }
805 
806 TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
807  Matcher<ConvertibleFromAny> m =
808  SafeMatcherCast<ConvertibleFromAny>(Eq(ConvertibleFromAny(1)));
809  EXPECT_TRUE(m.Matches(ConvertibleFromAny(1)));
810  EXPECT_FALSE(m.Matches(ConvertibleFromAny(2)));
811 }
812 } // namespace convertible_from_any
813 
814 #endif // !defined _MSC_VER
815 
816 TEST(SafeMatcherCastTest, ValueIsNotCopied) {
817  int n = 42;
818  Matcher<IntReferenceWrapper> m = SafeMatcherCast<IntReferenceWrapper>(n);
819  // Verify that the matcher holds a reference to n, not to its temporary copy.
820  EXPECT_TRUE(m.Matches(n));
821 }
822 
823 TEST(ExpectThat, TakesLiterals) {
824  EXPECT_THAT(1, 1);
825  EXPECT_THAT(1.0, 1.0);
826  EXPECT_THAT(std::string(), "");
827 }
828 
829 TEST(ExpectThat, TakesFunctions) {
830  struct Helper {
831  static void Func() {}
832  };
833  void (*func)() = Helper::Func;
836 }
837 
838 // Tests that A<T>() matches any value of type T.
839 TEST(ATest, MatchesAnyValue) {
840  // Tests a matcher for a value type.
841  Matcher<double> m1 = A<double>();
842  EXPECT_TRUE(m1.Matches(91.43));
843  EXPECT_TRUE(m1.Matches(-15.32));
844 
845  // Tests a matcher for a reference type.
846  int a = 2;
847  int b = -6;
848  Matcher<int&> m2 = A<int&>();
849  EXPECT_TRUE(m2.Matches(a));
850  EXPECT_TRUE(m2.Matches(b));
851 }
852 
853 TEST(ATest, WorksForDerivedClass) {
854  Base base;
855  Derived derived;
857  // This shouldn't compile: EXPECT_THAT(&base, A<Derived*>());
858  EXPECT_THAT(&derived, A<Base*>());
859  EXPECT_THAT(&derived, A<Derived*>());
860 }
861 
862 // Tests that A<T>() describes itself properly.
863 TEST(ATest, CanDescribeSelf) {
864  EXPECT_EQ("is anything", Describe(A<bool>()));
865 }
866 
867 // Tests that An<T>() matches any value of type T.
868 TEST(AnTest, MatchesAnyValue) {
869  // Tests a matcher for a value type.
870  Matcher<int> m1 = An<int>();
871  EXPECT_TRUE(m1.Matches(9143));
872  EXPECT_TRUE(m1.Matches(-1532));
873 
874  // Tests a matcher for a reference type.
875  int a = 2;
876  int b = -6;
877  Matcher<int&> m2 = An<int&>();
878  EXPECT_TRUE(m2.Matches(a));
879  EXPECT_TRUE(m2.Matches(b));
880 }
881 
882 // Tests that An<T>() describes itself properly.
883 TEST(AnTest, CanDescribeSelf) {
884  EXPECT_EQ("is anything", Describe(An<int>()));
885 }
886 
887 // Tests that _ can be used as a matcher for any type and matches any
888 // value of that type.
889 TEST(UnderscoreTest, MatchesAnyValue) {
890  // Uses _ as a matcher for a value type.
891  Matcher<int> m1 = _;
892  EXPECT_TRUE(m1.Matches(123));
893  EXPECT_TRUE(m1.Matches(-242));
894 
895  // Uses _ as a matcher for a reference type.
896  bool a = false;
897  const bool b = true;
898  Matcher<const bool&> m2 = _;
899  EXPECT_TRUE(m2.Matches(a));
900  EXPECT_TRUE(m2.Matches(b));
901 }
902 
903 // Tests that _ describes itself properly.
904 TEST(UnderscoreTest, CanDescribeSelf) {
905  Matcher<int> m = _;
906  EXPECT_EQ("is anything", Describe(m));
907 }
908 
909 // Tests that Eq(x) matches any value equal to x.
910 TEST(EqTest, MatchesEqualValue) {
911  // 2 C-strings with same content but different addresses.
912  const char a1[] = "hi";
913  const char a2[] = "hi";
914 
915  Matcher<const char*> m1 = Eq(a1);
916  EXPECT_TRUE(m1.Matches(a1));
917  EXPECT_FALSE(m1.Matches(a2));
918 }
919 
920 // Tests that Eq(v) describes itself properly.
921 
922 class Unprintable {
923  public:
924  Unprintable() : c_('a') {}
925 
926  bool operator==(const Unprintable& /* rhs */) const { return true; }
927  // -Wunused-private-field: dummy accessor for `c_`.
928  char dummy_c() { return c_; }
929  private:
930  char c_;
931 };
932 
933 TEST(EqTest, CanDescribeSelf) {
934  Matcher<Unprintable> m = Eq(Unprintable());
935  EXPECT_EQ("is equal to 1-byte object <61>", Describe(m));
936 }
937 
938 // Tests that Eq(v) can be used to match any type that supports
939 // comparing with type T, where T is v's type.
940 TEST(EqTest, IsPolymorphic) {
941  Matcher<int> m1 = Eq(1);
942  EXPECT_TRUE(m1.Matches(1));
943  EXPECT_FALSE(m1.Matches(2));
944 
945  Matcher<char> m2 = Eq(1);
946  EXPECT_TRUE(m2.Matches('\1'));
947  EXPECT_FALSE(m2.Matches('a'));
948 }
949 
950 // Tests that TypedEq<T>(v) matches values of type T that's equal to v.
951 TEST(TypedEqTest, ChecksEqualityForGivenType) {
952  Matcher<char> m1 = TypedEq<char>('a');
953  EXPECT_TRUE(m1.Matches('a'));
954  EXPECT_FALSE(m1.Matches('b'));
955 
956  Matcher<int> m2 = TypedEq<int>(6);
957  EXPECT_TRUE(m2.Matches(6));
958  EXPECT_FALSE(m2.Matches(7));
959 }
960 
961 // Tests that TypedEq(v) describes itself properly.
962 TEST(TypedEqTest, CanDescribeSelf) {
963  EXPECT_EQ("is equal to 2", Describe(TypedEq<int>(2)));
964 }
965 
966 // Tests that TypedEq<T>(v) has type Matcher<T>.
967 
968 // Type<T>::IsTypeOf(v) compiles if and only if the type of value v is T, where
969 // T is a "bare" type (i.e. not in the form of const U or U&). If v's type is
970 // not T, the compiler will generate a message about "undefined reference".
971 template <typename T>
972 struct Type {
973  static bool IsTypeOf(const T& /* v */) { return true; }
974 
975  template <typename T2>
976  static void IsTypeOf(T2 v);
977 };
978 
979 TEST(TypedEqTest, HasSpecifiedType) {
980  // Verfies that the type of TypedEq<T>(v) is Matcher<T>.
981  Type<Matcher<int> >::IsTypeOf(TypedEq<int>(5));
982  Type<Matcher<double> >::IsTypeOf(TypedEq<double>(5));
983 }
984 
985 // Tests that Ge(v) matches anything >= v.
986 TEST(GeTest, ImplementsGreaterThanOrEqual) {
987  Matcher<int> m1 = Ge(0);
988  EXPECT_TRUE(m1.Matches(1));
989  EXPECT_TRUE(m1.Matches(0));
990  EXPECT_FALSE(m1.Matches(-1));
991 }
992 
993 // Tests that Ge(v) describes itself properly.
994 TEST(GeTest, CanDescribeSelf) {
995  Matcher<int> m = Ge(5);
996  EXPECT_EQ("is >= 5", Describe(m));
997 }
998 
999 // Tests that Gt(v) matches anything > v.
1000 TEST(GtTest, ImplementsGreaterThan) {
1001  Matcher<double> m1 = Gt(0);
1002  EXPECT_TRUE(m1.Matches(1.0));
1003  EXPECT_FALSE(m1.Matches(0.0));
1004  EXPECT_FALSE(m1.Matches(-1.0));
1005 }
1006 
1007 // Tests that Gt(v) describes itself properly.
1008 TEST(GtTest, CanDescribeSelf) {
1009  Matcher<int> m = Gt(5);
1010  EXPECT_EQ("is > 5", Describe(m));
1011 }
1012 
1013 // Tests that Le(v) matches anything <= v.
1014 TEST(LeTest, ImplementsLessThanOrEqual) {
1015  Matcher<char> m1 = Le('b');
1016  EXPECT_TRUE(m1.Matches('a'));
1017  EXPECT_TRUE(m1.Matches('b'));
1018  EXPECT_FALSE(m1.Matches('c'));
1019 }
1020 
1021 // Tests that Le(v) describes itself properly.
1022 TEST(LeTest, CanDescribeSelf) {
1023  Matcher<int> m = Le(5);
1024  EXPECT_EQ("is <= 5", Describe(m));
1025 }
1026 
1027 // Tests that Lt(v) matches anything < v.
1028 TEST(LtTest, ImplementsLessThan) {
1029  Matcher<const std::string&> m1 = Lt("Hello");
1030  EXPECT_TRUE(m1.Matches("Abc"));
1031  EXPECT_FALSE(m1.Matches("Hello"));
1032  EXPECT_FALSE(m1.Matches("Hello, world!"));
1033 }
1034 
1035 // Tests that Lt(v) describes itself properly.
1036 TEST(LtTest, CanDescribeSelf) {
1037  Matcher<int> m = Lt(5);
1038  EXPECT_EQ("is < 5", Describe(m));
1039 }
1040 
1041 // Tests that Ne(v) matches anything != v.
1042 TEST(NeTest, ImplementsNotEqual) {
1043  Matcher<int> m1 = Ne(0);
1044  EXPECT_TRUE(m1.Matches(1));
1045  EXPECT_TRUE(m1.Matches(-1));
1046  EXPECT_FALSE(m1.Matches(0));
1047 }
1048 
1049 // Tests that Ne(v) describes itself properly.
1050 TEST(NeTest, CanDescribeSelf) {
1051  Matcher<int> m = Ne(5);
1052  EXPECT_EQ("isn't equal to 5", Describe(m));
1053 }
1054 
1055 class MoveOnly {
1056  public:
1057  explicit MoveOnly(int i) : i_(i) {}
1058  MoveOnly(const MoveOnly&) = delete;
1059  MoveOnly(MoveOnly&&) = default;
1060  MoveOnly& operator=(const MoveOnly&) = delete;
1061  MoveOnly& operator=(MoveOnly&&) = default;
1062 
1063  bool operator==(const MoveOnly& other) const { return i_ == other.i_; }
1064  bool operator!=(const MoveOnly& other) const { return i_ != other.i_; }
1065  bool operator<(const MoveOnly& other) const { return i_ < other.i_; }
1066  bool operator<=(const MoveOnly& other) const { return i_ <= other.i_; }
1067  bool operator>(const MoveOnly& other) const { return i_ > other.i_; }
1068  bool operator>=(const MoveOnly& other) const { return i_ >= other.i_; }
1069 
1070  private:
1071  int i_;
1072 };
1073 
1074 struct MoveHelper {
1075  MOCK_METHOD1(Call, void(MoveOnly));
1076 };
1077 
1078 // Disable this test in VS 2015 (version 14), where it fails when SEH is enabled
1079 #if defined(_MSC_VER) && (_MSC_VER < 1910)
1080 TEST(ComparisonBaseTest, DISABLED_WorksWithMoveOnly) {
1081 #else
1082 TEST(ComparisonBaseTest, WorksWithMoveOnly) {
1083 #endif
1084  MoveOnly m{0};
1085  MoveHelper helper;
1086 
1087  EXPECT_CALL(helper, Call(Eq(ByRef(m))));
1088  helper.Call(MoveOnly(0));
1089  EXPECT_CALL(helper, Call(Ne(ByRef(m))));
1090  helper.Call(MoveOnly(1));
1091  EXPECT_CALL(helper, Call(Le(ByRef(m))));
1092  helper.Call(MoveOnly(0));
1093  EXPECT_CALL(helper, Call(Lt(ByRef(m))));
1094  helper.Call(MoveOnly(-1));
1095  EXPECT_CALL(helper, Call(Ge(ByRef(m))));
1096  helper.Call(MoveOnly(0));
1097  EXPECT_CALL(helper, Call(Gt(ByRef(m))));
1098  helper.Call(MoveOnly(1));
1099 }
1100 
1101 // Tests that IsNull() matches any NULL pointer of any type.
1102 TEST(IsNullTest, MatchesNullPointer) {
1103  Matcher<int*> m1 = IsNull();
1104  int* p1 = nullptr;
1105  int n = 0;
1106  EXPECT_TRUE(m1.Matches(p1));
1107  EXPECT_FALSE(m1.Matches(&n));
1108 
1109  Matcher<const char*> m2 = IsNull();
1110  const char* p2 = nullptr;
1111  EXPECT_TRUE(m2.Matches(p2));
1112  EXPECT_FALSE(m2.Matches("hi"));
1113 
1114  Matcher<void*> m3 = IsNull();
1115  void* p3 = nullptr;
1116  EXPECT_TRUE(m3.Matches(p3));
1117  EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef)));
1118 }
1119 
1120 TEST(IsNullTest, StdFunction) {
1121  const Matcher<std::function<void()>> m = IsNull();
1122 
1123  EXPECT_TRUE(m.Matches(std::function<void()>()));
1124  EXPECT_FALSE(m.Matches([]{}));
1125 }
1126 
1127 // Tests that IsNull() describes itself properly.
1128 TEST(IsNullTest, CanDescribeSelf) {
1129  Matcher<int*> m = IsNull();
1130  EXPECT_EQ("is NULL", Describe(m));
1131  EXPECT_EQ("isn't NULL", DescribeNegation(m));
1132 }
1133 
1134 // Tests that NotNull() matches any non-NULL pointer of any type.
1135 TEST(NotNullTest, MatchesNonNullPointer) {
1136  Matcher<int*> m1 = NotNull();
1137  int* p1 = nullptr;
1138  int n = 0;
1139  EXPECT_FALSE(m1.Matches(p1));
1140  EXPECT_TRUE(m1.Matches(&n));
1141 
1142  Matcher<const char*> m2 = NotNull();
1143  const char* p2 = nullptr;
1144  EXPECT_FALSE(m2.Matches(p2));
1145  EXPECT_TRUE(m2.Matches("hi"));
1146 }
1147 
1148 TEST(NotNullTest, LinkedPtr) {
1149  const Matcher<std::shared_ptr<int>> m = NotNull();
1150  const std::shared_ptr<int> null_p;
1151  const std::shared_ptr<int> non_null_p(new int);
1152 
1153  EXPECT_FALSE(m.Matches(null_p));
1154  EXPECT_TRUE(m.Matches(non_null_p));
1155 }
1156 
1157 TEST(NotNullTest, ReferenceToConstLinkedPtr) {
1158  const Matcher<const std::shared_ptr<double>&> m = NotNull();
1159  const std::shared_ptr<double> null_p;
1160  const std::shared_ptr<double> non_null_p(new double);
1161 
1162  EXPECT_FALSE(m.Matches(null_p));
1163  EXPECT_TRUE(m.Matches(non_null_p));
1164 }
1165 
1166 TEST(NotNullTest, StdFunction) {
1167  const Matcher<std::function<void()>> m = NotNull();
1168 
1169  EXPECT_TRUE(m.Matches([]{}));
1170  EXPECT_FALSE(m.Matches(std::function<void()>()));
1171 }
1172 
1173 // Tests that NotNull() describes itself properly.
1174 TEST(NotNullTest, CanDescribeSelf) {
1175  Matcher<int*> m = NotNull();
1176  EXPECT_EQ("isn't NULL", Describe(m));
1177 }
1178 
1179 // Tests that Ref(variable) matches an argument that references
1180 // 'variable'.
1181 TEST(RefTest, MatchesSameVariable) {
1182  int a = 0;
1183  int b = 0;
1184  Matcher<int&> m = Ref(a);
1185  EXPECT_TRUE(m.Matches(a));
1186  EXPECT_FALSE(m.Matches(b));
1187 }
1188 
1189 // Tests that Ref(variable) describes itself properly.
1190 TEST(RefTest, CanDescribeSelf) {
1191  int n = 5;
1192  Matcher<int&> m = Ref(n);
1193  stringstream ss;
1194  ss << "references the variable @" << &n << " 5";
1195  EXPECT_EQ(ss.str(), Describe(m));
1196 }
1197 
1198 // Test that Ref(non_const_varialbe) can be used as a matcher for a
1199 // const reference.
1200 TEST(RefTest, CanBeUsedAsMatcherForConstReference) {
1201  int a = 0;
1202  int b = 0;
1203  Matcher<const int&> m = Ref(a);
1204  EXPECT_TRUE(m.Matches(a));
1205  EXPECT_FALSE(m.Matches(b));
1206 }
1207 
1208 // Tests that Ref(variable) is covariant, i.e. Ref(derived) can be
1209 // used wherever Ref(base) can be used (Ref(derived) is a sub-type
1210 // of Ref(base), but not vice versa.
1211 
1212 TEST(RefTest, IsCovariant) {
1213  Base base, base2;
1214  Derived derived;
1215  Matcher<const Base&> m1 = Ref(base);
1216  EXPECT_TRUE(m1.Matches(base));
1217  EXPECT_FALSE(m1.Matches(base2));
1218  EXPECT_FALSE(m1.Matches(derived));
1219 
1220  m1 = Ref(derived);
1221  EXPECT_TRUE(m1.Matches(derived));
1222  EXPECT_FALSE(m1.Matches(base));
1223  EXPECT_FALSE(m1.Matches(base2));
1224 }
1225 
1226 TEST(RefTest, ExplainsResult) {
1227  int n = 0;
1228  EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), n),
1229  StartsWith("which is located @"));
1230 
1231  int m = 0;
1232  EXPECT_THAT(Explain(Matcher<const int&>(Ref(n)), m),
1233  StartsWith("which is located @"));
1234 }
1235 
1236 // Tests string comparison matchers.
1237 
1238 template <typename T = std::string>
1239 std::string FromStringLike(internal::StringLike<T> str) {
1240  return std::string(str);
1241 }
1242 
1243 TEST(StringLike, TestConversions) {
1244  EXPECT_EQ("foo", FromStringLike("foo"));
1245  EXPECT_EQ("foo", FromStringLike(std::string("foo")));
1246 #if GTEST_INTERNAL_HAS_STRING_VIEW
1247  EXPECT_EQ("foo", FromStringLike(internal::StringView("foo")));
1248 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1249 
1250  // Non deducible types.
1251  EXPECT_EQ("", FromStringLike({}));
1252  EXPECT_EQ("foo", FromStringLike({'f', 'o', 'o'}));
1253  const char buf[] = "foo";
1254  EXPECT_EQ("foo", FromStringLike({buf, buf + 3}));
1255 }
1256 
1257 TEST(StrEqTest, MatchesEqualString) {
1258  Matcher<const char*> m = StrEq(std::string("Hello"));
1259  EXPECT_TRUE(m.Matches("Hello"));
1260  EXPECT_FALSE(m.Matches("hello"));
1261  EXPECT_FALSE(m.Matches(nullptr));
1262 
1263  Matcher<const std::string&> m2 = StrEq("Hello");
1264  EXPECT_TRUE(m2.Matches("Hello"));
1265  EXPECT_FALSE(m2.Matches("Hi"));
1266 
1267 #if GTEST_INTERNAL_HAS_STRING_VIEW
1268  Matcher<const internal::StringView&> m3 =
1269  StrEq(internal::StringView("Hello"));
1270  EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));
1271  EXPECT_FALSE(m3.Matches(internal::StringView("hello")));
1272  EXPECT_FALSE(m3.Matches(internal::StringView()));
1273 
1274  Matcher<const internal::StringView&> m_empty = StrEq("");
1275  EXPECT_TRUE(m_empty.Matches(internal::StringView("")));
1276  EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1277  EXPECT_FALSE(m_empty.Matches(internal::StringView("hello")));
1278 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1279 }
1280 
1281 TEST(StrEqTest, CanDescribeSelf) {
1282  Matcher<std::string> m = StrEq("Hi-\'\"?\\\a\b\f\n\r\t\v\xD3");
1283  EXPECT_EQ("is equal to \"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\\xD3\"",
1284  Describe(m));
1285 
1286  std::string str("01204500800");
1287  str[3] = '\0';
1288  Matcher<std::string> m2 = StrEq(str);
1289  EXPECT_EQ("is equal to \"012\\04500800\"", Describe(m2));
1290  str[0] = str[6] = str[7] = str[9] = str[10] = '\0';
1291  Matcher<std::string> m3 = StrEq(str);
1292  EXPECT_EQ("is equal to \"\\012\\045\\0\\08\\0\\0\"", Describe(m3));
1293 }
1294 
1295 TEST(StrNeTest, MatchesUnequalString) {
1296  Matcher<const char*> m = StrNe("Hello");
1297  EXPECT_TRUE(m.Matches(""));
1298  EXPECT_TRUE(m.Matches(nullptr));
1299  EXPECT_FALSE(m.Matches("Hello"));
1300 
1301  Matcher<std::string> m2 = StrNe(std::string("Hello"));
1302  EXPECT_TRUE(m2.Matches("hello"));
1303  EXPECT_FALSE(m2.Matches("Hello"));
1304 
1305 #if GTEST_INTERNAL_HAS_STRING_VIEW
1306  Matcher<const internal::StringView> m3 = StrNe(internal::StringView("Hello"));
1307  EXPECT_TRUE(m3.Matches(internal::StringView("")));
1308  EXPECT_TRUE(m3.Matches(internal::StringView()));
1309  EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));
1310 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1311 }
1312 
1313 TEST(StrNeTest, CanDescribeSelf) {
1314  Matcher<const char*> m = StrNe("Hi");
1315  EXPECT_EQ("isn't equal to \"Hi\"", Describe(m));
1316 }
1317 
1318 TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
1319  Matcher<const char*> m = StrCaseEq(std::string("Hello"));
1320  EXPECT_TRUE(m.Matches("Hello"));
1321  EXPECT_TRUE(m.Matches("hello"));
1322  EXPECT_FALSE(m.Matches("Hi"));
1323  EXPECT_FALSE(m.Matches(nullptr));
1324 
1325  Matcher<const std::string&> m2 = StrCaseEq("Hello");
1326  EXPECT_TRUE(m2.Matches("hello"));
1327  EXPECT_FALSE(m2.Matches("Hi"));
1328 
1329 #if GTEST_INTERNAL_HAS_STRING_VIEW
1330  Matcher<const internal::StringView&> m3 =
1331  StrCaseEq(internal::StringView("Hello"));
1332  EXPECT_TRUE(m3.Matches(internal::StringView("Hello")));
1333  EXPECT_TRUE(m3.Matches(internal::StringView("hello")));
1334  EXPECT_FALSE(m3.Matches(internal::StringView("Hi")));
1335  EXPECT_FALSE(m3.Matches(internal::StringView()));
1336 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1337 }
1338 
1339 TEST(StrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
1340  std::string str1("oabocdooeoo");
1341  std::string str2("OABOCDOOEOO");
1342  Matcher<const std::string&> m0 = StrCaseEq(str1);
1343  EXPECT_FALSE(m0.Matches(str2 + std::string(1, '\0')));
1344 
1345  str1[3] = str2[3] = '\0';
1346  Matcher<const std::string&> m1 = StrCaseEq(str1);
1347  EXPECT_TRUE(m1.Matches(str2));
1348 
1349  str1[0] = str1[6] = str1[7] = str1[10] = '\0';
1350  str2[0] = str2[6] = str2[7] = str2[10] = '\0';
1351  Matcher<const std::string&> m2 = StrCaseEq(str1);
1352  str1[9] = str2[9] = '\0';
1353  EXPECT_FALSE(m2.Matches(str2));
1354 
1355  Matcher<const std::string&> m3 = StrCaseEq(str1);
1356  EXPECT_TRUE(m3.Matches(str2));
1357 
1358  EXPECT_FALSE(m3.Matches(str2 + "x"));
1359  str2.append(1, '\0');
1360  EXPECT_FALSE(m3.Matches(str2));
1361  EXPECT_FALSE(m3.Matches(std::string(str2, 0, 9)));
1362 }
1363 
1364 TEST(StrCaseEqTest, CanDescribeSelf) {
1365  Matcher<std::string> m = StrCaseEq("Hi");
1366  EXPECT_EQ("is equal to (ignoring case) \"Hi\"", Describe(m));
1367 }
1368 
1369 TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
1370  Matcher<const char*> m = StrCaseNe("Hello");
1371  EXPECT_TRUE(m.Matches("Hi"));
1372  EXPECT_TRUE(m.Matches(nullptr));
1373  EXPECT_FALSE(m.Matches("Hello"));
1374  EXPECT_FALSE(m.Matches("hello"));
1375 
1376  Matcher<std::string> m2 = StrCaseNe(std::string("Hello"));
1377  EXPECT_TRUE(m2.Matches(""));
1378  EXPECT_FALSE(m2.Matches("Hello"));
1379 
1380 #if GTEST_INTERNAL_HAS_STRING_VIEW
1381  Matcher<const internal::StringView> m3 =
1382  StrCaseNe(internal::StringView("Hello"));
1383  EXPECT_TRUE(m3.Matches(internal::StringView("Hi")));
1384  EXPECT_TRUE(m3.Matches(internal::StringView()));
1385  EXPECT_FALSE(m3.Matches(internal::StringView("Hello")));
1386  EXPECT_FALSE(m3.Matches(internal::StringView("hello")));
1387 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1388 }
1389 
1390 TEST(StrCaseNeTest, CanDescribeSelf) {
1391  Matcher<const char*> m = StrCaseNe("Hi");
1392  EXPECT_EQ("isn't equal to (ignoring case) \"Hi\"", Describe(m));
1393 }
1394 
1395 // Tests that HasSubstr() works for matching string-typed values.
1396 TEST(HasSubstrTest, WorksForStringClasses) {
1397  const Matcher<std::string> m1 = HasSubstr("foo");
1398  EXPECT_TRUE(m1.Matches(std::string("I love food.")));
1399  EXPECT_FALSE(m1.Matches(std::string("tofo")));
1400 
1401  const Matcher<const std::string&> m2 = HasSubstr("foo");
1402  EXPECT_TRUE(m2.Matches(std::string("I love food.")));
1403  EXPECT_FALSE(m2.Matches(std::string("tofo")));
1404 
1405  const Matcher<std::string> m_empty = HasSubstr("");
1406  EXPECT_TRUE(m_empty.Matches(std::string()));
1407  EXPECT_TRUE(m_empty.Matches(std::string("not empty")));
1408 }
1409 
1410 // Tests that HasSubstr() works for matching C-string-typed values.
1411 TEST(HasSubstrTest, WorksForCStrings) {
1412  const Matcher<char*> m1 = HasSubstr("foo");
1413  EXPECT_TRUE(m1.Matches(const_cast<char*>("I love food.")));
1414  EXPECT_FALSE(m1.Matches(const_cast<char*>("tofo")));
1415  EXPECT_FALSE(m1.Matches(nullptr));
1416 
1417  const Matcher<const char*> m2 = HasSubstr("foo");
1418  EXPECT_TRUE(m2.Matches("I love food."));
1419  EXPECT_FALSE(m2.Matches("tofo"));
1420  EXPECT_FALSE(m2.Matches(nullptr));
1421 
1422  const Matcher<const char*> m_empty = HasSubstr("");
1423  EXPECT_TRUE(m_empty.Matches("not empty"));
1424  EXPECT_TRUE(m_empty.Matches(""));
1425  EXPECT_FALSE(m_empty.Matches(nullptr));
1426 }
1427 
1428 #if GTEST_INTERNAL_HAS_STRING_VIEW
1429 // Tests that HasSubstr() works for matching StringView-typed values.
1430 TEST(HasSubstrTest, WorksForStringViewClasses) {
1431  const Matcher<internal::StringView> m1 =
1432  HasSubstr(internal::StringView("foo"));
1433  EXPECT_TRUE(m1.Matches(internal::StringView("I love food.")));
1434  EXPECT_FALSE(m1.Matches(internal::StringView("tofo")));
1435  EXPECT_FALSE(m1.Matches(internal::StringView()));
1436 
1437  const Matcher<const internal::StringView&> m2 = HasSubstr("foo");
1438  EXPECT_TRUE(m2.Matches(internal::StringView("I love food.")));
1439  EXPECT_FALSE(m2.Matches(internal::StringView("tofo")));
1440  EXPECT_FALSE(m2.Matches(internal::StringView()));
1441 
1442  const Matcher<const internal::StringView&> m3 = HasSubstr("");
1443  EXPECT_TRUE(m3.Matches(internal::StringView("foo")));
1444  EXPECT_TRUE(m3.Matches(internal::StringView("")));
1445  EXPECT_TRUE(m3.Matches(internal::StringView()));
1446 }
1447 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1448 
1449 // Tests that HasSubstr(s) describes itself properly.
1450 TEST(HasSubstrTest, CanDescribeSelf) {
1451  Matcher<std::string> m = HasSubstr("foo\n\"");
1452  EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m));
1453 }
1454 
1455 TEST(KeyTest, CanDescribeSelf) {
1456  Matcher<const pair<std::string, int>&> m = Key("foo");
1457  EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m));
1458  EXPECT_EQ("doesn't have a key that is equal to \"foo\"", DescribeNegation(m));
1459 }
1460 
1461 TEST(KeyTest, ExplainsResult) {
1462  Matcher<pair<int, bool> > m = Key(GreaterThan(10));
1463  EXPECT_EQ("whose first field is a value which is 5 less than 10",
1464  Explain(m, make_pair(5, true)));
1465  EXPECT_EQ("whose first field is a value which is 5 more than 10",
1466  Explain(m, make_pair(15, true)));
1467 }
1468 
1469 TEST(KeyTest, MatchesCorrectly) {
1470  pair<int, std::string> p(25, "foo");
1471  EXPECT_THAT(p, Key(25));
1472  EXPECT_THAT(p, Not(Key(42)));
1473  EXPECT_THAT(p, Key(Ge(20)));
1474  EXPECT_THAT(p, Not(Key(Lt(25))));
1475 }
1476 
1477 TEST(KeyTest, WorksWithMoveOnly) {
1478  pair<std::unique_ptr<int>, std::unique_ptr<int>> p;
1479  EXPECT_THAT(p, Key(Eq(nullptr)));
1480 }
1481 
1482 template <size_t I>
1483 struct Tag {};
1484 
1485 struct PairWithGet {
1488  using first_type = int;
1489  using second_type = std::string;
1490 
1491  const int& GetImpl(Tag<0>) const { return member_1; }
1492  const std::string& GetImpl(Tag<1>) const { return member_2; }
1493 };
1494 template <size_t I>
1495 auto get(const PairWithGet& value) -> decltype(value.GetImpl(Tag<I>())) {
1496  return value.GetImpl(Tag<I>());
1497 }
1498 TEST(PairTest, MatchesPairWithGetCorrectly) {
1499  PairWithGet p{25, "foo"};
1500  EXPECT_THAT(p, Key(25));
1501  EXPECT_THAT(p, Not(Key(42)));
1502  EXPECT_THAT(p, Key(Ge(20)));
1503  EXPECT_THAT(p, Not(Key(Lt(25))));
1504 
1505  std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};
1506  EXPECT_THAT(v, Contains(Key(29)));
1507 }
1508 
1509 TEST(KeyTest, SafelyCastsInnerMatcher) {
1510  Matcher<int> is_positive = Gt(0);
1511  Matcher<int> is_negative = Lt(0);
1512  pair<char, bool> p('a', true);
1513  EXPECT_THAT(p, Key(is_positive));
1514  EXPECT_THAT(p, Not(Key(is_negative)));
1515 }
1516 
1517 TEST(KeyTest, InsideContainsUsingMap) {
1518  map<int, char> container;
1519  container.insert(make_pair(1, 'a'));
1520  container.insert(make_pair(2, 'b'));
1521  container.insert(make_pair(4, 'c'));
1524 }
1525 
1526 TEST(KeyTest, InsideContainsUsingMultimap) {
1527  multimap<int, char> container;
1528  container.insert(make_pair(1, 'a'));
1529  container.insert(make_pair(2, 'b'));
1530  container.insert(make_pair(4, 'c'));
1531 
1533  container.insert(make_pair(25, 'd'));
1535  container.insert(make_pair(25, 'e'));
1537 
1540 }
1541 
1542 TEST(PairTest, Typing) {
1543  // Test verifies the following type conversions can be compiled.
1544  Matcher<const pair<const char*, int>&> m1 = Pair("foo", 42);
1545  Matcher<const pair<const char*, int> > m2 = Pair("foo", 42);
1546  Matcher<pair<const char*, int> > m3 = Pair("foo", 42);
1547 
1548  Matcher<pair<int, const std::string> > m4 = Pair(25, "42");
1549  Matcher<pair<const std::string, int> > m5 = Pair("25", 42);
1550 }
1551 
1552 TEST(PairTest, CanDescribeSelf) {
1553  Matcher<const pair<std::string, int>&> m1 = Pair("foo", 42);
1554  EXPECT_EQ("has a first field that is equal to \"foo\""
1555  ", and has a second field that is equal to 42",
1556  Describe(m1));
1557  EXPECT_EQ("has a first field that isn't equal to \"foo\""
1558  ", or has a second field that isn't equal to 42",
1559  DescribeNegation(m1));
1560  // Double and triple negation (1 or 2 times not and description of negation).
1561  Matcher<const pair<int, int>&> m2 = Not(Pair(Not(13), 42));
1562  EXPECT_EQ("has a first field that isn't equal to 13"
1563  ", and has a second field that is equal to 42",
1564  DescribeNegation(m2));
1565 }
1566 
1567 TEST(PairTest, CanExplainMatchResultTo) {
1568  // If neither field matches, Pair() should explain about the first
1569  // field.
1570  const Matcher<pair<int, int> > m = Pair(GreaterThan(0), GreaterThan(0));
1571  EXPECT_EQ("whose first field does not match, which is 1 less than 0",
1572  Explain(m, make_pair(-1, -2)));
1573 
1574  // If the first field matches but the second doesn't, Pair() should
1575  // explain about the second field.
1576  EXPECT_EQ("whose second field does not match, which is 2 less than 0",
1577  Explain(m, make_pair(1, -2)));
1578 
1579  // If the first field doesn't match but the second does, Pair()
1580  // should explain about the first field.
1581  EXPECT_EQ("whose first field does not match, which is 1 less than 0",
1582  Explain(m, make_pair(-1, 2)));
1583 
1584  // If both fields match, Pair() should explain about them both.
1585  EXPECT_EQ("whose both fields match, where the first field is a value "
1586  "which is 1 more than 0, and the second field is a value "
1587  "which is 2 more than 0",
1588  Explain(m, make_pair(1, 2)));
1589 
1590  // If only the first match has an explanation, only this explanation should
1591  // be printed.
1592  const Matcher<pair<int, int> > explain_first = Pair(GreaterThan(0), 0);
1593  EXPECT_EQ("whose both fields match, where the first field is a value "
1594  "which is 1 more than 0",
1595  Explain(explain_first, make_pair(1, 0)));
1596 
1597  // If only the second match has an explanation, only this explanation should
1598  // be printed.
1599  const Matcher<pair<int, int> > explain_second = Pair(0, GreaterThan(0));
1600  EXPECT_EQ("whose both fields match, where the second field is a value "
1601  "which is 1 more than 0",
1602  Explain(explain_second, make_pair(0, 1)));
1603 }
1604 
1605 TEST(PairTest, MatchesCorrectly) {
1606  pair<int, std::string> p(25, "foo");
1607 
1608  // Both fields match.
1609  EXPECT_THAT(p, Pair(25, "foo"));
1610  EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o")));
1611 
1612  // 'first' doesnt' match, but 'second' matches.
1613  EXPECT_THAT(p, Not(Pair(42, "foo")));
1614  EXPECT_THAT(p, Not(Pair(Lt(25), "foo")));
1615 
1616  // 'first' matches, but 'second' doesn't match.
1617  EXPECT_THAT(p, Not(Pair(25, "bar")));
1618  EXPECT_THAT(p, Not(Pair(25, Not("foo"))));
1619 
1620  // Neither field matches.
1621  EXPECT_THAT(p, Not(Pair(13, "bar")));
1622  EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a"))));
1623 }
1624 
1625 TEST(PairTest, WorksWithMoveOnly) {
1626  pair<std::unique_ptr<int>, std::unique_ptr<int>> p;
1627  p.second.reset(new int(7));
1628  EXPECT_THAT(p, Pair(Eq(nullptr), Ne(nullptr)));
1629 }
1630 
1631 TEST(PairTest, SafelyCastsInnerMatchers) {
1632  Matcher<int> is_positive = Gt(0);
1633  Matcher<int> is_negative = Lt(0);
1634  pair<char, bool> p('a', true);
1635  EXPECT_THAT(p, Pair(is_positive, _));
1636  EXPECT_THAT(p, Not(Pair(is_negative, _)));
1637  EXPECT_THAT(p, Pair(_, is_positive));
1638  EXPECT_THAT(p, Not(Pair(_, is_negative)));
1639 }
1640 
1641 TEST(PairTest, InsideContainsUsingMap) {
1642  map<int, char> container;
1643  container.insert(make_pair(1, 'a'));
1644  container.insert(make_pair(2, 'b'));
1645  container.insert(make_pair(4, 'c'));
1646  EXPECT_THAT(container, Contains(Pair(1, 'a')));
1648  EXPECT_THAT(container, Contains(Pair(_, 'a')));
1650 }
1651 
1652 TEST(FieldsAreTest, MatchesCorrectly) {
1653  std::tuple<int, std::string, double> p(25, "foo", .5);
1654 
1655  // All fields match.
1656  EXPECT_THAT(p, FieldsAre(25, "foo", .5));
1657  EXPECT_THAT(p, FieldsAre(Ge(20), HasSubstr("o"), DoubleEq(.5)));
1658 
1659  // Some don't match.
1660  EXPECT_THAT(p, Not(FieldsAre(26, "foo", .5)));
1661  EXPECT_THAT(p, Not(FieldsAre(25, "fo", .5)));
1662  EXPECT_THAT(p, Not(FieldsAre(25, "foo", .6)));
1663 }
1664 
1665 TEST(FieldsAreTest, CanDescribeSelf) {
1666  Matcher<const pair<std::string, int>&> m1 = FieldsAre("foo", 42);
1667  EXPECT_EQ(
1668  "has field #0 that is equal to \"foo\""
1669  ", and has field #1 that is equal to 42",
1670  Describe(m1));
1671  EXPECT_EQ(
1672  "has field #0 that isn't equal to \"foo\""
1673  ", or has field #1 that isn't equal to 42",
1674  DescribeNegation(m1));
1675 }
1676 
1677 TEST(FieldsAreTest, CanExplainMatchResultTo) {
1678  // The first one that fails is the one that gives the error.
1679  Matcher<std::tuple<int, int, int>> m =
1680  FieldsAre(GreaterThan(0), GreaterThan(0), GreaterThan(0));
1681 
1682  EXPECT_EQ("whose field #0 does not match, which is 1 less than 0",
1683  Explain(m, std::make_tuple(-1, -2, -3)));
1684  EXPECT_EQ("whose field #1 does not match, which is 2 less than 0",
1685  Explain(m, std::make_tuple(1, -2, -3)));
1686  EXPECT_EQ("whose field #2 does not match, which is 3 less than 0",
1687  Explain(m, std::make_tuple(1, 2, -3)));
1688 
1689  // If they all match, we get a long explanation of success.
1690  EXPECT_EQ(
1691  "whose all elements match, "
1692  "where field #0 is a value which is 1 more than 0"
1693  ", and field #1 is a value which is 2 more than 0"
1694  ", and field #2 is a value which is 3 more than 0",
1695  Explain(m, std::make_tuple(1, 2, 3)));
1696 
1697  // Only print those that have an explanation.
1698  m = FieldsAre(GreaterThan(0), 0, GreaterThan(0));
1699  EXPECT_EQ(
1700  "whose all elements match, "
1701  "where field #0 is a value which is 1 more than 0"
1702  ", and field #2 is a value which is 3 more than 0",
1703  Explain(m, std::make_tuple(1, 0, 3)));
1704 
1705  // If only one has an explanation, then print that one.
1706  m = FieldsAre(0, GreaterThan(0), 0);
1707  EXPECT_EQ(
1708  "whose all elements match, "
1709  "where field #1 is a value which is 1 more than 0",
1710  Explain(m, std::make_tuple(0, 1, 0)));
1711 }
1712 
1713 #if defined(__cpp_structured_bindings) && __cpp_structured_bindings >= 201606
1714 TEST(FieldsAreTest, StructuredBindings) {
1715  // testing::FieldsAre can also match aggregates and such with C++17 and up.
1716  struct MyType {
1717  int i;
1718  std::string str;
1719  };
1720  EXPECT_THAT((MyType{17, "foo"}), FieldsAre(Eq(17), HasSubstr("oo")));
1721 
1722  // Test all the supported arities.
1723  struct MyVarType1 {
1724  int a;
1725  };
1726  EXPECT_THAT(MyVarType1{}, FieldsAre(0));
1727  struct MyVarType2 {
1728  int a, b;
1729  };
1730  EXPECT_THAT(MyVarType2{}, FieldsAre(0, 0));
1731  struct MyVarType3 {
1732  int a, b, c;
1733  };
1734  EXPECT_THAT(MyVarType3{}, FieldsAre(0, 0, 0));
1735  struct MyVarType4 {
1736  int a, b, c, d;
1737  };
1738  EXPECT_THAT(MyVarType4{}, FieldsAre(0, 0, 0, 0));
1739  struct MyVarType5 {
1740  int a, b, c, d, e;
1741  };
1742  EXPECT_THAT(MyVarType5{}, FieldsAre(0, 0, 0, 0, 0));
1743  struct MyVarType6 {
1744  int a, b, c, d, e, f;
1745  };
1746  EXPECT_THAT(MyVarType6{}, FieldsAre(0, 0, 0, 0, 0, 0));
1747  struct MyVarType7 {
1748  int a, b, c, d, e, f, g;
1749  };
1750  EXPECT_THAT(MyVarType7{}, FieldsAre(0, 0, 0, 0, 0, 0, 0));
1751  struct MyVarType8 {
1752  int a, b, c, d, e, f, g, h;
1753  };
1754  EXPECT_THAT(MyVarType8{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0));
1755  struct MyVarType9 {
1756  int a, b, c, d, e, f, g, h, i;
1757  };
1758  EXPECT_THAT(MyVarType9{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0));
1759  struct MyVarType10 {
1760  int a, b, c, d, e, f, g, h, i, j;
1761  };
1762  EXPECT_THAT(MyVarType10{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1763  struct MyVarType11 {
1764  int a, b, c, d, e, f, g, h, i, j, k;
1765  };
1766  EXPECT_THAT(MyVarType11{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1767  struct MyVarType12 {
1768  int a, b, c, d, e, f, g, h, i, j, k, l;
1769  };
1770  EXPECT_THAT(MyVarType12{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1771  struct MyVarType13 {
1772  int a, b, c, d, e, f, g, h, i, j, k, l, m;
1773  };
1774  EXPECT_THAT(MyVarType13{}, FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1775  struct MyVarType14 {
1776  int a, b, c, d, e, f, g, h, i, j, k, l, m, n;
1777  };
1778  EXPECT_THAT(MyVarType14{},
1779  FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1780  struct MyVarType15 {
1781  int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o;
1782  };
1783  EXPECT_THAT(MyVarType15{},
1784  FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1785  struct MyVarType16 {
1786  int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
1787  };
1788  EXPECT_THAT(MyVarType16{},
1789  FieldsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1790 }
1791 #endif
1792 
1793 TEST(ContainsTest, WorksWithMoveOnly) {
1794  ContainerHelper helper;
1795  EXPECT_CALL(helper, Call(Contains(Pointee(2))));
1796  helper.Call(MakeUniquePtrs({1, 2}));
1797 }
1798 
1799 TEST(PairTest, UseGetInsteadOfMembers) {
1800  PairWithGet pair{7, "ABC"};
1801  EXPECT_THAT(pair, Pair(7, "ABC"));
1802  EXPECT_THAT(pair, Pair(Ge(7), HasSubstr("AB")));
1803  EXPECT_THAT(pair, Not(Pair(Lt(7), "ABC")));
1804 
1805  std::vector<PairWithGet> v = {{11, "Foo"}, {29, "gMockIsBestMock"}};
1806  EXPECT_THAT(v,
1807  ElementsAre(Pair(11, std::string("Foo")), Pair(Ge(10), Not(""))));
1808 }
1809 
1810 // Tests StartsWith(s).
1811 
1812 TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
1813  const Matcher<const char*> m1 = StartsWith(std::string(""));
1814  EXPECT_TRUE(m1.Matches("Hi"));
1815  EXPECT_TRUE(m1.Matches(""));
1816  EXPECT_FALSE(m1.Matches(nullptr));
1817 
1818  const Matcher<const std::string&> m2 = StartsWith("Hi");
1819  EXPECT_TRUE(m2.Matches("Hi"));
1820  EXPECT_TRUE(m2.Matches("Hi Hi!"));
1821  EXPECT_TRUE(m2.Matches("High"));
1822  EXPECT_FALSE(m2.Matches("H"));
1823  EXPECT_FALSE(m2.Matches(" Hi"));
1824 
1825 #if GTEST_INTERNAL_HAS_STRING_VIEW
1826  const Matcher<internal::StringView> m_empty =
1827  StartsWith(internal::StringView(""));
1828  EXPECT_TRUE(m_empty.Matches(internal::StringView()));
1829  EXPECT_TRUE(m_empty.Matches(internal::StringView("")));
1830  EXPECT_TRUE(m_empty.Matches(internal::StringView("not empty")));
1831 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1832 }
1833 
1834 TEST(StartsWithTest, CanDescribeSelf) {
1835  Matcher<const std::string> m = StartsWith("Hi");
1836  EXPECT_EQ("starts with \"Hi\"", Describe(m));
1837 }
1838 
1839 // Tests EndsWith(s).
1840 
1841 TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
1842  const Matcher<const char*> m1 = EndsWith("");
1843  EXPECT_TRUE(m1.Matches("Hi"));
1844  EXPECT_TRUE(m1.Matches(""));
1845  EXPECT_FALSE(m1.Matches(nullptr));
1846 
1847  const Matcher<const std::string&> m2 = EndsWith(std::string("Hi"));
1848  EXPECT_TRUE(m2.Matches("Hi"));
1849  EXPECT_TRUE(m2.Matches("Wow Hi Hi"));
1850  EXPECT_TRUE(m2.Matches("Super Hi"));
1851  EXPECT_FALSE(m2.Matches("i"));
1852  EXPECT_FALSE(m2.Matches("Hi "));
1853 
1854 #if GTEST_INTERNAL_HAS_STRING_VIEW
1855  const Matcher<const internal::StringView&> m4 =
1856  EndsWith(internal::StringView(""));
1857  EXPECT_TRUE(m4.Matches("Hi"));
1858  EXPECT_TRUE(m4.Matches(""));
1859  EXPECT_TRUE(m4.Matches(internal::StringView()));
1860  EXPECT_TRUE(m4.Matches(internal::StringView("")));
1861 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1862 }
1863 
1864 TEST(EndsWithTest, CanDescribeSelf) {
1865  Matcher<const std::string> m = EndsWith("Hi");
1866  EXPECT_EQ("ends with \"Hi\"", Describe(m));
1867 }
1868 
1869 // Tests WhenBase64Unescaped.
1870 
1871 TEST(WhenBase64UnescapedTest, MatchesUnescapedBase64Strings) {
1872  const Matcher<const char*> m1 = WhenBase64Unescaped(EndsWith("!"));
1873  EXPECT_FALSE(m1.Matches("invalid base64"));
1874  EXPECT_FALSE(m1.Matches("aGVsbG8gd29ybGQ=")); // hello world
1875  EXPECT_TRUE(m1.Matches("aGVsbG8gd29ybGQh")); // hello world!
1876 
1877  const Matcher<const std::string&> m2 = WhenBase64Unescaped(EndsWith("!"));
1878  EXPECT_FALSE(m2.Matches("invalid base64"));
1879  EXPECT_FALSE(m2.Matches("aGVsbG8gd29ybGQ=")); // hello world
1880  EXPECT_TRUE(m2.Matches("aGVsbG8gd29ybGQh")); // hello world!
1881 
1882 #if GTEST_INTERNAL_HAS_STRING_VIEW
1883  const Matcher<const internal::StringView&> m3 =
1884  WhenBase64Unescaped(EndsWith("!"));
1885  EXPECT_FALSE(m3.Matches("invalid base64"));
1886  EXPECT_FALSE(m3.Matches("aGVsbG8gd29ybGQ=")); // hello world
1887  EXPECT_TRUE(m3.Matches("aGVsbG8gd29ybGQh")); // hello world!
1888 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1889 }
1890 
1891 TEST(WhenBase64UnescapedTest, CanDescribeSelf) {
1892  const Matcher<const char*> m = WhenBase64Unescaped(EndsWith("!"));
1893  EXPECT_EQ("matches after Base64Unescape ends with \"!\"", Describe(m));
1894 }
1895 
1896 // Tests MatchesRegex().
1897 
1898 TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
1899  const Matcher<const char*> m1 = MatchesRegex("a.*z");
1900  EXPECT_TRUE(m1.Matches("az"));
1901  EXPECT_TRUE(m1.Matches("abcz"));
1902  EXPECT_FALSE(m1.Matches(nullptr));
1903 
1904  const Matcher<const std::string&> m2 = MatchesRegex(new RE("a.*z"));
1905  EXPECT_TRUE(m2.Matches("azbz"));
1906  EXPECT_FALSE(m2.Matches("az1"));
1907  EXPECT_FALSE(m2.Matches("1az"));
1908 
1909 #if GTEST_INTERNAL_HAS_STRING_VIEW
1910  const Matcher<const internal::StringView&> m3 = MatchesRegex("a.*z");
1911  EXPECT_TRUE(m3.Matches(internal::StringView("az")));
1912  EXPECT_TRUE(m3.Matches(internal::StringView("abcz")));
1913  EXPECT_FALSE(m3.Matches(internal::StringView("1az")));
1914  EXPECT_FALSE(m3.Matches(internal::StringView()));
1915  const Matcher<const internal::StringView&> m4 =
1916  MatchesRegex(internal::StringView(""));
1917  EXPECT_TRUE(m4.Matches(internal::StringView("")));
1918  EXPECT_TRUE(m4.Matches(internal::StringView()));
1919 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1920 }
1921 
1922 TEST(MatchesRegexTest, CanDescribeSelf) {
1923  Matcher<const std::string> m1 = MatchesRegex(std::string("Hi.*"));
1924  EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
1925 
1926  Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
1927  EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
1928 
1929 #if GTEST_INTERNAL_HAS_STRING_VIEW
1930  Matcher<const internal::StringView> m3 = MatchesRegex(new RE("0.*"));
1931  EXPECT_EQ("matches regular expression \"0.*\"", Describe(m3));
1932 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1933 }
1934 
1935 // Tests ContainsRegex().
1936 
1937 TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
1938  const Matcher<const char*> m1 = ContainsRegex(std::string("a.*z"));
1939  EXPECT_TRUE(m1.Matches("az"));
1940  EXPECT_TRUE(m1.Matches("0abcz1"));
1941  EXPECT_FALSE(m1.Matches(nullptr));
1942 
1943  const Matcher<const std::string&> m2 = ContainsRegex(new RE("a.*z"));
1944  EXPECT_TRUE(m2.Matches("azbz"));
1945  EXPECT_TRUE(m2.Matches("az1"));
1946  EXPECT_FALSE(m2.Matches("1a"));
1947 
1948 #if GTEST_INTERNAL_HAS_STRING_VIEW
1949  const Matcher<const internal::StringView&> m3 =
1950  ContainsRegex(new RE("a.*z"));
1951  EXPECT_TRUE(m3.Matches(internal::StringView("azbz")));
1952  EXPECT_TRUE(m3.Matches(internal::StringView("az1")));
1953  EXPECT_FALSE(m3.Matches(internal::StringView("1a")));
1954  EXPECT_FALSE(m3.Matches(internal::StringView()));
1955  const Matcher<const internal::StringView&> m4 =
1956  ContainsRegex(internal::StringView(""));
1957  EXPECT_TRUE(m4.Matches(internal::StringView("")));
1958  EXPECT_TRUE(m4.Matches(internal::StringView()));
1959 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1960 }
1961 
1962 TEST(ContainsRegexTest, CanDescribeSelf) {
1963  Matcher<const std::string> m1 = ContainsRegex("Hi.*");
1964  EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
1965 
1966  Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
1967  EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
1968 
1969 #if GTEST_INTERNAL_HAS_STRING_VIEW
1970  Matcher<const internal::StringView> m3 = ContainsRegex(new RE("0.*"));
1971  EXPECT_EQ("contains regular expression \"0.*\"", Describe(m3));
1972 #endif // GTEST_INTERNAL_HAS_STRING_VIEW
1973 }
1974 
1975 // Tests for wide strings.
1976 #if GTEST_HAS_STD_WSTRING
1977 TEST(StdWideStrEqTest, MatchesEqual) {
1978  Matcher<const wchar_t*> m = StrEq(::std::wstring(L"Hello"));
1979  EXPECT_TRUE(m.Matches(L"Hello"));
1980  EXPECT_FALSE(m.Matches(L"hello"));
1981  EXPECT_FALSE(m.Matches(nullptr));
1982 
1983  Matcher<const ::std::wstring&> m2 = StrEq(L"Hello");
1984  EXPECT_TRUE(m2.Matches(L"Hello"));
1985  EXPECT_FALSE(m2.Matches(L"Hi"));
1986 
1987  Matcher<const ::std::wstring&> m3 = StrEq(L"\xD3\x576\x8D3\xC74D");
1988  EXPECT_TRUE(m3.Matches(L"\xD3\x576\x8D3\xC74D"));
1989  EXPECT_FALSE(m3.Matches(L"\xD3\x576\x8D3\xC74E"));
1990 
1991  ::std::wstring str(L"01204500800");
1992  str[3] = L'\0';
1993  Matcher<const ::std::wstring&> m4 = StrEq(str);
1994  EXPECT_TRUE(m4.Matches(str));
1995  str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
1996  Matcher<const ::std::wstring&> m5 = StrEq(str);
1997  EXPECT_TRUE(m5.Matches(str));
1998 }
1999 
2000 TEST(StdWideStrEqTest, CanDescribeSelf) {
2001  Matcher< ::std::wstring> m = StrEq(L"Hi-\'\"?\\\a\b\f\n\r\t\v");
2002  EXPECT_EQ("is equal to L\"Hi-\'\\\"?\\\\\\a\\b\\f\\n\\r\\t\\v\"",
2003  Describe(m));
2004 
2005  Matcher< ::std::wstring> m2 = StrEq(L"\xD3\x576\x8D3\xC74D");
2006  EXPECT_EQ("is equal to L\"\\xD3\\x576\\x8D3\\xC74D\"",
2007  Describe(m2));
2008 
2009  ::std::wstring str(L"01204500800");
2010  str[3] = L'\0';
2011  Matcher<const ::std::wstring&> m4 = StrEq(str);
2012  EXPECT_EQ("is equal to L\"012\\04500800\"", Describe(m4));
2013  str[0] = str[6] = str[7] = str[9] = str[10] = L'\0';
2014  Matcher<const ::std::wstring&> m5 = StrEq(str);
2015  EXPECT_EQ("is equal to L\"\\012\\045\\0\\08\\0\\0\"", Describe(m5));
2016 }
2017 
2018 TEST(StdWideStrNeTest, MatchesUnequalString) {
2019  Matcher<const wchar_t*> m = StrNe(L"Hello");
2020  EXPECT_TRUE(m.Matches(L""));
2021  EXPECT_TRUE(m.Matches(nullptr));
2022  EXPECT_FALSE(m.Matches(L"Hello"));
2023 
2024  Matcher< ::std::wstring> m2 = StrNe(::std::wstring(L"Hello"));
2025  EXPECT_TRUE(m2.Matches(L"hello"));
2026  EXPECT_FALSE(m2.Matches(L"Hello"));
2027 }
2028 
2029 TEST(StdWideStrNeTest, CanDescribeSelf) {
2030  Matcher<const wchar_t*> m = StrNe(L"Hi");
2031  EXPECT_EQ("isn't equal to L\"Hi\"", Describe(m));
2032 }
2033 
2034 TEST(StdWideStrCaseEqTest, MatchesEqualStringIgnoringCase) {
2035  Matcher<const wchar_t*> m = StrCaseEq(::std::wstring(L"Hello"));
2036  EXPECT_TRUE(m.Matches(L"Hello"));
2037  EXPECT_TRUE(m.Matches(L"hello"));
2038  EXPECT_FALSE(m.Matches(L"Hi"));
2039  EXPECT_FALSE(m.Matches(nullptr));
2040 
2041  Matcher<const ::std::wstring&> m2 = StrCaseEq(L"Hello");
2042  EXPECT_TRUE(m2.Matches(L"hello"));
2043  EXPECT_FALSE(m2.Matches(L"Hi"));
2044 }
2045 
2046 TEST(StdWideStrCaseEqTest, MatchesEqualStringWith0IgnoringCase) {
2047  ::std::wstring str1(L"oabocdooeoo");
2048  ::std::wstring str2(L"OABOCDOOEOO");
2049  Matcher<const ::std::wstring&> m0 = StrCaseEq(str1);
2050  EXPECT_FALSE(m0.Matches(str2 + ::std::wstring(1, L'\0')));
2051 
2052  str1[3] = str2[3] = L'\0';
2053  Matcher<const ::std::wstring&> m1 = StrCaseEq(str1);
2054  EXPECT_TRUE(m1.Matches(str2));
2055 
2056  str1[0] = str1[6] = str1[7] = str1[10] = L'\0';
2057  str2[0] = str2[6] = str2[7] = str2[10] = L'\0';
2058  Matcher<const ::std::wstring&> m2 = StrCaseEq(str1);
2059  str1[9] = str2[9] = L'\0';
2060  EXPECT_FALSE(m2.Matches(str2));
2061 
2062  Matcher<const ::std::wstring&> m3 = StrCaseEq(str1);
2063  EXPECT_TRUE(m3.Matches(str2));
2064 
2065  EXPECT_FALSE(m3.Matches(str2 + L"x"));
2066  str2.append(1, L'\0');
2067  EXPECT_FALSE(m3.Matches(str2));
2068  EXPECT_FALSE(m3.Matches(::std::wstring(str2, 0, 9)));
2069 }
2070 
2071 TEST(StdWideStrCaseEqTest, CanDescribeSelf) {
2072  Matcher< ::std::wstring> m = StrCaseEq(L"Hi");
2073  EXPECT_EQ("is equal to (ignoring case) L\"Hi\"", Describe(m));
2074 }
2075 
2076 TEST(StdWideStrCaseNeTest, MatchesUnequalStringIgnoringCase) {
2077  Matcher<const wchar_t*> m = StrCaseNe(L"Hello");
2078  EXPECT_TRUE(m.Matches(L"Hi"));
2079  EXPECT_TRUE(m.Matches(nullptr));
2080  EXPECT_FALSE(m.Matches(L"Hello"));
2081  EXPECT_FALSE(m.Matches(L"hello"));
2082 
2083  Matcher< ::std::wstring> m2 = StrCaseNe(::std::wstring(L"Hello"));
2084  EXPECT_TRUE(m2.Matches(L""));
2085  EXPECT_FALSE(m2.Matches(L"Hello"));
2086 }
2087 
2088 TEST(StdWideStrCaseNeTest, CanDescribeSelf) {
2089  Matcher<const wchar_t*> m = StrCaseNe(L"Hi");
2090  EXPECT_EQ("isn't equal to (ignoring case) L\"Hi\"", Describe(m));
2091 }
2092 
2093 // Tests that HasSubstr() works for matching wstring-typed values.
2094 TEST(StdWideHasSubstrTest, WorksForStringClasses) {
2095  const Matcher< ::std::wstring> m1 = HasSubstr(L"foo");
2096  EXPECT_TRUE(m1.Matches(::std::wstring(L"I love food.")));
2097  EXPECT_FALSE(m1.Matches(::std::wstring(L"tofo")));
2098 
2099  const Matcher<const ::std::wstring&> m2 = HasSubstr(L"foo");
2100  EXPECT_TRUE(m2.Matches(::std::wstring(L"I love food.")));
2101  EXPECT_FALSE(m2.Matches(::std::wstring(L"tofo")));
2102 }
2103 
2104 // Tests that HasSubstr() works for matching C-wide-string-typed values.
2105 TEST(StdWideHasSubstrTest, WorksForCStrings) {
2106  const Matcher<wchar_t*> m1 = HasSubstr(L"foo");
2107  EXPECT_TRUE(m1.Matches(const_cast<wchar_t*>(L"I love food.")));
2108  EXPECT_FALSE(m1.Matches(const_cast<wchar_t*>(L"tofo")));
2109  EXPECT_FALSE(m1.Matches(nullptr));
2110 
2111  const Matcher<const wchar_t*> m2 = HasSubstr(L"foo");
2112  EXPECT_TRUE(m2.Matches(L"I love food."));
2113  EXPECT_FALSE(m2.Matches(L"tofo"));
2114  EXPECT_FALSE(m2.Matches(nullptr));
2115 }
2116 
2117 // Tests that HasSubstr(s) describes itself properly.
2118 TEST(StdWideHasSubstrTest, CanDescribeSelf) {
2119  Matcher< ::std::wstring> m = HasSubstr(L"foo\n\"");
2120  EXPECT_EQ("has substring L\"foo\\n\\\"\"", Describe(m));
2121 }
2122 
2123 // Tests StartsWith(s).
2124 
2125 TEST(StdWideStartsWithTest, MatchesStringWithGivenPrefix) {
2126  const Matcher<const wchar_t*> m1 = StartsWith(::std::wstring(L""));
2127  EXPECT_TRUE(m1.Matches(L"Hi"));
2128  EXPECT_TRUE(m1.Matches(L""));
2129  EXPECT_FALSE(m1.Matches(nullptr));
2130 
2131  const Matcher<const ::std::wstring&> m2 = StartsWith(L"Hi");
2132  EXPECT_TRUE(m2.Matches(L"Hi"));
2133  EXPECT_TRUE(m2.Matches(L"Hi Hi!"));
2134  EXPECT_TRUE(m2.Matches(L"High"));
2135  EXPECT_FALSE(m2.Matches(L"H"));
2136  EXPECT_FALSE(m2.Matches(L" Hi"));
2137 }
2138 
2139 TEST(StdWideStartsWithTest, CanDescribeSelf) {
2140  Matcher<const ::std::wstring> m = StartsWith(L"Hi");
2141  EXPECT_EQ("starts with L\"Hi\"", Describe(m));
2142 }
2143 
2144 // Tests EndsWith(s).
2145 
2146 TEST(StdWideEndsWithTest, MatchesStringWithGivenSuffix) {
2147  const Matcher<const wchar_t*> m1 = EndsWith(L"");
2148  EXPECT_TRUE(m1.Matches(L"Hi"));
2149  EXPECT_TRUE(m1.Matches(L""));
2150  EXPECT_FALSE(m1.Matches(nullptr));
2151 
2152  const Matcher<const ::std::wstring&> m2 = EndsWith(::std::wstring(L"Hi"));
2153  EXPECT_TRUE(m2.Matches(L"Hi"));
2154  EXPECT_TRUE(m2.Matches(L"Wow Hi Hi"));
2155  EXPECT_TRUE(m2.Matches(L"Super Hi"));
2156  EXPECT_FALSE(m2.Matches(L"i"));
2157  EXPECT_FALSE(m2.Matches(L"Hi "));
2158 }
2159 
2160 TEST(StdWideEndsWithTest, CanDescribeSelf) {
2161  Matcher<const ::std::wstring> m = EndsWith(L"Hi");
2162  EXPECT_EQ("ends with L\"Hi\"", Describe(m));
2163 }
2164 
2165 #endif // GTEST_HAS_STD_WSTRING
2166 
2167 typedef ::std::tuple<long, int> Tuple2; // NOLINT
2168 
2169 // Tests that Eq() matches a 2-tuple where the first field == the
2170 // second field.
2171 TEST(Eq2Test, MatchesEqualArguments) {
2172  Matcher<const Tuple2&> m = Eq();
2173  EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
2174  EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
2175 }
2176 
2177 // Tests that Eq() describes itself properly.
2178 TEST(Eq2Test, CanDescribeSelf) {
2179  Matcher<const Tuple2&> m = Eq();
2180  EXPECT_EQ("are an equal pair", Describe(m));
2181 }
2182 
2183 // Tests that Ge() matches a 2-tuple where the first field >= the
2184 // second field.
2185 TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) {
2186  Matcher<const Tuple2&> m = Ge();
2187  EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
2188  EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
2189  EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
2190 }
2191 
2192 // Tests that Ge() describes itself properly.
2193 TEST(Ge2Test, CanDescribeSelf) {
2194  Matcher<const Tuple2&> m = Ge();
2195  EXPECT_EQ("are a pair where the first >= the second", Describe(m));
2196 }
2197 
2198 // Tests that Gt() matches a 2-tuple where the first field > the
2199 // second field.
2200 TEST(Gt2Test, MatchesGreaterThanArguments) {
2201  Matcher<const Tuple2&> m = Gt();
2202  EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
2203  EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
2204  EXPECT_FALSE(m.Matches(Tuple2(5L, 6)));
2205 }
2206 
2207 // Tests that Gt() describes itself properly.
2208 TEST(Gt2Test, CanDescribeSelf) {
2209  Matcher<const Tuple2&> m = Gt();
2210  EXPECT_EQ("are a pair where the first > the second", Describe(m));
2211 }
2212 
2213 // Tests that Le() matches a 2-tuple where the first field <= the
2214 // second field.
2215 TEST(Le2Test, MatchesLessThanOrEqualArguments) {
2216  Matcher<const Tuple2&> m = Le();
2217  EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
2218  EXPECT_TRUE(m.Matches(Tuple2(5L, 5)));
2219  EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
2220 }
2221 
2222 // Tests that Le() describes itself properly.
2223 TEST(Le2Test, CanDescribeSelf) {
2224  Matcher<const Tuple2&> m = Le();
2225  EXPECT_EQ("are a pair where the first <= the second", Describe(m));
2226 }
2227 
2228 // Tests that Lt() matches a 2-tuple where the first field < the
2229 // second field.
2230 TEST(Lt2Test, MatchesLessThanArguments) {
2231  Matcher<const Tuple2&> m = Lt();
2232  EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
2233  EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
2234  EXPECT_FALSE(m.Matches(Tuple2(5L, 4)));
2235 }
2236 
2237 // Tests that Lt() describes itself properly.
2238 TEST(Lt2Test, CanDescribeSelf) {
2239  Matcher<const Tuple2&> m = Lt();
2240  EXPECT_EQ("are a pair where the first < the second", Describe(m));
2241 }
2242 
2243 // Tests that Ne() matches a 2-tuple where the first field != the
2244 // second field.
2245 TEST(Ne2Test, MatchesUnequalArguments) {
2246  Matcher<const Tuple2&> m = Ne();
2247  EXPECT_TRUE(m.Matches(Tuple2(5L, 6)));
2248  EXPECT_TRUE(m.Matches(Tuple2(5L, 4)));
2249  EXPECT_FALSE(m.Matches(Tuple2(5L, 5)));
2250 }
2251 
2252 // Tests that Ne() describes itself properly.
2253 TEST(Ne2Test, CanDescribeSelf) {
2254  Matcher<const Tuple2&> m = Ne();
2255  EXPECT_EQ("are an unequal pair", Describe(m));
2256 }
2257 
2258 TEST(PairMatchBaseTest, WorksWithMoveOnly) {
2259  using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>;
2260  Matcher<Pointers> matcher = Eq();
2261  Pointers pointers;
2262  // Tested values don't matter; the point is that matcher does not copy the
2263  // matched values.
2264  EXPECT_TRUE(matcher.Matches(pointers));
2265 }
2266 
2267 // Tests that IsNan() matches a NaN, with float.
2268 TEST(IsNan, FloatMatchesNan) {
2269  float quiet_nan = std::numeric_limits<float>::quiet_NaN();
2270  float other_nan = std::nanf("1");
2271  float real_value = 1.0f;
2272 
2273  Matcher<float> m = IsNan();
2274  EXPECT_TRUE(m.Matches(quiet_nan));
2275  EXPECT_TRUE(m.Matches(other_nan));
2276  EXPECT_FALSE(m.Matches(real_value));
2277 
2278  Matcher<float&> m_ref = IsNan();
2279  EXPECT_TRUE(m_ref.Matches(quiet_nan));
2280  EXPECT_TRUE(m_ref.Matches(other_nan));
2281  EXPECT_FALSE(m_ref.Matches(real_value));
2282 
2283  Matcher<const float&> m_cref = IsNan();
2284  EXPECT_TRUE(m_cref.Matches(quiet_nan));
2285  EXPECT_TRUE(m_cref.Matches(other_nan));
2286  EXPECT_FALSE(m_cref.Matches(real_value));
2287 }
2288 
2289 // Tests that IsNan() matches a NaN, with double.
2290 TEST(IsNan, DoubleMatchesNan) {
2291  double quiet_nan = std::numeric_limits<double>::quiet_NaN();
2292  double other_nan = std::nan("1");
2293  double real_value = 1.0;
2294 
2295  Matcher<double> m = IsNan();
2296  EXPECT_TRUE(m.Matches(quiet_nan));
2297  EXPECT_TRUE(m.Matches(other_nan));
2298  EXPECT_FALSE(m.Matches(real_value));
2299 
2300  Matcher<double&> m_ref = IsNan();
2301  EXPECT_TRUE(m_ref.Matches(quiet_nan));
2302  EXPECT_TRUE(m_ref.Matches(other_nan));
2303  EXPECT_FALSE(m_ref.Matches(real_value));
2304 
2305  Matcher<const double&> m_cref = IsNan();
2306  EXPECT_TRUE(m_cref.Matches(quiet_nan));
2307  EXPECT_TRUE(m_cref.Matches(other_nan));
2308  EXPECT_FALSE(m_cref.Matches(real_value));
2309 }
2310 
2311 // Tests that IsNan() matches a NaN, with long double.
2312 TEST(IsNan, LongDoubleMatchesNan) {
2313  long double quiet_nan = std::numeric_limits<long double>::quiet_NaN();
2314  long double other_nan = std::nan("1");
2315  long double real_value = 1.0;
2316 
2317  Matcher<long double> m = IsNan();
2318  EXPECT_TRUE(m.Matches(quiet_nan));
2319  EXPECT_TRUE(m.Matches(other_nan));
2320  EXPECT_FALSE(m.Matches(real_value));
2321 
2322  Matcher<long double&> m_ref = IsNan();
2323  EXPECT_TRUE(m_ref.Matches(quiet_nan));
2324  EXPECT_TRUE(m_ref.Matches(other_nan));
2325  EXPECT_FALSE(m_ref.Matches(real_value));
2326 
2327  Matcher<const long double&> m_cref = IsNan();
2328  EXPECT_TRUE(m_cref.Matches(quiet_nan));
2329  EXPECT_TRUE(m_cref.Matches(other_nan));
2330  EXPECT_FALSE(m_cref.Matches(real_value));
2331 }
2332 
2333 // Tests that IsNan() works with Not.
2334 TEST(IsNan, NotMatchesNan) {
2335  Matcher<float> mf = Not(IsNan());
2336  EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN()));
2337  EXPECT_FALSE(mf.Matches(std::nanf("1")));
2338  EXPECT_TRUE(mf.Matches(1.0));
2339 
2340  Matcher<double> md = Not(IsNan());
2341  EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN()));
2342  EXPECT_FALSE(md.Matches(std::nan("1")));
2343  EXPECT_TRUE(md.Matches(1.0));
2344 
2345  Matcher<long double> mld = Not(IsNan());
2346  EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN()));
2347  EXPECT_FALSE(mld.Matches(std::nanl("1")));
2348  EXPECT_TRUE(mld.Matches(1.0));
2349 }
2350 
2351 // Tests that IsNan() can describe itself.
2352 TEST(IsNan, CanDescribeSelf) {
2353  Matcher<float> mf = IsNan();
2354  EXPECT_EQ("is NaN", Describe(mf));
2355 
2356  Matcher<double> md = IsNan();
2357  EXPECT_EQ("is NaN", Describe(md));
2358 
2359  Matcher<long double> mld = IsNan();
2360  EXPECT_EQ("is NaN", Describe(mld));
2361 }
2362 
2363 // Tests that IsNan() can describe itself with Not.
2364 TEST(IsNan, CanDescribeSelfWithNot) {
2365  Matcher<float> mf = Not(IsNan());
2366  EXPECT_EQ("isn't NaN", Describe(mf));
2367 
2368  Matcher<double> md = Not(IsNan());
2369  EXPECT_EQ("isn't NaN", Describe(md));
2370 
2371  Matcher<long double> mld = Not(IsNan());
2372  EXPECT_EQ("isn't NaN", Describe(mld));
2373 }
2374 
2375 // Tests that FloatEq() matches a 2-tuple where
2376 // FloatEq(first field) matches the second field.
2377 TEST(FloatEq2Test, MatchesEqualArguments) {
2378  typedef ::std::tuple<float, float> Tpl;
2379  Matcher<const Tpl&> m = FloatEq();
2380  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
2381  EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f)));
2382  EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
2383 }
2384 
2385 // Tests that FloatEq() describes itself properly.
2386 TEST(FloatEq2Test, CanDescribeSelf) {
2387  Matcher<const ::std::tuple<float, float>&> m = FloatEq();
2388  EXPECT_EQ("are an almost-equal pair", Describe(m));
2389 }
2390 
2391 // Tests that NanSensitiveFloatEq() matches a 2-tuple where
2392 // NanSensitiveFloatEq(first field) matches the second field.
2393 TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) {
2394  typedef ::std::tuple<float, float> Tpl;
2395  Matcher<const Tpl&> m = NanSensitiveFloatEq();
2396  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
2397  EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2398  std::numeric_limits<float>::quiet_NaN())));
2399  EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
2400  EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
2401  EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
2402 }
2403 
2404 // Tests that NanSensitiveFloatEq() describes itself properly.
2405 TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) {
2406  Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq();
2407  EXPECT_EQ("are an almost-equal pair", Describe(m));
2408 }
2409 
2410 // Tests that DoubleEq() matches a 2-tuple where
2411 // DoubleEq(first field) matches the second field.
2412 TEST(DoubleEq2Test, MatchesEqualArguments) {
2413  typedef ::std::tuple<double, double> Tpl;
2414  Matcher<const Tpl&> m = DoubleEq();
2415  EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));
2416  EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1)));
2417  EXPECT_FALSE(m.Matches(Tpl(1.1, 1.0)));
2418 }
2419 
2420 // Tests that DoubleEq() describes itself properly.
2421 TEST(DoubleEq2Test, CanDescribeSelf) {
2422  Matcher<const ::std::tuple<double, double>&> m = DoubleEq();
2423  EXPECT_EQ("are an almost-equal pair", Describe(m));
2424 }
2425 
2426 // Tests that NanSensitiveDoubleEq() matches a 2-tuple where
2427 // NanSensitiveDoubleEq(first field) matches the second field.
2428 TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) {
2429  typedef ::std::tuple<double, double> Tpl;
2430  Matcher<const Tpl&> m = NanSensitiveDoubleEq();
2431  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
2432  EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2433  std::numeric_limits<double>::quiet_NaN())));
2434  EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f)));
2435  EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
2436  EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
2437 }
2438 
2439 // Tests that DoubleEq() describes itself properly.
2440 TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) {
2441  Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq();
2442  EXPECT_EQ("are an almost-equal pair", Describe(m));
2443 }
2444 
2445 // Tests that FloatEq() matches a 2-tuple where
2446 // FloatNear(first field, max_abs_error) matches the second field.
2447 TEST(FloatNear2Test, MatchesEqualArguments) {
2448  typedef ::std::tuple<float, float> Tpl;
2449  Matcher<const Tpl&> m = FloatNear(0.5f);
2450  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
2451  EXPECT_TRUE(m.Matches(Tpl(1.3f, 1.0f)));
2452  EXPECT_FALSE(m.Matches(Tpl(1.8f, 1.0f)));
2453 }
2454 
2455 // Tests that FloatNear() describes itself properly.
2456 TEST(FloatNear2Test, CanDescribeSelf) {
2457  Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f);
2458  EXPECT_EQ("are an almost-equal pair", Describe(m));
2459 }
2460 
2461 // Tests that NanSensitiveFloatNear() matches a 2-tuple where
2462 // NanSensitiveFloatNear(first field) matches the second field.
2463 TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) {
2464  typedef ::std::tuple<float, float> Tpl;
2465  Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f);
2466  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
2467  EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));
2468  EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(),
2469  std::numeric_limits<float>::quiet_NaN())));
2470  EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));
2471  EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN())));
2472  EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f)));
2473 }
2474 
2475 // Tests that NanSensitiveFloatNear() describes itself properly.
2476 TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) {
2477  Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f);
2478  EXPECT_EQ("are an almost-equal pair", Describe(m));
2479 }
2480 
2481 // Tests that FloatEq() matches a 2-tuple where
2482 // DoubleNear(first field, max_abs_error) matches the second field.
2483 TEST(DoubleNear2Test, MatchesEqualArguments) {
2484  typedef ::std::tuple<double, double> Tpl;
2485  Matcher<const Tpl&> m = DoubleNear(0.5);
2486  EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0)));
2487  EXPECT_TRUE(m.Matches(Tpl(1.3, 1.0)));
2488  EXPECT_FALSE(m.Matches(Tpl(1.8, 1.0)));
2489 }
2490 
2491 // Tests that DoubleNear() describes itself properly.
2492 TEST(DoubleNear2Test, CanDescribeSelf) {
2493  Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5);
2494  EXPECT_EQ("are an almost-equal pair", Describe(m));
2495 }
2496 
2497 // Tests that NanSensitiveDoubleNear() matches a 2-tuple where
2498 // NanSensitiveDoubleNear(first field) matches the second field.
2499 TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) {
2500  typedef ::std::tuple<double, double> Tpl;
2501  Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f);
2502  EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f)));
2503  EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f)));
2504  EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(),
2505  std::numeric_limits<double>::quiet_NaN())));
2506  EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f)));
2507  EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN())));
2508  EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f)));
2509 }
2510 
2511 // Tests that NanSensitiveDoubleNear() describes itself properly.
2512 TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) {
2513  Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f);
2514  EXPECT_EQ("are an almost-equal pair", Describe(m));
2515 }
2516 
2517 // Tests that Not(m) matches any value that doesn't match m.
2518 TEST(NotTest, NegatesMatcher) {
2519  Matcher<int> m;
2520  m = Not(Eq(2));
2521  EXPECT_TRUE(m.Matches(3));
2522  EXPECT_FALSE(m.Matches(2));
2523 }
2524 
2525 // Tests that Not(m) describes itself properly.
2526 TEST(NotTest, CanDescribeSelf) {
2527  Matcher<int> m = Not(Eq(5));
2528  EXPECT_EQ("isn't equal to 5", Describe(m));
2529 }
2530 
2531 // Tests that monomorphic matchers are safely cast by the Not matcher.
2532 TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) {
2533  // greater_than_5 is a monomorphic matcher.
2534  Matcher<int> greater_than_5 = Gt(5);
2535 
2536  Matcher<const int&> m = Not(greater_than_5);
2537  Matcher<int&> m2 = Not(greater_than_5);
2538  Matcher<int&> m3 = Not(m);
2539 }
2540 
2541 // Helper to allow easy testing of AllOf matchers with num parameters.
2542 void AllOfMatches(int num, const Matcher<int>& m) {
2543  SCOPED_TRACE(Describe(m));
2544  EXPECT_TRUE(m.Matches(0));
2545  for (int i = 1; i <= num; ++i) {
2546  EXPECT_FALSE(m.Matches(i));
2547  }
2548  EXPECT_TRUE(m.Matches(num + 1));
2549 }
2550 
2551 // Tests that AllOf(m1, ..., mn) matches any value that matches all of
2552 // the given matchers.
2553 TEST(AllOfTest, MatchesWhenAllMatch) {
2554  Matcher<int> m;
2555  m = AllOf(Le(2), Ge(1));
2556  EXPECT_TRUE(m.Matches(1));
2557  EXPECT_TRUE(m.Matches(2));
2558  EXPECT_FALSE(m.Matches(0));
2559  EXPECT_FALSE(m.Matches(3));
2560 
2561  m = AllOf(Gt(0), Ne(1), Ne(2));
2562  EXPECT_TRUE(m.Matches(3));
2563  EXPECT_FALSE(m.Matches(2));
2564  EXPECT_FALSE(m.Matches(1));
2565  EXPECT_FALSE(m.Matches(0));
2566 
2567  m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2568  EXPECT_TRUE(m.Matches(4));
2569  EXPECT_FALSE(m.Matches(3));
2570  EXPECT_FALSE(m.Matches(2));
2571  EXPECT_FALSE(m.Matches(1));
2572  EXPECT_FALSE(m.Matches(0));
2573 
2574  m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2575  EXPECT_TRUE(m.Matches(0));
2576  EXPECT_TRUE(m.Matches(1));
2577  EXPECT_FALSE(m.Matches(3));
2578 
2579  // The following tests for varying number of sub-matchers. Due to the way
2580  // the sub-matchers are handled it is enough to test every sub-matcher once
2581  // with sub-matchers using the same matcher type. Varying matcher types are
2582  // checked for above.
2583  AllOfMatches(2, AllOf(Ne(1), Ne(2)));
2584  AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3)));
2585  AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4)));
2586  AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5)));
2587  AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6)));
2588  AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7)));
2589  AllOfMatches(8, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2590  Ne(8)));
2591  AllOfMatches(9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7),
2592  Ne(8), Ne(9)));
2593  AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8),
2594  Ne(9), Ne(10)));
2595  AllOfMatches(
2596  50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
2597  Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17),
2598  Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25),
2599  Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33),
2600  Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41),
2601  Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49),
2602  Ne(50)));
2603 }
2604 
2605 
2606 // Tests that AllOf(m1, ..., mn) describes itself properly.
2607 TEST(AllOfTest, CanDescribeSelf) {
2608  Matcher<int> m;
2609  m = AllOf(Le(2), Ge(1));
2610  EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m));
2611 
2612  m = AllOf(Gt(0), Ne(1), Ne(2));
2613  std::string expected_descr1 =
2614  "(is > 0) and (isn't equal to 1) and (isn't equal to 2)";
2615  EXPECT_EQ(expected_descr1, Describe(m));
2616 
2617  m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2618  std::string expected_descr2 =
2619  "(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal "
2620  "to 3)";
2621  EXPECT_EQ(expected_descr2, Describe(m));
2622 
2623  m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2624  std::string expected_descr3 =
2625  "(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) "
2626  "and (isn't equal to 7)";
2627  EXPECT_EQ(expected_descr3, Describe(m));
2628 }
2629 
2630 // Tests that AllOf(m1, ..., mn) describes its negation properly.
2631 TEST(AllOfTest, CanDescribeNegation) {
2632  Matcher<int> m;
2633  m = AllOf(Le(2), Ge(1));
2634  std::string expected_descr4 = "(isn't <= 2) or (isn't >= 1)";
2635  EXPECT_EQ(expected_descr4, DescribeNegation(m));
2636 
2637  m = AllOf(Gt(0), Ne(1), Ne(2));
2638  std::string expected_descr5 =
2639  "(isn't > 0) or (is equal to 1) or (is equal to 2)";
2640  EXPECT_EQ(expected_descr5, DescribeNegation(m));
2641 
2642  m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3));
2643  std::string expected_descr6 =
2644  "(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)";
2645  EXPECT_EQ(expected_descr6, DescribeNegation(m));
2646 
2647  m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7));
2648  std::string expected_desr7 =
2649  "(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or "
2650  "(is equal to 7)";
2651  EXPECT_EQ(expected_desr7, DescribeNegation(m));
2652 
2653  m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9),
2654  Ne(10), Ne(11));
2655  AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2656  EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11)"));
2657  AllOfMatches(11, m);
2658 }
2659 
2660 // Tests that monomorphic matchers are safely cast by the AllOf matcher.
2661 TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) {
2662  // greater_than_5 and less_than_10 are monomorphic matchers.
2663  Matcher<int> greater_than_5 = Gt(5);
2664  Matcher<int> less_than_10 = Lt(10);
2665 
2666  Matcher<const int&> m = AllOf(greater_than_5, less_than_10);
2667  Matcher<int&> m2 = AllOf(greater_than_5, less_than_10);
2668  Matcher<int&> m3 = AllOf(greater_than_5, m2);
2669 
2670  // Tests that BothOf works when composing itself.
2671  Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10);
2672  Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10);
2673 }
2674 
2675 TEST(AllOfTest, ExplainsResult) {
2676  Matcher<int> m;
2677 
2678  // Successful match. Both matchers need to explain. The second
2679  // matcher doesn't give an explanation, so only the first matcher's
2680  // explanation is printed.
2681  m = AllOf(GreaterThan(10), Lt(30));
2682  EXPECT_EQ("which is 15 more than 10", Explain(m, 25));
2683 
2684  // Successful match. Both matchers need to explain.
2685  m = AllOf(GreaterThan(10), GreaterThan(20));
2686  EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20",
2687  Explain(m, 30));
2688 
2689  // Successful match. All matchers need to explain. The second
2690  // matcher doesn't given an explanation.
2691  m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20));
2692  EXPECT_EQ("which is 15 more than 10, and which is 5 more than 20",
2693  Explain(m, 25));
2694 
2695  // Successful match. All matchers need to explain.
2696  m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2697  EXPECT_EQ("which is 30 more than 10, and which is 20 more than 20, "
2698  "and which is 10 more than 30",
2699  Explain(m, 40));
2700 
2701  // Failed match. The first matcher, which failed, needs to
2702  // explain.
2703  m = AllOf(GreaterThan(10), GreaterThan(20));
2704  EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2705 
2706  // Failed match. The second matcher, which failed, needs to
2707  // explain. Since it doesn't given an explanation, nothing is
2708  // printed.
2709  m = AllOf(GreaterThan(10), Lt(30));
2710  EXPECT_EQ("", Explain(m, 40));
2711 
2712  // Failed match. The second matcher, which failed, needs to
2713  // explain.
2714  m = AllOf(GreaterThan(10), GreaterThan(20));
2715  EXPECT_EQ("which is 5 less than 20", Explain(m, 15));
2716 }
2717 
2718 // Helper to allow easy testing of AnyOf matchers with num parameters.
2719 static void AnyOfMatches(int num, const Matcher<int>& m) {
2720  SCOPED_TRACE(Describe(m));
2721  EXPECT_FALSE(m.Matches(0));
2722  for (int i = 1; i <= num; ++i) {
2723  EXPECT_TRUE(m.Matches(i));
2724  }
2725  EXPECT_FALSE(m.Matches(num + 1));
2726 }
2727 
2728 static void AnyOfStringMatches(int num, const Matcher<std::string>& m) {
2729  SCOPED_TRACE(Describe(m));
2730  EXPECT_FALSE(m.Matches(std::to_string(0)));
2731 
2732  for (int i = 1; i <= num; ++i) {
2733  EXPECT_TRUE(m.Matches(std::to_string(i)));
2734  }
2735  EXPECT_FALSE(m.Matches(std::to_string(num + 1)));
2736 }
2737 
2738 // Tests that AnyOf(m1, ..., mn) matches any value that matches at
2739 // least one of the given matchers.
2740 TEST(AnyOfTest, MatchesWhenAnyMatches) {
2741  Matcher<int> m;
2742  m = AnyOf(Le(1), Ge(3));
2743  EXPECT_TRUE(m.Matches(1));
2744  EXPECT_TRUE(m.Matches(4));
2745  EXPECT_FALSE(m.Matches(2));
2746 
2747  m = AnyOf(Lt(0), Eq(1), Eq(2));
2748  EXPECT_TRUE(m.Matches(-1));
2749  EXPECT_TRUE(m.Matches(1));
2750  EXPECT_TRUE(m.Matches(2));
2751  EXPECT_FALSE(m.Matches(0));
2752 
2753  m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2754  EXPECT_TRUE(m.Matches(-1));
2755  EXPECT_TRUE(m.Matches(1));
2756  EXPECT_TRUE(m.Matches(2));
2757  EXPECT_TRUE(m.Matches(3));
2758  EXPECT_FALSE(m.Matches(0));
2759 
2760  m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2761  EXPECT_TRUE(m.Matches(0));
2762  EXPECT_TRUE(m.Matches(11));
2763  EXPECT_TRUE(m.Matches(3));
2764  EXPECT_FALSE(m.Matches(2));
2765 
2766  // The following tests for varying number of sub-matchers. Due to the way
2767  // the sub-matchers are handled it is enough to test every sub-matcher once
2768  // with sub-matchers using the same matcher type. Varying matcher types are
2769  // checked for above.
2770  AnyOfMatches(2, AnyOf(1, 2));
2771  AnyOfMatches(3, AnyOf(1, 2, 3));
2772  AnyOfMatches(4, AnyOf(1, 2, 3, 4));
2773  AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5));
2774  AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6));
2775  AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7));
2776  AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8));
2777  AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9));
2778  AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
2779 }
2780 
2781 // Tests the variadic version of the AnyOfMatcher.
2782 TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) {
2783  // Also make sure AnyOf is defined in the right namespace and does not depend
2784  // on ADL.
2785  Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
2786 
2787  EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)"));
2788  AnyOfMatches(11, m);
2789  AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2790  11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
2791  21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
2792  31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
2793  41, 42, 43, 44, 45, 46, 47, 48, 49, 50));
2794  AnyOfStringMatches(
2795  50, AnyOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
2796  "13", "14", "15", "16", "17", "18", "19", "20", "21", "22",
2797  "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
2798  "33", "34", "35", "36", "37", "38", "39", "40", "41", "42",
2799  "43", "44", "45", "46", "47", "48", "49", "50"));
2800 }
2801 
2802 TEST(ConditionalTest, MatchesFirstIfCondition) {
2803  Matcher<std::string> eq_red = Eq("red");
2804  Matcher<std::string> ne_red = Ne("red");
2805  Matcher<std::string> m = Conditional(true, eq_red, ne_red);
2806  EXPECT_TRUE(m.Matches("red"));
2807  EXPECT_FALSE(m.Matches("green"));
2808 
2809  StringMatchResultListener listener;
2810  StringMatchResultListener expected;
2811  EXPECT_FALSE(m.MatchAndExplain("green", &listener));
2812  EXPECT_FALSE(eq_red.MatchAndExplain("green", &expected));
2813  EXPECT_THAT(listener.str(), Eq(expected.str()));
2814 }
2815 
2816 TEST(ConditionalTest, MatchesSecondIfCondition) {
2817  Matcher<std::string> eq_red = Eq("red");
2818  Matcher<std::string> ne_red = Ne("red");
2819  Matcher<std::string> m = Conditional(false, eq_red, ne_red);
2820  EXPECT_FALSE(m.Matches("red"));
2821  EXPECT_TRUE(m.Matches("green"));
2822 
2823  StringMatchResultListener listener;
2824  StringMatchResultListener expected;
2825  EXPECT_FALSE(m.MatchAndExplain("red", &listener));
2826  EXPECT_FALSE(ne_red.MatchAndExplain("red", &expected));
2827  EXPECT_THAT(listener.str(), Eq(expected.str()));
2828 }
2829 
2830 // Tests the variadic version of the ElementsAreMatcher
2831 TEST(ElementsAreTest, HugeMatcher) {
2832  vector<int> test_vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
2833 
2835  ElementsAre(Eq(1), Eq(2), Lt(13), Eq(4), Eq(5), Eq(6), Eq(7),
2836  Eq(8), Eq(9), Eq(10), Gt(1), Eq(12)));
2837 }
2838 
2839 // Tests the variadic version of the UnorderedElementsAreMatcher
2840 TEST(ElementsAreTest, HugeMatcherStr) {
2841  vector<std::string> test_vector{
2842  "literal_string", "", "", "", "", "", "", "", "", "", "", ""};
2843 
2844  EXPECT_THAT(test_vector, UnorderedElementsAre("literal_string", _, _, _, _, _,
2845  _, _, _, _, _, _));
2846 }
2847 
2848 // Tests the variadic version of the UnorderedElementsAreMatcher
2849 TEST(ElementsAreTest, HugeMatcherUnordered) {
2850  vector<int> test_vector{2, 1, 8, 5, 4, 6, 7, 3, 9, 12, 11, 10};
2851 
2853  Eq(2), Eq(1), Gt(7), Eq(5), Eq(4), Eq(6), Eq(7),
2854  Eq(3), Eq(9), Eq(12), Eq(11), Ne(122)));
2855 }
2856 
2857 
2858 // Tests that AnyOf(m1, ..., mn) describes itself properly.
2859 TEST(AnyOfTest, CanDescribeSelf) {
2860  Matcher<int> m;
2861  m = AnyOf(Le(1), Ge(3));
2862 
2863  EXPECT_EQ("(is <= 1) or (is >= 3)",
2864  Describe(m));
2865 
2866  m = AnyOf(Lt(0), Eq(1), Eq(2));
2867  EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m));
2868 
2869  m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2870  EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)",
2871  Describe(m));
2872 
2873  m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2874  EXPECT_EQ(
2875  "(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is "
2876  "equal to 7)",
2877  Describe(m));
2878 }
2879 
2880 // Tests that AnyOf(m1, ..., mn) describes its negation properly.
2881 TEST(AnyOfTest, CanDescribeNegation) {
2882  Matcher<int> m;
2883  m = AnyOf(Le(1), Ge(3));
2884  EXPECT_EQ("(isn't <= 1) and (isn't >= 3)",
2885  DescribeNegation(m));
2886 
2887  m = AnyOf(Lt(0), Eq(1), Eq(2));
2888  EXPECT_EQ("(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)",
2889  DescribeNegation(m));
2890 
2891  m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3));
2892  EXPECT_EQ(
2893  "(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't "
2894  "equal to 3)",
2895  DescribeNegation(m));
2896 
2897  m = AnyOf(Le(0), Gt(10), 3, 5, 7);
2898  EXPECT_EQ(
2899  "(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal "
2900  "to 5) and (isn't equal to 7)",
2901  DescribeNegation(m));
2902 }
2903 
2904 // Tests that monomorphic matchers are safely cast by the AnyOf matcher.
2905 TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) {
2906  // greater_than_5 and less_than_10 are monomorphic matchers.
2907  Matcher<int> greater_than_5 = Gt(5);
2908  Matcher<int> less_than_10 = Lt(10);
2909 
2910  Matcher<const int&> m = AnyOf(greater_than_5, less_than_10);
2911  Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10);
2912  Matcher<int&> m3 = AnyOf(greater_than_5, m2);
2913 
2914  // Tests that EitherOf works when composing itself.
2915  Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10);
2916  Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10);
2917 }
2918 
2919 TEST(AnyOfTest, ExplainsResult) {
2920  Matcher<int> m;
2921 
2922  // Failed match. Both matchers need to explain. The second
2923  // matcher doesn't give an explanation, so only the first matcher's
2924  // explanation is printed.
2925  m = AnyOf(GreaterThan(10), Lt(0));
2926  EXPECT_EQ("which is 5 less than 10", Explain(m, 5));
2927 
2928  // Failed match. Both matchers need to explain.
2929  m = AnyOf(GreaterThan(10), GreaterThan(20));
2930  EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20",
2931  Explain(m, 5));
2932 
2933  // Failed match. All matchers need to explain. The second
2934  // matcher doesn't given an explanation.
2935  m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30));
2936  EXPECT_EQ("which is 5 less than 10, and which is 25 less than 30",
2937  Explain(m, 5));
2938 
2939  // Failed match. All matchers need to explain.
2940  m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30));
2941  EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20, "
2942  "and which is 25 less than 30",
2943  Explain(m, 5));
2944 
2945  // Successful match. The first matcher, which succeeded, needs to
2946  // explain.
2947  m = AnyOf(GreaterThan(10), GreaterThan(20));
2948  EXPECT_EQ("which is 5 more than 10", Explain(m, 15));
2949 
2950  // Successful match. The second matcher, which succeeded, needs to
2951  // explain. Since it doesn't given an explanation, nothing is
2952  // printed.
2953  m = AnyOf(GreaterThan(10), Lt(30));
2954  EXPECT_EQ("", Explain(m, 0));
2955 
2956  // Successful match. The second matcher, which succeeded, needs to
2957  // explain.
2958  m = AnyOf(GreaterThan(30), GreaterThan(20));
2959  EXPECT_EQ("which is 5 more than 20", Explain(m, 25));
2960 }
2961 
2962 // The following predicate function and predicate functor are for
2963 // testing the Truly(predicate) matcher.
2964 
2965 // Returns non-zero if the input is positive. Note that the return
2966 // type of this function is not bool. It's OK as Truly() accepts any
2967 // unary function or functor whose return type can be implicitly
2968 // converted to bool.
2969 int IsPositive(double x) {
2970  return x > 0 ? 1 : 0;
2971 }
2972 
2973 // This functor returns true if the input is greater than the given
2974 // number.
2975 class IsGreaterThan {
2976  public:
2977  explicit IsGreaterThan(int threshold) : threshold_(threshold) {}
2978 
2979  bool operator()(int n) const { return n > threshold_; }
2980 
2981  private:
2983 };
2984 
2985 // For testing Truly().
2986 const int foo = 0;
2987 
2988 // This predicate returns true if and only if the argument references foo and
2989 // has a zero value.
2990 bool ReferencesFooAndIsZero(const int& n) {
2991  return (&n == &foo) && (n == 0);
2992 }
2993 
2994 // Tests that Truly(predicate) matches what satisfies the given
2995 // predicate.
2996 TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) {
2997  Matcher<double> m = Truly(IsPositive);
2998  EXPECT_TRUE(m.Matches(2.0));
2999  EXPECT_FALSE(m.Matches(-1.5));
3000 }
3001 
3002 // Tests that Truly(predicate_functor) works too.
3003 TEST(TrulyTest, CanBeUsedWithFunctor) {
3004  Matcher<int> m = Truly(IsGreaterThan(5));
3005  EXPECT_TRUE(m.Matches(6));
3006  EXPECT_FALSE(m.Matches(4));
3007 }
3008 
3009 // A class that can be implicitly converted to bool.
3010 class ConvertibleToBool {
3011  public:
3012  explicit ConvertibleToBool(int number) : number_(number) {}
3013  operator bool() const { return number_ != 0; }
3014 
3015  private:
3016  int number_;
3017 };
3018 
3019 ConvertibleToBool IsNotZero(int number) {
3020  return ConvertibleToBool(number);
3021 }
3022 
3023 // Tests that the predicate used in Truly() may return a class that's
3024 // implicitly convertible to bool, even when the class has no
3025 // operator!().
3026 TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) {
3027  Matcher<int> m = Truly(IsNotZero);
3028  EXPECT_TRUE(m.Matches(1));
3029  EXPECT_FALSE(m.Matches(0));
3030 }
3031 
3032 // Tests that Truly(predicate) can describe itself properly.
3033 TEST(TrulyTest, CanDescribeSelf) {
3034  Matcher<double> m = Truly(IsPositive);
3035  EXPECT_EQ("satisfies the given predicate",
3036  Describe(m));
3037 }
3038 
3039 // Tests that Truly(predicate) works when the matcher takes its
3040 // argument by reference.
3041 TEST(TrulyTest, WorksForByRefArguments) {
3042  Matcher<const int&> m = Truly(ReferencesFooAndIsZero);
3043  EXPECT_TRUE(m.Matches(foo));
3044  int n = 0;
3045  EXPECT_FALSE(m.Matches(n));
3046 }
3047 
3048 // Tests that Truly(predicate) provides a helpful reason when it fails.
3049 TEST(TrulyTest, ExplainsFailures) {
3050  StringMatchResultListener listener;
3051  EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener));
3052  EXPECT_EQ(listener.str(), "didn't satisfy the given predicate");
3053 }
3054 
3055 // Tests that Matches(m) is a predicate satisfied by whatever that
3056 // matches matcher m.
3057 TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) {
3058  EXPECT_TRUE(Matches(Ge(0))(1));
3059  EXPECT_FALSE(Matches(Eq('a'))('b'));
3060 }
3061 
3062 // Tests that Matches(m) works when the matcher takes its argument by
3063 // reference.
3064 TEST(MatchesTest, WorksOnByRefArguments) {
3065  int m = 0, n = 0;
3066  EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n));
3067  EXPECT_FALSE(Matches(Ref(m))(n));
3068 }
3069 
3070 // Tests that a Matcher on non-reference type can be used in
3071 // Matches().
3072 TEST(MatchesTest, WorksWithMatcherOnNonRefType) {
3073  Matcher<int> eq5 = Eq(5);
3074  EXPECT_TRUE(Matches(eq5)(5));
3075  EXPECT_FALSE(Matches(eq5)(2));
3076 }
3077 
3078 // Tests Value(value, matcher). Since Value() is a simple wrapper for
3079 // Matches(), which has been tested already, we don't spend a lot of
3080 // effort on testing Value().
3081 TEST(ValueTest, WorksWithPolymorphicMatcher) {
3082  EXPECT_TRUE(Value("hi", StartsWith("h")));
3083  EXPECT_FALSE(Value(5, Gt(10)));
3084 }
3085 
3086 TEST(ValueTest, WorksWithMonomorphicMatcher) {
3087  const Matcher<int> is_zero = Eq(0);
3088  EXPECT_TRUE(Value(0, is_zero));
3089  EXPECT_FALSE(Value('a', is_zero));
3090 
3091  int n = 0;
3092  const Matcher<const int&> ref_n = Ref(n);
3093  EXPECT_TRUE(Value(n, ref_n));
3094  EXPECT_FALSE(Value(1, ref_n));
3095 }
3096 
3097 TEST(ExplainMatchResultTest, WorksWithPolymorphicMatcher) {
3098  StringMatchResultListener listener1;
3099  EXPECT_TRUE(ExplainMatchResult(PolymorphicIsEven(), 42, &listener1));
3100  EXPECT_EQ("% 2 == 0", listener1.str());
3101 
3102  StringMatchResultListener listener2;
3103  EXPECT_FALSE(ExplainMatchResult(Ge(42), 1.5, &listener2));
3104  EXPECT_EQ("", listener2.str());
3105 }
3106 
3107 TEST(ExplainMatchResultTest, WorksWithMonomorphicMatcher) {
3108  const Matcher<int> is_even = PolymorphicIsEven();
3109  StringMatchResultListener listener1;
3110  EXPECT_TRUE(ExplainMatchResult(is_even, 42, &listener1));
3111  EXPECT_EQ("% 2 == 0", listener1.str());
3112 
3113  const Matcher<const double&> is_zero = Eq(0);
3114  StringMatchResultListener listener2;
3115  EXPECT_FALSE(ExplainMatchResult(is_zero, 1.5, &listener2));
3116  EXPECT_EQ("", listener2.str());
3117 }
3118 
3119 MATCHER(ConstructNoArg, "") { return true; }
3120 MATCHER_P(Construct1Arg, arg1, "") { return true; }
3121 MATCHER_P2(Construct2Args, arg1, arg2, "") { return true; }
3122 
3123 TEST(MatcherConstruct, ExplicitVsImplicit) {
3124  {
3125  // No arg constructor can be constructed with empty brace.
3126  ConstructNoArgMatcher m = {};
3127  (void)m;
3128  // And with no args
3129  ConstructNoArgMatcher m2;
3130  (void)m2;
3131  }
3132  {
3133  // The one arg constructor has an explicit constructor.
3134  // This is to prevent the implicit conversion.
3135  using M = Construct1ArgMatcherP<int>;
3138  }
3139  {
3140  // Multiple arg matchers can be constructed with an implicit construction.
3141  Construct2ArgsMatcherP2<int, double> m = {1, 2.2};
3142  (void)m;
3143  }
3144 }
3145 
3146 MATCHER_P(Really, inner_matcher, "") {
3147  return ExplainMatchResult(inner_matcher, arg, result_listener);
3148 }
3149 
3150 TEST(ExplainMatchResultTest, WorksInsideMATCHER) {
3151  EXPECT_THAT(0, Really(Eq(0)));
3152 }
3153 
3154 TEST(DescribeMatcherTest, WorksWithValue) {
3155  EXPECT_EQ("is equal to 42", DescribeMatcher<int>(42));
3156  EXPECT_EQ("isn't equal to 42", DescribeMatcher<int>(42, true));
3157 }
3158 
3159 TEST(DescribeMatcherTest, WorksWithMonomorphicMatcher) {
3160  const Matcher<int> monomorphic = Le(0);
3161  EXPECT_EQ("is <= 0", DescribeMatcher<int>(monomorphic));
3162  EXPECT_EQ("isn't <= 0", DescribeMatcher<int>(monomorphic, true));
3163 }
3164 
3165 TEST(DescribeMatcherTest, WorksWithPolymorphicMatcher) {
3166  EXPECT_EQ("is even", DescribeMatcher<int>(PolymorphicIsEven()));
3167  EXPECT_EQ("is odd", DescribeMatcher<int>(PolymorphicIsEven(), true));
3168 }
3169 
3170 TEST(AllArgsTest, WorksForTuple) {
3171  EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt()));
3172  EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt())));
3173 }
3174 
3175 TEST(AllArgsTest, WorksForNonTuple) {
3176  EXPECT_THAT(42, AllArgs(Gt(0)));
3177  EXPECT_THAT('a', Not(AllArgs(Eq('b'))));
3178 }
3179 
3180 class AllArgsHelper {
3181  public:
3182  AllArgsHelper() {}
3183 
3184  MOCK_METHOD2(Helper, int(char x, int y));
3185 
3186  private:
3187  GTEST_DISALLOW_COPY_AND_ASSIGN_(AllArgsHelper);
3188 };
3189 
3190 TEST(AllArgsTest, WorksInWithClause) {
3191  AllArgsHelper helper;
3192  ON_CALL(helper, Helper(_, _))
3193  .With(AllArgs(Lt()))
3194  .WillByDefault(Return(1));
3195  EXPECT_CALL(helper, Helper(_, _));
3196  EXPECT_CALL(helper, Helper(_, _))
3197  .With(AllArgs(Gt()))
3198  .WillOnce(Return(2));
3199 
3200  EXPECT_EQ(1, helper.Helper('\1', 2));
3201  EXPECT_EQ(2, helper.Helper('a', 1));
3202 }
3203 
3204 class OptionalMatchersHelper {
3205  public:
3206  OptionalMatchersHelper() {}
3207 
3208  MOCK_METHOD0(NoArgs, int());
3209 
3210  MOCK_METHOD1(OneArg, int(int y));
3211 
3212  MOCK_METHOD2(TwoArgs, int(char x, int y));
3213 
3214  MOCK_METHOD1(Overloaded, int(char x));
3215  MOCK_METHOD2(Overloaded, int(char x, int y));
3216 
3217  private:
3218  GTEST_DISALLOW_COPY_AND_ASSIGN_(OptionalMatchersHelper);
3219 };
3220 
3221 TEST(AllArgsTest, WorksWithoutMatchers) {
3222  OptionalMatchersHelper helper;
3223 
3224  ON_CALL(helper, NoArgs).WillByDefault(Return(10));
3225  ON_CALL(helper, OneArg).WillByDefault(Return(20));
3226  ON_CALL(helper, TwoArgs).WillByDefault(Return(30));
3227 
3228  EXPECT_EQ(10, helper.NoArgs());
3229  EXPECT_EQ(20, helper.OneArg(1));
3230  EXPECT_EQ(30, helper.TwoArgs('\1', 2));
3231 
3232  EXPECT_CALL(helper, NoArgs).Times(1);
3233  EXPECT_CALL(helper, OneArg).WillOnce(Return(100));
3234  EXPECT_CALL(helper, OneArg(17)).WillOnce(Return(200));
3235  EXPECT_CALL(helper, TwoArgs).Times(0);
3236 
3237  EXPECT_EQ(10, helper.NoArgs());
3238  EXPECT_EQ(100, helper.OneArg(1));
3239  EXPECT_EQ(200, helper.OneArg(17));
3240 }
3241 
3242 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
3243 // matches the matcher.
3244 TEST(MatcherAssertionTest, WorksWhenMatcherIsSatisfied) {
3245  ASSERT_THAT(5, Ge(2)) << "This should succeed.";
3246  ASSERT_THAT("Foo", EndsWith("oo"));
3247  EXPECT_THAT(2, AllOf(Le(7), Ge(0))) << "This should succeed too.";
3248  EXPECT_THAT("Hello", StartsWith("Hell"));
3249 }
3250 
3251 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
3252 // doesn't match the matcher.
3253 TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) {
3254  // 'n' must be static as it is used in an EXPECT_FATAL_FAILURE(),
3255  // which cannot reference auto variables.
3256  static unsigned short n; // NOLINT
3257  n = 5;
3258 
3260  "Value of: n\n"
3261  "Expected: is > 10\n"
3262  " Actual: 5" + OfType("unsigned short"));
3263  n = 0;
3265  EXPECT_THAT(n, AllOf(Le(7), Ge(5))),
3266  "Value of: n\n"
3267  "Expected: (is <= 7) and (is >= 5)\n"
3268  " Actual: 0" + OfType("unsigned short"));
3269 }
3270 
3271 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument
3272 // has a reference type.
3273 TEST(MatcherAssertionTest, WorksForByRefArguments) {
3274  // We use a static variable here as EXPECT_FATAL_FAILURE() cannot
3275  // reference auto variables.
3276  static int n;
3277  n = 0;
3278  EXPECT_THAT(n, AllOf(Le(7), Ref(n)));
3280  "Value of: n\n"
3281  "Expected: does not reference the variable @");
3282  // Tests the "Actual" part.
3284  "Actual: 0" + OfType("int") + ", which is located @");
3285 }
3286 
3287 // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is
3288 // monomorphic.
3289 TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) {
3290  Matcher<const char*> starts_with_he = StartsWith("he");
3291  ASSERT_THAT("hello", starts_with_he);
3292 
3293  Matcher<const std::string&> ends_with_ok = EndsWith("ok");
3294  ASSERT_THAT("book", ends_with_ok);
3295  const std::string bad = "bad";
3296  EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
3297  "Value of: bad\n"
3298  "Expected: ends with \"ok\"\n"
3299  " Actual: \"bad\"");
3300  Matcher<int> is_greater_than_5 = Gt(5);
3301  EXPECT_NONFATAL_FAILURE(EXPECT_THAT(5, is_greater_than_5),
3302  "Value of: 5\n"
3303  "Expected: is > 5\n"
3304  " Actual: 5" + OfType("int"));
3305 }
3306 
3307 // Tests floating-point matchers.
3308 template <typename RawType>
3309 class FloatingPointTest : public testing::Test {
3310  protected:
3312  typedef typename Floating::Bits Bits;
3313 
3314  FloatingPointTest()
3315  : max_ulps_(Floating::kMaxUlps),
3316  zero_bits_(Floating(0).bits()),
3317  one_bits_(Floating(1).bits()),
3318  infinity_bits_(Floating(Floating::Infinity()).bits()),
3320  Floating::ReinterpretBits(zero_bits_ + max_ulps_/2)),
3322  -Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_/2)),
3323  further_from_negative_zero_(-Floating::ReinterpretBits(
3324  zero_bits_ + max_ulps_ + 1 - max_ulps_/2)),
3325  close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)),
3326  further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)),
3327  infinity_(Floating::Infinity()),
3329  Floating::ReinterpretBits(infinity_bits_ - max_ulps_)),
3331  Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)),
3332  max_(Floating::Max()),
3333  nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)),
3334  nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {
3335  }
3336 
3337  void TestSize() {
3338  EXPECT_EQ(sizeof(RawType), sizeof(Bits));
3339  }
3340 
3341  // A battery of tests for FloatingEqMatcher::Matches.
3342  // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
3343  void TestMatches(
3344  testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) {
3345  Matcher<RawType> m1 = matcher_maker(0.0);
3346  EXPECT_TRUE(m1.Matches(-0.0));
3347  EXPECT_TRUE(m1.Matches(close_to_positive_zero_));
3348  EXPECT_TRUE(m1.Matches(close_to_negative_zero_));
3349  EXPECT_FALSE(m1.Matches(1.0));
3350 
3351  Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_);
3353 
3354  Matcher<RawType> m3 = matcher_maker(1.0);
3355  EXPECT_TRUE(m3.Matches(close_to_one_));
3356  EXPECT_FALSE(m3.Matches(further_from_one_));
3357 
3358  // Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above.
3359  EXPECT_FALSE(m3.Matches(0.0));
3360 
3361  Matcher<RawType> m4 = matcher_maker(-infinity_);
3362  EXPECT_TRUE(m4.Matches(-close_to_infinity_));
3363 
3364  Matcher<RawType> m5 = matcher_maker(infinity_);
3365  EXPECT_TRUE(m5.Matches(close_to_infinity_));
3366 
3367  // This is interesting as the representations of infinity_ and nan1_
3368  // are only 1 DLP apart.
3369  EXPECT_FALSE(m5.Matches(nan1_));
3370 
3371  // matcher_maker can produce a Matcher<const RawType&>, which is needed in
3372  // some cases.
3373  Matcher<const RawType&> m6 = matcher_maker(0.0);
3374  EXPECT_TRUE(m6.Matches(-0.0));
3375  EXPECT_TRUE(m6.Matches(close_to_positive_zero_));
3376  EXPECT_FALSE(m6.Matches(1.0));
3377 
3378  // matcher_maker can produce a Matcher<RawType&>, which is needed in some
3379  // cases.
3380  Matcher<RawType&> m7 = matcher_maker(0.0);
3381  RawType x = 0.0;
3382  EXPECT_TRUE(m7.Matches(x));
3383  x = 0.01f;
3384  EXPECT_FALSE(m7.Matches(x));
3385  }
3386 
3387  // Pre-calculated numbers to be used by the tests.
3388 
3389  const Bits max_ulps_;
3390 
3391  const Bits zero_bits_; // The bits that represent 0.0.
3392  const Bits one_bits_; // The bits that represent 1.0.
3393  const Bits infinity_bits_; // The bits that represent +infinity.
3394 
3395  // Some numbers close to 0.0.
3399 
3400  // Some numbers close to 1.0.
3401  const RawType close_to_one_;
3402  const RawType further_from_one_;
3403 
3404  // Some numbers close to +infinity.
3405  const RawType infinity_;
3406  const RawType close_to_infinity_;
3407  const RawType further_from_infinity_;
3408 
3409  // Maximum representable value that's not infinity.
3410  const RawType max_;
3411 
3412  // Some NaNs.
3413  const RawType nan1_;
3414  const RawType nan2_;
3415 };
3416 
3417 // Tests floating-point matchers with fixed epsilons.
3418 template <typename RawType>
3419 class FloatingPointNearTest : public FloatingPointTest<RawType> {
3420  protected:
3421  typedef FloatingPointTest<RawType> ParentType;
3422 
3423  // A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon.
3424  // matcher_maker is a pointer to a function which creates a FloatingEqMatcher.
3425  void TestNearMatches(
3427  (*matcher_maker)(RawType, RawType)) {
3428  Matcher<RawType> m1 = matcher_maker(0.0, 0.0);
3429  EXPECT_TRUE(m1.Matches(0.0));
3430  EXPECT_TRUE(m1.Matches(-0.0));
3433  EXPECT_FALSE(m1.Matches(1.0));
3434 
3435  Matcher<RawType> m2 = matcher_maker(0.0, 1.0);
3436  EXPECT_TRUE(m2.Matches(0.0));
3437  EXPECT_TRUE(m2.Matches(-0.0));
3438  EXPECT_TRUE(m2.Matches(1.0));
3439  EXPECT_TRUE(m2.Matches(-1.0));
3442 
3443  // Check that inf matches inf, regardless of the of the specified max
3444  // absolute error.
3445  Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0);
3446  EXPECT_TRUE(m3.Matches(ParentType::infinity_));
3448  EXPECT_FALSE(m3.Matches(-ParentType::infinity_));
3449 
3450  Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0);
3451  EXPECT_TRUE(m4.Matches(-ParentType::infinity_));
3453  EXPECT_FALSE(m4.Matches(ParentType::infinity_));
3454 
3455  // Test various overflow scenarios.
3456  Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_);
3457  EXPECT_TRUE(m5.Matches(ParentType::max_));
3458  EXPECT_FALSE(m5.Matches(-ParentType::max_));
3459 
3460  Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_);
3461  EXPECT_FALSE(m6.Matches(ParentType::max_));
3462  EXPECT_TRUE(m6.Matches(-ParentType::max_));
3463 
3464  Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0);
3465  EXPECT_TRUE(m7.Matches(ParentType::max_));
3466  EXPECT_FALSE(m7.Matches(-ParentType::max_));
3467 
3468  Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0);
3469  EXPECT_FALSE(m8.Matches(ParentType::max_));
3470  EXPECT_TRUE(m8.Matches(-ParentType::max_));
3471 
3472  // The difference between max() and -max() normally overflows to infinity,
3473  // but it should still match if the max_abs_error is also infinity.
3474  Matcher<RawType> m9 = matcher_maker(
3476  EXPECT_TRUE(m8.Matches(-ParentType::max_));
3477 
3478  // matcher_maker can produce a Matcher<const RawType&>, which is needed in
3479  // some cases.
3480  Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0);
3481  EXPECT_TRUE(m10.Matches(-0.0));
3484 
3485  // matcher_maker can produce a Matcher<RawType&>, which is needed in some
3486  // cases.
3487  Matcher<RawType&> m11 = matcher_maker(0.0, 1.0);
3488  RawType x = 0.0;
3489  EXPECT_TRUE(m11.Matches(x));
3490  x = 1.0f;
3491  EXPECT_TRUE(m11.Matches(x));
3492  x = -1.0f;
3493  EXPECT_TRUE(m11.Matches(x));
3494  x = 1.1f;
3495  EXPECT_FALSE(m11.Matches(x));
3496  x = -1.1f;
3497  EXPECT_FALSE(m11.Matches(x));
3498  }
3499 };
3500 
3501 // Instantiate FloatingPointTest for testing floats.
3502 typedef FloatingPointTest<float> FloatTest;
3503 
3504 TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) {
3505  TestMatches(&FloatEq);
3506 }
3507 
3508 TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) {
3509  TestMatches(&NanSensitiveFloatEq);
3510 }
3511 
3512 TEST_F(FloatTest, FloatEqCannotMatchNaN) {
3513  // FloatEq never matches NaN.
3514  Matcher<float> m = FloatEq(nan1_);
3515  EXPECT_FALSE(m.Matches(nan1_));
3516  EXPECT_FALSE(m.Matches(nan2_));
3517  EXPECT_FALSE(m.Matches(1.0));
3518 }
3519 
3520 TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) {
3521  // NanSensitiveFloatEq will match NaN.
3522  Matcher<float> m = NanSensitiveFloatEq(nan1_);
3523  EXPECT_TRUE(m.Matches(nan1_));
3524  EXPECT_TRUE(m.Matches(nan2_));
3525  EXPECT_FALSE(m.Matches(1.0));
3526 }
3527 
3528 TEST_F(FloatTest, FloatEqCanDescribeSelf) {
3529  Matcher<float> m1 = FloatEq(2.0f);
3530  EXPECT_EQ("is approximately 2", Describe(m1));
3531  EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
3532 
3533  Matcher<float> m2 = FloatEq(0.5f);
3534  EXPECT_EQ("is approximately 0.5", Describe(m2));
3535  EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
3536 
3537  Matcher<float> m3 = FloatEq(nan1_);
3538  EXPECT_EQ("never matches", Describe(m3));
3539  EXPECT_EQ("is anything", DescribeNegation(m3));
3540 }
3541 
3542 TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) {
3543  Matcher<float> m1 = NanSensitiveFloatEq(2.0f);
3544  EXPECT_EQ("is approximately 2", Describe(m1));
3545  EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
3546 
3547  Matcher<float> m2 = NanSensitiveFloatEq(0.5f);
3548  EXPECT_EQ("is approximately 0.5", Describe(m2));
3549  EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
3550 
3551  Matcher<float> m3 = NanSensitiveFloatEq(nan1_);
3552  EXPECT_EQ("is NaN", Describe(m3));
3553  EXPECT_EQ("isn't NaN", DescribeNegation(m3));
3554 }
3555 
3556 // Instantiate FloatingPointTest for testing floats with a user-specified
3557 // max absolute error.
3558 typedef FloatingPointNearTest<float> FloatNearTest;
3559 
3560 TEST_F(FloatNearTest, FloatNearMatches) {
3561  TestNearMatches(&FloatNear);
3562 }
3563 
3564 TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) {
3565  TestNearMatches(&NanSensitiveFloatNear);
3566 }
3567 
3568 TEST_F(FloatNearTest, FloatNearCanDescribeSelf) {
3569  Matcher<float> m1 = FloatNear(2.0f, 0.5f);
3570  EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
3571  EXPECT_EQ(
3572  "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3573 
3574  Matcher<float> m2 = FloatNear(0.5f, 0.5f);
3575  EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3576  EXPECT_EQ(
3577  "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3578 
3579  Matcher<float> m3 = FloatNear(nan1_, 0.0);
3580  EXPECT_EQ("never matches", Describe(m3));
3581  EXPECT_EQ("is anything", DescribeNegation(m3));
3582 }
3583 
3584 TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) {
3585  Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f);
3586  EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
3587  EXPECT_EQ(
3588  "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3589 
3590  Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f);
3591  EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3592  EXPECT_EQ(
3593  "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3594 
3595  Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f);
3596  EXPECT_EQ("is NaN", Describe(m3));
3597  EXPECT_EQ("isn't NaN", DescribeNegation(m3));
3598 }
3599 
3600 TEST_F(FloatNearTest, FloatNearCannotMatchNaN) {
3601  // FloatNear never matches NaN.
3602  Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f);
3603  EXPECT_FALSE(m.Matches(nan1_));
3604  EXPECT_FALSE(m.Matches(nan2_));
3605  EXPECT_FALSE(m.Matches(1.0));
3606 }
3607 
3608 TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) {
3609  // NanSensitiveFloatNear will match NaN.
3610  Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f);
3611  EXPECT_TRUE(m.Matches(nan1_));
3612  EXPECT_TRUE(m.Matches(nan2_));
3613  EXPECT_FALSE(m.Matches(1.0));
3614 }
3615 
3616 // Instantiate FloatingPointTest for testing doubles.
3617 typedef FloatingPointTest<double> DoubleTest;
3618 
3619 TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) {
3620  TestMatches(&DoubleEq);
3621 }
3622 
3623 TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) {
3624  TestMatches(&NanSensitiveDoubleEq);
3625 }
3626 
3627 TEST_F(DoubleTest, DoubleEqCannotMatchNaN) {
3628  // DoubleEq never matches NaN.
3629  Matcher<double> m = DoubleEq(nan1_);
3630  EXPECT_FALSE(m.Matches(nan1_));
3631  EXPECT_FALSE(m.Matches(nan2_));
3632  EXPECT_FALSE(m.Matches(1.0));
3633 }
3634 
3635 TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) {
3636  // NanSensitiveDoubleEq will match NaN.
3637  Matcher<double> m = NanSensitiveDoubleEq(nan1_);
3638  EXPECT_TRUE(m.Matches(nan1_));
3639  EXPECT_TRUE(m.Matches(nan2_));
3640  EXPECT_FALSE(m.Matches(1.0));
3641 }
3642 
3643 TEST_F(DoubleTest, DoubleEqCanDescribeSelf) {
3644  Matcher<double> m1 = DoubleEq(2.0);
3645  EXPECT_EQ("is approximately 2", Describe(m1));
3646  EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
3647 
3648  Matcher<double> m2 = DoubleEq(0.5);
3649  EXPECT_EQ("is approximately 0.5", Describe(m2));
3650  EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
3651 
3652  Matcher<double> m3 = DoubleEq(nan1_);
3653  EXPECT_EQ("never matches", Describe(m3));
3654  EXPECT_EQ("is anything", DescribeNegation(m3));
3655 }
3656 
3657 TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) {
3658  Matcher<double> m1 = NanSensitiveDoubleEq(2.0);
3659  EXPECT_EQ("is approximately 2", Describe(m1));
3660  EXPECT_EQ("isn't approximately 2", DescribeNegation(m1));
3661 
3662  Matcher<double> m2 = NanSensitiveDoubleEq(0.5);
3663  EXPECT_EQ("is approximately 0.5", Describe(m2));
3664  EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2));
3665 
3666  Matcher<double> m3 = NanSensitiveDoubleEq(nan1_);
3667  EXPECT_EQ("is NaN", Describe(m3));
3668  EXPECT_EQ("isn't NaN", DescribeNegation(m3));
3669 }
3670 
3671 // Instantiate FloatingPointTest for testing floats with a user-specified
3672 // max absolute error.
3673 typedef FloatingPointNearTest<double> DoubleNearTest;
3674 
3675 TEST_F(DoubleNearTest, DoubleNearMatches) {
3676  TestNearMatches(&DoubleNear);
3677 }
3678 
3679 TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) {
3680  TestNearMatches(&NanSensitiveDoubleNear);
3681 }
3682 
3683 TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) {
3684  Matcher<double> m1 = DoubleNear(2.0, 0.5);
3685  EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
3686  EXPECT_EQ(
3687  "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3688 
3689  Matcher<double> m2 = DoubleNear(0.5, 0.5);
3690  EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3691  EXPECT_EQ(
3692  "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3693 
3694  Matcher<double> m3 = DoubleNear(nan1_, 0.0);
3695  EXPECT_EQ("never matches", Describe(m3));
3696  EXPECT_EQ("is anything", DescribeNegation(m3));
3697 }
3698 
3699 TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) {
3700  EXPECT_EQ("", Explain(DoubleNear(2.0, 0.1), 2.05));
3701  EXPECT_EQ("which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2));
3702  EXPECT_EQ("which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7));
3703 
3704  const std::string explanation =
3705  Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10);
3706  // Different C++ implementations may print floating-point numbers
3707  // slightly differently.
3708  EXPECT_TRUE(explanation == "which is 1.2e-10 from 2.1" || // GCC
3709  explanation == "which is 1.2e-010 from 2.1") // MSVC
3710  << " where explanation is \"" << explanation << "\".";
3711 }
3712 
3713 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) {
3714  Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5);
3715  EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1));
3716  EXPECT_EQ(
3717  "isn't approximately 2 (absolute error > 0.5)", DescribeNegation(m1));
3718 
3719  Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5);
3720  EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2));
3721  EXPECT_EQ(
3722  "isn't approximately 0.5 (absolute error > 0.5)", DescribeNegation(m2));
3723 
3724  Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1);
3725  EXPECT_EQ("is NaN", Describe(m3));
3726  EXPECT_EQ("isn't NaN", DescribeNegation(m3));
3727 }
3728 
3729 TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) {
3730  // DoubleNear never matches NaN.
3731  Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1);
3732  EXPECT_FALSE(m.Matches(nan1_));
3733  EXPECT_FALSE(m.Matches(nan2_));
3734  EXPECT_FALSE(m.Matches(1.0));
3735 }
3736 
3737 TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) {
3738  // NanSensitiveDoubleNear will match NaN.
3739  Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1);
3740  EXPECT_TRUE(m.Matches(nan1_));
3741  EXPECT_TRUE(m.Matches(nan2_));
3742  EXPECT_FALSE(m.Matches(1.0));
3743 }
3744 
3745 TEST(PointeeTest, RawPointer) {
3746  const Matcher<int*> m = Pointee(Ge(0));
3747 
3748  int n = 1;
3749  EXPECT_TRUE(m.Matches(&n));
3750  n = -1;
3751  EXPECT_FALSE(m.Matches(&n));
3752  EXPECT_FALSE(m.Matches(nullptr));
3753 }
3754 
3755 TEST(PointeeTest, RawPointerToConst) {
3756  const Matcher<const double*> m = Pointee(Ge(0));
3757 
3758  double x = 1;
3759  EXPECT_TRUE(m.Matches(&x));
3760  x = -1;
3761  EXPECT_FALSE(m.Matches(&x));
3762  EXPECT_FALSE(m.Matches(nullptr));
3763 }
3764 
3765 TEST(PointeeTest, ReferenceToConstRawPointer) {
3766  const Matcher<int* const &> m = Pointee(Ge(0));
3767 
3768  int n = 1;
3769  EXPECT_TRUE(m.Matches(&n));
3770  n = -1;
3771  EXPECT_FALSE(m.Matches(&n));
3772  EXPECT_FALSE(m.Matches(nullptr));
3773 }
3774 
3775 TEST(PointeeTest, ReferenceToNonConstRawPointer) {
3776  const Matcher<double* &> m = Pointee(Ge(0));
3777 
3778  double x = 1.0;
3779  double* p = &x;
3780  EXPECT_TRUE(m.Matches(p));
3781  x = -1;
3782  EXPECT_FALSE(m.Matches(p));
3783  p = nullptr;
3784  EXPECT_FALSE(m.Matches(p));
3785 }
3786 
3787 TEST(PointeeTest, SmartPointer) {
3788  const Matcher<std::unique_ptr<int>> m = Pointee(Ge(0));
3789 
3790  std::unique_ptr<int> n(new int(1));
3791  EXPECT_TRUE(m.Matches(n));
3792 }
3793 
3794 TEST(PointeeTest, SmartPointerToConst) {
3795  const Matcher<std::unique_ptr<const int>> m = Pointee(Ge(0));
3796 
3797  // There's no implicit conversion from unique_ptr<int> to const
3798  // unique_ptr<const int>, so we must pass a unique_ptr<const int> into the
3799  // matcher.
3800  std::unique_ptr<const int> n(new int(1));
3801  EXPECT_TRUE(m.Matches(n));
3802 }
3803 
3804 TEST(PointerTest, RawPointer) {
3805  int n = 1;
3806  const Matcher<int*> m = Pointer(Eq(&n));
3807 
3808  EXPECT_TRUE(m.Matches(&n));
3809 
3810  int* p = nullptr;
3811  EXPECT_FALSE(m.Matches(p));
3812  EXPECT_FALSE(m.Matches(nullptr));
3813 }
3814 
3815 TEST(PointerTest, RawPointerToConst) {
3816  int n = 1;
3817  const Matcher<const int*> m = Pointer(Eq(&n));
3818 
3819  EXPECT_TRUE(m.Matches(&n));
3820 
3821  int* p = nullptr;
3822  EXPECT_FALSE(m.Matches(p));
3823  EXPECT_FALSE(m.Matches(nullptr));
3824 }
3825 
3826 TEST(PointerTest, SmartPointer) {
3827  std::unique_ptr<int> n(new int(10));
3828  int* raw_n = n.get();
3829  const Matcher<std::unique_ptr<int>> m = Pointer(Eq(raw_n));
3830 
3831  EXPECT_TRUE(m.Matches(n));
3832 }
3833 
3834 TEST(PointerTest, SmartPointerToConst) {
3835  std::unique_ptr<const int> n(new int(10));
3836  const int* raw_n = n.get();
3837  const Matcher<std::unique_ptr<const int>> m = Pointer(Eq(raw_n));
3838 
3839  // There's no implicit conversion from unique_ptr<int> to const
3840  // unique_ptr<const int>, so we must pass a unique_ptr<const int> into the
3841  // matcher.
3842  std::unique_ptr<const int> p(new int(10));
3843  EXPECT_FALSE(m.Matches(p));
3844 }
3845 
3846 TEST(AddressTest, NonConst) {
3847  int n = 1;
3848  const Matcher<int> m = Address(Eq(&n));
3849 
3850  EXPECT_TRUE(m.Matches(n));
3851 
3852  int other = 5;
3853 
3854  EXPECT_FALSE(m.Matches(other));
3855 
3856  int& n_ref = n;
3857 
3858  EXPECT_TRUE(m.Matches(n_ref));
3859 }
3860 
3861 TEST(AddressTest, Const) {
3862  const int n = 1;
3863  const Matcher<int> m = Address(Eq(&n));
3864 
3865  EXPECT_TRUE(m.Matches(n));
3866 
3867  int other = 5;
3868 
3869  EXPECT_FALSE(m.Matches(other));
3870 }
3871 
3872 TEST(AddressTest, MatcherDoesntCopy) {
3873  std::unique_ptr<int> n(new int(1));
3874  const Matcher<std::unique_ptr<int>> m = Address(Eq(&n));
3875 
3876  EXPECT_TRUE(m.Matches(n));
3877 }
3878 
3879 TEST(AddressTest, Describe) {
3880  Matcher<int> matcher = Address(_);
3881  EXPECT_EQ("has address that is anything", Describe(matcher));
3882  EXPECT_EQ("does not have address that is anything",
3883  DescribeNegation(matcher));
3884 }
3885 
3886 MATCHER_P(FieldIIs, inner_matcher, "") {
3887  return ExplainMatchResult(inner_matcher, arg.i, result_listener);
3888 }
3889 
3890 #if GTEST_HAS_RTTI
3891 TEST(WhenDynamicCastToTest, SameType) {
3892  Derived derived;
3893  derived.i = 4;
3894 
3895  // Right type. A pointer is passed down.
3896  Base* as_base_ptr = &derived;
3897  EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Not(IsNull())));
3898  EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(4))));
3899  EXPECT_THAT(as_base_ptr,
3900  Not(WhenDynamicCastTo<Derived*>(Pointee(FieldIIs(5)))));
3901 }
3902 
3903 TEST(WhenDynamicCastToTest, WrongTypes) {
3904  Base base;
3905  Derived derived;
3906  OtherDerived other_derived;
3907 
3908  // Wrong types. NULL is passed.
3909  EXPECT_THAT(&base, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
3910  EXPECT_THAT(&base, WhenDynamicCastTo<Derived*>(IsNull()));
3911  Base* as_base_ptr = &derived;
3912  EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<OtherDerived*>(Pointee(_))));
3913  EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<OtherDerived*>(IsNull()));
3914  as_base_ptr = &other_derived;
3915  EXPECT_THAT(as_base_ptr, Not(WhenDynamicCastTo<Derived*>(Pointee(_))));
3916  EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));
3917 }
3918 
3919 TEST(WhenDynamicCastToTest, AlreadyNull) {
3920  // Already NULL.
3921  Base* as_base_ptr = nullptr;
3922  EXPECT_THAT(as_base_ptr, WhenDynamicCastTo<Derived*>(IsNull()));
3923 }
3924 
3925 struct AmbiguousCastTypes {
3926  class VirtualDerived : public virtual Base {};
3927  class DerivedSub1 : public VirtualDerived {};
3928  class DerivedSub2 : public VirtualDerived {};
3929  class ManyDerivedInHierarchy : public DerivedSub1, public DerivedSub2 {};
3930 };
3931 
3932 TEST(WhenDynamicCastToTest, AmbiguousCast) {
3933  AmbiguousCastTypes::DerivedSub1 sub1;
3934  AmbiguousCastTypes::ManyDerivedInHierarchy many_derived;
3935  // Multiply derived from Base. dynamic_cast<> returns NULL.
3936  Base* as_base_ptr =
3937  static_cast<AmbiguousCastTypes::DerivedSub1*>(&many_derived);
3938  EXPECT_THAT(as_base_ptr,
3939  WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(IsNull()));
3940  as_base_ptr = &sub1;
3941  EXPECT_THAT(
3942  as_base_ptr,
3943  WhenDynamicCastTo<AmbiguousCastTypes::VirtualDerived*>(Not(IsNull())));
3944 }
3945 
3946 TEST(WhenDynamicCastToTest, Describe) {
3947  Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
3948  const std::string prefix =
3949  "when dynamic_cast to " + internal::GetTypeName<Derived*>() + ", ";
3950  EXPECT_EQ(prefix + "points to a value that is anything", Describe(matcher));
3951  EXPECT_EQ(prefix + "does not point to a value that is anything",
3952  DescribeNegation(matcher));
3953 }
3954 
3955 TEST(WhenDynamicCastToTest, Explain) {
3956  Matcher<Base*> matcher = WhenDynamicCastTo<Derived*>(Pointee(_));
3957  Base* null = nullptr;
3958  EXPECT_THAT(Explain(matcher, null), HasSubstr("NULL"));
3959  Derived derived;
3960  EXPECT_TRUE(matcher.Matches(&derived));
3961  EXPECT_THAT(Explain(matcher, &derived), HasSubstr("which points to "));
3962 
3963  // With references, the matcher itself can fail. Test for that one.
3964  Matcher<const Base&> ref_matcher = WhenDynamicCastTo<const OtherDerived&>(_);
3965  EXPECT_THAT(Explain(ref_matcher, derived),
3966  HasSubstr("which cannot be dynamic_cast"));
3967 }
3968 
3969 TEST(WhenDynamicCastToTest, GoodReference) {
3970  Derived derived;
3971  derived.i = 4;
3972  Base& as_base_ref = derived;
3973  EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(FieldIIs(4)));
3974  EXPECT_THAT(as_base_ref, WhenDynamicCastTo<const Derived&>(Not(FieldIIs(5))));
3975 }
3976 
3977 TEST(WhenDynamicCastToTest, BadReference) {
3978  Derived derived;
3979  Base& as_base_ref = derived;
3980  EXPECT_THAT(as_base_ref, Not(WhenDynamicCastTo<const OtherDerived&>(_)));
3981 }
3982 #endif // GTEST_HAS_RTTI
3983 
3984 // Minimal const-propagating pointer.
3985 template <typename T>
3986 class ConstPropagatingPtr {
3987  public:
3988  typedef T element_type;
3989 
3990  ConstPropagatingPtr() : val_() {}
3991  explicit ConstPropagatingPtr(T* t) : val_(t) {}
3992  ConstPropagatingPtr(const ConstPropagatingPtr& other) : val_(other.val_) {}
3993 
3994  T* get() { return val_; }
3995  T& operator*() { return *val_; }
3996  // Most smart pointers return non-const T* and T& from the next methods.
3997  const T* get() const { return val_; }
3998  const T& operator*() const { return *val_; }
3999 
4000  private:
4002 };
4003 
4004 TEST(PointeeTest, WorksWithConstPropagatingPointers) {
4005  const Matcher< ConstPropagatingPtr<int> > m = Pointee(Lt(5));
4006  int three = 3;
4007  const ConstPropagatingPtr<int> co(&three);
4008  ConstPropagatingPtr<int> o(&three);
4009  EXPECT_TRUE(m.Matches(o));
4010  EXPECT_TRUE(m.Matches(co));
4011  *o = 6;
4012  EXPECT_FALSE(m.Matches(o));
4013  EXPECT_FALSE(m.Matches(ConstPropagatingPtr<int>()));
4014 }
4015 
4016 TEST(PointeeTest, NeverMatchesNull) {
4017  const Matcher<const char*> m = Pointee(_);
4018  EXPECT_FALSE(m.Matches(nullptr));
4019 }
4020 
4021 // Tests that we can write Pointee(value) instead of Pointee(Eq(value)).
4022 TEST(PointeeTest, MatchesAgainstAValue) {
4023  const Matcher<int*> m = Pointee(5);
4024 
4025  int n = 5;
4026  EXPECT_TRUE(m.Matches(&n));
4027  n = -1;
4028  EXPECT_FALSE(m.Matches(&n));
4029  EXPECT_FALSE(m.Matches(nullptr));
4030 }
4031 
4032 TEST(PointeeTest, CanDescribeSelf) {
4033  const Matcher<int*> m = Pointee(Gt(3));
4034  EXPECT_EQ("points to a value that is > 3", Describe(m));
4035  EXPECT_EQ("does not point to a value that is > 3",
4036  DescribeNegation(m));
4037 }
4038 
4039 TEST(PointeeTest, CanExplainMatchResult) {
4040  const Matcher<const std::string*> m = Pointee(StartsWith("Hi"));
4041 
4042  EXPECT_EQ("", Explain(m, static_cast<const std::string*>(nullptr)));
4043 
4044  const Matcher<long*> m2 = Pointee(GreaterThan(1)); // NOLINT
4045  long n = 3; // NOLINT
4046  EXPECT_EQ("which points to 3" + OfType("long") + ", which is 2 more than 1",
4047  Explain(m2, &n));
4048 }
4049 
4050 TEST(PointeeTest, AlwaysExplainsPointee) {
4051  const Matcher<int*> m = Pointee(0);
4052  int n = 42;
4053  EXPECT_EQ("which points to 42" + OfType("int"), Explain(m, &n));
4054 }
4055 
4056 // An uncopyable class.
4057 class Uncopyable {
4058  public:
4059  Uncopyable() : value_(-1) {}
4060  explicit Uncopyable(int a_value) : value_(a_value) {}
4061 
4062  int value() const { return value_; }
4063  void set_value(int i) { value_ = i; }
4064 
4065  private:
4066  int value_;
4067  GTEST_DISALLOW_COPY_AND_ASSIGN_(Uncopyable);
4068 };
4069 
4070 // Returns true if and only if x.value() is positive.
4071 bool ValueIsPositive(const Uncopyable& x) { return x.value() > 0; }
4072 
4073 MATCHER_P(UncopyableIs, inner_matcher, "") {
4074  return ExplainMatchResult(inner_matcher, arg.value(), result_listener);
4075 }
4076 
4077 // A user-defined struct for testing Field().
4078 struct AStruct {
4079  AStruct() : x(0), y(1.0), z(5), p(nullptr) {}
4080  AStruct(const AStruct& rhs)
4081  : x(rhs.x), y(rhs.y), z(rhs.z.value()), p(rhs.p) {}
4082 
4083  int x; // A non-const field.
4084  const double y; // A const field.
4085  Uncopyable z; // An uncopyable field.
4086  const char* p; // A pointer field.
4087 };
4088 
4089 // A derived struct for testing Field().
4090 struct DerivedStruct : public AStruct {
4091  char ch;
4092 };
4093 
4094 // Tests that Field(&Foo::field, ...) works when field is non-const.
4095 TEST(FieldTest, WorksForNonConstField) {
4096  Matcher<AStruct> m = Field(&AStruct::x, Ge(0));
4097  Matcher<AStruct> m_with_name = Field("x", &AStruct::x, Ge(0));
4098 
4099  AStruct a;
4100  EXPECT_TRUE(m.Matches(a));
4101  EXPECT_TRUE(m_with_name.Matches(a));
4102  a.x = -1;
4103  EXPECT_FALSE(m.Matches(a));
4104  EXPECT_FALSE(m_with_name.Matches(a));
4105 }
4106 
4107 // Tests that Field(&Foo::field, ...) works when field is const.
4108 TEST(FieldTest, WorksForConstField) {
4109  AStruct a;
4110 
4111  Matcher<AStruct> m = Field(&AStruct::y, Ge(0.0));
4112  Matcher<AStruct> m_with_name = Field("y", &AStruct::y, Ge(0.0));
4113  EXPECT_TRUE(m.Matches(a));
4114  EXPECT_TRUE(m_with_name.Matches(a));
4115  m = Field(&AStruct::y, Le(0.0));
4116  m_with_name = Field("y", &AStruct::y, Le(0.0));
4117  EXPECT_FALSE(m.Matches(a));
4118  EXPECT_FALSE(m_with_name.Matches(a));
4119 }
4120 
4121 // Tests that Field(&Foo::field, ...) works when field is not copyable.
4122 TEST(FieldTest, WorksForUncopyableField) {
4123  AStruct a;
4124 
4125  Matcher<AStruct> m = Field(&AStruct::z, Truly(ValueIsPositive));
4126  EXPECT_TRUE(m.Matches(a));
4127  m = Field(&AStruct::z, Not(Truly(ValueIsPositive)));
4128  EXPECT_FALSE(m.Matches(a));
4129 }
4130 
4131 // Tests that Field(&Foo::field, ...) works when field is a pointer.
4132 TEST(FieldTest, WorksForPointerField) {
4133  // Matching against NULL.
4134  Matcher<AStruct> m = Field(&AStruct::p, static_cast<const char*>(nullptr));
4135  AStruct a;
4136  EXPECT_TRUE(m.Matches(a));
4137  a.p = "hi";
4138  EXPECT_FALSE(m.Matches(a));
4139 
4140  // Matching a pointer that is not NULL.
4141  m = Field(&AStruct::p, StartsWith("hi"));
4142  a.p = "hill";
4143  EXPECT_TRUE(m.Matches(a));
4144  a.p = "hole";
4145  EXPECT_FALSE(m.Matches(a));
4146 }
4147 
4148 // Tests that Field() works when the object is passed by reference.
4149 TEST(FieldTest, WorksForByRefArgument) {
4150  Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
4151 
4152  AStruct a;
4153  EXPECT_TRUE(m.Matches(a));
4154  a.x = -1;
4155  EXPECT_FALSE(m.Matches(a));
4156 }
4157 
4158 // Tests that Field(&Foo::field, ...) works when the argument's type
4159 // is a sub-type of Foo.
4160 TEST(FieldTest, WorksForArgumentOfSubType) {
4161  // Note that the matcher expects DerivedStruct but we say AStruct
4162  // inside Field().
4163  Matcher<const DerivedStruct&> m = Field(&AStruct::x, Ge(0));
4164 
4165  DerivedStruct d;
4166  EXPECT_TRUE(m.Matches(d));
4167  d.x = -1;
4168  EXPECT_FALSE(m.Matches(d));
4169 }
4170 
4171 // Tests that Field(&Foo::field, m) works when field's type and m's
4172 // argument type are compatible but not the same.
4173 TEST(FieldTest, WorksForCompatibleMatcherType) {
4174  // The field is an int, but the inner matcher expects a signed char.
4175  Matcher<const AStruct&> m = Field(&AStruct::x,
4176  Matcher<signed char>(Ge(0)));
4177 
4178  AStruct a;
4179  EXPECT_TRUE(m.Matches(a));
4180  a.x = -1;
4181  EXPECT_FALSE(m.Matches(a));
4182 }
4183 
4184 // Tests that Field() can describe itself.
4185 TEST(FieldTest, CanDescribeSelf) {
4186  Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
4187 
4188  EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
4189  EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
4190 }
4191 
4192 TEST(FieldTest, CanDescribeSelfWithFieldName) {
4193  Matcher<const AStruct&> m = Field("field_name", &AStruct::x, Ge(0));
4194 
4195  EXPECT_EQ("is an object whose field `field_name` is >= 0", Describe(m));
4196  EXPECT_EQ("is an object whose field `field_name` isn't >= 0",
4197  DescribeNegation(m));
4198 }
4199 
4200 // Tests that Field() can explain the match result.
4201 TEST(FieldTest, CanExplainMatchResult) {
4202  Matcher<const AStruct&> m = Field(&AStruct::x, Ge(0));
4203 
4204  AStruct a;
4205  a.x = 1;
4206  EXPECT_EQ("whose given field is 1" + OfType("int"), Explain(m, a));
4207 
4208  m = Field(&AStruct::x, GreaterThan(0));
4209  EXPECT_EQ(
4210  "whose given field is 1" + OfType("int") + ", which is 1 more than 0",
4211  Explain(m, a));
4212 }
4213 
4214 TEST(FieldTest, CanExplainMatchResultWithFieldName) {
4215  Matcher<const AStruct&> m = Field("field_name", &AStruct::x, Ge(0));
4216 
4217  AStruct a;
4218  a.x = 1;
4219  EXPECT_EQ("whose field `field_name` is 1" + OfType("int"), Explain(m, a));
4220 
4221  m = Field("field_name", &AStruct::x, GreaterThan(0));
4222  EXPECT_EQ("whose field `field_name` is 1" + OfType("int") +
4223  ", which is 1 more than 0",
4224  Explain(m, a));
4225 }
4226 
4227 // Tests that Field() works when the argument is a pointer to const.
4228 TEST(FieldForPointerTest, WorksForPointerToConst) {
4229  Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
4230 
4231  AStruct a;
4232  EXPECT_TRUE(m.Matches(&a));
4233  a.x = -1;
4234  EXPECT_FALSE(m.Matches(&a));
4235 }
4236 
4237 // Tests that Field() works when the argument is a pointer to non-const.
4238 TEST(FieldForPointerTest, WorksForPointerToNonConst) {
4239  Matcher<AStruct*> m = Field(&AStruct::x, Ge(0));
4240 
4241  AStruct a;
4242  EXPECT_TRUE(m.Matches(&a));
4243  a.x = -1;
4244  EXPECT_FALSE(m.Matches(&a));
4245 }
4246 
4247 // Tests that Field() works when the argument is a reference to a const pointer.
4248 TEST(FieldForPointerTest, WorksForReferenceToConstPointer) {
4249  Matcher<AStruct* const&> m = Field(&AStruct::x, Ge(0));
4250 
4251  AStruct a;
4252  EXPECT_TRUE(m.Matches(&a));
4253  a.x = -1;
4254  EXPECT_FALSE(m.Matches(&a));
4255 }
4256 
4257 // Tests that Field() does not match the NULL pointer.
4258 TEST(FieldForPointerTest, DoesNotMatchNull) {
4259  Matcher<const AStruct*> m = Field(&AStruct::x, _);
4260  EXPECT_FALSE(m.Matches(nullptr));
4261 }
4262 
4263 // Tests that Field(&Foo::field, ...) works when the argument's type
4264 // is a sub-type of const Foo*.
4265 TEST(FieldForPointerTest, WorksForArgumentOfSubType) {
4266  // Note that the matcher expects DerivedStruct but we say AStruct
4267  // inside Field().
4268  Matcher<DerivedStruct*> m = Field(&AStruct::x, Ge(0));
4269 
4270  DerivedStruct d;
4271  EXPECT_TRUE(m.Matches(&d));
4272  d.x = -1;
4273  EXPECT_FALSE(m.Matches(&d));
4274 }
4275 
4276 // Tests that Field() can describe itself when used to match a pointer.
4277 TEST(FieldForPointerTest, CanDescribeSelf) {
4278  Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
4279 
4280  EXPECT_EQ("is an object whose given field is >= 0", Describe(m));
4281  EXPECT_EQ("is an object whose given field isn't >= 0", DescribeNegation(m));
4282 }
4283 
4284 TEST(FieldForPointerTest, CanDescribeSelfWithFieldName) {
4285  Matcher<const AStruct*> m = Field("field_name", &AStruct::x, Ge(0));
4286 
4287  EXPECT_EQ("is an object whose field `field_name` is >= 0", Describe(m));
4288  EXPECT_EQ("is an object whose field `field_name` isn't >= 0",
4289  DescribeNegation(m));
4290 }
4291 
4292 // Tests that Field() can explain the result of matching a pointer.
4293 TEST(FieldForPointerTest, CanExplainMatchResult) {
4294  Matcher<const AStruct*> m = Field(&AStruct::x, Ge(0));
4295 
4296  AStruct a;
4297  a.x = 1;
4298  EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(nullptr)));
4299  EXPECT_EQ("which points to an object whose given field is 1" + OfType("int"),
4300  Explain(m, &a));
4301 
4302  m = Field(&AStruct::x, GreaterThan(0));
4303  EXPECT_EQ("which points to an object whose given field is 1" + OfType("int") +
4304  ", which is 1 more than 0", Explain(m, &a));
4305 }
4306 
4307 TEST(FieldForPointerTest, CanExplainMatchResultWithFieldName) {
4308  Matcher<const AStruct*> m = Field("field_name", &AStruct::x, Ge(0));
4309 
4310  AStruct a;
4311  a.x = 1;
4312  EXPECT_EQ("", Explain(m, static_cast<const AStruct*>(nullptr)));
4313  EXPECT_EQ(
4314  "which points to an object whose field `field_name` is 1" + OfType("int"),
4315  Explain(m, &a));
4316 
4317  m = Field("field_name", &AStruct::x, GreaterThan(0));
4318  EXPECT_EQ("which points to an object whose field `field_name` is 1" +
4319  OfType("int") + ", which is 1 more than 0",
4320  Explain(m, &a));
4321 }
4322 
4323 // A user-defined class for testing Property().
4324 class AClass {
4325  public:
4326  AClass() : n_(0) {}
4327 
4328  // A getter that returns a non-reference.
4329  int n() const { return n_; }
4330 
4331  void set_n(int new_n) { n_ = new_n; }
4332 
4333  // A getter that returns a reference to const.
4334  const std::string& s() const { return s_; }
4335 
4336  const std::string& s_ref() const & { return s_; }
4337 
4338  void set_s(const std::string& new_s) { s_ = new_s; }
4339 
4340  // A getter that returns a reference to non-const.
4341  double& x() const { return x_; }
4342 
4343  private:
4344  int n_;
4346 
4347  static double x_;
4348 };
4349 
4350 double AClass::x_ = 0.0;
4351 
4352 // A derived class for testing Property().
4353 class DerivedClass : public AClass {
4354  public:
4355  int k() const { return k_; }
4356  private:
4357  int k_;
4358 };
4359 
4360 // Tests that Property(&Foo::property, ...) works when property()
4361 // returns a non-reference.
4362 TEST(PropertyTest, WorksForNonReferenceProperty) {
4363  Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
4364  Matcher<const AClass&> m_with_name = Property("n", &AClass::n, Ge(0));
4365 
4366  AClass a;
4367  a.set_n(1);
4368  EXPECT_TRUE(m.Matches(a));
4369  EXPECT_TRUE(m_with_name.Matches(a));
4370 
4371  a.set_n(-1);
4372  EXPECT_FALSE(m.Matches(a));
4373  EXPECT_FALSE(m_with_name.Matches(a));
4374 }
4375 
4376 // Tests that Property(&Foo::property, ...) works when property()
4377 // returns a reference to const.
4378 TEST(PropertyTest, WorksForReferenceToConstProperty) {
4379  Matcher<const AClass&> m = Property(&AClass::s, StartsWith("hi"));
4380  Matcher<const AClass&> m_with_name =
4381  Property("s", &AClass::s, StartsWith("hi"));
4382 
4383  AClass a;
4384  a.set_s("hill");
4385  EXPECT_TRUE(m.Matches(a));
4386  EXPECT_TRUE(m_with_name.Matches(a));
4387 
4388  a.set_s("hole");
4389  EXPECT_FALSE(m.Matches(a));
4390  EXPECT_FALSE(m_with_name.Matches(a));
4391 }
4392 
4393 // Tests that Property(&Foo::property, ...) works when property() is
4394 // ref-qualified.
4395 TEST(PropertyTest, WorksForRefQualifiedProperty) {
4396  Matcher<const AClass&> m = Property(&AClass::s_ref, StartsWith("hi"));
4397  Matcher<const AClass&> m_with_name =
4398  Property("s", &AClass::s_ref, StartsWith("hi"));
4399 
4400  AClass a;
4401  a.set_s("hill");
4402  EXPECT_TRUE(m.Matches(a));
4403  EXPECT_TRUE(m_with_name.Matches(a));
4404 
4405  a.set_s("hole");
4406  EXPECT_FALSE(m.Matches(a));
4407  EXPECT_FALSE(m_with_name.Matches(a));
4408 }
4409 
4410 // Tests that Property(&Foo::property, ...) works when property()
4411 // returns a reference to non-const.
4412 TEST(PropertyTest, WorksForReferenceToNonConstProperty) {
4413  double x = 0.0;
4414  AClass a;
4415 
4416  Matcher<const AClass&> m = Property(&AClass::x, Ref(x));
4417  EXPECT_FALSE(m.Matches(a));
4418 
4419  m = Property(&AClass::x, Not(Ref(x)));
4420  EXPECT_TRUE(m.Matches(a));
4421 }
4422 
4423 // Tests that Property(&Foo::property, ...) works when the argument is
4424 // passed by value.
4425 TEST(PropertyTest, WorksForByValueArgument) {
4426  Matcher<AClass> m = Property(&AClass::s, StartsWith("hi"));
4427 
4428  AClass a;
4429  a.set_s("hill");
4430  EXPECT_TRUE(m.Matches(a));
4431 
4432  a.set_s("hole");
4433  EXPECT_FALSE(m.Matches(a));
4434 }
4435 
4436 // Tests that Property(&Foo::property, ...) works when the argument's
4437 // type is a sub-type of Foo.
4438 TEST(PropertyTest, WorksForArgumentOfSubType) {
4439  // The matcher expects a DerivedClass, but inside the Property() we
4440  // say AClass.
4441  Matcher<const DerivedClass&> m = Property(&AClass::n, Ge(0));
4442 
4443  DerivedClass d;
4444  d.set_n(1);
4445  EXPECT_TRUE(m.Matches(d));
4446 
4447  d.set_n(-1);
4448  EXPECT_FALSE(m.Matches(d));
4449 }
4450 
4451 // Tests that Property(&Foo::property, m) works when property()'s type
4452 // and m's argument type are compatible but different.
4453 TEST(PropertyTest, WorksForCompatibleMatcherType) {
4454  // n() returns an int but the inner matcher expects a signed char.
4455  Matcher<const AClass&> m = Property(&AClass::n,
4456  Matcher<signed char>(Ge(0)));
4457 
4458  Matcher<const AClass&> m_with_name =
4459  Property("n", &AClass::n, Matcher<signed char>(Ge(0)));
4460 
4461  AClass a;
4462  EXPECT_TRUE(m.Matches(a));
4463  EXPECT_TRUE(m_with_name.Matches(a));
4464  a.set_n(-1);
4465  EXPECT_FALSE(m.Matches(a));
4466  EXPECT_FALSE(m_with_name.Matches(a));
4467 }
4468 
4469 // Tests that Property() can describe itself.
4470 TEST(PropertyTest, CanDescribeSelf) {
4471  Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
4472 
4473  EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
4474  EXPECT_EQ("is an object whose given property isn't >= 0",
4475  DescribeNegation(m));
4476 }
4477 
4478 TEST(PropertyTest, CanDescribeSelfWithPropertyName) {
4479  Matcher<const AClass&> m = Property("fancy_name", &AClass::n, Ge(0));
4480 
4481  EXPECT_EQ("is an object whose property `fancy_name` is >= 0", Describe(m));
4482  EXPECT_EQ("is an object whose property `fancy_name` isn't >= 0",
4483  DescribeNegation(m));
4484 }
4485 
4486 // Tests that Property() can explain the match result.
4487 TEST(PropertyTest, CanExplainMatchResult) {
4488  Matcher<const AClass&> m = Property(&AClass::n, Ge(0));
4489 
4490  AClass a;
4491  a.set_n(1);
4492  EXPECT_EQ("whose given property is 1" + OfType("int"), Explain(m, a));
4493 
4494  m = Property(&AClass::n, GreaterThan(0));
4495  EXPECT_EQ(
4496  "whose given property is 1" + OfType("int") + ", which is 1 more than 0",
4497  Explain(m, a));
4498 }
4499 
4500 TEST(PropertyTest, CanExplainMatchResultWithPropertyName) {
4501  Matcher<const AClass&> m = Property("fancy_name", &AClass::n, Ge(0));
4502 
4503  AClass a;
4504  a.set_n(1);
4505  EXPECT_EQ("whose property `fancy_name` is 1" + OfType("int"), Explain(m, a));
4506 
4507  m = Property("fancy_name", &AClass::n, GreaterThan(0));
4508  EXPECT_EQ("whose property `fancy_name` is 1" + OfType("int") +
4509  ", which is 1 more than 0",
4510  Explain(m, a));
4511 }
4512 
4513 // Tests that Property() works when the argument is a pointer to const.
4514 TEST(PropertyForPointerTest, WorksForPointerToConst) {
4515  Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
4516 
4517  AClass a;
4518  a.set_n(1);
4519  EXPECT_TRUE(m.Matches(&a));
4520 
4521  a.set_n(-1);
4522  EXPECT_FALSE(m.Matches(&a));
4523 }
4524 
4525 // Tests that Property() works when the argument is a pointer to non-const.
4526 TEST(PropertyForPointerTest, WorksForPointerToNonConst) {
4527  Matcher<AClass*> m = Property(&AClass::s, StartsWith("hi"));
4528 
4529  AClass a;
4530  a.set_s("hill");
4531  EXPECT_TRUE(m.Matches(&a));
4532 
4533  a.set_s("hole");
4534  EXPECT_FALSE(m.Matches(&a));
4535 }
4536 
4537 // Tests that Property() works when the argument is a reference to a
4538 // const pointer.
4539 TEST(PropertyForPointerTest, WorksForReferenceToConstPointer) {
4540  Matcher<AClass* const&> m = Property(&AClass::s, StartsWith("hi"));
4541 
4542  AClass a;
4543  a.set_s("hill");
4544  EXPECT_TRUE(m.Matches(&a));
4545 
4546  a.set_s("hole");
4547  EXPECT_FALSE(m.Matches(&a));
4548 }
4549 
4550 // Tests that Property() does not match the NULL pointer.
4551 TEST(PropertyForPointerTest, WorksForReferenceToNonConstProperty) {
4552  Matcher<const AClass*> m = Property(&AClass::x, _);
4553  EXPECT_FALSE(m.Matches(nullptr));
4554 }
4555 
4556 // Tests that Property(&Foo::property, ...) works when the argument's
4557 // type is a sub-type of const Foo*.
4558 TEST(PropertyForPointerTest, WorksForArgumentOfSubType) {
4559  // The matcher expects a DerivedClass, but inside the Property() we
4560  // say AClass.
4561  Matcher<const DerivedClass*> m = Property(&AClass::n, Ge(0));
4562 
4563  DerivedClass d;
4564  d.set_n(1);
4565  EXPECT_TRUE(m.Matches(&d));
4566 
4567  d.set_n(-1);
4568  EXPECT_FALSE(m.Matches(&d));
4569 }
4570 
4571 // Tests that Property() can describe itself when used to match a pointer.
4572 TEST(PropertyForPointerTest, CanDescribeSelf) {
4573  Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
4574 
4575  EXPECT_EQ("is an object whose given property is >= 0", Describe(m));
4576  EXPECT_EQ("is an object whose given property isn't >= 0",
4577  DescribeNegation(m));
4578 }
4579 
4580 TEST(PropertyForPointerTest, CanDescribeSelfWithPropertyDescription) {
4581  Matcher<const AClass*> m = Property("fancy_name", &AClass::n, Ge(0));
4582 
4583  EXPECT_EQ("is an object whose property `fancy_name` is >= 0", Describe(m));
4584  EXPECT_EQ("is an object whose property `fancy_name` isn't >= 0",
4585  DescribeNegation(m));
4586 }
4587 
4588 // Tests that Property() can explain the result of matching a pointer.
4589 TEST(PropertyForPointerTest, CanExplainMatchResult) {
4590  Matcher<const AClass*> m = Property(&AClass::n, Ge(0));
4591 
4592  AClass a;
4593  a.set_n(1);
4594  EXPECT_EQ("", Explain(m, static_cast<const AClass*>(nullptr)));
4595  EXPECT_EQ(
4596  "which points to an object whose given property is 1" + OfType("int"),
4597  Explain(m, &a));
4598 
4599  m = Property(&AClass::n, GreaterThan(0));
4600  EXPECT_EQ("which points to an object whose given property is 1" +
4601  OfType("int") + ", which is 1 more than 0",
4602  Explain(m, &a));
4603 }
4604 
4605 TEST(PropertyForPointerTest, CanExplainMatchResultWithPropertyName) {
4606  Matcher<const AClass*> m = Property("fancy_name", &AClass::n, Ge(0));
4607 
4608  AClass a;
4609  a.set_n(1);
4610  EXPECT_EQ("", Explain(m, static_cast<const AClass*>(nullptr)));
4611  EXPECT_EQ("which points to an object whose property `fancy_name` is 1" +
4612  OfType("int"),
4613  Explain(m, &a));
4614 
4615  m = Property("fancy_name", &AClass::n, GreaterThan(0));
4616  EXPECT_EQ("which points to an object whose property `fancy_name` is 1" +
4617  OfType("int") + ", which is 1 more than 0",
4618  Explain(m, &a));
4619 }
4620 
4621 // Tests ResultOf.
4622 
4623 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
4624 // function pointer.
4625 std::string IntToStringFunction(int input) {
4626  return input == 1 ? "foo" : "bar";
4627 }
4628 
4629 TEST(ResultOfTest, WorksForFunctionPointers) {
4630  Matcher<int> matcher = ResultOf(&IntToStringFunction, Eq(std::string("foo")));
4631 
4632  EXPECT_TRUE(matcher.Matches(1));
4633  EXPECT_FALSE(matcher.Matches(2));
4634 }
4635 
4636 // Tests that ResultOf() can describe itself.
4637 TEST(ResultOfTest, CanDescribeItself) {
4638  Matcher<int> matcher = ResultOf(&IntToStringFunction, StrEq("foo"));
4639 
4640  EXPECT_EQ("is mapped by the given callable to a value that "
4641  "is equal to \"foo\"", Describe(matcher));
4642  EXPECT_EQ("is mapped by the given callable to a value that "
4643  "isn't equal to \"foo\"", DescribeNegation(matcher));
4644 }
4645 
4646 // Tests that ResultOf() can describe itself when provided a result description.
4647 TEST(ResultOfTest, CanDescribeItselfWithResultDescription) {
4648  Matcher<int> matcher =
4649  ResultOf("string conversion", &IntToStringFunction, StrEq("foo"));
4650 
4651  EXPECT_EQ("whose string conversion is equal to \"foo\"", Describe(matcher));
4652  EXPECT_EQ("whose string conversion isn't equal to \"foo\"",
4653  DescribeNegation(matcher));
4654 }
4655 
4656 // Tests that ResultOf() can explain the match result.
4657 int IntFunction(int input) { return input == 42 ? 80 : 90; }
4658 
4659 TEST(ResultOfTest, CanExplainMatchResult) {
4660  Matcher<int> matcher = ResultOf(&IntFunction, Ge(85));
4661  EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int"),
4662  Explain(matcher, 36));
4663 
4664  matcher = ResultOf(&IntFunction, GreaterThan(85));
4665  EXPECT_EQ("which is mapped by the given callable to 90" + OfType("int") +
4666  ", which is 5 more than 85", Explain(matcher, 36));
4667 }
4668 
4669 // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
4670 // returns a non-reference.
4671 TEST(ResultOfTest, WorksForNonReferenceResults) {
4672  Matcher<int> matcher = ResultOf(&IntFunction, Eq(80));
4673 
4674  EXPECT_TRUE(matcher.Matches(42));
4675  EXPECT_FALSE(matcher.Matches(36));
4676 }
4677 
4678 // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
4679 // returns a reference to non-const.
4680 double& DoubleFunction(double& input) { return input; } // NOLINT
4681 
4682 Uncopyable& RefUncopyableFunction(Uncopyable& obj) { // NOLINT
4683  return obj;
4684 }
4685 
4686 TEST(ResultOfTest, WorksForReferenceToNonConstResults) {
4687  double x = 3.14;
4688  double x2 = x;
4689  Matcher<double&> matcher = ResultOf(&DoubleFunction, Ref(x));
4690 
4691  EXPECT_TRUE(matcher.Matches(x));
4692  EXPECT_FALSE(matcher.Matches(x2));
4693 
4694  // Test that ResultOf works with uncopyable objects
4695  Uncopyable obj(0);
4696  Uncopyable obj2(0);
4697  Matcher<Uncopyable&> matcher2 =
4698  ResultOf(&RefUncopyableFunction, Ref(obj));
4699 
4700  EXPECT_TRUE(matcher2.Matches(obj));
4701  EXPECT_FALSE(matcher2.Matches(obj2));
4702 }
4703 
4704 // Tests that ResultOf(f, ...) compiles and works as expected when f(x)
4705 // returns a reference to const.
4706 const std::string& StringFunction(const std::string& input) { return input; }
4707 
4708 TEST(ResultOfTest, WorksForReferenceToConstResults) {
4709  std::string s = "foo";
4710  std::string s2 = s;
4711  Matcher<const std::string&> matcher = ResultOf(&StringFunction, Ref(s));
4712 
4713  EXPECT_TRUE(matcher.Matches(s));
4714  EXPECT_FALSE(matcher.Matches(s2));
4715 }
4716 
4717 // Tests that ResultOf(f, m) works when f(x) and m's
4718 // argument types are compatible but different.
4719 TEST(ResultOfTest, WorksForCompatibleMatcherTypes) {
4720  // IntFunction() returns int but the inner matcher expects a signed char.
4721  Matcher<int> matcher = ResultOf(IntFunction, Matcher<signed char>(Ge(85)));
4722 
4723  EXPECT_TRUE(matcher.Matches(36));
4724  EXPECT_FALSE(matcher.Matches(42));
4725 }
4726 
4727 // Tests that the program aborts when ResultOf is passed
4728 // a NULL function pointer.
4729 TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) {
4731  ResultOf(static_cast<std::string (*)(int dummy)>(nullptr),
4732  Eq(std::string("foo"))),
4733  "NULL function pointer is passed into ResultOf\\(\\)\\.");
4734 }
4735 
4736 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
4737 // function reference.
4738 TEST(ResultOfTest, WorksForFunctionReferences) {
4739  Matcher<int> matcher = ResultOf(IntToStringFunction, StrEq("foo"));
4740  EXPECT_TRUE(matcher.Matches(1));
4741  EXPECT_FALSE(matcher.Matches(2));
4742 }
4743 
4744 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
4745 // function object.
4746 struct Functor {
4747  std::string operator()(int input) const {
4748  return IntToStringFunction(input);
4749  }
4750 };
4751 
4752 TEST(ResultOfTest, WorksForFunctors) {
4753  Matcher<int> matcher = ResultOf(Functor(), Eq(std::string("foo")));
4754 
4755  EXPECT_TRUE(matcher.Matches(1));
4756  EXPECT_FALSE(matcher.Matches(2));
4757 }
4758 
4759 // Tests that ResultOf(f, ...) compiles and works as expected when f is a
4760 // functor with more than one operator() defined. ResultOf() must work
4761 // for each defined operator().
4762 struct PolymorphicFunctor {
4763  typedef int result_type;
4764  int operator()(int n) { return n; }
4765  int operator()(const char* s) { return static_cast<int>(strlen(s)); }
4766  std::string operator()(int *p) { return p ? "good ptr" : "null"; }
4767 };
4768 
4769 TEST(ResultOfTest, WorksForPolymorphicFunctors) {
4770  Matcher<int> matcher_int = ResultOf(PolymorphicFunctor(), Ge(5));
4771 
4772  EXPECT_TRUE(matcher_int.Matches(10));
4773  EXPECT_FALSE(matcher_int.Matches(2));
4774 
4775  Matcher<const char*> matcher_string = ResultOf(PolymorphicFunctor(), Ge(5));
4776 
4777  EXPECT_TRUE(matcher_string.Matches("long string"));
4778  EXPECT_FALSE(matcher_string.Matches("shrt"));
4779 }
4780 
4781 TEST(ResultOfTest, WorksForPolymorphicFunctorsIgnoringResultType) {
4782  Matcher<int*> matcher = ResultOf(PolymorphicFunctor(), "good ptr");
4783 
4784  int n = 0;
4785  EXPECT_TRUE(matcher.Matches(&n));
4786  EXPECT_FALSE(matcher.Matches(nullptr));
4787 }
4788 
4789 TEST(ResultOfTest, WorksForLambdas) {
4790  Matcher<int> matcher = ResultOf(
4791  [](int str_len) {
4792  return std::string(static_cast<size_t>(str_len), 'x');
4793  },
4794  "xxx");
4795  EXPECT_TRUE(matcher.Matches(3));
4796  EXPECT_FALSE(matcher.Matches(1));
4797 }
4798 
4799 TEST(ResultOfTest, WorksForNonCopyableArguments) {
4800  Matcher<std::unique_ptr<int>> matcher = ResultOf(
4801  [](const std::unique_ptr<int>& str_len) {
4802  return std::string(static_cast<size_t>(*str_len), 'x');
4803  },
4804  "xxx");
4805  EXPECT_TRUE(matcher.Matches(std::unique_ptr<int>(new int(3))));
4806  EXPECT_FALSE(matcher.Matches(std::unique_ptr<int>(new int(1))));
4807 }
4808 
4809 const int* ReferencingFunction(const int& n) { return &n; }
4810 
4811 struct ReferencingFunctor {
4812  typedef const int* result_type;
4813  result_type operator()(const int& n) { return &n; }
4814 };
4815 
4816 TEST(ResultOfTest, WorksForReferencingCallables) {
4817  const int n = 1;
4818  const int n2 = 1;
4819  Matcher<const int&> matcher2 = ResultOf(ReferencingFunction, Eq(&n));
4820  EXPECT_TRUE(matcher2.Matches(n));
4821  EXPECT_FALSE(matcher2.Matches(n2));
4822 
4823  Matcher<const int&> matcher3 = ResultOf(ReferencingFunctor(), Eq(&n));
4824  EXPECT_TRUE(matcher3.Matches(n));
4825  EXPECT_FALSE(matcher3.Matches(n2));
4826 }
4827 
4828 class DivisibleByImpl {
4829  public:
4830  explicit DivisibleByImpl(int a_divider) : divider_(a_divider) {}
4831 
4832  // For testing using ExplainMatchResultTo() with polymorphic matchers.
4833  template <typename T>
4834  bool MatchAndExplain(const T& n, MatchResultListener* listener) const {
4835  *listener << "which is " << (n % divider_) << " modulo "
4836  << divider_;
4837  return (n % divider_) == 0;
4838  }
4839 
4840  void DescribeTo(ostream* os) const {
4841  *os << "is divisible by " << divider_;
4842  }
4843 
4844  void DescribeNegationTo(ostream* os) const {
4845  *os << "is not divisible by " << divider_;
4846  }
4847 
4848  void set_divider(int a_divider) { divider_ = a_divider; }
4849  int divider() const { return divider_; }
4850 
4851  private:
4853 };
4854 
4855 PolymorphicMatcher<DivisibleByImpl> DivisibleBy(int n) {
4856  return MakePolymorphicMatcher(DivisibleByImpl(n));
4857 }
4858 
4859 // Tests that when AllOf() fails, only the first failing matcher is
4860 // asked to explain why.
4861 TEST(ExplainMatchResultTest, AllOf_False_False) {
4862  const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
4863  EXPECT_EQ("which is 1 modulo 4", Explain(m, 5));
4864 }
4865 
4866 // Tests that when AllOf() fails, only the first failing matcher is
4867 // asked to explain why.
4868 TEST(ExplainMatchResultTest, AllOf_False_True) {
4869  const Matcher<int> m = AllOf(DivisibleBy(4), DivisibleBy(3));
4870  EXPECT_EQ("which is 2 modulo 4", Explain(m, 6));
4871 }
4872 
4873 // Tests that when AllOf() fails, only the first failing matcher is
4874 // asked to explain why.
4875 TEST(ExplainMatchResultTest, AllOf_True_False) {
4876  const Matcher<int> m = AllOf(Ge(1), DivisibleBy(3));
4877  EXPECT_EQ("which is 2 modulo 3", Explain(m, 5));
4878 }
4879 
4880 // Tests that when AllOf() succeeds, all matchers are asked to explain
4881 // why.
4882 TEST(ExplainMatchResultTest, AllOf_True_True) {
4883  const Matcher<int> m = AllOf(DivisibleBy(2), DivisibleBy(3));
4884  EXPECT_EQ("which is 0 modulo 2, and which is 0 modulo 3", Explain(m, 6));
4885 }
4886 
4887 TEST(ExplainMatchResultTest, AllOf_True_True_2) {
4888  const Matcher<int> m = AllOf(Ge(2), Le(3));
4889  EXPECT_EQ("", Explain(m, 2));
4890 }
4891 
4892 TEST(ExplainmatcherResultTest, MonomorphicMatcher) {
4893  const Matcher<int> m = GreaterThan(5);
4894  EXPECT_EQ("which is 1 more than 5", Explain(m, 6));
4895 }
4896 
4897 // The following two tests verify that values without a public copy
4898 // ctor can be used as arguments to matchers like Eq(), Ge(), and etc
4899 // with the help of ByRef().
4900 
4901 class NotCopyable {
4902  public:
4903  explicit NotCopyable(int a_value) : value_(a_value) {}
4904 
4905  int value() const { return value_; }
4906 
4907  bool operator==(const NotCopyable& rhs) const {
4908  return value() == rhs.value();
4909  }
4910 
4911  bool operator>=(const NotCopyable& rhs) const {
4912  return value() >= rhs.value();
4913  }
4914  private:
4915  int value_;
4916 
4917  GTEST_DISALLOW_COPY_AND_ASSIGN_(NotCopyable);
4918 };
4919 
4920 TEST(ByRefTest, AllowsNotCopyableConstValueInMatchers) {
4921  const NotCopyable const_value1(1);
4922  const Matcher<const NotCopyable&> m = Eq(ByRef(const_value1));
4923 
4924  const NotCopyable n1(1), n2(2);
4925  EXPECT_TRUE(m.Matches(n1));
4926  EXPECT_FALSE(m.Matches(n2));
4927 }
4928 
4929 TEST(ByRefTest, AllowsNotCopyableValueInMatchers) {
4930  NotCopyable value2(2);
4931  const Matcher<NotCopyable&> m = Ge(ByRef(value2));
4932 
4933  NotCopyable n1(1), n2(2);
4934  EXPECT_FALSE(m.Matches(n1));
4935  EXPECT_TRUE(m.Matches(n2));
4936 }
4937 
4938 TEST(IsEmptyTest, ImplementsIsEmpty) {
4939  vector<int> container;
4941  container.push_back(0);
4943  container.push_back(1);
4945 }
4946 
4947 TEST(IsEmptyTest, WorksWithString) {
4948  std::string text;
4949  EXPECT_THAT(text, IsEmpty());
4950  text = "foo";
4951  EXPECT_THAT(text, Not(IsEmpty()));
4952  text = std::string("\0", 1);
4953  EXPECT_THAT(text, Not(IsEmpty()));
4954 }
4955 
4956 TEST(IsEmptyTest, CanDescribeSelf) {
4957  Matcher<vector<int> > m = IsEmpty();
4958  EXPECT_EQ("is empty", Describe(m));
4959  EXPECT_EQ("isn't empty", DescribeNegation(m));
4960 }
4961 
4962 TEST(IsEmptyTest, ExplainsResult) {
4963  Matcher<vector<int> > m = IsEmpty();
4964  vector<int> container;
4965  EXPECT_EQ("", Explain(m, container));
4966  container.push_back(0);
4967  EXPECT_EQ("whose size is 1", Explain(m, container));
4968 }
4969 
4970 TEST(IsEmptyTest, WorksWithMoveOnly) {
4971  ContainerHelper helper;
4972  EXPECT_CALL(helper, Call(IsEmpty()));
4973  helper.Call({});
4974 }
4975 
4976 TEST(IsTrueTest, IsTrueIsFalse) {
4977  EXPECT_THAT(true, IsTrue());
4978  EXPECT_THAT(false, IsFalse());
4979  EXPECT_THAT(true, Not(IsFalse()));
4980  EXPECT_THAT(false, Not(IsTrue()));
4981  EXPECT_THAT(0, Not(IsTrue()));
4982  EXPECT_THAT(0, IsFalse());
4983  EXPECT_THAT(nullptr, Not(IsTrue()));
4984  EXPECT_THAT(nullptr, IsFalse());
4985  EXPECT_THAT(-1, IsTrue());
4986  EXPECT_THAT(-1, Not(IsFalse()));
4987  EXPECT_THAT(1, IsTrue());
4988  EXPECT_THAT(1, Not(IsFalse()));
4989  EXPECT_THAT(2, IsTrue());
4990  EXPECT_THAT(2, Not(IsFalse()));
4991  int a = 42;
4992  EXPECT_THAT(a, IsTrue());
4993  EXPECT_THAT(a, Not(IsFalse()));
4994  EXPECT_THAT(&a, IsTrue());
4995  EXPECT_THAT(&a, Not(IsFalse()));
4996  EXPECT_THAT(false, Not(IsTrue()));
4997  EXPECT_THAT(true, Not(IsFalse()));
4999  EXPECT_THAT(std::true_type(), Not(IsFalse()));
5000  EXPECT_THAT(std::false_type(), IsFalse());
5002  EXPECT_THAT(nullptr, Not(IsTrue()));
5003  EXPECT_THAT(nullptr, IsFalse());
5004  std::unique_ptr<int> null_unique;
5005  std::unique_ptr<int> nonnull_unique(new int(0));
5006  EXPECT_THAT(null_unique, Not(IsTrue()));
5007  EXPECT_THAT(null_unique, IsFalse());
5008  EXPECT_THAT(nonnull_unique, IsTrue());
5009  EXPECT_THAT(nonnull_unique, Not(IsFalse()));
5010 }
5011 
5012 TEST(SizeIsTest, ImplementsSizeIs) {
5013  vector<int> container;
5016  container.push_back(0);
5019  container.push_back(0);
5022 }
5023 
5024 TEST(SizeIsTest, WorksWithMap) {
5025  map<std::string, int> container;
5028  container.insert(make_pair("foo", 1));
5031  container.insert(make_pair("bar", 2));
5034 }
5035 
5036 TEST(SizeIsTest, WorksWithReferences) {
5037  vector<int> container;
5038  Matcher<const vector<int>&> m = SizeIs(1);
5040  container.push_back(0);
5042 }
5043 
5044 TEST(SizeIsTest, WorksWithMoveOnly) {
5045  ContainerHelper helper;
5046  EXPECT_CALL(helper, Call(SizeIs(3)));
5047  helper.Call(MakeUniquePtrs({1, 2, 3}));
5048 }
5049 
5050 // SizeIs should work for any type that provides a size() member function.
5051 // For example, a size_type member type should not need to be provided.
5052 struct MinimalistCustomType {
5053  int size() const { return 1; }
5054 };
5055 TEST(SizeIsTest, WorksWithMinimalistCustomType) {
5056  MinimalistCustomType container;
5059 }
5060 
5061 TEST(SizeIsTest, CanDescribeSelf) {
5062  Matcher<vector<int> > m = SizeIs(2);
5063  EXPECT_EQ("size is equal to 2", Describe(m));
5064  EXPECT_EQ("size isn't equal to 2", DescribeNegation(m));
5065 }
5066 
5067 TEST(SizeIsTest, ExplainsResult) {
5068  Matcher<vector<int> > m1 = SizeIs(2);
5069  Matcher<vector<int> > m2 = SizeIs(Lt(2u));
5070  Matcher<vector<int> > m3 = SizeIs(AnyOf(0, 3));
5071  Matcher<vector<int> > m4 = SizeIs(Gt(1u));
5072  vector<int> container;
5073  EXPECT_EQ("whose size 0 doesn't match", Explain(m1, container));
5074  EXPECT_EQ("whose size 0 matches", Explain(m2, container));
5075  EXPECT_EQ("whose size 0 matches", Explain(m3, container));
5076  EXPECT_EQ("whose size 0 doesn't match", Explain(m4, container));
5077  container.push_back(0);
5078  container.push_back(0);
5079  EXPECT_EQ("whose size 2 matches", Explain(m1, container));
5080  EXPECT_EQ("whose size 2 doesn't match", Explain(m2, container));
5081  EXPECT_EQ("whose size 2 doesn't match", Explain(m3, container));
5082  EXPECT_EQ("whose size 2 matches", Explain(m4, container));
5083 }
5084 
5085 #if GTEST_HAS_TYPED_TEST
5086 // Tests ContainerEq with different container types, and
5087 // different element types.
5088 
5089 template <typename T>
5090 class ContainerEqTest : public testing::Test {};
5091 
5092 typedef testing::Types<
5093  set<int>,
5094  vector<size_t>,
5095  multiset<size_t>,
5096  list<int> >
5097  ContainerEqTestTypes;
5098 
5099 TYPED_TEST_SUITE(ContainerEqTest, ContainerEqTestTypes);
5100 
5101 // Tests that the filled container is equal to itself.
5102 TYPED_TEST(ContainerEqTest, EqualsSelf) {
5103  static const int vals[] = {1, 1, 2, 3, 5, 8};
5104  TypeParam my_set(vals, vals + 6);
5105  const Matcher<TypeParam> m = ContainerEq(my_set);
5106  EXPECT_TRUE(m.Matches(my_set));
5107  EXPECT_EQ("", Explain(m, my_set));
5108 }
5109 
5110 // Tests that missing values are reported.
5111 TYPED_TEST(ContainerEqTest, ValueMissing) {
5112  static const int vals[] = {1, 1, 2, 3, 5, 8};
5113  static const int test_vals[] = {2, 1, 8, 5};
5114  TypeParam my_set(vals, vals + 6);
5115  TypeParam test_set(test_vals, test_vals + 4);
5116  const Matcher<TypeParam> m = ContainerEq(my_set);
5117  EXPECT_FALSE(m.Matches(test_set));
5118  EXPECT_EQ("which doesn't have these expected elements: 3",
5119  Explain(m, test_set));
5120 }
5121 
5122 // Tests that added values are reported.
5123 TYPED_TEST(ContainerEqTest, ValueAdded) {
5124  static const int vals[] = {1, 1, 2, 3, 5, 8};
5125  static const int test_vals[] = {1, 2, 3, 5, 8, 46};
5126  TypeParam my_set(vals, vals + 6);
5127  TypeParam test_set(test_vals, test_vals + 6);
5128  const Matcher<const TypeParam&> m = ContainerEq(my_set);
5129  EXPECT_FALSE(m.Matches(test_set));
5130  EXPECT_EQ("which has these unexpected elements: 46", Explain(m, test_set));
5131 }
5132 
5133 // Tests that added and missing values are reported together.
5134 TYPED_TEST(ContainerEqTest, ValueAddedAndRemoved) {
5135  static const int vals[] = {1, 1, 2, 3, 5, 8};
5136  static const int test_vals[] = {1, 2, 3, 8, 46};
5137  TypeParam my_set(vals, vals + 6);
5138  TypeParam test_set(test_vals, test_vals + 5);
5139  const Matcher<TypeParam> m = ContainerEq(my_set);
5140  EXPECT_FALSE(m.Matches(test_set));
5141  EXPECT_EQ("which has these unexpected elements: 46,\n"
5142  "and doesn't have these expected elements: 5",
5143  Explain(m, test_set));
5144 }
5145 
5146 // Tests duplicated value -- expect no explanation.
5147 TYPED_TEST(ContainerEqTest, DuplicateDifference) {
5148  static const int vals[] = {1, 1, 2, 3, 5, 8};
5149  static const int test_vals[] = {1, 2, 3, 5, 8};
5150  TypeParam my_set(vals, vals + 6);
5151  TypeParam test_set(test_vals, test_vals + 5);
5152  const Matcher<const TypeParam&> m = ContainerEq(my_set);
5153  // Depending on the container, match may be true or false
5154  // But in any case there should be no explanation.
5155  EXPECT_EQ("", Explain(m, test_set));
5156 }
5157 #endif // GTEST_HAS_TYPED_TEST
5158 
5159 // Tests that multiple missing values are reported.
5160 // Using just vector here, so order is predictable.
5161 TEST(ContainerEqExtraTest, MultipleValuesMissing) {
5162  static const int vals[] = {1, 1, 2, 3, 5, 8};
5163  static const int test_vals[] = {2, 1, 5};
5164  vector<int> my_set(vals, vals + 6);
5165  vector<int> test_set(test_vals, test_vals + 3);
5166  const Matcher<vector<int> > m = ContainerEq(my_set);
5167  EXPECT_FALSE(m.Matches(test_set));
5168  EXPECT_EQ("which doesn't have these expected elements: 3, 8",
5169  Explain(m, test_set));
5170 }
5171 
5172 // Tests that added values are reported.
5173 // Using just vector here, so order is predictable.
5174 TEST(ContainerEqExtraTest, MultipleValuesAdded) {
5175  static const int vals[] = {1, 1, 2, 3, 5, 8};
5176  static const int test_vals[] = {1, 2, 92, 3, 5, 8, 46};
5177  list<size_t> my_set(vals, vals + 6);
5178  list<size_t> test_set(test_vals, test_vals + 7);
5179  const Matcher<const list<size_t>&> m = ContainerEq(my_set);
5180  EXPECT_FALSE(m.Matches(test_set));
5181  EXPECT_EQ("which has these unexpected elements: 92, 46",
5182  Explain(m, test_set));
5183 }
5184 
5185 // Tests that added and missing values are reported together.
5186 TEST(ContainerEqExtraTest, MultipleValuesAddedAndRemoved) {
5187  static const int vals[] = {1, 1, 2, 3, 5, 8};
5188  static const int test_vals[] = {1, 2, 3, 92, 46};
5189  list<size_t> my_set(vals, vals + 6);
5190  list<size_t> test_set(test_vals, test_vals + 5);
5191  const Matcher<const list<size_t> > m = ContainerEq(my_set);
5192  EXPECT_FALSE(m.Matches(test_set));
5193  EXPECT_EQ("which has these unexpected elements: 92, 46,\n"
5194  "and doesn't have these expected elements: 5, 8",
5195  Explain(m, test_set));
5196 }
5197 
5198 // Tests to see that duplicate elements are detected,
5199 // but (as above) not reported in the explanation.
5200 TEST(ContainerEqExtraTest, MultiSetOfIntDuplicateDifference) {
5201  static const int vals[] = {1, 1, 2, 3, 5, 8};
5202  static const int test_vals[] = {1, 2, 3, 5, 8};
5203  vector<int> my_set(vals, vals + 6);
5204  vector<int> test_set(test_vals, test_vals + 5);
5205  const Matcher<vector<int> > m = ContainerEq(my_set);
5206  EXPECT_TRUE(m.Matches(my_set));
5207  EXPECT_FALSE(m.Matches(test_set));
5208  // There is nothing to report when both sets contain all the same values.
5209  EXPECT_EQ("", Explain(m, test_set));
5210 }
5211 
5212 // Tests that ContainerEq works for non-trivial associative containers,
5213 // like maps.
5214 TEST(ContainerEqExtraTest, WorksForMaps) {
5215  map<int, std::string> my_map;
5216  my_map[0] = "a";
5217  my_map[1] = "b";
5218 
5219  map<int, std::string> test_map;
5220  test_map[0] = "aa";
5221  test_map[1] = "b";
5222 
5223  const Matcher<const map<int, std::string>&> m = ContainerEq(my_map);
5224  EXPECT_TRUE(m.Matches(my_map));
5225  EXPECT_FALSE(m.Matches(test_map));
5226 
5227  EXPECT_EQ("which has these unexpected elements: (0, \"aa\"),\n"
5228  "and doesn't have these expected elements: (0, \"a\")",
5229  Explain(m, test_map));
5230 }
5231 
5232 TEST(ContainerEqExtraTest, WorksForNativeArray) {
5233  int a1[] = {1, 2, 3};
5234  int a2[] = {1, 2, 3};
5235  int b[] = {1, 2, 4};
5236 
5239 }
5240 
5241 TEST(ContainerEqExtraTest, WorksForTwoDimensionalNativeArray) {
5242  const char a1[][3] = {"hi", "lo"};
5243  const char a2[][3] = {"hi", "lo"};
5244  const char b[][3] = {"lo", "hi"};
5245 
5246  // Tests using ContainerEq() in the first dimension.
5249 
5250  // Tests using ContainerEq() in the second dimension.
5253 }
5254 
5255 TEST(ContainerEqExtraTest, WorksForNativeArrayAsTuple) {
5256  const int a1[] = {1, 2, 3};
5257  const int a2[] = {1, 2, 3};
5258  const int b[] = {1, 2, 3, 4};
5259 
5260  const int* const p1 = a1;
5263 
5264  const int c[] = {1, 3, 2};
5266 }
5267 
5268 TEST(ContainerEqExtraTest, CopiesNativeArrayParameter) {
5269  std::string a1[][3] = {
5270  {"hi", "hello", "ciao"},
5271  {"bye", "see you", "ciao"}
5272  };
5273 
5274  std::string a2[][3] = {
5275  {"hi", "hello", "ciao"},
5276  {"bye", "see you", "ciao"}
5277  };
5278 
5279  const Matcher<const std::string(&)[2][3]> m = ContainerEq(a2);
5280  EXPECT_THAT(a1, m);
5281 
5282  a2[0][0] = "ha";
5283  EXPECT_THAT(a1, m);
5284 }
5285 
5286 TEST(WhenSortedByTest, WorksForEmptyContainer) {
5287  const vector<int> numbers;
5288  EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre()));
5289  EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1))));
5290 }
5291 
5292 TEST(WhenSortedByTest, WorksForNonEmptyContainer) {
5293  vector<unsigned> numbers;
5294  numbers.push_back(3);
5295  numbers.push_back(1);
5296  numbers.push_back(2);
5297  numbers.push_back(2);
5298  EXPECT_THAT(numbers, WhenSortedBy(greater<unsigned>(),
5299  ElementsAre(3, 2, 2, 1)));
5300  EXPECT_THAT(numbers, Not(WhenSortedBy(greater<unsigned>(),
5301  ElementsAre(1, 2, 2, 3))));
5302 }
5303 
5304 TEST(WhenSortedByTest, WorksForNonVectorContainer) {
5305  list<std::string> words;
5306  words.push_back("say");
5307  words.push_back("hello");
5308  words.push_back("world");
5309  EXPECT_THAT(words, WhenSortedBy(less<std::string>(),
5310  ElementsAre("hello", "say", "world")));
5311  EXPECT_THAT(words, Not(WhenSortedBy(less<std::string>(),
5312  ElementsAre("say", "hello", "world"))));
5313 }
5314 
5315 TEST(WhenSortedByTest, WorksForNativeArray) {
5316  const int numbers[] = {1, 3, 2, 4};
5317  const int sorted_numbers[] = {1, 2, 3, 4};
5318  EXPECT_THAT(numbers, WhenSortedBy(less<int>(), ElementsAre(1, 2, 3, 4)));
5319  EXPECT_THAT(numbers, WhenSortedBy(less<int>(),
5320  ElementsAreArray(sorted_numbers)));
5321  EXPECT_THAT(numbers, Not(WhenSortedBy(less<int>(), ElementsAre(1, 3, 2, 4))));
5322 }
5323 
5324 TEST(WhenSortedByTest, CanDescribeSelf) {
5325  const Matcher<vector<int> > m = WhenSortedBy(less<int>(), ElementsAre(1, 2));
5326  EXPECT_EQ("(when sorted) has 2 elements where\n"
5327  "element #0 is equal to 1,\n"
5328  "element #1 is equal to 2",
5329  Describe(m));
5330  EXPECT_EQ("(when sorted) doesn't have 2 elements, or\n"
5331  "element #0 isn't equal to 1, or\n"
5332  "element #1 isn't equal to 2",
5333  DescribeNegation(m));
5334 }
5335 
5336 TEST(WhenSortedByTest, ExplainsMatchResult) {
5337  const int a[] = {2, 1};
5338  EXPECT_EQ("which is { 1, 2 } when sorted, whose element #0 doesn't match",
5339  Explain(WhenSortedBy(less<int>(), ElementsAre(2, 3)), a));
5340  EXPECT_EQ("which is { 1, 2 } when sorted",
5341  Explain(WhenSortedBy(less<int>(), ElementsAre(1, 2)), a));
5342 }
5343 
5344 // WhenSorted() is a simple wrapper on WhenSortedBy(). Hence we don't
5345 // need to test it as exhaustively as we test the latter.
5346 
5347 TEST(WhenSortedTest, WorksForEmptyContainer) {
5348  const vector<int> numbers;
5349  EXPECT_THAT(numbers, WhenSorted(ElementsAre()));
5350  EXPECT_THAT(numbers, Not(WhenSorted(ElementsAre(1))));
5351 }
5352 
5353 TEST(WhenSortedTest, WorksForNonEmptyContainer) {
5354  list<std::string> words;
5355  words.push_back("3");
5356  words.push_back("1");
5357  words.push_back("2");
5358  words.push_back("2");
5359  EXPECT_THAT(words, WhenSorted(ElementsAre("1", "2", "2", "3")));
5360  EXPECT_THAT(words, Not(WhenSorted(ElementsAre("3", "1", "2", "2"))));
5361 }
5362 
5363 TEST(WhenSortedTest, WorksForMapTypes) {
5364  map<std::string, int> word_counts;
5365  word_counts["and"] = 1;
5366  word_counts["the"] = 1;
5367  word_counts["buffalo"] = 2;
5368  EXPECT_THAT(word_counts,
5369  WhenSorted(ElementsAre(Pair("and", 1), Pair("buffalo", 2),
5370  Pair("the", 1))));
5371  EXPECT_THAT(word_counts,
5372  Not(WhenSorted(ElementsAre(Pair("and", 1), Pair("the", 1),
5373  Pair("buffalo", 2)))));
5374 }
5375 
5376 TEST(WhenSortedTest, WorksForMultiMapTypes) {
5377  multimap<int, int> ifib;
5378  ifib.insert(make_pair(8, 6));
5379  ifib.insert(make_pair(2, 3));
5380  ifib.insert(make_pair(1, 1));
5381  ifib.insert(make_pair(3, 4));
5382  ifib.insert(make_pair(1, 2));
5383  ifib.insert(make_pair(5, 5));
5384  EXPECT_THAT(ifib, WhenSorted(ElementsAre(Pair(1, 1),
5385  Pair(1, 2),
5386  Pair(2, 3),
5387  Pair(3, 4),
5388  Pair(5, 5),
5389  Pair(8, 6))));
5391  Pair(2, 3),
5392  Pair(1, 1),
5393  Pair(3, 4),
5394  Pair(1, 2),
5395  Pair(5, 5)))));
5396 }
5397 
5398 TEST(WhenSortedTest, WorksForPolymorphicMatcher) {
5399  std::deque<int> d;
5400  d.push_back(2);
5401  d.push_back(1);
5402  EXPECT_THAT(d, WhenSorted(ElementsAre(1, 2)));
5403  EXPECT_THAT(d, Not(WhenSorted(ElementsAre(2, 1))));
5404 }
5405 
5406 TEST(WhenSortedTest, WorksForVectorConstRefMatcher) {
5407  std::deque<int> d;
5408  d.push_back(2);
5409  d.push_back(1);
5410  Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2);
5411  EXPECT_THAT(d, WhenSorted(vector_match));
5412  Matcher<const std::vector<int>&> not_vector_match = ElementsAre(2, 1);
5413  EXPECT_THAT(d, Not(WhenSorted(not_vector_match)));
5414 }
5415 
5416 // Deliberately bare pseudo-container.
5417 // Offers only begin() and end() accessors, yielding InputIterator.
5418 template <typename T>
5419 class Streamlike {
5420  private:
5421  class ConstIter;
5422  public:
5423  typedef ConstIter const_iterator;
5424  typedef T value_type;
5425 
5426  template <typename InIter>
5427  Streamlike(InIter first, InIter last) : remainder_(first, last) {}
5428 
5429  const_iterator begin() const {
5430  return const_iterator(this, remainder_.begin());
5431  }
5432  const_iterator end() const {
5433  return const_iterator(this, remainder_.end());
5434  }
5435 
5436  private:
5437  class ConstIter {
5438  public:
5439  using iterator_category = std::input_iterator_tag;
5440  using value_type = T;
5441  using difference_type = ptrdiff_t;
5442  using pointer = const value_type*;
5443  using reference = const value_type&;
5444 
5445  ConstIter(const Streamlike* s,
5447  : s_(s), pos_(pos) {}
5448 
5449  const value_type& operator*() const { return *pos_; }
5450  const value_type* operator->() const { return &*pos_; }
5451  ConstIter& operator++() {
5452  s_->remainder_.erase(pos_++);
5453  return *this;
5454  }
5455 
5456  // *iter++ is required to work (see std::istreambuf_iterator).
5457  // (void)iter++ is also required to work.
5458  class PostIncrProxy {
5459  public:
5460  explicit PostIncrProxy(const value_type& value) : value_(value) {}
5461  value_type operator*() const { return value_; }
5462  private:
5464  };
5465  PostIncrProxy operator++(int) {
5466  PostIncrProxy proxy(**this);
5467  ++(*this);
5468  return proxy;
5469  }
5470 
5471  friend bool operator==(const ConstIter& a, const ConstIter& b) {
5472  return a.s_ == b.s_ && a.pos_ == b.pos_;
5473  }
5474  friend bool operator!=(const ConstIter& a, const ConstIter& b) {
5475  return !(a == b);
5476  }
5477 
5478  private:
5479  const Streamlike* s_;
5481  };
5482 
5483  friend std::ostream& operator<<(std::ostream& os, const Streamlike& s) {
5484  os << "[";
5485  typedef typename std::list<value_type>::const_iterator Iter;
5486  const char* sep = "";
5487  for (Iter it = s.remainder_.begin(); it != s.remainder_.end(); ++it) {
5488  os << sep << *it;
5489  sep = ",";
5490  }
5491  os << "]";
5492  return os;
5493  }
5494 
5495  mutable std::list<value_type> remainder_; // modified by iteration
5496 };
5497 
5498 TEST(StreamlikeTest, Iteration) {
5499  const int a[5] = {2, 1, 4, 5, 3};
5500  Streamlike<int> s(a, a + 5);
5501  Streamlike<int>::const_iterator it = s.begin();
5502  const int* ip = a;
5503  while (it != s.end()) {
5504  SCOPED_TRACE(ip - a);
5505  EXPECT_EQ(*ip++, *it++);
5506  }
5507 }
5508 
5509 TEST(BeginEndDistanceIsTest, WorksWithForwardList) {
5510  std::forward_list<int> container;
5513  container.push_front(0);
5516  container.push_front(0);
5519 }
5520 
5521 TEST(BeginEndDistanceIsTest, WorksWithNonStdList) {
5522  const int a[5] = {1, 2, 3, 4, 5};
5523  Streamlike<int> s(a, a + 5);
5525 }
5526 
5527 TEST(BeginEndDistanceIsTest, CanDescribeSelf) {
5528  Matcher<vector<int> > m = BeginEndDistanceIs(2);
5529  EXPECT_EQ("distance between begin() and end() is equal to 2", Describe(m));
5530  EXPECT_EQ("distance between begin() and end() isn't equal to 2",
5531  DescribeNegation(m));
5532 }
5533 
5534 TEST(BeginEndDistanceIsTest, WorksWithMoveOnly) {
5535  ContainerHelper helper;
5536  EXPECT_CALL(helper, Call(BeginEndDistanceIs(2)));
5537  helper.Call(MakeUniquePtrs({1, 2}));
5538 }
5539 
5540 TEST(BeginEndDistanceIsTest, ExplainsResult) {
5541  Matcher<vector<int> > m1 = BeginEndDistanceIs(2);
5542  Matcher<vector<int> > m2 = BeginEndDistanceIs(Lt(2));
5543  Matcher<vector<int> > m3 = BeginEndDistanceIs(AnyOf(0, 3));
5544  Matcher<vector<int> > m4 = BeginEndDistanceIs(GreaterThan(1));
5545  vector<int> container;
5546  EXPECT_EQ("whose distance between begin() and end() 0 doesn't match",
5547  Explain(m1, container));
5548  EXPECT_EQ("whose distance between begin() and end() 0 matches",
5549  Explain(m2, container));
5550  EXPECT_EQ("whose distance between begin() and end() 0 matches",
5551  Explain(m3, container));
5552  EXPECT_EQ(
5553  "whose distance between begin() and end() 0 doesn't match, which is 1 "
5554  "less than 1",
5555  Explain(m4, container));
5556  container.push_back(0);
5557  container.push_back(0);
5558  EXPECT_EQ("whose distance between begin() and end() 2 matches",
5559  Explain(m1, container));
5560  EXPECT_EQ("whose distance between begin() and end() 2 doesn't match",
5561  Explain(m2, container));
5562  EXPECT_EQ("whose distance between begin() and end() 2 doesn't match",
5563  Explain(m3, container));
5564  EXPECT_EQ(
5565  "whose distance between begin() and end() 2 matches, which is 1 more "
5566  "than 1",
5567  Explain(m4, container));
5568 }
5569 
5570 TEST(WhenSortedTest, WorksForStreamlike) {
5571  // Streamlike 'container' provides only minimal iterator support.
5572  // Its iterators are tagged with input_iterator_tag.
5573  const int a[5] = {2, 1, 4, 5, 3};
5574  Streamlike<int> s(std::begin(a), std::end(a));
5575  EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5)));
5576  EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
5577 }
5578 
5579 TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) {
5580  const int a[] = {2, 1, 4, 5, 3};
5581  Streamlike<int> s(std::begin(a), std::end(a));
5582  Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5);
5583  EXPECT_THAT(s, WhenSorted(vector_match));
5584  EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3))));
5585 }
5586 
5587 TEST(IsSupersetOfTest, WorksForNativeArray) {
5588  const int subset[] = {1, 4};
5589  const int superset[] = {1, 2, 4};
5590  const int disjoint[] = {1, 0, 3};
5591  EXPECT_THAT(subset, IsSupersetOf(subset));
5592  EXPECT_THAT(subset, Not(IsSupersetOf(superset)));
5593  EXPECT_THAT(superset, IsSupersetOf(subset));
5594  EXPECT_THAT(subset, Not(IsSupersetOf(disjoint)));
5595  EXPECT_THAT(disjoint, Not(IsSupersetOf(subset)));
5596 }
5597 
5598 TEST(IsSupersetOfTest, WorksWithDuplicates) {
5599  const int not_enough[] = {1, 2};
5600  const int enough[] = {1, 1, 2};
5601  const int expected[] = {1, 1};
5602  EXPECT_THAT(not_enough, Not(IsSupersetOf(expected)));
5603  EXPECT_THAT(enough, IsSupersetOf(expected));
5604 }
5605 
5606 TEST(IsSupersetOfTest, WorksForEmpty) {
5607  vector<int> numbers;
5608  vector<int> expected;
5609  EXPECT_THAT(numbers, IsSupersetOf(expected));
5610  expected.push_back(1);
5611  EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
5612  expected.clear();
5613  numbers.push_back(1);
5614  numbers.push_back(2);
5615  EXPECT_THAT(numbers, IsSupersetOf(expected));
5616  expected.push_back(1);
5617  EXPECT_THAT(numbers, IsSupersetOf(expected));
5618  expected.push_back(2);
5619  EXPECT_THAT(numbers, IsSupersetOf(expected));
5620  expected.push_back(3);
5621  EXPECT_THAT(numbers, Not(IsSupersetOf(expected)));
5622 }
5623 
5624 TEST(IsSupersetOfTest, WorksForStreamlike) {
5625  const int a[5] = {1, 2, 3, 4, 5};
5626  Streamlike<int> s(std::begin(a), std::end(a));
5627 
5628  vector<int> expected;
5629  expected.push_back(1);
5630  expected.push_back(2);
5631  expected.push_back(5);
5632  EXPECT_THAT(s, IsSupersetOf(expected));
5633 
5634  expected.push_back(0);
5635  EXPECT_THAT(s, Not(IsSupersetOf(expected)));
5636 }
5637 
5638 TEST(IsSupersetOfTest, TakesStlContainer) {
5639  const int actual[] = {3, 1, 2};
5640 
5641  ::std::list<int> expected;
5642  expected.push_back(1);
5643  expected.push_back(3);
5644  EXPECT_THAT(actual, IsSupersetOf(expected));
5645 
5646  expected.push_back(4);
5647  EXPECT_THAT(actual, Not(IsSupersetOf(expected)));
5648 }
5649 
5650 TEST(IsSupersetOfTest, Describe) {
5651  typedef std::vector<int> IntVec;
5652  IntVec expected;
5653  expected.push_back(111);
5654  expected.push_back(222);
5655  expected.push_back(333);
5656  EXPECT_THAT(
5657  Describe<IntVec>(IsSupersetOf(expected)),
5658  Eq("a surjection from elements to requirements exists such that:\n"
5659  " - an element is equal to 111\n"
5660  " - an element is equal to 222\n"
5661  " - an element is equal to 333"));
5662 }
5663 
5664 TEST(IsSupersetOfTest, DescribeNegation) {
5665  typedef std::vector<int> IntVec;
5666  IntVec expected;
5667  expected.push_back(111);
5668  expected.push_back(222);
5669  expected.push_back(333);
5670  EXPECT_THAT(
5671  DescribeNegation<IntVec>(IsSupersetOf(expected)),
5672  Eq("no surjection from elements to requirements exists such that:\n"
5673  " - an element is equal to 111\n"
5674  " - an element is equal to 222\n"
5675  " - an element is equal to 333"));
5676 }
5677 
5678 TEST(IsSupersetOfTest, MatchAndExplain) {
5679  std::vector<int> v;
5680  v.push_back(2);
5681  v.push_back(3);
5682  std::vector<int> expected;
5683  expected.push_back(1);
5684  expected.push_back(2);
5685  StringMatchResultListener listener;
5686  ASSERT_FALSE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
5687  << listener.str();
5688  EXPECT_THAT(listener.str(),
5689  Eq("where the following matchers don't match any elements:\n"
5690  "matcher #0: is equal to 1"));
5691 
5692  v.push_back(1);
5693  listener.Clear();
5694  ASSERT_TRUE(ExplainMatchResult(IsSupersetOf(expected), v, &listener))
5695  << listener.str();
5696  EXPECT_THAT(listener.str(), Eq("where:\n"
5697  " - element #0 is matched by matcher #1,\n"
5698  " - element #2 is matched by matcher #0"));
5699 }
5700 
5701 TEST(IsSupersetOfTest, WorksForRhsInitializerList) {
5702  const int numbers[] = {1, 3, 6, 2, 4, 5};
5703  EXPECT_THAT(numbers, IsSupersetOf({1, 2}));
5704  EXPECT_THAT(numbers, Not(IsSupersetOf({3, 0})));
5705 }
5706 
5707 TEST(IsSupersetOfTest, WorksWithMoveOnly) {
5708  ContainerHelper helper;
5709  EXPECT_CALL(helper, Call(IsSupersetOf({Pointee(1)})));
5710  helper.Call(MakeUniquePtrs({1, 2}));
5711  EXPECT_CALL(helper, Call(Not(IsSupersetOf({Pointee(1), Pointee(2)}))));
5712  helper.Call(MakeUniquePtrs({2}));
5713 }
5714 
5715 TEST(IsSubsetOfTest, WorksForNativeArray) {
5716  const int subset[] = {1, 4};
5717  const int superset[] = {1, 2, 4};
5718  const int disjoint[] = {1, 0, 3};
5719  EXPECT_THAT(subset, IsSubsetOf(subset));
5720  EXPECT_THAT(subset, IsSubsetOf(superset));
5721  EXPECT_THAT(superset, Not(IsSubsetOf(subset)));
5722  EXPECT_THAT(subset, Not(IsSubsetOf(disjoint)));
5723  EXPECT_THAT(disjoint, Not(IsSubsetOf(subset)));
5724 }
5725 
5726 TEST(IsSubsetOfTest, WorksWithDuplicates) {
5727  const int not_enough[] = {1, 2};
5728  const int enough[] = {1, 1, 2};
5729  const int actual[] = {1, 1};
5730  EXPECT_THAT(actual, Not(IsSubsetOf(not_enough)));
5731  EXPECT_THAT(actual, IsSubsetOf(enough));
5732 }
5733 
5734 TEST(IsSubsetOfTest, WorksForEmpty) {
5735  vector<int> numbers;
5736  vector<int> expected;
5737  EXPECT_THAT(numbers, IsSubsetOf(expected));
5738  expected.push_back(1);
5739  EXPECT_THAT(numbers, IsSubsetOf(expected));
5740  expected.clear();
5741  numbers.push_back(1);
5742  numbers.push_back(2);
5743  EXPECT_THAT(numbers, Not(IsSubsetOf(expected)));
5744  expected.push_back(1);
5745  EXPECT_THAT(numbers, Not(IsSubsetOf(expected)));
5746  expected.push_back(2);
5747  EXPECT_THAT(numbers, IsSubsetOf(expected));
5748  expected.push_back(3);
5749  EXPECT_THAT(numbers, IsSubsetOf(expected));
5750 }
5751 
5752 TEST(IsSubsetOfTest, WorksForStreamlike) {
5753  const int a[5] = {1, 2};
5754  Streamlike<int> s(std::begin(a), std::end(a));
5755 
5756  vector<int> expected;
5757  expected.push_back(1);
5758  EXPECT_THAT(s, Not(IsSubsetOf(expected)));
5759  expected.push_back(2);
5760  expected.push_back(5);
5761  EXPECT_THAT(s, IsSubsetOf(expected));
5762 }
5763 
5764 TEST(IsSubsetOfTest, TakesStlContainer) {
5765  const int actual[] = {3, 1, 2};
5766 
5767  ::std::list<int> expected;
5768  expected.push_back(1);
5769  expected.push_back(3);
5770  EXPECT_THAT(actual, Not(IsSubsetOf(expected)));
5771 
5772  expected.push_back(2);
5773  expected.push_back(4);
5774  EXPECT_THAT(actual, IsSubsetOf(expected));
5775 }
5776 
5777 TEST(IsSubsetOfTest, Describe) {
5778  typedef std::vector<int> IntVec;
5779  IntVec expected;
5780  expected.push_back(111);
5781  expected.push_back(222);
5782  expected.push_back(333);
5783 
5784  EXPECT_THAT(
5785  Describe<IntVec>(IsSubsetOf(expected)),
5786  Eq("an injection from elements to requirements exists such that:\n"
5787  " - an element is equal to 111\n"
5788  " - an element is equal to 222\n"
5789  " - an element is equal to 333"));
5790 }
5791 
5792 TEST(IsSubsetOfTest, DescribeNegation) {
5793  typedef std::vector<int> IntVec;
5794  IntVec expected;
5795  expected.push_back(111);
5796  expected.push_back(222);
5797  expected.push_back(333);
5798  EXPECT_THAT(
5799  DescribeNegation<IntVec>(IsSubsetOf(expected)),
5800  Eq("no injection from elements to requirements exists such that:\n"
5801  " - an element is equal to 111\n"
5802  " - an element is equal to 222\n"
5803  " - an element is equal to 333"));
5804 }
5805 
5806 TEST(IsSubsetOfTest, MatchAndExplain) {
5807  std::vector<int> v;
5808  v.push_back(2);
5809  v.push_back(3);
5810  std::vector<int> expected;
5811  expected.push_back(1);
5812  expected.push_back(2);
5813  StringMatchResultListener listener;
5814  ASSERT_FALSE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
5815  << listener.str();
5816  EXPECT_THAT(listener.str(),
5817  Eq("where the following elements don't match any matchers:\n"
5818  "element #1: 3"));
5819 
5820  expected.push_back(3);
5821  listener.Clear();
5822  ASSERT_TRUE(ExplainMatchResult(IsSubsetOf(expected), v, &listener))
5823  << listener.str();
5824  EXPECT_THAT(listener.str(), Eq("where:\n"
5825  " - element #0 is matched by matcher #1,\n"
5826  " - element #1 is matched by matcher #2"));
5827 }
5828 
5829 TEST(IsSubsetOfTest, WorksForRhsInitializerList) {
5830  const int numbers[] = {1, 2, 3};
5831  EXPECT_THAT(numbers, IsSubsetOf({1, 2, 3, 4}));
5832  EXPECT_THAT(numbers, Not(IsSubsetOf({1, 2})));
5833 }
5834 
5835 TEST(IsSubsetOfTest, WorksWithMoveOnly) {
5836  ContainerHelper helper;
5837  EXPECT_CALL(helper, Call(IsSubsetOf({Pointee(1), Pointee(2)})));
5838  helper.Call(MakeUniquePtrs({1}));
5839  EXPECT_CALL(helper, Call(Not(IsSubsetOf({Pointee(1)}))));
5840  helper.Call(MakeUniquePtrs({2}));
5841 }
5842 
5843 // Tests using ElementsAre() and ElementsAreArray() with stream-like
5844 // "containers".
5845 
5846 TEST(ElemensAreStreamTest, WorksForStreamlike) {
5847  const int a[5] = {1, 2, 3, 4, 5};
5848  Streamlike<int> s(std::begin(a), std::end(a));
5849  EXPECT_THAT(s, ElementsAre(1, 2, 3, 4, 5));
5850  EXPECT_THAT(s, Not(ElementsAre(2, 1, 4, 5, 3)));
5851 }
5852 
5853 TEST(ElemensAreArrayStreamTest, WorksForStreamlike) {
5854  const int a[5] = {1, 2, 3, 4, 5};
5855  Streamlike<int> s(std::begin(a), std::end(a));
5856 
5857  vector<int> expected;
5858  expected.push_back(1);
5859  expected.push_back(2);
5860  expected.push_back(3);
5861  expected.push_back(4);
5862  expected.push_back(5);
5863  EXPECT_THAT(s, ElementsAreArray(expected));
5864 
5865  expected[3] = 0;
5866  EXPECT_THAT(s, Not(ElementsAreArray(expected)));
5867 }
5868 
5869 TEST(ElementsAreTest, WorksWithUncopyable) {
5870  Uncopyable objs[2];
5871  objs[0].set_value(-3);
5872  objs[1].set_value(1);
5873  EXPECT_THAT(objs, ElementsAre(UncopyableIs(-3), Truly(ValueIsPositive)));
5874 }
5875 
5876 TEST(ElementsAreTest, WorksWithMoveOnly) {
5877  ContainerHelper helper;
5878  EXPECT_CALL(helper, Call(ElementsAre(Pointee(1), Pointee(2))));
5879  helper.Call(MakeUniquePtrs({1, 2}));
5880 
5881  EXPECT_CALL(helper, Call(ElementsAreArray({Pointee(3), Pointee(4)})));
5882  helper.Call(MakeUniquePtrs({3, 4}));
5883 }
5884 
5885 TEST(ElementsAreTest, TakesStlContainer) {
5886  const int actual[] = {3, 1, 2};
5887 
5888  ::std::list<int> expected;
5889  expected.push_back(3);
5890  expected.push_back(1);
5891  expected.push_back(2);
5892  EXPECT_THAT(actual, ElementsAreArray(expected));
5893 
5894  expected.push_back(4);
5895  EXPECT_THAT(actual, Not(ElementsAreArray(expected)));
5896 }
5897 
5898 // Tests for UnorderedElementsAreArray()
5899 
5900 TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) {
5901  const int a[] = {0, 1, 2, 3, 4};
5902  std::vector<int> s(std::begin(a), std::end(a));
5903  do {
5904  StringMatchResultListener listener;
5906  s, &listener)) << listener.str();
5907  } while (std::next_permutation(s.begin(), s.end()));
5908 }
5909 
5910 TEST(UnorderedElementsAreArrayTest, VectorBool) {
5911  const bool a[] = {0, 1, 0, 1, 1};
5912  const bool b[] = {1, 0, 1, 1, 0};
5913  std::vector<bool> expected(std::begin(a), std::end(a));
5914  std::vector<bool> actual(std::begin(b), std::end(b));
5915  StringMatchResultListener listener;
5917  actual, &listener)) << listener.str();
5918 }
5919 
5920 TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) {
5921  // Streamlike 'container' provides only minimal iterator support.
5922  // Its iterators are tagged with input_iterator_tag, and it has no
5923  // size() or empty() methods.
5924  const int a[5] = {2, 1, 4, 5, 3};
5925  Streamlike<int> s(std::begin(a), std::end(a));
5926 
5927  ::std::vector<int> expected;
5928  expected.push_back(1);
5929  expected.push_back(2);
5930  expected.push_back(3);
5931  expected.push_back(4);
5932  expected.push_back(5);
5933  EXPECT_THAT(s, UnorderedElementsAreArray(expected));
5934 
5935  expected.push_back(6);
5936  EXPECT_THAT(s, Not(UnorderedElementsAreArray(expected)));
5937 }
5938 
5939 TEST(UnorderedElementsAreArrayTest, TakesStlContainer) {
5940  const int actual[] = {3, 1, 2};
5941 
5942  ::std::list<int> expected;
5943  expected.push_back(1);
5944  expected.push_back(2);
5945  expected.push_back(3);
5946  EXPECT_THAT(actual, UnorderedElementsAreArray(expected));
5947 
5948  expected.push_back(4);
5949  EXPECT_THAT(actual, Not(UnorderedElementsAreArray(expected)));
5950 }
5951 
5952 
5953 TEST(UnorderedElementsAreArrayTest, TakesInitializerList) {
5954  const int a[5] = {2, 1, 4, 5, 3};
5955  EXPECT_THAT(a, UnorderedElementsAreArray({1, 2, 3, 4, 5}));
5956  EXPECT_THAT(a, Not(UnorderedElementsAreArray({1, 2, 3, 4, 6})));
5957 }
5958 
5959 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfCStrings) {
5960  const std::string a[5] = {"a", "b", "c", "d", "e"};
5961  EXPECT_THAT(a, UnorderedElementsAreArray({"a", "b", "c", "d", "e"}));
5962  EXPECT_THAT(a, Not(UnorderedElementsAreArray({"a", "b", "c", "d", "ef"})));
5963 }
5964 
5965 TEST(UnorderedElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
5966  const int a[5] = {2, 1, 4, 5, 3};
5968  {Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
5970  {Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
5971 }
5972 
5973 TEST(UnorderedElementsAreArrayTest,
5974  TakesInitializerListOfDifferentTypedMatchers) {
5975  const int a[5] = {2, 1, 4, 5, 3};
5976  // The compiler cannot infer the type of the initializer list if its
5977  // elements have different types. We must explicitly specify the
5978  // unified element type in this case.
5979  EXPECT_THAT(a, UnorderedElementsAreArray<Matcher<int> >(
5980  {Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
5981  EXPECT_THAT(a, Not(UnorderedElementsAreArray<Matcher<int> >(
5982  {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
5983 }
5984 
5985 
5986 TEST(UnorderedElementsAreArrayTest, WorksWithMoveOnly) {
5987  ContainerHelper helper;
5988  EXPECT_CALL(helper,
5990  helper.Call(MakeUniquePtrs({2, 1}));
5991 }
5992 
5993 class UnorderedElementsAreTest : public testing::Test {
5994  protected:
5995  typedef std::vector<int> IntVec;
5996 };
5997 
5998 TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) {
5999  Uncopyable objs[2];
6000  objs[0].set_value(-3);
6001  objs[1].set_value(1);
6002  EXPECT_THAT(objs,
6003  UnorderedElementsAre(Truly(ValueIsPositive), UncopyableIs(-3)));
6004 }
6005 
6006 TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) {
6007  const int a[] = {1, 2, 3};
6008  std::vector<int> s(std::begin(a), std::end(a));
6009  do {
6010  StringMatchResultListener listener;
6012  s, &listener)) << listener.str();
6013  } while (std::next_permutation(s.begin(), s.end()));
6014 }
6015 
6016 TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) {
6017  const int a[] = {1, 2, 3};
6018  std::vector<int> s(std::begin(a), std::end(a));
6019  std::vector<Matcher<int> > mv;
6020  mv.push_back(1);
6021  mv.push_back(2);
6022  mv.push_back(2);
6023  // The element with value '3' matches nothing: fail fast.
6024  StringMatchResultListener listener;
6026  s, &listener)) << listener.str();
6027 }
6028 
6029 TEST_F(UnorderedElementsAreTest, WorksForStreamlike) {
6030  // Streamlike 'container' provides only minimal iterator support.
6031  // Its iterators are tagged with input_iterator_tag, and it has no
6032  // size() or empty() methods.
6033  const int a[5] = {2, 1, 4, 5, 3};
6034  Streamlike<int> s(std::begin(a), std::end(a));
6035 
6036  EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
6037  EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5)));
6038 }
6039 
6040 TEST_F(UnorderedElementsAreTest, WorksWithMoveOnly) {
6041  ContainerHelper helper;
6043  helper.Call(MakeUniquePtrs({2, 1}));
6044 }
6045 
6046 // One naive implementation of the matcher runs in O(N!) time, which is too
6047 // slow for many real-world inputs. This test shows that our matcher can match
6048 // 100 inputs very quickly (a few milliseconds). An O(100!) is 10^158
6049 // iterations and obviously effectively incomputable.
6050 // [ RUN ] UnorderedElementsAreTest.Performance
6051 // [ OK ] UnorderedElementsAreTest.Performance (4 ms)
6052 TEST_F(UnorderedElementsAreTest, Performance) {
6053  std::vector<int> s;
6054  std::vector<Matcher<int> > mv;
6055  for (int i = 0; i < 100; ++i) {
6056  s.push_back(i);
6057  mv.push_back(_);
6058  }
6059  mv[50] = Eq(0);
6060  StringMatchResultListener listener;
6062  s, &listener)) << listener.str();
6063 }
6064 
6065 // Another variant of 'Performance' with similar expectations.
6066 // [ RUN ] UnorderedElementsAreTest.PerformanceHalfStrict
6067 // [ OK ] UnorderedElementsAreTest.PerformanceHalfStrict (4 ms)
6068 TEST_F(UnorderedElementsAreTest, PerformanceHalfStrict) {
6069  std::vector<int> s;
6070  std::vector<Matcher<int> > mv;
6071  for (int i = 0; i < 100; ++i) {
6072  s.push_back(i);
6073  if (i & 1) {
6074  mv.push_back(_);
6075  } else {
6076  mv.push_back(i);
6077  }
6078  }
6079  StringMatchResultListener listener;
6081  s, &listener)) << listener.str();
6082 }
6083 
6084 TEST_F(UnorderedElementsAreTest, FailMessageCountWrong) {
6085  std::vector<int> v;
6086  v.push_back(4);
6087  StringMatchResultListener listener;
6089  v, &listener)) << listener.str();
6090  EXPECT_THAT(listener.str(), Eq("which has 1 element"));
6091 }
6092 
6093 TEST_F(UnorderedElementsAreTest, FailMessageCountWrongZero) {
6094  std::vector<int> v;
6095  StringMatchResultListener listener;
6097  v, &listener)) << listener.str();
6098  EXPECT_THAT(listener.str(), Eq(""));
6099 }
6100 
6101 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatchers) {
6102  std::vector<int> v;
6103  v.push_back(1);
6104  v.push_back(1);
6105  StringMatchResultListener listener;
6107  v, &listener)) << listener.str();
6108  EXPECT_THAT(
6109  listener.str(),
6110  Eq("where the following matchers don't match any elements:\n"
6111  "matcher #1: is equal to 2"));
6112 }
6113 
6114 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedElements) {
6115  std::vector<int> v;
6116  v.push_back(1);
6117  v.push_back(2);
6118  StringMatchResultListener listener;
6120  v, &listener)) << listener.str();
6121  EXPECT_THAT(
6122  listener.str(),
6123  Eq("where the following elements don't match any matchers:\n"
6124  "element #1: 2"));
6125 }
6126 
6127 TEST_F(UnorderedElementsAreTest, FailMessageUnmatchedMatcherAndElement) {
6128  std::vector<int> v;
6129  v.push_back(2);
6130  v.push_back(3);
6131  StringMatchResultListener listener;
6133  v, &listener)) << listener.str();
6134  EXPECT_THAT(
6135  listener.str(),
6136  Eq("where"
6137  " the following matchers don't match any elements:\n"
6138  "matcher #0: is equal to 1\n"
6139  "and"
6140  " where"
6141  " the following elements don't match any matchers:\n"
6142  "element #1: 3"));
6143 }
6144 
6145 // Test helper for formatting element, matcher index pairs in expectations.
6146 static std::string EMString(int element, int matcher) {
6147  stringstream ss;
6148  ss << "(element #" << element << ", matcher #" << matcher << ")";
6149  return ss.str();
6150 }
6151 
6152 TEST_F(UnorderedElementsAreTest, FailMessageImperfectMatchOnly) {
6153  // A situation where all elements and matchers have a match
6154  // associated with them, but the max matching is not perfect.
6155  std::vector<std::string> v;
6156  v.push_back("a");
6157  v.push_back("b");
6158  v.push_back("c");
6159  StringMatchResultListener listener;
6161  UnorderedElementsAre("a", "a", AnyOf("b", "c")), v, &listener))
6162  << listener.str();
6163 
6165  "where no permutation of the elements can satisfy all matchers, "
6166  "and the closest match is 2 of 3 matchers with the "
6167  "pairings:\n";
6168 
6169  // We have to be a bit loose here, because there are 4 valid max matches.
6170  EXPECT_THAT(
6171  listener.str(),
6172  AnyOf(prefix + "{\n " + EMString(0, 0) +
6173  ",\n " + EMString(1, 2) + "\n}",
6174  prefix + "{\n " + EMString(0, 1) +
6175  ",\n " + EMString(1, 2) + "\n}",
6176  prefix + "{\n " + EMString(0, 0) +
6177  ",\n " + EMString(2, 2) + "\n}",
6178  prefix + "{\n " + EMString(0, 1) +
6179  ",\n " + EMString(2, 2) + "\n}"));
6180 }
6181 
6182 TEST_F(UnorderedElementsAreTest, Describe) {
6183  EXPECT_THAT(Describe<IntVec>(UnorderedElementsAre()),
6184  Eq("is empty"));
6185  EXPECT_THAT(
6186  Describe<IntVec>(UnorderedElementsAre(345)),
6187  Eq("has 1 element and that element is equal to 345"));
6188  EXPECT_THAT(
6189  Describe<IntVec>(UnorderedElementsAre(111, 222, 333)),
6190  Eq("has 3 elements and there exists some permutation "
6191  "of elements such that:\n"
6192  " - element #0 is equal to 111, and\n"
6193  " - element #1 is equal to 222, and\n"
6194  " - element #2 is equal to 333"));
6195 }
6196 
6197 TEST_F(UnorderedElementsAreTest, DescribeNegation) {
6198  EXPECT_THAT(DescribeNegation<IntVec>(UnorderedElementsAre()),
6199  Eq("isn't empty"));
6200  EXPECT_THAT(
6201  DescribeNegation<IntVec>(UnorderedElementsAre(345)),
6202  Eq("doesn't have 1 element, or has 1 element that isn't equal to 345"));
6203  EXPECT_THAT(
6204  DescribeNegation<IntVec>(UnorderedElementsAre(123, 234, 345)),
6205  Eq("doesn't have 3 elements, or there exists no permutation "
6206  "of elements such that:\n"
6207  " - element #0 is equal to 123, and\n"
6208  " - element #1 is equal to 234, and\n"
6209  " - element #2 is equal to 345"));
6210 }
6211 
6212 namespace {
6213 
6214 // Used as a check on the more complex max flow method used in the
6215 // real testing::internal::FindMaxBipartiteMatching. This method is
6216 // compatible but runs in worst-case factorial time, so we only
6217 // use it in testing for small problem sizes.
6218 template <typename Graph>
6219 class BacktrackingMaxBPMState {
6220  public:
6221  // Does not take ownership of 'g'.
6222  explicit BacktrackingMaxBPMState(const Graph* g) : graph_(g) { }
6223 
6224  ElementMatcherPairs Compute() {
6225  if (graph_->LhsSize() == 0 || graph_->RhsSize() == 0) {
6226  return best_so_far_;
6227  }
6228  lhs_used_.assign(graph_->LhsSize(), kUnused);
6229  rhs_used_.assign(graph_->RhsSize(), kUnused);
6230  for (size_t irhs = 0; irhs < graph_->RhsSize(); ++irhs) {
6231  matches_.clear();
6232  RecurseInto(irhs);
6233  if (best_so_far_.size() == graph_->RhsSize())
6234  break;
6235  }
6236  return best_so_far_;
6237  }
6238 
6239  private:
6240  static const size_t kUnused = static_cast<size_t>(-1);
6241 
6242  void PushMatch(size_t lhs, size_t rhs) {
6243  matches_.push_back(ElementMatcherPair(lhs, rhs));
6244  lhs_used_[lhs] = rhs;
6245  rhs_used_[rhs] = lhs;
6246  if (matches_.size() > best_so_far_.size()) {
6248  }
6249  }
6250 
6251  void PopMatch() {
6252  const ElementMatcherPair& back = matches_.back();
6253  lhs_used_[back.first] = kUnused;
6254  rhs_used_[back.second] = kUnused;
6255  matches_.pop_back();
6256  }
6257 
6258  bool RecurseInto(size_t irhs) {
6259  if (rhs_used_[irhs] != kUnused) {
6260  return true;
6261  }
6262  for (size_t ilhs = 0; ilhs < graph_->LhsSize(); ++ilhs) {
6263  if (lhs_used_[ilhs] != kUnused) {
6264  continue;
6265  }
6266  if (!graph_->HasEdge(ilhs, irhs)) {
6267  continue;
6268  }
6269  PushMatch(ilhs, irhs);
6270  if (best_so_far_.size() == graph_->RhsSize()) {
6271  return false;
6272  }
6273  for (size_t mi = irhs + 1; mi < graph_->RhsSize(); ++mi) {
6274  if (!RecurseInto(mi)) return false;
6275  }
6276  PopMatch();
6277  }
6278  return true;
6279  }
6280 
6281  const Graph* graph_; // not owned
6282  std::vector<size_t> lhs_used_;
6283  std::vector<size_t> rhs_used_;
6286 };
6287 
6288 template <typename Graph>
6290 
6291 } // namespace
6292 
6293 // Implement a simple backtracking algorithm to determine if it is possible
6294 // to find one element per matcher, without reusing elements.
6295 template <typename Graph>
6297 FindBacktrackingMaxBPM(const Graph& g) {
6298  return BacktrackingMaxBPMState<Graph>(&g).Compute();
6299 }
6300 
6301 class BacktrackingBPMTest : public ::testing::Test { };
6302 
6303 // Tests the MaxBipartiteMatching algorithm with square matrices.
6304 // The single int param is the # of nodes on each of the left and right sides.
6305 class BipartiteTest : public ::testing::TestWithParam<size_t> {};
6306 
6307 // Verify all match graphs up to some moderate number of edges.
6308 TEST_P(BipartiteTest, Exhaustive) {
6309  size_t nodes = GetParam();
6310  MatchMatrix graph(nodes, nodes);
6311  do {
6312  ElementMatcherPairs matches =
6314  EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(), matches.size())
6315  << "graph: " << graph.DebugString();
6316  // Check that all elements of matches are in the graph.
6317  // Check that elements of first and second are unique.
6318  std::vector<bool> seen_element(graph.LhsSize());
6319  std::vector<bool> seen_matcher(graph.RhsSize());
6320  SCOPED_TRACE(PrintToString(matches));
6321  for (size_t i = 0; i < matches.size(); ++i) {
6322  size_t ilhs = matches[i].first;
6323  size_t irhs = matches[i].second;
6324  EXPECT_TRUE(graph.HasEdge(ilhs, irhs));
6325  EXPECT_FALSE(seen_element[ilhs]);
6326  EXPECT_FALSE(seen_matcher[irhs]);
6327  seen_element[ilhs] = true;
6328  seen_matcher[irhs] = true;
6329  }
6330  } while (graph.NextGraph());
6331 }
6332 
6333 INSTANTIATE_TEST_SUITE_P(AllGraphs, BipartiteTest,
6334  ::testing::Range(size_t{0}, size_t{5}));
6335 
6336 // Parameterized by a pair interpreted as (LhsSize, RhsSize).
6337 class BipartiteNonSquareTest
6338  : public ::testing::TestWithParam<std::pair<size_t, size_t> > {
6339 };
6340 
6341 TEST_F(BipartiteNonSquareTest, SimpleBacktracking) {
6342  // .......
6343  // 0:-----\ :
6344  // 1:---\ | :
6345  // 2:---\ | :
6346  // 3:-\ | | :
6347  // :.......:
6348  // 0 1 2
6349  MatchMatrix g(4, 3);
6350  constexpr std::array<std::array<size_t, 2>, 4> kEdges = {
6351  {{{0, 2}}, {{1, 1}}, {{2, 1}}, {{3, 0}}}};
6352  for (size_t i = 0; i < kEdges.size(); ++i) {
6353  g.SetEdge(kEdges[i][0], kEdges[i][1], true);
6354  }
6355  EXPECT_THAT(FindBacktrackingMaxBPM(g),
6356  ElementsAre(Pair(3, 0),
6357  Pair(AnyOf(1, 2), 1),
6358  Pair(0, 2))) << g.DebugString();
6359 }
6360 
6361 // Verify a few nonsquare matrices.
6362 TEST_P(BipartiteNonSquareTest, Exhaustive) {
6363  size_t nlhs = GetParam().first;
6364  size_t nrhs = GetParam().second;
6365  MatchMatrix graph(nlhs, nrhs);
6366  do {
6367  EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(),
6369  << "graph: " << graph.DebugString()
6370  << "\nbacktracking: "
6371  << PrintToString(FindBacktrackingMaxBPM(graph))
6372  << "\nmax flow: "
6374  } while (graph.NextGraph());
6375 }
6376 
6377 INSTANTIATE_TEST_SUITE_P(AllGraphs, BipartiteNonSquareTest,
6379  std::make_pair(1, 2),
6380  std::make_pair(2, 1),
6381  std::make_pair(3, 2),
6382  std::make_pair(2, 3),
6383  std::make_pair(4, 1),
6384  std::make_pair(1, 4),
6385  std::make_pair(4, 3),
6386  std::make_pair(3, 4)));
6387 
6388 class BipartiteRandomTest
6389  : public ::testing::TestWithParam<std::pair<int, int> > {
6390 };
6391 
6392 // Verifies a large sample of larger graphs.
6393 TEST_P(BipartiteRandomTest, LargerNets) {
6394  int nodes = GetParam().first;
6395  int iters = GetParam().second;
6396  MatchMatrix graph(static_cast<size_t>(nodes), static_cast<size_t>(nodes));
6397 
6398  auto seed = static_cast<uint32_t>(GTEST_FLAG_GET(random_seed));
6399  if (seed == 0) {
6400  seed = static_cast<uint32_t>(time(nullptr));
6401  }
6402 
6403  for (; iters > 0; --iters, ++seed) {
6404  srand(static_cast<unsigned int>(seed));
6405  graph.Randomize();
6406  EXPECT_EQ(FindBacktrackingMaxBPM(graph).size(),
6408  << " graph: " << graph.DebugString()
6409  << "\nTo reproduce the failure, rerun the test with the flag"
6410  " --" << GTEST_FLAG_PREFIX_ << "random_seed=" << seed;
6411  }
6412 }
6413 
6414 // Test argument is a std::pair<int, int> representing (nodes, iters).
6415 INSTANTIATE_TEST_SUITE_P(Samples, BipartiteRandomTest,
6417  std::make_pair(5, 10000),
6418  std::make_pair(6, 5000),
6419  std::make_pair(7, 2000),
6420  std::make_pair(8, 500),
6421  std::make_pair(9, 100)));
6422 
6423 // Tests IsReadableTypeName().
6424 
6425 TEST(IsReadableTypeNameTest, ReturnsTrueForShortNames) {
6427  EXPECT_TRUE(IsReadableTypeName("const unsigned char*"));
6428  EXPECT_TRUE(IsReadableTypeName("MyMap<int, void*>"));
6429  EXPECT_TRUE(IsReadableTypeName("void (*)(int, bool)"));
6430 }
6431 
6432 TEST(IsReadableTypeNameTest, ReturnsTrueForLongNonTemplateNonFunctionNames) {
6433  EXPECT_TRUE(IsReadableTypeName("my_long_namespace::MyClassName"));
6434  EXPECT_TRUE(IsReadableTypeName("int [5][6][7][8][9][10][11]"));
6435  EXPECT_TRUE(IsReadableTypeName("my_namespace::MyOuterClass::MyInnerClass"));
6436 }
6437 
6438 TEST(IsReadableTypeNameTest, ReturnsFalseForLongTemplateNames) {
6439  EXPECT_FALSE(
6440  IsReadableTypeName("basic_string<char, std::char_traits<char> >"));
6441  EXPECT_FALSE(IsReadableTypeName("std::vector<int, std::alloc_traits<int> >"));
6442 }
6443 
6444 TEST(IsReadableTypeNameTest, ReturnsFalseForLongFunctionTypeNames) {
6445  EXPECT_FALSE(IsReadableTypeName("void (&)(int, bool, char, float)"));
6446 }
6447 
6448 // Tests FormatMatcherDescription().
6449 
6450 TEST(FormatMatcherDescriptionTest, WorksForEmptyDescription) {
6451  EXPECT_EQ("is even",
6452  FormatMatcherDescription(false, "IsEven", {}, Strings()));
6453  EXPECT_EQ("not (is even)",
6454  FormatMatcherDescription(true, "IsEven", {}, Strings()));
6455 
6456  EXPECT_EQ("equals (a: 5)",
6457  FormatMatcherDescription(false, "Equals", {"a"}, {"5"}));
6458 
6459  EXPECT_EQ(
6460  "is in range (a: 5, b: 8)",
6461  FormatMatcherDescription(false, "IsInRange", {"a", "b"}, {"5", "8"}));
6462 }
6463 
6464 // Tests PolymorphicMatcher::mutable_impl().
6465 TEST(PolymorphicMatcherTest, CanAccessMutableImpl) {
6466  PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
6467  DivisibleByImpl& impl = m.mutable_impl();
6468  EXPECT_EQ(42, impl.divider());
6469 
6470  impl.set_divider(0);
6471  EXPECT_EQ(0, m.mutable_impl().divider());
6472 }
6473 
6474 // Tests PolymorphicMatcher::impl().
6475 TEST(PolymorphicMatcherTest, CanAccessImpl) {
6476  const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42));
6477  const DivisibleByImpl& impl = m.impl();
6478  EXPECT_EQ(42, impl.divider());
6479 }
6480 
6481 TEST(MatcherTupleTest, ExplainsMatchFailure) {
6482  stringstream ss1;
6484  std::make_tuple(Matcher<char>(Eq('a')), GreaterThan(5)),
6485  std::make_tuple('a', 10), &ss1);
6486  EXPECT_EQ("", ss1.str()); // Successful match.
6487 
6488  stringstream ss2;
6490  std::make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
6491  std::make_tuple(2, 'b'), &ss2);
6492  EXPECT_EQ(" Expected arg #0: is > 5\n"
6493  " Actual: 2, which is 3 less than 5\n"
6494  " Expected arg #1: is equal to 'a' (97, 0x61)\n"
6495  " Actual: 'b' (98, 0x62)\n",
6496  ss2.str()); // Failed match where both arguments need explanation.
6497 
6498  stringstream ss3;
6500  std::make_tuple(GreaterThan(5), Matcher<char>(Eq('a'))),
6501  std::make_tuple(2, 'a'), &ss3);
6502  EXPECT_EQ(" Expected arg #0: is > 5\n"
6503  " Actual: 2, which is 3 less than 5\n",
6504  ss3.str()); // Failed match where only one argument needs
6505  // explanation.
6506 }
6507 
6508 // Tests Each().
6509 
6510 TEST(EachTest, ExplainsMatchResultCorrectly) {
6511  set<int> a; // empty
6512 
6513  Matcher<set<int> > m = Each(2);
6514  EXPECT_EQ("", Explain(m, a));
6515 
6516  Matcher<const int(&)[1]> n = Each(1); // NOLINT
6517 
6518  const int b[1] = {1};
6519  EXPECT_EQ("", Explain(n, b));
6520 
6521  n = Each(3);
6522  EXPECT_EQ("whose element #0 doesn't match", Explain(n, b));
6523 
6524  a.insert(1);
6525  a.insert(2);
6526  a.insert(3);
6527  m = Each(GreaterThan(0));
6528  EXPECT_EQ("", Explain(m, a));
6529 
6530  m = Each(GreaterThan(10));
6531  EXPECT_EQ("whose element #0 doesn't match, which is 9 less than 10",
6532  Explain(m, a));
6533 }
6534 
6535 TEST(EachTest, DescribesItselfCorrectly) {
6536  Matcher<vector<int> > m = Each(1);
6537  EXPECT_EQ("only contains elements that is equal to 1", Describe(m));
6538 
6539  Matcher<vector<int> > m2 = Not(m);
6540  EXPECT_EQ("contains some element that isn't equal to 1", Describe(m2));
6541 }
6542 
6543 TEST(EachTest, MatchesVectorWhenAllElementsMatch) {
6544  vector<int> some_vector;
6545  EXPECT_THAT(some_vector, Each(1));
6546  some_vector.push_back(3);
6547  EXPECT_THAT(some_vector, Not(Each(1)));
6548  EXPECT_THAT(some_vector, Each(3));
6549  some_vector.push_back(1);
6550  some_vector.push_back(2);
6551  EXPECT_THAT(some_vector, Not(Each(3)));
6552  EXPECT_THAT(some_vector, Each(Lt(3.5)));
6553 
6554  vector<std::string> another_vector;
6555  another_vector.push_back("fee");
6556  EXPECT_THAT(another_vector, Each(std::string("fee")));
6557  another_vector.push_back("fie");
6558  another_vector.push_back("foe");
6559  another_vector.push_back("fum");
6560  EXPECT_THAT(another_vector, Not(Each(std::string("fee"))));
6561 }
6562 
6563 TEST(EachTest, MatchesMapWhenAllElementsMatch) {
6564  map<const char*, int> my_map;
6565  const char* bar = "a string";
6566  my_map[bar] = 2;
6567  EXPECT_THAT(my_map, Each(make_pair(bar, 2)));
6568 
6569  map<std::string, int> another_map;
6570  EXPECT_THAT(another_map, Each(make_pair(std::string("fee"), 1)));
6571  another_map["fee"] = 1;
6572  EXPECT_THAT(another_map, Each(make_pair(std::string("fee"), 1)));
6573  another_map["fie"] = 2;
6574  another_map["foe"] = 3;
6575  another_map["fum"] = 4;
6576  EXPECT_THAT(another_map, Not(Each(make_pair(std::string("fee"), 1))));
6577  EXPECT_THAT(another_map, Not(Each(make_pair(std::string("fum"), 1))));
6578  EXPECT_THAT(another_map, Each(Pair(_, Gt(0))));
6579 }
6580 
6581 TEST(EachTest, AcceptsMatcher) {
6582  const int a[] = {1, 2, 3};
6583  EXPECT_THAT(a, Each(Gt(0)));
6584  EXPECT_THAT(a, Not(Each(Gt(1))));
6585 }
6586 
6587 TEST(EachTest, WorksForNativeArrayAsTuple) {
6588  const int a[] = {1, 2};
6589  const int* const pointer = a;
6590  EXPECT_THAT(std::make_tuple(pointer, 2), Each(Gt(0)));
6591  EXPECT_THAT(std::make_tuple(pointer, 2), Not(Each(Gt(1))));
6592 }
6593 
6594 TEST(EachTest, WorksWithMoveOnly) {
6595  ContainerHelper helper;
6596  EXPECT_CALL(helper, Call(Each(Pointee(Gt(0)))));
6597  helper.Call(MakeUniquePtrs({1, 2}));
6598 }
6599 
6600 // For testing Pointwise().
6601 class IsHalfOfMatcher {
6602  public:
6603  template <typename T1, typename T2>
6604  bool MatchAndExplain(const std::tuple<T1, T2>& a_pair,
6605  MatchResultListener* listener) const {
6606  if (std::get<0>(a_pair) == std::get<1>(a_pair) / 2) {
6607  *listener << "where the second is " << std::get<1>(a_pair);
6608  return true;
6609  } else {
6610  *listener << "where the second/2 is " << std::get<1>(a_pair) / 2;
6611  return false;
6612  }
6613  }
6614 
6615  void DescribeTo(ostream* os) const {
6616  *os << "are a pair where the first is half of the second";
6617  }
6618 
6619  void DescribeNegationTo(ostream* os) const {
6620  *os << "are a pair where the first isn't half of the second";
6621  }
6622 };
6623 
6624 PolymorphicMatcher<IsHalfOfMatcher> IsHalfOf() {
6625  return MakePolymorphicMatcher(IsHalfOfMatcher());
6626 }
6627 
6628 TEST(PointwiseTest, DescribesSelf) {
6629  vector<int> rhs;
6630  rhs.push_back(1);
6631  rhs.push_back(2);
6632  rhs.push_back(3);
6633  const Matcher<const vector<int>&> m = Pointwise(IsHalfOf(), rhs);
6634  EXPECT_EQ("contains 3 values, where each value and its corresponding value "
6635  "in { 1, 2, 3 } are a pair where the first is half of the second",
6636  Describe(m));
6637  EXPECT_EQ("doesn't contain exactly 3 values, or contains a value x at some "
6638  "index i where x and the i-th value of { 1, 2, 3 } are a pair "
6639  "where the first isn't half of the second",
6640  DescribeNegation(m));
6641 }
6642 
6643 TEST(PointwiseTest, MakesCopyOfRhs) {
6644  list<signed char> rhs;
6645  rhs.push_back(2);
6646  rhs.push_back(4);
6647 
6648  int lhs[] = {1, 2};
6649  const Matcher<const int (&)[2]> m = Pointwise(IsHalfOf(), rhs);
6650  EXPECT_THAT(lhs, m);
6651 
6652  // Changing rhs now shouldn't affect m, which made a copy of rhs.
6653  rhs.push_back(6);
6654  EXPECT_THAT(lhs, m);
6655 }
6656 
6657 TEST(PointwiseTest, WorksForLhsNativeArray) {
6658  const int lhs[] = {1, 2, 3};
6659  vector<int> rhs;
6660  rhs.push_back(2);
6661  rhs.push_back(4);
6662  rhs.push_back(6);
6663  EXPECT_THAT(lhs, Pointwise(Lt(), rhs));
6664  EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
6665 }
6666 
6667 TEST(PointwiseTest, WorksForRhsNativeArray) {
6668  const int rhs[] = {1, 2, 3};
6669  vector<int> lhs;
6670  lhs.push_back(2);
6671  lhs.push_back(4);
6672  lhs.push_back(6);
6673  EXPECT_THAT(lhs, Pointwise(Gt(), rhs));
6674  EXPECT_THAT(lhs, Not(Pointwise(Lt(), rhs)));
6675 }
6676 
6677 // Test is effective only with sanitizers.
6678 TEST(PointwiseTest, WorksForVectorOfBool) {
6679  vector<bool> rhs(3, false);
6680  rhs[1] = true;
6681  vector<bool> lhs = rhs;
6682  EXPECT_THAT(lhs, Pointwise(Eq(), rhs));
6683  rhs[0] = true;
6684  EXPECT_THAT(lhs, Not(Pointwise(Eq(), rhs)));
6685 }
6686 
6687 
6688 TEST(PointwiseTest, WorksForRhsInitializerList) {
6689  const vector<int> lhs{2, 4, 6};
6690  EXPECT_THAT(lhs, Pointwise(Gt(), {1, 2, 3}));
6691  EXPECT_THAT(lhs, Not(Pointwise(Lt(), {3, 3, 7})));
6692 }
6693 
6694 
6695 TEST(PointwiseTest, RejectsWrongSize) {
6696  const double lhs[2] = {1, 2};
6697  const int rhs[1] = {0};
6698  EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs)));
6699  EXPECT_EQ("which contains 2 values",
6700  Explain(Pointwise(Gt(), rhs), lhs));
6701 
6702  const int rhs2[3] = {0, 1, 2};
6703  EXPECT_THAT(lhs, Not(Pointwise(Gt(), rhs2)));
6704 }
6705 
6706 TEST(PointwiseTest, RejectsWrongContent) {
6707  const double lhs[3] = {1, 2, 3};
6708  const int rhs[3] = {2, 6, 4};
6709  EXPECT_THAT(lhs, Not(Pointwise(IsHalfOf(), rhs)));
6710  EXPECT_EQ("where the value pair (2, 6) at index #1 don't match, "
6711  "where the second/2 is 3",
6712  Explain(Pointwise(IsHalfOf(), rhs), lhs));
6713 }
6714 
6715 TEST(PointwiseTest, AcceptsCorrectContent) {
6716  const double lhs[3] = {1, 2, 3};
6717  const int rhs[3] = {2, 4, 6};
6718  EXPECT_THAT(lhs, Pointwise(IsHalfOf(), rhs));
6719  EXPECT_EQ("", Explain(Pointwise(IsHalfOf(), rhs), lhs));
6720 }
6721 
6722 TEST(PointwiseTest, AllowsMonomorphicInnerMatcher) {
6723  const double lhs[3] = {1, 2, 3};
6724  const int rhs[3] = {2, 4, 6};
6725  const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6726  EXPECT_THAT(lhs, Pointwise(m1, rhs));
6727  EXPECT_EQ("", Explain(Pointwise(m1, rhs), lhs));
6728 
6729  // This type works as a std::tuple<const double&, const int&> can be
6730  // implicitly cast to std::tuple<double, int>.
6731  const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6732  EXPECT_THAT(lhs, Pointwise(m2, rhs));
6733  EXPECT_EQ("", Explain(Pointwise(m2, rhs), lhs));
6734 }
6735 
6736 MATCHER(PointeeEquals, "Points to an equal value") {
6737  return ExplainMatchResult(::testing::Pointee(::testing::get<1>(arg)),
6738  ::testing::get<0>(arg), result_listener);
6739 }
6740 
6741 TEST(PointwiseTest, WorksWithMoveOnly) {
6742  ContainerHelper helper;
6743  EXPECT_CALL(helper, Call(Pointwise(PointeeEquals(), std::vector<int>{1, 2})));
6744  helper.Call(MakeUniquePtrs({1, 2}));
6745 }
6746 
6747 TEST(UnorderedPointwiseTest, DescribesSelf) {
6748  vector<int> rhs;
6749  rhs.push_back(1);
6750  rhs.push_back(2);
6751  rhs.push_back(3);
6752  const Matcher<const vector<int>&> m = UnorderedPointwise(IsHalfOf(), rhs);
6753  EXPECT_EQ(
6754  "has 3 elements and there exists some permutation of elements such "
6755  "that:\n"
6756  " - element #0 and 1 are a pair where the first is half of the second, "
6757  "and\n"
6758  " - element #1 and 2 are a pair where the first is half of the second, "
6759  "and\n"
6760  " - element #2 and 3 are a pair where the first is half of the second",
6761  Describe(m));
6762  EXPECT_EQ(
6763  "doesn't have 3 elements, or there exists no permutation of elements "
6764  "such that:\n"
6765  " - element #0 and 1 are a pair where the first is half of the second, "
6766  "and\n"
6767  " - element #1 and 2 are a pair where the first is half of the second, "
6768  "and\n"
6769  " - element #2 and 3 are a pair where the first is half of the second",
6770  DescribeNegation(m));
6771 }
6772 
6773 TEST(UnorderedPointwiseTest, MakesCopyOfRhs) {
6774  list<signed char> rhs;
6775  rhs.push_back(2);
6776  rhs.push_back(4);
6777 
6778  int lhs[] = {2, 1};
6779  const Matcher<const int (&)[2]> m = UnorderedPointwise(IsHalfOf(), rhs);
6780  EXPECT_THAT(lhs, m);
6781 
6782  // Changing rhs now shouldn't affect m, which made a copy of rhs.
6783  rhs.push_back(6);
6784  EXPECT_THAT(lhs, m);
6785 }
6786 
6787 TEST(UnorderedPointwiseTest, WorksForLhsNativeArray) {
6788  const int lhs[] = {1, 2, 3};
6789  vector<int> rhs;
6790  rhs.push_back(4);
6791  rhs.push_back(6);
6792  rhs.push_back(2);
6793  EXPECT_THAT(lhs, UnorderedPointwise(Lt(), rhs));
6794  EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
6795 }
6796 
6797 TEST(UnorderedPointwiseTest, WorksForRhsNativeArray) {
6798  const int rhs[] = {1, 2, 3};
6799  vector<int> lhs;
6800  lhs.push_back(4);
6801  lhs.push_back(2);
6802  lhs.push_back(6);
6803  EXPECT_THAT(lhs, UnorderedPointwise(Gt(), rhs));
6804  EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), rhs)));
6805 }
6806 
6807 
6808 TEST(UnorderedPointwiseTest, WorksForRhsInitializerList) {
6809  const vector<int> lhs{2, 4, 6};
6810  EXPECT_THAT(lhs, UnorderedPointwise(Gt(), {5, 1, 3}));
6811  EXPECT_THAT(lhs, Not(UnorderedPointwise(Lt(), {1, 1, 7})));
6812 }
6813 
6814 
6815 TEST(UnorderedPointwiseTest, RejectsWrongSize) {
6816  const double lhs[2] = {1, 2};
6817  const int rhs[1] = {0};
6818  EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs)));
6819  EXPECT_EQ("which has 2 elements",
6820  Explain(UnorderedPointwise(Gt(), rhs), lhs));
6821 
6822  const int rhs2[3] = {0, 1, 2};
6823  EXPECT_THAT(lhs, Not(UnorderedPointwise(Gt(), rhs2)));
6824 }
6825 
6826 TEST(UnorderedPointwiseTest, RejectsWrongContent) {
6827  const double lhs[3] = {1, 2, 3};
6828  const int rhs[3] = {2, 6, 6};
6829  EXPECT_THAT(lhs, Not(UnorderedPointwise(IsHalfOf(), rhs)));
6830  EXPECT_EQ("where the following elements don't match any matchers:\n"
6831  "element #1: 2",
6832  Explain(UnorderedPointwise(IsHalfOf(), rhs), lhs));
6833 }
6834 
6835 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInSameOrder) {
6836  const double lhs[3] = {1, 2, 3};
6837  const int rhs[3] = {2, 4, 6};
6838  EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
6839 }
6840 
6841 TEST(UnorderedPointwiseTest, AcceptsCorrectContentInDifferentOrder) {
6842  const double lhs[3] = {1, 2, 3};
6843  const int rhs[3] = {6, 4, 2};
6844  EXPECT_THAT(lhs, UnorderedPointwise(IsHalfOf(), rhs));
6845 }
6846 
6847 TEST(UnorderedPointwiseTest, AllowsMonomorphicInnerMatcher) {
6848  const double lhs[3] = {1, 2, 3};
6849  const int rhs[3] = {4, 6, 2};
6850  const Matcher<std::tuple<const double&, const int&>> m1 = IsHalfOf();
6851  EXPECT_THAT(lhs, UnorderedPointwise(m1, rhs));
6852 
6853  // This type works as a std::tuple<const double&, const int&> can be
6854  // implicitly cast to std::tuple<double, int>.
6855  const Matcher<std::tuple<double, int>> m2 = IsHalfOf();
6856  EXPECT_THAT(lhs, UnorderedPointwise(m2, rhs));
6857 }
6858 
6859 TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
6860  ContainerHelper helper;
6861  EXPECT_CALL(helper, Call(UnorderedPointwise(PointeeEquals(),
6862  std::vector<int>{1, 2})));
6863  helper.Call(MakeUniquePtrs({2, 1}));
6864 }
6865 
6866 // Sample optional type implementation with minimal requirements for use with
6867 // Optional matcher.
6868 template <typename T>
6869 class SampleOptional {
6870  public:
6871  using value_type = T;
6872  explicit SampleOptional(T value)
6873  : value_(std::move(value)), has_value_(true) {}
6874  SampleOptional() : value_(), has_value_(false) {}
6875  operator bool() const { return has_value_; }
6876  const T& operator*() const { return value_; }
6877 
6878  private:
6879  T value_;
6881 };
6882 
6883 TEST(OptionalTest, DescribesSelf) {
6884  const Matcher<SampleOptional<int>> m = Optional(Eq(1));
6885  EXPECT_EQ("value is equal to 1", Describe(m));
6886 }
6887 
6888 TEST(OptionalTest, ExplainsSelf) {
6889  const Matcher<SampleOptional<int>> m = Optional(Eq(1));
6890  EXPECT_EQ("whose value 1 matches", Explain(m, SampleOptional<int>(1)));
6891  EXPECT_EQ("whose value 2 doesn't match", Explain(m, SampleOptional<int>(2)));
6892 }
6893 
6894 TEST(OptionalTest, MatchesNonEmptyOptional) {
6895  const Matcher<SampleOptional<int>> m1 = Optional(1);
6896  const Matcher<SampleOptional<int>> m2 = Optional(Eq(2));
6897  const Matcher<SampleOptional<int>> m3 = Optional(Lt(3));
6898  SampleOptional<int> opt(1);
6899  EXPECT_TRUE(m1.Matches(opt));
6900  EXPECT_FALSE(m2.Matches(opt));
6901  EXPECT_TRUE(m3.Matches(opt));
6902 }
6903 
6904 TEST(OptionalTest, DoesNotMatchNullopt) {
6905  const Matcher<SampleOptional<int>> m = Optional(1);
6906  SampleOptional<int> empty;
6907  EXPECT_FALSE(m.Matches(empty));
6908 }
6909 
6910 TEST(OptionalTest, WorksWithMoveOnly) {
6911  Matcher<SampleOptional<std::unique_ptr<int>>> m = Optional(Eq(nullptr));
6912  EXPECT_TRUE(m.Matches(SampleOptional<std::unique_ptr<int>>(nullptr)));
6913 }
6914 
6915 class SampleVariantIntString {
6916  public:
6917  SampleVariantIntString(int i) : i_(i), has_int_(true) {}
6918  SampleVariantIntString(const std::string& s) : s_(s), has_int_(false) {}
6919 
6920  template <typename T>
6921  friend bool holds_alternative(const SampleVariantIntString& value) {
6922  return value.has_int_ == std::is_same<T, int>::value;
6923  }
6924 
6925  template <typename T>
6926  friend const T& get(const SampleVariantIntString& value) {
6927  return value.get_impl(static_cast<T*>(nullptr));
6928  }
6929 
6930  private:
6931  const int& get_impl(int*) const { return i_; }
6932  const std::string& get_impl(std::string*) const { return s_; }
6933 
6934  int i_;
6935  std::string s_;
6936  bool has_int_;
6937 };
6938 
6939 TEST(VariantTest, DescribesSelf) {
6940  const Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6941  EXPECT_THAT(Describe(m), ContainsRegex("is a variant<> with value of type "
6942  "'.*' and the value is equal to 1"));
6943 }
6944 
6945 TEST(VariantTest, ExplainsSelf) {
6946  const Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6947  EXPECT_THAT(Explain(m, SampleVariantIntString(1)),
6948  ContainsRegex("whose value 1"));
6949  EXPECT_THAT(Explain(m, SampleVariantIntString("A")),
6950  HasSubstr("whose value is not of type '"));
6951  EXPECT_THAT(Explain(m, SampleVariantIntString(2)),
6952  "whose value 2 doesn't match");
6953 }
6954 
6955 TEST(VariantTest, FullMatch) {
6956  Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6957  EXPECT_TRUE(m.Matches(SampleVariantIntString(1)));
6958 
6959  m = VariantWith<std::string>(Eq("1"));
6960  EXPECT_TRUE(m.Matches(SampleVariantIntString("1")));
6961 }
6962 
6963 TEST(VariantTest, TypeDoesNotMatch) {
6964  Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6965  EXPECT_FALSE(m.Matches(SampleVariantIntString("1")));
6966 
6967  m = VariantWith<std::string>(Eq("1"));
6968  EXPECT_FALSE(m.Matches(SampleVariantIntString(1)));
6969 }
6970 
6971 TEST(VariantTest, InnerDoesNotMatch) {
6972  Matcher<SampleVariantIntString> m = VariantWith<int>(Eq(1));
6973  EXPECT_FALSE(m.Matches(SampleVariantIntString(2)));
6974 
6975  m = VariantWith<std::string>(Eq("1"));
6976  EXPECT_FALSE(m.Matches(SampleVariantIntString("2")));
6977 }
6978 
6979 class SampleAnyType {
6980  public:
6981  explicit SampleAnyType(int i) : index_(0), i_(i) {}
6982  explicit SampleAnyType(const std::string& s) : index_(1), s_(s) {}
6983 
6984  template <typename T>
6985  friend const T* any_cast(const SampleAnyType* any) {
6986  return any->get_impl(static_cast<T*>(nullptr));
6987  }
6988 
6989  private:
6990  int index_;
6991  int i_;
6992  std::string s_;
6993 
6994  const int* get_impl(int*) const { return index_ == 0 ? &i_ : nullptr; }
6995  const std::string* get_impl(std::string*) const {
6996  return index_ == 1 ? &s_ : nullptr;
6997  }
6998 };
6999 
7000 TEST(AnyWithTest, FullMatch) {
7001  Matcher<SampleAnyType> m = AnyWith<int>(Eq(1));
7002  EXPECT_TRUE(m.Matches(SampleAnyType(1)));
7003 }
7004 
7005 TEST(AnyWithTest, TestBadCastType) {
7006  Matcher<SampleAnyType> m = AnyWith<std::string>(Eq("fail"));
7007  EXPECT_FALSE(m.Matches(SampleAnyType(1)));
7008 }
7009 
7010 TEST(AnyWithTest, TestUseInContainers) {
7011  std::vector<SampleAnyType> a;
7012  a.emplace_back(1);
7013  a.emplace_back(2);
7014  a.emplace_back(3);
7015  EXPECT_THAT(
7016  a, ElementsAreArray({AnyWith<int>(1), AnyWith<int>(2), AnyWith<int>(3)}));
7017 
7018  std::vector<SampleAnyType> b;
7019  b.emplace_back("hello");
7020  b.emplace_back("merhaba");
7021  b.emplace_back("salut");
7022  EXPECT_THAT(b, ElementsAreArray({AnyWith<std::string>("hello"),
7023  AnyWith<std::string>("merhaba"),
7024  AnyWith<std::string>("salut")}));
7025 }
7026 TEST(AnyWithTest, TestCompare) {
7027  EXPECT_THAT(SampleAnyType(1), AnyWith<int>(Gt(0)));
7028 }
7029 
7030 TEST(AnyWithTest, DescribesSelf) {
7031  const Matcher<const SampleAnyType&> m = AnyWith<int>(Eq(1));
7032  EXPECT_THAT(Describe(m), ContainsRegex("is an 'any' type with value of type "
7033  "'.*' and the value is equal to 1"));
7034 }
7035 
7036 TEST(AnyWithTest, ExplainsSelf) {
7037  const Matcher<const SampleAnyType&> m = AnyWith<int>(Eq(1));
7038 
7039  EXPECT_THAT(Explain(m, SampleAnyType(1)), ContainsRegex("whose value 1"));
7040  EXPECT_THAT(Explain(m, SampleAnyType("A")),
7041  HasSubstr("whose value is not of type '"));
7042  EXPECT_THAT(Explain(m, SampleAnyType(2)), "whose value 2 doesn't match");
7043 }
7044 
7045 TEST(PointeeTest, WorksOnMoveOnlyType) {
7046  std::unique_ptr<int> p(new int(3));
7047  EXPECT_THAT(p, Pointee(Eq(3)));
7048  EXPECT_THAT(p, Not(Pointee(Eq(2))));
7049 }
7050 
7051 TEST(NotTest, WorksOnMoveOnlyType) {
7052  std::unique_ptr<int> p(new int(3));
7053  EXPECT_THAT(p, Pointee(Eq(3)));
7054  EXPECT_THAT(p, Not(Pointee(Eq(2))));
7055 }
7056 
7057 // Tests Args<k0, ..., kn>(m).
7058 
7059 TEST(ArgsTest, AcceptsZeroTemplateArg) {
7060  const std::tuple<int, bool> t(5, true);
7061  EXPECT_THAT(t, Args<>(Eq(std::tuple<>())));
7062  EXPECT_THAT(t, Not(Args<>(Ne(std::tuple<>()))));
7063 }
7064 
7065 TEST(ArgsTest, AcceptsOneTemplateArg) {
7066  const std::tuple<int, bool> t(5, true);
7067  EXPECT_THAT(t, Args<0>(Eq(std::make_tuple(5))));
7068  EXPECT_THAT(t, Args<1>(Eq(std::make_tuple(true))));
7069  EXPECT_THAT(t, Not(Args<1>(Eq(std::make_tuple(false)))));
7070 }
7071 
7072 TEST(ArgsTest, AcceptsTwoTemplateArgs) {
7073  const std::tuple<short, int, long> t(4, 5, 6L); // NOLINT
7074 
7075  EXPECT_THAT(t, (Args<0, 1>(Lt())));
7076  EXPECT_THAT(t, (Args<1, 2>(Lt())));
7077  EXPECT_THAT(t, Not(Args<0, 2>(Gt())));
7078 }
7079 
7080 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
7081  const std::tuple<short, int, long> t(4, 5, 6L); // NOLINT
7082  EXPECT_THAT(t, (Args<0, 0>(Eq())));
7083  EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
7084 }
7085 
7086 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
7087  const std::tuple<short, int, long> t(4, 5, 6L); // NOLINT
7088  EXPECT_THAT(t, (Args<2, 0>(Gt())));
7089  EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
7090 }
7091 
7092 MATCHER(SumIsZero, "") {
7093  return std::get<0>(arg) + std::get<1>(arg) + std::get<2>(arg) == 0;
7094 }
7095 
7096 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
7097  EXPECT_THAT(std::make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
7098  EXPECT_THAT(std::make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
7099 }
7100 
7101 TEST(ArgsTest, CanBeNested) {
7102  const std::tuple<short, int, long, int> t(4, 5, 6L, 6); // NOLINT
7103  EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
7104  EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
7105 }
7106 
7107 TEST(ArgsTest, CanMatchTupleByValue) {
7108  typedef std::tuple<char, int, int> Tuple3;
7109  const Matcher<Tuple3> m = Args<1, 2>(Lt());
7110  EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2)));
7111  EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2)));
7112 }
7113 
7114 TEST(ArgsTest, CanMatchTupleByReference) {
7115  typedef std::tuple<char, char, int> Tuple3;
7116  const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
7117  EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
7118  EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
7119 }
7120 
7121 // Validates that arg is printed as str.
7122 MATCHER_P(PrintsAs, str, "") {
7123  return testing::PrintToString(arg) == str;
7124 }
7125 
7126 TEST(ArgsTest, AcceptsTenTemplateArgs) {
7127  EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
7128  (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
7129  PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
7130  EXPECT_THAT(std::make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
7131  Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
7132  PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
7133 }
7134 
7135 TEST(ArgsTest, DescirbesSelfCorrectly) {
7136  const Matcher<std::tuple<int, bool, char> > m = Args<2, 0>(Lt());
7137  EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where "
7138  "the first < the second",
7139  Describe(m));
7140 }
7141 
7142 TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
7143  const Matcher<const std::tuple<int, bool, char, int>&> m =
7144  Args<0, 2, 3>(Args<2, 0>(Lt()));
7145  EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple "
7146  "whose fields (#2, #0) are a pair where the first < the second",
7147  Describe(m));
7148 }
7149 
7150 TEST(ArgsTest, DescribesNegationCorrectly) {
7151  const Matcher<std::tuple<int, char> > m = Args<1, 0>(Gt());
7152  EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair "
7153  "where the first > the second",
7154  DescribeNegation(m));
7155 }
7156 
7157 TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
7158  const Matcher<std::tuple<bool, int, int> > m = Args<1, 2>(Eq());
7159  EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
7160  Explain(m, std::make_tuple(false, 42, 42)));
7161  EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
7162  Explain(m, std::make_tuple(false, 42, 43)));
7163 }
7164 
7165 // For testing Args<>'s explanation.
7166 class LessThanMatcher : public MatcherInterface<std::tuple<char, int> > {
7167  public:
7168  void DescribeTo(::std::ostream* /*os*/) const override {}
7169 
7170  bool MatchAndExplain(std::tuple<char, int> value,
7171  MatchResultListener* listener) const override {
7172  const int diff = std::get<0>(value) - std::get<1>(value);
7173  if (diff > 0) {
7174  *listener << "where the first value is " << diff
7175  << " more than the second";
7176  }
7177  return diff < 0;
7178  }
7179 };
7180 
7181 Matcher<std::tuple<char, int> > LessThan() {
7182  return MakeMatcher(new LessThanMatcher);
7183 }
7184 
7185 TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
7186  const Matcher<std::tuple<char, int, int> > m = Args<0, 2>(LessThan());
7187  EXPECT_EQ(
7188  "whose fields (#0, #2) are ('a' (97, 0x61), 42), "
7189  "where the first value is 55 more than the second",
7190  Explain(m, std::make_tuple('a', 42, 42)));
7191  EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
7192  Explain(m, std::make_tuple('\0', 42, 43)));
7193 }
7194 
7195 class PredicateFormatterFromMatcherTest : public ::testing::Test {
7196  protected:
7197  enum Behavior { kInitialSuccess, kAlwaysFail, kFlaky };
7198 
7199  // A matcher that can return different results when used multiple times on the
7200  // same input. No real matcher should do this; but this lets us test that we
7201  // detect such behavior and fail appropriately.
7202  class MockMatcher : public MatcherInterface<Behavior> {
7203  public:
7204  bool MatchAndExplain(Behavior behavior,
7205  MatchResultListener* listener) const override {
7206  *listener << "[MatchAndExplain]";
7207  switch (behavior) {
7208  case kInitialSuccess:
7209  // The first call to MatchAndExplain should use a "not interested"
7210  // listener; so this is expected to return |true|. There should be no
7211  // subsequent calls.
7212  return !listener->IsInterested();
7213 
7214  case kAlwaysFail:
7215  return false;
7216 
7217  case kFlaky:
7218  // The first call to MatchAndExplain should use a "not interested"
7219  // listener; so this will return |false|. Subsequent calls should have
7220  // an "interested" listener; so this will return |true|, thus
7221  // simulating a flaky matcher.
7222  return listener->IsInterested();
7223  }
7224 
7225  GTEST_LOG_(FATAL) << "This should never be reached";
7226  return false;
7227  }
7228 
7229  void DescribeTo(ostream* os) const override { *os << "[DescribeTo]"; }
7230 
7231  void DescribeNegationTo(ostream* os) const override {
7232  *os << "[DescribeNegationTo]";
7233  }
7234  };
7235 
7236  AssertionResult RunPredicateFormatter(Behavior behavior) {
7237  auto matcher = MakeMatcher(new MockMatcher);
7238  PredicateFormatterFromMatcher<Matcher<Behavior>> predicate_formatter(
7239  matcher);
7240  return predicate_formatter("dummy-name", behavior);
7241  }
7242 };
7243 
7244 TEST_F(PredicateFormatterFromMatcherTest, ShortCircuitOnSuccess) {
7245  AssertionResult result = RunPredicateFormatter(kInitialSuccess);
7246  EXPECT_TRUE(result); // Implicit cast to bool.
7247  std::string expect;
7248  EXPECT_EQ(expect, result.message());
7249 }
7250 
7251 TEST_F(PredicateFormatterFromMatcherTest, NoShortCircuitOnFailure) {
7252  AssertionResult result = RunPredicateFormatter(kAlwaysFail);
7253  EXPECT_FALSE(result); // Implicit cast to bool.
7254  std::string expect =
7255  "Value of: dummy-name\nExpected: [DescribeTo]\n"
7256  " Actual: 1" +
7257  OfType(internal::GetTypeName<Behavior>()) + ", [MatchAndExplain]";
7258  EXPECT_EQ(expect, result.message());
7259 }
7260 
7261 TEST_F(PredicateFormatterFromMatcherTest, DetectsFlakyShortCircuit) {
7262  AssertionResult result = RunPredicateFormatter(kFlaky);
7263  EXPECT_FALSE(result); // Implicit cast to bool.
7264  std::string expect =
7265  "Value of: dummy-name\nExpected: [DescribeTo]\n"
7266  " The matcher failed on the initial attempt; but passed when rerun to "
7267  "generate the explanation.\n"
7268  " Actual: 2" +
7269  OfType(internal::GetTypeName<Behavior>()) + ", [MatchAndExplain]";
7270  EXPECT_EQ(expect, result.message());
7271 }
7272 
7273 // Tests for ElementsAre().
7274 
7275 TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
7276  Matcher<const vector<int>&> m = ElementsAre();
7277  EXPECT_EQ("is empty", Describe(m));
7278 }
7279 
7280 TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
7281  Matcher<vector<int>> m = ElementsAre(Gt(5));
7282  EXPECT_EQ("has 1 element that is > 5", Describe(m));
7283 }
7284 
7285 TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
7286  Matcher<list<std::string>> m = ElementsAre(StrEq("one"), "two");
7287  EXPECT_EQ(
7288  "has 2 elements where\n"
7289  "element #0 is equal to \"one\",\n"
7290  "element #1 is equal to \"two\"",
7291  Describe(m));
7292 }
7293 
7294 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
7295  Matcher<vector<int>> m = ElementsAre();
7296  EXPECT_EQ("isn't empty", DescribeNegation(m));
7297 }
7298 
7299 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElement) {
7300  Matcher<const list<int>&> m = ElementsAre(Gt(5));
7301  EXPECT_EQ(
7302  "doesn't have 1 element, or\n"
7303  "element #0 isn't > 5",
7304  DescribeNegation(m));
7305 }
7306 
7307 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
7308  Matcher<const list<std::string>&> m = ElementsAre("one", "two");
7309  EXPECT_EQ(
7310  "doesn't have 2 elements, or\n"
7311  "element #0 isn't equal to \"one\", or\n"
7312  "element #1 isn't equal to \"two\"",
7313  DescribeNegation(m));
7314 }
7315 
7316 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
7317  Matcher<const list<int>&> m = ElementsAre(1, Ne(2));
7318 
7319  list<int> test_list;
7320  test_list.push_back(1);
7321  test_list.push_back(3);
7322  EXPECT_EQ("", Explain(m, test_list)); // No need to explain anything.
7323 }
7324 
7325 TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
7326  Matcher<const vector<int>&> m =
7327  ElementsAre(GreaterThan(1), 0, GreaterThan(2));
7328 
7329  const int a[] = {10, 0, 100};
7330  vector<int> test_vector(std::begin(a), std::end(a));
7331  EXPECT_EQ(
7332  "whose element #0 matches, which is 9 more than 1,\n"
7333  "and whose element #2 matches, which is 98 more than 2",
7334  Explain(m, test_vector));
7335 }
7336 
7337 TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
7338  Matcher<const list<int>&> m = ElementsAre(1, 3);
7339 
7340  list<int> test_list;
7341  // No need to explain when the container is empty.
7342  EXPECT_EQ("", Explain(m, test_list));
7343 
7344  test_list.push_back(1);
7345  EXPECT_EQ("which has 1 element", Explain(m, test_list));
7346 }
7347 
7348 TEST(ElementsAreTest, CanExplainMismatchRightSize) {
7349  Matcher<const vector<int>&> m = ElementsAre(1, GreaterThan(5));
7350 
7351  vector<int> v;
7352  v.push_back(2);
7353  v.push_back(1);
7354  EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
7355 
7356  v[0] = 1;
7357  EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
7358  Explain(m, v));
7359 }
7360 
7361 TEST(ElementsAreTest, MatchesOneElementVector) {
7362  vector<std::string> test_vector;
7363  test_vector.push_back("test string");
7364 
7365  EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
7366 }
7367 
7368 TEST(ElementsAreTest, MatchesOneElementList) {
7369  list<std::string> test_list;
7370  test_list.push_back("test string");
7371 
7372  EXPECT_THAT(test_list, ElementsAre("test string"));
7373 }
7374 
7375 TEST(ElementsAreTest, MatchesThreeElementVector) {
7376  vector<std::string> test_vector;
7377  test_vector.push_back("one");
7378  test_vector.push_back("two");
7379  test_vector.push_back("three");
7380 
7381  EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
7382 }
7383 
7384 TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
7385  vector<int> test_vector;
7386  test_vector.push_back(4);
7387 
7389 }
7390 
7391 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
7392  vector<int> test_vector;
7393  test_vector.push_back(4);
7394 
7396 }
7397 
7398 TEST(ElementsAreTest, MatchesOneElementValue) {
7399  vector<int> test_vector;
7400  test_vector.push_back(4);
7401 
7403 }
7404 
7405 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
7406  vector<int> test_vector;
7407  test_vector.push_back(1);
7408  test_vector.push_back(2);
7409  test_vector.push_back(3);
7410 
7412 }
7413 
7414 TEST(ElementsAreTest, MatchesTenElementVector) {
7415  const int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
7416  vector<int> test_vector(std::begin(a), std::end(a));
7417 
7419  // The element list can contain values and/or matchers
7420  // of different types.
7421  ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
7422 }
7423 
7424 TEST(ElementsAreTest, DoesNotMatchWrongSize) {
7425  vector<std::string> test_vector;
7426  test_vector.push_back("test string");
7427  test_vector.push_back("test string");
7428 
7429  Matcher<vector<std::string>> m = ElementsAre(StrEq("test string"));
7430  EXPECT_FALSE(m.Matches(test_vector));
7431 }
7432 
7433 TEST(ElementsAreTest, DoesNotMatchWrongValue) {
7434  vector<std::string> test_vector;
7435  test_vector.push_back("other string");
7436 
7437  Matcher<vector<std::string>> m = ElementsAre(StrEq("test string"));
7438  EXPECT_FALSE(m.Matches(test_vector));
7439 }
7440 
7441 TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
7442  vector<std::string> test_vector;
7443  test_vector.push_back("one");
7444  test_vector.push_back("three");
7445  test_vector.push_back("two");
7446 
7447  Matcher<vector<std::string>> m =
7448  ElementsAre(StrEq("one"), StrEq("two"), StrEq("three"));
7449  EXPECT_FALSE(m.Matches(test_vector));
7450 }
7451 
7452 TEST(ElementsAreTest, WorksForNestedContainer) {
7453  constexpr std::array<const char*, 2> strings = {{"Hi", "world"}};
7454 
7455  vector<list<char>> nested;
7456  for (const auto& s : strings) {
7457  nested.emplace_back(s, s + strlen(s));
7458  }
7459 
7461  ElementsAre('w', 'o', _, _, 'd')));
7463  ElementsAre('w', 'o', _, _, 'd'))));
7464 }
7465 
7466 TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
7467  int a[] = {0, 1, 2};
7468  vector<int> v(std::begin(a), std::end(a));
7469 
7470  EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
7471  EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
7472 }
7473 
7474 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
7475  int a[] = {0, 1, 2};
7476  vector<int> v(std::begin(a), std::end(a));
7477 
7478  EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
7479  EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
7480 }
7481 
7482 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
7483  int array[] = {0, 1, 2};
7484  EXPECT_THAT(array, ElementsAre(0, 1, _));
7485  EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
7487 }
7488 
7489 class NativeArrayPassedAsPointerAndSize {
7490  public:
7491  NativeArrayPassedAsPointerAndSize() {}
7492 
7493  MOCK_METHOD(void, Helper, (int* array, int size));
7494 
7495  private:
7496  GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
7497 };
7498 
7499 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
7500  int array[] = {0, 1};
7501  ::std::tuple<int*, size_t> array_as_tuple(array, 2);
7502  EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
7503  EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
7504 
7505  NativeArrayPassedAsPointerAndSize helper;
7506  EXPECT_CALL(helper, Helper(_, _)).With(ElementsAre(0, 1));
7507  helper.Helper(array, 2);
7508 }
7509 
7510 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
7511  const char a2[][3] = {"hi", "lo"};
7512  EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
7513  ElementsAre('l', 'o', '\0')));
7514  EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
7515  EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
7516  ElementsAre('l', 'o', '\0')));
7517 }
7518 
7519 TEST(ElementsAreTest, AcceptsStringLiteral) {
7520  std::string array[] = {"hi", "one", "two"};
7521  EXPECT_THAT(array, ElementsAre("hi", "one", "two"));
7522  EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too")));
7523 }
7524 
7525 // Declared here with the size unknown. Defined AFTER the following test.
7526 extern const char kHi[];
7527 
7528 TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
7529  // The size of kHi is not known in this test, but ElementsAre() should
7530  // still accept it.
7531 
7532  std::string array1[] = {"hi"};
7533  EXPECT_THAT(array1, ElementsAre(kHi));
7534 
7535  std::string array2[] = {"ho"};
7536  EXPECT_THAT(array2, Not(ElementsAre(kHi)));
7537 }
7538 
7539 const char kHi[] = "hi";
7540 
7541 TEST(ElementsAreTest, MakesCopyOfArguments) {
7542  int x = 1;
7543  int y = 2;
7544  // This should make a copy of x and y.
7546  polymorphic_matcher = ElementsAre(x, y);
7547  // Changing x and y now shouldn't affect the meaning of the above matcher.
7548  x = y = 0;
7549  const int array1[] = {1, 2};
7550  EXPECT_THAT(array1, polymorphic_matcher);
7551  const int array2[] = {0, 0};
7552  EXPECT_THAT(array2, Not(polymorphic_matcher));
7553 }
7554 
7555 // Tests for ElementsAreArray(). Since ElementsAreArray() shares most
7556 // of the implementation with ElementsAre(), we don't test it as
7557 // thoroughly here.
7558 
7559 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
7560  const int a[] = {1, 2, 3};
7561 
7562  vector<int> test_vector(std::begin(a), std::end(a));
7564 
7565  test_vector[2] = 0;
7567 }
7568 
7569 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
7570  std::array<const char*, 3> a = {{"one", "two", "three"}};
7571 
7572  vector<std::string> test_vector(std::begin(a), std::end(a));
7573  EXPECT_THAT(test_vector, ElementsAreArray(a.data(), a.size()));
7574 
7575  const char** p = a.data();
7576  test_vector[0] = "1";
7578 }
7579 
7580 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
7581  const char* a[] = {"one", "two", "three"};
7582 
7583  vector<std::string> test_vector(std::begin(a), std::end(a));
7585 
7586  test_vector[0] = "1";
7588 }
7589 
7590 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
7591  const Matcher<std::string> kMatcherArray[] = {StrEq("one"), StrEq("two"),
7592  StrEq("three")};
7593 
7594  vector<std::string> test_vector;
7595  test_vector.push_back("one");
7596  test_vector.push_back("two");
7597  test_vector.push_back("three");
7598  EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
7599 
7600  test_vector.push_back("three");
7601  EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
7602 }
7603 
7604 TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
7605  const int a[] = {1, 2, 3};
7606  vector<int> test_vector(std::begin(a), std::end(a));
7607  const vector<int> expected(std::begin(a), std::end(a));
7609  test_vector.push_back(4);
7611 }
7612 
7613 TEST(ElementsAreArrayTest, TakesInitializerList) {
7614  const int a[5] = {1, 2, 3, 4, 5};
7615  EXPECT_THAT(a, ElementsAreArray({1, 2, 3, 4, 5}));
7616  EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 5, 4})));
7617  EXPECT_THAT(a, Not(ElementsAreArray({1, 2, 3, 4, 6})));
7618 }
7619 
7620 TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
7621  const std::string a[5] = {"a", "b", "c", "d", "e"};
7622  EXPECT_THAT(a, ElementsAreArray({"a", "b", "c", "d", "e"}));
7623  EXPECT_THAT(a, Not(ElementsAreArray({"a", "b", "c", "e", "d"})));
7624  EXPECT_THAT(a, Not(ElementsAreArray({"a", "b", "c", "d", "ef"})));
7625 }
7626 
7627 TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
7628  const int a[5] = {1, 2, 3, 4, 5};
7629  EXPECT_THAT(a, ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(5)}));
7630  EXPECT_THAT(a, Not(ElementsAreArray({Eq(1), Eq(2), Eq(3), Eq(4), Eq(6)})));
7631 }
7632 
7633 TEST(ElementsAreArrayTest, TakesInitializerListOfDifferentTypedMatchers) {
7634  const int a[5] = {1, 2, 3, 4, 5};
7635  // The compiler cannot infer the type of the initializer list if its
7636  // elements have different types. We must explicitly specify the
7637  // unified element type in this case.
7638  EXPECT_THAT(
7639  a, ElementsAreArray<Matcher<int>>({Eq(1), Ne(-2), Ge(3), Le(4), Eq(5)}));
7640  EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int>>(
7641  {Eq(1), Ne(-2), Ge(3), Le(4), Eq(6)})));
7642 }
7643 
7644 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
7645  const int a[] = {1, 2, 3};
7646  const Matcher<int> kMatchers[] = {Eq(1), Eq(2), Eq(3)};
7647  vector<int> test_vector(std::begin(a), std::end(a));
7648  const vector<Matcher<int>> expected(std::begin(kMatchers),
7649  std::end(kMatchers));
7651  test_vector.push_back(4);
7653 }
7654 
7655 TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
7656  const int a[] = {1, 2, 3};
7657  const vector<int> test_vector(std::begin(a), std::end(a));
7658  const vector<int> expected(std::begin(a), std::end(a));
7659  EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
7660  // Pointers are iterators, too.
7662  // The empty range of NULL pointers should also be okay.
7663  int* const null_int = nullptr;
7664  EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
7665  EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
7666 }
7667 
7668 // Since ElementsAre() and ElementsAreArray() share much of the
7669 // implementation, we only do a sanity test for native arrays here.
7670 TEST(ElementsAreArrayTest, WorksWithNativeArray) {
7671  ::std::string a[] = {"hi", "ho"};
7672  ::std::string b[] = {"hi", "ho"};
7673 
7675  EXPECT_THAT(a, ElementsAreArray(b, 2));
7676  EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
7677 }
7678 
7679 TEST(ElementsAreArrayTest, SourceLifeSpan) {
7680  const int a[] = {1, 2, 3};
7681  vector<int> test_vector(std::begin(a), std::end(a));
7682  vector<int> expect(std::begin(a), std::end(a));
7683  ElementsAreArrayMatcher<int> matcher_maker =
7684  ElementsAreArray(expect.begin(), expect.end());
7685  EXPECT_THAT(test_vector, matcher_maker);
7686  // Changing in place the values that initialized matcher_maker should not
7687  // affect matcher_maker anymore. It should have made its own copy of them.
7688  for (int& i : expect) {
7689  i += 10;
7690  }
7691  EXPECT_THAT(test_vector, matcher_maker);
7692  test_vector.push_back(3);
7693  EXPECT_THAT(test_vector, Not(matcher_maker));
7694 }
7695 
7696 // Tests for the MATCHER*() macro family.
7697 
7698 // Tests that a simple MATCHER() definition works.
7699 
7700 MATCHER(IsEven, "") { return (arg % 2) == 0; }
7701 
7702 TEST(MatcherMacroTest, Works) {
7703  const Matcher<int> m = IsEven();
7704  EXPECT_TRUE(m.Matches(6));
7705  EXPECT_FALSE(m.Matches(7));
7706 
7707  EXPECT_EQ("is even", Describe(m));
7708  EXPECT_EQ("not (is even)", DescribeNegation(m));
7709  EXPECT_EQ("", Explain(m, 6));
7710  EXPECT_EQ("", Explain(m, 7));
7711 }
7712 
7713 // This also tests that the description string can reference 'negation'.
7714 MATCHER(IsEven2, negation ? "is odd" : "is even") {
7715  if ((arg % 2) == 0) {
7716  // Verifies that we can stream to result_listener, a listener
7717  // supplied by the MATCHER macro implicitly.
7718  *result_listener << "OK";
7719  return true;
7720  } else {
7721  *result_listener << "% 2 == " << (arg % 2);
7722  return false;
7723  }
7724 }
7725 
7726 // This also tests that the description string can reference matcher
7727 // parameters.
7728 MATCHER_P2(EqSumOf, x, y,
7729  std::string(negation ? "doesn't equal" : "equals") + " the sum of " +
7730  PrintToString(x) + " and " + PrintToString(y)) {
7731  if (arg == (x + y)) {
7732  *result_listener << "OK";
7733  return true;
7734  } else {
7735  // Verifies that we can stream to the underlying stream of
7736  // result_listener.
7737  if (result_listener->stream() != nullptr) {
7738  *result_listener->stream() << "diff == " << (x + y - arg);
7739  }
7740  return false;
7741  }
7742 }
7743 
7744 // Tests that the matcher description can reference 'negation' and the
7745 // matcher parameters.
7746 TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
7747  const Matcher<int> m1 = IsEven2();
7748  EXPECT_EQ("is even", Describe(m1));
7749  EXPECT_EQ("is odd", DescribeNegation(m1));
7750 
7751  const Matcher<int> m2 = EqSumOf(5, 9);
7752  EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
7753  EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
7754 }
7755 
7756 // Tests explaining match result in a MATCHER* macro.
7757 TEST(MatcherMacroTest, CanExplainMatchResult) {
7758  const Matcher<int> m1 = IsEven2();
7759  EXPECT_EQ("OK", Explain(m1, 4));
7760  EXPECT_EQ("% 2 == 1", Explain(m1, 5));
7761 
7762  const Matcher<int> m2 = EqSumOf(1, 2);
7763  EXPECT_EQ("OK", Explain(m2, 3));
7764  EXPECT_EQ("diff == -1", Explain(m2, 4));
7765 }
7766 
7767 // Tests that the body of MATCHER() can reference the type of the
7768 // value being matched.
7769 
7770 MATCHER(IsEmptyString, "") {
7771  StaticAssertTypeEq<::std::string, arg_type>();
7772  return arg.empty();
7773 }
7774 
7775 MATCHER(IsEmptyStringByRef, "") {
7776  StaticAssertTypeEq<const ::std::string&, arg_type>();
7777  return arg.empty();
7778 }
7779 
7780 TEST(MatcherMacroTest, CanReferenceArgType) {
7781  const Matcher<::std::string> m1 = IsEmptyString();
7782  EXPECT_TRUE(m1.Matches(""));
7783 
7784  const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
7785  EXPECT_TRUE(m2.Matches(""));
7786 }
7787 
7788 // Tests that MATCHER() can be used in a namespace.
7789 
7790 namespace matcher_test {
7791 MATCHER(IsOdd, "") { return (arg % 2) != 0; }
7792 } // namespace matcher_test
7793 
7794 TEST(MatcherMacroTest, WorksInNamespace) {
7795  Matcher<int> m = matcher_test::IsOdd();
7796  EXPECT_FALSE(m.Matches(4));
7797  EXPECT_TRUE(m.Matches(5));
7798 }
7799 
7800 // Tests that Value() can be used to compose matchers.
7801 MATCHER(IsPositiveOdd, "") {
7802  return Value(arg, matcher_test::IsOdd()) && arg > 0;
7803 }
7804 
7805 TEST(MatcherMacroTest, CanBeComposedUsingValue) {
7806  EXPECT_THAT(3, IsPositiveOdd());
7807  EXPECT_THAT(4, Not(IsPositiveOdd()));
7808  EXPECT_THAT(-1, Not(IsPositiveOdd()));
7809 }
7810 
7811 // Tests that a simple MATCHER_P() definition works.
7812 
7813 MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
7814 
7815 TEST(MatcherPMacroTest, Works) {
7816  const Matcher<int> m = IsGreaterThan32And(5);
7817  EXPECT_TRUE(m.Matches(36));
7818  EXPECT_FALSE(m.Matches(5));
7819 
7820  EXPECT_EQ("is greater than 32 and (n: 5)", Describe(m));
7821  EXPECT_EQ("not (is greater than 32 and (n: 5))", DescribeNegation(m));
7822  EXPECT_EQ("", Explain(m, 36));
7823  EXPECT_EQ("", Explain(m, 5));
7824 }
7825 
7826 // Tests that the description is calculated correctly from the matcher name.
7827 MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
7828 
7829 TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
7830  const Matcher<int> m = _is_Greater_Than32and_(5);
7831 
7832  EXPECT_EQ("is greater than 32 and (n: 5)", Describe(m));
7833  EXPECT_EQ("not (is greater than 32 and (n: 5))", DescribeNegation(m));
7834  EXPECT_EQ("", Explain(m, 36));
7835  EXPECT_EQ("", Explain(m, 5));
7836 }
7837 
7838 // Tests that a MATCHER_P matcher can be explicitly instantiated with
7839 // a reference parameter type.
7840 
7841 class UncopyableFoo {
7842  public:
7843  explicit UncopyableFoo(char value) : value_(value) { (void)value_; }
7844 
7845  UncopyableFoo(const UncopyableFoo&) = delete;
7846  void operator=(const UncopyableFoo&) = delete;
7847 
7848  private:
7849  char value_;
7850 };
7851 
7852 MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
7853 
7854 TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
7855  UncopyableFoo foo1('1'), foo2('2');
7856  const Matcher<const UncopyableFoo&> m =
7857  ReferencesUncopyable<const UncopyableFoo&>(foo1);
7858 
7859  EXPECT_TRUE(m.Matches(foo1));
7860  EXPECT_FALSE(m.Matches(foo2));
7861 
7862  // We don't want the address of the parameter printed, as most
7863  // likely it will just annoy the user. If the address is
7864  // interesting, the user should consider passing the parameter by
7865  // pointer instead.
7866  EXPECT_EQ("references uncopyable (variable: 1-byte object <31>)",
7867  Describe(m));
7868 }
7869 
7870 // Tests that the body of MATCHER_Pn() can reference the parameter
7871 // types.
7872 
7873 MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
7874  StaticAssertTypeEq<int, foo_type>();
7875  StaticAssertTypeEq<long, bar_type>(); // NOLINT
7876  StaticAssertTypeEq<char, baz_type>();
7877  return arg == 0;
7878 }
7879 
7880 TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
7881  EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
7882 }
7883 
7884 // Tests that a MATCHER_Pn matcher can be explicitly instantiated with
7885 // reference parameter types.
7886 
7887 MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
7888  return &arg == &variable1 || &arg == &variable2;
7889 }
7890 
7891 TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
7892  UncopyableFoo foo1('1'), foo2('2'), foo3('3');
7893  const Matcher<const UncopyableFoo&> const_m =
7894  ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
7895 
7896  EXPECT_TRUE(const_m.Matches(foo1));
7897  EXPECT_TRUE(const_m.Matches(foo2));
7898  EXPECT_FALSE(const_m.Matches(foo3));
7899 
7900  const Matcher<UncopyableFoo&> m =
7901  ReferencesAnyOf<UncopyableFoo&, UncopyableFoo&>(foo1, foo2);
7902 
7903  EXPECT_TRUE(m.Matches(foo1));
7904  EXPECT_TRUE(m.Matches(foo2));
7905  EXPECT_FALSE(m.Matches(foo3));
7906 }
7907 
7908 TEST(MatcherPnMacroTest,
7909  GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
7910  UncopyableFoo foo1('1'), foo2('2');
7911  const Matcher<const UncopyableFoo&> m =
7912  ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
7913 
7914  // We don't want the addresses of the parameters printed, as most
7915  // likely they will just annoy the user. If the addresses are
7916  // interesting, the user should consider passing the parameters by
7917  // pointers instead.
7918  EXPECT_EQ(
7919  "references any of (variable1: 1-byte object <31>, variable2: 1-byte "
7920  "object <32>)",
7921  Describe(m));
7922 }
7923 
7924 // Tests that a simple MATCHER_P2() definition works.
7925 
7926 MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
7927 
7928 TEST(MatcherPnMacroTest, Works) {
7929  const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT
7930  EXPECT_TRUE(m.Matches(36L));
7931  EXPECT_FALSE(m.Matches(15L));
7932 
7933  EXPECT_EQ("is not in closed range (low: 10, hi: 20)", Describe(m));
7934  EXPECT_EQ("not (is not in closed range (low: 10, hi: 20))",
7935  DescribeNegation(m));
7936  EXPECT_EQ("", Explain(m, 36L));
7937  EXPECT_EQ("", Explain(m, 15L));
7938 }
7939 
7940 // Tests that MATCHER*() definitions can be overloaded on the number
7941 // of parameters; also tests MATCHER_Pn() where n >= 3.
7942 
7943 MATCHER(EqualsSumOf, "") { return arg == 0; }
7944 MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
7945 MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
7946 MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
7947 MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
7948 MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
7949 MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
7950  return arg == a + b + c + d + e + f;
7951 }
7952 MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
7953  return arg == a + b + c + d + e + f + g;
7954 }
7955 MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
7956  return arg == a + b + c + d + e + f + g + h;
7957 }
7958 MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
7959  return arg == a + b + c + d + e + f + g + h + i;
7960 }
7961 MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
7962  return arg == a + b + c + d + e + f + g + h + i + j;
7963 }
7964 
7965 TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
7966  EXPECT_THAT(0, EqualsSumOf());
7967  EXPECT_THAT(1, EqualsSumOf(1));
7968  EXPECT_THAT(12, EqualsSumOf(10, 2));
7969  EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
7970  EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
7971  EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
7972  EXPECT_THAT("abcdef",
7973  EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
7974  EXPECT_THAT("abcdefg",
7975  EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
7976  EXPECT_THAT("abcdefgh", EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e",
7977  'f', 'g', "h"));
7978  EXPECT_THAT("abcdefghi", EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e",
7979  'f', 'g', "h", 'i'));
7980  EXPECT_THAT("abcdefghij",
7981  EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g', "h",
7982  'i', ::std::string("j")));
7983 
7984  EXPECT_THAT(1, Not(EqualsSumOf()));
7985  EXPECT_THAT(-1, Not(EqualsSumOf(1)));
7986  EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
7987  EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
7988  EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
7989  EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
7990  EXPECT_THAT("abcdef ",
7991  Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
7992  EXPECT_THAT("abcdefg ", Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d",
7993  "e", 'f', 'g')));
7994  EXPECT_THAT("abcdefgh ", Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d",
7995  "e", 'f', 'g', "h")));
7996  EXPECT_THAT("abcdefghi ", Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d",
7997  "e", 'f', 'g', "h", 'i')));
7998  EXPECT_THAT("abcdefghij ",
7999  Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
8000  "h", 'i', ::std::string("j"))));
8001 }
8002 
8003 // Tests that a MATCHER_Pn() definition can be instantiated with any
8004 // compatible parameter types.
8005 TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
8006  EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
8007  EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
8008 
8009  EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
8010  EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
8011 }
8012 
8013 // Tests that the matcher body can promote the parameter types.
8014 
8015 MATCHER_P2(EqConcat, prefix, suffix, "") {
8016  // The following lines promote the two parameters to desired types.
8017  std::string prefix_str(prefix);
8018  char suffix_char = static_cast<char>(suffix);
8019  return arg == prefix_str + suffix_char;
8020 }
8021 
8022 TEST(MatcherPnMacroTest, SimpleTypePromotion) {
8023  Matcher<std::string> no_promo = EqConcat(std::string("foo"), 't');
8024  Matcher<const std::string&> promo = EqConcat("foo", static_cast<int>('t'));
8025  EXPECT_FALSE(no_promo.Matches("fool"));
8026  EXPECT_FALSE(promo.Matches("fool"));
8027  EXPECT_TRUE(no_promo.Matches("foot"));
8028  EXPECT_TRUE(promo.Matches("foot"));
8029 }
8030 
8031 // Verifies the type of a MATCHER*.
8032 
8033 TEST(MatcherPnMacroTest, TypesAreCorrect) {
8034  // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
8035  EqualsSumOfMatcher a0 = EqualsSumOf();
8036 
8037  // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
8038  EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
8039 
8040  // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
8041  // variable, and so on.
8042  EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
8043  EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
8044  EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
8045  EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
8046  EqualsSumOf(1, 2, 3, 4, '5');
8047  EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
8048  EqualsSumOf(1, 2, 3, 4, 5, '6');
8049  EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
8050  EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
8051  EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
8052  EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
8053  EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
8054  EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
8055  EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
8056  EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
8057 
8058  // Avoid "unused variable" warnings.
8059  (void)a0;
8060  (void)a1;
8061  (void)a2;
8062  (void)a3;
8063  (void)a4;
8064  (void)a5;
8065  (void)a6;
8066  (void)a7;
8067  (void)a8;
8068  (void)a9;
8069  (void)a10;
8070 }
8071 
8072 // Tests that matcher-typed parameters can be used in Value() inside a
8073 // MATCHER_Pn definition.
8074 
8075 // Succeeds if arg matches exactly 2 of the 3 matchers.
8076 MATCHER_P3(TwoOf, m1, m2, m3, "") {
8077  const int count = static_cast<int>(Value(arg, m1)) +
8078  static_cast<int>(Value(arg, m2)) +
8079  static_cast<int>(Value(arg, m3));
8080  return count == 2;
8081 }
8082 
8083 TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
8084  EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
8085  EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
8086 }
8087 
8088 // Tests Contains().
8089 
8090 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
8091  list<int> some_list;
8092  some_list.push_back(3);
8093  some_list.push_back(1);
8094  some_list.push_back(2);
8095  some_list.push_back(3);
8096  EXPECT_THAT(some_list, Contains(1));
8097  EXPECT_THAT(some_list, Contains(Gt(2.5)));
8098  EXPECT_THAT(some_list, Contains(Eq(2.0f)));
8099 
8100  list<std::string> another_list;
8101  another_list.push_back("fee");
8102  another_list.push_back("fie");
8103  another_list.push_back("foe");
8104  another_list.push_back("fum");
8105  EXPECT_THAT(another_list, Contains(std::string("fee")));
8106 }
8107 
8108 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
8109  list<int> some_list;
8110  some_list.push_back(3);
8111  some_list.push_back(1);
8112  EXPECT_THAT(some_list, Not(Contains(4)));
8113 }
8114 
8115 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
8116  set<int> some_set;
8117  some_set.insert(3);
8118  some_set.insert(1);
8119  some_set.insert(2);
8120  EXPECT_THAT(some_set, Contains(Eq(1.0)));
8121  EXPECT_THAT(some_set, Contains(Eq(3.0f)));
8122  EXPECT_THAT(some_set, Contains(2));
8123 
8124  set<std::string> another_set;
8125  another_set.insert("fee");
8126  another_set.insert("fie");
8127  another_set.insert("foe");
8128  another_set.insert("fum");
8129  EXPECT_THAT(another_set, Contains(Eq(std::string("fum"))));
8130 }
8131 
8132 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
8133  set<int> some_set;
8134  some_set.insert(3);
8135  some_set.insert(1);
8136  EXPECT_THAT(some_set, Not(Contains(4)));
8137 
8138  set<std::string> c_string_set;
8139  c_string_set.insert("hello");
8140  EXPECT_THAT(c_string_set, Not(Contains(std::string("goodbye"))));
8141 }
8142 
8143 TEST(ContainsTest, ExplainsMatchResultCorrectly) {
8144  const int a[2] = {1, 2};
8145  Matcher<const int(&)[2]> m = Contains(2);
8146  EXPECT_EQ("whose element #1 matches", Explain(m, a));
8147 
8148  m = Contains(3);
8149  EXPECT_EQ("", Explain(m, a));
8150 
8151  m = Contains(GreaterThan(0));
8152  EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
8153 
8154  m = Contains(GreaterThan(10));
8155  EXPECT_EQ("", Explain(m, a));
8156 }
8157 
8158 TEST(ContainsTest, DescribesItselfCorrectly) {
8159  Matcher<vector<int>> m = Contains(1);
8160  EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
8161 
8162  Matcher<vector<int>> m2 = Not(m);
8163  EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
8164 }
8165 
8166 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
8167  map<std::string, int> my_map;
8168  const char* bar = "a string";
8169  my_map[bar] = 2;
8170  EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
8171 
8172  map<std::string, int> another_map;
8173  another_map["fee"] = 1;
8174  another_map["fie"] = 2;
8175  another_map["foe"] = 3;
8176  another_map["fum"] = 4;
8177  EXPECT_THAT(another_map,
8178  Contains(pair<const std::string, int>(std::string("fee"), 1)));
8179  EXPECT_THAT(another_map, Contains(pair<const std::string, int>("fie", 2)));
8180 }
8181 
8182 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
8183  map<int, int> some_map;
8184  some_map[1] = 11;
8185  some_map[2] = 22;
8186  EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
8187 }
8188 
8189 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
8190  const char* string_array[] = {"fee", "fie", "foe", "fum"};
8191  EXPECT_THAT(string_array, Contains(Eq(std::string("fum"))));
8192 }
8193 
8194 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
8195  int int_array[] = {1, 2, 3, 4};
8196  EXPECT_THAT(int_array, Not(Contains(5)));
8197 }
8198 
8199 TEST(ContainsTest, AcceptsMatcher) {
8200  const int a[] = {1, 2, 3};
8201  EXPECT_THAT(a, Contains(Gt(2)));
8202  EXPECT_THAT(a, Not(Contains(Gt(4))));
8203 }
8204 
8205 TEST(ContainsTest, WorksForNativeArrayAsTuple) {
8206  const int a[] = {1, 2};
8207  const int* const pointer = a;
8208  EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1));
8209  EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3))));
8210 }
8211 
8212 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
8213  int a[][3] = {{1, 2, 3}, {4, 5, 6}};
8214  EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
8215  EXPECT_THAT(a, Contains(Contains(5)));
8216  EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
8217  EXPECT_THAT(a, Contains(Not(Contains(5))));
8218 }
8219 
8220 // Tests Contains().Times().
8221 
8222 TEST(ContainsTimes, ListMatchesWhenElementQuantityMatches) {
8223  list<int> some_list;
8224  some_list.push_back(3);
8225  some_list.push_back(1);
8226  some_list.push_back(2);
8227  some_list.push_back(3);
8228  EXPECT_THAT(some_list, Contains(3).Times(2));
8229  EXPECT_THAT(some_list, Contains(2).Times(1));
8230  EXPECT_THAT(some_list, Contains(Ge(2)).Times(3));
8231  EXPECT_THAT(some_list, Contains(Ge(2)).Times(Gt(2)));
8232  EXPECT_THAT(some_list, Contains(4).Times(0));
8233  EXPECT_THAT(some_list, Contains(_).Times(4));
8234  EXPECT_THAT(some_list, Not(Contains(5).Times(1)));
8235  EXPECT_THAT(some_list, Contains(5).Times(_)); // Times(_) always matches
8236  EXPECT_THAT(some_list, Not(Contains(3).Times(1)));
8237  EXPECT_THAT(some_list, Contains(3).Times(Not(1)));
8238  EXPECT_THAT(list<int>{}, Not(Contains(_)));
8239 }
8240 
8241 TEST(ContainsTimes, ExplainsMatchResultCorrectly) {
8242  const int a[2] = {1, 2};
8243  Matcher<const int(&)[2]> m = Contains(2).Times(3);
8244  EXPECT_EQ(
8245  "whose element #1 matches but whose match quantity of 1 does not match",
8246  Explain(m, a));
8247 
8248  m = Contains(3).Times(0);
8249  EXPECT_EQ("has no element that matches and whose match quantity of 0 matches",
8250  Explain(m, a));
8251 
8252  m = Contains(3).Times(4);
8253  EXPECT_EQ(
8254  "has no element that matches and whose match quantity of 0 does not "
8255  "match",
8256  Explain(m, a));
8257 
8258  m = Contains(2).Times(4);
8259  EXPECT_EQ(
8260  "whose element #1 matches but whose match quantity of 1 does not "
8261  "match",
8262  Explain(m, a));
8263 
8264  m = Contains(GreaterThan(0)).Times(2);
8265  EXPECT_EQ("whose elements (0, 1) match and whose match quantity of 2 matches",
8266  Explain(m, a));
8267 
8268  m = Contains(GreaterThan(10)).Times(Gt(1));
8269  EXPECT_EQ(
8270  "has no element that matches and whose match quantity of 0 does not "
8271  "match",
8272  Explain(m, a));
8273 
8274  m = Contains(GreaterThan(0)).Times(GreaterThan<size_t>(5));
8275  EXPECT_EQ(
8276  "whose elements (0, 1) match but whose match quantity of 2 does not "
8277  "match, which is 3 less than 5",
8278  Explain(m, a));
8279 }
8280 
8281 TEST(ContainsTimes, DescribesItselfCorrectly) {
8282  Matcher<vector<int>> m = Contains(1).Times(2);
8283  EXPECT_EQ("quantity of elements that match is equal to 1 is equal to 2",
8284  Describe(m));
8285 
8286  Matcher<vector<int>> m2 = Not(m);
8287  EXPECT_EQ("quantity of elements that match is equal to 1 isn't equal to 2",
8288  Describe(m2));
8289 }
8290 
8291 // Tests AllOfArray()
8292 
8293 TEST(AllOfArrayTest, BasicForms) {
8294  // Iterator
8295  std::vector<int> v0{};
8296  std::vector<int> v1{1};
8297  std::vector<int> v2{2, 3};
8298  std::vector<int> v3{4, 4, 4};
8299  EXPECT_THAT(0, AllOfArray(v0.begin(), v0.end()));
8300  EXPECT_THAT(1, AllOfArray(v1.begin(), v1.end()));
8301  EXPECT_THAT(2, Not(AllOfArray(v1.begin(), v1.end())));
8302  EXPECT_THAT(3, Not(AllOfArray(v2.begin(), v2.end())));
8303  EXPECT_THAT(4, AllOfArray(v3.begin(), v3.end()));
8304  // Pointer + size
8305  int ar[6] = {1, 2, 3, 4, 4, 4};
8306  EXPECT_THAT(0, AllOfArray(ar, 0));
8307  EXPECT_THAT(1, AllOfArray(ar, 1));
8308  EXPECT_THAT(2, Not(AllOfArray(ar, 1)));
8309  EXPECT_THAT(3, Not(AllOfArray(ar + 1, 3)));
8310  EXPECT_THAT(4, AllOfArray(ar + 3, 3));
8311  // Array
8312  // int ar0[0]; Not usable
8313  int ar1[1] = {1};
8314  int ar2[2] = {2, 3};
8315  int ar3[3] = {4, 4, 4};
8316  // EXPECT_THAT(0, Not(AllOfArray(ar0))); // Cannot work
8317  EXPECT_THAT(1, AllOfArray(ar1));
8318  EXPECT_THAT(2, Not(AllOfArray(ar1)));
8319  EXPECT_THAT(3, Not(AllOfArray(ar2)));
8320  EXPECT_THAT(4, AllOfArray(ar3));
8321  // Container
8322  EXPECT_THAT(0, AllOfArray(v0));
8323  EXPECT_THAT(1, AllOfArray(v1));
8324  EXPECT_THAT(2, Not(AllOfArray(v1)));
8325  EXPECT_THAT(3, Not(AllOfArray(v2)));
8326  EXPECT_THAT(4, AllOfArray(v3));
8327  // Initializer
8328  EXPECT_THAT(0, AllOfArray<int>({})); // Requires template arg.
8329  EXPECT_THAT(1, AllOfArray({1}));
8330  EXPECT_THAT(2, Not(AllOfArray({1})));
8331  EXPECT_THAT(3, Not(AllOfArray({2, 3})));
8332  EXPECT_THAT(4, AllOfArray({4, 4, 4}));
8333 }
8334 
8335 TEST(AllOfArrayTest, Matchers) {
8336  // vector
8337  std::vector<Matcher<int>> matchers{Ge(1), Lt(2)};
8338  EXPECT_THAT(0, Not(AllOfArray(matchers)));
8339  EXPECT_THAT(1, AllOfArray(matchers));
8340  EXPECT_THAT(2, Not(AllOfArray(matchers)));
8341  // initializer_list
8342  EXPECT_THAT(0, Not(AllOfArray({Ge(0), Ge(1)})));
8343  EXPECT_THAT(1, AllOfArray({Ge(0), Ge(1)}));
8344 }
8345 
8346 TEST(AnyOfArrayTest, BasicForms) {
8347  // Iterator
8348  std::vector<int> v0{};
8349  std::vector<int> v1{1};
8350  std::vector<int> v2{2, 3};
8351  EXPECT_THAT(0, Not(AnyOfArray(v0.begin(), v0.end())));
8352  EXPECT_THAT(1, AnyOfArray(v1.begin(), v1.end()));
8353  EXPECT_THAT(2, Not(AnyOfArray(v1.begin(), v1.end())));
8354  EXPECT_THAT(3, AnyOfArray(v2.begin(), v2.end()));
8355  EXPECT_THAT(4, Not(AnyOfArray(v2.begin(), v2.end())));
8356  // Pointer + size
8357  int ar[3] = {1, 2, 3};
8358  EXPECT_THAT(0, Not(AnyOfArray(ar, 0)));
8359  EXPECT_THAT(1, AnyOfArray(ar, 1));
8360  EXPECT_THAT(2, Not(AnyOfArray(ar, 1)));
8361  EXPECT_THAT(3, AnyOfArray(ar + 1, 2));
8362  EXPECT_THAT(4, Not(AnyOfArray(ar + 1, 2)));
8363  // Array
8364  // int ar0[0]; Not usable
8365  int ar1[1] = {1};
8366  int ar2[2] = {2, 3};
8367  // EXPECT_THAT(0, Not(AnyOfArray(ar0))); // Cannot work
8368  EXPECT_THAT(1, AnyOfArray(ar1));
8369  EXPECT_THAT(2, Not(AnyOfArray(ar1)));
8370  EXPECT_THAT(3, AnyOfArray(ar2));
8371  EXPECT_THAT(4, Not(AnyOfArray(ar2)));
8372  // Container
8373  EXPECT_THAT(0, Not(AnyOfArray(v0)));
8374  EXPECT_THAT(1, AnyOfArray(v1));
8375  EXPECT_THAT(2, Not(AnyOfArray(v1)));
8376  EXPECT_THAT(3, AnyOfArray(v2));
8377  EXPECT_THAT(4, Not(AnyOfArray(v2)));
8378  // Initializer
8379  EXPECT_THAT(0, Not(AnyOfArray<int>({}))); // Requires template arg.
8380  EXPECT_THAT(1, AnyOfArray({1}));
8381  EXPECT_THAT(2, Not(AnyOfArray({1})));
8382  EXPECT_THAT(3, AnyOfArray({2, 3}));
8383  EXPECT_THAT(4, Not(AnyOfArray({2, 3})));
8384 }
8385 
8386 TEST(AnyOfArrayTest, Matchers) {
8387  // We negate test AllOfArrayTest.Matchers.
8388  // vector
8389  std::vector<Matcher<int>> matchers{Lt(1), Ge(2)};
8390  EXPECT_THAT(0, AnyOfArray(matchers));
8391  EXPECT_THAT(1, Not(AnyOfArray(matchers)));
8392  EXPECT_THAT(2, AnyOfArray(matchers));
8393  // initializer_list
8394  EXPECT_THAT(0, AnyOfArray({Lt(0), Lt(1)}));
8395  EXPECT_THAT(1, Not(AllOfArray({Lt(0), Lt(1)})));
8396 }
8397 
8398 TEST(AnyOfArrayTest, ExplainsMatchResultCorrectly) {
8399  // AnyOfArray and AllOfArry use the same underlying template-template,
8400  // thus it is sufficient to test one here.
8401  const std::vector<int> v0{};
8402  const std::vector<int> v1{1};
8403  const std::vector<int> v2{2, 3};
8404  const Matcher<int> m0 = AnyOfArray(v0);
8405  const Matcher<int> m1 = AnyOfArray(v1);
8406  const Matcher<int> m2 = AnyOfArray(v2);
8407  EXPECT_EQ("", Explain(m0, 0));
8408  EXPECT_EQ("", Explain(m1, 1));
8409  EXPECT_EQ("", Explain(m1, 2));
8410  EXPECT_EQ("", Explain(m2, 3));
8411  EXPECT_EQ("", Explain(m2, 4));
8412  EXPECT_EQ("()", Describe(m0));
8413  EXPECT_EQ("(is equal to 1)", Describe(m1));
8414  EXPECT_EQ("(is equal to 2) or (is equal to 3)", Describe(m2));
8415  EXPECT_EQ("()", DescribeNegation(m0));
8416  EXPECT_EQ("(isn't equal to 1)", DescribeNegation(m1));
8417  EXPECT_EQ("(isn't equal to 2) and (isn't equal to 3)", DescribeNegation(m2));
8418  // Explain with matchers
8419  const Matcher<int> g1 = AnyOfArray({GreaterThan(1)});
8420  const Matcher<int> g2 = AnyOfArray({GreaterThan(1), GreaterThan(2)});
8421  // Explains the first positive match and all prior negative matches...
8422  EXPECT_EQ("which is 1 less than 1", Explain(g1, 0));
8423  EXPECT_EQ("which is the same as 1", Explain(g1, 1));
8424  EXPECT_EQ("which is 1 more than 1", Explain(g1, 2));
8425  EXPECT_EQ("which is 1 less than 1, and which is 2 less than 2",
8426  Explain(g2, 0));
8427  EXPECT_EQ("which is the same as 1, and which is 1 less than 2",
8428  Explain(g2, 1));
8429  EXPECT_EQ("which is 1 more than 1", // Only the first
8430  Explain(g2, 2));
8431 }
8432 
8433 TEST(AllOfTest, HugeMatcher) {
8434  // Verify that using AllOf with many arguments doesn't cause
8435  // the compiler to exceed template instantiation depth limit.
8436  EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
8437  testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
8438 }
8439 
8440 TEST(AnyOfTest, HugeMatcher) {
8441  // Verify that using AnyOf with many arguments doesn't cause
8442  // the compiler to exceed template instantiation depth limit.
8443  EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
8444  testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
8445 }
8446 
8447 namespace adl_test {
8448 
8449 // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
8450 // don't issue unqualified recursive calls. If they do, the argument dependent
8451 // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
8452 // as a candidate and the compilation will break due to an ambiguous overload.
8453 
8454 // The matcher must be in the same namespace as AllOf/AnyOf to make argument
8455 // dependent lookup find those.
8456 MATCHER(M, "") {
8457  (void)arg;
8458  return true;
8459 }
8460 
8461 template <typename T1, typename T2>
8462 bool AllOf(const T1& /*t1*/, const T2& /*t2*/) {
8463  return true;
8464 }
8465 
8466 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
8467  EXPECT_THAT(42,
8468  testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
8469 }
8470 
8471 template <typename T1, typename T2>
8472 bool AnyOf(const T1&, const T2&) {
8473  return true;
8474 }
8475 
8476 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
8477  EXPECT_THAT(42,
8478  testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
8479 }
8480 
8481 } // namespace adl_test
8482 
8483 TEST(AllOfTest, WorksOnMoveOnlyType) {
8484  std::unique_ptr<int> p(new int(3));
8485  EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5))));
8486  EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3)))));
8487 }
8488 
8489 TEST(AnyOfTest, WorksOnMoveOnlyType) {
8490  std::unique_ptr<int> p(new int(3));
8491  EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5))));
8492  EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5)))));
8493 }
8494 
8495 MATCHER(IsNotNull, "") { return arg != nullptr; }
8496 
8497 // Verifies that a matcher defined using MATCHER() can work on
8498 // move-only types.
8499 TEST(MatcherMacroTest, WorksOnMoveOnlyType) {
8500  std::unique_ptr<int> p(new int(3));
8501  EXPECT_THAT(p, IsNotNull());
8502  EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull()));
8503 }
8504 
8505 MATCHER_P(UniquePointee, pointee, "") { return *arg == pointee; }
8506 
8507 // Verifies that a matcher defined using MATCHER_P*() can work on
8508 // move-only types.
8509 TEST(MatcherPMacroTest, WorksOnMoveOnlyType) {
8510  std::unique_ptr<int> p(new int(3));
8511  EXPECT_THAT(p, UniquePointee(3));
8512  EXPECT_THAT(p, Not(UniquePointee(2)));
8513 }
8514 
8515 #if GTEST_HAS_EXCEPTIONS
8516 
8517 // std::function<void()> is used below for compatibility with older copies of
8518 // GCC. Normally, a raw lambda is all that is needed.
8519 
8520 // Test that examples from documentation compile
8521 TEST(ThrowsTest, Examples) {
8522  EXPECT_THAT(
8523  std::function<void()>([]() { throw std::runtime_error("message"); }),
8524  Throws<std::runtime_error>());
8525 
8526  EXPECT_THAT(
8527  std::function<void()>([]() { throw std::runtime_error("message"); }),
8528  ThrowsMessage<std::runtime_error>(HasSubstr("message")));
8529 }
8530 
8531 TEST(ThrowsTest, PrintsExceptionWhat) {
8532  EXPECT_THAT(
8533  std::function<void()>([]() { throw std::runtime_error("ABC123XYZ"); }),
8534  ThrowsMessage<std::runtime_error>(HasSubstr("ABC123XYZ")));
8535 }
8536 
8537 TEST(ThrowsTest, DoesNotGenerateDuplicateCatchClauseWarning) {
8538  EXPECT_THAT(std::function<void()>([]() { throw std::exception(); }),
8539  Throws<std::exception>());
8540 }
8541 
8542 TEST(ThrowsTest, CallableExecutedExactlyOnce) {
8543  size_t a = 0;
8544 
8545  EXPECT_THAT(std::function<void()>([&a]() {
8546  a++;
8547  throw 10;
8548  }),
8549  Throws<int>());
8550  EXPECT_EQ(a, 1u);
8551 
8552  EXPECT_THAT(std::function<void()>([&a]() {
8553  a++;
8554  throw std::runtime_error("message");
8555  }),
8556  Throws<std::runtime_error>());
8557  EXPECT_EQ(a, 2u);
8558 
8559  EXPECT_THAT(std::function<void()>([&a]() {
8560  a++;
8561  throw std::runtime_error("message");
8562  }),
8563  ThrowsMessage<std::runtime_error>(HasSubstr("message")));
8564  EXPECT_EQ(a, 3u);
8565 
8566  EXPECT_THAT(std::function<void()>([&a]() {
8567  a++;
8568  throw std::runtime_error("message");
8569  }),
8570  Throws<std::runtime_error>(
8571  Property(&std::runtime_error::what, HasSubstr("message"))));
8572  EXPECT_EQ(a, 4u);
8573 }
8574 
8575 TEST(ThrowsTest, Describe) {
8576  Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
8577  std::stringstream ss;
8578  matcher.DescribeTo(&ss);
8579  auto explanation = ss.str();
8580  EXPECT_THAT(explanation, HasSubstr("std::runtime_error"));
8581 }
8582 
8583 TEST(ThrowsTest, Success) {
8584  Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
8585  StringMatchResultListener listener;
8586  EXPECT_TRUE(matcher.MatchAndExplain(
8587  []() { throw std::runtime_error("error message"); }, &listener));
8588  EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error"));
8589 }
8590 
8591 TEST(ThrowsTest, FailWrongType) {
8592  Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
8593  StringMatchResultListener listener;
8594  EXPECT_FALSE(matcher.MatchAndExplain(
8595  []() { throw std::logic_error("error message"); }, &listener));
8596  EXPECT_THAT(listener.str(), HasSubstr("std::logic_error"));
8597  EXPECT_THAT(listener.str(), HasSubstr("\"error message\""));
8598 }
8599 
8600 TEST(ThrowsTest, FailWrongTypeNonStd) {
8601  Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
8602  StringMatchResultListener listener;
8603  EXPECT_FALSE(matcher.MatchAndExplain([]() { throw 10; }, &listener));
8604  EXPECT_THAT(listener.str(),
8605  HasSubstr("throws an exception of an unknown type"));
8606 }
8607 
8608 TEST(ThrowsTest, FailNoThrow) {
8609  Matcher<std::function<void()>> matcher = Throws<std::runtime_error>();
8610  StringMatchResultListener listener;
8611  EXPECT_FALSE(matcher.MatchAndExplain([]() { (void)0; }, &listener));
8612  EXPECT_THAT(listener.str(), HasSubstr("does not throw any exception"));
8613 }
8614 
8615 class ThrowsPredicateTest
8616  : public TestWithParam<Matcher<std::function<void()>>> {};
8617 
8618 TEST_P(ThrowsPredicateTest, Describe) {
8619  Matcher<std::function<void()>> matcher = GetParam();
8620  std::stringstream ss;
8621  matcher.DescribeTo(&ss);
8622  auto explanation = ss.str();
8623  EXPECT_THAT(explanation, HasSubstr("std::runtime_error"));
8624  EXPECT_THAT(explanation, HasSubstr("error message"));
8625 }
8626 
8627 TEST_P(ThrowsPredicateTest, Success) {
8628  Matcher<std::function<void()>> matcher = GetParam();
8629  StringMatchResultListener listener;
8630  EXPECT_TRUE(matcher.MatchAndExplain(
8631  []() { throw std::runtime_error("error message"); }, &listener));
8632  EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error"));
8633 }
8634 
8635 TEST_P(ThrowsPredicateTest, FailWrongType) {
8636  Matcher<std::function<void()>> matcher = GetParam();
8637  StringMatchResultListener listener;
8638  EXPECT_FALSE(matcher.MatchAndExplain(
8639  []() { throw std::logic_error("error message"); }, &listener));
8640  EXPECT_THAT(listener.str(), HasSubstr("std::logic_error"));
8641  EXPECT_THAT(listener.str(), HasSubstr("\"error message\""));
8642 }
8643 
8644 TEST_P(ThrowsPredicateTest, FailWrongTypeNonStd) {
8645  Matcher<std::function<void()>> matcher = GetParam();
8646  StringMatchResultListener listener;
8647  EXPECT_FALSE(matcher.MatchAndExplain([]() { throw 10; }, &listener));
8648  EXPECT_THAT(listener.str(),
8649  HasSubstr("throws an exception of an unknown type"));
8650 }
8651 
8652 TEST_P(ThrowsPredicateTest, FailNoThrow) {
8653  Matcher<std::function<void()>> matcher = GetParam();
8654  StringMatchResultListener listener;
8655  EXPECT_FALSE(matcher.MatchAndExplain([]() {}, &listener));
8656  EXPECT_THAT(listener.str(), HasSubstr("does not throw any exception"));
8657 }
8658 
8660  AllMessagePredicates, ThrowsPredicateTest,
8661  Values(Matcher<std::function<void()>>(
8662  ThrowsMessage<std::runtime_error>(HasSubstr("error message")))));
8663 
8664 // Tests that Throws<E1>(Matcher<E2>{}) compiles even when E2 != const E1&.
8665 TEST(ThrowsPredicateCompilesTest, ExceptionMatcherAcceptsBroadType) {
8666  {
8667  Matcher<std::function<void()>> matcher =
8668  ThrowsMessage<std::runtime_error>(HasSubstr("error message"));
8669  EXPECT_TRUE(
8670  matcher.Matches([]() { throw std::runtime_error("error message"); }));
8671  EXPECT_FALSE(
8672  matcher.Matches([]() { throw std::runtime_error("wrong message"); }));
8673  }
8674 
8675  {
8676  Matcher<uint64_t> inner = Eq(10);
8677  Matcher<std::function<void()>> matcher = Throws<uint32_t>(inner);
8678  EXPECT_TRUE(matcher.Matches([]() { throw(uint32_t) 10; }));
8679  EXPECT_FALSE(matcher.Matches([]() { throw(uint32_t) 11; }));
8680  }
8681 }
8682 
8683 // Tests that ThrowsMessage("message") is equivalent
8684 // to ThrowsMessage(Eq<std::string>("message")).
8685 TEST(ThrowsPredicateCompilesTest, MessageMatcherAcceptsNonMatcher) {
8686  Matcher<std::function<void()>> matcher =
8687  ThrowsMessage<std::runtime_error>("error message");
8688  EXPECT_TRUE(
8689  matcher.Matches([]() { throw std::runtime_error("error message"); }));
8690  EXPECT_FALSE(matcher.Matches(
8691  []() { throw std::runtime_error("wrong error message"); }));
8692 }
8693 
8694 #endif // GTEST_HAS_EXCEPTIONS
8695 
8696 } // namespace
8697 } // namespace gmock_matchers_test
8698 } // namespace testing
8699 
8700 #ifdef _MSC_VER
8701 # pragma warning(pop)
8702 #endif
xds_interop_client.str
str
Definition: xds_interop_client.py:487
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
testing::TEST_F
TEST_F(TestInfoTest, Names)
Definition: bloaty/third_party/googletest/googletest/test/gtest_unittest.cc:5305
testing::DoubleEq
internal::FloatingEqMatcher< double > DoubleEq(double rhs)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8634
testing::internal::FloatingEqMatcher
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:6707
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
obj
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1671
testing
Definition: aws_request_signer_test.cc:25
absl::debugging_internal::Optional
static bool Optional(bool)
Definition: abseil-cpp/absl/debugging/internal/demangle.cc:333
check_banned_filenames.bad
bad
Definition: check_banned_filenames.py:26
testing::ContainsRegex
PolymorphicMatcher< internal::MatchesRegexMatcher > ContainsRegex(const internal::RE *regex)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8835
testing::ResultOf
internal::ResultOfMatcher< Callable > ResultOf(Callable callable, const ResultOfMatcher &matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8758
further_from_infinity_
const RawType further_from_infinity_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3407
regen-readme.it
it
Definition: regen-readme.py:15
testing::internal::ExplainMatchFailureTupleTo
void ExplainMatchFailureTupleTo(const MatcherTuple &matchers, const ValueTuple &values, ::std::ostream *os)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:5629
testing::Gt
internal::GtMatcher< Rhs > Gt(Rhs x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8591
absl::str_format_internal::LengthMod::j
@ j
testing::Pointee
internal::PointeeMatcher< InnerMatcher > Pointee(const InnerMatcher &inner_matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8691
element
static std::function< Slot &(Slot *)> element
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:44
close_to_negative_zero_
const RawType close_to_negative_zero_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3397
testing::internal::IsTrue
GTEST_API_ bool IsTrue(bool condition)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:5710
grpc_event_engine::experimental::slice_detail::operator==
bool operator==(const BaseSlice &a, const BaseSlice &b)
Definition: include/grpc/event_engine/slice.h:117
testing::internal::MatchMatrix
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8076
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
testing::internal::FloatingPoint
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-internal.h:238
get
absl::string_view get(const Cont &c)
Definition: abseil-cpp/absl/strings/str_replace_test.cc:185
testing::Lt
internal::LtMatcher< Rhs > Lt(Rhs x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8603
testing::Not
internal::NotMatcher< InnerMatcher > Not(InnerMatcher m)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8926
const
#define const
Definition: bloaty/third_party/zlib/zconf.h:230
bool
bool
Definition: setup_once.h:312
MATCHER_P4
#define MATCHER_P4(name, p0, p1, p2, p3, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:485
enough
void enough(int syms)
Definition: bloaty/third_party/zlib/examples/enough.c:403
value_
int value_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:526
testing::WhenSorted
internal::WhenSortedByMatcher< internal::LessComparator, ContainerMatcher > WhenSorted(const ContainerMatcher &container_matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8991
fix_build_deps.c
list c
Definition: fix_build_deps.py:490
testing::internal::StreamMatchResultListener
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:5058
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
MATCHER_P8
#define MATCHER_P8(name, p0, p1, p2, p3, p4, p5, p6, p7, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:800
testing::DoubleNear
internal::FloatingEqMatcher< double > DoubleNear(double rhs, double max_abs_error)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8647
EXPECT_THAT
#define EXPECT_THAT(value, matcher)
absl::container_internal::internal_layout::adl_barrier::Max
constexpr size_t Max(size_t a)
Definition: abseil-cpp/absl/container/internal/layout.h:287
false
#define false
Definition: setup_once.h:323
std::tr1::make_tuple
tuple make_tuple()
Definition: cares/cares/test/gmock-1.8.0/gtest/gtest.h:1619
testing::Return
internal::ReturnAction< R > Return(R value)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-actions.h:1004
close_to_one_
const RawType close_to_one_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3401
EXPECT_FATAL_FAILURE
#define EXPECT_FATAL_FAILURE(statement, substr)
testing::Ne
internal::NeMatcher< Rhs > Ne(Rhs x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8609
bar
Definition: bloaty/third_party/googletest/googletest/test/googletest-output-test_.cc:562
match
unsigned char match[65280+2]
Definition: bloaty/third_party/zlib/examples/gun.c:165
string.h
google::protobuf.internal::true_type
integral_constant< bool, true > true_type
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/template_util.h:89
kUnused
static const size_t kUnused
Definition: googletest/googlemock/test/gmock-matchers_test.cc:6240
seed
static const uint8_t seed[20]
Definition: dsa_test.cc:79
testing::Truly
PolymorphicMatcher< internal::TrulyMatcher< Predicate > > Truly(Predicate pred)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8935
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
member_1
int member_1
Definition: googletest/googlemock/test/gmock-matchers_test.cc:1486
threshold_
int threshold_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:2982
google::protobuf.internal::false_type
integral_constant< bool, false > false_type
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/template_util.h:90
matchers
XdsRouteConfigResource::Route::Matchers matchers
Definition: xds_server_config_fetcher.cc:317
infinity_
const RawType infinity_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3405
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
arg::value
void * value
Definition: cmdline.cc:44
foo
Definition: bloaty/third_party/googletest/googletest/test/googletest-output-test_.cc:546
testing::internal::PredicateFormatterFromMatcher
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:6653
value
int value
Definition: googletest/googlemock/test/gmock-matchers_test.cc:665
gen_header_frame.vals
list vals
Definition: gen_header_frame.py:73
words
std::vector< std::string > words
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field_unittest.cc:1787
val_
T * val_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4001
absl::FormatConversionChar::s
@ s
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
testing::NotNull
PolymorphicMatcher< internal::NotNullMatcher > NotNull()
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8621
testing::Range
internal::ParamGenerator< T > Range(T start, T end, IncrementT step)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:229
testing::Ge
internal::GeMatcher< Rhs > Ge(Rhs x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8585
max_ulps_
const Bits max_ulps_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3389
testing::ByRef
inline ::std::reference_wrapper< T > ByRef(T &l_value)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-actions.h:1130
xds_manager.p
p
Definition: xds_manager.py:60
infinity_bits_
const Bits infinity_bits_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3393
testing::AnyOf
internal::AnyOfResult2< M1, M2 >::type AnyOf(M1 m1, M2 m2)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:13555
testing::BeginEndDistanceIs
internal::BeginEndDistanceIsMatcher< DistanceMatcher > BeginEndDistanceIs(const DistanceMatcher &distance_matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8958
testing::UnorderedPointwise
internal::UnorderedElementsAreArrayMatcher< typename internal::BoundSecondMatcher< Tuple2Matcher, typename internal::StlContainerView< GTEST_REMOVE_CONST_(RhsContainer)>::type::value_type > > UnorderedPointwise(const Tuple2Matcher &tuple2_matcher, const RhsContainer &rhs_container)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9042
z
Uncopyable z
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3612
MOCK_METHOD0
#define MOCK_METHOD0(m,...)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h:599
iterator
const typedef MCPhysReg * iterator
Definition: MCRegisterInfo.h:27
setup.k
k
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
testing::Const
const T & Const(const T &x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:11004
testing::NanSensitiveFloatEq
internal::FloatingEqMatcher< float > NanSensitiveFloatEq(float rhs)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8668
y
const double y
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4084
T
#define T(upbtypeconst, upbtype, ctype, default_value)
p
const char * p
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4086
testing::AllArgs
InnerMatcher AllArgs(const InnerMatcher &matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9201
testing::Pointwise
internal::PointwiseMatcher< TupleMatcher, GTEST_REMOVE_CONST_(Container)> Pointwise(const TupleMatcher &tuple_matcher, const Container &rhs)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9006
testing::MatchesRegex
PolymorphicMatcher< internal::MatchesRegexMatcher > MatchesRegex(const internal::RE *regex)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8824
true
#define true
Definition: setup_once.h:324
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
testing::ElementsAreArray
internal::ElementsAreArrayMatcher< typename ::std::iterator_traits< Iter >::value_type > ElementsAreArray(Iter first, Iter last)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8465
MATCHER_P10
#define MATCHER_P10(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:992
testing::TestWithParam
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1883
x_
static double x_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4347
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
grpc::operator>=
bool operator>=(string_ref x, string_ref y)
Definition: grpcpp/impl/codegen/string_ref.h:143
testing::ElementsAre
internal::ElementsAreMatcher< ::testing::tuple<> > ElementsAre()
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:13040
testing::TEST_P
TEST_P(CodeLocationForTESTP, Verify)
Definition: bloaty/third_party/googletest/googletest/test/gtest_unittest.cc:5344
testing::operator<<
std::ostream & operator<<(std::ostream &os, const Message &sb)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-message.h:198
rhs_used_
std::vector< size_t > rhs_used_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:6283
GTEST_FLAG_PREFIX_
#define GTEST_FLAG_PREFIX_
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:277
GTEST_LOG_
#define GTEST_LOG_(severity)
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:975
o
UnboundConversion o
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:97
grpc::operator<
bool operator<(string_ref x, string_ref y)
Definition: grpcpp/impl/codegen/string_ref.h:140
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
testing::FloatEq
internal::FloatingEqMatcher< float > FloatEq(float rhs)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8662
testing::Property
PolymorphicMatcher< internal::PropertyMatcher< Class, PropertyType > > Property(PropertyType(Class::*property)() const, const PropertyMatcher &matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8732
best_so_far_
ElementMatcherPairs best_so_far_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:6285
re2::T1
@ T1
Definition: bloaty/third_party/re2/util/rune.cc:31
testing::StrNe
PolymorphicMatcher< internal::StrEqualityMatcher< internal::string > > StrNe(const internal::string &str)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8781
testing::NanSensitiveFloatNear
internal::FloatingEqMatcher< float > NanSensitiveFloatNear(float rhs, float max_abs_error)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8683
testing::internal::ProxyTypeList
Definition: googletest/googletest/include/gtest/internal/gtest-type-util.h:155
gen_gtest_pred_impl.Iter
def Iter(n, format, sep='')
Definition: bloaty/third_party/googletest/googletest/scripts/gen_gtest_pred_impl.py:188
grpc::operator<=
bool operator<=(string_ref x, string_ref y)
Definition: grpcpp/impl/codegen/string_ref.h:141
n_
int n_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4344
has_value_
bool has_value_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:6880
absl::FormatConversionChar::e
@ e
SCOPED_TRACE
#define SCOPED_TRACE(message)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2264
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
close_to_infinity_
const RawType close_to_infinity_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3406
d2
static const fe d2
Definition: curve25519_tables.h:39
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
a2
T::first_type a2
Definition: abseil-cpp/absl/container/internal/hash_function_defaults_test.cc:307
xds_interop_client.int
int
Definition: xds_interop_client.py:113
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
array
Definition: undname.c:101
testing::MakePolymorphicMatcher
PolymorphicMatcher< Impl > MakePolymorphicMatcher(const Impl &impl)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:5315
further_from_negative_zero_
const RawType further_from_negative_zero_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3398
Type
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:182
absl::random_internal_nanobenchmark::Func
FuncOutput(*)(const void *, FuncInput) Func
Definition: abseil-cpp/absl/random/internal/nanobenchmark.h:68
member_2
std::string member_2
Definition: googletest/googlemock/test/gmock-matchers_test.cc:1487
MOCK_METHOD1
#define MOCK_METHOD1(m,...)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h:600
ASSERT_THAT
#define ASSERT_THAT(value, matcher)
max_
const RawType max_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3410
text_format_test_wrapper.sep
sep
Definition: text_format_test_wrapper.py:34
one_bits_
const Bits one_bits_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3392
test_vector
static void test_vector(const char *raw, size_t raw_length, const char *encoded, size_t encoded_length, grpc_core::PercentEncodingType type)
Definition: percent_encoding_test.cc:37
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
testing::StartsWith
PolymorphicMatcher< internal::StartsWithMatcher< internal::string > > StartsWith(const internal::string &prefix)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8810
google::protobuf.internal::IsNan
bool IsNan(T)
Definition: protobuf/src/google/protobuf/stubs/mathutil.h:54
testing::Eq
internal::EqMatcher< T > Eq(T x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8561
remainder_
std::list< value_type > remainder_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:5495
opencensus.proto.stats.v1.stats_pb2.Tag
Tag
Definition: stats_pb2.py:431
bits
OPENSSL_EXPORT ASN1_BIT_STRING * bits
Definition: x509v3.h:482
grpc::operator>
bool operator>(string_ref x, string_ref y)
Definition: grpcpp/impl/codegen/string_ref.h:142
bm_diff.diff
diff
Definition: bm_diff.py:274
absl::inlined_vector_internal::Pointer
typename AllocatorTraits< A >::pointer Pointer
Definition: abseil-cpp/absl/container/internal/inlined_vector.h:52
arg
Definition: cmdline.cc:40
ref
unsigned ref
Definition: cxa_demangle.cpp:4909
absl::compare_internal::value_type
int8_t value_type
Definition: abseil-cpp/absl/types/compare.h:45
number
int32_t number
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:850
operator!=
bool operator!=(const Bytes &a, const Bytes &b)
Definition: boringssl-with-bazel/src/crypto/test/test_util.h:58
testing::NanSensitiveDoubleNear
internal::FloatingEqMatcher< double > NanSensitiveDoubleNear(double rhs, double max_abs_error)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8655
Call
Definition: api_fuzzer.cc:319
testing::SizeIs
internal::SizeIsMatcher< SizeMatcher > SizeIs(const SizeMatcher &size_matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8947
testing::internal::wstring
::std::wstring wstring
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:887
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
gen_synthetic_protos.base
base
Definition: gen_synthetic_protos.py:31
zero_bits_
const Bits zero_bits_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3391
matches_
ElementMatcherPairs matches_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:6284
testing::internal::FormatMatcherDescription
GTEST_API_ std::string FormatMatcherDescription(bool negation, const char *matcher_name, const Strings &param_values)
Definition: bloaty/third_party/googletest/googlemock/src/gmock-matchers.cc:52
a1
T::first_type a1
Definition: abseil-cpp/absl/container/internal/hash_function_defaults_test.cc:305
testing::internal::ElementsAreMatcher
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8296
testing::internal::DummyMatchResultListener
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:5047
kInt
static const int kInt
Definition: googletest/googlemock/test/gmock-matchers_test.cc:290
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
testing::WhenSortedBy
internal::WhenSortedByMatcher< Comparator, ContainerMatcher > WhenSortedBy(const Comparator &comparator, const ContainerMatcher &container_matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8981
g
struct @717 g
ADD_FAILURE
#define ADD_FAILURE()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1911
rhs_
const T rhs_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:137
further_from_one_
const RawType further_from_one_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3402
c_
char c_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:930
absl::holds_alternative
constexpr bool holds_alternative(const variant< Types... > &v) noexcept
Definition: abseil-cpp/absl/types/variant.h:264
testing::Matcher
Definition: bloaty/third_party/googletest/googlemock/include/gmock/internal/gmock-internal-utils.h:52
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
EXPECT_DEATH_IF_SUPPORTED
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-death-test.h:335
adl_test
Definition: bloaty/third_party/googletest/googlemock/test/gmock-generated-matchers_test.cc:1253
testing::Pair
internal::PairMatcher< FirstMatcher, SecondMatcher > Pair(FirstMatcher first_matcher, SecondMatcher second_matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9152
re2::operator++
static void operator++(Engine &e, int unused)
Definition: bloaty/third_party/re2/re2/testing/tester.h:39
nested
static int nested
Definition: test-callback-stack.c:39
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
MATCHER_P6
#define MATCHER_P6(name, p0, p1, p2, p3, p4, p5, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:633
testing::StrCaseEq
PolymorphicMatcher< internal::StrEqualityMatcher< internal::string > > StrCaseEq(const internal::string &str)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8788
EXPECT_CALL
#define EXPECT_CALL(obj, call)
framework.rpc.grpc_channelz.Address
Address
Definition: grpc_channelz.py:50
testing::MATCHER
MATCHER(IsEmpty, negation ? "isn't empty" :"is empty")
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-more-matchers.h:61
MOCK_METHOD2
#define MOCK_METHOD2(m,...)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h:601
GTEST_DISALLOW_COPY_AND_ASSIGN_
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:683
divider_
int divider_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4852
ON_CALL
#define ON_CALL(obj, call)
z
Uncopyable z
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4085
googletest-output-test.test_list
def test_list
Definition: bloaty/third_party/googletest/googletest/test/googletest-output-test.py:250
FATAL
#define FATAL(msg)
Definition: task.h:88
testing::MakeMatcher
Matcher< T > MakeMatcher(const MatcherInterface< T > *impl)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:5303
testing::Value
bool Value(const T &value, M matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9166
benchmark.md
md
Definition: benchmark.py:86
testing::_
const internal::AnythingMatcher _
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8548
testing::TYPED_TEST_SUITE
TYPED_TEST_SUITE(CodeLocationForTYPEDTEST, int)
func
const EVP_CIPHER *(* func)(void)
Definition: cipher_extra.c:73
testing::Values
internal::ValueArray< T... > Values(T... v)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-param-test.h:335
grpc_core::operator*
Duration operator*(Duration lhs, double rhs)
Definition: src/core/lib/gprpp/time.h:257
MATCHER_P2
#define MATCHER_P2(name, p0, p1, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:364
testing::Matches
internal::MatcherAsPredicate< M > Matches(M matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9160
s_
std::string s_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4345
suffix
unsigned char suffix[65536]
Definition: bloaty/third_party/zlib/examples/gun.c:164
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
MATCHER_P3
#define MATCHER_P3(name, p0, p1, p2, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:423
tests.unit._server_ssl_cert_config_test.Call
Call
Definition: _server_ssl_cert_config_test.py:70
testing::Le
internal::LeMatcher< Rhs > Le(Rhs x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8597
re2::T2
@ T2
Definition: bloaty/third_party/re2/util/rune.cc:33
absl::container_internal::IsEmpty
bool IsEmpty(ctrl_t c)
Definition: abseil-cpp/absl/container/internal/raw_hash_set.h:489
absl::str_format_internal::LengthMod::t
@ t
testing::FloatNear
internal::FloatingEqMatcher< float > FloatNear(float rhs, float max_abs_error)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8675
testing::internal::ElementsAreArrayMatcher
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8344
testing::Key
internal::KeyMatcher< M > Key(M inner_matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9141
testing::Field
PolymorphicMatcher< internal::FieldMatcher< Class, FieldType > > Field(FieldType Class::*field, const FieldMatcher &matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8715
nan1_
const RawType nan1_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3413
k_
int k_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4357
L
lua_State * L
Definition: upb/upb/bindings/lua/main.c:35
i_
int i_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:1071
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
has_int_
bool has_int_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:6936
testing::ExplainMatchResult
bool ExplainMatchResult(M matcher, const T &value, MatchResultListener *listener)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9173
first
StrT first
Definition: cxa_demangle.cpp:4884
testing::internal::ElementMatcherPairs
::std::vector< ElementMatcherPair > ElementMatcherPairs
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8117
MATCHER_P
#define MATCHER_P(name, p0, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:311
testing::INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(, CodeLocationForTESTP, Values(0))
A
Definition: miscompile_with_no_unique_address_test.cc:23
testing::TEST
TEST(GrpcAwsRequestSignerTest, AWSOfficialExample)
Definition: aws_request_signer_test.cc:68
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
cpp.gmock_class.set
set
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/gmock_class.py:44
xds_manager.num
num
Definition: xds_manager.py:56
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
testing::UnorderedElementsAre
internal::UnorderedElementsAreMatcher< ::testing::tuple<> > UnorderedElementsAre()
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:13255
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1976
arg
struct arg arg
testing::internal::FloatingPoint::Bits
TypeWithSize< sizeof(RawType)>::UInt Bits
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-internal.h:242
MATCHER_P5
#define MATCHER_P5(name, p0, p1, p2, p3, p4, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:557
absl::ABSL_NAMESPACE_BEGIN::dummy
int dummy
Definition: function_type_benchmark.cc:28
MOCK_METHOD
#define MOCK_METHOD(...)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-function-mocker.h:42
matcher_test
Definition: bloaty/third_party/googletest/googlemock/test/gmock-generated-matchers_test.cc:675
testing::IsNull
PolymorphicMatcher< internal::IsNullMatcher > IsNull()
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8614
testing::internal::FindMaxBipartiteMatching
GTEST_API_ ElementMatcherPairs FindMaxBipartiteMatching(const MatchMatrix &g)
Definition: bloaty/third_party/googletest/googlemock/src/gmock-matchers.cc:228
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
nan2_
const RawType nan2_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3414
testing::internal::Strings
::std::vector< ::std::string > Strings
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-printers.h:872
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
GTEST_FLAG_GET
#define GTEST_FLAG_GET(name)
Definition: googletest/googletest/include/gtest/internal/gtest-port.h:2218
testing::internal::IsReadableTypeName
bool IsReadableTypeName(const string &type_name)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:5507
testing::AllOf
internal::AllOfResult2< M1, M2 >::type AllOf(M1 m1, M2 m2)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:13472
lhs_used_
std::vector< size_t > lhs_used_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:6282
testing::ContainerEq
PolymorphicMatcher< internal::ContainerEqMatcher< GTEST_REMOVE_CONST_(Container)> > ContainerEq(const Container &rhs)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8969
testing::NanSensitiveDoubleEq
internal::FloatingEqMatcher< double > NanSensitiveDoubleEq(double rhs)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8640
testing::UnorderedElementsAreArray
internal::UnorderedElementsAreArrayMatcher< typename ::std::iterator_traits< Iter >::value_type > UnorderedElementsAreArray(Iter first, Iter last)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8507
MATCHER_P9
#define MATCHER_P9(name, p0, p1, p2, p3, p4, p5, p6, p7, p8, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:894
testing::StrCaseNe
PolymorphicMatcher< internal::StrEqualityMatcher< internal::string > > StrCaseNe(const internal::string &str)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8795
testing::StrEq
PolymorphicMatcher< internal::StrEqualityMatcher< internal::string > > StrEq(const internal::string &str)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8774
make_curve25519_tables.d
int d
Definition: make_curve25519_tables.py:53
MATCHER_P7
#define MATCHER_P7(name, p0, p1, p2, p3, p4, p5, p6, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:712
EXPECT_NONFATAL_FAILURE
#define EXPECT_NONFATAL_FAILURE(statement, substr)
close_to_positive_zero_
const RawType close_to_positive_zero_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3396
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
run_grpclb_interop_tests.l
dictionary l
Definition: run_grpclb_interop_tests.py:410
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
testing::EndsWith
PolymorphicMatcher< internal::EndsWithMatcher< internal::string > > EndsWith(const internal::string &suffix)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8817
testing::Each
internal::EachMatcher< M > Each(M matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9133
testing::TYPED_TEST
TYPED_TEST(CodeLocationForTYPEDTEST, Verify)
Definition: bloaty/third_party/googletest/googletest/test/gtest_unittest.cc:5356
testing::Ref
internal::RefMatcher< T & > Ref(T &x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8628
regress.m
m
Definition: regress/regress.py:25
index_
int index_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:6990
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
absl::str_format_internal::LengthMod::h
@ h
graph_
const Graph * graph_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:6281
testing::HasSubstr
PolymorphicMatcher< internal::HasSubstrMatcher< internal::string > > HasSubstr(const internal::string &substring)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8803
testing::Contains
internal::ContainsMatcher< M > Contains(M matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9101
pos_
std::list< value_type >::iterator pos_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:5480
ch
char ch
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4091
pair
std::pair< std::string, std::string > pair
Definition: abseil-cpp/absl/container/internal/raw_hash_set_benchmark.cc:78
testing::internal::RE
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:882
x
int x
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4083
testing::internal::ElementMatcherPair
::std::pair< size_t, size_t > ElementMatcherPair
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8116
absl::any_cast
ValueType any_cast(const any &operand)
Definition: abseil-cpp/absl/types/any.h:454
container
static struct async_container * container
Definition: benchmark-million-async.c:33
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
bar
void bar()
Definition: bar.cc:3
result_type
const typedef int * result_type
Definition: googletest/googlemock/test/gmock-matchers_test.cc:4812
type_name
static const char * type_name(int type)
Definition: adig.c:889
testing::PrintToString
::std::string PrintToString(const T &value)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-printers.h:915
number_
int number_
Definition: googletest/googlemock/test/gmock-matchers_test.cc:3016
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:35