Skip to content

Commit 4f13699

Browse files
committed
TEST: Add test for arrays of int, real in bindpy
1 parent d2ea867 commit 4f13699

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ RUN(NAME bindc_05 LABELS llvm c
516516
RUN(NAME bindc_06 LABELS llvm c
517517
EXTRAFILES bindc_06b.c)
518518
RUN(NAME bindpy_01 LABELS cpython c ENABLE_CPYTHON NOFAST EXTRAFILES bindpy_01_module.py)
519+
RUN(NAME bindpy_02 LABELS cpython c ENABLE_CPYTHON ENABLE_CNUMPY EXTRAFILES bindpy_02_module.py)
519520
RUN(NAME test_generics_01 LABELS cpython llvm c NOFAST)
520521
RUN(NAME test_cmath LABELS cpython llvm c NOFAST)
521522
RUN(NAME test_complex_01 LABELS cpython llvm c wasm wasm_x64)

integration_tests/bindpy_02.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from lpython import i32, f64, pythoncall, Const
2+
from numpy import empty
3+
4+
@pythoncall(module = "bindpy_02_module")
5+
def get_cpython_version() -> str:
6+
pass
7+
8+
@pythoncall(module = "bindpy_02_module")
9+
def get_int_array_sum(a: i32[:]) -> i32:
10+
pass
11+
12+
@pythoncall(module = "bindpy_02_module")
13+
def get_int_array_product(a: i32[:]) -> i32:
14+
pass
15+
16+
@pythoncall(module = "bindpy_02_module")
17+
def get_float_array_sum(a: f64[:]) -> f64:
18+
pass
19+
20+
@pythoncall(module = "bindpy_02_module")
21+
def get_float_array_product(a: f64[:]) -> f64:
22+
pass
23+
24+
@pythoncall(module = "bindpy_02_module")
25+
def show_array_dot_product(a: i32[:], b: f64[:]):
26+
pass
27+
28+
# Integers:
29+
def test_array_ints():
30+
n: Const[i32] = 5
31+
a: i32[n] = empty([n], dtype=int)
32+
33+
i: i32
34+
for i in range(n):
35+
a[i] = i + 10
36+
37+
assert get_int_array_sum(a) == i32(60)
38+
assert get_int_array_product(a) == i32(240240)
39+
40+
# Floats
41+
def test_array_floats():
42+
n: Const[i32] = 3
43+
m: Const[i32] = 5
44+
b: f64[n, m] = empty([n, m], dtype=float)
45+
46+
i: i32
47+
j: i32
48+
49+
for i in range(n):
50+
for j in range(m):
51+
b[i, j] = f64((i + 1) * (j + 1))
52+
53+
assert abs(get_float_array_sum(b) - (90.000000)) <= 1e-4
54+
assert abs(get_float_array_product(b) - (13436928000.000000)) <= 1e-4
55+
56+
def test_array_broadcast():
57+
n: Const[i32] = 3
58+
m: Const[i32] = 5
59+
a: i32[n] = empty([n], dtype=int)
60+
b: f64[n, m] = empty([n, m], dtype=float)
61+
62+
i: i32
63+
j: i32
64+
for i in range(n):
65+
a[i] = i + 10
66+
67+
for i in range(n):
68+
for j in range(m):
69+
b[i, j] = f64((i + 1) * (j + 1))
70+
71+
show_array_dot_product(a, b)
72+
73+
def main0():
74+
print("CPython version: ", get_cpython_version())
75+
76+
test_array_ints()
77+
test_array_floats()
78+
test_array_broadcast()
79+
80+
main0()

integration_tests/bindpy_02_module.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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(a):
8+
return np.sum(a)
9+
10+
def get_int_array_product(a):
11+
return np.prod(a)
12+
13+
def get_float_array_sum(a):
14+
return np.sum(a)
15+
16+
def get_float_array_product(a):
17+
return np.prod(a)
18+
19+
def show_array_dot_product(a, b):
20+
print(a, b)
21+
print(a @ b)

0 commit comments

Comments
 (0)