Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
88 changes: 88 additions & 0 deletions dpnp/tests/test_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,33 @@ def test_error(self):
assert_raises(TypeError, dpnp.array, x, ndmin=3.0)


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)


class TestTrace:
@pytest.mark.parametrize("a_sh", [(3, 4), (2, 2, 2)])
@pytest.mark.parametrize(
Expand Down Expand Up @@ -214,6 +241,20 @@ def test_arange(start, stop, step, dtype):
assert_array_equal(exp_array, res_array)


@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.dpnp_array.dpnp_array._create_from_usm_ndarray(arr)


@pytest.mark.parametrize("func", ["diag", "diagflat"])
@pytest.mark.parametrize("k", [-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6])
@pytest.mark.parametrize(
Expand Down Expand Up @@ -341,6 +382,12 @@ def test_identity(n, dtype):
assert_array_equal(func(numpy), func(dpnp))


def test_identity_error():
# negative dimensions
with pytest.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 @@ -917,6 +964,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 @@ -962,6 +1015,41 @@ 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, 5j), # complex 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
],
)
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
6 changes: 6 additions & 0 deletions dpnp/tests/test_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,12 @@ 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
with pytest.raises(ValueError):
_ = dpnp.fft.ihfft(a, norm="backwards")


class TestIrfft:
def setup_method(self):
Expand Down
85 changes: 52 additions & 33 deletions dpnp/tests/test_flat.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
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

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):
with pytest.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]
Loading