From 710c7e77cdf517f118aa05d8ce41d47fc9fbae74 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 7 Nov 2024 16:39:05 +1300 Subject: [PATCH 1/5] Improve test coverage --- src/Containers/DenseAxisArray.jl | 1 - test/Containers/test_DenseAxisArray.jl | 47 +++++++++++++++++++ test/Containers/test_SparseAxisArray.jl | 7 +++ test/Containers/test_macro.jl | 6 +++ .../test_vectorized_product_iterator.jl | 11 +++++ 5 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/Containers/DenseAxisArray.jl b/src/Containers/DenseAxisArray.jl index 0e421cd36c6..4f124de0a50 100644 --- a/src/Containers/DenseAxisArray.jl +++ b/src/Containers/DenseAxisArray.jl @@ -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)) diff --git a/test/Containers/test_DenseAxisArray.jl b/test/Containers/test_DenseAxisArray.jl index fd780720a35..5d4d1345621 100644 --- a/test/Containers/test_DenseAxisArray.jl +++ b/test/Containers/test_DenseAxisArray.jl @@ -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 diff --git a/test/Containers/test_SparseAxisArray.jl b/test/Containers/test_SparseAxisArray.jl index 86a6b0d85df..296dc83dfba 100644 --- a/test/Containers/test_SparseAxisArray.jl +++ b/test/Containers/test_SparseAxisArray.jl @@ -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 diff --git a/test/Containers/test_macro.jl b/test/Containers/test_macro.jl index 9227e240fe0..a4c11493c91 100644 --- a/test/Containers/test_macro.jl +++ b/test/Containers/test_macro.jl @@ -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 diff --git a/test/Containers/test_vectorized_product_iterator.jl b/test/Containers/test_vectorized_product_iterator.jl index 03b509fbbf3..67f7ae5bbf3 100644 --- a/test/Containers/test_vectorized_product_iterator.jl +++ b/test/Containers/test_vectorized_product_iterator.jl @@ -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 @@ -35,4 +36,14 @@ 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 From 4a72c5f20388012d0565efefc2fabb898dccd833 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 7 Nov 2024 16:56:46 +1300 Subject: [PATCH 2/5] Update --- test/test_constraint.jl | 17 ++++++++++++----- test/test_model.jl | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/test/test_constraint.jl b/test/test_constraint.jl index cb63bb99e29..ad580398414 100644 --- a/test/test_constraint.jl +++ b/test/test_constraint.jl @@ -2194,9 +2194,8 @@ 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}()), @@ -2206,15 +2205,23 @@ function test_shadow_price_errors() 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 diff --git a/test/test_model.jl b/test/test_model.jl index 6db3a692ed0..dd6fc3e8a38 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -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 From 8a6b809f5d5c72cb563ba4121d76d11454492c52 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 7 Nov 2024 16:57:59 +1300 Subject: [PATCH 3/5] Update --- test/Containers/test_macro.jl | 2 +- test/Containers/test_vectorized_product_iterator.jl | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/test/Containers/test_macro.jl b/test/Containers/test_macro.jl index a4c11493c91..cdb2d3fe613 100644 --- a/test/Containers/test_macro.jl +++ b/test/Containers/test_macro.jl @@ -267,7 +267,7 @@ function test_add_additional_args() end function test_trailing_semicolon() - Containers.@container(x[a in 2:3; ], a) + Containers.@container(x[a in 2:3;], a) @test x isa DenseAxisArray return end diff --git a/test/Containers/test_vectorized_product_iterator.jl b/test/Containers/test_vectorized_product_iterator.jl index 67f7ae5bbf3..d38a07850b3 100644 --- a/test/Containers/test_vectorized_product_iterator.jl +++ b/test/Containers/test_vectorized_product_iterator.jl @@ -45,5 +45,4 @@ function test_container_two_arg() return end - end # module From 76409f894eb9d54bab562e368c7e9220d2e8b719 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 7 Nov 2024 17:00:20 +1300 Subject: [PATCH 4/5] Update --- test/test_model.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_model.jl b/test/test_model.jl index dd6fc3e8a38..2f6009acf3f 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -1394,7 +1394,7 @@ struct _ModelNoSolverName <: MOI.AbstractOptimizer end MOI.is_empty(::_ModelNoSolverName) = true function test_solver_name_not_implemented() - model = direct_model(_ModelNoSolverName()) + model = direct_model(_ModelNoSolverName()) @test solver_name(model) == "SolverName() attribute not implemented by the optimizer." return @@ -1407,7 +1407,7 @@ MOI.is_empty(::_ModelSolverNameError) = true MOI.get(::_ModelSolverNameError, ::MOI.SolverName) = error("test") function test_solver_name_error() - model = direct_model(_ModelSolverNameError()) + model = direct_model(_ModelSolverNameError()) @test_throws ErrorException("test") solver_name(model) return end From d87a9c5c1baa000ee151fff85adc8aebfe573ef5 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 7 Nov 2024 17:40:44 +1300 Subject: [PATCH 5/5] Update --- test/test_constraint.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/test_constraint.jl b/test/test_constraint.jl index ad580398414..0ae9d46fca5 100644 --- a/test/test_constraint.jl +++ b/test/test_constraint.jl @@ -2198,7 +2198,10 @@ function test_shadow_price_errors() @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)