Skip to content

Commit

Permalink
Merge pull request CodieTamida#232 from CodieTamida/fix/_r18_if_missi…
Browse files Browse the repository at this point in the history
…ng_a_JUMP_to_skip_ELSE_case

Added unittest of r_18 <if>
  • Loading branch information
NLTN authored May 12, 2024
2 parents 15e28c2 + 2bb9755 commit b15b235
Showing 1 changed file with 73 additions and 2 deletions.
75 changes: 73 additions & 2 deletions tests/unit/test_CG_r18_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def test_if_else(self):

def test_nested_if_else(self):
input_string = """
$
$
$
$
integer a, b, c;
$
if (a == b )
Expand Down Expand Up @@ -130,6 +130,77 @@ def test_nested_if_else(self):
# Assert
self.assertEqual(actual_output, expected_output)

def test_nested_ifs(self):
input_string = """
$
$
integer a, b, c, d;
$
if (a == b)
if (a => b)
if (a <= b) {
a = b + 1;
if (a != b)
print(c);
else
print(d);
endif
}
else {
b = b + 3;
a = a * b;
}
endif
endif
endif
$
"""
string_builder = StringIO()
string_builder.write("PUSHM 5000\n")
string_builder.write("PUSHM 5001\n")
string_builder.write("EQU\n")
string_builder.write("JUMP0 38\n")
string_builder.write("PUSHM 5000\n")
string_builder.write("PUSHM 5001\n")
string_builder.write("GEQ\n")
string_builder.write("JUMP0 37\n")
string_builder.write("PUSHM 5000\n")
string_builder.write("PUSHM 5001\n")
string_builder.write("LEQ\n")
string_builder.write("JUMP0 28\n")
string_builder.write("PUSHM 5001\n")
string_builder.write("PUSHI 1\n")
string_builder.write("A\n")
string_builder.write("POPM 5000\n")
string_builder.write("PUSHM 5000\n")
string_builder.write("PUSHM 5001\n")
string_builder.write("NEQ\n")
string_builder.write("JUMP0 24\n")
string_builder.write("PUSHM 5002\n")
string_builder.write("SOUT\n")
string_builder.write("JUMP 26\n")
string_builder.write("PUSHM 5003\n")
string_builder.write("SOUT\n")
string_builder.write("LABEL\n")
string_builder.write("JUMP 36\n")
string_builder.write("PUSHM 5001\n")
string_builder.write("PUSHI 3\n")
string_builder.write("A\n")
string_builder.write("POPM 5001\n")
string_builder.write("PUSHM 5000\n")
string_builder.write("PUSHM 5001\n")
string_builder.write("M\n")
string_builder.write("POPM 5000\n")
string_builder.write("LABEL\n")
string_builder.write("LABEL\n")
string_builder.write("LABEL\n")
expected_output = string_builder.getvalue()

# Act
write_to_file(self.SAMPLE_FILE_PATH, input_string)
actual_output = get_result_from_code_generator(self.SAMPLE_FILE_PATH)

# Assert
self.assertEqual(actual_output, expected_output)
if __name__ == '__main__':
unittest.main()

0 comments on commit b15b235

Please sign in to comment.