Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/Test/test_attribute.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""
test_attribute_NumberThreads(model::MOI.ModelLike, config::Config)
test_attribute_NumberThreads(model::MOI.AbstractOptimizer, config::Config)

Test that the [`MOI.NumberOfThreads`](@ref) attribute is implemented for
`model`.
"""
function test_attribute_NumberThreads(model::MOI.ModelLike, ::Config)
function test_attribute_NumberThreads(model::MOI.AbstractOptimizer, ::Config)
@requires MOI.supports(model, MOI.NumberOfThreads())
# Get the current value to restore it at the end of the test
value = MOI.get(model, MOI.NumberOfThreads())
Expand All @@ -16,6 +16,7 @@ function test_attribute_NumberThreads(model::MOI.ModelLike, ::Config)
@test value == MOI.get(model, MOI.NumberOfThreads())
return
end
test_attribute_NumberThreads(::MOI.ModelLike, ::Config) = nothing

function setup_test(
::typeof(test_attribute_NumberThreads),
Expand All @@ -27,19 +28,23 @@ function setup_test(
end

"""
test_attribute_RawStatusString(model::MOI.ModelLike, config::Config)
test_attribute_RawStatusString(model::MOI.AbstractOptimizer, config::Config)

Test that the [`MOI.RawStatusString`](@ref) attribute is implemented for
`model`.
"""
function test_attribute_RawStatusString(model::MOI.ModelLike, config::Config)
function test_attribute_RawStatusString(
model::MOI.AbstractOptimizer,
config::Config,
)
@requires _supports(config, MOI.optimize!)
@requires _supports(config, MOI.RawStatusString)
MOI.add_variable(model)
MOI.optimize!(model)
@test MOI.get(model, MOI.RawStatusString()) isa AbstractString
return
end
test_attribute_RawStatusString(::MOI.ModelLike, ::Config) = nothing

function setup_test(
::typeof(test_attribute_RawStatusString),
Expand All @@ -60,11 +65,11 @@ function setup_test(
end

"""
test_attribute_Silent(model::MOI.ModelLike, config::Config)
test_attribute_Silent(model::MOI.AbstractOptimizer, config::Config)

Test that the [`MOI.Silent`](@ref) attribute is implemented for `model`.
"""
function test_attribute_Silent(model::MOI.ModelLike, ::Config)
function test_attribute_Silent(model::MOI.AbstractOptimizer, ::Config)
@requires MOI.supports(model, MOI.Silent())
# Get the current value to restore it at the end of the test
value = MOI.get(model, MOI.Silent())
Expand All @@ -77,6 +82,7 @@ function test_attribute_Silent(model::MOI.ModelLike, ::Config)
@test value == MOI.get(model, MOI.Silent())
return
end
test_attribute_Silent(::MOI.ModelLike, ::Config) = nothing

function setup_test(
::typeof(test_attribute_Silent),
Expand All @@ -88,30 +94,35 @@ function setup_test(
end

"""
test_attribute_SolverName(model::MOI.ModelLike, config::Config)
test_attribute_SolverName(model::MOI.AbstractOptimizer, config::Config)

Test that the [`MOI.SolverName`](@ref) attribute is implemented for `model`.
"""
function test_attribute_SolverName(model::MOI.ModelLike, config::Config)
function test_attribute_SolverName(model::MOI.AbstractOptimizer, config::Config)
if _supports(config, MOI.SolverName)
@test MOI.get(model, MOI.SolverName()) isa AbstractString
end
return
end
test_attribute_SolverName(::MOI.ModelLike, ::Config) = nothing

"""
test_attribute_SolveTimeSec(model::MOI.ModelLike, config::Config)
test_attribute_SolveTimeSec(model::MOI.AbstractOptimizer, config::Config)

Test that the [`MOI.SolveTimeSec`](@ref) attribute is implemented for `model`.
"""
function test_attribute_SolveTimeSec(model::MOI.ModelLike, config::Config)
function test_attribute_SolveTimeSec(
model::MOI.AbstractOptimizer,
config::Config,
)
@requires _supports(config, MOI.optimize!)
@requires _supports(config, MOI.SolveTimeSec)
MOI.add_variable(model)
MOI.optimize!(model)
@test MOI.get(model, MOI.SolveTimeSec()) >= 0.0
return
end
test_attribute_SolveTimeSec(::MOI.ModelLike, ::Config) = nothing

function setup_test(
::typeof(test_attribute_SolveTimeSec),
Expand All @@ -126,11 +137,11 @@ function setup_test(
end

"""
test_attribute_TimeLimitSec(model::MOI.ModelLike, config::Config)
test_attribute_TimeLimitSec(model::MOI.AbstractOptimizer, config::Config)

