diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index 3c802ff165b..46424634d29 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -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) @@ -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])] ) @@ -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): diff --git a/dpnp/tests/test_binary_ufuncs.py b/dpnp/tests/test_binary_ufuncs.py index 95a8d09852a..6fae555194e 100644 --- a/dpnp/tests/test_binary_ufuncs.py +++ b/dpnp/tests/test_binary_ufuncs.py @@ -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): diff --git a/dpnp/tests/test_fft.py b/dpnp/tests/test_fft.py index c54401fd8bf..418f05f47c6 100644 --- a/dpnp/tests/test_fft.py +++ b/dpnp/tests/test_fft.py @@ -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): diff --git a/dpnp/tests/test_flat.py b/dpnp/tests/test_flat.py index e061520f61c..c40e95d3ee8 100644 --- a/dpnp/tests/test_flat.py +++ b/dpnp/tests/test_flat.py @@ -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] diff --git a/dpnp/tests/test_ndarray.py b/dpnp/tests/test_ndarray.py index ecc560aa918..0a394c29158 100644 --- a/dpnp/tests/test_ndarray.py +++ b/dpnp/tests/test_ndarray.py @@ -4,6 +4,7 @@ from numpy.testing import ( assert_allclose, assert_array_equal, + assert_equal, assert_raises_regex, ) @@ -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", diff --git a/dpnp/tests/test_sycl_queue.py b/dpnp/tests/test_sycl_queue.py index dff8618cca6..7e3218b2b41 100644 --- a/dpnp/tests/test_sycl_queue.py +++ b/dpnp/tests/test_sycl_queue.py @@ -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)], {}),