Skip to content

Commit

Permalink
Convert more test files to functional form (#3146)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Dec 8, 2022
1 parent 4a7352e commit 11c1782
Show file tree
Hide file tree
Showing 3 changed files with 696 additions and 800 deletions.
32 changes: 27 additions & 5 deletions test/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

module TestCallbacks

using JuMP
using Test

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

struct DummyCallbackData end

@testset "LazyConstraint" begin
function test_LazyConstraint()
mock = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
Expand All @@ -28,9 +41,10 @@ struct DummyCallbackData end
@test length(c[1][1].terms) == 1
@test c[1][1].terms[1].coefficient == 1.0
@test c[1][2] == MOI.LessThan(2.0)
return
end

@testset "UserCut" begin
function test_UserCut()
mock = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
Expand All @@ -43,9 +57,10 @@ end
@test length(c) == 1
@test c[1][1] moi_function(1.0 * x)
@test c[1][2] == MOI.LessThan(2.0)
return
end

@testset "HeuristicSolution" begin
function test_HeuristicSolution()
mock = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
Expand All @@ -65,9 +80,10 @@ end
@test length(c) == 2
@test c[2][1] == [index(x)]
@test c[2][2] == [1.0]
return
end

@testset "callback_value" begin
function test_callback_value()
mock = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
Expand All @@ -87,9 +103,10 @@ end
@test callback_value(cb, expr) == 2
quad_expr = expr^2
@test callback_value(cb, quad_expr) == 4
return
end

@testset "callback_node_status" begin
function test_callback_node_status()
mock = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
Expand All @@ -109,4 +126,9 @@ end
)
optimize!(model)
@test callback_node_status(cb, model) == MOI.CALLBACK_NODE_STATUS_INTEGER
return
end

end # module

TestCallbacks.runtests()
254 changes: 138 additions & 116 deletions test/complement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,138 +3,160 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

module TestComplement

using JuMP
using Test

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

include(joinpath(@__DIR__, "utilities.jl"))

@testset "Scalar Complementarity" begin
@testset "complements" begin
model = Model()
@variable(model, x >= 0)
@constraint(model, c, complements(2x - 1, x))
obj = constraint_object(c)
@test obj.func == [2x - 1, x]
@test obj.set == MOI.Complements(2)
end
function test_scalar_complements()
model = Model()
@variable(model, x >= 0)
@constraint(model, c, complements(2x - 1, x))
obj = constraint_object(c)
@test obj.func == [2x - 1, x]
@test obj.set == MOI.Complements(2)
return
end

@testset "" begin
model = Model()
@variable(model, x >= 0)
@variable(model, y >= 0)
@constraint(model, c, x y)
obj = constraint_object(c)
@test obj.func == [x, y]
@test obj.set == MOI.Complements(2)
end
function test_scalar_perp()
model = Model()
@variable(model, x >= 0)
@variable(model, y >= 0)
@constraint(model, c, x y)
obj = constraint_object(c)
@test obj.func == [x, y]
@test obj.set == MOI.Complements(2)
return
end

@testset "error: x--F" begin
model = Model()
@variable(model, x >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, x ⟂ 2x - 1)`: second term must be a " *
"variable.",
),
@constraint(model, x 2x - 1)
)
end
function test_scalar_error_x_F()
model = Model()
@variable(model, x >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, x ⟂ 2x - 1)`: second term must be a " *
"variable.",
),
@constraint(model, x 2x - 1)
)
return
end

@testset "error: F--F" begin
model = Model()
@variable(model, x >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, x + 1 ⟂ 2x - 1)`: second term must " *
"be a variable.",
),
@constraint(model, x + 1 2x - 1)
)
end
function test_scalar_error_F_F()
model = Model()
@variable(model, x >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, x + 1 ⟂ 2x - 1)`: second term must " *
"be a variable.",
),
@constraint(model, x + 1 2x - 1)
)
return
end

@testset "Vector Complementarity" begin
@testset "complements" begin
model = Model()
@variable(model, x[1:2] >= 0)
@constraint(model, c, complements(2x .- 1, x))
obj = constraint_object(c)
@test obj.func == [2x .- 1; x]
@test obj.set == MOI.Complements(4)
end
function test_vector_complements()
model = Model()
@variable(model, x[1:2] >= 0)
@constraint(model, c, complements(2x .- 1, x))
obj = constraint_object(c)
@test obj.func == [2x .- 1; x]
@test obj.set == MOI.Complements(4)
return
end

