OLD | NEW |
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 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 int tripleMoveOnlysByValues(MoveOnly first, MoveOnly second, MoveOnly third) { | 471 int tripleMoveOnlysByValues(MoveOnly first, MoveOnly second, MoveOnly third) { |
472 return first.value() + second.value() + third.value(); | 472 return first.value() + second.value() + third.value(); |
473 } | 473 } |
474 | 474 |
475 TEST(FunctionalTest, BindMoveOnlyObjects) { | 475 TEST(FunctionalTest, BindMoveOnlyObjects) { |
476 MoveOnly one(1); | 476 MoveOnly one(1); |
477 std::unique_ptr<Function<int()>> bound = | 477 std::unique_ptr<Function<int()>> bound = |
478 bind(singleMoveOnlyByRvalueReference, passed(std::move(one))); | 478 bind(singleMoveOnlyByRvalueReference, passed(std::move(one))); |
479 EXPECT_EQ(0, one.value()); // Should be moved away. | 479 EXPECT_EQ(0, one.value()); // Should be moved away. |
480 EXPECT_EQ(1, (*bound)()); | 480 EXPECT_EQ(1, (*bound)()); |
481 EXPECT_EQ( | 481 // The stored value must be cleared in the first function call. |
482 0, | 482 EXPECT_EQ(0, (*bound)()); |
483 (*bound)()); // The stored value must be cleared in the first function ca
ll. | |
484 | 483 |
485 bound = bind(singleMoveOnlyByValue, passed(MoveOnly(1))); | 484 bound = bind(singleMoveOnlyByValue, passed(MoveOnly(1))); |
486 EXPECT_EQ(1, (*bound)()); | 485 EXPECT_EQ(1, (*bound)()); |
487 EXPECT_EQ(0, (*bound)()); | 486 EXPECT_EQ(0, (*bound)()); |
488 | 487 |
489 bound = bind(tripleMoveOnlysByRvalueReferences, passed(MoveOnly(1)), | 488 bound = bind(tripleMoveOnlysByRvalueReferences, passed(MoveOnly(1)), |
490 passed(MoveOnly(2)), passed(MoveOnly(3))); | 489 passed(MoveOnly(2)), passed(MoveOnly(3))); |
491 EXPECT_EQ(6, (*bound)()); | 490 EXPECT_EQ(6, (*bound)()); |
492 EXPECT_EQ(0, (*bound)()); | 491 EXPECT_EQ(0, (*bound)()); |
493 | 492 |
(...skipping 29 matching lines...) Expand all Loading... |
523 TEST(FunctionalTest, CountCopiesOfBoundArguments) { | 522 TEST(FunctionalTest, CountCopiesOfBoundArguments) { |
524 CountCopy lvalue; | 523 CountCopy lvalue; |
525 std::unique_ptr<Function<int()>> bound = | 524 std::unique_ptr<Function<int()>> bound = |
526 bind(takeCountCopyAsConstReference, lvalue); | 525 bind(takeCountCopyAsConstReference, lvalue); |
527 EXPECT_EQ(2, (*bound)()); // wrapping and unwrapping. | 526 EXPECT_EQ(2, (*bound)()); // wrapping and unwrapping. |
528 | 527 |
529 bound = bind(takeCountCopyAsConstReference, CountCopy()); // Rvalue. | 528 bound = bind(takeCountCopyAsConstReference, CountCopy()); // Rvalue. |
530 EXPECT_EQ(2, (*bound)()); | 529 EXPECT_EQ(2, (*bound)()); |
531 | 530 |
532 bound = bind(takeCountCopyAsValue, lvalue); | 531 bound = bind(takeCountCopyAsValue, lvalue); |
533 EXPECT_EQ( | 532 // wrapping, unwrapping and copying in the final function argument. |
534 3, | 533 EXPECT_EQ(3, (*bound)()); |
535 (*bound)()); // wrapping, unwrapping and copying in the final function ar
gument. | |
536 | 534 |
537 bound = bind(takeCountCopyAsValue, CountCopy()); | 535 bound = bind(takeCountCopyAsValue, CountCopy()); |
538 EXPECT_EQ(3, (*bound)()); | 536 EXPECT_EQ(3, (*bound)()); |
539 } | 537 } |
540 | 538 |
541 TEST(FunctionalTest, MoveUnboundArgumentsByRvalueReference) { | 539 TEST(FunctionalTest, MoveUnboundArgumentsByRvalueReference) { |
542 std::unique_ptr<Function<int(MoveOnly &&)>> boundSingle = | 540 std::unique_ptr<Function<int(MoveOnly &&)>> boundSingle = |
543 bind(singleMoveOnlyByRvalueReference); | 541 bind(singleMoveOnlyByRvalueReference); |
544 EXPECT_EQ(1, (*boundSingle)(MoveOnly(1))); | 542 EXPECT_EQ(1, (*boundSingle)(MoveOnly(1))); |
545 EXPECT_EQ(42, (*boundSingle)(MoveOnly(42))); | 543 EXPECT_EQ(42, (*boundSingle)(MoveOnly(42))); |
(...skipping 13 matching lines...) Expand all Loading... |
559 } | 557 } |
560 | 558 |
561 TEST(FunctionalTest, CountCopiesOfUnboundArguments) { | 559 TEST(FunctionalTest, CountCopiesOfUnboundArguments) { |
562 CountCopy lvalue; | 560 CountCopy lvalue; |
563 std::unique_ptr<Function<int(const CountCopy&)>> bound1 = | 561 std::unique_ptr<Function<int(const CountCopy&)>> bound1 = |
564 bind(takeCountCopyAsConstReference); | 562 bind(takeCountCopyAsConstReference); |
565 EXPECT_EQ(0, (*bound1)(lvalue)); // No copies! | 563 EXPECT_EQ(0, (*bound1)(lvalue)); // No copies! |
566 EXPECT_EQ(0, (*bound1)(CountCopy())); | 564 EXPECT_EQ(0, (*bound1)(CountCopy())); |
567 | 565 |
568 std::unique_ptr<Function<int(CountCopy)>> bound2 = bind(takeCountCopyAsValue); | 566 std::unique_ptr<Function<int(CountCopy)>> bound2 = bind(takeCountCopyAsValue); |
569 EXPECT_EQ( | 567 // At Function::operator(), at Callback::Run() and at the destination |
570 3, | 568 // function. |
571 (*bound2)( | 569 EXPECT_EQ(3, (*bound2)(lvalue)); |
572 lvalue)); // At Function::operator(), at Callback::Run() and at the d
estination function. | 570 // Compiler is allowed to optimize one copy away if the argument is rvalue. |
573 EXPECT_LE( | 571 EXPECT_LE((*bound2)(CountCopy()), 2); |
574 (*bound2)(CountCopy()), | |
575 2); // Compiler is allowed to optimize one copy away if the argument is r
value. | |
576 } | 572 } |
577 | 573 |
578 TEST(FunctionalTest, WeakPtr) { | 574 TEST(FunctionalTest, WeakPtr) { |
579 HasWeakPtrSupport obj; | 575 HasWeakPtrSupport obj; |
580 int counter = 0; | 576 int counter = 0; |
581 std::unique_ptr<WTF::Closure> bound = | 577 std::unique_ptr<WTF::Closure> bound = |
582 WTF::bind(&HasWeakPtrSupport::increment, obj.createWeakPtr(), | 578 WTF::bind(&HasWeakPtrSupport::increment, obj.createWeakPtr(), |
583 WTF::unretained(&counter)); | 579 WTF::unretained(&counter)); |
584 | 580 |
585 (*bound)(); | 581 (*bound)(); |
586 EXPECT_FALSE(bound->isCancelled()); | 582 EXPECT_FALSE(bound->isCancelled()); |
587 EXPECT_EQ(1, counter); | 583 EXPECT_EQ(1, counter); |
588 | 584 |
589 obj.revokeAll(); | 585 obj.revokeAll(); |
590 EXPECT_TRUE(bound->isCancelled()); | 586 EXPECT_TRUE(bound->isCancelled()); |
591 (*bound)(); | 587 (*bound)(); |
592 EXPECT_EQ(1, counter); | 588 EXPECT_EQ(1, counter); |
593 } | 589 } |
594 | 590 |
595 } // anonymous namespace | 591 } // anonymous namespace |
596 | 592 |
597 } // namespace WTF | 593 } // namespace WTF |
OLD | NEW |