|
| 1 | +import dpctl |
| 2 | +import pytest |
| 3 | + |
| 4 | +from numba_dpex.core.types import USMNdArray |
| 5 | + |
| 6 | +"""Negative tests for expected exceptions raised during USMNdArray creation. |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | + |
| 11 | +def test_default_type_construction(): |
| 12 | + """Tests call USMNdArray constructor with no device or queue args.""" |
| 13 | + usma = USMNdArray(1, queue=None) |
| 14 | + |
| 15 | + assert usma.ndim == 1 |
| 16 | + assert usma.layout == "C" |
| 17 | + assert usma.addrspace == 1 |
| 18 | + assert usma.usm_type == "device" |
| 19 | + |
| 20 | + default_device = dpctl.SyclDevice() |
| 21 | + cached_queue = dpctl.get_device_cached_queue(default_device) |
| 22 | + |
| 23 | + assert usma.device == default_device.filter_string |
| 24 | + assert usma.queue == cached_queue |
| 25 | + |
| 26 | + |
| 27 | +def test_type_creation_with_device(): |
| 28 | + """Tests creating a USMNdArray with a device arg and no queue""" |
| 29 | + |
| 30 | + default_device_str = dpctl.SyclDevice().filter_string |
| 31 | + |
| 32 | + usma = USMNdArray(1, device=default_device_str, queue=None) |
| 33 | + |
| 34 | + assert usma.ndim == 1 |
| 35 | + assert usma.layout == "C" |
| 36 | + assert usma.addrspace == 1 |
| 37 | + assert usma.usm_type == "device" |
| 38 | + |
| 39 | + assert usma.device == default_device_str |
| 40 | + |
| 41 | + cached_queue = dpctl.get_device_cached_queue(default_device_str) |
| 42 | + |
| 43 | + assert usma.queue == cached_queue |
| 44 | + |
| 45 | + |
| 46 | +def test_type_creation_with_queue(): |
| 47 | + """Tests creating a USMNdArray with a queue arg and no device""" |
| 48 | + queue = dpctl.SyclQueue() |
| 49 | + usma = USMNdArray(1, queue=queue) |
| 50 | + |
| 51 | + assert usma.ndim == 1 |
| 52 | + assert usma.layout == "C" |
| 53 | + assert usma.addrspace == 1 |
| 54 | + assert usma.usm_type == "device" |
| 55 | + |
| 56 | + assert usma.device == queue.sycl_device.filter_string |
| 57 | + assert usma.queue == queue |
| 58 | + |
| 59 | + |
| 60 | +def test_exception_when_both_device_and_queue_arg_specified(): |
| 61 | + """Tests if TypeError is raised when both queue and device specified""" |
| 62 | + |
| 63 | + queue = dpctl.SyclQueue() |
| 64 | + with pytest.raises(TypeError): |
| 65 | + USMNdArray(1, device="cpu", queue=queue) |
| 66 | + |
| 67 | + |
| 68 | +def test_improper_queue_type(): |
| 69 | + """Tests if TypeError is raised if queue argument is of invalid type""" |
| 70 | + |
| 71 | + with pytest.raises(TypeError): |
| 72 | + USMNdArray(1, queue="cpu") |
| 73 | + |
| 74 | + |
| 75 | +def test_improper_device_type(): |
| 76 | + """Tests if TypeError is raised if device argument is of invalid type""" |
| 77 | + |
| 78 | + with pytest.raises(TypeError): |
| 79 | + USMNdArray(1, device=0) |
0 commit comments