# This file is a part of Julia. License is MIT: http://julialang.org/license using Base.Order: Forward @test sort([2,3,1]) == [1,2,3] @test sort([2,3,1], rev=true) == [3,2,1] @test sort(['z':-1:'a';]) == ['a':'z';] @test sort(['a':'z';], rev=true) == ['z':-1:'a';] @test sortperm([2,3,1]) == [3,1,2] @test sortperm!([1,2,3], [2,3,1]) == [3,1,2] let s = sub([1,2,3,4], 1:3) r = sortperm!(s, [2,3,1]) @test r == [3,1,2] @test r === s end @test_throws ArgumentError sortperm!(sub([1,2,3,4], 1:4), [2,3,1]) @test !issorted([2,3,1]) @test issorted([1,2,3]) @test reverse([2,3,1]) == [1,3,2] @test select([3,6,30,1,9],3) == 6 @test select([3,6,30,1,9],3:4) == [6,9] @test selectperm([3,6,30,1,9], 3:4) == [2,5] @test selectperm!(collect(1:5), [3,6,30,1,9], 3:4) == [2,5] let a=[1:10;] for r in Any[2:4, 1:2, 10:10, 4:2, 2:1, 4:-1:2, 2:-1:1, 10:-1:10, 4:1:3, 1:2:8, 10:-3:1] @test select(a, r) == [r;] @test selectperm(a, r) == [r;] @test select(a, r, rev=true) == (11 .- [r;]) @test selectperm(a, r, rev=true) == (11 .- [r;]) end end @test sum(randperm(6)) == 21 numTypes = [ Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128, Float16, Float32, Float64, BigInt, BigFloat] @test searchsorted([1:10;], 1, by=(x -> x >= 5)) == 1:4 @test searchsorted([1:10;], 10, by=(x -> x >= 5)) == 5:10 @test searchsorted([1:5; 1:5; 1:5], 1, 6, 10, Forward) == 6:6 @test searchsorted(ones(15), 1, 6, 10, Forward) == 6:10 for R in numTypes, T in numTypes @test searchsorted(R[1, 1, 2, 2, 3, 3], T(0)) == 1:0 @test searchsorted(R[1, 1, 2, 2, 3, 3], T(1)) == 1:2 @test searchsorted(R[1, 1, 2, 2, 3, 3], T(2)) == 3:4 @test searchsorted(R[1, 1, 2, 2, 3, 3], T(4)) == 7:6 @test searchsorted(R[1, 1, 2, 2, 3, 3], 2.5) == 5:4 @test searchsorted(1:3, T(0)) == 1:0 @test searchsorted(1:3, T(1)) == 1:1 @test searchsorted(1:3, T(2)) == 2:2 @test searchsorted(1:3, T(4)) == 4:3 @test searchsorted(R[1:10;], T(1), by=(x -> x >= 5)) == 1:4 @test searchsorted(R[1:10;], T(10), by=(x -> x >= 5)) == 5:10 @test searchsorted(R[1:5; 1:5; 1:5], T(1), 6, 10, Forward) == 6:6 @test searchsorted(ones(R, 15), T(1), 6, 10, Forward) == 6:10 end for (rg,I) in [(49:57,47:59), (1:2:17,-1:19), (-3:0.5:2,-5:.5:4)] rg_r = reverse(rg) rgv, rgv_r = [rg;], [rg_r;] for i = I @test searchsorted(rg,i) == searchsorted(rgv,i) @test searchsorted(rg_r,i,rev=true) == searchsorted(rgv_r,i,rev=true) end end rg = 0.0:0.01:1.0 for i = 2:101 @test searchsorted(rg, rg[i]) == i:i @test searchsorted(rg, prevfloat(rg[i])) == i:i-1 @test searchsorted(rg, nextfloat(rg[i])) == i+1:i end rg_r = reverse(rg) for i = 1:100 @test searchsorted(rg_r, rg_r[i], rev=true) == i:i @test searchsorted(rg_r, prevfloat(rg_r[i]), rev=true) == i+1:i @test searchsorted(rg_r, nextfloat(rg_r[i]), rev=true) == i:i-1 end @test searchsorted(1:10, 1, by=(x -> x >= 5)) == searchsorted([1:10;], 1, by=(x -> x >= 5)) @test searchsorted(1:10, 10, by=(x -> x >= 5)) == searchsorted([1:10;], 10, by=(x -> x >= 5)) @test searchsorted([], 0) == 1:0 @test searchsorted([1,2,3], 0) == 1:0 @test searchsorted([1,2,3], 4) == 4:3 # issue 8866 @test searchsortedfirst(500:1.0:600, -1.0e20) == 1 @test searchsortedfirst(500:1.0:600, 1.0e20) == 102 @test searchsortedlast(500:1.0:600, -1.0e20) == 0 @test searchsortedlast(500:1.0:600, 1.0e20) == 101 # exercise the codepath in searchsorted* methods for ranges that check for zero step range immutable ConstantRange{T} <: Range{T} val::T len::Int end Base.length(r::ConstantRange) = r.len Base.getindex(r::ConstantRange, i::Int) = (1 <= i <= r.len || throw(BoundsError(r,i)); r.val) Base.step(r::ConstantRange) = 0 r = ConstantRange(1, 5) @test searchsortedfirst(r, 1.0, Forward) == 1 @test searchsortedfirst(r, 1, Forward) == 1 @test searchsortedfirst(r, UInt(1), Forward) == 1 @test searchsortedlast(r, 1.0, Forward) == 5 @test searchsortedlast(r, 1, Forward) == 5 @test searchsortedlast(r, UInt(1), Forward) == 5 a = rand(1:10000, 1000) for alg in [InsertionSort, MergeSort] b = sort(a, alg=alg) @test issorted(b) ix = sortperm(a, alg=alg) b = a[ix] @test issorted(b) @test a[ix] == b sortperm!(sub(ix, 1:100), sub(a, 1:100), alg=alg) b = a[ix][1:100] @test issorted(b) sortperm!(ix, a, alg=alg) b = a[ix] @test issorted(b) @test a[ix] == b b = sort(a, alg=alg, rev=true) @test issorted(b, rev=true) ix = sortperm(a, alg=alg, rev=true) b = a[ix] @test issorted(b, rev=true) @test a[ix] == b sortperm!(sub(ix, 1:100), sub(a, 1:100), alg=alg, rev=true) b = a[ix][1:100] @test issorted(b, rev=true) sortperm!(ix, a, alg=alg, rev=true) b = a[ix] @test issorted(b, rev=true) @test a[ix] == b b = sort(a, alg=alg, by=x->1/x) @test issorted(b, by=x->1/x) ix = sortperm(a, alg=alg, by=x->1/x) b = a[ix] @test issorted(b, by=x->1/x) @test a[ix] == b sortperm!(sub(ix, 1:100), sub(a, 1:100), by=x->1/x) b = a[ix][1:100] @test issorted(b, by=x->1/x) sortperm!(ix, a, alg=alg, by=x->1/x) b = a[ix] @test issorted(b, by=x->1/x) @test a[ix] == b c = copy(a) permute!(c, ix) @test c == b ipermute!(c, ix) @test c == a c = sort(a, alg=alg, lt=(>)) @test b == c c = sort(a, alg=alg, by=x->1/x) @test b == c end # unstable algorithms for alg in [QuickSort, PartialQuickSort(length(a))] b = sort(a, alg=alg) @test issorted(b) b = sort(a, alg=alg, rev=true) @test issorted(b, rev=true) b = sort(a, alg=alg, by=x->1/x) @test issorted(b, by=x->1/x) end # test PartialQuickSort only does a partial sort let alg = PartialQuickSort(div(length(a), 10)) k = alg.k b = sort(a, alg=alg) c = sort(a, alg=alg, by=x->1/x) d = sort(a, alg=alg, rev=true) @test issorted(b[1:k]) @test issorted(c[1:k], by=x->1/x) @test issorted(d[1:k], rev=true) @test !issorted(b) @test !issorted(c, by=x->1/x) @test !issorted(d, rev=true) end @test select([3,6,30,1,9], 2, rev=true) == 9 @test select([3,6,30,1,9], 2, by=x->1/x) == 9 @test selectperm([3,6,30,1,9], 2, rev=true) == 5 @test selectperm([3,6,30,1,9], 2, by=x->1/x) == 5 ## more advanced sorting tests ## randnans(n) = reinterpret(Float64,[rand(UInt64)|0x7ff8000000000000 for i=1:n]) function randn_with_nans(n,p) v = randn(n) x = find(rand(n).