summaryrefslogtreecommitdiff
path: root/tests/run/numpy_attributes.pyx
blob: c77346a492d34260f0e538d06ae4b597774c7efa (plain)
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
77
78
79
80
81
82
83
84
85
86
87
# mode: run
# tag: numpy

cimport cython

import numpy as np
cimport numpy as cnp

cnp.import_array()


@cython.test_assert_path_exists(
    "//ReturnStatNode",
    "//ReturnStatNode//IndexNode",
    "//ReturnStatNode//IndexNode//SimpleCallNode",
)
@cython.test_fail_if_path_exists(
    "//ReturnStatNode//AttributeNode",
)
def access_shape():
    """
    >>> print(access_shape())
    10
    """
    cdef cnp.ndarray[double, ndim=2, mode='c'] array_in = \
        1e10 * np.ones((10, 10))

    return array_in.shape[0]


@cython.test_assert_path_exists(
    "//ReturnStatNode",
    "//ReturnStatNode//SimpleCallNode",
)
@cython.test_fail_if_path_exists(
    "//ReturnStatNode//AttributeNode",
)
def access_size():
    """
    >>> print(access_size())
    100
    """
    cdef cnp.ndarray[double, ndim=2, mode='c'] array_in = \
        1e10 * np.ones((10, 10))

    return array_in.size


@cython.test_assert_path_exists(
    "//ReturnStatNode",
    "//ReturnStatNode//IndexNode",
    "//ReturnStatNode//IndexNode//SimpleCallNode",
)
@cython.test_fail_if_path_exists(
    "//ReturnStatNode//AttributeNode",
)
def access_strides():
    """
    >>> x, y = access_strides()
    >>> print(x)
    80
    >>> print(y)
    8
    """
    cdef cnp.ndarray[double, ndim=2, mode='c'] array_in = \
        1e10 * np.ones((10, 10), dtype=np.float64)

    return (array_in.strides[0], array_in.strides[1])


@cython.test_assert_path_exists(
    "//ReturnStatNode",
    "//ReturnStatNode//PrimaryCmpNode",
    "//ReturnStatNode//PrimaryCmpNode//SimpleCallNode",
)
@cython.test_fail_if_path_exists(
    "//ReturnStatNode//AttributeNode",
)
def access_data():
    """
    >>> access_data()
    True
    """
    cdef cnp.ndarray[double, ndim=2, mode='c'] array_in = \
        1e10 * np.ones((10, 10), dtype=np.float64)

    return array_in.data is not NULL