Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Side by Side Diff: base/containers/mru_cache_unittest.cc

Issue 1852433005: Convert //base to use std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase after r384946 Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/command_line_unittest.cc ('k') | base/containers/scoped_ptr_hash_map.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/containers/mru_cache.h"
6
5 #include <stddef.h> 7 #include <stddef.h>
6 8
7 #include "base/containers/mru_cache.h" 9 #include <memory>
8 #include "base/memory/scoped_ptr.h" 10
11 #include "base/memory/ptr_util.h"
9 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
10 13
14 namespace base {
15
11 namespace { 16 namespace {
12 17
13 int cached_item_live_count = 0; 18 int cached_item_live_count = 0;
14 19
15 struct CachedItem { 20 struct CachedItem {
16 CachedItem() : value(0) { 21 CachedItem() : value(0) {
17 cached_item_live_count++; 22 cached_item_live_count++;
18 } 23 }
19 24
20 explicit CachedItem(int new_value) : value(new_value) { 25 explicit CachedItem(int new_value) : value(new_value) {
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 // Make it so only the most important element is there. 186 // Make it so only the most important element is there.
182 cache.ShrinkToSize(1); 187 cache.ShrinkToSize(1);
183 188
184 Cache::iterator iter = cache.begin(); 189 Cache::iterator iter = cache.begin();
185 EXPECT_EQ(kItem3Key, iter->first); 190 EXPECT_EQ(kItem3Key, iter->first);
186 EXPECT_EQ(item5.value, iter->second.value); 191 EXPECT_EQ(item5.value, iter->second.value);
187 } 192 }
188 193
189 // Make sure that the owning version release its pointers properly. 194 // Make sure that the owning version release its pointers properly.
190 TEST(MRUCacheTest, Owning) { 195 TEST(MRUCacheTest, Owning) {
191 using Cache = base::MRUCache<int, scoped_ptr<CachedItem>>; 196 using Cache = base::MRUCache<int, std::unique_ptr<CachedItem>>;
192 Cache cache(Cache::NO_AUTO_EVICT); 197 Cache cache(Cache::NO_AUTO_EVICT);
193 198
194 int initial_count = cached_item_live_count; 199 int initial_count = cached_item_live_count;
195 200
196 // First insert and item and then overwrite it. 201 // First insert and item and then overwrite it.
197 static const int kItem1Key = 1; 202 static const int kItem1Key = 1;
198 cache.Put(kItem1Key, make_scoped_ptr(new CachedItem(20))); 203 cache.Put(kItem1Key, WrapUnique(new CachedItem(20)));
199 cache.Put(kItem1Key, make_scoped_ptr(new CachedItem(22))); 204 cache.Put(kItem1Key, WrapUnique(new CachedItem(22)));
200 205
201 // There should still be one item, and one extra live item. 206 // There should still be one item, and one extra live item.
202 Cache::iterator iter = cache.Get(kItem1Key); 207 Cache::iterator iter = cache.Get(kItem1Key);
203 EXPECT_EQ(1U, cache.size()); 208 EXPECT_EQ(1U, cache.size());
204 EXPECT_TRUE(iter != cache.end()); 209 EXPECT_TRUE(iter != cache.end());
205 EXPECT_EQ(initial_count + 1, cached_item_live_count); 210 EXPECT_EQ(initial_count + 1, cached_item_live_count);
206 211
207 // Now remove it. 212 // Now remove it.
208 cache.Erase(cache.begin()); 213 cache.Erase(cache.begin());
209 EXPECT_EQ(initial_count, cached_item_live_count); 214 EXPECT_EQ(initial_count, cached_item_live_count);
210 215
211 // Now try another cache that goes out of scope to make sure its pointers 216 // Now try another cache that goes out of scope to make sure its pointers
212 // go away. 217 // go away.
213 { 218 {
214 Cache cache2(Cache::NO_AUTO_EVICT); 219 Cache cache2(Cache::NO_AUTO_EVICT);
215 cache2.Put(1, make_scoped_ptr(new CachedItem(20))); 220 cache2.Put(1, WrapUnique(new CachedItem(20)));
216 cache2.Put(2, make_scoped_ptr(new CachedItem(20))); 221 cache2.Put(2, WrapUnique(new CachedItem(20)));
217 } 222 }
218 223
219 // There should be no objects leaked. 224 // There should be no objects leaked.
220 EXPECT_EQ(initial_count, cached_item_live_count); 225 EXPECT_EQ(initial_count, cached_item_live_count);
221 226
222 // Check that Clear() also frees things correctly. 227 // Check that Clear() also frees things correctly.
223 { 228 {
224 Cache cache2(Cache::NO_AUTO_EVICT); 229 Cache cache2(Cache::NO_AUTO_EVICT);
225 cache2.Put(1, make_scoped_ptr(new CachedItem(20))); 230 cache2.Put(1, WrapUnique(new CachedItem(20)));
226 cache2.Put(2, make_scoped_ptr(new CachedItem(20))); 231 cache2.Put(2, WrapUnique(new CachedItem(20)));
227 EXPECT_EQ(initial_count + 2, cached_item_live_count); 232 EXPECT_EQ(initial_count + 2, cached_item_live_count);
228 cache2.Clear(); 233 cache2.Clear();
229 EXPECT_EQ(initial_count, cached_item_live_count); 234 EXPECT_EQ(initial_count, cached_item_live_count);
230 } 235 }
231 } 236 }
232 237
233 TEST(MRUCacheTest, AutoEvict) { 238 TEST(MRUCacheTest, AutoEvict) {
234 using Cache = base::MRUCache<int, scoped_ptr<CachedItem>>; 239 using Cache = base::MRUCache<int, std::unique_ptr<CachedItem>>;
235 static const Cache::size_type kMaxSize = 3; 240 static const Cache::size_type kMaxSize = 3;
236 241
237 int initial_count = cached_item_live_count; 242 int initial_count = cached_item_live_count;
238 243
239 { 244 {
240 Cache cache(kMaxSize); 245 Cache cache(kMaxSize);
241 246
242 static const int kItem1Key = 1, kItem2Key = 2, kItem3Key = 3, kItem4Key = 4; 247 static const int kItem1Key = 1, kItem2Key = 2, kItem3Key = 3, kItem4Key = 4;
243 cache.Put(kItem1Key, make_scoped_ptr(new CachedItem(20))); 248 cache.Put(kItem1Key, WrapUnique(new CachedItem(20)));
244 cache.Put(kItem2Key, make_scoped_ptr(new CachedItem(21))); 249 cache.Put(kItem2Key, WrapUnique(new CachedItem(21)));
245 cache.Put(kItem3Key, make_scoped_ptr(new CachedItem(22))); 250 cache.Put(kItem3Key, WrapUnique(new CachedItem(22)));
246 cache.Put(kItem4Key, make_scoped_ptr(new CachedItem(23))); 251 cache.Put(kItem4Key, WrapUnique(new CachedItem(23)));
247 252
248 // The cache should only have kMaxSize items in it even though we inserted 253 // The cache should only have kMaxSize items in it even though we inserted
249 // more. 254 // more.
250 EXPECT_EQ(kMaxSize, cache.size()); 255 EXPECT_EQ(kMaxSize, cache.size());
251 } 256 }
252 257
253 // There should be no objects leaked. 258 // There should be no objects leaked.
254 EXPECT_EQ(initial_count, cached_item_live_count); 259 EXPECT_EQ(initial_count, cached_item_live_count);
255 } 260 }
256 261
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 ASSERT_TRUE(iter != cache2.end()); 373 ASSERT_TRUE(iter != cache2.end());
369 EXPECT_EQ(kItem2Key, iter->first); 374 EXPECT_EQ(kItem2Key, iter->first);
370 EXPECT_EQ(item2.value, iter->second.value); 375 EXPECT_EQ(item2.value, iter->second.value);
371 376
372 ++iter; 377 ++iter;
373 ASSERT_TRUE(iter != cache2.end()); 378 ASSERT_TRUE(iter != cache2.end());
374 EXPECT_EQ(kItem1Key, iter->first); 379 EXPECT_EQ(kItem1Key, iter->first);
375 EXPECT_EQ(item1.value, iter->second.value); 380 EXPECT_EQ(item1.value, iter->second.value);
376 } 381 }
377 } 382 }
383
384 } // namespace base
OLDNEW
« no previous file with comments | « base/command_line_unittest.cc ('k') | base/containers/scoped_ptr_hash_map.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698