Skip to content

Commit 5b16c20

Browse files
committed
test: add tests for fmpz and fmpz_mat
1 parent f7d27a5 commit 5b16c20

4 files changed

Lines changed: 30 additions & 20 deletions

File tree

.coveragerc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
[run]
22
plugins = coverage_plugin
3+
4+
[report]
5+
exclude_lines =
6+
assert False

src/flint/test/test_all.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def raises(f, exception):
2121
return False
2222

2323

24+
def test_raises():
25+
assert raises(lambda: 1/0, ZeroDivisionError) is True
26+
assert raises(lambda: 1/1, ZeroDivisionError) is False
27+
28+
2429
_default_ctx_string = """\
2530
pretty = True # pretty-print repr() output
2631
unicode = False # use unicode characters in output
@@ -153,8 +158,9 @@ def test_fmpz():
153158
# https://github.com/flintlib/python-flint/issues/74
154159
if not PYPY:
155160
assert pow(a, flint.fmpz(b), c) == ab_mod_c
156-
assert pow(a, b, flint.fmpz(c)) == ab_mod_c
157161
assert pow(a, flint.fmpz(b), flint.fmpz(c)) == ab_mod_c
162+
assert pow(a, b, flint.fmpz(c)) == ab_mod_c
163+
assert raises(lambda: pow([], flint.fmpz(2), 2), TypeError)
158164

159165
assert raises(lambda: pow(flint.fmpz(2), 2, 0), ValueError)
160166
# XXX: Handle negative modulus like int?
@@ -602,7 +608,8 @@ def set_bad(i,j):
602608
assert raises(lambda: M([[1,1],[1,1]]).solve(b), ZeroDivisionError)
603609
assert raises(lambda: M([[1,2],[3,4],[5,6]]).solve(b), ValueError)
604610
assert M([[1,0],[1,2]]).solve(b) == flint.fmpq_mat([[3],[2]])
605-
assert raises(lambda: M([[1,0],[1,2]]).solve(b, integer=True), ValueError)
611+
assert raises(lambda: M([[1,0],[1,0]]).solve(b, integer=True), ZeroDivisionError)
612+
assert raises(lambda: M([[1,0],[1,2]]).solve(b, integer=True), DomainError)
606613
assert raises(lambda: M([[1,2,3],[4,5,6]]).inv(), ValueError)
607614
assert raises(lambda: M([[1,1],[1,1]]).inv(), ZeroDivisionError)
608615
assert raises(lambda: M([[1,0],[1,2]]).inv(integer=True), ValueError)
@@ -632,6 +639,7 @@ def set_bad(i,j):
632639
for gram in "approx", "exact":
633640
assert M4.lll(rep=rep, gram=gram) == L4
634641
assert M4.lll(rep=rep, gram=gram, transform=True) == (L4, T4)
642+
assert raises(lambda: M4.lll(rep="gram"), AssertionError)
635643
assert raises(lambda: M4.lll(rep="bad"), ValueError)
636644
assert raises(lambda: M4.lll(gram="bad"), ValueError)
637645
M5 = M([[1,2,3],[4,5,6]])
@@ -4561,6 +4569,7 @@ def test_all_tests():
45614569

45624570
all_tests = [
45634571

4572+
test_raises,
45644573
test_pyflint,
45654574
test_showgood,
45664575

src/flint/types/fmpz.pyx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -395,18 +395,15 @@ cdef class fmpz(flint_scalar):
395395
return u
396396

397397
def __pow__(s, t, m):
398-
cdef fmpz_struct sval[1]
399-
cdef fmpz_struct tval[1]
400-
cdef fmpz_struct mval[1]
401-
cdef int stype = FMPZ_UNKNOWN
398+
cdef fmpz_t tval
399+
cdef fmpz_t mval
402400
cdef int ttype = FMPZ_UNKNOWN
403401
cdef int mtype = FMPZ_UNKNOWN
404402
cdef int success
405403
u = NotImplemented
406404

407405
try:
408-
stype = fmpz_set_any_ref(sval, s)
409-
if stype == FMPZ_UNKNOWN:
406+
if not typecheck(s, fmpz):
410407
return NotImplemented
411408
ttype = fmpz_set_any_ref(tval, t)
412409
if ttype == FMPZ_UNKNOWN:
@@ -440,12 +437,10 @@ cdef class fmpz(flint_scalar):
440437
raise ValueError("pow(): negative modulus not supported")
441438

442439
u = fmpz.__new__(fmpz)
443-
fmpz_powm((<fmpz>u).val, sval, tval, mval)
440+
fmpz_powm((<fmpz>u).val, s.val, tval, mval)
444441

445442
return u
446443
finally:
447-
if stype == FMPZ_TMP:
448-
fmpz_clear(sval)
449444
if ttype == FMPZ_TMP:
450445
fmpz_clear(tval)
451446
if mtype == FMPZ_TMP:

src/flint/types/fmpz_mat.pyx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,16 @@ cdef class fmpz_mat(flint_mat):
300300
def __pow__(self, e, m):
301301
cdef fmpz_mat t
302302
cdef ulong ee
303-
if not typecheck(self, fmpz_mat):
304-
return NotImplemented
305-
if not fmpz_mat_is_square((<fmpz_mat>self).val):
303+
if not fmpz_mat_is_square(self.val):
306304
raise ValueError("matrix must be square")
307305
if m is not None:
308306
raise NotImplementedError("modular matrix exponentiation")
309307
if e < 0:
308+
# Allow unimodular?
310309
raise DomainError("negative power of integer matrix: M**%i" % e)
311310
ee = e
312311
t = fmpz_mat.__new__(fmpz_mat)
313-
fmpz_mat_init_set(t.val, (<fmpz_mat>self).val)
312+
fmpz_mat_init_set(t.val, self.val)
314313
fmpz_mat_pow(t.val, t.val, ee)
315314
return t
316315

@@ -518,7 +517,7 @@ cdef class fmpz_mat(flint_mat):
518517
>>> A.solve(B, integer=True)
519518
Traceback (most recent call last):
520519
...
521-
ValueError: matrix is not invertible over the integers
520+
flint.utils.flint_exceptions.DomainError: matrix is not invertible over the integers
522521
>>> fmpz_mat([[1,2], [3,5]]).solve(B, integer=True)
523522
[ 6, 3, 0]
524523
[-3, -1, 1]
@@ -554,11 +553,11 @@ cdef class fmpz_mat(flint_mat):
554553
fmpz_mat_ncols((<fmpz_mat>t).val))
555554
d = fmpz.__new__(fmpz)
556555
result = fmpz_mat_solve(u.val, d.val, self.val, (<fmpz_mat>t).val)
557-
if not fmpz_is_pm1(d.val):
558-
raise ValueError("matrix is not invertible over the integers")
559-
u *= d
560556
if not result:
561557
raise ZeroDivisionError("singular matrix in solve()")
558+
if not fmpz_is_pm1(d.val):
559+
raise DomainError("matrix is not invertible over the integers")
560+
u *= d
562561
return u
563562

564563
def rref(self, inplace=False):
@@ -640,7 +639,10 @@ cdef class fmpz_mat(flint_mat):
640639
if rep == "zbasis":
641640
rt = 1
642641
elif rep == "gram":
643-
rt = 0
642+
# rt = 0
643+
# XXX: This consumes all memory and crashes. Maybe the parameters
644+
# need to be different or something? Best to disable this for now.
645+
assert False, "rep = 'gram' does not work currently."
644646
else:
645647
raise ValueError("rep must be 'zbasis' or 'gram'")
646648
if gram == "approx":

0 commit comments

Comments
 (0)