Skip to content

Commit

Permalink
Fix findfirst for InfStepRange for negative step (#192)
Browse files Browse the repository at this point in the history
* Fix findfirst for InfStepRange for negative step

* Use parantheses in bounds check
  • Loading branch information
jishnub committed Aug 27, 2024
1 parent d9f862f commit 847dca1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/infrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ MemoryLayout(::Type{<:AbstractInfUnitRange}) = LazyLayout()

# from array.jl
function _step_findfirst(p, r::InfStepRange{T,S}) where {T,S}
first(r) <= p.x || return nothing
(first(r) <= p.x && step(r) > zero(step(r))) || (first(r) >= p.x && step(r) < zero(step(r))) || return nothing
d = convert(S, p.x - first(r))
iszero(d % step(r)) || return nothing
return d ÷ step(r) + 1
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,16 @@ end
@test v[1:∞] == v
end

@testset "issue #180" begin
@test isnothing(findfirst(==(21), 10:-1:-∞))
@test isnothing(findfirst(==(11), 10:-1:-∞))
@test findfirst(==(9), 10:-1:-∞) == 2
r = 10:-1:-
v = -20
ind = findfirst(==(v), r)
@test r[ind] == v
end

include("test_infconv.jl")
include("test_block.jl")

Expand Down

0 comments on commit 847dca1

Please sign in to comment.