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