| 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 "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| 11 class IDMapTest : public testing::Test { | |
| 12 }; | |
| 13 | |
| 14 class TestObject { | 11 class TestObject { |
| 15 }; | 12 }; |
| 16 | 13 |
| 17 class DestructorCounter { | 14 class DestructorCounter { |
| 18 public: | 15 public: |
| 19 explicit DestructorCounter(int* counter) : counter_(counter) {} | 16 explicit DestructorCounter(int* counter) : counter_(counter) {} |
| 20 ~DestructorCounter() { ++(*counter_); } | 17 ~DestructorCounter() { ++(*counter_); } |
| 18 |
| 21 private: | 19 private: |
| 22 int* counter_; | 20 int* counter_; |
| 23 }; | 21 }; |
| 24 | 22 |
| 25 TEST_F(IDMapTest, Basic) { | 23 TEST(IDMapTest, Basic) { |
| 26 IDMap<TestObject> map; | 24 IDMap<TestObject> map; |
| 27 EXPECT_TRUE(map.IsEmpty()); | 25 EXPECT_TRUE(map.IsEmpty()); |
| 28 EXPECT_EQ(0U, map.size()); | 26 EXPECT_EQ(0U, map.size()); |
| 29 | 27 |
| 30 TestObject obj1; | 28 TestObject obj1; |
| 31 TestObject obj2; | 29 TestObject obj2; |
| 32 | 30 |
| 33 int32 id1 = map.Add(&obj1); | 31 int32 id1 = map.Add(&obj1); |
| 34 EXPECT_FALSE(map.IsEmpty()); | 32 EXPECT_FALSE(map.IsEmpty()); |
| 35 EXPECT_EQ(1U, map.size()); | 33 EXPECT_EQ(1U, map.size()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 51 EXPECT_EQ(0U, map.size()); | 49 EXPECT_EQ(0U, map.size()); |
| 52 | 50 |
| 53 map.AddWithID(&obj1, 1); | 51 map.AddWithID(&obj1, 1); |
| 54 map.AddWithID(&obj2, 2); | 52 map.AddWithID(&obj2, 2); |
| 55 EXPECT_EQ(&obj1, map.Lookup(1)); | 53 EXPECT_EQ(&obj1, map.Lookup(1)); |
| 56 EXPECT_EQ(&obj2, map.Lookup(2)); | 54 EXPECT_EQ(&obj2, map.Lookup(2)); |
| 57 | 55 |
| 58 EXPECT_EQ(0, map.iteration_depth()); | 56 EXPECT_EQ(0, map.iteration_depth()); |
| 59 } | 57 } |
| 60 | 58 |
| 61 TEST_F(IDMapTest, IteratorRemainsValidWhenRemovingCurrentElement) { | 59 TEST(IDMapTest, IteratorRemainsValidWhenRemovingCurrentElement) { |
| 62 IDMap<TestObject> map; | 60 IDMap<TestObject> map; |
| 63 | 61 |
| 64 TestObject obj1; | 62 TestObject obj1; |
| 65 TestObject obj2; | 63 TestObject obj2; |
| 66 TestObject obj3; | 64 TestObject obj3; |
| 67 | 65 |
| 68 map.Add(&obj1); | 66 map.Add(&obj1); |
| 69 map.Add(&obj2); | 67 map.Add(&obj2); |
| 70 map.Add(&obj3); | 68 map.Add(&obj3); |
| 71 | 69 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 84 EXPECT_TRUE(map.IsEmpty()); | 82 EXPECT_TRUE(map.IsEmpty()); |
| 85 EXPECT_EQ(0U, map.size()); | 83 EXPECT_EQ(0U, map.size()); |
| 86 } | 84 } |
| 87 | 85 |
| 88 EXPECT_TRUE(map.IsEmpty()); | 86 EXPECT_TRUE(map.IsEmpty()); |
| 89 EXPECT_EQ(0U, map.size()); | 87 EXPECT_EQ(0U, map.size()); |
| 90 | 88 |
| 91 EXPECT_EQ(0, map.iteration_depth()); | 89 EXPECT_EQ(0, map.iteration_depth()); |
| 92 } | 90 } |
| 93 | 91 |
| 94 TEST_F(IDMapTest, IteratorRemainsValidWhenRemovingOtherElements) { | 92 TEST(IDMapTest, IteratorRemainsValidWhenRemovingOtherElements) { |
| 95 IDMap<TestObject> map; | 93 IDMap<TestObject> map; |
| 96 | 94 |
| 97 const int kCount = 5; | 95 const int kCount = 5; |
| 98 TestObject obj[kCount]; | 96 TestObject obj[kCount]; |
| 99 int32 ids[kCount]; | 97 int32 ids[kCount]; |
| 100 | 98 |
| 101 for (int i = 0; i < kCount; i++) | 99 for (int i = 0; i < kCount; i++) |
| 102 ids[i] = map.Add(&obj[i]); | 100 ids[i] = map.Add(&obj[i]); |
| 103 | 101 |
| 104 int counter = 0; | 102 int counter = 0; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 126 FAIL() << "should not have that many elements"; | 124 FAIL() << "should not have that many elements"; |
| 127 break; | 125 break; |
| 128 } | 126 } |
| 129 | 127 |
| 130 counter++; | 128 counter++; |
| 131 } | 129 } |
| 132 | 130 |
| 133 EXPECT_EQ(0, map.iteration_depth()); | 131 EXPECT_EQ(0, map.iteration_depth()); |
| 134 } | 132 } |
| 135 | 133 |
| 136 TEST_F(IDMapTest, CopyIterator) { | 134 TEST(IDMapTest, CopyIterator) { |
| 137 IDMap<TestObject> map; | 135 IDMap<TestObject> map; |
| 138 | 136 |
| 139 TestObject obj1; | 137 TestObject obj1; |
| 140 TestObject obj2; | 138 TestObject obj2; |
| 141 TestObject obj3; | 139 TestObject obj3; |
| 142 | 140 |
| 143 map.Add(&obj1); | 141 map.Add(&obj1); |
| 144 map.Add(&obj2); | 142 map.Add(&obj2); |
| 145 map.Add(&obj3); | 143 map.Add(&obj3); |
| 146 | 144 |
| 147 EXPECT_EQ(0, map.iteration_depth()); | 145 EXPECT_EQ(0, map.iteration_depth()); |
| 148 | 146 |
| 149 { | 147 { |
| 150 IDMap<TestObject>::const_iterator iter1(&map); | 148 IDMap<TestObject>::const_iterator iter1(&map); |
| 151 EXPECT_EQ(1, map.iteration_depth()); | 149 EXPECT_EQ(1, map.iteration_depth()); |
| 152 | 150 |
| 153 // Make sure that copying the iterator correctly increments | 151 // Make sure that copying the iterator correctly increments |
| 154 // map's iteration depth. | 152 // map's iteration depth. |
| 155 IDMap<TestObject>::const_iterator iter2(iter1); | 153 IDMap<TestObject>::const_iterator iter2(iter1); |
| 156 EXPECT_EQ(2, map.iteration_depth()); | 154 EXPECT_EQ(2, map.iteration_depth()); |
| 157 } | 155 } |
| 158 | 156 |
| 159 // Make sure after destroying all iterators the map's iteration depth | 157 // Make sure after destroying all iterators the map's iteration depth |
| 160 // returns to initial state. | 158 // returns to initial state. |
| 161 EXPECT_EQ(0, map.iteration_depth()); | 159 EXPECT_EQ(0, map.iteration_depth()); |
| 162 } | 160 } |
| 163 | 161 |
| 164 TEST_F(IDMapTest, AssignIterator) { | 162 TEST(IDMapTest, AssignIterator) { |
| 165 IDMap<TestObject> map; | 163 IDMap<TestObject> map; |
| 166 | 164 |
| 167 TestObject obj1; | 165 TestObject obj1; |
| 168 TestObject obj2; | 166 TestObject obj2; |
| 169 TestObject obj3; | 167 TestObject obj3; |
| 170 | 168 |
| 171 map.Add(&obj1); | 169 map.Add(&obj1); |
| 172 map.Add(&obj2); | 170 map.Add(&obj2); |
| 173 map.Add(&obj3); | 171 map.Add(&obj3); |
| 174 | 172 |
| 175 EXPECT_EQ(0, map.iteration_depth()); | 173 EXPECT_EQ(0, map.iteration_depth()); |
| 176 | 174 |
| 177 { | 175 { |
| 178 IDMap<TestObject>::const_iterator iter1(&map); | 176 IDMap<TestObject>::const_iterator iter1(&map); |
| 179 EXPECT_EQ(1, map.iteration_depth()); | 177 EXPECT_EQ(1, map.iteration_depth()); |
| 180 | 178 |
| 181 IDMap<TestObject>::const_iterator iter2(&map); | 179 IDMap<TestObject>::const_iterator iter2(&map); |
| 182 EXPECT_EQ(2, map.iteration_depth()); | 180 EXPECT_EQ(2, map.iteration_depth()); |
| 183 | 181 |
| 184 // Make sure that assigning the iterator correctly updates | 182 // Make sure that assigning the iterator correctly updates |
| 185 // map's iteration depth (-1 for destruction, +1 for assignment). | 183 // map's iteration depth (-1 for destruction, +1 for assignment). |
| 186 EXPECT_EQ(2, map.iteration_depth()); | 184 EXPECT_EQ(2, map.iteration_depth()); |
| 187 } | 185 } |
| 188 | 186 |
| 189 // Make sure after destroying all iterators the map's iteration depth | 187 // Make sure after destroying all iterators the map's iteration depth |
| 190 // returns to initial state. | 188 // returns to initial state. |
| 191 EXPECT_EQ(0, map.iteration_depth()); | 189 EXPECT_EQ(0, map.iteration_depth()); |
| 192 } | 190 } |
| 193 | 191 |
| 194 TEST_F(IDMapTest, IteratorRemainsValidWhenClearing) { | 192 TEST(IDMapTest, IteratorRemainsValidWhenClearing) { |
| 195 IDMap<TestObject> map; | 193 IDMap<TestObject> map; |
| 196 | 194 |
| 197 const int kCount = 5; | 195 const int kCount = 5; |
| 198 TestObject obj[kCount]; | 196 TestObject obj[kCount]; |
| 199 int32 ids[kCount]; | 197 int32 ids[kCount]; |
| 200 | 198 |
| 201 for (int i = 0; i < kCount; i++) | 199 for (int i = 0; i < kCount; i++) |
| 202 ids[i] = map.Add(&obj[i]); | 200 ids[i] = map.Add(&obj[i]); |
| 203 | 201 |
| 204 int counter = 0; | 202 int counter = 0; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 220 FAIL() << "should not have that many elements"; | 218 FAIL() << "should not have that many elements"; |
| 221 break; | 219 break; |
| 222 } | 220 } |
| 223 counter++; | 221 counter++; |
| 224 } | 222 } |
| 225 | 223 |
| 226 EXPECT_TRUE(map.IsEmpty()); | 224 EXPECT_TRUE(map.IsEmpty()); |
| 227 EXPECT_EQ(0U, map.size()); | 225 EXPECT_EQ(0U, map.size()); |
| 228 } | 226 } |
| 229 | 227 |
| 230 TEST_F(IDMapTest, OwningPointersDeletesThemOnRemove) { | 228 TEST(IDMapTest, OwningPointersDeletesThemOnRemove) { |
| 231 const int kCount = 3; | 229 const int kCount = 3; |
| 232 | 230 |
| 233 int external_del_count = 0; | 231 int external_del_count = 0; |
| 234 DestructorCounter* external_obj[kCount]; | 232 DestructorCounter* external_obj[kCount]; |
| 235 int map_external_ids[kCount]; | 233 int map_external_ids[kCount]; |
| 236 | 234 |
| 237 int owned_del_count = 0; | 235 int owned_del_count = 0; |
| 238 DestructorCounter* owned_obj[kCount]; | 236 DestructorCounter* owned_obj[kCount]; |
| 239 int map_owned_ids[kCount]; | 237 int map_owned_ids[kCount]; |
| 240 | 238 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 258 } | 256 } |
| 259 | 257 |
| 260 for (int i = 0; i < kCount; ++i) { | 258 for (int i = 0; i < kCount; ++i) { |
| 261 delete external_obj[i]; | 259 delete external_obj[i]; |
| 262 } | 260 } |
| 263 | 261 |
| 264 EXPECT_EQ(external_del_count, kCount); | 262 EXPECT_EQ(external_del_count, kCount); |
| 265 EXPECT_EQ(owned_del_count, kCount); | 263 EXPECT_EQ(owned_del_count, kCount); |
| 266 } | 264 } |
| 267 | 265 |
| 268 TEST_F(IDMapTest, OwningPointersDeletesThemOnClear) { | 266 TEST(IDMapTest, OwningPointersDeletesThemOnClear) { |
| 269 const int kCount = 3; | 267 const int kCount = 3; |
| 270 | 268 |
| 271 int external_del_count = 0; | 269 int external_del_count = 0; |
| 272 DestructorCounter* external_obj[kCount]; | 270 DestructorCounter* external_obj[kCount]; |
| 273 | 271 |
| 274 int owned_del_count = 0; | 272 int owned_del_count = 0; |
| 275 DestructorCounter* owned_obj[kCount]; | 273 DestructorCounter* owned_obj[kCount]; |
| 276 | 274 |
| 277 IDMap<DestructorCounter> map_external; | 275 IDMap<DestructorCounter> map_external; |
| 278 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; | 276 IDMap<DestructorCounter, IDMapOwnPointer> map_owned; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 295 EXPECT_EQ(owned_del_count, kCount); | 293 EXPECT_EQ(owned_del_count, kCount); |
| 296 | 294 |
| 297 for (int i = 0; i < kCount; ++i) { | 295 for (int i = 0; i < kCount; ++i) { |
| 298 delete external_obj[i]; | 296 delete external_obj[i]; |
| 299 } | 297 } |
| 300 | 298 |
| 301 EXPECT_EQ(external_del_count, kCount); | 299 EXPECT_EQ(external_del_count, kCount); |
| 302 EXPECT_EQ(owned_del_count, kCount); | 300 EXPECT_EQ(owned_del_count, kCount); |
| 303 } | 301 } |
| 304 | 302 |
| 305 TEST_F(IDMapTest, OwningPointersDeletesThemOnDestruct) { | 303 TEST(IDMapTest, OwningPointersDeletesThemOnDestruct) { |
| 306 const int kCount = 3; | 304 const int kCount = 3; |
| 307 | 305 |
| 308 int external_del_count = 0; | 306 int external_del_count = 0; |
| 309 DestructorCounter* external_obj[kCount]; | 307 DestructorCounter* external_obj[kCount]; |
| 310 | 308 |
| 311 int owned_del_count = 0; | 309 int owned_del_count = 0; |
| 312 DestructorCounter* owned_obj[kCount]; | 310 DestructorCounter* owned_obj[kCount]; |
| 313 | 311 |
| 314 { | 312 { |
| 315 IDMap<DestructorCounter> map_external; | 313 IDMap<DestructorCounter> map_external; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 328 | 326 |
| 329 for (int i = 0; i < kCount; ++i) { | 327 for (int i = 0; i < kCount; ++i) { |
| 330 delete external_obj[i]; | 328 delete external_obj[i]; |
| 331 } | 329 } |
| 332 | 330 |
| 333 EXPECT_EQ(external_del_count, kCount); | 331 EXPECT_EQ(external_del_count, kCount); |
| 334 EXPECT_EQ(owned_del_count, kCount); | 332 EXPECT_EQ(owned_del_count, kCount); |
| 335 } | 333 } |
| 336 | 334 |
| 337 } // namespace | 335 } // namespace |
| OLD | NEW |