Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase code coverage for .py files #2268

Merged
merged 17 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 53 additions & 0 deletions dpnp/tests/test_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ def test_identity(n, dtype):
assert_array_equal(func(numpy), func(dpnp))


def test_identity_error():
# negative dimensions
assert_raises(ValueError, dpnp.identity, -5)


@pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False))
def test_loadtxt(dtype):
func = lambda xp: xp.loadtxt(fh, dtype=dtype)
Expand Down Expand Up @@ -918,6 +923,12 @@ def test_logspace_axis(axis):
assert_dtype_allclose(func(dpnp), func(numpy))


def test_logspace_list_input():
res_np = numpy.logspace([0], [2], base=[5])
res_dp = dpnp.logspace([0], [2], base=[5])
assert_allclose(res_dp, res_np)


@pytest.mark.parametrize(
"data", [(), 1, (2, 3), [4], numpy.array(5), numpy.array([6, 7])]
)
Expand Down Expand Up @@ -963,6 +974,48 @@ def test_meshgrid_raise_error():
dpnp.meshgrid(b, indexing="ab")


class TestMgrid:
def check_results(self, result, expected):
if isinstance(result, (list, tuple)):
assert len(result) == len(expected)
for dp_arr, np_arr in zip(result, expected):
assert_allclose(dp_arr, np_arr)
else:
assert_allclose(result, expected)

@pytest.mark.parametrize(
"slice",
[
slice(0, 5, 0.5), # float step
slice(0, 5, 1j), # complex step
slice(0, 5, 5j), # complex step
slice(None, 5, 1), # no start
slice(0, 5, None), # no step
],
)
def test_single_slice(self, slice):
dpnp_result = dpnp.mgrid[slice]
numpy_result = numpy.mgrid[slice]
self.check_results(dpnp_result, numpy_result)

@pytest.mark.parametrize(
"slices",
[
(slice(None, 5, 1), slice(None, 10, 2)), # no start
(slice(0, 5), slice(0, 10)), # no step
(slice(0, 5.5, 1), slice(0, 10, 3j)), # float stop and complex step
(
slice(0.0, 5, 1),
slice(0, 10, 1j),
), # float start and complex step
],
)
def test_md_slice(self, slices):
dpnp_result = dpnp.mgrid[slices]
numpy_result = numpy.mgrid[slices]
self.check_results(dpnp_result, numpy_result)


def test_exception_tri():
x = dpnp.ones((2, 2))
with pytest.raises(TypeError):
Expand Down
11 changes: 11 additions & 0 deletions dpnp/tests/test_binary_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ def test_inplace_strides(self, func, dtype):

assert_dtype_allclose(ia, a)

@pytest.mark.parametrize("dtype", ALL_DTYPES)
def test_inplace_scalar(self, func, dtype):

a = numpy.array(10, dtype=dtype)
self.do_inplace_op(10, a, func)

ia = dpnp.array(10, dtype=dtype)
self.do_inplace_op(10, ia, func)

assert_dtype_allclose(ia, a)

@pytest.mark.parametrize("dtype1", [dpnp.bool] + ALL_DTYPES)
@pytest.mark.parametrize("dtype2", get_float_dtypes())
def test_inplace_dtype(self, func, dtype1, dtype2):
Expand Down
5 changes: 5 additions & 0 deletions dpnp/tests/test_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,11 @@ def test_ihfft_bool(self, n, norm):
expected = numpy.fft.ihfft(a_np, n=n, norm=norm)
assert_dtype_allclose(result, expected, check_only_type_kind=True)

def test_ihfft_error(self):
a = dpnp.ones(11)
# incorrect norm
assert_raises(ValueError, dpnp.fft.ihfft, a, norm="backwards")


class TestIrfft:
def setup_method(self):
Expand Down
84 changes: 51 additions & 33 deletions dpnp/tests/test_flat.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
import numpy
import numpy as np
import pytest

import dpnp as inp


@pytest.mark.parametrize("type", [numpy.int64], ids=["int64"])
def test_flat(type):
a = numpy.array([1, 0, 2, -3, -1, 2, 21, -9])
ia = inp.array(a)

result = ia.flat[0]
expected = a.flat[0]
numpy.testing.assert_array_equal(expected, result)


@pytest.mark.parametrize("type", [numpy.int64], ids=["int64"])
def test_flat2(type):
a = numpy.arange(1, 7).reshape(2, 3)
ia = inp.array(a)

