|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Algorithms open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +import XCTest |
| 13 | +import Algorithms |
| 14 | + |
| 15 | +final class StridingTests: XCTestCase { |
| 16 | + |
| 17 | + func testStride() { |
| 18 | + let a = (0...10) |
| 19 | + XCTAssertEqualSequences(a.striding(by: 1), (0...10)) |
| 20 | + XCTAssertEqualSequences(a.striding(by: 2), [0, 2, 4, 6, 8, 10]) |
| 21 | + XCTAssertEqualSequences(a.striding(by: 3), [0, 3, 6, 9]) |
| 22 | + XCTAssertEqualSequences(a.striding(by: 4), [0, 4, 8]) |
| 23 | + XCTAssertEqualSequences(a.striding(by: 5), [0, 5, 10]) |
| 24 | + XCTAssertEqualSequences(a.striding(by: 10), [0, 10]) |
| 25 | + XCTAssertEqualSequences(a.striding(by: 11), [0]) |
| 26 | + } |
| 27 | + |
| 28 | + func testStrideString() { |
| 29 | + let s = "swift" |
| 30 | + XCTAssertEqualSequences(s.striding(by: 2), ["s", "i", "t"]) |
| 31 | + } |
| 32 | + |
| 33 | + func testStrideReversed() { |
| 34 | + let a = [0, 1, 2, 3, 4, 5] |
| 35 | + XCTAssertEqualSequences(a.striding(by: 3).reversed(), [3, 0]) |
| 36 | + XCTAssertEqualSequences(a.reversed().striding(by: 2), [5, 3, 1]) |
| 37 | + } |
| 38 | + |
| 39 | + func testStrideIndexes() { |
| 40 | + let a = [0, 1, 2, 3, 4, 5].striding(by: 2) |
| 41 | + var i = a.startIndex |
| 42 | + XCTAssertEqual(a[i], 0) |
| 43 | + a.formIndex(after: &i) |
| 44 | + XCTAssertEqual(a[i], 2) |
| 45 | + a.formIndex(after: &i) |
| 46 | + XCTAssertEqual(a[i], 4) |
| 47 | + a.formIndex(before: &i) |
| 48 | + XCTAssertEqual(a[i], 2) |
| 49 | + a.formIndex(before: &i) |
| 50 | + XCTAssertEqual(a[i], 0) |
| 51 | +// a.formIndex(before: &i) // Precondition failed: Advancing past start index |
| 52 | +// a.index(after: a.endIndex) // Precondition failed: Advancing past end index |
| 53 | + } |
| 54 | + |
| 55 | + func testStrideCompositionEquivalence() { |
| 56 | + let a = (0...10) |
| 57 | + XCTAssertEqualSequences(a.striding(by: 6), a.striding(by: 2).striding(by: 3)) |
| 58 | + XCTAssertTrue(a.striding(by: 6) == a.striding(by: 2).striding(by: 3)) |
| 59 | + XCTAssert(type(of: a.striding(by: 2).striding(by: 3)) == Stride<ClosedRange<Int>>.self) |
| 60 | + } |
| 61 | + |
| 62 | + func testEquality() { |
| 63 | + let a = [1, 2, 3, 4, 5].striding(by: 2) |
| 64 | + let b = [1, 0, 3, 0, 5].striding(by: 2) |
| 65 | + XCTAssertEqual(a, b) |
| 66 | + } |
| 67 | + |
| 68 | + func testStrideLast() { |
| 69 | + XCTAssertEqual((1...10).striding(by: 2).last, 9) // 1, 3, 5, 7, 9 |
| 70 | + XCTAssertEqual((1...10).striding(by: 3).last, 10) // 1, 4, 7, 10 |
| 71 | + XCTAssertEqual((1...10).striding(by: 4).last, 9) // 1, 5, 9 |
| 72 | + XCTAssertEqual((1...10).striding(by: 5).last, 6) // 1, 6 |
| 73 | + XCTAssertEqual((1...100).striding(by: 50).last, 51) // 1, 51 |
| 74 | + XCTAssertEqual((1...5).striding(by: 2).last, 5) // 1, 3, 5 |
| 75 | + XCTAssertEqual([Int]().striding(by: 2).last, nil) // empty |
| 76 | + } |
| 77 | + |
| 78 | + func testCount() { |
| 79 | + let empty = [Int]().striding(by: 2) |
| 80 | + XCTAssertEqual(empty.count, 0) |
| 81 | + let a = (0...10) |
| 82 | + XCTAssertEqual(a.striding(by: 1).count, (0...10).count) |
| 83 | + XCTAssertEqual(a.striding(by: 2).count, [0, 2, 4, 6, 8, 10].count) |
| 84 | + XCTAssertEqual(a.striding(by: 3).count, [0, 3, 6, 9].count) |
| 85 | + XCTAssertEqual(a.striding(by: 4).count, [0, 4, 8].count) |
| 86 | + XCTAssertEqual(a.striding(by: 5).count, [0, 5, 10].count) |
| 87 | + XCTAssertEqual(a.striding(by: 10).count, [0, 10].count) |
| 88 | + XCTAssertEqual(a.striding(by: 11).count, [0].count) |
| 89 | + } |
| 90 | + |
| 91 | + func testDistance() { |
| 92 | + |
| 93 | + do { |
| 94 | + let a = (0...100).striding(by: 11) |
| 95 | + XCTAssertEqual(a.distance(from: a.startIndex, to: a.endIndex), a.count) |
| 96 | + for (i, index) in a.indices.enumerated() { |
| 97 | + XCTAssertEqual(a.distance(from: a.startIndex, to: index), i) |
| 98 | + } |
| 99 | + |
| 100 | + var i = a.startIndex |
| 101 | + a.formIndex(&i, offsetBy: 3) |
| 102 | + XCTAssertEqual(a.distance(from: a.startIndex, to: i), 3) |
| 103 | + XCTAssertEqual(a[i], 33) |
| 104 | + } |
| 105 | + |
| 106 | + do { |
| 107 | + |
| 108 | + let a = (0...100).striding(by: 10) |
| 109 | + XCTAssertEqual(a.distance(from: a.startIndex, to: a.endIndex), a.count) |
| 110 | + |
| 111 | + for (i, index) in a.indices.enumerated() { |
| 112 | + XCTAssertEqual(a.distance(from: a.startIndex, to: index), i) |
| 113 | + } |
| 114 | + |
| 115 | + var i = a.startIndex |
| 116 | + a.formIndex(&i, offsetBy: 3) |
| 117 | + XCTAssertEqual(a.distance(from: a.startIndex, to: i), 3) |
| 118 | + XCTAssertEqual(a[i], 30) |
| 119 | + } |
| 120 | + |
| 121 | + do { |
| 122 | + |
| 123 | + let a = (0...100).striding(by: 101) |
| 124 | + XCTAssertEqual(a.distance(from: a.startIndex, to: a.endIndex), a.count) |
| 125 | + |
| 126 | + for (i, index) in a.indices.enumerated() { |
| 127 | + XCTAssertEqual(a.distance(from: a.startIndex, to: index), i) |
| 128 | + } |
| 129 | + |
| 130 | + var i = a.startIndex |
| 131 | + a.formIndex(&i, offsetBy: 3) |
| 132 | + XCTAssertEqual(a.distance(from: a.startIndex, to: i), a.count) |
| 133 | + XCTAssertEqual(i, a.endIndex) |
| 134 | +// a[i] // == Fatal error: Index out of range |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + func testOffsetBy() { |
| 139 | + let a = (0...100).striding(by: 22) |
| 140 | + let b = [0, 22, 44, 66, 88] |
| 141 | + for i in 0..<a.count { |
| 142 | + XCTAssertEqual(a[a.index(a.startIndex, offsetBy: i)], b[i]) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + func testOffsetByEndIndex() { |
| 147 | + let a = 1...5 |
| 148 | + let b = a.striding(by: 3) // [1, 4] |
| 149 | + let i = b.index(b.startIndex, offsetBy: 2) |
| 150 | + XCTAssertEqual(i, b.endIndex) |
| 151 | + } |
| 152 | +} |
0 commit comments