Skip to content

Commit f4889b3

Browse files
committed
unittests for integers and backward compatibilities
1 parent 26898c3 commit f4889b3

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ install:
1414

1515
check:
1616
ulimit -s 8192; $(PYTHON) -u tests/rundoctest.py
17+
ulimit -s 8192; $(PYTHON) tests/test_integers.py
18+
ulimit -s 8192; $(PYTHON) tests/test_backward.py
1719

1820
dist:
1921
chmod go+rX-w -R .

tests/test_backward.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
#*****************************************************************************
3+
# Copyright (C) 2020 Vincent Delecroix <[email protected]>
4+
#
5+
# Distributed under the terms of the GNU General Public License (GPL)
6+
# as published by the Free Software Foundation; either version 2 of
7+
# the License, or (at your option) any later version.
8+
# http://www.gnu.org/licenses/
9+
#*****************************************************************************
10+
11+
import cypari2
12+
import unittest
13+
14+
class TestBackward(unittest.TestCase):
15+
def test_polisirreducible(self):
16+
pari = cypari2.Pari()
17+
p = pari('x^2 + 1')
18+
self.assertTrue(p.polisirreducible())
19+
20+
def test_sqrtint(self):
21+
pari = cypari2.Pari()
22+
self.assertEqual(pari(10).sqrtint(), 3)
23+
24+
def test_poldegree(self):
25+
pari = cypari2.Pari()
26+
self.assertEqual(pari('x + 1').poldegree(), 1)
27+
self.assertEqual(pari('x*y^2 + 1').poldegree(pari('x')), 1)
28+
self.assertEqual(pari('x*y^2 + 1').poldegree(pari('y')), 2)
29+
30+
def test_nfbasis(self):
31+
pari = cypari2.Pari()
32+
x = pari('x')
33+
third = pari('1/3')
34+
self.assertEqual(pari(x**3 - 17).nfbasis(), [1, x, third*x**2 - third*x + third])
35+
36+
p = pari(10**10).nextprime()
37+
q = (p+1).nextprime()
38+
x = pari('x')
39+
f = x**2 + p**2*q
40+
# Correct result
41+
frac = pari('1/10000000019')
42+
self.assertEqual(pari(f).nfbasis(), [1, frac*x])
43+
44+
# Wrong result
45+
self.assertEqual(pari.nfbasis([f,1]), [1, x])
46+
# Check primes up to 10^6: wrong result
47+
self.assertEqual(pari.nfbasis([f, 10**6]), [1, x])
48+
# Correct result and faster
49+
self.assertEqual(pari.nfbasis([f, pari("[2,2; %s,2]"%p)]), [1, frac*x])
50+
# Equivalent with the above
51+
self.assertEqual(pari.nfbasis([f, [2,p]]), [1, frac*x])
52+
53+
if __name__ == '__main__':
54+
unittest.main()

tests/test_integers.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
#*****************************************************************************
3+
# Copyright (C) 2020 Vincent Delecroix <[email protected]>
4+
#
5+
# Distributed under the terms of the GNU General Public License (GPL)
6+
# as published by the Free Software Foundation; either version 2 of
7+
# the License, or (at your option) any later version.
8+
# http://www.gnu.org/licenses/
9+
#*****************************************************************************
10+
11+
import random
12+
import cypari2
13+
import unittest
14+
15+
class TestPariInteger(unittest.TestCase):
16+
def randint(self):
17+
p = random.random()
18+
if p < 0.05:
19+
return random.randint(-2, 2)
20+
elif p < 0.5:
21+
return random.randint(-2**30, 2**30)
22+
else:
23+
return random.randint(-2**100, 2**100)
24+
25+
def cmp(self, a, b):
26+
pari = cypari2.Pari()
27+
pa = pari(a)
28+
pb = pari(b)
29+
30+
self.assertTrue(pa == pa and a == pa and pa == a)
31+
self.assertEqual(a == b, pa == pb)
32+
self.assertEqual(a != b, pa != pb)
33+
self.assertEqual(a < b, pa < pb)
34+
self.assertEqual(a <= b, pa <= pb)
35+
self.assertEqual(a > b, pa > pb)
36+
self.assertEqual(a >= b, pa >= pb)
37+
38+
def test_cmp(self):
39+
for _ in range(100):
40+
a = self.randint()
41+
b = self.randint()
42+
self.cmp(a, a)
43+
self.cmp(a, b)
44+
45+
def test_binop(self):
46+
pari = cypari2.Pari()
47+
48+
for _ in range(100):
49+
a = self.randint()
50+
b = self.randint()
51+
52+
self.assertEqual(a + b, pari(a) + pari(b))
53+
self.assertEqual(a - b, pari(a) - pari(b))
54+
self.assertEqual(a * b, pari(a) * pari(b))
55+
56+
if b > 0:
57+
self.assertEqual(a % b, pari(a) % pari(b))
58+
59+
def test_zero_division(self):
60+
pari = cypari2.Pari()
61+
with self.assertRaises(cypari2.PariError):
62+
pari(2) / pari(0)
63+
64+
if __name__ == '__main__':
65+
unittest.main()

0 commit comments

Comments
 (0)