Skip to content

Commit 58d0f13

Browse files
committed
Update tests to cover new behavior
1 parent 883a654 commit 58d0f13

File tree

2 files changed

+67
-40
lines changed

2 files changed

+67
-40
lines changed

dpnp/tests/test_mathematical.py

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,37 +1480,34 @@ def test_boolean_array(self):
14801480
expected = numpy.nan_to_num(a)
14811481
assert_allclose(result, expected)
14821482

1483-
def test_errors(self):
1484-
ia = dpnp.array([0, 1, dpnp.nan, dpnp.inf, -dpnp.inf])
1485-
1486-
# unsupported type `a`
1487-
a = dpnp.asnumpy(ia)
1488-
assert_raises(TypeError, dpnp.nan_to_num, a)
1489-
1490-
# unsupported type `nan`
1491-
i_nan = dpnp.array(1)
1492-
assert_raises(TypeError, dpnp.nan_to_num, ia, nan=i_nan)
1483+
@pytest.mark.parametrize("kw_name", ["nan", "posinf", "neginf"])
1484+
@pytest.mark.parametrize("val", [[1, 2, -1, -2, 7], (7,), numpy.array(1)])
1485+
def test_nan_infs_array_like(self, kw_name, val):
1486+
a = numpy.array([0, 1, dpnp.nan, dpnp.inf, -dpnp.inf])
1487+
ia = dpnp.array(a)
14931488

1494-
# unsupported type `posinf`
1495-
i_posinf = dpnp.array(1)
1496-
assert_raises(TypeError, dpnp.nan_to_num, ia, posinf=i_posinf)
1489+
result = dpnp.nan_to_num(ia, **{kw_name: val})
1490+
expected = numpy.nan_to_num(a, **{kw_name: val})
1491+
assert_allclose(result, expected)
14971492

1498-
# unsupported type `neginf`
1499-
i_neginf = dpnp.array(1)
1500-
assert_raises(TypeError, dpnp.nan_to_num, ia, neginf=i_neginf)
1493+
@pytest.mark.parametrize("xp", [dpnp, numpy])
1494+
@pytest.mark.parametrize("kw_name", ["nan", "posinf", "neginf"])
1495+
def test_nan_infs_complex_dtype(self, xp, kw_name):
1496+
ia = xp.array([0, 1, xp.nan, xp.inf, -xp.inf])
1497+
with pytest.raises((TypeError, ValueError), match="complex.*type"):
1498+
xp.nan_to_num(ia, **{kw_name: 1j})
15011499

1502-
@pytest.mark.parametrize("kwarg", ["nan", "posinf", "neginf"])
1503-
@pytest.mark.parametrize("value", [1 - 0j, [1, 2], (1,)])
1504-
def test_errors_diff_types(self, kwarg, value):
1505-
ia = dpnp.array([0, 1, dpnp.nan, dpnp.inf, -dpnp.inf])
1506-
with pytest.raises(TypeError):
1507-
dpnp.nan_to_num(ia, **{kwarg: value})
1500+
def test_numpy_input_array(self):
1501+
a = numpy.array([0, 1, dpnp.nan, dpnp.inf, -dpnp.inf])
1502+
with pytest.raises(TypeError, match="must be any of supported type"):
1503+
dpnp.nan_to_num(a)
15081504

1509-
def test_error_readonly(self):
1510-
a = dpnp.array([0, 1, dpnp.nan, dpnp.inf, -dpnp.inf])
1511-
a.flags.writable = False
1512-
with pytest.raises(ValueError):
1513-
dpnp.nan_to_num(a, copy=False)
1505+
@pytest.mark.parametrize("xp", [dpnp, numpy])
1506+
def test_error_readonly(self, xp):
1507+
a = xp.array([0, 1, xp.nan, xp.inf, -xp.inf])
1508+
a.flags["W"] = False
1509+
with pytest.raises(ValueError, match="read-only"):
1510+
xp.nan_to_num(a, copy=False)
15141511

15151512
@pytest.mark.parametrize("copy", [True, False])
15161513
@pytest.mark.parametrize("dt", get_all_dtypes(no_bool=True, no_none=True))
@@ -1522,9 +1519,9 @@ def test_strided(self, copy, dt):
15221519
if dt.kind in "fc":
15231520
a[::4] = numpy.nan
15241521
ia[::4] = dpnp.nan
1522+
15251523
result = dpnp.nan_to_num(ia[::-2], copy=copy, nan=57.0)
15261524
expected = numpy.nan_to_num(a[::-2], copy=copy, nan=57.0)
1527-
15281525
assert_dtype_allclose(result, expected)
15291526

