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 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 | 524 |
525 int takeCountCopyAsValue(CountCopy counter) | 525 int takeCountCopyAsValue(CountCopy counter) |
526 { | 526 { |
527 return counter.copies(); | 527 return counter.copies(); |
528 } | 528 } |
529 | 529 |
530 TEST(FunctionalTest, CountCopiesOfBoundArguments) | 530 TEST(FunctionalTest, CountCopiesOfBoundArguments) |
531 { | 531 { |
532 CountCopy lvalue; | 532 CountCopy lvalue; |
533 std::unique_ptr<Function<int()>> bound = bind(takeCountCopyAsConstReference,
lvalue); | 533 std::unique_ptr<Function<int()>> bound = bind(takeCountCopyAsConstReference,
lvalue); |
534 EXPECT_EQ(1, (*bound)()); // unwrapping. | 534 EXPECT_EQ(2, (*bound)()); // wrapping and unwrapping. |
535 | 535 |
536 bound = bind(takeCountCopyAsConstReference, CountCopy()); // Rvalue. | 536 bound = bind(takeCountCopyAsConstReference, CountCopy()); // Rvalue. |
537 EXPECT_EQ(1, (*bound)()); | 537 EXPECT_EQ(2, (*bound)()); |
538 | 538 |
539 bound = bind(takeCountCopyAsValue, lvalue); | 539 bound = bind(takeCountCopyAsValue, lvalue); |
540 EXPECT_EQ(2, (*bound)()); // unwrapping and copying in the final function ar
gument. | 540 EXPECT_EQ(3, (*bound)()); // wrapping, unwrapping and copying in the final f
unction argument. |
541 | 541 |
542 bound = bind(takeCountCopyAsValue, CountCopy()); | 542 bound = bind(takeCountCopyAsValue, CountCopy()); |
543 EXPECT_EQ(2, (*bound)()); | 543 EXPECT_EQ(3, (*bound)()); |
544 } | 544 } |
545 | 545 |
546 TEST(FunctionalTest, MoveUnboundArgumentsByRvalueReference) | 546 TEST(FunctionalTest, MoveUnboundArgumentsByRvalueReference) |
547 { | 547 { |
548 std::unique_ptr<Function<int(MoveOnly&&)>> boundSingle = bind(singleMoveOnly
ByRvalueReference); | 548 std::unique_ptr<Function<int(MoveOnly&&)>> boundSingle = bind(singleMoveOnly
ByRvalueReference); |
549 EXPECT_EQ(1, (*boundSingle)(MoveOnly(1))); | 549 EXPECT_EQ(1, (*boundSingle)(MoveOnly(1))); |
550 EXPECT_EQ(42, (*boundSingle)(MoveOnly(42))); | 550 EXPECT_EQ(42, (*boundSingle)(MoveOnly(42))); |
551 | 551 |
552 std::unique_ptr<Function<int(MoveOnly&&, MoveOnly&&, MoveOnly&&)>> boundTrip
le = bind(tripleMoveOnlysByRvalueReferences); | 552 std::unique_ptr<Function<int(MoveOnly&&, MoveOnly&&, MoveOnly&&)>> boundTrip
le = bind(tripleMoveOnlysByRvalueReferences); |
553 EXPECT_EQ(6, (*boundTriple)(MoveOnly(1), MoveOnly(2), MoveOnly(3))); | 553 EXPECT_EQ(6, (*boundTriple)(MoveOnly(1), MoveOnly(2), MoveOnly(3))); |
554 EXPECT_EQ(666, (*boundTriple)(MoveOnly(111), MoveOnly(222), MoveOnly(333))); | 554 EXPECT_EQ(666, (*boundTriple)(MoveOnly(111), MoveOnly(222), MoveOnly(333))); |
555 | 555 |
556 std::unique_ptr<Function<int(MoveOnly)>> boundSingleByValue = bind(singleMov
eOnlyByValue); | 556 std::unique_ptr<Function<int(MoveOnly)>> boundSingleByValue = bind(singleMov
eOnlyByValue); |
557 EXPECT_EQ(1, (*boundSingleByValue)(MoveOnly(1))); | 557 EXPECT_EQ(1, (*boundSingleByValue)(MoveOnly(1))); |
558 | 558 |
559 std::unique_ptr<Function<int(MoveOnly, MoveOnly, MoveOnly)>> boundTripleByVa
lue = bind(tripleMoveOnlysByValues); | 559 std::unique_ptr<Function<int(MoveOnly, MoveOnly, MoveOnly)>> boundTripleByVa
lue = bind(tripleMoveOnlysByValues); |
560 EXPECT_EQ(6, (*boundTripleByValue)(MoveOnly(1), MoveOnly(2), MoveOnly(3))); | 560 EXPECT_EQ(6, (*boundTripleByValue)(MoveOnly(1), MoveOnly(2), MoveOnly(3))); |
561 } | 561 } |
562 | 562 |
563 TEST(FunctionalTest, CountCopiesOfUnboundArguments) | 563 TEST(FunctionalTest, CountCopiesOfUnboundArguments) |
564 { | 564 { |
565 CountCopy lvalue; | 565 CountCopy lvalue; |
566 std::unique_ptr<Function<int(const CountCopy&)>> bound1 = bind(takeCountCopy
AsConstReference); | 566 std::unique_ptr<Function<int(const CountCopy&)>> bound1 = bind(takeCountCopy
AsConstReference); |
567 EXPECT_EQ(0, (*bound1)(lvalue)); // No copies! | 567 EXPECT_EQ(0, (*bound1)(lvalue)); // No copies! |
568 EXPECT_EQ(0, (*bound1)(CountCopy())); | 568 EXPECT_EQ(0, (*bound1)(CountCopy())); |
569 | 569 |
570 std::unique_ptr<Function<int(CountCopy)>> bound2 = bind(takeCountCopyAsValue
); | 570 std::unique_ptr<Function<int(CountCopy)>> bound2 = bind(takeCountCopyAsValue
); |
571 EXPECT_EQ(2, (*bound2)(lvalue)); // At PartBoundFunctionImpl::operator() and
at the destination function. | 571 EXPECT_EQ(3, (*bound2)(lvalue)); // At Function::operator(), at Callback::Ru
n() and at the destination function. |
572 EXPECT_LE((*bound2)(CountCopy()), 2); // Compiler is allowed to optimize one
copy away if the argument is rvalue. | 572 EXPECT_LE((*bound2)(CountCopy()), 2); // Compiler is allowed to optimize one
copy away if the argument is rvalue. |
573 } | 573 } |
574 | 574 |
575 TEST(FunctionalTest, WeakPtr) | 575 TEST(FunctionalTest, WeakPtr) |
576 { | 576 { |
577 HasWeakPtrSupport obj; | 577 HasWeakPtrSupport obj; |
578 int counter = 0; | 578 int counter = 0; |
579 std::unique_ptr<WTF::SameThreadClosure> bound = WTF::bind(&HasWeakPtrSupport
::increment, obj.createWeakPtr(), WTF::unretained(&counter)); | 579 std::unique_ptr<WTF::SameThreadClosure> bound = WTF::bind(&HasWeakPtrSupport
::increment, obj.createWeakPtr(), WTF::unretained(&counter)); |
580 | 580 |
581 (*bound)(); | 581 (*bound)(); |
582 EXPECT_EQ(1, counter); | 582 EXPECT_EQ(1, counter); |
583 | 583 |
584 obj.revokeAll(); | 584 obj.revokeAll(); |
585 (*bound)(); | 585 (*bound)(); |
586 EXPECT_EQ(1, counter); | 586 EXPECT_EQ(1, counter); |
587 } | 587 } |
588 | 588 |
589 } // anonymous namespace | 589 } // anonymous namespace |
590 | 590 |
591 } // namespace WTF | 591 } // namespace WTF |
OLD | NEW |