-
Notifications
You must be signed in to change notification settings - Fork 171
Fix logical comparison #2598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix logical comparison #2598
Changes from all commits
3e87bc9
e25820b
b79daf5
94530a2
bc2de09
bcc1c42
5a3213b
2eb2edd
2376719
60a4a1b
46ab7be
64b1fd2
1013838
430eca4
2473f76
6441912
5aebcb6
cc5aaa8
94041a2
e644814
4973f05
a827e1c
332a987
cd26f8f
74b8d51
a756338
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created a separate test for testing assignments through logical operations that we implemented. I am adding the print statements. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, Let's merge this test with the other integration test added in this PR. |
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" | ||
kmr-srbh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
test_logical_compare_literal() | ||
test_logical_compare_variable() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
def f(): | ||
kmr-srbh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print("hello" or 10) | ||
|
||
|
||
f() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we do not need this test. It is redundant at the moment. Both the operands of the operation are of the same type, so we do not expect any SemanticError here. It is a different case that we do not support lists in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please ensure that you also remove the reference outputs associated with this test. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"basename": "asr-test_logical_compare_01-5db0e2e", | ||
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}", | ||
"infile": "tests/errors/test_logical_compare_01.py", | ||
"infile_hash": "467dc216d8ce90f4b3a1ec06610cea226ae96152763cfa42d5ab8f33", | ||
"outfile": null, | ||
"outfile_hash": null, | ||
"stdout": null, | ||
"stdout_hash": null, | ||
"stderr": "asr-test_logical_compare_01-5db0e2e.stderr", | ||
"stderr_hash": "d10cac68687315b5d29828e0acb5170f44bd91dd30784f8bd4943bb0", | ||
"returncode": 2 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
semantic error: Type mismatch: 'str' and 'i32'. Both operands must be of the same type. | ||
--> tests/errors/test_logical_compare_01.py:2:11 | ||
| | ||
2 | print("hello" or 10) | ||
| ^^^^^^^^^^^^^ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please open an issue for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Shaikh-Ubaid What can be a possible way to solve this? This is the same problem which causes issues with the
input()
accepting a string variable as an argument.