Skip to content

Commit e72c1e8

Browse files
committed
add test for extrema(!)
Generalize sparsevector reduction and add few more optimization for `min/max/extrema`
1 parent f9c958a commit e72c1e8

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

test/reducedim.jl

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,17 +458,47 @@ end
458458
for R in (fill(0, 4), fill(0, 4, 1), fill(0, 4, 1, 1))
459459
@test @inferred(maximum!(R, B)) == reshape(21:24, size(R))
460460
@test @inferred(minimum!(R, B)) == reshape(1:4, size(R))
461+
@test @inferred(extrema!(fill((0,0), size(R)), B)) == reshape(tuple.(1:4, 21:24), size(R))
461462
end
462463
for R in (fill(0, 1, 3), fill(0, 1, 3, 1))
463464
@test @inferred(maximum!(R, B)) == reshape(16:4:24, size(R))
464465
@test @inferred(minimum!(R, B)) == reshape(1:4:9, size(R))
466+
@test @inferred(extrema!(fill((0,0), size(R)), B)) == reshape(tuple.(1:4:9, 16:4:24), size(R))
467+
end
468+
for (ini, f!) in zip((0,0,(0,0)), (maximum!, minimum!, extrema!))
469+
@test_throws DimensionMismatch f!(fill(ini, 4, 1, 1, 1), B)
470+
@test_throws DimensionMismatch f!(fill(ini, 1, 3, 1, 1), B)
471+
@test_throws DimensionMismatch f!(fill(ini, 1, 1, 2, 1), B)
472+
end
473+
end
474+
475+
function unordered_test_for_extrema(a; dims_test = ((), 1, 2, (1,2), 3))
476+
for dims in dims_test
477+
vext = extrema(a; dims)
478+
vmin, vmax = minimum(a; dims), maximum(a; dims)
479+
@test isequal(extrema!(copy(vext), a), vext)
480+
@test all(x -> isequal(x[1], x[2:3]), zip(vext,vmin,vmax))
481+
end
482+
end
483+
@testset "0.0,-0.0 test for extrema with dims" begin
484+
@test extrema([-0.0;0.0], dims = 1)[1] === (-0.0,0.0)
485+
@test tuple(extrema([-0.0;0.0], dims = 2)...) === ((-0.0, -0.0), (0.0, 0.0))
486+
end
487+
@testset "NaN/missing test for extrema with dims #43599" begin
488+
for sz = (3, 10, 100)
489+
for T in (Int, BigInt, Float64, BigFloat)
490+
Aₘ = Matrix{Union{T, Missing}}(rand(-sz:sz, sz, sz))
491+
Aₘ[rand(1:sz*sz, sz)] .= missing
492+
unordered_test_for_extrema(Aₘ)
493+
if T <: AbstractFloat
494+
Aₙ = map(i -> ismissing(i) ? T(NaN) : i, Aₘ)
495+
unordered_test_for_extrema(Aₙ)
496+
p = rand(1:sz*sz, sz)
497+
Aₘ[p] .= NaN
498+
unordered_test_for_extrema(Aₘ)
499+
end
500+
end
465501
end
466-
@test_throws DimensionMismatch maximum!(fill(0, 4, 1, 1, 1), B)
467-
@test_throws DimensionMismatch minimum!(fill(0, 4, 1, 1, 1), B)
468-
@test_throws DimensionMismatch maximum!(fill(0, 1, 3, 1, 1), B)
469-
@test_throws DimensionMismatch minimum!(fill(0, 1, 3, 1, 1), B)
470-
@test_throws DimensionMismatch maximum!(fill(0, 1, 1, 2, 1), B)
471-
@test_throws DimensionMismatch minimum!(fill(0, 1, 1, 2, 1), B)
472502
end
473503

474504
# issue #26709

0 commit comments

Comments
 (0)