Skip to content

Commit

Permalink
Merge pull request #456 from skirpichev/pytest5
Browse files Browse the repository at this point in the history
Port some tests to pytest framework (step 5)
  • Loading branch information
casevh authored Nov 24, 2023
2 parents 1be5f1a + 327ca2b commit 2183205
Show file tree
Hide file tree
Showing 15 changed files with 1,699 additions and 2,907 deletions.
9 changes: 2 additions & 7 deletions src/gmpy2_mpfr_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ GMPy_MPFR_SizeOf_Method(PyObject *self, PyObject *other)
}

PyDoc_STRVAR(GMPy_doc_method_round10,
"__round__(x[, n = 0]) -> mpfr\n\n"
"x.__round__(n = 0, /) -> mpfr\n\n"
"Return x rounded to n decimal digits before (n < 0) or after (n > 0)\n"
"the decimal point. Rounds to an integer if n is not specified.");

Expand Down Expand Up @@ -752,12 +752,7 @@ GMPy_MPFR_Method_Round10(PyObject *self, PyObject *args)
return self;
}

if (PyTuple_GET_SIZE(args) > 1) {
TYPE_ERROR("__round__() requires 0 or 1 argument");
return NULL;
}

if (PyTuple_GET_SIZE(args) == 1) {
if (PyTuple_GET_SIZE(args) >= 1) {
digits = PyLong_AsLong(PyTuple_GET_ITEM(args, 0));
if (digits == -1 && PyErr_Occurred()) {
TYPE_ERROR("__round__() requires 'int' argument");
Expand Down
13 changes: 1 addition & 12 deletions test/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,14 @@
print(" Complex library: {0}".format(gmpy2.mpc_version()))
print()

mpz_doctests = ["test_mpz.txt"]

mpq_doctests = ["test_mpq.txt"]

mpfr_doctests = ["test_mpfr.txt",
"test_mpfr_trig.txt", "test_context.txt"]

gmpy2_tests = [os.path.basename(i)
for i in glob.glob(os.path.join(test_dir,
"test_gmpy2*.txt"))]

failed = 0
attempted = 0

all_doctests = gmpy2_tests + mpz_doctests + mpq_doctests

all_doctests += mpfr_doctests

for test in sorted(all_doctests):
for test in sorted(gmpy2_tests):
result = doctest.testfile(test, globs=globals(),
optionflags=doctest.IGNORE_EXCEPTION_DETAIL |
doctest.NORMALIZE_WHITESPACE |
Expand Down
206 changes: 205 additions & 1 deletion test/test_context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest

import gmpy2
from gmpy2 import mpc, mpfr, mpz
from gmpy2 import (context, get_context, ieee, local_context, mpc, mpfr, mpz,
set_context)


def test_context_abs():
Expand All @@ -14,3 +17,204 @@ def test_context_abs():
assert ctx.abs(mpfr(-2)) == mpfr('2.0')
assert ctx.abs(2+3j) == mpfr('3.6055512754639891')
assert ctx.abs(mpc(2+3j)) == mpfr('3.6055512754639891')


def test_context_ieee():
ctx = ieee(32)

assert (ctx.precision == 24 and ctx.emax == 128 and
ctx.emin == -148 and ctx.subnormalize)

ctx = ieee(64)

assert (ctx.precision == 53 and ctx.emax == 1024 and
ctx.emin == -1073 and ctx.subnormalize)

ctx = ieee(128)

assert (ctx.precision == 113 and ctx.emax == 16384 and
ctx.emin == -16493 and ctx.subnormalize)

ctx = ieee(256)

assert (ctx.precision == 237 and ctx.emax == 262144 and
ctx.emin == -262377 and ctx.subnormalize)

pytest.raises(ValueError, lambda: ieee(-1))
pytest.raises(TypeError, lambda: ieee("a"))

set_context(ieee(32))

assert gmpy2.const_pi().digits(2) == ('110010010000111111011011', 2, 24)

set_context(ieee(64))

assert gmpy2.const_pi().digits(2) == ('11001001000011111101101010100010001000010110100011000', 2, 53)

set_context(ieee(128))

assert gmpy2.const_pi().digits(2) == ('11001001000011111101101010100010001000010110100011000010001101001100010011000110011000101000101110000000110111000', 2, 113)


def test_context():
ctx = context()

assert (ctx.precision == 53 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)

ctx = context(precision=100)

assert (ctx.precision == 100 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)

ctx = context(real_prec=100)

assert (ctx.precision == 53 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize and
ctx.real_prec == 100)

ctx = context(real_prec=100,imag_prec=200)

assert (ctx.precision == 53 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize and
ctx.real_prec == 100 and ctx.imag_prec == 200)


def test_get_context():
set_context(context())

ctx = get_context()

assert (ctx.precision == 53 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)

ctx = get_context()
ctx.precision = 100

assert (ctx.precision == 100 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)

ctx = get_context()

assert (ctx.precision == 100 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)

b = ctx.copy()
b.precision = 200

assert (b.precision == 200 and b.emax == 1073741823 and
b.emin == -1073741823 and not b.subnormalize)
assert (ctx.precision == 100 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)

ctx = get_context()

assert (ctx.precision == 100 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)


def test_local_context():
set_context(context())

with local_context() as ctx:
assert ctx.precision == 53
ctx.precision += 20
assert ctx.precision == 73

ctx = get_context()

assert (ctx.precision == 53 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)

with local_context(ieee(64)) as ctx:
assert (ctx.precision == 53 and ctx.emax == 1024 and
ctx.emin == -1073 and ctx.subnormalize)

ctx = get_context()

assert (ctx.precision == 53 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)

with get_context() as ctx:
assert ctx.precision == 53
ctx.precision += 100
assert ctx.precision == 153

ctx = get_context()

assert (ctx.precision == 53 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)

with local_context(precision=200) as ctx:
assert ctx.precision == 200
ctx.precision += 100
assert ctx.precision == 300

ctx = get_context()

assert (ctx.precision == 53 and ctx.emax == 1073741823 and
ctx.emin == -1073741823 and not ctx.subnormalize)


def test_context_repr():
ctx = get_context()
assert repr(ctx) == \
"""context(precision=53, real_prec=Default, imag_prec=Default,\n\
round=RoundToNearest, real_round=Default, imag_round=Default,\n\
emax=1073741823, emin=-1073741823,\n subnormalize=False,\n\
trap_underflow=False, underflow=False,\n trap_overflow=False,\
overflow=False,\n trap_inexact=False, inexact=False,\n\
trap_invalid=False, invalid=False,\n trap_erange=False,\
erange=False,\n trap_divzero=False, divzero=False,\n\
allow_complex=False,\n rational_division=False,\n\
allow_release_gil=False)"""

ctx.real_prec = 100
ctx.imag_prec = 200
assert repr(ctx) == \
"""context(precision=53, real_prec=100, imag_prec=200,\n\
round=RoundToNearest, real_round=Default, imag_round=Default,\n\
emax=1073741823, emin=-1073741823,\n subnormalize=False,\n\
trap_underflow=False, underflow=False,\n trap_overflow=False,\
overflow=False,\n trap_inexact=False, inexact=False,\n\
trap_invalid=False, invalid=False,\n trap_erange=False,\
erange=False,\n trap_divzero=False, divzero=False,\n\
allow_complex=False,\n rational_division=False,\n\
allow_release_gil=False)"""
ctx.trap_invalid = True
assert repr(ctx) == \
"""context(precision=53, real_prec=100, imag_prec=200,\n\
round=RoundToNearest, real_round=Default, imag_round=Default,\n\
emax=1073741823, emin=-1073741823,\n subnormalize=False,\n\
trap_underflow=False, underflow=False,\n trap_overflow=False,\
overflow=False,\n trap_inexact=False, inexact=False,\n\
trap_invalid=True, invalid=False,\n trap_erange=False,\
erange=False,\n trap_divzero=False, divzero=False,\n\
allow_complex=False,\n rational_division=False,\n\
allow_release_gil=False)"""
pytest.raises(gmpy2.InvalidOperationError, lambda: mpfr('nan') % 123)
assert repr(ctx) == \
"""context(precision=53, real_prec=100, imag_prec=200,\n\
round=RoundToNearest, real_round=Default, imag_round=Default,\n\
emax=1073741823, emin=-1073741823,\n subnormalize=False,\n\
trap_underflow=False, underflow=False,\n trap_overflow=False,\
overflow=False,\n trap_inexact=False, inexact=False,\n\
trap_invalid=True, invalid=True,\n trap_erange=False,\
erange=False,\n trap_divzero=False, divzero=False,\n\
allow_complex=False,\n rational_division=False,\n\
allow_release_gil=False)"""
set_context(ieee(32))
ctx = get_context()
ctx.trap_underflow = True
c = mpc(0.1 + 0.1j)
pytest.raises(gmpy2.UnderflowResultError, lambda: c**201)
assert repr(ctx) == \
"""context(precision=24, real_prec=Default, imag_prec=Default,\n\
round=RoundToNearest, real_round=Default, imag_round=Default,\n\
emax=128, emin=-148,\n subnormalize=True,\n\
trap_underflow=True, underflow=True,\n trap_overflow=False,\
overflow=False,\n trap_inexact=False, inexact=True,\n\
trap_invalid=False, invalid=False,\n trap_erange=False,\
erange=False,\n trap_divzero=False, divzero=False,\n\
allow_complex=False,\n rational_division=False,\n\
allow_release_gil=False)"""
Loading

0 comments on commit 2183205

Please sign in to comment.