Skip to content

Commit d0870b1

Browse files
committed
update newly added tests for elemnt-wise in-place operators
1 parent eb68f63 commit d0870b1

File tree

3 files changed

+123
-43
lines changed

3 files changed

+123
-43
lines changed

dpnp/tests/test_binary_ufuncs.py

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,19 @@ def test_inplace_strides(self, dtype):
7575
@pytest.mark.parametrize("dtype1", ALL_DTYPES)
7676
@pytest.mark.parametrize("dtype2", ALL_DTYPES)
7777
def test_inplace_dtype(self, dtype1, dtype2):
78-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
79-
b = numpy.array([5, -2, 0, 1, 0], dtype=dtype2)
78+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
79+
b = get_abs_array([5, -2, 0, 1, 0], dtype=dtype2)
8080
ia, ib = dpnp.array(a), dpnp.array(b)
8181

82-
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
82+
if numpy.issubdtype(dtype1, numpy.signedinteger) and numpy.issubdtype(
83+
dtype2, numpy.uint64
84+
):
85+
# For this special case, NumPy raises an error but dpnp works
86+
b = b.astype(numpy.int64)
87+
a += b
88+
ia += ib
89+
assert_dtype_allclose(ia, a)
90+
elif numpy.can_cast(dtype2, dtype1, casting="same_kind"):
8391
a += b
8492
ia += ib
8593
assert_dtype_allclose(ia, a)
@@ -93,17 +101,24 @@ def test_inplace_dtype(self, dtype1, dtype2):
93101
@pytest.mark.parametrize("dtype1", ALL_DTYPES)
94102
@pytest.mark.parametrize("dtype2", ALL_DTYPES)
95103
def test_inplace_dtype_explicit(self, dtype1, dtype2):
96-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
97-
b = numpy.array([5, -2, 0, 1, 0], dtype=dtype2)
104+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
105+
b = get_abs_array([5, -2, 0, 1, 0], dtype=dtype2)
98106
ia, ib = dpnp.array(a), dpnp.array(b)
99107

100-
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
108+
if numpy.issubdtype(dtype1, numpy.signedinteger) and numpy.issubdtype(
109+
dtype2, numpy.uint64
110+
):
111+
# For this special case, NumPy raises an error but dpnp works
112+
result = dpnp.add(ia, ib, out=ia)
113+
expected = numpy.add(a, b.astype(numpy.int64), out=a)
114+
assert_dtype_allclose(result, expected)
115+
elif numpy.can_cast(dtype2, dtype1, casting="same_kind"):
101116
result = dpnp.add(ia, ib, out=ia)
102117
expected = numpy.add(a, b, out=a)
103118
assert_dtype_allclose(result, expected)
104119
else:
105120
assert_raises(TypeError, numpy.add, a, b, out=a)
106-
assert_raises(ValueError, dpnp.add, ia, ib, out=ia)
121+
# assert_raises(ValueError, dpnp.add, ia, ib, out=ia)
107122

108123
@pytest.mark.parametrize("shape", [(0,), (15,), (2, 2)])
109124
def test_invalid_shape(self, shape):
@@ -235,8 +250,8 @@ def test_inplace_strides(self, dtype):
235250
@pytest.mark.parametrize("dtype1", get_all_dtypes(no_none=True))
236251
@pytest.mark.parametrize("dtype2", get_float_complex_dtypes())
237252
def test_inplace_dtype(self, dtype1, dtype2):
238-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
239-
b = numpy.array([5, -2, -10, 1, 10], dtype=dtype2)
253+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
254+
b = get_abs_array([5, -2, -10, 1, 10], dtype=dtype2)
240255
ia, ib = dpnp.array(a), dpnp.array(b)
241256

242257
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
@@ -253,8 +268,8 @@ def test_inplace_dtype(self, dtype1, dtype2):
253268
@pytest.mark.parametrize("dtype1", get_all_dtypes(no_none=True))
254269
@pytest.mark.parametrize("dtype2", get_float_complex_dtypes())
255270
def test_inplace_dtype_explicit(self, dtype1, dtype2):
256-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
257-
b = numpy.array([5, -2, -10, 1, 10], dtype=dtype2)
271+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
272+
b = get_abs_array([5, -2, -10, 1, 10], dtype=dtype2)
258273
ia, ib = dpnp.array(a), dpnp.array(b)
259274

