Commit e6f478e6 authored by Christoph Groth's avatar Christoph Groth
Browse files

Fix overflowing test on Windows

The test assumed incorrectly that Python’s sys.maxsize is the size of
the C long type.  In particular, this is not true on 64 bit Windows.
parent 0c9ebf9d
Pipeline #45318 passed with stages
in 7 minutes and 15 seconds
...@@ -15,6 +15,7 @@ from pytest import raises ...@@ -15,6 +15,7 @@ from pytest import raises
import numpy as np import numpy as np
from numpy.testing import assert_equal, assert_almost_equal from numpy.testing import assert_equal, assert_almost_equal
import sys import sys
import ctypes
import random import random
...@@ -166,24 +167,33 @@ def test_conversion(): ...@@ -166,24 +167,33 @@ def test_conversion():
assert isinstance(dest[0], dest_dtype) assert isinstance(dest[0], dest_dtype)
assert src == dest assert src == dest
# Check correct overflow detection. We assume a typical architecture: # Determine maximum value of the C "long" type. This is different from
# sys.maxsize is also the maximum size of an integer held in a tinyarray # sys.maxsize, notably on 64 bit Windows.
# array, and that Python floats are double-precision IEEE numbers. maxlong = 2 ** (ctypes.sizeof(ctypes.c_long) * 8 - 1) - 1
for n in [10**100, -10**100, 123 * 10**20, -2 * sys.maxsize,
sys.maxsize + 1, np.array(sys.maxsize + 1), # Check correct overflow detection when integers are used to initialize
-sys.maxsize - 2]: # integer tinyarrays.
for n in [10**100, -10**100, 123 * 10**20, -2 * maxlong,
maxlong + 1, np.array(maxlong + 1),
-maxlong - 2]:
raises(OverflowError, ta.array, n, int) raises(OverflowError, ta.array, n, int)
# Check that values just below the threshold of overflow work. # Same as above, but check that values just under the threshold of overflow
for n in [sys.maxsize, np.array(sys.maxsize), # do work.
-sys.maxsize - 1, np.array(-sys.maxsize - 1)]: for n in [maxlong, np.array(maxlong),
-maxlong - 1, np.array(-maxlong - 1)]:
ta.array(n, int) ta.array(n, int)
# If tinyarray integers are longer than 32 bit, numbers around the maximal # Check correct overflow detection when floating point numbers are used to
# and minimal values cannot be represented exactly as double precision # initialize integer tinyarrays.
# floating point numbers. Check correct overflow detection also in this #
# case. # Correct overflow detection is tricky when tinyarray integers are 64 bit,
n = sys.maxsize + 1 # since the distance between adjacent floating point numbers is larger than
# one for numbers corresponding to large integers.
#
# The following assumes that Python floats are common double-precision IEEE
# numbers.
n = maxlong + 1
for dtype in [float, np.float64, np.float32]: for dtype in [float, np.float64, np.float32]:
# The following assumes that n can be represented exactly. This should # The following assumes that n can be represented exactly. This should
# be true for typical (all?) architectures. # be true for typical (all?) architectures.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment