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