Skip to content

Commit ca3c28e

Browse files
authored
Merge 2bd0227 into 25fb39b
2 parents 25fb39b + 2bd0227 commit ca3c28e

File tree

3 files changed

+179
-284
lines changed

3 files changed

+179
-284
lines changed

dpnp/tests/test_binary_ufuncs.py

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from .helper import (
1515
assert_dtype_allclose,
16+
generate_random_numpy_array,
1617
get_abs_array,
1718
get_all_dtypes,
1819
get_complex_dtypes,
@@ -22,10 +23,7 @@
2223
has_support_aspect16,
2324
numpy_version,
2425
)
25-
from .test_umath import (
26-
_get_numpy_arrays_2in_1out,
27-
_get_output_data_type,
28-
)
26+
from .test_umath import _get_output_data_type
2927

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

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

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

141141

142+
@pytest.mark.parametrize("func", ["fmax", "fmin", "maximum", "minimum"])
142143
class TestBoundFuncs:
143-
@pytest.fixture(
144-
params=[
145-
{"func_name": "fmax", "input_values": [-5, 5, 10]},
146-
{"func_name": "fmin", "input_values": [-5, 5, 10]},
147-
{"func_name": "maximum", "input_values": [-5, 5, 10]},
148-
{"func_name": "minimum", "input_values": [-5, 5, 10]},
149-
],
150-
ids=[
151-
"fmax",
152-
"fmin",
153-
"maximum",
154-
"minimum",
155-
],
156-
)
157-
def func_params(self, request):
158-
return request.param
159-
160144
@pytest.mark.parametrize(
161145
"dtype", get_all_dtypes(no_bool=True, no_complex=True)
162146
)
163-
def test_out(self, func_params, dtype):
164-
func_name = func_params["func_name"]
165-
input_values = func_params["input_values"]
166-
a, b, expected = _get_numpy_arrays_2in_1out(
167-
func_name, dtype, input_values
168-
)
147+
def test_out(self, func, dtype):
148+
a = generate_random_numpy_array(10, dtype)
149+
b = generate_random_numpy_array(10, dtype)
150+
expected = getattr(numpy, func)(a, b)
169151

170152
ia, ib = dpnp.array(a), dpnp.array(b)
171153
iout = dpnp.empty(expected.shape, dtype=dtype)
172-
result = getattr(dpnp, func_name)(ia, ib, out=iout)
154+
result = getattr(dpnp, func)(ia, ib, out=iout)
173155

174156
assert result is iout
175157
assert_dtype_allclose(result, expected)
176158

177159
@pytest.mark.parametrize(
178160
"dtype", get_all_dtypes(no_bool=True, no_complex=True)
179161
)
180-
def test_out_overlap(self, func_params, dtype):
181-
func_name = func_params["func_name"]
162+
def test_out_overlap(self, func, dtype):
182163
size = 15
183164
a = numpy.arange(2 * size, dtype=dtype)
184165
ia = dpnp.array(a)
185166

186-
getattr(dpnp, func_name)(ia[size::], ia[::2], out=ia[:size:])
187-
getattr(numpy, func_name)(a[size::], a[::2], out=a[:size:])
167+
getattr(dpnp, func)(ia[size::], ia[::2], out=ia[:size:])
168+
getattr(numpy, func)(a[size::], a[::2], out=a[:size:])
188169

189170
assert_dtype_allclose(ia, a)
190171

191172
@pytest.mark.parametrize("shape", [(0,), (15,), (2, 2)])
192-
def test_invalid_shape(self, func_params, shape):
193-
func_name = func_params["func_name"]
173+
def test_invalid_shape(self, func, shape):
194174
a, b = dpnp.arange(10), dpnp.arange(10)
195175
out = dpnp.empty(shape)
196176

197177
with pytest.raises(ValueError):
198-
getattr(dpnp, func_name)(a, b, out=out)
178+
getattr(dpnp, func)(a, b, out=out)
199179

