Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 33 additions & 51 deletions dpnp/tests/test_binary_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .helper import (
assert_dtype_allclose,
generate_random_numpy_array,
get_abs_array,
get_all_dtypes,
get_complex_dtypes,
Expand All @@ -22,10 +23,7 @@
has_support_aspect16,
numpy_version,
)
from .test_umath import (
_get_numpy_arrays_2in_1out,
_get_output_data_type,
)
from .test_umath import _get_output_data_type

"""
The scope includes tests with only functions which are instances of
Expand All @@ -39,7 +37,9 @@ class TestAdd:

@pytest.mark.parametrize("dtype", ALL_DTYPES)
def test_add(self, dtype):
a, b, expected = _get_numpy_arrays_2in_1out("add", dtype, [-5, 5, 10])
a = generate_random_numpy_array(10, dtype)
b = generate_random_numpy_array(10, dtype)
expected = numpy.add(a, b)

ia, ib = dpnp.array(a), dpnp.array(b)
iout = dpnp.empty(expected.shape, dtype=dtype)
Expand Down Expand Up @@ -139,74 +139,53 @@ def test_invalid_out(self, xp, out):
assert_raises(TypeError, xp.add, a, 2, out)


@pytest.mark.parametrize("func", ["fmax", "fmin", "maximum", "minimum"])
class TestBoundFuncs:
@pytest.fixture(
params=[
{"func_name": "fmax", "input_values": [-5, 5, 10]},
{"func_name": "fmin", "input_values": [-5, 5, 10]},
{"func_name": "maximum", "input_values": [-5, 5, 10]},
{"func_name": "minimum", "input_values": [-5, 5, 10]},
],
ids=[
"fmax",
"fmin",
"maximum",
"minimum",
],
)
def func_params(self, request):
return request.param

@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_bool=True, no_complex=True)
)
def test_out(self, func_params, dtype):
func_name = func_params["func_name"]
input_values = func_params["input_values"]
a, b, expected = _get_numpy_arrays_2in_1out(
func_name, dtype, input_values
)
def test_out(self, func, dtype):
a = generate_random_numpy_array(10, dtype)
b = generate_random_numpy_array(10, dtype)
expected = getattr(numpy, func)(a, b)

ia, ib = dpnp.array(a), dpnp.array(b)
iout = dpnp.empty(expected.shape, dtype=dtype)
result = getattr(dpnp, func_name)(ia, ib, out=iout)
result = getattr(dpnp, func)(ia, ib, out=iout)

assert result is iout
assert_dtype_allclose(result, expected)

@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_bool=True, no_complex=True)
)
def test_out_overlap(self, func_params, dtype):
func_name = func_params["func_name"]
def test_out_overlap(self, func, dtype):
size = 15
a = numpy.arange(2 * size, dtype=dtype)
ia = dpnp.array(a)

getattr(dpnp, func_name)(ia[size::], ia[::2], out=ia[:size:])
getattr(numpy, func_name)(a[size::], a[::2], out=a[:size:])
getattr(dpnp, func)(ia[size::], ia[::2], out=ia[:size:])
getattr(numpy, func)(a[size::], a[::2], out=a[:size:])

assert_dtype_allclose(ia, a)

@pytest.mark.parametrize("shape", [(0,), (15,), (2, 2)])
def test_invalid_shape(self, func_params, shape):
func_name = func_params["func_name"]
def test_invalid_shape(self, func, shape):
a, b = dpnp.arange(10), dpnp.arange(10)
out = dpnp.empty(shape)

with pytest.raises(ValueError):
getattr(dpnp, func_name)(a, b, out=out)
getattr(dpnp, func)(a, b, out=out)

@pytest.mark.parametrize("xp", [dpnp, numpy])
@pytest.mark.parametrize(
"out",
[4, (), [], (3, 7), [2, 4]],
ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"],
)
def test_invalid_out(self, func_params, xp, out):
func_name = func_params["func_name"]
def test_invalid_out(self, func, xp, out):
a = xp.arange(10)
assert_raises(TypeError, getattr(xp, func_name), a, 2, out)
assert_raises(TypeError, getattr(xp, func), a, 2, out)


class TestDivide:
Expand All @@ -215,9 +194,9 @@ class TestDivide:
"dtype", get_all_dtypes(no_none=True, no_bool=True)
)
def test_divide(self, dtype):
a, b, expected = _get_numpy_arrays_2in_1out(
"divide", dtype, [-5, 5, 10]
)
a = generate_random_numpy_array(10, dtype)
b = generate_random_numpy_array(10, dtype)
expected = numpy.divide(a, b)

ia, ib = dpnp.array(a), dpnp.array(b)
if numpy.issubdtype(dtype, numpy.integer):
Expand Down Expand Up @@ -318,7 +297,9 @@ def do_inplace_op(self, base, other, func):
@pytest.mark.usefixtures("suppress_divide_numpy_warnings")
@pytest.mark.parametrize("dtype", ALL_DTYPES)
def test_basic(self, func, dtype):
a, b, expected = _get_numpy_arrays_2in_1out(func, dtype, [-5, 5, 10])
a = generate_random_numpy_array(10, dtype)
b = generate_random_numpy_array(10, dtype)
expected = getattr(numpy, func)(a, b)

ia, ib = dpnp.array(a), dpnp.array(b)
iout = dpnp.empty(expected.shape, dtype=dtype)
Expand Down Expand Up @@ -602,9 +583,9 @@ class TestMultiply:

@pytest.mark.parametrize("dtype", ALL_DTYPES)
def test_multiply(self, dtype):
a, b, expected = _get_numpy_arrays_2in_1out(
"multiply", dtype, [0, 10, 10]
)
a = generate_random_numpy_array(10, dtype)
b = generate_random_numpy_array(10, dtype)
expected = numpy.multiply(a, b)

ia, ib = dpnp.array(a), dpnp.array(b)
iout = dpnp.empty(expected.shape, dtype=dtype)
Expand Down Expand Up @@ -853,8 +834,9 @@ def test_basic(self, array, val, data_type, val_type):

@pytest.mark.parametrize("dtype", ALL_DTYPES)
def test_power(self, dtype):
numpy.random.seed(42)
a, b, expected = _get_numpy_arrays_2in_1out("power", dtype, [0, 10, 10])
a = generate_random_numpy_array(10, dtype, low=0)
b = generate_random_numpy_array(10, dtype, low=0)
expected = numpy.power(a, b)

ia, ib = dpnp.array(a), dpnp.array(b)
out_dtype = numpy.int8 if dtype == numpy.bool_ else dtype
Expand Down Expand Up @@ -1075,9 +1057,9 @@ class TestSubtract:

@pytest.mark.parametrize("dtype", ALL_DTYPES)
def test_add(self, dtype):
a, b, expected = _get_numpy_arrays_2in_1out(
"subtract", dtype, [-5, 5, 10]
)
a = generate_random_numpy_array(10, dtype)
b = generate_random_numpy_array(10, dtype)
expected = numpy.subtract(a, b)

ia, ib = dpnp.array(a), dpnp.array(b)
iout = dpnp.empty(expected.shape, dtype=dtype)
Expand Down
42 changes: 14 additions & 28 deletions dpnp/tests/test_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@
has_support_aspect64,
numpy_version,
)
from .test_umath import (
_get_numpy_arrays_1in_1out,
_get_numpy_arrays_2in_1out,
_get_output_data_type,
)
from .test_umath import _get_output_data_type
from .third_party.cupy import testing


Expand Down Expand Up @@ -2271,30 +2267,19 @@ def test_projection(self, dtype):
assert dpnp.allclose(result, expected)


@pytest.mark.parametrize("func", ["ceil", "floor", "trunc"])
class TestRoundingFuncs:
@pytest.fixture(
params=[
{"func_name": "ceil", "input_values": [-5, 5, 10]},
{"func_name": "floor", "input_values": [-5, 5, 10]},
{"func_name": "trunc", "input_values": [-5, 5, 10]},
],
ids=["ceil", "floor", "trunc"],
)
def func_params(self, request):
return request.param

@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_none=True, no_complex=True)
)
def test_out(self, func_params, dtype):
func_name = func_params["func_name"]
input_values = func_params["input_values"]
a, expected = _get_numpy_arrays_1in_1out(func_name, dtype, input_values)
def test_out(self, func, dtype):
a = generate_random_numpy_array(10, dtype)
expected = getattr(numpy, func)(a)

ia = dpnp.array(a)
out_dt = numpy.int8 if dtype == dpnp.bool else dtype
iout = dpnp.empty(expected.shape, dtype=out_dt)
result = getattr(dpnp, func_name)(ia, out=iout)
result = getattr(dpnp, func)(ia, out=iout)

assert result is iout
# numpy.ceil, numpy.floor, numpy.trunc always return float dtype for
Expand All @@ -2310,31 +2295,32 @@ def test_out(self, func_params, dtype):
@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1]
)
def test_invalid_dtype(self, func_params, dtype):
func_name = func_params["func_name"]
def test_invalid_dtype(self, func, dtype):
dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1]
ia = dpnp.arange(10, dtype=dpnp_dtype)
iout = dpnp.empty(10, dtype=dtype)

with pytest.raises(ValueError):
getattr(dpnp, func_name)(ia, out=iout)
getattr(dpnp, func)(ia, out=iout)

@pytest.mark.parametrize(
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15,)", "(2, 2)"]
)
def test_invalid_shape(self, func_params, shape):
func_name = func_params["func_name"]
def test_invalid_shape(self, func, shape):
ia = dpnp.arange(10, dtype=dpnp.float32)
iout = dpnp.empty(shape, dtype=dpnp.float32)
assert_raises(ValueError, getattr(dpnp, func_name), ia, out=iout)

assert_raises(ValueError, getattr(dpnp, func), ia, out=iout)


class TestHypot:
@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True)
)
def test_hypot(self, dtype):
a, b, expected = _get_numpy_arrays_2in_1out("hypot", dtype, [0, 10, 10])
a = generate_random_numpy_array(10, dtype, low=0)
b = generate_random_numpy_array(10, dtype, low=0)
expected = numpy.hypot(a, b)

ia, ib = dpnp.array(a), dpnp.array(b)
out_dt = _get_output_data_type(dtype)
Expand Down
Loading
Loading