Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import unittest |
| 5 | |
| 6 | sys.path.insert(0, '..') # for c-to-c.py |
| 7 | sys.path.insert(0, '../..') # for pycparser libs |
| 8 | sys.path.insert(0, 'examples') # for running from root dir |
| 9 | |
| 10 | from pycparser import c_parser |
| 11 | c2cmodule = __import__('c-to-c') |
| 12 | |
| 13 | _c_parser = c_parser.CParser( |
| 14 | lex_optimize=False, |
| 15 | yacc_debug=True, |
| 16 | yacc_optimize=False, |
| 17 | yacctab='yacctab') |
| 18 | |
| 19 | |
eli.bendersky | 8e6c586 | 2011-05-20 12:35:08 +0300 | [diff] [blame] | 20 | def compare_asts(ast1, ast2): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 21 | if type(ast1) != type(ast2): |
| 22 | return False |
| 23 | if isinstance(ast1, tuple) and isinstance(ast2, tuple): |
| 24 | if ast1[0] != ast2[0]: |
| 25 | return False |
| 26 | ast1 = ast1[1] |
| 27 | ast2 = ast2[1] |
| 28 | for attr in ast1.attr_names: |
| 29 | if getattr(ast1, attr) != getattr(ast2, attr): |
| 30 | return False |
| 31 | for i, c1 in enumerate(ast1.children()): |
| 32 | if compare_asts(c1, ast2.children()[i]) == False: |
| 33 | return False |
| 34 | return True |
| 35 | |
| 36 | |
eli.bendersky | 8e6c586 | 2011-05-20 12:35:08 +0300 | [diff] [blame] | 37 | def parse_to_ast(src): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 38 | return _c_parser.parse(src) |
| 39 | |
| 40 | |
| 41 | class TestCtoC(unittest.TestCase): |
eli.bendersky | 8e6c586 | 2011-05-20 12:35:08 +0300 | [diff] [blame] | 42 | def _run_c_to_c(self, src): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 43 | ast = parse_to_ast(src) |
| 44 | generator = c2cmodule.CGenerator() |
| 45 | return generator.visit(ast) |
| 46 | |
eli.bendersky | 8e6c586 | 2011-05-20 12:35:08 +0300 | [diff] [blame] | 47 | def _assert_ctoc_correct(self, src): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 48 | """ Checks that the c2c translation was correct by parsing the code |
| 49 | generated by c2c for src and comparing the AST with the original |
eli.bendersky | 8e6c586 | 2011-05-20 12:35:08 +0300 | [diff] [blame] | 50 | AST. |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 51 | """ |
| 52 | src2 = self._run_c_to_c(src) |
| 53 | self.assertTrue(compare_asts(parse_to_ast(src), parse_to_ast(src2)), src2) |
| 54 | |
eli.bendersky | 8e6c586 | 2011-05-20 12:35:08 +0300 | [diff] [blame] | 55 | def test_trivial_decls(self): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 56 | self._assert_ctoc_correct('int a;') |
| 57 | self._assert_ctoc_correct('int b, a;') |
| 58 | self._assert_ctoc_correct('int c, b, a;') |
| 59 | |
eli.bendersky | 8e6c586 | 2011-05-20 12:35:08 +0300 | [diff] [blame] | 60 | def test_complex_decls(self): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 61 | self._assert_ctoc_correct('int** (*a)(void);') |
| 62 | self._assert_ctoc_correct('int** (*a)(void*, int);') |
| 63 | |
eli.bendersky | 8e6c586 | 2011-05-20 12:35:08 +0300 | [diff] [blame] | 64 | def test_casts(self): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 65 | self._assert_ctoc_correct(r''' |
| 66 | int main() { |
| 67 | int b = (int) f; |
| 68 | int c = (int*) f; |
| 69 | }''') |
| 70 | |
| 71 | def test_initlist(self): |
| 72 | self._assert_ctoc_correct('int arr[] = {1, 2, 3};') |
| 73 | |
eli.bendersky | d4a9975 | 2011-05-26 07:04:19 +0300 | [diff] [blame] | 74 | def test_exprs(self): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 75 | self._assert_ctoc_correct(''' |
| 76 | int main(void) |
| 77 | { |
| 78 | int a; |
| 79 | int b = a++; |
| 80 | int c = ++a; |
| 81 | int d = a--; |
| 82 | int e = --a; |
| 83 | }''') |
| 84 | |
eli.bendersky | 8e6c586 | 2011-05-20 12:35:08 +0300 | [diff] [blame] | 85 | def test_statements(self): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 86 | self._assert_ctoc_correct(r''' |
| 87 | int main() { |
| 88 | int a; |
| 89 | a = 5; |
| 90 | ; |
| 91 | return a; |
| 92 | }''') |
| 93 | |
| 94 | def test_casts(self): |
| 95 | self._assert_ctoc_correct(r''' |
| 96 | int main() { |
| 97 | int a = (int) b + 8; |
| 98 | int t = (int) c; |
| 99 | } |
| 100 | ''') |
| 101 | |
eli.bendersky | 1bd6c17 | 2011-07-16 06:43:20 +0300 | [diff] [blame] | 102 | def test_struct_decl(self): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 103 | self._assert_ctoc_correct(r''' |
| 104 | typedef struct node_t { |
| 105 | struct node_t* next; |
| 106 | int data; |
| 107 | } node; |
| 108 | ''') |
| 109 | |
eli.bendersky | cad1cfd | 2011-05-26 06:56:27 +0300 | [diff] [blame] | 110 | def test_issue36(self): |
Eli Bendersky | dfd0df3 | 2012-01-19 06:20:24 +0200 | [diff] [blame^] | 111 | self._assert_ctoc_correct(r''' |
| 112 | int main() { |
| 113 | }''') |
| 114 | |
| 115 | def test_issue37(self): |
| 116 | self._assert_ctoc_correct(r''' |
| 117 | int main(void) |
| 118 | { |
| 119 | unsigned size; |
| 120 | size = sizeof(size); |
| 121 | return 0; |
| 122 | }''') |
| 123 | |
| 124 | |
| 125 | |
| 126 | if __name__ == "__main__": |
| 127 | unittest.main() |