Working test suite
authorVivien Maisonneuve <v.maisonneuve@gmail.com>
Thu, 19 Jun 2014 08:18:15 +0000 (10:18 +0200)
committerVivien Maisonneuve <v.maisonneuve@gmail.com>
Thu, 19 Jun 2014 08:18:15 +0000 (10:18 +0200)
tests/test_isl.py
tests/test_linear.py

index 7186dad..421b1be 100644 (file)
@@ -12,188 +12,3 @@ class TestContext(unittest.TestCase):
         ctx1, ctx2 = Context(), Context()
         self.assertEqual(ctx1, ctx1)
         self.assertNotEqual(ctx1, ctx2)
-
-
-class TestValue(unittest.TestCase):
-
-    def setUp(self):
-        self.context = Context()
-        self.zero = Value(self.context)
-        self.nan = Value(self.context, 'NaN')
-        self.inf = Value(self.context, 'Inf')
-        self.neginf = Value(self.context, '-Inf')
-        self.answer = Value(self.context, 42)
-        self.pi = Value(self.context, 22, 7)
-
-    def test_init(self):
-        self.assertEqual(Value(self.context, 42), self.answer)
-        self.assertEqual(Value(self.context, '42'), self.answer)
-        self.assertEqual(Value(self.context, 22, 7), self.pi)
-        self.assertEqual(Value(self.context, '-22/7'), -self.pi)
-        self.assertTrue(Value(self.context, 'nan').is_nan())
-        self.assertTrue(Value(self.context, '-nan').is_nan())
-        self.assertTrue(Value(self.context, 'NaN').is_nan())
-        self.assertEqual(Value(self.context, '-inf'), self.neginf)
-        self.assertEqual(Value(self.context, '-Infinity'), self.neginf)
-
-    def test_numerator(self):
-        self.assertEqual(self.zero.numerator, 0)
-        self.assertEqual(self.answer.numerator, 42)
-        self.assertEqual(self.pi.numerator, 22)
-        with self.assertRaises(ValueError):
-            self.nan.numerator
-        with self.assertRaises(ValueError):
-            self.inf.numerator
-
-    def test_denominator(self):
-        self.assertEqual(self.zero.denominator, 1)
-        self.assertEqual(self.answer.denominator, 1)
-        self.assertEqual(self.pi.denominator, 7)
-        with self.assertRaises(ValueError):
-            self.nan.denominator
-        with self.assertRaises(ValueError):
-            self.inf.denominator
-
-    def test_bool(self):
-        self.assertFalse(self.zero)
-        self.assertTrue(self.answer)
-        self.assertTrue(self.pi)
-        self.assertEqual(bool(self.nan), bool(float('nan')))
-        self.assertEqual(bool(self.inf), bool(float('inf')))
-
-    def test_lt(self):
-        self.assertTrue(self.neginf < self.zero)
-        self.assertTrue(self.zero < self.pi)
-        self.assertTrue(self.pi < self.answer)
-        self.assertTrue(self.answer < self.inf)
-        self.assertFalse(self.nan < self.answer)
-        self.assertFalse(self.nan < self.inf)
-        self.assertFalse(self.nan < self.neginf)
-        self.assertTrue(self.neginf < self.inf)
-
-    def test_le(self):
-        self.assertTrue(self.pi <= self.pi)
-        self.assertTrue(self.pi <= self.answer)
-        self.assertFalse(self.answer <= self.pi)
-
-    def test_gt(self):
-        self.assertFalse(self.pi > self.pi)
-        self.assertTrue(self.answer > self.pi)
-        self.assertFalse(self.pi > self.answer)
-
-    def test_ge(self):
-        self.assertTrue(self.pi >= self.pi)
-        self.assertTrue(self.answer >= self.pi)
-        self.assertFalse(self.pi >= self.answer)
-
-    def test_eq(self):
-        self.assertEqual(self.pi, self.pi)
-        self.assertEqual(self.inf, self.inf)
-        self.assertNotEqual(self.neginf, self.inf)
-        self.assertNotEqual(self.nan, self.nan)
-        self.assertEqual(self.zero, 0)
-        self.assertEqual(0, self.zero)
-        self.assertEqual(self.pi, Fraction(22, 7))
-        self.assertEqual(Fraction(22, 7), self.pi)
-        with self.assertRaises(TypeError):
-            self.zero == 0.
-
-    def test_ne(self):
-        self.assertTrue(self.pi != self.answer)
-        self.assertFalse(self.pi != self.pi)
-        self.assertTrue(self.neginf != self.inf)
-        self.assertTrue(self.nan != self.nan)
-
-    def test_abs(self):
-        self.assertEqual(abs(self.pi), self.pi)
-        self.assertEqual(abs(self.neginf), self.inf)
-        self.assertEqual(abs(-self.pi), self.pi)
-        self.assertTrue(abs(self.nan).is_nan())
-
-    def test_pos(self):
-        self.assertEqual(+self.pi, self.pi)
-
-    def test_neg(self):
-        self.assertEqual(-self.neginf, self.inf)
-        self.assertEqual(-(-self.pi), self.pi)
-
-    def test_floor(self):
-        self.assertEqual(floor(self.pi), Value(self.context, 3))
-        self.assertEqual(floor(-self.pi), Value(self.context, -4))
-        # not float behavior, but makes sense
-        self.assertEqual(floor(self.inf), self.inf)
-        self.assertTrue(floor(self.nan).is_nan())
-
-    def test_ceil(self):
-        self.assertEqual(ceil(self.pi), Value(self.context, 4))
-        self.assertRaises(ceil(-self.pi) == Value(self.context, -3))
-
-    def test_trunc(self):
-        self.assertEqual(trunc(self.pi), Value(self.context, 3))
-        self.assertEqual(trunc(-self.pi), Value(self.context, -3))
-
-    def test_add(self):
-        self.assertEqual(self.answer + self.answer, Value(self.context, 84))
-        self.assertEqual(self.answer + self.pi, Value(self.context, 316, 7))
-        self.assertEqual(self.pi + self.pi, Value(self.context, 44, 7))
-        self.assertEqual(self.pi + self.neginf, self.neginf)
-        self.assertEqual(self.pi + self.inf, self.inf)
-        self.assertTrue((self.pi + self.nan).is_nan())
-        self.assertTrue((self.inf + self.nan).is_nan())
-        self.assertTrue((self.inf + self.neginf).is_nan())
-        self.assertEqual(self.pi + 42, Value(self.context, 316, 7))
-        self.assertEqual(42 + self.pi, Value(self.context, 316, 7))
-        self.assertEqual(self.pi + Fraction(22, 7), Value(self.context, 44, 7))
-        with self.assertRaises(TypeError):
-            self.pi + float(42)
-
-    def test_sub(self):
-        self.assertEqual(self.answer - self.pi, Value(self.context, 272, 7))
-
-    def test_mul(self):
-        self.assertEqual(Value(self.context, 6) * Value(self.context, 7), self.answer)
-        self.assertNotEqual(Value(self.context, 6) * Value(self.context, 9), self.answer)
-        self.assertEqual(self.inf * Value(self.context, 2), self.inf)
-        self.assertEqual(self.inf * Value(self.context, -2), self.neginf)
-        self.assertTrue((self.nan * Value(self.context, 2)).is_nan())
-        self.assertTrue((self.nan * self.inf).is_nan())
-
-    def test_div(self):
-        self.assertEqual(Value(self.context, 22) / Value(self.context, 7), self.pi)
-        self.assertEqual(self.pi / self.pi, Value(self.context, 1))
-        # not float behavior, but makes sense
-        self.assertTrue((self.pi / self.zero).is_nan())
-
-    def test_float(self):
-        self.assertAlmostEqual(float(Value(self.context, 1, 2)), 0.5)
-        self.assertTrue(math.isnan(float(Value(self.context, 'NaN'))))
-        self.assertAlmostEqual(float(Value(self.context, 'Inf')), float('inf'))
-
-    def test_is_finite(self):
-        self.assertTrue(self.pi.is_finite())
-        self.assertFalse(self.inf.is_finite())
-        self.assertFalse(self.nan.is_finite())
-
-    def test_is_infinite(self):
-        self.assertFalse(self.pi.is_infinite())
-        self.assertTrue(self.inf.is_infinite())
-        self.assertFalse(self.nan.is_infinite())
-
-    def test_is_nan(self):
-        self.assertFalse(self.pi.is_nan())
-        self.assertFalse(self.inf.is_nan())
-        self.assertTrue(self.nan.is_nan())
-
-    def test_str(self):
-        self.assertEqual(str(self.answer), '42')
-        self.assertEqual(str(self.pi), '22/7')
-        self.assertEqual(str(self.nan), 'NaN')
-        self.assertEqual(str(self.inf), 'Infinity')
-        self.assertEqual(str(self.neginf), '-Infinity')
-
-    def test_repr(self):
-        self.assertEqual(repr(self.answer), "Value('42')")
-        self.assertEqual(repr(self.pi), "Value('22/7')")
-        self.assertEqual(repr(self.nan), "Value('NaN')")
-        self.assertEqual(repr(self.inf), "Value('Infinity')")
-        self.assertEqual(repr(self.neginf), "Value('-Infinity')")
index 998a022..039ffc7 100644 (file)
@@ -9,158 +9,167 @@ from pypol.linear import *
 class TestExpression(unittest.TestCase):
 
     def setUp(self):
