Skip to content

Commit dd564ca

Browse files
committed
Add unit tests for documented examples of rotate functions
1 parent 44d359b commit dd564ca

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Tests/SwiftAlgorithmsTests/RotateTests.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,49 @@ import XCTest
1313
import Algorithms
1414

1515
final class RotateTests: XCTestCase {
16+
/// Tests `rotate(subrange:toStartAt:)` with an empty subrange
17+
/// The order of elements are unchanged
18+
func testRotateEmptySubrange() {
19+
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
20+
let oldStart = numbers.rotate(subrange: 3..<3, toStartAt: 3)
21+
XCTAssertEqual(numbers, [10, 20, 30, 40, 50, 60, 70, 80])
22+
XCTAssertEqual(numbers[oldStart], 40)
23+
}
24+
25+
/// Tests `rotate(subrange:toStartAt:)` with an empty collection
26+
func testRotateSubrangeOnEmptyCollection() {
27+
var numbers = [Int]()
28+
let oldStart = numbers.rotate(subrange: 0..<0, toStartAt: 0)
29+
XCTAssertEqual(numbers, [])
30+
XCTAssertEqual(oldStart, numbers.startIndex)
31+
}
32+
33+
/// Tests `rotate(subrange:toStartAt:)` with the full range of the collection
34+
func testRotateFullRange() {
35+
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
36+
let oldStart = numbers.rotate(subrange: 0..<8, toStartAt: 1)
37+
XCTAssertEqual(numbers, [20, 30, 40, 50, 60, 70, 80, 10])
38+
XCTAssertEqual(numbers[oldStart], 10)
39+
}
40+
41+
/// Tests the example given in `rotate(subrange:toStartAt:)`’s documentation
42+
func testRotateSubrange() {
43+
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
44+
let oldStart = numbers.rotate(subrange: 0..<4, toStartAt: 2)
45+
XCTAssertEqual(numbers, [30, 40, 10, 20, 50, 60, 70, 80])
46+
XCTAssertEqual(numbers[oldStart], 10)
47+
}
48+
49+
/// Tests the example given in `rotate(toStartAt:)`’s documentation
50+
func testRotateExample() {
51+
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
52+
let oldStart = numbers.rotate(toStartAt: 3)
53+
XCTAssertEqual(numbers, [40, 50, 60, 70, 80, 10, 20, 30])
54+
XCTAssertEqual(numbers[oldStart], 10)
55+
}
56+
57+
/// Tests `rotate(toStartAt:)` on collections of varying lengths, at different
58+
/// starting points
1659
func testRotate() {
1760
for length in 0...15 {
1861
let a = Array(0..<length)

0 commit comments

Comments
 (0)