pph34r@gmail.com | 4f029ab | 2011-06-25 13:33:18 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/id_map.h" |
| 6 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
rlanday | f5b01dd | 2016-12-01 03:59:30 +0900 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
| 11 | #include "base/memory/ptr_util.h" |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
| 14 | namespace { |
| 15 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 16 | class TestObject { |
| 17 | }; |
| 18 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 19 | class DestructorCounter { |
| 20 | public: |
| 21 | explicit DestructorCounter(int* counter) : counter_(counter) {} |
| 22 | ~DestructorCounter() { ++(*counter_); } |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 23 | |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 24 | private: |
| 25 | int* counter_; |
| 26 | }; |
| 27 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 28 | TEST(IDMapTest, Basic) { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 29 | IDMap<TestObject*> map; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 30 | EXPECT_TRUE(map.IsEmpty()); |
| 31 | EXPECT_EQ(0U, map.size()); |
| 32 | |
| 33 | TestObject obj1; |
| 34 | TestObject obj2; |
| 35 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 36 | int32_t id1 = map.Add(&obj1); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 37 | EXPECT_FALSE(map.IsEmpty()); |
| 38 | EXPECT_EQ(1U, map.size()); |
| 39 | EXPECT_EQ(&obj1, map.Lookup(id1)); |
| 40 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 41 | int32_t id2 = map.Add(&obj2); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 42 | EXPECT_FALSE(map.IsEmpty()); |
| 43 | EXPECT_EQ(2U, map.size()); |
| 44 | |
| 45 | EXPECT_EQ(&obj1, map.Lookup(id1)); |
| 46 | EXPECT_EQ(&obj2, map.Lookup(id2)); |
| 47 | |
| 48 | map.Remove(id1); |
| 49 | EXPECT_FALSE(map.IsEmpty()); |
| 50 | EXPECT_EQ(1U, map.size()); |
| 51 | |
| 52 | map.Remove(id2); |
| 53 | EXPECT_TRUE(map.IsEmpty()); |
| 54 | EXPECT_EQ(0U, map.size()); |
| 55 | |
| 56 | map.AddWithID(&obj1, 1); |
| 57 | map.AddWithID(&obj2, 2); |
| 58 | EXPECT_EQ(&obj1, map.Lookup(1)); |
| 59 | EXPECT_EQ(&obj2, map.Lookup(2)); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 60 | |
michaeln | 699d8f5 | 2014-12-10 08:16:13 +0900 | [diff] [blame] | 61 | EXPECT_EQ(&obj2, map.Replace(2, &obj1)); |
| 62 | EXPECT_EQ(&obj1, map.Lookup(2)); |
| 63 | |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 64 | EXPECT_EQ(0, map.iteration_depth()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 65 | } |
| 66 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 67 | TEST(IDMapTest, IteratorRemainsValidWhenRemovingCurrentElement) { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 68 | IDMap<TestObject*> map; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 69 | |
| 70 | TestObject obj1; |
| 71 | TestObject obj2; |
| 72 | TestObject obj3; |
| 73 | |
| 74 | map.Add(&obj1); |
| 75 | map.Add(&obj2); |
| 76 | map.Add(&obj3); |
| 77 | |
phajdan.jr@chromium.org | 61544a0 | 2010-02-16 18:15:38 +0900 | [diff] [blame] | 78 | { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 79 | IDMap<TestObject*>::const_iterator iter(&map); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 80 | |
| 81 | EXPECT_EQ(1, map.iteration_depth()); |
| 82 | |
phajdan.jr@chromium.org | 61544a0 | 2010-02-16 18:15:38 +0900 | [diff] [blame] | 83 | while (!iter.IsAtEnd()) { |
| 84 | map.Remove(iter.GetCurrentKey()); |
| 85 | iter.Advance(); |
| 86 | } |
| 87 | |
| 88 | // Test that while an iterator is still in scope, we get the map emptiness |
| 89 | // right (http://crbug.com/35571). |
| 90 | EXPECT_TRUE(map.IsEmpty()); |
| 91 | EXPECT_EQ(0U, map.size()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 92 | } |
phajdan.jr@chromium.org | 61544a0 | 2010-02-16 18:15:38 +0900 | [diff] [blame] | 93 | |
| 94 | EXPECT_TRUE(map.IsEmpty()); |
| 95 | EXPECT_EQ(0U, map.size()); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 96 | |
| 97 | EXPECT_EQ(0, map.iteration_depth()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 98 | } |
| 99 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 100 | TEST(IDMapTest, IteratorRemainsValidWhenRemovingOtherElements) { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 101 | IDMap<TestObject*> map; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 102 | |
| 103 | const int kCount = 5; |
| 104 | TestObject obj[kCount]; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 105 | |
| 106 | for (int i = 0; i < kCount; i++) |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 107 | map.Add(&obj[i]); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 108 | |
brettw | 0b7bff3 | 2017-04-28 04:42:32 +0900 | [diff] [blame] | 109 | // IDMap has no predictable iteration order. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 110 | int32_t ids_in_iteration_order[kCount]; |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 111 | const TestObject* objs_in_iteration_order[kCount]; |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 112 | int counter = 0; |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 113 | for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd(); |
| 114 | iter.Advance()) { |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 115 | ids_in_iteration_order[counter] = iter.GetCurrentKey(); |
| 116 | objs_in_iteration_order[counter] = iter.GetCurrentValue(); |
| 117 | counter++; |
| 118 | } |
| 119 | |
| 120 | counter = 0; |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 121 | for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd(); |
| 122 | iter.Advance()) { |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 123 | EXPECT_EQ(1, map.iteration_depth()); |
| 124 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 125 | switch (counter) { |
| 126 | case 0: |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 127 | EXPECT_EQ(ids_in_iteration_order[0], iter.GetCurrentKey()); |
| 128 | EXPECT_EQ(objs_in_iteration_order[0], iter.GetCurrentValue()); |
| 129 | map.Remove(ids_in_iteration_order[1]); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 130 | break; |
| 131 | case 1: |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 132 | EXPECT_EQ(ids_in_iteration_order[2], iter.GetCurrentKey()); |
| 133 | EXPECT_EQ(objs_in_iteration_order[2], iter.GetCurrentValue()); |
| 134 | map.Remove(ids_in_iteration_order[3]); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 135 | break; |
| 136 | case 2: |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 137 | EXPECT_EQ(ids_in_iteration_order[4], iter.GetCurrentKey()); |
| 138 | EXPECT_EQ(objs_in_iteration_order[4], iter.GetCurrentValue()); |
| 139 | map.Remove(ids_in_iteration_order[0]); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 140 | break; |
| 141 | default: |
| 142 | FAIL() << "should not have that many elements"; |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | counter++; |
| 147 | } |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 148 | |
| 149 | EXPECT_EQ(0, map.iteration_depth()); |
| 150 | } |
| 151 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 152 | TEST(IDMapTest, CopyIterator) { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 153 | IDMap<TestObject*> map; |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 154 | |
| 155 | TestObject obj1; |
| 156 | TestObject obj2; |
| 157 | TestObject obj3; |
| 158 | |
| 159 | map.Add(&obj1); |
| 160 | map.Add(&obj2); |
| 161 | map.Add(&obj3); |
| 162 | |
| 163 | EXPECT_EQ(0, map.iteration_depth()); |
| 164 | |
| 165 | { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 166 | IDMap<TestObject*>::const_iterator iter1(&map); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 167 | EXPECT_EQ(1, map.iteration_depth()); |
| 168 | |
| 169 | // Make sure that copying the iterator correctly increments |
| 170 | // map's iteration depth. |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 171 | IDMap<TestObject*>::const_iterator iter2(iter1); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 172 | EXPECT_EQ(2, map.iteration_depth()); |
| 173 | } |
| 174 | |
| 175 | // Make sure after destroying all iterators the map's iteration depth |
| 176 | // returns to initial state. |
| 177 | EXPECT_EQ(0, map.iteration_depth()); |
| 178 | } |
| 179 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 180 | TEST(IDMapTest, AssignIterator) { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 181 | IDMap<TestObject*> map; |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 182 | |
| 183 | TestObject obj1; |
| 184 | TestObject obj2; |
| 185 | TestObject obj3; |
| 186 | |
| 187 | map.Add(&obj1); |
| 188 | map.Add(&obj2); |
| 189 | map.Add(&obj3); |
| 190 | |
| 191 | EXPECT_EQ(0, map.iteration_depth()); |
| 192 | |
| 193 | { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 194 | IDMap<TestObject*>::const_iterator iter1(&map); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 195 | EXPECT_EQ(1, map.iteration_depth()); |
| 196 | |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 197 | IDMap<TestObject*>::const_iterator iter2(&map); |
phajdan.jr@chromium.org | 7c63ed8 | 2012-10-27 10:03:01 +0900 | [diff] [blame] | 198 | EXPECT_EQ(2, map.iteration_depth()); |
| 199 | |
| 200 | // Make sure that assigning the iterator correctly updates |
| 201 | // map's iteration depth (-1 for destruction, +1 for assignment). |
| 202 | EXPECT_EQ(2, map.iteration_depth()); |
| 203 | } |
| 204 | |
| 205 | // Make sure after destroying all iterators the map's iteration depth |
| 206 | // returns to initial state. |
| 207 | EXPECT_EQ(0, map.iteration_depth()); |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 208 | } |
| 209 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 210 | TEST(IDMapTest, IteratorRemainsValidWhenClearing) { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 211 | IDMap<TestObject*> map; |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 212 | |
| 213 | const int kCount = 5; |
| 214 | TestObject obj[kCount]; |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 215 | |
| 216 | for (int i = 0; i < kCount; i++) |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 217 | map.Add(&obj[i]); |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 218 | |
brettw | 0b7bff3 | 2017-04-28 04:42:32 +0900 | [diff] [blame] | 219 | // IDMap has no predictable iteration order. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 220 | int32_t ids_in_iteration_order[kCount]; |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 221 | const TestObject* objs_in_iteration_order[kCount]; |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 222 | int counter = 0; |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 223 | for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd(); |
| 224 | iter.Advance()) { |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 225 | ids_in_iteration_order[counter] = iter.GetCurrentKey(); |
| 226 | objs_in_iteration_order[counter] = iter.GetCurrentValue(); |
| 227 | counter++; |
| 228 | } |
| 229 | |
| 230 | counter = 0; |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 231 | for (IDMap<TestObject*>::const_iterator iter(&map); !iter.IsAtEnd(); |
| 232 | iter.Advance()) { |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 233 | switch (counter) { |
| 234 | case 0: |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 235 | EXPECT_EQ(ids_in_iteration_order[0], iter.GetCurrentKey()); |
| 236 | EXPECT_EQ(objs_in_iteration_order[0], iter.GetCurrentValue()); |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 237 | break; |
| 238 | case 1: |
earthdok@chromium.org | 8be6e99 | 2014-01-24 03:17:15 +0900 | [diff] [blame] | 239 | EXPECT_EQ(ids_in_iteration_order[1], iter.GetCurrentKey()); |
| 240 | EXPECT_EQ(objs_in_iteration_order[1], iter.GetCurrentValue()); |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 241 | map.Clear(); |
| 242 | EXPECT_TRUE(map.IsEmpty()); |
| 243 | EXPECT_EQ(0U, map.size()); |
| 244 | break; |
| 245 | default: |
| 246 | FAIL() << "should not have that many elements"; |
| 247 | break; |
| 248 | } |
| 249 | counter++; |
| 250 | } |
| 251 | |
| 252 | EXPECT_TRUE(map.IsEmpty()); |
| 253 | EXPECT_EQ(0U, map.size()); |
| 254 | } |
| 255 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 256 | TEST(IDMapTest, OwningPointersDeletesThemOnRemove) { |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 257 | const int kCount = 3; |
| 258 | |
| 259 | int external_del_count = 0; |
| 260 | DestructorCounter* external_obj[kCount]; |
| 261 | int map_external_ids[kCount]; |
| 262 | |
| 263 | int owned_del_count = 0; |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 264 | int map_owned_ids[kCount]; |
| 265 | |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 266 | IDMap<DestructorCounter*> map_external; |
| 267 | IDMap<std::unique_ptr<DestructorCounter>> map_owned; |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 268 | |
| 269 | for (int i = 0; i < kCount; ++i) { |
| 270 | external_obj[i] = new DestructorCounter(&external_del_count); |
| 271 | map_external_ids[i] = map_external.Add(external_obj[i]); |
| 272 | |
rlanday | f5b01dd | 2016-12-01 03:59:30 +0900 | [diff] [blame] | 273 | map_owned_ids[i] = |
| 274 | map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | for (int i = 0; i < kCount; ++i) { |
| 278 | EXPECT_EQ(external_del_count, 0); |
| 279 | EXPECT_EQ(owned_del_count, i); |
| 280 | |
| 281 | map_external.Remove(map_external_ids[i]); |
| 282 | map_owned.Remove(map_owned_ids[i]); |
| 283 | } |
| 284 | |
| 285 | for (int i = 0; i < kCount; ++i) { |
| 286 | delete external_obj[i]; |
| 287 | } |
| 288 | |
| 289 | EXPECT_EQ(external_del_count, kCount); |
| 290 | EXPECT_EQ(owned_del_count, kCount); |
| 291 | } |
| 292 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 293 | TEST(IDMapTest, OwningPointersDeletesThemOnClear) { |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 294 | const int kCount = 3; |
| 295 | |
| 296 | int external_del_count = 0; |
| 297 | DestructorCounter* external_obj[kCount]; |
| 298 | |
| 299 | int owned_del_count = 0; |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 300 | |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 301 | IDMap<DestructorCounter*> map_external; |
| 302 | IDMap<std::unique_ptr<DestructorCounter>> map_owned; |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 303 | |
| 304 | for (int i = 0; i < kCount; ++i) { |
| 305 | external_obj[i] = new DestructorCounter(&external_del_count); |
| 306 | map_external.Add(external_obj[i]); |
| 307 | |
rlanday | f5b01dd | 2016-12-01 03:59:30 +0900 | [diff] [blame] | 308 | map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); |
jsbell@chromium.org | 8fe2a8f | 2012-10-30 06:36:22 +0900 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | EXPECT_EQ(external_del_count, 0); |
| 312 | EXPECT_EQ(owned_del_count, 0); |
| 313 | |
| 314 | map_external.Clear(); |
| 315 | map_owned.Clear(); |
| 316 | |
| 317 | EXPECT_EQ(external_del_count, 0); |
| 318 | EXPECT_EQ(owned_del_count, kCount); |
| 319 | |
| 320 | for (int i = 0; i < kCount; ++i) { |
| 321 | delete external_obj[i]; |
| 322 | } |
| 323 | |
| 324 | EXPECT_EQ(external_del_count, kCount); |
| 325 | EXPECT_EQ(owned_del_count, kCount); |
| 326 | } |
| 327 | |
tfarina@chromium.org | 6ac1228 | 2012-12-04 02:04:28 +0900 | [diff] [blame] | 328 | TEST(IDMapTest, OwningPointersDeletesThemOnDestruct) { |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 329 | const int kCount = 3; |
| 330 | |
| 331 | int external_del_count = 0; |
| 332 | DestructorCounter* external_obj[kCount]; |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 333 | |
| 334 | int owned_del_count = 0; |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 335 | |
| 336 | { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 337 | IDMap<DestructorCounter*> map_external; |
| 338 | IDMap<std::unique_ptr<DestructorCounter>> map_owned; |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 339 | |
| 340 | for (int i = 0; i < kCount; ++i) { |
| 341 | external_obj[i] = new DestructorCounter(&external_del_count); |
pph34r@gmail.com | 4f029ab | 2011-06-25 13:33:18 +0900 | [diff] [blame] | 342 | map_external.Add(external_obj[i]); |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 343 | |
rlanday | f5b01dd | 2016-12-01 03:59:30 +0900 | [diff] [blame] | 344 | map_owned.Add(base::MakeUnique<DestructorCounter>(&owned_del_count)); |
neb@chromium.org | ed1f8d7 | 2010-01-26 08:25:16 +0900 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | EXPECT_EQ(external_del_count, 0); |
| 349 | |
| 350 | for (int i = 0; i < kCount; ++i) { |
| 351 | delete external_obj[i]; |
| 352 | } |
| 353 | |
| 354 | EXPECT_EQ(external_del_count, kCount); |
| 355 | EXPECT_EQ(owned_del_count, kCount); |
| 356 | } |
| 357 | |
jkarlin | a576291 | 2015-09-26 05:45:47 +0900 | [diff] [blame] | 358 | TEST(IDMapTest, Int64KeyType) { |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 359 | IDMap<TestObject*, int64_t> map; |
jkarlin | a576291 | 2015-09-26 05:45:47 +0900 | [diff] [blame] | 360 | TestObject obj1; |
| 361 | const int64_t kId1 = 999999999999999999; |
| 362 | |
| 363 | map.AddWithID(&obj1, kId1); |
| 364 | EXPECT_EQ(&obj1, map.Lookup(kId1)); |
| 365 | |
rlanday | 644d4f4 | 2016-12-01 12:05:40 +0900 | [diff] [blame] | 366 | IDMap<TestObject*, int64_t>::const_iterator iter(&map); |
bcwhite | 0ac12b0 | 2016-02-23 07:16:10 +0900 | [diff] [blame] | 367 | ASSERT_FALSE(iter.IsAtEnd()); |
| 368 | EXPECT_EQ(kId1, iter.GetCurrentKey()); |
| 369 | EXPECT_EQ(&obj1, iter.GetCurrentValue()); |
| 370 | iter.Advance(); |
| 371 | ASSERT_TRUE(iter.IsAtEnd()); |
| 372 | |
jkarlin | a576291 | 2015-09-26 05:45:47 +0900 | [diff] [blame] | 373 | map.Remove(kId1); |
| 374 | EXPECT_TRUE(map.IsEmpty()); |
| 375 | } |
| 376 | |
phajdan.jr@chromium.org | a12746c | 2009-08-19 23:56:38 +0900 | [diff] [blame] | 377 | } // namespace |