diff options
author | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-21 19:23:44 +0000 |
---|---|---|
committer | ajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-21 19:23:44 +0000 |
commit | 7296f2763bd40e560466f09836c7208c42d90f94 (patch) | |
tree | 142a03c36f75f42738206355a029707ad88cdffc /base/bind_unittest.cc | |
parent | 1fbbf963079bc977ea041b7e50ddf4666f6fec38 (diff) | |
download | chromium_src-7296f2763bd40e560466f09836c7208c42d90f94.zip chromium_src-7296f2763bd40e560466f09836c7208c42d90f94.tar.gz chromium_src-7296f2763bd40e560466f09836c7208c42d90f94.tar.bz2 |
Callback API Change: Reimplement Bind(); support IgnoreResult, full currying, and use less types.
The main API change IgnoreResult() and fully currying. See unittest for what the new API looks like. The rest of the changes are done to support that.
Previously, IgnoreReturn could not be used with WeakPtr<> Bind()s as it was applied after the fact to the Callback object. Now, IgnoreResult() wraps the function like Unretained().
As an incidental benefit, the new implementation gave us fully currying for free.
Also, the new implementation scales better when supporting higher arities of functions. The new type growth is:
(n^2 + 20n) / 2
as opposed to
(3n^2 + 17n) / 2
where n == arity.
For n = 6 and n=10, the new implementation has 81 and 155 templates respectively.
The old implementation had 105 and 235 templates respectively.
BUG=35233,98919,98542
TEST=existing unittests
Review URL: http://codereview.chromium.org/8483003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110975 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/bind_unittest.cc')
-rw-r--r-- | base/bind_unittest.cc | 94 |
1 files changed, 60 insertions, 34 deletions
diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc index 0fdd241..654a277 100644 --- a/base/bind_unittest.cc +++ b/base/bind_unittest.cc @@ -260,35 +260,28 @@ TEST_F(BindTest, ArityTest) { EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14)); } -// Bind should be able to take existing Callbacks and convert to a Closure. -TEST_F(BindTest, CallbackBindMore) { - int output = 0; - Closure c; +// Test the Currying ability of the Callback system. +TEST_F(BindTest, CurryingTest) { + Callback<int(int,int,int,int,int,int)> c6 = Bind(&Sum); + EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14)); - Callback<void(int)> c1 = Bind(&OutputSum, &output, 16, 8, 4, 2); - c = Bind(c1, 10); - c.Run(); - EXPECT_EQ(40, output); + Callback<int(int,int,int,int,int)> c5 = Bind(c6, 32); + EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9)); - Callback<void(int,int)> c2 = Bind(&OutputSum, &output, 16, 8, 4); - c = Bind(c2, 10, 9); - c.Run(); - EXPECT_EQ(47, output); + Callback<int(int,int,int,int)> c4 = Bind(c5, 16); + EXPECT_EQ(94, c4.Run(13, 12, 11, 10)); - Callback<void(int,int,int)> c3 = Bind(&OutputSum, &output, 16, 8); - c = Bind(c3, 10, 9, 8); - c.Run(); - EXPECT_EQ(51, output); + Callback<int(int,int,int)> c3 = Bind(c4, 8); + EXPECT_EQ(92, c3.Run(13, 12, 11)); - Callback<void(int,int,int,int)> c4 = Bind(&OutputSum, &output, 16); - c = Bind(c4, 10, 9, 8, 7); - c.Run(); - EXPECT_EQ(50, output); + Callback<int(int,int)> c2 = Bind(c3, 4); + EXPECT_EQ(85, c2.Run(13, 12)); - Callback<void(int,int,int,int,int)> c5 = Bind(&OutputSum, &output); - c = Bind(c5, 10, 9, 8, 7, 6); - c.Run(); - EXPECT_EQ(40, output); + Callback<int(int)> c1 = Bind(c2, 2); + EXPECT_EQ(75, c1.Run(13)); + + Callback<int(void)> c0 = Bind(c1, 1); + EXPECT_EQ(63, c0.Run()); } // Function type support. @@ -363,13 +356,46 @@ TEST_F(BindTest, ReturnValues) { EXPECT_EQ(51337, const_method_const_obj_cb.Run()); } -// IgnoreReturn adapter test. -// - Function with return value, and no params can be converted to Closure. -TEST_F(BindTest, IgnoreReturn) { +// IgnoreResult adapter test. +// - Function with return value. +// - Method with return value. +// - Const Method with return. +// - Method with return value bound to WeakPtr<>. +// - Const Method with return bound to WeakPtr<>. +TEST_F(BindTest, IgnoreResult) { EXPECT_CALL(static_func_mock_, IntMethod0()).WillOnce(Return(1337)); - Callback<int(void)> normal_cb = Bind(&IntFunc0); - Closure c = IgnoreReturn(normal_cb); - c.Run(); + EXPECT_CALL(has_ref_, AddRef()).Times(2); + EXPECT_CALL(has_ref_, Release()).Times(2); + EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(10)); + EXPECT_CALL(has_ref_, IntConstMethod0()).WillOnce(Return(11)); + EXPECT_CALL(no_ref_, IntMethod0()).WillOnce(Return(12)); + EXPECT_CALL(no_ref_, IntConstMethod0()).WillOnce(Return(13)); + + Closure normal_func_cb = Bind(IgnoreResult(&IntFunc0)); + normal_func_cb.Run(); + + Closure non_void_method_cb = + Bind(IgnoreResult(&HasRef::IntMethod0), &has_ref_); + non_void_method_cb.Run(); + + Closure non_void_const_method_cb = + Bind(IgnoreResult(&HasRef::IntConstMethod0), &has_ref_); + non_void_const_method_cb.Run(); + + WeakPtrFactory<NoRef> weak_factory(&no_ref_); + WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); + + Closure non_void_weak_method_cb = + Bind(IgnoreResult(&NoRef::IntMethod0), weak_factory.GetWeakPtr()); + non_void_weak_method_cb.Run(); + + Closure non_void_weak_const_method_cb = + Bind(IgnoreResult(&NoRef::IntConstMethod0), weak_factory.GetWeakPtr()); + non_void_weak_const_method_cb.Run(); + + weak_factory.InvalidateWeakPtrs(); + non_void_weak_const_method_cb.Run(); + non_void_weak_method_cb.Run(); } // Argument binding tests. @@ -453,7 +479,7 @@ TEST_F(BindTest, UnboundArgumentTypeSupport) { } // Function with unbound reference parameter. -// - Original paraemter is modified by callback. +// - Original parameter is modified by callback. TEST_F(BindTest, UnboundReferenceSupport) { int n = 0; Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet); @@ -554,15 +580,15 @@ TEST_F(BindTest, WeakPtr) { WeakPtrFactory<NoRef> weak_factory(&no_ref_); WeakPtrFactory<const NoRef> const_weak_factory(const_no_ref_ptr_); - Callback<void(void)> method_cb = + Closure method_cb = Bind(&NoRef::VoidMethod0, weak_factory.GetWeakPtr()); method_cb.Run(); - Callback<void(void)> const_method_cb = + Closure const_method_cb = Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); const_method_cb.Run(); - Callback<void(void)> const_method_const_ptr_cb = + Closure const_method_const_ptr_cb = Bind(&NoRef::VoidConstMethod0, const_weak_factory.GetWeakPtr()); const_method_const_ptr_cb.Run(); |