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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/containers/mru_cache.h" | 6 #include "base/containers/mru_cache.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 namespace { | 9 namespace { |
10 | 10 |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 262 |
263 CachedItem two(2); | 263 CachedItem two(2); |
264 cache.Put("Second", two); | 264 cache.Put("Second", two); |
265 | 265 |
266 EXPECT_EQ(one.value, cache.Get("First")->second.value); | 266 EXPECT_EQ(one.value, cache.Get("First")->second.value); |
267 EXPECT_EQ(two.value, cache.Get("Second")->second.value); | 267 EXPECT_EQ(two.value, cache.Get("Second")->second.value); |
268 cache.ShrinkToSize(1); | 268 cache.ShrinkToSize(1); |
269 EXPECT_EQ(two.value, cache.Get("Second")->second.value); | 269 EXPECT_EQ(two.value, cache.Get("Second")->second.value); |
270 EXPECT_TRUE(cache.Get("First") == cache.end()); | 270 EXPECT_TRUE(cache.Get("First") == cache.end()); |
271 } | 271 } |
| 272 |
| 273 TEST(MRUCacheTest, Swap) { |
| 274 typedef base::MRUCache<int, CachedItem> Cache; |
| 275 Cache cache1(Cache::NO_AUTO_EVICT); |
| 276 |
| 277 // Insert two items into cache1. |
| 278 static const int kItem1Key = 1; |
| 279 CachedItem item1(2); |
| 280 Cache::iterator inserted_item = cache1.Put(kItem1Key, item1); |
| 281 EXPECT_EQ(1U, cache1.size()); |
| 282 |
| 283 static const int kItem2Key = 3; |
| 284 CachedItem item2(4); |
| 285 cache1.Put(kItem2Key, item2); |
| 286 EXPECT_EQ(2U, cache1.size()); |
| 287 |
| 288 // Verify cache1's elements. |
| 289 { |
| 290 Cache::iterator iter = cache1.begin(); |
| 291 ASSERT_TRUE(iter != cache1.end()); |
| 292 EXPECT_EQ(kItem2Key, iter->first); |
| 293 EXPECT_EQ(item2.value, iter->second.value); |
| 294 |
| 295 ++iter; |
| 296 ASSERT_TRUE(iter != cache1.end()); |
| 297 EXPECT_EQ(kItem1Key, iter->first); |
| 298 EXPECT_EQ(item1.value, iter->second.value); |
| 299 } |
| 300 |
| 301 // Create another cache2. |
| 302 Cache cache2(Cache::NO_AUTO_EVICT); |
| 303 |
| 304 // Insert three items into cache2. |
| 305 static const int kItem3Key = 5; |
| 306 CachedItem item3(6); |
| 307 inserted_item = cache2.Put(kItem3Key, item3); |
| 308 EXPECT_EQ(1U, cache2.size()); |
| 309 |
| 310 static const int kItem4Key = 7; |
| 311 CachedItem item4(8); |
| 312 cache2.Put(kItem4Key, item4); |
| 313 EXPECT_EQ(2U, cache2.size()); |
| 314 |
| 315 static const int kItem5Key = 9; |
| 316 CachedItem item5(10); |
| 317 cache2.Put(kItem5Key, item5); |
| 318 EXPECT_EQ(3U, cache2.size()); |
| 319 |
| 320 // Verify cache2's elements. |
| 321 { |
| 322 Cache::iterator iter = cache2.begin(); |
| 323 ASSERT_TRUE(iter != cache2.end()); |
| 324 EXPECT_EQ(kItem5Key, iter->first); |
| 325 EXPECT_EQ(item5.value, iter->second.value); |
| 326 |
| 327 ++iter; |
| 328 ASSERT_TRUE(iter != cache2.end()); |
| 329 EXPECT_EQ(kItem4Key, iter->first); |
| 330 EXPECT_EQ(item4.value, iter->second.value); |
| 331 |
| 332 ++iter; |
| 333 ASSERT_TRUE(iter != cache2.end()); |
| 334 EXPECT_EQ(kItem3Key, iter->first); |
| 335 EXPECT_EQ(item3.value, iter->second.value); |
| 336 } |
| 337 |
| 338 // Swap cache1 and cache2 and verify cache2 has cache1's elements and cache1 |
| 339 // has cache2's elements. |
| 340 cache2.Swap(cache1); |
| 341 |
| 342 EXPECT_EQ(3U, cache1.size()); |
| 343 EXPECT_EQ(2U, cache2.size()); |
| 344 |
| 345 // Verify cache1's elements. |
| 346 { |
| 347 Cache::iterator iter = cache1.begin(); |
| 348 ASSERT_TRUE(iter != cache1.end()); |
| 349 EXPECT_EQ(kItem5Key, iter->first); |
| 350 EXPECT_EQ(item5.value, iter->second.value); |
| 351 |
| 352 ++iter; |
| 353 ASSERT_TRUE(iter != cache1.end()); |
| 354 EXPECT_EQ(kItem4Key, iter->first); |
| 355 EXPECT_EQ(item4.value, iter->second.value); |
| 356 |
| 357 ++iter; |
| 358 ASSERT_TRUE(iter != cache1.end()); |
| 359 EXPECT_EQ(kItem3Key, iter->first); |
| 360 EXPECT_EQ(item3.value, iter->second.value); |
| 361 } |
| 362 |
| 363 // Verify cache2's elements. |
| 364 { |
| 365 Cache::iterator iter = cache2.begin(); |
| 366 ASSERT_TRUE(iter != cache2.end()); |
| 367 EXPECT_EQ(kItem2Key, iter->first); |
| 368 EXPECT_EQ(item2.value, iter->second.value); |
| 369 |
| 370 ++iter; |
| 371 ASSERT_TRUE(iter != cache2.end()); |
| 372 EXPECT_EQ(kItem1Key, iter->first); |
| 373 EXPECT_EQ(item1.value, iter->second.value); |
| 374 } |
| 375 } |
OLD | NEW |