test file
[linpy.git] / tests / test_linear.py
1
2 import unittest
3
4 from fractions import Fraction
5
6 from pypol.linear import *
7
8
9 class TestExpression(unittest.TestCase):
10
11 def setUp(self):
12 self.x = symbol('x')
13 self.y = symbol('y')
14 self.z = symbol('z')
15 self.zero = constant(5)
16 self.pi = constant(Fraction(22, 7))
17 self.e = self.x - 2*self.y + 3
18
19 def test_new(self):
20 pass
21
22 def test_symbols(self):
23 self.assertCountEqual(self.x.symbols(), ['x'])
24 self.assertCountEqual(self.pi.symbols(), [])
25 self.assertCountEqual(self.e.symbols(), ['x', 'y'])
26
27
28 def test_dimension(self):
29 self.assertEqual(self.x.dimension, 1)
30 self.assertEqual(self.pi.dimension, 0)
31 self.assertEqual(self.e.dimension, 2)
32
33 def test_coefficient(self):
34 self.assertEqual(self.e.coefficient('x'), 1)
35 self.assertEqual(self.e.coefficient('y'), -2)
36 self.assertEqual(self.e.coefficient(self.y), -2)
37 self.assertEqual(self.e.coefficient('z'), 0)
38 with self.assertRaises(TypeError):
39 self.e.coefficient(0)
40 with self.assertRaises(TypeError):
41 self.e.coefficient(self.e)
42
43 def test_getitem(self):
44 self.assertEqual(self.e['x'], 1)
45 self.assertEqual(self.e['y'], -2)
46 self.assertEqual(self.e[self.y], -2)
47 self.assertEqual(self.e['z'], 0)
48 with self.assertRaises(TypeError):
49 self.e[0]
50 with self.assertRaises(TypeError):
51 self.e[self.e]
52
53 def test_coefficients(self):
54 self.assertCountEqual(self.e.coefficients(), [('x', 1), ('y', -2)])
55
56 def test_constant(self):
57 self.assertEqual(self.x.constant, 0)
58 self.assertEqual(self.pi.constant, Fraction(22, 7))
59 self.assertEqual(self.e.constant, 3)
60
61 def test_isconstant(self):
62 self.assertFalse(self.x.isconstant())
63 self.assertTrue(self.pi.isconstant())
64 self.assertFalse(self.e.isconstant())
65
66 def test_values(self):
67 self.assertCountEqual(self.e.values(), [1, -2, 3])
68
69 def test_symbol(self):
70 self.assertEqual(self.x.symbol(), 'x')
71 with self.assertRaises(ValueError):
72 self.pi.symbol()
73 with self.assertRaises(ValueError):
74 self.e.symbol()
75
76 def test_issymbol(self):
77 self.assertTrue(self.x.issymbol())
78 self.assertFalse(self.pi.issymbol())
79 self.assertFalse(self.e.issymbol())
80
81 def test_bool(self):
82 self.assertTrue(self.x)
83 self.assertFalse(self.zero)
84 self.assertTrue(self.pi)
85 self.assertTrue(self.e)
86
87 def test_pos(self):
88 self.assertEqual(+self.e, self.e)
89
90 def test_neg(self):
91 self.assertEqual(-self.e, -self.x + 2*self.y - 3)
92
93 def test_add(self):
94 self.assertEqual(self.x + Fraction(22, 7), self.x + self.pi)
95 self.assertEqual(Fraction(22, 7) + self.x, self.x + self.pi)
96 self.assertEqual(self.x + self.x, 2 * self.x)
97 self.assertEqual(self.e + 2*self.y, self.x + 3)
98
99 def test_sub(self):
100 self.assertEqual(self.x - self.x, 0)
101 self.assertEqual(self.e - 3, self.x - 2*self.y)
102
103 def test_mul(self):
104 self.assertEqual(self.pi * 7, 22)
105 self.assertEqual(self.e * 0, 0)
106 self.assertEqual(self.e * 2, 2*self.x - 4*self.y + 6)
107
108 def test_div(self):
109 with self.assertRaises(ZeroDivisionError):
110 self.e / 0
111 self.assertEqual(self.e / 2, self.x / 2 - self.y + Fraction(3, 2))
112
113 def test_str(self):
114 self.assertEqual(str(Expression()), '0')
115 self.assertEqual(str(self.x), 'x')
116 self.assertEqual(str(-self.x), '-x')
117 self.assertEqual(str(self.pi), '22/7')
118 self.assertEqual(str(self.e), 'x - 2*y + 3')
119
120 def test_repr(self):
121 self.assertEqual(repr(self.e), "Expression({'x': 1, 'y': -2}, 3)")
122
123 @unittest.expectedFailure
124 def test_fromstring(self):
125 self.assertEqual(Expression.fromstring('x'), self.x)
126 self.assertEqual(Expression.fromstring('-x'), -self.x)
127 self.assertEqual(Expression.fromstring('22/7'), self.pi)
128 self.assertEqual(Expression.fromstring('x - 2y + 3'), self.e)
129 self.assertEqual(Expression.fromstring('x - (3-1)y + 3'), self.e)
130 self.assertEqual(Expression.fromstring('x - 2*y + 3'), self.e)
131
132 def test_eq(self):
133 self.assertEqual(self.e, self.e)
134 self.assertNotEqual(self.x, self.y)
135 self.assertEqual(self.zero, 0)
136
137 def test_canonify(self):
138 self.assertEqual((self.x + self.y/2 + self.z/3)._canonify(),
139 6*self.x + 3*self.y + 2*self.z)
140
141
142 class TestHelpers(unittest.TestCase):
143
144 def setUp(self):
145 self.x = symbol('x')
146 self.y = symbol('y')
147
148 def test_constant(self):
149 self.assertEqual(constant(3), 3)
150 self.assertEqual(constant('3'), 3)
151 self.assertEqual(constant(Fraction(3, 4)), Fraction(3, 4))
152 self.assertEqual(constant('3/4'), Fraction(3, 4))
153 with self.assertRaises(ValueError):
154 constant('a')
155 with self.assertRaises(TypeError):
156 constant([])
157
158 def test_symbol(self):
159 self.assertEqual(symbol('x'), self.x)
160 self.assertNotEqual(symbol('y'), self.x)
161 with self.assertRaises(TypeError):
162 symbol(0)
163
164 def test_symbols(self):
165 self.assertListEqual(list(symbols('x y')), [self.x, self.y])
166 self.assertListEqual(list(symbols('x,y')), [self.x, self.y])
167 self.assertListEqual(list(symbols(['x', 'y'])), [self.x, self.y])
168
169
170 class TestOperators(unittest.TestCase):
171
172 pass
173
174
175 class TestPolyhedron(unittest.TestCase):
176
177 pass
178
179