Version 1.0.3
[linpy.git] / linpy / tests / test_polyhedra.py
1 # Copyright 2014 MINES ParisTech
2 #
3 # This file is part of LinPy.
4 #
5 # LinPy is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # LinPy is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with LinPy. If not, see <http://www.gnu.org/licenses/>.
17
18 import unittest
19
20 from ..linexprs import symbols
21 from ..polyhedra import Empty, Polyhedron, Universe
22 from .libhelper import requires_sympy
23
24
25 class TestPolyhedron(unittest.TestCase):
26
27 def setUp(self):
28 x, y = symbols('x y')
29 self.square = Polyhedron(inequalities=[x, 1 - x, y, 1 - y])
30
31 def test_symbols(self):
32 self.assertTupleEqual(self.square.symbols, symbols('x y'))
33
34 def test_dimension(self):
35 self.assertEqual(self.square.dimension, 2)
36
37 def test_repr(self):
38 self.assertEqual(repr(self.square),
39 'And(0 <= x, x <= 1, 0 <= y, y <= 1)')
40
41 def test_fromstring(self):
42 self.assertEqual(
43 Polyhedron.fromstring(
44 '{x >= 0, -x + 1 >= 0, y >= 0, -y + 1 >= 0}'),
45 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 import sympy
56 sp_x, sp_y = sympy.symbols('x y')
57 self.assertEqual(
58 Polyhedron.fromsympy(
59 (sp_x >= 0) & (sp_x <= 1) & (sp_y >= 0) & (sp_y <= 1)),
60 self.square)
61
62 @requires_sympy
63 def test_tosympy(self):
64 import sympy
65 sp_x, sp_y = sympy.symbols('x y')
66 self.assertEqual(
67 self.square.tosympy(),
68 sympy.And(-sp_x + 1 >= 0, -sp_y + 1 >= 0, sp_x >= 0, sp_y >= 0))
69
70
71 class TestEmpty:
72
73 def test_repr(self):
74 self.assertEqual(repr(Empty), 'Empty')
75
76 def test_isempty(self):
77 self.assertTrue(Empty.isempty())
78
79 def test_isuniverse(self):
80 self.assertFalse(Empty.isuniverse())
81
82
83 class TestUniverse:
84
85 def test_repr(self):
86 self.assertEqual(repr(Universe), 'Universe')
87
88 def test_isempty(self):
89 self.assertTrue(Universe.isempty())
90
91 def test_isuniverse(self):
92 self.assertTrue(Universe.isuniverse())