Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Oct 24, 2024
1 parent dd2d06b commit 82caf73
Showing 1 changed file with 42 additions and 46 deletions.
88 changes: 42 additions & 46 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,19 @@ using Test
import AmplNLWriter
import AmplNLWriter: MOI

function runtests(path)
function runtests(args...)
for name in names(@__MODULE__; all = true)
if !startswith("$(name)", "test_")
continue
end
@testset "$(name)" begin
getfield(@__MODULE__, name)(path)
if startswith("$(name)", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)(args...)
end
end
end
return
end

function optimizer(path, args...; kwargs...)
model = AmplNLWriter.Optimizer(path, args...; kwargs...)
MOI.set(model, MOI.RawOptimizerAttribute("print_level"), 0)
MOI.set(
model,
MOI.RawOptimizerAttribute("option_file_name"),
joinpath(@__DIR__, "ipopt.opt"),
)
function optimizer(args...; kwargs...)
model = AmplNLWriter.Optimizer(args...; kwargs...)
return MOI.Utilities.CachingOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
MOI.Bridges.full_bridge_optimizer(
Expand All @@ -42,20 +35,20 @@ function optimizer(path, args...; kwargs...)
)
end

function test_show(path)
@test sprint(show, AmplNLWriter.Optimizer(path)) == "An AMPL (.nl) model"
function test_show(args...)
@test sprint(show, AmplNLWriter.Optimizer(args...)) == "An AMPL (.nl) model"
end

function test_name(path)
model = AmplNLWriter.Optimizer(path)
function test_name(args...)
model = AmplNLWriter.Optimizer(args...)
@test MOI.supports(model, MOI.Name())
MOI.set(model, MOI.Name(), "Foo")
@test MOI.get(model, MOI.Name()) == "Foo"
end

function test_runtests(path)
function test_runtests(args...)
MOI.Test.runtests(
optimizer(path),
optimizer(args...),
MOI.Test.Config(
atol = 1e-4,
rtol = 1e-4,
Expand Down Expand Up @@ -93,16 +86,16 @@ function test_runtests(path)
return
end

function test_show(path)
@test sprint(show, AmplNLWriter.Optimizer(path)) == "An AMPL (.nl) model"
function test_show(args...)
@test sprint(show, AmplNLWriter.Optimizer(args...)) == "An AMPL (.nl) model"
end

function test_solver_name(path)
@test MOI.get(optimizer(path), MOI.SolverName()) == "AmplNLWriter"
function test_solver_name(args...)
@test MOI.get(optimizer(args...), MOI.SolverName()) == "AmplNLWriter"
end

function test_abstractoptimizer(path)
@test optimizer(path) isa MOI.AbstractOptimizer
function test_abstractoptimizer(args...)
@test optimizer(args...) isa MOI.AbstractOptimizer
end

function test_bad_string(::Any)
Expand All @@ -113,8 +106,8 @@ function test_bad_string(::Any)
@test occursin("IOError", MOI.get(model, MOI.RawStatusString()))
end

function test_function_constant_nonzero(path)
model = optimizer(path)
function test_function_constant_nonzero(args...)
model = optimizer(args...)
x = MOI.add_variable(model)
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 1.0)
MOI.add_constraint(model, f, MOI.GreaterThan(3.0))
Expand All @@ -125,16 +118,16 @@ function test_function_constant_nonzero(path)
@test isapprox(MOI.get(model, MOI.ObjectiveValue()), 3.0, atol = 1e-6)
end

function test_raw_parameter(path)
model = AmplNLWriter.Optimizer(path)
function test_raw_parameter(args...)
model = AmplNLWriter.Optimizer(args...)
attr = MOI.RawOptimizerAttribute("print_level")
@test MOI.supports(model, attr)
@test MOI.get(model, attr) === nothing
MOI.set(model, attr, 0)
@test MOI.get(model, attr) == 0
end

function test_io(path)
function test_io(args...)
io = IOBuffer()
model = optimizer(path; stdin = stdin, stdout = io)
MOI.set(model, MOI.RawOptimizerAttribute("print_level"), 1)
Expand All @@ -152,8 +145,8 @@ function test_io(path)
return
end

function test_single_variable_interval_dual(path)
model = optimizer(path)
function test_single_variable_interval_dual(args...)
model = optimizer(args...)
x = MOI.add_variable(model)
c = MOI.add_constraint(model, x, MOI.Interval(0.0, 1.0))
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 2.0)
Expand All @@ -167,8 +160,8 @@ function test_single_variable_interval_dual(path)
return
end

function test_nlpblockdual(path)
model = optimizer(path)
function test_nlpblockdual(args...)
model = optimizer(args...)
v = MOI.add_variables(model, 4)
l = [1.1, 1.2, 1.3, 1.4]
u = [5.1, 5.2, 5.3, 5.4]
Expand All @@ -190,14 +183,14 @@ function test_nlpblockdual(path)
@test isapprox(dual, [0.0, -5.008488314902599], atol = 1e-6)
end

function test_AbstractSolverCommand(path)
cmd = AmplNLWriter._DefaultSolverCommand(f -> f(path))
function test_AbstractSolverCommand(args...)
cmd = AmplNLWriter._DefaultSolverCommand(f -> f(args...))
model = AmplNLWriter.Optimizer(cmd)
@test model.solver_command === cmd
end

function test_solve_time(path)
model = optimizer(path)
function test_solve_time(args...)
model = optimizer(args...)
@test isnan(MOI.get(model, MOI.SolveTimeSec()))
v = MOI.add_variables(model, 4)
l = [1.1, 1.2, 1.3, 1.4]
Expand All @@ -216,7 +209,7 @@ function test_solve_time(path)
return
end

function test_directory(path)
function test_directory(args...)
temp_dir = mktempdir()
model = optimizer(path; directory = temp_dir)
v = MOI.add_variables(model, 4)
Expand All @@ -237,8 +230,8 @@ function test_directory(path)
return
end

function test_no_sol_file(path)
model = optimizer(path)
function test_no_sol_file(args...)
model = optimizer(args...)
x = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.GreaterThan(2.0))
MOI.add_constraint(model, x, MOI.LessThan(1.0))
Expand All @@ -251,16 +244,19 @@ function test_no_sol_file(path)
return
end

function test_supports_incremental_interface(path)
model = AmplNLWriter.Optimizer(path)
function test_supports_incremental_interface(args...)
model = AmplNLWriter.Optimizer(args...)
@test !MOI.supports_incremental_interface(model)
return
end

end # module

import Ipopt_jll
TestMOIWrapper.runtests(Ipopt_jll.amplexe)
TestMOIWrapper.runtests(
Ipopt_jll.amplexe,
["print_level=0", "option_file_name=$(joinpath(@__DIR__, "ipopt.opt"))"],
)

import Uno_jll
TestMOIWrapper.runtests(Uno_jll.amplexe)
TestMOIWrapper.runtests(Uno_jll.amplexe, ["logger=SILENT", "preset=ipopt"])

0 comments on commit 82caf73

Please sign in to comment.