Skip to content

Commit 040761c

Browse files
committed
test: add tests for fmpz and fmpz_mat
1 parent 30fb7e4 commit 040761c

4 files changed

Lines changed: 30 additions & 19 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]])
@@ -4505,6 +4513,7 @@ def test_all_tests():
45054513

45064514
all_tests = [
45074515

4516+
test_raises,
45084517
test_pyflint,
45094518
test_showgood,
45104519

src/flint/types/fmpz.pyx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -378,18 +378,15 @@ cdef class fmpz(flint_scalar):
378378
return u
379379

380380
def __pow__(s, t, m):
381-
cdef fmpz_struct sval[1]
382-
cdef fmpz_struct tval[1]
383-
cdef fmpz_struct mval[1]
384-
cdef int stype = FMPZ_UNKNOWN
381+
cdef fmpz_t tval
382+
cdef fmpz_t mval
385383
cdef int ttype = FMPZ_UNKNOWN
386384
cdef int mtype = FMPZ_UNKNOWN
387385
cdef int success
388386
u = NotImplemented
389387

390388
try:
391-
stype = fmpz_set_any_ref(sval, s)
392-
if stype == FMPZ_UNKNOWN:
389+
if not typecheck(s, fmpz):
393390
return NotImplemented
394391
ttype = fmpz_set_any_ref(tval, t)
395392
if ttype == FMPZ_UNKNOWN:
@@ -423,11 +420,10 @@ cdef class fmpz(flint_scalar):
423420
raise ValueError("pow(): negative modulus not supported")
424421

425422
u = fmpz.__new__(fmpz)
426-
fmpz_powm((<fmpz>u).val, sval, tval, mval)
423+
fmpz_powm((<fmpz>u).val, s.val, tval, mval)
427424

428425
return u
429426
finally:
430-
if stype == FMPZ_TMP: fmpz_clear(sval)
431427
if ttype == FMPZ_TMP: fmpz_clear(tval)
432428
if mtype == FMPZ_TMP: fmpz_clear(mval)
433429

src/flint/types/fmpz_mat.pyx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,16 @@ cdef class fmpz_mat(flint_mat):
302302
def __pow__(self, e, m):
303303
cdef fmpz_mat t
304304
cdef ulong ee
305-
if not typecheck(self, fmpz_mat):
306-
return NotImplemented
307-
if not fmpz_mat_is_square((<fmpz_mat>self).val):
305+
if not fmpz_mat_is_square(self.val):
308306
raise ValueError("matrix must be square")
309307
if m is not None:
310308
raise NotImplementedError("modular matrix exponentiation")
311309
if e < 0:
310+
# Allow unimodular?
312311
raise DomainError("negative power of integer matrix: M**%i" % e)
313312
ee = e
314313
t = fmpz_mat.__new__(fmpz_mat)
315-
fmpz_mat_init_set(t.val, (<fmpz_mat>self).val)
314+
fmpz_mat_init_set(t.val, self.val)
316315
fmpz_mat_pow(t.val, t.val, ee)
317316
return t
318317

@@ -520,7 +519,7 @@ cdef class fmpz_mat(flint_mat):
520519
>>> A.solve(B, integer=True)
521520
Traceback (most recent call last):
522521
...
523-
ValueError: matrix is not invertible over the integers
522+
flint.utils.flint_exceptions.DomainError: matrix is not invertible over the integers
524523
>>> fmpz_mat([[1,2], [3,5]]).solve(B, integer=True)
525524
[ 6, 3, 0]
526525
[-3, -1, 1]
@@ -556,11 +555,11 @@ cdef class fmpz_mat(flint_mat):
556555
fmpz_mat_ncols((<fmpz_mat>t).val))
557556
d = fmpz.__new__(fmpz)
558557
result = fmpz_mat_solve(u.val, d.val, self.val, (<fmpz_mat>t).val)
559-
if not fmpz_is_pm1(d.val):
560-
raise ValueError("matrix is not invertible over the integers")
561-
u *= d
562558
if not result:
563559
raise ZeroDivisionError("singular matrix in solve()")
560+
if not fmpz_is_pm1(d.val):
561+
raise DomainError("matrix is not invertible over the integers")
562+
u *= d
564563
return u
565564

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

0 commit comments

Comments
 (0)