From cec90f60fb8acc74e7da9ab478435551eb3c48ca Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 26 Jul 2021 17:27:35 +1200 Subject: [PATCH 1/2] Improve coverage of constraints.jl --- src/constraints.jl | 22 +++++------------- test/constraints.jl | 55 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/constraints.jl b/src/constraints.jl index 47099cff76..1fb98de123 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -274,14 +274,9 @@ function Base.showerror(io::IO, err::LowerBoundAlreadySet{S1,S2}) where {S1,S2} return print( io, typeof(err), - ": Cannot add `SingleVariable`-in`", - S2, - "` constraint for variable ", - err.vi, - " as a `SingleVariable`-in`", - S1, - "` constraint was already set for this variable and both", - " constraints set a lower bound.", + ": Cannot add `SingleVariable`-in-`$(S2)` constraint for variable ", + "$(err.vi) as a `SingleVariable`-in-`$(S1)` constraint was already ", + "set for this variable and both constraints set a lower bound.", ) end @@ -301,14 +296,9 @@ function Base.showerror(io::IO, err::UpperBoundAlreadySet{S1,S2}) where {S1,S2} return print( io, typeof(err), - ": Cannot add `SingleVariable`-in`", - S2, - "` constraint for variable ", - err.vi, - " as a `SingleVariable`-in`", - S1, - "` constraint was already set for this variable and both", - " constraints set an upper bound.", + ": Cannot add `SingleVariable`-in-`$(S2)` constraint for variable ", + "$(err.vi) as a `SingleVariable`-in-`$(S1)` constraint was already ", + "set for this variable and both constraints set an upper bound.", ) end diff --git a/test/constraints.jl b/test/constraints.jl index a06718f028..062f1973a1 100644 --- a/test/constraints.jl +++ b/test/constraints.jl @@ -1,9 +1,20 @@ module TestConstraints using Test + 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 +end + function _constant_not_zero_test(::Type{T}) where {T} S = MOI.EqualTo{T} x = MOI.VariableIndex(1) @@ -25,14 +36,42 @@ function test_constraints_ConstantNotZero() return end -function runtests() - for name in names(@__MODULE__; all = true) - if startswith("$name", "test_") - @testset "$(name)" begin - getfield(@__MODULE__, name)() - end - end - end +function test_LowerBoundAlreadySet_error() + x = MOI.VariableIndex(1) + S1 = MOI.LessThan{Int} + S2 = MOI.Interval{Int} + err = MOI.LowerBoundAlreadySet{S1,S2}(x) + @test sprint(showerror, err) == + "$(typeof(err)): Cannot add `SingleVariable`-in-`$(S2)` constraint " * + "for variable $(x) as a `SingleVariable`-in-`$(S1)` constraint was " * + "already set for this variable and both constraints set a lower bound." + return +end + +function test_UpperBoundAlreadySet_error() + x = MOI.VariableIndex(1) + S1 = MOI.GreaterThan{Int} + S2 = MOI.Interval{Int} + err = MOI.UpperBoundAlreadySet{S1,S2}(x) + @test sprint(showerror, err) == + "$(typeof(err)): Cannot add `SingleVariable`-in-`$(S2)` constraint " * + "for variable $(x) as a `SingleVariable`-in-`$(S1)` constraint was " * + "already set for this variable and both constraints set an upper bound." + return +end + +function test_ScalarFunctionConstantNotZero_error() + x = MOI.VariableIndex(1) + T = Int + F = MOI.ScalarAffineFunction{T} + S = MOI.EqualTo{T} + err = MOI.ScalarFunctionConstantNotZero{T,F,S}(one(T)) + @test sprint(showerror, err) == + "In `$F`-in-`$S` constraint: Constant $(one(T)) of the function is " * + "not zero. The function constant should be moved to the set. You can " * + "use `MOI.Utilities.normalize_and_add_constraint` which does this " * + "automatically." + return end end From 555ce768380402a9d075a12895195b8f337aca8c Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Mon, 26 Jul 2021 17:45:37 +1200 Subject: [PATCH 2/2] Update constraints.jl --- test/constraints.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/constraints.jl b/test/constraints.jl index 062f1973a1..2d6643a2bc 100644 --- a/test/constraints.jl +++ b/test/constraints.jl @@ -42,9 +42,9 @@ function test_LowerBoundAlreadySet_error() S2 = MOI.Interval{Int} err = MOI.LowerBoundAlreadySet{S1,S2}(x) @test sprint(showerror, err) == - "$(typeof(err)): Cannot add `SingleVariable`-in-`$(S2)` constraint " * - "for variable $(x) as a `SingleVariable`-in-`$(S1)` constraint was " * - "already set for this variable and both constraints set a lower bound." + "$(typeof(err)): Cannot add `SingleVariable`-in-`$(S2)` constraint " * + "for variable $(x) as a `SingleVariable`-in-`$(S1)` constraint was " * + "already set for this variable and both constraints set a lower bound." return end @@ -54,9 +54,9 @@ function test_UpperBoundAlreadySet_error() S2 = MOI.Interval{Int} err = MOI.UpperBoundAlreadySet{S1,S2}(x) @test sprint(showerror, err) == - "$(typeof(err)): Cannot add `SingleVariable`-in-`$(S2)` constraint " * - "for variable $(x) as a `SingleVariable`-in-`$(S1)` constraint was " * - "already set for this variable and both constraints set an upper bound." + "$(typeof(err)): Cannot add `SingleVariable`-in-`$(S2)` constraint " * + "for variable $(x) as a `SingleVariable`-in-`$(S1)` constraint was " * + "already set for this variable and both constraints set an upper bound." return end @@ -67,10 +67,10 @@ function test_ScalarFunctionConstantNotZero_error() S = MOI.EqualTo{T} err = MOI.ScalarFunctionConstantNotZero{T,F,S}(one(T)) @test sprint(showerror, err) == - "In `$F`-in-`$S` constraint: Constant $(one(T)) of the function is " * - "not zero. The function constant should be moved to the set. You can " * - "use `MOI.Utilities.normalize_and_add_constraint` which does this " * - "automatically." + "In `$F`-in-`$S` constraint: Constant $(one(T)) of the function is " * + "not zero. The function constant should be moved to the set. You can " * + "use `MOI.Utilities.normalize_and_add_constraint` which does this " * + "automatically." return end