From 1398d77b3f2b4b374cb63be0ef2f25b91a35c167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sat, 8 Jun 2019 19:03:23 +0200 Subject: [PATCH] Fix fallback load_constraint --- src/Utilities/copy.jl | 4 ++-- test/errors.jl | 30 +++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Utilities/copy.jl b/src/Utilities/copy.jl index 506f506174..e3dbc48c66 100644 --- a/src/Utilities/copy.jl +++ b/src/Utilities/copy.jl @@ -305,8 +305,8 @@ end Sets the constraint function and set for the constraint of index `ci`. """ -function load_constraint(model::MOI.ModelLike, func::MOI.AbstractFunction, - set::MOI.AbstractSet) +function load_constraint(model::MOI.ModelLike, ::MOI.ConstraintIndex, + func::MOI.AbstractFunction, set::MOI.AbstractSet) MOI.throw_add_constraint_error_fallback(model, func, set; error_if_supported=ALLOCATE_LOAD_NOT_IMPLEMENTED) end diff --git a/test/errors.jl b/test/errors.jl index f88a68e517..690ff279c0 100644 --- a/test/errors.jl +++ b/test/errors.jl @@ -26,19 +26,21 @@ MOI.add_constraint(model, func, MOI.Nonnegatives(2)) end end - @testset "Unsupported constraint with $f" for f in (MOI.add_constraint, - MOIU.allocate_constraint, - MOIU.load_constraint) + @testset "Unsupported constraint with $f" for f in ( + MOI.add_constraint, MOIU.allocate_constraint, + (model, func, set) -> MOIU.load_constraint(model, MOI.ConstraintIndex{typeof(func), typeof(set)}(1), func, set)) @test_throws MOI.UnsupportedConstraint begin - MOI.add_constraint(model, func, MOI.EqualTo(0)) + f(model, func, MOI.EqualTo(0)) end try - MOI.add_constraint(model, func, MOI.EqualTo(0)) + f(model, func, MOI.EqualTo(0)) catch err @test sprint(showerror, err) == "$MOI.UnsupportedConstraint{$MOI.SingleVariable,$MOI.EqualTo{$Int}}:" * " `$MOI.SingleVariable`-in-`$MOI.EqualTo{$Int}` constraint is" * " not supported by the model." end + end + @testset "Unsupported constraint for shortcuts" begin @test_throws MOI.UnsupportedConstraint begin MOI.add_constraint(model, vi, MOI.EqualTo(0)) end @@ -76,13 +78,23 @@ end end - @testset "$f errors" for f in (MOIU.allocate_constraint, - MOIU.load_constraint) + @testset "allocate_constraint errors" begin + @test_throws MOI.ErrorException begin + MOIU.allocate_constraint(model, func, MOI.EqualTo(0.0)) + end + try + MOIU.allocate_constraint(model, func, MOI.EqualTo(0.0)) + catch err + @test err === MOIU.ALLOCATE_LOAD_NOT_IMPLEMENTED + end + end + @testset "load_constraint errors" begin + ci = MOI.ConstraintIndex{typeof(func), MOI.EqualTo{Float64}}(1) @test_throws MOI.ErrorException begin - f(model, func, MOI.EqualTo(0.0)) + MOIU.load_constraint(model, ci, func, MOI.EqualTo(0.0)) end try - f(model, func, MOI.EqualTo(0.0)) + MOIU.load_constraint(model, ci, func, MOI.EqualTo(0.0)) catch err @test err === MOIU.ALLOCATE_LOAD_NOT_IMPLEMENTED end