1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
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,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27
28 #include "wtf/HashMap.h"
29 #include "wtf/OwnPtr.h"
30 #include "wtf/PassOwnPtr.h"
31 #include "wtf/PassRefPtr.h"
32 #include "wtf/RefCounted.h"
33 #include <gtest/gtest.h>
34
35 namespace {
36
37 typedef WTF::HashMap<int, int> IntHashMap;
38
TEST(HashMapTest,IteratorComparison)39 TEST(HashMapTest, IteratorComparison)
40 {
41 IntHashMap map;
42 map.add(1, 2);
43 EXPECT_TRUE(map.begin() != map.end());
44 EXPECT_FALSE(map.begin() == map.end());
45
46 IntHashMap::const_iterator begin = map.begin();
47 EXPECT_TRUE(begin == map.begin());
48 EXPECT_TRUE(map.begin() == begin);
49 EXPECT_TRUE(begin != map.end());
50 EXPECT_TRUE(map.end() != begin);
51 EXPECT_FALSE(begin != map.begin());
52 EXPECT_FALSE(map.begin() != begin);
53 EXPECT_FALSE(begin == map.end());
54 EXPECT_FALSE(map.end() == begin);
55 }
56
57 struct TestDoubleHashTraits : HashTraits<double> {
58 static const unsigned minimumTableSize = 8;
59 };
60
61 typedef HashMap<double, int64_t, DefaultHash<double>::Hash, TestDoubleHashTraits> DoubleHashMap;
62
bucketForKey(double key)63 static int bucketForKey(double key)
64 {
65 return DefaultHash<double>::Hash::hash(key) & (TestDoubleHashTraits::minimumTableSize - 1);
66 }
67
TEST(HashMapTest,DoubleHashCollisions)68 TEST(HashMapTest, DoubleHashCollisions)
69 {
70 // The "clobber" key here is one that ends up stealing the bucket that the -0 key
71 // originally wants to be in. This makes the 0 and -0 keys collide and the test then
72 // fails unless the FloatHash::equals() implementation can distinguish them.
73 const double clobberKey = 6;
74 const double zeroKey = 0;
75 const double negativeZeroKey = -zeroKey;
76
77 DoubleHashMap map;
78
79 map.add(clobberKey, 1);
80 map.add(zeroKey, 2);
81 map.add(negativeZeroKey, 3);
82
83 EXPECT_EQ(bucketForKey(clobberKey), bucketForKey(negativeZeroKey));
84 EXPECT_EQ(1, map.get(clobberKey));
85 EXPECT_EQ(2, map.get(zeroKey));
86 EXPECT_EQ(3, map.get(negativeZeroKey));
87 }
88
89 class DestructCounter {
90 public:
DestructCounter(int i,int * destructNumber)91 explicit DestructCounter(int i, int* destructNumber)
92 : m_i(i)
93 , m_destructNumber(destructNumber)
94 { }
95
~DestructCounter()96 ~DestructCounter() { ++(*m_destructNumber); }
get() const97 int get() const { return m_i; }
98
99 private:
100 int m_i;
101 int* m_destructNumber;
102 };
103
104 typedef WTF::HashMap<int, OwnPtr<DestructCounter> > OwnPtrHashMap;
105
TEST(HashMapTest,OwnPtrAsValue)106 TEST(HashMapTest, OwnPtrAsValue)
107 {
108 int destructNumber = 0;
109 OwnPtrHashMap map;
110 map.add(1, adoptPtr(new DestructCounter(1, &destructNumber)));
111 map.add(2, adoptPtr(new DestructCounter(2, &destructNumber)));
112
113 DestructCounter* counter1 = map.get(1);
114 EXPECT_EQ(1, counter1->get());
115 DestructCounter* counter2 = map.get(2);
116 EXPECT_EQ(2, counter2->get());
117 EXPECT_EQ(0, destructNumber);
118
119 for (OwnPtrHashMap::iterator iter = map.begin(); iter != map.end(); ++iter) {
120 OwnPtr<DestructCounter>& ownCounter = iter->value;
121 EXPECT_EQ(iter->key, ownCounter->get());
122 }
123 ASSERT_EQ(0, destructNumber);
124
125 OwnPtr<DestructCounter> ownCounter1 = map.take(1);
126 EXPECT_EQ(ownCounter1.get(), counter1);
127 EXPECT_FALSE(map.contains(1));
128 EXPECT_EQ(0, destructNumber);
129
130 map.remove(2);
131 EXPECT_FALSE(map.contains(2));
132 EXPECT_EQ(0UL, map.size());
133 EXPECT_EQ(1, destructNumber);
134
135 ownCounter1.clear();
136 EXPECT_EQ(2, destructNumber);
137 }
138
139
140 class DummyRefCounted: public WTF::RefCounted<DummyRefCounted> {
141 public:
DummyRefCounted(bool & isDeleted)142 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = false; }
~DummyRefCounted()143 ~DummyRefCounted()
144 {
145 ASSERT(!m_isDeleted);
146 m_isDeleted = true;
147 }
148
ref()149 void ref()
150 {
151 ASSERT(!m_isDeleted);
152 WTF::RefCounted<DummyRefCounted>::ref();
153 ++m_refInvokesCount;
154 }
155
deref()156 void deref()
157 {
158 ASSERT(!m_isDeleted);
159 WTF::RefCounted<DummyRefCounted>::deref();
160 }
161
162 static int m_refInvokesCount;
163
164 private:
165 bool& m_isDeleted;
166 };
167
168 int DummyRefCounted::m_refInvokesCount = 0;
169
TEST(HashMapTest,RefPtrAsKey)170 TEST(HashMapTest, RefPtrAsKey)
171 {
172 bool isDeleted = false;
173 DummyRefCounted::m_refInvokesCount = 0;
174 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted));
175 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount);
176 HashMap<RefPtr<DummyRefCounted>, int> map;
177 map.add(ptr, 1);
178 // Referenced only once (to store a copy in the container).
179 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount);
180 EXPECT_EQ(1, map.get(ptr));
181
182 DummyRefCounted* rawPtr = ptr.get();
183
184 EXPECT_TRUE(map.contains(rawPtr));
185 EXPECT_NE(map.end(), map.find(rawPtr));
186 EXPECT_TRUE(map.contains(ptr));
187 EXPECT_NE(map.end(), map.find(ptr));
188 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount);
189
190 ptr.clear();
191 EXPECT_FALSE(isDeleted);
192
193 map.remove(rawPtr);
194 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount);
195 EXPECT_TRUE(isDeleted);
196 EXPECT_TRUE(map.isEmpty());
197 }
198
TEST(HashMaptest,RemoveAdd)199 TEST(HashMaptest, RemoveAdd)
200 {
201 DummyRefCounted::m_refInvokesCount = 0;
202 bool isDeleted = false;
203
204 typedef HashMap<int, RefPtr<DummyRefCounted>> Map;
205 Map map;
206
207 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted));
208 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount);
209
210 map.add(1, ptr);
211 // Referenced only once (to store a copy in the container).
212 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount);
213 EXPECT_EQ(ptr, map.get(1));
214
215 ptr.clear();
216 EXPECT_FALSE(isDeleted);
217
218 map.remove(1);
219 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount);
220 EXPECT_TRUE(isDeleted);
221 EXPECT_TRUE(map.isEmpty());
222
223 // Add and remove until the deleted slot is reused.
224 for (int i = 1; i < 100; i++) {
225 bool isDeleted2 = false;
226 RefPtr<DummyRefCounted> ptr2 = adoptRef(new DummyRefCounted(isDeleted2));
227 map.add(i, ptr2);
228 EXPECT_FALSE(isDeleted2);
229 ptr2.clear();
230 EXPECT_FALSE(isDeleted2);
231 map.remove(i);
232 EXPECT_TRUE(isDeleted2);
233 }
234 }
235
236 class SimpleClass {
237 public:
SimpleClass(int v)238 explicit SimpleClass(int v) : m_v(v) { }
v()239 int v() { return m_v; }
240
241 private:
242 int m_v;
243 };
244 typedef HashMap<int, OwnPtr<SimpleClass> > IntSimpleMap;
245
TEST(HashMapTest,AddResult)246 TEST(HashMapTest, AddResult)
247 {
248 IntSimpleMap map;
249 IntSimpleMap::AddResult result = map.add(1, nullptr);
250 EXPECT_TRUE(result.isNewEntry);
251 EXPECT_EQ(1, result.storedValue->key);
252 EXPECT_EQ(0, result.storedValue->value.get());
253
254 SimpleClass* simple1 = new SimpleClass(1);
255 result.storedValue->value = adoptPtr(simple1);
256 EXPECT_EQ(simple1, map.get(1));
257
258 IntSimpleMap::AddResult result2 = map.add(1, adoptPtr(new SimpleClass(2)));
259 EXPECT_FALSE(result2.isNewEntry);
260 EXPECT_EQ(1, map.get(1)->v());
261 }
262
263 } // namespace
264