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

Side by Side Diff: base/bind_unittest.cc

Issue 6718021: Callback support for unbound reference and array arguments. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleaned up. Created 9 years, 9 months 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
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 #if defined(BASE_CALLBACK_H_) 7 #if defined(BASE_CALLBACK_H_)
8 // We explicitly do not want to include callback.h so people are not tempted 8 // We explicitly do not want to include callback.h so people are not tempted
9 // to use bind.h in a headerfile for getting the Callback types. 9 // to use bind.h in a headerfile for getting the Callback types.
10 #error "base/bind.h should avoid pulling in callback.h by default." 10 #error "base/bind.h should avoid pulling in callback.h by default."
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 } 167 }
168 168
169 int UnwrapNoRefParentPtr(NoRefParent* p) { 169 int UnwrapNoRefParentPtr(NoRefParent* p) {
170 return p->value; 170 return p->value;
171 } 171 }
172 172
173 int UnwrapNoRefParentConstRef(const NoRefParent& p) { 173 int UnwrapNoRefParentConstRef(const NoRefParent& p) {
174 return p.value; 174 return p.value;
175 } 175 }
176 176
177 void RefArgSet(int &n) {
178 n = 2;
179 }
180
177 // Only useful in no-compile tests. 181 // Only useful in no-compile tests.
178 int UnwrapNoRefParentRef(Parent& p) { 182 int UnwrapNoRefParentRef(Parent& p) {
179 return p.value; 183 return p.value;
180 } 184 }
181 185
182 class BindTest : public ::testing::Test { 186 class BindTest : public ::testing::Test {
183 public: 187 public:
184 BindTest() { 188 BindTest() {
185 const_has_ref_ptr_ = &has_ref_; 189 const_has_ref_ptr_ = &has_ref_;
186 const_no_ref_ptr_ = &no_ref_; 190 const_no_ref_ptr_ = &no_ref_;
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 Callback<int(void)> bind_pointer_promotes_cb = 348 Callback<int(void)> bind_pointer_promotes_cb =
345 Bind(&UnwrapNoRefParentPtr, &c); 349 Bind(&UnwrapNoRefParentPtr, &c);
346 EXPECT_EQ(7, bind_pointer_promotes_cb.Run()); 350 EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
347 351
348 c.value = 8; 352 c.value = 8;
349 Callback<int(void)> bind_const_reference_promotes_cb = 353 Callback<int(void)> bind_const_reference_promotes_cb =
350 Bind(&UnwrapNoRefParentConstRef, c); 354 Bind(&UnwrapNoRefParentConstRef, c);
351 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run()); 355 EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
352 } 356 }
353 357
358 // Unbound argument type support tests.
359 // - Unbound value.
360 // - Unbound pointer.
361 // - Unbound reference.
362 // - Unbound const reference.
363 // - Unbound unsized array.
364 // - Unbound sized array.
365 // - Unbound array-of-arrays.
366 TEST_F(BindTest, UnboundArgumentTypeSupport) {
367 Callback<void(int)> unbound_value_cb = Bind(&VoidPolymorphic1<int>);
368 Callback<void(int*)> unbound_pointer_cb = Bind(&VoidPolymorphic1<int*>);
369 Callback<void(int&)> unbound_ref_cb = Bind(&VoidPolymorphic1<int&>);
370 Callback<void(const int&)> unbound_const_ref_cb =
371 Bind(&VoidPolymorphic1<const int&>);
372 Callback<void(int[])> unbound_unsized_array_cb =
373 Bind(&VoidPolymorphic1<int[]>);
374 Callback<void(int[2])> unbound_sized_array_cb =
375 Bind(&VoidPolymorphic1<int[2]>);
376 Callback<void(int[][2])> unbound_array_of_arrays_cb =
377 Bind(&VoidPolymorphic1<int[][2]>);
378 }
379
380 // Function with unbound reference parameter.
381 // - Original paraemter is modified by callback.
382 TEST_F(BindTest, UnboundReferenceSupport) {
383 int n = 0;
384 Callback<void(int&)> unbound_ref_cb = Bind(&RefArgSet);
385 unbound_ref_cb.Run(n);
386 EXPECT_EQ(2, n);
387 }
388
354 // Functions that take reference parameters. 389 // Functions that take reference parameters.
355 // - Forced reference parameter type still stores a copy. 390 // - Forced reference parameter type still stores a copy.
356 // - Forced const reference parameter type still stores a copy. 391 // - Forced const reference parameter type still stores a copy.
357 TEST_F(BindTest, ReferenceArgumentBinding) { 392 TEST_F(BindTest, ReferenceArgumentBinding) {
358 int n = 1; 393 int n = 1;
359 int& ref_n = n; 394 int& ref_n = n;
360 const int& const_ref_n = n; 395 const int& const_ref_n = n;
361 396
362 Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n); 397 Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n);
363 EXPECT_EQ(n, ref_copies_cb.Run()); 398 EXPECT_EQ(n, ref_copies_cb.Run());
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 // ref_cb.Run(); 598 // ref_cb.Run();
564 599
565 // - A method should not be bindable with an array of objects. 600 // - A method should not be bindable with an array of objects.
566 // 601 //
567 // This is likely not wanted behavior. We specifically check for it though 602 // This is likely not wanted behavior. We specifically check for it though
568 // because it is possible, depending on how you implement prebinding, to 603 // because it is possible, depending on how you implement prebinding, to
569 // implicitly convert an array type to a pointer type. 604 // implicitly convert an array type to a pointer type.
570 // 605 //
571 // HasRef p[10]; 606 // HasRef p[10];
572 // Callback<void(void)> method_bound_to_array_cb = 607 // Callback<void(void)> method_bound_to_array_cb =
573 // Bind(&HasRef::VoidConstMethod0, p); 608 // Bind(&HasRef::VoidConstMethod0, p);
574 // method_bound_to_array_cb.Run(); 609 // method_bound_to_array_cb.Run();
575 610
576 // - Refcounted types should not be bound as a raw pointer. 611 // - Refcounted types should not be bound as a raw pointer.
577 // HasRef for_raw_ptr; 612 // HasRef for_raw_ptr;
578 // Callback<void(void)> ref_count_as_raw_ptr = 613 // Callback<void(void)> ref_count_as_raw_ptr =
579 // Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); 614 // Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
580 // ASSERT_EQ(&for_raw_ptr, ref_count_as_raw_ptr.Run());
581 615
582 } 616 }
583 617
584 #if defined(OS_WIN) 618 #if defined(OS_WIN)
585 int __fastcall FastCallFunc(int n) { 619 int __fastcall FastCallFunc(int n) {
586 return n; 620 return n;
587 } 621 }
588 622
589 int __stdcall StdCallFunc(int n) { 623 int __stdcall StdCallFunc(int n) {
590 return n; 624 return n;
591 } 625 }
592 626
593 // Windows specific calling convention support. 627 // Windows specific calling convention support.
594 // - Can bind a __fastcall function. 628 // - Can bind a __fastcall function.
595 // - Can bind a __stdcall function. 629 // - Can bind a __stdcall function.
596 TEST_F(BindTest, WindowsCallingConventions) { 630 TEST_F(BindTest, WindowsCallingConventions) {
597 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1); 631 Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1);
598 EXPECT_EQ(1, fastcall_cb.Run()); 632 EXPECT_EQ(1, fastcall_cb.Run());
599 633
600 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2); 634 Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2);
601 EXPECT_EQ(2, stdcall_cb.Run()); 635 EXPECT_EQ(2, stdcall_cb.Run());
602 } 636 }
603 #endif 637 #endif
604 638
605 } // namespace 639 } // namespace
606 } // namespace base 640 } // namespace base
OLDNEW
« no previous file with comments | « base/bind_internal_win.h.pump ('k') | base/callback.h » ('j') | base/callback_internal.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698