From fbe4a5eeab2107dfe03fc097bc1f9627b222adbd Mon Sep 17 00:00:00 2001 From: aqua Date: Sun, 22 Oct 2023 18:42:58 +0300 Subject: Initial commit --- codegen/test_cgen.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 codegen/test_cgen.py (limited to 'codegen/test_cgen.py') diff --git a/codegen/test_cgen.py b/codegen/test_cgen.py new file mode 100644 index 0000000..6d1d4ef --- /dev/null +++ b/codegen/test_cgen.py @@ -0,0 +1,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) -- cgit v1.2.1