Skip to content

Commit 6acead2

Browse files
committed
Add tests
1 parent 412ad2e commit 6acead2

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/Utilities/functions.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,9 @@ of `t1` as the `variable_index`'s of the output without checking that they are
196196
the same (up to permutation) to those of `t2`.
197197
"""
198198
function unsafe_add(t1::MOI.ScalarQuadraticTerm, t2::MOI.ScalarQuadraticTerm)
199-
return MOI.ScalarAffineTerm(t1.coefficient + t2.coefficient,
200-
t1.variable_index)
199+
return MOI.ScalarQuadraticTerm(t1.coefficient + t2.coefficient,
200+
t1.variable_index_1,
201+
t1.variable_index_2)
201202
end
202203

203204
"""
@@ -688,6 +689,11 @@ function operate!(op::Union{typeof(+), typeof(-)}, ::Type{T},
688689
f.constant = op(f.constant, g.constant)
689690
return f
690691
end
692+
function operate!(op::Union{typeof(+), typeof(-)}, ::Type{T},
693+
f::MOI.ScalarAffineFunction{T},
694+
g::MOI.ScalarQuadraticFunction{T}) where T
695+
return operate(op, T, f, g)
696+
end
691697
# Scalar Quadratic +/-! ...
692698
function operate!(op::Union{typeof(+), typeof(-)}, ::Type{T},
693699
f::MOI.ScalarQuadraticFunction{T},
@@ -786,8 +792,8 @@ end
786792
function Base.:+(args::ScalarLike{T}...) where T
787793
return operate(+, T, args...)
788794
end
789-
function Base.:+::T, f::ScalarLike{T}) where T
790-
return operate(+, T, α, f)
795+
function Base.:+::T, f::ScalarLike{T}...) where T
796+
return operate(+, T, α, f...)
791797
end
792798
function Base.:+(f::ScalarLike{T}, α::T) where T
793799
return operate(+, T, f, α)

test/functions.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,18 @@
226226
@test MOIU.promote_operation(+, Int,
227227
MOI.ScalarAffineFunction{Int},
228228
MOI.ScalarQuadraticFunction{Int}) == MOI.ScalarQuadraticFunction{Int}
229+
@test MOIU.promote_operation(*, Int,
230+
MOI.SingleVariable,
231+
MOI.SingleVariable) == MOI.ScalarQuadraticFunction{Int}
232+
@test MOIU.promote_operation(*, Float64,
233+
MOI.SingleVariable,
234+
MOI.ScalarAffineFunction{Float64}) == MOI.ScalarQuadraticFunction{Float64}
235+
@test MOIU.promote_operation(*, Int,
236+
MOI.ScalarAffineFunction{Int},
237+
MOI.SingleVariable) == MOI.ScalarQuadraticFunction{Int}
238+
@test MOIU.promote_operation(*, Float64,
239+
MOI.ScalarAffineFunction{Float64},
240+
MOI.ScalarAffineFunction{Float64}) == MOI.ScalarQuadraticFunction{Float64}
229241
end
230242
fx = MOI.SingleVariable(x)
231243
fy = MOI.SingleVariable(y)
@@ -236,8 +248,13 @@
236248
@test !MOIU.isapprox_zero(f, 1e-8)
237249
# Test isapprox_zero with zero terms
238250
@test MOIU.isapprox_zero(0 * f, 1e-8)
251+
g = 1.0fx * fy - (1 + 1e-6) * fy * fx
252+
MOIU.canonicalize!(g)
253+
@test MOIU.isapprox_zero(g, 1e-5)
254+
@test !MOIU.isapprox_zero(g, 1e-7)
239255
end
240256
@testset "operate" begin
257+
@test f 7 + (fx + 2fy) * (1fx + fy) + 3fx
241258
@test f 7 + MOIU.operate(*, Int, fx, fx) + 3fx * (fy + 1) + 2fy * fy
242259
@test f (fx + 2) * (fx + 1) + (fy + 1) * (2fy + 3fx) + (5 - 3fx - 2fy)
243260
@test f begin

0 commit comments

Comments
 (0)