Index: base/prebind_unittest.cc |
diff --git a/base/prebind_unittest.cc b/base/prebind_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..83c43228448837ccf189675d69372b9bf8637ec9 |
--- /dev/null |
+++ b/base/prebind_unittest.cc |
@@ -0,0 +1,484 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/prebind.h" |
+#include "base/uber_callback.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using ::testing::Mock; |
+using ::testing::Return; |
+using ::testing::StrictMock; |
+ |
+namespace base { |
+namespace { |
+ |
+class NoRef { |
+ public: |
+ NoRef() {} |
+ |
+ MOCK_METHOD0(VoidMethod0, void(void)); |
+ MOCK_CONST_METHOD0(VoidConstMethod0, void(void)); |
+ |
+ MOCK_METHOD0(IntMethod0, int(void)); |
+ MOCK_CONST_METHOD0(IntConstMethod0, int(void)); |
+ |
+ private: |
+ // Particularly important in test to ensure no copies are made. |
+ DISALLOW_COPY_AND_ASSIGN(NoRef); |
+}; |
+ |
+class HasRef : public NoRef { |
+ public: |
+ HasRef() {} |
+ |
+ MOCK_CONST_METHOD0(AddRef, void(void)); |
+ MOCK_CONST_METHOD0(Release, void(void)); |
+ |
+ private: |
+ // Particularly important in test to ensure no copies are made. |
+ DISALLOW_COPY_AND_ASSIGN(HasRef); |
+}; |
+ |
+class Parent { |
+ public: |
+ static const int kParentValue; |
+ void AddRef(void) const {} |
+ void Release(void) const {} |
+ virtual void VirtualSet() { value = kParentValue; } |
+ virtual void NonVirtualSet() { value = kParentValue; } |
+ int value; |
+}; |
+const int Parent::kParentValue = 2; |
+ |
+class Child : public Parent { |
+ public: |
+ static const int kChildValue; |
+ virtual void VirtualSet() { value = kChildValue; } |
+ virtual void NonVirtualSet() { value = kChildValue; } |
+}; |
+const int Child::kChildValue = 2; |
+ |
+// Used for probing the number of copies that occur if a type must be coerced |
+// during argument forwarding in the Run() methods. |
+struct DerivedCopyCounter { |
+ DerivedCopyCounter(int* copies, int* assigns) |
+ : copies_(copies), assigns_(assigns) { |
+ } |
+ int* copies_; |
+ int* assigns_; |
+}; |
+ |
+// Used for probing the number of copies in an argument. |
+class CopyCounter { |
+ public: |
+ CopyCounter(int* copies, int* assigns) |
+ : copies_(copies), assigns_(assigns) { |
+ } |
+ |
+ CopyCounter(const CopyCounter& other) |
+ : copies_(other.copies_), |
+ assigns_(other.assigns_) { |
+ (*copies_)++; |
+ } |
+ |
+ // Probing for copies from coerscion. |
+ CopyCounter(const DerivedCopyCounter& other) |
+ : copies_(other.copies_), |
+ assigns_(other.assigns_) { |
+ (*copies_)++; |
+ } |
+ |
+ const CopyCounter& operator=(const CopyCounter& rhs) { |
+ copies_ = rhs.copies_; |
+ assigns_ = rhs.assigns_; |
+ |
+ if (assigns_) { |
+ (*assigns_)++; |
+ } |
+ |
+ return *this; |
+ } |
+ |
+ private: |
+ int* copies_; |
+ int* assigns_; |
+}; |
+ |
+// Test functions that we can bind on. |
+template <typename T> |
+T PolymorphicIdentity(T t) { |
+ return t; |
+} |
+ |
+template <typename T> |
+void VoidPolymorphic1(T t) { |
+} |
+ |
+int Identity(int n) { |
+ return n; |
+} |
+ |
+int Sum(int a, int b, int c, int d, int e, int f) { |
+ return a + b + c + d + e + f; |
+} |
+ |
+const char* CStringIdentity(const char* s) { |
+ return s; |
+} |
+ |
+int UnwrapParent(Parent p) { |
+ return p.value; |
+} |
+ |
+int UnwrapParentPtr(Parent* p) { |
+ return p->value; |
+} |
+ |
+int UnwrapParentConstRef(const Parent& p) { |
+ return p.value; |
+} |
+ |
+// Only useful in no-compile tests. |
+int UnwrapParentRef(Parent& p) { |
+ return p.value; |
+} |
+ |
+class PrebindTest : public ::testing::Test { |
+ public: |
+ PrebindTest() { |
+ const_has_ref_ptr_ = &has_ref_; |
+ const_no_ref_ptr_ = &no_ref_; |
+ } |
+ |
+ virtual ~PrebindTest() { |
+ Mock::VerifyAndClearExpectations(&static_func_mock); |
+ } |
+ |
+ static void VoidFunc0(void) { |
+ static_func_mock.VoidMethod0(); |
+ } |
+ |
+ static int IntFunc0(void) { return static_func_mock.IntMethod0(); } |
+ |
+ protected: |
+ StrictMock<NoRef> no_ref_; |
+ StrictMock<HasRef> has_ref_; |
+ const HasRef* const_has_ref_ptr_; |
+ const NoRef* const_no_ref_ptr_; |
+ |
+ // Used by the static functions to perform expectations. |
+ static StrictMock<NoRef> static_func_mock; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(PrebindTest); |
+}; |
+ |
+StrictMock<NoRef> PrebindTest::static_func_mock; |
+ |
+// Ensur we can create unbound callbacks. We need this to be able to store them |
+// in class members that can be assigned after construction. |
+TEST_F(PrebindTest, DefaultConstruction) { |
+ /* |
+ Callback<void(void)> c0; |
+ Callback<void(int)> c1; |
+ Callback<void(int,int)> c2; |
+ Callback<void(int,int,int)> c3; |
+ Callback<void(int,int,int,int)> c4; |
+ Callback<void(int,int,int,int,int)> c5; |
+ Callback<void(int,int,int,int,int,int)> c6; |
+ */ |
+} |
+ |
+// Sanity check that we can instantiate a callback for each arity. |
+TEST_F(PrebindTest, ArityTest) { |
+ Callback<int(void)> c0 = Prebind(&Sum, 32, 16, 8, 4, 2, 1); |
+ EXPECT_EQ(63, c0.Run()); |
+ |
+ Callback<int(int)> c1= Prebind(&Sum, 32, 16, 8, 4, 2); |
+ EXPECT_EQ(75, c1.Run(13)); |
+ |
+ Callback<int(int,int)> c2= Prebind(&Sum, 32, 16, 8, 4); |
+ EXPECT_EQ(85, c2.Run(13, 12)); |
+ |
+ Callback<int(int,int,int)> c3= Prebind(&Sum, 32, 16, 8); |
+ EXPECT_EQ(92, c3.Run(13, 12, 11)); |
+ |
+ Callback<int(int,int,int,int)> c4= Prebind(&Sum, 32, 16); |
+ EXPECT_EQ(94, c4.Run(13, 12, 11, 10)); |
+ |
+ Callback<int(int,int,int,int,int)> c5= Prebind(&Sum, 32); |
+ EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9)); |
+ |
+ Callback<int(int,int,int,int,int,int)> c6= Prebind(&Sum); |
+ EXPECT_EQ(69, c6.Run(13, 12, 11, 10, 9, 14)); |
+} |
+ |
+// Function type support. |
+// - Normal function. |
+// - Method bound to non-const object. |
+// - Const method bound to non-const object. |
+// - Const method bound to const object. |
+// - Derived classes can be used with pointers to non-virtual base functions. |
+// - Derived classes can be used with pointers to virtual base functions (and |
+// preserve virtual dispatch). |
+TEST_F(PrebindTest, FunctionTypeSupport) { |
+ EXPECT_CALL(static_func_mock, VoidMethod0()); |
+ EXPECT_CALL(has_ref_, AddRef()).Times(3); |
+ EXPECT_CALL(has_ref_, Release()).Times(3); |
+ EXPECT_CALL(has_ref_, VoidMethod0()); |
+ EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2); |
+ |
+ Closure cb_normal = Prebind(&VoidFunc0); |
+ Closure cb_method = Prebind(&HasRef::VoidMethod0, &has_ref_); |
+ Closure cb_const_method_nonconst_obj = Prebind(&HasRef::VoidConstMethod0, |
+ &has_ref_); |
+ Closure cb_const_method_const_obj = Prebind(&HasRef::VoidConstMethod0, |
+ const_has_ref_ptr_); |
+ cb_normal.Run(); |
+ cb_method.Run(); |
+ cb_const_method_nonconst_obj.Run(); |
+ cb_const_method_const_obj.Run(); |
+ |
+ Child child; |
+ child.value = 0; |
+ Closure cb_virtual_set = Prebind(&Parent::VirtualSet, &child); |
+ cb_virtual_set.Run(); |
+ EXPECT_EQ(Child::kChildValue, child.value); |
+ |
+ child.value = 0; |
+ Closure cb_non_virtual_set = Prebind(&Parent::NonVirtualSet, &child); |
+ cb_non_virtual_set.Run(); |
+ EXPECT_EQ(Parent::kParentValue, child.value); |
+} |
+ |
+// Return value support. |
+// - function with return value. |
+// - method with return value. |
+// - const method with return value. |
+TEST_F(PrebindTest, ReturnValues) { |
+ EXPECT_CALL(static_func_mock, IntMethod0()).WillOnce(Return(1337)); |
+ EXPECT_CALL(has_ref_, AddRef()).Times(3); |
+ EXPECT_CALL(has_ref_, Release()).Times(3); |
+ EXPECT_CALL(has_ref_, IntMethod0()).WillOnce(Return(31337)); |
+ EXPECT_CALL(has_ref_, IntConstMethod0()) |
+ .WillOnce(Return(41337)) |
+ .WillOnce(Return(51337)); |
+ |
+ Callback<int(void)> cb_normal = Prebind(&IntFunc0); |
+ Callback<int(void)> cb_method = Prebind(&HasRef::IntMethod0, &has_ref_); |
+ Callback<int(void)> cb_const_method_nonconst_obj = |
+ Prebind(&HasRef::IntConstMethod0, &has_ref_); |
+ Callback<int(void)> cb_const_method_const_obj = |
+ Prebind(&HasRef::IntConstMethod0, const_has_ref_ptr_); |
+ EXPECT_EQ(1337, cb_normal.Run()); |
+ EXPECT_EQ(31337, cb_method.Run()); |
+ EXPECT_EQ(41337, cb_const_method_nonconst_obj.Run()); |
+ EXPECT_EQ(51337, cb_const_method_const_obj.Run()); |
+} |
+ |
+// Argument binding tests. |
+// - Argument binding to primitive. |
+// - Argument binding to a literal integer. |
+// - Argument binding to a literal string. |
+// - Argument binding with template function. |
+// - Argument binding to an object. |
+// - Argument gets type converted. |
+// - Pointer argument gets converted. |
+// - Const Reference forces conversion. |
+TEST_F(PrebindTest, ArgumentBinding) { |
+ int n = 2; |
+ |
+ Callback<int(void)> cb_bind_primitive = Prebind(&Identity, n); |
+ EXPECT_EQ(n, cb_bind_primitive.Run()); |
+ |
+ Callback<int(void)> cb_bind_int_literal = Prebind(&Identity, 3); |
+ EXPECT_EQ(3, cb_bind_int_literal.Run()); |
+ |
+ /* |
+ Callback<const char*(void)> cb_bind_string_literal = |
+ Prebind(&CStringIdentity, "hi"); |
+ EXPECT_STREQ("hi", cb_bind_string_literal.Run()); |
+ */ |
+ |
+ Callback<int(void)> cb_bind_template_function = |
+ Prebind(&PolymorphicIdentity<int>, 4); |
+ EXPECT_EQ(4, cb_bind_template_function.Run()); |
+ |
+ Parent p; |
+ p.value = 5; |
+ Callback<int(void)> cb_bind_object = Prebind(&UnwrapParent, p); |
+ EXPECT_EQ(5, cb_bind_object.Run()); |
+ |
+ Child c; |
+ c.value = 6; |
+ Callback<int(void)> cb_bind_promotes = Prebind(&UnwrapParent, c); |
+ EXPECT_EQ(6, cb_bind_promotes.Run()); |
+ |
+ c.value = 7; |
+ Callback<int(void)> cb_bind_pointer_promotes = Prebind(&UnwrapParentPtr, &c); |
+ EXPECT_EQ(7, cb_bind_pointer_promotes.Run()); |
+ |
+ c.value = 8; |
+ Callback<int(void)> cb_bind_const_reference_promotes = |
+ Prebind(&UnwrapParentConstRef, c); |
+ EXPECT_EQ(8, cb_bind_const_reference_promotes.Run()); |
+} |
+ |
+// Unretained() wrapper support. |
+// - method bound to Unretained() non-object. |
+// - const method bound to Unretained() non-const object. |
+// - const method bound to Unretained() const object. |
+// - Unretained does not take a copy of the object. |
+TEST_F(PrebindTest, Unretained) { |
+ EXPECT_CALL(no_ref_, VoidMethod0()); |
+ EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2); |
+ |
+ Callback<void(void)> cb_method = |
+ Prebind(&NoRef::VoidMethod0, Unretained(&no_ref_)); |
+ cb_method.Run(); |
+ |
+ Callback<void(void)> cb_const_method = |
+ Prebind(&NoRef::VoidConstMethod0, Unretained(&no_ref_)); |
+ cb_const_method.Run(); |
+ |
+ Callback<void(void)> cb_const_method_const_ptr = |
+ Prebind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_)); |
+ cb_const_method_const_ptr.Run(); |
+} |
+ |
+// ConstRef() wrapper support. |
+// - binding w/o ConstRef takes a copy. |
+// - binding a ConstRef takes a reference. |
+TEST_F(PrebindTest, ConstRef) { |
+ int n = 1; |
+ |
+ Callback<int(void)> cb_copy = Prebind(&Identity, n); |
+ Callback<int(void)> cb_const_ref = Prebind(&Identity, ConstRef(n)); |
+ EXPECT_EQ(n, cb_copy.Run()); |
+ EXPECT_EQ(n, cb_const_ref.Run()); |
+ n++; |
+ EXPECT_EQ(n - 1, cb_copy.Run()); |
+ EXPECT_EQ(n, cb_const_ref.Run()); |
+} |
+ |
+// Argument Copy-constructor usage for non-reference parameters. |
+// - Prebound arguments are only copied once. |
+// - Forwarded arguments are only copied once. |
+// - Forwarded arguments with coerscions are only copied twice (once for the |
+// coerscion, and one for the final dispatch). |
+TEST_F(PrebindTest, Copies) { |
+ int copies = 0; |
+ int assigns = 0; |
+ |
+ CopyCounter counter(&copies, &assigns); |
+ |
+ Callback<void(void)> cb_copy = |
+ Prebind(&VoidPolymorphic1<CopyCounter>, counter); |
+ EXPECT_GE(1, copies); |
+ EXPECT_EQ(0, assigns); |
+ |
+ copies = 0; |
+ assigns = 0; |
+ Callback<void(CopyCounter)> cb_forward = |
+ Prebind(&VoidPolymorphic1<CopyCounter>); |
+ cb_forward.Run(counter); |
+ EXPECT_GE(1, copies); |
+ EXPECT_EQ(0, assigns); |
+ |
+ copies = 0; |
+ assigns = 0; |
+ DerivedCopyCounter dervied(&copies, &assigns); |
+ Callback<void(CopyCounter)> cb_coerce = |
+ Prebind(&VoidPolymorphic1<CopyCounter>); |
+ cb_coerce.Run(dervied); |
+ EXPECT_GE(2, copies); |
+ EXPECT_EQ(0, assigns); |
+} |
+ |
+// Callback construction and assignment tests. |
+// - Construction from an InvokerStorageHolder should not cause ref/deref. |
+// - Assignment from other callback should only cause one ref |
+// TODO(ajwong): Is there actually a way to test this? |
+ |
+// Missing functionality. |
+// - Invoking the return of Prebind. Prebind(&foo).Run() does not work; |
+ |
+// No-compile tests. These should not compile. If they do, we are allowing |
+// error-prone, or incorrect behavior in the callback system. Uncomment the |
+// tests to check. |
+TEST_F(PrebindTest, NoCompile) { |
+ // - Method bound to const-object. |
+ // |
+ // Only const methods should be allowed to work with const objects. |
+ // |
+ // Callback<void(void)> cb_method_to_const = |
+ // Prebind(&HasRef::VoidMethod0, const_has_ref_ptr_); |
+ // cb_method_to_const.Run(); |
+ |
+ // - Method bound to non-refcounted object. |
+ // - Const Method bound to non-refcounted object. |
+ // |
+ // We require refcounts unless you have Unretained(). |
+ // |
+ // Callback<void(void)> cb_no_ref = |
+ // Prebind(&NoRef::VoidMethod0, &no_ref_); |
+ // cb_no_ref.Run(); |
+ // Callback<void(void)> cb_no_ref_const = |
+ // Prebind(&NoRef::VoidConstMethod0, &no_ref_); |
+ // cb_no_ref_const.Run(); |
+ |
+ // - Unretained() used with a refcounted object. |
+ // |
+ // If the object supports refcounts, unretaining it in the callback is a |
+ // memory management contract break. |
+ Callback<void(void)> cb_unretained = |
+ Prebind(&HasRef::VoidConstMethod0, Unretained(&has_ref_)); |
+ cb_unretained.Run(); |
+ |
+ // - Const argument used with non-const pointer parameter of same type. |
+ // - Const argument used with non-const pointer parameter of super type. |
+ // |
+ // This is just a const-correctness check. |
+ // |
+ // const Parent* const_parent_ptr; |
+ // const Child* const_child_ptr; |
+ // Callback<Parent*(void)> cb_pointer_same = |
+ // Prebind(&PolymorphicIdentity<Parent*>, const_parent_ptr); |
+ // cb_pointer_same.Run(); |
+ // Callback<Parent*(void)> cb_pointer_super = |
+ // Prebind(&PolymorphicIdentity<Parent*>, const_child_ptr); |
+ // cb_pointer_super.Run(); |
+ |
+ // - Construction of Callback<A> from Callback<B> if A is supertype of B. |
+ // Specific example: Callback<void(void)> a; Callback<int(void)> b; a = b; |
+ // |
+ // While this is technically safe, most people aren't used to it when coding |
+ // C++ so if this is happening, it is almost certainly an error. |
+ // |
+ // Callback<int(void)> cb_a0 = Prebind(&Identity, 1); |
+ // Callback<void(void)> cb_b0 = cb_a0; |
+ |
+ // - Assignment of Callback<A> from Callback<B> if A is supertype of B. |
+ // See explanation above. |
+ // |
+ // Callback<int(void)> cb_a1 = Prebind(&Identity, 1); |
+ // Callback<void(void)> cb_b1; |
+ // cb_a1 = cb_b1; |
+ |
+ // - Functions with reference parameters, unsupported. |
+ // |
+ // Reference parameters are disallowed by the google style guide, and since we |
+ // are doing argument forwarding it becomes very tricky to avoid copies, |
+ // maintain const correctness, and not accidentally have the function be |
+ // modifying a temporary. |
+ Parent p; |
+ Callback<int(Parent&)> cb_ref_arg = Prebind(&UnwrapParentRef); |
+ cb_ref_arg.Run(p); |
+ // Callback<int(void)> cb_ref = Prebind(&UnwrapParentRef, p); |
+ // cb_ref.Run(); |
+} |
+ |
+} // namespace |
+} // namespace base |