15301527

dpnp/tests/third_party/cupy/math_tests/test_misc.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from __future__ import annotations
2+
13
import numpy
24
import pytest
35

46
import dpnp as cupy
5-
from dpnp.tests.helper import has_support_aspect64
7+
from dpnp.tests.helper import has_support_aspect64, numpy_version
68
from dpnp.tests.third_party.cupy import testing
79

810

@@ -155,10 +157,7 @@ def test_external_clip4(self, dtype):
155157
# (min or max) as a keyword argument according to Python Array API.
156158
# In older versions of numpy, both arguments must be positional;
157159
# passing only one raises a TypeError.
158-
if (
159-
xp is numpy
160-
and numpy.lib.NumpyVersion(numpy.__version__) < "2.1.0"
161-
):
160+
if xp is numpy and numpy_version() < "2.1.0":
162161
with pytest.raises(TypeError):
163162
xp.clip(a, 3)
164163
else:
@@ -257,9 +256,10 @@ def test_nan_to_num_inf(self):
257256
def test_nan_to_num_nan(self):
258257
self.check_unary_nan("nan_to_num")
259258

260-
@testing.numpy_cupy_allclose(atol=1e-5, type_check=has_support_aspect64())
259+
@pytest.mark.skip("no scalar support")
260+
@testing.numpy_cupy_allclose(atol=1e-5)
261261
def test_nan_to_num_scalar_nan(self, xp):
262-
return xp.nan_to_num(xp.array(xp.nan))
262+
return xp.nan_to_num(xp.nan)
263263

264264
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
265265
def test_nan_to_num_inf_nan(self):
@@ -286,14 +286,44 @@ def test_nan_to_num_inplace(self, xp):
286286
return y
287287

288288
@pytest.mark.parametrize("kwarg", ["nan", "posinf", "neginf"])
289-
def test_nan_to_num_broadcast(self, kwarg):
289+
@testing.numpy_cupy_array_equal()
290+
def test_nan_to_num_broadcast_same_shapes(self, xp, kwarg):
291+
x = xp.asarray(
292+
[[0, 1, xp.nan, 4], [11, xp.inf, 12, 13]],
293+
dtype=cupy.default_float_type(),
294+
)
295+
y = xp.zeros((2, 4), dtype=x.dtype)
296+
return xp.nan_to_num(x, **{kwarg: y})
297+
298+
@pytest.mark.parametrize("kwarg", ["nan", "posinf", "neginf"])
299+
@testing.numpy_cupy_array_equal()
300+
def test_nan_to_num_broadcast_different_columns(self, xp, kwarg):
301+
x = xp.asarray(
302+
[[0, 1, xp.nan, 4], [11, xp.inf, 12, 13]],
303+
dtype=cupy.default_float_type(),
304+
)
305+
y = xp.zeros((2, 1), dtype=x.dtype)
306+
return xp.nan_to_num(x, **{kwarg: y})
307+
308+
@pytest.mark.parametrize("kwarg", ["nan", "posinf", "neginf"])
309+
@testing.numpy_cupy_array_equal()
310+
def test_nan_to_num_broadcast_different_rows(self, xp, kwarg):
311+
x = xp.asarray(
312+
[[0, 1, xp.nan, 4], [11, -xp.inf, 12, 13]],
313+
dtype=cupy.default_float_type(),
314+
)
315+
y = xp.zeros((1, 4), dtype=x.dtype)
316+
return xp.nan_to_num(x, **{kwarg: y})
317+
318+
@pytest.mark.parametrize("kwarg", ["nan", "posinf", "neginf"])
319+
def test_nan_to_num_broadcast_invalid_shapes(self, kwarg):
290320
for xp in (numpy, cupy):
291321
x = xp.asarray([0, 1, xp.nan, 4], dtype=cupy.default_float_type())
292-
y = xp.zeros((2, 4), dtype=cupy.default_float_type())
293-
with pytest.raises((ValueError, TypeError)):
322+
y = xp.zeros((2, 4), dtype=x.dtype)
323+
with pytest.raises(ValueError):
294324
xp.nan_to_num(x, **{kwarg: y})
295-
with pytest.raises((ValueError, TypeError)):
296-
xp.nan_to_num(0.0, **{kwarg: y})
325+
with pytest.raises(ValueError):
326+
xp.nan_to_num(xp.array(0.0), **{kwarg: y})
297327

298328
@testing.for_all_dtypes(no_bool=True, no_complex=True)
299329
@testing.numpy_cupy_array_equal()

0 commit comments

Comments
 (0)