Skip to content

Commit 5649b06

Browse files
Increase code coverage for .py files (#2268)
This PR suggests increasing code coverage by adding missing tests for uncovered lines of code for `python` files --------- Co-authored-by: Anton <[email protected]>
1 parent d415fe2 commit 5649b06

File tree

6 files changed

+167
-33
lines changed

6 files changed

+167
-33
lines changed

dpnp/tests/test_arraycreation.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ def test_identity(n, dtype):
342342
assert_array_equal(func(numpy), func(dpnp))
343343

344344

345+
def test_identity_error():
346+
# negative dimensions
347+
assert_raises(ValueError, dpnp.identity, -5)
348+
349+
345350
@pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False))
346351
def test_loadtxt(dtype):
347352
func = lambda xp: xp.loadtxt(fh, dtype=dtype)
@@ -918,6 +923,12 @@ def test_logspace_axis(axis):
918923
assert_dtype_allclose(func(dpnp), func(numpy))
919924

920925

926+
def test_logspace_list_input():
927+
res_np = numpy.logspace([0], [2], base=[5])
928+
res_dp = dpnp.logspace([0], [2], base=[5])
929+
assert_allclose(res_dp, res_np)
930+
931+
921932
@pytest.mark.parametrize(
922933
"data", [(), 1, (2, 3), [4], numpy.array(5), numpy.array([6, 7])]
923934
)
@@ -963,6 +974,48 @@ def test_meshgrid_raise_error():
963974
dpnp.meshgrid(b, indexing="ab")
964975

965976

977+
class TestMgrid:
978+
def check_results(self, result, expected):
979+
if isinstance(result, (list, tuple)):
980+
assert len(result) == len(expected)
981+
for dp_arr, np_arr in zip(result, expected):
982+
assert_allclose(dp_arr, np_arr)
983+
else:
984+
assert_allclose(result, expected)
985+
986+
@pytest.mark.parametrize(
987+
"slice",
988+
[
989+
slice(0, 5, 0.5), # float step
990+
slice(0, 5, 1j), # complex step
991+
slice(0, 5, 5j), # complex step
992+
slice(None, 5, 1), # no start
993+
slice(0, 5, None), # no step
994+
],
995+
)
996+
def test_single_slice(self, slice):
997+
dpnp_result = dpnp.mgrid[slice]
998+
numpy_result = numpy.mgrid[slice]
999+
self.check_results(dpnp_result, numpy_result)
1000+
1001+
@pytest.mark.parametrize(
1002+
"slices",
1003+
[
1004+
(slice(None, 5, 1), slice(None, 10, 2)), # no start
1005+
(slice(0, 5), slice(0, 10)), # no step
1006+
(slice(0, 5.5, 1), slice(0, 10, 3j)), # float stop and complex step
1007+
(
1008+
slice(0.0, 5, 1),
1009+
slice(0, 10, 1j),
1010+
), # float start and complex step
1011+
],
1012+
)
1013+
def test_md_slice(self, slices):
1014+
dpnp_result = dpnp.mgrid[slices]
1015+
numpy_result = numpy.mgrid[slices]
1016+
self.check_results(dpnp_result, numpy_result)
1017+
1018+
9661019
def test_exception_tri():
9671020
x = dpnp.ones((2, 2))
9681021
with pytest.raises(TypeError):

dpnp/tests/test_binary_ufuncs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,17 @@ def test_inplace_strides(self, func, dtype):
320320

321321
assert_dtype_allclose(ia, a)
322322

323+
@pytest.mark.parametrize("dtype", ALL_DTYPES)
324+
def test_inplace_scalar(self, func, dtype):
325+
326+
a = numpy.array(10, dtype=dtype)
327+
self.do_inplace_op(10, a, func)
328+
329+
ia = dpnp.array(10, dtype=dtype)
330+
self.do_inplace_op(10, ia, func)
331+
332+
assert_dtype_allclose(ia, a)
333+
323334
@pytest.mark.parametrize("dtype1", [dpnp.bool] + ALL_DTYPES)
324335
@pytest.mark.parametrize("dtype2", get_float_dtypes())
325336
def test_inplace_dtype(self, func, dtype1, dtype2):

dpnp/tests/test_fft.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,11 @@ def test_ihfft_bool(self, n, norm):
670670
expected = numpy.fft.ihfft(a_np, n=n, norm=norm)
671671
assert_dtype_allclose(result, expected, check_only_type_kind=True)
672672

673+
def test_ihfft_error(self):
674+
a = dpnp.ones(11)
675+
# incorrect norm
676+
assert_raises(ValueError, dpnp.fft.ihfft, a, norm="backwards")
677+
673678

674679
class TestIrfft:
675680
def setup_method(self):

