test_array_utils.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. import numpy as np
  2. from numpy.lib import array_utils
  3. from numpy.testing import assert_equal
  4. class TestByteBounds:
  5. def test_byte_bounds(self):
  6. # pointer difference matches size * itemsize
  7. # due to contiguity
  8. a = np.arange(12).reshape(3, 4)
  9. low, high = array_utils.byte_bounds(a)
  10. assert_equal(high - low, a.size * a.itemsize)
  11. def test_unusual_order_positive_stride(self):
  12. a = np.arange(12).reshape(3, 4)
  13. b = a.T
  14. low, high = array_utils.byte_bounds(b)
  15. assert_equal(high - low, b.size * b.itemsize)
  16. def test_unusual_order_negative_stride(self):
  17. a = np.arange(12).reshape(3, 4)
  18. b = a.T[::-1]
  19. low, high = array_utils.byte_bounds(b)
  20. assert_equal(high - low, b.size * b.itemsize)
  21. def test_strided(self):
  22. a = np.arange(12)
  23. b = a[::2]
  24. low, high = array_utils.byte_bounds(b)
  25. # the largest pointer address is lost (even numbers only in the
  26. # stride), and compensate addresses for striding by 2
  27. assert_equal(high - low, b.size * 2 * b.itemsize - b.itemsize)