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

Added tests for constructors with invalid capsules #577

Merged
merged 3 commits into from
Sep 7, 2021
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
6 changes: 4 additions & 2 deletions dpctl/_sycl_event.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ cdef class _SyclEvent:
"""

def __dealloc__(self):
DPCTLEvent_Wait(self._event_ref)
DPCTLEvent_Delete(self._event_ref)
if (self._event_ref):
DPCTLEvent_Wait(self._event_ref)
DPCTLEvent_Delete(self._event_ref)
self._event_ref = NULL
self.args = None


Expand Down
12 changes: 12 additions & 0 deletions dpctl/tests/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ def has_cpu(backend="opencl"):

def has_sycl_platforms():
return bool(len(dpctl.get_platforms()))


def create_invalid_capsule():
"""Creates an invalid capsule for the purpose of testing dpctl
constructors that accept capsules.
"""
import ctypes

ctor = ctypes.pythonapi.PyCapsule_New
ctor.restype = ctypes.py_object
ctor.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_void_p]
return ctor(id(ctor), b"invalid", 0)
17 changes: 16 additions & 1 deletion dpctl/tests/test_sycl_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

""" Defines unit test cases for the SyclContxt class.
""" Defines unit test cases for the :class:`dpctl.SyclContext` class.
"""

import pytest

import dpctl

from ._helper import create_invalid_capsule

list_of_valid_filter_selectors = [
"opencl",
"opencl:gpu",
Expand Down Expand Up @@ -210,3 +212,16 @@ def test_cpython_api():
r2 = ctx.addressof_ref()
r1 = get_context_ref_fn(ctx)
assert r1 == r2


def test_invalid_capsule():
cap = create_invalid_capsule()
with pytest.raises(ValueError):
dpctl.SyclContext(cap)


def test_multi_device_different_platforms():
devs = dpctl.get_devices() # all devices
if len(devs) > 1:
with pytest.raises(ValueError):
dpctl.SyclContext(devs)
8 changes: 7 additions & 1 deletion dpctl/tests/test_sycl_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import dpctl.program as dpctl_prog
from dpctl import event_status_type as esty

from ._helper import has_cpu
from ._helper import create_invalid_capsule, has_cpu


def produce_event(profiling=False):
Expand Down Expand Up @@ -223,6 +223,12 @@ def test_event_capsule():
del cap2


def test_event_invalid_capsule():
cap = create_invalid_capsule()
with pytest.raises(TypeError):
dpctl.SyclEvent(cap)


def test_addressof_ref():
ev = dpctl.SyclEvent()
ref = ev.addressof_ref()
Expand Down
29 changes: 28 additions & 1 deletion dpctl/tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import dpctl

from ._helper import create_invalid_capsule

list_of_standard_selectors = [
dpctl.select_accelerator_device,
dpctl.select_cpu_device,
Expand Down Expand Up @@ -359,6 +361,8 @@ def test_context_not_equals():
ctx_cpu = cpuQ.get_sycl_context()
assert ctx_cpu != ctx_gpu
assert hash(ctx_cpu) != hash(ctx_gpu)
assert gpuQ != cpuQ
assert hash(cpuQ) != hash(gpuQ)


def test_context_equals():
Expand Down Expand Up @@ -497,11 +501,34 @@ def test_constructor_many_arg():
dpctl.SyclQueue(ctx)


def test_constructor_inconsistent_ctx_dev():
try:
q = dpctl.SyclQueue("cpu")
except dpctl.SyclQueueCreationError:
pytest.skip("Failed to create CPU queue")
cpuD = q.sycl_device
n_eu = cpuD.max_compute_units
n_half = n_eu // 2
try:
d0, d1 = cpuD.create_sub_devices(partition=[n_half, n_eu - n_half])
except Exception:
pytest.skip("Could not create CPU sub-devices")
ctx = dpctl.SyclContext(d0)
with pytest.raises(dpctl.SyclQueueCreationError):
dpctl.SyclQueue(ctx, d1)


def test_constructor_invalid_capsule():
cap = create_invalid_capsule()
with pytest.raises(TypeError):
dpctl.SyclQueue(cap)


def test_queue_wait():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Failed to create device with supported filter")
pytest.skip("Failed to create default queue")
q.wait()


Expand Down