From 5148524410ddea05fb971f511062029a9cb1d732 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 20 Feb 2025 15:10:17 +1300 Subject: [PATCH 1/4] [Utilities] improve test coverage --- test/Utilities/model.jl | 32 +++++++++++++++++++++++++++++ test/Utilities/print.jl | 11 ++++++++++ test/Utilities/product_of_sets.jl | 18 ++++++---------- test/Utilities/universalfallback.jl | 23 +++++++++++++++++++++ 4 files changed, 72 insertions(+), 12 deletions(-) diff --git a/test/Utilities/model.jl b/test/Utilities/model.jl index c8790aaf2a..142e6dee5f 100644 --- a/test/Utilities/model.jl +++ b/test/Utilities/model.jl @@ -584,6 +584,38 @@ function test_variable_coefficient_VectorQuadraticFunction() return end +function test_constraints_invalid_index() + model = MOI.Utilities.Model{Float64}() + F, S = FunctionNotSupportedBySolvers, MOI.ZeroOne + ci = MOI.ConstraintIndex{F,S}(1) + @test_throws( + MOI.InvalidIndex(ci), + MOI.Utilities.constraints(model.constraints, ci), + ) + return +end + +function test_struct_of_constraints_by_set_types() + MOI.Utilities.@struct_of_constraints_by_set_types( + MySet1, + Union{MOI.LessThan{T},MOI.GreaterThan{T}}, + MOI.EqualTo{T}, + MOI.ZeroOne, + Int, + ) + set = MySet1{Float64,Vector{Float64},Vector{Float64},Vector{Bool},Int}() + @test set.num_variables == 0 + @test set.int === nothing + set.int = 1 + @test set.moi_zeroone === nothing + set.moi_zeroone = Bool[] + @test set.moi_equalto === nothing + set.moi_equalto = Float64[] + @test set.moi_lessthan === nothing + set.moi_lessthan = Float64[] + return +end + end # module TestModel.runtests() diff --git a/test/Utilities/print.jl b/test/Utilities/print.jl index 3ea458ff83..a22f12e67c 100644 --- a/test/Utilities/print.jl +++ b/test/Utilities/print.jl @@ -751,6 +751,17 @@ function test_print_model_to_stdout() return end +function test_print_constraint_name_unsupported() + model = MOI.Utilities.MockOptimizer( + MOI.Utilities.Model{Float64}(); + supports_names = false, + ) + x = MOI.add_variable(model) + MOI.add_constraint(model, 1.0 * x, MOI.LessThan(1.0)) + @test occursin("0.0 + 1.0 v[$(x.value)] <= 1.0", sprint(print, model)) + return +end + end TestPrint.runtests() diff --git a/test/Utilities/product_of_sets.jl b/test/Utilities/product_of_sets.jl index b42bb2b2df..7a913ecb47 100644 --- a/test/Utilities/product_of_sets.jl +++ b/test/Utilities/product_of_sets.jl @@ -50,23 +50,17 @@ end function test_scalar_basic() sets = _ScalarSets{Float64}() - ci = MOI.ConstraintIndex{ - MOI.ScalarAffineFunction{Float64}, - MOI.EqualTo{Float64}, - }( - 12345, - ) + F, S = MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64} + ci = MOI.ConstraintIndex{F,S}(12345) @test !MOI.is_valid(sets, ci) i = MOI.Utilities.set_index(sets, MOI.EqualTo{Float64}) ci_value = MOI.Utilities.add_set(sets, i) - ci = MOI.ConstraintIndex{ - MOI.ScalarAffineFunction{Float64}, - MOI.EqualTo{Float64}, - }( - ci_value, - ) + ci = MOI.ConstraintIndex{F,S}(ci_value) @test MOI.is_valid(sets, ci) @test MOI.Utilities.rows(sets, ci) == ci.value + ci = MOI.ConstraintIndex{F,MOI.ZeroOne}(1) + @test !MOI.is_valid(sets, ci) + return end function test_scalar_dimension() diff --git a/test/Utilities/universalfallback.jl b/test/Utilities/universalfallback.jl index fae6c8df3f..39477e2130 100644 --- a/test/Utilities/universalfallback.jl +++ b/test/Utilities/universalfallback.jl @@ -487,6 +487,29 @@ function test_ListOfConstraintsWithAttributeSet() return end +function test_delete_ci_attribute() + model = MOI.Utilities.UniversalFallback( + ModelForUniversalFallback{Float64}(), + ) + x = MOI.add_variable(model) + c = MOI.add_constraint(model, MOI.VectorOfVariables([x]), MOI.Nonnegatives(1)) + MOI.set(model, MOI.ConstraintPrimalStart(), c, [1.0]) + @test model.conattr[MOI.ConstraintPrimalStart()][c] == [1.0] + MOI.delete(model, x) + @test !haskey(model.conattr[MOI.ConstraintPrimalStart()], c) + # Vector + model = MOI.Utilities.UniversalFallback( + ModelForUniversalFallback{Float64}(), + ) + x = MOI.add_variable(model) + c = MOI.add_constraint(model, MOI.VectorOfVariables([x]), MOI.Nonnegatives(1)) + MOI.set(model, MOI.ConstraintPrimalStart(), c, [1.0]) + @test model.conattr[MOI.ConstraintPrimalStart()][c] == [1.0] + MOI.delete(model, [x]) + @test !haskey(model.conattr[MOI.ConstraintPrimalStart()], c) + return +end + end # module TestUniversalFallback.runtests() From e529302008dc76d7cd8366b8189302d80e7c75c7 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 20 Feb 2025 15:16:09 +1300 Subject: [PATCH 2/4] Fix formatting --- test/Utilities/universalfallback.jl | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test/Utilities/universalfallback.jl b/test/Utilities/universalfallback.jl index 39477e2130..ab85b19199 100644 --- a/test/Utilities/universalfallback.jl +++ b/test/Utilities/universalfallback.jl @@ -488,21 +488,27 @@ function test_ListOfConstraintsWithAttributeSet() end function test_delete_ci_attribute() - model = MOI.Utilities.UniversalFallback( - ModelForUniversalFallback{Float64}(), - ) + model = + MOI.Utilities.UniversalFallback(ModelForUniversalFallback{Float64}()) x = MOI.add_variable(model) - c = MOI.add_constraint(model, MOI.VectorOfVariables([x]), MOI.Nonnegatives(1)) + c = MOI.add_constraint( + model, + MOI.VectorOfVariables([x]), + MOI.Nonnegatives(1), + ) MOI.set(model, MOI.ConstraintPrimalStart(), c, [1.0]) @test model.conattr[MOI.ConstraintPrimalStart()][c] == [1.0] MOI.delete(model, x) @test !haskey(model.conattr[MOI.ConstraintPrimalStart()], c) # Vector - model = MOI.Utilities.UniversalFallback( - ModelForUniversalFallback{Float64}(), - ) + model = + MOI.Utilities.UniversalFallback(ModelForUniversalFallback{Float64}()) x = MOI.add_variable(model) - c = MOI.add_constraint(model, MOI.VectorOfVariables([x]), MOI.Nonnegatives(1)) + c = MOI.add_constraint( + model, + MOI.VectorOfVariables([x]), + MOI.Nonnegatives(1), + ) MOI.set(model, MOI.ConstraintPrimalStart(), c, [1.0]) @test model.conattr[MOI.ConstraintPrimalStart()][c] == [1.0] MOI.delete(model, [x]) From 15f4a6fffebdf703763923f7fea1c36f5c240838 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 20 Feb 2025 15:16:50 +1300 Subject: [PATCH 3/4] More tests --- test/Utilities/parser.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/Utilities/parser.jl b/test/Utilities/parser.jl index 4c46501a4b..303b2ddbcc 100644 --- a/test/Utilities/parser.jl +++ b/test/Utilities/parser.jl @@ -441,6 +441,10 @@ function test_parse_unrecognized_expression() ErrorException("Unrecognized expression []"), MOI.Utilities.loadfromstring!(model, "[]"), ) + @test_throws( + ErrorException("Unrecognized expression foo(x)"), + MOI.Utilities.loadfromstring!(model, "variables: x\nfoo(x)"), + ) return end From 792cd12836235698cec6c98e10a50a2f9e118add Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 20 Feb 2025 17:18:01 +1300 Subject: [PATCH 4/4] Update model.jl --- test/Utilities/model.jl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/test/Utilities/model.jl b/test/Utilities/model.jl index 142e6dee5f..62d5d57a7e 100644 --- a/test/Utilities/model.jl +++ b/test/Utilities/model.jl @@ -595,15 +595,16 @@ function test_constraints_invalid_index() return end +MOI.Utilities.@struct_of_constraints_by_set_types( + Set4802, + Union{MOI.LessThan{T},MOI.GreaterThan{T}}, + MOI.EqualTo{T}, + MOI.ZeroOne, + Int, +) + function test_struct_of_constraints_by_set_types() - MOI.Utilities.@struct_of_constraints_by_set_types( - MySet1, - Union{MOI.LessThan{T},MOI.GreaterThan{T}}, - MOI.EqualTo{T}, - MOI.ZeroOne, - Int, - ) - set = MySet1{Float64,Vector{Float64},Vector{Float64},Vector{Bool},Int}() + set = Set4802{Float64,Vector{Float64},Vector{Float64},Vector{Bool},Int}() @test set.num_variables == 0 @test set.int === nothing set.int = 1