Back to home page

sPhenix code displayed by LXR

 
 

    


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

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 in gmock-more-actions.h.
0035 
0036 #include "gmock/gmock-more-actions.h"
0037 
0038 #include <functional>
0039 #include <sstream>
0040 #include <string>
0041 #include "gmock/gmock.h"
0042 #include "gtest/gtest.h"
0043 #include "gtest/internal/gtest-linked_ptr.h"
0044 
0045 namespace testing {
0046 namespace gmock_more_actions_test {
0047 
0048 using ::std::plus;
0049 using ::std::string;
0050 using testing::get;
0051 using testing::make_tuple;
0052 using testing::tuple;
0053 using testing::tuple_element;
0054 using testing::_;
0055 using testing::Action;
0056 using testing::ActionInterface;
0057 using testing::DeleteArg;
0058 using testing::Invoke;
0059 using testing::Return;
0060 using testing::ReturnArg;
0061 using testing::ReturnPointee;
0062 using testing::SaveArg;
0063 using testing::SaveArgPointee;
0064 using testing::SetArgReferee;
0065 using testing::StaticAssertTypeEq;
0066 using testing::Unused;
0067 using testing::WithArg;
0068 using testing::WithoutArgs;
0069 using testing::internal::linked_ptr;
0070 
0071 // For suppressing compiler warnings on conversion possibly losing precision.
0072 inline short Short(short n) { return n; }  // NOLINT
0073 inline char Char(char ch) { return ch; }
0074 
0075 // Sample functions and functors for testing Invoke() and etc.
0076 int Nullary() { return 1; }
0077 
0078 class NullaryFunctor {
0079  public:
0080   int operator()() { return 2; }
0081 };
0082 
0083 bool g_done = false;
0084 void VoidNullary() { g_done = true; }
0085 
0086 class VoidNullaryFunctor {
0087  public:
0088   void operator()() { g_done = true; }
0089 };
0090 
0091 bool Unary(int x) { return x < 0; }
0092 
0093 const char* Plus1(const char* s) { return s + 1; }
0094 
0095 void VoidUnary(int /* n */) { g_done = true; }
0096 
0097 bool ByConstRef(const string& s) { return s == "Hi"; }
0098 
0099 const double g_double = 0;
0100 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
0101 
0102 string ByNonConstRef(string& s) { return s += "+"; }  // NOLINT
0103 
0104 struct UnaryFunctor {
0105   int operator()(bool x) { return x ? 1 : -1; }
0106 };
0107 
0108 const char* Binary(const char* input, short n) { return input + n; }  // NOLINT
0109 
0110 void VoidBinary(int, char) { g_done = true; }
0111 
0112 int Ternary(int x, char y, short z) { return x + y + z; }  // NOLINT
0113 
0114 void VoidTernary(int, char, bool) { g_done = true; }
0115 
0116 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
0117 
0118 int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
0119 
0120 void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
0121 
0122 string Concat4(const char* s1, const char* s2, const char* s3,
0123                const char* s4) {
0124   return string(s1) + s2 + s3 + s4;
0125 }
0126 
0127 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
0128 
0129 struct SumOf5Functor {
0130   int operator()(int a, int b, int c, int d, int e) {
0131     return a + b + c + d + e;
0132   }
0133 };
0134 
0135 string Concat5(const char* s1, const char* s2, const char* s3,
0136                const char* s4, const char* s5) {
0137   return string(s1) + s2 + s3 + s4 + s5;
0138 }
0139 
0140 int SumOf6(int a, int b, int c, int d, int e, int f) {
0141   return a + b + c + d + e + f;
0142 }
0143 
0144 struct SumOf6Functor {
0145   int operator()(int a, int b, int c, int d, int e, int f) {
0146     return a + b + c + d + e + f;
0147   }
0148 };
0149 
0150 string Concat6(const char* s1, const char* s2, const char* s3,
0151                const char* s4, const char* s5, const char* s6) {
0152   return string(s1) + s2 + s3 + s4 + s5 + s6;
0153 }
0154 
0155 string Concat7(const char* s1, const char* s2, const char* s3,
0156                const char* s4, const char* s5, const char* s6,
0157                const char* s7) {
0158   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
0159 }
0160 
0161 string Concat8(const char* s1, const char* s2, const char* s3,
0162                const char* s4, const char* s5, const char* s6,
0163                const char* s7, const char* s8) {
0164   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
0165 }
0166 
0167 string Concat9(const char* s1, const char* s2, const char* s3,
0168                const char* s4, const char* s5, const char* s6,
0169                const char* s7, const char* s8, const char* s9) {
0170   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
0171 }
0172 
0173 string Concat10(const char* s1, const char* s2, const char* s3,
0174                 const char* s4, const char* s5, const char* s6,
0175                 const char* s7, const char* s8, const char* s9,
0176                 const char* s10) {
0177   return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
0178 }
0179 
0180 class Foo {
0181  public:
0182   Foo() : value_(123) {}
0183 
0184   int Nullary() const { return value_; }
0185 
0186   short Unary(long x) { return static_cast<short>(value_ + x); }  // NOLINT
0187 
0188   string Binary(const string& str, char c) const { return str + c; }
0189 
0190   int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
0191 
0192   int SumOf4(int a, int b, int c, int d) const {
0193     return a + b + c + d + value_;
0194   }
0195 
0196   int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
0197 
0198   int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
0199 
0200   int SumOf6(int a, int b, int c, int d, int e, int f) {
0201     return a + b + c + d + e + f;
0202   }
0203 
0204   string Concat7(const char* s1, const char* s2, const char* s3,
0205                  const char* s4, const char* s5, const char* s6,
0206                  const char* s7) {
0207     return string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
0208   }
0209 
0210   string Concat8(const char* s1, const char* s2, const char* s3,
0211                  const char* s4, const char* s5, const char* s6,
0212                  const char* s7, const char* s8) {
0213     return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
0214   }
0215 
0216   string Concat9(const char* s1, const char* s2, const char* s3,
0217                  const char* s4, const char* s5, const char* s6,
0218                  const char* s7, const char* s8, const char* s9) {
0219     return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
0220   }
0221 
0222   string Concat10(const char* s1, const char* s2, const char* s3,
0223                   const char* s4, const char* s5, const char* s6,
0224                   const char* s7, const char* s8, const char* s9,
0225                   const char* s10) {
0226     return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
0227   }
0228 
0229  private:
0230   int value_;
0231 };
0232 
0233 // Tests using Invoke() with a nullary function.
0234 TEST(InvokeTest, Nullary) {
0235   Action<int()> a = Invoke(Nullary);  // NOLINT
0236   EXPECT_EQ(1, a.Perform(make_tuple()));
0237 }
0238 
0239 // Tests using Invoke() with a unary function.
0240 TEST(InvokeTest, Unary) {
0241   Action<bool(int)> a = Invoke(Unary);  // NOLINT
0242   EXPECT_FALSE(a.Perform(make_tuple(1)));
0243   EXPECT_TRUE(a.Perform(make_tuple(-1)));
0244 }
0245 
0246 // Tests using Invoke() with a binary function.
0247 TEST(InvokeTest, Binary) {
0248   Action<const char*(const char*, short)> a = Invoke(Binary);  // NOLINT
0249   const char* p = "Hello";
0250   EXPECT_EQ(p + 2, a.Perform(make_tuple(p, Short(2))));
0251 }
0252 
0253 // Tests using Invoke() with a ternary function.
0254 TEST(InvokeTest, Ternary) {
0255   Action<int(int, char, short)> a = Invoke(Ternary);  // NOLINT
0256   EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', Short(3))));
0257 }
0258 
0259 // Tests using Invoke() with a 4-argument function.
0260 TEST(InvokeTest, FunctionThatTakes4Arguments) {
0261   Action<int(int, int, int, int)> a = Invoke(SumOf4);  // NOLINT
0262   EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4)));
0263 }
0264 
0265 // Tests using Invoke() with a 5-argument function.
0266 TEST(InvokeTest, FunctionThatTakes5Arguments) {
0267   Action<int(int, int, int, int, int)> a = Invoke(SumOf5);  // NOLINT
0268   EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
0269 }
0270 
0271 // Tests using Invoke() with a 6-argument function.
0272 TEST(InvokeTest, FunctionThatTakes6Arguments) {
0273   Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6);  // NOLINT
0274   EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
0275 }
0276 
0277 // A helper that turns the type of a C-string literal from const
0278 // char[N] to const char*.
0279 inline const char* CharPtr(const char* s) { return s; }
0280 
0281 // Tests using Invoke() with a 7-argument function.
0282 TEST(InvokeTest, FunctionThatTakes7Arguments) {
0283   Action<string(const char*, const char*, const char*, const char*,
0284                 const char*, const char*, const char*)> a =
0285       Invoke(Concat7);
0286   EXPECT_EQ("1234567",
0287             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
0288                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
0289                                  CharPtr("7"))));
0290 }
0291 
0292 // Tests using Invoke() with a 8-argument function.
0293 TEST(InvokeTest, FunctionThatTakes8Arguments) {
0294   Action<string(const char*, const char*, const char*, const char*,
0295                 const char*, const char*, const char*, const char*)> a =
0296       Invoke(Concat8);
0297   EXPECT_EQ("12345678",
0298             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
0299                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
0300                                  CharPtr("7"), CharPtr("8"))));
0301 }
0302 
0303 // Tests using Invoke() with a 9-argument function.
0304 TEST(InvokeTest, FunctionThatTakes9Arguments) {
0305   Action<string(const char*, const char*, const char*, const char*,
0306                 const char*, const char*, const char*, const char*,
0307                 const char*)> a = Invoke(Concat9);
0308   EXPECT_EQ("123456789",
0309             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
0310                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
0311                                  CharPtr("7"), CharPtr("8"), CharPtr("9"))));
0312 }
0313 
0314 // Tests using Invoke() with a 10-argument function.
0315 TEST(InvokeTest, FunctionThatTakes10Arguments) {
0316   Action<string(const char*, const char*, const char*, const char*,
0317                 const char*, const char*, const char*, const char*,
0318                 const char*, const char*)> a = Invoke(Concat10);
0319   EXPECT_EQ("1234567890",
0320             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
0321                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
0322                                  CharPtr("7"), CharPtr("8"), CharPtr("9"),
0323                                  CharPtr("0"))));
0324 }
0325 
0326 // Tests using Invoke() with functions with parameters declared as Unused.
0327 TEST(InvokeTest, FunctionWithUnusedParameters) {
0328   Action<int(int, int, double, const string&)> a1 =
0329       Invoke(SumOfFirst2);
0330   string s("hi");
0331   EXPECT_EQ(12, a1.Perform(
0332     tuple<int, int, double, const string&>(10, 2, 5.6, s)));
0333 
0334   Action<int(int, int, bool, int*)> a2 =
0335       Invoke(SumOfFirst2);
0336   EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL))));
0337 }
0338 
0339 // Tests using Invoke() with methods with parameters declared as Unused.
0340 TEST(InvokeTest, MethodWithUnusedParameters) {
0341   Foo foo;
0342   Action<int(string, bool, int, int)> a1 =
0343       Invoke(&foo, &Foo::SumOfLast2);
0344   EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2)));
0345 
0346   Action<int(char, double, int, int)> a2 =
0347       Invoke(&foo, &Foo::SumOfLast2);
0348   EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3)));
0349 }
0350 
0351 // Tests using Invoke() with a functor.
0352 TEST(InvokeTest, Functor) {
0353   Action<long(long, int)> a = Invoke(plus<long>());  // NOLINT
0354   EXPECT_EQ(3L, a.Perform(make_tuple(1, 2)));
0355 }
0356 
0357 // Tests using Invoke(f) as an action of a compatible type.
0358 TEST(InvokeTest, FunctionWithCompatibleType) {
0359   Action<long(int, short, char, bool)> a = Invoke(SumOf4);  // NOLINT
0360   EXPECT_EQ(4321, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
0361 }
0362 
0363 // Tests using Invoke() with an object pointer and a method pointer.
0364 
0365 // Tests using Invoke() with a nullary method.
0366 TEST(InvokeMethodTest, Nullary) {
0367   Foo foo;
0368   Action<int()> a = Invoke(&foo, &Foo::Nullary);  // NOLINT
0369   EXPECT_EQ(123, a.Perform(make_tuple()));
0370 }
0371 
0372 // Tests using Invoke() with a unary method.
0373 TEST(InvokeMethodTest, Unary) {
0374   Foo foo;
0375   Action<short(long)> a = Invoke(&foo, &Foo::Unary);  // NOLINT
0376   EXPECT_EQ(4123, a.Perform(make_tuple(4000)));
0377 }
0378 
0379 // Tests using Invoke() with a binary method.
0380 TEST(InvokeMethodTest, Binary) {
0381   Foo foo;
0382   Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary);
0383   string s("Hell");
0384   EXPECT_EQ("Hello", a.Perform(
0385       tuple<const string&, char>(s, 'o')));
0386 }
0387 
0388 // Tests using Invoke() with a ternary method.
0389 TEST(InvokeMethodTest, Ternary) {
0390   Foo foo;
0391   Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary);  // NOLINT
0392   EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, Char(1))));
0393 }
0394 
0395 // Tests using Invoke() with a 4-argument method.
0396 TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
0397   Foo foo;
0398   Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4);  // NOLINT
0399   EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4)));
0400 }
0401 
0402 // Tests using Invoke() with a 5-argument method.
0403 TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
0404   Foo foo;
0405   Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5);  // NOLINT
0406   EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
0407 }
0408 
0409 // Tests using Invoke() with a 6-argument method.
0410 TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
0411   Foo foo;
0412   Action<int(int, int, int, int, int, int)> a =  // NOLINT
0413       Invoke(&foo, &Foo::SumOf6);
0414   EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
0415 }
0416 
0417 // Tests using Invoke() with a 7-argument method.
0418 TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
0419   Foo foo;
0420   Action<string(const char*, const char*, const char*, const char*,
0421                 const char*, const char*, const char*)> a =
0422       Invoke(&foo, &Foo::Concat7);
0423   EXPECT_EQ("1234567",
0424             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
0425                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
0426                                  CharPtr("7"))));
0427 }
0428 
0429 // Tests using Invoke() with a 8-argument method.
0430 TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
0431   Foo foo;
0432   Action<string(const char*, const char*, const char*, const char*,
0433                 const char*, const char*, const char*, const char*)> a =
0434       Invoke(&foo, &Foo::Concat8);
0435   EXPECT_EQ("12345678",
0436             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
0437                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
0438                                  CharPtr("7"), CharPtr("8"))));
0439 }
0440 
0441 // Tests using Invoke() with a 9-argument method.
0442 TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
0443   Foo foo;
0444   Action<string(const char*, const char*, const char*, const char*,
0445                 const char*, const char*, const char*, const char*,
0446                 const char*)> a = Invoke(&foo, &Foo::Concat9);
0447   EXPECT_EQ("123456789",
0448             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
0449                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
0450                                  CharPtr("7"), CharPtr("8"), CharPtr("9"))));
0451 }
0452 
0453 // Tests using Invoke() with a 10-argument method.
0454 TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
0455   Foo foo;
0456   Action<string(const char*, const char*, const char*, const char*,
0457                 const char*, const char*, const char*, const char*,
0458                 const char*, const char*)> a = Invoke(&foo, &Foo::Concat10);
0459   EXPECT_EQ("1234567890",
0460             a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
0461                                  CharPtr("4"), CharPtr("5"), CharPtr("6"),
0462                                  CharPtr("7"), CharPtr("8"), CharPtr("9"),
0463                                  CharPtr("0"))));
0464 }
0465 
0466 // Tests using Invoke(f) as an action of a compatible type.
0467 TEST(InvokeMethodTest, MethodWithCompatibleType) {
0468   Foo foo;
0469   Action<long(int, short, char, bool)> a =  // NOLINT
0470       Invoke(&foo, &Foo::SumOf4);
0471   EXPECT_EQ(4444, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
0472 }
0473 
0474 // Tests using WithoutArgs with an action that takes no argument.
0475 TEST(WithoutArgsTest, NoArg) {
0476   Action<int(int n)> a = WithoutArgs(Invoke(Nullary));  // NOLINT
0477   EXPECT_EQ(1, a.Perform(make_tuple(2)));
0478 }
0479 
0480 // Tests using WithArg with an action that takes 1 argument.
0481 TEST(WithArgTest, OneArg) {
0482   Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary));  // NOLINT
0483   EXPECT_TRUE(b.Perform(make_tuple(1.5, -1)));
0484   EXPECT_FALSE(b.Perform(make_tuple(1.5, 1)));
0485 }
0486 
0487 TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
0488   const Action<int(int)> a = ReturnArg<0>();
0489   EXPECT_EQ(5, a.Perform(make_tuple(5)));
0490 }
0491 
0492 TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
0493   const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
0494   EXPECT_TRUE(a.Perform(make_tuple(true, false, false)));
0495 }
0496 
0497 TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
0498   const Action<string(int, int, string, int)> a = ReturnArg<2>();
0499   EXPECT_EQ("seven", a.Perform(make_tuple(5, 6, string("seven"), 8)));
0500 }
0501 
0502 TEST(SaveArgActionTest, WorksForSameType) {
0503   int result = 0;
0504   const Action<void(int n)> a1 = SaveArg<0>(&result);
0505   a1.Perform(make_tuple(5));
0506   EXPECT_EQ(5, result);
0507 }
0508 
0509 TEST(SaveArgActionTest, WorksForCompatibleType) {
0510   int result = 0;
0511   const Action<void(bool, char)> a1 = SaveArg<1>(&result);
0512   a1.Perform(make_tuple(true, 'a'));
0513   EXPECT_EQ('a', result);
0514 }
0515 
0516 TEST(SaveArgPointeeActionTest, WorksForSameType) {
0517   int result = 0;
0518   const int value = 5;
0519   const Action<void(const int*)> a1 = SaveArgPointee<0>(&result);
0520   a1.Perform(make_tuple(&value));
0521   EXPECT_EQ(5, result);
0522 }
0523 
0524 TEST(SaveArgPointeeActionTest, WorksForCompatibleType) {
0525   int result = 0;
0526   char value = 'a';
0527   const Action<void(bool, char*)> a1 = SaveArgPointee<1>(&result);
0528   a1.Perform(make_tuple(true, &value));
0529   EXPECT_EQ('a', result);
0530 }
0531 
0532 TEST(SaveArgPointeeActionTest, WorksForLinkedPtr) {
0533   int result = 0;
0534   linked_ptr<int> value(new int(5));
0535   const Action<void(linked_ptr<int>)> a1 = SaveArgPointee<0>(&result);
0536   a1.Perform(make_tuple(value));
0537   EXPECT_EQ(5, result);
0538 }
0539 
0540 TEST(SetArgRefereeActionTest, WorksForSameType) {
0541   int value = 0;
0542   const Action<void(int&)> a1 = SetArgReferee<0>(1);
0543   a1.Perform(tuple<int&>(value));
0544   EXPECT_EQ(1, value);
0545 }
0546 
0547 TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
0548   int value = 0;
0549   const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
0550   a1.Perform(tuple<int, int&>(0, value));
0551   EXPECT_EQ('a', value);
0552 }
0553 
0554 TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
0555   int value = 0;
0556   const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
0557   a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
0558   EXPECT_EQ('a', value);
0559 }
0560 
0561 // A class that can be used to verify that its destructor is called: it will set
0562 // the bool provided to the constructor to true when destroyed.
0563 class DeletionTester {
0564  public:
0565   explicit DeletionTester(bool* is_deleted)
0566     : is_deleted_(is_deleted) {
0567     // Make sure the bit is set to false.
0568     *is_deleted_ = false;
0569   }
0570 
0571   ~DeletionTester() {
0572     *is_deleted_ = true;
0573   }
0574 
0575  private:
0576   bool* is_deleted_;
0577 };
0578 
0579 TEST(DeleteArgActionTest, OneArg) {
0580   bool is_deleted = false;
0581   DeletionTester* t = new DeletionTester(&is_deleted);
0582   const Action<void(DeletionTester*)> a1 = DeleteArg<0>();      // NOLINT
0583   EXPECT_FALSE(is_deleted);
0584   a1.Perform(make_tuple(t));
0585   EXPECT_TRUE(is_deleted);
0586 }
0587 
0588 TEST(DeleteArgActionTest, TenArgs) {
0589   bool is_deleted = false;
0590   DeletionTester* t = new DeletionTester(&is_deleted);
0591   const Action<void(bool, int, int, const char*, bool,
0592                     int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>();
0593   EXPECT_FALSE(is_deleted);
0594   a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
0595   EXPECT_TRUE(is_deleted);
0596 }
0597 
0598 #if GTEST_HAS_EXCEPTIONS
0599 
0600 TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
0601   const Action<void(int n)> a = Throw('a');
0602   EXPECT_THROW(a.Perform(make_tuple(0)), char);
0603 }
0604 
0605 class MyException {};
0606 
0607 TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
0608   const Action<double(char ch)> a = Throw(MyException());
0609   EXPECT_THROW(a.Perform(make_tuple('0')), MyException);
0610 }
0611 
0612 TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
0613   const Action<double()> a = Throw(MyException());
0614   EXPECT_THROW(a.Perform(make_tuple()), MyException);
0615 }
0616 
0617 #endif  // GTEST_HAS_EXCEPTIONS
0618 
0619 // Tests that SetArrayArgument<N>(first, last) sets the elements of the array
0620 // pointed to by the N-th (0-based) argument to values in range [first, last).
0621 TEST(SetArrayArgumentTest, SetsTheNthArray) {
0622   typedef void MyFunction(bool, int*, char*);
0623   int numbers[] = { 1, 2, 3 };
0624   Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
0625 
0626   int n[4] = {};
0627   int* pn = n;
0628   char ch[4] = {};
0629   char* pch = ch;
0630   a.Perform(make_tuple(true, pn, pch));
0631   EXPECT_EQ(1, n[0]);
0632   EXPECT_EQ(2, n[1]);
0633   EXPECT_EQ(3, n[2]);
0634   EXPECT_EQ(0, n[3]);
0635   EXPECT_EQ('\0', ch[0]);
0636   EXPECT_EQ('\0', ch[1]);
0637   EXPECT_EQ('\0', ch[2]);
0638   EXPECT_EQ('\0', ch[3]);
0639 
0640   // Tests first and last are iterators.
0641   std::string letters = "abc";
0642   a = SetArrayArgument<2>(letters.begin(), letters.end());
0643   std::fill_n(n, 4, 0);
0644   std::fill_n(ch, 4, '\0');
0645   a.Perform(make_tuple(true, pn, pch));
0646   EXPECT_EQ(0, n[0]);
0647   EXPECT_EQ(0, n[1]);
0648   EXPECT_EQ(0, n[2]);
0649   EXPECT_EQ(0, n[3]);
0650   EXPECT_EQ('a', ch[0]);
0651   EXPECT_EQ('b', ch[1]);
0652   EXPECT_EQ('c', ch[2]);
0653   EXPECT_EQ('\0', ch[3]);
0654 }
0655 
0656 // Tests SetArrayArgument<N>(first, last) where first == last.
0657 TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
0658   typedef void MyFunction(bool, int*);
0659   int numbers[] = { 1, 2, 3 };
0660   Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
0661 
0662   int n[4] = {};
0663   int* pn = n;
0664   a.Perform(make_tuple(true, pn));
0665   EXPECT_EQ(0, n[0]);
0666   EXPECT_EQ(0, n[1]);
0667   EXPECT_EQ(0, n[2]);
0668   EXPECT_EQ(0, n[3]);
0669 }
0670 
0671 // Tests SetArrayArgument<N>(first, last) where *first is convertible
0672 // (but not equal) to the argument type.
0673 TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
0674   typedef void MyFunction(bool, int*);
0675   char chars[] = { 97, 98, 99 };
0676   Action<MyFunction> a = SetArrayArgument<1>(chars, chars + 3);
0677 
0678   int codes[4] = { 111, 222, 333, 444 };
0679   int* pcodes = codes;
0680   a.Perform(make_tuple(true, pcodes));
0681   EXPECT_EQ(97, codes[0]);
0682   EXPECT_EQ(98, codes[1]);
0683   EXPECT_EQ(99, codes[2]);
0684   EXPECT_EQ(444, codes[3]);
0685 }
0686 
0687 // Test SetArrayArgument<N>(first, last) with iterator as argument.
0688 TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
0689   typedef void MyFunction(bool, std::back_insert_iterator<std::string>);
0690   std::string letters = "abc";
0691   Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
0692 
0693   std::string s;
0694   a.Perform(make_tuple(true, back_inserter(s)));
0695   EXPECT_EQ(letters, s);
0696 }
0697 
0698 TEST(ReturnPointeeTest, Works) {
0699   int n = 42;
0700   const Action<int()> a = ReturnPointee(&n);
0701   EXPECT_EQ(42, a.Perform(make_tuple()));
0702 
0703   n = 43;
0704   EXPECT_EQ(43, a.Perform(make_tuple()));
0705 }
0706 
0707 }  // namespace gmock_generated_actions_test
0708 }  // namespace testing