Skip to content

Commit 5148524

Browse files
committed
[Utilities] improve test coverage
1 parent 95f1c7b commit 5148524

File tree

4 files changed

+72
-12
lines changed

4 files changed

+72
-12
lines changed

test/Utilities/model.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,38 @@ function test_variable_coefficient_VectorQuadraticFunction()
584584
return
585585
end
586586

587+
function test_constraints_invalid_index()
588+
model = MOI.Utilities.Model{Float64}()
589+
F, S = FunctionNotSupportedBySolvers, MOI.ZeroOne
590+
ci = MOI.ConstraintIndex{F,S}(1)
591+
@test_throws(
592+
MOI.InvalidIndex(ci),
593+
MOI.Utilities.constraints(model.constraints, ci),
594+
)
595+
return
596+
end
597+
598+
function test_struct_of_constraints_by_set_types()
599+
MOI.Utilities.@struct_of_constraints_by_set_types(
600+
MySet1,
601+
Union{MOI.LessThan{T},MOI.GreaterThan{T}},
602+
MOI.EqualTo{T},
603+
MOI.ZeroOne,
604+
Int,
605+
)
606+
set = MySet1{Float64,Vector{Float64},Vector{Float64},Vector{Bool},Int}()
607+
@test set.num_variables == 0
608+
@test set.int === nothing
609+
set.int = 1
610+
@test set.moi_zeroone === nothing
611+
set.moi_zeroone = Bool[]
612+
@test set.moi_equalto === nothing
613+
set.moi_equalto = Float64[]
614+
@test set.moi_lessthan === nothing
615+
set.moi_lessthan = Float64[]
616+
return
617+
end
618+
587619
end # module
588620

589621
TestModel.runtests()

test/Utilities/print.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,17 @@ function test_print_model_to_stdout()
751751
return
752752
end
753753

754+
function test_print_constraint_name_unsupported()
755+
model = MOI.Utilities.MockOptimizer(
756+
MOI.Utilities.Model{Float64}();
757+
supports_names = false,
758+
)
759+
x = MOI.add_variable(model)
760+
MOI.add_constraint(model, 1.0 * x, MOI.LessThan(1.0))
761+
@test occursin("0.0 + 1.0 v[$(x.value)] <= 1.0", sprint(print, model))
762+
return
763+
end
764+
754765
end
755766

756767
TestPrint.runtests()

test/Utilities/product_of_sets.jl

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,17 @@ end
5050

5151
function test_scalar_basic()
5252
sets = _ScalarSets{Float64}()
53-
ci = MOI.ConstraintIndex{
54-
MOI.ScalarAffineFunction{Float64},
55-
MOI.EqualTo{Float64},
56-
}(
57-
12345,
58-
)
53+
F, S = MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}
54+
ci = MOI.ConstraintIndex{F,S}(12345)
5955
@test !MOI.is_valid(sets, ci)
6056
i = MOI.Utilities.set_index(sets, MOI.EqualTo{Float64})
6157
ci_value = MOI.Utilities.add_set(sets, i)
62-
ci = MOI.ConstraintIndex{
63-
MOI.ScalarAffineFunction{Float64},
64-
MOI.EqualTo{Float64},
65-
}(
66-
ci_value,
67-
)
58+
ci = MOI.ConstraintIndex{F,S}(ci_value)
6859
@test MOI.is_valid(sets, ci)
6960
@test MOI.Utilities.rows(sets, ci) == ci.value
61+
ci = MOI.ConstraintIndex{F,MOI.ZeroOne}(1)
62+
@test !MOI.is_valid(sets, ci)
63+
return
7064
end
7165

7266
function test_scalar_dimension()

test/Utilities/universalfallback.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,29 @@ function test_ListOfConstraintsWithAttributeSet()
487487
return
488488
end
489489

490+
function test_delete_ci_attribute()
491+
model = MOI.Utilities.UniversalFallback(
492+
ModelForUniversalFallback{Float64}(),
493+
)
494+
x = MOI.add_variable(model)
495+
c = MOI.add_constraint(model, MOI.VectorOfVariables([x]), MOI.Nonnegatives(1))
496+
MOI.set(model, MOI.ConstraintPrimalStart(), c, [1.0])
497+
@test model.conattr[MOI.ConstraintPrimalStart()][c] == [1.0]
498+
MOI.delete(model, x)
499+
@test !haskey(model.conattr[MOI.ConstraintPrimalStart()], c)
500+
# Vector
501+
model = MOI.Utilities.UniversalFallback(
502+
ModelForUniversalFallback{Float64}(),
503+
)
504+
x = MOI.add_variable(model)
505+
c = MOI.add_constraint(model, MOI.VectorOfVariables([x]), MOI.Nonnegatives(1))
506+
MOI.set(model, MOI.ConstraintPrimalStart(), c, [1.0])
507+
@test model.conattr[MOI.ConstraintPrimalStart()][c] == [1.0]
508+
MOI.delete(model, [x])
509+
@test !haskey(model.conattr[MOI.ConstraintPrimalStart()], c)
510+
return
511+
end
512+
490513
end # module
491514

492515
TestUniversalFallback.runtests()

0 commit comments

Comments
 (0)