Skip to content

Commit 5c9d6fb

Browse files
authored
[Bridges] add input and output functions in Bridges.runtests (#2497)
1 parent dfea155 commit 5c9d6fb

File tree

1 file changed

+61
-18
lines changed

1 file changed

+61
-18
lines changed

src/Bridges/Bridges.jl

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -220,39 +220,35 @@ end
220220
"""
221221
runtests(
222222
Bridge::Type{<:AbstractBridge},
223-
input::String,
224-
output::String;
223+
input_fn::Function,
224+
output_fn::Function;
225225
variable_start = 1.2,
226226
constraint_start = 1.2,
227227
eltype = Float64,
228228
)
229229
230230
Run a series of tests that check the correctness of `Bridge`.
231231
232-
`input` and `output` are models in the style of
233-
[`MOI.Utilities.loadfromstring!`](@ref).
232+
`input_fn` and `output_fn` are functions such that `input_fn(model)`
233+
and `output_fn(model)` load the corresponding model into `model`.
234234
235235
## Example
236236
237237
```jldoctest; setup=:(import MathOptInterface as MOI)
238238
julia> MOI.Bridges.runtests(
239239
MOI.Bridges.Constraint.ZeroOneBridge,
240-
\"\"\"
241-
variables: x
242-
x in ZeroOne()
243-
\"\"\",
244-
\"\"\"
245-
variables: x
246-
x in Integer()
247-
1.0 * x in Interval(0.0, 1.0)
248-
\"\"\",
240+
model -> MOI.add_constrained_variable(model, MOI.ZeroOne()),
241+
model -> begin
242+
x, _ = MOI.add_constrained_variable(model, MOI.Integer())
243+
MOI.add_constraint(model, 1.0 * x, MOI.Interval(0.0, 1.0))
244+
end,
249245
)
250246
```
251247
"""
252248
function runtests(
253249
Bridge::Type{<:AbstractBridge},
254-
input::String,
255-
output::String;
250+
input_fn::Function,
251+
output_fn::Function;
256252
variable_start = 1.2,
257253
constraint_start = 1.2,
258254
eltype = Float64,
@@ -261,7 +257,7 @@ function runtests(
261257
# Load model and bridge it
262258
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
263259
model = _bridged_model(Bridge{eltype}, inner)
264-
MOI.Utilities.loadfromstring!(model, input)
260+
input_fn(model)
265261
final_touch(model)
266262
# Should be able to call final_touch multiple times.
267263
final_touch(model)
@@ -270,11 +266,11 @@ function runtests(
270266
end
271267
# Load a non-bridged input model, and check that getters are the same.
272268
test = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
273-
MOI.Utilities.loadfromstring!(test, input)
269+
input_fn(test)
274270
_test_structural_identical(test, model)
275271
# Load a bridged target model, and check that getters are the same.
276272
target = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
277-
MOI.Utilities.loadfromstring!(target, output)
273+
output_fn(target)
278274
_test_structural_identical(target, inner)
279275
# Test VariablePrimalStart
280276
attr = MOI.VariablePrimalStart()
@@ -318,6 +314,53 @@ function runtests(
318314
return
319315
end
320316

317+
"""
318+
runtests(
319+
Bridge::Type{<:AbstractBridge},
320+
input::String,
321+
output::String;
322+
variable_start = 1.2,
323+
constraint_start = 1.2,
324+
eltype = Float64,
325+
)
326+
327+
Run a series of tests that check the correctness of `Bridge`.
328+
329+
`input` and `output` are models in the style of
330+
[`MOI.Utilities.loadfromstring!`](@ref).
331+
332+
## Example
333+
334+
```jldoctest; setup=:(import MathOptInterface as MOI)
335+
julia> MOI.Bridges.runtests(
336+
MOI.Bridges.Constraint.ZeroOneBridge,
337+
\"\"\"
338+
variables: x
339+
x in ZeroOne()
340+
\"\"\",
341+
\"\"\"
342+
variables: x
343+
x in Integer()
344+
1.0 * x in Interval(0.0, 1.0)
345+
\"\"\",
346+
)
347+
```
348+
"""
349+
function runtests(
350+
Bridge::Type{<:AbstractBridge},
351+
input::String,
352+
output::String;
353+
kwargs...,
354+
)
355+
runtests(
356+
Bridge,
357+
model -> MOI.Utilities.loadfromstring!(model, input),
358+
model -> MOI.Utilities.loadfromstring!(model, output);
359+
kwargs...,
360+
)
361+
return
362+
end
363+
321364
_test_delete(::Type{<:Variable.AbstractBridge}, model, inner) = nothing
322365

323366
function _test_delete(Bridge, model, inner)

0 commit comments

Comments
 (0)