Skip to content

Commit

Permalink
Merge pull request #1537 from IntelPython/fix-coverity-issues-in-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-pavlyk authored Feb 7, 2024
2 parents f9d3501 + bb81be5 commit 004614e
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 79 deletions.
31 changes: 21 additions & 10 deletions dpctl/tests/elementwise/test_greater.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,40 @@ def test_greater_complex_matrix(op_dtype):
sz = 127
ar1_np_real = np.random.randint(0, 10, sz)
ar1_np_imag = np.random.randint(0, 10, sz)
ar1 = dpt.asarray(ar1_np_real + 1j * ar1_np_imag, dtype=op_dtype)
ar1_np = ar1_np_real + 1j * ar1_np_imag
ar1 = dpt.asarray(ar1_np, dtype=op_dtype)

ar2_np_real = np.random.randint(0, 10, sz)
ar2_np_imag = np.random.randint(0, 10, sz)
ar2 = dpt.asarray(ar2_np_real + 1j * ar2_np_imag, dtype=op_dtype)
ar2_np = ar2_np_real + 1j * ar2_np_imag
ar2 = dpt.asarray(ar2_np, dtype=op_dtype)

r = dpt.greater(ar1, ar2)
expected = np.greater(dpt.asnumpy(ar1), dpt.asnumpy(ar2))
expected = np.greater(ar1_np, ar2_np)
assert _compare_dtypes(r.dtype, expected.dtype, sycl_queue=q)
assert r.shape == expected.shape
assert (dpt.asnumpy(r) == expected).all()

r1 = dpt.greater(ar1[::-2], ar2[::2])
expected1 = np.greater(dpt.asnumpy(ar1[::-2]), dpt.asnumpy(ar2[::2]))
expected1 = np.greater(ar1_np[::-2], ar2_np[::2])
assert _compare_dtypes(r.dtype, expected1.dtype, sycl_queue=q)
assert r1.shape == expected1.shape
assert (dpt.asnumpy(r1) == expected1).all()

ar3 = dpt.asarray([1.0 + 9j, 2.0 + 0j, 2.0 + 1j, 2.0 + 2j], dtype=op_dtype)
ar4 = dpt.asarray([2.0 + 0j, dpt.nan, dpt.inf, -dpt.inf], dtype=op_dtype)

ar3_np = dpt.asnumpy(ar3)
ar4_np = dpt.asnumpy(ar4)

r2 = dpt.greater(ar3, ar4)
with np.errstate(invalid="ignore"):
expected2 = np.greater(dpt.asnumpy(ar3), dpt.asnumpy(ar4))
expected2 = np.greater(ar3_np, ar4_np)
assert (dpt.asnumpy(r2) == expected2).all()

r3 = dpt.greater(ar4, ar4)
with np.errstate(invalid="ignore"):
expected3 = np.greater(dpt.asnumpy(ar4), dpt.asnumpy(ar4))
expected3 = np.greater(ar4_np, ar4_np)
assert (dpt.asnumpy(r3) == expected3).all()


Expand All @@ -105,23 +111,28 @@ def test_greater_complex_float():
ar1 = dpt.asarray([1.0 + 9j, 2.0 + 0j, 2.0 + 1j, 2.0 + 2j], dtype="c8")
ar2 = dpt.full((4,), 2, dtype="f4")

ar1_np = dpt.asnumpy(ar1)
ar2_np = dpt.asnumpy(ar2)

r = dpt.greater(ar1, ar2)
expected = np.greater(dpt.asnumpy(ar1), dpt.asnumpy(ar2))
expected = np.greater(ar1_np, ar2_np)
assert (dpt.asnumpy(r) == expected).all()

r1 = dpt.greater(ar2, ar1)
expected1 = np.greater(dpt.asnumpy(ar2), dpt.asnumpy(ar1))
expected1 = np.greater(ar2_np, ar1_np)
assert (dpt.asnumpy(r1) == expected1).all()
with np.errstate(invalid="ignore"):
for tp in [dpt.nan, dpt.inf, -dpt.inf]:

ar3 = dpt.full((4,), tp)
ar3_np = dpt.asnumpy(ar3)

r2 = dpt.greater(ar1, ar3)
expected2 = np.greater(dpt.asnumpy(ar1), dpt.asnumpy(ar3))
expected2 = np.greater(ar1_np, ar3_np)
assert (dpt.asnumpy(r2) == expected2).all()

r3 = dpt.greater(ar3, ar1)
expected3 = np.greater(dpt.asnumpy(ar3), dpt.asnumpy(ar1))
expected3 = np.greater(ar3_np, ar1_np)
assert (dpt.asnumpy(r3) == expected3).all()


