Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add offset overflow check #144

Merged
merged 2 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@ include("axes.jl")
struct OffsetArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N}
parent::AA
offsets::NTuple{N,Int}
function OffsetArray{T, N, AA}(parent::AA, offsets::NTuple{N, Int}) where {T, N, AA<:AbstractArray}
overflow_check.(axes(parent), offsets)
new{T, N, AA}(parent, offsets)
end
end
OffsetVector{T,AA<:AbstractArray} = OffsetArray{T,1,AA}
OffsetMatrix{T,AA<:AbstractArray} = OffsetArray{T,2,AA}

function overflow_check(r, offset::T) where T
if offset > 0 && last(r) > typemax(T) - offset
jishnub marked this conversation as resolved.
Show resolved Hide resolved
throw(ArgumentError("Boundary overflow detected: offset $offset should be equal or less than $(typemax(T) - last(r))"))
elseif offset < 0 && first(r) < typemin(T) - offset
throw(ArgumentError("Boundary overflow detected: offset $offset should be equal or greater than $(typemin(T) - first(r))"))
end
end
## OffsetArray constructors

offset(axparent::AbstractUnitRange, ax::AbstractUnitRange) = first(ax) - first(axparent)
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using OffsetArrays
using OffsetArrays: IdentityUnitRange, no_offset_view
using OffsetArrays: IdOffsetRange
using Test, Aqua
using LinearAlgebra
using DelimitedFiles
Expand Down Expand Up @@ -125,6 +126,13 @@ end
w = zeros(5:6)
@test OffsetVector(w, :) == OffsetArray(w, (:,)) == OffsetArray(w, :) == OffsetArray(w, axes(w))
@test axes(OffsetVector(w, :)) == axes(w)

@test axes(OffsetVector(v, typemax(Int)-length(v))) == (IdOffsetRange(axes(v)[1], typemax(Int)-length(v)), )
@test_throws ArgumentError OffsetVector(v, typemax(Int)-length(v)+1)
ao = OffsetArray(v, typemin(Int))
@test_nowarn OffsetArray{Float64, 1, typeof(ao)}(ao, (-1, ))
@test_throws ArgumentError OffsetArray{Float64, 1, typeof(ao)}(ao, (-2, )) # inner Constructor
@test_throws ArgumentError OffsetArray(ao, (-2, )) # convinient constructor accumulate offsets
end

@testset "OffsetMatrix constructors" begin
Expand Down Expand Up @@ -158,6 +166,10 @@ end

@test axes(OffsetMatrix(w, :, CartesianIndices((0:1,)))) == (3:4, 0:1)
@test axes(OffsetMatrix(w, CartesianIndices((0:1,)), :)) == (0:1, 5:6)

@test axes(OffsetMatrix(v, typemax(Int)-size(v, 1), 0)) == (IdOffsetRange(axes(v)[1], typemax(Int)-size(v, 1)), axes(v, 2))
@test_throws ArgumentError OffsetMatrix(v, typemax(Int)-size(v,1)+1, 0)
@test_throws ArgumentError OffsetMatrix(v, 0, typemax(Int)-size(v, 2)+1)
end

@testset "construct OffsetArray with CartesianIndices" begin
Expand Down