Skip to content

Commit

Permalink
document and add test_broken on argmin of degenerate ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 committed Jan 21, 2020
1 parent e3ca9ad commit f8e839f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
19 changes: 17 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -577,20 +577,35 @@ maximum(r::AbstractUnitRange) = isempty(r) ? throw(ArgumentError("range must be
minimum(r::AbstractRange) = isempty(r) ? throw(ArgumentError("range must be non-empty")) : min(first(r), last(r))
maximum(r::AbstractRange) = isempty(r) ? throw(ArgumentError("range must be non-empty")) : max(first(r), last(r))

"""
argmin(r::AbstractRange)
Ranges can have multiple minimal elements. In that case
`argmin` will return a minimal index, but not necessarily the
first one.
"""
function argmin(r::AbstractRange)
if isempty(r)
throw(ArgumentError("range must be non-empty"))
elseif step(r) > 0
firstindex(r)
else
lastindex(r)
first(searchsorted(r, last(r)))
end
end

"""
argmax(r::AbstractRange)
Ranges can have multiple maximal elements. In that case
`argmax` will return a maximal index, but not necessarily the
first one.
"""
function argmax(r::AbstractRange)
if isempty(r)
throw(ArgumentError("range must be non-empty"))
elseif step(r) > 0
lastindex(r)
first(searchsorted(r, last(r)))
else
firstindex(r)
end
Expand Down
4 changes: 4 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,10 @@ end
@test imax === argmax(r)
@test extrema(r) === (r[imin], r[imax])
end

r = 1f8-10:1f8
@test_broken argmin(f) == argmin(collect(r))
@test_broken argmax(f) == argmax(collect(r))
end

@testset "OneTo" begin
Expand Down
5 changes: 5 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ end

end
end
@testset "issue ##34408" begin
r = 1f8-10:1f8
# collect(r) = Float32[9.999999e7, 9.999999e7, 9.999999e7, 9.999999e7, 1.0e8, 1.0e8, 1.0e8, 1.0e8, 1.0e8]
@test_broken searchsorted(collect(r)) == searchsorted(r)
end
end
end
# exercise the codepath in searchsorted* methods for ranges that check for zero step range
Expand Down

0 comments on commit f8e839f

Please sign in to comment.