From 2bb9755fb201d5836c6ae2c48d5a67d00d339d36 Mon Sep 17 00:00:00 2001 From: Codie Tamida Date: Sun, 12 May 2024 00:00:39 -0700 Subject: [PATCH] Added unittest of r_18 --- tests/unit/test_CG_r18_if.py | 75 +++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_CG_r18_if.py b/tests/unit/test_CG_r18_if.py index 267732e..5a53bd3 100644 --- a/tests/unit/test_CG_r18_if.py +++ b/tests/unit/test_CG_r18_if.py @@ -75,8 +75,8 @@ def test_if_else(self): def test_nested_if_else(self): input_string = """ - $ - $ + $ + $ integer a, b, c; $ if (a == b ) @@ -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() \ No newline at end of file