drgn/tests/test_typename.py

284 lines
13 KiB
Python
Raw Normal View History

import unittest
from drgn.typename import (
parse_type_name,
ArrayTypeName,
2018-03-07 08:31:00 +00:00
BasicTypeName,
EnumTypeName,
PointerTypeName,
StructTypeName,
TypedefTypeName,
UnionTypeName,
2018-03-07 08:31:00 +00:00
VoidTypeName,
)
# TODO: complex types
class TestParseTypeName(unittest.TestCase):
def test_empty(self):
self.assertRaises(ValueError, parse_type_name, '')
self.assertRaises(ValueError, parse_type_name, ' ')
def test_invalid_character(self):
self.assertRaises(ValueError, parse_type_name, '`')
def test_void(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(parse_type_name('void'), VoidTypeName())
def test_basic_types(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(parse_type_name('char'), BasicTypeName('char'))
self.assertEqual(parse_type_name('int'), BasicTypeName('int'))
self.assertEqual(parse_type_name('float'), BasicTypeName('float'))
self.assertEqual(parse_type_name('double'), BasicTypeName('double'))
self.assertEqual(parse_type_name('_Bool'), BasicTypeName('_Bool'))
def test_size(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(parse_type_name('long int'),
BasicTypeName('long int'))
self.assertEqual(parse_type_name('long'), BasicTypeName('long int'))
self.assertEqual(parse_type_name('int long'),
BasicTypeName('long int'))
self.assertEqual(parse_type_name('long long int'),
2018-03-07 08:31:00 +00:00
BasicTypeName('long long int'))
self.assertEqual(parse_type_name('long long'),
2018-03-07 08:31:00 +00:00
BasicTypeName('long long int'))
self.assertEqual(parse_type_name('int long long'),
2018-03-07 08:31:00 +00:00
BasicTypeName('long long int'))
self.assertEqual(parse_type_name('long int long'),
2018-03-07 08:31:00 +00:00
BasicTypeName('long long int'))
2018-03-07 08:31:00 +00:00
self.assertEqual(parse_type_name('short int'),
BasicTypeName('short int'))
self.assertEqual(parse_type_name('short'), BasicTypeName('short int'))
self.assertEqual(parse_type_name('int short'),
BasicTypeName('short int'))
self.assertEqual(parse_type_name('long double'),
2018-03-07 08:31:00 +00:00
BasicTypeName('long double'))
self.assertEqual(parse_type_name('double long'),
2018-03-07 08:31:00 +00:00
BasicTypeName('long double'))
self.assertRaises(ValueError, parse_type_name, 'short long int')
self.assertRaises(ValueError, parse_type_name, 'long long long int')
self.assertRaises(ValueError, parse_type_name, 'short double')
self.assertRaises(ValueError, parse_type_name, 'double short')
self.assertRaises(ValueError, parse_type_name, 'long long double')
self.assertRaises(ValueError, parse_type_name, 'long double long')
self.assertRaises(ValueError, parse_type_name, 'double long long')
self.assertRaises(ValueError, parse_type_name, 'long char')
self.assertRaises(ValueError, parse_type_name, 'char long')
def test_sign(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(parse_type_name('signed int'), BasicTypeName('int'))
self.assertEqual(parse_type_name('signed'), BasicTypeName('int'))
self.assertEqual(parse_type_name('int signed'), BasicTypeName('int'))
self.assertEqual(parse_type_name('unsigned int'),
2018-03-07 08:31:00 +00:00
BasicTypeName('unsigned int'))
self.assertEqual(parse_type_name('unsigned'),
2018-03-07 08:31:00 +00:00
BasicTypeName('unsigned int'))
self.assertEqual(parse_type_name('int unsigned'),
2018-03-07 08:31:00 +00:00
BasicTypeName('unsigned int'))
self.assertEqual(parse_type_name('signed char'),
2018-03-07 08:31:00 +00:00
BasicTypeName('signed char'))
self.assertEqual(parse_type_name('char signed'),
2018-03-07 08:31:00 +00:00
BasicTypeName('signed char'))
self.assertEqual(parse_type_name('unsigned char'),
2018-03-07 08:31:00 +00:00
BasicTypeName('unsigned char'))
self.assertEqual(parse_type_name('char unsigned'),
2018-03-07 08:31:00 +00:00
BasicTypeName('unsigned char'))
self.assertRaises(ValueError, parse_type_name, 'signed unsigned int')
self.assertRaises(ValueError, parse_type_name, 'signed _Bool')
self.assertRaises(ValueError, parse_type_name, '_Bool signed')
def test_qualifiers(self):
self.assertEqual(parse_type_name('const int'),
2018-03-07 08:31:00 +00:00
BasicTypeName('int', qualifiers={'const'}))
self.assertEqual(parse_type_name('restrict int'),
2018-03-07 08:31:00 +00:00
BasicTypeName('int', qualifiers={'restrict'}))
self.assertEqual(parse_type_name('volatile int'),
2018-03-07 08:31:00 +00:00
BasicTypeName('int', qualifiers={'volatile'}))
self.assertEqual(parse_type_name('_Atomic int'),
2018-03-07 08:31:00 +00:00
BasicTypeName('int', qualifiers={'_Atomic'}))
self.assertEqual(parse_type_name('const volatile int'),
2018-03-07 08:31:00 +00:00
BasicTypeName('int', qualifiers={'const', 'volatile'}))
self.assertEqual(parse_type_name('const const int'),
2018-03-07 08:31:00 +00:00
BasicTypeName('int', qualifiers={'const'}))
def test_specifiers_qualifiers(self):
self.assertEqual(parse_type_name('long const int unsigned'),
2018-03-07 08:31:00 +00:00
BasicTypeName('long unsigned int',
qualifiers={'const'}))
def test_typedef(self):
self.assertEqual(parse_type_name('u32'), TypedefTypeName('u32'))
def test_tagged_type(self):
self.assertEqual(parse_type_name('struct point'),
StructTypeName('point'))
self.assertEqual(parse_type_name('union value'),
UnionTypeName('value'))
self.assertEqual(parse_type_name('enum color'), EnumTypeName('color'))
def test_pointer(self):
self.assertEqual(parse_type_name('int *'),
2018-03-07 08:31:00 +00:00
PointerTypeName(BasicTypeName('int')))
self.assertEqual(parse_type_name('int * const'),
2018-03-07 08:31:00 +00:00
PointerTypeName(BasicTypeName('int'),
qualifiers={'const'}))
self.assertEqual(parse_type_name('struct point *'),
PointerTypeName(StructTypeName('point')))
self.assertEqual(parse_type_name('int **'),
2018-03-07 08:31:00 +00:00
PointerTypeName(PointerTypeName(BasicTypeName('int'))))
self.assertEqual(parse_type_name('int *((*))'),
2018-03-07 08:31:00 +00:00
PointerTypeName(PointerTypeName(BasicTypeName('int'))))
self.assertEqual(parse_type_name('int * const *'),
2018-03-07 08:31:00 +00:00
PointerTypeName(PointerTypeName(BasicTypeName('int'),
qualifiers={'const'})))
def test_array(self):
self.assertEqual(parse_type_name('int []'),
2018-03-07 08:31:00 +00:00
ArrayTypeName(BasicTypeName('int'), None))
self.assertEqual(parse_type_name('int [2]'),
2018-03-07 08:31:00 +00:00
ArrayTypeName(BasicTypeName('int'), 2))
self.assertEqual(parse_type_name('int [0x10]'),
2018-03-07 08:31:00 +00:00
ArrayTypeName(BasicTypeName('int'), 16))
self.assertEqual(parse_type_name('int [010]'),
2018-03-07 08:31:00 +00:00
ArrayTypeName(BasicTypeName('int'), 8))
self.assertEqual(parse_type_name('int [2][3]'),
2018-03-07 08:31:00 +00:00
ArrayTypeName(ArrayTypeName(BasicTypeName('int'), 3), 2))
self.assertEqual(parse_type_name('int [2][3][4]'),
2018-03-07 08:31:00 +00:00
ArrayTypeName(ArrayTypeName(ArrayTypeName(BasicTypeName('int'), 4), 3), 2))
def test_array_of_pointers(self):
self.assertEqual(parse_type_name('int *[2][3]'),
2018-03-07 08:31:00 +00:00
ArrayTypeName(ArrayTypeName(PointerTypeName(BasicTypeName('int')), 3), 2))
def test_pointer_to_array(self):
self.assertEqual(parse_type_name('int (*)[2]'),
2018-03-07 08:31:00 +00:00
PointerTypeName(ArrayTypeName(BasicTypeName('int'), 2)))
self.assertEqual(parse_type_name('int (*)[2][3]'),
2018-03-07 08:31:00 +00:00
PointerTypeName(ArrayTypeName(ArrayTypeName(BasicTypeName('int'), 3), 2)))
def test_pointer_to_pointer_to_array(self):
self.assertEqual(parse_type_name('int (**)[2]'),
2018-03-07 08:31:00 +00:00
PointerTypeName(PointerTypeName(ArrayTypeName(BasicTypeName('int'), 2))))
def test_pointer_to_array_of_pointers(self):
self.assertEqual(parse_type_name('int *(*)[2]'),
2018-03-07 08:31:00 +00:00
PointerTypeName(ArrayTypeName(PointerTypeName(BasicTypeName('int')), 2)))
self.assertEqual(parse_type_name('int *((*)[2])'),
2018-03-07 08:31:00 +00:00
PointerTypeName(ArrayTypeName(PointerTypeName(BasicTypeName('int')), 2)))
def test_array_of_pointers_to_array(self):
self.assertEqual(parse_type_name('int (*[2])[3]'),
2018-03-07 08:31:00 +00:00
ArrayTypeName(PointerTypeName(ArrayTypeName(BasicTypeName('int'), 3)), 2))
class TestTypeStr(unittest.TestCase):
def test_void(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(BasicTypeName('void')), 'void')
def test_basic_types(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(BasicTypeName('char')), 'char')
self.assertEqual(str(BasicTypeName('int')), 'int')
self.assertEqual(str(BasicTypeName('float')), 'float')
self.assertEqual(str(BasicTypeName('double')), 'double')
self.assertEqual(str(BasicTypeName('_Bool')), '_Bool')
def test_size(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(BasicTypeName('long int')), 'long int')
self.assertEqual(str(BasicTypeName('long long int')), 'long long int')
self.assertEqual(str(BasicTypeName('short int')), 'short int')
self.assertEqual(str(BasicTypeName('long double')), 'long double')
def test_sign(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(BasicTypeName('unsigned int')), 'unsigned int')
self.assertEqual(str(BasicTypeName('signed char')), 'signed char')
self.assertEqual(str(BasicTypeName('unsigned char')), 'unsigned char')
def test_qualifiers(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(BasicTypeName('int', qualifiers={'const'})),
'const int')
2018-03-07 08:31:00 +00:00
self.assertEqual(str(BasicTypeName('int', qualifiers={'restrict'})),
'restrict int')
2018-03-07 08:31:00 +00:00
self.assertEqual(str(BasicTypeName('int', qualifiers={'volatile'})),
'volatile int')
2018-03-07 08:31:00 +00:00
self.assertEqual(str(BasicTypeName('int', qualifiers={'_Atomic'})),
'_Atomic int')
2018-03-07 08:31:00 +00:00
self.assertEqual(str(BasicTypeName('int', qualifiers={'const', 'volatile'})),
'const volatile int')
def test_specifiers_qualifiers(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(BasicTypeName('long unsigned int',
qualifiers={'const'})),
'const long unsigned int')
def test_typedef(self):
self.assertEqual(str(TypedefTypeName('u32')), 'u32')
def test_tagged_type(self):
self.assertEqual(str(StructTypeName('point')), 'struct point')
self.assertEqual(str(UnionTypeName('value')), 'union value')
self.assertEqual(str(EnumTypeName('color')), 'enum color')
self.assertEqual(str(StructTypeName(None)), 'struct')
self.assertEqual(str(UnionTypeName(None)), 'union')
self.assertEqual(str(EnumTypeName(None)), 'enum')
def test_pointer(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(PointerTypeName(BasicTypeName('int'))), 'int *')
self.assertEqual(str(PointerTypeName(BasicTypeName('int'), qualifiers={'const'})),
'int * const')
2018-03-07 08:31:00 +00:00
self.assertEqual(str(PointerTypeName(BasicTypeName('struct point'))),
'struct point *')
2018-03-07 08:31:00 +00:00
self.assertEqual(str(PointerTypeName(PointerTypeName(BasicTypeName('int')))),
'int **')
2018-03-07 08:31:00 +00:00
self.assertEqual(str(PointerTypeName(PointerTypeName(BasicTypeName('int'),
qualifiers={'const'}))),
'int * const *')
def test_array(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(ArrayTypeName(BasicTypeName('int'), None)),
'int []')
2018-03-07 08:31:00 +00:00
self.assertEqual(str(ArrayTypeName(BasicTypeName('int'), 2)),
'int [2]')
2018-03-07 08:31:00 +00:00
self.assertEqual(str(ArrayTypeName(ArrayTypeName(BasicTypeName('int'), 3), 2)),
'int [2][3]')
2018-03-07 08:31:00 +00:00
self.assertEqual(str(ArrayTypeName(ArrayTypeName(ArrayTypeName(BasicTypeName('int'), 4), 3), 2)),
'int [2][3][4]')
def test_array_of_pointers(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(ArrayTypeName(ArrayTypeName(PointerTypeName(BasicTypeName('int')), 3), 2)),
'int *[2][3]')
def test_pointer_to_array(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(PointerTypeName(ArrayTypeName(BasicTypeName('int'), 2))),
'int (*)[2]')
self.assertEqual(parse_type_name('int (*)[2][3]'),
2018-03-07 08:31:00 +00:00
PointerTypeName(ArrayTypeName(ArrayTypeName(BasicTypeName('int'), 3), 2)))
def test_pointer_to_pointer_to_array(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(PointerTypeName(PointerTypeName(ArrayTypeName(BasicTypeName('int'), 2)))),
'int (**)[2]')
def test_pointer_to_array_of_pointers(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(PointerTypeName(ArrayTypeName(PointerTypeName(BasicTypeName('int')), 2))),
'int *(*)[2]')
def test_array_of_pointers_to_array(self):
2018-03-07 08:31:00 +00:00
self.assertEqual(str(ArrayTypeName(PointerTypeName(ArrayTypeName(BasicTypeName('int'), 3)), 2)),
'int (*[2])[3]')