Skip to content

Commit 4f08b2f

Browse files
Add tests for SyclQueue.memset
1 parent 30834c0 commit 4f08b2f

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2026 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Defines unit test cases for the SyclQueue.memset."""
18+
19+
import pytest
20+
21+
import dpctl
22+
import dpctl.memory
23+
24+
25+
def _create_memory(q, nbytes=1024):
26+
return dpctl.memory.MemoryUSMShared(nbytes, queue=q)
27+
28+
29+
def test_memset_fills_whole_allocation():
30+
try:
31+
q = dpctl.SyclQueue()
32+
except dpctl.SyclQueueCreationError:
33+
pytest.skip("Default constructor for SyclQueue failed")
34+
nbytes = 256
35+
mobj = _create_memory(q, nbytes)
36+
37+
q.memset(mobj, 0xAB)
38+
39+
assert bytes(memoryview(mobj)) == b"\xab" * nbytes
40+
41+
42+
def test_memset_zero_count_fills_whole_allocation():
43+
try:
44+
q = dpctl.SyclQueue()
45+
except dpctl.SyclQueueCreationError:
46+
pytest.skip("Default constructor for SyclQueue failed")
47+
nbytes = 64
48+
mobj = _create_memory(q, nbytes)
49+
50+
q.memset(mobj, 0x01, 0)
51+
52+
assert bytes(memoryview(mobj)) == b"\x01" * nbytes
53+
54+
55+
def test_memset_partial_count():
56+
try:
57+
q = dpctl.SyclQueue()
58+
except dpctl.SyclQueueCreationError:
59+
pytest.skip("Default constructor for SyclQueue failed")
60+
nbytes = 16
61+
mobj = _create_memory(q, nbytes)
62+
63+
# zero-out first, then fill only the leading 4 bytes
64+
q.memset(mobj, 0x00)
65+
q.memset(mobj, 0x7F, 4)
66+
67+
assert bytes(memoryview(mobj)) == b"\x7f" * 4 + b"\x00" * (nbytes - 4)
68+
69+
70+
def test_memset_count_clamped_to_allocation():
71+
try:
72+
q = dpctl.SyclQueue()
73+
except dpctl.SyclQueueCreationError:
74+
pytest.skip("Default constructor for SyclQueue failed")
75+
nbytes = 8
76+
mobj = _create_memory(q, nbytes)
77+
78+
# requesting more bytes than allocated must not overrun; it is clamped
79+
q.memset(mobj, 0x02, 4 * nbytes)
80+
81+
assert bytes(memoryview(mobj)) == b"\x02" * nbytes
82+
83+
84+
def test_memset_zero_value():
85+
try:
86+
q = dpctl.SyclQueue()
87+
except dpctl.SyclQueueCreationError:
88+
pytest.skip("Default constructor for SyclQueue failed")
89+
nbytes = 32
90+
mobj = _create_memory(q, nbytes)
91+
92+
q.memset(mobj, 0xFF)
93+
q.memset(mobj, 0)
94+
95+
assert bytes(memoryview(mobj)) == b"\x00" * nbytes
96+
97+
98+
def test_memset_type_error():
99+
try:
100+
q = dpctl.SyclQueue()
101+
except dpctl.SyclQueueCreationError:
102+
pytest.skip("Default constructor for SyclQueue failed")
103+
104+
with pytest.raises(TypeError) as cm:
105+
q.memset(None, 1)
106+
assert "_Memory" in str(cm.value)

0 commit comments

Comments
 (0)