200180
@pytest.mark.parametrize("xp", [dpnp, numpy])
201181
@pytest.mark.parametrize(
202182
"out",
203183
[4, (), [], (3, 7), [2, 4]],
204184
ids=["scalar", "empty_tuple", "empty_list", "tuple", "list"],
205185
)
206-
def test_invalid_out(self, func_params, xp, out):
207-
func_name = func_params["func_name"]
186+
def test_invalid_out(self, func, xp, out):
208187
a = xp.arange(10)
209-
assert_raises(TypeError, getattr(xp, func_name), a, 2, out)
188+
assert_raises(TypeError, getattr(xp, func), a, 2, out)
210189

211190

212191
class TestDivide:
@@ -215,9 +194,9 @@ class TestDivide:
215194
"dtype", get_all_dtypes(no_none=True, no_bool=True)
216195
)
217196
def test_divide(self, dtype):
218-
a, b, expected = _get_numpy_arrays_2in_1out(
219-
"divide", dtype, [-5, 5, 10]
220-
)
197+
a = generate_random_numpy_array(10, dtype)
198+
b = generate_random_numpy_array(10, dtype)
199+
expected = numpy.divide(a, b)
221200

222201
ia, ib = dpnp.array(a), dpnp.array(b)
223202
if numpy.issubdtype(dtype, numpy.integer):
@@ -318,7 +297,9 @@ def do_inplace_op(self, base, other, func):
318297
@pytest.mark.usefixtures("suppress_divide_numpy_warnings")
319298
@pytest.mark.parametrize("dtype", ALL_DTYPES)
320299
def test_basic(self, func, dtype):
321-
a, b, expected = _get_numpy_arrays_2in_1out(func, dtype, [-5, 5, 10])
300+
a = generate_random_numpy_array(10, dtype)
301+
b = generate_random_numpy_array(10, dtype)
302+
expected = getattr(numpy, func)(a, b)
322303

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

603584
@pytest.mark.parametrize("dtype", ALL_DTYPES)
604585
def test_multiply(self, dtype):
605-
a, b, expected = _get_numpy_arrays_2in_1out(
606-
"multiply", dtype, [0, 10, 10]
607-
)
586+
a = generate_random_numpy_array(10, dtype)
587+
b = generate_random_numpy_array(10, dtype)
588+
expected = numpy.multiply(a, b)
608589

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

854835
@pytest.mark.parametrize("dtype", ALL_DTYPES)
855836
def test_power(self, dtype):
856-
numpy.random.seed(42)
857-
a, b, expected = _get_numpy_arrays_2in_1out("power", dtype, [0, 10, 10])
837+
a = generate_random_numpy_array(10, dtype, low=0)
838+
b = generate_random_numpy_array(10, dtype, low=0)
839+
expected = numpy.power(a, b)
858840

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

10761058
@pytest.mark.parametrize("dtype", ALL_DTYPES)
10771059
def test_add(self, dtype):
1078-
a, b, expected = _get_numpy_arrays_2in_1out(
1079-
"subtract", dtype, [-5, 5, 10]
1080-
)
1060+
a = generate_random_numpy_array(10, dtype)
1061+
b = generate_random_numpy_array(10, dtype)
1062+
expected = numpy.subtract(a, b)
10811063

10821064
ia, ib = dpnp.array(a), dpnp.array(b)
10831065
iout = dpnp.empty(expected.shape, dtype=dtype)

dpnp/tests/test_mathematical.py

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@
3333
has_support_aspect64,
3434
numpy_version,
3535
)
36-
from .test_umath import (
37-
_get_numpy_arrays_1in_1out,
38-
_get_numpy_arrays_2in_1out,
39-
_get_output_data_type,
40-
)
36+
from .test_umath import _get_output_data_type
4137
from .third_party.cupy import testing
4238

4339

@@ -2414,34 +2410,17 @@ def test_projection(self, dtype):
24142410
assert_allclose(result, expected)
24152411

24162412

