Skip to content

Commit

Permalink
Fix error in VaueArraySlice subscripting
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Isaza committed Dec 9, 2015
1 parent 3a07250 commit 4a85429
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Source/Array/ValueArraySlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public struct ValueArraySlice<Element: Value> : ContiguousMutableMemory, Mutable

public subscript(index: Int) -> Element {
get {
let baseIndex = startIndex + index * step
let baseIndex = startIndex + (index - startIndex) * step
precondition(0 <= baseIndex && baseIndex < base.count)
return pointer[baseIndex]
}
set {
let baseIndex = startIndex + index * step
let baseIndex = startIndex + (index - startIndex) * step
precondition(0 <= baseIndex && baseIndex < base.count)
mutablePointer[baseIndex] = newValue
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/RealArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ class RealArrayTests: XCTestCase {
XCTAssertEqual(n[0], 1.0)
XCTAssertEqual(n[1], 2.0)
}

func testSlice() {
let array = RealArray((0..<10).map({ Double($0) }))
let slice = array[5...8]

XCTAssertEqual(slice.count, 4)
XCTAssertEqual(slice.startIndex, 5)
XCTAssertEqual(slice.endIndex, 9)
XCTAssertEqual([Double](slice), [5.0, 6.0, 7.0, 8.0])
}
}

0 comments on commit 4a85429

Please sign in to comment.