Skip to content

Commit

Permalink
fix #35272, searchsorted(3:-1:1, 2.5, rev=true) (#35274)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet authored and KristofferC committed Apr 11, 2020
1 parent 8628143 commit a741865
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 10 additions & 2 deletions base/sort.jl
Expand Up @@ -262,7 +262,11 @@ function searchsortedlast(a::AbstractRange{<:Integer}, x::Real, o::DirectOrderin
elseif h < 0 && x <= last(a)
lastindex(a)
else
fld(floor(Integer, x) - first(a), h) + 1
if o isa ForwardOrdering
fld(floor(Integer, x) - first(a), h) + 1
else
fld(ceil(Integer, x) - first(a), h) + 1
end
end
end

Expand All @@ -280,7 +284,11 @@ function searchsortedfirst(a::AbstractRange{<:Integer}, x::Real, o::DirectOrderi
elseif h < 0 && x < last(a)
lastindex(a) + 1
else
-fld(floor(Integer, -x) + first(a), h) + 1
if o isa ForwardOrdering
-fld(floor(Integer, -x) + first(a), h) + 1
else
-fld(ceil(Integer, -x) + first(a), h) + 1
end
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/sorting.jl
Expand Up @@ -177,6 +177,17 @@ end
end
end
end
@testset "issue #35272" begin
for v0 = (3:-1:1, 3.0:-1.0:1.0), v = (v0, collect(v0))
@test searchsorted(v, 3, rev=true) == 1:1
@test searchsorted(v, 3.0, rev=true) == 1:1
@test searchsorted(v, 2.5, rev=true) == 2:1
@test searchsorted(v, 2, rev=true) == 2:2
@test searchsorted(v, 1.2, rev=true) == 3:2
@test searchsorted(v, 1, rev=true) == 3:3
@test searchsorted(v, 0.1, rev=true) == 4:3
end
end
end
# exercise the codepath in searchsorted* methods for ranges that check for zero step range
struct ConstantRange{T} <: AbstractRange{T}
Expand Down

0 comments on commit a741865

Please sign in to comment.