Skip to content

Commit 117f153

Browse files
committed
TEST: Add test for @ pythoncall()
1 parent b637e27 commit 117f153

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ RUN(NAME bindc_05 LABELS llvm c
476476
EXTRAFILES bindc_05b.c)
477477
RUN(NAME bindc_06 LABELS llvm c
478478
EXTRAFILES bindc_06b.c)
479+
RUN(NAME bindpy_01 LABELS c ENABLE_CPYTHON EXTRAFILES bindpy_01_module.py)
479480
RUN(NAME test_generics_01 LABELS cpython llvm c)
480481
RUN(NAME test_cmath LABELS cpython llvm c)
481482
RUN(NAME test_complex_01 LABELS cpython llvm c wasm wasm_x64)

integration_tests/bindpy_01.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from lpython import i32, i64, u32, u64, f32, f64, pythoncall
2+
3+
@pythoncall(module = "bindpy_01_module")
4+
def add_ints(a: i32, b: i64, c: u32, d: u64) -> i64:
5+
pass
6+
7+
@pythoncall(module = "bindpy_01_module")
8+
def multiply_ints(a: i32, b: i64, c: u32, d: u64) -> i64:
9+
pass
10+
11+
@pythoncall(module = "bindpy_01_module")
12+
def add_floats(a: f32, b: f64) -> f64:
13+
pass
14+
15+
@pythoncall(module = "bindpy_01_module")
16+
def multiply_floats(a: f32, b: f64) -> f64:
17+
pass
18+
19+
@pythoncall(module = "bindpy_01_module")
20+
def get_hello_world(a: str, b: str) -> str:
21+
pass
22+
23+
@pythoncall(module = "bindpy_01_module")
24+
def str_n_times(a: str, n: i32) -> str:
25+
pass
26+
27+
@pythoncall(module = "bindpy_01_module")
28+
def get_cpython_version() -> str:
29+
pass
30+
31+
# Integers:
32+
def test_ints():
33+
i: i32
34+
j: i64
35+
k: u32
36+
l: u64
37+
i = -5
38+
j = i64(24)
39+
k = u32(20)
40+
l = u64(92)
41+
42+
assert add_ints(i, j, k, l) == i64(131)
43+
assert multiply_ints(i, j, k, l) == i64(-220800)
44+
45+
# Floats
46+
def test_floats():
47+
a: f32
48+
b: f64
49+
a = f32(3.14)
50+
b = -100.00
51+
52+
assert abs(add_floats(a, b) - (-96.86)) <= 1e-4
53+
assert abs(multiply_floats(a, b) - (-314.0)) <= 1e-4
54+
55+
# Strings
56+
def test_strings():
57+
a: str
58+
b: str
59+
c: str
60+
i: i32
61+
a = "hello"
62+
b = "world"
63+
i = 3
64+
65+
assert get_hello_world(a, b) == "hello world!"
66+
assert str_n_times(a, i) == "hellohellohello"
67+
assert get_hello_world(str_n_times(a, i), b) == "hellohellohello world!"
68+
69+
def main0():
70+
print("CPython version: ", get_cpython_version())
71+
72+
test_ints()
73+
test_floats()
74+
test_strings()
75+
76+
77+
main0()

integration_tests/bindpy_01_module.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def get_cpython_version():
2+
import platform
3+
return platform.python_version()
4+
5+
def add_ints(a, b, c, d):
6+
e = a + b + c + d
7+
return e
8+
9+
def multiply_ints(a, b, c, d):
10+
e = a * b * c * d
11+
return e
12+
13+
def add_floats(a, b):
14+
return a + b
15+
16+
def multiply_floats(a, b):
17+
return a * b
18+
19+
def get_hello_world(a, b):
20+
return f"{a} {b}!"
21+
22+
def str_n_times(a, n):
23+
return a * n

0 commit comments

Comments
 (0)