Expand Down
29 changes: 19 additions & 10 deletions dpctl/tests/elementwise/test_greater_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,39 @@ def test_greater_equal_complex_matrix(op_dtype):
sz = 127
ar1_np_real = np.random.randint(0, 10, sz)
ar1_np_imag = np.random.randint(0, 10, sz)
ar1 = dpt.asarray(ar1_np_real + 1j * ar1_np_imag, dtype=op_dtype)
ar1_np = ar1_np_real + 1j * ar1_np_imag
ar1 = dpt.asarray(ar1_np, dtype=op_dtype)

ar2_np_real = np.random.randint(0, 10, sz)
ar2_np_imag = np.random.randint(0, 10, sz)
ar2 = dpt.asarray(ar2_np_real + 1j * ar2_np_imag, dtype=op_dtype)
ar2_np = ar2_np_real + 1j * ar2_np_imag
ar2 = dpt.asarray(ar2_np, dtype=op_dtype)

r = dpt.greater_equal(ar1, ar2)
expected = np.greater_equal(dpt.asnumpy(ar1), dpt.asnumpy(ar2))
expected = np.greater_equal(ar1_np, ar2_np)
assert _compare_dtypes(r.dtype, expected.dtype, sycl_queue=q)
assert r.shape == expected.shape
assert (dpt.asnumpy(r) == expected).all()

r1 = dpt.greater_equal(ar1[::-2], ar2[::2])
expected1 = np.greater_equal(dpt.asnumpy(ar1[::-2]), dpt.asnumpy(ar2[::2]))
expected1 = np.greater_equal(ar1_np[::-2], ar2_np[::2])
assert _compare_dtypes(r.dtype, expected1.dtype, sycl_queue=q)
assert r1.shape == expected1.shape
assert (dpt.asnumpy(r1) == expected1).all()

ar3 = dpt.asarray([1.0 + 9j, 2.0 + 0j, 2.0 + 1j, 2.0 + 2j], dtype=op_dtype)
ar4 = dpt.asarray([2.0 + 0j, dpt.nan, dpt.inf, -dpt.inf], dtype=op_dtype)

ar3_np = dpt.asnumpy(ar3)
ar4_np = dpt.asnumpy(ar4)
r2 = dpt.greater_equal(ar3, ar4)
with np.errstate(invalid="ignore"):
expected2 = np.greater_equal(dpt.asnumpy(ar3), dpt.asnumpy(ar4))
expected2 = np.greater_equal(ar3_np, ar4_np)
assert (dpt.asnumpy(r2) == expected2).all()

r3 = dpt.greater_equal(ar4, ar4)
with np.errstate(invalid="ignore"):
expected3 = np.greater_equal(dpt.asnumpy(ar4), dpt.asnumpy(ar4))
expected3 = np.greater_equal(ar4_np, ar4_np)
assert (dpt.asnumpy(r3) == expected3).all()


Expand All @@ -105,23 +110,27 @@ def test_greater_equal_complex_float():
ar1 = dpt.asarray([1.0 + 9j, 2.0 + 0j, 2.0 + 1j, 2.0 + 2j], dtype="c8")
ar2 = dpt.full((4,), 2, dtype="f4")

ar1_np = dpt.asnumpy(ar1)
ar2_np = dpt.asnumpy(ar2)

r = dpt.greater_equal(ar1, ar2)
expected = np.greater_equal(dpt.asnumpy(ar1), dpt.asnumpy(ar2))
expected = np.greater_equal(ar1_np, ar2_np)
assert (dpt.asnumpy(r) == expected).all()

r1 = dpt.greater_equal(ar2, ar1)
expected1 = np.greater_equal(dpt.asnumpy(ar2), dpt.asnumpy(ar1))
expected1 = np.greater_equal(ar2_np, ar1_np)
assert (dpt.asnumpy(r1) == expected1).all()
with np.errstate(invalid="ignore"):
for tp in [dpt.nan, dpt.inf, -dpt.inf]:

ar3 = dpt.full((4,), tp)
ar3_np = dpt.asnumpy(ar3)
r2 = dpt.greater_equal(ar1, ar3)
expected2 = np.greater_equal(dpt.asnumpy(ar1), dpt.asnumpy(ar3))
expected2 = np.greater_equal(ar1_np, ar3_np)
assert (dpt.asnumpy(r2) == expected2).all()

r3 = dpt.greater_equal(ar3, ar1)
expected3 = np.greater_equal(dpt.asnumpy(ar3), dpt.asnumpy(ar1))
expected3 = np.greater_equal(ar3_np, ar1_np)
assert (dpt.asnumpy(r3) == expected3).all()


