| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google 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/HashMap.h" | 26 #include "wtf/HashMap.h" |
| 27 | 27 |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "wtf/OwnPtr.h" | |
| 30 #include "wtf/PassOwnPtr.h" | |
| 31 #include "wtf/PassRefPtr.h" | 29 #include "wtf/PassRefPtr.h" |
| 30 #include "wtf/PtrUtil.h" |
| 32 #include "wtf/RefCounted.h" | 31 #include "wtf/RefCounted.h" |
| 33 #include "wtf/Vector.h" | 32 #include "wtf/Vector.h" |
| 34 #include <memory> | 33 #include <memory> |
| 35 | 34 |
| 36 namespace WTF { | 35 namespace WTF { |
| 37 | 36 |
| 38 namespace { | 37 namespace { |
| 39 | 38 |
| 40 using IntHashMap = HashMap<int, int>; | 39 using IntHashMap = HashMap<int, int>; |
| 41 | 40 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 { } | 96 { } |
| 98 | 97 |
| 99 ~DestructCounter() { ++(*m_destructNumber); } | 98 ~DestructCounter() { ++(*m_destructNumber); } |
| 100 int get() const { return m_i; } | 99 int get() const { return m_i; } |
| 101 | 100 |
| 102 private: | 101 private: |
| 103 int m_i; | 102 int m_i; |
| 104 int* m_destructNumber; | 103 int* m_destructNumber; |
| 105 }; | 104 }; |
| 106 | 105 |
| 107 using OwnPtrHashMap = HashMap<int, OwnPtr<DestructCounter>>; | 106 using OwnPtrHashMap = HashMap<int, std::unique_ptr<DestructCounter>>; |
| 108 | 107 |
| 109 TEST(HashMapTest, OwnPtrAsValue) | 108 TEST(HashMapTest, OwnPtrAsValue) |
| 110 { | 109 { |
| 111 int destructNumber = 0; | 110 int destructNumber = 0; |
| 112 OwnPtrHashMap map; | 111 OwnPtrHashMap map; |
| 113 map.add(1, adoptPtr(new DestructCounter(1, &destructNumber))); | 112 map.add(1, wrapUnique(new DestructCounter(1, &destructNumber))); |
| 114 map.add(2, adoptPtr(new DestructCounter(2, &destructNumber))); | 113 map.add(2, wrapUnique(new DestructCounter(2, &destructNumber))); |
| 115 | 114 |
| 116 DestructCounter* counter1 = map.get(1); | 115 DestructCounter* counter1 = map.get(1); |
| 117 EXPECT_EQ(1, counter1->get()); | 116 EXPECT_EQ(1, counter1->get()); |
| 118 DestructCounter* counter2 = map.get(2); | 117 DestructCounter* counter2 = map.get(2); |
| 119 EXPECT_EQ(2, counter2->get()); | 118 EXPECT_EQ(2, counter2->get()); |
| 120 EXPECT_EQ(0, destructNumber); | 119 EXPECT_EQ(0, destructNumber); |
| 121 | 120 |
| 122 for (OwnPtrHashMap::iterator iter = map.begin(); iter != map.end(); ++iter)
{ | 121 for (OwnPtrHashMap::iterator iter = map.begin(); iter != map.end(); ++iter)
{ |
| 123 OwnPtr<DestructCounter>& ownCounter = iter->value; | 122 std::unique_ptr<DestructCounter>& ownCounter = iter->value; |
| 124 EXPECT_EQ(iter->key, ownCounter->get()); | 123 EXPECT_EQ(iter->key, ownCounter->get()); |
| 125 } | 124 } |
| 126 ASSERT_EQ(0, destructNumber); | 125 ASSERT_EQ(0, destructNumber); |
| 127 | 126 |
| 128 OwnPtr<DestructCounter> ownCounter1 = map.take(1); | 127 std::unique_ptr<DestructCounter> ownCounter1 = map.take(1); |
| 129 EXPECT_EQ(ownCounter1.get(), counter1); | 128 EXPECT_EQ(ownCounter1.get(), counter1); |
| 130 EXPECT_FALSE(map.contains(1)); | 129 EXPECT_FALSE(map.contains(1)); |
| 131 EXPECT_EQ(0, destructNumber); | 130 EXPECT_EQ(0, destructNumber); |
| 132 | 131 |
| 133 map.remove(2); | 132 map.remove(2); |
| 134 EXPECT_FALSE(map.contains(2)); | 133 EXPECT_FALSE(map.contains(2)); |
| 135 EXPECT_EQ(0UL, map.size()); | 134 EXPECT_EQ(0UL, map.size()); |
| 136 EXPECT_EQ(1, destructNumber); | 135 EXPECT_EQ(1, destructNumber); |
| 137 | 136 |
| 138 ownCounter1.reset(); | 137 ownCounter1.reset(); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 } | 235 } |
| 237 | 236 |
| 238 class SimpleClass { | 237 class SimpleClass { |
| 239 public: | 238 public: |
| 240 explicit SimpleClass(int v) : m_v(v) { } | 239 explicit SimpleClass(int v) : m_v(v) { } |
| 241 int v() { return m_v; } | 240 int v() { return m_v; } |
| 242 | 241 |
| 243 private: | 242 private: |
| 244 int m_v; | 243 int m_v; |
| 245 }; | 244 }; |
| 246 using IntSimpleMap = HashMap<int, OwnPtr<SimpleClass>>; | 245 using IntSimpleMap = HashMap<int, std::unique_ptr<SimpleClass>>; |
| 247 | 246 |
| 248 TEST(HashMapTest, AddResult) | 247 TEST(HashMapTest, AddResult) |
| 249 { | 248 { |
| 250 IntSimpleMap map; | 249 IntSimpleMap map; |
| 251 IntSimpleMap::AddResult result = map.add(1, nullptr); | 250 IntSimpleMap::AddResult result = map.add(1, nullptr); |
| 252 EXPECT_TRUE(result.isNewEntry); | 251 EXPECT_TRUE(result.isNewEntry); |
| 253 EXPECT_EQ(1, result.storedValue->key); | 252 EXPECT_EQ(1, result.storedValue->key); |
| 254 EXPECT_EQ(0, result.storedValue->value.get()); | 253 EXPECT_EQ(0, result.storedValue->value.get()); |
| 255 | 254 |
| 256 SimpleClass* simple1 = new SimpleClass(1); | 255 SimpleClass* simple1 = new SimpleClass(1); |
| 257 result.storedValue->value = adoptPtr(simple1); | 256 result.storedValue->value = wrapUnique(simple1); |
| 258 EXPECT_EQ(simple1, map.get(1)); | 257 EXPECT_EQ(simple1, map.get(1)); |
| 259 | 258 |
| 260 IntSimpleMap::AddResult result2 = map.add(1, adoptPtr(new SimpleClass(2))); | 259 IntSimpleMap::AddResult result2 = map.add(1, wrapUnique(new SimpleClass(2)))
; |
| 261 EXPECT_FALSE(result2.isNewEntry); | 260 EXPECT_FALSE(result2.isNewEntry); |
| 262 EXPECT_EQ(1, result.storedValue->key); | 261 EXPECT_EQ(1, result.storedValue->key); |
| 263 EXPECT_EQ(1, result.storedValue->value->v()); | 262 EXPECT_EQ(1, result.storedValue->value->v()); |
| 264 EXPECT_EQ(1, map.get(1)->v()); | 263 EXPECT_EQ(1, map.get(1)->v()); |
| 265 } | 264 } |
| 266 | 265 |
| 267 TEST(HashMapTest, AddResultVectorValue) | 266 TEST(HashMapTest, AddResultVectorValue) |
| 268 { | 267 { |
| 269 using IntVectorMap = HashMap<int, Vector<int>>; | 268 using IntVectorMap = HashMap<int, Vector<int>>; |
| 270 IntVectorMap map; | 269 IntVectorMap map; |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 EXPECT_EQ(130, oneThirty); | 664 EXPECT_EQ(130, oneThirty); |
| 666 iter = map.find(Pair(MoveOnly(13), -13)); | 665 iter = map.find(Pair(MoveOnly(13), -13)); |
| 667 EXPECT_TRUE(iter == map.end()); | 666 EXPECT_TRUE(iter == map.end()); |
| 668 | 667 |
| 669 map.clear(); | 668 map.clear(); |
| 670 } | 669 } |
| 671 | 670 |
| 672 } // anonymous namespace | 671 } // anonymous namespace |
| 673 | 672 |
| 674 } // namespace WTF | 673 } // namespace WTF |
| OLD | NEW |