From 6577f3f85a574a9b63f8b0cd44c56e6438455eab Mon Sep 17 00:00:00 2001 From: jishnub Date: Mon, 18 Jan 2021 16:01:18 +0400 Subject: [PATCH] Type assertions in converting offsets --- src/OffsetArrays.jl | 2 +- test/runtests.jl | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/OffsetArrays.jl b/src/OffsetArrays.jl index b57b27ec..582661dd 100644 --- a/src/OffsetArrays.jl +++ b/src/OffsetArrays.jl @@ -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 """ diff --git a/test/runtests.jl b/test/runtests.jl index dcfff33a..91571549 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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