From a024e3f31d495af0d05835052bb57cd0a4b280a5 Mon Sep 17 00:00:00 2001 From: joaquimg Date: Sun, 15 Mar 2026 17:45:08 -0300 Subject: [PATCH 1/2] More units tests --- test/test_MathOptInterface.jl | 58 ++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/test/test_MathOptInterface.jl b/test/test_MathOptInterface.jl index 2e9dccd..dcda270 100644 --- a/test/test_MathOptInterface.jl +++ b/test/test_MathOptInterface.jl @@ -377,9 +377,13 @@ function test_moi_ListOfConstraintTypesPresent() model = POI.Optimizer(ipopt) MOI.set(model, MOI.Silent(), true) x = MOI.add_variables(model, N / 2) - y = first.( - MOI.add_constrained_variable.(model, MOI.Parameter.(ones(Int(N / 2)))), - ) + y = + first.( + MOI.add_constrained_variable.( + model, + MOI.Parameter.(ones(Int(N / 2))), + ), + ) MOI.add_constraint( model, @@ -673,10 +677,11 @@ function test_vector_parameter_affine_nonnegatives() t, ct = MOI.add_constrained_variable(model, MOI.Parameter(5.0)) A = [1.0 0 -1; 0 1 -1] b = [1.0; 2] - terms = MOI.VectorAffineTerm.( - 1:2, - MOI.ScalarAffineTerm.(A, reshape([x, y, t], 1, 3)), - ) + terms = + MOI.VectorAffineTerm.( + 1:2, + MOI.ScalarAffineTerm.(A, reshape([x, y, t], 1, 3)), + ) f = MOI.VectorAffineFunction(vec(terms), b) set = MOI.Nonnegatives(2) cnn = MOI.add_constraint(model, f, MOI.Nonnegatives(2)) @@ -725,10 +730,11 @@ function test_vector_parameter_affine_nonpositives() t, ct = MOI.add_constrained_variable(model, MOI.Parameter(5.0)) A = [-1.0 0 1; 0 -1 1] b = [-1.0; -2] - terms = MOI.VectorAffineTerm.( - 1:2, - MOI.ScalarAffineTerm.(A, reshape([x, y, t], 1, 3)), - ) + terms = + MOI.VectorAffineTerm.( + 1:2, + MOI.ScalarAffineTerm.(A, reshape([x, y, t], 1, 3)), + ) f = MOI.VectorAffineFunction(vec(terms), b) set = MOI.Nonnegatives(2) cnn = MOI.add_constraint(model, f, MOI.Nonpositives(2)) @@ -2524,6 +2530,36 @@ function test_vector_quadratic_no_parameters_affine_get_constraint_function() return end +function test_delete_variable_errors() + model = POI.Optimizer(MOI.Utilities.Model{Float64}()) + p, _ = MOI.add_constrained_variable(model, MOI.Parameter(1.0)) + @test_throws( + ErrorException("Cannot delete parameters in ParametricOptInterface."), + MOI.delete(model, p), + ) + @test_throws MOI.InvalidIndex MOI.delete(model, MOI.VariableIndex(999999)) + return +end + +function test_parameter_only_affine_objective() + # Objective with only parameter terms (no decision variable terms). + # This tests MOI.ScalarConstantChange in isolation. + model = POI.Optimizer(HiGHS.Optimizer) + MOI.set(model, MOI.Silent(), true) + x = MOI.add_variable(model) + MOI.add_constraint(model, x, MOI.GreaterThan(0.0)) + p, pc = MOI.add_constrained_variable(model, MOI.Parameter(3.0)) + f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(2.0, p)], 1.0) + MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f) + MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) + MOI.optimize!(model) + @test MOI.get(model, MOI.ObjectiveValue()) ≈ 7.0 # 2*3 + 1 + MOI.set(model, MOI.ConstraintSet(), pc, MOI.Parameter(5.0)) + MOI.optimize!(model) + @test MOI.get(model, MOI.ObjectiveValue()) ≈ 11.0 # 2*5 + 1 + return +end + end # module TestMathOptInterfaceTests.runtests() From ade1867995986fc3a55475728273db38f1906e98 Mon Sep 17 00:00:00 2001 From: joaquimg Date: Sun, 15 Mar 2026 17:47:10 -0300 Subject: [PATCH 2/2] fmt --- test/test_MathOptInterface.jl | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/test/test_MathOptInterface.jl b/test/test_MathOptInterface.jl index dcda270..c82dc0e 100644 --- a/test/test_MathOptInterface.jl +++ b/test/test_MathOptInterface.jl @@ -377,13 +377,9 @@ function test_moi_ListOfConstraintTypesPresent() model = POI.Optimizer(ipopt) MOI.set(model, MOI.Silent(), true) x = MOI.add_variables(model, N / 2) - y = - first.( - MOI.add_constrained_variable.( - model, - MOI.Parameter.(ones(Int(N / 2))), - ), - ) + y = first.( + MOI.add_constrained_variable.(model, MOI.Parameter.(ones(Int(N / 2)))), + ) MOI.add_constraint( model, @@ -677,11 +673,10 @@ function test_vector_parameter_affine_nonnegatives() t, ct = MOI.add_constrained_variable(model, MOI.Parameter(5.0)) A = [1.0 0 -1; 0 1 -1] b = [1.0; 2] - terms = - MOI.VectorAffineTerm.( - 1:2, - MOI.ScalarAffineTerm.(A, reshape([x, y, t], 1, 3)), - ) + terms = MOI.VectorAffineTerm.( + 1:2, + MOI.ScalarAffineTerm.(A, reshape([x, y, t], 1, 3)), + ) f = MOI.VectorAffineFunction(vec(terms), b) set = MOI.Nonnegatives(2) cnn = MOI.add_constraint(model, f, MOI.Nonnegatives(2)) @@ -730,11 +725,10 @@ function test_vector_parameter_affine_nonpositives() t, ct = MOI.add_constrained_variable(model, MOI.Parameter(5.0)) A = [-1.0 0 1; 0 -1 1] b = [-1.0; -2] - terms = - MOI.VectorAffineTerm.( - 1:2, - MOI.ScalarAffineTerm.(A, reshape([x, y, t], 1, 3)), - ) + terms = MOI.VectorAffineTerm.( + 1:2, + MOI.ScalarAffineTerm.(A, reshape([x, y, t], 1, 3)), + ) f = MOI.VectorAffineFunction(vec(terms), b) set = MOI.Nonnegatives(2) cnn = MOI.add_constraint(model, f, MOI.Nonpositives(2))