Implement IslObjects, delete Value
[linpy.git] / tests / test_isl.py
1
2 import unittest
3
4 from math import floor, ceil, trunc
5
6 from pypol.isl import *
7
8
9 class TestContext(unittest.TestCase):
10
11 def test_eq(self):
12 ctx1, ctx2 = Context(), Context()
13 self.assertEqual(ctx1, ctx1)
14 self.assertNotEqual(ctx1, ctx2)
15
16
17 class TestValue(unittest.TestCase):
18
19 def setUp(self):
20 self.context = Context()
21 self.zero = Value(self.context)
22 self.nan = Value(self.context, 'NaN')
23 self.inf = Value(self.context, 'Inf')
24 self.neginf = Value(self.context, '-Inf')
25 self.answer = Value(self.context, 42)
26 self.pi = Value(self.context, 22, 7)
27
28 def test_init(self):
29 self.assertEqual(Value(self.context, 42), self.answer)
30 self.assertEqual(Value(self.context, '42'), self.answer)
31 self.assertEqual(Value(self.context, 22, 7), self.pi)
32 self.assertEqual(Value(self.context, '-22/7'), -self.pi)
33 self.assertTrue(Value(self.context, 'nan').is_nan())
34 self.assertTrue(Value(self.context, '-nan').is_nan())
35 self.assertTrue(Value(self.context, 'NaN').is_nan())
36 self.assertEqual(Value(self.context, '-inf'), self.neginf)
37 self.assertEqual(Value(self.context, '-Infinity'), self.neginf)
38
39 def test_numerator(self):
40 self.assertEqual(self.zero.numerator, 0)
41 self.assertEqual(self.answer.numerator, 42)
42 self.assertEqual(self.pi.numerator, 22)
43 with self.assertRaises(ValueError):
44 self.nan.numerator
45 with self.assertRaises(ValueError):
46 self.inf.numerator
47
48 def test_denominator(self):
49 self.assertEqual(self.zero.denominator, 1)
50 self.assertEqual(self.answer.denominator, 1)
51 self.assertEqual(self.pi.denominator, 7)
52 with self.assertRaises(ValueError):
53 self.nan.denominator
54 with self.assertRaises(ValueError):
55 self.inf.denominator
56
57 def test_bool(self):
58 self.assertFalse(self.zero)
59 self.assertTrue(self.answer)
60 self.assertTrue(self.pi)
61 self.assertEqual(bool(self.nan), bool(float('nan')))
62 self.assertEqual(bool(self.inf), bool(float('inf')))
63
64 def test_lt(self):
65 self.assertTrue(self.neginf < self.zero)
66 self.assertTrue(self.zero < self.pi)
67 self.assertTrue(self.pi < self.answer)
68 self.assertTrue(self.answer < self.inf)
69 self.assertFalse(self.nan < self.answer)
70 self.assertFalse(self.nan < self.inf)
71 self.assertFalse(self.nan < self.neginf)
72 self.assertTrue(self.neginf < self.inf)
73
74 def test_le(self):
75 self.assertTrue(self.pi <= self.pi)
76 self.assertTrue(self.pi <= self.answer)
77 self.assertFalse(self.answer <= self.pi)
78
79 def test_gt(self):
80 self.assertFalse(self.pi > self.pi)
81 self.assertTrue(self.answer > self.pi)
82 self.assertFalse(self.pi > self.answer)
83
84 def test_ge(self):
85 self.assertTrue(self.pi >= self.pi)
86 self.assertTrue(self.answer >= self.pi)
87 self.assertFalse(self.pi >= self.answer)
88
89 def test_eq(self):
90 self.assertEqual(self.pi, self.pi)
91 self.assertEqual(self.inf, self.inf)
92 self.assertNotEqual(self.neginf, self.inf)
93 self.assertNotEqual(self.nan, self.nan)
94 self.assertEqual(self.zero, 0)
95 self.assertEqual(0, self.zero)
96 self.assertEqual(self.pi, Fraction(22, 7))
97 self.assertEqual(Fraction(22, 7), self.pi)
98 with self.assertRaises(TypeError):
99 self.zero == 0.
100
101 def test_ne(self):
102 self.assertTrue(self.pi != self.answer)
103 self.assertFalse(self.pi != self.pi)
104 self.assertTrue(self.neginf != self.inf)
105 self.assertTrue(self.nan != self.nan)
106
107 def test_abs(self):
108 self.assertEqual(abs(self.pi), self.pi)
109 self.assertEqual(abs(self.neginf), self.inf)
110 self.assertEqual(abs(-self.pi), self.pi)
111 self.assertTrue(abs(self.nan).is_nan())
112
113 def test_pos(self):
114 self.assertEqual(+self.pi, self.pi)
115
116 def test_neg(self):
117 self.assertEqual(-self.neginf, self.inf)
118 self.assertEqual(-(-self.pi), self.pi)
119
120 def test_floor(self):
121 self.assertEqual(floor(self.pi), Value(self.context, 3))
122 self.assertEqual(floor(-self.pi), Value(self.context, -4))
123 # not float behavior, but makes sense
124 self.assertEqual(floor(self.inf), self.inf)
125 self.assertTrue(floor(self.nan).is_nan())
126
127 def test_ceil(self):
128 self.assertEqual(ceil(self.pi), Value(self.context, 4))
129 self.assertRaises(ceil(-self.pi) == Value(self.context, -3))
130
131 def test_trunc(self):
132 self.assertEqual(trunc(self.pi), Value(self.context, 3))
133 self.assertEqual(trunc(-self.pi), Value(self.context, -3))
134
135 def test_add(self):
136 self.assertEqual(self.answer + self.answer, Value(self.context, 84))
137 self.assertEqual(self.answer + self.pi, Value(self.context, 316, 7))
138 self.assertEqual(self.pi + self.pi, Value(self.context, 44, 7))
139 self.assertEqual(self.pi + self.neginf, self.neginf)
140 self.assertEqual(self.pi + self.inf, self.inf)
141 self.assertTrue((self.pi + self.nan).is_nan())
142 self.assertTrue((self.inf + self.nan).is_nan())
143 self.assertTrue((self.inf + self.neginf).is_nan())
144 self.assertEqual(self.pi + 42, Value(self.context, 316, 7))
145 self.assertEqual(42 + self.pi, Value(self.context, 316, 7))
146 self.assertEqual(self.pi + Fraction(22, 7), Value(self.context, 44, 7))
147 with self.assertRaises(TypeError):
148 self.pi + float(42)
149
150 def test_sub(self):
151 self.assertEqual(self.answer - self.pi, Value(self.context, 272, 7))
152
153 def test_mul(self):
154 self.assertEqual(Value(self.context, 6) * Value(self.context, 7), self.answer)
155 self.assertNotEqual(Value(self.context, 6) * Value(self.context, 9), self.answer)
156 self.assertEqual(self.inf * Value(self.context, 2), self.inf)
157 self.assertEqual(self.inf * Value(self.context, -2), self.neginf)
158 self.assertTrue((self.nan * Value(self.context, 2)).is_nan())
159 self.assertTrue((self.nan * self.inf).is_nan())
160
161 def test_div(self):
162 self.assertEqual(Value(self.context, 22) / Value(self.context, 7), self.pi)
163 self.assertEqual(self.pi / self.pi, Value(self.context, 1))
164 # not float behavior, but makes sense
165 self.assertTrue((self.pi / self.zero).is_nan())
166
167 def test_float(self):
168 self.assertAlmostEqual(float(Value(self.context, 1, 2)), 0.5)
169 self.assertTrue(math.isnan(float(Value(self.context, 'NaN'))))
170 self.assertAlmostEqual(float(Value(self.context, 'Inf')), float('inf'))
171
172 def test_is_finite(self):
173 self.assertTrue(self.pi.is_finite())
174 self.assertFalse(self.inf.is_finite())
175 self.assertFalse(self.nan.is_finite())
176
177 def test_is_infinite(self):
178 self.assertFalse(self.pi.is_infinite())
179 self.assertTrue(self.inf.is_infinite())
180 self.assertFalse(self.nan.is_infinite())
181
182 def test_is_nan(self):
183 self.assertFalse(self.pi.is_nan())
184 self.assertFalse(self.inf.is_nan())
185 self.assertTrue(self.nan.is_nan())
186
187 def test_str(self):
188 self.assertEqual(str(self.answer), '42')
189 self.assertEqual(str(self.pi), '22/7')
190 self.assertEqual(str(self.nan), 'NaN')
191 self.assertEqual(str(self.inf), 'Infinity')
192 self.assertEqual(str(self.neginf), '-Infinity')
193
194 def test_repr(self):
195 self.assertEqual(repr(self.answer), "Value('42')")
196 self.assertEqual(repr(self.pi), "Value('22/7')")
197 self.assertEqual(repr(self.nan), "Value('NaN')")
198 self.assertEqual(repr(self.inf), "Value('Infinity')")
199 self.assertEqual(repr(self.neginf), "Value('-Infinity')")