From cdb6d1a8bfcee1e7ce5e04a75126669b56ce96f1 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Mon, 30 Jul 2018 09:23:41 +1200 Subject: [PATCH] Fix tests adding multiple variable bounds (#434) * Stop adding multiple SingleVariable-in-Set constraints in the unit tests. * Fix orderedindices test --- src/Test/UnitTests/basic_constraint_tests.jl | 36 ++++++++++---------- src/Test/modellike.jl | 12 +++---- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Test/UnitTests/basic_constraint_tests.jl b/src/Test/UnitTests/basic_constraint_tests.jl index 91c96708e0..f635a07b6d 100644 --- a/src/Test/UnitTests/basic_constraint_tests.jl +++ b/src/Test/UnitTests/basic_constraint_tests.jl @@ -174,9 +174,9 @@ function basic_constraint_test_helper(model::MOI.ModelLike, config::TestConfig, end @testset "addconstraint!" begin - n = MOI.get(model, MOI.NumberOfConstraints{F,S}()) + @test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == 0 c = MOI.addconstraint!(model, constraint_function, set) - @test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == n + 1 + @test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == 1 @testset "ConstraintName" begin @test MOI.canget(model, MOI.ConstraintName(), typeof(c)) @@ -200,37 +200,37 @@ function basic_constraint_test_helper(model::MOI.ModelLike, config::TestConfig, end end - @testset "addconstraints!" begin - n = MOI.get(model, MOI.NumberOfConstraints{F,S}()) - cc = MOI.addconstraints!(model, - [constraint_function, constraint_function], - [set, set] - ) - @test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == n + 2 - end - @testset "ListOfConstraintIndices" begin @test MOI.canget(model, MOI.ListOfConstraintIndices{F,S}()) c_indices = MOI.get(model, MOI.ListOfConstraintIndices{F,S}()) - # for sanity, check that we've added 3 constraints as expected. - @test length(c_indices) == MOI.get(model, MOI.NumberOfConstraints{F,S}()) == 3 + @test length(c_indices) == MOI.get(model, MOI.NumberOfConstraints{F,S}()) == 1 + end + + if F != MOI.SingleVariable + # We can't add multiple single variable constraints as these are + # interpreted as bounds etc. + @testset "addconstraints!" begin + n = MOI.get(model, MOI.NumberOfConstraints{F,S}()) + cc = MOI.addconstraints!(model, + [constraint_function, constraint_function], + [set, set] + ) + @test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == 3 + @test length(MOI.get(model, MOI.ListOfConstraintIndices{F,S}())) == 3 + end end @testset "isvalid" begin c_indices = MOI.get(model, MOI.ListOfConstraintIndices{F,S}()) - # for sanity, check that we've added 3 constraints as expected. - @test length(c_indices) == 3 @test all(MOI.isvalid.(Ref(model), c_indices)) end if delete @testset "delete!" begin c_indices = MOI.get(model, MOI.ListOfConstraintIndices{F,S}()) - # for sanity, check that we've added 3 constraints as expected. - @test length(c_indices) == 3 @test MOI.candelete(model, c_indices[1]) MOI.delete!(model, c_indices[1]) - @test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == length(c_indices)-1 == 2 + @test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == length(c_indices)-1 @test !MOI.isvalid(model, c_indices[1]) end end diff --git a/src/Test/modellike.jl b/src/Test/modellike.jl index 76ed1a102d..858d5713f4 100644 --- a/src/Test/modellike.jl +++ b/src/Test/modellike.jl @@ -315,17 +315,17 @@ function orderedindicestest(model::MOI.ModelLike) @test MOI.get(model, MOI.ListOfVariableIndices()) == [v2] v3 = MOI.addvariable!(model) @test MOI.get(model, MOI.ListOfVariableIndices()) == [v2, v3] + v4 = MOI.addvariable!(model) + @test MOI.get(model, MOI.ListOfVariableIndices()) == [v2, v3, v4] - # Note: there are too many combinations to - # test, so we're just going to check - # SingleVariable-in-LessThan and hope it - # works for the rest + # Note: there are too many combinations to test, so we're just going to + # check SingleVariable-in-LessThan and hope it works for the rest c1 = MOI.addconstraint!(model, MOI.SingleVariable(v2), MOI.LessThan(1.0)) @test MOI.get(model, MOI.ListOfConstraintIndices{MOI.SingleVariable, MOI.LessThan{Float64}}()) == [c1] - c2 = MOI.addconstraint!(model, MOI.SingleVariable(v2), MOI.LessThan(2.0)) + c2 = MOI.addconstraint!(model, MOI.SingleVariable(v3), MOI.LessThan(2.0)) @test MOI.get(model, MOI.ListOfConstraintIndices{MOI.SingleVariable, MOI.LessThan{Float64}}()) == [c1, c2] MOI.delete!(model, c1) @test MOI.get(model, MOI.ListOfConstraintIndices{MOI.SingleVariable, MOI.LessThan{Float64}}()) == [c2] - c3 = MOI.addconstraint!(model, MOI.SingleVariable(v2), MOI.LessThan(3.0)) + c3 = MOI.addconstraint!(model, MOI.SingleVariable(v4), MOI.LessThan(3.0)) @test MOI.get(model, MOI.ListOfConstraintIndices{MOI.SingleVariable, MOI.LessThan{Float64}}()) == [c2, c3] end