|
| 1 | +# Copyright (c) 2017: Miles Lubin and contributors |
| 2 | +# Copyright (c) 2017: Google Inc. |
| 3 | +# |
| 4 | +# Use of this source code is governed by an MIT-style license that can be found |
| 5 | +# in the LICENSE.md file or at https://opensource.org/licenses/MIT. |
| 6 | + |
| 7 | +# Test with a bridge for which the map is defined on the bridge value and not |
| 8 | +# the bridge type |
| 9 | +module TestAbstractBridge |
| 10 | + |
| 11 | +using Test |
| 12 | + |
| 13 | +import MathOptInterface as MOI |
| 14 | + |
| 15 | +# A minimal constraint bridge that doesn't implement ConstraintFunction getter. |
| 16 | +# Used to test that `cannot_unbridge = true` works with the default bridge |
| 17 | +# `MOI.get` (which throws `GetAttributeNotAllowed`). |
| 18 | +struct _NoUnbridgeTestBridge{T} <: MOI.Bridges.Constraint.AbstractBridge |
| 19 | + ci::MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}} |
| 20 | +end |
| 21 | + |
| 22 | +function MOI.Bridges.Constraint.bridge_constraint( |
| 23 | + ::Type{_NoUnbridgeTestBridge{T}}, |
| 24 | + model::MOI.ModelLike, |
| 25 | + f::MOI.ScalarAffineFunction{T}, |
| 26 | + ::MOI.GreaterThan{T}, |
| 27 | +) where {T} |
| 28 | + ci = MOI.add_constraint(model, f, MOI.EqualTo(zero(T))) |
| 29 | + return _NoUnbridgeTestBridge{T}(ci) |
| 30 | +end |
| 31 | + |
| 32 | +function MOI.supports_constraint( |
| 33 | + ::Type{_NoUnbridgeTestBridge{T}}, |
| 34 | + ::Type{MOI.ScalarAffineFunction{T}}, |
| 35 | + ::Type{MOI.GreaterThan{T}}, |
| 36 | +) where {T} |
| 37 | + return true |
| 38 | +end |
| 39 | + |
| 40 | +function MOI.Bridges.Constraint.concrete_bridge_type( |
| 41 | + ::Type{_NoUnbridgeTestBridge{T}}, |
| 42 | + ::Type{MOI.ScalarAffineFunction{T}}, |
| 43 | + ::Type{MOI.GreaterThan{T}}, |
| 44 | +) where {T} |
| 45 | + return _NoUnbridgeTestBridge{T} |
| 46 | +end |
| 47 | + |
| 48 | +function MOI.Bridges.added_constraint_types( |
| 49 | + ::Type{_NoUnbridgeTestBridge{T}}, |
| 50 | +) where {T} |
| 51 | + return Tuple{Type,Type}[(MOI.ScalarAffineFunction{T}, MOI.EqualTo{T})] |
| 52 | +end |
| 53 | + |
| 54 | +function MOI.Bridges.added_constrained_variable_types( |
| 55 | + ::Type{<:_NoUnbridgeTestBridge}, |
| 56 | +) |
| 57 | + return Tuple{Type}[] |
| 58 | +end |
| 59 | + |
| 60 | +function MOI.get( |
| 61 | + ::_NoUnbridgeTestBridge{T}, |
| 62 | + ::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}}, |
| 63 | +) where {T} |
| 64 | + return 1 |
| 65 | +end |
| 66 | + |
| 67 | +function MOI.get( |
| 68 | + bridge::_NoUnbridgeTestBridge{T}, |
| 69 | + ::MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}}, |
| 70 | +) where {T} |
| 71 | + return [bridge.ci] |
| 72 | +end |
| 73 | + |
| 74 | +function MOI.delete(model::MOI.ModelLike, bridge::_NoUnbridgeTestBridge) |
| 75 | + MOI.delete(model, bridge.ci) |
| 76 | + return |
| 77 | +end |
| 78 | + |
| 79 | +# Note: ConstraintFunction and ConstraintSet getters are NOT implemented. |
| 80 | +# The default MOI.get on AbstractBridge throws GetAttributeNotAllowed. |
| 81 | + |
| 82 | +function test_cannot_unbridge_with_default_getter() |
| 83 | + # This test verifies that `cannot_unbridge = true` works when a constraint |
| 84 | + # bridge does not implement `ConstraintFunction` getter. Before the fix, |
| 85 | + # the default bridge `MOI.get` threw `ArgumentError` which was not caught |
| 86 | + # by `_runtests_error_handler`. |
| 87 | + MOI.Bridges.runtests( |
| 88 | + _NoUnbridgeTestBridge, |
| 89 | + model -> begin |
| 90 | + x = MOI.add_variable(model) |
| 91 | + MOI.add_constraint(model, 1.0 * x, MOI.GreaterThan(0.0)) |
| 92 | + end, |
| 93 | + model -> begin |
| 94 | + x = MOI.add_variable(model) |
| 95 | + MOI.add_constraint(model, 1.0 * x, MOI.EqualTo(0.0)) |
| 96 | + end; |
| 97 | + cannot_unbridge = true, |
| 98 | + ) |
| 99 | + return |
| 100 | +end |
| 101 | + |
| 102 | +function runtests() |
| 103 | + for name in names(@__MODULE__; all = true) |
| 104 | + if startswith("$(name)", "test_") |
| 105 | + @testset "$(name)" begin |
| 106 | + getfield(@__MODULE__, name)() |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + return |
| 111 | +end |
| 112 | + |
| 113 | +end |
| 114 | + |
| 115 | +TestAbstractBridge.runtests() |
0 commit comments