Skip to content

Commit

Permalink
Improve test coverage for gmpy2_mpfr_misc.c
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Oct 24, 2023
1 parent ed3036c commit 9c21e09
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/gmpy2_mpfr_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,11 @@ GMPy_MPFR_Integer_Ratio_Method(PyObject *self, PyObject *args)
num = GMPy_MPZ_New(context);
den = GMPy_MPZ_New(context);
if (!num || !den) {
/* LCOV_EXCL_START */
Py_XDECREF((PyObject*)num);
Py_XDECREF((PyObject*)den);
return NULL;
/* LCOV_EXCL_STOP */
}

if (mpfr_zero_p(MPFR(self))) {
Expand All @@ -472,8 +474,10 @@ GMPy_MPFR_Integer_Ratio_Method(PyObject *self, PyObject *args)
}
result = Py_BuildValue("(NN)", (PyObject*)num, (PyObject*)den);
if (!result) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)num);
Py_DECREF((PyObject*)den);
/* LCOV_EXCL_STOP */
}
return result;
}
Expand Down Expand Up @@ -505,9 +509,11 @@ GMPy_MPFR_Mantissa_Exp_Method(PyObject *self, PyObject *args)
mantissa = GMPy_MPZ_New(context);
exponent = GMPy_MPZ_New(context);
if (!mantissa || !exponent) {
/* LCOV_EXCL_START */
Py_XDECREF((PyObject*)mantissa);
Py_XDECREF((PyObject*)exponent);
return NULL;
/* LCOV_EXCL_STOP */
}

if (mpfr_zero_p(MPFR(self))) {
Expand All @@ -520,8 +526,10 @@ GMPy_MPFR_Mantissa_Exp_Method(PyObject *self, PyObject *args)
}
result = Py_BuildValue("(NN)", (PyObject*)mantissa, (PyObject*)exponent);
if (!result) {
/* LCOV_EXCL_START */
Py_DECREF((PyObject*)mantissa);
Py_DECREF((PyObject*)exponent);
/* LCOV_EXCL_STOP */
}
return result;
}
Expand Down
37 changes: 35 additions & 2 deletions test/test_functions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import pytest

import gmpy2
from gmpy2 import (root, rootn, zero, mpz, mpq, mpfr, mpc, is_nan, maxnum,
minnum, fma, fms, ieee, fmma, fmms)
from gmpy2 import (can_round, fac, fma, fmma, fmms, fms, get_exp, ieee, is_nan,
maxnum, minnum, mpc, mpfr, mpq, mpz, root, rootn, set_exp,
zero)


def test_root():
Expand Down Expand Up @@ -133,3 +134,35 @@ def test_trigonometric():
assert gmpy2.atanh(mpc(2.0, 3.0)) == gmpy2.atanh(complex(2, 3))

assert gmpy2.tanh(mpc(4,5)) == mpc('1.0005630461157933-0.00036520305451130409j')


def test_get_exp():
ctx = gmpy2.get_context()
ctx.trap_erange = True

pytest.raises(gmpy2.RangeError, lambda: get_exp(mpfr('inf')))


def test_set_exp():
pytest.raises(ValueError, lambda: set_exp(mpfr('1.0'), int(fac(100))))

gmpy2.set_context(gmpy2.ieee(32))
ctx = gmpy2.get_context()
ctx.trap_erange = True

pytest.raises(gmpy2.RangeError, lambda: set_exp(mpfr('1.0'), 1000))

ctx.trap_erange = False
assert set_exp(mpfr('1.0'), 1000) == mpfr('1.0')


def test_can_round():
pytest.raises(TypeError, lambda: can_round(mpfr('1.1'), 10, "spam"))
pytest.raises(ValueError, lambda: can_round(mpfr('1.1'), 10, 111, 111, 111))
pytest.raises(ValueError, lambda: can_round(mpfr('1.1'), 10, 1, 111, 111))
pytest.raises(ValueError, lambda: can_round(mpfr('1.1'), 10, 1, 1, -111))

x = mpfr('-1.112')

assert can_round(x, 10, 1, 1, 1)
assert not can_round(x, 10, 1, 1, 10)
6 changes: 6 additions & 0 deletions test/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import gmpy2


Expand All @@ -13,3 +15,7 @@ def test_misc():
'3 or later. The supported versions of the GMP, '
'MPFR, and MPC libraries are also licensed '
'under LGPL 3 or later.')


def test_sizeof():
assert sys.getsizeof(gmpy2.mpfr('1.0')) > 0
13 changes: 13 additions & 0 deletions test/test_mpfr.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,16 @@ def fmt(v):
assert fmt(gmpy2.next_toward(a, 10)) == '1.00000000000000000000001p-126'

gmpy2.set_context(gmpy2.context())


def test_mpfr_as_integer_ratio():
assert mpfr('1.1e+2').as_integer_ratio() == (mpz(110), mpz(1))


def test_mpfr_round():
pytest.raises(TypeError, lambda: round(mpfr('1.0'), "spam"))

r = round(mpfr('-0.0'), 123)
assert r.is_zero() and r.is_signed()

assert round(mpfr('12.34'), -1) == mpfr('10.0')

0 comments on commit 9c21e09

Please sign in to comment.