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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/containers/mru_cache.h" | 7 #include "base/containers/mru_cache.h" |
8 #include "base/memory/scoped_ptr.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace { | 11 namespace { |
11 | 12 |
12 int cached_item_live_count = 0; | 13 int cached_item_live_count = 0; |
13 | 14 |
14 struct CachedItem { | 15 struct CachedItem { |
15 CachedItem() : value(0) { | 16 CachedItem() : value(0) { |
16 cached_item_live_count++; | 17 cached_item_live_count++; |
17 } | 18 } |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 // Make it so only the most important element is there. | 181 // Make it so only the most important element is there. |
181 cache.ShrinkToSize(1); | 182 cache.ShrinkToSize(1); |
182 | 183 |
183 Cache::iterator iter = cache.begin(); | 184 Cache::iterator iter = cache.begin(); |
184 EXPECT_EQ(kItem3Key, iter->first); | 185 EXPECT_EQ(kItem3Key, iter->first); |
185 EXPECT_EQ(item5.value, iter->second.value); | 186 EXPECT_EQ(item5.value, iter->second.value); |
186 } | 187 } |
187 | 188 |
188 // Make sure that the owning version release its pointers properly. | 189 // Make sure that the owning version release its pointers properly. |
189 TEST(MRUCacheTest, Owning) { | 190 TEST(MRUCacheTest, Owning) { |
190 typedef base::OwningMRUCache<int, CachedItem*> Cache; | 191 typedef base::MRUCache<int, scoped_ptr<CachedItem>> Cache; |
danakj
2016/03/07 21:27:10
dittos
vmpstr
2016/03/07 21:45:32
Done.
| |
191 Cache cache(Cache::NO_AUTO_EVICT); | 192 Cache cache(Cache::NO_AUTO_EVICT); |
192 | 193 |
193 int initial_count = cached_item_live_count; | 194 int initial_count = cached_item_live_count; |
194 | 195 |
195 // First insert and item and then overwrite it. | 196 // First insert and item and then overwrite it. |
196 static const int kItem1Key = 1; | 197 static const int kItem1Key = 1; |
197 cache.Put(kItem1Key, new CachedItem(20)); | 198 cache.Put(kItem1Key, make_scoped_ptr(new CachedItem(20))); |
198 cache.Put(kItem1Key, new CachedItem(22)); | 199 cache.Put(kItem1Key, make_scoped_ptr(new CachedItem(22))); |
199 | 200 |
200 // There should still be one item, and one extra live item. | 201 // There should still be one item, and one extra live item. |
201 Cache::iterator iter = cache.Get(kItem1Key); | 202 Cache::iterator iter = cache.Get(kItem1Key); |
202 EXPECT_EQ(1U, cache.size()); | 203 EXPECT_EQ(1U, cache.size()); |
203 EXPECT_TRUE(iter != cache.end()); | 204 EXPECT_TRUE(iter != cache.end()); |
204 EXPECT_EQ(initial_count + 1, cached_item_live_count); | 205 EXPECT_EQ(initial_count + 1, cached_item_live_count); |
205 | 206 |
206 // Now remove it. | 207 // Now remove it. |
207 cache.Erase(cache.begin()); | 208 cache.Erase(cache.begin()); |
208 EXPECT_EQ(initial_count, cached_item_live_count); | 209 EXPECT_EQ(initial_count, cached_item_live_count); |
209 | 210 |
210 // Now try another cache that goes out of scope to make sure its pointers | 211 // Now try another cache that goes out of scope to make sure its pointers |
211 // go away. | 212 // go away. |
212 { | 213 { |
213 Cache cache2(Cache::NO_AUTO_EVICT); | 214 Cache cache2(Cache::NO_AUTO_EVICT); |
214 cache2.Put(1, new CachedItem(20)); | 215 cache2.Put(1, make_scoped_ptr(new CachedItem(20))); |
215 cache2.Put(2, new CachedItem(20)); | 216 cache2.Put(2, make_scoped_ptr(new CachedItem(20))); |
216 } | 217 } |
217 | 218 |
218 // There should be no objects leaked. | 219 // There should be no objects leaked. |
219 EXPECT_EQ(initial_count, cached_item_live_count); | 220 EXPECT_EQ(initial_count, cached_item_live_count); |
220 | 221 |
221 // Check that Clear() also frees things correctly. | 222 // Check that Clear() also frees things correctly. |
222 { | 223 { |
223 Cache cache2(Cache::NO_AUTO_EVICT); | 224 Cache cache2(Cache::NO_AUTO_EVICT); |
224 cache2.Put(1, new CachedItem(20)); | 225 cache2.Put(1, make_scoped_ptr(new CachedItem(20))); |
225 cache2.Put(2, new CachedItem(20)); | 226 cache2.Put(2, make_scoped_ptr(new CachedItem(20))); |
226 EXPECT_EQ(initial_count + 2, cached_item_live_count); | 227 EXPECT_EQ(initial_count + 2, cached_item_live_count); |
227 cache2.Clear(); | 228 cache2.Clear(); |
228 EXPECT_EQ(initial_count, cached_item_live_count); | 229 EXPECT_EQ(initial_count, cached_item_live_count); |
229 } | 230 } |
230 } | 231 } |
231 | 232 |
232 TEST(MRUCacheTest, AutoEvict) { | 233 TEST(MRUCacheTest, AutoEvict) { |
233 typedef base::OwningMRUCache<int, CachedItem*> Cache; | 234 typedef base::MRUCache<int, scoped_ptr<CachedItem>> Cache; |
danakj
2016/03/07 21:27:10
dittoss
vmpstr
2016/03/07 21:45:32
Done.
| |
234 static const Cache::size_type kMaxSize = 3; | 235 static const Cache::size_type kMaxSize = 3; |
235 | 236 |
236 int initial_count = cached_item_live_count; | 237 int initial_count = cached_item_live_count; |
237 | 238 |
238 { | 239 { |
239 Cache cache(kMaxSize); | 240 Cache cache(kMaxSize); |
240 | 241 |
241 static const int kItem1Key = 1, kItem2Key = 2, kItem3Key = 3, kItem4Key = 4; | 242 static const int kItem1Key = 1, kItem2Key = 2, kItem3Key = 3, kItem4Key = 4; |
242 cache.Put(kItem1Key, new CachedItem(20)); | 243 cache.Put(kItem1Key, make_scoped_ptr(new CachedItem(20))); |
243 cache.Put(kItem2Key, new CachedItem(21)); | 244 cache.Put(kItem2Key, make_scoped_ptr(new CachedItem(21))); |
244 cache.Put(kItem3Key, new CachedItem(22)); | 245 cache.Put(kItem3Key, make_scoped_ptr(new CachedItem(22))); |
245 cache.Put(kItem4Key, new CachedItem(23)); | 246 cache.Put(kItem4Key, make_scoped_ptr(new CachedItem(23))); |
246 | 247 |
247 // The cache should only have kMaxSize items in it even though we inserted | 248 // The cache should only have kMaxSize items in it even though we inserted |
248 // more. | 249 // more. |
249 EXPECT_EQ(kMaxSize, cache.size()); | 250 EXPECT_EQ(kMaxSize, cache.size()); |
250 } | 251 } |
251 | 252 |
252 // There should be no objects leaked. | 253 // There should be no objects leaked. |
253 EXPECT_EQ(initial_count, cached_item_live_count); | 254 EXPECT_EQ(initial_count, cached_item_live_count); |
254 } | 255 } |
255 | 256 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 ASSERT_TRUE(iter != cache2.end()); | 368 ASSERT_TRUE(iter != cache2.end()); |
368 EXPECT_EQ(kItem2Key, iter->first); | 369 EXPECT_EQ(kItem2Key, iter->first); |
369 EXPECT_EQ(item2.value, iter->second.value); | 370 EXPECT_EQ(item2.value, iter->second.value); |
370 | 371 |
371 ++iter; | 372 ++iter; |
372 ASSERT_TRUE(iter != cache2.end()); | 373 ASSERT_TRUE(iter != cache2.end()); |
373 EXPECT_EQ(kItem1Key, iter->first); | 374 EXPECT_EQ(kItem1Key, iter->first); |
374 EXPECT_EQ(item1.value, iter->second.value); | 375 EXPECT_EQ(item1.value, iter->second.value); |
375 } | 376 } |
376 } | 377 } |
OLD | NEW |