Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 25, 2024
1 parent b99e039 commit 619f6ab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/Bridges/Bridges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ include("precompile.jl")
function _test_structural_identical(
a::MOI.ModelLike,
b::MOI.ModelLike;
allow_constraint_function_error::Bool = false,
cannot_unbridge::Bool = false,
)
# Test that the variables are the same. We make the strong assumption that
# the variables are added in the same order to both models.
Expand Down Expand Up @@ -194,10 +194,10 @@ function _test_structural_identical(
Test.@test MOI.supports_constraint(b, F, S)
# Check that each function in `b` matches a function in `a`
for ci in MOI.get(b, MOI.ListOfConstraintIndices{F,S}())
try
f_b = MOI.get(b, MOI.ConstraintFunction(), ci)
f_b = try
MOI.get(b, MOI.ConstraintFunction(), ci)
catch err
if allow_constraint_function_error &&
if cannot_unbridge &&
err isa MOI.GetAttributeNotAllowed{MOI.ConstraintFunction}
continue
else
Expand Down Expand Up @@ -238,13 +238,18 @@ end
variable_start = 1.2,
constraint_start = 1.2,
eltype = Float64,
cannot_unbridge::Bool = false,
)
Run a series of tests that check the correctness of `Bridge`.
`input_fn` and `output_fn` are functions such that `input_fn(model)`
and `output_fn(model)` load the corresponding model into `model`.
Set `cannot_unbridge` to `true` if the bridge is a variable bridge
that does not supports [`Variables.unbridged_func`](@ref) so that
the tests allow errors that can be raised due to this.
## Example
```jldoctest; setup=:(import MathOptInterface as MOI)
Expand All @@ -266,7 +271,7 @@ function runtests(
constraint_start = 1.2,
eltype = Float64,
print_inner_model::Bool = false,
allow_outer_constraint_function_error::Bool = false,
cannot_unbridge::Bool = false,
)
# Load model and bridge it
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
Expand All @@ -284,7 +289,7 @@ function runtests(
_test_structural_identical(
test,
model;
allow_constraint_function_error = allow_outer_constraint_function_error,
cannot_unbridge = cannot_unbridge,
)
# Load a bridged target model, and check that getters are the same.
target = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
Expand All @@ -309,7 +314,17 @@ function runtests(
# Test ConstraintPrimalStart and ConstraintDualStart
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
for ci in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
set = MOI.get(model, MOI.ConstraintSet(), ci)
set = try
MOI.get(model, MOI.ConstraintSet(), ci)
catch err
# Could be thrown by `unbridged_function`
if cannot_unbridge &&
err isa MOI.GetAttributeNotAllowed{MOI.ConstraintFunction}
continue
else
rethrow(err)

Check warning on line 325 in src/Bridges/Bridges.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L325

Added line #L325 was not covered by tests
end
end
for attr in (MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart())
if MOI.supports(model, attr, MOI.ConstraintIndex{F,S})
MOI.set(model, attr, ci, nothing)
Expand Down
15 changes: 15 additions & 0 deletions test/Bridges/Variable/zeros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ end

include("../utilities.jl")

function test_runtests()
MOI.Bridges.runtests(
MOI.Bridges.Variable.ZerosBridge,
model -> begin
x, _ = MOI.add_constrained_variables(model, MOI.Zeros(2))
MOI.add_constraint(model, 1.0 * x[1] + 2.0 * x[2], MOI.EqualTo(3.0))
end,
model -> begin
MOI.add_constraint(model, zero(MOI.ScalarAffineFunction{Float64}), MOI.EqualTo(3.0))
end;
cannot_unbridge = true,
)
return
end

function test_zeros()
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
bridged_mock = MOI.Bridges.Variable.Zeros{Float64}(mock)
Expand Down

0 comments on commit 619f6ab

Please sign in to comment.