Expand Down
31 changes: 21 additions & 10 deletions dpctl/tests/elementwise/test_less.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,40 @@ def test_less_complex_matrix(op_dtype):
sz = 127
ar1_np_real = np.random.randint(0, 10, sz)
ar1_np_imag = np.random.randint(0, 10, sz)
ar1 = dpt.asarray(ar1_np_real + 1j * ar1_np_imag, dtype=op_dtype)
ar1_np = ar1_np_real + 1j * ar1_np_imag
ar1 = dpt.asarray(ar1_np, dtype=op_dtype)

ar2_np_real = np.random.randint(0, 10, sz)
ar2_np_imag = np.random.randint(0, 10, sz)
ar2 = dpt.asarray(ar2_np_real + 1j * ar2_np_imag, dtype=op_dtype)
ar2_np = ar2_np_real + 1j * ar2_np_imag
ar2 = dpt.asarray(ar2_np, dtype=op_dtype)

r = dpt.less(ar1, ar2)
expected = np.less(dpt.asnumpy(ar1), dpt.asnumpy(ar2))
expected = np.less(ar1_np, ar2_np)
assert _compare_dtypes(r.dtype, expected.dtype, sycl_queue=q)
assert r.shape == expected.shape
assert (dpt.asnumpy(r) == expected).all()

r1 = dpt.less(ar1[::-2], ar2[::2])
expected1 = np.less(dpt.asnumpy(ar1[::-2]), dpt.asnumpy(ar2[::2]))
expected1 = np.less(ar1_np[::-2], ar2_np[::2])
assert _compare_dtypes(r.dtype, expected1.dtype, sycl_queue=q)
assert r1.shape == expected1.shape
assert (dpt.asnumpy(r1) == expected1).all()

ar3 = dpt.asarray([1.0 + 9j, 2.0 + 0j, 2.0 + 1j, 2.0 + 2j], dtype=op_dtype)
ar4 = dpt.asarray([2.0 + 0j, dpt.nan, dpt.inf, -dpt.inf], dtype=op_dtype)

ar3_np = dpt.asnumpy(ar3)
ar4_np = dpt.asnumpy(ar4)

r2 = dpt.less(ar3, ar4)
with np.errstate(invalid="ignore"):
expected2 = np.less(dpt.asnumpy(ar3), dpt.asnumpy(ar4))
expected2 = np.less(ar3_np, ar4_np)
assert (dpt.asnumpy(r2) == expected2).all()

r3 = dpt.less(ar4, ar4)
with np.errstate(invalid="ignore"):
expected3 = np.less(dpt.asnumpy(ar4), dpt.asnumpy(ar4))
expected3 = np.less(ar4_np, ar4_np)
assert (dpt.asnumpy(r3) == expected3).all()


Expand All @@ -105,23 +111,28 @@ def test_less_complex_float():
ar1 = dpt.asarray([1.0 + 9j, 2.0 + 0j, 2.0 + 1j, 2.0 + 2j], dtype="c8")
ar2 = dpt.full((4,), 2, dtype="f4")

ar1_np = dpt.asnumpy(ar1)
ar2_np = dpt.asnumpy(ar2)

r = dpt.less(ar1, ar2)
expected = np.less(dpt.asnumpy(ar1), dpt.asnumpy(ar2))
expected = np.less(ar1_np, ar2_np)
assert (dpt.asnumpy(r) == expected).all()

r1 = dpt.less(ar2, ar1)
expected1 = np.less(dpt.asnumpy(ar2), dpt.asnumpy(ar1))
expected1 = np.less(ar2_np, ar1_np)
assert (dpt.asnumpy(r1) == expected1).all()
with np.errstate(invalid="ignore"):
for tp in [dpt.nan, dpt.inf, -dpt.inf]:

ar3 = dpt.full((4,), tp)
ar3_np = dpt.asnumpy(ar3)

r2 = dpt.less(ar1, ar3)
expected2 = np.less(dpt.asnumpy(ar1), dpt.asnumpy(ar3))
expected2 = np.less(ar1_np, ar3_np)
assert (dpt.asnumpy(r2) == expected2).all()

r3 = dpt.less(ar3, ar1)
expected3 = np.less(dpt.asnumpy(ar3), dpt.asnumpy(ar1))
expected3 = np.less(ar3_np, ar1_np)
assert (dpt.asnumpy(r3) == expected3).all()


