Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 26, 2023
1 parent dba4848 commit 1788385
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ function function_string(::MIME"text/latex", x::GenericNonlinearExpr)
if arg isa GenericNonlinearExpr
if arg.head in _PREFIX_OPERATORS && length(arg.args) > 1
if _needs_parentheses(arg.args[1])
print(io, "\\left({")
print(io, "{\\left({")
end
if _needs_parentheses(arg.args[end])
push!(stack, "}\\right)")
push!(stack, "}\\right)}")
end
for i in length(arg.args):-1:2
push!(stack, arg.args[i])
Expand Down
15 changes: 14 additions & 1 deletion test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ function test_extension_latex(ModelType = Model, VariableRefType = VariableRef)
@expression(model, g, ifelse(x > 0, sin(x), x + cos(x)^2))
@test function_string(MIME("text/latex"), g) ==
raw"\textsf{ifelse}\left({x} > {0}, {\textsf{sin}\left({x}\right)}, {x} + {\left({\textsf{cos}\left({x}\right)} ^ {2.0}\right)}\right)"
@test function_string(MIME("text/latex"), (x + 1) / (x + 1)) ==
raw"{\left({x + 1}\right)} / {\left({x + 1}\right)}"
return
end

Expand Down Expand Up @@ -470,6 +472,7 @@ function test_register_univariate()
@variable(model, x)
@register(model, f, 1, x -> x^2)
@test f isa NonlinearOperator
@test sprint(show, f) == "NonlinearOperator(:f, $(f.func))"
@test isequal_canonical(@expression(model, f(x)), f(x))
@test isequal_canonical(f(x), GenericNonlinearExpr(:f, Any[x]))
attrs = MOI.get(model, MOI.ListOfModelAttributesSet())
Expand Down Expand Up @@ -509,7 +512,7 @@ function test_register_univariate_gradient_hessian()
return
end

function test_register_multivariate_()
function test_register_multivariate()
model = Model()
@variable(model, x[1:2])
f = (x...) -> sum(x .^ 2)
Expand Down Expand Up @@ -552,6 +555,16 @@ function test_register_multivariate_gradient_hessian()
return
end

function test_register_multivariate_many_args()
model = Model()
@variable(model, x[1:10])
f = (x...) -> sum(x .^ 2)
@register(model, foo, 10, f)
@test isequal_canonical(foo(x...), GenericNonlinearExpr(:foo, Any[x...]))
@test foo((1:10)...) == 385
return
end

function test_register_errors()
model = Model()
f = x -> x^2
Expand Down

0 comments on commit 1788385

Please sign in to comment.