-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into docs-enhancement
- Loading branch information
Showing
27 changed files
with
670 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from lpython import (i32, Const) | ||
from numpy import empty, int32 | ||
|
||
dim: Const[i32] = 2 | ||
dim2: Const[i32] = 3 | ||
|
||
def g(): | ||
a: i32[dim, dim2] = empty((dim, dim2), dtype=int32) | ||
i1: i32 = 0 | ||
i2: i32 = 0 | ||
for i1 in range(dim): | ||
for i2 in range(dim2): | ||
a[i1, i2] = i32(i1 * dim2 + i2) | ||
# a: [[0, 1, 2], [3, 4, 5]] | ||
print(a) | ||
assert a[-1, -1] == 5 | ||
assert a[-1, -2] == 4 | ||
assert a[-1, -3] == 3 | ||
assert a[-2, -1] == 2 | ||
assert a[-2, -2] == 1 | ||
assert a[-2, -3] == 0 | ||
|
||
g() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from lpython import i32 | ||
from numpy import empty, int32, array | ||
|
||
def foo(x: i32[:]): | ||
print(x[3], x[4], x[-1], x[-2]) | ||
assert x[-1] == 5 | ||
assert x[-2] == 4 | ||
assert x[-3] == 3 | ||
assert x[-4] == 2 | ||
assert x[-5] == 1 | ||
|
||
def main(): | ||
x: i32[5] = empty(5, dtype=int32) | ||
x = array([1, 2, 3, 4, 5]) | ||
foo(x) | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from lpython import i32, f64 | ||
|
||
def test_builtin_type(): | ||
i: i32 = 42 | ||
f: f64 = 64.0 | ||
s: str = "Hello, LPython!" | ||
l: list[i32] = [1, 2, 3, 4, 5] | ||
d: dict[str, i32] = {"a": 1, "b": 2, "c": 3} | ||
res: str = "" | ||
|
||
res = str(type(i)) | ||
print(res) | ||
assert res == "<class 'int'>" | ||
res = str(type(f)) | ||
print(res) | ||
assert res == "<class 'float'>" | ||
res = str(type(s)) | ||
print(res) | ||
assert res == "<class 'str'>" | ||
res = str(type(l)) | ||
print(res) | ||
assert res == "<class 'list'>" | ||
res = str(type(d)) | ||
print(res) | ||
assert res == "<class 'dict'>" | ||
|
||
test_builtin_type() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from lpython import i32 | ||
|
||
s1: set[str] = {"a", "b", "c", "a"} | ||
s2: set[i32] = {1, 2, 3, 1} | ||
s3: set[tuple[i32, i32]] = {(1, 2), (2, 3), (4, 5)} | ||
|
||
assert len(s1) == 3 | ||
assert len(s2) == 3 | ||
assert len(s3) == 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from lpython import i32, f64 | ||
|
||
|
||
def test_logical_assignment(): | ||
# Can be uncommented after fixing the segfault | ||
# _LPYTHON: str = "LPython" | ||
# s_var: str = "" or _LPYTHON | ||
# assert s_var == "LPython" | ||
# print(s_var) | ||
|
||
_MAX_VAL: i32 = 100 | ||
i_var: i32 = 0 and 100 | ||
assert i_var == 0 | ||
print(i_var) | ||
|
||
_PI: f64 = 3.14 | ||
f_var: f64 = 2.0 * _PI or _PI**2.0 | ||
assert f_var == 6.28 | ||
print(f_var) | ||
|
||
|
||
test_logical_assignment() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
from lpython import i32, f64 | ||
|
||
|
||
def test_logical_compare_literal(): | ||
# Integers | ||
print(1 or 3) | ||
assert (1 or 3) == 1 | ||
|
||
print(1 and 3) | ||
assert (1 and 3) == 3 | ||
|
||
print(2 or 3 or 5 or 6) | ||
assert (2 or 3 or 5 or 6) == 2 | ||
|
||
print(1 and 3 or 2 and 4) | ||
assert (1 and 3 or 2 and 4) == 3 | ||
|
||
print(1 or 3 and 0 or 4) | ||
assert (1 or 3 and 0 or 4) == 1 | ||
|
||
print(1 and 3 or 2 and 0) | ||
assert (1 and 3 or 2 and 0) == 3 | ||
|
||
print(1 and 0 or 3 and 4) | ||
assert (1 and 0 or 3 and 4) == 4 | ||
|
||
# Floating-point numbers | ||
print(1.33 or 6.67) | ||
assert (1.33 or 6.67) == 1.33 | ||
|
||
print(1.33 and 6.67) | ||
assert (1.33 and 6.67) == 6.67 | ||
|
||
print(1.33 or 6.67 and 3.33 or 0.0) | ||
assert (1.33 or 6.67 and 3.33 or 0.0) == 1.33 | ||
|
||
print(1.33 and 6.67 or 3.33 and 0.0) | ||
assert (1.33 and 6.67 or 3.33 and 0.0) == 6.67 | ||
|
||
print(1.33 and 0.0 and 3.33 and 6.67) | ||
assert (1.33 and 0.0 and 3.33 and 6.67) == 0.0 | ||
|
||
# Strings | ||
print("a" or "b") | ||
assert ("a" or "b") == "a" | ||
|
||
print("abc" or "b") | ||
assert ("abc" or "b") == "abc" | ||
|
||
print("a" and "b") | ||
assert ("a" and "b") == "b" | ||
|
||
print("a" or "b" and "c" or "d") | ||
assert ("a" or "b" and "c" or "d") == "a" | ||
|
||
print("" or " ") | ||
assert ("" or " ") == " " | ||
|
||
print("" and " " or "a" and "b" and "c") | ||
assert ("" and " " or "a" and "b" and "c") == "c" | ||
|
||
print("" and " " and "a" and "b" and "c") | ||
assert ("" and " " and "a" and "b" and "c") == "" | ||
|
||
|
||
def test_logical_compare_variable(): | ||
# Integers | ||
i_a: i32 = 1 | ||
i_b: i32 = 3 | ||
|
||
print(i_a and i_b) | ||
assert (i_a and i_b) == 3 | ||
|
||
print(i_a or i_b or 2 or 4) | ||
assert (i_a or i_b or 2 or 4) == 1 | ||
|
||
print(i_a and i_b or 2 and 4) | ||
assert (i_a and i_b or 2 and 4) == 3 | ||
|
||
print(i_a or i_b and 0 or 4) | ||
assert (i_a or i_b and 0 or 4) == i_a | ||
|
||
print(i_a and i_b or 2 and 0) | ||
assert (i_a and i_b or 2 and 0) == i_b | ||
|
||
print(i_a and 0 or i_b and 4) | ||
assert (i_a and 0 or i_b and 4) == 4 | ||
|
||
print(i_a + i_b or 0 - 4) | ||
assert (i_a + i_b or 0 - 4) == 4 | ||
|
||
# Floating-point numbers | ||
f_a: f64 = 1.67 | ||
f_b: f64 = 3.33 | ||
|
||
print(f_a // f_b and f_a - f_b) | ||
assert (f_a // f_b and f_a - f_b) == 0.0 | ||
|
||
print(f_a**3.0 or 3.0**f_a) | ||
assert (f_a**3.0 or 3.0**f_a) == 4.657462999999999 | ||
|
||
print(f_a - 3.0 and f_a + 3.0 or f_b - 3.0 and f_b + 3.0) | ||
assert (f_a - 3.0 and f_a + 3.0 or f_b - 3.0 and f_b + 3.0) == 4.67 | ||
|
||
# Can be uncommented after fixing the segfault | ||
# Strings | ||
# s_a: str = "a" | ||
# s_b: str = "b" | ||
|
||
# print(s_a or s_b) | ||
# assert (s_a or s_b) == s_a | ||
|
||
# print(s_a and s_b) | ||
# assert (s_a and s_b) == s_b | ||
|
||
# print(s_a + s_b or s_b + s_a) | ||
# assert (s_a + s_b or s_b + s_a) == "ab" | ||
|
||
# print(s_a[0] or s_b[-1]) | ||
# assert (s_a[0] or s_b[-1]) == "a" | ||
|
||
# print(s_a[0] and s_b[-1]) | ||
# assert (s_a[0] and s_b[-1]) == "b" | ||
|
||
# print(s_a + s_b or s_b + s_a + s_a[0] and s_b[-1]) | ||
# assert (s_a + s_b or s_b + s_a + s_a[0] and s_b[-1]) == "ab" | ||
|
||
|
||
test_logical_compare_literal() | ||
test_logical_compare_variable() |
Oops, something went wrong.