Skip to content

Commit

Permalink
Add tests for OffsetArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Jul 10, 2020
1 parent 90e4b74 commit c96f9b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ julia> first(Bool[], 1)
0-element Array{Bool,1}
```
"""
first(v::AbstractVector, n::Integer) = @inbounds v[firstindex(v):min(firstindex(v) - 1 + n, end)]
first(v::AbstractVector, n::Integer) = @inbounds v[firstindex(v):min(firstindex(v) + n - 1, end)]

"""
last(coll)
Expand Down Expand Up @@ -401,7 +401,7 @@ julia> last(Float64[], 1)
0-element Array{Float64,1}
```
"""
last(v::AbstractArray, n::Integer) = @inbounds v[max(firstindex(v), end + 1 - n):end]
last(v::AbstractArray, n::Integer) = @inbounds v[max(firstindex(v), end - n + 1):end]

"""
strides(A)
Expand Down
14 changes: 14 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,17 @@ end
@test_throws DimensionMismatch maximum!(fill(0, -4:-4, 7:7, -6:-5, 1:1), B)
@test_throws DimensionMismatch minimum!(fill(0, -4:-4, 7:7, -6:-5, 1:1), B)
end

@testset "first/last n elements of vector" begin
f = firstindex(v)
@test first(v, -2) == []
@test first(v, 2) == v[f:f+1]
@test first(v, 100) == v0
@test first(v, 100) !== v
@test first(v, 1) != v[f]
@test last(v, -2) == []
@test last(v, 2) == v[end-1:end]
@test last(v, 100) == v0
@test last(v, 100) !== v
@test last(v, 1) != v[end]
end

0 comments on commit c96f9b5

Please sign in to comment.