|
852 | 852 | end
|
853 | 853 | end
|
854 | 854 |
|
| 855 | +@testset "invalid lt (#11429)" begin |
| 856 | + # lt must be a total linear order (e.g. < not <=) so this usage is |
| 857 | + # not allowed. 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 | + v = rand(1:5, n); |
| 862 | + s = sort(v); |
| 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(v, alg=alg, lt = <=) == s |
| 868 | + end |
| 869 | + @test partialsort(v, 172, lt = <=) == s[172] |
| 870 | + @test partialsort(v, 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 | + perm = reverse(sortperm(v, 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) -> v[i]<=v[j]) == perm |
| 879 | + end |
| 880 | + @test partialsort(1:n, 172, lt = (i,j) -> v[i]<=v[j]) == perm[172] |
| 881 | + @test partialsort(1:n, 315:415, lt = (i,j) -> v[i]<=v[j]) == perm[315:415] |
| 882 | + |
| 883 | + # lt can be very poorly behaved and sort will still permute its input in some way. |
| 884 | + for alg in [InsertionSort, MergeSort, QuickSort, |
| 885 | + Base.Sort.AdaptiveSort, Base.DEFAULT_STABLE, Base.DEFAULT_UNSTABLE] |
| 886 | + @test sort!(sort(v, alg=alg, lt = (x,y) -> rand([false, true]))) == s |
| 887 | + end |
| 888 | + @test partialsort(v, 172, lt = (x,y) -> rand([false, true])) ∈ 1:5 |
| 889 | + @test all(partialsort(v, 315:415, lt = (x,y) -> rand([false, true])) .∈ (1:5,)) |
| 890 | +end |
| 891 | + |
855 | 892 | end
|
0 commit comments