Skip to content

Commit

Permalink
Improve test coverage (#3869)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 5, 2024
1 parent b68df42 commit 6768f9b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
15 changes: 2 additions & 13 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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 =
Expand Down
19 changes: 19 additions & 0 deletions test/test_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,23 @@ 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
44 changes: 44 additions & 0 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2443,4 +2443,48 @@ function test_constraint_vect_vcat()
return
end

function test_set_bounds_twice_error()
model = Model()
@test_throws_parsetime(
ErrorException(
"In `@variable(model, x >= 0, lower_bound = 1)`: " *
"Cannot specify variable lower_bound twice",
),
@variable(model, x >= 0, lower_bound = 1),
)
@test_throws_parsetime(
ErrorException(
"In `@variable(model, x <= 0, upper_bound = 1)`: " *
"Cannot specify variable upper_bound twice",
),
@variable(model, x <= 0, upper_bound = 1),
)
@test_throws_parsetime(
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_parsetime(
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
10 changes: 10 additions & 0 deletions test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1338,4 +1338,14 @@ function test_set_abstract_string()
return
end

function test_iterate_scalar()
model = Model()
@variable(model, x)
@test collect(x) == [x]
@test !isempty(x)
@test iterate(x) == (x, true)
@test iterate(x, true) === nothing
return
end

end # module TestModels

0 comments on commit 6768f9b

Please sign in to comment.