Skip to content

Commit d9b5089

Browse files
authored
Improve LazyAxis test coverage (#311)
* Improve LazyAxis test coverage
1 parent c0046f8 commit d9b5089

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "6.0.15"
3+
version = "6.0.16"
44

55
[deps]
66
ArrayInterfaceCore = "30b0a656-2188-435a-8636-2ec0e6a096e2"

src/axes.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ struct LazyAxis{N,P} <: AbstractUnitRange{Int}
191191
end
192192

193193
@inline Base.parent(x::LazyAxis{N,P}) where {N,P} = axes(getfield(x, :parent), static(N))
194-
@inline function Base.parent(x::LazyAxis{:,P}) where {P}
195-
return eachindex(IndexLinear(), getfield(x, :parent))
196-
end
194+
@inline Base.parent(x::LazyAxis{:,P}) where {P} = eachindex(IndexLinear(), getfield(x, :parent))
197195

198196
@inline parent_type(::Type{LazyAxis{N,P}}) where {N,P} = axes_types(P, static(N))
199197
# TODO this approach to parent_type(::Type{LazyAxis{:}}) is a bit hacky. Something like
@@ -229,7 +227,8 @@ end
229227
ArrayInterfaceCore.known_last(::Type{LazyAxis{N,P}}) where {N,P} = known_last(axes_types(P, static(N)))
230228
ArrayInterfaceCore.known_last(::Type{LazyAxis{:,P}}) where {P} = known_length(P)
231229
Base.last(x::LazyAxis) = _last(known_last(x), x)
232-
_last(::Nothing, x) = last(parent(x))
230+
_last(::Nothing, x::LazyAxis{:}) = lastindex(getfield(x, :parent))
231+
_last(::Nothing, x::LazyAxis{N}) where {N} = lastindex(getfield(x, :parent), N)
233232
_last(N::Int, x) = N
234233

235234
known_length(::Type{<:LazyAxis{:,P}}) where {P} = known_length(P)
@@ -242,7 +241,8 @@ Base.axes1(x::LazyAxis) = x
242241
Base.axes(x::Slice{<:LazyAxis}) = (Base.axes1(x),)
243242
# assuming that lazy loaded params like dynamic length from `size(::Array, dim)` are going
244243
# be used again later with `Slice{LazyAxis}`, we quickly load indices
245-
Base.axes1(x::Slice{<:LazyAxis}) = indices(parent(x.indices))
244+
Base.axes1(x::Slice{LazyAxis{N,A}}) where {N,A} = indices(getfield(x.indices, :parent), StaticInt(N))
245+
Base.axes1(x::Slice{LazyAxis{:,A}}) where {A} = indices(getfield(x.indices, :parent))
246246
Base.to_shape(x::LazyAxis) = length(x)
247247

248248
@propagate_inbounds function Base.getindex(x::LazyAxis, i::CanonicalInt)

test/axes.jl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,27 @@ m = Array{Float64}(undef, 4, 3)
1111
@testset "LazyAxis" begin
1212
A = zeros(3,4,5);
1313
SA = MArray(zeros(3,4,5))
14+
DA = MArray(zeros(3,4,5), LinearIndices((Base.Slice(1:3), 1:4, 1:5)))
1415
lz1 = ArrayInterface.LazyAxis{1}(A)
1516
slz1 = ArrayInterface.LazyAxis{1}(SA)
17+
dlz1 = ArrayInterface.LazyAxis{1}(DA)
1618
lzc = ArrayInterface.LazyAxis{:}(A)
1719
slzc = ArrayInterface.LazyAxis{:}(SA)
18-
19-
@test @inferred(first(lz1)) === @inferred(first(slz1))
20-
@test @inferred(first(lzc)) === @inferred(first(slzc))
21-
@test @inferred(last(lz1)) === @inferred(last(slz1))
22-
@test @inferred(last(lzc)) === @inferred(last(slzc))
23-
@test @inferred(length(lz1)) === @inferred(length(slz1))
24-
@test @inferred(length(lzc)) === @inferred(length(slzc))
25-
@test @inferred(Base.to_shape(lzc)) == length(slzc)
20+
dlzc = ArrayInterface.LazyAxis{:}(DA)
21+
22+
@test @inferred(first(lz1)) === @inferred(first(slz1)) === @inferred(first(dlz1))
23+
@test @inferred(first(lzc)) === @inferred(first(slzc)) === @inferred(first(dlzc))
24+
@test @inferred(last(lz1)) === @inferred(last(slz1)) === @inferred(last(dlz1))
25+
@test @inferred(last(lzc)) === @inferred(last(slzc)) === @inferred(last(dlzc))
26+
@test @inferred(length(lz1)) === @inferred(length(slz1)) === @inferred(length(dlz1))
27+
@test @inferred(length(lzc)) === @inferred(length(slzc)) === @inferred(length(dlzc))
28+
@test @inferred(Base.to_shape(lzc)) == length(slzc) == length(dlzc)
2629
@test @inferred(Base.checkindex(Bool, lzc, 1)) & @inferred(Base.checkindex(Bool, slzc, 1))
2730
@test axes(lzc)[1] == Base.axes1(lzc) == axes(Base.Slice(lzc))[1] == Base.axes1(Base.Slice(lzc))
31+
@test keys(axes(A, 1)) == @inferred(keys(lz1))
32+
33+
@test @inferred(ArrayInterface.known_first(slzc)) === 1
34+
@test @inferred(ArrayInterface.known_length(slz1)) === 3
2835

2936
@test @inferred(getindex(lz1, 2)) == 2
3037
@test @inferred(getindex(lz1, 1:2)) == 1:2
@@ -33,6 +40,8 @@ m = Array{Float64}(undef, 4, 3)
3340
@test @inferred(ArrayInterface.parent_type(ArrayInterface.LazyAxis{:}(A))) <: Base.OneTo{Int}
3441
@test @inferred(ArrayInterface.parent_type(ArrayInterface.LazyAxis{4}(SA))) <: ArrayInterface.SOneTo{1}
3542
@test @inferred(ArrayInterface.parent_type(ArrayInterface.LazyAxis{:}(SA))) <: ArrayInterface.SOneTo{60}
43+
@test @inferred(IndexStyle(SA)) isa IndexLinear
44+
@test @inferred(IndexStyle(DA)) isa IndexLinear
3645
@test ArrayInterface.can_change_size(ArrayInterface.LazyAxis{1,Vector{Any}})
3746

3847
Aperm = PermutedDimsArray(A, (3,1,2))

0 commit comments

Comments
 (0)