| 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 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #include <memory> | 33 #include <memory> |
| 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 IntHashMap map; | 42 IntHashMap map; |
| 43 map.add(1, 2); | 43 map.insert(1, 2); |
| 44 EXPECT_TRUE(map.begin() != map.end()); | 44 EXPECT_TRUE(map.begin() != map.end()); |
| 45 EXPECT_FALSE(map.begin() == map.end()); | 45 EXPECT_FALSE(map.begin() == map.end()); |
| 46 | 46 |
| 47 IntHashMap::const_iterator begin = map.begin(); | 47 IntHashMap::const_iterator begin = map.begin(); |
| 48 EXPECT_TRUE(begin == map.begin()); | 48 EXPECT_TRUE(begin == map.begin()); |
| 49 EXPECT_TRUE(map.begin() == begin); | 49 EXPECT_TRUE(map.begin() == begin); |
| 50 EXPECT_TRUE(begin != map.end()); | 50 EXPECT_TRUE(begin != map.end()); |
| 51 EXPECT_TRUE(map.end() != begin); | 51 EXPECT_TRUE(map.end() != begin); |
| 52 EXPECT_FALSE(begin != map.begin()); | 52 EXPECT_FALSE(begin != map.begin()); |
| 53 EXPECT_FALSE(map.begin() != begin); | 53 EXPECT_FALSE(map.begin() != begin); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 71 // The "clobber" key here is one that ends up stealing the bucket that the -0 | 71 // The "clobber" key here is one that ends up stealing the bucket that the -0 |
| 72 // key originally wants to be in. This makes the 0 and -0 keys collide and | 72 // key originally wants to be in. This makes the 0 and -0 keys collide and |
| 73 // the test then fails unless the FloatHash::equals() implementation can | 73 // the test then fails unless the FloatHash::equals() implementation can |
| 74 // distinguish them. | 74 // distinguish them. |
| 75 const double clobberKey = 6; | 75 const double clobberKey = 6; |
| 76 const double zeroKey = 0; | 76 const double zeroKey = 0; |
| 77 const double negativeZeroKey = -zeroKey; | 77 const double negativeZeroKey = -zeroKey; |
| 78 | 78 |
| 79 DoubleHashMap map; | 79 DoubleHashMap map; |
| 80 | 80 |
| 81 map.add(clobberKey, 1); | 81 map.insert(clobberKey, 1); |
| 82 map.add(zeroKey, 2); | 82 map.insert(zeroKey, 2); |
| 83 map.add(negativeZeroKey, 3); | 83 map.insert(negativeZeroKey, 3); |
| 84 | 84 |
| 85 EXPECT_EQ(bucketForKey(clobberKey), bucketForKey(negativeZeroKey)); | 85 EXPECT_EQ(bucketForKey(clobberKey), bucketForKey(negativeZeroKey)); |
| 86 EXPECT_EQ(1, map.get(clobberKey)); | 86 EXPECT_EQ(1, map.get(clobberKey)); |
| 87 EXPECT_EQ(2, map.get(zeroKey)); | 87 EXPECT_EQ(2, map.get(zeroKey)); |
| 88 EXPECT_EQ(3, map.get(negativeZeroKey)); | 88 EXPECT_EQ(3, map.get(negativeZeroKey)); |
| 89 } | 89 } |
| 90 | 90 |
| 91 class DestructCounter { | 91 class DestructCounter { |
| 92 public: | 92 public: |
| 93 explicit DestructCounter(int i, int* destructNumber) | 93 explicit DestructCounter(int i, int* destructNumber) |
| 94 : m_i(i), m_destructNumber(destructNumber) {} | 94 : m_i(i), m_destructNumber(destructNumber) {} |
| 95 | 95 |
| 96 ~DestructCounter() { ++(*m_destructNumber); } | 96 ~DestructCounter() { ++(*m_destructNumber); } |
| 97 int get() const { return m_i; } | 97 int get() const { return m_i; } |
| 98 | 98 |
| 99 private: | 99 private: |
| 100 int m_i; | 100 int m_i; |
| 101 int* m_destructNumber; | 101 int* m_destructNumber; |
| 102 }; | 102 }; |
| 103 | 103 |
| 104 using OwnPtrHashMap = HashMap<int, std::unique_ptr<DestructCounter>>; | 104 using OwnPtrHashMap = HashMap<int, std::unique_ptr<DestructCounter>>; |
| 105 | 105 |
| 106 TEST(HashMapTest, OwnPtrAsValue) { | 106 TEST(HashMapTest, OwnPtrAsValue) { |
| 107 int destructNumber = 0; | 107 int destructNumber = 0; |
| 108 OwnPtrHashMap map; | 108 OwnPtrHashMap map; |
| 109 map.add(1, WTF::wrapUnique(new DestructCounter(1, &destructNumber))); | 109 map.insert(1, WTF::wrapUnique(new DestructCounter(1, &destructNumber))); |
| 110 map.add(2, WTF::wrapUnique(new DestructCounter(2, &destructNumber))); | 110 map.insert(2, WTF::wrapUnique(new DestructCounter(2, &destructNumber))); |
| 111 | 111 |
| 112 DestructCounter* counter1 = map.get(1); | 112 DestructCounter* counter1 = map.get(1); |
| 113 EXPECT_EQ(1, counter1->get()); | 113 EXPECT_EQ(1, counter1->get()); |
| 114 DestructCounter* counter2 = map.get(2); | 114 DestructCounter* counter2 = map.get(2); |
| 115 EXPECT_EQ(2, counter2->get()); | 115 EXPECT_EQ(2, counter2->get()); |
| 116 EXPECT_EQ(0, destructNumber); | 116 EXPECT_EQ(0, destructNumber); |
| 117 | 117 |
| 118 for (OwnPtrHashMap::iterator iter = map.begin(); iter != map.end(); ++iter) { | 118 for (OwnPtrHashMap::iterator iter = map.begin(); iter != map.end(); ++iter) { |
| 119 std::unique_ptr<DestructCounter>& ownCounter = iter->value; | 119 std::unique_ptr<DestructCounter>& ownCounter = iter->value; |
| 120 EXPECT_EQ(iter->key, ownCounter->get()); | 120 EXPECT_EQ(iter->key, ownCounter->get()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 }; | 163 }; |
| 164 | 164 |
| 165 int DummyRefCounted::m_refInvokesCount = 0; | 165 int DummyRefCounted::m_refInvokesCount = 0; |
| 166 | 166 |
| 167 TEST(HashMapTest, RefPtrAsKey) { | 167 TEST(HashMapTest, RefPtrAsKey) { |
| 168 bool isDeleted = false; | 168 bool isDeleted = false; |
| 169 DummyRefCounted::m_refInvokesCount = 0; | 169 DummyRefCounted::m_refInvokesCount = 0; |
| 170 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); | 170 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); |
| 171 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount); | 171 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount); |
| 172 HashMap<RefPtr<DummyRefCounted>, int> map; | 172 HashMap<RefPtr<DummyRefCounted>, int> map; |
| 173 map.add(ptr, 1); | 173 map.insert(ptr, 1); |
| 174 // Referenced only once (to store a copy in the container). | 174 // Referenced only once (to store a copy in the container). |
| 175 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | 175 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); |
| 176 EXPECT_EQ(1, map.get(ptr)); | 176 EXPECT_EQ(1, map.get(ptr)); |
| 177 | 177 |
| 178 DummyRefCounted* rawPtr = ptr.get(); | 178 DummyRefCounted* rawPtr = ptr.get(); |
| 179 | 179 |
| 180 EXPECT_TRUE(map.contains(rawPtr)); | 180 EXPECT_TRUE(map.contains(rawPtr)); |
| 181 EXPECT_NE(map.end(), map.find(rawPtr)); | 181 EXPECT_NE(map.end(), map.find(rawPtr)); |
| 182 EXPECT_TRUE(map.contains(ptr)); | 182 EXPECT_TRUE(map.contains(ptr)); |
| 183 EXPECT_NE(map.end(), map.find(ptr)); | 183 EXPECT_NE(map.end(), map.find(ptr)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 195 TEST(HashMaptest, RemoveAdd) { | 195 TEST(HashMaptest, RemoveAdd) { |
| 196 DummyRefCounted::m_refInvokesCount = 0; | 196 DummyRefCounted::m_refInvokesCount = 0; |
| 197 bool isDeleted = false; | 197 bool isDeleted = false; |
| 198 | 198 |
| 199 typedef HashMap<int, RefPtr<DummyRefCounted>> Map; | 199 typedef HashMap<int, RefPtr<DummyRefCounted>> Map; |
| 200 Map map; | 200 Map map; |
| 201 | 201 |
| 202 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); | 202 RefPtr<DummyRefCounted> ptr = adoptRef(new DummyRefCounted(isDeleted)); |
| 203 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount); | 203 EXPECT_EQ(0, DummyRefCounted::m_refInvokesCount); |
| 204 | 204 |
| 205 map.add(1, ptr); | 205 map.insert(1, ptr); |
| 206 // Referenced only once (to store a copy in the container). | 206 // Referenced only once (to store a copy in the container). |
| 207 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | 207 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); |
| 208 EXPECT_EQ(ptr, map.get(1)); | 208 EXPECT_EQ(ptr, map.get(1)); |
| 209 | 209 |
| 210 ptr.clear(); | 210 ptr.clear(); |
| 211 EXPECT_FALSE(isDeleted); | 211 EXPECT_FALSE(isDeleted); |
| 212 | 212 |
| 213 map.erase(1); | 213 map.erase(1); |
| 214 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | 214 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); |
| 215 EXPECT_TRUE(isDeleted); | 215 EXPECT_TRUE(isDeleted); |
| 216 EXPECT_TRUE(map.isEmpty()); | 216 EXPECT_TRUE(map.isEmpty()); |
| 217 | 217 |
| 218 // Add and remove until the deleted slot is reused. | 218 // Add and remove until the deleted slot is reused. |
| 219 for (int i = 1; i < 100; i++) { | 219 for (int i = 1; i < 100; i++) { |
| 220 bool isDeleted2 = false; | 220 bool isDeleted2 = false; |
| 221 RefPtr<DummyRefCounted> ptr2 = adoptRef(new DummyRefCounted(isDeleted2)); | 221 RefPtr<DummyRefCounted> ptr2 = adoptRef(new DummyRefCounted(isDeleted2)); |
| 222 map.add(i, ptr2); | 222 map.insert(i, ptr2); |
| 223 EXPECT_FALSE(isDeleted2); | 223 EXPECT_FALSE(isDeleted2); |
| 224 ptr2.clear(); | 224 ptr2.clear(); |
| 225 EXPECT_FALSE(isDeleted2); | 225 EXPECT_FALSE(isDeleted2); |
| 226 map.erase(i); | 226 map.erase(i); |
| 227 EXPECT_TRUE(isDeleted2); | 227 EXPECT_TRUE(isDeleted2); |
| 228 } | 228 } |
| 229 } | 229 } |
| 230 | 230 |
| 231 class SimpleClass { | 231 class SimpleClass { |
| 232 public: | 232 public: |
| 233 explicit SimpleClass(int v) : m_v(v) {} | 233 explicit SimpleClass(int v) : m_v(v) {} |
| 234 int v() { return m_v; } | 234 int v() { return m_v; } |
| 235 | 235 |
| 236 private: | 236 private: |
| 237 int m_v; | 237 int m_v; |
| 238 }; | 238 }; |
| 239 using IntSimpleMap = HashMap<int, std::unique_ptr<SimpleClass>>; | 239 using IntSimpleMap = HashMap<int, std::unique_ptr<SimpleClass>>; |
| 240 | 240 |
| 241 TEST(HashMapTest, AddResult) { | 241 TEST(HashMapTest, AddResult) { |
| 242 IntSimpleMap map; | 242 IntSimpleMap map; |
| 243 IntSimpleMap::AddResult result = map.add(1, nullptr); | 243 IntSimpleMap::AddResult result = map.insert(1, nullptr); |
| 244 EXPECT_TRUE(result.isNewEntry); | 244 EXPECT_TRUE(result.isNewEntry); |
| 245 EXPECT_EQ(1, result.storedValue->key); | 245 EXPECT_EQ(1, result.storedValue->key); |
| 246 EXPECT_EQ(0, result.storedValue->value.get()); | 246 EXPECT_EQ(0, result.storedValue->value.get()); |
| 247 | 247 |
| 248 SimpleClass* simple1 = new SimpleClass(1); | 248 SimpleClass* simple1 = new SimpleClass(1); |
| 249 result.storedValue->value = WTF::wrapUnique(simple1); | 249 result.storedValue->value = WTF::wrapUnique(simple1); |
| 250 EXPECT_EQ(simple1, map.get(1)); | 250 EXPECT_EQ(simple1, map.get(1)); |
| 251 | 251 |
| 252 IntSimpleMap::AddResult result2 = map.add(1, WTF::makeUnique<SimpleClass>(2)); | 252 IntSimpleMap::AddResult result2 = |
| 253 map.insert(1, WTF::makeUnique<SimpleClass>(2)); |
| 253 EXPECT_FALSE(result2.isNewEntry); | 254 EXPECT_FALSE(result2.isNewEntry); |
| 254 EXPECT_EQ(1, result.storedValue->key); | 255 EXPECT_EQ(1, result.storedValue->key); |
| 255 EXPECT_EQ(1, result.storedValue->value->v()); | 256 EXPECT_EQ(1, result.storedValue->value->v()); |
| 256 EXPECT_EQ(1, map.get(1)->v()); | 257 EXPECT_EQ(1, map.get(1)->v()); |
| 257 } | 258 } |
| 258 | 259 |
| 259 TEST(HashMapTest, AddResultVectorValue) { | 260 TEST(HashMapTest, AddResultVectorValue) { |
| 260 using IntVectorMap = HashMap<int, Vector<int>>; | 261 using IntVectorMap = HashMap<int, Vector<int>>; |
| 261 IntVectorMap map; | 262 IntVectorMap map; |
| 262 IntVectorMap::AddResult result = map.add(1, Vector<int>()); | 263 IntVectorMap::AddResult result = map.insert(1, Vector<int>()); |
| 263 EXPECT_TRUE(result.isNewEntry); | 264 EXPECT_TRUE(result.isNewEntry); |
| 264 EXPECT_EQ(1, result.storedValue->key); | 265 EXPECT_EQ(1, result.storedValue->key); |
| 265 EXPECT_EQ(0u, result.storedValue->value.size()); | 266 EXPECT_EQ(0u, result.storedValue->value.size()); |
| 266 | 267 |
| 267 result.storedValue->value.push_back(11); | 268 result.storedValue->value.push_back(11); |
| 268 EXPECT_EQ(1u, map.find(1)->value.size()); | 269 EXPECT_EQ(1u, map.find(1)->value.size()); |
| 269 EXPECT_EQ(11, map.find(1)->value.front()); | 270 EXPECT_EQ(11, map.find(1)->value.front()); |
| 270 | 271 |
| 271 IntVectorMap::AddResult result2 = map.add(1, Vector<int>()); | 272 IntVectorMap::AddResult result2 = map.insert(1, Vector<int>()); |
| 272 EXPECT_FALSE(result2.isNewEntry); | 273 EXPECT_FALSE(result2.isNewEntry); |
| 273 EXPECT_EQ(1, result.storedValue->key); | 274 EXPECT_EQ(1, result.storedValue->key); |
| 274 EXPECT_EQ(1u, result.storedValue->value.size()); | 275 EXPECT_EQ(1u, result.storedValue->value.size()); |
| 275 EXPECT_EQ(11, result.storedValue->value.front()); | 276 EXPECT_EQ(11, result.storedValue->value.front()); |
| 276 EXPECT_EQ(11, map.find(1)->value.front()); | 277 EXPECT_EQ(11, map.find(1)->value.front()); |
| 277 } | 278 } |
| 278 | 279 |
| 279 class InstanceCounter { | 280 class InstanceCounter { |
| 280 public: | 281 public: |
| 281 InstanceCounter() { ++counter; } | 282 InstanceCounter() { ++counter; } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 struct DefaultHash<MoveOnly> { | 355 struct DefaultHash<MoveOnly> { |
| 355 using Hash = MoveOnlyHash; | 356 using Hash = MoveOnlyHash; |
| 356 }; | 357 }; |
| 357 | 358 |
| 358 namespace { | 359 namespace { |
| 359 | 360 |
| 360 TEST(HashMapTest, MoveOnlyValueType) { | 361 TEST(HashMapTest, MoveOnlyValueType) { |
| 361 using TheMap = HashMap<int, MoveOnly>; | 362 using TheMap = HashMap<int, MoveOnly>; |
| 362 TheMap map; | 363 TheMap map; |
| 363 { | 364 { |
| 364 TheMap::AddResult addResult = map.add(1, MoveOnly(10)); | 365 TheMap::AddResult addResult = map.insert(1, MoveOnly(10)); |
| 365 EXPECT_TRUE(addResult.isNewEntry); | 366 EXPECT_TRUE(addResult.isNewEntry); |
| 366 EXPECT_EQ(1, addResult.storedValue->key); | 367 EXPECT_EQ(1, addResult.storedValue->key); |
| 367 EXPECT_EQ(10, addResult.storedValue->value.value()); | 368 EXPECT_EQ(10, addResult.storedValue->value.value()); |
| 368 } | 369 } |
| 369 auto iter = map.find(1); | 370 auto iter = map.find(1); |
| 370 ASSERT_TRUE(iter != map.end()); | 371 ASSERT_TRUE(iter != map.end()); |
| 371 EXPECT_EQ(1, iter->key); | 372 EXPECT_EQ(1, iter->key); |
| 372 EXPECT_EQ(10, iter->value.value()); | 373 EXPECT_EQ(10, iter->value.value()); |
| 373 | 374 |
| 374 iter = map.find(2); | 375 iter = map.find(2); |
| 375 EXPECT_TRUE(iter == map.end()); | 376 EXPECT_TRUE(iter == map.end()); |
| 376 | 377 |
| 377 // Try to add more to trigger rehashing. | 378 // Try to add more to trigger rehashing. |
| 378 for (int i = 2; i < 32; ++i) { | 379 for (int i = 2; i < 32; ++i) { |
| 379 TheMap::AddResult addResult = map.add(i, MoveOnly(i * 10)); | 380 TheMap::AddResult addResult = map.insert(i, MoveOnly(i * 10)); |
| 380 EXPECT_TRUE(addResult.isNewEntry); | 381 EXPECT_TRUE(addResult.isNewEntry); |
| 381 EXPECT_EQ(i, addResult.storedValue->key); | 382 EXPECT_EQ(i, addResult.storedValue->key); |
| 382 EXPECT_EQ(i * 10, addResult.storedValue->value.value()); | 383 EXPECT_EQ(i * 10, addResult.storedValue->value.value()); |
| 383 } | 384 } |
| 384 | 385 |
| 385 iter = map.find(1); | 386 iter = map.find(1); |
| 386 ASSERT_TRUE(iter != map.end()); | 387 ASSERT_TRUE(iter != map.end()); |
| 387 EXPECT_EQ(1, iter->key); | 388 EXPECT_EQ(1, iter->key); |
| 388 EXPECT_EQ(10, iter->value.value()); | 389 EXPECT_EQ(10, iter->value.value()); |
| 389 | 390 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 410 | 411 |
| 411 map.clear(); | 412 map.clear(); |
| 412 } | 413 } |
| 413 | 414 |
| 414 TEST(HashMapTest, MoveOnlyKeyType) { | 415 TEST(HashMapTest, MoveOnlyKeyType) { |
| 415 // The content of this test is similar to the test above, except that the | 416 // The content of this test is similar to the test above, except that the |
| 416 // types of key and value are swapped. | 417 // types of key and value are swapped. |
| 417 using TheMap = HashMap<MoveOnly, int>; | 418 using TheMap = HashMap<MoveOnly, int>; |
| 418 TheMap map; | 419 TheMap map; |
| 419 { | 420 { |
| 420 TheMap::AddResult addResult = map.add(MoveOnly(1), 10); | 421 TheMap::AddResult addResult = map.insert(MoveOnly(1), 10); |
| 421 EXPECT_TRUE(addResult.isNewEntry); | 422 EXPECT_TRUE(addResult.isNewEntry); |
| 422 EXPECT_EQ(1, addResult.storedValue->key.value()); | 423 EXPECT_EQ(1, addResult.storedValue->key.value()); |
| 423 EXPECT_EQ(10, addResult.storedValue->value); | 424 EXPECT_EQ(10, addResult.storedValue->value); |
| 424 } | 425 } |
| 425 auto iter = map.find(MoveOnly(1)); | 426 auto iter = map.find(MoveOnly(1)); |
| 426 ASSERT_TRUE(iter != map.end()); | 427 ASSERT_TRUE(iter != map.end()); |
| 427 EXPECT_EQ(1, iter->key.value()); | 428 EXPECT_EQ(1, iter->key.value()); |
| 428 EXPECT_EQ(10, iter->value); | 429 EXPECT_EQ(10, iter->value); |
| 429 | 430 |
| 430 iter = map.find(MoveOnly(2)); | 431 iter = map.find(MoveOnly(2)); |
| 431 EXPECT_TRUE(iter == map.end()); | 432 EXPECT_TRUE(iter == map.end()); |
| 432 | 433 |
| 433 for (int i = 2; i < 32; ++i) { | 434 for (int i = 2; i < 32; ++i) { |
| 434 TheMap::AddResult addResult = map.add(MoveOnly(i), i * 10); | 435 TheMap::AddResult addResult = map.insert(MoveOnly(i), i * 10); |
| 435 EXPECT_TRUE(addResult.isNewEntry); | 436 EXPECT_TRUE(addResult.isNewEntry); |
| 436 EXPECT_EQ(i, addResult.storedValue->key.value()); | 437 EXPECT_EQ(i, addResult.storedValue->key.value()); |
| 437 EXPECT_EQ(i * 10, addResult.storedValue->value); | 438 EXPECT_EQ(i * 10, addResult.storedValue->value); |
| 438 } | 439 } |
| 439 | 440 |
| 440 iter = map.find(MoveOnly(1)); | 441 iter = map.find(MoveOnly(1)); |
| 441 ASSERT_TRUE(iter != map.end()); | 442 ASSERT_TRUE(iter != map.end()); |
| 442 EXPECT_EQ(1, iter->key.value()); | 443 EXPECT_EQ(1, iter->key.value()); |
| 443 EXPECT_EQ(10, iter->value); | 444 EXPECT_EQ(10, iter->value); |
| 444 | 445 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 return *this; | 482 return *this; |
| 482 } | 483 } |
| 483 | 484 |
| 484 private: | 485 private: |
| 485 int* m_counter; | 486 int* m_counter; |
| 486 }; | 487 }; |
| 487 | 488 |
| 488 TEST(HashMapTest, MoveShouldNotMakeCopy) { | 489 TEST(HashMapTest, MoveShouldNotMakeCopy) { |
| 489 HashMap<int, CountCopy> map; | 490 HashMap<int, CountCopy> map; |
| 490 int counter = 0; | 491 int counter = 0; |
| 491 map.add(1, CountCopy(counter)); | 492 map.insert(1, CountCopy(counter)); |
| 492 | 493 |
| 493 HashMap<int, CountCopy> other(map); | 494 HashMap<int, CountCopy> other(map); |
| 494 counter = 0; | 495 counter = 0; |
| 495 map = std::move(other); | 496 map = std::move(other); |
| 496 EXPECT_EQ(0, counter); | 497 EXPECT_EQ(0, counter); |
| 497 | 498 |
| 498 counter = 0; | 499 counter = 0; |
| 499 HashMap<int, CountCopy> yetAnother(std::move(map)); | 500 HashMap<int, CountCopy> yetAnother(std::move(map)); |
| 500 EXPECT_EQ(0, counter); | 501 EXPECT_EQ(0, counter); |
| 501 } | 502 } |
| 502 | 503 |
| 503 TEST(HashMapTest, UniquePtrAsKey) { | 504 TEST(HashMapTest, UniquePtrAsKey) { |
| 504 using Pointer = std::unique_ptr<int>; | 505 using Pointer = std::unique_ptr<int>; |
| 505 using Map = HashMap<Pointer, int>; | 506 using Map = HashMap<Pointer, int>; |
| 506 Map map; | 507 Map map; |
| 507 int* onePointer = new int(1); | 508 int* onePointer = new int(1); |
| 508 { | 509 { |
| 509 Map::AddResult addResult = map.add(Pointer(onePointer), 1); | 510 Map::AddResult addResult = map.insert(Pointer(onePointer), 1); |
| 510 EXPECT_TRUE(addResult.isNewEntry); | 511 EXPECT_TRUE(addResult.isNewEntry); |
| 511 EXPECT_EQ(onePointer, addResult.storedValue->key.get()); | 512 EXPECT_EQ(onePointer, addResult.storedValue->key.get()); |
| 512 EXPECT_EQ(1, *addResult.storedValue->key); | 513 EXPECT_EQ(1, *addResult.storedValue->key); |
| 513 EXPECT_EQ(1, addResult.storedValue->value); | 514 EXPECT_EQ(1, addResult.storedValue->value); |
| 514 } | 515 } |
| 515 auto iter = map.find(onePointer); | 516 auto iter = map.find(onePointer); |
| 516 ASSERT_TRUE(iter != map.end()); | 517 ASSERT_TRUE(iter != map.end()); |
| 517 EXPECT_EQ(onePointer, iter->key.get()); | 518 EXPECT_EQ(onePointer, iter->key.get()); |
| 518 EXPECT_EQ(1, iter->value); | 519 EXPECT_EQ(1, iter->value); |
| 519 | 520 |
| 520 Pointer nonexistent(new int(42)); | 521 Pointer nonexistent(new int(42)); |
| 521 iter = map.find(nonexistent.get()); | 522 iter = map.find(nonexistent.get()); |
| 522 EXPECT_TRUE(iter == map.end()); | 523 EXPECT_TRUE(iter == map.end()); |
| 523 | 524 |
| 524 // Insert more to cause a rehash. | 525 // Insert more to cause a rehash. |
| 525 for (int i = 2; i < 32; ++i) { | 526 for (int i = 2; i < 32; ++i) { |
| 526 Map::AddResult addResult = map.add(Pointer(new int(i)), i); | 527 Map::AddResult addResult = map.insert(Pointer(new int(i)), i); |
| 527 EXPECT_TRUE(addResult.isNewEntry); | 528 EXPECT_TRUE(addResult.isNewEntry); |
| 528 EXPECT_EQ(i, *addResult.storedValue->key); | 529 EXPECT_EQ(i, *addResult.storedValue->key); |
| 529 EXPECT_EQ(i, addResult.storedValue->value); | 530 EXPECT_EQ(i, addResult.storedValue->value); |
| 530 } | 531 } |
| 531 | 532 |
| 532 iter = map.find(onePointer); | 533 iter = map.find(onePointer); |
| 533 ASSERT_TRUE(iter != map.end()); | 534 ASSERT_TRUE(iter != map.end()); |
| 534 EXPECT_EQ(onePointer, iter->key.get()); | 535 EXPECT_EQ(onePointer, iter->key.get()); |
| 535 EXPECT_EQ(1, iter->value); | 536 EXPECT_EQ(1, iter->value); |
| 536 | 537 |
| 537 EXPECT_EQ(1, map.take(onePointer)); | 538 EXPECT_EQ(1, map.take(onePointer)); |
| 538 // From now on, |onePointer| is a dangling pointer. | 539 // From now on, |onePointer| is a dangling pointer. |
| 539 | 540 |
| 540 iter = map.find(onePointer); | 541 iter = map.find(onePointer); |
| 541 EXPECT_TRUE(iter == map.end()); | 542 EXPECT_TRUE(iter == map.end()); |
| 542 } | 543 } |
| 543 | 544 |
| 544 TEST(HashMapTest, UniquePtrAsValue) { | 545 TEST(HashMapTest, UniquePtrAsValue) { |
| 545 using Pointer = std::unique_ptr<int>; | 546 using Pointer = std::unique_ptr<int>; |
| 546 using Map = HashMap<int, Pointer>; | 547 using Map = HashMap<int, Pointer>; |
| 547 Map map; | 548 Map map; |
| 548 { | 549 { |
| 549 Map::AddResult addResult = map.add(1, Pointer(new int(1))); | 550 Map::AddResult addResult = map.insert(1, Pointer(new int(1))); |
| 550 EXPECT_TRUE(addResult.isNewEntry); | 551 EXPECT_TRUE(addResult.isNewEntry); |
| 551 EXPECT_EQ(1, addResult.storedValue->key); | 552 EXPECT_EQ(1, addResult.storedValue->key); |
| 552 EXPECT_EQ(1, *addResult.storedValue->value); | 553 EXPECT_EQ(1, *addResult.storedValue->value); |
| 553 } | 554 } |
| 554 auto iter = map.find(1); | 555 auto iter = map.find(1); |
| 555 ASSERT_TRUE(iter != map.end()); | 556 ASSERT_TRUE(iter != map.end()); |
| 556 EXPECT_EQ(1, iter->key); | 557 EXPECT_EQ(1, iter->key); |
| 557 EXPECT_EQ(1, *iter->value); | 558 EXPECT_EQ(1, *iter->value); |
| 558 | 559 |
| 559 int* onePointer = map.get(1); | 560 int* onePointer = map.get(1); |
| 560 EXPECT_TRUE(onePointer); | 561 EXPECT_TRUE(onePointer); |
| 561 EXPECT_EQ(1, *onePointer); | 562 EXPECT_EQ(1, *onePointer); |
| 562 | 563 |
| 563 iter = map.find(42); | 564 iter = map.find(42); |
| 564 EXPECT_TRUE(iter == map.end()); | 565 EXPECT_TRUE(iter == map.end()); |
| 565 | 566 |
| 566 for (int i = 2; i < 32; ++i) { | 567 for (int i = 2; i < 32; ++i) { |
| 567 Map::AddResult addResult = map.add(i, Pointer(new int(i))); | 568 Map::AddResult addResult = map.insert(i, Pointer(new int(i))); |
| 568 EXPECT_TRUE(addResult.isNewEntry); | 569 EXPECT_TRUE(addResult.isNewEntry); |
| 569 EXPECT_EQ(i, addResult.storedValue->key); | 570 EXPECT_EQ(i, addResult.storedValue->key); |
| 570 EXPECT_EQ(i, *addResult.storedValue->value); | 571 EXPECT_EQ(i, *addResult.storedValue->value); |
| 571 } | 572 } |
| 572 | 573 |
| 573 iter = map.find(1); | 574 iter = map.find(1); |
| 574 ASSERT_TRUE(iter != map.end()); | 575 ASSERT_TRUE(iter != map.end()); |
| 575 EXPECT_EQ(1, iter->key); | 576 EXPECT_EQ(1, iter->key); |
| 576 EXPECT_EQ(1, *iter->value); | 577 EXPECT_EQ(1, *iter->value); |
| 577 | 578 |
| 578 Pointer one(map.take(1)); | 579 Pointer one(map.take(1)); |
| 579 ASSERT_TRUE(one); | 580 ASSERT_TRUE(one); |
| 580 EXPECT_EQ(1, *one); | 581 EXPECT_EQ(1, *one); |
| 581 | 582 |
| 582 Pointer empty(map.take(42)); | 583 Pointer empty(map.take(42)); |
| 583 EXPECT_TRUE(!empty); | 584 EXPECT_TRUE(!empty); |
| 584 | 585 |
| 585 iter = map.find(1); | 586 iter = map.find(1); |
| 586 EXPECT_TRUE(iter == map.end()); | 587 EXPECT_TRUE(iter == map.end()); |
| 587 | 588 |
| 588 { | 589 { |
| 589 Map::AddResult addResult = map.add(1, std::move(one)); | 590 Map::AddResult addResult = map.insert(1, std::move(one)); |
| 590 EXPECT_TRUE(addResult.isNewEntry); | 591 EXPECT_TRUE(addResult.isNewEntry); |
| 591 EXPECT_EQ(1, addResult.storedValue->key); | 592 EXPECT_EQ(1, addResult.storedValue->key); |
| 592 EXPECT_EQ(1, *addResult.storedValue->value); | 593 EXPECT_EQ(1, *addResult.storedValue->value); |
| 593 } | 594 } |
| 594 } | 595 } |
| 595 | 596 |
| 596 TEST(HashMapTest, MoveOnlyPairKeyType) { | 597 TEST(HashMapTest, MoveOnlyPairKeyType) { |
| 597 using Pair = std::pair<MoveOnly, int>; | 598 using Pair = std::pair<MoveOnly, int>; |
| 598 using TheMap = HashMap<Pair, int>; | 599 using TheMap = HashMap<Pair, int>; |
| 599 TheMap map; | 600 TheMap map; |
| 600 { | 601 { |
| 601 TheMap::AddResult addResult = map.add(Pair(MoveOnly(1), -1), 10); | 602 TheMap::AddResult addResult = map.insert(Pair(MoveOnly(1), -1), 10); |
| 602 EXPECT_TRUE(addResult.isNewEntry); | 603 EXPECT_TRUE(addResult.isNewEntry); |
| 603 EXPECT_EQ(1, addResult.storedValue->key.first.value()); | 604 EXPECT_EQ(1, addResult.storedValue->key.first.value()); |
| 604 EXPECT_EQ(-1, addResult.storedValue->key.second); | 605 EXPECT_EQ(-1, addResult.storedValue->key.second); |
| 605 EXPECT_EQ(10, addResult.storedValue->value); | 606 EXPECT_EQ(10, addResult.storedValue->value); |
| 606 } | 607 } |
| 607 auto iter = map.find(Pair(MoveOnly(1), -1)); | 608 auto iter = map.find(Pair(MoveOnly(1), -1)); |
| 608 ASSERT_TRUE(iter != map.end()); | 609 ASSERT_TRUE(iter != map.end()); |
| 609 EXPECT_EQ(1, iter->key.first.value()); | 610 EXPECT_EQ(1, iter->key.first.value()); |
| 610 EXPECT_EQ(-1, iter->key.second); | 611 EXPECT_EQ(-1, iter->key.second); |
| 611 EXPECT_EQ(10, iter->value); | 612 EXPECT_EQ(10, iter->value); |
| 612 | 613 |
| 613 iter = map.find(Pair(MoveOnly(1), 0)); | 614 iter = map.find(Pair(MoveOnly(1), 0)); |
| 614 EXPECT_TRUE(iter == map.end()); | 615 EXPECT_TRUE(iter == map.end()); |
| 615 | 616 |
| 616 for (int i = 2; i < 32; ++i) { | 617 for (int i = 2; i < 32; ++i) { |
| 617 TheMap::AddResult addResult = map.add(Pair(MoveOnly(i), -i), i * 10); | 618 TheMap::AddResult addResult = map.insert(Pair(MoveOnly(i), -i), i * 10); |
| 618 EXPECT_TRUE(addResult.isNewEntry); | 619 EXPECT_TRUE(addResult.isNewEntry); |
| 619 EXPECT_EQ(i, addResult.storedValue->key.first.value()); | 620 EXPECT_EQ(i, addResult.storedValue->key.first.value()); |
| 620 EXPECT_EQ(-i, addResult.storedValue->key.second); | 621 EXPECT_EQ(-i, addResult.storedValue->key.second); |
| 621 EXPECT_EQ(i * 10, addResult.storedValue->value); | 622 EXPECT_EQ(i * 10, addResult.storedValue->value); |
| 622 } | 623 } |
| 623 | 624 |
| 624 iter = map.find(Pair(MoveOnly(1), -1)); | 625 iter = map.find(Pair(MoveOnly(1), -1)); |
| 625 ASSERT_TRUE(iter != map.end()); | 626 ASSERT_TRUE(iter != map.end()); |
| 626 EXPECT_EQ(1, iter->key.first.value()); | 627 EXPECT_EQ(1, iter->key.first.value()); |
| 627 EXPECT_EQ(-1, iter->key.second); | 628 EXPECT_EQ(-1, iter->key.second); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 HashMap<int, int> oneTwoThree({{1, 11}, {2, 22}, {3, 33}}); | 676 HashMap<int, int> oneTwoThree({{1, 11}, {2, 22}, {3, 33}}); |
| 676 EXPECT_EQ(oneTwoThree.size(), 3u); | 677 EXPECT_EQ(oneTwoThree.size(), 3u); |
| 677 EXPECT_TRUE(oneTwoThree.contains(1)); | 678 EXPECT_TRUE(oneTwoThree.contains(1)); |
| 678 EXPECT_TRUE(oneTwoThree.contains(2)); | 679 EXPECT_TRUE(oneTwoThree.contains(2)); |
| 679 EXPECT_TRUE(oneTwoThree.contains(3)); | 680 EXPECT_TRUE(oneTwoThree.contains(3)); |
| 680 EXPECT_EQ(oneTwoThree.get(1), 11); | 681 EXPECT_EQ(oneTwoThree.get(1), 11); |
| 681 EXPECT_EQ(oneTwoThree.get(2), 22); | 682 EXPECT_EQ(oneTwoThree.get(2), 22); |
| 682 EXPECT_EQ(oneTwoThree.get(3), 33); | 683 EXPECT_EQ(oneTwoThree.get(3), 33); |
| 683 | 684 |
| 684 // Put some jank so we can check if the assignments can clear them later. | 685 // Put some jank so we can check if the assignments can clear them later. |
| 685 empty.add(9999, 99999); | 686 empty.insert(9999, 99999); |
| 686 one.add(9999, 99999); | 687 one.insert(9999, 99999); |
| 687 oneTwoThree.add(9999, 99999); | 688 oneTwoThree.insert(9999, 99999); |
| 688 | 689 |
| 689 empty = {}; | 690 empty = {}; |
| 690 EXPECT_TRUE(empty.isEmpty()); | 691 EXPECT_TRUE(empty.isEmpty()); |
| 691 | 692 |
| 692 one = {{1, 11}}; | 693 one = {{1, 11}}; |
| 693 EXPECT_EQ(one.size(), 1u); | 694 EXPECT_EQ(one.size(), 1u); |
| 694 EXPECT_TRUE(one.contains(1)); | 695 EXPECT_TRUE(one.contains(1)); |
| 695 EXPECT_EQ(one.get(1), 11); | 696 EXPECT_EQ(one.get(1), 11); |
| 696 | 697 |
| 697 oneTwoThree = {{1, 11}, {2, 22}, {3, 33}}; | 698 oneTwoThree = {{1, 11}, {2, 22}, {3, 33}}; |
| 698 EXPECT_EQ(oneTwoThree.size(), 3u); | 699 EXPECT_EQ(oneTwoThree.size(), 3u); |
| 699 EXPECT_TRUE(oneTwoThree.contains(1)); | 700 EXPECT_TRUE(oneTwoThree.contains(1)); |
| 700 EXPECT_TRUE(oneTwoThree.contains(2)); | 701 EXPECT_TRUE(oneTwoThree.contains(2)); |
| 701 EXPECT_TRUE(oneTwoThree.contains(3)); | 702 EXPECT_TRUE(oneTwoThree.contains(3)); |
| 702 EXPECT_EQ(oneTwoThree.get(1), 11); | 703 EXPECT_EQ(oneTwoThree.get(1), 11); |
| 703 EXPECT_EQ(oneTwoThree.get(2), 22); | 704 EXPECT_EQ(oneTwoThree.get(2), 22); |
| 704 EXPECT_EQ(oneTwoThree.get(3), 33); | 705 EXPECT_EQ(oneTwoThree.get(3), 33); |
| 705 | 706 |
| 706 // Other ways of construction: as a function parameter and in a return | 707 // Other ways of construction: as a function parameter and in a return |
| 707 // statement. | 708 // statement. |
| 708 EXPECT_TRUE(isOneTwoThree({{1, 11}, {2, 22}, {3, 33}})); | 709 EXPECT_TRUE(isOneTwoThree({{1, 11}, {2, 22}, {3, 33}})); |
| 709 EXPECT_TRUE(isOneTwoThree(returnOneTwoThree())); | 710 EXPECT_TRUE(isOneTwoThree(returnOneTwoThree())); |
| 710 } | 711 } |
| 711 | 712 |
| 712 } // anonymous namespace | 713 } // anonymous namespace |
| 713 | 714 |
| 714 } // namespace WTF | 715 } // namespace WTF |
| OLD | NEW |