| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "base/mac/scoped_nsobject.h" |
| 6 #import "ios/chrome/browser/snapshots/lru_cache.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 @interface LRUCacheTestDelegate : NSObject<LRUCacheDelegate> |
| 10 |
| 11 @property(nonatomic, retain) id<NSObject> lastEvictedObject; |
| 12 @property(nonatomic, assign) NSInteger evictedObjectsCount; |
| 13 |
| 14 @end |
| 15 |
| 16 @implementation LRUCacheTestDelegate |
| 17 |
| 18 @synthesize lastEvictedObject = _lastEvictedObject; |
| 19 @synthesize evictedObjectsCount = _evictedObjectsCount; |
| 20 |
| 21 - (void)lruCacheWillEvictObject:(id<NSObject>)obj { |
| 22 self.lastEvictedObject = obj; |
| 23 ++_evictedObjectsCount; |
| 24 } |
| 25 |
| 26 @end |
| 27 |
| 28 namespace { |
| 29 |
| 30 TEST(LRUCacheTest, Basic) { |
| 31 base::scoped_nsobject<LRUCache> cache([[LRUCache alloc] initWithCacheSize:3]); |
| 32 base::scoped_nsobject<LRUCacheTestDelegate> delegate( |
| 33 [[LRUCacheTestDelegate alloc] init]); |
| 34 [cache setDelegate:delegate]; |
| 35 |
| 36 base::scoped_nsobject<NSString> value1( |
| 37 [[NSString alloc] initWithString:@"Value 1"]); |
| 38 base::scoped_nsobject<NSString> value2( |
| 39 [[NSString alloc] initWithString:@"Value 2"]); |
| 40 base::scoped_nsobject<NSString> value3( |
| 41 [[NSString alloc] initWithString:@"Value 3"]); |
| 42 base::scoped_nsobject<NSString> value4( |
| 43 [[NSString alloc] initWithString:@"Value 4"]); |
| 44 |
| 45 EXPECT_TRUE([cache count] == 0); |
| 46 EXPECT_TRUE([cache isEmpty]); |
| 47 |
| 48 [cache setObject:value1 forKey:@"VALUE 1"]; |
| 49 [cache setObject:value2 forKey:@"VALUE 2"]; |
| 50 [cache setObject:value3 forKey:@"VALUE 3"]; |
| 51 [cache setObject:value4 forKey:@"VALUE 4"]; |
| 52 |
| 53 EXPECT_TRUE([cache count] == 3); |
| 54 EXPECT_TRUE([delegate evictedObjectsCount] == 1); |
| 55 EXPECT_TRUE([delegate lastEvictedObject] == value1.get()); |
| 56 |
| 57 // Check LRU behaviour, the value least recently added value should have been |
| 58 // evicted. |
| 59 id value = [cache objectForKey:@"VALUE 1"]; |
| 60 EXPECT_TRUE(value == nil); |
| 61 |
| 62 value = [cache objectForKey:@"VALUE 2"]; |
| 63 EXPECT_TRUE(value == value2.get()); |
| 64 |
| 65 // Removing a non existing key shouldn't do anything. |
| 66 [cache removeObjectForKey:@"XXX"]; |
| 67 EXPECT_TRUE([cache count] == 3); |
| 68 |
| 69 [cache removeAllObjects]; |
| 70 EXPECT_TRUE([cache isEmpty]); |
| 71 } |
| 72 |
| 73 } // namespace |
| OLD | NEW |