1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
// RUN: rm -rf %t ; mkdir -p %t
// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-codesign %t/a.out4 && %target-run %t/a.out4
// RUN: %target-build-swift %s -o %t/a.out42 -swift-version 4.2 && %target-codesign %t/a.out42 && %target-run %t/a.out42
// REQUIRES: executable_test
import StdlibUnittest
import StdlibCollectionUnittest
//===--- MyCollection -----------------------------------------------------===//
/// A simple collection that attempts to use an Int16 IndexDistance
struct MyCollection<Element>: Collection {
var _elements: [Element]
typealias IndexDistance = Int16
typealias Index = Int16
var startIndex: Index { return 0 }
var endIndex: Index { return numericCast(_elements.count) }
subscript(i: Index) -> Element { return _elements[Int(i)] }
func index(after: Index) -> Index { return after+1 }
}
//===--- MyBidiCollection -------------------------------------------------===//
/// A simple collection that doesn't declare an IndexDistance
struct MyBidiCollection<Element>: BidirectionalCollection {
var _elements: [Element]
typealias Index = Int64
var startIndex: Index { return 0 }
var endIndex: Index { return numericCast(_elements.count) }
subscript(i: Index) -> Element { return _elements[Int(i)] }
func index(after: Index) -> Index { return after+1 }
func index(before: Index) -> Index { return before-1 }
func index(_ i: Index, advancedBy d: Int64) -> Index { return i+d }
}
let CollectionDistance = TestSuite("Collection.IndexDistance")
CollectionDistance.test("Int16/distance") {
let c = MyCollection<Int>(_elements: [1,2,3])
var d: Int16 = c.distance(from: c.startIndex, to: c.endIndex)
expectEqual(3, d)
expectType(MyCollection<Int>.IndexDistance.self, &d)
// without type context, you now get an Int
var i = c.distance(from: c.startIndex, to: c.endIndex)
expectType(Int.self, &i)
}
CollectionDistance.test("Int16/advance") {
let c = MyCollection<Int>(_elements: [1,2,3])
let d: Int16 = 1
var i = c.index(c.startIndex, offsetBy: d)
expectEqual(1, i)
c.formIndex(&i, offsetBy: d)
expectEqual(2, i)
let j = c.index(c.startIndex, offsetBy: d, limitedBy: c.endIndex)
expectEqual(1, j)
var b = c.formIndex(&i, offsetBy: d, limitedBy: c.endIndex)
expectTrue(b)
expectEqual(3, i)
b = c.formIndex(&i, offsetBy: d+5, limitedBy: c.endIndex)
expectFalse(b)
expectEqual(3, i)
let k = c.index(c.startIndex, offsetBy: d+5, limitedBy: c.endIndex)
expectEqual(nil, k)
checkCollection(c, [1,2,3], stackTrace: SourceLocStack()) { $0 == $1 }
}
CollectionDistance.test("Int64/distance") {
let c = MyBidiCollection<Int>(_elements: [1,2,3])
var d = c.distance(from: c.startIndex, to: c.endIndex)
expectEqual(3, d)
expectType(Int.self, &d)
expectType(MyBidiCollection<Int>.IndexDistance.self, &d)
}
CollectionDistance.test("Int64/advance") {
let c = MyBidiCollection<Int>(_elements: [1,2,3])
let d: Int16 = 1
var i = c.index(c.startIndex, offsetBy: d)
expectEqual(1, i)
c.formIndex(&i, offsetBy: d)
expectEqual(2, i)
let j = c.index(c.startIndex, offsetBy: d, limitedBy: c.endIndex)
expectEqual(1, j)
var b = c.formIndex(&i, offsetBy: d, limitedBy: c.endIndex)
expectTrue(b)
expectEqual(3, i)
b = c.formIndex(&i, offsetBy: d+5, limitedBy: c.endIndex)
expectFalse(b)
expectEqual(3, i)
let k = c.index(c.startIndex, offsetBy: d+5, limitedBy: c.endIndex)
expectEqual(nil, k)
checkCollection(c, [1,2,3], stackTrace: SourceLocStack()) { $0 == $1 }
checkBidirectionalCollection(c, [1,2,3])
}
extension Collection where Index == Int, IndexDistance == Int {
var myCount: Int {
return distance(from: startIndex, to: endIndex)
}
}
CollectionDistance.test("IndexDistance/constraint") {
let n = [1,2,3].myCount
expectEqual(3, n)
}
runAllTests()
|