Add __slots__ declarations
[linpy.git] / tests / test_linear.py
index 3912fd3..b722726 100644 (file)
@@ -1,4 +1,3 @@
-
 import unittest
 
 from fractions import Fraction
 import unittest
 
 from fractions import Fraction
@@ -9,155 +8,162 @@ from pypol.linear import *
 class TestExpression(unittest.TestCase):
 
     def setUp(self):
 class TestExpression(unittest.TestCase):
 
     def setUp(self):
-        self.x = symbol('x')
-        self.y = symbol('y')
-        self.z = symbol('z')
-        self.zero = constant(0)
-        self.pi = constant(Fraction(22, 7))
-        self.e = self.x - 2*self.y + 3
-
-    def test_new(self):
-        pass
+        self.x = Expression({'x': 1})
+        self.y = Expression({'y': 1})
+        self.z = Expression({'z': 1})
+        self.zero = Expression(constant=0)
+        self.one = Expression(constant=1)
+        self.pi = Expression(constant=Fraction(22, 7))
+        self.expr = self.x - 2*self.y + 3
+
+    def test_new_subclass(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({0: 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):
 
     def test_symbols(self):
-        self.assertCountEqual(self.x.symbols(), ['x'])
-        self.assertCountEqual(self.pi.symbols(), [])
-        self.assertCountEqual(self.e.symbols(), ['x', 'y'])
+        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)
 
     def test_dimension(self):
         self.assertEqual(self.x.dimension, 1)
         self.assertEqual(self.pi.dimension, 0)
-        self.assertEqual(self.e.dimension, 2)
+        self.assertEqual(self.expr.dimension, 2)
 
     def test_coefficient(self):
 
     def test_coefficient(self):
-        self.assertEqual(self.e.coefficient('x'), 1)
-        self.assertEqual(self.e.coefficient('y'), -2)
-        self.assertEqual(self.e.coefficient(self.y), -2)
-        self.assertEqual(self.e.coefficient('z'), 0)
+        self.assertEqual(self.expr.coefficient('x'), 1)
+        self.assertEqual(self.expr.coefficient('y'), -2)
+        self.assertEqual(self.expr.coefficient(self.y), -2)
+        self.assertEqual(self.expr.coefficient('z'), 0)
         with self.assertRaises(TypeError):
         with self.assertRaises(TypeError):
-            self.e.coefficient(0)
+            self.expr.coefficient(0)
         with self.assertRaises(TypeError):
         with self.assertRaises(TypeError):
-            self.e.coefficient(self.e)
+            self.expr.coefficient(self.expr)
 
     def test_getitem(self):
 
     def test_getitem(self):
-        self.assertEqual(self.e['x'], 1)
-        self.assertEqual(self.e['y'], -2)
-        self.assertEqual(self.e[self.y], -2)
-        self.assertEqual(self.e['z'], 0)
+        self.assertEqual(self.expr['x'], 1)
+        self.assertEqual(self.expr['y'], -2)
+        self.assertEqual(self.expr[self.y], -2)
+        self.assertEqual(self.expr['z'], 0)
         with self.assertRaises(TypeError):
         with self.assertRaises(TypeError):
-            self.e[0]
+            self.expr[0]
         with self.assertRaises(TypeError):
         with self.assertRaises(TypeError):
-            self.e[self.e]
+            self.expr[self.expr]
 
     def test_coefficients(self):
 
     def test_coefficients(self):
-        self.assertCountEqual(self.e.coefficients(), [('x', 1), ('y', -2)])
+        self.assertCountEqual(self.expr.coefficients(), [('x', 1), ('y', -2)])
 
     def test_constant(self):
         self.assertEqual(self.x.constant, 0)
         self.assertEqual(self.pi.constant, Fraction(22, 7))
 
     def test_constant(self):
         self.assertEqual(self.x.constant, 0)
         self.assertEqual(self.pi.constant, Fraction(22, 7))
-        self.assertEqual(self.e.constant, 3)
+        self.assertEqual(self.expr.constant, 3)
 
     def test_isconstant(self):
         self.assertFalse(self.x.isconstant())
         self.assertTrue(self.pi.isconstant())
 
     def test_isconstant(self):
         self.assertFalse(self.x.isconstant())
         self.assertTrue(self.pi.isconstant())
-        self.assertFalse(self.e.isconstant())
+        self.assertFalse(self.expr.isconstant())
 
     def test_values(self):
 
     def test_values(self):
-        self.assertCountEqual(self.e.values(), [1, -2, 3])
-
-    def test_symbol(self):
-        self.assertEqual(self.x.symbol(), 'x')
-        with self.assertRaises(ValueError):
-            self.pi.symbol()
-        with self.assertRaises(ValueError):
-            self.e.symbol()
+        self.assertCountEqual(self.expr.values(), [1, -2, 3])
 
     def test_issymbol(self):
         self.assertTrue(self.x.issymbol())
         self.assertFalse(self.pi.issymbol())
 
     def test_issymbol(self):
         self.assertTrue(self.x.issymbol())
         self.assertFalse(self.pi.issymbol())
-        self.assertFalse(self.e.issymbol())
+        self.assertFalse(self.expr.issymbol())
 
     def test_bool(self):
         self.assertTrue(self.x)
         self.assertFalse(self.zero)
         self.assertTrue(self.pi)
 
     def test_bool(self):
         self.assertTrue(self.x)
         self.assertFalse(self.zero)
         self.assertTrue(self.pi)
-        self.assertTrue(self.e)
+        self.assertTrue(self.expr)
 
     def test_pos(self):
 
     def test_pos(self):
-        self.assertEqual(+self.e, self.e)
+        self.assertEqual(+self.expr, self.expr)
 
     def test_neg(self):
 
     def test_neg(self):
-        self.assertEqual(-self.e, -self.x + 2*self.y - 3)
+        self.assertEqual(-self.expr, -self.x + 2*self.y - 3)
 
     def test_add(self):
         self.assertEqual(self.x + Fraction(22, 7), self.x + self.pi)
         self.assertEqual(Fraction(22, 7) + self.x, self.x + self.pi)
         self.assertEqual(self.x + self.x, 2 * self.x)
 
     def test_add(self):
         self.assertEqual(self.x + Fraction(22, 7), self.x + self.pi)
         self.assertEqual(Fraction(22, 7) + self.x, self.x + self.pi)
         self.assertEqual(self.x + self.x, 2 * self.x)
-        self.assertEqual(self.e + 2*self.y, self.x + 3)
+        self.assertEqual(self.expr + 2*self.y, self.x + 3)
 
     def test_sub(self):
         self.assertEqual(self.x - self.x, 0)
 
     def test_sub(self):
         self.assertEqual(self.x - self.x, 0)
-        self.assertEqual(self.e - 3, self.x - 2*self.y)
+        self.assertEqual(self.expr - 3, self.x - 2*self.y)
+        self.assertEqual(0 - self.x, -self.x)
 
     def test_mul(self):
         self.assertEqual(self.pi * 7, 22)
 
     def test_mul(self):
         self.assertEqual(self.pi * 7, 22)
-        self.assertEqual(self.e * 0, 0)
-        self.assertEqual(self.e * 2, 2*self.x - 4*self.y + 6)
+        self.assertEqual(self.expr * 0, 0)
+        self.assertEqual(0 * self.expr, 0)
+        self.assertEqual(self.expr * 2, 2*self.x - 4*self.y + 6)
 
     def test_div(self):
         with self.assertRaises(ZeroDivisionError):
 
     def test_div(self):
         with self.assertRaises(ZeroDivisionError):
-            self.e / 0
-        self.assertEqual(self.e / 2, self.x / 2 - self.y + Fraction(3, 2))
+            self.expr / 0
+        self.assertEqual(self.expr / 2, self.x / 2 - self.y + Fraction(3, 2))
 
     def test_str(self):
 
     def test_str(self):
+        self.assertEqual(str(Expression()), '0')
         self.assertEqual(str(self.x), 'x')
         self.assertEqual(str(-self.x), '-x')
         self.assertEqual(str(self.pi), '22/7')
         self.assertEqual(str(self.x), 'x')
         self.assertEqual(str(-self.x), '-x')
         self.assertEqual(str(self.pi), '22/7')
-        self.assertEqual(str(self.e), 'x - 2*y + 3')
+        self.assertEqual(str(self.expr), 'x - 2*y + 3')
 
     def test_repr(self):
 
     def test_repr(self):
-        self.assertEqual(repr(self.e), "Expression({'x': 1, 'y': -2}, 3)")
+        self.assertEqual(repr(self.x), "Symbol('x')")
+        self.assertEqual(repr(self.one), 'Constant(1)')
+        self.assertEqual(repr(self.expr), "Expression({'x': 1, 'y': -2}, 3)")
 
 
-    @unittest.expectedFailure
     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)
     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.e)
