test_byte_bounds.py 2.7 KB
Newer Older
root's avatar
root committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import cupy
from cupy import testing


class TestByteBounds:

    @testing.for_all_dtypes()
    def test_1d_contiguous(self, dtype):
        a = cupy.zeros(12, dtype=dtype)
        itemsize = a.itemsize
        a_low = a.data.ptr
        a_high = a.data.ptr + 12 * itemsize
        assert cupy.byte_bounds(a) == (a_low, a_high)

    @testing.for_all_dtypes()
    def test_2d_contiguous(self, dtype):
        a = cupy.zeros((4, 7), dtype=dtype)
        itemsize = a.itemsize
        a_low = a.data.ptr
        a_high = a.data.ptr + 4 * 7 * itemsize
        assert cupy.byte_bounds(a) == (a_low, a_high)

    @testing.for_all_dtypes()
    def test_1d_noncontiguous_pos_stride(self, dtype):
        a = cupy.zeros(12, dtype=dtype)
        itemsize = a.itemsize
        b = a[::2]
        b_low = b.data.ptr
        b_high = b.data.ptr + 11 * itemsize  # a[10]
        assert cupy.byte_bounds(b) == (b_low, b_high)

    @testing.for_all_dtypes()
    def test_2d_noncontiguous_pos_stride(self, dtype):
        a = cupy.zeros((4, 7), dtype=dtype)
        b = a[::2, ::2]
        itemsize = b.itemsize
        b_low = a.data.ptr
        b_high = b.data.ptr + 3 * 7 * itemsize  # a[2][6]
        assert cupy.byte_bounds(b) == (b_low, b_high)

    @testing.for_all_dtypes()
    def test_1d_contiguous_neg_stride(self, dtype):
        a = cupy.zeros(12, dtype=dtype)
        b = a[::-1]
        itemsize = b.itemsize
        b_low = b.data.ptr - 11 * itemsize
        b_high = b.data.ptr + 1 * itemsize
        assert cupy.byte_bounds(b) == (b_low, b_high)

    @testing.for_all_dtypes()
    def test_2d_noncontiguous_neg_stride(self, dtype):
        a = cupy.zeros((4, 7), dtype=dtype)
        b = a[::-2, ::-2]  # strides = (-56, -8), shape = (2, 4)
        itemsize = b.itemsize
        b_low = b.data.ptr - 2 * 7 * itemsize * \
            (2 - 1) - 2 * itemsize * (4 - 1)
        b_high = b.data.ptr + 1 * itemsize
        assert cupy.byte_bounds(b) == (b_low, b_high)

    @testing.for_all_dtypes()
    def test_2d_noncontiguous_posneg_stride_1(self, dtype):
        a = cupy.zeros((4, 7), dtype=dtype)
        b = a[::1, ::-1]  # strides = (28, -4), shape=(4, 7)
        itemsize = b.itemsize
        b_low = b.data.ptr - itemsize * (7 - 1)
        b_high = b.data.ptr + 1 * itemsize + 7 * itemsize * (4 - 1)
        assert cupy.byte_bounds(b) == (b_low, b_high)

    @testing.for_all_dtypes()
    def test_2d_noncontiguous_posneg_stride_2(self, dtype):
        a = cupy.zeros((4, 7), dtype=dtype)
        b = a[::2, ::-2]  # strides = (56, -8), shape=(2, 4)
        itemsize = b.itemsize
        b_low = b.data.ptr - 2 * itemsize * (4 - 1)
        b_high = b.data.ptr + 1 * itemsize + 2 * 7 * itemsize * (2 - 1)
        assert cupy.byte_bounds(b) == (b_low, b_high)