- Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestNSCache.swift
199 lines (151 loc) · 7.7 KB
/
TestNSCache.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
classTestNSCache:XCTestCase{
func test_setWithUnmutableKeys(){
letcache=NSCache<NSString,NSString>()
varkey1=NSString(string:"key")
varkey2=NSString(string:"key")
varvalue=NSString(string:"value")
cache.setObject(value, forKey: key1)
XCTAssertEqual(cache.object(forKey: key1), value,"should be equal to \(value) when using first key")
XCTAssertEqual(cache.object(forKey: key2), value,"should be equal to \(value) when using second key")
value =NSString(string:"value1")
cache.setObject(value, forKey: key2)
XCTAssertEqual(cache.object(forKey: key1), value,"should be equal to \(value) when using first key")
XCTAssertEqual(cache.object(forKey: key2), value,"should be equal to \(value) when using second key")
key1 ="kkey"
key2 ="kkey"
letvalue1=NSString(string:"value1")
letvalue2=NSString(string:"value1")
cache.setObject(value1, forKey: key1)
XCTAssertEqual(cache.object(forKey: key1), value1,"should be equal to \(value1) when using first key")
XCTAssertEqual(cache.object(forKey: key2), value1,"should be equal to \(value1) when using second key")
XCTAssertEqual(cache.object(forKey: key1), value2,"should be equal to \(value1) when using first key")
XCTAssertEqual(cache.object(forKey: key2), value2,"should be equal to \(value1) when using second key")
}
func test_setWithMutableKeys(){
letcache=NSCache<NSMutableString,NSString>()
letkey1=NSMutableString(string:"key")
letkey2=NSMutableString(string:"key")
letvalue=NSString(string:"value")
cache.setObject(value, forKey: key1)
XCTAssertEqual(cache.object(forKey: key1), value,"should be equal to \(value) when using first key")
XCTAssertEqual(cache.object(forKey: key2), value,"should be equal to \(value) when using second key")
key1.append("1")
// Mutating the key probably changes the hash value, which often makes
// the value inaccessible by sorting the key into a different bucket.
// On the other hand, the bucket may remain the same by coincidence.
// Therefore, `cache.object(forKey: key1)` may or may not be nil at
// this point -- no useful check can be made.
// The object can definitely not be reached via the original key,
// though.
XCTAssertNil(cache.object(forKey: key2),"should be nil")
// Restoring key1 to the original string will make the value
// accessible again.
key1.setString("key")
XCTAssertEqual(cache.object(forKey: key1), value,"should be equal to \(value) when using first key")
XCTAssertEqual(cache.object(forKey: key2), value,"should be equal to \(value) when using second key")
}
func test_costLimit(){
letcache=NSCache<NSString,NSString>()
cache.totalCostLimit =10
cache.setObject("object0", forKey:"0", cost:4)
cache.setObject("object2", forKey:"2", cost:5)
cache.setObject("object1", forKey:"1", cost:5)
XCTAssertNil(cache.object(forKey:"0"),"should be nil")
XCTAssertEqual(cache.object(forKey:"2"),"object2","should be equal to 'object2'")
XCTAssertEqual(cache.object(forKey:"1"),"object1","should be equal to 'object1'")
}
func test_countLimit(){
letcache=NSCache<NSString,NSString>()
cache.countLimit =2
letkey1=NSString(string:"key1")
letkey2=NSString(string:"key2")
letkey3=NSString(string:"key3")
letvalue=NSString(string:"value")
cache.setObject(value, forKey: key1, cost:1)
cache.setObject(value, forKey: key2, cost:2)
cache.setObject(value, forKey: key3, cost:3)
XCTAssertEqual(cache.object(forKey: key2), value,"should be equal to \(value)")
XCTAssertEqual(cache.object(forKey: key3), value,"should be equal to \(value)")
XCTAssertNil(cache.object(forKey: key1),"should be nil")
}
classTestHashableCacheKey:Hashable{
letstring:String
init(string:String){
self.string = string
}
func hash(into hasher:inoutHasher){
hasher.combine(string)
}
staticfunc==(lhs:TestHashableCacheKey,
rhs:TestHashableCacheKey)->Bool{
return lhs.string == rhs.string
}
}
// Test when NSCacheKey.value is AnyHashable
func test_hashableKey(){
letcache=NSCache<TestHashableCacheKey,NSString>()
cache.countLimit =2
letkey1=TestHashableCacheKey(string:"key1")
letkey2=TestHashableCacheKey(string:"key2")
letkey3=TestHashableCacheKey(string:"key3")
letvalue=NSString(string:"value")
cache.setObject(value, forKey: key1, cost:1)
cache.setObject(value, forKey: key2, cost:2)
cache.setObject(value, forKey: key3, cost:3)
XCTAssertEqual(cache.object(forKey: key2), value,"should be equal to \(value)")
XCTAssertEqual(cache.object(forKey: key3), value,"should be equal to \(value)")
XCTAssertNil(cache.object(forKey: key1),"should be nil")
}
classTestCacheKey{
letstring:String
init(string:String){
self.string = string
}
}
// Test when NSCacheKey.value is neither NSObject or AnyHashable
func test_nonHashableKey(){
letcache=NSCache<TestCacheKey,NSString>()
cache.countLimit =2
letkey1=TestCacheKey(string:"key1")
letkey2=TestCacheKey(string:"key2")
letkey3=TestCacheKey(string:"key3")
letvalue=NSString(string:"value")
cache.setObject(value, forKey: key1, cost:1)
cache.setObject(value, forKey: key2, cost:2)
cache.setObject(value, forKey: key3, cost:3)
XCTAssertEqual(cache.object(forKey: key2), value,"should be equal to \(value)")
XCTAssertEqual(cache.object(forKey: key3), value,"should be equal to \(value)")
XCTAssertNil(cache.object(forKey: key1),"should be nil")
}
func test_objectCorrectlyReleased(){
letcache=NSCache<NSString,AnyObject>()
cache.totalCostLimit =10
varobject1=NSObject()
weak varweakObject1:NSObject?= object1
varobject2=NSObject()
weak varweakObject2:NSObject?= object2
varobject3=NSObject()
weak varweakObject3:NSObject?= object3
letobject4=NSObject()
letobject5=NSObject()
cache.setObject(object1, forKey:"key1", cost:1)
cache.setObject(object2, forKey:"key2", cost:2)
cache.setObject(object3, forKey:"key3", cost:3)
cache.setObject(object4, forKey:"key4", cost:4)
cache.setObject(object5, forKey:"key5", cost:5)
object1 =NSObject()
object2 =NSObject()
object3 =NSObject()
XCTAssertNil(weakObject1,"removed cached object not released")
XCTAssertNil(weakObject2,"removed cached object not released")
XCTAssertNil(weakObject3,"removed cached object not released")
}
}