@@ -21,7 +21,8 @@ class TestNSCache : XCTestCase {
21
21
return [
22
22
( " test_setWithUnmutableKeys " , test_setWithUnmutableKeys) ,
23
23
( " test_setWithMutableKeys " , test_setWithMutableKeys) ,
24
- // ("test_countLimit", test_countLimit),
24
+ ( " test_costLimit " , test_costLimit) ,
25
+ ( " test_countLimit " , test_countLimit) ,
25
26
]
26
27
}
27
28
@@ -73,19 +74,36 @@ class TestNSCache : XCTestCase {
73
74
XCTAssertNil ( cache. object ( forKey: key2) , " should be nil " )
74
75
}
75
76
77
+ func test_costLimit( ) {
78
+ let cache = NSCache < NSString , NSString > ( )
79
+ cache. totalCostLimit = 10
80
+
81
+ cache. setObject ( " object0 " , forKey: " 0 " , cost: 4 )
82
+ cache. setObject ( " object2 " , forKey: " 2 " , cost: 5 )
83
+
84
+ cache. setObject ( " object1 " , forKey: " 1 " , cost: 5 )
85
+
86
+ XCTAssertNil ( cache. object ( forKey: " 0 " ) , " should be nil " )
87
+ XCTAssertEqual ( cache. object ( forKey: " 2 " ) , " object2 " , " should be equal to 'object2' " )
88
+ XCTAssertEqual ( cache. object ( forKey: " 1 " ) , " object1 " , " should be equal to 'object1' " )
89
+ }
90
+
76
91
func test_countLimit( ) {
77
92
let cache = NSCache < NSString , NSString > ( )
78
- cache. countLimit = 1
93
+ cache. countLimit = 2
79
94
80
95
let key1 = NSString ( string: " key1 " )
81
96
let key2 = NSString ( string: " key2 " )
97
+ let key3 = NSString ( string: " key3 " )
82
98
let value = NSString ( string: " value " )
83
99
84
100
cache. setObject ( value, forKey: key1)
85
101
cache. setObject ( value, forKey: key2)
102
+ cache. setObject ( value, forKey: key3)
86
103
87
104
XCTAssertEqual ( cache. object ( forKey: key2) , value, " should be equal to \( value) " )
105
+ XCTAssertEqual ( cache. object ( forKey: key3) , value, " should be equal to \( value) " )
88
106
XCTAssertNil ( cache. object ( forKey: key1) , " should be nil " )
89
-
107
+
90
108
}
91
109
}
0 commit comments