from fractions import Fraction
-from pypol.linear import *
+from ..linexprs import *
try:
self.pi = Expression(constant=Fraction(22, 7))
self.expr = self.x - 2*self.y + 3
- def test_new_subclass(self):
+ def test_new(self):
self.assertIsInstance(self.x, Symbol)
self.assertIsInstance(self.pi, Constant)
self.assertNotIsInstance(self.x + self.pi, Symbol)
self.assertNotIsInstance(self.x + self.pi, Constant)
xx = Expression({'x': 2})
self.assertNotIsInstance(xx, Symbol)
-
- def test_new_types(self):
with self.assertRaises(TypeError):
Expression('x + y', 2)
self.assertEqual(Expression({'x': 2}), Expression({self.x: 2}))
with self.assertRaises(TypeError):
Expression({'x': '2'})
self.assertEqual(Expression(constant=1), Expression(constant=self.one))
- with self.assertRaises(TypeError):
- Expression(constant='1')
-
- def test_symbols(self):
- self.assertCountEqual(self.x.symbols, ['x'])
- self.assertCountEqual(self.pi.symbols, [])
- self.assertCountEqual(self.expr.symbols, ['x', 'y'])
-
- def test_dimension(self):
- self.assertEqual(self.x.dimension, 1)
- self.assertEqual(self.pi.dimension, 0)
- self.assertEqual(self.expr.dimension, 2)
+ self.assertEqual(Expression(constant='1'), Expression(constant=self.one))
+ with self.assertRaises(ValueError):
+ Expression(constant='a')
def test_coefficient(self):
self.assertEqual(self.expr.coefficient('x'), 1)
self.assertEqual(self.pi.constant, Fraction(22, 7))
self.assertEqual(self.expr.constant, 3)
+ def test_symbols(self):
+ self.assertCountEqual(self.x.symbols, ['x'])
+ self.assertCountEqual(self.pi.symbols, [])
+ self.assertCountEqual(self.expr.symbols, ['x', 'y'])
+
+ def test_dimension(self):
+ self.assertEqual(self.x.dimension, 1)
+ self.assertEqual(self.pi.dimension, 0)
+ self.assertEqual(self.expr.dimension, 2)
+
def test_isconstant(self):
self.assertFalse(self.x.isconstant())
self.assertTrue(self.pi.isconstant())
self.assertFalse(self.expr.isconstant())
- def test_values(self):
- self.assertCountEqual(self.expr.values(), [1, -2, 3])
-
def test_issymbol(self):
self.assertTrue(self.x.issymbol())
self.assertFalse(self.pi.issymbol())
self.assertFalse(self.expr.issymbol())
+ def test_values(self):
+ self.assertCountEqual(self.expr.values(), [1, -2, 3])
+
def test_bool(self):
self.assertTrue(self.x)
self.assertFalse(self.zero)
self.assertEqual(0 * self.expr, 0)
self.assertEqual(self.expr * 2, 2*self.x - 4*self.y + 6)
- def test_div(self):
+ def test_truediv(self):
with self.assertRaises(ZeroDivisionError):
self.expr / 0
self.assertEqual(self.expr / 2, self.x / 2 - self.y + Fraction(3, 2))
+ def test_eq(self):
+ self.assertEqual(self.expr, self.expr)
+ self.assertNotEqual(self.x, self.y)
+ self.assertEqual(self.zero, 0)
+
+ def test__toint(self):
+ self.assertEqual((self.x + self.y/2 + self.z/3)._toint(),
+ 6*self.x + 3*self.y + 2*self.z)
+
+ def test_fromstring(self):
+ self.assertEqual(Expression.fromstring('x'), self.x)
+ self.assertEqual(Expression.fromstring('-x'), -self.x)
+ self.assertEqual(Expression.fromstring('22/7'), self.pi)
+ self.assertEqual(Expression.fromstring('x - 2y + 3'), self.expr)
+ self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.expr)
+ self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.expr)
+
def test_str(self):
self.assertEqual(str(Expression()), '0')
self.assertEqual(str(self.x), 'x')
self.assertEqual(repr(self.x + self.one), "Expression('x + 1')")
self.assertEqual(repr(self.expr), "Expression('x - 2*y + 3')")
- def test_fromstring(self):
- self.assertEqual(Expression.fromstring('x'), self.x)
- self.assertEqual(Expression.fromstring('-x'), -self.x)
- self.assertEqual(Expression.fromstring('22/7'), self.pi)
- self.assertEqual(Expression.fromstring('x - 2y + 3'), self.expr)
- self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.expr)
- self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.expr)
-
- def test_eq(self):
- self.assertEqual(self.expr, self.expr)
- self.assertNotEqual(self.x, self.y)
- self.assertEqual(self.zero, 0)
-
- def test__toint(self):
- self.assertEqual((self.x + self.y/2 + self.z/3)._toint(),
- 6*self.x + 3*self.y + 2*self.z)
-
@_requires_sympy
def test_fromsympy(self):
sp_x, sp_y = sympy.symbols('x y')
self.assertEqual(self.expr.tosympy(), sp_x - 2*sp_y + 3)
-class TestConstant(unittest.TestCase):
-
- def setUp(self):
- self.zero = Constant(0)
- self.one = Constant(1)
- self.pi = Constant(Fraction(22, 7))
-
- @_requires_sympy
- def test_fromsympy(self):
- self.assertEqual(Constant.fromsympy(sympy.Rational(22, 7)), self.pi)
- with self.assertRaises(TypeError):
- Constant.fromsympy(sympy.Symbol('x'))
-
-
class TestSymbol(unittest.TestCase):
def setUp(self):
self.x = Symbol('x')
self.y = Symbol('y')
+ def test_new(self):
+ self.assertEqual(Symbol(' x '), self.x)
+ self.assertEqual(Symbol(self.x), self.x)
+ with self.assertRaises(TypeError):
+ Symbol(1)
+
def test_name(self):
self.assertEqual(self.x.name, 'x')
- def test_symbols(self):
- self.assertListEqual(list(symbols('x y')), [self.x, self.y])
- self.assertListEqual(list(symbols('x,y')), [self.x, self.y])
- self.assertListEqual(list(symbols(['x', 'y'])), [self.x, self.y])
+ def test_issymbol(self):
+ self.assertTrue(self.x.issymbol())
+
+ def test_fromstring(self):
+ self.assertEqual(Symbol.fromstring('x'), self.x)
+ with self.assertRaises(SyntaxError):
+ Symbol.fromstring('1')
+
+ def test_str(self):
+ self.assertEqual(str(self.x), 'x')
+
+ def test_repr(self):
+ self.assertEqual(repr(self.x), "Symbol('x')")
@_requires_sympy
def test_fromsympy(self):
with self.assertRaises(TypeError):
Symbol.fromsympy(sp_x*sp_x)
-
-class TestOperators(unittest.TestCase):
-
- pass
+ def test_symbols(self):
+ self.assertListEqual(list(symbols('x y')), [self.x, self.y])
+ self.assertListEqual(list(symbols('x,y')), [self.x, self.y])
+ self.assertListEqual(list(symbols(['x', 'y'])), [self.x, self.y])
-class TestPolyhedron(unittest.TestCase):
+class TestConstant(unittest.TestCase):
def setUp(self):
- x, y = symbols('x y')
- self.square = Polyhedron(inequalities=[x, 1 - x, y, 1 - y])
-
- def test_symbols(self):
- self.assertCountEqual(self.square.symbols, ['x', 'y'])
+ self.zero = Constant(0)
+ self.one = Constant(1)
+ self.pi = Constant(Fraction(22, 7))
- def test_dimension(self):
- self.assertEqual(self.square.dimension, 2)
+ def test_new(self):
+ self.assertEqual(Constant(), self.zero)
+ self.assertEqual(Constant(1), self.one)
+ self.assertEqual(Constant(self.pi), self.pi)
+ self.assertEqual(Constant('22/7'), self.pi)
- def test_str(self):
- self.assertEqual(str(self.square),
- 'x >= 0, -x + 1 >= 0, y >= 0, -y + 1 >= 0')
+ def test_isconstant(self):
+ self.assertTrue(self.zero.isconstant())
- def test_repr(self):
- self.assertEqual(repr(self.square),
- "Polyhedron('x >= 0, -x + 1 >= 0, y >= 0, -y + 1 >= 0')")
+ def test_bool(self):
+ self.assertFalse(self.zero)
+ self.assertTrue(self.pi)
def test_fromstring(self):
- self.assertEqual(Polyhedron.fromstring('{x >= 0, -x + 1 >= 0, '
- 'y >= 0, -y + 1 >= 0}'), self.square)
-
- def test_isempty(self):
- self.assertFalse(self.square.isempty())
+ self.assertEqual(Constant.fromstring('22/7'), self.pi)
+ with self.assertRaises(ValueError):
+ Constant.fromstring('a')
+ with self.assertRaises(TypeError):
+ Constant.fromstring(1)
- def test_isuniverse(self):
- self.assertFalse(self.square.isuniverse())
+ def test_repr(self):
+ self.assertEqual(repr(self.zero), 'Constant(0)')
+ self.assertEqual(repr(self.one), 'Constant(1)')
+ self.assertEqual(repr(self.pi), 'Constant(22, 7)')
- @unittest.expectedFailure
@_requires_sympy
def test_fromsympy(self):
- sp_x, sp_y = sympy.symbols('x y')
- self.assertEqual(Polyhedron.fromsympy((sp_x >= 0) & (sp_x <= 1) &
- (sp_y >= 0) & (sp_y <= 1)), self.square)
-
- @_requires_sympy
- def test_tosympy(self):
- sp_x, sp_y = sympy.symbols('x y')
- self.assertEqual(self.square.tosympy(),
- sympy.And(-sp_x + 1 >= 0, -sp_y + 1 >= 0, sp_x >= 0, sp_y >= 0))
-
-
-class TestEmpty:
-
- def test_repr(self):
- self.assertEqual(repr(Empty), 'Empty')
-
- def test_isempty(self):
- self.assertTrue(Empty.isempty())
-
- def test_isuniverse(self):
- self.assertFalse(Empty.isuniverse())
-
-
-class TestUniverse:
-
- def test_repr(self):
- self.assertEqual(repr(Universe), 'Universe')
-
- def test_isempty(self):
- self.assertTrue(Universe.isempty())
-
- def test_isuniverse(self):
- self.assertTrue(Universe.isuniverse())
+ self.assertEqual(Constant.fromsympy(sympy.Rational(22, 7)), self.pi)
+ with self.assertRaises(TypeError):
+ Constant.fromsympy(sympy.Symbol('x'))