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