Skip to content

Commit

Permalink
views for CartesianIndex, inferred in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed May 12, 2021
1 parent 673adf4 commit 2724123
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,7 @@ end
# wrap elements in Scalar to be consistent with 0D views
_maybewrapscalar(S::SArray{<:Any,T}, r::T) where {T} = Scalar{T}(r)
_maybewrapscalar(S, r) = r
Base.view(S::SArray, I::Union{Colon, Integer, SOneTo, StaticArray{<:Tuple, Int}}...) = _maybewrapscalar(S, getindex(S, I...))
function Base.view(S::SArray, I::Union{Colon, Integer, SOneTo, StaticArray{<:Tuple, Int}, CartesianIndex}...)
V = getindex(S, I...)
_maybewrapscalar(S, V)
end
15 changes: 9 additions & 6 deletions test/SArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,15 @@

@test Base.dataids(m) === ()

@test (@view m[:, :]) === m
@test (@view m[:, 1]) === @SArray [11, 12]
@test (@view m[SVector{2,Int}(1,2), 1]) === @SArray [11, 12]
@test (@view m[SMatrix{2,2,Int}(1,2,3,4)]) === m
@test (@view m[SOneTo(2), 1]) === @SArray [11, 12]
@test (@view m[1, 1])[] === m[1, 1]
@test (@inferred view(m, :, :)) === m
@test (@inferred view(m, :, 1)) === @SArray [11, 12]
@test (@inferred view(m, SVector{2,Int}(1,2), 1)) === @SArray [11, 12]
@test (@inferred view(m, SMatrix{2,2,Int}(1,2,3,4))) === m
@test (@inferred view(m, SOneTo(2), 1)) === @SArray [11, 12]
@test (@inferred view(m, 1, 1)) === Scalar(m[1, 1])
@test (@inferred view(m, CartesianIndex(1, 1))) === Scalar(m[1, 1])
@test (@inferred view(m, CartesianIndex(1, 1, 1))) === Scalar(m[1, 1])
@test (@inferred view(m, 1, 1, CartesianIndex(1))) === Scalar(m[1, 1])
end

@testset "promotion" begin
Expand Down
8 changes: 8 additions & 0 deletions test/SVector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@
@test length(v) === 3

@test_throws Exception v[1] = 1

@test (@inferred view(v, :)) === v
@test (@inferred view(v, 1)) === Scalar(v[1])
@test (@inferred view(v, CartesianIndex(1))) === Scalar(v[1])
@test (@inferred view(v, CartesianIndex(1,1))) === Scalar(v[1])
@test (@inferred view(v, 1, CartesianIndex(1))) === Scalar(v[1])
@test (@inferred view(v, SVector{2,Int}(1,2))) === @SArray [11, 12]
@test (@inferred view(v, SOneTo(2))) === @SArray [11, 12]
end

@testset "CartesianIndex" begin
Expand Down

0 comments on commit 2724123

Please sign in to comment.