Skip to content

Commit

Permalink
Moved prev_prime tests to test/test_mpz.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sethtroisi committed Sep 7, 2023
1 parent b24fdea commit 97295d6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
19 changes: 10 additions & 9 deletions src/gmpy2_mpz_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1505,12 +1505,13 @@ GMPy_MPZ_Function_NextPrime(PyObject *self, PyObject *other)

PyDoc_STRVAR(GMPy_doc_mpz_function_prev_prime,
"prev_prime(x, /) -> mpz\n\n"
"Return the previous *probable* prime number < x.");
"Return the previous *probable* prime number < x.\n"
"Only present when compiled with GMP 6.3.0 or later.");

#if (__GNU_MP_VERSION > 6) || (__GNU_MP_VERSION == 6 && __GNU_MP_VERSION_MINOR >= 3)
static PyObject *
GMPy_MPZ_Function_PrevPrime(PyObject *self, PyObject *other)
{
#if (__GNU_MP_VERSION > 6) || (__GNU_MP_VERSION == 6 && __GNU_MP_VERSION_MINOR >= 3)
MPZ_Object *result;

if(MPZ_Check(other)) {
Expand All @@ -1520,8 +1521,9 @@ GMPy_MPZ_Function_PrevPrime(PyObject *self, PyObject *other)
/* LCOV_EXCL_STOP */
}
if (!mpz_prevprime(result->z, MPZ(other))) {
/* no previous prime, default to 2 in this case */
mpz_set_ui(result->z, 2);
/* no previous prime, raise value error. */
VALUE_ERROR("x must be >= 3");
return NULL;
}
}
else {
Expand All @@ -1531,16 +1533,15 @@ GMPy_MPZ_Function_PrevPrime(PyObject *self, PyObject *other)
}
else {
if (!mpz_prevprime(result->z, result->z)) {
/* no previous prime, default to 2 in this case */
mpz_set_ui(result->z, 2);
/* no previous prime, raise value error. */
VALUE_ERROR("x must be >= 3");
return NULL;
}
}
}
return (PyObject*)result;
#endif

Py_RETURN_NOTIMPLEMENTED;
}
#endif


PyDoc_STRVAR(GMPy_doc_mpz_function_jacobi,
Expand Down
21 changes: 0 additions & 21 deletions test/test_gmpy2_mpz_misc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -657,27 +657,6 @@ mpz(1000003)
>>> gmpy2.next_prime(2357*7069-1) == 2357*7069
False

Test prev_prime
---------------
>>> gmpy2.prev_prime('a')
Traceback (most recent call last):
...
TypeError:
>>> gmpy2.prev_prime(2)
mpz(2)
>>> gmpy2.prev_prime(mpz(3))
mpz(2)
>>> gmpy2.prev_prime(3)
mpz(2)
>>> gmpy2.prev_prime(1000004)
mpz(1000003)
>>> gmpy2.prev_prime(1000033)
mpz(1000003)
>>> gmpy2.prev_prime(-100)
mpz(2)
>>> gmpy2.prev_prime(1)
mpz(2)

Test iroot
----------
>>> gmpy2.iroot(1,2,3)
Expand Down
24 changes: 23 additions & 1 deletion test/test_mpz.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from pytest import raises

from gmpy2 import (mpz, pack, unpack, cmp, cmp_abs, to_binary, from_binary,
random_state, mpz_random, mpz_urandomb, mpz_rrandomb)
random_state, mpz_random, mpz_urandomb, mpz_rrandomb,
prev_prime)
from supportclasses import a, b, c, d, z, q


Expand Down Expand Up @@ -309,3 +310,24 @@ def test_mpz_urandomb():
def test_mpz_rrandomb():
assert (mpz_rrandomb(random_state(42), 64).digits(2) ==
'1111111111111111111111111100000000111111111111111111000000000000')


def test_prev_prime():
assert prev_prime(3) == mpz(2)
assert prev_prime(4) == mpz(3)
assert prev_prime(5) == mpz(3)
assert prev_prime(6) == mpz(5)
assert prev_prime(1000004) == mpz(1000003)
assert prev_prime(1000033) == mpz(1000003)

with raises(ValueError):
prev_prime(-100)

with raises(ValueError):
prev_prime(2)

with raises(TypeError):
prev_prime('a')

with raises(TypeError):
prev_prime(4.5)

0 comments on commit 97295d6

Please sign in to comment.