Skip to content

Commit 5396824

Browse files
committed
Add tests for power operator on polynomial expressions
Introduces test_pow to verify correct behavior of the power (**) operator for PolynomialExpr and ConstExpr, including type and value checks, as well as error handling for invalid exponent types.
1 parent 9cbc350 commit 5396824

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

tests/test_PolynomialExpr.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,21 @@ def test_neg(model):
189189
expr = -ConstExpr(-3.0)
190190
assert isinstance(expr, ConstExpr)
191191
assert str(expr) == "Expr({Term(): 3.0})"
192+
193+
194+
def test_pow(model):
195+
m, x, y = model
196+
197+
expr = PolynomialExpr({Term(x): -2.0, Term(y): 4.0})
198+
res = expr**2
199+
assert type(res) is PolynomialExpr
200+
assert str(res) == "Expr({Term(x, x): 4.0, Term(x, y): -16.0, Term(y, y): 16.0})"
201+
202+
expr = ConstExpr(-1.0)
203+
res = expr**2
204+
assert isinstance(res, ConstExpr)
205+
assert str(res) == "Expr({Term(): 1.0})"
206+
207+
expr = ConstExpr(-1.0)
208+
with pytest.raises(TypeError):
209+
expr**x

0 commit comments

Comments
 (0)