Skip to content

Commit

Permalink
Merge pull request #178 from hffmnn/feature/splitAtBug
Browse files Browse the repository at this point in the history
splitAt does not behave like documented
  • Loading branch information
CodaFi committed Feb 8, 2015
2 parents 66c83d0 + 13ec9b0 commit 54328ae
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Swiftz/ArrayExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ public func find<T>(list : [T], f : (T -> Bool)) -> T? {
/// splitAt(0, [1,2,3]) == ([],[1,2,3])
public func splitAt<T>(index : Int, list : [T]) -> ([T], [T]) {
switch index {
case 0..<list.count:
case 0..<list.count:
return (Array(list[0..<index]), Array(list[index..<list.count]))
case _:
case list.count...Int.max:
return (list, [T]())
default:
return ([T](), [T]())
}
}
Expand Down
8 changes: 6 additions & 2 deletions SwiftzTests/ArrayExtSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,19 @@ class ArrayExtSpec : XCTestCase {
XCTAssert(found == 4, "Should be found")
}
}

func testSplitAt() {
let withArray = [1,2,3,4]

let tuple = splitAt(2,withArray)

XCTAssert(tuple.0 == [1,2] && tuple.1 == [3,4], "Should be equal")

XCTAssert(splitAt(0,withArray).0 == Array() && splitAt(0, withArray).1 == [1,2,3,4], "Should be equal")
XCTAssert(splitAt(1,withArray).0 == [1] && splitAt(1, withArray).1 == [2,3,4], "Should be equal")
XCTAssert(splitAt(3,withArray).0 == [1,2,3] && splitAt(3, withArray).1 == [4], "Should be equal")
XCTAssert(splitAt(4,withArray).0 == [1,2,3,4] && splitAt(4, withArray).1 == Array(), "Should be equal")
XCTAssert(splitAt(5,withArray).0 == [1,2,3,4] && splitAt(5, withArray).1 == Array(), "Should be equal")

XCTAssert(withArray == [1,2,3,4], "Should be equal(immutablility test)")
}

Expand Down

0 comments on commit 54328ae

Please sign in to comment.