1 import functools
2 import unittest
4 from ..linexprs import symbols
5 from ..polyhedra import *
6 from .libhelper import requires_sympy
9 class TestPolyhedron(unittest.TestCase):
11 def setUp(self):
12 x, y = symbols('x y')
13 self.square = Polyhedron(inequalities=[x, 1 - x, y, 1 - y])
15 def test_symbols(self):
16 self.assertTupleEqual(self.square.symbols, symbols('x y'))
18 def test_dimension(self):
19 self.assertEqual(self.square.dimension, 2)
21 def test_str(self):
22 self.assertEqual(str(self.square),
23 'And(0 <= x, 0 <= -x + 1, 0 <= y, 0 <= -y + 1)')
25 def test_repr(self):
26 self.assertEqual(repr(self.square),
27 "And(0 <= x, 0 <= -x + 1, 0 <= y, 0 <= -y + 1)")
29 def test_fromstring(self):
30 self.assertEqual(Polyhedron.fromstring('{x >= 0, -x + 1 >= 0, '
31 'y >= 0, -y + 1 >= 0}'), self.square)
33 def test_isempty(self):
34 self.assertFalse(self.square.isempty())
36 def test_isuniverse(self):
37 self.assertFalse(self.square.isuniverse())
39 @requires_sympy
40 def test_fromsympy(self):
41 import sympy
42 sp_x, sp_y = sympy.symbols('x y')
43 self.assertEqual(Polyhedron.fromsympy((sp_x >= 0) & (sp_x <= 1) &
44 (sp_y >= 0) & (sp_y <= 1)), self.square)
46 @requires_sympy
47 def test_tosympy(self):
48 import sympy
49 sp_x, sp_y = sympy.symbols('x y')
50 self.assertEqual(self.square.tosympy(),
51 sympy.And(-sp_x + 1 >= 0, -sp_y + 1 >= 0, sp_x >= 0, sp_y >= 0))
54 class TestEmpty:
56 def test_repr(self):
57 self.assertEqual(repr(Empty), 'Empty')
59 def test_isempty(self):
60 self.assertTrue(Empty.isempty())
62 def test_isuniverse(self):
63 self.assertFalse(Empty.isuniverse())
66 class TestUniverse:
68 def test_repr(self):
69 self.assertEqual(repr(Universe), 'Universe')
71 def test_isempty(self):
72 self.assertTrue(Universe.isempty())
74 def test_isuniverse(self):
75 self.assertTrue(Universe.isuniverse())