MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_shape_strides_span

Function test_shape_strides_span

tests/test_numpy_array.py:72–107  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

70
71@pytest.mark.skipif(not hasattr(m, "shape_span"), reason="std::span not available")
72def test_shape_strides_span():
73 # Test 0-dimensional array (scalar)
74 a = np.array(42, "f8")
75 assert m.ndim(a) == 0
76 assert m.shape_span(a) == []
77 assert m.strides_span(a) == []
78
79 # Test 1-dimensional array
80 a = np.array([1, 2, 3, 4], "u2")
81 assert m.ndim(a) == 1
82 assert m.shape_span(a) == [4]
83 assert m.strides_span(a) == [2]
84
85 # Test 2-dimensional array
86 a = np.array([[1, 2, 3], [4, 5, 6]], "u2").view()
87 a.flags.writeable = False
88 assert m.ndim(a) == 2
89 assert m.shape_span(a) == [2, 3]
90 assert m.strides_span(a) == [6, 2]
91
92 # Test 3-dimensional array
93 a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], "i4")
94 assert m.ndim(a) == 3
95 assert m.shape_span(a) == [2, 2, 2]
96 # Verify spans match regular shape/strides
97 assert list(m.shape_span(a)) == list(m.shape(a))
98 assert list(m.strides_span(a)) == list(m.strides(a))
99
100 # Test that spans can be used to construct new arrays
101 original = np.array([[1, 2, 3], [4, 5, 6]], "f4")
102 new_array = m.array_from_spans(original)
103 assert new_array.shape == original.shape
104 assert new_array.strides == original.strides
105 assert new_array.dtype == original.dtype
106 # Verify data is shared (since we pass the same data pointer)
107 np.testing.assert_array_equal(new_array, original)
108
109
110@pytest.mark.parametrize(

Callers

nothing calls this directly

Calls 8

listClass · 0.85
arrayMethod · 0.80
shape_spanMethod · 0.80
strides_spanMethod · 0.80
stridesMethod · 0.80
ndimMethod · 0.45
viewMethod · 0.45
shapeMethod · 0.45

Tested by

no test coverage detected