Skip to content

Commit db2c00f

Browse files
authored
[Test] Tidy Complex Zeros tests (#1970)
1 parent 6222c56 commit db2c00f

File tree

3 files changed

+68
-71
lines changed

3 files changed

+68
-71
lines changed

src/Test/test_linear.jl

+64-71
Original file line numberDiff line numberDiff line change
@@ -3898,46 +3898,38 @@ function setup_test(
38983898
return
38993899
end
39003900

3901-
function test_linear_complex_Zeros(optimizer, config::Config{T}) where {T}
3902-
atol = config.atol
3903-
rtol = config.rtol
3901+
"""
3902+
test_linear_complex_Zeros(model::MOI.ModelLike, config::Config{T}) where {T}
39043903
3905-
MOI.empty!(optimizer)
3906-
o = one(T)
3907-
t = 2o
3908-
x, cx = MOI.add_constrained_variables(optimizer, MOI.Nonnegatives(2))
3909-
func = (o + t * im) * x[1] + (o - o * im) * x[2] + (-o + -o * im)
3910-
c = MOI.add_constraint(
3911-
optimizer,
3912-
MOI.Utilities.operate(vcat, Complex{T}, func),
3913-
MOI.Zeros(1),
3904+
Run an integration test on complex-valued affine constraints in Zeros.
3905+
"""
3906+
function test_linear_complex_Zeros(
3907+
model::MOI.ModelLike,
3908+
config::Config{T},
3909+
) where {T}
3910+
@requires _supports(config, MOI.optimize!)
3911+
@requires MOI.supports_constraint(
3912+
model,
3913+
MOI.VectorAffineFunction{Complex{T}},
3914+
MOI.Zeros,
39143915
)
3915-
if _supports(config, MOI.optimize!)
3916-
@test MOI.get(optimizer, MOI.TerminationStatus()) ==
3917-
MOI.OPTIMIZE_NOT_CALLED
3918-
MOI.optimize!(optimizer)
3919-
@test MOI.get(optimizer, MOI.TerminationStatus()) ==
3920-
config.optimal_status
3921-
@test (
3922-
MOI.get(optimizer, MOI.VariablePrimal(), x),
3923-
[T(2) / T(3), T(1) / T(3)],
3924-
config,
3925-
)
3926-
@test (
3927-
MOI.get(optimizer, MOI.ConstraintPrimal(), cx),
3928-
[T(2) / T(3), T(1) / T(3)],
3929-
config,
3930-
)
3931-
z = [zero(Complex{T})]
3932-
@test (MOI.get(optimizer, MOI.ConstraintPrimal(), c), z, config)
3933-
if _supports(config, MOI.ConstraintDual)
3934-
@test (
3935-
MOI.get(optimizer, MOI.ConstraintDual(), cx),
3936-
zeros(T, 2),
3937-
config,
3938-
)
3939-
@test (MOI.get(optimizer, MOI.ConstraintDual(), c), z, config)
3940-
end
3916+
x, cx = MOI.add_constrained_variables(model, MOI.Nonnegatives(2))
3917+
scalar_f =
3918+
(T(1) + T(2) * im) * x[1] + (T(1) - T(1) * im) * x[2] -
3919+
(T(1) + T(1) * im)
3920+
vector_f = MOI.Utilities.operate(vcat, Complex{T}, scalar_f)
3921+
c = MOI.add_constraint(model, vector_f, MOI.Zeros(1))
3922+
@test MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMIZE_NOT_CALLED
3923+
MOI.optimize!(model)
3924+
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
3925+
primal = [T(2) / T(3), T(1) / T(3)]
3926+
@test (MOI.get(model, MOI.VariablePrimal(), x), primal, config)
3927+
@test (MOI.get(model, MOI.ConstraintPrimal(), cx), primal, config)
3928+
z = [zero(Complex{T})]
3929+
@test (MOI.get(model, MOI.ConstraintPrimal(), c), z, config)
3930+
if _supports(config, MOI.ConstraintDual)
3931+
@test (MOI.get(model, MOI.ConstraintDual(), cx), T[0, 0], config)
3932+
@test (MOI.get(model, MOI.ConstraintDual(), c), z, config)
39413933
end
39423934
return
39433935
end
@@ -3961,43 +3953,42 @@ function setup_test(
39613953
return
39623954
end
39633955

3956+
version_added(::typeof(test_linear_complex_Zeros)) = v"1.7.0"
3957+
3958+
"""
3959+
test_linear_complex_Zeros_duplicate(
3960+
model::MOI.ModelLike,
3961+
config::Config{T},
3962+
) where {T}
3963+
3964+
Run an integration test on complex-valued affine constraints in Zeros.
3965+
"""
39643966
function test_linear_complex_Zeros_duplicate(
3965-
optimizer,
3967+
model::MOI.ModelLike,
39663968
config::Config{T},
39673969
) where {T}
3968-
atol = config.atol
3969-
rtol = config.rtol
3970-
3971-
MOI.empty!(optimizer)
3972-
o = one(T)
3973-
t = 2o
3974-
x, cx = MOI.add_constrained_variables(optimizer, MOI.Nonnegatives(1))
3975-
func =
3976-
(o + zero(T) * im) * x[1] + o * im * x[1] - t * im -
3977-
(o + zero(T) * im) * x[1]
3978-
c = MOI.add_constraint(
3979-
optimizer,
3980-
MOI.Utilities.operate(vcat, Complex{T}, func),
3981-
MOI.Zeros(1),
3970+
@requires _supports(config, MOI.optimize!)
3971+
@requires MOI.supports_constraint(
3972+
model,
3973+
MOI.VectorAffineFunction{Complex{T}},
3974+
MOI.Zeros,
39823975
)
3983-
if _supports(config, MOI.optimize!)
3984-
@test MOI.get(optimizer, MOI.TerminationStatus()) ==
3985-
MOI.OPTIMIZE_NOT_CALLED
3986-
MOI.optimize!(optimizer)
3987-
@test MOI.get(optimizer, MOI.TerminationStatus()) ==
3988-
config.optimal_status
3989-
@test (MOI.get(optimizer, MOI.VariablePrimal(), x), T[2], config)
3990-
@test (MOI.get(optimizer, MOI.ConstraintPrimal(), cx), T[2], config)
3991-
z = [zero(Complex{T})]
3992-
@test (MOI.get(optimizer, MOI.ConstraintPrimal(), c), z, config)
3993-
if _supports(config, MOI.ConstraintDual)
3994-
@test (
3995-
MOI.get(optimizer, MOI.ConstraintDual(), cx),
3996-
zeros(T, 1),
3997-
config,
3998-
)
3999-
@test (MOI.get(optimizer, MOI.ConstraintDual(), c), z, config)
4000-
end
3976+
x, cx = MOI.add_constrained_variables(model, MOI.Nonnegatives(1))
3977+
scalar_f =
3978+
(T(1) + T(0) * im) * x[1] + T(1) * im * x[1] - T(2) * im -
3979+
(T(1) + T(0) * im) * x[1]
3980+
vector_f = MOI.Utilities.operate(vcat, Complex{T}, scalar_f)
3981+
c = MOI.add_constraint(model, vector_f, MOI.Zeros(1))
3982+
@test MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMIZE_NOT_CALLED
3983+
MOI.optimize!(model)
3984+
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
3985+
@test (MOI.get(model, MOI.VariablePrimal(), x), T[2], config)
3986+
@test (MOI.get(model, MOI.ConstraintPrimal(), cx), T[2], config)
3987+
z = [zero(Complex{T})]
3988+
@test (MOI.get(model, MOI.ConstraintPrimal(), c), z, config)
3989+
if _supports(config, MOI.ConstraintDual)
3990+
@test (MOI.get(model, MOI.ConstraintDual(), cx), T[0], config)
3991+
@test (MOI.get(model, MOI.ConstraintDual(), c), z, config)
40013992
end
40023993
return
40033994
end
@@ -4021,6 +4012,8 @@ function setup_test(
40214012
return
40224013
end
40234014

4015+
version_added(::typeof(test_linear_complex_Zeros_duplicate)) = v"1.7.0"
4016+
40244017
"""
40254018
test_linear_open_intervals(
40264019
model::MOI.ModelLike,

test/Bridges/Constraint/split_complex_equalto.jl

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function test_complex_zeros()
4040
),
4141
)
4242
config = MOI.Test.Config()
43+
MOI.empty!(bridged_mock)
4344
MOI.Test.test_linear_complex_Zeros(bridged_mock, config)
4445
cis = MOI.get(
4546
mock,
@@ -66,6 +67,7 @@ function test_complex_zeros()
6667
(MOI.ScalarAffineFunction{T}, MOI.EqualTo{T}) => zeros(T, 1),
6768
),
6869
)
70+
MOI.empty!(bridged_mock)
6971
MOI.Test.test_linear_complex_Zeros_duplicate(bridged_mock, config)
7072
cis = MOI.get(
7173
mock,

test/Bridges/Constraint/split_complex_zeros.jl

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function test_complex_zeros()
3939
),
4040
)
4141
config = MOI.Test.Config()
42+
MOI.empty!(bridged_mock)
4243
MOI.Test.test_linear_complex_Zeros(bridged_mock, config)
4344
cis = MOI.get(
4445
mock,
@@ -62,6 +63,7 @@ function test_complex_zeros()
6263
(MOI.VectorAffineFunction{T}, MOI.Zeros) => [zeros(T, 1)],
6364
),
6465
)
66+
MOI.empty!(bridged_mock)
6567
MOI.Test.test_linear_complex_Zeros_duplicate(bridged_mock, config)
6668
cis = MOI.get(
6769
mock,

0 commit comments

Comments
 (0)