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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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); |
125 EXPECT_EQ(ownCounter1.get(), counter1); | 125 EXPECT_EQ(ownCounter1.get(), counter1); |
126 EXPECT_FALSE(map.contains(1)); | 126 EXPECT_FALSE(map.contains(1)); |
127 EXPECT_EQ(0, destructNumber); | 127 EXPECT_EQ(0, destructNumber); |
128 | 128 |
129 map.remove(2); | 129 map.erase(2); |
130 EXPECT_FALSE(map.contains(2)); | 130 EXPECT_FALSE(map.contains(2)); |
131 EXPECT_EQ(0UL, map.size()); | 131 EXPECT_EQ(0UL, map.size()); |
132 EXPECT_EQ(1, destructNumber); | 132 EXPECT_EQ(1, destructNumber); |
133 | 133 |
134 ownCounter1.reset(); | 134 ownCounter1.reset(); |
135 EXPECT_EQ(2, destructNumber); | 135 EXPECT_EQ(2, destructNumber); |
136 } | 136 } |
137 | 137 |
138 class DummyRefCounted : public RefCounted<DummyRefCounted> { | 138 class DummyRefCounted : public RefCounted<DummyRefCounted> { |
139 public: | 139 public: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(); |
187 EXPECT_FALSE(isDeleted); | 187 EXPECT_FALSE(isDeleted); |
188 | 188 |
189 map.remove(rawPtr); | 189 map.erase(rawPtr); |
190 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); | 190 EXPECT_EQ(1, DummyRefCounted::m_refInvokesCount); |
191 EXPECT_TRUE(isDeleted); | 191 EXPECT_TRUE(isDeleted); |
192 EXPECT_TRUE(map.isEmpty()); | 192 EXPECT_TRUE(map.isEmpty()); |
193 } | 193 } |
194 | 194 |
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.add(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.remove(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.add(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.remove(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: |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 EXPECT_EQ(7, iter->key); | 392 EXPECT_EQ(7, iter->key); |
393 EXPECT_EQ(70, iter->value.value()); | 393 EXPECT_EQ(70, iter->value.value()); |
394 | 394 |
395 { | 395 { |
396 TheMap::AddResult addResult = map.set(9, MoveOnly(999)); | 396 TheMap::AddResult addResult = map.set(9, MoveOnly(999)); |
397 EXPECT_FALSE(addResult.isNewEntry); | 397 EXPECT_FALSE(addResult.isNewEntry); |
398 EXPECT_EQ(9, addResult.storedValue->key); | 398 EXPECT_EQ(9, addResult.storedValue->key); |
399 EXPECT_EQ(999, addResult.storedValue->value.value()); | 399 EXPECT_EQ(999, addResult.storedValue->value.value()); |
400 } | 400 } |
401 | 401 |
402 map.remove(11); | 402 map.erase(11); |
403 iter = map.find(11); | 403 iter = map.find(11); |
404 EXPECT_TRUE(iter == map.end()); | 404 EXPECT_TRUE(iter == map.end()); |
405 | 405 |
406 MoveOnly oneThirty(map.take(13)); | 406 MoveOnly oneThirty(map.take(13)); |
407 EXPECT_EQ(130, oneThirty.value()); | 407 EXPECT_EQ(130, oneThirty.value()); |
408 iter = map.find(13); | 408 iter = map.find(13); |
409 EXPECT_TRUE(iter == map.end()); | 409 EXPECT_TRUE(iter == map.end()); |
410 | 410 |
411 map.clear(); | 411 map.clear(); |
412 } | 412 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 EXPECT_EQ(7, iter->key.value()); | 447 EXPECT_EQ(7, iter->key.value()); |
448 EXPECT_EQ(70, iter->value); | 448 EXPECT_EQ(70, iter->value); |
449 | 449 |
450 { | 450 { |
451 TheMap::AddResult addResult = map.set(MoveOnly(9), 999); | 451 TheMap::AddResult addResult = map.set(MoveOnly(9), 999); |
452 EXPECT_FALSE(addResult.isNewEntry); | 452 EXPECT_FALSE(addResult.isNewEntry); |
453 EXPECT_EQ(9, addResult.storedValue->key.value()); | 453 EXPECT_EQ(9, addResult.storedValue->key.value()); |
454 EXPECT_EQ(999, addResult.storedValue->value); | 454 EXPECT_EQ(999, addResult.storedValue->value); |
455 } | 455 } |
456 | 456 |
457 map.remove(MoveOnly(11)); | 457 map.erase(MoveOnly(11)); |
458 iter = map.find(MoveOnly(11)); | 458 iter = map.find(MoveOnly(11)); |
459 EXPECT_TRUE(iter == map.end()); | 459 EXPECT_TRUE(iter == map.end()); |
460 | 460 |
461 int oneThirty = map.take(MoveOnly(13)); | 461 int oneThirty = map.take(MoveOnly(13)); |
462 EXPECT_EQ(130, oneThirty); | 462 EXPECT_EQ(130, oneThirty); |
463 iter = map.find(MoveOnly(13)); | 463 iter = map.find(MoveOnly(13)); |
464 EXPECT_TRUE(iter == map.end()); | 464 EXPECT_TRUE(iter == map.end()); |
465 | 465 |
466 map.clear(); | 466 map.clear(); |
467 } | 467 } |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 EXPECT_EQ(70, iter->value); | 634 EXPECT_EQ(70, iter->value); |
635 | 635 |
636 { | 636 { |
637 TheMap::AddResult addResult = map.set(Pair(MoveOnly(9), -9), 999); | 637 TheMap::AddResult addResult = map.set(Pair(MoveOnly(9), -9), 999); |
638 EXPECT_FALSE(addResult.isNewEntry); | 638 EXPECT_FALSE(addResult.isNewEntry); |
639 EXPECT_EQ(9, addResult.storedValue->key.first.value()); | 639 EXPECT_EQ(9, addResult.storedValue->key.first.value()); |
640 EXPECT_EQ(-9, addResult.storedValue->key.second); | 640 EXPECT_EQ(-9, addResult.storedValue->key.second); |
641 EXPECT_EQ(999, addResult.storedValue->value); | 641 EXPECT_EQ(999, addResult.storedValue->value); |
642 } | 642 } |
643 | 643 |
644 map.remove(Pair(MoveOnly(11), -11)); | 644 map.erase(Pair(MoveOnly(11), -11)); |
645 iter = map.find(Pair(MoveOnly(11), -11)); | 645 iter = map.find(Pair(MoveOnly(11), -11)); |
646 EXPECT_TRUE(iter == map.end()); | 646 EXPECT_TRUE(iter == map.end()); |
647 | 647 |
648 int oneThirty = map.take(Pair(MoveOnly(13), -13)); | 648 int oneThirty = map.take(Pair(MoveOnly(13), -13)); |
649 EXPECT_EQ(130, oneThirty); | 649 EXPECT_EQ(130, oneThirty); |
650 iter = map.find(Pair(MoveOnly(13), -13)); | 650 iter = map.find(Pair(MoveOnly(13), -13)); |
651 EXPECT_TRUE(iter == map.end()); | 651 EXPECT_TRUE(iter == map.end()); |
652 | 652 |
653 map.clear(); | 653 map.clear(); |
654 } | 654 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 | 705 |
706 // Other ways of construction: as a function parameter and in a return | 706 // Other ways of construction: as a function parameter and in a return |
707 // statement. | 707 // statement. |
708 EXPECT_TRUE(isOneTwoThree({{1, 11}, {2, 22}, {3, 33}})); | 708 EXPECT_TRUE(isOneTwoThree({{1, 11}, {2, 22}, {3, 33}})); |
709 EXPECT_TRUE(isOneTwoThree(returnOneTwoThree())); | 709 EXPECT_TRUE(isOneTwoThree(returnOneTwoThree())); |
710 } | 710 } |
711 | 711 |
712 } // anonymous namespace | 712 } // anonymous namespace |
713 | 713 |
714 } // namespace WTF | 714 } // namespace WTF |
OLD | NEW |