Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add a test for a struct from a C pointer #2126

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

Merged
merged 3 commits into from
Jul 9, 2023
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
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ RUN(NAME bindc_04 LABELS llvm c NOFAST)
RUN(NAME bindc_07 LABELS cpython llvm c NOFAST)
RUN(NAME bindc_08 LABELS cpython llvm c)
RUN(NAME bindc_09 LABELS cpython llvm c)
RUN(NAME bindc_10 LABELS cpython llvm c NOFAST)
RUN(NAME bindc_11 LABELS cpython) # This is CPython test only
RUN(NAME exit_01 LABELS cpython llvm c NOFAST)
RUN(NAME exit_02 FAIL LABELS cpython llvm c NOFAST)
RUN(NAME exit_03 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
Expand Down
32 changes: 32 additions & 0 deletions integration_tests/bindc_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from lpython import (i64, i16, CPtr, c_p_pointer, Pointer, sizeof, packed,
dataclass, ccallable, ccall, i32)

@ccall
def _lfortran_malloc(size: i32) -> CPtr:
pass


def alloc(buf_size:i64) -> CPtr:
return _lfortran_malloc(i32(buf_size))


@ccallable
@packed
@dataclass
class S:
a: i16
b: i64


def main():
p1: CPtr = alloc(sizeof(S))
print(p1)
p2: Pointer[S] = c_p_pointer(p1, S)
p2.a = i16(5)
p2.b = i64(4)
print(p2.a, p2.b)
assert p2.a == i16(5)
assert p2.b == i64(4)


main()
34 changes: 34 additions & 0 deletions integration_tests/bindc_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy, ctypes
from lpython import (i64, i16, CPtr, c_p_pointer, Pointer, sizeof, packed,
dataclass, ccallable, ccall, i32)

global_arrays = []


def alloc(buf_size:i64) -> CPtr:
xs = numpy.empty(buf_size, dtype=numpy.uint8)
global_arrays.append(xs)
p = ctypes.c_void_p(xs.ctypes.data)
return ctypes.cast(p.value, ctypes.c_void_p)


@ccallable
@packed
@dataclass
class S:
a: i16
b: i64


def main():
p1: CPtr = alloc(sizeof(S))
print(p1)
p2: Pointer[S] = c_p_pointer(p1, S)
p2.a = i16(5)
p2.b = i64(4)
print(p2.a, p2.b)
assert p2.a == i16(5)
assert p2.b == i64(4)


main()