Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
Update reduce.jl
  • Loading branch information
N5N3 committed Jan 4, 2022
1 parent 099fab8 commit 941e3b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions stdlib/SparseArrays/test/higherorderfns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -718,12 +718,12 @@ end
@test extrema(A; dims=2) == extrema(B; dims=2)
@test extrema(A; dims=(1,2)) == extrema(B; dims=(1,2))
@test extrema(f, A; dims=1) == extrema(f, B; dims=1)
@test extrema(sparse(C); dims=1) == extrema(C; dims=1)
@test_throws ArgumentError extrema(sparse(C); dims=1) == extrema(C; dims=1) # empty reduction is not allowed anymore
@test extrema(A; dims=[]) == extrema(B; dims=[])
@test extrema(x; dims=:) == extrema(y; dims=:)
@test extrema(x; dims=1) == extrema(y; dims=1)
@test extrema(f, x; dims=1) == extrema(f, y; dims=1)
@test_throws BoundsError extrema(sparse(z); dims=1)
@test_throws ArgumentError extrema(sparse(z); dims=1) # empty reduction is not allowed anymore
@test extrema(x; dims=[]) == extrema(y; dims=[])
end

Expand Down
34 changes: 34 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,40 @@ A = circshift(reshape(1:24,2,3,4), (0,1,1))
@test size(extrema(A,dims=(1,2,3))) == size(maximum(A,dims=(1,2,3)))
@test extrema(x->div(x, 2), A, dims=(2,3)) == reshape([(0,11),(1,12)],2,1,1)

# TODO: drop `a′` once `minimum` and `maximum` is fixed
# (the following test_broken pass)
function test_extrema(a, a′ = a; dims_test = ((), 1, 2, (1,2), 3))
for dims in dims_test
vext = extrema(a; dims)
vmin, vmax = minimum(a′; dims), maximum(a′; dims)
@test all(x -> isequal(x[1], x[2:3]), zip(vext,vmin,vmax)) || foreach(i -> display(i),(eltype(a), vext,vmin,vmax))
end
end
@test_broken minimum([missing BigInt(1)], dims = 2)[1] === missing
@testset "0.0,-0.0 test for extrema with dims" begin
@test extrema([-0.0;0.0], dims = 1)[1] === (-0.0,0.0)
@test tuple(extrema([-0.0;0.0], dims = 2)...) === ((-0.0, -0.0), (0.0, 0.0))
end
@testset "NaN/missing test for extrema with dims #43599" begin
for sz = (3, 10, 100)
for T in (Int, BigInt, Float64, BigFloat)
Aₘ = Matrix{Union{Float64, Missing}}(rand(-sz:sz, sz, sz))
Aₘ[rand(1:sz*sz, sz)] .= missing
ATₘ = Matrix{Union{T, Missing}}(Aₘ)
test_extrema(ATₘ, Aₘ)
if T <: AbstractFloat
Aₙ = map(i -> ismissing(i) ? T(NaN) : i, Aₘ)
ATₙ = map(i -> ismissing(i) ? T(NaN) : i, ATₘ)
test_extrema(ATₙ, Aₙ)
p = rand(1:sz*sz, sz)
Aₘ[p] .= NaN
ATₘ[p] .= NaN
test_extrema(ATₘ, Aₘ)
end
end
end
end

@testset "maximum/minimum/extrema with missing values" begin
for x in (Vector{Union{Int,Missing}}(missing, 10),
Vector{Union{Int,Missing}}(missing, 257))
Expand Down

0 comments on commit 941e3b9

Please sign in to comment.