Skip to content

Commit

Permalink
allow format for zero length linrange (#392)
Browse files Browse the repository at this point in the history
* allow format for zero length linrange

* test zero length LinRange
  • Loading branch information
rafaqz authored Aug 14, 2022
1 parent 2ec0e0b commit 509160b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Dimensions/format.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ function _format(span::Regular, D::Type, index::AbstractRange)
step(span) isa Number && !(step(span) step(index)) && _steperror(index, span)
return span
end
function _format(span::Regular, D::Type, index::LinRange{T}) where T
step(span) isa Number && step(index) > zero(T) && !(step(span) step(index)) && _steperror(index, span)
return span
end
_format(span::Irregular{AutoBounds}, D, index) = Irregular(nothing, nothing)
_format(span::Irregular{<:Tuple}, D, index) = span
_format(span::Explicit, D, index) = span
Expand Down
7 changes: 7 additions & 0 deletions test/format.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ end
Sampled(A, Unordered(), Irregular(nothing, nothing), Points(), NoMetadata())
end

@testset "Zero length LinRange is handled" begin
l = LinRange(2.0, 3.0, 2)
@test format(l, X, Base.OneTo(2)) === Sampled(l, ForwardOrdered(), Regular(1.0), Points(), NoMetadata())
l = LinRange(2.0, 2.0, 1)
@test format(l, X, Base.OneTo(2)) === Sampled(l, ForwardOrdered(), Regular(0.0), Points(), NoMetadata())
end

end

0 comments on commit 509160b

Please sign in to comment.