def test_to_vtype_with_layout_with_one_field(self): layout_to_convert = layout.Layout( bit_size=32, bit_alignment=16, fields=[ layout.Field(bit_offset=0, name='x', layout=layout.Layout( bit_size=32, bit_alignment=16, )) ], ) type_definition = c_ast.CStruct([ c_ast.CField( name='x', type_definition=c_ast.CTypeReference('some_type'), ), ]) self.type_description_visitor.get_description.return_value = ( 'some_description') actual = self.layout_to_vtype_converter.to_vtype( layout_to_convert, type_definition, 'some_types', ) self.type_description_visitor.get_description.assert_called_with( c_ast.CTypeReference('some_type'), 'some_types', ) expected = [4, {'x': [0, 'some_description']}] self.assertEqual(actual, expected)
def test_with_chained_typedef_references_ended_by_unresolvable_type(self): some_type = c_ast.CSimpleType(33, 42) some_other_type = c_ast.CSimpleType(42, 33) type_1_t = c_ast.CTypedef( name='type_1_t', type_definition=c_ast.CTypeReference('some_type'), ) type_2_t = c_ast.CTypedef( name='type_2_t', type_definition=c_ast.CTypeReference('type_1_t'), ) type_3_t = c_ast.CTypedef( name='type_3_t', type_definition=c_ast.CTypeReference('type_2_t'), ) self.types.update({ 'some_type': some_type, 'some_other_type': some_other_type, 'type_1_t': type_1_t, 'type_2_t': type_2_t, 'type_3_t': type_3_t, }) node = c_ast.CTypeReference('type_3_t') actual = self.typedef_resolving_visitor.resolve(node, self.types) expected = c_ast.CTypeReference('some_type') self.assertEqual(actual, expected)
def test_resolve_with_type_reference_with_resolvable_definition(self): other_type_definition = c_ast.CTypeReference('some_other_name') self.types.update({ 'some_type_name': other_type_definition, }) node = c_ast.CTypeReference('some_type_name') actual = self.typedef_resolving_visitor.resolve(node, self.types) self.assertEqual(actual, other_type_definition)
def test_get_description_with_pointer(self): type_definition = c_ast.CPointer(c_ast.CTypeReference('some_type')) self.typedef_resolver.resolve.return_value = ( c_ast.CTypeReference('some_type')) actual = self.type_description_visitor.get_description( type_definition, self.types, ) expected = ['Pointer', {'target_args': None, 'target': 'some_type'}] self.assertEqual(actual, expected)
def test_get_description_with_type_reference_resolving_to_other_reference( self, ): type_definition = c_ast.CTypeReference('some_type') self.typedef_resolver.resolve.return_value = ( c_ast.CTypeReference('some_other_type')) actual = self.type_description_visitor.get_description( type_definition, self.types, ) expected = ['some_other_type'] self.assertEqual(actual, expected)
def test_to_vtype_with_layout_with_bit_field(self): layout_to_convert = layout.Layout( bit_size=48, bit_alignment=16, fields=[ layout.Field(bit_offset=33, name='x', layout=layout.Layout( bit_size=9, bit_alignment=16, bit_field=True, )) ], ) type_definition = c_ast.CStruct([ c_ast.CField( name='x', type_definition=c_ast.CTypeReference('some_type'), bit_size=9, ), ]) self.type_description_visitor.get_description.return_value = [ 'some_type' ] actual = self.layout_to_vtype_converter.to_vtype( layout_to_convert, type_definition, 'some_types', ) self.type_description_visitor.get_description.assert_called_with( c_ast.CTypeReference('some_type'), 'some_types', ) expected = [ 6, { 'x': [ 4, [ 'BitField', { 'start_bit': 1, 'end_bit': 10, 'target': 'some_type', } ], ], }, ] self.assertEqual(actual, expected)
def test_resolve_with_type_reference_with_unresolvable_definition(self): self.types.update({ 'some_type_name': c_ast.CSimpleType(33, 42), }) node = c_ast.CTypeReference('some_type_name') actual = self.typedef_resolving_visitor.resolve(node, self.types) self.assertEqual(actual, node)
def test_resolve_with_type_reference_with_name_not_in_types(self): self.types.update({ 'some_type_name': 'some_type_definition', }) node = c_ast.CTypeReference('some_other_name') actual = self.typedef_resolving_visitor.resolve(node, self.types) self.assertEqual(actual, node)
def test_resolve_with_typedef_with_resolvable_type(self): other_type_reference = c_ast.CTypeReference('some_other_name') node = c_ast.CTypedef( name='some_name', type_definition=other_type_reference, ) actual = self.typedef_resolving_visitor.resolve(node, self.types) self.assertEqual(actual, other_type_reference)
def test_get_description_with_type_reference_to_union(self): type_definition = c_ast.CTypeReference('struct some_union') self.typedef_resolver.resolve.return_value = type_definition actual = self.type_description_visitor.get_description( type_definition, self.types, ) expected = ['some_union'] self.assertEqual(actual, expected)
def test_get_descripiton_with_array_of_structs(self): type_definition = c_ast.CArray( length=c_ast.CNumber(33), type_definition=c_ast.CTypeReference('struct some_struct'), evaluated_length=33, ) self.typedef_resolver.resolve.return_value = ( c_ast.CTypeReference('struct some_struct')) actual = self.type_description_visitor.get_description( type_definition, self.types, ) expected = [ 'Array', { 'count': 33, 'target_args': None, 'target': 'some_struct' } ] self.assertEqual(actual, expected)
def test_get_descripiton_with_array_of_pointers_to_struct(self): type_definition = c_ast.CArray( length=c_ast.CNumber(42), type_definition=c_ast.CPointer(c_ast.CTypeReference('struct s')), evaluated_length=42, ) self.typedef_resolver.resolve.return_value = ( c_ast.CTypeReference('struct s')) actual = self.type_description_visitor.get_description( type_definition, self.types, ) expected = [ 'Array', { 'count': 42, 'target_args': { 'target_args': None, 'target': 's', }, 'target': 'Pointer' } ] self.assertEqual(actual, expected)
def test_get_description_with_type_reference_to_enum(self): type_definition = c_ast.CTypeReference('enum some_enum') self.typedef_resolver.resolve.return_value = type_definition actual = self.type_description_visitor.get_description( type_definition, self.types, ) expected = [ 'Enumeration', { 'target': 'long', 'enum_name': 'some_enum' } ] self.assertEqual(actual, expected)
def visit_c_enum(self, enum): _ = enum return self.compute_layout(c_ast.CTypeReference('int'))
def test_to_vtype_with_layout_with_three_fields(self): layout_to_convert = layout.Layout( bit_size=48, bit_alignment=32, fields=[ layout.Field(bit_offset=0, name='x', layout=layout.Layout( bit_size=16, bit_alignment=8, )), layout.Field(bit_offset=16, name='y', layout=layout.Layout( bit_size=8, bit_alignment=32, )), layout.Field(bit_offset=24, name='z', layout=layout.Layout( bit_size=24, bit_alignment=16, )), ], ) some_type = c_ast.CTypeReference('some_type') some_other_type = c_ast.CTypeReference('some_other_type') type_definition = c_ast.CStruct([ c_ast.CField( name='x', type_definition=some_type, ), c_ast.CField( name='y', type_definition=some_other_type, ), c_ast.CField( name='z', type_definition=some_type, ), ]) self.type_description_visitor.get_description.side_effect = ( 'some_description', 'some_other_description', 'some_description', ) actual = self.layout_to_vtype_converter.to_vtype( layout_to_convert, type_definition, 'some_types', ) expected_get_description_calls = [ mock.call(some_type, 'some_types'), mock.call(some_other_type, 'some_types'), mock.call(some_type, 'some_types'), ] self.assertEqual( self.type_description_visitor.get_description.call_args_list, expected_get_description_calls, ) expected = [ 6, { 'x': [0, 'some_description'], 'y': [2, 'some_other_description'], 'z': [3, 'some_description'], } ] self.assertEqual(actual, expected)
def test_collect_types_with_type_reference(self): type_reference = c_ast.CTypeReference('type_name') actual = self.type_collector.collect_types(type_reference) self.assertEqual(actual, {})