File indexing completed on 2025-08-06 08:19:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
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
0072 inline short Short(short n) { return n; }
0073 inline char Char(char ch) { return ch; }
0074
0075
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 ) { 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 += "+"; }
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; }
0109
0110 void VoidBinary(int, char) { g_done = true; }
0111
0112 int Ternary(int x, char y, short z) { return x + y + z; }
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); }
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
0234 TEST(InvokeTest, Nullary) {
0235 Action<int()> a = Invoke(Nullary);
0236 EXPECT_EQ(1, a.Perform(make_tuple()));
0237 }
0238
0239
0240 TEST(InvokeTest, Unary) {
0241 Action<bool(int)> a = Invoke(Unary);
0242 EXPECT_FALSE(a.Perform(make_tuple(1)));
0243 EXPECT_TRUE(a.Perform(make_tuple(-1)));
0244 }
0245
0246
0247 TEST(InvokeTest, Binary) {
0248 Action<const char*(const char*, short)> a = Invoke(Binary);
0249 const char* p = "Hello";
0250 EXPECT_EQ(p + 2, a.Perform(make_tuple(p, Short(2))));
0251 }
0252
0253
0254 TEST(InvokeTest, Ternary) {
0255 Action<int(int, char, short)> a = Invoke(Ternary);
0256 EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', Short(3))));
0257 }
0258
0259
0260 TEST(InvokeTest, FunctionThatTakes4Arguments) {
0261 Action<int(int, int, int, int)> a = Invoke(SumOf4);
0262 EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4)));
0263 }
0264
0265
0266 TEST(InvokeTest, FunctionThatTakes5Arguments) {
0267 Action<int(int, int, int, int, int)> a = Invoke(SumOf5);
0268 EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
0269 }
0270
0271
0272 TEST(InvokeTest, FunctionThatTakes6Arguments) {
0273 Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6);
0274 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
0275 }
0276
0277
0278
0279 inline const char* CharPtr(const char* s) { return s; }
0280
0281
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
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
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
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
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
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
0352 TEST(InvokeTest, Functor) {
0353 Action<long(long, int)> a = Invoke(plus<long>());
0354 EXPECT_EQ(3L, a.Perform(make_tuple(1, 2)));
0355 }
0356
0357
0358 TEST(InvokeTest, FunctionWithCompatibleType) {
0359 Action<long(int, short, char, bool)> a = Invoke(SumOf4);
0360 EXPECT_EQ(4321, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
0361 }
0362
0363
0364
0365
0366 TEST(InvokeMethodTest, Nullary) {
0367 Foo foo;
0368 Action<int()> a = Invoke(&foo, &Foo::Nullary);
0369 EXPECT_EQ(123, a.Perform(make_tuple()));
0370 }
0371
0372
0373 TEST(InvokeMethodTest, Unary) {
0374 Foo foo;
0375 Action<short(long)> a = Invoke(&foo, &Foo::Unary);
0376 EXPECT_EQ(4123, a.Perform(make_tuple(4000)));
0377 }
0378
0379
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
0389 TEST(InvokeMethodTest, Ternary) {
0390 Foo foo;
0391 Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary);
0392 EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, Char(1))));
0393 }
0394
0395
0396 TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
0397 Foo foo;
0398 Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4);
0399 EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4)));
0400 }
0401
0402
0403 TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
0404 Foo foo;
0405 Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5);
0406 EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
0407 }
0408
0409
0410 TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
0411 Foo foo;
0412 Action<int(int, int, int, int, int, int)> a =
0413 Invoke(&foo, &Foo::SumOf6);
0414 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
0415 }
0416
0417
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
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
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
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
0467 TEST(InvokeMethodTest, MethodWithCompatibleType) {
0468 Foo foo;
0469 Action<long(int, short, char, bool)> a =
0470 Invoke(&foo, &Foo::SumOf4);
0471 EXPECT_EQ(4444, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
0472 }
0473
0474
0475 TEST(WithoutArgsTest, NoArg) {
0476 Action<int(int n)> a = WithoutArgs(Invoke(Nullary));
0477 EXPECT_EQ(1, a.Perform(make_tuple(2)));
0478 }
0479
0480
0481 TEST(WithArgTest, OneArg) {
0482 Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary));
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
0562
0563 class DeletionTester {
0564 public:
0565 explicit DeletionTester(bool* is_deleted)
0566 : is_deleted_(is_deleted) {
0567
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>();
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
0618
0619
0620
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
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
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
0672
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
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 }
0708 }