Test that the [`MOI.TimeLimitSec`](@ref) attribute is implemented for `model`.
"""
function test_attribute_TimeLimitSec(model::MOI.ModelLike, ::Config)
function test_attribute_TimeLimitSec(model::MOI.AbstractOptimizer, ::Config)
@requires MOI.supports(model, MOI.TimeLimitSec())
# Get the current value to restore it at the end of the test
value = MOI.get(model, MOI.TimeLimitSec())
Expand All @@ -142,6 +153,7 @@ function test_attribute_TimeLimitSec(model::MOI.ModelLike, ::Config)
@test value == MOI.get(model, MOI.TimeLimitSec()) # Equality should hold
return
end
test_attribute_TimeLimitSec(::MOI.ModelLike, ::Config) = nothing

function setup_test(
::typeof(test_attribute_TimeLimitSec),
Expand Down
10 changes: 10 additions & 0 deletions src/Test/test_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,11 @@ function test_constraint_ConstraintPrimalStart(
MOI.VectorAffineFunction{T},
MOI.Nonnegatives,
)
@requires MOI.supports(
model,
MOI.ConstraintPrimalStart(),
MOI.ConstraintIndex{MOI.VectorAffineFunction{T},MOI.Nonnegatives},
)
x = MOI.add_variable(model)
f = MOI.VectorAffineFunction(
[MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(one(T), x))],
Expand Down Expand Up @@ -759,6 +764,11 @@ function test_constraint_ConstraintDualStart(
MOI.VectorAffineFunction{T},
MOI.Nonnegatives,
)
@requires MOI.supports(
model,
MOI.ConstraintDualStart(),
MOI.ConstraintIndex{MOI.VectorAffineFunction{T},MOI.Nonnegatives},
)
x = MOI.add_variable(model)
f = MOI.VectorAffineFunction(
[MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(one(T), x))],
Expand Down
54 changes: 25 additions & 29 deletions src/Test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,10 @@ end
config::Config,
)

Test that `MOI.ScalarFunctionConstantNotZero` is thrown when a constraint with
a function with nonzero constant is added.
Test adding a linear constraint with a non-zero function constant.

**This test is optional because solvers could choose to support scalar functions
with nonzero constants.**
This should either work, or error with `MOI.ScalarFunctionConstantNotZero` if
the model does not support it.

To skip this test, pass `MOI.ScalarFunctionConstantNotZero` to the `exclude`
argument of [`Config`](@ref).
Expand All @@ -756,33 +755,30 @@ function test_model_ScalarFunctionConstantNotZero(
config::Config,
)
@requires _supports(config, MOI.ScalarFunctionConstantNotZero)
err = MOI.ScalarFunctionConstantNotZero{
Float64,
MOI.ScalarAffineFunction{Float64},
MOI.EqualTo{Float64},
}(
1.0,
)
@test_throws err begin
MOI.add_constraint(
model,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm{Float64}[], 1.0),
MOI.EqualTo(2.0),
function _error(S, value)
return MOI.ScalarFunctionConstantNotZero{
Float64,
MOI.ScalarAffineFunction{Float64},
S,
}(
value,
)
end
err = MOI.ScalarFunctionConstantNotZero{
Float64,
MOI.ScalarAffineFunction{Float64},
MOI.GreaterThan{Float64},
}(
2.0,
)
@test_throws err begin
MOI.add_constraint(
model,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm{Float64}[], 2.0),
MOI.GreaterThan(1.0),
)
try
f = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm{Float64}[], 1.0)
c = MOI.add_constraint(model, f, MOI.EqualTo(2.0))
@requires _supports(config, MOI.ConstraintFunction)
@test MOI.get(model, MOI.ConstraintFunction(), c) ≈ f
catch err
@test err == _error(MOI.EqualTo{Float64}, 1.0)
end
try
f = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm{Float64}[], 2.0)
c = MOI.add_constraint(model, f, MOI.GreaterThan(1.0))
@requires _supports(config, MOI.ConstraintFunction)
@test MOI.get(model, MOI.ConstraintFunction(), c) ≈ f
catch err
@test err == _error(MOI.GreaterThan{Float64}, 2.0)
end
return
end
Expand Down
9 changes: 1 addition & 8 deletions test/Utilities/cachingoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,7 @@ function test_MOI_Test()
MOI.Test.runtests(
model,
MOI.Test.Config(exclude = Any[MOI.optimize!]),
exclude = String[
# This test is optional.
"test_model_ScalarFunctionConstantNotZero",
# MockOptimizer doesnt' support these
"test_attribute_SolverName",
"test_constraint_ConstraintDualStart",
"test_constraint_ConstraintPrimalStart",
],
exclude = ["test_attribute_SolverName"],
)
end
end
Expand Down
10 changes: 1 addition & 9 deletions test/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,7 @@ struct SetNotSupportedBySolvers <: MOI.AbstractSet end
function test_MOI_Test()
MOI.Test.runtests(
MOI.Utilities.Model{Float64}(),
MOI.Test.Config(exclude = Any[MOI.optimize!]),
exclude = String[
# This test is optional.
"test_model_ScalarFunctionConstantNotZero",
# Unsupported attributes
"test_constraint_ConstraintPrimalStart",
"test_constraint_ConstraintDualStart",
"test_attribute_SolverName",
],
MOI.Test.Config(exclude = Any[MOI.optimize!,]),
)
return
end
Expand Down
2 changes: 0 additions & 2 deletions test/Utilities/universalfallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ function test_MOI_Test()
model,
MOI.Test.Config(exclude = Any[MOI.optimize!]),
exclude = String[
# This test is optional.
"test_model_ScalarFunctionConstantNotZero",
# UniversalFallback fails all these tests because it supports
# everything...
"test_attribute_",
Expand Down