Improve comparison methods in LinExpr
[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_str(self):
39 self.assertEqual(str(self.square),
40 'And(Ge(x, 0), Ge(-x + 1, 0), Ge(y, 0), Ge(-y + 1, 0))')
41
42 def test_repr(self):
43 self.assertEqual(repr(self.square),
44 "And(Ge(x, 0), Ge(-x + 1, 0), Ge(y, 0), Ge(-y + 1, 0))")
45
46 def test_fromstring(self):
47 self.assertEqual(Polyhedron.fromstring('{x >= 0, -x + 1 >= 0, '
48 'y >= 0, -y + 1 >= 0}'), self.square)
49
50 def test_isempty(self):
51 self.assertFalse(self.square.isempty())
52
53 def test_isuniverse(self):
54 self.assertFalse(self.square.isuniverse())
55
56 @requires_sympy
57 def test_fromsympy(self):
58 import sympy
59 sp_x, sp_y = sympy.symbols('x y')
60 self.assertEqual(Polyhedron.fromsympy((sp_x >= 0) & (sp_x <= 1) &
61 (sp_y >= 0) & (sp_y <= 1)), self.square)
62
63 @requires_sympy
64 def test_tosympy(self):
65 import sympy
66 sp_x, sp_y = sympy.symbols('x y')
67 self.assertEqual(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())