[stdlib][swift-3-indexing-model] fixing Index tests

This commit is contained in:
Max Moiseev
2016-03-23 16:05:35 -07:00
parent 8d9e62b274
commit d721a03d90
4 changed files with 107 additions and 135 deletions

View File

@@ -27,6 +27,12 @@ import SwiftPrivate
import ObjectiveC
#endif
extension Collection {
func indexAt(offset offset: Int) -> Index {
return self.advance(self.startIndex, by: numericCast(offset))
}
}
struct DistanceToTest {
let startIndex: Int
let endIndex: Int
@@ -40,11 +46,11 @@ struct DistanceToTest {
self.startIndex = startIndex
self.endIndex = endIndex
self.expectedDistance = expectedDistance
self.loc = SourceLoc(file, line, comment: "distance(to:) test data")
self.loc = SourceLoc(file, line, comment: "distance(from:to:) test data")
}
}
struct AdvancedByTest {
struct AvanceByTest {
let startIndex: Int
let distance: Int
let limit: Int?
@@ -59,7 +65,7 @@ struct AdvancedByTest {
self.distance = distance
self.expectedIndex = expectedIndex
self.limit = limit
self.loc = SourceLoc(file, line, comment: "advanced(by:) test data")
self.loc = SourceLoc(file, line, comment: "advance(_:by:) test data")
}
}
@@ -87,53 +93,53 @@ let distanceToTests = [
]
let advancedByTests = [
AdvancedByTest(
AvanceByTest(
startIndex: 0,
distance: 0,
expectedIndex: 0
),
AdvancedByTest(
AvanceByTest(
startIndex: 0,
distance: -1,
expectedIndex: -1
),
AdvancedByTest(
AvanceByTest(
startIndex: 0,
distance: 0,
expectedIndex: 0,
limit: 0
),
AdvancedByTest(
AvanceByTest(
startIndex: 0,
distance: 0,
expectedIndex: 0,
limit: 10
),
AdvancedByTest(
AvanceByTest(
startIndex: 0,
distance: 10,
expectedIndex: 0,
limit: 0
),
AdvancedByTest(
AvanceByTest(
startIndex: 0,
distance: -10,
expectedIndex: 0,
limit: 0
),
AdvancedByTest(
AvanceByTest(
startIndex: 0,
distance: 10,
expectedIndex: 10,
limit: 10
),
AdvancedByTest(
AvanceByTest(
startIndex: 0,
distance: 20,
expectedIndex: 10,
limit: 10
),
AdvancedByTest(
AvanceByTest(
startIndex: 10,
distance: -20,
expectedIndex: 0,
@@ -143,120 +149,90 @@ let advancedByTests = [
var Index = TestSuite("Index")
% from gyb_stdlib_support import collectionForTraversal
% # RandomAccess does not add any new behaviors to Bidirectional
% # so we are left with just 2 traversals for the following tests
% for Traversal in ['Forward', 'Bidirectional']:
% TraversalCollection = collectionForTraversal(Traversal)
Index.test("${TraversalCollection}/distance(to:)/dispatch") {
let c = ${TraversalCollection}Log.dispatchTester(Array(0..<10))
_ = c.distance(from: 0, to: 10)
expectCustomizable(c, c.log.distance)
}
Index.test("${TraversalCollection}/advance(_:by:)/dispatch") {
let c = ${TraversalCollection}Log.dispatchTester(Array(0..<10))
_ = c.advance(c.startIndex, by: 10)
expectCustomizable(c, c.log.advance)
}
Index.test("${TraversalCollection}/advance(_:by:limit:)/dispatch") {
let c = ${TraversalCollection}Log.dispatchTester(Array(0..<10))
_ = c.advance(c.startIndex, by: 10, limit: 5)
expectCustomizable(c, c.log.advanceLimit)
}
% end
% for Traversal in ['Forward', 'Bidirectional', 'RandomAccess']:
Index.test("${Traversal}Index/distance(to:)/dispatch") {
var start = ${Traversal}IndexLog.dispatchTester(0)
var end = ${Traversal}IndexLog.dispatchTester(10)
_ = start.distance(to: end)
expectCustomizable(start, start.log.distanceTo)
}
Index.test("${Traversal}Index/advanced(by:)/dispatch") {
var start = ${Traversal}IndexLog.dispatchTester(0)
_ = start.advanced(by: 10)
expectCustomizable(start, start.log.advancedBy)
}
Index.test("${Traversal}Index/advanced(by: n, limit:)/dispatch") {
let start = ${Traversal}IndexLog.dispatchTester(0)
let limit = ${Traversal}IndexLog.dispatchTester(5)
_ = start.advanced(by: 10, limit: limit)
expectCustomizable(start, start.log.advancedByWithLimit)
}
% CollectionPrefix = collectionForTraversal(Traversal).replace('Collection', '')
% for Base in ['Minimal', 'Defaulted']:
% Kind = '{}{}'.format(Base, Traversal)
% Kind = '{}{}'.format(Base, CollectionPrefix)
Index.test("${Kind}Index/distance(to:)/semantics") {
Index.test("${Kind}Collection/distance(from:to:)/semantics") {
let c = ${Kind}Collection(elements: Array(0..<20))
for test in distanceToTests {
let start = ${Kind}Index(
position: test.startIndex,
startIndex: -20,
endIndex: 20)
let end = ${Kind}Index(
position: test.endIndex,
startIndex: -20,
endIndex: 20)
let d = start.distance(to: end)
let d = c.distance(
from: c.indexAt(offset: test.startIndex),
to: c.indexAt(offset: test.endIndex))
expectEqual(test.expectedDistance, d,
stackTrace: SourceLocStack().with(test.loc))
}
}
Index.test("${Kind}Index/advanced(by: n)/semantics") {
for test in advancedByTests.filter({$0.limit == nil}) {
let start = ${Kind}Index(
position: test.startIndex,
startIndex: -20,
endIndex: 20)
let expected = ${Kind}Index(
position: test.expectedIndex,
startIndex: -20,
endIndex: 20)
Index.test("${Kind}Collection/advance(_:by: n)/semantics") {
for test in advancedByTests.filter({$0.limit == nil && $0.distance >= 0}) {
let c = ${Kind}Collection(elements: Array(0..<10))
% if Traversal == 'Forward':
if test.distance < 0 {
// Negative distance trap tests are handled in
// advanced(by:-n)/semantics
continue
}
% end
let new = start.advanced(by: test.distance)
expectEqual(expected, new,
let new = c.advance(c.indexAt(offset: test.startIndex), by: test.distance)
// Since the `indexAt(offset:)` method performs the same operation
// (i.e. adavances `c.startIndex` by `test.distance`, it would be
// silly to compare index values. Luckily the underlying collection
// contains exactly index offsets.
expectEqual(test.expectedIndex, c[new],
stackTrace: SourceLocStack().with(test.loc))
}
}
% if Traversal == 'Forward':
Index.test("${Kind}Index/advanced(by: -n)/semantics") {
Index.test("${Kind}Collection/advance(_:by: -n)/semantics") {
for test in advancedByTests.filter({$0.limit == nil && $0.distance < 0}) {
let start = ${Kind}Index(
position: test.startIndex,
startIndex: -20,
endIndex: 20)
let expected = ${Kind}Index(
position: test.expectedIndex,
startIndex: -20,
endIndex: 20)
let c = ${Kind}Collection(elements: Array(0..<10))
expectCrashLater()
let new = start.advanced(by: test.distance)
_ = c.advance(c.indexAt(offset: test.startIndex), by: test.distance)
}
}
% end
% if Traversal == 'Forward':
Index.test("${Kind}Index/advanced(by: -n, limit:)/semantics") {
Index.test("${Kind}Collection/advance(by: -n, limit:)/semantics") {
for test in advancedByTests.filter({$0.limit != nil && $0.distance < 0}) {
let start = ${Kind}Index(
position: test.startIndex,
startIndex: -20,
endIndex: 20)
let expected = ${Kind}Index(
position: test.expectedIndex,
startIndex: -20,
endIndex: 20)
let c = ${Kind}Collection(elements: Array(0..<10))
let limit = c.indexAt(offset: test.limit.unsafelyUnwrapped)
expectCrashLater()
let new = start.advanced(by: test.distance)
_ = c.advance(c.indexAt(offset: test.startIndex),
by: test.distance, limit: limit)
}
}
% end
Index.test("${Kind}Index/advanced(by: n, limit:)/semantics") {
for test in advancedByTests.filter({$0.limit != nil}) {
let startIndex = test.startIndex
let endIndex = test.startIndex + test.distance
let start = ${Kind}Index(
position: test.startIndex,
startIndex: -20,
endIndex: 20)
let limit = ${Kind}Index(
position: test.limit!,
startIndex: -20,
endIndex: 20)
Index.test("${Kind}Collection/advance(by: n, limit:)/semantics") {
for test in advancedByTests.filter({$0.limit != nil && $0.distance >= 0}) {
let c = ${Kind}Collection(elements: Array(0..<10))
let limit = c.indexAt(offset: test.limit.unsafelyUnwrapped)
% if Traversal == 'Forward':
@@ -266,43 +242,46 @@ Index.test("${Kind}Index/advanced(by: n, limit:)/semantics") {
% end
let new = start.advanced(by: test.distance, limit: limit)
let expected = ${Kind}Index(
position: test.expectedIndex,
startIndex: -20,
endIndex: 20)
expectEqual(expected, new,
let new = c.advance(c.indexAt(offset: test.startIndex),
by: test.distance, limit: limit)
expectEqual(c.indexAt(offset: test.expectedIndex), new,
stackTrace: SourceLocStack().with(test.loc))
}
}
// Check that a random access index doesn't call into O(n) predecessor calls
// when it has a more efficient implementation.
// Check that a random access collection doesn't call into O(n) predecessor
// calls when it has a more efficient implementation.
% if Traversal == 'RandomAccess' and Base == 'Defaulted':
Index.test(
"${Kind}Index/advanced(by: n)/avoidsSuccessorAndPredecessor/dispatch"
"${Kind}Collection/advance(_:by:)/avoidsSuccessorAndPredecessor/dispatch"
) {
for test in advancedByTests.filter({$0.limit == nil}) {
let i = ${Kind}Index(test.startIndex)
let result = i.advanced(by: test.distance)
expectEqual(test.expectedIndex, result.base)
expectEqual(0, ${Kind}Index.timesSuccessorCalled.value)
expectEqual(0, ${Kind}Index.timesPredecessorCalled.value)
for test in advancedByTests.filter({$0.limit == nil && $0.distance >= 0}) {
let c = ${Kind}Collection(Array(0..<10))
let i = c.indexAt(offset: test.startIndex)
let result = c.advance(i, by: test.distance)
expectEqual(0, c.timesSuccessorCalled.value)
expectEqual(0, c.timesPredecessorCalled.value)
}
}
Index.test(
"${Kind}Index/advanced(by: n, limit:)/avoidsSuccessorAndPredecessor/dispatch"
) {
for test in advancedByTests.filter({$0.limit != nil}) {
let i = ${Kind}Index(test.startIndex)
let result = i.advanced(by: test.distance, limit: ${Kind}Index(test.limit!))
expectEqual(test.expectedIndex, result.base)
expectEqual(0, ${Kind}Index.timesSuccessorCalled.value)
expectEqual(0, ${Kind}Index.timesPredecessorCalled.value)
"${Kind}Index/advance(_:by:limit:)/avoidsSuccessorAndPredecessor/dispatch"
).xfail(.custom({ true }, reason: "Implementation missing in new indexing model"))
.code {
for test in advancedByTests.filter({$0.limit != nil && $0.distance >= 0}) {
let c = ${Kind}Collection(Array(0..<10))
let i = c.indexAt(offset: test.startIndex)
let limit = c.indexAt(offset: test.limit.unsafelyUnwrapped)
let result = c.advance(i, by: test.distance, limit: limit)
expectEqual(0, c.timesSuccessorCalled.value)
expectEqual(0, c.timesPredecessorCalled.value)
}
}
% end
% end
% end