README on how to run examples
[linpy.git] / pypol / tests / test_polyhedra.py
1 import functools
2 import unittest
3
4 from ..linexprs import symbols
5 from ..polyhedra import *
6
7
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
21
22
23 class TestPolyhedron(unittest.TestCase):
24
25 def setUp(self):
26 x, y = symbols('x y')
27 self.square = Polyhedron(inequalities=[x, 1 - x, y, 1 - y])
28
29 def test_symbols(self):
30 self.assertCountEqual(self.square.symbols, ['x', 'y'])
31
32 def test_dimension(self):
33 self.assertEqual(self.square.dimension, 2)
34
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))')
38
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))")
42
43 def test_fromstring(self):
44 self.assertEqual(Polyhedron.fromstring('{x >= 0, -x + 1 >= 0, '
45 'y >= 0, -y + 1 >= 0}'), self.square)
46
47 def test_isempty(self):
48 self.assertFalse(self.square.isempty())
49
50 def test_isuniverse(self):
51 self.assertFalse(self.square.isuniverse())
52
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)
58
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))
64
65
66 class TestEmpty:
67
68 def test_repr(self):
69 self.assertEqual(repr(Empty), 'Empty')
70
71 def test_isempty(self):
72 self.assertTrue(Empty.isempty())
73
74 def test_isuniverse(self):
75 self.assertFalse(Empty.isuniverse())
76
77
78 class TestUniverse:
79
80 def test_repr(self):
81 self.assertEqual(repr(Universe), 'Universe')
82
83 def test_isempty(self):
84 self.assertTrue(Universe.isempty())
85
86 def test_isuniverse(self):
87 self.assertTrue(Universe.isuniverse())