c74e25f794d06f54c75aa3ba8b4b2b89d2fd9220
1 import functools
2 import unittest
4 from ..linexprs import symbols
5 from ..polyhedra import *
8 try:
9 import sympy
10 def _requires_sympy(func):
11 @functools.wraps(func)
12 def wrapper(self):
13 return func(self)
14 return wrapper
15 except ImportError:
16 def _requires_sympy(func):
17 @functools.wraps(func)
18 def wrapper(self):
19 raise unittest.SkipTest('SymPy is not available')
20 return wrapper
23 class TestPolyhedron(unittest.TestCase):
25 def setUp(self):
26 x, y = symbols('x y')
27 self.square = Polyhedron(inequalities=[x, 1 - x, y, 1 - y])
29 def test_symbols(self):
30 self.assertCountEqual(self.square.symbols, ['x', 'y'])
32 def test_dimension(self):
33 self.assertEqual(self.square.dimension, 2)
35 def test_str(self):
36 self.assertEqual(str(self.square),
37 'And(Ge(x, 0), Ge(-x + 1, 0), Ge(y, 0), Ge(-y + 1, 0))')
39 def test_repr(self):
40 self.assertEqual(repr(self.square),
41 "And(Ge(x, 0), Ge(-x + 1, 0), Ge(y, 0), Ge(-y + 1, 0))")
43 def test_fromstring(self):
44 self.assertEqual(Polyhedron.fromstring('{x >= 0, -x + 1 >= 0, '
45 'y >= 0, -y + 1 >= 0}'), self.square)
47 def test_isempty(self):
48 self.assertFalse(self.square.isempty())
50 def test_isuniverse(self):
51 self.assertFalse(self.square.isuniverse())
53 @_requires_sympy
54 def test_fromsympy(self):
55 sp_x, sp_y = sympy.symbols('x y')
56 self.assertEqual(Polyhedron.fromsympy((sp_x >= 0) & (sp_x <= 1) &
57 (sp_y >= 0) & (sp_y <= 1)), self.square)
59 @_requires_sympy
60 def test_tosympy(self):
61 sp_x, sp_y = sympy.symbols('x y')
62 self.assertEqual(self.square.tosympy(),
63 sympy.And(-sp_x + 1 >= 0, -sp_y + 1 >= 0, sp_x >= 0, sp_y >= 0))
66 class TestEmpty:
68 def test_repr(self):
69 self.assertEqual(repr(Empty), 'Empty')
71 def test_isempty(self):
72 self.assertTrue(Empty.isempty())
74 def test_isuniverse(self):
75 self.assertFalse(Empty.isuniverse())
78 class TestUniverse:
80 def test_repr(self):
81 self.assertEqual(repr(Universe), 'Universe')
83 def test_isempty(self):
84 self.assertTrue(Universe.isempty())
86 def test_isuniverse(self):
87 self.assertTrue(Universe.isuniverse())