OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/id_map.h" | 5 #include "base/id_map.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
8 | 10 |
9 namespace { | 11 namespace { |
10 | 12 |
11 class TestObject { | 13 class TestObject { |
12 }; | 14 }; |
13 | 15 |
14 class DestructorCounter { | 16 class DestructorCounter { |
15 public: | 17 public: |
16 explicit DestructorCounter(int* counter) : counter_(counter) {} | 18 explicit DestructorCounter(int* counter) : counter_(counter) {} |
17 ~DestructorCounter() { ++(*counter_); } | 19 ~DestructorCounter() { ++(*counter_); } |
18 | 20 |
19 private: | 21 private: |
20 int* counter_; | 22 int* counter_; |
21 }; | 23 }; |
22 | 24 |
23 TEST(IDMapTest, Basic) { | 25 TEST(IDMapTest, Basic) { |
24 IDMap<TestObject> map; | 26 IDMap<TestObject> map; |
25 EXPECT_TRUE(map.IsEmpty()); | 27 EXPECT_TRUE(map.IsEmpty()); |
26 EXPECT_EQ(0U, map.size()); | 28 EXPECT_EQ(0U, map.size()); |
27 | 29 |
28 TestObject obj1; | 30 TestObject obj1; |
29 TestObject obj2; | 31 TestObject obj2; |
30 | 32 |
31 int32 id1 = map.Add(&obj1); | 33 int32_t id1 = map.Add(&obj1); |
32 EXPECT_FALSE(map.IsEmpty()); | 34 EXPECT_FALSE(map.IsEmpty()); |
33 EXPECT_EQ(1U, map.size()); | 35 EXPECT_EQ(1U, map.size()); |
34 EXPECT_EQ(&obj1, map.Lookup(id1)); | 36 EXPECT_EQ(&obj1, map.Lookup(id1)); |
35 | 37 |
36 int32 id2 = map.Add(&obj2); | 38 int32_t id2 = map.Add(&obj2); |
37 EXPECT_FALSE(map.IsEmpty()); | 39 EXPECT_FALSE(map.IsEmpty()); |
38 EXPECT_EQ(2U, map.size()); | 40 EXPECT_EQ(2U, map.size()); |
39 | 41 |
40 EXPECT_EQ(&obj1, map.Lookup(id1)); | 42 EXPECT_EQ(&obj1, map.Lookup(id1)); |
41 EXPECT_EQ(&obj2, map.Lookup(id2)); | 43 EXPECT_EQ(&obj2, map.Lookup(id2)); |
42 | 44 |
43 map.Remove(id1); | 45 map.Remove(id1); |
44 EXPECT_FALSE(map.IsEmpty()); | 46 EXPECT_FALSE(map.IsEmpty()); |
45 EXPECT_EQ(1U, map.size()); | 47 EXPECT_EQ(1U, map.size()); |
46 | 48 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 TEST(IDMapTest, IteratorRemainsValidWhenRemovingOtherElements) { | 97 TEST(IDMapTest, IteratorRemainsValidWhenRemovingOtherElements) { |
96 IDMap<TestObject> map; | 98 IDMap<TestObject> map; |
97 | 99 |
98 const int kCount = 5; | 100 const int kCount = 5; |
99 TestObject obj[kCount]; | 101 TestObject obj[kCount]; |
100 | 102 |
101 for (int i = 0; i < kCount; i++) | 103 for (int i = 0; i < kCount; i++) |
102 map.Add(&obj[i]); | 104 map.Add(&obj[i]); |
103 | 105 |
104 // IDMap uses a hash_map, which has no predictable iteration order. | 106 // IDMap uses a hash_map, which has no predictable iteration order. |
105 int32 ids_in_iteration_order[kCount]; | 107 int32_t ids_in_iteration_order[kCount]; |
106 const TestObject* objs_in_iteration_order[kCount]; | 108 const TestObject* objs_in_iteration_order[kCount]; |
107 int counter = 0; | 109 int counter = 0; |
108 for (IDMap<TestObject>::const_iterator iter(&map); | 110 for (IDMap<TestObject>::const_iterator iter(&map); |
109 !iter.IsAtEnd(); iter.Advance()) { | 111 !iter.IsAtEnd(); iter.Advance()) { |
110 ids_in_iteration_order[counter] = iter.GetCurrentKey(); | 112 ids_in_iteration_order[counter] = iter.GetCurrentKey(); |
111 objs_in_iteration_order[counter] = iter.GetCurrentValue(); | 113 objs_in_iteration_order[counter] = iter.GetCurrentValue(); |
112 counter++; | 114 counter++; |
113 } | 115 } |
114 | 116 |
115 counter = 0; | 117 counter = 0; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 TEST(IDMapTest, IteratorRemainsValidWhenClearing) { | 207 TEST(IDMapTest, IteratorRemainsValidWhenClearing) { |
206 IDMap<TestObject> map; | 208 IDMap<TestObject> map; |
207 | 209 |
208 const int kCount = 5; | 210 const int kCount = 5; |
209 TestObject obj[kCount]; | 211 TestObject obj[kCount]; |
210 | 212 |
211 for (int i = 0; i < kCount; i++) | 213 for (int i = 0; i < kCount; i++) |
212 map.Add(&obj[i]); | 214 map.Add(&obj[i]); |
213 | 215 |
214 // IDMap uses a hash_map, which has no predictable iteration order. | 216 // IDMap uses a hash_map, which has no predictable iteration order. |
215 int32 ids_in_iteration_order[kCount]; | 217 int32_t ids_in_iteration_order[kCount]; |
216 const TestObject* objs_in_iteration_order[kCount]; | 218 const TestObject* objs_in_iteration_order[kCount]; |
217 int counter = 0; | 219 int counter = 0; |
218 for (IDMap<TestObject>::const_iterator iter(&map); | 220 for (IDMap<TestObject>::const_iterator iter(&map); |
219 !iter.IsAtEnd(); iter.Advance()) { | 221 !iter.IsAtEnd(); iter.Advance()) { |
220 ids_in_iteration_order[counter] = iter.GetCurrentKey(); | 222 ids_in_iteration_order[counter] = iter.GetCurrentKey(); |
221 objs_in_iteration_order[counter] = iter.GetCurrentValue(); | 223 objs_in_iteration_order[counter] = iter.GetCurrentValue(); |
222 counter++; | 224 counter++; |
223 } | 225 } |
224 | 226 |
225 counter = 0; | 227 counter = 0; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 const int64_t kId1 = 999999999999999999; | 363 const int64_t kId1 = 999999999999999999; |
362 | 364 |
363 map.AddWithID(&obj1, kId1); | 365 map.AddWithID(&obj1, kId1); |
364 EXPECT_EQ(&obj1, map.Lookup(kId1)); | 366 EXPECT_EQ(&obj1, map.Lookup(kId1)); |
365 | 367 |
366 map.Remove(kId1); | 368 map.Remove(kId1); |
367 EXPECT_TRUE(map.IsEmpty()); | 369 EXPECT_TRUE(map.IsEmpty()); |
368 } | 370 } |
369 | 371 |
370 } // namespace | 372 } // namespace |
OLD | NEW |