Skip to content

Commit 54328ae

Browse files
committed
Merge pull request #178 from hffmnn/feature/splitAtBug
splitAt does not behave like documented
2 parents 66c83d0 + 13ec9b0 commit 54328ae

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Swiftz/ArrayExt.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ public func find<T>(list : [T], f : (T -> Bool)) -> T? {
7777
/// splitAt(0, [1,2,3]) == ([],[1,2,3])
7878
public func splitAt<T>(index : Int, list : [T]) -> ([T], [T]) {
7979
switch index {
80-
case 0..<list.count:
80+
case 0..<list.count:
8181
return (Array(list[0..<index]), Array(list[index..<list.count]))
82-
case _:
82+
case list.count...Int.max:
83+
return (list, [T]())
84+
default:
8385
return ([T](), [T]())
8486
}
8587
}

SwiftzTests/ArrayExtSpec.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,19 @@ class ArrayExtSpec : XCTestCase {
7070
XCTAssert(found == 4, "Should be found")
7171
}
7272
}
73-
73+
7474
func testSplitAt() {
7575
let withArray = [1,2,3,4]
7676

7777
let tuple = splitAt(2,withArray)
78-
7978
XCTAssert(tuple.0 == [1,2] && tuple.1 == [3,4], "Should be equal")
8079

8180
XCTAssert(splitAt(0,withArray).0 == Array() && splitAt(0, withArray).1 == [1,2,3,4], "Should be equal")
81+
XCTAssert(splitAt(1,withArray).0 == [1] && splitAt(1, withArray).1 == [2,3,4], "Should be equal")
82+
XCTAssert(splitAt(3,withArray).0 == [1,2,3] && splitAt(3, withArray).1 == [4], "Should be equal")
83+
XCTAssert(splitAt(4,withArray).0 == [1,2,3,4] && splitAt(4, withArray).1 == Array(), "Should be equal")
84+
XCTAssert(splitAt(5,withArray).0 == [1,2,3,4] && splitAt(5, withArray).1 == Array(), "Should be equal")
85+
8286
XCTAssert(withArray == [1,2,3,4], "Should be equal(immutablility test)")
8387
}
8488

0 commit comments

Comments
 (0)