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