Skip to content

Commit 115f851

Browse files
Lilith HafnerLilith Hafner
authored andcommitted
test invalid lt to close #11429
1 parent afbed36 commit 115f851

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/sorting.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,4 +852,33 @@ end
852852
end
853853
end
854854

855+
@testset "invalid lt (#11429)" begin
856+
# lt must be a total order (i.e. < not <=) so this usage is
857+
# invalid. Consequently, none of the behavior tested in this
858+
# testset is gaurunteed to work in future minor versions of Julia.
859+
860+
n = 1000
861+
a = rand(1:5, n);
862+
s = sort(a);
863+
864+
# Nevertheless, it still works...
865+
for alg in [InsertionSort, MergeSort, QuickSort,
866+
Base.Sort.AdaptiveSort, Base.DEFAULT_STABLE, Base.DEFAULT_UNSTABLE]
867+
@test sort(a, alg=alg, lt = <=) == s
868+
end
869+
@test partialsort(a, 172, lt = <=) == s[172]
870+
@test partialsort(a, 315:415, lt = <=) == s[315:415]
871+
872+
# ...and it is consistantly reverse stable. All these algorithms swap v[i] and v[j]
873+
# where i < j if and only if lt(o, v[j], v[i]). This invariant holds even for
874+
# this invalid lt order.
875+
s = reverse(sortperm(a, rev=true))
876+
for alg in [InsertionSort, MergeSort, QuickSort,
877+
Base.Sort.AdaptiveSort, Base.DEFAULT_STABLE, Base.DEFAULT_UNSTABLE]
878+
@test sort(1:n, alg=alg, lt = (i,j) -> a[i]<=a[j]) == s
879+
end
880+
@test partialsort(1:n, 172, lt = (i,j) -> a[i]<=a[j]) == s[172]
881+
@test partialsort(1:n, 315:415, lt = (i,j) -> a[i]<=a[j]) == s[315:415]
882+
end
883+
855884
end

0 commit comments

Comments
 (0)