Skip to content

Commit 0cd9293

Browse files
add size error test
1 parent c8d3131 commit 0cd9293

1 file changed

Lines changed: 63 additions & 3 deletions

File tree

test/vno_bridge.jl

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,69 @@ function test_VectorNonlinearOracle_bridge_utility_paths()
635635
MOI.delete(mock, b_delete)
636636
@test mock.deleted == MOI.ConstraintIndex[leq(10), leq(11), geq(20), eq(30)]
637637

638-
# Length mismatch should hit the early return path without setting any dual starts.
639-
MOI.set(mock, MOI.ConstraintDualStart(), bridge, [1.0, 2.0])
640-
@test isempty(mock.dual_start)
638+
# Length mismatch should now throw an informative error.
639+
@test_throws ErrorException MOI.set(
640+
mock,
641+
MOI.ConstraintDualStart(),
642+
bridge,
643+
[1.0, 2.0],
644+
)
645+
end
646+
647+
function test_VectorNonlinearOracle_dual_start_size_error()
648+
# Passing a value vector of unexpected length to ConstraintDualStart should
649+
# throw an error with a message that reports the expected dimensions.
650+
NLP = DiffOpt.NonLinearProgram
651+
652+
function eval_f(ret::AbstractVector, z::AbstractVector)
653+
ret[1] = z[1]^2 + z[2]^2
654+
return
655+
end
656+
jacobian_structure = [(1, 1), (1, 2)]
657+
function eval_jacobian(ret::AbstractVector, z::AbstractVector)
658+
ret[1] = 2.0 * z[1]
659+
ret[2] = 2.0 * z[2]
660+
return
661+
end
662+
hessian_lagrangian_structure = [(1, 1), (2, 2)]
663+
function eval_hessian_lagrangian(
664+
ret::AbstractVector,
665+
z::AbstractVector,
666+
μ::AbstractVector,
667+
)
668+
ret[1] = 2.0 * μ[1]
669+
ret[2] = 2.0 * μ[1]
670+
return
671+
end
672+
673+
# m = 1 output, n = 2 inputs
674+
set = MOI.VectorNonlinearOracle(;
675+
dimension = 2,
676+
l = [-Inf],
677+
u = [1.0],
678+
eval_f,
679+
jacobian_structure,
680+
eval_jacobian,
681+
hessian_lagrangian_structure,
682+
eval_hessian_lagrangian,
683+
)
684+
f = MOI.VectorOfVariables([MOI.VariableIndex(1), MOI.VariableIndex(2)])
685+
686+
leq = MOI.ConstraintIndex{MOI.ScalarNonlinearFunction,MOI.LessThan{Float64}}
687+
bridge = NLP.VNOToScalarNLBridge{Float64}(f, set, [leq(1)], [], [])
688+
689+
mock = _BridgeMockModel()
690+
691+
# Length 3 is neither m (1) nor n (2) – must error.
692+
err = @test_throws ErrorException MOI.set(
693+
mock,
694+
MOI.ConstraintDualStart(),
695+
bridge,
696+
[1.0, 2.0, 3.0],
697+
)
698+
@test occursin("output dimension", err.value.msg)
699+
@test occursin("input dimension", err.value.msg)
700+
@test occursin("3", err.value.msg)
641701
end
642702

643703
end

0 commit comments

Comments
 (0)