@testset "" begin
model = Model()
@variable(model, x[1:2] >= 0)
@variable(model, y[1:2] >= 0)
@constraint(model, c, x y)
obj = constraint_object(c)
@test obj.func == [x; y]
@test obj.set == MOI.Complements(4)
end
function test_vector_perp()
model = Model()
@variable(model, x[1:2] >= 0)
@variable(model, y[1:2] >= 0)
@constraint(model, c, x y)
obj = constraint_object(c)
@test obj.func == [x; y]
@test obj.set == MOI.Complements(4)
return
end

@testset "error: length mismatch" begin
model = Model()
@variable(model, x[1:2] >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, x ⟂ [x[1]])`: size of mapping does " *
"not match size of variables: (2,) != (1,).",
),
@constraint(model, x [x[1]])
)
end
function test_vector_error_length_mismatch()
model = Model()
@variable(model, x[1:2] >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, x ⟂ [x[1]])`: size of mapping does " *
"not match size of variables: (2,) != (1,).",
),
@constraint(model, x [x[1]])
)
return
end

@testset "error: x--F" begin
model = Model()
@variable(model, x[1:2] >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, x ⟂ 2x .- 1)`: second term must be an " *
"array of variables.",
),
@constraint(model, x 2x .- 1)
)
end
function test_vector_error_x_F()
model = Model()
@variable(model, x[1:2] >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, x ⟂ 2x .- 1)`: second term must be an " *
"array of variables.",
),
@constraint(model, x 2x .- 1)
)
end

@testset "error: F--F" begin
model = Model()
@variable(model, x[1:2] >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, x .+ 1 ⟂ 2x .- 1)`: second term must " *
"be an array of variables.",
),
@constraint(model, x .+ 1 2x .- 1)
)
end
function test_vector_error_F_F()
model = Model()
@variable(model, x[1:2] >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, x .+ 1 ⟂ 2x .- 1)`: second term must " *
"be an array of variables.",
),
@constraint(model, x .+ 1 2x .- 1)
)
return
end

@testset "SparseAxisArray" begin
@testset "complements" begin
model = Model()
@variable(model, x[i = 1:3; isodd(i)] >= 0)
@constraint(model, c, complements(2x .- 1, x))
obj = constraint_object(c)
@test obj.func == [2x[3] - 1, 2x[1] - 1, x[3], x[1]] ||
obj.func == [2x[1] - 1, 2x[3] - 1, x[1], x[3]]
@test obj.set == MOI.Complements(4)
end
function test_sparse_complements()
model = Model()
@variable(model, x[i = 1:3; isodd(i)] >= 0)
@constraint(model, c, complements(2x .- 1, x))
obj = constraint_object(c)
@test obj.func == [2x[3] - 1, 2x[1] - 1, x[3], x[1]] ||
obj.func == [2x[1] - 1, 2x[3] - 1, x[1], x[3]]
@test obj.set == MOI.Complements(4)
return
end

@testset "" begin
model = Model()
@variable(model, x[i = 1:3; isodd(i)] >= 0)
@constraint(model, c, 2x .- 1 x)
obj = constraint_object(c)
@test obj.func == [2x[3] - 1, 2x[1] - 1, x[3], x[1]] ||
obj.func == [2x[1] - 1, 2x[3] - 1, x[1], x[3]]
@test obj.set == MOI.Complements(4)
end
function test_sparse_perp()
model = Model()
@variable(model, x[i = 1:3; isodd(i)] >= 0)
@constraint(model, c, 2x .- 1 x)
obj = constraint_object(c)
@test obj.func == [2x[3] - 1, 2x[1] - 1, x[3], x[1]] ||
obj.func == [2x[1] - 1, 2x[3] - 1, x[1], x[3]]
@test obj.set == MOI.Complements(4)
return
end

@testset "key mismatch" begin
model = Model()
@variable(model, x[i = 1:3; isodd(i)] >= 0)
@variable(model, y[i = 3:5; isodd(i)] >= 0)
@test_throws(ErrorException, @constraint(model, 2x .- 1 y))
end
function test_sparse_key_mismatch()
model = Model()
@variable(model, x[i = 1:3; isodd(i)] >= 0)
@variable(model, y[i = 3:5; isodd(i)] >= 0)
@test_throws(ErrorException, @constraint(model, 2x .- 1 y))
return
end

end # module

TestComplement.runtests()
Loading

0 comments on commit 11c1782

Please sign in to comment.