From c96f9b523ab919fb88cbe01379ad13f876a076c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mos=C3=A8=20Giordano?= Date: Tue, 25 Feb 2020 18:42:55 +0000 Subject: [PATCH] Add tests for OffsetArrays --- base/abstractarray.jl | 4 ++-- test/offsetarray.jl | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index e1c0c1574b5a5..f654bd8bdf196 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -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) @@ -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) diff --git a/test/offsetarray.jl b/test/offsetarray.jl index d8e2518e2f139..caa8199670958 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -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