diff --git a/src/gmpy2_mpfr_misc.c b/src/gmpy2_mpfr_misc.c index 00bf2c9f..13c2eed8 100644 --- a/src/gmpy2_mpfr_misc.c +++ b/src/gmpy2_mpfr_misc.c @@ -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))) { @@ -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; } @@ -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))) { @@ -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; } diff --git a/test/test_functions.py b/test/test_functions.py index 16bd9db9..19492246 100644 --- a/test/test_functions.py +++ b/test/test_functions.py @@ -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(): @@ -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) diff --git a/test/test_misc.py b/test/test_misc.py index c335e5c3..c60aa185 100644 --- a/test/test_misc.py +++ b/test/test_misc.py @@ -1,3 +1,5 @@ +import sys + import gmpy2 @@ -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 diff --git a/test/test_mpfr.py b/test/test_mpfr.py index 6bee22fe..245db419 100644 --- a/test/test_mpfr.py +++ b/test/test_mpfr.py @@ -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')