Skip to content

Commit 0432c48

Browse files
committed
Fix evaluate sum method and add missing tests
1 parent 3521576 commit 0432c48

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/forward.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ function evaluate(::Add, arg1::BinaryOperation{Sub}, arg2::BinaryOperation{Mult}
740740
end
741741

742742
function evaluate(::Add, arg1::BinaryOperation{Add}, arg2::BinaryOperation{Sub})
743-
return evaluate(Add, arg2, arg2)
743+
return evaluate(Add(), arg2, arg1)
744744
end
745745

746746
function evaluate(::Add, arg1::BinaryOperation{Sub}, arg2::BinaryOperation{Add})

test/ForwardTest.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ end
114114
@test evaluate(dc.BinaryOperation{dc.Add}(z, d)) == d
115115
end
116116

117+
@testset "evaluate sum of real and real" begin
118+
function add(l, r)
119+
return dc.BinaryOperation{dc.Add}(l, r)
120+
end
121+
122+
@test evaluate(add(2, 2)) == 4
123+
end
124+
117125
@testset "evaluate sum of addition and addition" begin
118126
a = Variable("a", Upper(1))
119127
b = Variable("b", Upper(1))
@@ -196,6 +204,24 @@ end
196204
sub = dc.BinaryOperation{dc.Sub}(a, b)
197205
add = dc.BinaryOperation{dc.Add}(sub, add_inner)
198206
@test evaluate(add) == add
207+
208+
# c + d + a - b
209+
add_inner = dc.BinaryOperation{dc.Add}(c, d)
210+
sub = dc.BinaryOperation{dc.Sub}(a, b)
211+
add = dc.BinaryOperation{dc.Add}(add_inner, sub)
212+
@test evaluate(add) == add
213+
end
214+
215+
@testset "evaluate sum of subtraction and zero" begin
216+
a = Variable("a", Upper(1))
217+
b = Variable("b", Upper(1))
218+
c = Variable("c", Upper(1))
219+
220+
# a + b - (c - c)
221+
add = dc.BinaryOperation{dc.Add}(a, b)
222+
sub = dc.BinaryOperation{dc.Sub}(c, c)
223+
add = dc.BinaryOperation{dc.Add}(add, sub)
224+
@test evaluate(add) == dc.BinaryOperation{dc.Add}(a, b)
199225
end
200226

201227
@testset "evaluate sum of subtraction and subtraction" begin
@@ -597,6 +623,16 @@ end
597623
@test dc.evaluate(sub(Z, prod)) == -prod
598624
end
599625

626+
@testset "evaluate subtraction with real and real" begin
627+
function sub(l, r)
628+
return dc.BinaryOperation{dc.Sub}(l, r)
629+
end
630+
631+
@test dc.evaluate(sub(3, 2)) == 1
632+
@test dc.evaluate(sub(3, 3)) == 0
633+
@test dc.evaluate(sub(2, 3)) == -1
634+
end
635+
600636
@testset "evaluate unary operations" begin
601637
A = Variable("A", Upper(1), Lower(2))
602638

0 commit comments

Comments
 (0)