@@ -349,10 +349,13 @@ public struct IndexOffsetByTest {
349
349
}
350
350
351
351
public let distanceFromToTests = [
352
+ // 0 - 1: no distance.
352
353
DistanceFromToTest(start: 0, end: 0),
353
354
DistanceFromToTest(start: 10, end: 10),
355
+ // 2 - 3: forward distance.
354
356
DistanceFromToTest(start: 10, end: 13),
355
357
DistanceFromToTest(start: 7, end: 10),
358
+ // 4 - 6: backward distance.
356
359
DistanceFromToTest(start: 12, end: 4),
357
360
DistanceFromToTest(start: 8, end: 2),
358
361
DistanceFromToTest(start: 19, end: 0)
@@ -1594,26 +1597,25 @@ extension TestSuite {
1594
1597
}
1595
1598
% end
1596
1599
1597
- self.test("\(testNamePrefix)/distance(from:to:)/semantics") {
1598
- let c = toCollection(0..<20)
1599
- for test in distanceFromToTests {
1600
- let d = c.distance(
1601
- from: c.nthIndex(test.startOffset), to: c.nthIndex(test.endOffset))
1602
-
1603
- let backwards = (test.startOffset < test.endOffset)
1600
+ self.test("\(testNamePrefix)/distance(from:to:)/semantics")
1601
+ .forEach(in: distanceFromToTests) { (test) in
1602
+ let c = toCollection(0..<20)
1603
+ let backwards = (test.startOffset > test.endOffset)
1604
1604
if backwards && !collectionIsBidirectional {
1605
1605
expectCrashLater()
1606
1606
}
1607
+
1608
+ let d = c.distance(
1609
+ from: c.nthIndex(test.startOffset), to: c.nthIndex(test.endOffset))
1607
1610
expectEqual(
1608
1611
numericCast(test.expectedDistance),
1609
1612
d, stackTrace: SourceLocStack().with(test.loc))
1610
- }
1611
1613
}
1612
1614
1613
- self.test("\(testNamePrefix)/index(_:offsetBy: n)/semantics") {
1614
- for test in indexOffsetByTests.filter(
1615
+ self.test("\(testNamePrefix)/index(_:offsetBy: n)/semantics")
1616
+ .forEach(in: indexOffsetByTests.filter(
1615
1617
{$0.limit == nil && $0.distance >= 0}
1616
- ) {
1618
+ )) { (test) in
1617
1619
let max = 10
1618
1620
let c = toCollection(0..<max)
1619
1621
@@ -1630,13 +1632,12 @@ extension TestSuite {
1630
1632
// contains exactly index offsets.
1631
1633
expectEqual(test.expectedOffset!, extractValue(c[new]).value,
1632
1634
stackTrace: SourceLocStack().with(test.loc))
1633
- }
1634
1635
}
1635
1636
1636
- self.test("\(testNamePrefix)/formIndex(_:offsetBy: n)/semantics") {
1637
- for test in indexOffsetByTests.filter(
1637
+ self.test("\(testNamePrefix)/formIndex(_:offsetBy: n)/semantics")
1638
+ .forEach(in: indexOffsetByTests.filter(
1638
1639
{$0.limit == nil && $0.distance >= 0}
1639
- ) {
1640
+ )) { (test) in
1640
1641
let max = 10
1641
1642
let c = toCollection(0..<max)
1642
1643
@@ -1648,14 +1649,13 @@ extension TestSuite {
1648
1649
c.formIndex(&new, offsetBy: numericCast(test.distance))
1649
1650
expectEqual(test.expectedOffset!, extractValue(c[new]).value,
1650
1651
stackTrace: SourceLocStack().with(test.loc))
1651
- }
1652
1652
}
1653
1653
1654
1654
% for function_name in ['index', 'formIndex']:
1655
- self.test("\(testNamePrefix)/${function_name}(_:offsetBy: -n)/semantics") {
1656
- for test in indexOffsetByTests.filter(
1655
+ self.test("\(testNamePrefix)/${function_name}(_:offsetBy: -n)/semantics")
1656
+ .forEach(in: indexOffsetByTests.filter(
1657
1657
{$0.limit == nil && $0.distance < 0}
1658
- ) {
1658
+ )) { (test) in
1659
1659
let c = toCollection(0..<20)
1660
1660
% if function_name is 'index':
1661
1661
let start = c.nthIndex(test.startOffset)
@@ -1673,7 +1673,6 @@ extension TestSuite {
1673
1673
% end
1674
1674
expectEqual(test.expectedOffset!, extractValue(c[new]).value,
1675
1675
stackTrace: SourceLocStack().with(test.loc))
1676
- }
1677
1676
}
1678
1677
% end
1679
1678
@@ -1716,10 +1715,10 @@ extension TestSuite {
1716
1715
}
1717
1716
}
1718
1717
1719
- self.test("\(testNamePrefix)/index(_:offsetBy: -n, limitedBy:)/semantics") {
1720
- for test in indexOffsetByTests.filter(
1718
+ self.test("\(testNamePrefix)/index(_:offsetBy: -n, limitedBy:)/semantics")
1719
+ .forEach(in: indexOffsetByTests.filter(
1721
1720
{$0.limit != nil && $0.distance < 0}
1722
- ) {
1721
+ )) { (test) in
1723
1722
let c = toCollection(0..<20)
1724
1723
let limit = c.nthIndex(test.limit.unsafelyUnwrapped)
1725
1724
if collectionIsBidirectional {
@@ -1740,13 +1739,12 @@ extension TestSuite {
1740
1739
offsetBy: numericCast(test.distance),
1741
1740
limitedBy: limit)
1742
1741
}
1743
- }
1744
1742
}
1745
1743
1746
- self.test("\(testNamePrefix)/formIndex(_:offsetBy: -n, limitedBy:)/semantics") {
1747
- for test in indexOffsetByTests.filter(
1748
- {$0.limit != nil && $0.distance < 0}
1749
- ) {
1744
+ self.test("\(testNamePrefix)/formIndex(_:offsetBy: -n, limitedBy:)/semantics")
1745
+ .forEach(in: indexOffsetByTests.filter(
1746
+ {$0.limit != nil && $0.distance < 0}
1747
+ )) { (test) in
1750
1748
let c = toCollection(0..<20)
1751
1749
let limit = c.nthIndex(test.limit.unsafelyUnwrapped)
1752
1750
var new = c.nthIndex(test.startOffset)
@@ -1767,7 +1765,6 @@ extension TestSuite {
1767
1765
expectCrashLater()
1768
1766
_ = c.formIndex(&new, offsetBy: numericCast(test.distance), limitedBy: limit)
1769
1767
}
1770
- }
1771
1768
}
1772
1769
1773
1770
}
0 commit comments