Skip to content

Commit b20b4db

Browse files
authored
Type assertions in converting offsets (#191)
1 parent e65fd01 commit b20b4db

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/OffsetArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct OffsetArray{T,N,AA<:AbstractArray{T,N}} <: AbstractArray{T,N}
118118
end
119119

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

124124
"""

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ end
185185
@test first(rred) == first(r)
186186
end
187187

188+
# used in testing the constructor
189+
struct WeirdInteger{T} <: Integer
190+
x :: T
191+
end
192+
# assume that it doesn't behave as expected
193+
Base.convert(::Type{Int}, a::WeirdInteger) = a
194+
188195
@testset "Constructors" begin
189196
@testset "Single-entry arrays in dims 0:5" begin
190197
for n = 0:5
@@ -535,6 +542,9 @@ end
535542
@test_throws TypeError OffsetArray{Float64,2,Matrix{ComplexF64}}
536543
# ndim of an OffsetArray should match that of the parent
537544
@test_throws TypeError OffsetArray{Float64,3,Matrix{Float64}}
545+
546+
# should throw a TypeError if the offsets can not be converted to Ints
547+
@test_throws TypeError OffsetVector{Int,Vector{Int}}(zeros(Int,2), (WeirdInteger(1),))
538548
end
539549

540550
@testset "custom range types" begin

0 commit comments

Comments
 (0)