Skip to content

Commit 2a0a8e5

Browse files
committed
[cppyy] lowlevel view unittest for multidim fixed-length array to numpy
1 parent 5645a80 commit 2a0a8e5

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

bindings/pyroot/cppyy/cppyy/test/test_lowlevel.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,43 @@ def test05_char_multidim(self):
743743
assert len(ns.str_array[i]) == 7
744744
assert ns.str_array[i].as_string() == v
745745

746+
def test06_fixed_multidim_array_itemsize(self):
747+
"""conversion of fixed-length array low level views into NumPy arrays"""
748+
import cppyy
749+
750+
try:
751+
import numpy as np
752+
except ImportError:
753+
skip("numpy is not installed")
754+
755+
cases = [
756+
("float", np.float32, (3, 5)),
757+
("int", np.intc, (2, 6)),
758+
("short", np.short, (5, 3)),
759+
("unsigned char", np.ubyte, (4, 4)),
760+
("int32_t", np.int32, (2, 8)),
761+
("uint16_t", np.uint16, (7, 3)),
762+
]
763+
764+
for cpp_type, np_dtype, (rows, cols) in cases:
765+
tag = cpp_type.replace(" ", "_")
766+
cppyy.cppdef(f"""
767+
struct cpp_arr_{tag} {{
768+
{cpp_type} a[{rows}][{cols}];
769+
}};
770+
""")
771+
s = getattr(cppyy.gbl, f"cpp_arr_{tag}")()
772+
773+
itemsize = np.dtype(np_dtype).itemsize
774+
mv = memoryview(s.a)
775+
assert mv.ndim == 2
776+
assert mv.shape == (rows, cols)
777+
assert mv.itemsize == itemsize
778+
assert mv.strides == (cols * itemsize, itemsize)
779+
780+
arr = np.array(s.a, dtype=np_dtype)
781+
assert arr.shape == (rows, cols)
782+
746783

747784
if __name__ == "__main__":
748785
exit(pytest.main(args=['-sv', '-ra', __file__]))

0 commit comments

Comments
 (0)