From c285066ba45d716e13e4060a5b202fa86ffbc82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 14 Jul 2019 16:01:14 +0200 Subject: [PATCH 1/2] Test modification errors --- src/modifications.jl | 26 +++++++++++++++----------- test/errors.jl | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/modifications.jl b/src/modifications.jl index f82645da5f..0029e95c8d 100644 --- a/src/modifications.jl +++ b/src/modifications.jl @@ -1,6 +1,6 @@ """ struct ModifyConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet, - C<:AbstractFunctionModification} <: NotAllowedError + C<:AbstractFunctionModification} <: NotAllowedError constraint_index::ConstraintIndex{F, S} change::C message::String @@ -10,15 +10,18 @@ An error indicating that the constraint modification `change` cannot be applied to the constraint of index `ci`. """ struct ModifyConstraintNotAllowed{F<:AbstractFunction, S<:AbstractSet, - C<:AbstractFunctionModification} <: NotAllowedError + C<:AbstractFunctionModification} <: NotAllowedError constraint_index::ConstraintIndex{F, S} change::C message::String end -function ModifyConstraintNotAllowed(ci::ConstraintIndex{F, S}, - change::AbstractFunctionModification) where {F<:AbstractFunction, S<:AbstractSet} - ModifyConstraintNotAllowed{F, S, typeof(change)}(ci, change, "") +function ModifyConstraintNotAllowed( + ci::ConstraintIndex{F, S}, + change::AbstractFunctionModification, + message="") where {F<:AbstractFunction, S<:AbstractSet} + ModifyConstraintNotAllowed{F, S, typeof(change)}(ci, change, message) end +throw_modify_not_allowed(ci::ConstraintIndex, args...) = throw(ModifyConstraintNotAllowed(ci, args...)) operation_name(err::ModifyConstraintNotAllowed{F, S}) where {F, S} = "Modifying the constraints $(err.constraint_index) with $(err.change)" @@ -38,6 +41,7 @@ end function ModifyObjectiveNotAllowed(change::AbstractFunctionModification) ModifyObjectiveNotAllowed(change, "") end +throw_modify_not_allowed(::ObjectiveFunction, args...) = throw(ModifyObjectiveNotAllowed(args...)) operation_name(err::ModifyObjectiveNotAllowed) = "Modifying the objective function with $(err.change)" @@ -75,12 +79,12 @@ modify(model, ObjectiveFunction{ScalarAffineFunction{Float64}}(), ScalarConstant """ function modify end -function modify(model::ModelLike, ci::ConstraintIndex{F, S}, - change::AbstractFunctionModification) where {F, S} - throw(ModifyConstraintNotAllowed(ci, change)) +function modify(model::ModelLike, ci::ConstraintIndex, + change::AbstractFunctionModification) + throw_modify_not_allowed(ci, change) end -function modify(model::ModelLike, ::ObjectiveFunction, - change::AbstractFunctionModification) - throw(ModifyObjectiveNotAllowed(change)) +function modify(model::ModelLike, attr::ObjectiveFunction, + change::AbstractFunctionModification) + throw_modify_not_allowed(attr, change) end diff --git a/test/errors.jl b/test/errors.jl index 690ff279c0..fb008a11d4 100644 --- a/test/errors.jl +++ b/test/errors.jl @@ -1,3 +1,9 @@ +using Test +using MathOptInterface +const MOI = MathOptInterface + +include("dummy.jl") + @testset "Fallbacks for `set` methods" begin model = DummyModel() @@ -182,6 +188,32 @@ MOI.set(model, MOI.ConstraintSet(), ci, MOI.GreaterThan(1.0)) end end + + @testset "ModifyNotAllowed" begin + change = MOI.ScalarConstantChange(1.0) + @testset "Constraint" begin + err = MOI.ModifyConstraintNotAllowed(ci, change) + @test_throws err MOI.modify(model, ci, change) + @test sprint(showerror, err) == "MathOptInterface.ModifyConstraintNotAllowed{MathOptInterface.SingleVariable,MathOptInterface.EqualTo{Float64},MathOptInterface.ScalarConstantChange{Float64}}:" * + " Modifying the constraints MathOptInterface.ConstraintIndex{MathOptInterface.SingleVariable,MathOptInterface.EqualTo{Float64}}(1)" * + " with MathOptInterface.ScalarConstantChange{Float64}(1.0) cannot" * + " be performed. You may want to use a `CachingOptimizer` in" * + " `AUTOMATIC` mode or you may need to call `reset_optimizer`" * + " before doing this operation if the `CachingOptimizer` is in" * + " `MANUAL` mode." + end + @testset "Objective" begin + attr = MOI.ObjectiveFunction{MOI.SingleVariable}() + err = MOI.ModifyObjectiveNotAllowed(change) + @test_throws err MOI.modify(model, attr, change) + @test sprint(showerror, err) == "MathOptInterface.ModifyObjectiveNotAllowed{MathOptInterface.ScalarConstantChange{Float64}}:" * + " Modifying the objective function with MathOptInterface.ScalarConstantChange{Float64}(1.0)" * + " cannot be performed. You may want to use a `CachingOptimizer`" * + " in `AUTOMATIC` mode or you may need to call `reset_optimizer`" * + " before doing this operation if the `CachingOptimizer` is in" * + " `MANUAL` mode." + end + end end @testset "Error messages" begin From d95e7f56546b42ed4cca00dc38820feca8e37b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 14 Jul 2019 17:40:44 -0400 Subject: [PATCH 2/2] [ci skip] Missing return --- src/modifications.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modifications.jl b/src/modifications.jl index 0029e95c8d..b9ed5eb15a 100644 --- a/src/modifications.jl +++ b/src/modifications.jl @@ -19,7 +19,7 @@ function ModifyConstraintNotAllowed( ci::ConstraintIndex{F, S}, change::AbstractFunctionModification, message="") where {F<:AbstractFunction, S<:AbstractSet} - ModifyConstraintNotAllowed{F, S, typeof(change)}(ci, change, message) + return ModifyConstraintNotAllowed{F, S, typeof(change)}(ci, change, message) end throw_modify_not_allowed(ci::ConstraintIndex, args...) = throw(ModifyConstraintNotAllowed(ci, args...)) @@ -39,7 +39,7 @@ struct ModifyObjectiveNotAllowed{C<:AbstractFunctionModification} <: NotAllowedE message::String end function ModifyObjectiveNotAllowed(change::AbstractFunctionModification) - ModifyObjectiveNotAllowed(change, "") + return ModifyObjectiveNotAllowed(change, "") end throw_modify_not_allowed(::ObjectiveFunction, args...) = throw(ModifyObjectiveNotAllowed(args...))