Skip to content

Commit 206419e

Browse files
zayassparkera
authored andcommitted
Fix IndexSet/NSIndexSet range merging (#1437)
1 parent 1266b8e commit 206419e

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

Foundation/NSIndexSet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ open class NSMutableIndexSet : NSIndexSet {
589589
let nextRange = _ranges[rangeIndex + 1]
590590
let currentEnd = currentRange.location + currentRange.length
591591
let nextEnd = nextRange.location + nextRange.length
592-
if nextEnd >= nextRange.location {
592+
if currentEnd >= nextRange.location {
593593
// overlaps
594594
if currentEnd < nextEnd {
595595
// next range extends beyond current range

TestFoundation/TestIndexSet.swift

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class TestIndexSet : XCTestCase {
5757
("test_AnyHashableContainingIndexSet", test_AnyHashableContainingIndexSet),
5858
("test_AnyHashableCreatedFromNSIndexSet", test_AnyHashableCreatedFromNSIndexSet),
5959
("test_unconditionallyBridgeFromObjectiveC", test_unconditionallyBridgeFromObjectiveC),
60+
("testInsertNonOverlapping", testInsertNonOverlapping),
61+
("testInsertOverlapping", testInsertOverlapping),
62+
("testInsertOverlappingExtend", testInsertOverlappingExtend),
63+
("testInsertOverlappingMultiple", testInsertOverlappingMultiple),
64+
("testRemoveNonOverlapping", testRemoveNonOverlapping),
65+
("testRemoveOverlapping", testRemoveOverlapping),
66+
("testRemoveSplitting", testRemoveSplitting),
6067
]
6168
}
6269

@@ -1082,4 +1089,120 @@ class TestIndexSet : XCTestCase {
10821089
func test_unconditionallyBridgeFromObjectiveC() {
10831090
XCTAssertEqual(IndexSet(), IndexSet._unconditionallyBridgeFromObjectiveC(nil))
10841091
}
1092+
1093+
func testInsertNonOverlapping() {
1094+
var tested = IndexSet()
1095+
tested.insert(integersIn: 1..<2)
1096+
tested.insert(integersIn: 100..<200)
1097+
tested.insert(integersIn: 1000..<2000)
1098+
1099+
tested.insert(200)
1100+
1101+
var expected = IndexSet()
1102+
expected.insert(integersIn: 1..<2)
1103+
expected.insert(integersIn: 100..<201)
1104+
expected.insert(integersIn: 1000..<2000)
1105+
1106+
XCTAssertEqual(tested, expected)
1107+
}
1108+
1109+
func testInsertOverlapping() {
1110+
var tested = IndexSet()
1111+
tested.insert(integersIn: 1..<2)
1112+
tested.insert(integersIn: 100..<200)
1113+
tested.insert(integersIn: 1000..<2000)
1114+
tested.insert(integersIn: 10000..<20000)
1115+
1116+
tested.insert(integersIn: 150..<1500)
1117+
1118+
var expected = IndexSet()
1119+
expected.insert(integersIn: 1..<2)
1120+
expected.insert(integersIn: 100..<2000)
1121+
expected.insert(integersIn: 10000..<20000)
1122+
1123+
XCTAssertEqual(tested, expected)
1124+
}
1125+
1126+
func testInsertOverlappingExtend() {
1127+
var tested = IndexSet()
1128+
tested.insert(integersIn: 1..<2)
1129+
tested.insert(integersIn: 100..<200)
1130+
tested.insert(integersIn: 1000..<2000)
1131+
1132+
tested.insert(integersIn: 50..<500)
1133+
1134+
var expected = IndexSet()
1135+
expected.insert(integersIn: 1..<2)
1136+
expected.insert(integersIn: 50..<500)
1137+
expected.insert(integersIn: 1000..<2000)
1138+
1139+
XCTAssertEqual(tested, expected)
1140+
}
1141+
1142+
func testInsertOverlappingMultiple() {
1143+
var tested = IndexSet()
1144+
tested.insert(integersIn: 1..<2)
1145+
tested.insert(integersIn: 100..<200)
1146+
tested.insert(integersIn: 1000..<2000)
1147+
tested.insert(integersIn: 10000..<20000)
1148+
1149+
tested.insert(integersIn: 150..<3000)
1150+
1151+
var expected = IndexSet()
1152+
expected.insert(integersIn: 1..<2)
1153+
expected.insert(integersIn: 100..<3000)
1154+
expected.insert(integersIn: 10000..<20000)
1155+
1156+
XCTAssertEqual(tested, expected)
1157+
}
1158+
1159+
func testRemoveNonOverlapping() {
1160+
var tested = IndexSet()
1161+
tested.insert(integersIn: 1..<2)
1162+
tested.insert(integersIn: 100..<200)
1163+
tested.insert(integersIn: 1000..<2000)
1164+
1165+
tested.remove(199)
1166+
1167+
var expected = IndexSet()
1168+
expected.insert(integersIn: 1..<2)
1169+
expected.insert(integersIn: 100..<199)
1170+
expected.insert(integersIn: 1000..<2000)
1171+
1172+
XCTAssertEqual(tested, expected)
1173+
}
1174+
1175+
func testRemoveOverlapping() {
1176+
var tested = IndexSet()
1177+
tested.insert(integersIn: 1..<2)
1178+
tested.insert(integersIn: 100..<200)
1179+
tested.insert(integersIn: 1000..<2000)
1180+
1181+
tested.remove(integersIn: 150..<1500)
1182+
1183+
var expected = IndexSet()
1184+
expected.insert(integersIn: 1..<2)
1185+
expected.insert(integersIn: 100..<150)
1186+
expected.insert(integersIn: 1500..<2000)
1187+
1188+
XCTAssertEqual(tested, expected)
1189+
}
1190+
1191+
func testRemoveSplitting() {
1192+
var tested = IndexSet()
1193+
tested.insert(integersIn: 1..<2)
1194+
tested.insert(integersIn: 100..<200)
1195+
tested.insert(integersIn: 1000..<2000)
1196+
1197+
tested.remove(integersIn: 150..<160)
1198+
1199+
var expected = IndexSet()
1200+
expected.insert(integersIn: 1..<2)
1201+
expected.insert(integersIn: 100..<150)
1202+
expected.insert(integersIn: 160..<200)
1203+
expected.insert(integersIn: 1000..<2000)
1204+
1205+
XCTAssertEqual(tested, expected)
1206+
}
1207+
10851208
}

0 commit comments

Comments
 (0)