diff options
author | Julian <cod3monk@users.noreply.github.com> | 2020-10-05 15:27:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-05 06:27:24 -0700 |
commit | 75e875732d3c4b923ea0d1f0713b88d35f5ef612 (patch) | |
tree | a8ded3224038259d802377fbd66f8715d612f7db /tests | |
parent | ba80794f1460a87eed2f8f918e47868878f3a5ad (diff) | |
download | pycparser-75e875732d3c4b923ea0d1f0713b88d35f5ef612.tar.gz |
Added flattening of abundant parenthesis in CGenerator (#394)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_c_generator.py | 33 |
1 files changed, 28 insertions, 5 deletions
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): |