Skip to content

Commit

Permalink
TST: Division overflow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ganesh-k13 committed May 14, 2022
1 parent b70ef06 commit 254dbd0
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,49 @@ def test_float_remainder_corner_cases(self):
assert_(np.isnan(fmod), 'dt: %s, fmod: %s' % (dt, rem))


class TestDivisionOverflows:
@pytest.mark.parametrize("dividend_dtype",
np.sctypes['int'])
@pytest.mark.parametrize("divisor_dtype",
np.sctypes['int'])
@pytest.mark.parametrize("operation",
[np.remainder, np.fmod, np.floor_divide])
def test_scalars(self, dividend_dtype, divisor_dtype, operation):
with np.errstate(divide='raise'):
# If dividend is a larger type than the divisor (`else` case),
# then, result will be a larger type than dividend and will not
# result in an overflow.
if np.dtype(dividend_dtype).itemsize >= np.dtype(
divisor_dtype).itemsize:
with pytest.raises(FloatingPointError):
operation(
dividend_dtype(np.iinfo(dividend_dtype).min),
divisor_dtype(-1)
)
else:
operation(
dividend_dtype(np.iinfo(dividend_dtype).min),
divisor_dtype(-1)
)

@pytest.mark.parametrize("dividend_dtype",
np.sctypes['int'])
@pytest.mark.parametrize("divisor_dtype",
np.sctypes['int'])
@pytest.mark.parametrize("operation",
[np.remainder, np.fmod, np.floor_divide])
def test_arrays(self, dividend_dtype, divisor_dtype, operation):
# Cover all edge cases in SIMD
arrays = [np.array(
[dividend_dtype(
np.iinfo(dividend_dtype).min)]*i
) for i in range(1, 129)]
with np.errstate(divide='raise'):
for a in arrays:
with pytest.raises(FloatingPointError):
operation(a, divisor_dtype(-1))


class TestCbrt:
def test_cbrt_scalar(self):
assert_almost_equal((np.cbrt(np.float32(-2.5)**3)), -2.5)
Expand Down

0 comments on commit 254dbd0

Please sign in to comment.