From 661d8b655f8f7f564583a931214da35f5fc236bc Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 5 Nov 2024 14:09:53 +1300 Subject: [PATCH] Add more tests --- src/variables.jl | 15 ++------------- test/test_expr.jl | 20 ++++++++++++++++++++ test/test_macros.jl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 13 deletions(-) diff --git a/src/variables.jl b/src/variables.jl index 07912a8c5c4..9fd501b90ae 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -73,9 +73,7 @@ function _set_upper_bound_or_error( end function _fix_or_error(error_fn::Function, info::_VariableInfoExpr, value) - if info.has_fix - error_fn("Cannot specify variable fixed value twice") - end + @assert !info.has_fix info.has_fix = true info.fixed_value = value return @@ -2206,15 +2204,6 @@ function VariablesConstrainedOnCreation( return VariablesConstrainedOnCreation(variables, set, VectorShape()) end -_vectorize_names(names, shape) = vectorize(names, shape) - -# In some cases `names` may be a "" passed in from the macros. For now, this is -# the only case we support, so throw an assertion error if not empty. -function _vectorize_names(name::String, ::Any) - @assert isempty(name) - return -end - function add_variable( model::GenericModel{T}, variable::VariablesConstrainedOnCreation, @@ -2224,7 +2213,7 @@ function add_variable( backend(model), variable.scalar_variables, variable.set, - _vectorize_names(names, variable.shape), + vectorize(names, variable.shape), T, ) var_refs = diff --git a/test/test_expr.jl b/test/test_expr.jl index d5b11771dce..3e2dc96525c 100644 --- a/test/test_expr.jl +++ b/test/test_expr.jl @@ -552,4 +552,24 @@ function test_multiply_expr_MA_Zero() return end +function test_aff_expr_constructors() + model = Model() + @variable(model, x) + y = 3.0 * x + 1.0 + @test isequal_canonical(AffExpr(1.0, x => 1.0, x => 2.0), y) + @test isequal_canonical(GenericAffExpr(1.0, [x => 1.0, x => 2.0]), y) + @test isequal_canonical(AffExpr(2 // 2, x => 1.0, x => 2.0), y) + @test isequal_canonical(AffExpr(2 // 2, [x => 1.0, x => 2.0]), y) + return +end + +function test_LinearTermIterator_eltype() + model = Model() + @variable(model, x) + y = 3.0 * x + 1.0 + @test eltype(linear_terms(y)) == Tuple{Float64,VariableRef} + return +end + + end # TestExpr diff --git a/test/test_macros.jl b/test/test_macros.jl index 469cf4d173a..a005fc5b364 100644 --- a/test/test_macros.jl +++ b/test/test_macros.jl @@ -2443,4 +2443,48 @@ function test_constraint_vect_vcat() return end +function test_set_bounds_twice_error() + model = Model() + @test_throws_runtime( + ErrorException( + "In `@variable(model, x >= 0, lower_bound = 1)`: " * + "Cannot specify variable lower_bound twice", + ), + @variable(model, x >= 0, lower_bound = 1), + ) + @test_throws_runtime( + ErrorException( + "In `@variable(model, x <= 0, upper_bound = 1)`: " * + "Cannot specify variable upper_bound twice", + ), + @variable(model, x <= 0, upper_bound = 1), + ) + @test_throws_runtime( + ErrorException( + "In `@variable(model, x, Bin, binary = true)`: " * + "'Bin' and 'binary' keyword argument cannot both be specified.", + ), + @variable(model, x, Bin, binary = true), + ) + @test_throws_runtime( + ErrorException( + "In `@variable(model, x, Int, integer = true)`: " * + "'Int' and 'integer' keyword argument cannot both be specified.", + ), + @variable(model, x, Int, integer = true), + ) + return +end + +function test_array_scalar_sets() + model = Model() + sets = [Semicontinuous(2, 3), Semiinteger(2, 3)] + @variable(model, x[1:2] in sets) + c = only(all_constraints(model, VariableRef, MOI.Semicontinuous{Int})) + @test constraint_object(c).func == x[1] + c = only(all_constraints(model, VariableRef, MOI.Semiinteger{Int})) + @test constraint_object(c).func == x[2] + return +end + end # module