Expand Down
30 changes: 20 additions & 10 deletions dpctl/tests/elementwise/test_less_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,40 @@ def test_less_equal_complex_matrix(op_dtype):
sz = 127
ar1_np_real = np.random.randint(0, 10, sz)
ar1_np_imag = np.random.randint(0, 10, sz)
ar1 = dpt.asarray(ar1_np_real + 1j * ar1_np_imag, dtype=op_dtype)
ar1_np = ar1_np_real + 1j * ar1_np_imag
ar1 = dpt.asarray(ar1_np, dtype=op_dtype)

ar2_np_real = np.random.randint(0, 10, sz)
ar2_np_imag = np.random.randint(0, 10, sz)
ar2 = dpt.asarray(ar2_np_real + 1j * ar2_np_imag, dtype=op_dtype)
ar2_np = ar2_np_real + 1j * ar2_np_imag
ar2 = dpt.asarray(ar2_np, dtype=op_dtype)

r = dpt.less_equal(ar1, ar2)
expected = np.less_equal(dpt.asnumpy(ar1), dpt.asnumpy(ar2))
expected = np.less_equal(ar1_np, ar2_np)
assert _compare_dtypes(r.dtype, expected.dtype, sycl_queue=q)
assert r.shape == expected.shape
assert (dpt.asnumpy(r) == expected).all()

r1 = dpt.less_equal(ar1[::-2], ar2[::2])
expected1 = np.less_equal(dpt.asnumpy(ar1[::-2]), dpt.asnumpy(ar2[::2]))
expected1 = np.less_equal(ar1_np[::-2], ar2_np[::2])
assert _compare_dtypes(r.dtype, expected1.dtype, sycl_queue=q)
assert r1.shape == expected1.shape
assert (dpt.asnumpy(r1) == expected1).all()

ar3 = dpt.asarray([1.0 + 9j, 2.0 + 0j, 2.0 + 1j, 2.0 + 2j], dtype=op_dtype)
ar4 = dpt.asarray([2.0 + 0j, dpt.nan, dpt.inf, -dpt.inf], dtype=op_dtype)

ar3_np = dpt.asnumpy(ar3)
ar4_np = dpt.asnumpy(ar4)

r2 = dpt.less_equal(ar3, ar4)
with np.errstate(invalid="ignore"):
expected2 = np.less_equal(dpt.asnumpy(ar3), dpt.asnumpy(ar4))
expected2 = np.less_equal(ar3_np, ar4_np)
assert (dpt.asnumpy(r2) == expected2).all()

r3 = dpt.less_equal(ar4, ar4)
with np.errstate(invalid="ignore"):
expected3 = np.less_equal(dpt.asnumpy(ar4), dpt.asnumpy(ar4))
expected3 = np.less_equal(ar4_np, ar4_np)
assert (dpt.asnumpy(r3) == expected3).all()


Expand All @@ -105,23 +111,27 @@ def test_less_equal_complex_float():
ar1 = dpt.asarray([1.0 + 9j, 2.0 + 0j, 2.0 + 1j, 2.0 + 2j], dtype="c8")
ar2 = dpt.full((4,), 2, dtype="f4")

ar1_np = dpt.asnumpy(ar1)
ar2_np = dpt.asnumpy(ar2)

r = dpt.less_equal(ar1, ar2)
expected = np.less_equal(dpt.asnumpy(ar1), dpt.asnumpy(ar2))
expected = np.less_equal(ar1_np, ar2_np)
assert (dpt.asnumpy(r) == expected).all()

r1 = dpt.less_equal(ar2, ar1)
expected1 = np.less_equal(dpt.asnumpy(ar2), dpt.asnumpy(ar1))
expected1 = np.less_equal(ar2_np, ar1_np)
assert (dpt.asnumpy(r1) == expected1).all()
with np.errstate(invalid="ignore"):
for tp in [dpt.nan, dpt.inf, -dpt.inf]:

ar3 = dpt.full((4,), tp)
ar3_np = dpt.asnumpy(ar3)
r2 = dpt.less_equal(ar1, ar3)
expected2 = np.less_equal(dpt.asnumpy(ar1), dpt.asnumpy(ar3))
expected2 = np.less_equal(ar1_np, ar3_np)
assert (dpt.asnumpy(r2) == expected2).all()

r3 = dpt.less_equal(ar3, ar1)
expected3 = np.less_equal(dpt.asnumpy(ar3), dpt.asnumpy(ar1))
expected3 = np.less_equal(ar3_np, ar1_np)
assert (dpt.asnumpy(r3) == expected3).all()


