Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:19:56

0001 // Copyright 2008, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 
0030 // Google Mock - a framework for writing C++ mock classes.
0031 //
0032 // This file tests the built-in matchers generated by a script.
0033 
0034 #include "gmock/gmock-generated-matchers.h"
0035 
0036 #include <list>
0037 #include <map>
0038 #include <set>
0039 #include <sstream>
0040 #include <string>
0041 #include <utility>
0042 #include <vector>
0043 
0044 #include "gmock/gmock.h"
0045 #include "gtest/gtest.h"
0046 #include "gtest/gtest-spi.h"
0047 
0048 namespace {
0049 
0050 using std::list;
0051 using std::map;
0052 using std::pair;
0053 using std::set;
0054 using std::stringstream;
0055 using std::vector;
0056 using testing::get;
0057 using testing::make_tuple;
0058 using testing::tuple;
0059 using testing::_;
0060 using testing::Args;
0061 using testing::Contains;
0062 using testing::ElementsAre;
0063 using testing::ElementsAreArray;
0064 using testing::Eq;
0065 using testing::Ge;
0066 using testing::Gt;
0067 using testing::Le;
0068 using testing::Lt;
0069 using testing::MakeMatcher;
0070 using testing::Matcher;
0071 using testing::MatcherInterface;
0072 using testing::MatchResultListener;
0073 using testing::Ne;
0074 using testing::Not;
0075 using testing::Pointee;
0076 using testing::PrintToString;
0077 using testing::Ref;
0078 using testing::StaticAssertTypeEq;
0079 using testing::StrEq;
0080 using testing::Value;
0081 using testing::internal::ElementsAreArrayMatcher;
0082 using testing::internal::string;
0083 
0084 // Returns the description of the given matcher.
0085 template <typename T>
0086 string Describe(const Matcher<T>& m) {
0087   stringstream ss;
0088   m.DescribeTo(&ss);
0089   return ss.str();
0090 }
0091 
0092 // Returns the description of the negation of the given matcher.
0093 template <typename T>
0094 string DescribeNegation(const Matcher<T>& m) {
0095   stringstream ss;
0096   m.DescribeNegationTo(&ss);
0097   return ss.str();
0098 }
0099 
0100 // Returns the reason why x matches, or doesn't match, m.
0101 template <typename MatcherType, typename Value>
0102 string Explain(const MatcherType& m, const Value& x) {
0103   stringstream ss;
0104   m.ExplainMatchResultTo(x, &ss);
0105   return ss.str();
0106 }
0107 
0108 // Tests Args<k0, ..., kn>(m).
0109 
0110 TEST(ArgsTest, AcceptsZeroTemplateArg) {
0111   const tuple<int, bool> t(5, true);
0112   EXPECT_THAT(t, Args<>(Eq(tuple<>())));
0113   EXPECT_THAT(t, Not(Args<>(Ne(tuple<>()))));
0114 }
0115 
0116 TEST(ArgsTest, AcceptsOneTemplateArg) {
0117   const tuple<int, bool> t(5, true);
0118   EXPECT_THAT(t, Args<0>(Eq(make_tuple(5))));
0119   EXPECT_THAT(t, Args<1>(Eq(make_tuple(true))));
0120   EXPECT_THAT(t, Not(Args<1>(Eq(make_tuple(false)))));
0121 }
0122 
0123 TEST(ArgsTest, AcceptsTwoTemplateArgs) {
0124   const tuple<short, int, long> t(4, 5, 6L);  // NOLINT
0125 
0126   EXPECT_THAT(t, (Args<0, 1>(Lt())));
0127   EXPECT_THAT(t, (Args<1, 2>(Lt())));
0128   EXPECT_THAT(t, Not(Args<0, 2>(Gt())));
0129 }
0130 
0131 TEST(ArgsTest, AcceptsRepeatedTemplateArgs) {
0132   const tuple<short, int, long> t(4, 5, 6L);  // NOLINT
0133   EXPECT_THAT(t, (Args<0, 0>(Eq())));
0134   EXPECT_THAT(t, Not(Args<1, 1>(Ne())));
0135 }
0136 
0137 TEST(ArgsTest, AcceptsDecreasingTemplateArgs) {
0138   const tuple<short, int, long> t(4, 5, 6L);  // NOLINT
0139   EXPECT_THAT(t, (Args<2, 0>(Gt())));
0140   EXPECT_THAT(t, Not(Args<2, 1>(Lt())));
0141 }
0142 
0143 // The MATCHER*() macros trigger warning C4100 (unreferenced formal
0144 // parameter) in MSVC with -W4.  Unfortunately they cannot be fixed in
0145 // the macro definition, as the warnings are generated when the macro
0146 // is expanded and macro expansion cannot contain #pragma.  Therefore
0147 // we suppress them here.
0148 #ifdef _MSC_VER
0149 # pragma warning(push)
0150 # pragma warning(disable:4100)
0151 #endif
0152 
0153 MATCHER(SumIsZero, "") {
0154   return get<0>(arg) + get<1>(arg) + get<2>(arg) == 0;
0155 }
0156 
0157 TEST(ArgsTest, AcceptsMoreTemplateArgsThanArityOfOriginalTuple) {
0158   EXPECT_THAT(make_tuple(-1, 2), (Args<0, 0, 1>(SumIsZero())));
0159   EXPECT_THAT(make_tuple(1, 2), Not(Args<0, 0, 1>(SumIsZero())));
0160 }
0161 
0162 TEST(ArgsTest, CanBeNested) {
0163   const tuple<short, int, long, int> t(4, 5, 6L, 6);  // NOLINT
0164   EXPECT_THAT(t, (Args<1, 2, 3>(Args<1, 2>(Eq()))));
0165   EXPECT_THAT(t, (Args<0, 1, 3>(Args<0, 2>(Lt()))));
0166 }
0167 
0168 TEST(ArgsTest, CanMatchTupleByValue) {
0169   typedef tuple<char, int, int> Tuple3;
0170   const Matcher<Tuple3> m = Args<1, 2>(Lt());
0171   EXPECT_TRUE(m.Matches(Tuple3('a', 1, 2)));
0172   EXPECT_FALSE(m.Matches(Tuple3('b', 2, 2)));
0173 }
0174 
0175 TEST(ArgsTest, CanMatchTupleByReference) {
0176   typedef tuple<char, char, int> Tuple3;
0177   const Matcher<const Tuple3&> m = Args<0, 1>(Lt());
0178   EXPECT_TRUE(m.Matches(Tuple3('a', 'b', 2)));
0179   EXPECT_FALSE(m.Matches(Tuple3('b', 'b', 2)));
0180 }
0181 
0182 // Validates that arg is printed as str.
0183 MATCHER_P(PrintsAs, str, "") {
0184   return testing::PrintToString(arg) == str;
0185 }
0186 
0187 TEST(ArgsTest, AcceptsTenTemplateArgs) {
0188   EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
0189               (Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
0190                   PrintsAs("(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
0191   EXPECT_THAT(make_tuple(0, 1L, 2, 3L, 4, 5, 6, 7, 8, 9),
0192               Not(Args<9, 8, 7, 6, 5, 4, 3, 2, 1, 0>(
0193                       PrintsAs("(0, 8, 7, 6, 5, 4, 3, 2, 1, 0)"))));
0194 }
0195 
0196 TEST(ArgsTest, DescirbesSelfCorrectly) {
0197   const Matcher<tuple<int, bool, char> > m = Args<2, 0>(Lt());
0198   EXPECT_EQ("are a tuple whose fields (#2, #0) are a pair where "
0199             "the first < the second",
0200             Describe(m));
0201 }
0202 
0203 TEST(ArgsTest, DescirbesNestedArgsCorrectly) {
0204   const Matcher<const tuple<int, bool, char, int>&> m =
0205       Args<0, 2, 3>(Args<2, 0>(Lt()));
0206   EXPECT_EQ("are a tuple whose fields (#0, #2, #3) are a tuple "
0207             "whose fields (#2, #0) are a pair where the first < the second",
0208             Describe(m));
0209 }
0210 
0211 TEST(ArgsTest, DescribesNegationCorrectly) {
0212   const Matcher<tuple<int, char> > m = Args<1, 0>(Gt());
0213   EXPECT_EQ("are a tuple whose fields (#1, #0) aren't a pair "
0214             "where the first > the second",
0215             DescribeNegation(m));
0216 }
0217 
0218 TEST(ArgsTest, ExplainsMatchResultWithoutInnerExplanation) {
0219   const Matcher<tuple<bool, int, int> > m = Args<1, 2>(Eq());
0220   EXPECT_EQ("whose fields (#1, #2) are (42, 42)",
0221             Explain(m, make_tuple(false, 42, 42)));
0222   EXPECT_EQ("whose fields (#1, #2) are (42, 43)",
0223             Explain(m, make_tuple(false, 42, 43)));
0224 }
0225 
0226 // For testing Args<>'s explanation.
0227 class LessThanMatcher : public MatcherInterface<tuple<char, int> > {
0228  public:
0229   virtual void DescribeTo(::std::ostream* os) const {}
0230 
0231   virtual bool MatchAndExplain(tuple<char, int> value,
0232                                MatchResultListener* listener) const {
0233     const int diff = get<0>(value) - get<1>(value);
0234     if (diff > 0) {
0235       *listener << "where the first value is " << diff
0236                 << " more than the second";
0237     }
0238     return diff < 0;
0239   }
0240 };
0241 
0242 Matcher<tuple<char, int> > LessThan() {
0243   return MakeMatcher(new LessThanMatcher);
0244 }
0245 
0246 TEST(ArgsTest, ExplainsMatchResultWithInnerExplanation) {
0247   const Matcher<tuple<char, int, int> > m = Args<0, 2>(LessThan());
0248   EXPECT_EQ("whose fields (#0, #2) are ('a' (97, 0x61), 42), "
0249             "where the first value is 55 more than the second",
0250             Explain(m, make_tuple('a', 42, 42)));
0251   EXPECT_EQ("whose fields (#0, #2) are ('\\0', 43)",
0252             Explain(m, make_tuple('\0', 42, 43)));
0253 }
0254 
0255 // For testing ExplainMatchResultTo().
0256 class GreaterThanMatcher : public MatcherInterface<int> {
0257  public:
0258   explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {}
0259 
0260   virtual void DescribeTo(::std::ostream* os) const {
0261     *os << "is greater than " << rhs_;
0262   }
0263 
0264   virtual bool MatchAndExplain(int lhs,
0265                                MatchResultListener* listener) const {
0266     const int diff = lhs - rhs_;
0267     if (diff > 0) {
0268       *listener << "which is " << diff << " more than " << rhs_;
0269     } else if (diff == 0) {
0270       *listener << "which is the same as " << rhs_;
0271     } else {
0272       *listener << "which is " << -diff << " less than " << rhs_;
0273     }
0274 
0275     return lhs > rhs_;
0276   }
0277 
0278  private:
0279   int rhs_;
0280 };
0281 
0282 Matcher<int> GreaterThan(int n) {
0283   return MakeMatcher(new GreaterThanMatcher(n));
0284 }
0285 
0286 // Tests for ElementsAre().
0287 
0288 TEST(ElementsAreTest, CanDescribeExpectingNoElement) {
0289   Matcher<const vector<int>&> m = ElementsAre();
0290   EXPECT_EQ("is empty", Describe(m));
0291 }
0292 
0293 TEST(ElementsAreTest, CanDescribeExpectingOneElement) {
0294   Matcher<vector<int> > m = ElementsAre(Gt(5));
0295   EXPECT_EQ("has 1 element that is > 5", Describe(m));
0296 }
0297 
0298 TEST(ElementsAreTest, CanDescribeExpectingManyElements) {
0299   Matcher<list<string> > m = ElementsAre(StrEq("one"), "two");
0300   EXPECT_EQ("has 2 elements where\n"
0301             "element #0 is equal to \"one\",\n"
0302             "element #1 is equal to \"two\"", Describe(m));
0303 }
0304 
0305 TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) {
0306   Matcher<vector<int> > m = ElementsAre();
0307   EXPECT_EQ("isn't empty", DescribeNegation(m));
0308 }
0309 
0310 TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) {
0311   Matcher<const list<int>& > m = ElementsAre(Gt(5));
0312   EXPECT_EQ("doesn't have 1 element, or\n"
0313             "element #0 isn't > 5", DescribeNegation(m));
0314 }
0315 
0316 TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) {
0317   Matcher<const list<string>& > m = ElementsAre("one", "two");
0318   EXPECT_EQ("doesn't have 2 elements, or\n"
0319             "element #0 isn't equal to \"one\", or\n"
0320             "element #1 isn't equal to \"two\"", DescribeNegation(m));
0321 }
0322 
0323 TEST(ElementsAreTest, DoesNotExplainTrivialMatch) {
0324   Matcher<const list<int>& > m = ElementsAre(1, Ne(2));
0325 
0326   list<int> test_list;
0327   test_list.push_back(1);
0328   test_list.push_back(3);
0329   EXPECT_EQ("", Explain(m, test_list));  // No need to explain anything.
0330 }
0331 
0332 TEST(ElementsAreTest, ExplainsNonTrivialMatch) {
0333   Matcher<const vector<int>& > m =
0334       ElementsAre(GreaterThan(1), 0, GreaterThan(2));
0335 
0336   const int a[] = { 10, 0, 100 };
0337   vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
0338   EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n"
0339             "and whose element #2 matches, which is 98 more than 2",
0340             Explain(m, test_vector));
0341 }
0342 
0343 TEST(ElementsAreTest, CanExplainMismatchWrongSize) {
0344   Matcher<const list<int>& > m = ElementsAre(1, 3);
0345 
0346   list<int> test_list;
0347   // No need to explain when the container is empty.
0348   EXPECT_EQ("", Explain(m, test_list));
0349 
0350   test_list.push_back(1);
0351   EXPECT_EQ("which has 1 element", Explain(m, test_list));
0352 }
0353 
0354 TEST(ElementsAreTest, CanExplainMismatchRightSize) {
0355   Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5));
0356 
0357   vector<int> v;
0358   v.push_back(2);
0359   v.push_back(1);
0360   EXPECT_EQ("whose element #0 doesn't match", Explain(m, v));
0361 
0362   v[0] = 1;
0363   EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5",
0364             Explain(m, v));
0365 }
0366 
0367 TEST(ElementsAreTest, MatchesOneElementVector) {
0368   vector<string> test_vector;
0369   test_vector.push_back("test string");
0370 
0371   EXPECT_THAT(test_vector, ElementsAre(StrEq("test string")));
0372 }
0373 
0374 TEST(ElementsAreTest, MatchesOneElementList) {
0375   list<string> test_list;
0376   test_list.push_back("test string");
0377 
0378   EXPECT_THAT(test_list, ElementsAre("test string"));
0379 }
0380 
0381 TEST(ElementsAreTest, MatchesThreeElementVector) {
0382   vector<string> test_vector;
0383   test_vector.push_back("one");
0384   test_vector.push_back("two");
0385   test_vector.push_back("three");
0386 
0387   EXPECT_THAT(test_vector, ElementsAre("one", StrEq("two"), _));
0388 }
0389 
0390 TEST(ElementsAreTest, MatchesOneElementEqMatcher) {
0391   vector<int> test_vector;
0392   test_vector.push_back(4);
0393 
0394   EXPECT_THAT(test_vector, ElementsAre(Eq(4)));
0395 }
0396 
0397 TEST(ElementsAreTest, MatchesOneElementAnyMatcher) {
0398   vector<int> test_vector;
0399   test_vector.push_back(4);
0400 
0401   EXPECT_THAT(test_vector, ElementsAre(_));
0402 }
0403 
0404 TEST(ElementsAreTest, MatchesOneElementValue) {
0405   vector<int> test_vector;
0406   test_vector.push_back(4);
0407 
0408   EXPECT_THAT(test_vector, ElementsAre(4));
0409 }
0410 
0411 TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) {
0412   vector<int> test_vector;
0413   test_vector.push_back(1);
0414   test_vector.push_back(2);
0415   test_vector.push_back(3);
0416 
0417   EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _));
0418 }
0419 
0420 TEST(ElementsAreTest, MatchesTenElementVector) {
0421   const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
0422   vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
0423 
0424   EXPECT_THAT(test_vector,
0425               // The element list can contain values and/or matchers
0426               // of different types.
0427               ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _));
0428 }
0429 
0430 TEST(ElementsAreTest, DoesNotMatchWrongSize) {
0431   vector<string> test_vector;
0432   test_vector.push_back("test string");
0433   test_vector.push_back("test string");
0434 
0435   Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
0436   EXPECT_FALSE(m.Matches(test_vector));
0437 }
0438 
0439 TEST(ElementsAreTest, DoesNotMatchWrongValue) {
0440   vector<string> test_vector;
0441   test_vector.push_back("other string");
0442 
0443   Matcher<vector<string> > m = ElementsAre(StrEq("test string"));
0444   EXPECT_FALSE(m.Matches(test_vector));
0445 }
0446 
0447 TEST(ElementsAreTest, DoesNotMatchWrongOrder) {
0448   vector<string> test_vector;
0449   test_vector.push_back("one");
0450   test_vector.push_back("three");
0451   test_vector.push_back("two");
0452 
0453   Matcher<vector<string> > m = ElementsAre(
0454     StrEq("one"), StrEq("two"), StrEq("three"));
0455   EXPECT_FALSE(m.Matches(test_vector));
0456 }
0457 
0458 TEST(ElementsAreTest, WorksForNestedContainer) {
0459   const char* strings[] = {
0460     "Hi",
0461     "world"
0462   };
0463 
0464   vector<list<char> > nested;
0465   for (size_t i = 0; i < GTEST_ARRAY_SIZE_(strings); i++) {
0466     nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i])));
0467   }
0468 
0469   EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')),
0470                                   ElementsAre('w', 'o', _, _, 'd')));
0471   EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'),
0472                                       ElementsAre('w', 'o', _, _, 'd'))));
0473 }
0474 
0475 TEST(ElementsAreTest, WorksWithByRefElementMatchers) {
0476   int a[] = { 0, 1, 2 };
0477   vector<int> v(a, a + GTEST_ARRAY_SIZE_(a));
0478 
0479   EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2])));
0480   EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2]))));
0481 }
0482 
0483 TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) {
0484   int a[] = { 0, 1, 2 };
0485   vector<int> v(a, a + GTEST_ARRAY_SIZE_(a));
0486 
0487   EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _)));
0488   EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3))));
0489 }
0490 
0491 TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) {
0492   int array[] = { 0, 1, 2 };
0493   EXPECT_THAT(array, ElementsAre(0, 1, _));
0494   EXPECT_THAT(array, Not(ElementsAre(1, _, _)));
0495   EXPECT_THAT(array, Not(ElementsAre(0, _)));
0496 }
0497 
0498 class NativeArrayPassedAsPointerAndSize {
0499  public:
0500   NativeArrayPassedAsPointerAndSize() {}
0501 
0502   MOCK_METHOD2(Helper, void(int* array, int size));
0503 
0504  private:
0505   GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize);
0506 };
0507 
0508 TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) {
0509   int array[] = { 0, 1 };
0510   ::testing::tuple<int*, size_t> array_as_tuple(array, 2);
0511   EXPECT_THAT(array_as_tuple, ElementsAre(0, 1));
0512   EXPECT_THAT(array_as_tuple, Not(ElementsAre(0)));
0513 
0514   NativeArrayPassedAsPointerAndSize helper;
0515   EXPECT_CALL(helper, Helper(_, _))
0516       .With(ElementsAre(0, 1));
0517   helper.Helper(array, 2);
0518 }
0519 
0520 TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) {
0521   const char a2[][3] = { "hi", "lo" };
0522   EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'),
0523                               ElementsAre('l', 'o', '\0')));
0524   EXPECT_THAT(a2, ElementsAre(StrEq("hi"), StrEq("lo")));
0525   EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')),
0526                               ElementsAre('l', 'o', '\0')));
0527 }
0528 
0529 TEST(ElementsAreTest, AcceptsStringLiteral) {
0530   string array[] = { "hi", "one", "two" };
0531   EXPECT_THAT(array, ElementsAre("hi", "one", "two"));
0532   EXPECT_THAT(array, Not(ElementsAre("hi", "one", "too")));
0533 }
0534 
0535 #ifndef _MSC_VER
0536 
0537 // The following test passes a value of type const char[] to a
0538 // function template that expects const T&.  Some versions of MSVC
0539 // generates a compiler error C2665 for that.  We believe it's a bug
0540 // in MSVC.  Therefore this test is #if-ed out for MSVC.
0541 
0542 // Declared here with the size unknown.  Defined AFTER the following test.
0543 extern const char kHi[];
0544 
0545 TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) {
0546   // The size of kHi is not known in this test, but ElementsAre() should
0547   // still accept it.
0548 
0549   string array1[] = { "hi" };
0550   EXPECT_THAT(array1, ElementsAre(kHi));
0551 
0552   string array2[] = { "ho" };
0553   EXPECT_THAT(array2, Not(ElementsAre(kHi)));
0554 }
0555 
0556 const char kHi[] = "hi";
0557 
0558 #endif  // _MSC_VER
0559 
0560 TEST(ElementsAreTest, MakesCopyOfArguments) {
0561   int x = 1;
0562   int y = 2;
0563   // This should make a copy of x and y.
0564   ::testing::internal::ElementsAreMatcher<testing::tuple<int, int> >
0565           polymorphic_matcher = ElementsAre(x, y);
0566   // Changing x and y now shouldn't affect the meaning of the above matcher.
0567   x = y = 0;
0568   const int array1[] = { 1, 2 };
0569   EXPECT_THAT(array1, polymorphic_matcher);
0570   const int array2[] = { 0, 0 };
0571   EXPECT_THAT(array2, Not(polymorphic_matcher));
0572 }
0573 
0574 
0575 // Tests for ElementsAreArray().  Since ElementsAreArray() shares most
0576 // of the implementation with ElementsAre(), we don't test it as
0577 // thoroughly here.
0578 
0579 TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) {
0580   const int a[] = { 1, 2, 3 };
0581 
0582   vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
0583   EXPECT_THAT(test_vector, ElementsAreArray(a));
0584 
0585   test_vector[2] = 0;
0586   EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
0587 }
0588 
0589 TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) {
0590   const char* a[] = { "one", "two", "three" };
0591 
0592   vector<string> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
0593   EXPECT_THAT(test_vector, ElementsAreArray(a, GTEST_ARRAY_SIZE_(a)));
0594 
0595   const char** p = a;
0596   test_vector[0] = "1";
0597   EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GTEST_ARRAY_SIZE_(a))));
0598 }
0599 
0600 TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) {
0601   const char* a[] = { "one", "two", "three" };
0602 
0603   vector<string> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
0604   EXPECT_THAT(test_vector, ElementsAreArray(a));
0605 
0606   test_vector[0] = "1";
0607   EXPECT_THAT(test_vector, Not(ElementsAreArray(a)));
0608 }
0609 
0610 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) {
0611   const Matcher<string> kMatcherArray[] =
0612     { StrEq("one"), StrEq("two"), StrEq("three") };
0613 
0614   vector<string> test_vector;
0615   test_vector.push_back("one");
0616   test_vector.push_back("two");
0617   test_vector.push_back("three");
0618   EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray));
0619 
0620   test_vector.push_back("three");
0621   EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray)));
0622 }
0623 
0624 TEST(ElementsAreArrayTest, CanBeCreatedWithVector) {
0625   const int a[] = { 1, 2, 3 };
0626   vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
0627   const vector<int> expected(a, a + GTEST_ARRAY_SIZE_(a));
0628   EXPECT_THAT(test_vector, ElementsAreArray(expected));
0629   test_vector.push_back(4);
0630   EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
0631 }
0632 
0633 #if GTEST_HAS_STD_INITIALIZER_LIST_
0634 
0635 TEST(ElementsAreArrayTest, TakesInitializerList) {
0636   const int a[5] = { 1, 2, 3, 4, 5 };
0637   EXPECT_THAT(a, ElementsAreArray({ 1, 2, 3, 4, 5 }));
0638   EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 5, 4 })));
0639   EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 4, 6 })));
0640 }
0641 
0642 TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) {
0643   const string a[5] = { "a", "b", "c", "d", "e" };
0644   EXPECT_THAT(a, ElementsAreArray({ "a", "b", "c", "d", "e" }));
0645   EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "e", "d" })));
0646   EXPECT_THAT(a, Not(ElementsAreArray({ "a", "b", "c", "d", "ef" })));
0647 }
0648 
0649 TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) {
0650   const int a[5] = { 1, 2, 3, 4, 5 };
0651   EXPECT_THAT(a, ElementsAreArray(
0652       { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) }));
0653   EXPECT_THAT(a, Not(ElementsAreArray(
0654       { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) })));
0655 }
0656 
0657 TEST(ElementsAreArrayTest,
0658      TakesInitializerListOfDifferentTypedMatchers) {
0659   const int a[5] = { 1, 2, 3, 4, 5 };
0660   // The compiler cannot infer the type of the initializer list if its
0661   // elements have different types.  We must explicitly specify the
0662   // unified element type in this case.
0663   EXPECT_THAT(a, ElementsAreArray<Matcher<int> >(
0664       { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) }));
0665   EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int> >(
0666       { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) })));
0667 }
0668 
0669 #endif  // GTEST_HAS_STD_INITIALIZER_LIST_
0670 
0671 TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) {
0672   const int a[] = { 1, 2, 3 };
0673   const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) };
0674   vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
0675   const vector<Matcher<int> > expected(
0676       kMatchers, kMatchers + GTEST_ARRAY_SIZE_(kMatchers));
0677   EXPECT_THAT(test_vector, ElementsAreArray(expected));
0678   test_vector.push_back(4);
0679   EXPECT_THAT(test_vector, Not(ElementsAreArray(expected)));
0680 }
0681 
0682 TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) {
0683   const int a[] = { 1, 2, 3 };
0684   const vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
0685   const vector<int> expected(a, a + GTEST_ARRAY_SIZE_(a));
0686   EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end()));
0687   // Pointers are iterators, too.
0688   EXPECT_THAT(test_vector, ElementsAreArray(a, a + GTEST_ARRAY_SIZE_(a)));
0689   // The empty range of NULL pointers should also be okay.
0690   int* const null_int = NULL;
0691   EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int)));
0692   EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int));
0693 }
0694 
0695 // Since ElementsAre() and ElementsAreArray() share much of the
0696 // implementation, we only do a sanity test for native arrays here.
0697 TEST(ElementsAreArrayTest, WorksWithNativeArray) {
0698   ::std::string a[] = { "hi", "ho" };
0699   ::std::string b[] = { "hi", "ho" };
0700 
0701   EXPECT_THAT(a, ElementsAreArray(b));
0702   EXPECT_THAT(a, ElementsAreArray(b, 2));
0703   EXPECT_THAT(a, Not(ElementsAreArray(b, 1)));
0704 }
0705 
0706 TEST(ElementsAreArrayTest, SourceLifeSpan) {
0707   const int a[] = { 1, 2, 3 };
0708   vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a));
0709   vector<int> expect(a, a + GTEST_ARRAY_SIZE_(a));
0710   ElementsAreArrayMatcher<int> matcher_maker =
0711       ElementsAreArray(expect.begin(), expect.end());
0712   EXPECT_THAT(test_vector, matcher_maker);
0713   // Changing in place the values that initialized matcher_maker should not
0714   // affect matcher_maker anymore. It should have made its own copy of them.
0715   typedef vector<int>::iterator Iter;
0716   for (Iter it = expect.begin(); it != expect.end(); ++it) { *it += 10; }
0717   EXPECT_THAT(test_vector, matcher_maker);
0718   test_vector.push_back(3);
0719   EXPECT_THAT(test_vector, Not(matcher_maker));
0720 }
0721 
0722 // Tests for the MATCHER*() macro family.
0723 
0724 // Tests that a simple MATCHER() definition works.
0725 
0726 MATCHER(IsEven, "") { return (arg % 2) == 0; }
0727 
0728 TEST(MatcherMacroTest, Works) {
0729   const Matcher<int> m = IsEven();
0730   EXPECT_TRUE(m.Matches(6));
0731   EXPECT_FALSE(m.Matches(7));
0732 
0733   EXPECT_EQ("is even", Describe(m));
0734   EXPECT_EQ("not (is even)", DescribeNegation(m));
0735   EXPECT_EQ("", Explain(m, 6));
0736   EXPECT_EQ("", Explain(m, 7));
0737 }
0738 
0739 // This also tests that the description string can reference 'negation'.
0740 MATCHER(IsEven2, negation ? "is odd" : "is even") {
0741   if ((arg % 2) == 0) {
0742     // Verifies that we can stream to result_listener, a listener
0743     // supplied by the MATCHER macro implicitly.
0744     *result_listener << "OK";
0745     return true;
0746   } else {
0747     *result_listener << "% 2 == " << (arg % 2);
0748     return false;
0749   }
0750 }
0751 
0752 // This also tests that the description string can reference matcher
0753 // parameters.
0754 MATCHER_P2(EqSumOf, x, y,
0755            string(negation ? "doesn't equal" : "equals") + " the sum of " +
0756            PrintToString(x) + " and " + PrintToString(y)) {
0757   if (arg == (x + y)) {
0758     *result_listener << "OK";
0759     return true;
0760   } else {
0761     // Verifies that we can stream to the underlying stream of
0762     // result_listener.
0763     if (result_listener->stream() != NULL) {
0764       *result_listener->stream() << "diff == " << (x + y - arg);
0765     }
0766     return false;
0767   }
0768 }
0769 
0770 // Tests that the matcher description can reference 'negation' and the
0771 // matcher parameters.
0772 TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) {
0773   const Matcher<int> m1 = IsEven2();
0774   EXPECT_EQ("is even", Describe(m1));
0775   EXPECT_EQ("is odd", DescribeNegation(m1));
0776 
0777   const Matcher<int> m2 = EqSumOf(5, 9);
0778   EXPECT_EQ("equals the sum of 5 and 9", Describe(m2));
0779   EXPECT_EQ("doesn't equal the sum of 5 and 9", DescribeNegation(m2));
0780 }
0781 
0782 // Tests explaining match result in a MATCHER* macro.
0783 TEST(MatcherMacroTest, CanExplainMatchResult) {
0784   const Matcher<int> m1 = IsEven2();
0785   EXPECT_EQ("OK", Explain(m1, 4));
0786   EXPECT_EQ("% 2 == 1", Explain(m1, 5));
0787 
0788   const Matcher<int> m2 = EqSumOf(1, 2);
0789   EXPECT_EQ("OK", Explain(m2, 3));
0790   EXPECT_EQ("diff == -1", Explain(m2, 4));
0791 }
0792 
0793 // Tests that the body of MATCHER() can reference the type of the
0794 // value being matched.
0795 
0796 MATCHER(IsEmptyString, "") {
0797   StaticAssertTypeEq< ::std::string, arg_type>();
0798   return arg == "";
0799 }
0800 
0801 MATCHER(IsEmptyStringByRef, "") {
0802   StaticAssertTypeEq<const ::std::string&, arg_type>();
0803   return arg == "";
0804 }
0805 
0806 TEST(MatcherMacroTest, CanReferenceArgType) {
0807   const Matcher< ::std::string> m1 = IsEmptyString();
0808   EXPECT_TRUE(m1.Matches(""));
0809 
0810   const Matcher<const ::std::string&> m2 = IsEmptyStringByRef();
0811   EXPECT_TRUE(m2.Matches(""));
0812 }
0813 
0814 // Tests that MATCHER() can be used in a namespace.
0815 
0816 namespace matcher_test {
0817 MATCHER(IsOdd, "") { return (arg % 2) != 0; }
0818 }  // namespace matcher_test
0819 
0820 TEST(MatcherMacroTest, WorksInNamespace) {
0821   Matcher<int> m = matcher_test::IsOdd();
0822   EXPECT_FALSE(m.Matches(4));
0823   EXPECT_TRUE(m.Matches(5));
0824 }
0825 
0826 // Tests that Value() can be used to compose matchers.
0827 MATCHER(IsPositiveOdd, "") {
0828   return Value(arg, matcher_test::IsOdd()) && arg > 0;
0829 }
0830 
0831 TEST(MatcherMacroTest, CanBeComposedUsingValue) {
0832   EXPECT_THAT(3, IsPositiveOdd());
0833   EXPECT_THAT(4, Not(IsPositiveOdd()));
0834   EXPECT_THAT(-1, Not(IsPositiveOdd()));
0835 }
0836 
0837 // Tests that a simple MATCHER_P() definition works.
0838 
0839 MATCHER_P(IsGreaterThan32And, n, "") { return arg > 32 && arg > n; }
0840 
0841 TEST(MatcherPMacroTest, Works) {
0842   const Matcher<int> m = IsGreaterThan32And(5);
0843   EXPECT_TRUE(m.Matches(36));
0844   EXPECT_FALSE(m.Matches(5));
0845 
0846   EXPECT_EQ("is greater than 32 and 5", Describe(m));
0847   EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
0848   EXPECT_EQ("", Explain(m, 36));
0849   EXPECT_EQ("", Explain(m, 5));
0850 }
0851 
0852 // Tests that the description is calculated correctly from the matcher name.
0853 MATCHER_P(_is_Greater_Than32and_, n, "") { return arg > 32 && arg > n; }
0854 
0855 TEST(MatcherPMacroTest, GeneratesCorrectDescription) {
0856   const Matcher<int> m = _is_Greater_Than32and_(5);
0857 
0858   EXPECT_EQ("is greater than 32 and 5", Describe(m));
0859   EXPECT_EQ("not (is greater than 32 and 5)", DescribeNegation(m));
0860   EXPECT_EQ("", Explain(m, 36));
0861   EXPECT_EQ("", Explain(m, 5));
0862 }
0863 
0864 // Tests that a MATCHER_P matcher can be explicitly instantiated with
0865 // a reference parameter type.
0866 
0867 class UncopyableFoo {
0868  public:
0869   explicit UncopyableFoo(char value) : value_(value) {}
0870  private:
0871   UncopyableFoo(const UncopyableFoo&);
0872   void operator=(const UncopyableFoo&);
0873 
0874   char value_;
0875 };
0876 
0877 MATCHER_P(ReferencesUncopyable, variable, "") { return &arg == &variable; }
0878 
0879 TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) {
0880   UncopyableFoo foo1('1'), foo2('2');
0881   const Matcher<const UncopyableFoo&> m =
0882       ReferencesUncopyable<const UncopyableFoo&>(foo1);
0883 
0884   EXPECT_TRUE(m.Matches(foo1));
0885   EXPECT_FALSE(m.Matches(foo2));
0886 
0887   // We don't want the address of the parameter printed, as most
0888   // likely it will just annoy the user.  If the address is
0889   // interesting, the user should consider passing the parameter by
0890   // pointer instead.
0891   EXPECT_EQ("references uncopyable 1-byte object <31>", Describe(m));
0892 }
0893 
0894 
0895 // Tests that the body of MATCHER_Pn() can reference the parameter
0896 // types.
0897 
0898 MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "") {
0899   StaticAssertTypeEq<int, foo_type>();
0900   StaticAssertTypeEq<long, bar_type>();  // NOLINT
0901   StaticAssertTypeEq<char, baz_type>();
0902   return arg == 0;
0903 }
0904 
0905 TEST(MatcherPnMacroTest, CanReferenceParamTypes) {
0906   EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a'));
0907 }
0908 
0909 // Tests that a MATCHER_Pn matcher can be explicitly instantiated with
0910 // reference parameter types.
0911 
0912 MATCHER_P2(ReferencesAnyOf, variable1, variable2, "") {
0913   return &arg == &variable1 || &arg == &variable2;
0914 }
0915 
0916 TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) {
0917   UncopyableFoo foo1('1'), foo2('2'), foo3('3');
0918   const Matcher<const UncopyableFoo&> m =
0919       ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
0920 
0921   EXPECT_TRUE(m.Matches(foo1));
0922   EXPECT_TRUE(m.Matches(foo2));
0923   EXPECT_FALSE(m.Matches(foo3));
0924 }
0925 
0926 TEST(MatcherPnMacroTest,
0927      GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) {
0928   UncopyableFoo foo1('1'), foo2('2');
0929   const Matcher<const UncopyableFoo&> m =
0930       ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2);
0931 
0932   // We don't want the addresses of the parameters printed, as most
0933   // likely they will just annoy the user.  If the addresses are
0934   // interesting, the user should consider passing the parameters by
0935   // pointers instead.
0936   EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)",
0937             Describe(m));
0938 }
0939 
0940 // Tests that a simple MATCHER_P2() definition works.
0941 
0942 MATCHER_P2(IsNotInClosedRange, low, hi, "") { return arg < low || arg > hi; }
0943 
0944 TEST(MatcherPnMacroTest, Works) {
0945   const Matcher<const long&> m = IsNotInClosedRange(10, 20);  // NOLINT
0946   EXPECT_TRUE(m.Matches(36L));
0947   EXPECT_FALSE(m.Matches(15L));
0948 
0949   EXPECT_EQ("is not in closed range (10, 20)", Describe(m));
0950   EXPECT_EQ("not (is not in closed range (10, 20))", DescribeNegation(m));
0951   EXPECT_EQ("", Explain(m, 36L));
0952   EXPECT_EQ("", Explain(m, 15L));
0953 }
0954 
0955 // Tests that MATCHER*() definitions can be overloaded on the number
0956 // of parameters; also tests MATCHER_Pn() where n >= 3.
0957 
0958 MATCHER(EqualsSumOf, "") { return arg == 0; }
0959 MATCHER_P(EqualsSumOf, a, "") { return arg == a; }
0960 MATCHER_P2(EqualsSumOf, a, b, "") { return arg == a + b; }
0961 MATCHER_P3(EqualsSumOf, a, b, c, "") { return arg == a + b + c; }
0962 MATCHER_P4(EqualsSumOf, a, b, c, d, "") { return arg == a + b + c + d; }
0963 MATCHER_P5(EqualsSumOf, a, b, c, d, e, "") { return arg == a + b + c + d + e; }
0964 MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "") {
0965   return arg == a + b + c + d + e + f;
0966 }
0967 MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "") {
0968   return arg == a + b + c + d + e + f + g;
0969 }
0970 MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "") {
0971   return arg == a + b + c + d + e + f + g + h;
0972 }
0973 MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "") {
0974   return arg == a + b + c + d + e + f + g + h + i;
0975 }
0976 MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "") {
0977   return arg == a + b + c + d + e + f + g + h + i + j;
0978 }
0979 
0980 TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) {
0981   EXPECT_THAT(0, EqualsSumOf());
0982   EXPECT_THAT(1, EqualsSumOf(1));
0983   EXPECT_THAT(12, EqualsSumOf(10, 2));
0984   EXPECT_THAT(123, EqualsSumOf(100, 20, 3));
0985   EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4));
0986   EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5));
0987   EXPECT_THAT("abcdef",
0988               EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f'));
0989   EXPECT_THAT("abcdefg",
0990               EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g'));
0991   EXPECT_THAT("abcdefgh",
0992               EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
0993                           "h"));
0994   EXPECT_THAT("abcdefghi",
0995               EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
0996                           "h", 'i'));
0997   EXPECT_THAT("abcdefghij",
0998               EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
0999                           "h", 'i', ::std::string("j")));
1000 
1001   EXPECT_THAT(1, Not(EqualsSumOf()));
1002   EXPECT_THAT(-1, Not(EqualsSumOf(1)));
1003   EXPECT_THAT(-12, Not(EqualsSumOf(10, 2)));
1004   EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3)));
1005   EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4)));
1006   EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5)));
1007   EXPECT_THAT("abcdef ",
1008               Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f')));
1009   EXPECT_THAT("abcdefg ",
1010               Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f',
1011                               'g')));
1012   EXPECT_THAT("abcdefgh ",
1013               Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1014                               "h")));
1015   EXPECT_THAT("abcdefghi ",
1016               Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1017                               "h", 'i')));
1018   EXPECT_THAT("abcdefghij ",
1019               Not(EqualsSumOf(::std::string("a"), 'b', 'c', "d", "e", 'f', 'g',
1020                               "h", 'i', ::std::string("j"))));
1021 }
1022 
1023 // Tests that a MATCHER_Pn() definition can be instantiated with any
1024 // compatible parameter types.
1025 TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) {
1026   EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3)));
1027   EXPECT_THAT("abcd", EqualsSumOf(::std::string("a"), "b", 'c', "d"));
1028 
1029   EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3))));
1030   EXPECT_THAT("abcde", Not(EqualsSumOf(::std::string("a"), "b", 'c', "d")));
1031 }
1032 
1033 // Tests that the matcher body can promote the parameter types.
1034 
1035 MATCHER_P2(EqConcat, prefix, suffix, "") {
1036   // The following lines promote the two parameters to desired types.
1037   std::string prefix_str(prefix);
1038   char suffix_char = static_cast<char>(suffix);
1039   return arg == prefix_str + suffix_char;
1040 }
1041 
1042 TEST(MatcherPnMacroTest, SimpleTypePromotion) {
1043   Matcher<std::string> no_promo =
1044       EqConcat(std::string("foo"), 't');
1045   Matcher<const std::string&> promo =
1046       EqConcat("foo", static_cast<int>('t'));
1047   EXPECT_FALSE(no_promo.Matches("fool"));
1048   EXPECT_FALSE(promo.Matches("fool"));
1049   EXPECT_TRUE(no_promo.Matches("foot"));
1050   EXPECT_TRUE(promo.Matches("foot"));
1051 }
1052 
1053 // Verifies the type of a MATCHER*.
1054 
1055 TEST(MatcherPnMacroTest, TypesAreCorrect) {
1056   // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable.
1057   EqualsSumOfMatcher a0 = EqualsSumOf();
1058 
1059   // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable.
1060   EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1);
1061 
1062   // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk
1063   // variable, and so on.
1064   EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2');
1065   EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3');
1066   EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4');
1067   EqualsSumOfMatcherP5<int, int, int, int, char> a5 =
1068       EqualsSumOf(1, 2, 3, 4, '5');
1069   EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 =
1070       EqualsSumOf(1, 2, 3, 4, 5, '6');
1071   EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 =
1072       EqualsSumOf(1, 2, 3, 4, 5, 6, '7');
1073   EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 =
1074       EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8');
1075   EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 =
1076       EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9');
1077   EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 =
1078       EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
1079 
1080   // Avoid "unused variable" warnings.
1081   (void)a0;
1082   (void)a1;
1083   (void)a2;
1084   (void)a3;
1085   (void)a4;
1086   (void)a5;
1087   (void)a6;
1088   (void)a7;
1089   (void)a8;
1090   (void)a9;
1091   (void)a10;
1092 }
1093 
1094 // Tests that matcher-typed parameters can be used in Value() inside a
1095 // MATCHER_Pn definition.
1096 
1097 // Succeeds if arg matches exactly 2 of the 3 matchers.
1098 MATCHER_P3(TwoOf, m1, m2, m3, "") {
1099   const int count = static_cast<int>(Value(arg, m1))
1100       + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3));
1101   return count == 2;
1102 }
1103 
1104 TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) {
1105   EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10)));
1106   EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0))));
1107 }
1108 
1109 // Tests Contains().
1110 
1111 TEST(ContainsTest, ListMatchesWhenElementIsInContainer) {
1112   list<int> some_list;
1113   some_list.push_back(3);
1114   some_list.push_back(1);
1115   some_list.push_back(2);
1116   EXPECT_THAT(some_list, Contains(1));
1117   EXPECT_THAT(some_list, Contains(Gt(2.5)));
1118   EXPECT_THAT(some_list, Contains(Eq(2.0f)));
1119 
1120   list<string> another_list;
1121   another_list.push_back("fee");
1122   another_list.push_back("fie");
1123   another_list.push_back("foe");
1124   another_list.push_back("fum");
1125   EXPECT_THAT(another_list, Contains(string("fee")));
1126 }
1127 
1128 TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) {
1129   list<int> some_list;
1130   some_list.push_back(3);
1131   some_list.push_back(1);
1132   EXPECT_THAT(some_list, Not(Contains(4)));
1133 }
1134 
1135 TEST(ContainsTest, SetMatchesWhenElementIsInContainer) {
1136   set<int> some_set;
1137   some_set.insert(3);
1138   some_set.insert(1);
1139   some_set.insert(2);
1140   EXPECT_THAT(some_set, Contains(Eq(1.0)));
1141   EXPECT_THAT(some_set, Contains(Eq(3.0f)));
1142   EXPECT_THAT(some_set, Contains(2));
1143 
1144   set<const char*> another_set;
1145   another_set.insert("fee");
1146   another_set.insert("fie");
1147   another_set.insert("foe");
1148   another_set.insert("fum");
1149   EXPECT_THAT(another_set, Contains(Eq(string("fum"))));
1150 }
1151 
1152 TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) {
1153   set<int> some_set;
1154   some_set.insert(3);
1155   some_set.insert(1);
1156   EXPECT_THAT(some_set, Not(Contains(4)));
1157 
1158   set<const char*> c_string_set;
1159   c_string_set.insert("hello");
1160   EXPECT_THAT(c_string_set, Not(Contains(string("hello").c_str())));
1161 }
1162 
1163 TEST(ContainsTest, ExplainsMatchResultCorrectly) {
1164   const int a[2] = { 1, 2 };
1165   Matcher<const int (&)[2]> m = Contains(2);
1166   EXPECT_EQ("whose element #1 matches", Explain(m, a));
1167 
1168   m = Contains(3);
1169   EXPECT_EQ("", Explain(m, a));
1170 
1171   m = Contains(GreaterThan(0));
1172   EXPECT_EQ("whose element #0 matches, which is 1 more than 0", Explain(m, a));
1173 
1174   m = Contains(GreaterThan(10));
1175   EXPECT_EQ("", Explain(m, a));
1176 }
1177 
1178 TEST(ContainsTest, DescribesItselfCorrectly) {
1179   Matcher<vector<int> > m = Contains(1);
1180   EXPECT_EQ("contains at least one element that is equal to 1", Describe(m));
1181 
1182   Matcher<vector<int> > m2 = Not(m);
1183   EXPECT_EQ("doesn't contain any element that is equal to 1", Describe(m2));
1184 }
1185 
1186 TEST(ContainsTest, MapMatchesWhenElementIsInContainer) {
1187   map<const char*, int> my_map;
1188   const char* bar = "a string";
1189   my_map[bar] = 2;
1190   EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2)));
1191 
1192   map<string, int> another_map;
1193   another_map["fee"] = 1;
1194   another_map["fie"] = 2;
1195   another_map["foe"] = 3;
1196   another_map["fum"] = 4;
1197   EXPECT_THAT(another_map, Contains(pair<const string, int>(string("fee"), 1)));
1198   EXPECT_THAT(another_map, Contains(pair<const string, int>("fie", 2)));
1199 }
1200 
1201 TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) {
1202   map<int, int> some_map;
1203   some_map[1] = 11;
1204   some_map[2] = 22;
1205   EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23))));
1206 }
1207 
1208 TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) {
1209   const char* string_array[] = { "fee", "fie", "foe", "fum" };
1210   EXPECT_THAT(string_array, Contains(Eq(string("fum"))));
1211 }
1212 
1213 TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) {
1214   int int_array[] = { 1, 2, 3, 4 };
1215   EXPECT_THAT(int_array, Not(Contains(5)));
1216 }
1217 
1218 TEST(ContainsTest, AcceptsMatcher) {
1219   const int a[] = { 1, 2, 3 };
1220   EXPECT_THAT(a, Contains(Gt(2)));
1221   EXPECT_THAT(a, Not(Contains(Gt(4))));
1222 }
1223 
1224 TEST(ContainsTest, WorksForNativeArrayAsTuple) {
1225   const int a[] = { 1, 2 };
1226   const int* const pointer = a;
1227   EXPECT_THAT(make_tuple(pointer, 2), Contains(1));
1228   EXPECT_THAT(make_tuple(pointer, 2), Not(Contains(Gt(3))));
1229 }
1230 
1231 TEST(ContainsTest, WorksForTwoDimensionalNativeArray) {
1232   int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
1233   EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6)));
1234   EXPECT_THAT(a, Contains(Contains(5)));
1235   EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5))));
1236   EXPECT_THAT(a, Contains(Not(Contains(5))));
1237 }
1238 
1239 TEST(AllOfTest, HugeMatcher) {
1240   // Verify that using AllOf with many arguments doesn't cause
1241   // the compiler to exceed template instantiation depth limit.
1242   EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _,
1243                                 testing::AllOf(_, _, _, _, _, _, _, _, _, _)));
1244 }
1245 
1246 TEST(AnyOfTest, HugeMatcher) {
1247   // Verify that using AnyOf with many arguments doesn't cause
1248   // the compiler to exceed template instantiation depth limit.
1249   EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _,
1250                                 testing::AnyOf(_, _, _, _, _, _, _, _, _, _)));
1251 }
1252 
1253 namespace adl_test {
1254 
1255 // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf
1256 // don't issue unqualified recursive calls.  If they do, the argument dependent
1257 // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found
1258 // as a candidate and the compilation will break due to an ambiguous overload.
1259 
1260 // The matcher must be in the same namespace as AllOf/AnyOf to make argument
1261 // dependent lookup find those.
1262 MATCHER(M, "") { return true; }
1263 
1264 template <typename T1, typename T2>
1265 bool AllOf(const T1& t1, const T2& t2) { return true; }
1266 
1267 TEST(AllOfTest, DoesNotCallAllOfUnqualified) {
1268   EXPECT_THAT(42, testing::AllOf(
1269       M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1270 }
1271 
1272 template <typename T1, typename T2> bool
1273 AnyOf(const T1& t1, const T2& t2) { return true; }
1274 
1275 TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) {
1276   EXPECT_THAT(42, testing::AnyOf(
1277       M(), M(), M(), M(), M(), M(), M(), M(), M(), M()));
1278 }
1279 
1280 }  // namespace adl_test
1281 
1282 #ifdef _MSC_VER
1283 # pragma warning(pop)
1284 #endif
1285 
1286 }  // namespace