diff --git a/test/Utilities/mutable_arithmetics.jl b/test/Utilities/mutable_arithmetics.jl index 0634474a48..36f6fb7f8f 100644 --- a/test/Utilities/mutable_arithmetics.jl +++ b/test/Utilities/mutable_arithmetics.jl @@ -1,3 +1,5 @@ +module TestMutableArithmetics + using Test import MutableArithmetics @@ -6,36 +8,59 @@ const MA = MutableArithmetics using MathOptInterface const MOI = MathOptInterface +function runtests() + for name in names(@__MODULE__; all = true) + if startswith("$(name)", "test_") + @testset "$(name)" begin + getfield(@__MODULE__, name)() + end + end + end + return +end + _zero(::Type{MOI.SingleVariable}, T::Type) = zero(MOI.ScalarAffineFunction{T}) _zero(x, ::Type) = zero(x) -function promote_operation_test(op::Function, T, x::Type, y::Type) + +function _promote_operation_test(op::Function, T, x::Type, y::Type) f() = MA.promote_operation(op, x, y) @test typeof(op(_zero(x, T), _zero(y, T))) == f() @test 0 == @allocated f() end -@testset "promote_operation allocation with $T" for T in [Int, Float64, Float32] +function _test_promote_operation_allocation(T) AffType = MOI.ScalarAffineFunction{T} QuadType = MOI.ScalarQuadraticFunction{T} for op in [+, -, *] - promote_operation_test(op, T, T, MOI.SingleVariable) - promote_operation_test(op, T, MOI.SingleVariable, T) - promote_operation_test(op, T, T, AffType) - promote_operation_test(op, T, AffType, T) - promote_operation_test(op, T, T, QuadType) - promote_operation_test(op, T, QuadType, T) - promote_operation_test(op, T, MOI.SingleVariable, AffType) - promote_operation_test(op, T, AffType, MOI.SingleVariable) + _promote_operation_test(op, T, T, MOI.SingleVariable) + _promote_operation_test(op, T, MOI.SingleVariable, T) + _promote_operation_test(op, T, T, AffType) + _promote_operation_test(op, T, AffType, T) + _promote_operation_test(op, T, T, QuadType) + _promote_operation_test(op, T, QuadType, T) + _promote_operation_test(op, T, MOI.SingleVariable, AffType) + _promote_operation_test(op, T, AffType, MOI.SingleVariable) if op != * - promote_operation_test(op, T, MOI.SingleVariable, QuadType) - promote_operation_test(op, T, QuadType, MOI.SingleVariable) - promote_operation_test(op, T, AffType, QuadType) - promote_operation_test(op, T, QuadType, AffType) + _promote_operation_test(op, T, MOI.SingleVariable, QuadType) + _promote_operation_test(op, T, QuadType, MOI.SingleVariable) + _promote_operation_test(op, T, AffType, QuadType) + _promote_operation_test(op, T, QuadType, AffType) end end + return end -@testset "promote_operation with $T" for T in [Int, Float64, Float32] +function test_promote_operation_allocation_Int() + return _test_promote_operation_allocation(Int) +end +function test_promote_operation_allocation_Float64() + return _test_promote_operation_allocation(Float64) +end +function test_promote_operation_allocation_Float32() + return _test_promote_operation_allocation(Float32) +end + +function _test_promote_operation(T) @test MA.promote_operation(*, MOI.SingleVariable, T) == MOI.ScalarAffineFunction{T} @test MA.promote_operation(*, T, MOI.SingleVariable) == @@ -48,9 +73,14 @@ end MOI.ScalarQuadraticFunction{T} @test MA.promote_operation(*, T, MOI.ScalarQuadraticFunction{T}) == MOI.ScalarQuadraticFunction{T} + return end -@testset "scaling with $T" for T in [Float64, Float32] +test_promote_operation_Int() = _test_promote_operation(Int) +test_promote_operation_Float64() = _test_promote_operation(Float64) +test_promote_operation_Float32() = _test_promote_operation(Float32) + +function _test_scaling(T) x = MOI.VariableIndex(1) fx = MOI.SingleVariable(x) @test T(3) == MA.scaling(T(0)fx + T(3)) @@ -59,19 +89,25 @@ end @test_throws err MA.scaling(f) end -@testset "Unary `-` with $T" for T in [Float64, Float32] - x = MOI.VariableIndex(1) - fx = MOI.SingleVariable(x) - for f in [T(2)fx + T(3), T(4) * fx * fx + T(2)fx + T(3)] - g = -f - @test g ≈ MA.operate!(-, f) - @test g ≈ f - @test -g ≈ MA.operate!(-, f) - @test -g ≈ f +test_scaling_Float64() = _test_scaling(Float64) +test_scaling_Float32() = _test_scaling(Float32) + +function test_unary_minus() + for T in [Float64, Float32] + x = MOI.VariableIndex(1) + fx = MOI.SingleVariable(x) + for f in [T(2)fx + T(3), T(4) * fx * fx + T(2)fx + T(3)] + g = -f + @test g ≈ MA.operate!(-, f) + @test g ≈ f + @test -g ≈ MA.operate!(-, f) + @test -g ≈ f + end end + return end -function all_tests(T::Type, a, b, c, d, e, f, g) +function _run_all_tests(T::Type, a, b, c, d, e, f, g) exclude = ["scalar_uniform_scaling", "cube"] @testset "Scalar" begin MA.Test.scalar_test(a, exclude = exclude) @@ -133,14 +169,12 @@ function all_tests(T::Type, a, b, c, d, e, f, g) MA.Test.array_test([a b c; b c a; a b a], exclude = exclude) MA.Test.array_test([d b c; d c e; e b a], exclude = exclude) end + return end -x = MOI.VariableIndex(1) -y = MOI.VariableIndex(2) -fx = MOI.SingleVariable(x) -fy = MOI.SingleVariable(y) - -@testset "SingleVariable in $T" for T in [Int] +function test_SingleVariable() + x = MOI.VariableIndex(1) + fx = MOI.SingleVariable(x) a = 2fx MA.Test.@test_rewrite(a + fx) MA.Test.@test_rewrite(a + 3 * fx) @@ -154,13 +188,16 @@ fy = MOI.SingleVariable(y) MA.Test.@test_rewrite(fx - a) end -@testset "Affine in $T" for T in [Int] - @testset "Int" begin - MA.Test.int_test( - MOI.ScalarAffineFunction{T}, - exclude = ["int_mul", "int_add_mul"], - ) - end +function test_ScalarAffineFunction() + T = Int + MA.Test.int_test( + MOI.ScalarAffineFunction{T}, + exclude = ["int_mul", "int_add_mul"], + ) + x = MOI.VariableIndex(1) + y = MOI.VariableIndex(2) + fx = MOI.SingleVariable(x) + fy = MOI.SingleVariable(y) a = T(2) * fx + T(1) b = T(4) * fy + T(2) c = T(3) * fx - T(2) * fy - T(3) @@ -168,16 +205,20 @@ end e = T(5) * fx - T(5) f = T(1) * fy - T(2) * fx + T(2) g = T(2) * fx + T(3) * fx - all_tests(T, a, b, c, d, e, f, g) + _run_all_tests(T, a, b, c, d, e, f, g) + return end -@testset "Quadratic in $T" for T in [Int] - @testset "Int" begin - MA.Test.int_test( - MOI.ScalarQuadraticFunction{T}, - exclude = ["int_mul", "int_add_mul"], - ) - end +function test_ScalarQuadraticFunction() + T = Int + MA.Test.int_test( + MOI.ScalarQuadraticFunction{T}, + exclude = ["int_mul", "int_add_mul"], + ) + x = MOI.VariableIndex(1) + y = MOI.VariableIndex(2) + fx = MOI.SingleVariable(x) + fy = MOI.SingleVariable(y) a = T(2) * fx + T(1) + T(4) * fx * fy b = T(4) * fy + T(3) * fy * fy - T(3) * fy * fx + T(2) c = T(2) * fx * fx + T(3) * fx - T(2) * fy - T(3) @@ -185,5 +226,10 @@ end e = T(5) * fx - T(5) - T(4) * fx * fy f = T(1) * fy + T(2) * fy * fy - T(2) * fx + T(2) g = T(2) * fx + T(3) * fx + T(3) * fx * fy - all_tests(T, a, b, c, d, e, f, g) + _run_all_tests(T, a, b, c, d, e, f, g) + return end + +end # module + +TestMutableArithmetics.runtests()