Skip to content

Commit c1b0257

Browse files
oxinaboxKristofferC
authored andcommitted
add missing costructor for ReverseOrdering() and tidy sort tests (#33736)
1 parent 8f11314 commit c1b0257

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

base/ordering.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ end
2727

2828
ReverseOrdering(rev::ReverseOrdering) = rev.fwd
2929
ReverseOrdering(fwd::Fwd) where {Fwd} = ReverseOrdering{Fwd}(fwd)
30+
ReverseOrdering() = ReverseOrdering(ForwardOrdering())
3031

3132
const DirectOrdering = Union{ForwardOrdering,ReverseOrdering{ForwardOrdering}}
3233

3334
const Forward = ForwardOrdering()
34-
const Reverse = ReverseOrdering(Forward)
35+
const Reverse = ReverseOrdering()
3536

3637
struct By{T} <: Ordering
3738
by::T

test/sorting.jl

+49-32
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,62 @@
22

33
module SortingTests
44

5-
using Base.Order: Forward
5+
using Base.Order
66
using Random
77
using Test
88

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
1912
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
3429
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
4058
end
59+
@test_throws ArgumentError partialsortperm!([1,2], [2,3,1], 1:2)
4160
end
42-
@test_throws ArgumentError partialsortperm!([1,2], [2,3,1], 1:2)
43-
@test sum(randperm(6)) == 21
4461

4562
@testset "searchsorted" begin
4663
numTypes = [ Int8, Int16, Int32, Int64, Int128,

0 commit comments

Comments
 (0)