Skip to content

Commit

Permalink
TST: Fixup tests for strict Python integer conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
seberg committed Oct 5, 2022
1 parent afcc560 commit eec99b4
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion numpy/core/tests/test_array_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def test_default_dtype_instance(self, dtype_char):
@pytest.mark.parametrize("dtype", np.typecodes["Integer"])
@pytest.mark.parametrize(["scalar", "error"],
[(np.float64(np.nan), ValueError),
(np.ulonglong(-1), OverflowError)])
(np.array(-1).astype(np.ulonglong)[()], OverflowError)])
def test_scalar_to_int_coerce_does_not_cast(self, dtype, scalar, error):
"""
Signed integers are currently different in that they do not cast other
Expand Down
6 changes: 6 additions & 0 deletions numpy/core/tests/test_casting_unittests.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ def get_data(self, dtype1, dtype2):

for i, value in enumerate(values):
# Use item assignment to ensure this is not using casting:
if value < 0 and dtype1.kind == "u":
# Manually rollover unsigned integers (-1 -> int.max)
value = value + np.iinfo(dtype1).max + 1
arr1[i] = value

if dtype2 is None:
Expand All @@ -185,6 +188,9 @@ def get_data(self, dtype1, dtype2):

for i, value in enumerate(values):
# Use item assignment to ensure this is not using casting:
if value < 0 and dtype2.kind == "u":
# Manually rollover unsigned integers (-1 -> int.max)
value = value + np.iinfo(dtype2).max + 1
arr2[i] = value

return arr1, arr2, values
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ def test_union_struct(self):
dt2 = np.dtype({'names':['f2', 'f0', 'f1'],
'formats':['<u4', '<u2', '<u2'],
'offsets':[4, 0, 2]}, align=True)
vals = [(0, 1, 2), (3, -1, 4)]
vals2 = [(0, 1, 2), (3, -1, 4)]
vals = [(0, 1, 2), (3, 2**15-1, 4)]
vals2 = [(0, 1, 2), (3, 2**15-1, 4)]
a = np.array(vals, dt)
b = np.array(vals2, dt2)
assert_equal(a.astype(dt2), b)
Expand Down
7 changes: 5 additions & 2 deletions numpy/core/tests/test_einsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def test_einsum_views(self):

@np._no_nep50_warning()
def check_einsum_sums(self, dtype, do_opt=False):
dtype = np.dtype(dtype)
# Check various sums. Does many sizes to exercise unrolled loops.

# sum(a, axis=-1)
Expand Down Expand Up @@ -442,9 +443,11 @@ def check_einsum_sums(self, dtype, do_opt=False):
axes=([1, 0], [0, 1])).astype(dtype))

# logical_and(logical_and(a!=0, b!=0), c!=0)
a = np.array([1, 3, -2, 0, 12, 13, 0, 1], dtype=dtype)
b = np.array([0, 3.5, 0., -2, 0, 1, 3, 12], dtype=dtype)
neg_val = -2 if dtype.kind != "u" else np.iinfo(dtype).max - 1
a = np.array([1, 3, neg_val, 0, 12, 13, 0, 1], dtype=dtype)
b = np.array([0, 3.5, 0., neg_val, 0, 1, 3, 12], dtype=dtype)
c = np.array([True, True, False, True, True, False, True, True])

assert_equal(np.einsum("i,i,i->i", a, b, c,
dtype='?', casting='unsafe', optimize=do_opt),
np.logical_and(np.logical_and(a != 0, b != 0), c != 0))
Expand Down
4 changes: 3 additions & 1 deletion numpy/core/tests/test_getlimits.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def test_basic(self):
def test_unsigned_max(self):
types = np.sctypes['uint']
for T in types:
assert_equal(iinfo(T).max, T(-1))
with np.errstate(over="ignore"):
max_calculated = T(0) - T(1)
assert_equal(iinfo(T).max, max_calculated)

class TestRepr:
def test_iinfo_repr(self):
Expand Down
7 changes: 4 additions & 3 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -5020,6 +5020,8 @@ def test_ip_types(self):
for types in np.sctypes.values():
for T in types:
if T not in unchecked_types:
if val < 0 and np.dtype(T).kind == "u":
val = np.iinfo(T).max - 99
self.tst_basic(x.copy().astype(T), T, mask, val)

# Also test string of a length which uses an untypical length
Expand Down Expand Up @@ -7234,9 +7236,8 @@ def test_3d_tensor(self):
[2630, 2910, 3190]],

[[2198, 2542, 2886],
[3230, 3574, 3918]]]],
dtype=dt
)
[3230, 3574, 3918]]]]
).astype(dt)
assert_equal(np.inner(a, b), desired)
assert_equal(np.inner(b, a).transpose(2,3,0,1), desired)

Expand Down
11 changes: 5 additions & 6 deletions numpy/core/tests/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2824,12 +2824,11 @@ def setup(self):
def compare_array_value(self, dz, value, fill_value):
if value is not None:
if fill_value:
try:
z = dz.dtype.type(value)
except OverflowError:
pass
else:
assert_(np.all(dz == z))
# Conversion is close to what np.full_like uses
# but we may want to convert directly in the future
# which may result in errors (where this does not).
z = np.array(value).astype(dz.dtype)
assert_(np.all(dz == z))
else:
assert_(np.all(dz == value))

Expand Down
6 changes: 3 additions & 3 deletions numpy/core/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1922,15 +1922,15 @@ def test_pickle_string_overwrite(self):
# Check that loads does not clobber interned strings
s = re.sub("a(.)", "\x01\\1", "a_")
assert_equal(s[0], "\x01")
data[0] = 0xbb
data[0] = 0x6a
s = re.sub("a(.)", "\x01\\1", "a_")
assert_equal(s[0], "\x01")

def test_pickle_bytes_overwrite(self):
for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
data = np.array([1], dtype='b')
data = pickle.loads(pickle.dumps(data, protocol=proto))
data[0] = 0xdd
data[0] = 0x7d
bytestring = "\x01 ".encode('ascii')
assert_equal(bytestring[0:1], '\x01'.encode('ascii'))

Expand All @@ -1945,7 +1945,7 @@ def test_pickle_py2_array_latin1_hack(self):
b"p13\ntp14\nb.")
# This should work:
result = pickle.loads(data, encoding='latin1')
assert_array_equal(result, np.array([129], dtype='b'))
assert_array_equal(result, np.array([129]).astype('b'))
# Should not segfault:
assert_raises(Exception, pickle.loads, data, encoding='koi8-r')

Expand Down
3 changes: 2 additions & 1 deletion numpy/core/tests/test_scalar_ctors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def test_intp(self):
assert_equal(1024, np.intp(1024))

def test_uint64_from_negative(self):
assert_equal(np.uint64(-2), np.uint64(18446744073709551614))
with pytest.warns(DeprecationWarning):
assert_equal(np.uint64(-2), np.uint64(18446744073709551614))


int_types = [np.byte, np.short, np.intc, np.int_, np.longlong]
Expand Down
7 changes: 4 additions & 3 deletions numpy/core/tests/test_scalarmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ def test_int_from_long(self):

def test_iinfo_long_values(self):
for code in 'bBhH':
res = np.array(np.iinfo(code).max + 1, dtype=code)
with pytest.warns(DeprecationWarning):
res = np.array(np.iinfo(code).max + 1, dtype=code)
tgt = np.iinfo(code).min
assert_(res == tgt)

Expand Down Expand Up @@ -767,7 +768,7 @@ def test_shift_all_bits(self, type_code, op):
nbits = dt.itemsize * 8
for val in [5, -5]:
for shift in [nbits, nbits + 4]:
val_scl = dt.type(val)
val_scl = np.array(val).astype(dt)[()]
shift_scl = dt.type(shift)
res_scl = op(val_scl, shift_scl)
if val_scl < 0 and op is operator.rshift:
Expand All @@ -777,7 +778,7 @@ def test_shift_all_bits(self, type_code, op):
assert_equal(res_scl, 0)

# Result on scalars should be the same as on arrays
val_arr = np.array([val]*32, dtype=dt)
val_arr = np.array([val_scl]*32, dtype=dt)
shift_arr = np.array([shift]*32, dtype=dt)
res_arr = op(val_arr, shift_arr)
assert_equal(res_arr, res_scl)
Expand Down
26 changes: 13 additions & 13 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,24 +366,24 @@ def test_division_int(self):
np.sctypes['int'] + np.sctypes['uint'], (
(
# dividend
"np.arange(fo.max-lsize, fo.max, dtype=dtype),"
"np.array(range(fo.max-lsize, fo.max)).astype(dtype),"
# divisors
"np.arange(lsize, dtype=dtype),"
"np.arange(lsize).astype(dtype),"
# scalar divisors
"range(15)"
),
(
# dividend
"np.arange(fo.min, fo.min+lsize, dtype=dtype),"
"np.arange(fo.min, fo.min+lsize).astype(dtype),"
# divisors
"np.arange(lsize//-2, lsize//2, dtype=dtype),"
"np.arange(lsize//-2, lsize//2).astype(dtype),"
# scalar divisors
"range(fo.min, fo.min + 15)"
), (
# dividend
"np.arange(fo.max-lsize, fo.max, dtype=dtype),"
"np.array(range(fo.max-lsize, fo.max)).astype(dtype),"
# divisors
"np.arange(lsize, dtype=dtype),"
"np.arange(lsize).astype(dtype),"
# scalar divisors
"[1,3,9,13,neg, fo.min+1, fo.min//2, fo.max//3, fo.max//4]"
)
Expand Down Expand Up @@ -450,9 +450,9 @@ def test_division_int_boundary(self, dtype, ex_val):
@pytest.mark.parametrize("dtype,ex_val", itertools.product(
np.sctypes['int'] + np.sctypes['uint'], (
"np.array([fo.max, 1, 2, 1, 1, 2, 3], dtype=dtype)",
"np.array([fo.min, 1, -2, 1, 1, 2, -3], dtype=dtype)",
"np.array([fo.min, 1, -2, 1, 1, 2, -3]).astype(dtype)",
"np.arange(fo.min, fo.min+(100*10), 10, dtype=dtype)",
"np.arange(fo.max-(100*7), fo.max, 7, dtype=dtype)",
"np.array(range(fo.max-(100*7), fo.max, 7)).astype(dtype)",
)
))
def test_division_int_reduce(self, dtype, ex_val):
Expand All @@ -472,7 +472,7 @@ def test_division_int_reduce(self, dtype, ex_val):
with np.errstate(divide='raise', over='raise'):
with pytest.raises(FloatingPointError,
match="divide by zero encountered in reduce"):
np.floor_divide.reduce(np.arange(-100, 100, dtype=dtype))
np.floor_divide.reduce(np.arange(-100, 100).astype(dtype))
if fo.min:
with pytest.raises(FloatingPointError,
match='overflow encountered in reduce'):
Expand Down Expand Up @@ -2328,7 +2328,7 @@ class TestBitwiseUFuncs:
def test_values(self):
for dt in self.bitwise_types:
zeros = np.array([0], dtype=dt)
ones = np.array([-1], dtype=dt)
ones = np.array([-1]).astype(dt)
msg = "dt = '%s'" % dt.char

assert_equal(np.bitwise_not(zeros), ones, err_msg=msg)
Expand All @@ -2352,7 +2352,7 @@ def test_values(self):
def test_types(self):
for dt in self.bitwise_types:
zeros = np.array([0], dtype=dt)
ones = np.array([-1], dtype=dt)
ones = np.array([-1]).astype(dt)
msg = "dt = '%s'" % dt.char

assert_(np.bitwise_not(zeros).dtype == dt, msg)
Expand All @@ -2370,7 +2370,7 @@ def test_reduction(self):

for dt in self.bitwise_types:
zeros = np.array([0], dtype=dt)
ones = np.array([-1], dtype=dt)
ones = np.array([-1]).astype(dt)
for f in binary_funcs:
msg = "dt: '%s', f: '%s'" % (dt, f)
assert_equal(f.reduce(zeros), zeros, err_msg=msg)
Expand All @@ -2382,7 +2382,7 @@ def test_reduction(self):
empty = np.array([], dtype=dt)
for f in binary_funcs:
msg = "dt: '%s', f: '%s'" % (dt, f)
tgt = np.array(f.identity, dtype=dt)
tgt = np.array(f.identity).astype(dt)
res = f.reduce(empty)
assert_equal(res, tgt, err_msg=msg)
assert_(res.dtype == tgt.dtype, msg)
Expand Down
2 changes: 1 addition & 1 deletion numpy/f2py/tests/test_return_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def check_function(self, t, tname):
assert abs(t(array(23 + 4j, "F")) - (23 + 4j)) <= err
assert abs(t(array([234])) - 234.0) <= err
assert abs(t(array([[234]])) - 234.0) <= err
assert abs(t(array([234], "b")) + 22.0) <= err
assert abs(t(array([234]).astype("b")) + 22.0) <= err
assert abs(t(array([234], "h")) - 234.0) <= err
assert abs(t(array([234], "i")) - 234.0) <= err
assert abs(t(array([234], "l")) - 234.0) <= err
Expand Down
2 changes: 1 addition & 1 deletion numpy/f2py/tests/test_return_real.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def check_function(self, t, tname):
assert abs(t(array(234)) - 234.0) <= err
assert abs(t(array([234])) - 234.0) <= err
assert abs(t(array([[234]])) - 234.0) <= err
assert abs(t(array([234], "b")) + 22) <= err
assert abs(t(array([234]).astype("b")) + 22) <= err
assert abs(t(array([234], "h")) - 234.0) <= err
assert abs(t(array([234], "i")) - 234.0) <= err
assert abs(t(array([234], "l")) - 234.0) <= err
Expand Down

0 comments on commit eec99b4

Please sign in to comment.