| 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 |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 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/HashSet.h" | 26 #include "wtf/HashSet.h" |
| 27 | 27 |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "wtf/PtrUtil.h" | 29 #include "wtf/OwnPtr.h" |
| 30 #include "wtf/PassOwnPtr.h" |
| 30 #include "wtf/RefCounted.h" | 31 #include "wtf/RefCounted.h" |
| 31 #include <memory> | |
| 32 | 32 |
| 33 namespace WTF { | 33 namespace WTF { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 template<unsigned size> void testReserveCapacity(); | 37 template<unsigned size> void testReserveCapacity(); |
| 38 template<> void testReserveCapacity<0>() {} | 38 template<> void testReserveCapacity<0>() {} |
| 39 template<unsigned size> void testReserveCapacity() | 39 template<unsigned size> void testReserveCapacity() |
| 40 { | 40 { |
| 41 HashSet<int> testSet; | 41 HashSet<int> testSet; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 deleted = true; | 83 deleted = true; |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool& deleted; | 86 bool& deleted; |
| 87 }; | 87 }; |
| 88 | 88 |
| 89 TEST(HashSetTest, HashSetOwnPtr) | 89 TEST(HashSetTest, HashSetOwnPtr) |
| 90 { | 90 { |
| 91 bool deleted1 = false, deleted2 = false; | 91 bool deleted1 = false, deleted2 = false; |
| 92 | 92 |
| 93 typedef HashSet<std::unique_ptr<Dummy>> OwnPtrSet; | 93 typedef HashSet<OwnPtr<Dummy>> OwnPtrSet; |
| 94 OwnPtrSet set; | 94 OwnPtrSet set; |
| 95 | 95 |
| 96 Dummy* ptr1 = new Dummy(deleted1); | 96 Dummy* ptr1 = new Dummy(deleted1); |
| 97 { | 97 { |
| 98 // AddResult in a separate scope to avoid assertion hit, | 98 // AddResult in a separate scope to avoid assertion hit, |
| 99 // since we modify the container further. | 99 // since we modify the container further. |
| 100 HashSet<std::unique_ptr<Dummy>>::AddResult res1 = set.add(wrapUnique(ptr
1)); | 100 HashSet<OwnPtr<Dummy>>::AddResult res1 = set.add(adoptPtr(ptr1)); |
| 101 EXPECT_EQ(ptr1, res1.storedValue->get()); | 101 EXPECT_EQ(ptr1, res1.storedValue->get()); |
| 102 } | 102 } |
| 103 | 103 |
| 104 EXPECT_FALSE(deleted1); | 104 EXPECT_FALSE(deleted1); |
| 105 EXPECT_EQ(1UL, set.size()); | 105 EXPECT_EQ(1UL, set.size()); |
| 106 OwnPtrSet::iterator it1 = set.find(ptr1); | 106 OwnPtrSet::iterator it1 = set.find(ptr1); |
| 107 EXPECT_NE(set.end(), it1); | 107 EXPECT_NE(set.end(), it1); |
| 108 EXPECT_EQ(ptr1, (*it1).get()); | 108 EXPECT_EQ(ptr1, (*it1)); |
| 109 | 109 |
| 110 Dummy* ptr2 = new Dummy(deleted2); | 110 Dummy* ptr2 = new Dummy(deleted2); |
| 111 { | 111 { |
| 112 HashSet<std::unique_ptr<Dummy>>::AddResult res2 = set.add(wrapUnique(ptr
2)); | 112 HashSet<OwnPtr<Dummy>>::AddResult res2 = set.add(adoptPtr(ptr2)); |
| 113 EXPECT_EQ(res2.storedValue->get(), ptr2); | 113 EXPECT_EQ(res2.storedValue->get(), ptr2); |
| 114 } | 114 } |
| 115 | 115 |
| 116 EXPECT_FALSE(deleted2); | 116 EXPECT_FALSE(deleted2); |
| 117 EXPECT_EQ(2UL, set.size()); | 117 EXPECT_EQ(2UL, set.size()); |
| 118 OwnPtrSet::iterator it2 = set.find(ptr2); | 118 OwnPtrSet::iterator it2 = set.find(ptr2); |
| 119 EXPECT_NE(set.end(), it2); | 119 EXPECT_NE(set.end(), it2); |
| 120 EXPECT_EQ(ptr2, (*it2).get()); | 120 EXPECT_EQ(ptr2, (*it2)); |
| 121 | 121 |
| 122 set.remove(ptr1); | 122 set.remove(ptr1); |
| 123 EXPECT_TRUE(deleted1); | 123 EXPECT_TRUE(deleted1); |
| 124 | 124 |
| 125 set.clear(); | 125 set.clear(); |
| 126 EXPECT_TRUE(deleted2); | 126 EXPECT_TRUE(deleted2); |
| 127 EXPECT_TRUE(set.isEmpty()); | 127 EXPECT_TRUE(set.isEmpty()); |
| 128 | 128 |
| 129 deleted1 = false; | 129 deleted1 = false; |
| 130 deleted2 = false; | 130 deleted2 = false; |
| 131 { | 131 { |
| 132 OwnPtrSet set; | 132 OwnPtrSet set; |
| 133 set.add(wrapUnique(new Dummy(deleted1))); | 133 set.add(adoptPtr(new Dummy(deleted1))); |
| 134 set.add(wrapUnique(new Dummy(deleted2))); | 134 set.add(adoptPtr(new Dummy(deleted2))); |
| 135 } | 135 } |
| 136 EXPECT_TRUE(deleted1); | 136 EXPECT_TRUE(deleted1); |
| 137 EXPECT_TRUE(deleted2); | 137 EXPECT_TRUE(deleted2); |
| 138 | 138 |
| 139 deleted1 = false; | 139 deleted1 = false; |
| 140 deleted2 = false; | 140 deleted2 = false; |
| 141 std::unique_ptr<Dummy> ownPtr1; | 141 OwnPtr<Dummy> ownPtr1; |
| 142 std::unique_ptr<Dummy> ownPtr2; | 142 OwnPtr<Dummy> ownPtr2; |
| 143 ptr1 = new Dummy(deleted1); | 143 ptr1 = new Dummy(deleted1); |
| 144 ptr2 = new Dummy(deleted2); | 144 ptr2 = new Dummy(deleted2); |
| 145 { | 145 { |
| 146 OwnPtrSet set; | 146 OwnPtrSet set; |
| 147 set.add(wrapUnique(ptr1)); | 147 set.add(adoptPtr(ptr1)); |
| 148 set.add(wrapUnique(ptr2)); | 148 set.add(adoptPtr(ptr2)); |
| 149 ownPtr1 = set.take(ptr1); | 149 ownPtr1 = set.take(ptr1); |
| 150 EXPECT_EQ(1UL, set.size()); | 150 EXPECT_EQ(1UL, set.size()); |
| 151 ownPtr2 = set.takeAny(); | 151 ownPtr2 = set.takeAny(); |
| 152 EXPECT_TRUE(set.isEmpty()); | 152 EXPECT_TRUE(set.isEmpty()); |
| 153 } | 153 } |
| 154 EXPECT_FALSE(deleted1); | 154 EXPECT_FALSE(deleted1); |
| 155 EXPECT_FALSE(deleted2); | 155 EXPECT_FALSE(deleted2); |
| 156 | 156 |
| 157 EXPECT_EQ(ptr1, ownPtr1.get()); | 157 EXPECT_EQ(ptr1, ownPtr1); |
| 158 EXPECT_EQ(ptr2, ownPtr2.get()); | 158 EXPECT_EQ(ptr2, ownPtr2); |
| 159 } | 159 } |
| 160 | 160 |
| 161 class DummyRefCounted : public RefCounted<DummyRefCounted> { | 161 class DummyRefCounted : public RefCounted<DummyRefCounted> { |
| 162 public: | 162 public: |
| 163 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = fa
lse; } | 163 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = fa
lse; } |
| 164 ~DummyRefCounted() { m_isDeleted = true; } | 164 ~DummyRefCounted() { m_isDeleted = true; } |
| 165 | 165 |
| 166 void ref() | 166 void ref() |
| 167 { | 167 { |
| 168 WTF::RefCounted<DummyRefCounted>::ref(); | 168 WTF::RefCounted<DummyRefCounted>::ref(); |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 EXPECT_TRUE(oneTwoThree.contains(3)); | 497 EXPECT_TRUE(oneTwoThree.contains(3)); |
| 498 | 498 |
| 499 // Other ways of construction: as a function parameter and in a return state
ment. | 499 // Other ways of construction: as a function parameter and in a return state
ment. |
| 500 EXPECT_TRUE(isOneTwoThree({1, 2, 3})); | 500 EXPECT_TRUE(isOneTwoThree({1, 2, 3})); |
| 501 EXPECT_TRUE(isOneTwoThree(returnOneTwoThree())); | 501 EXPECT_TRUE(isOneTwoThree(returnOneTwoThree())); |
| 502 } | 502 } |
| 503 | 503 |
| 504 } // anonymous namespace | 504 } // anonymous namespace |
| 505 | 505 |
| 506 } // namespace WTF | 506 } // namespace WTF |
| OLD | NEW |