Skip to content

Commit de65afc

Browse files
committed
Add a test for a struct from a C pointer
1 parent f2500fa commit de65afc

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ RUN(NAME bindc_04 LABELS llvm c NOFAST)
374374
RUN(NAME bindc_07 LABELS cpython llvm c NOFAST)
375375
RUN(NAME bindc_08 LABELS cpython llvm c)
376376
RUN(NAME bindc_09 LABELS cpython llvm c)
377+
RUN(NAME bindc_10 LABELS cpython llvm c)
377378
RUN(NAME exit_01 LABELS cpython llvm c NOFAST)
378379
RUN(NAME exit_02 FAIL LABELS cpython llvm c NOFAST)
379380
RUN(NAME exit_03 LABELS cpython llvm c wasm wasm_x86 wasm_x64)

integration_tests/bindc_10.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from lpython import (i64, i16, CPtr, c_p_pointer, Pointer, sizeof, packed,
2+
dataclass, ccallable, ccall, i32)
3+
4+
@ccall
5+
def _lfortran_malloc(size: i32) -> CPtr:
6+
pass
7+
8+
9+
def alloc(buf_size:i64) -> CPtr:
10+
return _lfortran_malloc(i32(buf_size))
11+
12+
13+
@ccallable
14+
@packed
15+
@dataclass
16+
class S:
17+
a: i16
18+
b: i64
19+
20+
21+
def main():
22+
p1: CPtr = alloc(sizeof(S))
23+
print(p1)
24+
p2: Pointer[S] = c_p_pointer(p1, S)
25+
p2.a = i16(5)
26+
p2.b = i64(4)
27+
print(p2.a, p2.b)
28+
assert p2.a == i16(5)
29+
assert p2.b == i64(4)
30+
31+
32+
main()

0 commit comments

Comments
 (0)