summaryrefslogtreecommitdiff
path: root/codegen/test_cgen.py
blob: 6d1d4efb4c94a2d4117712de2388ca642206e096 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import unittest
from codegen.types import Variable, Function, Struct
from .cgen import as_str

test_variable_list = [
    (Variable('i', typedef='int'), 'int i;'),
]

test_function_list = [
    (Function('f', result='void'), 'void f();'),
    (Function('sum', result='uint32_t', args={'a': 'uint32_t', 'b': 'uint32_t'}),
     'uint32_t sum(uint32_t a, uint32_t b);'),
    (Function('printf', result='void', args={'format': 'const char *restrict', '...': None}),
     'void printf(const char *restrict format, ...);'),
]

test_struct_list = [
    (Struct('xyz', [ Variable('i', typedef='int'), Function('f', result='void') ]),
"""typedef struct {
  int i;
  void (*f)();
} xyz;"""),
]

class AsStrUnittests(unittest.TestCase):
    def test_as_str_variable(self):
        for item, expect in test_variable_list:
            with self.subTest(expect):
                self.assertEqual(as_str(item), expect)

    def test_as_str_function(self):
        for item, expect in test_function_list:
            with self.subTest(expect):
                self.assertEqual(as_str(item), expect)

    def test_as_str_struct(self):
        for item, expect in test_struct_list:
            with self.subTest(expect):
                self.assertEqual(as_str(item), expect)