Skip to content

Commit 66bd6bb

Browse files
committed
Add more tests to improve the coverage
1 parent b82dce8 commit 66bd6bb

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

dpnp/dpnp_algo/dpnp_elementwise_common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def __call__(
319319
if not isinstance(out, tuple):
320320
raise TypeError("'out' must be a tuple of arrays")
321321

322-
if len(out) != 2:
322+
if len(out) != self.nout:
323323
raise ValueError(
324324
"'out' tuple must have exactly one entry per ufunc output"
325325
)
@@ -341,7 +341,7 @@ def __call__(
341341

342342
res = dpnp.get_usm_ndarray(out[i])
343343
if not res.flags.writable:
344-
raise ValueError("provided output array is read-only")
344+
raise ValueError("output array is read-only")
345345

346346
if res.shape != x.shape:
347347
raise ValueError(

dpnp/tests/test_mathematical.py

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -802,12 +802,6 @@ def test_strides_out(self, stride, dt):
802802
assert_array_equal(iout_mant, out_mant)
803803
assert_array_equal(iout_exp, out_exp)
804804

805-
@pytest.mark.parametrize("xp", [numpy, dpnp])
806-
def test_out_wrong_type(self, xp):
807-
a = xp.array(0.5)
808-
with pytest.raises(TypeError, match="'out' must be a tuple of arrays"):
809-
_ = xp.frexp(a, out=xp.empty(()))
810-
811805
@pytest.mark.parametrize("xp", [numpy, dpnp])
812806
@pytest.mark.parametrize("dt", get_complex_dtypes())
813807
def test_complex_dtype(self, xp, dt):
@@ -2080,7 +2074,7 @@ def test_not_supported_kwargs(self, func, kwargs):
20802074

20812075
@pytest.mark.parametrize("func", ["abs", "frexp", "add"])
20822076
@pytest.mark.parametrize("x", [1, [1, 2], numpy.ones(5)])
2083-
def test_wrong_input(self, func, x):
2077+
def test_unary_wrong_input(self, func, x):
20842078
fn = getattr(dpnp, func)
20852079
args = [x] * fn.nin
20862080
with pytest.raises(TypeError):
@@ -2115,8 +2109,62 @@ def test_out_dtype(self, func):
21152109
):
21162110
fn(*args, out=out, dtype="f4")
21172111

2112+
@pytest.mark.parametrize("xp", [numpy, dpnp])
2113+
def test_unary_two_outs_out_ndarray(self, xp):
2114+
x = xp.array(0.5)
2115+
with pytest.raises(TypeError, match="'out' must be a tuple of arrays"):
2116+
_ = xp.frexp(x, out=xp.empty(()))
2117+
2118+
@pytest.mark.parametrize("xp", [numpy, dpnp])
2119+
@pytest.mark.parametrize("out", [(), (1,), (1, 2, 3)])
2120+
def test_unary_two_outs_out_wrong_tuple_len(self, xp, out):
2121+
x = xp.array(0.5)
2122+
with pytest.raises(
2123+
ValueError,
2124+
match="'out' tuple must have exactly one entry per ufunc output",
2125+
):
2126+
_ = xp.frexp(x, out=out)
2127+
2128+
@pytest.mark.parametrize("xp", [numpy, dpnp])
2129+
def test_unary_two_outs_out_mixed(self, xp):
2130+
x = xp.array(0.5)
2131+
with pytest.raises(
2132+
TypeError,
2133+
match="cannot specify 'out' as both a positional and keyword",
2134+
):
2135+
_ = xp.frexp(x, xp.empty(()), out=(xp.empty(()), None))
2136+
2137+
@pytest.mark.parametrize("func", ["abs", "add"])
2138+
@pytest.mark.parametrize("xp", [numpy, dpnp])
2139+
def test_unary_two_outs_out_not_writable(self, func, xp):
2140+
x = xp.array(0.5)
2141+
out1 = xp.empty(())
2142+
out1.flags["W"] = False
2143+
2144+
fn = getattr(xp, func)
2145+
args = [x] * fn.nin
2146+
with pytest.raises(ValueError, match="array is read-only"):
2147+
_ = fn(*args, out1)
2148+
2149+
out2 = xp.empty((), dtype="i")
2150+
out2.flags["W"] = False
2151+
with pytest.raises(ValueError, match="array is read-only"):
2152+
_ = fn(*args, out=(None, out2))
2153+
2154+
@pytest.mark.parametrize("xp", [numpy, dpnp])
2155+
def test_unary_two_outs_out_wrong_shape(self, xp):
2156+
x = xp.full(6, fill_value=0.5)
2157+
out1 = xp.empty(12)
2158+
with pytest.raises(ValueError):
2159+
_ = xp.frexp(x, out1)
2160+
2161+
out2 = xp.empty((2, 3), dtype="i")
2162+
with pytest.raises(ValueError):
2163+
_ = xp.frexp(x, out=(None, out2))
2164+
21182165
@pytest.mark.parametrize("func", ["abs", "frexp", "add"])
2119-
def test_order_none(self, func):
2166+
@pytest.mark.parametrize("order", [None, "K", "A", "f", "c"])
2167+
def test_order(self, func, order):
21202168
a = numpy.array([1, 2, 3])
21212169
ia = dpnp.array(a)
21222170

@@ -2126,8 +2174,8 @@ def test_order_none(self, func):
21262174
args = [a] * fn.nin
21272175
iargs = [ia] * ifn.nin
21282176

2129-
result = ifn(*iargs, order=None)
2130-
expected = fn(*args, order=None)
2177+
result = ifn(*iargs, order=order)
2178+
expected = fn(*args, order=order)
21312179
if fn.nout == 1:
21322180
assert_dtype_allclose(result, expected)
21332181
else:

0 commit comments

Comments
 (0)