From bdf36ad23d67184ef61b18257472473e211a4cd3 Mon Sep 17 00:00:00 2001 From: Matt Zanchelli Date: Tue, 9 Feb 2021 16:43:04 -0800 Subject: [PATCH 1/4] Avoid ambiguous single-letter variable names --- Sources/Algorithms/Rotate.swift | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/Algorithms/Rotate.swift b/Sources/Algorithms/Rotate.swift index 5b1fcfe..19196cb 100644 --- a/Sources/Algorithms/Rotate.swift +++ b/Sources/Algorithms/Rotate.swift @@ -25,23 +25,23 @@ extension MutableCollection where Self: BidirectionalCollection { /// Output: /// [p o n m e f g h i j k l d c b a] /// ^ ^ - /// f l + /// lower upper /// - /// - Postcondition: For returned indices `(f, l)`: - /// `f == limit || l == limit` + /// - Postcondition: For returned indices `(lower, upper)`: + /// `lower == limit || upper == limit` @usableFromInline @discardableResult internal mutating func _reverse( subrange: Range, until limit: Index ) -> (Index, Index) { - var f = subrange.lowerBound - var l = subrange.upperBound - while f != limit && l != limit { - formIndex(before: &l) - swapAt(f, l) - formIndex(after: &f) + var lower = subrange.lowerBound + var upper = subrange.upperBound + while lower != limit && upper != limit { + formIndex(before: &upper) + swapAt(lower, upper) + formIndex(after: &lower) } - return (f, l) + return (lower, upper) } /// Reverses the elements within the given subrange. From 44d359b4297a4edb948e599f6576668b1063b0ac Mon Sep 17 00:00:00 2001 From: Matt Zanchelli Date: Tue, 9 Feb 2021 16:43:15 -0800 Subject: [PATCH 2/4] Use consistent variable names --- Sources/Algorithms/Rotate.swift | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Sources/Algorithms/Rotate.swift b/Sources/Algorithms/Rotate.swift index 19196cb..08799ed 100644 --- a/Sources/Algorithms/Rotate.swift +++ b/Sources/Algorithms/Rotate.swift @@ -59,13 +59,12 @@ extension MutableCollection where Self: BidirectionalCollection { @inlinable public mutating func reverse(subrange: Range) { if subrange.isEmpty { return } - var lo = subrange.lowerBound - var hi = subrange.upperBound - - while lo < hi { - formIndex(before: &hi) - swapAt(lo, hi) - formIndex(after: &lo) + var lower = subrange.lowerBound + var upper = subrange.upperBound + while lower < upper { + formIndex(before: &upper) + swapAt(lower, upper) + formIndex(after: &lower) } } } From dd564ca517154a8df9d942a853089a69d5814859 Mon Sep 17 00:00:00 2001 From: Matt Zanchelli Date: Tue, 9 Feb 2021 16:41:56 -0800 Subject: [PATCH 3/4] Add unit tests for documented examples of rotate functions --- Tests/SwiftAlgorithmsTests/RotateTests.swift | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Tests/SwiftAlgorithmsTests/RotateTests.swift b/Tests/SwiftAlgorithmsTests/RotateTests.swift index c37b5ea..01b47d0 100644 --- a/Tests/SwiftAlgorithmsTests/RotateTests.swift +++ b/Tests/SwiftAlgorithmsTests/RotateTests.swift @@ -13,6 +13,49 @@ import XCTest import Algorithms final class RotateTests: XCTestCase { + /// Tests `rotate(subrange:toStartAt:)` with an empty subrange + /// The order of elements are unchanged + func testRotateEmptySubrange() { + var numbers = [10, 20, 30, 40, 50, 60, 70, 80] + let oldStart = numbers.rotate(subrange: 3..<3, toStartAt: 3) + XCTAssertEqual(numbers, [10, 20, 30, 40, 50, 60, 70, 80]) + XCTAssertEqual(numbers[oldStart], 40) + } + + /// Tests `rotate(subrange:toStartAt:)` with an empty collection + func testRotateSubrangeOnEmptyCollection() { + var numbers = [Int]() + let oldStart = numbers.rotate(subrange: 0..<0, toStartAt: 0) + XCTAssertEqual(numbers, []) + XCTAssertEqual(oldStart, numbers.startIndex) + } + + /// Tests `rotate(subrange:toStartAt:)` with the full range of the collection + func testRotateFullRange() { + var numbers = [10, 20, 30, 40, 50, 60, 70, 80] + let oldStart = numbers.rotate(subrange: 0..<8, toStartAt: 1) + XCTAssertEqual(numbers, [20, 30, 40, 50, 60, 70, 80, 10]) + XCTAssertEqual(numbers[oldStart], 10) + } + + /// Tests the example given in `rotate(subrange:toStartAt:)`’s documentation + func testRotateSubrange() { + var numbers = [10, 20, 30, 40, 50, 60, 70, 80] + let oldStart = numbers.rotate(subrange: 0..<4, toStartAt: 2) + XCTAssertEqual(numbers, [30, 40, 10, 20, 50, 60, 70, 80]) + XCTAssertEqual(numbers[oldStart], 10) + } + + /// Tests the example given in `rotate(toStartAt:)`’s documentation + func testRotateExample() { + var numbers = [10, 20, 30, 40, 50, 60, 70, 80] + let oldStart = numbers.rotate(toStartAt: 3) + XCTAssertEqual(numbers, [40, 50, 60, 70, 80, 10, 20, 30]) + XCTAssertEqual(numbers[oldStart], 10) + } + + /// Tests `rotate(toStartAt:)` on collections of varying lengths, at different + /// starting points func testRotate() { for length in 0...15 { let a = Array(0.. Date: Mon, 15 Feb 2021 13:31:34 -0800 Subject: [PATCH 4/4] Add unit tests for reverse functions --- Tests/SwiftAlgorithmsTests/RotateTests.swift | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Tests/SwiftAlgorithmsTests/RotateTests.swift b/Tests/SwiftAlgorithmsTests/RotateTests.swift index 01b47d0..8d369bb 100644 --- a/Tests/SwiftAlgorithmsTests/RotateTests.swift +++ b/Tests/SwiftAlgorithmsTests/RotateTests.swift @@ -10,9 +10,27 @@ //===----------------------------------------------------------------------===// import XCTest -import Algorithms +@testable import Algorithms final class RotateTests: XCTestCase { + /// Tests the example given in `_reverse(subrange:until:)`’s documentation + func testUnderscoreReverse() { + var input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"] + let limit: Int = 4 + let (lower, upper) = input._reverse(subrange: input.startIndex..