-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestNSIndexSet.swift
234 lines (189 loc) · 7.18 KB
/
TestNSIndexSet.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
// 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
//
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif
class TestNSIndexSet : XCTestCase {
static var allTests: [(String, (TestNSIndexSet) -> () throws -> Void)] {
return [
("test_BasicConstruction", test_BasicConstruction),
("test_enumeration", test_enumeration),
("test_sequenceType", test_sequenceType),
("test_removal", test_removal),
("test_addition", test_addition),
("test_setAlgebra", test_setAlgebra),
("test_copy", test_copy),
]
}
func test_BasicConstruction() {
let set = IndexSet()
let set2 = IndexSet(integersIn: 4..<11)
XCTAssertEqual(set.count, 0)
XCTAssertEqual(set.first, nil)
XCTAssertEqual(set.last, nil)
XCTAssertEqual(set2.count, 7)
XCTAssertEqual(set2.first, 4)
XCTAssertEqual(set2.last, 10)
let set3 = NSMutableIndexSet()
set3.add(2)
set3.add(5)
set3.add(in: NSMakeRange(4, 7))
set3.add(8)
XCTAssertEqual(set3.count, 8)
XCTAssertEqual(set3.firstIndex, 2)
XCTAssertEqual(set3.lastIndex, 10)
}
func test_copy() {
let range: NSRange = NSRange(location: 3, length: 4)
let array : [Int] = [1,2,3,4,5,6,7,8,9,10]
let indexSet = NSMutableIndexSet()
for index in array {
indexSet.add(index)
}
//Test copy operation of NSIndexSet case which is immutable
let selfIndexSet: NSIndexSet = NSIndexSet(indexesIn: range)
let selfIndexSetCopy = selfIndexSet.copy() as! NSIndexSet
XCTAssertTrue(selfIndexSetCopy === selfIndexSet)
XCTAssertTrue(selfIndexSetCopy.isEqual(to: selfIndexSet._bridgeToSwift()))
//Test copy operation of NSMutableIndexSet case
let mutableIndexSet: NSIndexSet = indexSet
indexSet.add(11)
let mutableIndexSetCopy = mutableIndexSet.copy() as! NSIndexSet
XCTAssertFalse(mutableIndexSetCopy === mutableIndexSet)
XCTAssertTrue(mutableIndexSetCopy.isEqual(to: mutableIndexSet._bridgeToSwift()))
}
func test_enumeration() {
let set = IndexSet(integersIn: 4..<11)
var result = Array<Int>()
for idx in set {
result.append(idx)
}
XCTAssertEqual(result, [4, 5, 6, 7, 8, 9, 10])
result = Array<Int>()
for idx in IndexSet() {
result.append(idx)
}
XCTAssertEqual(result, [])
let disjointSet = NSMutableIndexSet()
disjointSet.add(2)
disjointSet.add(5)
disjointSet.add(8)
disjointSet.add(in: NSMakeRange(7, 3))
disjointSet.add(11)
disjointSet.add(in: NSMakeRange(13, 2))
result = Array<Int>()
disjointSet.enumerate(options: []) { (idx, _) in
result.append(idx)
}
XCTAssertEqual(result, [2, 5, 7, 8, 9, 11, 13, 14])
}
func test_sequenceType() {
let set = IndexSet(integersIn: 4..<11)
var result = Array<Int>()
for idx in set {
result.append(idx)
}
XCTAssertEqual(result, [4, 5, 6, 7, 8, 9, 10])
}
func test_removal() {
let removalSet = NSMutableIndexSet(indexesIn: NSMakeRange(0, 10))
removalSet.remove(0)
removalSet.remove(in: NSMakeRange(9, 5))
removalSet.remove(in: NSMakeRange(2, 4))
XCTAssertEqual(removalSet.count, 4)
XCTAssertEqual(removalSet.firstIndex, 1)
XCTAssertEqual(removalSet.lastIndex, 8)
var additionSet = IndexSet()
additionSet.insert(1)
additionSet.insert(integersIn: 6..<9)
XCTAssertTrue(removalSet.isEqual(to: additionSet))
}
func test_addition() {
let testSetA = NSMutableIndexSet(index: 0)
testSetA.add(5)
testSetA.add(6)
testSetA.add(7)
testSetA.add(8)
testSetA.add(42)
let testInputA1 = [0,5,6,7,8,42]
var i = 0
if testInputA1.count == testSetA.count {
testSetA.enumerate(options: []) { (idx, _) in
XCTAssertEqual(idx, testInputA1[i])
i += 1
}
}
else {
XCTFail("IndexSet does not contain correct number of indexes")
}
let testInputA2 = [NSMakeRange(0, 1),NSMakeRange(5, 4),NSMakeRange(42, 1)]
i = 0
testSetA.enumerateRanges(options: []) { (range, _) in
let testRange = testInputA2[i]
XCTAssertEqual(range.location, testRange.location)
XCTAssertEqual(range.length, testRange.length)
i += 1
}
let testSetB = NSMutableIndexSet(indexesIn: NSMakeRange(0,5))
testSetB.add(in: NSMakeRange(42, 3))
testSetB.add(in: NSMakeRange(2, 2))
testSetB.add(in: NSMakeRange(18, 1))
let testInputB1 = [0,1,2,3,4,18,42,43,44]
i = 0
if testInputB1.count == testSetB.count {
testSetB.enumerate(options: []) { (idx, _) in
XCTAssertEqual(idx, testInputB1[i])
i += 1
}
}
else {
XCTFail("IndexSet does not contain correct number of indexes")
}
let testInputB2 = [NSMakeRange(0, 5),NSMakeRange(18, 1),NSMakeRange(42, 3)]
i = 0
testSetB.enumerateRanges(options: []) { (range, _) in
let testRange = testInputB2[i]
XCTAssertEqual(range.location, testRange.location)
XCTAssertEqual(range.length, testRange.length)
i += 1
}
}
func test_setAlgebra() {
var is1, is2, expected: IndexSet
do {
is1 = IndexSet(integersIn: 0..<5)
is2 = IndexSet(integersIn: 3..<10)
expected = IndexSet(integersIn: 0..<3)
expected.insert(integersIn: 5..<10)
XCTAssertTrue(expected == is1.symmetricDifference(is2))
XCTAssertTrue(expected == is2.symmetricDifference(is1))
}
do {
is1 = IndexSet([0, 2])
is2 = IndexSet([0, 1, 2])
XCTAssertTrue(IndexSet(integer: 1) == is1.symmetricDifference(is2))
}
do {
is1 = IndexSet(integersIn: 0..<5)
is2 = IndexSet(integersIn: 4..<10)
expected = IndexSet(integer: 4)
XCTAssertTrue(expected == is1.intersection(is2))
XCTAssertTrue(expected == is2.intersection(is1))
}
do {
is1 = IndexSet([0, 2])
is2 = IndexSet([0, 1, 2])
XCTAssertTrue(is1 == is1.intersection(is2))
}
}
}