Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add missing constructor for ReverseOrdering() and tidy sort tests #33736

Merged
merged 1 commit into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion base/ordering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ end

ReverseOrdering(rev::ReverseOrdering) = rev.fwd
ReverseOrdering(fwd::Fwd) where {Fwd} = ReverseOrdering{Fwd}(fwd)
ReverseOrdering() = ReverseOrdering(ForwardOrdering())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the focus of this PR


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

const Forward = ForwardOrdering()
const Reverse = ReverseOrdering(Forward)
const Reverse = ReverseOrdering()

struct By{T} <: Ordering
by::T
Expand Down
81 changes: 49 additions & 32 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,62 @@

module SortingTests

using Base.Order: Forward
using Base.Order
using Random
using Test

@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 = view([1,2,3,4], 1:3),
r = sortperm!(s, [2,3,1])
@test r == [3,1,2]
@test r === s
@testset "Order" begin
@test Forward == ForwardOrdering()
@test ReverseOrdering(Forward) == ReverseOrdering() == Reverse
end
@test_throws ArgumentError sortperm!(view([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 partialsort([3,6,30,1,9],3) == 6
@test partialsort([3,6,30,1,9],3:4) == [6,9]
@test partialsortperm([3,6,30,1,9], 3:4) == [2,5]
@test partialsortperm!(Vector(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, UInt(2):UInt(5)]
@test partialsort(a, r) == [r;]
@test partialsortperm(a, r) == [r;]
@test partialsort(a, r, rev=true) == (11 .- [r;])
@test partialsortperm(a, r, rev=true) == (11 .- [r;])


@testset "sort" begin
@test sort([2,3,1]) == [1,2,3] == sort([2,3,1]; order=Forward)
@test sort([2,3,1], rev=true) == [3,2,1] == sort([2,3,1], order=Reverse)
@test sort(['z':-1:'a';]) == ['a':'z';]
@test sort(['a':'z';], rev=true) == ['z':-1:'a';]
end

@testset "sortperm" begin
@test sortperm([2,3,1]) == [3,1,2]
@test sortperm!([1,2,3], [2,3,1]) == [3,1,2]
let s = view([1,2,3,4], 1:3),
r = sortperm!(s, [2,3,1])
@test r == [3,1,2]
@test r === s
end
for i in (2, UInt(2), Int128(1), big(10))
@test partialsort(a, i) == i
@test partialsortperm(a, i) == i
@test partialsort(a, i, rev=true) == (11 - i)
@test partialsortperm(a, i, rev=true) == (11 - i)
@test_throws ArgumentError sortperm!(view([1,2,3,4], 1:4), [2,3,1])
end

@testset "misc sorting" begin
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not extatic about this testsets name.
Should I just take these back out and maybe put them at the top?

@test !issorted([2,3,1])
@test issorted([1,2,3])
@test reverse([2,3,1]) == [1,3,2]
@test sum(randperm(6)) == 21
end

@testset "partialsort" begin
@test partialsort([3,6,30,1,9],3) == 6
@test partialsort([3,6,30,1,9],3:4) == [6,9]
@test partialsortperm([3,6,30,1,9], 3:4) == [2,5]
@test partialsortperm!(Vector(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, UInt(2):UInt(5)]
@test partialsort(a, r) == [r;]
@test partialsortperm(a, r) == [r;]
@test partialsort(a, r, rev=true) == (11 .- [r;])
@test partialsortperm(a, r, rev=true) == (11 .- [r;])
end
for i in (2, UInt(2), Int128(1), big(10))
@test partialsort(a, i) == i
@test partialsortperm(a, i) == i
@test partialsort(a, i, rev=true) == (11 - i)
@test partialsortperm(a, i, rev=true) == (11 - i)
end
end
@test_throws ArgumentError partialsortperm!([1,2], [2,3,1], 1:2)
end
@test_throws ArgumentError partialsortperm!([1,2], [2,3,1], 1:2)
@test sum(randperm(6)) == 21

@testset "searchsorted" begin
numTypes = [ Int8, Int16, Int32, Int64, Int128,
Expand Down