Skip to content

Commit 3098696

Browse files
committed
[FileFormats] improve test coverage
1 parent 23f7469 commit 3098696

File tree

5 files changed

+130
-24
lines changed

5 files changed

+130
-24
lines changed

src/FileFormats/LP/LP.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ function _parse_section(
10011001
for token in tokens[3:end]
10021002
items = String.(split(token, ":"))
10031003
if length(items) != 2
1004-
error("Invalid sequence: $(token)")
1004+
error("Invalid token in SOS constraint: $(token)")
10051005
end
10061006
push!(variables, _get_variable_from_name(model, cache, items[1]))
10071007
push!(weights, parse(Float64, items[2]))

test/FileFormats/LP/LP.jl

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ import MathOptInterface.FileFormats: LP
1313

1414
const LP_TEST_FILE = "test.lp"
1515

16+
function runtests()
17+
for name in names(@__MODULE__, all = true)
18+
if startswith("$(name)", "test_")
19+
@testset "$name" begin
20+
getfield(@__MODULE__, name)()
21+
end
22+
end
23+
end
24+
sleep(1.0) # Allow time for unlink to happen.
25+
rm(LP_TEST_FILE, force = true)
26+
return
27+
end
28+
1629
function test_show()
1730
@test sprint(summary, LP.Model()) == "MOI.FileFormats.LP.Model"
1831
return
@@ -1065,19 +1078,27 @@ function test_VectorAffineFunction_SOS()
10651078
return
10661079
end
10671080

1068-
function runtests()
1069-
for name in names(@__MODULE__, all = true)
1070-
if startswith("$(name)", "test_")
1071-
@testset "$name" begin
1072-
getfield(@__MODULE__, name)()
1073-
end
1074-
end
1075-
end
1076-
sleep(1.0) # Allow time for unlink to happen.
1077-
rm(LP_TEST_FILE, force = true)
1081+
function test_comprehensive_write()
1082+
model = LP.Model()
1083+
io = IOBuffer()
1084+
print(
1085+
io,
1086+
"""
1087+
minimize
1088+
obj: x + y
1089+
subject to
1090+
SOS
1091+
c11: S1:: x 1.0 y 2.0
1092+
""",
1093+
)
1094+
seekstart(io)
1095+
@test_throws(
1096+
ErrorException("Invalid token in SOS constraint: x"),
1097+
read!(io, model),
1098+
)
10781099
return
10791100
end
10801101

1081-
end
1102+
end # module
10821103

10831104
TestLP.runtests()

test/FileFormats/MOF/MOF.jl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,28 @@ function test_nonlinear_variable_real_nodes()
14971497
return
14981498
end
14991499

1500+
function test_nonlinear_variable_complex_nodes()
1501+
x = MOI.VariableIndex(1)
1502+
object = Dict{String,Any}(
1503+
"type" => "ScalarNonlinearFunction",
1504+
"root" => MOF.Dict{String,Any}(
1505+
"type" => "^",
1506+
"args" => Any[
1507+
Dict{String,Any}("type" => "node", "index" => 1),
1508+
Dict{String,Any}("type" => "node", "index" => 2),
1509+
],
1510+
),
1511+
"node_list" => Any[
1512+
Dict{String,Any}("type" => "variable", "name" => "x"),
1513+
Dict{String,Any}("type" => "complex", "real" => 2.0, "imag" => 3.0),
1514+
],
1515+
)
1516+
f = MOI.ScalarNonlinearFunction(:^, Any[x, 2.0+3im])
1517+
@test MOF.function_to_moi(object, Dict("x" => x)) f
1518+
return
1519+
end
1520+
1521+
15001522
function test_mof_scalaraffinefunction()
15011523
x = MOI.VariableIndex(1)
15021524
f = 1.0 * x + 2.0
@@ -1535,6 +1557,40 @@ function test_mof_scalarquadraticfunction()
15351557
return
15361558
end
15371559

1560+
function test_nonlinear_expression_not_call()
1561+
model = MOF.Model()
1562+
x = MOI.add_variable(model)
1563+
expr = :($x[1])
1564+
con = MOI.add_constraint(model, MOF.Nonlinear(expr), MOI.EqualTo(1.0))
1565+
io = IOBuffer()
1566+
@test_throws(
1567+
ErrorException(
1568+
"Expected an expression that was a function. Got $expr",
1569+
),
1570+
write(io, model),
1571+
)
1572+
return
1573+
end
1574+
1575+
function test_write_NLPBlock_no_objective()
1576+
model = MOF.Model()
1577+
x = MOI.add_variables(model, 4)
1578+
for (index, variable) in enumerate(x)
1579+
MOI.set(model, MOI.VariableName(), variable, "var_$(index)")
1580+
end
1581+
MOI.add_constraints(model, x, Ref(MOI.Interval(1.0, 5.0)))
1582+
block = HS071(x)
1583+
new_block =
1584+
MOI.NLPBlockData(block.constraint_bounds, block.evaluator, false)
1585+
MOI.set(model, MOI.NLPBlock(), new_block)
1586+
io = IOBuffer()
1587+
write(io, model)
1588+
seekstart(io)
1589+
contents = read(io, String)
1590+
@test occursin(""""objective":{"sense":"feasibility"}""", contents)
1591+
return
1592+
end
1593+
15381594
end
15391595

15401596
TestMOF.runtests()

test/FileFormats/NL/sol.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,16 @@ function test_result_count()
358358
return
359359
end
360360

361+
function test_eol()
362+
model = NL.Model()
363+
io = IOBuffer()
364+
@test_throws(
365+
ErrorException("Reached end of sol file unexpectedly."),
366+
NL.SolFileResults(io, model),
367+
)
368+
return
369+
end
370+
361371
end
362372

363373
TestNonlinearSolFiles.runtests()

test/FileFormats/SDPA/SDPA.jl

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,24 @@ using Test
1010

1111
import MathOptInterface as MOI
1212
import MathOptInterface.Utilities as MOIU
13-
const SDPA = MOI.FileFormats.SDPA
13+
import MathOptInterface.FileFormats: SDPA
14+
1415
const SDPA_TEST_FILE = "test.sdpa"
1516
const SDPA_MODELS_DIR = joinpath(@__DIR__, "models")
1617

18+
function runtests()
19+
for name in names(@__MODULE__, all = true)
20+
if startswith("$(name)", "test_")
21+
@testset "$name" begin
22+
getfield(@__MODULE__, name)()
23+
end
24+
end
25+
end
26+
sleep(1.0) # Allow time for unlink to happen.
27+
rm(SDPA_TEST_FILE, force = true)
28+
return
29+
end
30+
1731
function _set_var_and_con_names(model::MOI.ModelLike)
1832
variable_names = String[]
1933
for j in MOI.get(model, MOI.ListOfVariableIndices())
@@ -330,19 +344,24 @@ function test_dim_reader()
330344
end
331345
end
332346

333-
function runtests()
334-
for name in names(@__MODULE__, all = true)
335-
if startswith("$(name)", "test_")
336-
@testset "$name" begin
337-
getfield(@__MODULE__, name)()
338-
end
339-
end
340-
end
341-
sleep(1.0) # Allow time for unlink to happen.
342-
rm(SDPA_TEST_FILE, force = true)
347+
function test_integer_before_variables()
348+
file = """
349+
*INTEGER
350+
*1
351+
"""
352+
io = IOBuffer()
353+
print(io, file)
354+
seekstart(io)
355+
model = SDPA.Model()
356+
@test_throws(
357+
ErrorException(
358+
"The number of variables should be given before *INTEGER section.",
359+
),
360+
read!(io, model),
361+
)
343362
return
344363
end
345364

346-
end
365+
end # module
347366

348367
TestSDPA.runtests()

0 commit comments

Comments
 (0)