Skip to content

Commit 05ddddc

Browse files
committed
add test
Update reduce.jl
1 parent c148ec9 commit 05ddddc

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

stdlib/SparseArrays/test/higherorderfns.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,12 +718,12 @@ end
718718
@test extrema(A; dims=2) == extrema(B; dims=2)
719719
@test extrema(A; dims=(1,2)) == extrema(B; dims=(1,2))
720720
@test extrema(f, A; dims=1) == extrema(f, B; dims=1)
721-
@test extrema(sparse(C); dims=1) == extrema(C; dims=1)
721+
@test_throws ArgumentError extrema(sparse(C); dims=1) == extrema(C; dims=1) # empty reduction is not allowed anymore
722722
@test extrema(A; dims=[]) == extrema(B; dims=[])
723723
@test extrema(x; dims=:) == extrema(y; dims=:)
724724
@test extrema(x; dims=1) == extrema(y; dims=1)
725725
@test extrema(f, x; dims=1) == extrema(f, y; dims=1)
726-
@test_throws BoundsError extrema(sparse(z); dims=1)
726+
@test_throws ArgumentError extrema(sparse(z); dims=1) # empty reduction is not allowed anymore
727727
@test extrema(x; dims=[]) == extrema(y; dims=[])
728728
end
729729

test/reduce.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,40 @@ A = circshift(reshape(1:24,2,3,4), (0,1,1))
378378
@test size(extrema(A,dims=(1,2,3))) == size(maximum(A,dims=(1,2,3)))
379379
@test extrema(x->div(x, 2), A, dims=(2,3)) == reshape([(0,11),(1,12)],2,1,1)
380380

381+
# TODO: drop `a′` once `minimum` and `maximum` is fixed
382+
# (the following test_broken pass)
383+
function test_extrema(a, a′ = a; dims_test = ((), 1, 2, (1,2), 3))
384+
for dims in dims_test
385+
vext = extrema(a; dims)
386+
vmin, vmax = minimum(a′; dims), maximum(a′; dims)
387+
@test all(x -> isequal(x[1], x[2:3]), zip(vext,vmin,vmax)) || foreach(i -> display(i),(eltype(a), vext,vmin,vmax))
388+
end
389+
end
390+
@test_broken minimum([missing BigInt(1)], dims = 2)[1] === missing
391+
@testset "0.0,-0.0 test for extrema with dims" begin
392+
@test extrema([-0.0;0.0], dims = 1)[1] === (-0.0,0.0)
393+
@test tuple(extrema([-0.0;0.0], dims = 2)...) === ((-0.0, -0.0), (0.0, 0.0))
394+
end
395+
@testset "NaN/missing test for extrema with dims #43599" begin
396+
for sz = (3, 10, 100)
397+
for T in (Int, BigInt, Float64, BigFloat)
398+
Aₘ = Matrix{Union{Float64, Missing}}(rand(-sz:sz, sz, sz))
399+
Aₘ[rand(1:sz*sz, sz)] .= missing
400+
ATₘ = Matrix{Union{T, Missing}}(Aₘ)
401+
test_extrema(ATₘ, Aₘ)
402+
if T <: AbstractFloat
403+
Aₙ = map(i -> ismissing(i) ? T(NaN) : i, Aₘ)
404+
ATₙ = map(i -> ismissing(i) ? T(NaN) : i, ATₘ)
405+
test_extrema(ATₙ, Aₙ)
406+
p = rand(1:sz*sz, sz)
407+
Aₘ[p] .= NaN
408+
ATₘ[p] .= NaN
409+
test_extrema(ATₘ, Aₘ)
410+
end
411+
end
412+
end
413+
end
414+
381415
@testset "maximum/minimum/extrema with missing values" begin
382416
for x in (Vector{Union{Int,Missing}}(missing, 10),
383417
Vector{Union{Int,Missing}}(missing, 257))

0 commit comments

Comments
 (0)