dpnp/tests/test_flat.py

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
1-
import numpy
1+
import numpy as np
22
import pytest
3-
4-
import dpnp as inp
5-
6-
7-
@pytest.mark.parametrize("type", [numpy.int64], ids=["int64"])
8-
def test_flat(type):
9-
a = numpy.array([1, 0, 2, -3, -1, 2, 21, -9])
10-
ia = inp.array(a)
11-
12-
result = ia.flat[0]
13-
expected = a.flat[0]
14-
numpy.testing.assert_array_equal(expected, result)
15-
16-
17-
@pytest.mark.parametrize("type", [numpy.int64], ids=["int64"])
18-
def test_flat2(type):
19-
a = numpy.arange(1, 7).reshape(2, 3)
20-
ia = inp.array(a)
21-
22-
result = ia.flat[3]
23-
expected = a.flat[3]
24-
numpy.testing.assert_array_equal(expected, result)
25-
26-
27-
@pytest.mark.parametrize("type", [numpy.int64], ids=["int64"])
28-
def test_flat3(type):
29-
a = numpy.arange(1, 7).reshape(2, 3).T
30-
ia = inp.array(a)
31-
32-
result = ia.flat[3]
33-
expected = a.flat[3]
34-
numpy.testing.assert_array_equal(expected, result)
3+
from numpy.testing import assert_array_equal, assert_raises
4+
5+
import dpnp
6+
7+
8+
class TestFlatiter:
9+
@pytest.mark.parametrize(
10+
"a, index",
11+
[
12+
(np.array([1, 0, 2, -3, -1, 2, 21, -9]), 0),
13+
(np.arange(1, 7).reshape(2, 3), 3),
14+
(np.arange(1, 7).reshape(2, 3).T, 3),
15+
],
16+
ids=["1D array", "2D array", "2D.T array"],
17+
)
18+
def test_flat_getitem(self, a, index):
19+
a_dp = dpnp.array(a)
20+
result = a_dp.flat[index]
21+
expected = a.flat[index]
22+
assert_array_equal(expected, result)
23+
24+
def test_flat_iteration(self):
25+
a = np.array([[1, 2], [3, 4]])
26+
a_dp = dpnp.array(a)
27+
for dp_val, np_val in zip(a_dp.flat, a.flat):
28+
assert dp_val == np_val
29+
30+
def test_init_error(self):
31+
assert_raises(TypeError, dpnp.flatiter, [1, 2, 3])
32+
33+
def test_flat_key_error(self):
34+
a_dp = dpnp.array(42)
35+
with pytest.raises(KeyError):
36+
_ = a_dp.flat[1]
37+
38+
def test_flat_invalid_key(self):
39+
a_dp = dpnp.array([1, 2, 3])
40+
flat = dpnp.flatiter(a_dp)
41+
# check __getitem__
42+
with pytest.raises(TypeError):
43+
_ = flat["invalid"]
44+
# check __setitem__
45+
with pytest.raises(TypeError):
46+
flat["invalid"] = 42
47+
48+
def test_flat_out_of_bounds(self):
49+
a_dp = dpnp.array([1, 2, 3])
50+
flat = dpnp.flatiter(a_dp)
51+
with pytest.raises(IndexError):
52+
_ = flat[10]

dpnp/tests/test_ndarray.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from numpy.testing import (
55
assert_allclose,
66
assert_array_equal,
7+
assert_equal,
78
assert_raises_regex,
89
)
910

@@ -40,6 +41,51 @@ def test_astype_subok_error():
4041
x.astype("i4", subok=False)
4142

4243

44+
class TestAttributes:
45+
def setup_method(self):
46+
self.one = dpnp.arange(10)
47+
self.two = dpnp.arange(20).reshape(4, 5)
48+
self.three = dpnp.arange(60).reshape(2, 5, 6)
49+
50+
def test_attributes(self):
51+
assert_equal(self.one.shape, (10,))
52+
assert_equal(self.two.shape, (4, 5))
53+
assert_equal(self.three.shape, (2, 5, 6))
54+
55+
self.three.shape = (10, 3, 2)
56+
assert_equal(self.three.shape, (10, 3, 2))
57+
self.three.shape = (2, 5, 6)
58+
59+
assert_equal(self.one.strides, (self.one.itemsize / self.one.itemsize,))
60+
num = self.two.itemsize / self.two.itemsize
61+
assert_equal(self.two.strides, (5 * num, num))
62+
num = self.three.itemsize / self.three.itemsize
63+
assert_equal(self.three.strides, (30 * num, 6 * num, num))
64+
65+
assert_equal(self.one.ndim, 1)
66+
assert_equal(self.two.ndim, 2)
67+
assert_equal(self.three.ndim, 3)
68+
69+
num = self.two.itemsize
70+
assert_equal(self.two.size, 20)
71+
assert_equal(self.two.nbytes, 20 * num)
72+
assert_equal(self.two.itemsize, self.two.dtype.itemsize)
73+
74+
75+
@pytest.mark.parametrize(
76+
"arr",
77+
[
78+
numpy.array([1]),
79+
dpnp.array([1]),
80+
[1],
81+
],
82+
ids=["numpy", "dpnp", "list"],
83+
)
84+
def test_create_from_usm_ndarray_error(arr):
85+
with pytest.raises(TypeError):
86+
dpnp.ndarray._create_from_usm_ndarray(arr)
87+
88+
4389
@pytest.mark.parametrize("arr_dtype", get_all_dtypes())
4490
@pytest.mark.parametrize(
4591
"arr",

dpnp/tests/test_sycl_queue.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def vvsort(val, vec, size, xp):
112112
pytest.param("identity", [4], {}),
113113
pytest.param("linspace", [0, 4, 8], {}),
114114
pytest.param("logspace", [0, 4, 8], {}),
115+
pytest.param("logspace", [0, 4, 8], {"base": [10]}),
115116
pytest.param("ones", [(2, 2)], {}),
116117
pytest.param("tri", [3, 5, 2], {}),
117118
pytest.param("zeros", [(2, 2)], {}),

0 commit comments

Comments
 (0)