diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 8b82c9c373..d8fb3c5662 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -471,6 +471,7 @@ RUN(NAME print_float LABELS cpython llvm c wasm wasm_x64) RUN(NAME print_list_tuple_01 LABELS cpython llvm c NOFAST) RUN(NAME print_list_tuple_02 LABELS cpython llvm c NOFAST) RUN(NAME print_list_tuple_03 LABELS cpython llvm c NOFAST) +RUN(NAME test_list_item_mixed_print LABELS cpython llvm c NOFAST) # CPython and LLVM RUN(NAME const_01 LABELS cpython llvm c wasm) diff --git a/integration_tests/test_list_item_mixed_print.py b/integration_tests/test_list_item_mixed_print.py new file mode 100644 index 0000000000..10fdc32b75 --- /dev/null +++ b/integration_tests/test_list_item_mixed_print.py @@ -0,0 +1,44 @@ +from lpython import i32, f64 + +# Test for verifying printing items of different types with a list: +# 1. string and list item +# 2. integer and list item +# 3. float and list item +# 4. tuple and list item +# +# Also test with a list item which is a nested list. +def test_list_item_mixed_print(): + s_list: list[str] = ["Hello", "LPython"] + + print("", s_list[0]) + print("This is", s_list[1]) + + i_list: list[i32] = [1, 2, 3, 4, 5] + + print(i_list[0], i_list[1], i_list[2], "...", i_list[-3], i_list[-2], i_list[-1]) + print("The first element is:", i_list[0]) + + m: i32 = len(i_list) // 2 + print("The middle element is:", i_list[m]) + + f_list: list[f64] = [3.14, 6.28] + + print(f_list[0], "* 2 =", f_list[1]) + print("Total:", f_list[0] + f_list[1]) + + t: tuple[i32, i32, i32] = (1, 2, 3) + print(t, "is a tuple, but", i_list[0], "is a number.") + + i_list2: list[i32] = [1, 2, 3] + print(i_list2[0], i_list2[1], i_list2[2], sep=" is smaller than ") + + i: i32 + for i in range(len(i_list)): + print(i_list[i], end=" # ") + print("\n") + + n_list: list[list[i32]] = [[1, 2], [3, 4], [5, 6]] + for i in range(len(n_list)): + print("List ", i, ":", n_list[i]) + +test_list_item_mixed_print() \ No newline at end of file diff --git a/src/libasr/pass/replace_symbolic.cpp b/src/libasr/pass/replace_symbolic.cpp index 83da75603f..c263ea444c 100644 --- a/src/libasr/pass/replace_symbolic.cpp +++ b/src/libasr/pass/replace_symbolic.cpp @@ -842,6 +842,8 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitorm_a, list_item->m_pos, ASRUtils::TYPE(ASR::make_CPtr_t(al, x.base.base.loc)), nullptr)); print_tmp.push_back(basic_str(x.base.base.loc, value)); + } else { + print_tmp.push_back(val); } } else { print_tmp.push_back(x.m_values[i]); diff --git a/tests/reference/runtime-test_list_item_mixed_print-a3fd49f.json b/tests/reference/runtime-test_list_item_mixed_print-a3fd49f.json new file mode 100644 index 0000000000..d580243c7b --- /dev/null +++ b/tests/reference/runtime-test_list_item_mixed_print-a3fd49f.json @@ -0,0 +1,13 @@ +{ + "basename": "runtime-test_list_item_mixed_print-a3fd49f", + "cmd": "lpython {infile}", + "infile": "tests/../integration_tests/test_list_item_mixed_print.py", + "infile_hash": "14ce4950ca0ff6c6f610df787ad8d260148866f4c7062ab0b856ec5a", + "outfile": null, + "outfile_hash": null, + "stdout": "runtime-test_list_item_mixed_print-a3fd49f.stdout", + "stdout_hash": "cffcdb43864ef4e234dec5a10923f9077b7c6d1f4d9c37df1882f375", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/runtime-test_list_item_mixed_print-a3fd49f.stdout b/tests/reference/runtime-test_list_item_mixed_print-a3fd49f.stdout new file mode 100644 index 0000000000..8659fad42d --- /dev/null +++ b/tests/reference/runtime-test_list_item_mixed_print-a3fd49f.stdout @@ -0,0 +1,14 @@ + Hello +This is LPython +1 2 3 ... 3 4 5 +The first element is: 1 +The middle element is: 3 +3.14000000000000012e+00 * 2 = 6.28000000000000025e+00 +Total: 9.41999999999999993e+00 +(1, 2, 3) is a tuple, but 1 is a number. +1 is smaller than 2 is smaller than 3 +1 # 2 # 3 # 4 # 5 # + +List 0 : [1, 2] +List 1 : [3, 4] +List 2 : [5, 6] diff --git a/tests/tests.toml b/tests/tests.toml index 0ea59119d2..0c22c94aec 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -461,6 +461,10 @@ pass = "print_list_tuple" filename = "../integration_tests/print_04.py" llvm = true +[[test]] +filename = "../integration_tests/test_list_item_mixed_print.py" +run = true + [[test]] filename = "../integration_tests/generics_01.py" asr = true