def testStructSerialize(self): """Test generation of serialization code for typedefs.""" serialized_types = set(['int', 'FOO', 'BAR', 'TPMI_ALG_SYM_OBJECT']) struct = generator.Structure('TEST_STRUCT', False) struct.fields = [('TPMI_ALG_SYM_OBJECT', 'selector'), ('TPMU_SYM_MODE', 'mode'), ('int', 'sizeOfFoo'), ('int', 'foo[FOO_MAX]')] # Choose TPMU_SYM_MODE because it exists in the selectors definition and it # has few fields. union = generator.Structure('TPMU_SYM_MODE', True) union.fields = [('FOO', 'aes'), ('BAR', 'sm4')] typemap = {'TPMU_SYM_MODE': union} out_file = io.StringIO() struct.OutputSerialize(out_file, serialized_types, typemap) self.assertIn('TPMU_SYM_MODE', serialized_types) self.assertIn('TEST_STRUCT', serialized_types) out_file.close()
def testConstant(self): """Test generation of constant definitions and type dependencies.""" constant = generator.Constant('INT', 'test', '1') typemap = {'INT': generator.Structure('INT', False)} defined_types = set([]) out_file = StringIO.StringIO() constant.Output(out_file, defined_types, typemap) output_re = r'struct INT;\s+const INT test = 1;\s+' self.assertRegexpMatches(out_file.getvalue(), output_re) out_file.close()
def testStructure(self): """Test generation of structure declarations and field dependencies.""" struct = generator.Structure('STRUCT', False) struct.AddField('int', 'i') struct.AddDependency('DEPEND') union = generator.Structure('UNION', True) union.AddField('STRUCT', 'inner') depend = generator.Structure('DEPEND', False) defined_types = set(['int']) out_file = StringIO.StringIO() typemap = {'STRUCT': struct, 'DEPEND': depend} # Only output |union|, this will test the dependency logic. union.OutputForward(out_file, defined_types, typemap) union.OutputForward(out_file, defined_types, typemap) union.Output(out_file, defined_types, typemap) output_re = r'union UNION;\s+struct DEPEND {\s+};\s+' output_re += r'struct STRUCT {\s+int i;\s+};\s+' output_re += r'union UNION {\s+STRUCT inner;\s+};\s+' self.assertRegexpMatches(out_file.getvalue(), output_re) for t in ('STRUCT', 'DEPEND', 'UNION'): self.assertIn(t, defined_types) # Test serialize / parse code generation. out_file.close()
def testTypedef(self): """Test generation of typedefs and dependencies.""" typedef = generator.Typedef('int', 'INT') defined_types = set(['int']) typemap = {} out_file = StringIO.StringIO() # Expect this to just write the typedef. typedef.OutputForward(out_file, defined_types, typemap) # Expect this to know it has already been written. typedef.Output(out_file, defined_types, typemap) self.assertEqual(out_file.getvalue(), 'typedef int INT;\n') self.assertIn('INT', defined_types) typedef2 = generator.Typedef('TYPE1', 'TYPE2') typemap = {'TYPE1': generator.Structure('TYPE1', False)} defined_types = set([]) out_file2 = StringIO.StringIO() # Expect this to write first TYPE1 forward then TYPE2 typedef. typedef2.Output(out_file2, defined_types, typemap) output_re = r'struct TYPE1;\s+typedef TYPE1 TYPE2;\s+' self.assertRegexpMatches(out_file2.getvalue(), output_re) self.assertIn('TYPE2', defined_types) out_file.close() out_file2.close()