Skip to content

Commit 55bb1eb

Browse files
committed
add tests for topk
1 parent eafefa1 commit 55bb1eb

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ my_tests = ["constructors.jl",
2626
"sort.jl",
2727
"grouping.jl",
2828
"join.jl",
29-
"transpose.jl"
29+
"transpose.jl",
30+
"stats.jl"
3031
]
3132

3233
println("Running tests:")

test/stats.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@testset "topk" begin
2+
# general usage
3+
for i in 1:100
4+
x = rand(Int, 11)
5+
for j in 1:11
6+
@test partialsort(x, 1:j) == topk(x, j, rev=true)
7+
@test partialsort(x, 1:j, rev=true) == topk(x, j)
8+
@test partialsortperm(x, 1:j) == topk(x, j, rev=true, output_indices = true)[2]
9+
@test partialsortperm(x, 1:j, rev=true) == topk(x, j, output_indices = true)[2]
10+
end
11+
x = rand(11)
12+
for j in 1:11
13+
@test partialsort(x, 1:j) == topk(x, j, rev=true)
14+
@test partialsort(x, 1:j, rev=true) == topk(x, j)
15+
@test partialsortperm(x, 1:j) == topk(x, j, rev=true, output_indices = true)[2]
16+
@test partialsortperm(x, 1:j, rev=true) == topk(x, j, output_indices=true)[2]
17+
end
18+
x = randn(11)
19+
for j in 1:11
20+
@test partialsort(x, 1:j) == topk(x, j, rev=true)
21+
@test partialsort(x, 1:j, rev=true) == topk(x, j)
22+
@test partialsortperm(x, 1:j) == topk(x, j, rev=true, output_indices=true)[2]
23+
@test partialsortperm(x, 1:j, rev=true) == topk(x, j, output_indices=true)[2]
24+
end
25+
end
26+
x = [1, 10, missing, 100, -1000, 32, 54, 0, missing, missing, -1]
27+
@test topk(x, 2) == [100, 54]
28+
@test topk(x, 2, rev=true) == [-1000, -1]
29+
@test topk(x, 2, output_indices=true)[2] == [4, 7]
30+
@test topk(x, 2, rev=true, output_indices=true)[2] == [5, 11]
31+
@test topk(x, 10) == [100, 54, 32, 10, 1, 0, -1, -1000]
32+
@test topk(x, 10, rev=true) == [-1000, -1, 0, 1, 10, 32, 54, 100]
33+
@test topk(x, 10, output_indices=true)[2] == [4, 7, 6, 2, 1, 8, 11, 5]
34+
@test topk(x, 10, rev=true, output_indices=true)[2] == [5, 11, 8, 1, 2, 6, 7, 4]
35+
@test isequal(topk([missing, missing], 2), [missing])
36+
@test isequal(topk([missing, missing], 2, rev = true), [missing])
37+
@test isequal(topk([missing, missing], 2, output_indices=true)[2], [missing])
38+
@test isequal(topk([missing, missing], 2, rev=true, output_indices=true)[2], [missing])
39+
end

0 commit comments

Comments
 (0)