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" | |
6 #import "ios/chrome/browser/snapshots/lru_cache.h" | 5 #import "ios/chrome/browser/snapshots/lru_cache.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
8 | 7 |
| 8 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 9 #error "This file requires ARC support." |
| 10 #endif |
| 11 |
9 namespace { | 12 namespace { |
10 | 13 |
11 TEST(LRUCacheTest, Basic) { | 14 TEST(LRUCacheTest, Basic) { |
12 base::scoped_nsobject<LRUCache> cache([[LRUCache alloc] initWithCacheSize:3]); | 15 LRUCache* cache = [[LRUCache alloc] initWithCacheSize:3]; |
13 | 16 |
14 base::scoped_nsobject<NSString> value1( | 17 NSString* value1 = @"Value 1"; |
15 [[NSString alloc] initWithString:@"Value 1"]); | 18 NSString* value2 = @"Value 2"; |
16 base::scoped_nsobject<NSString> value2( | 19 NSString* value3 = @"Value 3"; |
17 [[NSString alloc] initWithString:@"Value 2"]); | 20 NSString* value4 = @"Value 4"; |
18 base::scoped_nsobject<NSString> value3( | |
19 [[NSString alloc] initWithString:@"Value 3"]); | |
20 base::scoped_nsobject<NSString> value4( | |
21 [[NSString alloc] initWithString:@"Value 4"]); | |
22 | 21 |
23 EXPECT_TRUE([cache count] == 0); | 22 EXPECT_TRUE([cache count] == 0); |
24 EXPECT_TRUE([cache isEmpty]); | 23 EXPECT_TRUE([cache isEmpty]); |
25 | 24 |
26 [cache setObject:value1 forKey:@"VALUE 1"]; | 25 [cache setObject:value1 forKey:@"VALUE 1"]; |
27 [cache setObject:value2 forKey:@"VALUE 2"]; | 26 [cache setObject:value2 forKey:@"VALUE 2"]; |
28 [cache setObject:value3 forKey:@"VALUE 3"]; | 27 [cache setObject:value3 forKey:@"VALUE 3"]; |
29 [cache setObject:value4 forKey:@"VALUE 4"]; | 28 [cache setObject:value4 forKey:@"VALUE 4"]; |
30 | 29 |
31 EXPECT_TRUE([cache count] == 3); | 30 EXPECT_TRUE([cache count] == 3); |
32 | 31 |
33 // Check LRU behaviour, the value least recently added value should have been | 32 // Check LRU behaviour, the value least recently added value should have been |
34 // evicted. | 33 // evicted. |
35 id value = [cache objectForKey:@"VALUE 1"]; | 34 id value = [cache objectForKey:@"VALUE 1"]; |
36 EXPECT_TRUE(value == nil); | 35 EXPECT_TRUE(value == nil); |
37 | 36 |
38 value = [cache objectForKey:@"VALUE 2"]; | 37 value = [cache objectForKey:@"VALUE 2"]; |
39 EXPECT_TRUE(value == value2.get()); | 38 EXPECT_TRUE(value == value2); |
40 | 39 |
41 // Removing a non existing key shouldn't do anything. | 40 // Removing a non existing key shouldn't do anything. |
42 [cache removeObjectForKey:@"XXX"]; | 41 [cache removeObjectForKey:@"XXX"]; |
43 EXPECT_TRUE([cache count] == 3); | 42 EXPECT_TRUE([cache count] == 3); |
44 | 43 |
45 [cache removeAllObjects]; | 44 [cache removeAllObjects]; |
46 EXPECT_TRUE([cache isEmpty]); | 45 EXPECT_TRUE([cache isEmpty]); |
47 } | 46 } |
48 | 47 |
49 } // namespace | 48 } // namespace |
OLD | NEW |