d0ebe57e09b0281ed5b02d0a0c4d954915f4ff35
[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 functools
19 import unittest
20
21 from ..linexprs import symbols
22 from ..polyhedra import *
23 from .libhelper import requires_sympy
24
25
26 class TestPolyhedron(unittest.TestCase):
27
28 def setUp(self):
29 x, y = symbols('x y')
30 self.square = Polyhedron(inequalities=[x, 1 - x, y, 1 - y])
31
32 def test_symbols(self):
33 self.assertTupleEqual(self.square.symbols, symbols('x y'))
34
35 def test_dimension(self):
36 self.assertEqual(self.square.dimension, 2)
37
38 def test_repr(self):
39 self.assertEqual(repr(self.square),
40 "And(0 <= x, x <= 1, 0 <= y, y <= 1)")
41
42 def test_fromstring(self):
43 self.assertEqual(Polyhedron.fromstring('{x >= 0, -x + 1 >= 0, '
44 'y >= 0, -y + 1 >= 0}'), self.square)
45
46 def test_isempty(self):
47 self.assertFalse(self.square.isempty())
48
49 def test_isuniverse(self):
50 self.assertFalse(self.square.isuniverse())
51
52 @requires_sympy
53 def test_fromsympy(self):
54 import sympy
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 import sympy
62 sp_x, sp_y = sympy.symbols('x y')
63 self.assertEqual(self.square.tosympy(),
64 sympy.And(-sp_x + 1 >= 0, -sp_y + 1 >= 0, sp_x >= 0, sp_y >= 0))
65
66
67 class TestEmpty:
68
69 def test_repr(self):
70 self.assertEqual(repr(Empty), 'Empty')
71
72 def test_isempty(self):
73 self.assertTrue(Empty.isempty())
74
75 def test_isuniverse(self):
76 self.assertFalse(Empty.isuniverse())
77
78
79 class TestUniverse:
80
81 def test_repr(self):
82 self.assertEqual(repr(Universe), 'Universe')
83
84 def test_isempty(self):
85 self.assertTrue(Universe.isempty())
86
87 def test_isuniverse(self):
88 self.assertTrue(Universe.isuniverse())