Skip to content

Commit 145c6ab

Browse files
committed
Test buffer contents if on CPU
Signed-off-by: Vasily Litvinov <vasilij.n.litvinov@intel.com>
1 parent 4adcd6b commit 145c6ab

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

pandas/tests/api/test_protocol.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import math
3+
import ctypes
34

45
@pytest.mark.parametrize("test_data",
56
[
@@ -98,7 +99,8 @@ def test_get_columns(df_from_dict):
9899
assert dfX.get_column(1).dtype[0] == 2
99100

100101
def test_buffer(df_from_dict):
101-
df = df_from_dict({"a": [0, 1]})
102+
arr = [0, 1, -1]
103+
df = df_from_dict({"a": arr})
102104
dfX = df.__dataframe__()
103105
colX = dfX.get_column(0)
104106
bufX = colX.get_buffers()
@@ -107,6 +109,17 @@ def test_buffer(df_from_dict):
107109

108110
assert dataBuf.bufsize > 0
109111
assert dataBuf.ptr != 0
110-
assert dataBuf.__dlpack_device__
112+
device, _ = dataBuf.__dlpack_device__
111113

112114
assert dataDtype[0] == 0
115+
116+
if device == 1: # CPU-only as we're going to directly read memory here
117+
bitwidth = dataDtype[1]
118+
ctype = {8: ctypes.c_int8,
119+
16: ctypes.c_int16,
120+
32: ctypes.c_int32,
121+
64: ctypes.c_int64}[bitwidth]
122+
123+
for idx, truth in enumerate(arr):
124+
val = ctype.from_address(dataBuf.ptr + idx * (bitwidth // 8)).value
125+
assert val == truth, f"Buffer at index {idx} mismatch"

0 commit comments

Comments
 (0)