Skip to content

Commit

Permalink
Add tests, but don't enable by default
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Mar 17, 2021
1 parent ff68024 commit d639eea
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/Test/UnitTests/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,94 @@ function solve_time(model::MOI.ModelLike, config::TestConfig)
end
end
unittests["solve_time"] = solve_time

# Model and Optimizer attributes.
for attr in (
:BarrierIterations,
:ConflictStatus,
:DualStatus,
:Name,
:NodeCount,
:NumberOfThreads,
:NumberOfVariables,
:ObjectiveFunctionType,
:ObjectiveSense,
:PrimalStatus,
:RelativeGap,
:ResultCount,
:Silent,
:SimplexIterations,
:SolverName,
:SolveTime,
:TerminationStatus,
:TimeLimitSec,
)
f = Symbol("test_attribute_$(attr)")
@eval begin
function $(f)(model::MOI.ModelLike, config::TestConfig)
MOI.empty!(model)
MOI.optimize!(model)
attribute = MOI.$(attr)()
T = MOI.attribute_value_type(attribute)
# Use this syntax if T is a small union like Union{Nothing,Int}.
@test @inferred(T, MOI.get(model, attribute)) isa T
end
# unittests["test_attribute_$(attr)"]
end
end

# Variable attributes.
for attr in (
:VariableName,
)
f = Symbol("test_attribute_$(attr)")
@eval begin
function $(f)(model::MOI.ModelLike, config::TestConfig)
MOI.empty!(model)
x = MOI.add_variable(model)
MOI.set(model, MOI.VariableName(), x, "x")
MOI.optimize!(model)
attribute = MOI.$(attr)()
T = MOI.attribute_value_type(attribute)
@test @inferred(T, MOI.get(model, attribute, x)) isa T
end
# unittests["test_attribute_$(attr)"]
end
end

# Constraint attributes.
for attr in (
:CanonicalConstraintFunction,
:ConstraintFunction,
:ConstraintName,
:ConstraintSet,
)
f = Symbol("test_attribute_$(attr)")
@eval begin
function $(f)(model::MOI.ModelLike, config::TestConfig)
MOI.empty!(model)
x = MOI.add_variable(model)
ci = MOI.add_constraint(
model,
MOI.SingleVariable(x),
MOI.GreaterThan(0.0),
)
MOI.optimize!(model)
MOI.set(model, MOI.ConstraintName(), ci, "ci")
attribute = MOI.$(attr)()
T = MOI.attribute_value_type(attribute)
@test @inferred(T, MOI.get(model, attribute, ci)) isa T
end
# unittests["test_attribute_$(attr)"]
end
end

# TODO(odow): attributes not tested:
# CallbackNodeStatus()
# LazyConstraintCallback()
# NumberOfConstraints{F,S}()
# ObjectiveFunction{F}()
# ConstraintBasisStatus()
# ConstraintConflictStatus()
# VariableBridgingCost()
# ConstraintBridgingCost()
66 changes: 66 additions & 0 deletions test/Utilities/mockoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,69 @@ end
@test MOI.get(mock, MOI.ConstraintConflictStatus(), cx) == MOI.NOT_IN_CONFLICT
@test MOI.get(mock, MOI.ConstraintConflictStatus(), c) == MOI.IN_CONFLICT
end

@testset "test_model_optimizer_attributes" begin
model = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
)
config = MOI.Test.TestConfig()
MOI.set(model, MOI.NumberOfThreads(), 4)
MOI.set(model, MOI.Silent(), true)
MOI.Utilities.set_mock_optimize!(
model,
mock -> MOI.set(mock, MOI.BarrierIterations(), 1),
mock -> nothing, # ConflictStatus
mock -> nothing, # DualStatus
mock -> MOI.set(mock, MOI.Name(), "Mock"),
mock -> MOI.set(mock, MOI.NodeCount(), 1),
mock -> nothing, # NumberOfThreads
mock -> nothing, # NumberOfVariables
mock -> nothing, # ObjectiveFunctionType
mock -> nothing, # ObjectiveSense
mock -> nothing, # PrimalStatus
mock -> MOI.set(mock, MOI.RelativeGap(), 0.0),
mock -> nothing, # ResultCount
mock -> nothing, # Silent
mock -> MOI.set(mock, MOI.SimplexIterations(), 1),
mock -> nothing,
mock -> MOI.set(mock, MOI.SolveTime(), 1.0),
mock -> MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL),
mock -> MOI.set(mock, MOI.TimeLimitSec(), 1.0),
)
MOI.Test.test_attribute_BarrierIterations(model, config)
MOI.Test.test_attribute_ConflictStatus(model, config)
MOI.Test.test_attribute_DualStatus(model, config)
MOI.Test.test_attribute_Name(model, config)
MOI.Test.test_attribute_NodeCount(model, config)
MOI.Test.test_attribute_NumberOfThreads(model, config)
MOI.Test.test_attribute_NumberOfVariables(model, config)
MOI.Test.test_attribute_ObjectiveFunctionType(model, config)
MOI.Test.test_attribute_ObjectiveSense(model, config)
MOI.Test.test_attribute_PrimalStatus(model, config)
MOI.Test.test_attribute_RelativeGap(model, config)
MOI.Test.test_attribute_ResultCount(model, config)
MOI.Test.test_attribute_Silent(model, config)
MOI.Test.test_attribute_SimplexIterations(model, config)
MOI.Test.test_attribute_SolverName(model, config)
MOI.Test.test_attribute_SolveTime(model, config)
MOI.Test.test_attribute_TerminationStatus(model, config)
MOI.Test.test_attribute_TimeLimitSec(model, config)
end

@testset "test_variable_attributes" begin
model = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
)
config = MOI.Test.TestConfig()
MOI.Test.test_attribute_VariableName(model, config)
end

@testset "test_constraint_attributes" begin
model = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
)
config = MOI.Test.TestConfig()
MOI.Test.test_attribute_ConstraintFunction(model, config)
MOI.Test.test_attribute_ConstraintName(model, config)
MOI.Test.test_attribute_ConstraintSet(model, config)
end

0 comments on commit d639eea

Please sign in to comment.