Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: third_party/WebKit/Source/wtf/HashMapTest.cpp

Issue 1611343002: wtf reformat test Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pydent Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/HashMap.h ('k') | third_party/WebKit/Source/wtf/HashSet.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 20 matching lines...) Expand all
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 34
35 namespace WTF { 35 namespace WTF {
36 36
37 namespace { 37 namespace {
38 38
39 using IntHashMap = HashMap<int, int>; 39 using IntHashMap = HashMap<int, int>;
40 40
41 TEST(HashMapTest, IteratorComparison) 41 TEST(HashMapTest, IteratorComparison) {
42 { 42 IntHashMap map;
43 IntHashMap map; 43 map.add(1, 2);
44 map.add(1, 2); 44 EXPECT_TRUE(map.begin() != map.end());
45 EXPECT_TRUE(map.begin() != map.end()); 45 EXPECT_FALSE(map.begin() == map.end());
46 EXPECT_FALSE(map.begin() == map.end()); 46
47 47 IntHashMap::const_iterator begin = map.begin();
48 IntHashMap::const_iterator begin = map.begin(); 48 EXPECT_TRUE(begin == map.begin());
49 EXPECT_TRUE(begin == map.begin()); 49 EXPECT_TRUE(map.begin() == begin);
50 EXPECT_TRUE(map.begin() == begin); 50 EXPECT_TRUE(begin != map.end());
51 EXPECT_TRUE(begin != map.end()); 51 EXPECT_TRUE(map.end() != begin);
52 EXPECT_TRUE(map.end() != begin); 52 EXPECT_FALSE(begin != map.begin());
53 EXPECT_FALSE(begin != map.begin()); 53 EXPECT_FALSE(map.begin() != begin);
54 EXPECT_FALSE(map.begin() != begin); 54 EXPECT_FALSE(begin == map.end());
55 EXPECT_FALSE(begin == map.end()); 55 EXPECT_FALSE(map.end() == begin);
56 EXPECT_FALSE(map.end() == begin);
57 } 56 }
58 57
59 struct TestDoubleHashTraits : HashTraits<double> { 58 struct TestDoubleHashTraits : HashTraits<double> {
60 static const unsigned minimumTableSize = 8; 59 static const unsigned minimumTableSize = 8;
61 }; 60 };
62 61
63 using DoubleHashMap = HashMap<double, int64_t, DefaultHash<double>::Hash, TestDo ubleHashTraits>; 62 using DoubleHashMap =
64 63 HashMap<double, int64_t, DefaultHash<double>::Hash, TestDoubleHashTraits>;
65 int bucketForKey(double key) 64
66 { 65 int bucketForKey(double key) {
67 return DefaultHash<double>::Hash::hash(key) & (TestDoubleHashTraits::minimum TableSize - 1); 66 return DefaultHash<double>::Hash::hash(key) &
68 } 67 (TestDoubleHashTraits::minimumTableSize - 1);
69 68 }
70 TEST(HashMapTest, DoubleHashCollisions) 69
71 { 70 TEST(HashMapTest, DoubleHashCollisions) {
72 // The "clobber" key here is one that ends up stealing the bucket that the - 0 key 71 // The "clobber" key here is one that ends up stealing the bucket that the -0 key
73 // originally wants to be in. This makes the 0 and -0 keys collide and the t est then 72 // originally wants to be in. This makes the 0 and -0 keys collide and the tes t then
74 // fails unless the FloatHash::equals() implementation can distinguish them. 73 // fails unless the FloatHash::equals() implementation can distinguish them.
75 const double clobberKey = 6; 74 const double clobberKey = 6;
76 const double zeroKey = 0; 75 const double zeroKey = 0;
77 const double negativeZeroKey = -zeroKey; 76 const double negativeZeroKey = -zeroKey;
78 77
79 DoubleHashMap map; 78 DoubleHashMap map;
80 79
81 map.add(clobberKey, 1); 80 map.add(clobberKey, 1);
82 map.add(zeroKey, 2); 81 map.add(zeroKey, 2);
83 map.add(negativeZeroKey, 3); 82 map.add(negativeZeroKey, 3);
84 83
85 EXPECT_EQ(bucketForKey(clobberKey), bucketForKey(negativeZeroKey)); 84 EXPECT_EQ(bucketForKey(clobberKey), bucketForKey(negativeZeroKey));
86 EXPECT_EQ(1, map.get(clobberKey)); 85 EXPECT_EQ(1, map.get(clobberKey));
87 EXPECT_EQ(2, map.get(zeroKey)); 86 EXPECT_EQ(2, map.get(zeroKey));
88 EXPECT_EQ(3, map.get(negativeZeroKey)); 87 EXPECT_EQ(3, map.get(negativeZeroKey));
89 } 88 }
90 89
91 class DestructCounter { 90 class DestructCounter {
92 public: 91 public:
93 explicit DestructCounter(int i, int* destructNumber) 92 explicit DestructCounter(int i, int* destructNumber)
94 : m_i(i) 93 : m_i(i), m_destructNumber(destructNumber) {}
95 , m_destructNumber(destructNumber) 94
96 { } 95 ~DestructCounter() { ++(*m_destructNumber); }
97 96 int get() const { return m_i; }
98 ~DestructCounter() { ++(*m_destructNumber); } 97
99 int get() const { return m_i; } 98 private:
100 99 int m_i;
101 private: 100 int* m_destructNumber;
102 int m_i;
103 int* m_destructNumber;
104 }; 101 };
105 102
106 using OwnPtrHashMap = HashMap<int, OwnPtr<DestructCounter>>; 103 using OwnPtrHashMap = HashMap<int, OwnPtr<DestructCounter>>;
107 104
108 TEST(HashMapTest, OwnPtrAsValue) 105 TEST(HashMapTest, OwnPtrAsValue) {
109 { 106 int destructNumber = 0;
110 int destructNumber = 0; 107 OwnPtrHashMap map;
111 OwnPtrHashMap map; 108 map.add(1, adoptPtr(new DestructCounter(1, &destructNumber)));
112 map.add(1, adoptPtr(new DestructCounter(1, &destructNumber))); 109 map.add(2, adoptPtr(new DestructCounter(2, &destructNumber)));
113 map.add(2, adoptPtr(new DestructCounter(2, &destructNumber))); 110
114 111 DestructCounter* counter1 = map.get(1);
115 DestructCounter* counter1 = map.get(1); 112 EXPECT_EQ(1, counter1->get());
116 EXPECT_EQ(1, counter1->get()); 113 DestructCounter* counter2 = map.get(2);
117 DestructCounter* counter2 = map.get(2); 114 EXPECT_EQ(2, counter2->get());
118 EXPECT_EQ(2, counter2->get()); 115 EXPECT_EQ(0, destructNumber);
119 EXPECT_EQ(0, destructNumber); 116
120 117 for (OwnPtrHashMap::iterator iter = map.begin(); iter != map.end(); ++iter) {
121 for (OwnPtrHashMap::iterator iter = map.begin(); iter != map.end(); ++iter) { 118 OwnPtr<DestructCounter>& ownCounter = iter->value;
122 OwnPtr<DestructCounter>& ownCounter = iter->value; 119 EXPECT_EQ(iter->key, ownCounter->get());
123 EXPECT_EQ(iter->key, ownCounter->get()); 120 }
124 } 121 ASSERT_EQ(0, destructNumber);
125 ASSERT_EQ(0, destructNumber); 122
126 123 OwnPtr<DestructCounter> ownCounter1 = map.take(1);
127 OwnPtr<DestructCounter> ownCounter1 = map.take(1); 124 EXPECT_EQ(ownCounter1.get(), counter1);
128 EXPECT_EQ(ownCounter1.get(), counter1); 125 EXPECT_FALSE(map.contains(1));
129 EXPECT_FALSE(map.contains(1)); 126 EXPECT_EQ(0, destructNumber);
130 EXPECT_EQ(0, destructNumber); 127
131 128 map.remove(2);
132 map.remove(2); 129 EXPECT_FALSE(map.contains(2));
133 EXPECT_FALSE(map.contains(2)); 130 EXPECT_EQ(0UL, map.size());
134 EXPECT_EQ(0UL, map.size()); 131 EXPECT_EQ(1, destructNumber);
135 EXPECT_EQ(1, destructNumber); 132
136 133 ownCounter1.clear();
137 ownCounter1.clear(); 134 EXPECT_EQ(2, destructNumber);
138 EXPECT_EQ(2, destructNumber);
139 } 135 }
140 136
141 class DummyRefCounted : public RefCounted<DummyRefCounted> { 137 class DummyRefCounted : public RefCounted<DummyRefCounted> {
142 public: 138 public:
143 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) { m_isDeleted = fa lse; } 139 DummyRefCounted(bool& isDeleted) : m_isDeleted(isDeleted) {
144 ~DummyRefCounted() 140 m_isDeleted = false;
145 { 141 }
146 ASSERT(!m_isDeleted); 142 ~DummyRefCounted() {
147 m_isDeleted = true; 143 ASSERT(!m_isDeleted);
148 } 144 m_isDeleted = true;
149 145 }
150 void ref() 146
151 { 147 void ref() {
152 ASSERT(!m_isDeleted); 148 ASSERT(!m_isDeleted);
153 WTF::RefCounted<DummyRefCounted>::ref(); 149 WTF::RefCounted<DummyRefCounted>::ref();
154 ++m_refInvokesCount; 150 ++m_refInvokesCount;
155 } 151 }
156 152
157 void deref() 153 void deref() {
158 { 154 ASSERT(!m_isDeleted);
159 ASSERT(!m_isDeleted); 155 WTF::RefCounted<DummyRefCounted>::deref();
160 WTF::RefCounted<DummyRefCounted>::deref(); 156 }
161 } 157
162 158 static int m_refInvokesCount;
163 static int m_refInvokesCount; 159
164 160 private:
165 private: 161 bool& m_isDeleted;
166 bool& m_isDeleted;
167 }; 162 };
168 163
169 int DummyRefCounted::m_refInvokesCount = 0; 164 int DummyRefCounted::m_refInvokesCount = 0;
170 165
171 TEST(HashMapTest, RefPtrAsKey) 166 TEST(HashMapTest, RefPtrAsKey) {
172 { 167 bool isDeleted = false;
173 bool isDeleted = false; 168 DummyRefCounted::m_refInvokesCount = 0;
174 DummyRefCounted::m_refInvokesCount = 0; 169 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted));
175 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); 170 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount);
176 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount); 171 HashMap<RefPtr<DummyRefCounted>, int> map;
177 HashMap<RefPtr<DummyRefCounted>, int> map; 172 map.add(ptr, 1);
178 map.add(ptr, 1); 173 // Referenced only once (to store a copy in the container).
179 // Referenced only once (to store a copy in the container). 174 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount);
180 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); 175 EXPECT_EQ(1, map.get(ptr));
181 EXPECT_EQ(1, map.get(ptr)); 176
182 177 DummyRefCounted* rawPtr = ptr.get();
183 DummyRefCounted* rawPtr = ptr.get(); 178
184 179 EXPECT_TRUE(map.contains(rawPtr));
185 EXPECT_TRUE(map.contains(rawPtr)); 180 EXPECT_NE(map.end(), map.find(rawPtr));
186 EXPECT_NE(map.end(), map.find(rawPtr)); 181 EXPECT_TRUE(map.contains(ptr));
187 EXPECT_TRUE(map.contains(ptr)); 182 EXPECT_NE(map.end(), map.find(ptr));
188 EXPECT_NE(map.end(), map.find(ptr)); 183 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount);
189 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); 184
190 185 ptr.clear();
191 ptr.clear(); 186 EXPECT_FALSE(isDeleted);
192 EXPECT_FALSE(isDeleted); 187
193 188 map.remove(rawPtr);
194 map.remove(rawPtr); 189 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount);
195 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); 190 EXPECT_TRUE(isDeleted);
196 EXPECT_TRUE(isDeleted); 191 EXPECT_TRUE(map.isEmpty());
197 EXPECT_TRUE(map.isEmpty()); 192 }
198 } 193
199 194 TEST(HashMaptest, RemoveAdd) {
200 TEST(HashMaptest, RemoveAdd) 195 DummyRefCounted::m_refInvokesCount = 0;
201 { 196 bool isDeleted = false;
202 DummyRefCounted::m_refInvokesCount = 0; 197
203 bool isDeleted = false; 198 typedef HashMap<int, RefPtr<DummyRefCounted>> Map;
204 199 Map map;
205 typedef HashMap<int, RefPtr<DummyRefCounted>> Map; 200
206 Map map; 201 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted));
207 202 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount);
208 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); 203
209 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount); 204 map.add(1, ptr);
210 205 // Referenced only once (to store a copy in the container).
211 map.add(1, ptr); 206 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount);
212 // Referenced only once (to store a copy in the container). 207 EXPECT_EQ(ptr, map.get(1));
213 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); 208
214 EXPECT_EQ(ptr, map.get(1)); 209 ptr.clear();
215 210 EXPECT_FALSE(isDeleted);
216 ptr.clear(); 211
217 EXPECT_FALSE(isDeleted); 212 map.remove(1);
218 213 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount);
219 map.remove(1); 214 EXPECT_TRUE(isDeleted);
220 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); 215 EXPECT_TRUE(map.isEmpty());
221 EXPECT_TRUE(isDeleted); 216
222 EXPECT_TRUE(map.isEmpty()); 217 // Add and remove until the deleted slot is reused.
223 218 for (int i = 1; i < 100; i++) {
224 // Add and remove until the deleted slot is reused. 219 bool isDeleted2 = false;
225 for (int i = 1; i < 100; i++) { 220 RefPtr<DummyRefCounted> ptr2 = adoptRef(new DummyRefCounted(isDeleted2));
226 bool isDeleted2 = false; 221 map.add(i, ptr2);
227 RefPtr<DummyRefCounted> ptr2 = adoptRef(new DummyRefCounted(isDeleted2)) ; 222 EXPECT_FALSE(isDeleted2);
228 map.add(i, ptr2); 223 ptr2.clear();
229 EXPECT_FALSE(isDeleted2); 224 EXPECT_FALSE(isDeleted2);
230 ptr2.clear(); 225 map.remove(i);
231 EXPECT_FALSE(isDeleted2); 226 EXPECT_TRUE(isDeleted2);
232 map.remove(i); 227 }
233 EXPECT_TRUE(isDeleted2);
234 }
235 } 228 }
236 229
237 class SimpleClass { 230 class SimpleClass {
238 public: 231 public:
239 explicit SimpleClass(int v) : m_v(v) { } 232 explicit SimpleClass(int v) : m_v(v) {}
240 int v() { return m_v; } 233 int v() { return m_v; }
241 234
242 private: 235 private:
243 int m_v; 236 int m_v;
244 }; 237 };
245 using IntSimpleMap = HashMap<int, OwnPtr<SimpleClass>>; 238 using IntSimpleMap = HashMap<int, OwnPtr<SimpleClass>>;
246 239
247 TEST(HashMapTest, AddResult) 240 TEST(HashMapTest, AddResult) {
248 { 241 IntSimpleMap map;
249 IntSimpleMap map; 242 IntSimpleMap::AddResult result = map.add(1, nullptr);
250 IntSimpleMap::AddResult result = map.add(1, nullptr); 243 EXPECT_TRUE(result.isNewEntry);
251 EXPECT_TRUE(result.isNewEntry); 244 EXPECT_EQ(1, result.storedValue->key);
252 EXPECT_EQ(1, result.storedValue->key); 245 EXPECT_EQ(0, result.storedValue->value.get());
253 EXPECT_EQ(0, result.storedValue->value.get()); 246
254 247 SimpleClass* simple1 = new SimpleClass(1);
255 SimpleClass* simple1 = new SimpleClass(1); 248 result.storedValue->value = adoptPtr(simple1);
256 result.storedValue->value = adoptPtr(simple1); 249 EXPECT_EQ(simple1, map.get(1));
257 EXPECT_EQ(simple1, map.get(1)); 250
258 251 IntSimpleMap::AddResult result2 = map.add(1, adoptPtr(new SimpleClass(2)));
259 IntSimpleMap::AddResult result2 = map.add(1, adoptPtr(new SimpleClass(2))); 252 EXPECT_FALSE(result2.isNewEntry);
260 EXPECT_FALSE(result2.isNewEntry); 253 EXPECT_EQ(1, result.storedValue->key);
261 EXPECT_EQ(1, result.storedValue->key); 254 EXPECT_EQ(1, result.storedValue->value->v());
262 EXPECT_EQ(1, result.storedValue->value->v()); 255 EXPECT_EQ(1, map.get(1)->v());
263 EXPECT_EQ(1, map.get(1)->v()); 256 }
264 } 257
265 258 TEST(HashMapTest, AddResultVectorValue) {
266 TEST(HashMapTest, AddResultVectorValue) 259 using IntVectorMap = HashMap<int, Vector<int>>;
267 { 260 IntVectorMap map;
268 using IntVectorMap = HashMap<int, Vector<int>>; 261 IntVectorMap::AddResult result = map.add(1, Vector<int>());
269 IntVectorMap map; 262 EXPECT_TRUE(result.isNewEntry);
270 IntVectorMap::AddResult result = map.add(1, Vector<int>()); 263 EXPECT_EQ(1, result.storedValue->key);
271 EXPECT_TRUE(result.isNewEntry); 264 EXPECT_EQ(0u, result.storedValue->value.size());
272 EXPECT_EQ(1, result.storedValue->key); 265
273 EXPECT_EQ(0u, result.storedValue->value.size()); 266 result.storedValue->value.append(11);
274 267 EXPECT_EQ(1u, map.find(1)->value.size());
275 result.storedValue->value.append(11); 268 EXPECT_EQ(11, map.find(1)->value.first());
276 EXPECT_EQ(1u, map.find(1)->value.size()); 269
277 EXPECT_EQ(11, map.find(1)->value.first()); 270 IntVectorMap::AddResult result2 = map.add(1, Vector<int>());
278 271 EXPECT_FALSE(result2.isNewEntry);
279 IntVectorMap::AddResult result2 = map.add(1, Vector<int>()); 272 EXPECT_EQ(1, result.storedValue->key);
280 EXPECT_FALSE(result2.isNewEntry); 273 EXPECT_EQ(1u, result.storedValue->value.size());
281 EXPECT_EQ(1, result.storedValue->key); 274 EXPECT_EQ(11, result.storedValue->value.first());
282 EXPECT_EQ(1u, result.storedValue->value.size()); 275 EXPECT_EQ(11, map.find(1)->value.first());
283 EXPECT_EQ(11, result.storedValue->value.first());
284 EXPECT_EQ(11, map.find(1)->value.first());
285 } 276 }
286 277
287 class InstanceCounter { 278 class InstanceCounter {
288 public: 279 public:
289 InstanceCounter() { ++counter; } 280 InstanceCounter() { ++counter; }
290 InstanceCounter(const InstanceCounter& another) { ++counter; } 281 InstanceCounter(const InstanceCounter& another) { ++counter; }
291 ~InstanceCounter() { --counter; } 282 ~InstanceCounter() { --counter; }
292 static int counter; 283 static int counter;
293 }; 284 };
294 int InstanceCounter::counter = 0; 285 int InstanceCounter::counter = 0;
295 286
296 TEST(HashMapTest, ValueTypeDestructed) 287 TEST(HashMapTest, ValueTypeDestructed) {
297 { 288 InstanceCounter::counter = 0;
298 InstanceCounter::counter = 0; 289 HashMap<int, InstanceCounter> map;
299 HashMap<int, InstanceCounter> map; 290 map.set(1, InstanceCounter());
300 map.set(1, InstanceCounter()); 291 map.clear();
301 map.clear(); 292 EXPECT_EQ(0, InstanceCounter::counter);
302 EXPECT_EQ(0, InstanceCounter::counter); 293 }
303 } 294
304 295 } // anonymous namespace
305 } // anonymous namespace 296
306 297 } // namespace WTF
307 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/HashMap.h ('k') | third_party/WebKit/Source/wtf/HashSet.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698