Skip to content

Commit 66e62b8

Browse files
authored
Add test coverage for the reshape copy keyword (#3020)
This PR adds test coverage for the `copy` keyword of `reshape`/`ndarray.reshape` to the third-party `manipulation_tests/test_shape.py` suite.
1 parent 9b62c27 commit 66e62b8

1 file changed

Lines changed: 51 additions & 6 deletions

File tree

dpnp/tests/third_party/cupy/manipulation_tests/test_shape.py

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
from dpnp.tests.helper import has_support_aspect64
88
from dpnp.tests.third_party.cupy import testing
99

10+
for_all_copy_args = pytest.mark.parametrize(
11+
"copy",
12+
[
13+
pytest.param(None, id="copy-None"),
14+
pytest.param(False, id="copy-False"),
15+
pytest.param(True, id="copy-True"),
16+
],
17+
)
18+
1019

1120
@pytest.mark.parametrize("shape", [(2, 3), (), (4,)])
1221
class TestShape:
@@ -56,6 +65,37 @@ def test_nocopy_reshape_with_order(self, xp, dtype, order):
5665
b[1] = 1
5766
return a
5867

68+
@testing.with_requires("numpy>=2.1")
69+
@testing.for_orders("CFA")
70+
@testing.for_all_dtypes()
71+
@for_all_copy_args
72+
@testing.numpy_cupy_array_equal(accept_error=ValueError)
73+
def test_copy_arg_reshape(self, xp, dtype, order, copy):
74+
a = xp.zeros((2, 3, 4), dtype=dtype)
75+
b = a.reshape(4, 3, 2, order=order, copy=copy)
76+
a[1] = 1
77+
return b
78+
79+
@testing.with_requires("numpy>=2.1")
80+
def test_copy_arg_str_reshape_raises(self):
81+
for xp in (numpy, cupy):
82+
with pytest.raises(
83+
ValueError,
84+
match="strings are not allowed for|Keyword 'copy' not recognized",
85+
):
86+
a = xp.zeros((2, 3, 4), dtype=numpy.int32)
87+
a.reshape(4, 3, 2, copy="False")
88+
89+
@testing.with_requires("numpy>=2.1")
90+
def test_copy_arg_false_reshape_raises(self):
91+
for xp in (numpy, cupy):
92+
a = xp.zeros((2, 3, 4))
93+
with pytest.raises(
94+
ValueError,
95+
match="Unable to avoid creating a copy|requires a copy",
96+
):
97+
a.transpose(2, 0, 1).reshape(4, 3, 2, order="F", copy=False)
98+
5999
@testing.for_orders("CFA")
60100
@testing.numpy_cupy_array_equal()
61101
def test_transposed_reshape2(self, xp, order):
@@ -77,7 +117,7 @@ def test_reshape_with_multiple_unknown_dimensions(self):
77117
def test_reshape_with_changed_arraysize(self):
78118
for xp in (numpy, cupy):
79119
a = testing.shaped_arange((2, 3, 4), xp)
80-
with pytest.raises(ValueError):
120+
with pytest.raises(ValueError, match="not reshape"):
81121
a.reshape(2, 4, 4)
82122

83123
def test_reshape_invalid_order(self):
@@ -89,13 +129,13 @@ def test_reshape_invalid_order(self):
89129
def test_reshape_zerosize_invalid(self):
90130
for xp in (numpy, cupy):
91131
a = xp.zeros((0,))
92-
with pytest.raises(ValueError):
132+
with pytest.raises(ValueError, match="not reshape"):
93133
a.reshape(())
94134

95135
def test_reshape_zerosize_invalid_unknown(self):
96136
for xp in (numpy, cupy):
97137
a = xp.zeros((0,))
98-
with pytest.raises(ValueError):
138+
with pytest.raises(ValueError, match="not reshape array of size"):
99139
a.reshape((-1, 0))
100140

101141
@testing.numpy_cupy_array_equal(type_check=has_support_aspect64())
@@ -119,11 +159,13 @@ def test_reshape_zerosize2(self, xp, order):
119159
assert b.base is a
120160
return b
121161

162+
@testing.with_requires("numpy>=2.1")
122163
@testing.for_orders("CFA")
164+
@for_all_copy_args
123165
@testing.numpy_cupy_array_equal()
124-
def test_external_reshape(self, xp, order):
166+
def test_external_reshape(self, xp, order, copy):
125167
a = xp.zeros((8,), dtype=xp.float32)
126-
return xp.reshape(a, (1, 1, 1, 4, 1, 2), order=order)
168+
return xp.reshape(a, (1, 1, 1, 4, 1, 2), order=order, copy=copy)
127169

128170
def _test_ndim_limit(self, xp, ndim, dtype, order):
129171
idx = [1] * ndim
@@ -146,7 +188,10 @@ def test_ndim_limit1(self, xp, dtype, order):
146188
@testing.for_all_dtypes()
147189
def test_ndim_limit2(self, dtype, order):
148190
for xp in (numpy, cupy):
149-
with pytest.raises(ValueError):
191+
with pytest.raises(
192+
ValueError,
193+
match="maximum supported dimension for an ndarray is",
194+
):
150195
self._test_ndim_limit(xp, 65, dtype, order)
151196

152197

0 commit comments

Comments
 (0)