Merge branch 'master' into new-integer-protocols

This commit is contained in:
Max Moiseev
2017-04-06 10:22:37 -07:00
108 changed files with 2536 additions and 1485 deletions

View File

@@ -269,9 +269,57 @@ public func checkAdvancesAndDistances<Instances, Distances, BaseCollection>(
% ('Expected: Collection', 'Expected.Iterator.Element', 'Expected'),
% ('Element' , 'Element' , 'Array<Element>')]:
// Top-level check for Collection instances. Alias for checkForwardCollection.
// Checks all slices: O(n^2).
public func checkCollection<${genericParam}, C : Collection>(
_ expected: ${Expected},
_ collection: C,
${TRACE},
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (${Element}, ${Element}) -> Bool
) where C.Iterator.Element == ${Element},
// FIXME(ABI) (Associated Types with where clauses): these constraints should be applied to
// associated types of Collection.
C.Indices.Iterator.Element == C.Index,
C.SubSequence : Collection,
C.SubSequence.Iterator.Element == ${Element},
C.SubSequence.Indices.Iterator.Element == C.Index,
C.SubSequence.Index == C.Index {
checkForwardCollection(expected, collection, message(),
stackTrace: stackTrace, showFrame: showFrame, file: file, line: line,
resiliencyChecks: resiliencyChecks,
sameValue: sameValue)
}
% for Traversal in TRAVERSALS:
% TraversalCollection = collectionForTraversal(Traversal)
// Calls check${Traversal}Collection with default `sameValue`.
public func check${Traversal}Collection<
${genericParam}, C : ${TraversalCollection}
>(
_ expected: ${Expected}, _ collection: C,
${TRACE},
resiliencyChecks: CollectionMisuseResiliencyChecks = .all
) where
C.Iterator.Element == ${Element},
C.Indices.Iterator.Element == C.Index,
C.SubSequence : ${TraversalCollection},
C.SubSequence.Iterator.Element == ${Element},
C.SubSequence.Indices.Iterator.Element == C.Index,
C.SubSequence.Index == C.Index,
${Element} : Equatable {
check${Traversal}Collection(
expected, collection, ${trace},
resiliencyChecks: resiliencyChecks) { $0 == $1 }
}
// Top-Level check for all ${TraversalCollection} semantics on a single
// instance. This constrains SubSequence associated types in order to check
// slice semantics.
// Checks all slices: O(n^2).
public func check${Traversal}Collection<
${genericParam}, C : ${TraversalCollection}
>(
@@ -281,6 +329,44 @@ public func check${Traversal}Collection<
sameValue: (${Element}, ${Element}) -> Bool
) where
C.Iterator.Element == ${Element},
// FIXME(ABI) (Associated Types with where clauses): these constraints should be applied to
// associated types of Collection.
C.Indices.Iterator.Element == C.Index,
C.SubSequence : ${TraversalCollection},
C.SubSequence.Iterator.Element == ${Element},
C.SubSequence.Index == C.Index,
C.SubSequence.Indices.Iterator.Element == C.Index {
checkOneLevelOf${Traversal}Collection(expected, collection, ${trace},
resiliencyChecks: resiliencyChecks, sameValue: sameValue)
// Avoid validation of all possible (n^2) slices on large collection.
// Test cases should call checkOneLevelOf${Traversal}Collection instead.
expectLT(expected.count, 30)
_checkSliceableWith${Traversal}Index(expected, collection, ${trace},
resiliencyChecks: resiliencyChecks, sameValue: sameValue)
}
// Helper for check${Traversal}Collection. Check that instance of `C`,
// `collection`, upholds the semantics of `${TraversalCollection}`,
// non-recursively. This does not check subsequences. It may be called for each
// subsequence without combinatorial explosion. Also, since recursive protocol
// contraints are not supported, our second level of checks cannot depend on the
// associated type properties of SubSequence.
//
// Checks all slices: O(n^2).
public func checkOneLevelOf${Traversal}Collection<
${genericParam}, C : ${TraversalCollection}
>(
_ expected: ${Expected}, _ collection: C,
${TRACE},
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (${Element}, ${Element}) -> Bool
) where
C.Iterator.Element == ${Element},
// FIXME(ABI) (Associated Types with where clauses): these constraints should be applied to
// associated types of Collection.
C.Indices.Iterator.Element == C.Index {
// A `Collection` is a multi-pass `Sequence`.
@@ -290,6 +376,10 @@ public func check${Traversal}Collection<
resiliencyChecks: resiliencyChecks, sameValue: sameValue)
}
//===------------------------------------------------------------------===//
// Check Index semantics
//===------------------------------------------------------------------===//
let succ = { collection.index(after: $0) }
% if Traversal != 'Forward':
let pred = { collection.index(before: $0) }
@@ -356,7 +446,12 @@ public func check${Traversal}Collection<
let expectedArray = Array(expected)
expectTrue(expectedArray.count == collection.count, ${trace})
// Check `count`.
expectEqual(Int64(expectedArray.count), Int64(collection.count), ${trace})
//===------------------------------------------------------------------===//
// Check Iteration behavior.
//===------------------------------------------------------------------===//
for _ in 0..<3 {
do {
@@ -413,7 +508,7 @@ public func check${Traversal}Collection<
expectedArray[i], collection[index], ${trace}, sameValue: sameValue)
}
% end
% end # Traversal == "Bidirectional"
} // end of `if expectedArray.count >= 2`
@@ -439,65 +534,59 @@ public func check${Traversal}Collection<
// FIXME: more checks for bidirectional and random access collections.
}
public func check${Traversal}Collection<
${genericParam}, C : ${TraversalCollection}
// Helper for check${Traversal}Collection to check Slices.
//
// Checks all slices: O(n^2).
internal func _checkSliceableWith${Traversal}Index<
${genericParam}, S : ${TraversalCollection}
>(
_ expected: ${Expected}, _ collection: C,
${TRACE},
resiliencyChecks: CollectionMisuseResiliencyChecks = .all
) where
C.Iterator.Element == ${Element},
C.Indices.Iterator.Element == C.Index,
${Element} : Equatable {
check${Traversal}Collection(
expected, collection, ${trace},
resiliencyChecks: resiliencyChecks) { $0 == $1 }
}
% end
// FIXME: merge into checkCollection()
public func checkSliceableWithBidirectionalIndex<
${genericParam}, S : BidirectionalCollection
>(
_ expected: ${Expected}, _ sliceable: S, ${TRACE}
_ expected: ${Expected}, _ sliceable: S, ${TRACE},
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (${Element}, ${Element}) -> Bool
) where
S.Iterator.Element == ${Element},
S.Indices.Iterator.Element == S.Index,
S.SubSequence : BidirectionalCollection,
S.SubSequence : ${TraversalCollection},
S.SubSequence.Iterator.Element == ${Element},
S.SubSequence.Indices.Iterator.Element == S.SubSequence.Index,
${Element} : Equatable {
// A `Sliceable` is a `Collection`.
checkBidirectionalCollection(expected, sliceable, ${trace})
S.SubSequence.Index == S.Index,
S.SubSequence.Indices.Iterator.Element == S.Index {
let expectedArray = Array(expected)
let succ = { sliceable.index(after: $0) }
% if Traversal != "Forward":
let pred = { sliceable.index(before: $0) }
% end
var start = sliceable.startIndex
for startNumericIndex in 0...expectedArray.count {
% if Traversal != "Forward":
if start != sliceable.endIndex {
start = succ(start)
start = pred(start)
start = succ(start)
start = pred(start)
}
% end
var end = start
for endNumericIndex in startNumericIndex...expectedArray.count {
% if Traversal != "Forward":
if end != sliceable.endIndex {
end = succ(end)
end = pred(end)
end = succ(end)
end = pred(end)
}
% end
let expectedSlice = expectedArray[startNumericIndex..<endNumericIndex]
let slice = sliceable[start..<end]
// For every possible slice, verify that the slice's bounds are identical
// to the indices used to form the slice.
expectEqual(start, slice.startIndex)
expectEqual(end, slice.endIndex)
checkBidirectionalCollection(expectedSlice, slice, ${trace})
checkOneLevelOf${Traversal}Collection(expectedSlice, slice, ${trace},
resiliencyChecks: resiliencyChecks,
sameValue: sameValue)
if end != sliceable.endIndex {
end = succ(end)
@@ -509,9 +598,13 @@ public func checkSliceableWithBidirectionalIndex<
}
}
% end
% end # Traversal
% end # genericParam, Elements, Expected
// Check RangeReplaceableCollection using a factory.
//
// Note: This does not invoke other collection tests.
public func checkRangeReplaceable<C, N>(
_ makeCollection: @escaping () -> C,
_ makeNewValues: (Int) -> N
@@ -573,13 +666,3 @@ public func checkRangeReplaceable<C, N>(
}
}
}
public func checkCollection<C : Collection, Expected : Collection>(
_ subject: C,
expected: Expected,
${TRACE},
resiliencyChecks: CollectionMisuseResiliencyChecks = .all,
sameValue: (Expected.Iterator.Element, Expected.Iterator.Element) -> Bool
) where C.Iterator.Element == Expected.Iterator.Element {
}