Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 // Copyright 2007, 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 // Author: wan@google.com (Zhanyong Wan)
0031 
0032 // Google Mock - a framework for writing C++ mock classes.
0033 //
0034 // This file tests the built-in actions generated by a script.
0035 
0036 #include "gmock/gmock-generated-actions.h"
0037 
0038 #include <functional>
0039 #include <sstream>
0040 #include <string>
0041 #include "gmock/gmock.h"
0042 #include "gtest/gtest.h"
0043 
0044 namespace testing {
0045 namespace gmock_generated_actions_test {
0046 
0047 using ::std::plus;
0048 using ::std::string;
0049 using testing::get;
0050 using testing::make_tuple;
0051 using testing::tuple;
0052 using testing::tuple_element;
0053 using testing::_;
0054 using testing::Action;
0055 using testing::ActionInterface;
0056 using testing::ByRef;
0057 using testing::DoAll;
0058 using testing::Invoke;
0059 using testing::Return;
0060 using testing::ReturnNew;
0061 using testing::SetArgPointee;
0062 using testing::StaticAssertTypeEq;
0063 using testing::Unused;
0064 using testing::WithArgs;
0065 
0066 // For suppressing compiler warnings on conversion possibly losing precision.
0067 inline short Short(short n) { return n; }  // NOLINT
0068 inline char Char(char ch) { return ch; }
0069 
0070 // Sample functions and functors for testing various actions.
0071 int Nullary() { return 1; }
0072 
0073 class NullaryFunctor {
0074  public:
0075   int operator()() { return 2; }
0076 };
0077 
0078 bool g_done = false;
0079 
0080 bool Unary(int x) { return x < 0; }
0081 
0082 const char* Plus1(const char* s) { return s + 1; }
0083 
0084 bool ByConstRef(const string& s) { return s == "Hi"; }
0085 
0086 const double g_double = 0;
0087 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
0088 
0089 string ByNonConstRef(string& s) { return s += "+"; }  // NOLINT
0090 
0091 struct UnaryFunctor {
0092   int operator()(bool x) { return x ? 1 : -1; }
0093 };
0094 
0095 const char* Binary(const char* input, short n) { return input + n; }  // NOLINT
0096 
0097 void VoidBinary(int, char) { g_done = true; }
0098 
0099 int Ternary(int x, char y, short z) { return x + y + z; }  // NOLINT
0100 
0101 void VoidTernary(int, char, bool) { g_done = true; }
0102 
0103 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
0104 
0105 string Concat4(const char* s1, const char* s2, const char* s3,
0106                const char* s4) {
0107   return string(s1) + s2 + s3 + s4;
0108 }
0109 
0110 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
0111 
0112 struct SumOf5Functor {
0113   int operator()(int a, int b, int c, int d, int e) {
0114     return a + b + c + d + e;
0115   }
0116 };
0117 
0118 string Concat5(const char* s1, const char* s2, const char* s3,
0119                const char* s4, const char* s5) {
0120   return string(s1) + s2 + s3 + s4 + s5;
0121 }
0122 
0123 int SumOf6(int a, int b, int c, int d, int e, int f) {
0124   return a + b + c + d + e + f;
0125 }
0126 
0127 struct SumOf6Functor {
0128   int operator()(int a, int b, int c, int d, int e, int f) {
0129     return a + b + c + d + e + f;
0130   }
0131 };
0132 
0133 string Concat6(const char* s1, const char* s2, const char* s3,
0134                const char* s4, const char* s5, const char* s6) {
0135   return string(s1) + s2 + s3 + s4 + s5 + s6;
0136 }
0137 
0138 string Concat7(const char* s1, const char* s2, const char* s3,
0139                const char* s4, const char* s5, const char* s6,
0140                const char* s7) {
0141   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
0142 }
0143 
0144 string Concat8(const char* s1, const char* s2, const char* s3,
0145                const char* s4, const char* s5, const char* s6,
0146                const char* s7, const char* s8) {
0147   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
0148 }
0149 
0150 string Concat9(const char* s1, const char* s2, const char* s3,
0151                const char* s4, const char* s5, const char* s6,
0152                const char* s7, const char* s8, const char* s9) {
0153   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
0154 }
0155 
0156 string Concat10(const char* s1, const char* s2, const char* s3,
0157                 const char* s4, const char* s5, const char* s6,
0158                 const char* s7, const char* s8, const char* s9,
0159                 const char* s10) {
0160   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
0161 }
0162 
0163 // A helper that turns the type of a C-string literal from const
0164 // char[N] to const char*.
0165 inline const char* CharPtr(const char* s) { return s; }
0166 
0167 // Tests InvokeArgument<N>(...).
0168 
0169 // Tests using InvokeArgument with a nullary function.
0170 TEST(InvokeArgumentTest, Function0) {
0171   Action<int(int, int(*)())> a = InvokeArgument<1>();  // NOLINT
0172   EXPECT_EQ(1, a.Perform(make_tuple(2, &Nullary)));
0173 }
0174 
0175 // Tests using InvokeArgument with a unary function.
0176 TEST(InvokeArgumentTest, Functor1) {
0177   Action<int(UnaryFunctor)> a = InvokeArgument<0>(true);  // NOLINT
0178   EXPECT_EQ(1, a.Perform(make_tuple(UnaryFunctor())));
0179 }
0180 
0181 // Tests using InvokeArgument with a 5-ary function.
0182 TEST(InvokeArgumentTest, Function5) {
0183   Action<int(int(*)(int, int, int, int, int))> a =  // NOLINT
0184       InvokeArgument<0>(10000, 2000, 300, 40, 5);
0185   EXPECT_EQ(12345, a.Perform(make_tuple(&SumOf5)));
0186 }
0187 
0188 // Tests using InvokeArgument with a 5-ary functor.
0189 TEST(InvokeArgumentTest, Functor5) {
0190   Action<int(SumOf5Functor)> a =  // NOLINT
0191       InvokeArgument<0>(10000, 2000, 300, 40, 5);
0192   EXPECT_EQ(12345, a.Perform(make_tuple(SumOf5Functor())));
0193 }
0194 
0195 // Tests using InvokeArgument with a 6-ary function.
0196 TEST(InvokeArgumentTest, Function6) {
0197   Action<int(int(*)(int, int, int, int, int, int))> a =  // NOLINT
0198       InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
0199   EXPECT_EQ(123456, a.Perform(make_tuple(&SumOf6)));
0200 }
0201 
0202 // Tests using InvokeArgument with a 6-ary functor.
0203 TEST(InvokeArgumentTest, Functor6) {
0204   Action<int(SumOf6Functor)> a =  // NOLINT
0205       InvokeArgument<0>(100000, 20000, 3000, 400, 50, 6);
0206   EXPECT_EQ(123456, a.Perform(make_tuple(SumOf6Functor())));
0207 }
0208 
0209 // Tests using InvokeArgument with a 7-ary function.
0210 TEST(InvokeArgumentTest, Function7) {
0211   Action<string(string(*)(const char*, const char*, const char*,
0212                           const char*, const char*, const char*,
0213                           const char*))> a =
0214       InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7");
0215   EXPECT_EQ("1234567", a.Perform(make_tuple(&Concat7)));
0216 }
0217 
0218 // Tests using InvokeArgument with a 8-ary function.
0219 TEST(InvokeArgumentTest, Function8) {
0220   Action<string(string(*)(const char*, const char*, const char*,
0221                           const char*, const char*, const char*,
0222                           const char*, const char*))> a =
0223       InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8");
0224   EXPECT_EQ("12345678", a.Perform(make_tuple(&Concat8)));
0225 }
0226 
0227 // Tests using InvokeArgument with a 9-ary function.
0228 TEST(InvokeArgumentTest, Function9) {
0229   Action<string(string(*)(const char*, const char*, const char*,
0230                           const char*, const char*, const char*,
0231                           const char*, const char*, const char*))> a =
0232       InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9");
0233   EXPECT_EQ("123456789", a.Perform(make_tuple(&Concat9)));
0234 }
0235 
0236 // Tests using InvokeArgument with a 10-ary function.
0237 TEST(InvokeArgumentTest, Function10) {
0238   Action<string(string(*)(const char*, const char*, const char*,
0239                           const char*, const char*, const char*,
0240                           const char*, const char*, const char*,
0241                           const char*))> a =
0242       InvokeArgument<0>("1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
0243   EXPECT_EQ("1234567890", a.Perform(make_tuple(&Concat10)));
0244 }
0245 
0246 // Tests using InvokeArgument with a function that takes a pointer argument.
0247 TEST(InvokeArgumentTest, ByPointerFunction) {
0248   Action<const char*(const char*(*)(const char* input, short n))> a =  // NOLINT
0249       InvokeArgument<0>(static_cast<const char*>("Hi"), Short(1));
0250   EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
0251 }
0252 
0253 // Tests using InvokeArgument with a function that takes a const char*
0254 // by passing it a C-string literal.
0255 TEST(InvokeArgumentTest, FunctionWithCStringLiteral) {
0256   Action<const char*(const char*(*)(const char* input, short n))> a =  // NOLINT
0257       InvokeArgument<0>("Hi", Short(1));
0258   EXPECT_STREQ("i", a.Perform(make_tuple(&Binary)));
0259 }
0260 
0261 // Tests using InvokeArgument with a function that takes a const reference.
0262 TEST(InvokeArgumentTest, ByConstReferenceFunction) {
0263   Action<bool(bool(*function)(const string& s))> a =  // NOLINT
0264       InvokeArgument<0>(string("Hi"));
0265   // When action 'a' is constructed, it makes a copy of the temporary
0266   // string object passed to it, so it's OK to use 'a' later, when the
0267   // temporary object has already died.
0268   EXPECT_TRUE(a.Perform(make_tuple(&ByConstRef)));
0269 }
0270 
0271 // Tests using InvokeArgument with ByRef() and a function that takes a
0272 // const reference.
0273 TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) {
0274   Action<bool(bool(*)(const double& x))> a =  // NOLINT
0275       InvokeArgument<0>(ByRef(g_double));
0276   // The above line calls ByRef() on a const value.
0277   EXPECT_TRUE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
0278 
0279   double x = 0;
0280   a = InvokeArgument<0>(ByRef(x));  // This calls ByRef() on a non-const.
0281   EXPECT_FALSE(a.Perform(make_tuple(&ReferencesGlobalDouble)));
0282 }
0283 
0284 // Tests using WithArgs and with an action that takes 1 argument.
0285 TEST(WithArgsTest, OneArg) {
0286   Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary));  // NOLINT
0287   EXPECT_TRUE(a.Perform(make_tuple(1.5, -1)));
0288   EXPECT_FALSE(a.Perform(make_tuple(1.5, 1)));
0289 }
0290 
0291 // Tests using WithArgs with an action that takes 2 arguments.
0292 TEST(WithArgsTest, TwoArgs) {
0293   Action<const char*(const char* s, double x, short n)> a =
0294       WithArgs<0, 2>(Invoke(Binary));
0295   const char s[] = "Hello";
0296   EXPECT_EQ(s + 2, a.Perform(make_tuple(CharPtr(s), 0.5, Short(2))));
0297 }
0298 
0299 // Tests using WithArgs with an action that takes 3 arguments.
0300 TEST(WithArgsTest, ThreeArgs) {
0301   Action<int(int, double, char, short)> a =  // NOLINT
0302       WithArgs<0, 2, 3>(Invoke(Ternary));
0303   EXPECT_EQ(123, a.Perform(make_tuple(100, 6.5, Char(20), Short(3))));
0304 }
0305 
0306 // Tests using WithArgs with an action that takes 4 arguments.
0307 TEST(WithArgsTest, FourArgs) {
0308   Action<string(const char*, const char*, double, const char*, const char*)> a =
0309       WithArgs<4, 3, 1, 0>(Invoke(Concat4));
0310   EXPECT_EQ("4310", a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), 2.5,
0311                                          CharPtr("3"), CharPtr("4"))));
0312 }
0313 
0314 // Tests using WithArgs with an action that takes 5 arguments.
0315 TEST(WithArgsTest, FiveArgs) {
0316   Action<string(const char*, const char*, const char*,
0317                 const char*, const char*)> a =
0318       WithArgs<4, 3, 2, 1, 0>(Invoke(Concat5));
0319   EXPECT_EQ("43210",
0320             a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
0321                                  CharPtr("3"), CharPtr("4"))));
0322 }
0323 
0324 // Tests using WithArgs with an action that takes 6 arguments.
0325 TEST(WithArgsTest, SixArgs) {
0326   Action<string(const char*, const char*, const char*)> a =
0327       WithArgs<0, 1, 2, 2, 1, 0>(Invoke(Concat6));
0328   EXPECT_EQ("012210",
0329             a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"))));
0330 }
0331 
0332 // Tests using WithArgs with an action that takes 7 arguments.
0333 TEST(WithArgsTest, SevenArgs) {
0334   Action<string(const char*, const char*, const char*, const char*)> a =
0335       WithArgs<0, 1, 2, 3, 2, 1, 0>(Invoke(Concat7));
0336   EXPECT_EQ("0123210",
0337             a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
0338                                  CharPtr("3"))));
0339 }
0340 
0341 // Tests using WithArgs with an action that takes 8 arguments.
0342 TEST(WithArgsTest, EightArgs) {
0343   Action<string(const char*, const char*, const char*, const char*)> a =
0344       WithArgs<0, 1, 2, 3, 0, 1, 2, 3>(Invoke(Concat8));
0345   EXPECT_EQ("01230123",
0346             a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
0347                                  CharPtr("3"))));
0348 }
0349 
0350 // Tests using WithArgs with an action that takes 9 arguments.
0351 TEST(WithArgsTest, NineArgs) {
0352   Action<string(const char*, const char*, const char*, const char*)> a =
0353       WithArgs<0, 1, 2, 3, 1, 2, 3, 2, 3>(Invoke(Concat9));
0354   EXPECT_EQ("012312323",
0355             a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
0356                                  CharPtr("3"))));
0357 }
0358 
0359 // Tests using WithArgs with an action that takes 10 arguments.
0360 TEST(WithArgsTest, TenArgs) {
0361   Action<string(const char*, const char*, const char*, const char*)> a =
0362       WithArgs<0, 1, 2, 3, 2, 1, 0, 1, 2, 3>(Invoke(Concat10));
0363   EXPECT_EQ("0123210123",
0364             a.Perform(make_tuple(CharPtr("0"), CharPtr("1"), CharPtr("2"),
0365                                  CharPtr("3"))));
0366 }
0367 
0368 // Tests using WithArgs with an action that is not Invoke().
0369 class SubstractAction : public ActionInterface<int(int, int)> {  // NOLINT
0370  public:
0371   virtual int Perform(const tuple<int, int>& args) {
0372     return get<0>(args) - get<1>(args);
0373   }
0374 };
0375 
0376 TEST(WithArgsTest, NonInvokeAction) {
0377   Action<int(const string&, int, int)> a =  // NOLINT
0378       WithArgs<2, 1>(MakeAction(new SubstractAction));
0379   string s("hello");
0380   EXPECT_EQ(8, a.Perform(tuple<const string&, int, int>(s, 2, 10)));
0381 }
0382 
0383 // Tests using WithArgs to pass all original arguments in the original order.
0384 TEST(WithArgsTest, Identity) {
0385   Action<int(int x, char y, short z)> a =  // NOLINT
0386       WithArgs<0, 1, 2>(Invoke(Ternary));
0387   EXPECT_EQ(123, a.Perform(make_tuple(100, Char(20), Short(3))));
0388 }
0389 
0390 // Tests using WithArgs with repeated arguments.
0391 TEST(WithArgsTest, RepeatedArguments) {
0392   Action<int(bool, int m, int n)> a =  // NOLINT
0393       WithArgs<1, 1, 1, 1>(Invoke(SumOf4));
0394   EXPECT_EQ(4, a.Perform(make_tuple(false, 1, 10)));
0395 }
0396 
0397 // Tests using WithArgs with reversed argument order.
0398 TEST(WithArgsTest, ReversedArgumentOrder) {
0399   Action<const char*(short n, const char* input)> a =  // NOLINT
0400       WithArgs<1, 0>(Invoke(Binary));
0401   const char s[] = "Hello";
0402   EXPECT_EQ(s + 2, a.Perform(make_tuple(Short(2), CharPtr(s))));
0403 }
0404 
0405 // Tests using WithArgs with compatible, but not identical, argument types.
0406 TEST(WithArgsTest, ArgsOfCompatibleTypes) {
0407   Action<long(short x, char y, double z, char c)> a =  // NOLINT
0408       WithArgs<0, 1, 3>(Invoke(Ternary));
0409   EXPECT_EQ(123, a.Perform(make_tuple(Short(100), Char(20), 5.6, Char(3))));
0410 }
0411 
0412 // Tests using WithArgs with an action that returns void.
0413 TEST(WithArgsTest, VoidAction) {
0414   Action<void(double x, char c, int n)> a = WithArgs<2, 1>(Invoke(VoidBinary));
0415   g_done = false;
0416   a.Perform(make_tuple(1.5, 'a', 3));
0417   EXPECT_TRUE(g_done);
0418 }
0419 
0420 // Tests DoAll(a1, a2).
0421 TEST(DoAllTest, TwoActions) {
0422   int n = 0;
0423   Action<int(int*)> a = DoAll(SetArgPointee<0>(1),  // NOLINT
0424                               Return(2));
0425   EXPECT_EQ(2, a.Perform(make_tuple(&n)));
0426   EXPECT_EQ(1, n);
0427 }
0428 
0429 // Tests DoAll(a1, a2, a3).
0430 TEST(DoAllTest, ThreeActions) {
0431   int m = 0, n = 0;
0432   Action<int(int*, int*)> a = DoAll(SetArgPointee<0>(1),  // NOLINT
0433                                     SetArgPointee<1>(2),
0434                                     Return(3));
0435   EXPECT_EQ(3, a.Perform(make_tuple(&m, &n)));
0436   EXPECT_EQ(1, m);
0437   EXPECT_EQ(2, n);
0438 }
0439 
0440 // Tests DoAll(a1, a2, a3, a4).
0441 TEST(DoAllTest, FourActions) {
0442   int m = 0, n = 0;
0443   char ch = '\0';
0444   Action<int(int*, int*, char*)> a =  // NOLINT
0445       DoAll(SetArgPointee<0>(1),
0446             SetArgPointee<1>(2),
0447             SetArgPointee<2>('a'),
0448             Return(3));
0449   EXPECT_EQ(3, a.Perform(make_tuple(&m, &n, &ch)));
0450   EXPECT_EQ(1, m);
0451   EXPECT_EQ(2, n);
0452   EXPECT_EQ('a', ch);
0453 }
0454 
0455 // Tests DoAll(a1, a2, a3, a4, a5).
0456 TEST(DoAllTest, FiveActions) {
0457   int m = 0, n = 0;
0458   char a = '\0', b = '\0';
0459   Action<int(int*, int*, char*, char*)> action =  // NOLINT
0460       DoAll(SetArgPointee<0>(1),
0461             SetArgPointee<1>(2),
0462             SetArgPointee<2>('a'),
0463             SetArgPointee<3>('b'),
0464             Return(3));
0465   EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b)));
0466   EXPECT_EQ(1, m);
0467   EXPECT_EQ(2, n);
0468   EXPECT_EQ('a', a);
0469   EXPECT_EQ('b', b);
0470 }
0471 
0472 // Tests DoAll(a1, a2, ..., a6).
0473 TEST(DoAllTest, SixActions) {
0474   int m = 0, n = 0;
0475   char a = '\0', b = '\0', c = '\0';
0476   Action<int(int*, int*, char*, char*, char*)> action =  // NOLINT
0477       DoAll(SetArgPointee<0>(1),
0478             SetArgPointee<1>(2),
0479             SetArgPointee<2>('a'),
0480             SetArgPointee<3>('b'),
0481             SetArgPointee<4>('c'),
0482             Return(3));
0483   EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c)));
0484   EXPECT_EQ(1, m);
0485   EXPECT_EQ(2, n);
0486   EXPECT_EQ('a', a);
0487   EXPECT_EQ('b', b);
0488   EXPECT_EQ('c', c);
0489 }
0490 
0491 // Tests DoAll(a1, a2, ..., a7).
0492 TEST(DoAllTest, SevenActions) {
0493   int m = 0, n = 0;
0494   char a = '\0', b = '\0', c = '\0', d = '\0';
0495   Action<int(int*, int*, char*, char*, char*, char*)> action =  // NOLINT
0496       DoAll(SetArgPointee<0>(1),
0497             SetArgPointee<1>(2),
0498             SetArgPointee<2>('a'),
0499             SetArgPointee<3>('b'),
0500             SetArgPointee<4>('c'),
0501             SetArgPointee<5>('d'),
0502             Return(3));
0503   EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d)));
0504   EXPECT_EQ(1, m);
0505   EXPECT_EQ(2, n);
0506   EXPECT_EQ('a', a);
0507   EXPECT_EQ('b', b);
0508   EXPECT_EQ('c', c);
0509   EXPECT_EQ('d', d);
0510 }
0511 
0512 // Tests DoAll(a1, a2, ..., a8).
0513 TEST(DoAllTest, EightActions) {
0514   int m = 0, n = 0;
0515   char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0';
0516   Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
0517              char*)> action =
0518       DoAll(SetArgPointee<0>(1),
0519             SetArgPointee<1>(2),
0520             SetArgPointee<2>('a'),
0521             SetArgPointee<3>('b'),
0522             SetArgPointee<4>('c'),
0523             SetArgPointee<5>('d'),
0524             SetArgPointee<6>('e'),
0525             Return(3));
0526   EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e)));
0527   EXPECT_EQ(1, m);
0528   EXPECT_EQ(2, n);
0529   EXPECT_EQ('a', a);
0530   EXPECT_EQ('b', b);
0531   EXPECT_EQ('c', c);
0532   EXPECT_EQ('d', d);
0533   EXPECT_EQ('e', e);
0534 }
0535 
0536 // Tests DoAll(a1, a2, ..., a9).
0537 TEST(DoAllTest, NineActions) {
0538   int m = 0, n = 0;
0539   char a = '\0', b = '\0', c = '\0', d = '\0', e = '\0', f = '\0';
0540   Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
0541              char*, char*)> action =
0542       DoAll(SetArgPointee<0>(1),
0543             SetArgPointee<1>(2),
0544             SetArgPointee<2>('a'),
0545             SetArgPointee<3>('b'),
0546             SetArgPointee<4>('c'),
0547             SetArgPointee<5>('d'),
0548             SetArgPointee<6>('e'),
0549             SetArgPointee<7>('f'),
0550             Return(3));
0551   EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f)));
0552   EXPECT_EQ(1, m);
0553   EXPECT_EQ(2, n);
0554   EXPECT_EQ('a', a);
0555   EXPECT_EQ('b', b);
0556   EXPECT_EQ('c', c);
0557   EXPECT_EQ('d', d);
0558   EXPECT_EQ('e', e);
0559   EXPECT_EQ('f', f);
0560 }
0561 
0562 // Tests DoAll(a1, a2, ..., a10).
0563 TEST(DoAllTest, TenActions) {
0564   int m = 0, n = 0;
0565   char a = '\0', b = '\0', c = '\0', d = '\0';
0566   char e = '\0', f = '\0', g = '\0';
0567   Action<int(int*, int*, char*, char*, char*, char*,  // NOLINT
0568              char*, char*, char*)> action =
0569       DoAll(SetArgPointee<0>(1),
0570             SetArgPointee<1>(2),
0571             SetArgPointee<2>('a'),
0572             SetArgPointee<3>('b'),
0573             SetArgPointee<4>('c'),
0574             SetArgPointee<5>('d'),
0575             SetArgPointee<6>('e'),
0576             SetArgPointee<7>('f'),
0577             SetArgPointee<8>('g'),
0578             Return(3));
0579   EXPECT_EQ(3, action.Perform(make_tuple(&m, &n, &a, &b, &c, &d, &e, &f, &g)));
0580   EXPECT_EQ(1, m);
0581   EXPECT_EQ(2, n);
0582   EXPECT_EQ('a', a);
0583   EXPECT_EQ('b', b);
0584   EXPECT_EQ('c', c);
0585   EXPECT_EQ('d', d);
0586   EXPECT_EQ('e', e);
0587   EXPECT_EQ('f', f);
0588   EXPECT_EQ('g', g);
0589 }
0590 
0591 // The ACTION*() macros trigger warning C4100 (unreferenced formal
0592 // parameter) in MSVC with -W4.  Unfortunately they cannot be fixed in
0593 // the macro definition, as the warnings are generated when the macro
0594 // is expanded and macro expansion cannot contain #pragma.  Therefore
0595 // we suppress them here.
0596 #ifdef _MSC_VER
0597 # pragma warning(push)
0598 # pragma warning(disable:4100)
0599 #endif
0600 
0601 // Tests the ACTION*() macro family.
0602 
0603 // Tests that ACTION() can define an action that doesn't reference the
0604 // mock function arguments.
0605 ACTION(Return5) { return 5; }
0606 
0607 TEST(ActionMacroTest, WorksWhenNotReferencingArguments) {
0608   Action<double()> a1 = Return5();
0609   EXPECT_DOUBLE_EQ(5, a1.Perform(make_tuple()));
0610 
0611   Action<int(double, bool)> a2 = Return5();
0612   EXPECT_EQ(5, a2.Perform(make_tuple(1, true)));
0613 }
0614 
0615 // Tests that ACTION() can define an action that returns void.
0616 ACTION(IncrementArg1) { (*arg1)++; }
0617 
0618 TEST(ActionMacroTest, WorksWhenReturningVoid) {
0619   Action<void(int, int*)> a1 = IncrementArg1();
0620   int n = 0;
0621   a1.Perform(make_tuple(5, &n));
0622   EXPECT_EQ(1, n);
0623 }
0624 
0625 // Tests that the body of ACTION() can reference the type of the
0626 // argument.
0627 ACTION(IncrementArg2) {
0628   StaticAssertTypeEq<int*, arg2_type>();
0629   arg2_type temp = arg2;
0630   (*temp)++;
0631 }
0632 
0633 TEST(ActionMacroTest, CanReferenceArgumentType) {
0634   Action<void(int, bool, int*)> a1 = IncrementArg2();
0635   int n = 0;
0636   a1.Perform(make_tuple(5, false, &n));
0637   EXPECT_EQ(1, n);
0638 }
0639 
0640 // Tests that the body of ACTION() can reference the argument tuple
0641 // via args_type and args.
0642 ACTION(Sum2) {
0643   StaticAssertTypeEq<tuple<int, char, int*>, args_type>();
0644   args_type args_copy = args;
0645   return get<0>(args_copy) + get<1>(args_copy);
0646 }
0647 
0648 TEST(ActionMacroTest, CanReferenceArgumentTuple) {
0649   Action<int(int, char, int*)> a1 = Sum2();
0650   int dummy = 0;
0651   EXPECT_EQ(11, a1.Perform(make_tuple(5, Char(6), &dummy)));
0652 }
0653 
0654 // Tests that the body of ACTION() can reference the mock function
0655 // type.
0656 int Dummy(bool flag) { return flag? 1 : 0; }
0657 
0658 ACTION(InvokeDummy) {
0659   StaticAssertTypeEq<int(bool), function_type>();
0660   function_type* fp = &Dummy;
0661   return (*fp)(true);
0662 }
0663 
0664 TEST(ActionMacroTest, CanReferenceMockFunctionType) {
0665   Action<int(bool)> a1 = InvokeDummy();
0666   EXPECT_EQ(1, a1.Perform(make_tuple(true)));
0667   EXPECT_EQ(1, a1.Perform(make_tuple(false)));
0668 }
0669 
0670 // Tests that the body of ACTION() can reference the mock function's
0671 // return type.
0672 ACTION(InvokeDummy2) {
0673   StaticAssertTypeEq<int, return_type>();
0674   return_type result = Dummy(true);
0675   return result;
0676 }
0677 
0678 TEST(ActionMacroTest, CanReferenceMockFunctionReturnType) {
0679   Action<int(bool)> a1 = InvokeDummy2();
0680   EXPECT_EQ(1, a1.Perform(make_tuple(true)));
0681   EXPECT_EQ(1, a1.Perform(make_tuple(false)));
0682 }
0683 
0684 // Tests that ACTION() works for arguments passed by const reference.
0685 ACTION(ReturnAddrOfConstBoolReferenceArg) {
0686   StaticAssertTypeEq<const bool&, arg1_type>();
0687   return &arg1;
0688 }
0689 
0690 TEST(ActionMacroTest, WorksForConstReferenceArg) {
0691   Action<const bool*(int, const bool&)> a = ReturnAddrOfConstBoolReferenceArg();
0692   const bool b = false;
0693   EXPECT_EQ(&b, a.Perform(tuple<int, const bool&>(0, b)));
0694 }
0695 
0696 // Tests that ACTION() works for arguments passed by non-const reference.
0697 ACTION(ReturnAddrOfIntReferenceArg) {
0698   StaticAssertTypeEq<int&, arg0_type>();
0699   return &arg0;
0700 }
0701 
0702 TEST(ActionMacroTest, WorksForNonConstReferenceArg) {
0703   Action<int*(int&, bool, int)> a = ReturnAddrOfIntReferenceArg();
0704   int n = 0;
0705   EXPECT_EQ(&n, a.Perform(tuple<int&, bool, int>(n, true, 1)));
0706 }
0707 
0708 // Tests that ACTION() can be used in a namespace.
0709 namespace action_test {
0710 ACTION(Sum) { return arg0 + arg1; }
0711 }  // namespace action_test
0712 
0713 TEST(ActionMacroTest, WorksInNamespace) {
0714   Action<int(int, int)> a1 = action_test::Sum();
0715   EXPECT_EQ(3, a1.Perform(make_tuple(1, 2)));
0716 }
0717 
0718 // Tests that the same ACTION definition works for mock functions with
0719 // different argument numbers.
0720 ACTION(PlusTwo) { return arg0 + 2; }
0721 
0722 TEST(ActionMacroTest, WorksForDifferentArgumentNumbers) {
0723   Action<int(int)> a1 = PlusTwo();
0724   EXPECT_EQ(4, a1.Perform(make_tuple(2)));
0725 
0726   Action<double(float, void*)> a2 = PlusTwo();
0727   int dummy;
0728   EXPECT_DOUBLE_EQ(6, a2.Perform(make_tuple(4.0f, &dummy)));
0729 }
0730 
0731 // Tests that ACTION_P can define a parameterized action.
0732 ACTION_P(Plus, n) { return arg0 + n; }
0733 
0734 TEST(ActionPMacroTest, DefinesParameterizedAction) {
0735   Action<int(int m, bool t)> a1 = Plus(9);
0736   EXPECT_EQ(10, a1.Perform(make_tuple(1, true)));
0737 }
0738 
0739 // Tests that the body of ACTION_P can reference the argument types
0740 // and the parameter type.
0741 ACTION_P(TypedPlus, n) {
0742   arg0_type t1 = arg0;
0743   n_type t2 = n;
0744   return t1 + t2;
0745 }
0746 
0747 TEST(ActionPMacroTest, CanReferenceArgumentAndParameterTypes) {
0748   Action<int(char m, bool t)> a1 = TypedPlus(9);
0749   EXPECT_EQ(10, a1.Perform(make_tuple(Char(1), true)));
0750 }
0751 
0752 // Tests that a parameterized action can be used in any mock function
0753 // whose type is compatible.
0754 TEST(ActionPMacroTest, WorksInCompatibleMockFunction) {
0755   Action<std::string(const std::string& s)> a1 = Plus("tail");
0756   const std::string re = "re";
0757   EXPECT_EQ("retail", a1.Perform(tuple<const std::string&>(re)));
0758 }
0759 
0760 // Tests that we can use ACTION*() to define actions overloaded on the
0761 // number of parameters.
0762 
0763 ACTION(OverloadedAction) { return arg0 ? arg1 : "hello"; }
0764 
0765 ACTION_P(OverloadedAction, default_value) {
0766   return arg0 ? arg1 : default_value;
0767 }
0768 
0769 ACTION_P2(OverloadedAction, true_value, false_value) {
0770   return arg0 ? true_value : false_value;
0771 }
0772 
0773 TEST(ActionMacroTest, CanDefineOverloadedActions) {
0774   typedef Action<const char*(bool, const char*)> MyAction;
0775 
0776   const MyAction a1 = OverloadedAction();
0777   EXPECT_STREQ("hello", a1.Perform(make_tuple(false, CharPtr("world"))));
0778   EXPECT_STREQ("world", a1.Perform(make_tuple(true, CharPtr("world"))));
0779 
0780   const MyAction a2 = OverloadedAction("hi");
0781   EXPECT_STREQ("hi", a2.Perform(make_tuple(false, CharPtr("world"))));
0782   EXPECT_STREQ("world", a2.Perform(make_tuple(true, CharPtr("world"))));
0783 
0784   const MyAction a3 = OverloadedAction("hi", "you");
0785   EXPECT_STREQ("hi", a3.Perform(make_tuple(true, CharPtr("world"))));
0786   EXPECT_STREQ("you", a3.Perform(make_tuple(false, CharPtr("world"))));
0787 }
0788 
0789 // Tests ACTION_Pn where n >= 3.
0790 
0791 ACTION_P3(Plus, m, n, k) { return arg0 + m + n + k; }
0792 
0793 TEST(ActionPnMacroTest, WorksFor3Parameters) {
0794   Action<double(int m, bool t)> a1 = Plus(100, 20, 3.4);
0795   EXPECT_DOUBLE_EQ(3123.4, a1.Perform(make_tuple(3000, true)));
0796 
0797   Action<std::string(const std::string& s)> a2 = Plus("tail", "-", ">");
0798   const std::string re = "re";
0799   EXPECT_EQ("retail->", a2.Perform(tuple<const std::string&>(re)));
0800 }
0801 
0802 ACTION_P4(Plus, p0, p1, p2, p3) { return arg0 + p0 + p1 + p2 + p3; }
0803 
0804 TEST(ActionPnMacroTest, WorksFor4Parameters) {
0805   Action<int(int)> a1 = Plus(1, 2, 3, 4);
0806   EXPECT_EQ(10 + 1 + 2 + 3 + 4, a1.Perform(make_tuple(10)));
0807 }
0808 
0809 ACTION_P5(Plus, p0, p1, p2, p3, p4) { return arg0 + p0 + p1 + p2 + p3 + p4; }
0810 
0811 TEST(ActionPnMacroTest, WorksFor5Parameters) {
0812   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5);
0813   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5, a1.Perform(make_tuple(10)));
0814 }
0815 
0816 ACTION_P6(Plus, p0, p1, p2, p3, p4, p5) {
0817   return arg0 + p0 + p1 + p2 + p3 + p4 + p5;
0818 }
0819 
0820 TEST(ActionPnMacroTest, WorksFor6Parameters) {
0821   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6);
0822   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6, a1.Perform(make_tuple(10)));
0823 }
0824 
0825 ACTION_P7(Plus, p0, p1, p2, p3, p4, p5, p6) {
0826   return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6;
0827 }
0828 
0829 TEST(ActionPnMacroTest, WorksFor7Parameters) {
0830   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7);
0831   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7, a1.Perform(make_tuple(10)));
0832 }
0833 
0834 ACTION_P8(Plus, p0, p1, p2, p3, p4, p5, p6, p7) {
0835   return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
0836 }
0837 
0838 TEST(ActionPnMacroTest, WorksFor8Parameters) {
0839   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8);
0840   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8, a1.Perform(make_tuple(10)));
0841 }
0842 
0843 ACTION_P9(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8) {
0844   return arg0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8;
0845 }
0846 
0847 TEST(ActionPnMacroTest, WorksFor9Parameters) {
0848   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9);
0849   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9, a1.Perform(make_tuple(10)));
0850 }
0851 
0852 ACTION_P10(Plus, p0, p1, p2, p3, p4, p5, p6, p7, p8, last_param) {
0853   arg0_type t0 = arg0;
0854   last_param_type t9 = last_param;
0855   return t0 + p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + t9;
0856 }
0857 
0858 TEST(ActionPnMacroTest, WorksFor10Parameters) {
0859   Action<int(int)> a1 = Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
0860   EXPECT_EQ(10 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
0861             a1.Perform(make_tuple(10)));
0862 }
0863 
0864 // Tests that the action body can promote the parameter types.
0865 
0866 ACTION_P2(PadArgument, prefix, suffix) {
0867   // The following lines promote the two parameters to desired types.
0868   std::string prefix_str(prefix);
0869   char suffix_char = static_cast<char>(suffix);
0870   return prefix_str + arg0 + suffix_char;
0871 }
0872 
0873 TEST(ActionPnMacroTest, SimpleTypePromotion) {
0874   Action<std::string(const char*)> no_promo =
0875       PadArgument(std::string("foo"), 'r');
0876   Action<std::string(const char*)> promo =
0877       PadArgument("foo", static_cast<int>('r'));
0878   EXPECT_EQ("foobar", no_promo.Perform(make_tuple(CharPtr("ba"))));
0879   EXPECT_EQ("foobar", promo.Perform(make_tuple(CharPtr("ba"))));
0880 }
0881 
0882 // Tests that we can partially restrict parameter types using a
0883 // straight-forward pattern.
0884 
0885 // Defines a generic action that doesn't restrict the types of its
0886 // parameters.
0887 ACTION_P3(ConcatImpl, a, b, c) {
0888   std::stringstream ss;
0889   ss << a << b << c;
0890   return ss.str();
0891 }
0892 
0893 // Next, we try to restrict that either the first parameter is a
0894 // string, or the second parameter is an int.
0895 
0896 // Defines a partially specialized wrapper that restricts the first
0897 // parameter to std::string.
0898 template <typename T1, typename T2>
0899 // ConcatImplActionP3 is the class template ACTION_P3 uses to
0900 // implement ConcatImpl.  We shouldn't change the name as this
0901 // pattern requires the user to use it directly.
0902 ConcatImplActionP3<std::string, T1, T2>
0903 Concat(const std::string& a, T1 b, T2 c) {
0904   GTEST_INTENTIONAL_CONST_COND_PUSH_()
0905   if (true) {
0906   GTEST_INTENTIONAL_CONST_COND_POP_()
0907     // This branch verifies that ConcatImpl() can be invoked without
0908     // explicit template arguments.
0909     return ConcatImpl(a, b, c);
0910   } else {
0911     // This branch verifies that ConcatImpl() can also be invoked with
0912     // explicit template arguments.  It doesn't really need to be
0913     // executed as this is a compile-time verification.
0914     return ConcatImpl<std::string, T1, T2>(a, b, c);
0915   }
0916 }
0917 
0918 // Defines another partially specialized wrapper that restricts the
0919 // second parameter to int.
0920 template <typename T1, typename T2>
0921 ConcatImplActionP3<T1, int, T2>
0922 Concat(T1 a, int b, T2 c) {
0923   return ConcatImpl(a, b, c);
0924 }
0925 
0926 TEST(ActionPnMacroTest, CanPartiallyRestrictParameterTypes) {
0927   Action<const std::string()> a1 = Concat("Hello", "1", 2);
0928   EXPECT_EQ("Hello12", a1.Perform(make_tuple()));
0929 
0930   a1 = Concat(1, 2, 3);
0931   EXPECT_EQ("123", a1.Perform(make_tuple()));
0932 }
0933 
0934 // Verifies the type of an ACTION*.
0935 
0936 ACTION(DoFoo) {}
0937 ACTION_P(DoFoo, p) {}
0938 ACTION_P2(DoFoo, p0, p1) {}
0939 
0940 TEST(ActionPnMacroTest, TypesAreCorrect) {
0941   // DoFoo() must be assignable to a DoFooAction variable.
0942   DoFooAction a0 = DoFoo();
0943 
0944   // DoFoo(1) must be assignable to a DoFooActionP variable.
0945   DoFooActionP<int> a1 = DoFoo(1);
0946 
0947   // DoFoo(p1, ..., pk) must be assignable to a DoFooActionPk
0948   // variable, and so on.
0949   DoFooActionP2<int, char> a2 = DoFoo(1, '2');
0950   PlusActionP3<int, int, char> a3 = Plus(1, 2, '3');
0951   PlusActionP4<int, int, int, char> a4 = Plus(1, 2, 3, '4');
0952   PlusActionP5<int, int, int, int, char> a5 = Plus(1, 2, 3, 4, '5');
0953   PlusActionP6<int, int, int, int, int, char> a6 = Plus(1, 2, 3, 4, 5, '6');
0954   PlusActionP7<int, int, int, int, int, int, char> a7 =
0955       Plus(1, 2, 3, 4, 5, 6, '7');
0956   PlusActionP8<int, int, int, int, int, int, int, char> a8 =
0957       Plus(1, 2, 3, 4, 5, 6, 7, '8');
0958   PlusActionP9<int, int, int, int, int, int, int, int, char> a9 =
0959       Plus(1, 2, 3, 4, 5, 6, 7, 8, '9');
0960   PlusActionP10<int, int, int, int, int, int, int, int, int, char> a10 =
0961       Plus(1, 2, 3, 4, 5, 6, 7, 8, 9, '0');
0962 
0963   // Avoid "unused variable" warnings.
0964   (void)a0;
0965   (void)a1;
0966   (void)a2;
0967   (void)a3;
0968   (void)a4;
0969   (void)a5;
0970   (void)a6;
0971   (void)a7;
0972   (void)a8;
0973   (void)a9;
0974   (void)a10;
0975 }
0976 
0977 // Tests that an ACTION_P*() action can be explicitly instantiated
0978 // with reference-typed parameters.
0979 
0980 ACTION_P(Plus1, x) { return x; }
0981 ACTION_P2(Plus2, x, y) { return x + y; }
0982 ACTION_P3(Plus3, x, y, z) { return x + y + z; }
0983 ACTION_P10(Plus10, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {
0984   return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9;
0985 }
0986 
0987 TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) {
0988   int x = 1, y = 2, z = 3;
0989   const tuple<> empty = make_tuple();
0990 
0991   Action<int()> a = Plus1<int&>(x);
0992   EXPECT_EQ(1, a.Perform(empty));
0993 
0994   a = Plus2<const int&, int&>(x, y);
0995   EXPECT_EQ(3, a.Perform(empty));
0996 
0997   a = Plus3<int&, const int&, int&>(x, y, z);
0998   EXPECT_EQ(6, a.Perform(empty));
0999 
1000   int n[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
1001   a = Plus10<const int&, int&, const int&, int&, const int&, int&, const int&,
1002       int&, const int&, int&>(n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7],
1003                               n[8], n[9]);
1004   EXPECT_EQ(55, a.Perform(empty));
1005 }
1006 
1007 class NullaryConstructorClass {
1008  public:
1009   NullaryConstructorClass() : value_(123) {}
1010   int value_;
1011 };
1012 
1013 // Tests using ReturnNew() with a nullary constructor.
1014 TEST(ReturnNewTest, NoArgs) {
1015   Action<NullaryConstructorClass*()> a = ReturnNew<NullaryConstructorClass>();
1016   NullaryConstructorClass* c = a.Perform(make_tuple());
1017   EXPECT_EQ(123, c->value_);
1018   delete c;
1019 }
1020 
1021 class UnaryConstructorClass {
1022  public:
1023   explicit UnaryConstructorClass(int value) : value_(value) {}
1024   int value_;
1025 };
1026 
1027 // Tests using ReturnNew() with a unary constructor.
1028 TEST(ReturnNewTest, Unary) {
1029   Action<UnaryConstructorClass*()> a = ReturnNew<UnaryConstructorClass>(4000);
1030   UnaryConstructorClass* c = a.Perform(make_tuple());
1031   EXPECT_EQ(4000, c->value_);
1032   delete c;
1033 }
1034 
1035 TEST(ReturnNewTest, UnaryWorksWhenMockMethodHasArgs) {
1036   Action<UnaryConstructorClass*(bool, int)> a =
1037       ReturnNew<UnaryConstructorClass>(4000);
1038   UnaryConstructorClass* c = a.Perform(make_tuple(false, 5));
1039   EXPECT_EQ(4000, c->value_);
1040   delete c;
1041 }
1042 
1043 TEST(ReturnNewTest, UnaryWorksWhenMockMethodReturnsPointerToConst) {
1044   Action<const UnaryConstructorClass*()> a =
1045       ReturnNew<UnaryConstructorClass>(4000);
1046   const UnaryConstructorClass* c = a.Perform(make_tuple());
1047   EXPECT_EQ(4000, c->value_);
1048   delete c;
1049 }
1050 
1051 class TenArgConstructorClass {
1052  public:
1053   TenArgConstructorClass(int a1, int a2, int a3, int a4, int a5,
1054                          int a6, int a7, int a8, int a9, int a10)
1055     : value_(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10) {
1056   }
1057   int value_;
1058 };
1059 
1060 // Tests using ReturnNew() with a 10-argument constructor.
1061 TEST(ReturnNewTest, ConstructorThatTakes10Arguments) {
1062   Action<TenArgConstructorClass*()> a =
1063       ReturnNew<TenArgConstructorClass>(1000000000, 200000000, 30000000,
1064                                         4000000, 500000, 60000,
1065                                         7000, 800, 90, 0);
1066   TenArgConstructorClass* c = a.Perform(make_tuple());
1067   EXPECT_EQ(1234567890, c->value_);
1068   delete c;
1069 }
1070 
1071 // Tests that ACTION_TEMPLATE works when there is no value parameter.
1072 ACTION_TEMPLATE(CreateNew,
1073                 HAS_1_TEMPLATE_PARAMS(typename, T),
1074                 AND_0_VALUE_PARAMS()) {
1075   return new T;
1076 }
1077 
1078 TEST(ActionTemplateTest, WorksWithoutValueParam) {
1079   const Action<int*()> a = CreateNew<int>();
1080   int* p = a.Perform(make_tuple());
1081   delete p;
1082 }
1083 
1084 // Tests that ACTION_TEMPLATE works when there are value parameters.
1085 ACTION_TEMPLATE(CreateNew,
1086                 HAS_1_TEMPLATE_PARAMS(typename, T),
1087                 AND_1_VALUE_PARAMS(a0)) {
1088   return new T(a0);
1089 }
1090 
1091 TEST(ActionTemplateTest, WorksWithValueParams) {
1092   const Action<int*()> a = CreateNew<int>(42);
1093   int* p = a.Perform(make_tuple());
1094   EXPECT_EQ(42, *p);
1095   delete p;
1096 }
1097 
1098 // Tests that ACTION_TEMPLATE works for integral template parameters.
1099 ACTION_TEMPLATE(MyDeleteArg,
1100                 HAS_1_TEMPLATE_PARAMS(int, k),
1101                 AND_0_VALUE_PARAMS()) {
1102   delete get<k>(args);
1103 }
1104 
1105 // Resets a bool variable in the destructor.
1106 class BoolResetter {
1107  public:
1108   explicit BoolResetter(bool* value) : value_(value) {}
1109   ~BoolResetter() { *value_ = false; }
1110  private:
1111   bool* value_;
1112 };
1113 
1114 TEST(ActionTemplateTest, WorksForIntegralTemplateParams) {
1115   const Action<void(int*, BoolResetter*)> a = MyDeleteArg<1>();
1116   int n = 0;
1117   bool b = true;
1118   BoolResetter* resetter = new BoolResetter(&b);
1119   a.Perform(make_tuple(&n, resetter));
1120   EXPECT_FALSE(b);  // Verifies that resetter is deleted.
1121 }
1122 
1123 // Tests that ACTION_TEMPLATES works for template template parameters.
1124 ACTION_TEMPLATE(ReturnSmartPointer,
1125                 HAS_1_TEMPLATE_PARAMS(template <typename Pointee> class,
1126                                       Pointer),
1127                 AND_1_VALUE_PARAMS(pointee)) {
1128   return Pointer<pointee_type>(new pointee_type(pointee));
1129 }
1130 
1131 TEST(ActionTemplateTest, WorksForTemplateTemplateParameters) {
1132   using ::testing::internal::linked_ptr;
1133   const Action<linked_ptr<int>()> a = ReturnSmartPointer<linked_ptr>(42);
1134   linked_ptr<int> p = a.Perform(make_tuple());
1135   EXPECT_EQ(42, *p);
1136 }
1137 
1138 // Tests that ACTION_TEMPLATE works for 10 template parameters.
1139 template <typename T1, typename T2, typename T3, int k4, bool k5,
1140           unsigned int k6, typename T7, typename T8, typename T9>
1141 struct GiantTemplate {
1142  public:
1143   explicit GiantTemplate(int a_value) : value(a_value) {}
1144   int value;
1145 };
1146 
1147 ACTION_TEMPLATE(ReturnGiant,
1148                 HAS_10_TEMPLATE_PARAMS(
1149                     typename, T1,
1150                     typename, T2,
1151                     typename, T3,
1152                     int, k4,
1153                     bool, k5,
1154                     unsigned int, k6,
1155                     class, T7,
1156                     class, T8,
1157                     class, T9,
1158                     template <typename T> class, T10),
1159                 AND_1_VALUE_PARAMS(value)) {
1160   return GiantTemplate<T10<T1>, T2, T3, k4, k5, k6, T7, T8, T9>(value);
1161 }
1162 
1163 TEST(ActionTemplateTest, WorksFor10TemplateParameters) {
1164   using ::testing::internal::linked_ptr;
1165   typedef GiantTemplate<linked_ptr<int>, bool, double, 5,
1166       true, 6, char, unsigned, int> Giant;
1167   const Action<Giant()> a = ReturnGiant<
1168       int, bool, double, 5, true, 6, char, unsigned, int, linked_ptr>(42);
1169   Giant giant = a.Perform(make_tuple());
1170   EXPECT_EQ(42, giant.value);
1171 }
1172 
1173 // Tests that ACTION_TEMPLATE works for 10 value parameters.
1174 ACTION_TEMPLATE(ReturnSum,
1175                 HAS_1_TEMPLATE_PARAMS(typename, Number),
1176                 AND_10_VALUE_PARAMS(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10)) {
1177   return static_cast<Number>(v1) + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10;
1178 }
1179 
1180 TEST(ActionTemplateTest, WorksFor10ValueParameters) {
1181   const Action<int()> a = ReturnSum<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
1182   EXPECT_EQ(55, a.Perform(make_tuple()));
1183 }
1184 
1185 // Tests that ACTION_TEMPLATE and ACTION/ACTION_P* can be overloaded
1186 // on the number of value parameters.
1187 
1188 ACTION(ReturnSum) { return 0; }
1189 
1190 ACTION_P(ReturnSum, x) { return x; }
1191 
1192 ACTION_TEMPLATE(ReturnSum,
1193                 HAS_1_TEMPLATE_PARAMS(typename, Number),
1194                 AND_2_VALUE_PARAMS(v1, v2)) {
1195   return static_cast<Number>(v1) + v2;
1196 }
1197 
1198 ACTION_TEMPLATE(ReturnSum,
1199                 HAS_1_TEMPLATE_PARAMS(typename, Number),
1200                 AND_3_VALUE_PARAMS(v1, v2, v3)) {
1201   return static_cast<Number>(v1) + v2 + v3;
1202 }
1203 
1204 ACTION_TEMPLATE(ReturnSum,
1205                 HAS_2_TEMPLATE_PARAMS(typename, Number, int, k),
1206                 AND_4_VALUE_PARAMS(v1, v2, v3, v4)) {
1207   return static_cast<Number>(v1) + v2 + v3 + v4 + k;
1208 }
1209 
1210 TEST(ActionTemplateTest, CanBeOverloadedOnNumberOfValueParameters) {
1211   const Action<int()> a0 = ReturnSum();
1212   const Action<int()> a1 = ReturnSum(1);
1213   const Action<int()> a2 = ReturnSum<int>(1, 2);
1214   const Action<int()> a3 = ReturnSum<int>(1, 2, 3);
1215   const Action<int()> a4 = ReturnSum<int, 10000>(2000, 300, 40, 5);
1216   EXPECT_EQ(0, a0.Perform(make_tuple()));
1217   EXPECT_EQ(1, a1.Perform(make_tuple()));
1218   EXPECT_EQ(3, a2.Perform(make_tuple()));
1219   EXPECT_EQ(6, a3.Perform(make_tuple()));
1220   EXPECT_EQ(12345, a4.Perform(make_tuple()));
1221 }
1222 
1223 #ifdef _MSC_VER
1224 # pragma warning(pop)
1225 #endif
1226 
1227 }  // namespace gmock_generated_actions_test
1228 }  // namespace testing