Skip to content

Commit

Permalink
test invalid lt to close #11429
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilith Hafner authored and Lilith Hafner committed May 8, 2022
1 parent afbed36 commit 115f851
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -852,4 +852,33 @@ end
end
end

@testset "invalid lt (#11429)" begin
# lt must be a total order (i.e. < not <=) so this usage is
# invalid. Consequently, none of the behavior tested in this
# testset is gaurunteed to work in future minor versions of Julia.

n = 1000
a = rand(1:5, n);
s = sort(a);

# Nevertheless, it still works...
for alg in [InsertionSort, MergeSort, QuickSort,
Base.Sort.AdaptiveSort, Base.DEFAULT_STABLE, Base.DEFAULT_UNSTABLE]
@test sort(a, alg=alg, lt = <=) == s
end
@test partialsort(a, 172, lt = <=) == s[172]
@test partialsort(a, 315:415, lt = <=) == s[315:415]

# ...and it is consistantly reverse stable. All these algorithms swap v[i] and v[j]
# where i < j if and only if lt(o, v[j], v[i]). This invariant holds even for
# this invalid lt order.
s = reverse(sortperm(a, rev=true))
for alg in [InsertionSort, MergeSort, QuickSort,
Base.Sort.AdaptiveSort, Base.DEFAULT_STABLE, Base.DEFAULT_UNSTABLE]
@test sort(1:n, alg=alg, lt = (i,j) -> a[i]<=a[j]) == s
end
@test partialsort(1:n, 172, lt = (i,j) -> a[i]<=a[j]) == s[172]
@test partialsort(1:n, 315:415, lt = (i,j) -> a[i]<=a[j]) == s[315:415]
end

end

0 comments on commit 115f851

Please sign in to comment.