Skip to content

Commit 613412c

Browse files
committed
TEST: Add tests for array as ret type
1 parent fe6c58d commit 613412c

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ RUN(NAME bindc_06 LABELS llvm c
533533
EXTRAFILES bindc_06b.c)
534534
RUN(NAME bindpy_01 LABELS cpython c_py ENABLE_CPYTHON NOFAST EXTRAFILES bindpy_01_module.py)
535535
RUN(NAME bindpy_02 LABELS cpython c_py LINK_NUMPY EXTRAFILES bindpy_02_module.py)
536+
RUN(NAME bindpy_03 LABELS cpython c_py LINK_NUMPY NOFAST EXTRAFILES bindpy_03_module.py)
536537
RUN(NAME test_generics_01 LABELS cpython llvm c NOFAST)
537538
RUN(NAME test_cmath LABELS cpython llvm c NOFAST)
538539
RUN(NAME test_complex_01 LABELS cpython llvm c wasm wasm_x64)

integration_tests/bindpy_03.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
from lpython import i32, i64, f64, pythoncall, Const, TypeVar
2+
from numpy import empty, int32, int64, float64
3+
4+
n = TypeVar("n")
5+
m = TypeVar("m")
6+
p = TypeVar("p")
7+
q = TypeVar("q")
8+
r = TypeVar("r")
9+
10+
@pythoncall(module = "bindpy_03_module")
11+
def get_cpython_version() -> str:
12+
pass
13+
14+
@pythoncall(module = "bindpy_03_module")
15+
def get_int_array_sum(n: i32, a: i32[:], b: i32[:]) -> i32[n]:
16+
pass
17+
18+
@pythoncall(module = "bindpy_03_module")
19+
def get_int_array_product(n: i32, a: i32[:], b: i32[:]) -> i32[n]:
20+
pass
21+
22+
@pythoncall(module = "bindpy_03_module")
23+
def get_float_array_sum(n: i32, m: i32, a: f64[:], b: f64[:]) -> f64[n, m]:
24+
pass
25+
26+
@pythoncall(module = "bindpy_03_module")
27+
def get_float_array_product(n: i32, m: i32, a: f64[:], b: f64[:]) -> f64[n, m]:
28+
pass
29+
30+
@pythoncall(module = "bindpy_03_module")
31+
def get_array_dot_product(m: i32, a: i64[:], b: f64[:]) -> f64[m]:
32+
pass
33+
34+
@pythoncall(module = "bindpy_03_module")
35+
def get_multidim_array_i64(p: i32, q: i32, r: i32) -> i64[p, q, r]:
36+
pass
37+
38+
# Integers:
39+
def test_array_ints():
40+
n: Const[i32] = 5
41+
a: i32[n] = empty([n], dtype=int32)
42+
b: i32[n] = empty([n], dtype=int32)
43+
44+
i: i32
45+
for i in range(n):
46+
a[i] = i + 10
47+
for i in range(n):
48+
b[i] = i + 20
49+
50+
c: i32[n] = get_int_array_sum(n, a, b)
51+
print(c)
52+
for i in range(n):
53+
assert c[i] == (i + i + 30)
54+
55+
56+
c = get_int_array_product(n, a, b)
57+
print(c)
58+
for i in range(n):
59+
assert c[i] == ((i + 10) * (i + 20))
60+
61+
# Floats
62+
def test_array_floats():
63+
n: Const[i32] = 3
64+
m: Const[i32] = 5
65+
a: f64[n, m] = empty([n, m], dtype=float64)
66+
b: f64[n, m] = empty([n, m], dtype=float64)
67+
68+
i: i32
69+
j: i32
70+
71+
for i in range(n):
72+
for j in range(m):
73+
a[i, j] = f64((i + 10) * (j + 10))
74+
75+
for i in range(n):
76+
for j in range(m):
77+
b[i, j] = f64((i + 20) * (j + 20))
78+
79+
c: f64[n, m] = get_float_array_sum(n, m, a, b)
80+
print(c)
81+
for i in range(n):
82+
for j in range(m):
83+
assert abs(c[i, j] - (f64((i + 10) * (j + 10)) + f64((i + 20) * (j + 20)))) <= 1e-4
84+
85+
c = get_float_array_product(n, m, a, b)
86+
print(c)
87+
for i in range(n):
88+
for j in range(m):
89+
assert abs(c[i, j] - (f64((i + 10) * (j + 10)) * f64((i + 20) * (j + 20)))) <= 1e-4
90+
91+
def test_array_broadcast():
92+
n: Const[i32] = 3
93+
m: Const[i32] = 5
94+
a: i64[n] = empty([n], dtype=int64)
95+
b: f64[n, m] = empty([n, m], dtype=float64)
96+
97+
i: i32
98+
j: i32
99+
for i in range(n):
100+
a[i] = i64(i + 10)
101+
102+
for i in range(n):
103+
for j in range(m):
104+
b[i, j] = f64((i + 1) * (j + 1))
105+
106+
c: f64[m] = get_array_dot_product(m, a, b)
107+
print(c)
108+
assert abs(c[0] - (68.0)) <= 1e-4
109+
assert abs(c[1] - (136.0)) <= 1e-4
110+
assert abs(c[2] - (204.0)) <= 1e-4
111+
assert abs(c[3] - (272.0)) <= 1e-4
112+
assert abs(c[4] - (340.0)) <= 1e-4
113+
114+
def test_multidim_array_return_i64():
115+
p: Const[i32] = 3
116+
q: Const[i32] = 4
117+
r: Const[i32] = 5
118+
a: i64[p, q, r] = empty([p, q, r], dtype=int64)
119+
a = get_multidim_array_i64(p, q, r)
120+
print(a)
121+
122+
i: i32; j: i32; k: i32
123+
for i in range(p):
124+
for j in range(q):
125+
for k in range(r):
126+
assert a[i, j, k] == i64(i * 2 + j * 3 + k * 4)
127+
128+
def main0():
129+
print("CPython version: ", get_cpython_version())
130+
131+
test_array_ints()
132+
test_array_floats()
133+
test_array_broadcast()
134+
test_multidim_array_return_i64()
135+
136+
main0()

integration_tests/bindpy_03_module.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import numpy as np
2+
3+
def get_cpython_version():
4+
import platform
5+
return platform.python_version()
6+
7+
def get_int_array_sum(n, a, b):
8+
return np.add(a, b)
9+
10+
def get_int_array_product(n, a, b):
11+
return np.multiply(a, b)
12+
13+
def get_float_array_sum(n, m, a, b):
14+
return np.add(a, b)
15+
16+
def get_float_array_product(n, m, a, b):
17+
return np.multiply(a, b)
18+
19+
def get_array_dot_product(m, a, b):
20+
print(a, b)
21+
c = a @ b
22+
print(c)
23+
return c
24+
25+
def get_multidim_array_i64(p, q, r):
26+
a = np.empty([p, q, r], dtype = np.int64)
27+
for i in range(p):
28+
for j in range(q):
29+
for k in range(r):
30+
a[i, j, k] = i * 2 + j * 3 + k * 4
31+
return a

0 commit comments

Comments
 (0)