Skip to content

Commit da6a952

Browse files
authored
Add extra tests to nametest (#833)
1 parent 3f48578 commit da6a952

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/Test/modellike.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,53 @@ function default_status_test(model::MOI.ModelLike)
1818
end
1919

2020
function nametest(model::MOI.ModelLike)
21+
@testset "Variables" begin
22+
MOI.empty!(model)
23+
x = MOI.add_variables(model, 2)
24+
MOI.set(model, MOI.VariableName(), x[1], "x1")
25+
@test MOI.get(model, MOI.VariableIndex, "x1") == x[1]
26+
MOI.set(model, MOI.VariableName(), x[1], "x2")
27+
@test MOI.get(model, MOI.VariableIndex, "x1") === nothing
28+
@test MOI.get(model, MOI.VariableIndex, "x2") == x[1]
29+
MOI.set(model, MOI.VariableName(), x[2], "x1")
30+
@test MOI.get(model, MOI.VariableIndex, "x1") == x[2]
31+
MOI.set(model, MOI.VariableName(), x[1], "x1")
32+
@test_throws ErrorException MOI.get(model, MOI.VariableIndex, "x1")
33+
end
34+
35+
@testset "Variable bounds" begin
36+
MOI.empty!(model)
37+
x = MOI.add_variable(model)
38+
c1 = MOI.add_constraint(model, MOI.SingleVariable(x), MOI.GreaterThan(0.0))
39+
c2 = MOI.add_constraint(model, MOI.SingleVariable(x), MOI.LessThan(1.0))
40+
MOI.set(model, MOI.ConstraintName(), c1, "c1")
41+
@test MOI.get(model, MOI.ConstraintIndex, "c1") == c1
42+
MOI.set(model, MOI.ConstraintName(), c1, "c2")
43+
@test MOI.get(model, MOI.ConstraintIndex, "c1") === nothing
44+
@test MOI.get(model, MOI.ConstraintIndex, "c2") == c1
45+
MOI.set(model, MOI.ConstraintName(), c2, "c1")
46+
@test MOI.get(model, MOI.ConstraintIndex, "c1") == c2
47+
MOI.set(model, MOI.ConstraintName(), c1, "c1")
48+
@test_throws ErrorException MOI.get(model, MOI.ConstraintIndex, "c1")
49+
end
50+
51+
@testset "Affine constraints" begin
52+
MOI.empty!(model)
53+
x = MOI.add_variable(model)
54+
f = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0)
55+
c1 = MOI.add_constraint(model, f, MOI.GreaterThan(0.0))
56+
c2 = MOI.add_constraint(model, f, MOI.LessThan(1.0))
57+
MOI.set(model, MOI.ConstraintName(), c1, "c1")
58+
@test MOI.get(model, MOI.ConstraintIndex, "c1") == c1
59+
MOI.set(model, MOI.ConstraintName(), c1, "c2")
60+
@test MOI.get(model, MOI.ConstraintIndex, "c1") === nothing
61+
@test MOI.get(model, MOI.ConstraintIndex, "c2") == c1
62+
MOI.set(model, MOI.ConstraintName(), c2, "c1")
63+
@test MOI.get(model, MOI.ConstraintIndex, "c1") == c2
64+
MOI.set(model, MOI.ConstraintName(), c1, "c1")
65+
@test_throws ErrorException MOI.get(model, MOI.ConstraintIndex, "c1")
66+
end
67+
2168
@testset "Name test with $(typeof(model))" begin
2269
MOI.empty!(model)
2370
@test MOIU.supports_default_copy_to(model, #=copy_names=# true)

0 commit comments

Comments
 (0)