Skip to content
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 comparsion for string #2699

Merged
merged 3 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,8 @@ RUN(NAME test_platform LABELS cpython llvm llvm_jit c)
RUN(NAME test_vars_01 LABELS cpython llvm llvm_jit)
RUN(NAME test_version LABELS cpython llvm llvm_jit)
RUN(NAME logical_binop1 LABELS cpython llvm llvm_jit)
RUN(NAME test_logical_compare LABELS cpython llvm llvm_jit)
RUN(NAME test_logical_assignment LABELS cpython llvm llvm_jit)
RUN(NAME test_logical_compare LABELS cpython llvm llvm_jit) # TODO: Add C backend after fixing issue #2708
RUN(NAME test_logical_assignment LABELS cpython llvm llvm_jit) # TODO: Add C backend after fixing issue #2708
RUN(NAME vec_01 LABELS cpython llvm llvm_jit c NOFAST)
RUN(NAME test_str_comparison LABELS cpython llvm llvm_jit c wasm)
RUN(NAME test_bit_length LABELS cpython llvm llvm_jit c)
Expand Down
9 changes: 4 additions & 5 deletions integration_tests/test_logical_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@


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)
_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
Expand Down
29 changes: 14 additions & 15 deletions integration_tests/test_logical_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,27 @@ def test_logical_compare_variable():
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"
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 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 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 + 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] 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[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"
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()
Expand Down
2 changes: 1 addition & 1 deletion src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5897,7 +5897,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
cond = builder->CreateFCmpUEQ(left_val, zero);
} else if (ASRUtils::is_character(*x.m_type)) {
zero = llvm::Constant::getNullValue(character_type);
zero = builder->CreateGlobalStringPtr("");
cond = lfortran_str_cmp(left_val, zero, "_lpython_str_compare_eq");
} else if (ASRUtils::is_logical(*x.m_type)) {
zero = llvm::ConstantInt::get(context,
Expand Down
Loading