Skip to content

Commit 19574de

Browse files
authored
[Utilities] improve test coverage (#2645)
1 parent 1a8faea commit 19574de

File tree

5 files changed

+83
-12
lines changed

5 files changed

+83
-12
lines changed

test/Utilities/model.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,39 @@ 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+
MOI.Utilities.@struct_of_constraints_by_set_types(
599+
Set4802,
600+
Union{MOI.LessThan{T},MOI.GreaterThan{T}},
601+
MOI.EqualTo{T},
602+
MOI.ZeroOne,
603+
Int,
604+
)
605+
606+
function test_struct_of_constraints_by_set_types()
607+
set = Set4802{Float64,Vector{Float64},Vector{Float64},Vector{Bool},Int}()
608+
@test set.num_variables == 0
609+
@test set.int === nothing
610+
set.int = 1
611+
@test set.moi_zeroone === nothing
612+
set.moi_zeroone = Bool[]
613+
@test set.moi_equalto === nothing
614+
set.moi_equalto = Float64[]
615+
@test set.moi_lessthan === nothing
616+
set.moi_lessthan = Float64[]
617+
return
618+
end
619+
587620
end # module
588621

589622
TestModel.runtests()

test/Utilities/parser.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ function test_parse_unrecognized_expression()
441441
ErrorException("Unrecognized expression []"),
442442
MOI.Utilities.loadfromstring!(model, "[]"),
443443
)
444+
@test_throws(
445+
ErrorException("Unrecognized expression foo(x)"),
446+
MOI.Utilities.loadfromstring!(model, "variables: x\nfoo(x)"),
447+
)
444448
return
445449
end
446450

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,35 @@ function test_ListOfConstraintsWithAttributeSet()
487487
return
488488
end
489489

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

492521
TestUniversalFallback.runtests()

0 commit comments

Comments
 (0)