aboutsummaryrefslogtreecommitdiff
path: root/tests/test_c_generator.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_c_generator.py')
-rw-r--r--tests/test_c_generator.py61
1 files changed, 59 insertions, 2 deletions
diff --git a/tests/test_c_generator.py b/tests/test_c_generator.py
index 3727f91..dd19a11 100644
--- a/tests/test_c_generator.py
+++ b/tests/test_c_generator.py
@@ -1,11 +1,12 @@
+import os
+import platform
import sys
-import textwrap
import unittest
# Run from the root dir
sys.path.insert(0, '.')
-from pycparser import c_parser, c_generator, c_ast
+from pycparser import c_parser, c_generator, c_ast, parse_file
_c_parser = c_parser.CParser(
lex_optimize=False,
@@ -332,6 +333,62 @@ class TestCtoC(unittest.TestCase):
name='',
)
+ def test_array_decl(self):
+ self._assert_ctoc_correct('int g(const int a[const 20]){}')
+ ast = parse_to_ast('const int a[const 20];')
+ generator = c_generator.CGenerator()
+ self.assertEqual(generator.visit(ast.ext[0].type),
+ 'const int [const 20]')
+ self.assertEqual(generator.visit(ast.ext[0].type.type),
+ 'const int')
+
+ def test_ptr_decl(self):
+ src = 'const int ** const x;'
+ self._assert_ctoc_correct(src)
+ ast = parse_to_ast(src)
+ generator = c_generator.CGenerator()
+ self.assertEqual(generator.visit(ast.ext[0].type),
+ 'const int ** const')
+ self.assertEqual(generator.visit(ast.ext[0].type.type),
+ 'const int *')
+ self.assertEqual(generator.visit(ast.ext[0].type.type.type),
+ 'const int')
+
+
+class TestCasttoC(unittest.TestCase):
+ def _find_file(self, name):
+ test_dir = os.path.dirname(__file__)
+ name = os.path.join(test_dir, 'c_files', name)
+ assert os.path.exists(name)
+ return name
+
+ def test_to_type(self):
+ src = 'int *x;'
+ generator = c_generator.CGenerator()
+ test_fun = c_ast.FuncCall(c_ast.ID('test_fun'), c_ast.ExprList([]))
+
+ ast1 = parse_to_ast(src)
+ int_ptr_type = ast1.ext[0].type
+ int_type = int_ptr_type.type
+ self.assertEqual(generator.visit(c_ast.Cast(int_ptr_type, test_fun)),
+ '(int *) test_fun()')
+ self.assertEqual(generator.visit(c_ast.Cast(int_type, test_fun)),
+ '(int) test_fun()')
+
+ @unittest.skipUnless(platform.system() == 'Linux',
+ 'cpp only works on Linux')
+ def test_to_type_with_cpp(self):
+ generator = c_generator.CGenerator()
+ test_fun = c_ast.FuncCall(c_ast.ID('test_fun'), c_ast.ExprList([]))
+ memmgr_path = self._find_file('memmgr.h')
+
+ ast2 = parse_file(memmgr_path, use_cpp=True)
+ void_ptr_type = ast2.ext[-3].type.type
+ void_type = void_ptr_type.type
+ self.assertEqual(generator.visit(c_ast.Cast(void_ptr_type, test_fun)),
+ '(void *) test_fun()')
+ self.assertEqual(generator.visit(c_ast.Cast(void_type, test_fun)),
+ '(void) test_fun()')
if __name__ == "__main__":
unittest.main()