result = ia.flat[3]
expected = a.flat[3]
numpy.testing.assert_array_equal(expected, result)


@pytest.mark.parametrize("type", [numpy.int64], ids=["int64"])
def test_flat3(type):
a = numpy.arange(1, 7).reshape(2, 3).T
ia = inp.array(a)

result = ia.flat[3]
expected = a.flat[3]
numpy.testing.assert_array_equal(expected, result)
from numpy.testing import assert_array_equal, assert_raises

import dpnp


class TestFlatiter:
@pytest.mark.parametrize(
"a, index",
[
(np.array([1, 0, 2, -3, -1, 2, 21, -9]), 0),
(np.arange(1, 7).reshape(2, 3), 3),
(np.arange(1, 7).reshape(2, 3).T, 3),
],
ids=["1D array", "2D array", "2D.T array"],
)
def test_flat_getitem(self, a, index):
a_dp = dpnp.array(a)
result = a_dp.flat[index]
expected = a.flat[index]
assert_array_equal(expected, result)

def test_flat_iteration(self):
a = np.array([[1, 2], [3, 4]])
a_dp = dpnp.array(a)
for dp_val, np_val in zip(a_dp.flat, a.flat):
assert dp_val == np_val

def test_init_error(self):
assert_raises(TypeError, dpnp.flatiter, [1, 2, 3])

def test_flat_key_error(self):
a_dp = dpnp.array(42)
with pytest.raises(KeyError):
_ = a_dp.flat[1]

def test_flat_invalid_key(self):
a_dp = dpnp.array([1, 2, 3])
flat = dpnp.flatiter(a_dp)
# check __getitem__
with pytest.raises(TypeError):
_ = flat["invalid"]
# check __setitem__
with pytest.raises(TypeError):
flat["invalid"] = 42

def test_flat_out_of_bounds(self):
a_dp = dpnp.array([1, 2, 3])
flat = dpnp.flatiter(a_dp)
with pytest.raises(IndexError):
_ = flat[10]
46 changes: 46 additions & 0 deletions dpnp/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from numpy.testing import (
assert_allclose,
assert_array_equal,
assert_equal,
assert_raises_regex,
)

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


class TestAttributes:
def setup_method(self):
self.one = dpnp.arange(10)
self.two = dpnp.arange(20).reshape(4, 5)
self.three = dpnp.arange(60).reshape(2, 5, 6)

def test_attributes(self):
assert_equal(self.one.shape, (10,))
assert_equal(self.two.shape, (4, 5))
assert_equal(self.three.shape, (2, 5, 6))

self.three.shape = (10, 3, 2)
assert_equal(self.three.shape, (10, 3, 2))
self.three.shape = (2, 5, 6)

assert_equal(self.one.strides, (self.one.itemsize / self.one.itemsize,))
num = self.two.itemsize / self.two.itemsize
assert_equal(self.two.strides, (5 * num, num))
num = self.three.itemsize / self.three.itemsize
assert_equal(self.three.strides, (30 * num, 6 * num, num))

assert_equal(self.one.ndim, 1)
assert_equal(self.two.ndim, 2)
assert_equal(self.three.ndim, 3)

num = self.two.itemsize
assert_equal(self.two.size, 20)
assert_equal(self.two.nbytes, 20 * num)
assert_equal(self.two.itemsize, self.two.dtype.itemsize)


@pytest.mark.parametrize(
"arr",
[
numpy.array([1]),
dpnp.array([1]),
[1],
],
ids=["numpy", "dpnp", "list"],
)
def test_create_from_usm_ndarray_error(arr):
with pytest.raises(TypeError):
dpnp.ndarray._create_from_usm_ndarray(arr)


@pytest.mark.parametrize("arr_dtype", get_all_dtypes())
@pytest.mark.parametrize(
"arr",
Expand Down
1 change: 1 addition & 0 deletions dpnp/tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def vvsort(val, vec, size, xp):
pytest.param("identity", [4], {}),
pytest.param("linspace", [0, 4, 8], {}),
pytest.param("logspace", [0, 4, 8], {}),
pytest.param("logspace", [0, 4, 8], {"base": [10]}),
pytest.param("ones", [(2, 2)], {}),
pytest.param("tri", [3, 5, 2], {}),
pytest.param("zeros", [(2, 2)], {}),
Expand Down
Loading