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

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

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