From 508f9d064372c5f0ad86ab9a6b12f0c0cb455fb0 Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 6 Nov 2024 16:35:00 +1300 Subject: [PATCH 1/3] Improve test coverage --- test/test_constraint.jl | 49 +++++++++++++++++++++++++++++++++++++++++ test/test_model.jl | 25 +++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/test/test_constraint.jl b/test/test_constraint.jl index 04214015ac4..3f800f419df 100644 --- a/test/test_constraint.jl +++ b/test/test_constraint.jl @@ -2171,4 +2171,53 @@ function test_constraint_by_name() return end +function test_shadow_price_errors() + model = Model() + @variable(model, x[1:2]) + c = @constraint(model, x in SOS1()) + @test_throws( + ErrorException( + "The shadow price is not defined or not implemented for this type " * + "of constraint.", + ), + shadow_price(c), + ) + err = ErrorException( + "The shadow price is not available because no dual result is " * + "available.", + ) + model = Model() + @variable(model, x) + c = @constraint(model, x <= 1) + @test_throws err shadow_price(c) + c = @constraint(model, x >= 1) + @test_throws err shadow_price(c) + c = @constraint(model, x == 1) + @test_throws err shadow_price(c) + + model = Model() + @variable(model, x >= 0) + set_optimizer( + model, + () -> MOI.Utilities.MockOptimizer( + MOI.Utilities.Model{Float64}(), + ), + ) + optimize!(model) + mock = unsafe_backend(model) + MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL) + MOI.set(mock, MOI.PrimalStatus(), MOI.FEASIBLE_POINT) + MOI.set(mock, MOI.DualStatus(), MOI.FEASIBLE_POINT) + F, S = MOI.VariableIndex, MOI.GreaterThan{Float64} + xi = only(MOI.get(mock, MOI.ListOfVariableIndices())) + MOI.set(mock, MOI.ConstraintDual(), MOI.ConstraintIndex{F,S}(xi.value), 1.0) + @test_throws( + ErrorException( + "The shadow price is not available because the objective sense $FEASIBILITY_SENSE is not minimization or maximization.", + ), + shadow_price(LowerBoundRef(x)), + ) + return +end + end # module diff --git a/test/test_model.jl b/test/test_model.jl index ee4c193e383..d3926b20e8e 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -1364,4 +1364,29 @@ function test_optimizer_index() return end +function test_copy_nonlinear() + model = Model() + @variable(model, x) + @NLconstraint(model, x <= 1) + @test_throws( + ErrorException( + "copy is not supported yet for models with nonlinear constraints" * + " and/or nonlinear objective function" + ), + copy_model(model), + ) + return +end + +function test_deepcopy() + model = Model() + @test_throws( + ErrorException( + "`JuMP.Model` does not support `deepcopy` as the reference to the underlying solver cannot be deep copied, use `copy` instead.", + ), + deepcopy(model), + ) + return +end + end # module TestModels From 4062b31ca9c2ea2147dc6d85380adada15f34f63 Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 6 Nov 2024 16:49:06 +1300 Subject: [PATCH 2/3] Update --- test/test_constraint.jl | 4 +--- test/test_model.jl | 2 +- test/test_nlp.jl | 42 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/test/test_constraint.jl b/test/test_constraint.jl index 3f800f419df..cb63bb99e29 100644 --- a/test/test_constraint.jl +++ b/test/test_constraint.jl @@ -2199,9 +2199,7 @@ function test_shadow_price_errors() @variable(model, x >= 0) set_optimizer( model, - () -> MOI.Utilities.MockOptimizer( - MOI.Utilities.Model{Float64}(), - ), + () -> MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}()), ) optimize!(model) mock = unsafe_backend(model) diff --git a/test/test_model.jl b/test/test_model.jl index d3926b20e8e..6db3a692ed0 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -1371,7 +1371,7 @@ function test_copy_nonlinear() @test_throws( ErrorException( "copy is not supported yet for models with nonlinear constraints" * - " and/or nonlinear objective function" + " and/or nonlinear objective function", ), copy_model(model), ) diff --git a/test/test_nlp.jl b/test/test_nlp.jl index fbbeea10edc..2dee2c61ca8 100644 --- a/test/test_nlp.jl +++ b/test/test_nlp.jl @@ -1683,4 +1683,46 @@ function test_get_node_type_comparison() return end +function test_NL_dot() + model = Model() + @variable(model, x) + y = (; abc = x) + @NLexpression(model, ref, y.abc) + nlp = nonlinear_model(model) + @test MOI.Nonlinear.parse_expression(nlp, x) == nlp[index(ref)] + return +end + +function test_NL_adjoint() + model = Model() + @variable(model, x) + @NLexpression(model, ref, x') + nlp = nonlinear_model(model) + @test MOI.Nonlinear.parse_expression(nlp, x) == nlp[index(ref)] + return +end + +function test_NL_too_many_arguments_errors() + model = Model() + @test_throws_parsetime( + ErrorException( + "At `@NLexpression()`: To few arguments (0); must pass the model and nonlinear expression as arguments.", + ), + @NLexpression(), + ) + @test_throws_parsetime( + ErrorException( + "At `@NLexpression(model, ex, 1, 2)`: To many arguments (4).", + ), + @NLexpression(model, ex, 1, 2), + ) + @test_throws_parsetime( + ErrorException( + "At `@NLconstraint(model, ex, 1, 2)`: too many arguments.", + ), + @NLconstraint(model, ex, 1, 2), + ) + return +end + end From 3f27023712806ae3a5f8e8087d2de6e212b9ce8d Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 6 Nov 2024 17:00:56 +1300 Subject: [PATCH 3/3] Update --- test/test_nlp.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_nlp.jl b/test/test_nlp.jl index 2dee2c61ca8..1c2d917f62e 100644 --- a/test/test_nlp.jl +++ b/test/test_nlp.jl @@ -1706,19 +1706,19 @@ function test_NL_too_many_arguments_errors() model = Model() @test_throws_parsetime( ErrorException( - "At `@NLexpression()`: To few arguments (0); must pass the model and nonlinear expression as arguments.", + "In `@NLexpression()`: To few arguments (0); must pass the model and nonlinear expression as arguments.", ), @NLexpression(), ) @test_throws_parsetime( ErrorException( - "At `@NLexpression(model, ex, 1, 2)`: To many arguments (4).", + "In `@NLexpression(model, ex, 1, 2)`: To many arguments (4).", ), @NLexpression(model, ex, 1, 2), ) @test_throws_parsetime( ErrorException( - "At `@NLconstraint(model, ex, 1, 2)`: too many arguments.", + "In `@NLconstraint(model, ex, 1, 2)`: too many arguments.", ), @NLconstraint(model, ex, 1, 2), )