Skip to content

Commit

Permalink
Add unit tests for presorted inputs to sorting (#48638)
Browse files Browse the repository at this point in the history
* Add unit tests for presorted inputs to sorting
  • Loading branch information
LilithHafner authored Feb 13, 2023
1 parent 983b007 commit 43cc94d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ function _sort!(v::AbstractVector, a::CheckSorted, o::Ordering, kw)

# For most large arrays, a reverse-sorted check is essentially free (overhead < 1%)
if hi-lo >= 500 && _issorted(v, lo, hi, ReverseOrdering(o))
# If reversing is valid, do so. This does violates stability.
# If reversing is valid, do so. This violates stability.
reverse!(v, lo, hi)
return scratch
end
Expand Down
11 changes: 11 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,17 @@ end
test_allocs()
end

@testset "Presorted and reverse-presorted" begin
for len in [7, 92, 412, 780]
x = sort(randn(len))
for _ in 1:2
@test issorted(sort(x))
@test issorted(sort(x), by=x -> x+7)
reverse!(x)
end
end
end

# This testset is at the end of the file because it is slow.
@testset "searchsorted" begin
numTypes = [ Int8, Int16, Int32, Int64, Int128,
Expand Down

0 comments on commit 43cc94d

Please sign in to comment.