2413+
@pytest.mark.parametrize("func", ["ceil", "floor", "trunc"])
24172414
class TestRoundingFuncs:
2418-
@pytest.fixture(
2419-
params=[
2420-
{"func_name": "ceil", "input_values": [-5, 5, 10]},
2421-
{"func_name": "floor", "input_values": [-5, 5, 10]},
2422-
{"func_name": "trunc", "input_values": [-5, 5, 10]},
2423-
],
2424-
ids=[
2425-
"ceil",
2426-
"floor",
2427-
"trunc",
2428-
],
2429-
)
2430-
def func_params(self, request):
2431-
return request.param
2432-
24332415
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
2434-
def test_out(self, func_params, dtype):
2435-
func_name = func_params["func_name"]
2436-
input_values = func_params["input_values"]
2437-
np_array, expected = _get_numpy_arrays_1in_1out(
2438-
func_name, dtype, input_values
2439-
)
2416+
def test_out(self, func, dtype):
2417+
a = generate_random_numpy_array(10, dtype)
2418+
expected = getattr(numpy, func)(a)
24402419

2441-
dp_array = dpnp.array(np_array)
2420+
dp_array = dpnp.array(a)
24422421
out_dtype = numpy.int8 if dtype == numpy.bool_ else dtype
24432422
dp_out = dpnp.empty(expected.shape, dtype=out_dtype)
2444-
result = getattr(dpnp, func_name)(dp_array, out=dp_out)
2423+
result = getattr(dpnp, func)(dp_array, out=dp_out)
24452424

24462425
assert result is dp_out
24472426
# numpy.ceil, numpy.floor, numpy.trunc always return float dtype for
@@ -2457,38 +2436,36 @@ def test_out(self, func_params, dtype):
24572436
@pytest.mark.parametrize(
24582437
"dtype", get_all_dtypes(no_complex=True, no_none=True)[:-1]
24592438
)
2460-
def test_invalid_dtype(self, func_params, dtype):
2461-
func_name = func_params["func_name"]
2439+
def test_invalid_dtype(self, func, dtype):
24622440
dpnp_dtype = get_all_dtypes(no_complex=True, no_none=True)[-1]
24632441
dp_array = dpnp.arange(10, dtype=dpnp_dtype)
24642442
dp_out = dpnp.empty(10, dtype=dtype)
24652443

24662444
with pytest.raises(ValueError):
2467-
getattr(dpnp, func_name)(dp_array, out=dp_out)
2445+
getattr(dpnp, func)(dp_array, out=dp_out)
24682446

24692447
@pytest.mark.parametrize(
2470-
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
2448+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15,)", "(2, 2)"]
24712449
)
2472-
def test_invalid_shape(self, func_params, shape):
2473-
func_name = func_params["func_name"]
2450+
def test_invalid_shape(self, func, shape):
24742451
dp_array = dpnp.arange(10, dtype=dpnp.float32)
24752452
dp_out = dpnp.empty(shape, dtype=dpnp.float32)
24762453

24772454
with pytest.raises(ValueError):
2478-
getattr(dpnp, func_name)(dp_array, out=dp_out)
2455+
getattr(dpnp, func)(dp_array, out=dp_out)
24792456

24802457

24812458
class TestHypot:
24822459
@pytest.mark.parametrize(
24832460
"dtype", get_all_dtypes(no_bool=True, no_complex=True)
24842461
)
24852462
def test_hypot(self, dtype):
2486-
np_array1, np_array2, expected = _get_numpy_arrays_2in_1out(
2487-
"hypot", dtype, [0, 10, 10]
2488-
)
2463+
a = generate_random_numpy_array(10, dtype, low=0)
2464+
b = generate_random_numpy_array(10, dtype, low=0)
2465+
expected = numpy.hypot(a, b)
24892466

2490-
dp_array1 = dpnp.array(np_array1)
2491-
dp_array2 = dpnp.array(np_array2)
2467+
dp_array1 = dpnp.array(a)
2468+
dp_array2 = dpnp.array(b)
24922469
out_dtype = _get_output_data_type(dtype)
24932470
dp_out = dpnp.empty(expected.shape, dtype=out_dtype)
24942471
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)

0 commit comments

Comments
 (0)