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