-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: unit tests #329
Merged
RFC: unit tests #329
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
faee6d9
Change order of tests
odow fb97f1f
Start some atomic tests
odow 10f8cfd
more tests
odow e89135c
Objective tests
odow b04d858
Test setting variable name
odow 710510c
reorganize and add test for SingleVariable
odow c387d55
Add atomic to tests
odow 496b98d
Address mlubin's comments
odow 218d5bc
Update filename
odow 026c0e3
Update doc strings
odow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
Test getting constraints by name. | ||
""" | ||
function getconstraint(model::MOI.ModelLike, config::TestConfig) | ||
MOI.empty!(model) | ||
MOIU.loadfromstring!(model,""" | ||
variables: x | ||
minobjective: 2.0x | ||
c1: x >= 1.0 | ||
c2: x <= 2.0 | ||
""") | ||
@test !MOI.canget(model, MOI.ConstraintIndex, "c3") | ||
@test MOI.canget(model, MOI.ConstraintIndex, "c1") | ||
@test MOI.canget(model, MOI.ConstraintIndex{MOI.SingleVariable, MOI.GreaterThan{Float64}}, "c1") | ||
@test !MOI.canget(model, MOI.ConstraintIndex{MOI.SingleVariable, MOI.LessThan{Float64}}, "c1") | ||
@test MOI.canget(model, MOI.ConstraintIndex, "c2") | ||
@test !MOI.canget(model, MOI.ConstraintIndex{MOI.SingleVariable, MOI.GreaterThan{Float64}}, "c2") | ||
@test MOI.canget(model, MOI.ConstraintIndex{MOI.SingleVariable, MOI.LessThan{Float64}}, "c2") | ||
c1 = MOI.get(model, MOI.ConstraintIndex{MOI.SingleVariable, MOI.GreaterThan{Float64}}, "c1") | ||
@test MOI.isvalid(model, c1) | ||
c2 = MOI.get(model, MOI.ConstraintIndex{MOI.SingleVariable, MOI.LessThan{Float64}}, "c2") | ||
@test MOI.isvalid(model, c2) | ||
end | ||
unittests["getconstraint"] = getconstraint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#= | ||
Functions in this file test functionality relating to objectives in MOI. | ||
|
||
### Requires | ||
- optimize! | ||
|
||
### Functionality currently tested | ||
- get/set ObjectiveSense | ||
- a constant in a affine objective | ||
- a blank objective | ||
|
||
### Functionality not yet tested | ||
- Quadratic Objectives | ||
- Modifications | ||
=# | ||
|
||
""" | ||
Set objective to MaxSense | ||
""" | ||
function max_sense(model::MOI.ModelLike, config::TestConfig) | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
@test MOI.canset(model, MOI.ObjectiveSense()) | ||
MOI.set!(model, MOI.ObjectiveSense(), MOI.MaxSense) | ||
@test MOI.canget(model, MOI.ObjectiveSense()) | ||
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.MaxSense | ||
end | ||
unittests["max_sense"] = max_sense | ||
|
||
""" | ||
Set objective to MinSense | ||
""" | ||
function min_sense(model::MOI.ModelLike, config::TestConfig) | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
@test MOI.canset(model, MOI.ObjectiveSense()) | ||
MOI.set!(model, MOI.ObjectiveSense(), MOI.MinSense) | ||
@test MOI.canget(model, MOI.ObjectiveSense()) | ||
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.MinSense | ||
end | ||
unittests["min_sense"] = min_sense | ||
|
||
""" | ||
Set objective to FeasibilitySense | ||
""" | ||
function feasibility_sense(model::MOI.ModelLike, config::TestConfig) | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
@test MOI.canset(model, MOI.ObjectiveSense()) | ||
MOI.set!(model, MOI.ObjectiveSense(), MOI.FeasibilitySense) | ||
@test MOI.canget(model, MOI.ObjectiveSense()) | ||
@test MOI.get(model, MOI.ObjectiveSense()) == MOI.FeasibilitySense | ||
end | ||
unittests["feasibility_sense"] = feasibility_sense | ||
|
||
""" | ||
Test constant in objective. | ||
""" | ||
function solve_constant_obj(model::MOI.ModelLike, config::TestConfig) | ||
atol, rtol = config.atol, config.rtol | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
MOIU.loadfromstring!(model,""" | ||
variables: x | ||
minobjective: 2.0x + 1.0 | ||
c: x >= 1.0 | ||
""") | ||
MOI.optimize!(model) | ||
@test MOI.get(model, MOI.ObjectiveValue()) ≈ 3.0 atol=atol rtol=rtol | ||
end | ||
unittests["solve_constant_obj"] = solve_constant_obj | ||
|
||
""" | ||
Test blank objective. | ||
""" | ||
function solve_blank_obj(model::MOI.ModelLike, config::TestConfig) | ||
atol, rtol = config.atol, config.rtol | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
MOIU.loadfromstring!(model,""" | ||
variables: x | ||
minobjective: 0.0x + 0.0 | ||
c: x >= 1.0 | ||
""") | ||
MOI.optimize!(model) | ||
@test MOI.get(model, MOI.ObjectiveValue()) ≈ 0.0 atol=atol rtol=rtol | ||
end | ||
unittests["solve_blank_obj"] = solve_blank_obj | ||
|
||
""" | ||
Test SingleVariable objective. | ||
""" | ||
function solve_singlevariable_obj(model::MOI.ModelLike, config::TestConfig) | ||
atol, rtol = config.atol, config.rtol | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
MOIU.loadfromstring!(model,""" | ||
variables: x | ||
minobjective: x | ||
c: x >= 1.0 | ||
""") | ||
MOI.optimize!(model) | ||
@test MOI.get(model, MOI.ObjectiveValue()) ≈ 1.0 atol=atol rtol=rtol | ||
end | ||
unittests["solve_singlevariable_obj"] = solve_singlevariable_obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#= | ||
These tests aim to minimally test each expected feature in MOI, in addition | ||
to the full end-to-end tests in contlinear.jl etc | ||
=# | ||
|
||
const unittests = Dict{String, Function}() | ||
|
||
include("variables.jl") | ||
include("objectives.jl") | ||
include("constraints.jl") | ||
|
||
@moitestset unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
#= | ||
Functions in this file test functionality relating to variables in MOI. | ||
|
||
### Functionality currently tested | ||
- canaddvariable | ||
- addvariables! | ||
- addvariable! | ||
- deleting variables | ||
- get/set! VariableName | ||
- isvalid for VariableIndex | ||
- get VariableIndex by name | ||
- NumberOfVariables | ||
|
||
### Functionality not yet tested | ||
- VariablePrimalStart | ||
- VariablePrimal | ||
- VariableBasisStatus | ||
- ListOfVariableIndices | ||
=# | ||
|
||
""" | ||
This function tests adding a single variable. | ||
""" | ||
function add_variable(model::MOI.ModelLike, config::TestConfig) | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
@test MOI.canaddvariable(model) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 0 | ||
v = MOI.addvariable!(model) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 1 | ||
end | ||
unittests["add_variable"] = add_variable | ||
|
||
""" | ||
This function tests adding multiple variables. | ||
""" | ||
function add_variables(model::MOI.ModelLike, config::TestConfig) | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
@test MOI.canaddvariable(model) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 0 | ||
v = MOI.addvariables!(model, 2) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 2 | ||
end | ||
unittests["add_variables"] = add_variables | ||
|
||
""" | ||
This function tests adding, and then deleting, | ||
a single variable. | ||
""" | ||
function delete_variable(model::MOI.ModelLike, config::TestConfig) | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
@test MOI.canaddvariable(model) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 0 | ||
v = MOI.addvariable!(model) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 1 | ||
@test MOI.candelete(model, v) | ||
MOI.delete!(model, v) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 0 | ||
end | ||
unittests["delete_variable"] = delete_variable | ||
|
||
""" | ||
This function tests adding, and then deleting, | ||
multiple variables. | ||
""" | ||
function delete_variables(model::MOI.ModelLike, config::TestConfig) | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
@test MOI.canaddvariable(model) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 0 | ||
v = MOI.addvariables!(model, 2) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 2 | ||
@test MOI.candelete(model, v) | ||
MOI.delete!(model, v) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 0 | ||
v = MOI.addvariables!(model, 2) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 2 | ||
@test MOI.candelete(model, v[1]) | ||
MOI.delete!(model, v[1]) | ||
@test MOI.get(model, MOI.NumberOfVariables()) == 1 | ||
@test !MOI.candelete(model, v[1]) | ||
@test MOI.candelete(model, v[2]) | ||
@test !MOI.isvalid(model, v[1]) | ||
@test MOI.isvalid(model, v[2]) | ||
end | ||
unittests["delete_variables"] = delete_variable | ||
|
||
""" | ||
Test getting variables by name. | ||
""" | ||
function getvariable(model::MOI.ModelLike, config::TestConfig) | ||
MOI.empty!(model) | ||
MOIU.loadfromstring!(model,""" | ||
variables: x | ||
minobjective: 2.0x | ||
c1: x >= 1.0 | ||
c2: x <= 2.0 | ||
""") | ||
@test MOI.canget(model, MOI.VariableIndex, "x") | ||
@test !MOI.canget(model, MOI.VariableIndex, "y") | ||
x = MOI.get(model, MOI.VariableIndex, "x") | ||
@test MOI.isvalid(model, x) | ||
end | ||
unittests["getvariable"] = getvariable | ||
|
||
""" | ||
Test getting and setting varaible names. | ||
""" | ||
function variablenames(model::MOI.ModelLike, config::TestConfig) | ||
MOI.empty!(model) | ||
v = MOI.addvariable!(model) | ||
@test MOI.get(model, MOI.VariableName(), v) == "" | ||
@test MOI.canset(model, MOI.VariableName(), typeof(v)) | ||
MOI.set!(model, MOI.VariableName(), v, "x") | ||
@test MOI.get(model, MOI.VariableName(), v) == "x" | ||
MOI.set!(model, MOI.VariableName(), v, "y") | ||
@test MOI.get(model, MOI.VariableName(), v) == "y" | ||
x = MOI.addvariable!(model) | ||
MOI.set!(model, MOI.VariableName(), x, "x") | ||
@test MOI.get(model, MOI.VariableName(), x) == "x" | ||
end | ||
unittests["variablenames"] = variablenames | ||
|
||
""" | ||
Test the setting of an upper bound | ||
""" | ||
function solve_with_upperbound(model::MOI.ModelLike, config::TestConfig) | ||
atol, rtol = config.atol, config.rtol | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
MOIU.loadfromstring!(model,""" | ||
variables: x | ||
maxobjective: 2.0x | ||
c1: x <= 1.0 | ||
c2: x >= 0.0 | ||
""") | ||
MOI.optimize!(model) | ||
@test MOI.get(model, MOI.PrimalStatus()) == MOI.FeasiblePoint | ||
v = MOI.get(model, MOI.VariableIndex, "x") | ||
@test MOI.get(model, MOI.VariablePrimal(), v) ≈ 1 atol=atol rtol=rtol | ||
if config.duals | ||
@test MOI.get(model, MOI.DualStatus()) == MOI.FeasiblePoint | ||
c1 = MOI.get(model, MOI.ConstraintIndex{MOI.SingleVariable,MOI.LessThan{Float64}}, "c1") | ||
@test MOI.get(model, MOI.ConstraintDual(), c1) ≈ -2.0 atol=atol rtol=rtol | ||
c2 = MOI.get(model, MOI.ConstraintIndex{MOI.SingleVariable,MOI.GreaterThan{Float64}}, "c2") | ||
@test MOI.get(model, MOI.ConstraintDual(), c2) ≈ 0.0 atol=atol rtol=rtol | ||
end | ||
end | ||
unittests["solve_with_upperbound"] = solve_with_upperbound | ||
|
||
""" | ||
Test the setting of an lower bound | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
""" | ||
function solve_with_lowerbound(model::MOI.ModelLike, config::TestConfig) | ||
atol, rtol = config.atol, config.rtol | ||
MOI.empty!(model) | ||
@test MOI.isempty(model) | ||
MOIU.loadfromstring!(model,""" | ||
variables: x | ||
minobjective: 2.0x | ||
c1: x >= 1.0 | ||
c2: x <= 2.0 | ||
""") | ||
MOI.optimize!(model) | ||
@test MOI.get(model, MOI.PrimalStatus()) == MOI.FeasiblePoint | ||
v = MOI.get(model, MOI.VariableIndex, "x") | ||
@test MOI.get(model, MOI.VariablePrimal(), v) ≈ 1 atol=atol rtol=rtol | ||
if config.duals | ||
@test MOI.get(model, MOI.DualStatus()) == MOI.FeasiblePoint | ||
c1 = MOI.get(model, MOI.ConstraintIndex{MOI.SingleVariable,MOI.GreaterThan{Float64}}, "c1") | ||
@test MOI.get(model, MOI.ConstraintDual(), c1) ≈ 2.0 atol=atol rtol=rtol | ||
c2 = MOI.get(model, MOI.ConstraintIndex{MOI.SingleVariable,MOI.LessThan{Float64}}, "c2") | ||
@test MOI.get(model, MOI.ConstraintDual(), c2) ≈ 0.0 atol=atol rtol=rtol | ||
end | ||
end | ||
unittests["solve_with_lowerbound"] = solve_with_lowerbound |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also update the docstring to mention that the test calls
optimize!
.