1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
// 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/bind.h"
#if defined(BASE_CALLBACK_H_)
// We explicitly do not want to include callback.h so people are not tempted
// to use bind.h in a headerfile for getting the Callback types.
#error "base/bind.h should avoid pulling in callback.h by default."
#endif
#include "base/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 this 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, bool(void));
private:
// Particularly important in this test to ensure no copies are made.
DISALLOW_COPY_AND_ASSIGN(HasRef);
};
static const int kParentValue = 1;
static const int kChildValue = 2;
class Parent {
public:
void AddRef(void) const {}
void Release(void) const {}
virtual void VirtualSet() { value = kParentValue; }
void NonVirtualSet() { value = kParentValue; }
int value;
};
class Child : public Parent {
public:
virtual void VirtualSet() { value = kChildValue; }
void NonVirtualSet() { value = kChildValue; }
};
class NoRefParent {
public:
virtual void VirtualSet() { value = kParentValue; }
void NonVirtualSet() { value = kParentValue; }
int value;
};
class NoRefChild : public NoRefParent {
virtual void VirtualSet() { value = kChildValue; }
void NonVirtualSet() { value = kChildValue; }
};
// 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;
}
int copies() const {
return *copies_;
}
int assigns() const {
return *assigns_;
}
private:
int* copies_;
int* assigns_;
};
// Some test functions that we can Bind to.
template <typename T>
T PolymorphicIdentity(T t) {
return t;
}
template <typename T>
void VoidPolymorphic1(T t) {
}
int Identity(int n) {
return n;
}
int ArrayGet(const int array[], int n) {
return array[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 GetCopies(const CopyCounter& counter) {
return counter.copies();
}
int UnwrapNoRefParent(NoRefParent p) {
return p.value;
}
int UnwrapNoRefParentPtr(NoRefParent* p) {
return p->value;
}
int UnwrapNoRefParentConstRef(const NoRefParent& p) {
return p.value;
}
// Only useful in no-compile tests.
int UnwrapNoRefParentRef(Parent& p) {
return p.value;
}
class BindTest : public ::testing::Test {
public:
BindTest() {
const_has_ref_ptr_ = &has_ref_;
const_no_ref_ptr_ = &no_ref_;
static_func_mock_ptr = &static_func_mock_;
}
virtual ~BindTest() {
}
static void VoidFunc0(void) {
static_func_mock_ptr->VoidMethod0();
}
static int IntFunc0(void) { return static_func_mock_ptr->IntMethod0(); }
protected:
StrictMock<NoRef> no_ref_;
StrictMock<HasRef> has_ref_;
const HasRef* const_has_ref_ptr_;
const NoRef* const_no_ref_ptr_;
StrictMock<NoRef> static_func_mock_;
// Used by the static functions to perform expectations.
static StrictMock<NoRef>* static_func_mock_ptr;
private:
DISALLOW_COPY_AND_ASSIGN(BindTest);
};
StrictMock<NoRef>* BindTest::static_func_mock_ptr;
// Sanity check that we can instantiate a callback for each arity.
TEST_F(BindTest, ArityTest) {
Callback<int(void)> c0 = Bind(&Sum, 32, 16, 8, 4, 2, 1);
EXPECT_EQ(63, c0.Run());
Callback<int(int)> c1 = Bind(&Sum, 32, 16, 8, 4, 2);
EXPECT_EQ(75, c1.Run(13));
Callback<int(int,int)> c2 = Bind(&Sum, 32, 16, 8, 4);
EXPECT_EQ(85, c2.Run(13, 12));
Callback<int(int,int,int)> c3 = Bind(&Sum, 32, 16, 8);
EXPECT_EQ(92, c3.Run(13, 12, 11));
Callback<int(int,int,int,int)> c4 = Bind(&Sum, 32, 16);
EXPECT_EQ(94, c4.Run(13, 12, 11, 10));
Callback<int(int,int,int,int,int)> c5 = Bind(&Sum, 32);
EXPECT_EQ(87, c5.Run(13, 12, 11, 10, 9));
Callback<int(int,int,int,int,int,int)> c6 = Bind(&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(BindTest, 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 normal_cb = Bind(&VoidFunc0);
Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0,
&has_ref_);
Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
const_has_ref_ptr_);
normal_cb.Run();
method_cb.Run();
const_method_nonconst_obj_cb.Run();
const_method_const_obj_cb.Run();
Child child;
child.value = 0;
Closure virtual_set_cb = Bind(&Parent::VirtualSet, &child);
virtual_set_cb.Run();
EXPECT_EQ(kChildValue, child.value);
child.value = 0;
Closure non_virtual_set_cb = Bind(&Parent::NonVirtualSet, &child);
non_virtual_set_cb.Run();
EXPECT_EQ(kParentValue, child.value);
}
// Return value support.
// - Function with return value.
// - Method with return value.
// - Const method with return value.
TEST_F(BindTest, 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)> normal_cb = Bind(&IntFunc0);
Callback<int(void)> method_cb = Bind(&HasRef::IntMethod0, &has_ref_);
Callback<int(void)> const_method_nonconst_obj_cb =
Bind(&HasRef::IntConstMethod0, &has_ref_);
Callback<int(void)> const_method_const_obj_cb =
Bind(&HasRef::IntConstMethod0, const_has_ref_ptr_);
EXPECT_EQ(1337, normal_cb.Run());
EXPECT_EQ(31337, method_cb.Run());
EXPECT_EQ(41337, const_method_nonconst_obj_cb.Run());
EXPECT_EQ(51337, const_method_const_obj_cb.Run());
}
// Argument binding tests.
// - Argument binding to primitive.
// - Argument binding to primitive pointer.
// - 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(BindTest, ArgumentBinding) {
int n = 2;
Callback<int(void)> bind_primitive_cb = Bind(&Identity, n);
EXPECT_EQ(n, bind_primitive_cb.Run());
Callback<int*(void)> bind_primitive_pointer_cb =
Bind(&PolymorphicIdentity<int*>, &n);
EXPECT_EQ(&n, bind_primitive_pointer_cb.Run());
Callback<int(void)> bind_int_literal_cb = Bind(&Identity, 3);
EXPECT_EQ(3, bind_int_literal_cb.Run());
Callback<const char*(void)> bind_string_literal_cb =
Bind(&CStringIdentity, "hi");
EXPECT_STREQ("hi", bind_string_literal_cb.Run());
Callback<int(void)> bind_template_function_cb =
Bind(&PolymorphicIdentity<int>, 4);
EXPECT_EQ(4, bind_template_function_cb.Run());
NoRefParent p;
p.value = 5;
Callback<int(void)> bind_object_cb = Bind(&UnwrapNoRefParent, p);
EXPECT_EQ(5, bind_object_cb.Run());
NoRefChild c;
c.value = 6;
Callback<int(void)> bind_promotes_cb = Bind(&UnwrapNoRefParent, c);
EXPECT_EQ(6, bind_promotes_cb.Run());
c.value = 7;
Callback<int(void)> bind_pointer_promotes_cb =
Bind(&UnwrapNoRefParentPtr, &c);
EXPECT_EQ(7, bind_pointer_promotes_cb.Run());
c.value = 8;
Callback<int(void)> bind_const_reference_promotes_cb =
Bind(&UnwrapNoRefParentConstRef, c);
EXPECT_EQ(8, bind_const_reference_promotes_cb.Run());
}
// Functions that take reference parameters.
// - Forced reference parameter type still stores a copy.
// - Forced const reference parameter type still stores a copy.
TEST_F(BindTest, ReferenceArgumentBinding) {
int n = 1;
int& ref_n = n;
const int& const_ref_n = n;
Callback<int(void)> ref_copies_cb = Bind(&Identity, ref_n);
EXPECT_EQ(n, ref_copies_cb.Run());
n++;
EXPECT_EQ(n - 1, ref_copies_cb.Run());
Callback<int(void)> const_ref_copies_cb = Bind(&Identity, const_ref_n);
EXPECT_EQ(n, const_ref_copies_cb.Run());
n++;
EXPECT_EQ(n - 1, const_ref_copies_cb.Run());
}
// Check that we can pass in arrays and have them be stored as a pointer.
// - Array of values stores a pointer.
// - Array of const values stores a pointer.
TEST_F(BindTest, ArrayArgumentBinding) {
int array[4] = {1, 1, 1, 1};
const int (*const_array_ptr)[4] = &array;
Callback<int(void)> array_cb = Bind(&ArrayGet, array, 1);
EXPECT_EQ(1, array_cb.Run());
Callback<int(void)> const_array_cb = Bind(&ArrayGet, *const_array_ptr, 1);
EXPECT_EQ(1, const_array_cb.Run());
array[1] = 3;
EXPECT_EQ(3, array_cb.Run());
EXPECT_EQ(3, const_array_cb.Run());
}
// Verify SupportsAddRefAndRelease correctly introspects the class type for
// AddRef() and Release().
TEST_F(BindTest, SupportsAddRefAndRelease) {
EXPECT_TRUE(internal::SupportsAddRefAndRelease<HasRef>::value);
EXPECT_FALSE(internal::SupportsAddRefAndRelease<NoRef>::value);
// StrictMock<T> is a derived class of T. So, we use StrictMock<HasRef> and
// StrictMock<NoRef> to test that SupportsAddRefAndRelease works over
// inheritance.
EXPECT_TRUE(internal::SupportsAddRefAndRelease<StrictMock<HasRef> >::value);
EXPECT_FALSE(internal::SupportsAddRefAndRelease<StrictMock<NoRef> >::value);
}
// Unretained() wrapper support.
// - Method bound to Unretained() non-object.
// - Const method bound to Unretained() non-const object.
// - Const method bound to Unretained() const object.
TEST_F(BindTest, Unretained) {
EXPECT_CALL(no_ref_, VoidMethod0());
EXPECT_CALL(no_ref_, VoidConstMethod0()).Times(2);
Callback<void(void)> method_cb =
Bind(&NoRef::VoidMethod0, Unretained(&no_ref_));
method_cb.Run();
Callback<void(void)> const_method_cb =
Bind(&NoRef::VoidConstMethod0, Unretained(&no_ref_));
const_method_cb.Run();
Callback<void(void)> const_method_const_ptr_cb =
Bind(&NoRef::VoidConstMethod0, Unretained(const_no_ref_ptr_));
const_method_const_ptr_cb.Run();
}
// ConstRef() wrapper support.
// - Binding w/o ConstRef takes a copy.
// - Binding a ConstRef takes a reference.
// - Binding ConstRef to a function ConstRef does not copy on invoke.
TEST_F(BindTest, ConstRef) {
int n = 1;
Callback<int(void)> copy_cb = Bind(&Identity, n);
Callback<int(void)> const_ref_cb = Bind(&Identity, ConstRef(n));
EXPECT_EQ(n, copy_cb.Run());
EXPECT_EQ(n, const_ref_cb.Run());
n++;
EXPECT_EQ(n - 1, copy_cb.Run());
EXPECT_EQ(n, const_ref_cb.Run());
int copies = 0;
int assigns = 0;
CopyCounter counter(&copies, &assigns);
Callback<int(void)> all_const_ref_cb =
Bind(&GetCopies, ConstRef(counter));
EXPECT_EQ(0, all_const_ref_cb.Run());
EXPECT_EQ(0, copies);
EXPECT_EQ(0, assigns);
}
// Argument Copy-constructor usage for non-reference parameters.
// - Bound 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(BindTest, ArgumentCopies) {
int copies = 0;
int assigns = 0;
CopyCounter counter(&copies, &assigns);
Callback<void(void)> copy_cb =
Bind(&VoidPolymorphic1<CopyCounter>, counter);
EXPECT_GE(1, copies);
EXPECT_EQ(0, assigns);
copies = 0;
assigns = 0;
Callback<void(CopyCounter)> forward_cb =
Bind(&VoidPolymorphic1<CopyCounter>);
forward_cb.Run(counter);
EXPECT_GE(1, copies);
EXPECT_EQ(0, assigns);
copies = 0;
assigns = 0;
DerivedCopyCounter dervied(&copies, &assigns);
Callback<void(CopyCounter)> coerce_cb =
Bind(&VoidPolymorphic1<CopyCounter>);
coerce_cb.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?
// 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(BindTest, NoCompile) {
// - Method bound to const-object.
//
// Only const methods should be allowed to work with const objects.
//
// Callback<void(void)> method_to_const_cb =
// Bind(&HasRef::VoidMethod0, const_has_ref_ptr_);
// method_to_const_cb.Run();
// - Method bound to non-refcounted object.
// - Const Method bound to non-refcounted object.
//
// We require refcounts unless you have Unretained().
//
// Callback<void(void)> no_ref_cb =
// Bind(&NoRef::VoidMethod0, &no_ref_);
// no_ref_cb.Run();
// Callback<void(void)> no_ref_const_cb =
// Bind(&NoRef::VoidConstMethod0, &no_ref_);
// no_ref_const_cb.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)> unretained_cb =
// Bind(&HasRef::VoidConstMethod0, Unretained(&has_ref_));
// unretained_cb.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)> pointer_same_cb =
// Bind(&PolymorphicIdentity<Parent*>, const_parent_ptr);
// pointer_same_cb.Run();
// Callback<Parent*(void)> pointer_super_cb =
// Bind(&PolymorphicIdentity<Parent*>, const_child_ptr);
// pointer_super_cb.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 = Bind(&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 = Bind(&Identity, 1);
// Callback<void(void)> cb_b1;
// cb_a1 = cb_b1;
// - Functions with reference parameters, unsupported.
//
// First, non-const reference parameters are disallowed by the Google
// style guide. Seconds, 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, or a copy.
//
// NoRefParent p;
// Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapNoRefParentRef);
// ref_arg_cb.Run(p);
// Callback<int(void)> ref_cb = Bind(&UnwrapNoRefParentRef, p);
// ref_cb.Run();
// - A method should not be bindable with an array of objects.
//
// This is likely not wanted behavior. We specifically check for it though
// because it is possible, depending on how you implement prebinding, to
// implicitly convert an array type to a pointer type.
//
// HasRef p[10];
// Callback<void(void)> method_bound_to_array_cb =
// Bind(&HasRef::VoidConstMethod0, p);
// method_bound_to_array_cb.Run();
// - Refcounted types should not be bound as a raw pointer.
// HasRef for_raw_ptr;
// Callback<void(void)> ref_count_as_raw_ptr =
// Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
// ASSERT_EQ(&for_raw_ptr, ref_count_as_raw_ptr.Run());
}
#if defined(OS_WIN)
int __fastcall FastCallFunc(int n) {
return n;
}
int __stdcall StdCallFunc(int n) {
return n;
}
// Windows specific calling convention support.
// - Can bind a __fastcall function.
// - Can bind a __stdcall function.
TEST_F(BindTest, WindowsCallingConventions) {
Callback<int(void)> fastcall_cb = Bind(&FastCallFunc, 1);
EXPECT_EQ(1, fastcall_cb.Run());
Callback<int(void)> stdcall_cb = Bind(&StdCallFunc, 2);
EXPECT_EQ(2, stdcall_cb.Run());
}
#endif
} // namespace
} // namespace base
|