Skip to content

Commit 08f969b

Browse files
authored
eltype and ndims of an OffsetArray matches that of the parent (#163)
1 parent 87666ee commit 08f969b

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/OffsetArrays.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ julia> OffsetArray(a, OffsetArrays.Origin(0)) # short notation for `Origin(0, ..
8989
9090
9191
"""
92-
struct OffsetArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N}
92+
struct OffsetArray{T,N,AA<:AbstractArray{T,N}} <: AbstractArray{T,N}
9393
parent::AA
9494
offsets::NTuple{N,Int}
95-
function OffsetArray{T, N, AA}(parent::AA, offsets::NTuple{N, Int}) where {T, N, AA<:AbstractArray}
95+
function OffsetArray{T, N, AA}(parent::AA, offsets::NTuple{N, Int}) where {T, N, AA<:AbstractArray{T,N}}
9696
@boundscheck overflow_check.(axes(parent), offsets)
9797
new{T, N, AA}(parent, offsets)
9898
end
@@ -103,14 +103,14 @@ end
103103
104104
Type alias and convenience constructor for one-dimensional [`OffsetArray`](@ref)s.
105105
"""
106-
const OffsetVector{T,AA<:AbstractArray} = OffsetArray{T,1,AA}
106+
const OffsetVector{T,AA<:AbstractVector{T}} = OffsetArray{T,1,AA}
107107

108108
"""
109109
OffsetMatrix(A, index1, index2)
110110
111111
Type alias and convenience constructor for two-dimensional [`OffsetArray`](@ref)s.
112112
"""
113-
const OffsetMatrix{T,AA<:AbstractArray} = OffsetArray{T,2,AA}
113+
const OffsetMatrix{T,AA<:AbstractMatrix{T}} = OffsetArray{T,2,AA}
114114

115115
function overflow_check(r, offset::T) where T
116116
# This gives some performance boost https://github.com/JuliaLang/julia/issues/33273

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ end
283283
@test_throws ArgumentError OffsetVector(zeros(2:3,2:3), (2, 4))
284284
@test_throws ArgumentError OffsetVector(zeros(2:3,2:3), ())
285285
@test_throws ArgumentError OffsetVector(zeros(2:3,2:3))
286+
287+
# eltype of an OffsetArray should match that of the parent (issue #162)
288+
@test_throws TypeError OffsetVector{Float64,Vector{ComplexF64}}
289+
# ndim of an OffsetArray should match that of the parent
290+
@test_throws TypeError OffsetVector{Float64,Matrix{Float64}}
286291
end
287292

288293
@testset "OffsetMatrix" begin
@@ -416,6 +421,11 @@ end
416421
@test_throws ArgumentError OffsetMatrix(zeros(2:3, 1:2, 1:2), 2,0,0)
417422
@test_throws ArgumentError OffsetMatrix(zeros(2:3, 1:2, 1:2), ())
418423
@test_throws ArgumentError OffsetMatrix(zeros(2:3, 1:2, 1:2))
424+
425+
# eltype of an OffsetArray should match that of the parent (issue #162)
426+
@test_throws TypeError OffsetMatrix{Float64,Matrix{ComplexF64}}
427+
# ndim of an OffsetArray should match that of the parent
428+
@test_throws TypeError OffsetMatrix{Float64,Vector{Float64}}
419429
end
420430

421431
# no need to duplicate the 2D case here,
@@ -466,6 +476,11 @@ end
466476
@test eltype(a) == Bool
467477
end
468478
end
479+
480+
# eltype of an OffsetArray should match that of the parent (issue #162)
481+
@test_throws TypeError OffsetArray{Float64,2,Matrix{ComplexF64}}
482+
# ndim of an OffsetArray should match that of the parent
483+
@test_throws TypeError OffsetArray{Float64,3,Matrix{Float64}}
469484
end
470485

471486
@testset "custom range types" begin

0 commit comments

Comments
 (0)