Expand Down
30 changes: 20 additions & 10 deletions dpctl/tests/elementwise/test_logical_and.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,22 @@ def test_logical_and_complex_matrix(op_dtype):
sz = 127
ar1_np_real = np.random.randint(0, 2, sz)
ar1_np_imag = np.random.randint(0, 2, sz)
ar1 = dpt.asarray(ar1_np_real + 1j * ar1_np_imag, dtype=op_dtype)
ar1_np = ar1_np_real + 1j * ar1_np_imag
ar1 = dpt.asarray(ar1_np, dtype=op_dtype)

ar2_np_real = np.random.randint(0, 2, sz)
ar2_np_imag = np.random.randint(0, 2, sz)
ar2 = dpt.asarray(ar2_np_real + 1j * ar2_np_imag, dtype=op_dtype)
ar2_np = ar2_np_real + 1j * ar2_np_imag
ar2 = dpt.asarray(ar2_np, dtype=op_dtype)

r = dpt.logical_and(ar1, ar2)
expected = np.logical_and(dpt.asnumpy(ar1), dpt.asnumpy(ar2))
expected = np.logical_and(ar1_np, ar2_np)
assert _compare_dtypes(r.dtype, expected.dtype, sycl_queue=q)
assert r.shape == expected.shape
assert (dpt.asnumpy(r) == expected).all()

r1 = dpt.logical_and(ar1[::-2], ar2[::2])
expected1 = np.logical_and(dpt.asnumpy(ar1[::-2]), dpt.asnumpy(ar2[::2]))
expected1 = np.logical_and(ar1_np[::-2], ar2_np[::2])
assert _compare_dtypes(r.dtype, expected1.dtype, sycl_queue=q)
assert r1.shape == expected1.shape
assert (dpt.asnumpy(r1) == expected1).all()
Expand All @@ -106,14 +108,18 @@ def test_logical_and_complex_matrix(op_dtype):
dtype=op_dtype,
)
ar4 = dpt.full(ar3.shape, fill_value=1.0 + 2j, dtype=op_dtype)

ar3_np = dpt.asnumpy(ar3)
ar4_np = dpt.asnumpy(ar4)

r2 = dpt.logical_and(ar3, ar4)
with np.errstate(invalid="ignore"):
expected2 = np.logical_and(dpt.asnumpy(ar3), dpt.asnumpy(ar4))
expected2 = np.logical_and(ar3_np, ar4_np)
assert (dpt.asnumpy(r2) == expected2).all()

r3 = dpt.logical_and(ar4, ar4)
with np.errstate(invalid="ignore"):
expected3 = np.logical_and(dpt.asnumpy(ar4), dpt.asnumpy(ar4))
expected3 = np.logical_and(ar4_np, ar4_np)
assert (dpt.asnumpy(r3) == expected3).all()


Expand All @@ -123,12 +129,15 @@ def test_logical_and_complex_float():
ar1 = dpt.asarray([1j, 1.0 + 9j, 2.0 + 0j, 2.0 + 1j], dtype="c8")
ar2 = dpt.full(ar1.shape, 2, dtype="f4")

ar1_np = dpt.asnumpy(ar1)
ar2_np = dpt.asnumpy(ar2)

r = dpt.logical_and(ar1, ar2)
expected = np.logical_and(dpt.asnumpy(ar1), dpt.asnumpy(ar2))
expected = np.logical_and(ar1_np, ar2_np)
assert (dpt.asnumpy(r) == expected).all()

r1 = dpt.logical_and(ar2, ar1)
expected1 = np.logical_and(dpt.asnumpy(ar2), dpt.asnumpy(ar1))
expected1 = np.logical_and(ar2_np, ar1_np)
assert (dpt.asnumpy(r1) == expected1).all()
with np.errstate(invalid="ignore"):
for tp in [
Expand All @@ -140,12 +149,13 @@ def test_logical_and_complex_float():
-dpt.inf * 1j,
]:
ar3 = dpt.full(ar1.shape, tp)
ar3_np = dpt.asnumpy(ar3)
r2 = dpt.logical_and(ar1, ar3)
expected2 = np.logical_and(dpt.asnumpy(ar1), dpt.asnumpy(ar3))
expected2 = np.logical_and(ar1_np, ar3_np)
assert (dpt.asnumpy(r2) == expected2).all()

r3 = dpt.logical_and(ar3, ar1)
expected3 = np.logical_and(dpt.asnumpy(ar3), dpt.asnumpy(ar1))
expected3 = np.logical_and(ar3_np, ar1_np)
assert (dpt.asnumpy(r3) == expected3).all()


Expand Down
Loading

0 comments on commit 004614e

Please sign in to comment.