-        self.x = symbol('x')
-        self.y = symbol('y')
-        self.z = symbol('z')
-        self.zero = constant(5)
-        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):
-        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)
-        self.assertEqual(self.e.dimension, 2)
+        self.assertEqual(self.expr.dimension, 2)
 
     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'), 3)
+        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):
-            self.e.coefficient(0)
+            self.expr.coefficient(0)
         with self.assertRaises(TypeError):
-            self.e.coefficient(self.e)
+            self.expr.coefficient(self.expr)
 
     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'], 3)
+        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):
-            self.e[0]
+            self.expr[0]
         with self.assertRaises(TypeError):
-            self.e[self.e]
+            self.expr[self.expr]
 
     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, 4)
+        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())
-        self.assertFalse(self.e.isconstant())
+        self.assertFalse(self.expr.isconstant())
 
     def test_values(self):
-        self.assertCountEqual(self.e.values(), [1, -2, 3])
+        self.assertCountEqual(self.expr.values(), [1, -2, 3])
 
     def test_symbol(self):
-        self.assertEqual(self.x.symbol(), 'x')
+        self.assertEqual(self.x.symbol, 'x')
         with self.assertRaises(ValueError):
-            self.pi.symbol()
+            self.pi.symbol
         with self.assertRaises(ValueError):
-            self.e.symbol()
+            self.expr.symbol
 
     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)
-        self.assertTrue(self.e)
+        self.assertTrue(self.expr)
 
     def test_pos(self):
-        self.assertEqual(+self.e, self.e)
+        self.assertEqual(+self.expr, self.expr)
 
     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)
-        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)
-        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)
-        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):
-            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):
-        self.assertEqual(str(Expression()), '5')
+        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.e), 'x - 2*y + 3')
+        self.assertEqual(str(self.expr), 'x - 2*y + 3')
 
     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)
-        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):
-        self.assertEqual(self.e, self.e)
+        self.assertEqual(self.expr, self.expr)
         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)
 
 
-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(4)
+class TestSymbol(unittest.TestCase):
+
+    def setUp(self):
+        self.x = Symbol('x')
+        self.y = Symbol('y')
 
     def test_symbols(self):
         self.assertListEqual(list(symbols('x y')), [self.x, self.y])
@@ -176,5 +185,3 @@ class TestOperators(unittest.TestCase):
 class TestPolyhedron(unittest.TestCase):
 
     pass
-
-