From 5655ab7b01901b9fa3464c86d331fcf2c2f330f2 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 25 Apr 2023 20:47:56 -0500 Subject: [PATCH] Unit tests for USMNdArray creation. Remove test_usm_ndarray_type_exceptions --- .../USMNdAArray/test_usm_ndarray_creation.py | 79 +++++++++++++++++++ .../test_usm_ndarray_type_exceptions.py | 31 -------- 2 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 numba_dpex/tests/core/types/USMNdAArray/test_usm_ndarray_creation.py delete mode 100644 numba_dpex/tests/core/types/USMNdAArray/test_usm_ndarray_type_exceptions.py diff --git a/numba_dpex/tests/core/types/USMNdAArray/test_usm_ndarray_creation.py b/numba_dpex/tests/core/types/USMNdAArray/test_usm_ndarray_creation.py new file mode 100644 index 0000000000..007b287190 --- /dev/null +++ b/numba_dpex/tests/core/types/USMNdAArray/test_usm_ndarray_creation.py @@ -0,0 +1,79 @@ +import dpctl +import pytest + +from numba_dpex.core.types import USMNdArray + +"""Negative tests for expected exceptions raised during USMNdArray creation. + +""" + + +def test_default_type_construction(): + """Tests call USMNdArray constructor with no device or queue args.""" + usma = USMNdArray(1, queue=None) + + assert usma.ndim == 1 + assert usma.layout == "C" + assert usma.addrspace == 1 + assert usma.usm_type == "device" + + default_device = dpctl.SyclDevice() + cached_queue = dpctl.get_device_cached_queue(default_device) + + assert usma.device == default_device.filter_string + assert usma.queue == cached_queue + + +def test_type_creation_with_device(): + """Tests creating a USMNdArray with a device arg and no queue""" + + default_device_str = dpctl.SyclDevice().filter_string + + usma = USMNdArray(1, device=default_device_str, queue=None) + + assert usma.ndim == 1 + assert usma.layout == "C" + assert usma.addrspace == 1 + assert usma.usm_type == "device" + + assert usma.device == default_device_str + + cached_queue = dpctl.get_device_cached_queue(default_device_str) + + assert usma.queue == cached_queue + + +def test_type_creation_with_queue(): + """Tests creating a USMNdArray with a queue arg and no device""" + queue = dpctl.SyclQueue() + usma = USMNdArray(1, queue=queue) + + assert usma.ndim == 1 + assert usma.layout == "C" + assert usma.addrspace == 1 + assert usma.usm_type == "device" + + assert usma.device == queue.sycl_device.filter_string + assert usma.queue == queue + + +def test_exception_when_both_device_and_queue_arg_specified(): + """Tests if TypeError is raised when both queue and device specified""" + + queue = dpctl.SyclQueue() + with pytest.raises(TypeError): + USMNdArray(1, device="cpu", queue=queue) + + +def test_improper_queue_type(): + """Tests if TypeError is raised if queue argument is of invalid type""" + + with pytest.raises(TypeError): + USMNdArray(1, queue="cpu") + + +def test_improper_device_type(): + """Tests if TypeError is raised if device argument is of invalid type""" + + with pytest.raises(TypeError): + USMNdArray(1, device=0) diff --git a/numba_dpex/tests/core/types/USMNdAArray/test_usm_ndarray_type_exceptions.py b/numba_dpex/tests/core/types/USMNdAArray/test_usm_ndarray_type_exceptions.py deleted file mode 100644 index 62aa17e663..0000000000 --- a/numba_dpex/tests/core/types/USMNdAArray/test_usm_ndarray_type_exceptions.py +++ /dev/null @@ -1,31 +0,0 @@ -# SPDX-FileCopyrightText: 2020 - 2023 Intel Corporation -# -# SPDX-License-Identifier: Apache-2.0 - -"""This is to test USMNdArray raising correct exceptions.""" - -import dpnp -import pytest -from numba import njit -from numba.core.errors import TypingError - -from numba_dpex.dpctl_iface import get_current_queue - -arguments = [ - ("shape=10", 'device="cpu"', "queue=a.sycl_queue"), - ("shape=10", "device=10", "queue=a.sycl_queue"), - ("shape=10", 'device="cpu"', "queue=test"), - ("shape=10", 'device="dpu"'), -] - - -@pytest.mark.parametrize("argument", arguments) -def test_usm_ndarray_type_exceptions(argument): - a = dpnp.ndarray(10) - - @njit - def func(a): - dpnp.empty(argument) - - with pytest.raises(TypingError): - func(a)