Files
swift-mirror/validation-test/stdlib/StdlibUnittestSequencesCollections.swift.gyb
2015-07-12 01:04:38 +00:00

321 lines
8.4 KiB
Swift

// -*- swift -*-
// RUN: rm -rf %t ; mkdir -p %t
// RUN: %S/../../utils/gyb %s -o %t/StdlibUnittestSequencesCollections.swift
// RUN: %S/../../utils/line-directive %t/StdlibUnittestSequencesCollections.swift -- %target-build-swift %t/StdlibUnittestSequencesCollections.swift -o %t/a.out
// RUN: %S/../../utils/line-directive %t/StdlibUnittestSequencesCollections.swift -- %target-run %t/a.out
// REQUIRES: executable_test
import SwiftPrivate
import StdlibUnittest
%for traversal in [ 'Forward', 'Bidirectional', 'RandomAccess' ]:
% Self = 'Minimal%sRangeReplaceableCollection' % traversal
var ${Self}TestSuite = TestSuite("${Self}")
${Self}TestSuite.test("startIndex/SameBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
expectEqual(c1.startIndex, c1.startIndex)
expectEqual(c2.startIndex, c2.startIndex)
expectEqual(c1.startIndex, c2.startIndex)
_blackHole(c2)
}
${Self}TestSuite.test("startIndex/DifferentBuffer") {
let c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
expectEqual(c1.startIndex, c1.startIndex)
expectEqual(c2.startIndex, c2.startIndex)
let i1 = c1.startIndex
let i2 = c2.startIndex
expectFailure {
_ = i1 == i2
}
}
${Self}TestSuite.test("endIndex/DifferentBuffer") {
let c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
expectEqual(c1.endIndex, c1.endIndex)
expectEqual(c2.endIndex, c2.endIndex)
let i1 = c1.endIndex
let i2 = c2.endIndex
expectFailure {
_ = i1 == i2
}
}
${Self}TestSuite.test("reserveCapacity()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
let i = c1.startIndex
c1.reserveCapacity(0)
expectFailure {
_ = i == c1.startIndex
}
_blackHole(c2)
}
${Self}TestSuite.test("reserveCapacity()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let i = c.startIndex
c.reserveCapacity(0)
expectFailure {
_ = i == c.startIndex
}
}
${Self}TestSuite.test("append()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
let i = c1.startIndex
c1.append(OpaqueValue(4040))
expectFailure {
_ = i == c1.startIndex
}
_blackHole(c2)
}
${Self}TestSuite.test("append()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let i = c.startIndex
c.append(OpaqueValue(4040))
expectFailure {
_ = i == c.startIndex
}
}
${Self}TestSuite.test("extend()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
let i = c1.startIndex
c1.extend([ 4040, 5050 ].map { OpaqueValue($0) })
expectFailure {
_ = i == c1.startIndex
}
_blackHole(c2)
}
${Self}TestSuite.test("extend()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let i = c.startIndex
c.extend([ 4040, 5050 ].map { OpaqueValue($0) })
expectFailure {
_ = i == c.startIndex
}
}
${Self}TestSuite.test("replaceRange()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
let i = c1.startIndex
c1.replaceRange(
c1.endIndex..<c1.endIndex,
with: [ 4040, 5050 ].map { OpaqueValue($0) })
expectFailure {
_ = i == c1.startIndex
}
_blackHole(c2)
}
${Self}TestSuite.test("replaceRange()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let i = c.startIndex
c.replaceRange(
c.endIndex..<c.endIndex,
with: [ 4040, 5050 ].map { OpaqueValue($0) })
expectFailure {
_ = i == c.startIndex
}
}
${Self}TestSuite.test("insert()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
let i = c1.startIndex
c1.insert(OpaqueValue(4040), atIndex: c1.endIndex)
expectFailure {
_ = i == c1.startIndex
}
_blackHole(c2)
}
${Self}TestSuite.test("insert()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let i = c.startIndex
c.insert(OpaqueValue(4040), atIndex: c.endIndex)
expectFailure {
_ = i == c.startIndex
}
}
${Self}TestSuite.test("splice()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
let i = c1.startIndex
c1.splice([ 4040, 5050 ].map { OpaqueValue($0) }, atIndex: c1.endIndex)
expectFailure {
_ = i == c1.startIndex
}
_blackHole(c2)
}
${Self}TestSuite.test("splice()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let i = c.startIndex
c.splice([ 4040, 5050 ].map { OpaqueValue($0) }, atIndex: c.endIndex)
expectFailure {
_ = i == c.startIndex
}
}
${Self}TestSuite.test("removeAtIndex()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
let i = c1.startIndex
c1.removeAtIndex(c1.startIndex)
expectFailure {
_ = i == c1.startIndex
}
_blackHole(c2)
}
${Self}TestSuite.test("removeAtIndex()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let i = c.startIndex
c.removeAtIndex(c.startIndex)
expectFailure {
_ = i == c.startIndex
}
}
${Self}TestSuite.test("removeLast()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
let i = c1.startIndex
c1.removeLast()
expectFailure {
_ = i == c1.startIndex
}
_blackHole(c2)
}
${Self}TestSuite.test("removeLast()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let i = c.startIndex
c.removeLast()
expectFailure {
_ = i == c.startIndex
}
}
${Self}TestSuite.test("removeRange()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
let i = c1.startIndex
c1.removeRange(c1.startIndex..<c1.startIndex)
expectFailure {
_ = i == c1.startIndex
}
_blackHole(c2)
}
${Self}TestSuite.test("removeRange()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let i = c.startIndex
c.removeRange(c.startIndex..<c.startIndex)
expectFailure {
_ = i == c.startIndex
}
}
${Self}TestSuite.test("removeAll()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let c2 = c1
let i = c1.startIndex
c1.removeAll(keepCapacity: true)
expectFailure {
_ = i == c1.startIndex
}
_blackHole(c2)
}
${Self}TestSuite.test("removeRange()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map { OpaqueValue($0) })
let i = c.startIndex
c.removeAll(keepCapacity: true)
expectFailure {
_ = i == c.startIndex
}
}
%end
var SequenceTypeAlgorithms = TestSuite("SequenceTypeAlgorithms")
SequenceTypeAlgorithms.test("forAllPermutations") {
if true {
var permutations: [[Int]] = []
forAllPermutations(0) {
permutations.append($0)
}
expectEqualSequence([] as [[Int]], permutations) { $0 == $1 }
}
if true {
var permutations: [[Int]] = []
forAllPermutations(1) {
permutations.append($0)
}
expectEqualSequence([[ 0 ]] as [[Int]], permutations) { $0 == $1 }
}
if true {
var permutations: [[Int]] = []
forAllPermutations(2) {
permutations.append($0)
}
expectEqualSequence(
[[ 0, 1 ], [ 1, 0 ]] as [[Int]], permutations) { $0 == $1 }
}
if true {
var permutations: [[Int]] = []
forAllPermutations(3) {
permutations.append($0)
}
expectEqualSequence(
[
[ 0, 1, 2 ],
[ 0, 2, 1 ],
[ 1, 0, 2 ],
[ 1, 2, 0 ],
[ 2, 0, 1 ],
[ 2, 1, 0 ],
] as [[Int]],
permutations) { $0 == $1 }
}
if true {
var permutations: [[Int]] = []
forAllPermutations([ 10, 20, 30 ]) {
permutations.append($0)
}
expectEqualSequence(
[
[ 10, 20, 30 ],
[ 10, 30, 20 ],
[ 20, 10, 30 ],
[ 20, 30, 10 ],
[ 30, 10, 20 ],
[ 30, 20, 10 ],
] as [[Int]],
permutations) { $0 == $1 }
}
}
runAllTests()