Skip to content

Commit

Permalink
Type assertions in converting offsets (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Jan 18, 2021
1 parent e65fd01 commit b20b4db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ struct OffsetArray{T,N,AA<:AbstractArray{T,N}} <: AbstractArray{T,N}
end

function OffsetArray{T, N, AA}(parent::AA, offsets::NTuple{N, <:Integer}) where {T, N, AA<:AbstractArray{T,N}}
OffsetArray{T, N, AA}(parent, map(x -> convert(Int, x), offsets))
OffsetArray{T, N, AA}(parent, map(x -> convert(Int, x)::Int, offsets))
end

"""
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ end
@test first(rred) == first(r)
end

# used in testing the constructor
struct WeirdInteger{T} <: Integer
x :: T
end
# assume that it doesn't behave as expected
Base.convert(::Type{Int}, a::WeirdInteger) = a

@testset "Constructors" begin
@testset "Single-entry arrays in dims 0:5" begin
for n = 0:5
Expand Down Expand Up @@ -535,6 +542,9 @@ end
@test_throws TypeError OffsetArray{Float64,2,Matrix{ComplexF64}}
# ndim of an OffsetArray should match that of the parent
@test_throws TypeError OffsetArray{Float64,3,Matrix{Float64}}

# should throw a TypeError if the offsets can not be converted to Ints
@test_throws TypeError OffsetVector{Int,Vector{Int}}(zeros(Int,2), (WeirdInteger(1),))
end

@testset "custom range types" begin
Expand Down

0 comments on commit b20b4db

Please sign in to comment.