|
2 | 2 |
|
3 | 3 | module SortingTests
|
4 | 4 |
|
5 |
| -using Base.Order: Forward |
| 5 | +using Base.Order |
6 | 6 | using Random
|
7 | 7 | using Test
|
8 | 8 |
|
9 |
| -@test sort([2,3,1]) == [1,2,3] |
10 |
| -@test sort([2,3,1], rev=true) == [3,2,1] |
11 |
| -@test sort(['z':-1:'a';]) == ['a':'z';] |
12 |
| -@test sort(['a':'z';], rev=true) == ['z':-1:'a';] |
13 |
| -@test sortperm([2,3,1]) == [3,1,2] |
14 |
| -@test sortperm!([1,2,3], [2,3,1]) == [3,1,2] |
15 |
| -let s = view([1,2,3,4], 1:3), |
16 |
| - r = sortperm!(s, [2,3,1]) |
17 |
| - @test r == [3,1,2] |
18 |
| - @test r === s |
| 9 | +@testset "Order" begin |
| 10 | + @test Forward == ForwardOrdering() |
| 11 | + @test ReverseOrdering(Forward) == ReverseOrdering() == Reverse |
19 | 12 | end
|
20 |
| -@test_throws ArgumentError sortperm!(view([1,2,3,4], 1:4), [2,3,1]) |
21 |
| -@test !issorted([2,3,1]) |
22 |
| -@test issorted([1,2,3]) |
23 |
| -@test reverse([2,3,1]) == [1,3,2] |
24 |
| -@test partialsort([3,6,30,1,9],3) == 6 |
25 |
| -@test partialsort([3,6,30,1,9],3:4) == [6,9] |
26 |
| -@test partialsortperm([3,6,30,1,9], 3:4) == [2,5] |
27 |
| -@test partialsortperm!(Vector(1:5), [3,6,30,1,9], 3:4) == [2,5] |
28 |
| -let a=[1:10;] |
29 |
| - 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, UInt(2):UInt(5)] |
30 |
| - @test partialsort(a, r) == [r;] |
31 |
| - @test partialsortperm(a, r) == [r;] |
32 |
| - @test partialsort(a, r, rev=true) == (11 .- [r;]) |
33 |
| - @test partialsortperm(a, r, rev=true) == (11 .- [r;]) |
| 13 | + |
| 14 | + |
| 15 | +@testset "sort" begin |
| 16 | + @test sort([2,3,1]) == [1,2,3] == sort([2,3,1]; order=Forward) |
| 17 | + @test sort([2,3,1], rev=true) == [3,2,1] == sort([2,3,1], order=Reverse) |
| 18 | + @test sort(['z':-1:'a';]) == ['a':'z';] |
| 19 | + @test sort(['a':'z';], rev=true) == ['z':-1:'a';] |
| 20 | +end |
| 21 | + |
| 22 | +@testset "sortperm" begin |
| 23 | + @test sortperm([2,3,1]) == [3,1,2] |
| 24 | + @test sortperm!([1,2,3], [2,3,1]) == [3,1,2] |
| 25 | + let s = view([1,2,3,4], 1:3), |
| 26 | + r = sortperm!(s, [2,3,1]) |
| 27 | + @test r == [3,1,2] |
| 28 | + @test r === s |
34 | 29 | end
|
35 |
| - for i in (2, UInt(2), Int128(1), big(10)) |
36 |
| - @test partialsort(a, i) == i |
37 |
| - @test partialsortperm(a, i) == i |
38 |
| - @test partialsort(a, i, rev=true) == (11 - i) |
39 |
| - @test partialsortperm(a, i, rev=true) == (11 - i) |
| 30 | + @test_throws ArgumentError sortperm!(view([1,2,3,4], 1:4), [2,3,1]) |
| 31 | +end |
| 32 | + |
| 33 | +@testset "misc sorting" begin |
| 34 | + @test !issorted([2,3,1]) |
| 35 | + @test issorted([1,2,3]) |
| 36 | + @test reverse([2,3,1]) == [1,3,2] |
| 37 | + @test sum(randperm(6)) == 21 |
| 38 | +end |
| 39 | + |
| 40 | +@testset "partialsort" begin |
| 41 | + @test partialsort([3,6,30,1,9],3) == 6 |
| 42 | + @test partialsort([3,6,30,1,9],3:4) == [6,9] |
| 43 | + @test partialsortperm([3,6,30,1,9], 3:4) == [2,5] |
| 44 | + @test partialsortperm!(Vector(1:5), [3,6,30,1,9], 3:4) == [2,5] |
| 45 | + let a=[1:10;] |
| 46 | + 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, UInt(2):UInt(5)] |
| 47 | + @test partialsort(a, r) == [r;] |
| 48 | + @test partialsortperm(a, r) == [r;] |
| 49 | + @test partialsort(a, r, rev=true) == (11 .- [r;]) |
| 50 | + @test partialsortperm(a, r, rev=true) == (11 .- [r;]) |
| 51 | + end |
| 52 | + for i in (2, UInt(2), Int128(1), big(10)) |
| 53 | + @test partialsort(a, i) == i |
| 54 | + @test partialsortperm(a, i) == i |
| 55 | + @test partialsort(a, i, rev=true) == (11 - i) |
| 56 | + @test partialsortperm(a, i, rev=true) == (11 - i) |
| 57 | + end |
40 | 58 | end
|
| 59 | + @test_throws ArgumentError partialsortperm!([1,2], [2,3,1], 1:2) |
41 | 60 | end
|
42 |
| -@test_throws ArgumentError partialsortperm!([1,2], [2,3,1], 1:2) |
43 |
| -@test sum(randperm(6)) == 21 |
44 | 61 |
|
45 | 62 | @testset "searchsorted" begin
|
46 | 63 | numTypes = [ Int8, Int16, Int32, Int64, Int128,
|
|
0 commit comments