Skip to content

Commit 47722cc

Browse files
committed
Add extra tests for stable sorting algorithms
1 parent 72a9550 commit 47722cc

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

test/runtests.jl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ using Test
33
using StatsBase
44
using Random
55

6+
stable_algorithms = [TimSort, RadixSort, PagedMergeSort, ThreadedPagedMergeSort]
7+
unstable_algorithms = [HeapSort, CombSort]
8+
69
a = rand(1:10000, 1000)
710

8-
for alg in [TimSort, HeapSort, RadixSort, CombSort, PagedMergeSort, ThreadedPagedMergeSort]
11+
for alg in [stable_algorithms; unstable_algorithms]
912
b = sort(a, alg=alg)
1013
@test issorted(b)
1114
ix = sortperm(a, alg=alg)
@@ -73,8 +76,7 @@ for n in [0:10..., 100, 101, 1000, 1001]
7376
invpermute!(c, pi)
7477
@test c == v
7578

76-
# stable algorithms
77-
for alg in [TimSort, RadixSort, PagedMergeSort, ThreadedPagedMergeSort]
79+
for alg in stable_algorithms
7880
p = sortperm(v, alg=alg, order=ord)
7981
@test p == pi
8082
s = copy(v)
@@ -84,8 +86,7 @@ for n in [0:10..., 100, 101, 1000, 1001]
8486
@test s == v
8587
end
8688

87-
# unstable algorithms
88-
for alg in [HeapSort, CombSort]
89+
for alg in unstable_algorithms
8990
p = sortperm(v, alg=alg, order=ord)
9091
@test isperm(p)
9192
@test v[p] == si
@@ -99,7 +100,7 @@ for n in [0:10..., 100, 101, 1000, 1001]
99100

100101
v = randn_with_nans(n,0.1)
101102
for ord in [Base.Order.Forward, Base.Order.Reverse],
102-
alg in [TimSort, HeapSort, RadixSort, CombSort, PagedMergeSort, ThreadedPagedMergeSort]
103+
alg in [stable_algorithms; unstable_algorithms]
103104
# test float sorting with NaNs
104105
s = sort(v, alg=alg, order=ord)
105106
@test issorted(s, order=ord)
@@ -117,3 +118,14 @@ for n in [0:10..., 100, 101, 1000, 1001]
117118
@test reinterpret(UInt64,vp) == reinterpret(UInt64,s)
118119
end
119120
end
121+
122+
for T in (Float64, Int, UInt8)
123+
for alg in stable_algorithms
124+
for ord in [Base.Order.By(identity), Base.Order.By(_ -> 0), Base.Order.By(Base.Fix2(÷, 100))]
125+
for n in vcat(0:31, 40:11:100, 110:51:1000)
126+
v = sort(rand(T, n))
127+
@test v == sort(v; alg=alg, order=ord)
128+
end
129+
end
130+
end
131+
end

0 commit comments

Comments
 (0)