Skip to content

Commit

Permalink
Improve test coverage (#3875)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Nov 7, 2024
1 parent 738e24a commit e42bf15
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/Containers/DenseAxisArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
struct _AxisLookup{D}
data::D
end
Base.:(==)(x::_AxisLookup{D}, y::_AxisLookup{D}) where {D} = x.data == y.data

# Default fallbacks.
Base.getindex(::_AxisLookup, key) = throw(KeyError(key))
Expand Down
47 changes: 47 additions & 0 deletions test/Containers/test_DenseAxisArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -874,4 +874,51 @@ function test_multi_arg_eachindex()
return
end

function test_LinearIndices()
Containers.@container(x[i in 2:3], i)
@test_throws(
ErrorException("DenseAxisArray does not support this operation."),
LinearIndices(x),
)
return
end

function test_CartesianIndices()
Containers.@container(x[i in 2:3], i)
@test CartesianIndices(x) == CartesianIndices((2,))
return
end

function test_show_nd()
Containers.@container(
x[a in 2:3, b in 2:3, c in 2:14, d in 2:14],
(a, b, c, d),
)
s = sprint(io -> show(IOContext(io, :limit => true), x))
limit_indices = [2, 3, 4, 12, 13, 14]
for c in 2:14, d in 2:14
is_visible = (c in limit_indices && d in limit_indices)
@test occursin("[:, :, $c, $d]", s) == is_visible
for a in 2:3, b in 2:3
@test occursin("($a, $b, $c, $d)", s) == is_visible
end
end
s = sprint(io -> show(IOContext(io, :limit => false), x))
limit_indices = [2, 3, 4, 12, 13, 14]
for c in 2:14, d in 2:14
@test occursin("[:, :, $c, $d]", s)
for a in 2:3, b in 2:3
@test occursin("($a, $b, $c, $d)", s)
end
end
return
end

function test_view_DenseAxisArray()
Containers.@container(x[a in 2:3], a)
@test_throws KeyError view(x, 3:4)
@test_throws KeyError view(x, 4)
return
end

end # module
7 changes: 7 additions & 0 deletions test/Containers/test_SparseAxisArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,11 @@ function test_sparseaxisarray_order()
return
end

function test_show_empty_limit()
x = SparseAxisArray(Dict{Tuple{Int},Int}())
@test sprint(io -> show(IOContext(io, :limit => false), x)) ==
"$SparseAxisArray{$Int, 1, Tuple{$Int}} with 0 entries"
return
end

end # module
6 changes: 6 additions & 0 deletions test/Containers/test_macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,10 @@ function test_add_additional_args()
return
end

function test_trailing_semicolon()
Containers.@container(x[a in 2:3;], a)
@test x isa DenseAxisArray
return
end

end # module
10 changes: 10 additions & 0 deletions test/Containers/test_vectorized_product_iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function test_VectorizedProductIterator()
@test isempty(collect(Containers.vectorized_product(2, I, 1:0)))
@test collect(Containers.vectorized_product(2, I)) ==
[(2, 1) (2, 3) (2, 2) (2, 4)]
@test ndims(Containers.vectorized_product(2, I)) == 2
return
end

Expand All @@ -35,4 +36,13 @@ function test_infinite_size()
return
end

function test_container_two_arg()
a = Containers.container(Containers.vectorized_product(2:3, 1:2)) do i, j
return (i, j)
end
Containers.@container(b[i in 2:3, j in 1:2], (i, j))
@test a == b
return
end

end # module
22 changes: 16 additions & 6 deletions test/test_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2194,27 +2194,37 @@ function test_shadow_price_errors()
@test_throws err shadow_price(c)
c = @constraint(model, x == 1)
@test_throws err shadow_price(c)

model = Model()
@variable(model, x >= 0)
@variable(model, 0 <= x <= 1)
set_optimizer(
model,
() -> MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}()),
() -> MOI.Utilities.MockOptimizer(
MOI.Utilities.Model{Float64}();
eval_variable_constraint_dual = false,
),
)
optimize!(model)
mock = unsafe_backend(model)
MOI.set(mock, MOI.TerminationStatus(), MOI.OPTIMAL)
MOI.set(mock, MOI.PrimalStatus(), MOI.FEASIBLE_POINT)
MOI.set(mock, MOI.DualStatus(), MOI.FEASIBLE_POINT)
F, S = MOI.VariableIndex, MOI.GreaterThan{Float64}
xi = only(MOI.get(mock, MOI.ListOfVariableIndices()))
MOI.set(mock, MOI.ConstraintDual(), MOI.ConstraintIndex{F,S}(xi.value), 1.0)
F = MOI.VariableIndex
S1, S2 = MOI.GreaterThan{Float64}, MOI.LessThan{Float64}
i = only(MOI.get(mock, MOI.ListOfVariableIndices())).value
MOI.set(mock, MOI.ConstraintDual(), MOI.ConstraintIndex{F,S1}(i), 1.0)
MOI.set(mock, MOI.ConstraintDual(), MOI.ConstraintIndex{F,S2}(i), -1.0)
@test_throws(
ErrorException(
"The shadow price is not available because the objective sense $FEASIBILITY_SENSE is not minimization or maximization.",
),
shadow_price(LowerBoundRef(x)),
)
@test_throws(
ErrorException(
"The shadow price is not available because the objective sense $FEASIBILITY_SENSE is not minimization or maximization.",
),
shadow_price(UpperBoundRef(x)),
)
return
end

Expand Down
23 changes: 23 additions & 0 deletions test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1389,4 +1389,27 @@ function test_deepcopy()
return
end

struct _ModelNoSolverName <: MOI.AbstractOptimizer end

MOI.is_empty(::_ModelNoSolverName) = true

function test_solver_name_not_implemented()
model = direct_model(_ModelNoSolverName())
@test solver_name(model) ==
"SolverName() attribute not implemented by the optimizer."
return
end

struct _ModelSolverNameError <: MOI.AbstractOptimizer end

MOI.is_empty(::_ModelSolverNameError) = true

MOI.get(::_ModelSolverNameError, ::MOI.SolverName) = error("test")

function test_solver_name_error()
model = direct_model(_ModelSolverNameError())
@test_throws ErrorException("test") solver_name(model)
return
end

end # module TestModels

0 comments on commit e42bf15

Please sign in to comment.