summaryrefslogtreecommitdiff
path: root/codegen/test_cgen.py
diff options
context:
space:
mode:
Diffstat (limited to 'codegen/test_cgen.py')
-rw-r--r--codegen/test_cgen.py39
1 files changed, 39 insertions, 0 deletions
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)