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

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: Remove redundant base:: prefix 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
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>
Nico 2016/04/04 17:14:55 (nit: in a previous file that included ptr_util, y
dcheng 2016/04/04 17:43:38 The automated rules are this: - scoped_ptr.h, if p
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
11 namespace { 14 namespace {
12 15
13 int cached_item_live_count = 0; 16 int cached_item_live_count = 0;
14 17
15 struct CachedItem { 18 struct CachedItem {
16 CachedItem() : value(0) { 19 CachedItem() : value(0) {
17 cached_item_live_count++; 20 cached_item_live_count++;
18 } 21 }
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 // Make it so only the most important element is there. 184 // Make it so only the most important element is there.
182 cache.ShrinkToSize(1); 185 cache.ShrinkToSize(1);
183 186
184 Cache::iterator iter = cache.begin(); 187 Cache::iterator iter = cache.begin();
185 EXPECT_EQ(kItem3Key, iter->first); 188 EXPECT_EQ(kItem3Key, iter->first);
186 EXPECT_EQ(item5.value, iter->second.value); 189 EXPECT_EQ(item5.value, iter->second.value);
187 } 190 }
188 191
189 // Make sure that the owning version release its pointers properly. 192 // Make sure that the owning version release its pointers properly.
190 TEST(MRUCacheTest, Owning) { 193 TEST(MRUCacheTest, Owning) {
191 using Cache = base::MRUCache<int, scoped_ptr<CachedItem>>; 194 using Cache = base::MRUCache<int, std::unique_ptr<CachedItem>>;
192 Cache cache(Cache::NO_AUTO_EVICT); 195 Cache cache(Cache::NO_AUTO_EVICT);
193 196
194 int initial_count = cached_item_live_count; 197 int initial_count = cached_item_live_count;
195 198
196 // First insert and item and then overwrite it. 199 // First insert and item and then overwrite it.
197 static const int kItem1Key = 1; 200 static const int kItem1Key = 1;
198 cache.Put(kItem1Key, make_scoped_ptr(new CachedItem(20))); 201 cache.Put(kItem1Key, WrapUnique(new CachedItem(20)));
199 cache.Put(kItem1Key, make_scoped_ptr(new CachedItem(22))); 202 cache.Put(kItem1Key, WrapUnique(new CachedItem(22)));
200 203
201 // There should still be one item, and one extra live item. 204 // There should still be one item, and one extra live item.
202 Cache::iterator iter = cache.Get(kItem1Key); 205 Cache::iterator iter = cache.Get(kItem1Key);
203 EXPECT_EQ(1U, cache.size()); 206 EXPECT_EQ(1U, cache.size());
204 EXPECT_TRUE(iter != cache.end()); 207 EXPECT_TRUE(iter != cache.end());
205 EXPECT_EQ(initial_count + 1, cached_item_live_count); 208 EXPECT_EQ(initial_count + 1, cached_item_live_count);
206 209
207 // Now remove it. 210 // Now remove it.
208 cache.Erase(cache.begin()); 211 cache.Erase(cache.begin());
209 EXPECT_EQ(initial_count, cached_item_live_count); 212 EXPECT_EQ(initial_count, cached_item_live_count);
210 213
211 // Now try another cache that goes out of scope to make sure its pointers 214 // Now try another cache that goes out of scope to make sure its pointers
212 // go away. 215 // go away.
213 { 216 {
214 Cache cache2(Cache::NO_AUTO_EVICT); 217 Cache cache2(Cache::NO_AUTO_EVICT);
215 cache2.Put(1, make_scoped_ptr(new CachedItem(20))); 218 cache2.Put(1, WrapUnique(new CachedItem(20)));
216 cache2.Put(2, make_scoped_ptr(new CachedItem(20))); 219 cache2.Put(2, WrapUnique(new CachedItem(20)));
217 } 220 }
218 221
219 // There should be no objects leaked. 222 // There should be no objects leaked.
220 EXPECT_EQ(initial_count, cached_item_live_count); 223 EXPECT_EQ(initial_count, cached_item_live_count);
221 224
222 // Check that Clear() also frees things correctly. 225 // Check that Clear() also frees things correctly.
223 { 226 {
224 Cache cache2(Cache::NO_AUTO_EVICT); 227 Cache cache2(Cache::NO_AUTO_EVICT);
225 cache2.Put(1, make_scoped_ptr(new CachedItem(20))); 228 cache2.Put(1, WrapUnique(new CachedItem(20)));
226 cache2.Put(2, make_scoped_ptr(new CachedItem(20))); 229 cache2.Put(2, WrapUnique(new CachedItem(20)));
227 EXPECT_EQ(initial_count + 2, cached_item_live_count); 230 EXPECT_EQ(initial_count + 2, cached_item_live_count);
228 cache2.Clear(); 231 cache2.Clear();
229 EXPECT_EQ(initial_count, cached_item_live_count); 232 EXPECT_EQ(initial_count, cached_item_live_count);
230 } 233 }
231 } 234 }
232 235
233 TEST(MRUCacheTest, AutoEvict) { 236 TEST(MRUCacheTest, AutoEvict) {
234 using Cache = base::MRUCache<int, scoped_ptr<CachedItem>>; 237 using Cache = base::MRUCache<int, std::unique_ptr<CachedItem>>;
235 static const Cache::size_type kMaxSize = 3; 238 static const Cache::size_type kMaxSize = 3;
236 239
237 int initial_count = cached_item_live_count; 240 int initial_count = cached_item_live_count;
238 241
239 { 242 {
240 Cache cache(kMaxSize); 243 Cache cache(kMaxSize);
241 244
242 static const int kItem1Key = 1, kItem2Key = 2, kItem3Key = 3, kItem4Key = 4; 245 static const int kItem1Key = 1, kItem2Key = 2, kItem3Key = 3, kItem4Key = 4;
243 cache.Put(kItem1Key, make_scoped_ptr(new CachedItem(20))); 246 cache.Put(kItem1Key, WrapUnique(new CachedItem(20)));
244 cache.Put(kItem2Key, make_scoped_ptr(new CachedItem(21))); 247 cache.Put(kItem2Key, WrapUnique(new CachedItem(21)));
245 cache.Put(kItem3Key, make_scoped_ptr(new CachedItem(22))); 248 cache.Put(kItem3Key, WrapUnique(new CachedItem(22)));
246 cache.Put(kItem4Key, make_scoped_ptr(new CachedItem(23))); 249 cache.Put(kItem4Key, WrapUnique(new CachedItem(23)));
247 250
248 // The cache should only have kMaxSize items in it even though we inserted 251 // The cache should only have kMaxSize items in it even though we inserted
249 // more. 252 // more.
250 EXPECT_EQ(kMaxSize, cache.size()); 253 EXPECT_EQ(kMaxSize, cache.size());
251 } 254 }
252 255
253 // There should be no objects leaked. 256 // There should be no objects leaked.
254 EXPECT_EQ(initial_count, cached_item_live_count); 257 EXPECT_EQ(initial_count, cached_item_live_count);
255 } 258 }
256 259
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 ASSERT_TRUE(iter != cache2.end()); 371 ASSERT_TRUE(iter != cache2.end());
369 EXPECT_EQ(kItem2Key, iter->first); 372 EXPECT_EQ(kItem2Key, iter->first);
370 EXPECT_EQ(item2.value, iter->second.value); 373 EXPECT_EQ(item2.value, iter->second.value);
371 374
372 ++iter; 375 ++iter;
373 ASSERT_TRUE(iter != cache2.end()); 376 ASSERT_TRUE(iter != cache2.end());
374 EXPECT_EQ(kItem1Key, iter->first); 377 EXPECT_EQ(kItem1Key, iter->first);
375 EXPECT_EQ(item1.value, iter->second.value); 378 EXPECT_EQ(item1.value, iter->second.value);
376 } 379 }
377 } 380 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698