def test_pointer_to_pointer_to_array(self): self.assertTypeName( pointer_type( 8, pointer_type(8, array_type(2, int_type("int", 4, True)))), "int (**)[2]", True, )
def test_pointer_to_function_returning_const_pointer(self): i = int_type('int', 4, True) self.assertTypeName( pointer_type( 8, function_type(pointer_type(8, i, Qualifiers.CONST), ((i, ), ), False)), 'int * const (*)(int)', True)
def test_pointer_to_array_of_pointers(self): self.assertTypeName( pointer_type( 8, array_type(2, pointer_type(8, int_type("int", 4, True)))), "int *(*)[2]", True, )
def test_pointer_to_pointer_to_array(self): prog = mock_program() self.assertEqual( prog.type("int (**)[2]"), pointer_type( 8, pointer_type(8, array_type(2, int_type("int", 4, True)))), )
def test_pointer(self): self.assertTypeName(pointer_type(8, void_type()), "void *", True) t = pointer_type(8, void_type(Qualifiers.VOLATILE)) self.assertTypeName(t, "volatile void *", True) t = pointer_type(8, void_type(Qualifiers.VOLATILE), Qualifiers.CONST) self.assertTypeName(t, "volatile void * const", True) t = pointer_type(8, t) self.assertTypeName(t, "volatile void * const *", True)
def test_const_pointer_to_function_returning_pointer(self): i = int_type("int", 4, True) self.assertTypeName( pointer_type( 8, function_type(pointer_type(8, i), ((i,),), False), Qualifiers.CONST ), "int *(* const)(int)", True, )
def test_pointer_to_array(self): prog = mock_program() self.assertEqual( prog.type('int (*)[2]'), pointer_type(8, array_type(2, int_type('int', 4, True)))) self.assertEqual( prog.type('int (*)[2][3]'), pointer_type( 8, array_type(2, array_type(3, int_type('int', 4, True)))))
def test_pointer_to_function_returning_pointer(self): i = int_type('int', 4, True) self.assertTypeName( pointer_type(8, function_type(pointer_type(8, i), ((i, ), ), False)), 'int *(*)(int)', True) self.assertTypeName( pointer_type( 8, function_type(pointer_type(8, i), ((pointer_type(8, i), ), ), False)), 'int *(*)(int *)', True)
def test_pointer_to_array_of_pointers(self): prog = mock_program() self.assertEqual( prog.type('int *(*)[2]'), pointer_type( 8, array_type(2, pointer_type(8, int_type('int', 4, True))))) self.assertEqual( prog.type('int *((*)[2])'), pointer_type( 8, array_type(2, pointer_type(8, int_type('int', 4, True)))))
def test_pointer(self): prog = mock_program() self.assertEqual(prog.type("int *"), pointer_type(8, int_type("int", 4, True))) self.assertEqual( prog.type("const int *"), pointer_type(8, int_type("int", 4, True, Qualifiers.CONST)), ) self.assertEqual( prog.type("int * const"), pointer_type(8, int_type("int", 4, True), Qualifiers.CONST), ) self.assertEqual( prog.type("int **"), pointer_type(8, pointer_type(8, int_type("int", 4, True))), ) self.assertEqual( prog.type("int *((*))"), pointer_type(8, pointer_type(8, int_type("int", 4, True))), ) self.assertEqual( prog.type("int * const *"), pointer_type( 8, pointer_type(8, int_type("int", 4, True), Qualifiers.CONST) ), )
def test_pointer_to_function(self): i = int_type('int', 4, True) self.assertTypeName( pointer_type(8, function_type(i, ((i, ), ), False)), 'int (*)(int)', True) self.assertTypeName( pointer_type(8, function_type(i, ((i, 'x'), ), False)), 'int (*)(int x)', True) self.assertTypeName( pointer_type( 8, function_type(i, ((i, ), (float_type('float', 4), )), False)), 'int (*)(int, float)', True)
def test_pointer_to_function_returning_pointer_to_const(self): i = int_type("int", 4, True) self.assertTypeName( pointer_type( 8, function_type( pointer_type(8, int_type("int", 4, True, Qualifiers.CONST)), (TypeParameter(i),), False, ), ), "const int *(*)(int)", True, )
def test_cycle2(self): t1 = struct_type('list_head', 16, ( (lambda: pointer_type(8, t1), 'next'), (lambda: pointer_type(8, t1), 'prev', 8), )) t2 = struct_type('list_head', 16, ( (lambda: pointer_type(8, t2), 'next'), (lambda: pointer_type(8, t2), 'prev', 8), )) self.assertEqual(t1, t2) self.assertEqual( repr(t1), "struct_type(tag='list_head', size=16, members=((pointer_type(size=8, type=struct_type(tag='list_head', ...)), 'next', 0, 0), (pointer_type(size=8, type=struct_type(tag='list_head', ...)), 'prev', 8, 0)))" )
def test_pointer_to_function(self): i = int_type("int", 4, True) self.assertTypeName( pointer_type(8, function_type(i, ((i,),), False)), "int (*)(int)", True ) self.assertTypeName( pointer_type(8, function_type(i, ((i, "x"),), False)), "int (*)(int x)", True, ) self.assertTypeName( pointer_type(8, function_type(i, ((i,), (float_type("float", 4),)), False)), "int (*)(int, float)", True, )
def test_cycle(self): t1 = struct_type('foo', 8, ((lambda: pointer_type(8, t1), 'next'), )) t2 = struct_type('foo', 8, ((lambda: pointer_type(8, t2), 'next'), )) t3, t4 = (struct_type('foo', 8, ((lambda: pointer_type(8, t4), 'next'), )), struct_type('foo', 8, ((lambda: pointer_type(8, t3), 'next'), ))) self.assertEqual(t1, t2) self.assertEqual(t2, t3) self.assertEqual(t3, t4) self.assertEqual( repr(t1), "struct_type(tag='foo', size=8, members=((pointer_type(size=8, type=struct_type(tag='foo', ...)), 'next', 0, 0),))" )
def test_array_of_const_pointers_to_functions(self): i = int_type('int', 4, True) self.assertTypeName( array_type( None, pointer_type(8, function_type(i, ((i, ), ), False), Qualifiers.CONST)), 'int (* const [])(int)', True)
def test_typedef(self): self.assertPrettyPrint(typedef_type('INT', int_type('int', 4, True)), 'typedef int INT') self.assertPrettyPrint( typedef_type('CINT', int_type('int', 4, True, Qualifiers.CONST)), 'typedef const int CINT') self.assertPrettyPrint( typedef_type('INT', int_type('int', 4, True), Qualifiers.CONST), 'const typedef int INT') self.assertPrettyPrint( typedef_type('string', pointer_type(8, int_type('char', 1, True))), 'typedef char *string') t = typedef_type( 'Point', struct_type(None, 8, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 4), ))) self.assertPrettyPrint( t, """\ typedef struct { int x; int y; } Point""")
def test_array_of_pointers(self): self.assertTypeName( array_type( 2, array_type(3, pointer_type(8, int_type("int", 4, True)))), "int *[2][3]", True, )
def test_typedef(self): self.assertPrettyPrint(typedef_type("INT", int_type("int", 4, True)), "typedef int INT") self.assertPrettyPrint( typedef_type("CINT", int_type("int", 4, True, Qualifiers.CONST)), "typedef const int CINT", ) self.assertPrettyPrint( typedef_type("INT", int_type("int", 4, True), Qualifiers.CONST), "const typedef int INT", ) self.assertPrettyPrint( typedef_type("string", pointer_type(8, int_type("char", 1, True))), "typedef char *string", ) t = typedef_type( "Point", struct_type( None, 8, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 4), ), ), ) self.assertPrettyPrint( t, """\ typedef struct { int x; int y; } Point""", )
def test_pointer_to_function_with_no_parameters(self): self.assertTypeName( pointer_type(8, function_type(int_type("int", 4, True), (), False)), "int (*)(void)", True, )
def test_pointer_to_variadic_function(self): i = int_type("int", 4, True) self.assertTypeName( pointer_type(8, function_type(i, (TypeParameter(i), ), True)), "int (*)(int, ...)", True, )
def test_array_of_pointers_to_array(self): prog = mock_program() self.assertEqual( prog.type("int (*[2])[3]"), array_type( 2, pointer_type(8, array_type(3, int_type("int", 4, True)))), )
def test_array_of_pointers_to_functions(self): i = int_type("int", 4, True) self.assertTypeName( array_type(4, pointer_type(8, function_type(i, ((i,),), False))), "int (*[4])(int)", True, )
def test_array_of_pointers_to_array(self): self.assertTypeName( array_type( 2, pointer_type(8, array_type(3, int_type("int", 4, True)))), "int (*[2])[3]", True, )
def test_pointer_to_function_returning_pointer(self): i = int_type("int", 4, True) self.assertTypeName( pointer_type( 8, function_type(pointer_type(8, i), (TypeParameter(i), ), False)), "int *(*)(int)", True, ) self.assertTypeName( pointer_type( 8, function_type(pointer_type(8, i), (TypeParameter(pointer_type(8, i)), ), False), ), "int *(*)(int *)", True, )
def test_cycle(self): t1 = struct_type("foo", 8, (TypeMember(lambda: pointer_type(8, t1), "next"), )) t2 = struct_type("foo", 8, (TypeMember(lambda: pointer_type(8, t2), "next"), )) t3, t4 = ( struct_type("foo", 8, (TypeMember(lambda: pointer_type(8, t4), "next"), )), struct_type("foo", 8, (TypeMember(lambda: pointer_type(8, t3), "next"), )), ) self.assertEqual(t1, t2) self.assertEqual(t2, t3) self.assertEqual(t3, t4) self.assertEqual( repr(t1), "struct_type(tag='foo', size=8, members=(TypeMember(type=pointer_type(size=8, type=struct_type(tag='foo', ...)), name='next', bit_offset=0),))", )
def test_function(self): t = function_type(void_type(), ((int_type('int', 4, True), 'n'), )) self.assertEqual(t.kind, TypeKind.FUNCTION) self.assertIsNone(t.primitive) self.assertEqual(t.type, void_type()) self.assertEqual(t.parameters, ((int_type('int', 4, True), 'n'), )) self.assertFalse(t.is_variadic) self.assertTrue(t.is_complete()) self.assertEqual( t, function_type(void_type(), ((int_type('int', 4, True), 'n'), ))) # Different return type. self.assertNotEqual( t, function_type(int_type('int', 4, True), ((int_type('int', 4, True), 'n'), ))) # Different parameter name. self.assertNotEqual( t, function_type(void_type(), ((int_type('int', 4, True), 'x'), ))) # Unnamed parameter. self.assertNotEqual( t, function_type(void_type(), ((int_type('int', 4, True), ), ))) # Different number of parameters. self.assertNotEqual( t, function_type(void_type(), ((int_type('int', 4, True), 'n'), (pointer_type(8, void_type()), 'p')))) # One is variadic. self.assertNotEqual( t, function_type(void_type(), ((int_type('int', 4, True), 'n'), ), True)) self.assertEqual( repr(t), "function_type(type=void_type(), parameters=((int_type(name='int', size=4, is_signed=True), 'n'),), is_variadic=False)" ) self.assertRaises(TypeError, sizeof, t) self.assertFalse(function_type(void_type(), (), False).is_variadic) self.assertTrue(function_type(void_type(), (), True).is_variadic) self.assertRaisesRegex(TypeError, 'must be Type', function_type, None, ()) self.assertRaisesRegex(TypeError, 'must be sequence', function_type, void_type(), None) self.assertRaisesRegex(TypeError, 'must be.*sequence', function_type, void_type(), (4, )) self.assertRaisesRegex(ValueError, 'must be.*sequence', function_type, void_type, ((), )) self.assertRaisesRegex(TypeError, 'must be string or None', function_type, void_type(), ((int_type('int', 4, True), 4), )) self.assertRaisesRegex(TypeError, 'must be Type', function_type, void_type(), ((None, 'n'), ))
def test_array_of_const_pointers_to_functions(self): i = int_type("int", 4, True) self.assertTypeName( array_type( None, pointer_type(8, function_type(i, (TypeParameter(i), ), False), Qualifiers.CONST), ), "int (* const [])(int)", True, )
def test_pointer(self): t = pointer_type(8, int_type('int', 4, True)) self.assertEqual(t.kind, TypeKind.POINTER) self.assertIsNone(t.primitive) self.assertEqual(t.size, 8) self.assertEqual(t.type, int_type('int', 4, True)) self.assertTrue(t.is_complete()) self.assertEqual(t, pointer_type(8, int_type('int', 4, True))) # Qualified type argument. self.assertEqual(t, pointer_type(8, int_type('int', 4, True))) # Different size. self.assertNotEqual(t, pointer_type(4, int_type('int', 4, True))) # Different type. self.assertNotEqual(t, pointer_type(8, void_type())) self.assertNotEqual(t, pointer_type(8, void_type(Qualifiers.CONST))) self.assertEqual( repr(t), "pointer_type(size=8, type=int_type(name='int', size=4, is_signed=True))" ) self.assertRaises(TypeError, pointer_type, None, int_type('int', 4, True)) self.assertRaises(TypeError, pointer_type, 8, 4)
def test_cycle2(self): t1 = struct_type( "list_head", 16, ( TypeMember(lambda: pointer_type(8, t1), "next"), TypeMember(lambda: pointer_type(8, t1), "prev", 8), ), ) t2 = struct_type( "list_head", 16, ( TypeMember(lambda: pointer_type(8, t2), "next"), TypeMember(lambda: pointer_type(8, t2), "prev", 8), ), ) self.assertEqual(t1, t2) self.assertEqual( repr(t1), "struct_type(tag='list_head', size=16, members=(TypeMember(type=pointer_type(size=8, type=struct_type(tag='list_head', ...)), name='next', bit_offset=0), TypeMember(type=pointer_type(size=8, type=struct_type(tag='list_head', ...)), name='prev', bit_offset=8)))", )