()
| 70 | |
| 71 | @pytest.mark.skipif(not hasattr(m, "shape_span"), reason="std::span not available") |
| 72 | def 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( |
nothing calls this directly
no test coverage detected