Skip to content

Commit 2dd0acc

Browse files
test: cover Optim extension error path
1 parent 92f590c commit 2dd0acc

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

ext/DynamicExpressionsOptimExt.jl

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,15 @@ function wrap_func(
9191
end
9292

9393
const _INPLACEOBJECTIVE_SPEC_V8 = (
94-
fields = (:fdf, :fgh, :hvp, :fghvp, :fjvp),
95-
x_last = (:fdf, :fgh),
96-
xv_tail = (:hvp, :fghvp, :fjvp),
94+
fields=(:fdf, :fgh, :hvp, :fghvp, :fjvp),
95+
x_last=(:fdf, :fgh),
96+
xv_tail=(:hvp, :fghvp, :fjvp),
9797
)
9898
const _INPLACEOBJECTIVE_SPEC_V7 = (
99-
fields = (:df, :fdf, :fgh, :hv, :fghv),
100-
x_last = (:df, :fdf, :fgh),
101-
xv_tail = (:hv, :fghv),
99+
fields=(:df, :fdf, :fgh, :hv, :fghv), x_last=(:df, :fdf, :fgh), xv_tail=(:hv, :fghv)
102100
)
103101
const _INPLACEOBJECTIVE_SPEC_OLD = (
104-
fields = (:fdf, :fgh, :hv, :fghv),
105-
x_last = (:fdf, :fgh),
106-
xv_tail = (:hv, :fghv),
102+
fields=(:fdf, :fgh, :hv, :fghv), x_last=(:fdf, :fgh), xv_tail=(:hv, :fghv)
107103
)
108104

109105
@inline function _wrap_inplaceobjective_field(
@@ -141,7 +137,8 @@ function wrap_func(
141137
#
142138
# We use `@static` branching so that only the relevant layout for the *installed*
143139
# NLSolversBase version is compiled/instrumented.
144-
@static if fieldnames(NLSolversBase.InplaceObjective) == _INPLACEOBJECTIVE_SPEC_V8.fields
140+
@static if fieldnames(NLSolversBase.InplaceObjective) ==
141+
_INPLACEOBJECTIVE_SPEC_V8.fields
145142
# NLSolversBase v8 / Optim v2
146143
return _wrap_inplaceobjective(f, tree, refs, _INPLACEOBJECTIVE_SPEC_V8)
147144
elseif fieldnames(NLSolversBase.InplaceObjective) == _INPLACEOBJECTIVE_SPEC_V7.fields

test/test_optim.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,31 @@ end
206206
wrapped = ext._wrap_objective_xv_tail((args...) -> nothing, tree, refs)
207207
@test_throws ArgumentError wrapped(x0)
208208
end
209+
210+
@testitem "Wrap InplaceObjective: error on unknown field" begin
211+
using DynamicExpressions, Optim
212+
include("test_optim_setup.jl")
213+
214+
ext = Base.get_extension(DynamicExpressions, :DynamicExpressionsOptimExt)
215+
216+
tree = copy(original_tree)
217+
_, refs = get_scalar_constants(tree)
218+
219+
# Construct a dummy InplaceObjective for the *installed* NLSolversBase/Optim
220+
# layout, then directly hit the internal error branch.
221+
fields = fieldnames(Optim.NLSolversBase.InplaceObjective)
222+
dummy = (args...) -> nothing
223+
obj = Optim.NLSolversBase.InplaceObjective((dummy for _ in fields)...)
224+
225+
spec = if fields == ext._INPLACEOBJECTIVE_SPEC_V8.fields
226+
ext._INPLACEOBJECTIVE_SPEC_V8
227+
elseif fields == ext._INPLACEOBJECTIVE_SPEC_V7.fields
228+
ext._INPLACEOBJECTIVE_SPEC_V7
229+
else
230+
ext._INPLACEOBJECTIVE_SPEC_OLD
231+
end
232+
233+
@test_throws ArgumentError ext._wrap_inplaceobjective_field(
234+
Val(:__unknown_field__), obj, tree, refs, spec
235+
)
236+
end

0 commit comments

Comments
 (0)