Skip to content

Commit 72fe299

Browse files
committed
Add tests for Expr addition and iadd
Add unit tests to verify Expr + Expr and Expr += Expr behavior. test_Expr_add_Expr constructs -x+1 and y-1, checks their string representations and the combined result (including a 0.0 constant term). test_Expr_iadd_Expr verifies in-place addition mutates the left expression, preserves the right expression, and checks their string representations.
1 parent dc8ca87 commit 72fe299

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

tests/test_expr.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,28 @@ def test_term_eq():
328328
assert t3 != t4 # same length, but different term
329329
assert t1 != t3 # different length
330330
assert t1 != "not a term" # different type
331+
332+
333+
def test_Expr_add_Expr():
334+
m = Model()
335+
x = m.addVar(name="x")
336+
y = m.addVar(name="y")
337+
338+
e1 = -x + 1
339+
e2 = y - 1
340+
e3 = e1 + e2
341+
assert str(e1) == "Expr({Term(x): -1.0, Term(): 1.0})"
342+
assert str(e2) == "Expr({Term(y): 1.0, Term(): -1.0})"
343+
assert str(e3) == "Expr({Term(x): -1.0, Term(): 0.0, Term(y): 1.0})"
344+
345+
346+
def test_Expr_iadd_Expr():
347+
m = Model()
348+
x = m.addVar(name="x")
349+
y = m.addVar(name="y")
350+
351+
e1 = -x + 1
352+
e2 = y - 1
353+
e1 += e2
354+
assert str(e1) == "Expr({Term(x): -1.0, Term(): 0.0, Term(y): 1.0})"
355+
assert str(e2) == "Expr({Term(y): 1.0, Term(): -1.0})"

0 commit comments

Comments
 (0)