| 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 "config.h" | 26 #include "config.h" |
| 27 #include "wtf/HashMap.h" |
| 27 | 28 |
| 28 #include "wtf/HashMap.h" | |
| 29 #include "wtf/OwnPtr.h" | 29 #include "wtf/OwnPtr.h" |
| 30 #include "wtf/PassOwnPtr.h" | 30 #include "wtf/PassOwnPtr.h" |
| 31 #include "wtf/PassRefPtr.h" | 31 #include "wtf/PassRefPtr.h" |
| 32 #include "wtf/RefCounted.h" | 32 #include "wtf/RefCounted.h" |
| 33 #include "wtf/Vector.h" | 33 #include "wtf/Vector.h" |
| 34 #include <gtest/gtest.h> | 34 #include <gtest/gtest.h> |
| 35 | 35 |
| 36 namespace WTF { |
| 37 |
| 36 namespace { | 38 namespace { |
| 37 | 39 |
| 38 typedef WTF::HashMap<int, int> IntHashMap; | 40 using IntHashMap = HashMap<int, int>; |
| 39 | 41 |
| 40 TEST(HashMapTest, IteratorComparison) | 42 TEST(HashMapTest, IteratorComparison) |
| 41 { | 43 { |
| 42 IntHashMap map; | 44 IntHashMap map; |
| 43 map.add(1, 2); | 45 map.add(1, 2); |
| 44 EXPECT_TRUE(map.begin() != map.end()); | 46 EXPECT_TRUE(map.begin() != map.end()); |
| 45 EXPECT_FALSE(map.begin() == map.end()); | 47 EXPECT_FALSE(map.begin() == map.end()); |
| 46 | 48 |
| 47 IntHashMap::const_iterator begin = map.begin(); | 49 IntHashMap::const_iterator begin = map.begin(); |
| 48 EXPECT_TRUE(begin == map.begin()); | 50 EXPECT_TRUE(begin == map.begin()); |
| 49 EXPECT_TRUE(map.begin() == begin); | 51 EXPECT_TRUE(map.begin() == begin); |
| 50 EXPECT_TRUE(begin != map.end()); | 52 EXPECT_TRUE(begin != map.end()); |
| 51 EXPECT_TRUE(map.end() != begin); | 53 EXPECT_TRUE(map.end() != begin); |
| 52 EXPECT_FALSE(begin != map.begin()); | 54 EXPECT_FALSE(begin != map.begin()); |
| 53 EXPECT_FALSE(map.begin() != begin); | 55 EXPECT_FALSE(map.begin() != begin); |
| 54 EXPECT_FALSE(begin == map.end()); | 56 EXPECT_FALSE(begin == map.end()); |
| 55 EXPECT_FALSE(map.end() == begin); | 57 EXPECT_FALSE(map.end() == begin); |
| 56 } | 58 } |
| 57 | 59 |
| 58 struct TestDoubleHashTraits : HashTraits<double> { | 60 struct TestDoubleHashTraits : HashTraits<double> { |
| 59 static const unsigned minimumTableSize = 8; | 61 static const unsigned minimumTableSize = 8; |
| 60 }; | 62 }; |
| 61 | 63 |
| 62 typedef HashMap<double, int64_t, DefaultHash<double>::Hash, TestDoubleHashTraits
> DoubleHashMap; | 64 using DoubleHashMap = HashMap<double, int64_t, DefaultHash<double>::Hash, TestDo
ubleHashTraits>; |
| 63 | 65 |
| 64 static int bucketForKey(double key) | 66 int bucketForKey(double key) |
| 65 { | 67 { |
| 66 return DefaultHash<double>::Hash::hash(key) & (TestDoubleHashTraits::minimum
TableSize - 1); | 68 return DefaultHash<double>::Hash::hash(key) & (TestDoubleHashTraits::minimum
TableSize - 1); |
| 67 } | 69 } |
| 68 | 70 |
| 69 TEST(HashMapTest, DoubleHashCollisions) | 71 TEST(HashMapTest, DoubleHashCollisions) |
| 70 { | 72 { |
| 71 // The "clobber" key here is one that ends up stealing the bucket that the -
0 key | 73 // The "clobber" key here is one that ends up stealing the bucket that the -
0 key |
| 72 // originally wants to be in. This makes the 0 and -0 keys collide and the t
est then | 74 // originally wants to be in. This makes the 0 and -0 keys collide and the t
est then |
| 73 // fails unless the FloatHash::equals() implementation can distinguish them. | 75 // fails unless the FloatHash::equals() implementation can distinguish them. |
| 74 const double clobberKey = 6; | 76 const double clobberKey = 6; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 95 { } | 97 { } |
| 96 | 98 |
| 97 ~DestructCounter() { ++(*m_destructNumber); } | 99 ~DestructCounter() { ++(*m_destructNumber); } |
| 98 int get() const { return m_i; } | 100 int get() const { return m_i; } |
| 99 | 101 |
| 100 private: | 102 private: |
| 101 int m_i; | 103 int m_i; |
| 102 int* m_destructNumber; | 104 int* m_destructNumber; |
| 103 }; | 105 }; |
| 104 | 106 |
| 105 typedef WTF::HashMap<int, OwnPtr<DestructCounter>> OwnPtrHashMap; | 107 using OwnPtrHashMap = HashMap<int, OwnPtr<DestructCounter>>; |
| 106 | 108 |
| 107 TEST(HashMapTest, OwnPtrAsValue) | 109 TEST(HashMapTest, OwnPtrAsValue) |
| 108 { | 110 { |
| 109 int destructNumber = 0; | 111 int destructNumber = 0; |
| 110 OwnPtrHashMap map; | 112 OwnPtrHashMap map; |
| 111 map.add(1, adoptPtr(new DestructCounter(1, &destructNumber))); | 113 map.add(1, adoptPtr(new DestructCounter(1, &destructNumber))); |
| 112 map.add(2, adoptPtr(new DestructCounter(2, &destructNumber))); | 114 map.add(2, adoptPtr(new DestructCounter(2, &destructNumber))); |
| 113 | 115 |
| 114 DestructCounter* counter1 = map.get(1); | 116 DestructCounter* counter1 = map.get(1); |
| 115 EXPECT_EQ(1, counter1->get()); | 117 EXPECT_EQ(1, counter1->get()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 130 | 132 |
| 131 map.remove(2); | 133 map.remove(2); |
| 132 EXPECT_FALSE(map.contains(2)); | 134 EXPECT_FALSE(map.contains(2)); |
| 133 EXPECT_EQ(0UL, map.size()); | 135 EXPECT_EQ(0UL, map.size()); |
| 134 EXPECT_EQ(1, destructNumber); | 136 EXPECT_EQ(1, destructNumber); |
| 135 | 137 |
| 136 ownCounter1.clear(); | 138 ownCounter1.clear(); |
| 137 EXPECT_EQ(2, destructNumber); | 139 EXPECT_EQ(2, destructNumber); |
| 138 } | 140 } |
| 139 | 141 |
| 140 | 142 class DummyRefCounted : public RefCounted<DummyRefCounted> { |
| 141 class DummyRefCounted: public WTF::RefCounted<DummyRefCounted> { | |
| 142 public: | 143 public: |
| 143 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = fa
lse; } | 144 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = fa
lse; } |
| 144 ~DummyRefCounted() | 145 ~DummyRefCounted() |
| 145 { | 146 { |
| 146 ASSERT(!m_isDeleted); | 147 ASSERT(!m_isDeleted); |
| 147 m_isDeleted = true; | 148 m_isDeleted = true; |
| 148 } | 149 } |
| 149 | 150 |
| 150 void ref() | 151 void ref() |
| 151 { | 152 { |
| (...skipping 83 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 typedef HashMap<int, OwnPtr<SimpleClass>> IntSimpleMap; | 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); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 | 296 |
| 296 TEST(HashMapTest, ValueTypeDestructed) | 297 TEST(HashMapTest, ValueTypeDestructed) |
| 297 { | 298 { |
| 298 InstanceCounter::counter = 0; | 299 InstanceCounter::counter = 0; |
| 299 HashMap<int, InstanceCounter> map; | 300 HashMap<int, InstanceCounter> map; |
| 300 map.set(1, InstanceCounter()); | 301 map.set(1, InstanceCounter()); |
| 301 map.clear(); | 302 map.clear(); |
| 302 EXPECT_EQ(0, InstanceCounter::counter); | 303 EXPECT_EQ(0, InstanceCounter::counter); |
| 303 } | 304 } |
| 304 | 305 |
| 305 } // namespace | 306 } // anonymous namespace |
| 307 |
| 308 } // namespace WTF |
| OLD | NEW |