Skip to content

Commit 00b8d59

Browse files
authored
Merge pull request #2065 from Shaikh-Ubaid/support_expr_in_assert
Support expression as message in assert
2 parents b2a3520 + c903be8 commit 00b8d59

File tree

6 files changed

+248
-175
lines changed

6 files changed

+248
-175
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ RUN(NAME expr_13 LABELS llvm c
416416
RUN(NAME expr_14 LABELS cpython llvm c)
417417
RUN(NAME expr_15 LABELS cpython llvm c)
418418
RUN(NAME expr_16 LABELS cpython c)
419+
RUN(NAME expr_17 LABELS cpython llvm c)
420+
RUN(NAME expr_18 FAIL LABELS cpython llvm c)
419421

420422
RUN(NAME expr_01u LABELS cpython llvm c NOFAST)
421423
RUN(NAME expr_02u LABELS cpython llvm c NOFAST)

integration_tests/expr_17.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from lpython import i32, f64
2+
3+
def main0():
4+
a: i32 = 4
5+
b: i32 = 3
6+
c: i32 = 12
7+
assert a * b == c, a * b
8+
9+
d: f64 = 0.4
10+
e: f64 = 2.5
11+
f: f64 = 1.0
12+
assert abs((d * e) - f) <= 1e-6, abs((d * e) - f)
13+
14+
assert a == b + 1, "Failed: a == b + 1"
15+
16+
main0()

integration_tests/expr_18.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from lpython import i32, f64
2+
3+
def main0():
4+
a: i32 = 4
5+
b: i32 = 3
6+
c: i32 = 12
7+
assert a * b != c, "Error: 3 * 4 equals 12"
8+
9+
main0()

src/libasr/codegen/asr_to_c.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,19 +1066,50 @@ R"( // Initialise Numpy
10661066
std::string out = indent;
10671067
bracket_open++;
10681068
visit_expr(*x.m_test);
1069+
std::string test_condition = src;
10691070
if (ASR::is_a<ASR::SymbolicCompare_t>(*x.m_test)){
10701071
out = symengine_src;
10711072
symengine_src = "";
10721073
out += indent;
10731074
}
10741075
if (x.m_msg) {
1076+
this->visit_expr(*x.m_msg);
1077+
std::string tmp_gen = "";
1078+
ASR::ttype_t* value_type = ASRUtils::expr_type(x.m_msg);
1079+
if( ASR::is_a<ASR::List_t>(*value_type) ||
1080+
ASR::is_a<ASR::Tuple_t>(*value_type)) {
1081+
std::string p_func = c_ds_api->get_print_func(value_type);
1082+
tmp_gen += indent + p_func + "(" + src + ");\n";
1083+
} else {
1084+
tmp_gen += "\"";
1085+
tmp_gen += c_ds_api->get_print_type(value_type, ASR::is_a<ASR::ArrayItem_t>(*x.m_msg));
1086+
tmp_gen += "\", ";
1087+
if( ASRUtils::is_array(value_type) ) {
1088+
src += "->data";
1089+
}
1090+
if(ASR::is_a<ASR::SymbolicExpression_t>(*value_type)) {
1091+
src += symengine_src;
1092+
symengine_src = "";
1093+
}
1094+
if (ASR::is_a<ASR::Complex_t>(*value_type)) {
1095+
tmp_gen += "creal(" + src + ")";
1096+
tmp_gen += ", ";
1097+
tmp_gen += "cimag(" + src + ")";
1098+
} else if(ASR::is_a<ASR::SymbolicExpression_t>(*value_type)){
1099+
tmp_gen += "basic_str(" + src + ")";
1100+
if(ASR::is_a<ASR::Var_t>(*x.m_msg)) {
1101+
symengine_queue.pop();
1102+
}
1103+
} else {
1104+
tmp_gen += src;
1105+
}
1106+
}
10751107
out += "ASSERT_MSG(";
1076-
out += src + ", ";
1077-
visit_expr(*x.m_msg);
1078-
out += src + ");\n";
1108+
out += test_condition + ", ";
1109+
out += tmp_gen + ");\n";
10791110
} else {
10801111
out += "ASSERT(";
1081-
out += src + ");\n";
1112+
out += test_condition + ");\n";
10821113
}
10831114
bracket_open--;
10841115
src = check_tmp_buffer() + out;

0 commit comments

Comments
 (0)