| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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.insert(clobberKey, 1); | 81 map.insert(clobberKey, 1); |
| 82 map.insert(zeroKey, 2); | 82 map.insert(zeroKey, 2); |
| 83 map.insert(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.at(clobberKey)); |
| 87 EXPECT_EQ(2, map.get(zeroKey)); | 87 EXPECT_EQ(2, map.at(zeroKey)); |
| 88 EXPECT_EQ(3, map.get(negativeZeroKey)); | 88 EXPECT_EQ(3, map.at(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.insert(1, WTF::wrapUnique(new DestructCounter(1, &destructNumber))); | 109 map.insert(1, WTF::wrapUnique(new DestructCounter(1, &destructNumber))); |
| 110 map.insert(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.at(1); |
| 113 EXPECT_EQ(1, counter1->get()); | 113 EXPECT_EQ(1, counter1->get()); |
| 114 DestructCounter* counter2 = map.get(2); | 114 DestructCounter* counter2 = map.at(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()); |
| 121 } | 121 } |
| 122 ASSERT_EQ(0, destructNumber); | 122 ASSERT_EQ(0, destructNumber); |
| 123 | 123 |
| 124 std::unique_ptr<DestructCounter> ownCounter1 = map.take(1); | 124 std::unique_ptr<DestructCounter> ownCounter1 = map.take(1); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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.insert(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.at(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)); |
| 184 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | 184 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); |
| 185 | 185 |
| 186 ptr.clear(); | 186 ptr.clear(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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.insert(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.at(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. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 240 | 240 |
| 241 TEST(HashMapTest, AddResult) { | 241 TEST(HashMapTest, AddResult) { |
| 242 IntSimpleMap map; | 242 IntSimpleMap map; |
| 243 IntSimpleMap::AddResult result = map.insert(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.at(1)); |
| 251 | 251 |
| 252 IntSimpleMap::AddResult result2 = | 252 IntSimpleMap::AddResult result2 = |
| 253 map.insert(1, WTF::makeUnique<SimpleClass>(2)); | 253 map.insert(1, WTF::makeUnique<SimpleClass>(2)); |
| 254 EXPECT_FALSE(result2.isNewEntry); | 254 EXPECT_FALSE(result2.isNewEntry); |
| 255 EXPECT_EQ(1, result.storedValue->key); | 255 EXPECT_EQ(1, result.storedValue->key); |
| 256 EXPECT_EQ(1, result.storedValue->value->v()); | 256 EXPECT_EQ(1, result.storedValue->value->v()); |
| 257 EXPECT_EQ(1, map.get(1)->v()); | 257 EXPECT_EQ(1, map.at(1)->v()); |
| 258 } | 258 } |
| 259 | 259 |
| 260 TEST(HashMapTest, AddResultVectorValue) { | 260 TEST(HashMapTest, AddResultVectorValue) { |
| 261 using IntVectorMap = HashMap<int, Vector<int>>; | 261 using IntVectorMap = HashMap<int, Vector<int>>; |
| 262 IntVectorMap map; | 262 IntVectorMap map; |
| 263 IntVectorMap::AddResult result = map.insert(1, Vector<int>()); | 263 IntVectorMap::AddResult result = map.insert(1, Vector<int>()); |
| 264 EXPECT_TRUE(result.isNewEntry); | 264 EXPECT_TRUE(result.isNewEntry); |
| 265 EXPECT_EQ(1, result.storedValue->key); | 265 EXPECT_EQ(1, result.storedValue->key); |
| 266 EXPECT_EQ(0u, result.storedValue->value.size()); | 266 EXPECT_EQ(0u, result.storedValue->value.size()); |
| 267 | 267 |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 Map::AddResult addResult = map.insert(1, Pointer(new int(1))); | 550 Map::AddResult addResult = map.insert(1, Pointer(new int(1))); |
| 551 EXPECT_TRUE(addResult.isNewEntry); | 551 EXPECT_TRUE(addResult.isNewEntry); |
| 552 EXPECT_EQ(1, addResult.storedValue->key); | 552 EXPECT_EQ(1, addResult.storedValue->key); |
| 553 EXPECT_EQ(1, *addResult.storedValue->value); | 553 EXPECT_EQ(1, *addResult.storedValue->value); |
| 554 } | 554 } |
| 555 auto iter = map.find(1); | 555 auto iter = map.find(1); |
| 556 ASSERT_TRUE(iter != map.end()); | 556 ASSERT_TRUE(iter != map.end()); |
| 557 EXPECT_EQ(1, iter->key); | 557 EXPECT_EQ(1, iter->key); |
| 558 EXPECT_EQ(1, *iter->value); | 558 EXPECT_EQ(1, *iter->value); |
| 559 | 559 |
| 560 int* onePointer = map.get(1); | 560 int* onePointer = map.at(1); |
| 561 EXPECT_TRUE(onePointer); | 561 EXPECT_TRUE(onePointer); |
| 562 EXPECT_EQ(1, *onePointer); | 562 EXPECT_EQ(1, *onePointer); |
| 563 | 563 |
| 564 iter = map.find(42); | 564 iter = map.find(42); |
| 565 EXPECT_TRUE(iter == map.end()); | 565 EXPECT_TRUE(iter == map.end()); |
| 566 | 566 |
| 567 for (int i = 2; i < 32; ++i) { | 567 for (int i = 2; i < 32; ++i) { |
| 568 Map::AddResult addResult = map.insert(i, Pointer(new int(i))); | 568 Map::AddResult addResult = map.insert(i, Pointer(new int(i))); |
| 569 EXPECT_TRUE(addResult.isNewEntry); | 569 EXPECT_TRUE(addResult.isNewEntry); |
| 570 EXPECT_EQ(i, addResult.storedValue->key); | 570 EXPECT_EQ(i, addResult.storedValue->key); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 int oneThirty = map.take(Pair(MoveOnly(13), -13)); | 649 int oneThirty = map.take(Pair(MoveOnly(13), -13)); |
| 650 EXPECT_EQ(130, oneThirty); | 650 EXPECT_EQ(130, oneThirty); |
| 651 iter = map.find(Pair(MoveOnly(13), -13)); | 651 iter = map.find(Pair(MoveOnly(13), -13)); |
| 652 EXPECT_TRUE(iter == map.end()); | 652 EXPECT_TRUE(iter == map.end()); |
| 653 | 653 |
| 654 map.clear(); | 654 map.clear(); |
| 655 } | 655 } |
| 656 | 656 |
| 657 bool isOneTwoThree(const HashMap<int, int>& map) { | 657 bool isOneTwoThree(const HashMap<int, int>& map) { |
| 658 return map.size() == 3 && map.contains(1) && map.contains(2) && | 658 return map.size() == 3 && map.contains(1) && map.contains(2) && |
| 659 map.contains(3) && map.get(1) == 11 && map.get(2) == 22 && | 659 map.contains(3) && map.at(1) == 11 && map.at(2) == 22 && |
| 660 map.get(3) == 33; | 660 map.at(3) == 33; |
| 661 }; | 661 }; |
| 662 | 662 |
| 663 HashMap<int, int> returnOneTwoThree() { | 663 HashMap<int, int> returnOneTwoThree() { |
| 664 return {{1, 11}, {2, 22}, {3, 33}}; | 664 return {{1, 11}, {2, 22}, {3, 33}}; |
| 665 }; | 665 }; |
| 666 | 666 |
| 667 TEST(HashMapTest, InitializerList) { | 667 TEST(HashMapTest, InitializerList) { |
| 668 HashMap<int, int> empty({}); | 668 HashMap<int, int> empty({}); |
| 669 EXPECT_TRUE(empty.isEmpty()); | 669 EXPECT_TRUE(empty.isEmpty()); |
| 670 | 670 |
| 671 HashMap<int, int> one({{1, 11}}); | 671 HashMap<int, int> one({{1, 11}}); |
| 672 EXPECT_EQ(one.size(), 1u); | 672 EXPECT_EQ(one.size(), 1u); |
| 673 EXPECT_TRUE(one.contains(1)); | 673 EXPECT_TRUE(one.contains(1)); |
| 674 EXPECT_EQ(one.get(1), 11); | 674 EXPECT_EQ(one.at(1), 11); |
| 675 | 675 |
| 676 HashMap<int, int> oneTwoThree({{1, 11}, {2, 22}, {3, 33}}); | 676 HashMap<int, int> oneTwoThree({{1, 11}, {2, 22}, {3, 33}}); |
| 677 EXPECT_EQ(oneTwoThree.size(), 3u); | 677 EXPECT_EQ(oneTwoThree.size(), 3u); |
| 678 EXPECT_TRUE(oneTwoThree.contains(1)); | 678 EXPECT_TRUE(oneTwoThree.contains(1)); |
| 679 EXPECT_TRUE(oneTwoThree.contains(2)); | 679 EXPECT_TRUE(oneTwoThree.contains(2)); |
| 680 EXPECT_TRUE(oneTwoThree.contains(3)); | 680 EXPECT_TRUE(oneTwoThree.contains(3)); |
| 681 EXPECT_EQ(oneTwoThree.get(1), 11); | 681 EXPECT_EQ(oneTwoThree.at(1), 11); |
| 682 EXPECT_EQ(oneTwoThree.get(2), 22); | 682 EXPECT_EQ(oneTwoThree.at(2), 22); |
| 683 EXPECT_EQ(oneTwoThree.get(3), 33); | 683 EXPECT_EQ(oneTwoThree.at(3), 33); |
| 684 | 684 |
| 685 // 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. |
| 686 empty.insert(9999, 99999); | 686 empty.insert(9999, 99999); |
| 687 one.insert(9999, 99999); | 687 one.insert(9999, 99999); |
| 688 oneTwoThree.insert(9999, 99999); | 688 oneTwoThree.insert(9999, 99999); |
| 689 | 689 |
| 690 empty = {}; | 690 empty = {}; |
| 691 EXPECT_TRUE(empty.isEmpty()); | 691 EXPECT_TRUE(empty.isEmpty()); |
| 692 | 692 |
| 693 one = {{1, 11}}; | 693 one = {{1, 11}}; |
| 694 EXPECT_EQ(one.size(), 1u); | 694 EXPECT_EQ(one.size(), 1u); |
| 695 EXPECT_TRUE(one.contains(1)); | 695 EXPECT_TRUE(one.contains(1)); |
| 696 EXPECT_EQ(one.get(1), 11); | 696 EXPECT_EQ(one.at(1), 11); |
| 697 | 697 |
| 698 oneTwoThree = {{1, 11}, {2, 22}, {3, 33}}; | 698 oneTwoThree = {{1, 11}, {2, 22}, {3, 33}}; |
| 699 EXPECT_EQ(oneTwoThree.size(), 3u); | 699 EXPECT_EQ(oneTwoThree.size(), 3u); |
| 700 EXPECT_TRUE(oneTwoThree.contains(1)); | 700 EXPECT_TRUE(oneTwoThree.contains(1)); |
| 701 EXPECT_TRUE(oneTwoThree.contains(2)); | 701 EXPECT_TRUE(oneTwoThree.contains(2)); |
| 702 EXPECT_TRUE(oneTwoThree.contains(3)); | 702 EXPECT_TRUE(oneTwoThree.contains(3)); |
| 703 EXPECT_EQ(oneTwoThree.get(1), 11); | 703 EXPECT_EQ(oneTwoThree.at(1), 11); |
| 704 EXPECT_EQ(oneTwoThree.get(2), 22); | 704 EXPECT_EQ(oneTwoThree.at(2), 22); |
| 705 EXPECT_EQ(oneTwoThree.get(3), 33); | 705 EXPECT_EQ(oneTwoThree.at(3), 33); |
| 706 | 706 |
| 707 // 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 |
| 708 // statement. | 708 // statement. |
| 709 EXPECT_TRUE(isOneTwoThree({{1, 11}, {2, 22}, {3, 33}})); | 709 EXPECT_TRUE(isOneTwoThree({{1, 11}, {2, 22}, {3, 33}})); |
| 710 EXPECT_TRUE(isOneTwoThree(returnOneTwoThree())); | 710 EXPECT_TRUE(isOneTwoThree(returnOneTwoThree())); |
| 711 } | 711 } |
| 712 | 712 |
| 713 } // anonymous namespace | 713 } // anonymous namespace |
| 714 | 714 |
| 715 } // namespace WTF | 715 } // namespace WTF |
| OLD | NEW |