-        self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.e)
-        self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.e)
+        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):
 
     def test_eq(self):
-        self.assertEqual(self.e, self.e)
+        self.assertEqual(self.expr, self.expr)
         self.assertNotEqual(self.x, self.y)
         self.assertEqual(self.zero, 0)
 
         self.assertNotEqual(self.x, self.y)
         self.assertEqual(self.zero, 0)
 
-    def test_canonify(self):
-        self.assertEqual((self.x + self.y/2 + self.z/3)._canonify(),
+    def test__toint(self):
+        self.assertEqual((self.x + self.y/2 + self.z/3)._toint(),
                 6*self.x + 3*self.y + 2*self.z)
 
 
                 6*self.x + 3*self.y + 2*self.z)
 
 
-class TestHelpers(unittest.TestCase):
+class TestConstant(unittest.TestCase):
 
 
-    def setUp(self):
-        self.x = symbol('x')
-        self.y = symbol('y')
+    pass
 
 
-    def test_constant(self):
-        self.assertEqual(constant(3), 3)
-        self.assertEqual(constant('3'), 3)
-        self.assertEqual(constant(Fraction(3, 4)), Fraction(3, 4))
-        self.assertEqual(constant('3/4'), Fraction(3, 4))
-        with self.assertRaises(ValueError):
-            constant('a')
-        with self.assertRaises(TypeError):
-            constant([])
 
 
-    def test_symbol(self):
-        self.assertEqual(symbol('x'), self.x)
-        self.assertNotEqual(symbol('y'), self.x)
-        with self.assertRaises(TypeError):
-            symbol(0)
+class TestSymbol(unittest.TestCase):
+
+    def setUp(self):
+        self.x = Symbol('x')
+        self.y = Symbol('y')
+
+    def test_name(self):
+        self.assertEqual(self.x.name, 'x')
 
     def test_symbols(self):
         self.assertListEqual(list(symbols('x y')), [self.x, self.y])
 
     def test_symbols(self):
         self.assertListEqual(list(symbols('x y')), [self.x, self.y])
@@ -173,4 +179,3 @@ class TestOperators(unittest.TestCase):
 class TestPolyhedron(unittest.TestCase):
 
     pass
 class TestPolyhedron(unittest.TestCase):
 
     pass
-