-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathstructs_13.py
57 lines (47 loc) · 1.59 KB
/
structs_13.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from lpython import (i32, i16, i64, CPtr, dataclass, ccall, Pointer,
c_p_pointer, sizeof, ccallable)
from numpy import array
@ccallable
@dataclass
class A:
x: i32
y: i16
@ccall
def cmalloc(size: i64) -> CPtr:
pass
@ccall
def is_null(ptr: CPtr) -> i32:
pass
def add_A_members(Ax: i32, Ay: i16) -> i32:
return Ax + i32(Ay)
@ccall
def add_Aptr_members(Ax: i32, Ay: i16) -> i32:
pass
def test_A_member_passing():
array_cptr: CPtr = cmalloc(sizeof(A) * i64(10))
assert not bool(is_null(array_cptr)), "Failed to allocate array on memory"
array_ptr: Pointer[A[:]] = c_p_pointer(array_cptr, A[:], array([10]))
i: i32; sum_A_members: i32
for i in range(10):
array_ptr[i] = A(i, i16(i + 1))
for i in range(5):
a: A = array_ptr[i]
sum_A_members = add_A_members(a.x, a.y)
assert a.x == i
assert a.y == i16(i + 1)
print(sum_A_members)
assert sum_A_members == 2*i + 1
for i in range(6, 10):
sum_A_members = add_A_members(array_ptr[i].x, array_ptr[i].y)
assert array_ptr[i].x == i
assert array_ptr[i].y == i16(i + 1)
print(sum_A_members)
assert sum_A_members == 2*i + 1
def test_Aptr_member_passing():
a_cptr: CPtr = cmalloc(sizeof(A) * i64(1))
assert not bool(is_null(a_cptr)), "Failed to allocate array on memory"
a_ptr: Pointer[A] = c_p_pointer(a_cptr, A)
print(add_A_members(a_ptr.x, a_ptr.y), add_Aptr_members(a_ptr.x, a_ptr.y))
assert add_A_members(a_ptr.x, a_ptr.y) == add_Aptr_members(a_ptr.x, a_ptr.y)
test_A_member_passing()
test_Aptr_member_passing()