xref: /aosp_15_r20/external/libchrome/base/containers/mru_cache_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/containers/mru_cache.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <cstddef>
8*635a8641SAndroid Build Coastguard Worker #include <memory>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include "base/memory/ptr_util.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/trace_event/memory_usage_estimator.h"
12*635a8641SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker namespace base {
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker namespace {
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker int cached_item_live_count = 0;
19*635a8641SAndroid Build Coastguard Worker 
20*635a8641SAndroid Build Coastguard Worker struct CachedItem {
CachedItembase::__anona0a6db340111::CachedItem21*635a8641SAndroid Build Coastguard Worker   CachedItem() : value(0) {
22*635a8641SAndroid Build Coastguard Worker     cached_item_live_count++;
23*635a8641SAndroid Build Coastguard Worker   }
24*635a8641SAndroid Build Coastguard Worker 
CachedItembase::__anona0a6db340111::CachedItem25*635a8641SAndroid Build Coastguard Worker   explicit CachedItem(int new_value) : value(new_value) {
26*635a8641SAndroid Build Coastguard Worker     cached_item_live_count++;
27*635a8641SAndroid Build Coastguard Worker   }
28*635a8641SAndroid Build Coastguard Worker 
CachedItembase::__anona0a6db340111::CachedItem29*635a8641SAndroid Build Coastguard Worker   explicit CachedItem(const CachedItem& other) : value(other.value) {
30*635a8641SAndroid Build Coastguard Worker     cached_item_live_count++;
31*635a8641SAndroid Build Coastguard Worker   }
32*635a8641SAndroid Build Coastguard Worker 
~CachedItembase::__anona0a6db340111::CachedItem33*635a8641SAndroid Build Coastguard Worker   ~CachedItem() {
34*635a8641SAndroid Build Coastguard Worker     cached_item_live_count--;
35*635a8641SAndroid Build Coastguard Worker   }
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker   int value;
38*635a8641SAndroid Build Coastguard Worker };
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker }  // namespace
41*635a8641SAndroid Build Coastguard Worker 
TEST(MRUCacheTest,Basic)42*635a8641SAndroid Build Coastguard Worker TEST(MRUCacheTest, Basic) {
43*635a8641SAndroid Build Coastguard Worker   typedef base::MRUCache<int, CachedItem> Cache;
44*635a8641SAndroid Build Coastguard Worker   Cache cache(Cache::NO_AUTO_EVICT);
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker   // Check failure conditions
47*635a8641SAndroid Build Coastguard Worker   {
48*635a8641SAndroid Build Coastguard Worker     CachedItem test_item;
49*635a8641SAndroid Build Coastguard Worker     EXPECT_TRUE(cache.Get(0) == cache.end());
50*635a8641SAndroid Build Coastguard Worker     EXPECT_TRUE(cache.Peek(0) == cache.end());
51*635a8641SAndroid Build Coastguard Worker   }
52*635a8641SAndroid Build Coastguard Worker 
53*635a8641SAndroid Build Coastguard Worker   static const int kItem1Key = 5;
54*635a8641SAndroid Build Coastguard Worker   CachedItem item1(10);
55*635a8641SAndroid Build Coastguard Worker   Cache::iterator inserted_item = cache.Put(kItem1Key, item1);
56*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(1U, cache.size());
57*635a8641SAndroid Build Coastguard Worker 
58*635a8641SAndroid Build Coastguard Worker   // Check that item1 was properly inserted.
59*635a8641SAndroid Build Coastguard Worker   {
60*635a8641SAndroid Build Coastguard Worker     Cache::iterator found = cache.Get(kItem1Key);
61*635a8641SAndroid Build Coastguard Worker     EXPECT_TRUE(inserted_item == cache.begin());
62*635a8641SAndroid Build Coastguard Worker     EXPECT_TRUE(found != cache.end());
63*635a8641SAndroid Build Coastguard Worker 
64*635a8641SAndroid Build Coastguard Worker     found = cache.Peek(kItem1Key);
65*635a8641SAndroid Build Coastguard Worker     EXPECT_TRUE(found != cache.end());
66*635a8641SAndroid Build Coastguard Worker 
67*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem1Key, found->first);
68*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item1.value, found->second.value);
69*635a8641SAndroid Build Coastguard Worker   }
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker   static const int kItem2Key = 7;
72*635a8641SAndroid Build Coastguard Worker   CachedItem item2(12);
73*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem2Key, item2);
74*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(2U, cache.size());
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker   // Check that item1 is the oldest since item2 was added afterwards.
77*635a8641SAndroid Build Coastguard Worker   {
78*635a8641SAndroid Build Coastguard Worker     Cache::reverse_iterator oldest = cache.rbegin();
79*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(oldest != cache.rend());
80*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem1Key, oldest->first);
81*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item1.value, oldest->second.value);
82*635a8641SAndroid Build Coastguard Worker   }
83*635a8641SAndroid Build Coastguard Worker 
84*635a8641SAndroid Build Coastguard Worker   // Check that item1 is still accessible by key.
85*635a8641SAndroid Build Coastguard Worker   {
86*635a8641SAndroid Build Coastguard Worker     Cache::iterator test_item = cache.Get(kItem1Key);
87*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(test_item != cache.end());
88*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem1Key, test_item->first);
89*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item1.value, test_item->second.value);
90*635a8641SAndroid Build Coastguard Worker   }
91*635a8641SAndroid Build Coastguard Worker 
92*635a8641SAndroid Build Coastguard Worker   // Check that retrieving item1 pushed item2 to oldest.
93*635a8641SAndroid Build Coastguard Worker   {
94*635a8641SAndroid Build Coastguard Worker     Cache::reverse_iterator oldest = cache.rbegin();
95*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(oldest != cache.rend());
96*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem2Key, oldest->first);
97*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item2.value, oldest->second.value);
98*635a8641SAndroid Build Coastguard Worker   }
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker   // Remove the oldest item and check that item1 is now the only member.
101*635a8641SAndroid Build Coastguard Worker   {
102*635a8641SAndroid Build Coastguard Worker     Cache::reverse_iterator next = cache.Erase(cache.rbegin());
103*635a8641SAndroid Build Coastguard Worker 
104*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(1U, cache.size());
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker     EXPECT_TRUE(next == cache.rbegin());
107*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem1Key, next->first);
108*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item1.value, next->second.value);
109*635a8641SAndroid Build Coastguard Worker 
110*635a8641SAndroid Build Coastguard Worker     cache.Erase(cache.begin());
111*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(0U, cache.size());
112*635a8641SAndroid Build Coastguard Worker   }
113*635a8641SAndroid Build Coastguard Worker 
114*635a8641SAndroid Build Coastguard Worker   // Check that Clear() works properly.
115*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem1Key, item1);
116*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem2Key, item2);
117*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(2U, cache.size());
118*635a8641SAndroid Build Coastguard Worker   cache.Clear();
119*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(0U, cache.size());
120*635a8641SAndroid Build Coastguard Worker }
121*635a8641SAndroid Build Coastguard Worker 
TEST(MRUCacheTest,GetVsPeek)122*635a8641SAndroid Build Coastguard Worker TEST(MRUCacheTest, GetVsPeek) {
123*635a8641SAndroid Build Coastguard Worker   typedef base::MRUCache<int, CachedItem> Cache;
124*635a8641SAndroid Build Coastguard Worker   Cache cache(Cache::NO_AUTO_EVICT);
125*635a8641SAndroid Build Coastguard Worker 
126*635a8641SAndroid Build Coastguard Worker   static const int kItem1Key = 1;
127*635a8641SAndroid Build Coastguard Worker   CachedItem item1(10);
128*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem1Key, item1);
129*635a8641SAndroid Build Coastguard Worker 
130*635a8641SAndroid Build Coastguard Worker   static const int kItem2Key = 2;
131*635a8641SAndroid Build Coastguard Worker   CachedItem item2(20);
132*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem2Key, item2);
133*635a8641SAndroid Build Coastguard Worker 
134*635a8641SAndroid Build Coastguard Worker   // This should do nothing since the size is bigger than the number of items.
135*635a8641SAndroid Build Coastguard Worker   cache.ShrinkToSize(100);
136*635a8641SAndroid Build Coastguard Worker 
137*635a8641SAndroid Build Coastguard Worker   // Check that item1 starts out as oldest
138*635a8641SAndroid Build Coastguard Worker   {
139*635a8641SAndroid Build Coastguard Worker     Cache::reverse_iterator iter = cache.rbegin();
140*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache.rend());
141*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem1Key, iter->first);
142*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item1.value, iter->second.value);
143*635a8641SAndroid Build Coastguard Worker   }
144*635a8641SAndroid Build Coastguard Worker 
145*635a8641SAndroid Build Coastguard Worker   // Check that Peek doesn't change ordering
146*635a8641SAndroid Build Coastguard Worker   {
147*635a8641SAndroid Build Coastguard Worker     Cache::iterator peekiter = cache.Peek(kItem1Key);
148*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(peekiter != cache.end());
149*635a8641SAndroid Build Coastguard Worker 
150*635a8641SAndroid Build Coastguard Worker     Cache::reverse_iterator iter = cache.rbegin();
151*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache.rend());
152*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem1Key, iter->first);
153*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item1.value, iter->second.value);
154*635a8641SAndroid Build Coastguard Worker   }
155*635a8641SAndroid Build Coastguard Worker }
156*635a8641SAndroid Build Coastguard Worker 
TEST(MRUCacheTest,KeyReplacement)157*635a8641SAndroid Build Coastguard Worker TEST(MRUCacheTest, KeyReplacement) {
158*635a8641SAndroid Build Coastguard Worker   typedef base::MRUCache<int, CachedItem> Cache;
159*635a8641SAndroid Build Coastguard Worker   Cache cache(Cache::NO_AUTO_EVICT);
160*635a8641SAndroid Build Coastguard Worker 
161*635a8641SAndroid Build Coastguard Worker   static const int kItem1Key = 1;
162*635a8641SAndroid Build Coastguard Worker   CachedItem item1(10);
163*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem1Key, item1);
164*635a8641SAndroid Build Coastguard Worker 
165*635a8641SAndroid Build Coastguard Worker   static const int kItem2Key = 2;
166*635a8641SAndroid Build Coastguard Worker   CachedItem item2(20);
167*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem2Key, item2);
168*635a8641SAndroid Build Coastguard Worker 
169*635a8641SAndroid Build Coastguard Worker   static const int kItem3Key = 3;
170*635a8641SAndroid Build Coastguard Worker   CachedItem item3(30);
171*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem3Key, item3);
172*635a8641SAndroid Build Coastguard Worker 
173*635a8641SAndroid Build Coastguard Worker   static const int kItem4Key = 4;
174*635a8641SAndroid Build Coastguard Worker   CachedItem item4(40);
175*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem4Key, item4);
176*635a8641SAndroid Build Coastguard Worker 
177*635a8641SAndroid Build Coastguard Worker   CachedItem item5(50);
178*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem3Key, item5);
179*635a8641SAndroid Build Coastguard Worker 
180*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(4U, cache.size());
181*635a8641SAndroid Build Coastguard Worker   for (int i = 0; i < 3; ++i) {
182*635a8641SAndroid Build Coastguard Worker     Cache::reverse_iterator iter = cache.rbegin();
183*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache.rend());
184*635a8641SAndroid Build Coastguard Worker   }
185*635a8641SAndroid Build Coastguard Worker 
186*635a8641SAndroid Build Coastguard Worker   // Make it so only the most important element is there.
187*635a8641SAndroid Build Coastguard Worker   cache.ShrinkToSize(1);
188*635a8641SAndroid Build Coastguard Worker 
189*635a8641SAndroid Build Coastguard Worker   Cache::iterator iter = cache.begin();
190*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(kItem3Key, iter->first);
191*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(item5.value, iter->second.value);
192*635a8641SAndroid Build Coastguard Worker }
193*635a8641SAndroid Build Coastguard Worker 
194*635a8641SAndroid Build Coastguard Worker // Make sure that the owning version release its pointers properly.
TEST(MRUCacheTest,Owning)195*635a8641SAndroid Build Coastguard Worker TEST(MRUCacheTest, Owning) {
196*635a8641SAndroid Build Coastguard Worker   using Cache = base::MRUCache<int, std::unique_ptr<CachedItem>>;
197*635a8641SAndroid Build Coastguard Worker   Cache cache(Cache::NO_AUTO_EVICT);
198*635a8641SAndroid Build Coastguard Worker 
199*635a8641SAndroid Build Coastguard Worker   int initial_count = cached_item_live_count;
200*635a8641SAndroid Build Coastguard Worker 
201*635a8641SAndroid Build Coastguard Worker   // First insert and item and then overwrite it.
202*635a8641SAndroid Build Coastguard Worker   static const int kItem1Key = 1;
203*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem1Key, WrapUnique(new CachedItem(20)));
204*635a8641SAndroid Build Coastguard Worker   cache.Put(kItem1Key, WrapUnique(new CachedItem(22)));
205*635a8641SAndroid Build Coastguard Worker 
206*635a8641SAndroid Build Coastguard Worker   // There should still be one item, and one extra live item.
207*635a8641SAndroid Build Coastguard Worker   Cache::iterator iter = cache.Get(kItem1Key);
208*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(1U, cache.size());
209*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(iter != cache.end());
210*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(initial_count + 1, cached_item_live_count);
211*635a8641SAndroid Build Coastguard Worker 
212*635a8641SAndroid Build Coastguard Worker   // Now remove it.
213*635a8641SAndroid Build Coastguard Worker   cache.Erase(cache.begin());
214*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(initial_count, cached_item_live_count);
215*635a8641SAndroid Build Coastguard Worker 
216*635a8641SAndroid Build Coastguard Worker   // Now try another cache that goes out of scope to make sure its pointers
217*635a8641SAndroid Build Coastguard Worker   // go away.
218*635a8641SAndroid Build Coastguard Worker   {
219*635a8641SAndroid Build Coastguard Worker     Cache cache2(Cache::NO_AUTO_EVICT);
220*635a8641SAndroid Build Coastguard Worker     cache2.Put(1, WrapUnique(new CachedItem(20)));
221*635a8641SAndroid Build Coastguard Worker     cache2.Put(2, WrapUnique(new CachedItem(20)));
222*635a8641SAndroid Build Coastguard Worker   }
223*635a8641SAndroid Build Coastguard Worker 
224*635a8641SAndroid Build Coastguard Worker   // There should be no objects leaked.
225*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(initial_count, cached_item_live_count);
226*635a8641SAndroid Build Coastguard Worker 
227*635a8641SAndroid Build Coastguard Worker   // Check that Clear() also frees things correctly.
228*635a8641SAndroid Build Coastguard Worker   {
229*635a8641SAndroid Build Coastguard Worker     Cache cache2(Cache::NO_AUTO_EVICT);
230*635a8641SAndroid Build Coastguard Worker     cache2.Put(1, WrapUnique(new CachedItem(20)));
231*635a8641SAndroid Build Coastguard Worker     cache2.Put(2, WrapUnique(new CachedItem(20)));
232*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(initial_count + 2, cached_item_live_count);
233*635a8641SAndroid Build Coastguard Worker     cache2.Clear();
234*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(initial_count, cached_item_live_count);
235*635a8641SAndroid Build Coastguard Worker   }
236*635a8641SAndroid Build Coastguard Worker }
237*635a8641SAndroid Build Coastguard Worker 
TEST(MRUCacheTest,AutoEvict)238*635a8641SAndroid Build Coastguard Worker TEST(MRUCacheTest, AutoEvict) {
239*635a8641SAndroid Build Coastguard Worker   using Cache = base::MRUCache<int, std::unique_ptr<CachedItem>>;
240*635a8641SAndroid Build Coastguard Worker   static const Cache::size_type kMaxSize = 3;
241*635a8641SAndroid Build Coastguard Worker 
242*635a8641SAndroid Build Coastguard Worker   int initial_count = cached_item_live_count;
243*635a8641SAndroid Build Coastguard Worker 
244*635a8641SAndroid Build Coastguard Worker   {
245*635a8641SAndroid Build Coastguard Worker     Cache cache(kMaxSize);
246*635a8641SAndroid Build Coastguard Worker 
247*635a8641SAndroid Build Coastguard Worker     static const int kItem1Key = 1, kItem2Key = 2, kItem3Key = 3, kItem4Key = 4;
248*635a8641SAndroid Build Coastguard Worker     cache.Put(kItem1Key, std::make_unique<CachedItem>(20));
249*635a8641SAndroid Build Coastguard Worker     cache.Put(kItem2Key, std::make_unique<CachedItem>(21));
250*635a8641SAndroid Build Coastguard Worker     cache.Put(kItem3Key, std::make_unique<CachedItem>(22));
251*635a8641SAndroid Build Coastguard Worker     cache.Put(kItem4Key, std::make_unique<CachedItem>(23));
252*635a8641SAndroid Build Coastguard Worker 
253*635a8641SAndroid Build Coastguard Worker     // The cache should only have kMaxSize items in it even though we inserted
254*635a8641SAndroid Build Coastguard Worker     // more.
255*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kMaxSize, cache.size());
256*635a8641SAndroid Build Coastguard Worker   }
257*635a8641SAndroid Build Coastguard Worker 
258*635a8641SAndroid Build Coastguard Worker   // There should be no objects leaked.
259*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(initial_count, cached_item_live_count);
260*635a8641SAndroid Build Coastguard Worker }
261*635a8641SAndroid Build Coastguard Worker 
TEST(MRUCacheTest,HashingMRUCache)262*635a8641SAndroid Build Coastguard Worker TEST(MRUCacheTest, HashingMRUCache) {
263*635a8641SAndroid Build Coastguard Worker   // Very simple test to make sure that the hashing cache works correctly.
264*635a8641SAndroid Build Coastguard Worker   typedef base::HashingMRUCache<std::string, CachedItem> Cache;
265*635a8641SAndroid Build Coastguard Worker   Cache cache(Cache::NO_AUTO_EVICT);
266*635a8641SAndroid Build Coastguard Worker 
267*635a8641SAndroid Build Coastguard Worker   CachedItem one(1);
268*635a8641SAndroid Build Coastguard Worker   cache.Put("First", one);
269*635a8641SAndroid Build Coastguard Worker 
270*635a8641SAndroid Build Coastguard Worker   CachedItem two(2);
271*635a8641SAndroid Build Coastguard Worker   cache.Put("Second", two);
272*635a8641SAndroid Build Coastguard Worker 
273*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(one.value, cache.Get("First")->second.value);
274*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(two.value, cache.Get("Second")->second.value);
275*635a8641SAndroid Build Coastguard Worker   cache.ShrinkToSize(1);
276*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(two.value, cache.Get("Second")->second.value);
277*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(cache.Get("First") == cache.end());
278*635a8641SAndroid Build Coastguard Worker }
279*635a8641SAndroid Build Coastguard Worker 
TEST(MRUCacheTest,Swap)280*635a8641SAndroid Build Coastguard Worker TEST(MRUCacheTest, Swap) {
281*635a8641SAndroid Build Coastguard Worker   typedef base::MRUCache<int, CachedItem> Cache;
282*635a8641SAndroid Build Coastguard Worker   Cache cache1(Cache::NO_AUTO_EVICT);
283*635a8641SAndroid Build Coastguard Worker 
284*635a8641SAndroid Build Coastguard Worker   // Insert two items into cache1.
285*635a8641SAndroid Build Coastguard Worker   static const int kItem1Key = 1;
286*635a8641SAndroid Build Coastguard Worker   CachedItem item1(2);
287*635a8641SAndroid Build Coastguard Worker   Cache::iterator inserted_item = cache1.Put(kItem1Key, item1);
288*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(1U, cache1.size());
289*635a8641SAndroid Build Coastguard Worker 
290*635a8641SAndroid Build Coastguard Worker   static const int kItem2Key = 3;
291*635a8641SAndroid Build Coastguard Worker   CachedItem item2(4);
292*635a8641SAndroid Build Coastguard Worker   cache1.Put(kItem2Key, item2);
293*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(2U, cache1.size());
294*635a8641SAndroid Build Coastguard Worker 
295*635a8641SAndroid Build Coastguard Worker   // Verify cache1's elements.
296*635a8641SAndroid Build Coastguard Worker   {
297*635a8641SAndroid Build Coastguard Worker     Cache::iterator iter = cache1.begin();
298*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache1.end());
299*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem2Key, iter->first);
300*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item2.value, iter->second.value);
301*635a8641SAndroid Build Coastguard Worker 
302*635a8641SAndroid Build Coastguard Worker     ++iter;
303*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache1.end());
304*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem1Key, iter->first);
305*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item1.value, iter->second.value);
306*635a8641SAndroid Build Coastguard Worker   }
307*635a8641SAndroid Build Coastguard Worker 
308*635a8641SAndroid Build Coastguard Worker   // Create another cache2.
309*635a8641SAndroid Build Coastguard Worker   Cache cache2(Cache::NO_AUTO_EVICT);
310*635a8641SAndroid Build Coastguard Worker 
311*635a8641SAndroid Build Coastguard Worker   // Insert three items into cache2.
312*635a8641SAndroid Build Coastguard Worker   static const int kItem3Key = 5;
313*635a8641SAndroid Build Coastguard Worker   CachedItem item3(6);
314*635a8641SAndroid Build Coastguard Worker   inserted_item = cache2.Put(kItem3Key, item3);
315*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(1U, cache2.size());
316*635a8641SAndroid Build Coastguard Worker 
317*635a8641SAndroid Build Coastguard Worker   static const int kItem4Key = 7;
318*635a8641SAndroid Build Coastguard Worker   CachedItem item4(8);
319*635a8641SAndroid Build Coastguard Worker   cache2.Put(kItem4Key, item4);
320*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(2U, cache2.size());
321*635a8641SAndroid Build Coastguard Worker 
322*635a8641SAndroid Build Coastguard Worker   static const int kItem5Key = 9;
323*635a8641SAndroid Build Coastguard Worker   CachedItem item5(10);
324*635a8641SAndroid Build Coastguard Worker   cache2.Put(kItem5Key, item5);
325*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(3U, cache2.size());
326*635a8641SAndroid Build Coastguard Worker 
327*635a8641SAndroid Build Coastguard Worker   // Verify cache2's elements.
328*635a8641SAndroid Build Coastguard Worker   {
329*635a8641SAndroid Build Coastguard Worker     Cache::iterator iter = cache2.begin();
330*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache2.end());
331*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem5Key, iter->first);
332*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item5.value, iter->second.value);
333*635a8641SAndroid Build Coastguard Worker 
334*635a8641SAndroid Build Coastguard Worker     ++iter;
335*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache2.end());
336*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem4Key, iter->first);
337*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item4.value, iter->second.value);
338*635a8641SAndroid Build Coastguard Worker 
339*635a8641SAndroid Build Coastguard Worker     ++iter;
340*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache2.end());
341*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem3Key, iter->first);
342*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item3.value, iter->second.value);
343*635a8641SAndroid Build Coastguard Worker   }
344*635a8641SAndroid Build Coastguard Worker 
345*635a8641SAndroid Build Coastguard Worker   // Swap cache1 and cache2 and verify cache2 has cache1's elements and cache1
346*635a8641SAndroid Build Coastguard Worker   // has cache2's elements.
347*635a8641SAndroid Build Coastguard Worker   cache2.Swap(cache1);
348*635a8641SAndroid Build Coastguard Worker 
349*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(3U, cache1.size());
350*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(2U, cache2.size());
351*635a8641SAndroid Build Coastguard Worker 
352*635a8641SAndroid Build Coastguard Worker   // Verify cache1's elements.
353*635a8641SAndroid Build Coastguard Worker   {
354*635a8641SAndroid Build Coastguard Worker     Cache::iterator iter = cache1.begin();
355*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache1.end());
356*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem5Key, iter->first);
357*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item5.value, iter->second.value);
358*635a8641SAndroid Build Coastguard Worker 
359*635a8641SAndroid Build Coastguard Worker     ++iter;
360*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache1.end());
361*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem4Key, iter->first);
362*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item4.value, iter->second.value);
363*635a8641SAndroid Build Coastguard Worker 
364*635a8641SAndroid Build Coastguard Worker     ++iter;
365*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache1.end());
366*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem3Key, iter->first);
367*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item3.value, iter->second.value);
368*635a8641SAndroid Build Coastguard Worker   }
369*635a8641SAndroid Build Coastguard Worker 
370*635a8641SAndroid Build Coastguard Worker   // Verify cache2's elements.
371*635a8641SAndroid Build Coastguard Worker   {
372*635a8641SAndroid Build Coastguard Worker     Cache::iterator iter = cache2.begin();
373*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache2.end());
374*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem2Key, iter->first);
375*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item2.value, iter->second.value);
376*635a8641SAndroid Build Coastguard Worker 
377*635a8641SAndroid Build Coastguard Worker     ++iter;
378*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(iter != cache2.end());
379*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(kItem1Key, iter->first);
380*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(item1.value, iter->second.value);
381*635a8641SAndroid Build Coastguard Worker   }
382*635a8641SAndroid Build Coastguard Worker }
383*635a8641SAndroid Build Coastguard Worker 
TEST(MRUCacheTest,EstimateMemory)384*635a8641SAndroid Build Coastguard Worker TEST(MRUCacheTest, EstimateMemory) {
385*635a8641SAndroid Build Coastguard Worker   base::MRUCache<std::string, int> cache(10);
386*635a8641SAndroid Build Coastguard Worker 
387*635a8641SAndroid Build Coastguard Worker   const std::string key(100u, 'a');
388*635a8641SAndroid Build Coastguard Worker   cache.Put(key, 1);
389*635a8641SAndroid Build Coastguard Worker 
390*635a8641SAndroid Build Coastguard Worker   EXPECT_GT(trace_event::EstimateMemoryUsage(cache),
391*635a8641SAndroid Build Coastguard Worker             trace_event::EstimateMemoryUsage(key));
392*635a8641SAndroid Build Coastguard Worker }
393*635a8641SAndroid Build Coastguard Worker 
394*635a8641SAndroid Build Coastguard Worker }  // namespace base
395