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

Side by Side Diff: third_party/WebKit/Source/wtf/FunctionalTest.cpp

Issue 1919723002: Introduce WTF::unretained() and WTF::crossThreadUnretained() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@Kuroneko_4b_ACTA_WeakPtr
Patch Set: Rebase. Created 4 years, 5 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
« no previous file with comments | « third_party/WebKit/Source/wtf/Functional.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved. 2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 bind(sixArgFunc, 3, 3.5); 185 bind(sixArgFunc, 3, 3.5);
186 (*twoBound)('d', &a, &b, &c); 186 (*twoBound)('d', &a, &b, &c);
187 assertArgs(a, b, c, 3, 3.5, 'd'); 187 assertArgs(a, b, c, 3, 3.5, 'd');
188 188
189 std::unique_ptr<Function<void(int*, double*, char*)>> threeBound = 189 std::unique_ptr<Function<void(int*, double*, char*)>> threeBound =
190 bind(sixArgFunc, 4, 4.5, 'e'); 190 bind(sixArgFunc, 4, 4.5, 'e');
191 (*threeBound)(&a, &b, &c); 191 (*threeBound)(&a, &b, &c);
192 assertArgs(a, b, c, 4, 4.5, 'e'); 192 assertArgs(a, b, c, 4, 4.5, 'e');
193 193
194 std::unique_ptr<Function<void(double*, char*)>> fourBound = 194 std::unique_ptr<Function<void(double*, char*)>> fourBound =
195 bind(sixArgFunc, 5, 5.5, 'f', &a); 195 bind(sixArgFunc, 5, 5.5, 'f', unretained(&a));
196 (*fourBound)(&b, &c); 196 (*fourBound)(&b, &c);
197 assertArgs(a, b, c, 5, 5.5, 'f'); 197 assertArgs(a, b, c, 5, 5.5, 'f');
198 198
199 std::unique_ptr<Function<void(char*)>> fiveBound = 199 std::unique_ptr<Function<void(char*)>> fiveBound =
200 bind(sixArgFunc, 6, 6.5, 'g', &a, &b); 200 bind(sixArgFunc, 6, 6.5, 'g', unretained(&a), unretained(&b));
201 (*fiveBound)(&c); 201 (*fiveBound)(&c);
202 assertArgs(a, b, c, 6, 6.5, 'g'); 202 assertArgs(a, b, c, 6, 6.5, 'g');
203 203
204 std::unique_ptr<Function<void()>> sixBound = 204 std::unique_ptr<Function<void()>> sixBound =
205 bind(sixArgFunc, 7, 7.5, 'h', &a, &b, &c); 205 bind(sixArgFunc, 7, 7.5, 'h', unretained(&a), unretained(&b), unretained (&c));
206 (*sixBound)(); 206 (*sixBound)();
207 assertArgs(a, b, c, 7, 7.5, 'h'); 207 assertArgs(a, b, c, 7, 7.5, 'h');
208 } 208 }
209 209
210 class A { 210 class A {
211 public: 211 public:
212 explicit A(int i) 212 explicit A(int i)
213 : m_i(i) 213 : m_i(i)
214 { 214 {
215 } 215 }
(...skipping 14 matching lines...) Expand all
230 } 230 }
231 231
232 int f() { return A::f() + 1; } 232 int f() { return A::f() + 1; }
233 int addF(int j) { return A::addF(j) + 1; } 233 int addF(int j) { return A::addF(j) + 1; }
234 int overridden() override { return 43; } 234 int overridden() override { return 43; }
235 }; 235 };
236 236
237 TEST(FunctionalTest, MemberFunctionBind) 237 TEST(FunctionalTest, MemberFunctionBind)
238 { 238 {
239 A a(10); 239 A a(10);
240 std::unique_ptr<Function<int()>> function1 = bind(&A::f, &a); 240 std::unique_ptr<Function<int()>> function1 = bind(&A::f, unretained(&a));
241 EXPECT_EQ(10, (*function1)()); 241 EXPECT_EQ(10, (*function1)());
242 242
243 std::unique_ptr<Function<int()>> function2 = bind(&A::addF, &a, 15); 243 std::unique_ptr<Function<int()>> function2 = bind(&A::addF, unretained(&a), 15);
244 EXPECT_EQ(25, (*function2)()); 244 EXPECT_EQ(25, (*function2)());
245 245
246 std::unique_ptr<Function<int()>> function3 = bind(&A::overridden, &a); 246 std::unique_ptr<Function<int()>> function3 = bind(&A::overridden, unretained (&a));
247 EXPECT_EQ(42, (*function3)()); 247 EXPECT_EQ(42, (*function3)());
248 } 248 }
249 249
250 TEST(FunctionalTest, MemberFunctionBindWithSubclassPointer) 250 TEST(FunctionalTest, MemberFunctionBindWithSubclassPointer)
251 { 251 {
252 B b(10); 252 B b(10);
253 std::unique_ptr<Function<int()>> function1 = bind(&A::f, &b); 253 std::unique_ptr<Function<int()>> function1 = bind(&A::f, unretained(&b));
254 EXPECT_EQ(10, (*function1)()); 254 EXPECT_EQ(10, (*function1)());
255 255
256 std::unique_ptr<Function<int()>> function2 = bind(&A::addF, &b, 15); 256 std::unique_ptr<Function<int()>> function2 = bind(&A::addF, unretained(&b), 15);
257 EXPECT_EQ(25, (*function2)()); 257 EXPECT_EQ(25, (*function2)());
258 258
259 std::unique_ptr<Function<int()>> function3 = bind(&A::overridden, &b); 259 std::unique_ptr<Function<int()>> function3 = bind(&A::overridden, unretained (&b));
260 EXPECT_EQ(43, (*function3)()); 260 EXPECT_EQ(43, (*function3)());
261 261
262 std::unique_ptr<Function<int()>> function4 = bind(&B::f, &b); 262 std::unique_ptr<Function<int()>> function4 = bind(&B::f, unretained(&b));
263 EXPECT_EQ(11, (*function4)()); 263 EXPECT_EQ(11, (*function4)());
264 264
265 std::unique_ptr<Function<int()>> function5 = bind(&B::addF, &b, 15); 265 std::unique_ptr<Function<int()>> function5 = bind(&B::addF, unretained(&b), 15);
266 EXPECT_EQ(26, (*function5)()); 266 EXPECT_EQ(26, (*function5)());
267 267
268 } 268 }
269 269
270 TEST(FunctionalTest, MemberFunctionBindWithSubclassPointerWithCast) 270 TEST(FunctionalTest, MemberFunctionBindWithSubclassPointerWithCast)
271 { 271 {
272 B b(10); 272 B b(10);
273 std::unique_ptr<Function<int()>> function1 = bind(&A::f, static_cast<A*>(&b) ); 273 std::unique_ptr<Function<int()>> function1 = bind(&A::f, unretained(static_c ast<A*>(&b)));
274 EXPECT_EQ(10, (*function1)()); 274 EXPECT_EQ(10, (*function1)());
275 275
276 std::unique_ptr<Function<int()>> function2 = bind(&A::addF, static_cast<A*>( &b), 15); 276 std::unique_ptr<Function<int()>> function2 = bind(&A::addF, unretained(stati c_cast<A*>(&b)), 15);
277 EXPECT_EQ(25, (*function2)()); 277 EXPECT_EQ(25, (*function2)());
278 278
279 std::unique_ptr<Function<int()>> function3 = bind(&A::overridden, static_cas t<A*>(&b)); 279 std::unique_ptr<Function<int()>> function3 = bind(&A::overridden, unretained (static_cast<A*>(&b)));
280 EXPECT_EQ(43, (*function3)()); 280 EXPECT_EQ(43, (*function3)());
281 } 281 }
282 282
283 TEST(FunctionalTest, MemberFunctionPartBind) 283 TEST(FunctionalTest, MemberFunctionPartBind)
284 { 284 {
285 A a(10); 285 A a(10);
286 std::unique_ptr<Function<int(class A*)>> function1 = bind(&A::f); 286 std::unique_ptr<Function<int(class A*)>> function1 = bind(&A::f);
287 EXPECT_EQ(10, (*function1)(&a)); 287 EXPECT_EQ(10, (*function1)(&a));
288 288
289 std::unique_ptr<Function<int(class A*, int)>> unboundFunction2 = 289 std::unique_ptr<Function<int(class A*, int)>> unboundFunction2 =
290 bind(&A::addF); 290 bind(&A::addF);
291 EXPECT_EQ(25, (*unboundFunction2)(&a, 15)); 291 EXPECT_EQ(25, (*unboundFunction2)(&a, 15));
292 std::unique_ptr<Function<int(int)>> objectBoundFunction2 = 292 std::unique_ptr<Function<int(int)>> objectBoundFunction2 =
293 bind(&A::addF, &a); 293 bind(&A::addF, unretained(&a));
294 EXPECT_EQ(25, (*objectBoundFunction2)(15)); 294 EXPECT_EQ(25, (*objectBoundFunction2)(15));
295 } 295 }
296 296
297 TEST(FunctionalTest, MemberFunctionBindByUniquePtr) 297 TEST(FunctionalTest, MemberFunctionBindByUniquePtr)
298 { 298 {
299 std::unique_ptr<Function<int()>> function1 = WTF::bind(&A::f, wrapUnique(new A(10))); 299 std::unique_ptr<Function<int()>> function1 = WTF::bind(&A::f, wrapUnique(new A(10)));
300 EXPECT_EQ(10, (*function1)()); 300 EXPECT_EQ(10, (*function1)());
301 } 301 }
302 302
303 TEST(FunctionalTest, MemberFunctionBindByPassedUniquePtr) 303 TEST(FunctionalTest, MemberFunctionBindByPassedUniquePtr)
(...skipping 26 matching lines...) Expand all
330 }; 330 };
331 331
332 int multiplyNumberByTwo(Number* number) 332 int multiplyNumberByTwo(Number* number)
333 { 333 {
334 return number->value() * 2; 334 return number->value() * 2;
335 } 335 }
336 336
337 TEST(FunctionalTest, RefCountedStorage) 337 TEST(FunctionalTest, RefCountedStorage)
338 { 338 {
339 RefPtr<Number> five = Number::create(5); 339 RefPtr<Number> five = Number::create(5);
340 EXPECT_EQ(1, five->refCount());
340 std::unique_ptr<Function<int()>> multiplyFiveByTwoFunction = bind(multiplyNu mberByTwo, five); 341 std::unique_ptr<Function<int()>> multiplyFiveByTwoFunction = bind(multiplyNu mberByTwo, five);
342 EXPECT_EQ(2, five->refCount());
341 EXPECT_EQ(10, (*multiplyFiveByTwoFunction)()); 343 EXPECT_EQ(10, (*multiplyFiveByTwoFunction)());
344 EXPECT_EQ(2, five->refCount());
342 345
343 std::unique_ptr<Function<int()>> multiplyFourByTwoFunction = bind(multiplyNu mberByTwo, Number::create(4)); 346 std::unique_ptr<Function<int()>> multiplyFourByTwoFunction = bind(multiplyNu mberByTwo, Number::create(4));
344 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); 347 EXPECT_EQ(8, (*multiplyFourByTwoFunction)());
345 348
346 RefPtr<Number> six = Number::create(6); 349 RefPtr<Number> six = Number::create(6);
347 std::unique_ptr<Function<int()>> multiplySixByTwoFunction = bind(multiplyNum berByTwo, six.release()); 350 std::unique_ptr<Function<int()>> multiplySixByTwoFunction = bind(multiplyNum berByTwo, six.release());
348 EXPECT_FALSE(six); 351 EXPECT_FALSE(six);
349 EXPECT_EQ(12, (*multiplySixByTwoFunction)()); 352 EXPECT_EQ(12, (*multiplySixByTwoFunction)());
350 } 353 }
351 354
355 TEST(FunctionalTest, UnretainedWithRefCounted)
356 {
357 RefPtr<Number> five = Number::create(5);
358 EXPECT_EQ(1, five->refCount());
359 std::unique_ptr<Function<int()>> multiplyFiveByTwoFunction = bind(multiplyNu mberByTwo, unretained(five.get()));
360 EXPECT_EQ(1, five->refCount());
361 EXPECT_EQ(10, (*multiplyFiveByTwoFunction)());
362 EXPECT_EQ(1, five->refCount());
363 }
364
352 int processUnwrappedClass(const UnwrappedClass& u, int v) 365 int processUnwrappedClass(const UnwrappedClass& u, int v)
353 { 366 {
354 return u.value() * v; 367 return u.value() * v;
355 } 368 }
356 369
357 // Tests that: 370 // Tests that:
358 // - ParamStorageTraits's wrap()/unwrap() are called, and 371 // - ParamStorageTraits's wrap()/unwrap() are called, and
359 // - bind()'s arguments are passed as references to ParamStorageTraits::wrap() 372 // - bind()'s arguments are passed as references to ParamStorageTraits::wrap()
360 // with no additional copies. 373 // with no additional copies.
361 // This test would fail in compile if something is wrong, 374 // This test would fail in compile if something is wrong,
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 EXPECT_EQ(0, (*bound1)(CountCopy())); 537 EXPECT_EQ(0, (*bound1)(CountCopy()));
525 538
526 std::unique_ptr<Function<int(CountCopy)>> bound2 = bind(takeCountCopyAsValue ); 539 std::unique_ptr<Function<int(CountCopy)>> bound2 = bind(takeCountCopyAsValue );
527 EXPECT_EQ(2, (*bound2)(lvalue)); // At PartBoundFunctionImpl::operator() and at the destination function. 540 EXPECT_EQ(2, (*bound2)(lvalue)); // At PartBoundFunctionImpl::operator() and at the destination function.
528 EXPECT_LE((*bound2)(CountCopy()), 2); // Compiler is allowed to optimize one copy away if the argument is rvalue. 541 EXPECT_LE((*bound2)(CountCopy()), 2); // Compiler is allowed to optimize one copy away if the argument is rvalue.
529 } 542 }
530 543
531 } // anonymous namespace 544 } // anonymous namespace
532 545
533 } // namespace WTF 546 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/Functional.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698