Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: base/bind_unittest.cc

Issue 8774032: Add Pass(), which implements move semantics, to scoped_ptr, scoped_array, and scoped_ptr_malloc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed spelling in comments Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/bind_internal.h.pump ('k') | base/callback.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
8 #include "testing/gmock/include/gmock/gmock.h" 11 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
10 13
11 using ::testing::Mock; 14 using ::testing::Mock;
12 using ::testing::Return; 15 using ::testing::Return;
13 using ::testing::StrictMock; 16 using ::testing::StrictMock;
14 17
15 namespace base { 18 namespace base {
16 namespace { 19 namespace {
17 20
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 CopyCounter(int* copies, int* assigns) 98 CopyCounter(int* copies, int* assigns)
96 : copies_(copies), assigns_(assigns) { 99 : copies_(copies), assigns_(assigns) {
97 } 100 }
98 101
99 CopyCounter(const CopyCounter& other) 102 CopyCounter(const CopyCounter& other)
100 : copies_(other.copies_), 103 : copies_(other.copies_),
101 assigns_(other.assigns_) { 104 assigns_(other.assigns_) {
102 (*copies_)++; 105 (*copies_)++;
103 } 106 }
104 107
105 // Probing for copies from coerscion. 108 // Probing for copies from coercion.
106 CopyCounter(const DerivedCopyCounter& other) 109 CopyCounter(const DerivedCopyCounter& other)
107 : copies_(other.copies_), 110 : copies_(other.copies_),
108 assigns_(other.assigns_) { 111 assigns_(other.assigns_) {
109 (*copies_)++; 112 (*copies_)++;
110 } 113 }
111 114
112 const CopyCounter& operator=(const CopyCounter& rhs) { 115 const CopyCounter& operator=(const CopyCounter& rhs) {
113 copies_ = rhs.copies_; 116 copies_ = rhs.copies_;
114 assigns_ = rhs.assigns_; 117 assigns_ = rhs.assigns_;
115 118
(...skipping 26 matching lines...) Expand all
142 ~DeleteCounter() { 145 ~DeleteCounter() {
143 (*deletes_)++; 146 (*deletes_)++;
144 } 147 }
145 148
146 void VoidMethod0() {} 149 void VoidMethod0() {}
147 150
148 private: 151 private:
149 int* deletes_; 152 int* deletes_;
150 }; 153 };
151 154
155 template <typename T>
156 T PassThru(T scoper) {
157 return scoper.Pass();
158 }
159
152 // Some test functions that we can Bind to. 160 // Some test functions that we can Bind to.
153 template <typename T> 161 template <typename T>
154 T PolymorphicIdentity(T t) { 162 T PolymorphicIdentity(T t) {
155 return t; 163 return t;
156 } 164 }
157 165
158 template <typename T> 166 template <typename T>
159 void VoidPolymorphic1(T t) { 167 void VoidPolymorphic1(T t) {
160 } 168 }
161 169
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 642
635 // Test Owned() support. 643 // Test Owned() support.
636 TEST_F(BindTest, Owned) { 644 TEST_F(BindTest, Owned) {
637 int deletes = 0; 645 int deletes = 0;
638 DeleteCounter* counter = new DeleteCounter(&deletes); 646 DeleteCounter* counter = new DeleteCounter(&deletes);
639 647
640 // If we don't capture, delete happens on Callback destruction/reset. 648 // If we don't capture, delete happens on Callback destruction/reset.
641 // return the same value. 649 // return the same value.
642 Callback<DeleteCounter*(void)> no_capture_cb = 650 Callback<DeleteCounter*(void)> no_capture_cb =
643 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter)); 651 Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
644 EXPECT_EQ(counter, no_capture_cb.Run()); 652 ASSERT_EQ(counter, no_capture_cb.Run());
645 EXPECT_EQ(counter, no_capture_cb.Run()); 653 ASSERT_EQ(counter, no_capture_cb.Run());
646 EXPECT_EQ(0, deletes); 654 EXPECT_EQ(0, deletes);
647 no_capture_cb.Reset(); // This should trigger a delete. 655 no_capture_cb.Reset(); // This should trigger a delete.
648 EXPECT_EQ(1, deletes); 656 EXPECT_EQ(1, deletes);
649 657
650 deletes = 0; 658 deletes = 0;
651 counter = new DeleteCounter(&deletes); 659 counter = new DeleteCounter(&deletes);
652 base::Closure own_object_cb = 660 base::Closure own_object_cb =
653 Bind(&DeleteCounter::VoidMethod0, Owned(counter)); 661 Bind(&DeleteCounter::VoidMethod0, Owned(counter));
654 own_object_cb.Run(); 662 own_object_cb.Run();
655 EXPECT_EQ(0, deletes); 663 EXPECT_EQ(0, deletes);
656 own_object_cb.Reset(); 664 own_object_cb.Reset();
657 EXPECT_EQ(1, deletes); 665 EXPECT_EQ(1, deletes);
658 } 666 }
659 667
668 // Passed() wrapper support.
669 // - Passed() can be constructed from a pointer to scoper.
670 // - Passed() can be constructed from a scoper rvalue.
671 // - Using Passed() gives Callback Ownership.
672 // - Ownership is transferred from Callback to callee on the first Run().
673 // - Callback supports unbound arguments.
674 TEST_F(BindTest, ScopedPtr) {
675 int deletes = 0;
676
677 // Tests the Passed() function's support for pointers.
678 scoped_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes));
679 Callback<scoped_ptr<DeleteCounter>(void)> unused_callback =
680 Bind(&PassThru<scoped_ptr<DeleteCounter> >, Passed(&ptr));
681 EXPECT_FALSE(ptr.get());
682 EXPECT_EQ(0, deletes);
683
684 // If we never invoke the Callback, it retains ownership and deletes.
685 unused_callback.Reset();
686 EXPECT_EQ(1, deletes);
687
688 // Tests the Passed() function's support for rvalues.
689 deletes = 0;
690 DeleteCounter* counter = new DeleteCounter(&deletes);
691 Callback<scoped_ptr<DeleteCounter>(void)> callback =
692 Bind(&PassThru<scoped_ptr<DeleteCounter> >,
693 Passed(scoped_ptr<DeleteCounter>(counter)));
694 EXPECT_FALSE(ptr.get());
695 EXPECT_EQ(0, deletes);
696
697 // Check that ownership can be transferred back out.
698 scoped_ptr<DeleteCounter> result = callback.Run();
699 ASSERT_EQ(counter, result.get());
700 EXPECT_EQ(0, deletes);
701
702 // Resetting does not delete since ownership was transferred.
703 callback.Reset();
704 EXPECT_EQ(0, deletes);
705
706 // Ensure that we actually did get ownership.
707 result.reset();
708 EXPECT_EQ(1, deletes);
709
710 // Test unbound argument forwarding.
711 Callback<scoped_ptr<DeleteCounter>(scoped_ptr<DeleteCounter>)> cb_unbound =
712 Bind(&PassThru<scoped_ptr<DeleteCounter> >);
713 ptr.reset(new DeleteCounter(&deletes));
714 cb_unbound.Run(ptr.Pass());
715 }
716
660 // Argument Copy-constructor usage for non-reference parameters. 717 // Argument Copy-constructor usage for non-reference parameters.
661 // - Bound arguments are only copied once. 718 // - Bound arguments are only copied once.
662 // - Forwarded arguments are only copied once. 719 // - Forwarded arguments are only copied once.
663 // - Forwarded arguments with coerscions are only copied twice (once for the 720 // - Forwarded arguments with coercions are only copied twice (once for the
664 // coerscion, and one for the final dispatch). 721 // coercion, and one for the final dispatch).
665 TEST_F(BindTest, ArgumentCopies) { 722 TEST_F(BindTest, ArgumentCopies) {
666 int copies = 0; 723 int copies = 0;
667 int assigns = 0; 724 int assigns = 0;
668 725
669 CopyCounter counter(&copies, &assigns); 726 CopyCounter counter(&copies, &assigns);
670 727
671 Callback<void(void)> copy_cb = 728 Callback<void(void)> copy_cb =
672 Bind(&VoidPolymorphic1<CopyCounter>, counter); 729 Bind(&VoidPolymorphic1<CopyCounter>, counter);
673 EXPECT_GE(1, copies); 730 EXPECT_GE(1, copies);
674 EXPECT_EQ(0, assigns); 731 EXPECT_EQ(0, assigns);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); 770 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1);
714 EXPECT_EQ(1, fastcall_cb.Run()); 771 EXPECT_EQ(1, fastcall_cb.Run());
715 772
716 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); 773 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2);
717 EXPECT_EQ(2, stdcall_cb.Run()); 774 EXPECT_EQ(2, stdcall_cb.Run());
718 } 775 }
719 #endif 776 #endif
720 777
721 } // namespace 778 } // namespace
722 } // namespace base 779 } // namespace base
OLDNEW
« no previous file with comments | « base/bind_internal.h.pump ('k') | base/callback.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698