Skip to content

Commit 08f5ca3

Browse files
Add test_overwrite_copy_special to improve coverage
1 parent a3d31c8 commit 08f5ca3

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed

dpnp/tests/test_linalg.py

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,15 @@ def test_cholesky_errors(self):
279279

280280

281281
class TestCond:
282-
_norms = [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"]
282+
def setup_method(self):
283+
numpy.random.seed(70)
283284

284285
@pytest.mark.parametrize(
285-
"shape", [(0, 4, 4), (4, 0, 3, 3)], ids=["(0, 4, 4)", "(4, 0, 3, 3)"]
286+
"shape", [(0, 4, 4), (4, 0, 3, 3)], ids=["(0, 5, 3)", "(4, 0, 2, 3)"]
287+
)
288+
@pytest.mark.parametrize(
289+
"p", [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"]
286290
)
287-
@pytest.mark.parametrize("p", _norms)
288291
def test_empty(self, shape, p):
289292
a = numpy.empty(shape)
290293
ia = dpnp.array(a)
@@ -293,27 +296,26 @@ def test_empty(self, shape, p):
293296
expected = numpy.linalg.cond(a, p=p)
294297
assert_dtype_allclose(result, expected)
295298

296-
# TODO: uncomment once numpy 2.3.3 release is published
297-
# @testing.with_requires("numpy>=2.3.3")
298299
@pytest.mark.parametrize(
299300
"dtype", get_all_dtypes(no_none=True, no_bool=True)
300301
)
301302
@pytest.mark.parametrize(
302303
"shape", [(4, 4), (2, 4, 3, 3)], ids=["(4, 4)", "(2, 4, 3, 3)"]
303304
)
304-
@pytest.mark.parametrize("p", _norms)
305+
@pytest.mark.parametrize(
306+
"p", [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"]
307+
)
305308
def test_basic(self, dtype, shape, p):
306309
a = generate_random_numpy_array(shape, dtype)
307310
ia = dpnp.array(a)
308311

309312
result = dpnp.linalg.cond(ia, p=p)
310313
expected = numpy.linalg.cond(a, p=p)
311-
# TODO: remove when numpy#29333 is released
312-
if numpy_version() < "2.3.3":
313-
expected = expected.real
314314
assert_dtype_allclose(result, expected, factor=16)
315315

316-
@pytest.mark.parametrize("p", _norms)
316+
@pytest.mark.parametrize(
317+
"p", [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"]
318+
)
317319
def test_bool(self, p):
318320
a = numpy.array([[True, True], [True, False]])
319321
ia = dpnp.array(a)
@@ -322,7 +324,9 @@ def test_bool(self, p):
322324
expected = numpy.linalg.cond(a, p=p)
323325
assert_dtype_allclose(result, expected)
324326

325-
@pytest.mark.parametrize("p", _norms)
327+
@pytest.mark.parametrize(
328+
"p", [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"]
329+
)
326330
def test_nan_to_inf(self, p):
327331
a = numpy.zeros((2, 2))
328332
ia = dpnp.array(a)
@@ -340,7 +344,9 @@ def test_nan_to_inf(self, p):
340344
else:
341345
assert_raises(dpnp.linalg.LinAlgError, dpnp.linalg.cond, ia, p=p)
342346

343-
@pytest.mark.parametrize("p", _norms)
347+
@pytest.mark.parametrize(
348+
"p", [None, -dpnp.inf, -2, -1, 1, 2, dpnp.inf, "fro"]
349+
)
344350
@pytest.mark.parametrize(
345351
"stride",
346352
[(-2, -3, 2, -2), (-2, 4, -4, -4), (2, 3, 4, 4), (-1, 3, 3, -3)],
@@ -352,23 +358,21 @@ def test_nan_to_inf(self, p):
352358
],
353359
)
354360
def test_strided(self, p, stride):
355-
A = generate_random_numpy_array(
356-
(6, 8, 10, 10), seed_value=70, low=0, high=1
357-
)
358-
iA = dpnp.array(A)
361+
A = numpy.random.rand(6, 8, 10, 10)
362+
B = dpnp.asarray(A)
359363
slices = tuple(slice(None, None, stride[i]) for i in range(A.ndim))
360-
a, ia = A[slices], iA[slices]
364+
a = A[slices]
365+
b = B[slices]
361366

362-
result = dpnp.linalg.cond(ia, p=p)
367+
result = dpnp.linalg.cond(b, p=p)
363368
expected = numpy.linalg.cond(a, p=p)
364369
assert_dtype_allclose(result, expected, factor=24)
365370

366-
@pytest.mark.parametrize("xp", [dpnp, numpy])
367-
def test_error(self, xp):
371+
def test_error(self):
368372
# cond is not defined on empty arrays
369-
a = xp.empty((2, 0))
373+
ia = dpnp.empty((2, 0))
370374
with pytest.raises(ValueError):
371-
xp.linalg.cond(a, p=1)
375+
dpnp.linalg.cond(ia, p=1)
372376

373377

374378
class TestDet:
@@ -1949,6 +1953,33 @@ def test_overwrite_copy(self, dtype):
19491953

19501954
assert_allclose(LU, PA, rtol=1e-6, atol=1e-6)
19511955

1956+
def test_overwrite_copy_special(self):
1957+
# F-contig but dtype != res_type
1958+
a1 = dpnp.array([[4, 3], [6, 3]], dtype=dpnp.int32, order="F")
1959+
a1_orig = a1.copy()
1960+
1961+
# F-contig, match dtype but read-only input
1962+
a2 = dpnp.array(
1963+
[[4, 3], [6, 3]], dtype=dpnp.default_float_type(), order="F"
1964+
)
1965+
a2_orig = a2.copy()
1966+
a2.flags["WRITABLE"] = False
1967+
1968+
for a_dp, a_orig in zip((a1, a1), (a1_orig, a2_orig)):
1969+
lu, piv = dpnp.linalg.lu_factor(
1970+
a_dp, overwrite_a=True, check_finite=False
1971+
)
1972+
1973+
assert lu is not a_dp
1974+
assert lu.flags["F_CONTIGUOUS"] is True
1975+
1976+
L, U = self._split_lu(lu, 2, 2)
1977+
PA = self._apply_pivots_rows(
1978+
a_orig.astype(L.dtype, copy=False), piv
1979+
)
1980+
LU = L @ U
1981+
assert_allclose(LU, PA, rtol=1e-6, atol=1e-6)
1982+
19521983
@pytest.mark.parametrize("shape", [(0, 0), (0, 2), (2, 0)])
19531984
def test_empty_inputs(self, shape):
19541985
a_dp = dpnp.empty(shape, dtype=dpnp.default_float_type(), order="F")

0 commit comments

Comments
 (0)