Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add any function for Bridges.runtests #2497

Merged
merged 4 commits into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 61 additions & 18 deletions src/Bridges/Bridges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,39 +220,35 @@
"""
runtests(
Bridge::Type{<:AbstractBridge},
input::String,
output::String;
input_fn::Function,
output_fn::Function;
variable_start = 1.2,
constraint_start = 1.2,
eltype = Float64,
)

Run a series of tests that check the correctness of `Bridge`.

`input` and `output` are models in the style of
[`MOI.Utilities.loadfromstring!`](@ref).
`input_fn` and `output_fn` are functions such that `input_fn(model)`
and `output_fn(model)` load the corresponding model into `model`.

## Example

```jldoctest; setup=:(import MathOptInterface as MOI)
julia> MOI.Bridges.runtests(
MOI.Bridges.Constraint.ZeroOneBridge,
\"\"\"
variables: x
x in ZeroOne()
\"\"\",
\"\"\"
variables: x
x in Integer()
1.0 * x in Interval(0.0, 1.0)
\"\"\",
model -> MOI.add_constrained_variable(model, MOI.ZeroOne()),
model -> begin
x, _ = MOI.add_constrained_variable(model, MOI.Integer())
MOI.add_constraint(model, 1.0 * x, MOI.Interval(0.0, 1.0))
end,
)
```
"""
function runtests(
Bridge::Type{<:AbstractBridge},
input::String,
output::String;
input_fn::Function,
output_fn::Function;
variable_start = 1.2,
constraint_start = 1.2,
eltype = Float64,
Expand All @@ -261,7 +257,7 @@
# Load model and bridge it
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
model = _bridged_model(Bridge{eltype}, inner)
MOI.Utilities.loadfromstring!(model, input)
input_fn(model)

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

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L260

Added line #L260 was not covered by tests
final_touch(model)
# Should be able to call final_touch multiple times.
final_touch(model)
Expand All @@ -270,11 +266,11 @@
end
# Load a non-bridged input model, and check that getters are the same.
test = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
MOI.Utilities.loadfromstring!(test, input)
input_fn(test)

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

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L269

Added line #L269 was not covered by tests
_test_structural_identical(test, model)
# Load a bridged target model, and check that getters are the same.
target = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
MOI.Utilities.loadfromstring!(target, output)
output_fn(target)

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

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L273

Added line #L273 was not covered by tests
_test_structural_identical(target, inner)
# Test VariablePrimalStart
attr = MOI.VariablePrimalStart()
Expand Down Expand Up @@ -318,6 +314,53 @@
return
end

"""
runtests(
Bridge::Type{<:AbstractBridge},
input::String,
output::String;
variable_start = 1.2,
constraint_start = 1.2,
eltype = Float64,
)

Run a series of tests that check the correctness of `Bridge`.

`input` and `output` are models in the style of
[`MOI.Utilities.loadfromstring!`](@ref).

## Example

```jldoctest; setup=:(import MathOptInterface as MOI)
julia> MOI.Bridges.runtests(
MOI.Bridges.Constraint.ZeroOneBridge,
\"\"\"
variables: x
x in ZeroOne()
\"\"\",
\"\"\"
variables: x
x in Integer()
1.0 * x in Interval(0.0, 1.0)
\"\"\",
)
```
"""
function runtests(

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

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L349

Added line #L349 was not covered by tests
Bridge::Type{<:AbstractBridge},
input::String,
output::String;
kwargs...,
)
runtests(

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

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L355

Added line #L355 was not covered by tests
Bridge,
model -> MOI.Utilities.loadfromstring!(model, input),
model -> MOI.Utilities.loadfromstring!(model, output);

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

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L357-L358

Added lines #L357 - L358 were not covered by tests
kwargs...,
)
return

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

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Bridges.jl#L361

Added line #L361 was not covered by tests
end

_test_delete(::Type{<:Variable.AbstractBridge}, model, inner) = nothing

function _test_delete(Bridge, model, inner)
Expand Down
Loading