OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2012 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 10 matching lines...) Expand all Loading... |
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "wtf/ListHashSet.h" | 26 #include "wtf/ListHashSet.h" |
27 | 27 |
28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "wtf/LinkedHashSet.h" | 29 #include "wtf/LinkedHashSet.h" |
30 #include "wtf/PassRefPtr.h" | 30 #include "wtf/PassRefPtr.h" |
| 31 #include "wtf/PtrUtil.h" |
31 #include "wtf/RefCounted.h" | 32 #include "wtf/RefCounted.h" |
32 #include "wtf/RefPtr.h" | 33 #include "wtf/RefPtr.h" |
| 34 #include <memory> |
33 #include <type_traits> | 35 #include <type_traits> |
34 | 36 |
35 namespace WTF { | 37 namespace WTF { |
36 | 38 |
37 namespace { | 39 namespace { |
38 | 40 |
39 template <typename Set> | 41 template <typename Set> |
40 class ListOrLinkedHashSetTest : public ::testing::Test { }; | 42 class ListOrLinkedHashSetTest : public ::testing::Test { }; |
41 | 43 |
42 using SetTypes = ::testing::Types<ListHashSet<int>, ListHashSet<int, 1>, LinkedH
ashSet<int>>; | 44 using SetTypes = ::testing::Types<ListHashSet<int>, ListHashSet<int, 1>, LinkedH
ashSet<int>>; |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 deleted = true; | 547 deleted = true; |
546 } | 548 } |
547 | 549 |
548 bool& deleted; | 550 bool& deleted; |
549 }; | 551 }; |
550 | 552 |
551 TEST(ListHashSetTest, WithOwnPtr) | 553 TEST(ListHashSetTest, WithOwnPtr) |
552 { | 554 { |
553 bool deleted1 = false, deleted2 = false; | 555 bool deleted1 = false, deleted2 = false; |
554 | 556 |
555 typedef ListHashSet<OwnPtr<Dummy>> OwnPtrSet; | 557 typedef ListHashSet<std::unique_ptr<Dummy>> OwnPtrSet; |
556 OwnPtrSet set; | 558 OwnPtrSet set; |
557 | 559 |
558 Dummy* ptr1 = new Dummy(deleted1); | 560 Dummy* ptr1 = new Dummy(deleted1); |
559 { | 561 { |
560 // AddResult in a separate scope to avoid assertion hit, | 562 // AddResult in a separate scope to avoid assertion hit, |
561 // since we modify the container further. | 563 // since we modify the container further. |
562 OwnPtrSet::AddResult res1 = set.add(adoptPtr(ptr1)); | 564 OwnPtrSet::AddResult res1 = set.add(wrapUnique(ptr1)); |
563 EXPECT_EQ(res1.storedValue->get(), ptr1); | 565 EXPECT_EQ(res1.storedValue->get(), ptr1); |
564 } | 566 } |
565 | 567 |
566 EXPECT_FALSE(deleted1); | 568 EXPECT_FALSE(deleted1); |
567 EXPECT_EQ(1UL, set.size()); | 569 EXPECT_EQ(1UL, set.size()); |
568 OwnPtrSet::iterator it1 = set.find(ptr1); | 570 OwnPtrSet::iterator it1 = set.find(ptr1); |
569 EXPECT_NE(set.end(), it1); | 571 EXPECT_NE(set.end(), it1); |
570 EXPECT_EQ(ptr1, (*it1)); | 572 EXPECT_EQ(ptr1, (*it1).get()); |
571 | 573 |
572 Dummy* ptr2 = new Dummy(deleted2); | 574 Dummy* ptr2 = new Dummy(deleted2); |
573 { | 575 { |
574 OwnPtrSet::AddResult res2 = set.add(adoptPtr(ptr2)); | 576 OwnPtrSet::AddResult res2 = set.add(wrapUnique(ptr2)); |
575 EXPECT_EQ(res2.storedValue->get(), ptr2); | 577 EXPECT_EQ(res2.storedValue->get(), ptr2); |
576 } | 578 } |
577 | 579 |
578 EXPECT_FALSE(deleted2); | 580 EXPECT_FALSE(deleted2); |
579 EXPECT_EQ(2UL, set.size()); | 581 EXPECT_EQ(2UL, set.size()); |
580 OwnPtrSet::iterator it2 = set.find(ptr2); | 582 OwnPtrSet::iterator it2 = set.find(ptr2); |
581 EXPECT_NE(set.end(), it2); | 583 EXPECT_NE(set.end(), it2); |
582 EXPECT_EQ(ptr2, (*it2)); | 584 EXPECT_EQ(ptr2, (*it2).get()); |
583 | 585 |
584 set.remove(ptr1); | 586 set.remove(ptr1); |
585 EXPECT_TRUE(deleted1); | 587 EXPECT_TRUE(deleted1); |
586 | 588 |
587 set.clear(); | 589 set.clear(); |
588 EXPECT_TRUE(deleted2); | 590 EXPECT_TRUE(deleted2); |
589 EXPECT_TRUE(set.isEmpty()); | 591 EXPECT_TRUE(set.isEmpty()); |
590 | 592 |
591 deleted1 = false; | 593 deleted1 = false; |
592 deleted2 = false; | 594 deleted2 = false; |
593 { | 595 { |
594 OwnPtrSet set; | 596 OwnPtrSet set; |
595 set.add(adoptPtr(new Dummy(deleted1))); | 597 set.add(wrapUnique(new Dummy(deleted1))); |
596 set.add(adoptPtr(new Dummy(deleted2))); | 598 set.add(wrapUnique(new Dummy(deleted2))); |
597 } | 599 } |
598 EXPECT_TRUE(deleted1); | 600 EXPECT_TRUE(deleted1); |
599 EXPECT_TRUE(deleted2); | 601 EXPECT_TRUE(deleted2); |
600 | 602 |
601 | 603 |
602 deleted1 = false; | 604 deleted1 = false; |
603 deleted2 = false; | 605 deleted2 = false; |
604 OwnPtr<Dummy> ownPtr1; | 606 std::unique_ptr<Dummy> ownPtr1; |
605 OwnPtr<Dummy> ownPtr2; | 607 std::unique_ptr<Dummy> ownPtr2; |
606 ptr1 = new Dummy(deleted1); | 608 ptr1 = new Dummy(deleted1); |
607 ptr2 = new Dummy(deleted2); | 609 ptr2 = new Dummy(deleted2); |
608 { | 610 { |
609 OwnPtrSet set; | 611 OwnPtrSet set; |
610 set.add(adoptPtr(ptr1)); | 612 set.add(wrapUnique(ptr1)); |
611 set.add(adoptPtr(ptr2)); | 613 set.add(wrapUnique(ptr2)); |
612 ownPtr1 = set.takeFirst(); | 614 ownPtr1 = set.takeFirst(); |
613 EXPECT_EQ(1UL, set.size()); | 615 EXPECT_EQ(1UL, set.size()); |
614 ownPtr2 = set.take(ptr2); | 616 ownPtr2 = set.take(ptr2); |
615 EXPECT_TRUE(set.isEmpty()); | 617 EXPECT_TRUE(set.isEmpty()); |
616 } | 618 } |
617 EXPECT_FALSE(deleted1); | 619 EXPECT_FALSE(deleted1); |
618 EXPECT_FALSE(deleted2); | 620 EXPECT_FALSE(deleted2); |
619 | 621 |
620 EXPECT_EQ(ptr1, ownPtr1); | 622 EXPECT_EQ(ptr1, ownPtr1.get()); |
621 EXPECT_EQ(ptr2, ownPtr2); | 623 EXPECT_EQ(ptr2, ownPtr2.get()); |
622 } | 624 } |
623 | 625 |
624 class CountCopy final { | 626 class CountCopy final { |
625 public: | 627 public: |
626 static int* const kDeletedValue; | 628 static int* const kDeletedValue; |
627 | 629 |
628 explicit CountCopy(int* counter = nullptr) : m_counter(counter) { } | 630 explicit CountCopy(int* counter = nullptr) : m_counter(counter) { } |
629 CountCopy(const CountCopy& other) : m_counter(other.m_counter) | 631 CountCopy(const CountCopy& other) : m_counter(other.m_counter) |
630 { | 632 { |
631 if (m_counter && m_counter != kDeletedValue) | 633 if (m_counter && m_counter != kDeletedValue) |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
821 // ... but they don't have any pass-out (like take()) methods. | 823 // ... but they don't have any pass-out (like take()) methods. |
822 | 824 |
823 set.remove(MoveOnly(3)); | 825 set.remove(MoveOnly(3)); |
824 set.clear(); | 826 set.clear(); |
825 } | 827 } |
826 | 828 |
827 | 829 |
828 } // anonymous namespace | 830 } // anonymous namespace |
829 | 831 |
830 } // namespace WTF | 832 } // namespace WTF |
OLD | NEW |