summaryrefslogtreecommitdiff
path: root/tests/memoryview/relaxed_strides.pyx
blob: 07e6164dd0eb18914f893a48373b37cf67138264 (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
# mode: run
# tag: numpy

"""
Test accepting NumPy arrays with arbitrary strides for zero- or one-sized
dimensions.

Thanks to Nathaniel Smith and Sebastian Berg.

See also:

    Mailing list threads:
      http://thread.gmane.org/gmane.comp.python.cython.devel/14762
      http://thread.gmane.org/gmane.comp.python.cython.devel/14634

    Detailed discussion of the difference between numpy/cython's current
    definition of "contiguity", and the correct definition:
      http://thread.gmane.org/gmane.comp.python.cython.devel/14634/focus=14640

    The PR implementing NPY_RELAXED_STRIDES_CHECKING:
      https://github.com/numpy/numpy/pull/3162

    Another test case:
      https://github.com/numpy/numpy/issues/2956
"""

import numpy as np

numpy_version = np.__version__.split('.')[:2]
try:
    numpy_version = tuple(map(int, numpy_version))
except ValueError:
    numpy_version = (20, 0)

NUMPY_HAS_RELAXED_STRIDES = (
    numpy_version < (1, 8) or
    np.ones((10, 1), order="C").flags.f_contiguous)


def test_one_sized(array):
    """
    >>> contig = np.ascontiguousarray(np.arange(10, dtype=np.double)[::100])
    >>> test_one_sized(contig)[0]
    1.0
    >>> a = np.arange(10, dtype=np.double)[::100]
    >>> if NUMPY_HAS_RELAXED_STRIDES: print(test_one_sized(a)[0])
    ... else: print(1.0)
    1.0
    """
    cdef double[::1] a = array
    a[0] += 1.
    return array


def test_zero_sized(array):
    """
    >>> contig = np.ascontiguousarray(np.arange(10, dtype=np.double)[100:200:10])
    >>> _ = test_zero_sized(contig)

    >>> a = np.arange(10, dtype=np.double)[100:200:10]
    >>> if NUMPY_HAS_RELAXED_STRIDES: _ = test_zero_sized(a)
    """
    cdef double[::1] a = array
    return a

def test_zero_sized_multidim_ccontig(array):
    """
    >>> contig = np.ascontiguousarray(np.zeros((4,4,4))[::2, 2:2, ::2])
    >>> _ = test_zero_sized_multidim_ccontig(contig)

    >>> a = np.zeros((4,4,4))[::2, 2:2, ::2]
    >>> if NUMPY_HAS_RELAXED_STRIDES: _ = test_zero_sized_multidim_ccontig(a)
    """
    cdef double[:, :, ::1] a = array
    return a

def test_zero_sized_multidim_fcontig(array):
    """
    >>> contig = np.ascontiguousarray(np.zeros((4,4,4))[::2, 2:2, ::2])
    >>> _ = test_zero_sized_multidim_fcontig(contig)

    >>> a = np.zeros((4,4,4))[::2, 2:2, ::2]
    >>> if NUMPY_HAS_RELAXED_STRIDES: _ = test_zero_sized_multidim_fcontig(a)
    """
    cdef double[::1, :, :] a = array
    return a