From 75e875732d3c4b923ea0d1f0713b88d35f5ef612 Mon Sep 17 00:00:00 2001 From: Julian Date: Mon, 5 Oct 2020 15:27:24 +0200 Subject: Added flattening of abundant parenthesis in CGenerator (#394) --- tests/test_c_generator.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'tests/test_c_generator.py') diff --git a/tests/test_c_generator.py b/tests/test_c_generator.py index 159c763..7937525 100644 --- a/tests/test_c_generator.py +++ b/tests/test_c_generator.py @@ -61,19 +61,22 @@ class TestFunctionDeclGeneration(unittest.TestCase): class TestCtoC(unittest.TestCase): - def _run_c_to_c(self, src): + def _run_c_to_c(self, src, *args, **kwargs): ast = parse_to_ast(src) - generator = c_generator.CGenerator() + generator = c_generator.CGenerator(*args, **kwargs) return generator.visit(ast) - def _assert_ctoc_correct(self, src): + def _assert_ctoc_correct(self, src, *args, **kwargs): """ Checks that the c2c translation was correct by parsing the code generated by c2c for src and comparing the AST with the original AST. + + Additional arguments are passed to CGenerator.__init__. """ - src2 = self._run_c_to_c(src) + src2 = self._run_c_to_c(src, *args, **kwargs) self.assertTrue(compare_asts(parse_to_ast(src), parse_to_ast(src2)), - src2) + "{!r} != {!r}".format(src, src2)) + return src2 def test_trivial_decls(self): self._assert_ctoc_correct('int a;') @@ -361,6 +364,26 @@ class TestCtoC(unittest.TestCase): src = 'int x = ' + src + ';' self._assert_ctoc_correct(src) + def test_flattened_binaryops(self): + # codes with minimum number of (necessary) parenthesis: + test_snippets = [ + 'int x = a*b*c*d;', + 'int x = a+b*c*d;', + 'int x = a*b+c*d;', + 'int x = a*b*c+d;', + 'int x = (a+b)*c*d;', + 'int x = (a+b)*(c+d);', + 'int x = (a+b)/(c-d);', + 'int x = a+b-c-d;', + 'int x = a+(b-c)-d;' + ] + for src in test_snippets: + src2 = self._assert_ctoc_correct(src, reduce_parentheses=True) + self.assertTrue( + src2.count('(') == src.count('('), + msg="{!r} did not have minimum number of parenthesis, should be like {!r}.".format( + src2, src)) + class TestCasttoC(unittest.TestCase): def _find_file(self, name): -- cgit v1.2.1