7186dad489acadc093d48ccd1db187441c1dcd81
4 from math
import floor
, ceil
, trunc
6 from pypol
.isl
import *
9 class TestContext(unittest
.TestCase
):
12 ctx1
, ctx2
= Context(), Context()
13 self
.assertEqual(ctx1
, ctx1
)
14 self
.assertNotEqual(ctx1
, ctx2
)
17 class TestValue(unittest
.TestCase
):
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)
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
)
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):
45 with self
.assertRaises(ValueError):
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):
54 with self
.assertRaises(ValueError):
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')))
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
)
75 self
.assertTrue(self
.pi
<= self
.pi
)
76 self
.assertTrue(self
.pi
<= self
.answer
)
77 self
.assertFalse(self
.answer
<= self
.pi
)
80 self
.assertFalse(self
.pi
> self
.pi
)
81 self
.assertTrue(self
.answer
> self
.pi
)
82 self
.assertFalse(self
.pi
> self
.answer
)
85 self
.assertTrue(self
.pi
>= self
.pi
)
86 self
.assertTrue(self
.answer
>= self
.pi
)
87 self
.assertFalse(self
.pi
>= self
.answer
)
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):
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
)
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())
114 self
.assertEqual(+self
.pi
, self
.pi
)
117 self
.assertEqual(-self
.neginf
, self
.inf
)
118 self
.assertEqual(-(-self
.pi
), self
.pi
)
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())
128 self
.assertEqual(ceil(self
.pi
), Value(self
.context
, 4))
129 self
.assertRaises(ceil(-self
.pi
) == Value(self
.context
, -3))
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))
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):
151 self
.assertEqual(self
.answer
- self
.pi
, Value(self
.context
, 272, 7))
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())
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())
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'))
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())
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())
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())
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')
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')")