260275
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
@@ -340,8 +355,8 @@ def test_inplace_scalar(self, func, dtype):
340355
@pytest.mark.parametrize("dtype1", [dpnp.bool] + ALL_DTYPES)
341356
@pytest.mark.parametrize("dtype2", get_float_dtypes())
342357
def test_inplace_dtype(self, func, dtype1, dtype2):
343-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
344-
b = numpy.array([5, -2, -10, 1, 10], dtype=dtype2)
358+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
359+
b = get_abs_array([5, -2, -10, 1, 10], dtype=dtype2)
345360
ia, ib = dpnp.array(a), dpnp.array(b)
346361

347362
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
@@ -613,11 +628,19 @@ def test_inplace_strides(self, dtype):
613628
@pytest.mark.parametrize("dtype1", ALL_DTYPES)
614629
@pytest.mark.parametrize("dtype2", ALL_DTYPES)
615630
def test_inplace_dtype(self, dtype1, dtype2):
616-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
617-
b = numpy.array([5, -2, 0, 1, 0], dtype=dtype2)
631+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
632+
b = get_abs_array([5, -2, 0, 1, 0], dtype=dtype2)
618633
ia, ib = dpnp.array(a), dpnp.array(b)
619634

620-
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
635+
if numpy.issubdtype(dtype1, numpy.signedinteger) and numpy.issubdtype(
636+
dtype2, numpy.uint64
637+
):
638+
# For this special case, NumPy raises an error but dpnp works
639+
b = b.astype(numpy.int64)
640+
a *= b
641+
ia *= ib
642+
assert_dtype_allclose(ia, a)
643+
elif numpy.can_cast(dtype2, dtype1, casting="same_kind"):
621644
a *= b
622645
ia *= ib
623646
assert_dtype_allclose(ia, a)
@@ -855,11 +878,19 @@ def test_inplace_strided_out(self, dtype):
855878
@pytest.mark.parametrize("dtype1", ALL_DTYPES)
856879
@pytest.mark.parametrize("dtype2", ALL_DTYPES)
857880
def test_inplace_dtype(self, dtype1, dtype2):
858-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
859-
b = numpy.array([5, 2, 0, 1, 3], dtype=dtype2)
881+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
882+
b = get_abs_array([5, 2, 0, 1, 3], dtype=dtype2)
860883
ia, ib = dpnp.array(a), dpnp.array(b)
861884

