Skip to content

Commit 619f6ab

Browse files
committed
Add tests
1 parent b99e039 commit 619f6ab

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/Bridges/Bridges.jl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ include("precompile.jl")
147147
function _test_structural_identical(
148148
a::MOI.ModelLike,
149149
b::MOI.ModelLike;
150-
allow_constraint_function_error::Bool = false,
150+
cannot_unbridge::Bool = false,
151151
)
152152
# Test that the variables are the same. We make the strong assumption that
153153
# the variables are added in the same order to both models.
@@ -194,10 +194,10 @@ function _test_structural_identical(
194194
Test.@test MOI.supports_constraint(b, F, S)
195195
# Check that each function in `b` matches a function in `a`
196196
for ci in MOI.get(b, MOI.ListOfConstraintIndices{F,S}())
197-
try
198-
f_b = MOI.get(b, MOI.ConstraintFunction(), ci)
197+
f_b = try
198+
MOI.get(b, MOI.ConstraintFunction(), ci)
199199
catch err
200-
if allow_constraint_function_error &&
200+
if cannot_unbridge &&
201201
err isa MOI.GetAttributeNotAllowed{MOI.ConstraintFunction}
202202
continue
203203
else
@@ -238,13 +238,18 @@ end
238238
variable_start = 1.2,
239239
constraint_start = 1.2,
240240
eltype = Float64,
241+
cannot_unbridge::Bool = false,
241242
)
242243
243244
Run a series of tests that check the correctness of `Bridge`.
244245
245246
`input_fn` and `output_fn` are functions such that `input_fn(model)`
246247
and `output_fn(model)` load the corresponding model into `model`.
247248
249+
Set `cannot_unbridge` to `true` if the bridge is a variable bridge
250+
that does not supports [`Variables.unbridged_func`](@ref) so that
251+
the tests allow errors that can be raised due to this.
252+
248253
## Example
249254
250255
```jldoctest; setup=:(import MathOptInterface as MOI)
@@ -266,7 +271,7 @@ function runtests(
266271
constraint_start = 1.2,
267272
eltype = Float64,
268273
print_inner_model::Bool = false,
269-
allow_outer_constraint_function_error::Bool = false,
274+
cannot_unbridge::Bool = false,
270275
)
271276
# Load model and bridge it
272277
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
@@ -284,7 +289,7 @@ function runtests(
284289
_test_structural_identical(
285290
test,
286291
model;
287-
allow_constraint_function_error = allow_outer_constraint_function_error,
292+
cannot_unbridge = cannot_unbridge,
288293
)
289294
# Load a bridged target model, and check that getters are the same.
290295
target = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{eltype}())
@@ -309,7 +314,17 @@ function runtests(
309314
# Test ConstraintPrimalStart and ConstraintDualStart
310315
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
311316
for ci in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
312-
set = MOI.get(model, MOI.ConstraintSet(), ci)
317+
set = try
318+
MOI.get(model, MOI.ConstraintSet(), ci)
319+
catch err
320+
# Could be thrown by `unbridged_function`
321+
if cannot_unbridge &&
322+
err isa MOI.GetAttributeNotAllowed{MOI.ConstraintFunction}
323+
continue
324+
else
325+
rethrow(err)
326+
end
327+
end
313328
for attr in (MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart())
314329
if MOI.supports(model, attr, MOI.ConstraintIndex{F,S})
315330
MOI.set(model, attr, ci, nothing)

test/Bridges/Variable/zeros.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ end
2323

2424
include("../utilities.jl")
2525

26+
function test_runtests()
27+
MOI.Bridges.runtests(
28+
MOI.Bridges.Variable.ZerosBridge,
29+
model -> begin
30+
x, _ = MOI.add_constrained_variables(model, MOI.Zeros(2))
31+
MOI.add_constraint(model, 1.0 * x[1] + 2.0 * x[2], MOI.EqualTo(3.0))
32+
end,
33+
model -> begin
34+
MOI.add_constraint(model, zero(MOI.ScalarAffineFunction{Float64}), MOI.EqualTo(3.0))
35+
end;
36+
cannot_unbridge = true,
37+
)
38+
return
39+
end
40+
2641
function test_zeros()
2742
mock = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
2843
bridged_mock = MOI.Bridges.Variable.Zeros{Float64}(mock)

0 commit comments

Comments
 (0)