def test_parse_struct_with_few_vars_defined_at_once_with_bitfield(self): s_info = parse_struct('struct s{int a, b : 1;};', 'file') s_expect = Struct('s', '', [Param('a', 'int'), Param('b', 'int')]) self.assertEqual(s_info, s_expect) self.assertEqual(s_info.members_list[0].size, '1') self.assertEqual(s_info.members_list[1].size, '1')
def test_parse_struct_few_vars_defined_at_once(self): self.assertEqual( parse_struct('struct s{int a, b, c;};', 'file'), Struct('s', '', [Param('a', 'int'), Param('b', 'int'), Param('c', 'int')]))
def test_parse_struct_nested_union(self): self.assertEqual( parse_struct('struct a{ union c{char b;}b; int a; };', 'file'), Struct('a', '', [ Param('b', Union('c', '', [Param('b', 'char')])), Param('a', 'int') ], 'file'))
def test_parse_struct_to_type_for_json(self): types = {} struct = 'typedef struct xy { int a;};' s_info = parse_struct(struct, 'file') parse_struct_to_type_for_json(s_info, types) st = StructType('xy', []) pt = PrimitiveType('int') expected = {st.type_hash: st, pt.type_hash: pt} self.assertEqual(types, expected)
def test_parse_typedef_to_struct_without_name_to_type_for_json(self): types = {} struct = 'typedef struct {} type_s;' s_info = parse_struct(struct, 'file') parse_struct_to_type_for_json(s_info, types) st = StructType('_TYPEDEF_type_s', []) typedef = TypedefedType('type_s', st) expected = {st.type_hash: st, typedef.type_hash: typedef} self.assertEqual(types, expected)
def test_parse_enum_inside_struct_to_type_for_json(self): struct = 'struct s{ enum e{ X, Y }p; };' types = {} struct = parse_struct(struct, 'file') parse_struct_to_type_for_json(struct, types) e = EnumType('e', [EnumItem('X', 0), EnumItem('Y', 1)]) s = StructType('s', [Param('p', e)]) expected = {s.type_hash: s, e.type_hash: e} self.assertEqual(types, expected)
def test_parse_nested_structs_without_name_to_type_for_json(self): struct = 'struct s1{ struct { char c; }s2; };' types = {} struct = parse_struct(struct, 'file') parse_struct_to_type_for_json(struct, types) prim = PrimitiveType('char') s1 = StructType('s1', []) s2 = StructType('_LOCAL_s1_s2', []) expected = {prim.type_hash: prim, s1.type_hash: s1, s2.type_hash: s2} self.assertEqual(types, expected)
def test_parse_nested_union_without_name_to_type_for_json(self): union = 'union s1{ union { char c; }s2; };' types = {} union = parse_struct(union, 'file') parse_union_to_type_for_json(union, types) prim = PrimitiveType('char') s1 = UnionType('s1', []) s2 = UnionType('_LOCAL_s1_s2', []) expected = {prim.type_hash: prim, s1.type_hash: s1, s2.type_hash: s2} self.assertEqual(types, expected)
def test_parse_struct_with_bitfields(self): s_info = parse_struct('struct s{int a : 1; int b: 3; char c:5;};', 'file') s_expect = Struct( 's', '', [Param('a', 'int'), Param('b', 'int'), Param('c', 'char')]) self.assertEqual(s_info, s_expect) self.assertEqual(s_info.members_list[0].size, '1') self.assertEqual(s_info.members_list[1].size, '3') self.assertEqual(s_info.members_list[2].size, '5')
def test_parse_typedef_ptr_to_struct_to_type_for_json(self): types = {} struct = 'typedef struct xy {} *ptr_s;' s_info = parse_struct(struct, 'file') parse_struct_to_type_for_json(s_info, types) st = StructType('xy', []) ptr = PointerType(st) typedef = TypedefedType('ptr_s', ptr) expected = { st.type_hash: st, ptr.type_hash: ptr, typedef.type_hash: typedef } self.assertEqual(types, expected)
def test_struct_with_func_ptr_to_type_for_json(self): types = {} struct = 'struct s{int *(* ptr)();};' s_info = parse_struct(struct, 'file') parse_struct_to_type_for_json(s_info, types) tint = PrimitiveType('int') ptrint = PointerType(tint) func = FunctionType(ptrint, []) func_ptr = PointerType(func) st = StructType('s', [func_ptr]) expected = { tint.type_hash: tint, ptrint.type_hash: ptrint, func.type_hash: func, func_ptr.type_hash: func_ptr, st.type_hash: st } self.assertEqual(types, expected)
def test_arithmetic_expr_in_array_size(self): self.assertEqual( parse_struct('struct s{int a[5 + 4 / 2 - sizeof(int)];};', 'file'), Struct('s', '', [Param('a', 'int [5 + 4 / 2 - sizeof(int)]')]))
def test_parse_struct_with_array_in_data(self): self.assertEqual( parse_struct('typedef struct{ int a[100];} newS, *ptr;', 'file'), Struct('', 'newS, *ptr', [Param('a', 'int [100]')], 'file'))
def test_parse_struct_with_func_as_attribute(self): self.assertEqual( parse_struct('struct s{int * (* func)(int);};', 'file'), Struct('s', '', [Param('func', 'int * (*)(int)')]))
def test_parse_struct_with_simple_data(self): self.assertEqual( parse_struct('struct x { int x; char * data; };', 'file'), Struct( 'x', '', [Param('x', 'int'), Param('data', 'char *')], 'file'))
def test_parse_struct_with_no_data(self): self.assertEqual(parse_struct('struct a{};', 'file'), Struct('a', '', [], 'file'))
def test_input_is_not_struct_contains_struct_keyword(self): self.assertEqual(parse_struct('no struct ( int ax;);', 'file'), Struct('', '', [], 'file'))
def test_parse_typedef_to_struct_without_name(self): self.assertEqual( parse_struct('typedef struct{ int a;} newS, *ptr;', 'file'), Struct('', 'newS, *ptr', [Param('a', 'int')], 'file'))
def test_name_after_struct_is_param_name_when_missing_typedef_keyword( self): self.assertEqual(parse_struct('struct s{int i;}param;', 'file'), Struct('s', '', [Param('i', 'int')]))
def test_member_wrapped_in_macro_is_recovered_as_type(self): self.assertEqual( parse_struct('struct s{ __MACRO(maybe_type); };', 'file'), Struct('s', '', [Param('', 'maybe_type')]))
def test_parameter_name_wrapped_in_macro_is_correctly_recovered(self): self.assertEqual( parse_struct('struct s{int __MACRO(name); };', 'file'), Struct('s', '', [Param('name', 'int')]))
def test_wrong_count_of_brackets_returns_what_is_already_parsed(self): self.assertEqual( parse_struct('struct s{int i; struct a{{{int b;}};', 'file'), Struct('s', '', [Param('i', 'int')]))
def test_invalid_member_is_returned_as_empty_type(self): self.assertEqual(parse_struct('struct s{ int x{int}z; };', 'file'), Struct('s', '', [Param('z', '')]))
def test_parse_struct_with_typedef_name(self): self.assertEqual( parse_struct('typedef struct xy{ int a; char c;} newS, *ptr;', 'file'), Struct('xy', 'newS, *ptr', [Param('a', 'int'), Param('c', 'char')], 'file'))