forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestNSSet.swift
304 lines (261 loc) · 11.2 KB
/
TestNSSet.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// 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
//
class TestNSSet : XCTestCase {
func test_BasicConstruction() {
let set = NSSet()
let set2 = NSSet(array: ["foo", "bar"])
XCTAssertEqual(set.count, 0)
XCTAssertEqual(set2.count, 2)
let set3 = NSMutableSet(capacity: 3)
set3.add(1)
set3.add("foo")
let set4 = NSSet(set: set3)
XCTAssertEqual(set3, set4)
set3.remove(1)
XCTAssertNotEqual(set3, set4)
let set5 = NSMutableSet(set: set3)
XCTAssertEqual(set5, set3)
set5.add(2)
XCTAssertNotEqual(set5, set3)
}
func testInitWithSet() {
let genres: Set<AnyHashable> = ["Rock", "Classical", "Hip hop"]
let set1 = NSSet(set: genres)
let set2 = NSSet(set: genres, copyItems: false)
XCTAssertEqual(set1.count, 3)
XCTAssertEqual(set2.count, 3)
XCTAssertEqual(set1, set2)
let set3 = NSSet(set: genres, copyItems: true)
XCTAssertEqual(set3.count, 3)
XCTAssertEqual(set3, set2)
}
func test_enumeration() {
let set = NSSet(array: ["foo", "bar", "baz"])
let e = set.objectEnumerator()
var result = Set<String>()
result.insert((e.nextObject()! as! String))
result.insert((e.nextObject()! as! String))
result.insert((e.nextObject()! as! String))
XCTAssertEqual(result, Set(["foo", "bar", "baz"]))
let empty = NSSet().objectEnumerator()
XCTAssertNil(empty.nextObject())
XCTAssertNil(empty.nextObject())
}
func test_sequenceType() {
let set = NSSet(array: ["foo", "bar", "baz"])
var res = Set<String>()
for obj in set {
res.insert((obj as! String))
}
XCTAssertEqual(res, Set(["foo", "bar", "baz"]))
}
func test_setOperations() {
let set = NSMutableSet(array: ["foo", "bar"])
set.union(["bar", "baz"])
XCTAssertTrue(set.isEqual(to: ["foo", "bar", "baz"]))
}
func test_equality() {
let inputArray1 = ["this", "is", "a", "test", "of", "equality", "with", "strings"]
let inputArray2 = ["this", "is", "a", "test", "of", "equality", "with", "objects"]
let set1 = NSSet(array: inputArray1)
let set2 = NSSet(array: inputArray1)
let set3 = NSSet(array: inputArray2)
XCTAssertTrue(set1 == set2)
XCTAssertTrue(set1.isEqual(set2))
XCTAssertTrue(set1.isEqual(to: Set(inputArray1)))
XCTAssertEqual(set1.hash, set2.hash)
XCTAssertEqual(set1.hashValue, set2.hashValue)
XCTAssertFalse(set1 == set3)
XCTAssertFalse(set1.isEqual(set3))
XCTAssertFalse(set1.isEqual(to: Set(inputArray2)))
XCTAssertFalse(set1.isEqual(nil))
XCTAssertFalse(set1.isEqual(NSObject()))
}
func test_copying() {
let inputArray = ["this", "is", "a", "test", "of", "copy", "with", "strings"]
let set = NSSet(array: inputArray)
let setCopy1 = set.copy() as! NSSet
XCTAssertTrue(set === setCopy1)
let setMutableCopy = set.mutableCopy() as! NSMutableSet
let setCopy2 = setMutableCopy.copy() as! NSSet
XCTAssertTrue(type(of: setCopy2) === NSSet.self)
XCTAssertFalse(setMutableCopy === setCopy2)
for entry in setCopy2 {
XCTAssertTrue(NSArray(array: setMutableCopy.allObjects).index(of: entry) != NSNotFound)
}
}
func test_mutableCopying() {
let inputArray = ["this", "is", "a", "test", "of", "mutableCopy", "with", "strings"]
let set = NSSet(array: inputArray)
let setMutableCopy1 = set.mutableCopy() as! NSMutableSet
XCTAssertTrue(type(of: setMutableCopy1) === NSMutableSet.self)
XCTAssertFalse(set === setMutableCopy1)
for entry in setMutableCopy1 {
XCTAssertTrue(NSArray(array: set.allObjects).index(of: entry) != NSNotFound)
}
let setMutableCopy2 = setMutableCopy1.mutableCopy() as! NSMutableSet
XCTAssertTrue(type(of: setMutableCopy2) === NSMutableSet.self)
XCTAssertFalse(setMutableCopy2 === setMutableCopy1)
for entry in setMutableCopy2 {
XCTAssertTrue(NSArray(array: setMutableCopy1.allObjects).index(of: entry) != NSNotFound)
}
}
func test_CountedSetBasicConstruction() {
let v1 = "v1"
let v2 = "v2"
let v3asv1 = "v1"
let set = NSCountedSet()
let set2 = NSCountedSet(array: [v1, v1, v2,v3asv1])
let set3 = NSCountedSet(set: [v1, v1, v2,v3asv1])
let set4 = NSCountedSet(capacity: 4)
XCTAssertEqual(set.count, 0)
XCTAssertEqual(set2.count, 2)
XCTAssertEqual(set3.count, 2)
XCTAssertEqual(set4.count, 0)
}
func test_CountedSetObjectCount() {
let v1 = "v1"
let v2 = "v2"
let v3asv1 = "v1"
let set = NSCountedSet()
let set2 = NSCountedSet(array: [v1, v1, v2,v3asv1])
let set3 = NSCountedSet(set: [v1, v1, v2,v3asv1])
XCTAssertEqual(set.count(for: v1), 0)
XCTAssertEqual(set2.count(for: v1), 3)
XCTAssertEqual(set2.count(for: v2), 1)
XCTAssertEqual(set2.count(for: v3asv1), 3)
XCTAssertEqual(set3.count(for: v1), 1)
XCTAssertEqual(set3.count(for: v2), 1)
XCTAssertEqual(set3.count(for: v3asv1), 1)
}
func test_CountedSetAddObject() {
let v1 = "v1"
let v2 = "v2"
let v3asv1 = "v1"
let set = NSCountedSet(array: [v1, v1, v2])
XCTAssertEqual(set.count(for: v1), 2)
XCTAssertEqual(set.count(for: v2), 1)
set.add(v3asv1)
XCTAssertEqual(set.count(for: v1), 3)
set.addObjects(from: [v1,v2])
XCTAssertEqual(set.count(for: v1), 4)
XCTAssertEqual(set.count(for: v2), 2)
}
func test_CountedSetRemoveObject() {
let v1 = "v1"
let v2 = "v2"
let set = NSCountedSet(array: [v1, v1, v2])
XCTAssertEqual(set.count(for: v1), 2)
XCTAssertEqual(set.count(for: v2), 1)
set.remove(v2)
XCTAssertEqual(set.count(for: v2), 0)
XCTAssertEqual(set.count(for: v1), 2)
set.remove(v2)
XCTAssertEqual(set.count(for: v2), 0)
XCTAssertEqual(set.count(for: v1), 2)
set.removeAllObjects()
XCTAssertEqual(set.count(for: v2), 0)
XCTAssertEqual(set.count(for: v1), 0)
}
func test_CountedSetCopying() {
let inputArray = ["this", "is", "a", "test", "of", "copy", "with", "strings"]
let set = NSCountedSet(array: inputArray)
let setCopy = set.copy() as! NSCountedSet
XCTAssertFalse(set === setCopy)
let setMutableCopy = set.mutableCopy() as! NSCountedSet
XCTAssertFalse(set === setMutableCopy)
XCTAssertTrue(type(of: setCopy) === NSCountedSet.self)
XCTAssertTrue(type(of: setMutableCopy) === NSCountedSet.self)
for entry in setCopy {
XCTAssertTrue(NSArray(array: setMutableCopy.allObjects).index(of: entry) != NSNotFound)
}
}
func test_mutablesetWithDictionary() {
let aSet = NSMutableSet()
let dictionary = NSMutableDictionary()
let key = NSString(string: "Hello")
aSet.add(["world": "again"])
dictionary.setObject(aSet, forKey: key)
XCTAssertNotNil(dictionary.description) //should not crash
}
func test_Subsets() {
let set = NSSet(array: ["foo", "bar", "baz"])
let otherSet = NSSet(array: ["foo", "bar"])
let otherOtherSet = Set<AnyHashable>(["foo", "bar", "baz", "123"])
let newSet = Set<AnyHashable>(["foo", "bin"])
XCTAssert(otherSet.isSubset(of: set as! Set<AnyHashable>))
XCTAssertFalse(set.isSubset(of: otherSet as! Set<AnyHashable>))
XCTAssert(set.isSubset(of: otherOtherSet))
XCTAssert(otherSet.isSubset(of: otherOtherSet))
XCTAssertFalse(newSet.isSubset(of: otherSet as! Set<AnyHashable>))
}
func test_description() {
let array = NSArray(array: ["array_element1", "arrayElement2", "", "!@#$%^&*()", "a+b"])
let dictionary = NSDictionary(dictionary: ["key1": "value1", "key2": "value2"])
let innerSet = NSSet(array: [4444, 5555])
let set: NSSet = NSSet(array: [array, dictionary, innerSet, 1111, 2222, 3333])
let description = NSString(string: set.description)
XCTAssertTrue(description.hasPrefix("{("))
XCTAssertTrue(description.hasSuffix(")}"))
XCTAssertTrue(description.contains(" (\n \"array_element1\",\n arrayElement2,\n \"\",\n \"!@#$%^&*()\",\n \"a+b\"\n )"))
XCTAssertTrue(description.contains(" key1 = value1"))
XCTAssertTrue(description.contains(" key2 = value2"))
XCTAssertTrue(description.contains(" 4444"))
XCTAssertTrue(description.contains(" 5555"))
XCTAssertTrue(description.contains(" 1111"))
XCTAssertTrue(description.contains(" 2222"))
XCTAssertTrue(description.contains(" 3333"))
}
let setFixtures = [
Fixtures.setOfNumbers,
Fixtures.setEmpty,
]
let mutableSetFixtures = [
Fixtures.mutableSetOfNumbers,
Fixtures.mutableSetEmpty,
]
func test_codingRoundtrip() throws {
for fixture in setFixtures {
try fixture.assertValueRoundtripsInCoder()
}
for fixture in mutableSetFixtures {
try fixture.assertValueRoundtripsInCoder()
}
}
func test_loadedValuesMatch() throws {
for fixture in setFixtures {
try fixture.assertLoadedValuesMatch()
}
for fixture in mutableSetFixtures {
try fixture.assertLoadedValuesMatch()
}
}
static var allTests: [(String, (TestNSSet) -> () throws -> Void)] {
return [
("test_BasicConstruction", test_BasicConstruction),
("testInitWithSet", testInitWithSet),
("test_enumeration", test_enumeration),
("test_sequenceType", test_sequenceType),
("test_setOperations", test_setOperations),
("test_equality", test_equality),
("test_copying", test_copying),
("test_mutableCopying", test_mutableCopying),
("test_CountedSetBasicConstruction", test_CountedSetBasicConstruction),
("test_CountedSetObjectCount", test_CountedSetObjectCount),
("test_CountedSetAddObject", test_CountedSetAddObject),
("test_CountedSetRemoveObject", test_CountedSetRemoveObject),
("test_CountedSetCopying", test_CountedSetCopying),
("test_mutablesetWithDictionary", test_mutablesetWithDictionary),
("test_Subsets", test_Subsets),
("test_description", test_description),
("test_codingRoundtrip", test_codingRoundtrip),
("test_loadedValuesMatch", test_loadedValuesMatch),
]
}
}