1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Google Mock - a framework for writing C++ mock classes.
33 // This file tests the built-in actions in gmock-more-actions.h.
35 #include "gmock/gmock-more-actions.h"
40 #include "gmock/gmock.h"
41 #include "gtest/gtest.h"
42 #include "gtest/internal/gtest-linked_ptr.h"
45 namespace gmock_more_actions_test {
50 using testing::make_tuple;
52 using testing::tuple_element;
54 using testing::Action;
55 using testing::ActionInterface;
56 using testing::DeleteArg;
57 using testing::Invoke;
58 using testing::Return;
59 using testing::ReturnArg;
60 using testing::ReturnPointee;
61 using testing::SaveArg;
62 using testing::SaveArgPointee;
63 using testing::SetArgReferee;
64 using testing::StaticAssertTypeEq;
65 using testing::Unused;
66 using testing::WithArg;
67 using testing::WithoutArgs;
68 using testing::internal::linked_ptr;
70 // For suppressing compiler warnings on conversion possibly losing precision.
71 inline short Short(short n) { return n; } // NOLINT
72 inline char Char(char ch) { return ch; }
74 // Sample functions and functors for testing Invoke() and etc.
75 int Nullary() { return 1; }
77 class NullaryFunctor {
79 int operator()() { return 2; }
83 void VoidNullary() { g_done = true; }
85 class VoidNullaryFunctor {
87 void operator()() { g_done = true; }
90 bool Unary(int x) { return x < 0; }
92 const char* Plus1(const char* s) { return s + 1; }
94 void VoidUnary(int /* n */) { g_done = true; }
96 bool ByConstRef(const std::string& s) { return s == "Hi"; }
98 const double g_double = 0;
99 bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; }
101 std::string ByNonConstRef(std::string& s) { return s += "+"; } // NOLINT
103 struct UnaryFunctor {
104 int operator()(bool x) { return x ? 1 : -1; }
107 const char* Binary(const char* input, short n) { return input + n; } // NOLINT
109 void VoidBinary(int, char) { g_done = true; }
111 int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT
113 void VoidTernary(int, char, bool) { g_done = true; }
115 int SumOf4(int a, int b, int c, int d) { return a + b + c + d; }
117 int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; }
119 void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; }
121 std::string Concat4(const char* s1, const char* s2, const char* s3,
123 return std::string(s1) + s2 + s3 + s4;
126 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
128 struct SumOf5Functor {
129 int operator()(int a, int b, int c, int d, int e) {
130 return a + b + c + d + e;
134 std::string Concat5(const char* s1, const char* s2, const char* s3,
135 const char* s4, const char* s5) {
136 return std::string(s1) + s2 + s3 + s4 + s5;
139 int SumOf6(int a, int b, int c, int d, int e, int f) {
140 return a + b + c + d + e + f;
143 struct SumOf6Functor {
144 int operator()(int a, int b, int c, int d, int e, int f) {
145 return a + b + c + d + e + f;
149 std::string Concat6(const char* s1, const char* s2, const char* s3,
150 const char* s4, const char* s5, const char* s6) {
151 return std::string(s1) + s2 + s3 + s4 + s5 + s6;
154 std::string Concat7(const char* s1, const char* s2, const char* s3,
155 const char* s4, const char* s5, const char* s6,
157 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
160 std::string Concat8(const char* s1, const char* s2, const char* s3,
161 const char* s4, const char* s5, const char* s6,
162 const char* s7, const char* s8) {
163 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
166 std::string Concat9(const char* s1, const char* s2, const char* s3,
167 const char* s4, const char* s5, const char* s6,
168 const char* s7, const char* s8, const char* s9) {
169 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
172 std::string Concat10(const char* s1, const char* s2, const char* s3,
173 const char* s4, const char* s5, const char* s6,
174 const char* s7, const char* s8, const char* s9,
176 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
181 Foo() : value_(123) {}
183 int Nullary() const { return value_; }
185 short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT
187 std::string Binary(const std::string& str, char c) const { return str + c; }
189 int Ternary(int x, bool y, char z) { return value_ + x + y*z; }
191 int SumOf4(int a, int b, int c, int d) const {
192 return a + b + c + d + value_;
195 int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; }
197 int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; }
199 int SumOf6(int a, int b, int c, int d, int e, int f) {
200 return a + b + c + d + e + f;
203 std::string Concat7(const char* s1, const char* s2, const char* s3,
204 const char* s4, const char* s5, const char* s6,
206 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7;
209 std::string Concat8(const char* s1, const char* s2, const char* s3,
210 const char* s4, const char* s5, const char* s6,
211 const char* s7, const char* s8) {
212 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8;
215 std::string Concat9(const char* s1, const char* s2, const char* s3,
216 const char* s4, const char* s5, const char* s6,
217 const char* s7, const char* s8, const char* s9) {
218 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9;
221 std::string Concat10(const char* s1, const char* s2, const char* s3,
222 const char* s4, const char* s5, const char* s6,
223 const char* s7, const char* s8, const char* s9,
225 return std::string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10;
232 // Tests using Invoke() with a nullary function.
233 TEST(InvokeTest, Nullary) {
234 Action<int()> a = Invoke(Nullary); // NOLINT
235 EXPECT_EQ(1, a.Perform(make_tuple()));
238 // Tests using Invoke() with a unary function.
239 TEST(InvokeTest, Unary) {
240 Action<bool(int)> a = Invoke(Unary); // NOLINT
241 EXPECT_FALSE(a.Perform(make_tuple(1)));
242 EXPECT_TRUE(a.Perform(make_tuple(-1)));
245 // Tests using Invoke() with a binary function.
246 TEST(InvokeTest, Binary) {
247 Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT
248 const char* p = "Hello";
249 EXPECT_EQ(p + 2, a.Perform(make_tuple(p, Short(2))));
252 // Tests using Invoke() with a ternary function.
253 TEST(InvokeTest, Ternary) {
254 Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT
255 EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', Short(3))));
258 // Tests using Invoke() with a 4-argument function.
259 TEST(InvokeTest, FunctionThatTakes4Arguments) {
260 Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT
261 EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4)));
264 // Tests using Invoke() with a 5-argument function.
265 TEST(InvokeTest, FunctionThatTakes5Arguments) {
266 Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT
267 EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
270 // Tests using Invoke() with a 6-argument function.
271 TEST(InvokeTest, FunctionThatTakes6Arguments) {
272 Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT
273 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
276 // A helper that turns the type of a C-string literal from const
277 // char[N] to const char*.
278 inline const char* CharPtr(const char* s) { return s; }
280 // Tests using Invoke() with a 7-argument function.
281 TEST(InvokeTest, FunctionThatTakes7Arguments) {
282 Action<std::string(const char*, const char*, const char*, const char*,
283 const char*, const char*, const char*)>
286 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
287 CharPtr("4"), CharPtr("5"), CharPtr("6"),
291 // Tests using Invoke() with a 8-argument function.
292 TEST(InvokeTest, FunctionThatTakes8Arguments) {
293 Action<std::string(const char*, const char*, const char*, const char*,
294 const char*, const char*, const char*, const char*)>
296 EXPECT_EQ("12345678",
297 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
298 CharPtr("4"), CharPtr("5"), CharPtr("6"),
299 CharPtr("7"), CharPtr("8"))));
302 // Tests using Invoke() with a 9-argument function.
303 TEST(InvokeTest, FunctionThatTakes9Arguments) {
304 Action<std::string(const char*, const char*, const char*, const char*,
305 const char*, const char*, const char*, const char*,
308 EXPECT_EQ("123456789",
309 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
310 CharPtr("4"), CharPtr("5"), CharPtr("6"),
311 CharPtr("7"), CharPtr("8"), CharPtr("9"))));
314 // Tests using Invoke() with a 10-argument function.
315 TEST(InvokeTest, FunctionThatTakes10Arguments) {
316 Action<std::string(const char*, const char*, const char*, const char*,
317 const char*, const char*, const char*, const char*,
318 const char*, const char*)>
319 a = Invoke(Concat10);
320 EXPECT_EQ("1234567890",
321 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
322 CharPtr("4"), CharPtr("5"), CharPtr("6"),
323 CharPtr("7"), CharPtr("8"), CharPtr("9"),
327 // Tests using Invoke() with functions with parameters declared as Unused.
328 TEST(InvokeTest, FunctionWithUnusedParameters) {
329 Action<int(int, int, double, const std::string&)> a1 = Invoke(SumOfFirst2);
330 tuple<int, int, double, std::string> dummy =
331 make_tuple(10, 2, 5.6, std::string("hi"));
332 EXPECT_EQ(12, a1.Perform(dummy));
334 Action<int(int, int, bool, int*)> a2 =
336 EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL))));
339 // Tests using Invoke() with methods with parameters declared as Unused.
340 TEST(InvokeTest, MethodWithUnusedParameters) {
342 Action<int(std::string, bool, int, int)> a1 = Invoke(&foo, &Foo::SumOfLast2);
343 EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2)));
345 Action<int(char, double, int, int)> a2 =
346 Invoke(&foo, &Foo::SumOfLast2);
347 EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3)));
350 // Tests using Invoke() with a functor.
351 TEST(InvokeTest, Functor) {
352 Action<long(long, int)> a = Invoke(plus<long>()); // NOLINT
353 EXPECT_EQ(3L, a.Perform(make_tuple(1, 2)));
356 // Tests using Invoke(f) as an action of a compatible type.
357 TEST(InvokeTest, FunctionWithCompatibleType) {
358 Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT
359 EXPECT_EQ(4321, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
362 // Tests using Invoke() with an object pointer and a method pointer.
364 // Tests using Invoke() with a nullary method.
365 TEST(InvokeMethodTest, Nullary) {
367 Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT
368 EXPECT_EQ(123, a.Perform(make_tuple()));
371 // Tests using Invoke() with a unary method.
372 TEST(InvokeMethodTest, Unary) {
374 Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT
375 EXPECT_EQ(4123, a.Perform(make_tuple(4000)));
378 // Tests using Invoke() with a binary method.
379 TEST(InvokeMethodTest, Binary) {
381 Action<std::string(const std::string&, char)> a = Invoke(&foo, &Foo::Binary);
382 std::string s("Hell");
383 tuple<std::string, char> dummy = make_tuple(s, 'o');
384 EXPECT_EQ("Hello", a.Perform(dummy));
387 // Tests using Invoke() with a ternary method.
388 TEST(InvokeMethodTest, Ternary) {
390 Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT
391 EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, Char(1))));
394 // Tests using Invoke() with a 4-argument method.
395 TEST(InvokeMethodTest, MethodThatTakes4Arguments) {
397 Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT
398 EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4)));
401 // Tests using Invoke() with a 5-argument method.
402 TEST(InvokeMethodTest, MethodThatTakes5Arguments) {
404 Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT
405 EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5)));
408 // Tests using Invoke() with a 6-argument method.
409 TEST(InvokeMethodTest, MethodThatTakes6Arguments) {
411 Action<int(int, int, int, int, int, int)> a = // NOLINT
412 Invoke(&foo, &Foo::SumOf6);
413 EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6)));
416 // Tests using Invoke() with a 7-argument method.
417 TEST(InvokeMethodTest, MethodThatTakes7Arguments) {
419 Action<std::string(const char*, const char*, const char*, const char*,
420 const char*, const char*, const char*)>
421 a = Invoke(&foo, &Foo::Concat7);
423 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
424 CharPtr("4"), CharPtr("5"), CharPtr("6"),
428 // Tests using Invoke() with a 8-argument method.
429 TEST(InvokeMethodTest, MethodThatTakes8Arguments) {
431 Action<std::string(const char*, const char*, const char*, const char*,
432 const char*, const char*, const char*, const char*)>
433 a = Invoke(&foo, &Foo::Concat8);
434 EXPECT_EQ("12345678",
435 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
436 CharPtr("4"), CharPtr("5"), CharPtr("6"),
437 CharPtr("7"), CharPtr("8"))));
440 // Tests using Invoke() with a 9-argument method.
441 TEST(InvokeMethodTest, MethodThatTakes9Arguments) {
443 Action<std::string(const char*, const char*, const char*, const char*,
444 const char*, const char*, const char*, const char*,
446 a = Invoke(&foo, &Foo::Concat9);
447 EXPECT_EQ("123456789",
448 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
449 CharPtr("4"), CharPtr("5"), CharPtr("6"),
450 CharPtr("7"), CharPtr("8"), CharPtr("9"))));
453 // Tests using Invoke() with a 10-argument method.
454 TEST(InvokeMethodTest, MethodThatTakes10Arguments) {
456 Action<std::string(const char*, const char*, const char*, const char*,
457 const char*, const char*, const char*, const char*,
458 const char*, const char*)>
459 a = Invoke(&foo, &Foo::Concat10);
460 EXPECT_EQ("1234567890",
461 a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"),
462 CharPtr("4"), CharPtr("5"), CharPtr("6"),
463 CharPtr("7"), CharPtr("8"), CharPtr("9"),
467 // Tests using Invoke(f) as an action of a compatible type.
468 TEST(InvokeMethodTest, MethodWithCompatibleType) {
470 Action<long(int, short, char, bool)> a = // NOLINT
471 Invoke(&foo, &Foo::SumOf4);
472 EXPECT_EQ(4444, a.Perform(make_tuple(4000, Short(300), Char(20), true)));
475 // Tests using WithoutArgs with an action that takes no argument.
476 TEST(WithoutArgsTest, NoArg) {
477 Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT
478 EXPECT_EQ(1, a.Perform(make_tuple(2)));
481 // Tests using WithArg with an action that takes 1 argument.
482 TEST(WithArgTest, OneArg) {
483 Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT
484 EXPECT_TRUE(b.Perform(make_tuple(1.5, -1)));
485 EXPECT_FALSE(b.Perform(make_tuple(1.5, 1)));
488 TEST(ReturnArgActionTest, WorksForOneArgIntArg0) {
489 const Action<int(int)> a = ReturnArg<0>();
490 EXPECT_EQ(5, a.Perform(make_tuple(5)));
493 TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) {
494 const Action<bool(bool, bool, bool)> a = ReturnArg<0>();
495 EXPECT_TRUE(a.Perform(make_tuple(true, false, false)));
498 TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) {
499 const Action<std::string(int, int, std::string, int)> a = ReturnArg<2>();
500 EXPECT_EQ("seven", a.Perform(make_tuple(5, 6, std::string("seven"), 8)));
503 TEST(SaveArgActionTest, WorksForSameType) {
505 const Action<void(int n)> a1 = SaveArg<0>(&result);
506 a1.Perform(make_tuple(5));
507 EXPECT_EQ(5, result);
510 TEST(SaveArgActionTest, WorksForCompatibleType) {
512 const Action<void(bool, char)> a1 = SaveArg<1>(&result);
513 a1.Perform(make_tuple(true, 'a'));
514 EXPECT_EQ('a', result);
517 TEST(SaveArgPointeeActionTest, WorksForSameType) {
520 const Action<void(const int*)> a1 = SaveArgPointee<0>(&result);
521 a1.Perform(make_tuple(&value));
522 EXPECT_EQ(5, result);
525 TEST(SaveArgPointeeActionTest, WorksForCompatibleType) {
528 const Action<void(bool, char*)> a1 = SaveArgPointee<1>(&result);
529 a1.Perform(make_tuple(true, &value));
530 EXPECT_EQ('a', result);
533 TEST(SaveArgPointeeActionTest, WorksForLinkedPtr) {
535 linked_ptr<int> value(new int(5));
536 const Action<void(linked_ptr<int>)> a1 = SaveArgPointee<0>(&result);
537 a1.Perform(make_tuple(value));
538 EXPECT_EQ(5, result);
541 TEST(SetArgRefereeActionTest, WorksForSameType) {
543 const Action<void(int&)> a1 = SetArgReferee<0>(1);
544 a1.Perform(tuple<int&>(value));
548 TEST(SetArgRefereeActionTest, WorksForCompatibleType) {
550 const Action<void(int, int&)> a1 = SetArgReferee<1>('a');
551 a1.Perform(tuple<int, int&>(0, value));
552 EXPECT_EQ('a', value);
555 TEST(SetArgRefereeActionTest, WorksWithExtraArguments) {
557 const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a');
558 a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi"));
559 EXPECT_EQ('a', value);
562 // A class that can be used to verify that its destructor is called: it will set
563 // the bool provided to the constructor to true when destroyed.
564 class DeletionTester {
566 explicit DeletionTester(bool* is_deleted)
567 : is_deleted_(is_deleted) {
568 // Make sure the bit is set to false.
569 *is_deleted_ = false;
580 TEST(DeleteArgActionTest, OneArg) {
581 bool is_deleted = false;
582 DeletionTester* t = new DeletionTester(&is_deleted);
583 const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT
584 EXPECT_FALSE(is_deleted);
585 a1.Perform(make_tuple(t));
586 EXPECT_TRUE(is_deleted);
589 TEST(DeleteArgActionTest, TenArgs) {
590 bool is_deleted = false;
591 DeletionTester* t = new DeletionTester(&is_deleted);
592 const Action<void(bool, int, int, const char*, bool,
593 int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>();
594 EXPECT_FALSE(is_deleted);
595 a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t));
596 EXPECT_TRUE(is_deleted);
599 #if GTEST_HAS_EXCEPTIONS
601 TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) {
602 const Action<void(int n)> a = Throw('a');
603 EXPECT_THROW(a.Perform(make_tuple(0)), char);
606 class MyException {};
608 TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) {
609 const Action<double(char ch)> a = Throw(MyException());
610 EXPECT_THROW(a.Perform(make_tuple('0')), MyException);
613 TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) {
614 const Action<double()> a = Throw(MyException());
615 EXPECT_THROW(a.Perform(make_tuple()), MyException);
618 #endif // GTEST_HAS_EXCEPTIONS
620 // Tests that SetArrayArgument<N>(first, last) sets the elements of the array
621 // pointed to by the N-th (0-based) argument to values in range [first, last).
622 TEST(SetArrayArgumentTest, SetsTheNthArray) {
623 typedef void MyFunction(bool, int*, char*);
624 int numbers[] = { 1, 2, 3 };
625 Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3);
631 a.Perform(make_tuple(true, pn, pch));
636 EXPECT_EQ('\0', ch[0]);
637 EXPECT_EQ('\0', ch[1]);
638 EXPECT_EQ('\0', ch[2]);
639 EXPECT_EQ('\0', ch[3]);
641 // Tests first and last are iterators.
642 std::string letters = "abc";
643 a = SetArrayArgument<2>(letters.begin(), letters.end());
644 std::fill_n(n, 4, 0);
645 std::fill_n(ch, 4, '\0');
646 a.Perform(make_tuple(true, pn, pch));
651 EXPECT_EQ('a', ch[0]);
652 EXPECT_EQ('b', ch[1]);
653 EXPECT_EQ('c', ch[2]);
654 EXPECT_EQ('\0', ch[3]);
657 // Tests SetArrayArgument<N>(first, last) where first == last.
658 TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) {
659 typedef void MyFunction(bool, int*);
660 int numbers[] = { 1, 2, 3 };
661 Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers);
665 a.Perform(make_tuple(true, pn));
672 // Tests SetArrayArgument<N>(first, last) where *first is convertible
673 // (but not equal) to the argument type.
674 TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) {
675 typedef void MyFunction(bool, int*);
676 char chars[] = { 97, 98, 99 };
677 Action<MyFunction> a = SetArrayArgument<1>(chars, chars + 3);
679 int codes[4] = { 111, 222, 333, 444 };
681 a.Perform(make_tuple(true, pcodes));
682 EXPECT_EQ(97, codes[0]);
683 EXPECT_EQ(98, codes[1]);
684 EXPECT_EQ(99, codes[2]);
685 EXPECT_EQ(444, codes[3]);
688 // Test SetArrayArgument<N>(first, last) with iterator as argument.
689 TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) {
690 typedef void MyFunction(bool, std::back_insert_iterator<std::string>);
691 std::string letters = "abc";
692 Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end());
695 a.Perform(make_tuple(true, back_inserter(s)));
696 EXPECT_EQ(letters, s);
699 TEST(ReturnPointeeTest, Works) {
701 const Action<int()> a = ReturnPointee(&n);
702 EXPECT_EQ(42, a.Perform(make_tuple()));
705 EXPECT_EQ(43, a.Perform(make_tuple()));
708 } // namespace gmock_generated_actions_test
709 } // namespace testing