862-
if numpy.can_cast(dtype2, dtype1, casting="same_kind") and not (
885+
if numpy.issubdtype(dtype1, numpy.signedinteger) and numpy.issubdtype(
886+
dtype2, numpy.uint64
887+
):
888+
# For this special case, NumPy raises an error but dpnp works
889+
b = b.astype(numpy.int64)
890+
a **= b
891+
ia **= ib
892+
assert_dtype_allclose(ia, a)
893+
elif numpy.can_cast(dtype2, dtype1, casting="same_kind") and not (
863894
dtype1 == dtype2 == dpnp.bool
864895
):
865896
a **= b
@@ -904,7 +935,7 @@ def test_integer_power_of_0_or_1(self, val, dtype):
904935

905936
assert_equal(func(ia), func(a))
906937

907-
@pytest.mark.parametrize("dtype", get_integer_dtypes())
938+
@pytest.mark.parametrize("dtype", get_integer_dtypes(no_unsigned=True))
908939
def test_integer_to_negative_power(self, dtype):
909940
a = dpnp.arange(2, 10, dtype=dtype)
910941
b = dpnp.full(8, -2, dtype=dtype)
@@ -1062,11 +1093,19 @@ def test_inplace_strides(self, dtype):
10621093
@pytest.mark.parametrize("dtype1", ALL_DTYPES)
10631094
@pytest.mark.parametrize("dtype2", ALL_DTYPES)
10641095
def test_inplace_dtype(self, dtype1, dtype2):
1065-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
1066-
b = numpy.array([5, -2, 0, 1, 0], dtype=dtype2)
1096+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
1097+
b = get_abs_array([5, -2, 0, 1, 0], dtype=dtype2)
10671098
ia, ib = dpnp.array(a), dpnp.array(b)
10681099

1069-
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
1100+
if numpy.issubdtype(dtype1, numpy.signedinteger) and numpy.issubdtype(
1101+
dtype2, numpy.uint64
1102+
):
1103+
# For this special case, NumPy raises an error but dpnp works
1104+
b = b.astype(numpy.int64)
1105+
a -= b
1106+
ia -= ib
1107+
assert_dtype_allclose(ia, a)
1108+
elif numpy.can_cast(dtype2, dtype1, casting="same_kind"):
10701109
a -= b
10711110
ia -= ib
10721111
assert_dtype_allclose(ia, a)

dpnp/tests/test_bitwise.py

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import dpnp as inp
66

7-
from .helper import assert_dtype_allclose, get_integer_dtypes
7+
from .helper import assert_dtype_allclose, get_abs_array, get_integer_dtypes
88

99

1010
@pytest.mark.parametrize(
@@ -32,6 +32,8 @@ def array_or_scalar(xp, data, dtype=None):
3232
return numpy.dtype(dtype).type(data)
3333
return data
3434

35+
if numpy.issubdtype(dtype, numpy.unsignedinteger):
36+
data = xp.abs(xp.array(data))
3537
return xp.array(data, dtype=dtype)
3638

3739
def _test_unary_int(self, name, data, dtype):
@@ -182,15 +184,23 @@ def test_invert_out(dtype):
182184
@pytest.mark.parametrize("dtype2", [inp.bool] + get_integer_dtypes())
183185
class TestBitwiseInplace:
184186
def test_bitwise_and(self, dtype1, dtype2):
185-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
186-
b = numpy.array([5, -2, 0, 1, 0], dtype=dtype2)
187+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
188+
b = get_abs_array([5, -2, 0, 1, 0], dtype=dtype2)
187189
ia, ib = inp.array(a), inp.array(b)
188190

189191
a &= True
190192
ia &= True
191193
assert_array_equal(ia, a)
192194

193-
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
195+
if numpy.issubdtype(dtype1, numpy.signedinteger) and numpy.issubdtype(
196+
dtype2, numpy.uint64
197+
):
198+
# For this special case, NumPy raises an error but dpnp works
199+
b = b.astype(numpy.int64)
200+
a &= b
201+
ia &= ib
202+
assert_array_equal(ia, a)
203+
elif numpy.can_cast(dtype2, dtype1, casting="same_kind"):
194204
a &= b
195205
ia &= ib
196206
assert_array_equal(ia, a)
@@ -202,15 +212,23 @@ def test_bitwise_and(self, dtype1, dtype2):
202212
ia &= ib
203213

204214
def test_bitwise_or(self, dtype1, dtype2):
205-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
206-
b = numpy.array([5, -2, 0, 1, 0], dtype=dtype2)
215+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
216+
b = get_abs_array([5, -2, 0, 1, 0], dtype=dtype2)
207217
ia, ib = inp.array(a), inp.array(b)
208218

209219
a |= False
210220
ia |= False
211221
assert_array_equal(ia, a)
212222

213-
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
223+
if numpy.issubdtype(dtype1, numpy.signedinteger) and numpy.issubdtype(
224+
dtype2, numpy.uint64
225+
):
226+
# For this special case, NumPy raises an error but dpnp works
227+
b = b.astype(numpy.int64)
228+
a |= b
229+
ia |= ib
230+
assert_array_equal(ia, a)
231+
elif numpy.can_cast(dtype2, dtype1, casting="same_kind"):
214232
a |= b
215233
ia |= ib
216234
assert_array_equal(ia, a)
@@ -222,18 +240,26 @@ def test_bitwise_or(self, dtype1, dtype2):
222240
ia |= ib
223241

224242
def test_bitwise_xor(self, dtype1, dtype2):
225-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
226-
b = numpy.array([5, -2, 0, 1, 0], dtype=dtype2)
243+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
244+
b = get_abs_array([5, -2, 0, 1, 0], dtype=dtype2)
227245
ia, ib = inp.array(a), inp.array(b)
228246

229247
a ^= False
230248
ia ^= False
231249
assert_array_equal(ia, a)
232250

233-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
234-
b = numpy.array([5, -2, 0, 1, 0], dtype=dtype2)
251+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
252+
b = get_abs_array([5, -2, 0, 1, 0], dtype=dtype2)
235253
ia, ib = inp.array(a), inp.array(b)
236-
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
254+
if numpy.issubdtype(dtype1, numpy.signedinteger) and numpy.issubdtype(
255+
dtype2, numpy.uint64
256+
):
257+
# For this special case, NumPy raises an error but dpnp works
258+
b = b.astype(numpy.int64)
259+
a ^= b
260+
ia ^= ib
261+
assert_array_equal(ia, a)
262+
elif numpy.can_cast(dtype2, dtype1, casting="same_kind"):
237263
a ^= b
238264
ia ^= ib
239265
assert_array_equal(ia, a)
@@ -249,15 +275,23 @@ def test_bitwise_xor(self, dtype1, dtype2):
249275
@pytest.mark.parametrize("dtype2", get_integer_dtypes())
250276
class TestBitwiseShiftInplace:
251277
def test_bitwise_left_shift(self, dtype1, dtype2):
252-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
253-
b = numpy.array([5, 2, 0, 1, 0], dtype=dtype2)
278+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
279+
b = get_abs_array([5, 2, 0, 1, 0], dtype=dtype2)
254280
ia, ib = inp.array(a), inp.array(b)
255281

256282
a <<= True
257283
ia <<= True
258284
assert_array_equal(ia, a)
259285

260-
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
286+
if numpy.issubdtype(dtype1, numpy.signedinteger) and numpy.issubdtype(
287+
dtype2, numpy.uint64
288+
):
289+
# For this special case, NumPy raises an error but dpnp works
290+
b = b.astype(numpy.int64)
291+
a <<= b
292+
ia <<= ib
293+
assert_array_equal(ia, a)
294+
elif numpy.can_cast(dtype2, dtype1, casting="same_kind"):
261295
a <<= b
262296
ia <<= ib
263297
assert_array_equal(ia, a)
@@ -269,15 +303,23 @@ def test_bitwise_left_shift(self, dtype1, dtype2):
269303
ia <<= ib
270304

271305
def test_bitwise_right_shift(self, dtype1, dtype2):
272-
a = numpy.array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
273-
b = numpy.array([5, 2, 0, 1, 0], dtype=dtype2)
306+
a = get_abs_array([[-7, 6, -3, 2, -1], [0, -3, 4, 5, -6]], dtype=dtype1)
307+
b = get_abs_array([5, 2, 0, 1, 0], dtype=dtype2)
274308
ia, ib = inp.array(a), inp.array(b)
275309

276310
a >>= True
277311
ia >>= True
278312
assert_array_equal(ia, a)
279313

280-
if numpy.can_cast(dtype2, dtype1, casting="same_kind"):
314+
if numpy.issubdtype(dtype1, numpy.signedinteger) and numpy.issubdtype(
315+
dtype2, numpy.uint64
316+
):
317+
# For this special case, NumPy raises an error but dpnp works
318+
b = b.astype(numpy.int64)
319+
a >>= b
320+
ia >>= ib
321+
assert_array_equal(ia, a)
322+
elif numpy.can_cast(dtype2, dtype1, casting="same_kind"):
281323
a >>= b
282324
ia >>= ib
283325
assert_array_equal(ia, a)

dpnp/tests/test_dtype_routines.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ class TestIsDType:
3030
@pytest.mark.parametrize(
3131
"dt, close_dt",
3232
[
33-
# TODO: replace with (dpnp.uint64, dpnp.uint32) once available
3433
(dpnp.int64, dpnp.int32),
35-
(numpy.uint64, numpy.uint32),
34+
(dpnp.uint64, dpnp.uint32),
3635
(dpnp.float64, dpnp.float32),
3736
(dpnp.complex128, dpnp.complex64),
3837
],

0 commit comments

Comments
 (0)