def test_class(self): t = class_type('coord', 12, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 32), (int_type('int', 4, True), 'z', 64), )) self.assertEqual(t.kind, TypeKind.CLASS) self.assertIsNone(t.primitive) self.assertEqual(t.tag, 'coord') self.assertEqual(t.size, 12) self.assertEqual(t.members, ( (int_type('int', 4, True), 'x', 0, 0), (int_type('int', 4, True), 'y', 32, 0), (int_type('int', 4, True), 'z', 64, 0), )) self.assertTrue(t.is_complete()) self.assertEqual( t, class_type('coord', 12, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 32), (int_type('int', 4, True), 'z', 64), ))) # Different tag. self.assertNotEqual( t, class_type('crd', 12, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 32), (int_type('int', 4, True), 'z', 64), ))) # Different size. self.assertNotEqual( t, class_type('coord', 16, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 32), (int_type('int', 4, True), 'z', 64), ))) # One is anonymous. self.assertNotEqual( t, class_type(None, 12, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 32), (int_type('int', 4, True), 'z', 64), ))) # Different members. self.assertNotEqual( t, class_type('coord', 12, ( (int_type('long', 8, True), 'x', 0), (int_type('long', 8, True), 'y', 64), (int_type('long', 8, True), 'z', 128), ))) # Different number of members. self.assertNotEqual( t, class_type('coord', 12, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 32), ))) # One member is anonymous. self.assertNotEqual( t, class_type('coord', 8, ( (int_type('int', 4, True), 'x', 0, 0), (int_type('int', 4, True), None, 32, 0), (int_type('int', 4, True), 'z', 64, 0), ))) # One is incomplete. self.assertNotEqual(t, class_type('coord')) self.assertEqual( repr(t), "class_type(tag='coord', size=12, members=((int_type(name='int', size=4, is_signed=True), 'x', 0, 0), (int_type(name='int', size=4, is_signed=True), 'y', 32, 0), (int_type(name='int', size=4, is_signed=True), 'z', 64, 0)))" ) self.assertEqual(sizeof(t), 12) t = class_type(None, 12, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 32), (int_type('int', 4, True), 'z', 64), )) self.assertEqual(t.kind, TypeKind.CLASS) self.assertIsNone(t.primitive) self.assertIsNone(t.tag) self.assertEqual(t.size, 12) self.assertEqual(t.members, ( (int_type('int', 4, True), 'x', 0, 0), (int_type('int', 4, True), 'y', 32, 0), (int_type('int', 4, True), 'z', 64, 0), )) self.assertTrue(t.is_complete()) t = class_type('color', 0, ()) self.assertEqual(t.kind, TypeKind.CLASS) self.assertIsNone(t.primitive) self.assertEqual(t.tag, 'color') self.assertEqual(t.size, 0) self.assertEqual(t.members, ()) self.assertTrue(t.is_complete()) self.assertEqual(repr(t), "class_type(tag='color', size=0, members=())") t = class_type('color') self.assertEqual(t.kind, TypeKind.CLASS) self.assertIsNone(t.primitive) self.assertEqual(t.tag, 'color') self.assertIsNone(t.size) self.assertIsNone(t.members) self.assertFalse(t.is_complete()) self.assertEqual(repr(t), "class_type(tag='color', size=None, members=None)") t = class_type(None, None, None) self.assertEqual(t.kind, TypeKind.CLASS) self.assertIsNone(t.primitive) self.assertEqual(t.tag, None) self.assertIsNone(t.size) self.assertIsNone(t.members) self.assertFalse(t.is_complete()) self.assertEqual(repr(t), "class_type(tag=None, size=None, members=None)") self.assertRaises(TypeError, class_type, 4) self.assertRaisesRegex(ValueError, 'must not have size', class_type, 'coord', 12, None) self.assertRaisesRegex(ValueError, 'must have size', class_type, 'coord', None, ()) self.assertRaisesRegex(TypeError, 'must be sequence or None', class_type, 'coord', 12, 4) self.assertRaisesRegex(TypeError, 'must be.*sequence', class_type, 'coord', 12, (4)) self.assertRaisesRegex(ValueError, 'must be.*sequence', class_type, 'coord', 12, ((), )) self.assertRaisesRegex(TypeError, 'must be string or None', class_type, 'coord', 12, ((int_type('int', 4, True), 4, 0), )) self.assertRaisesRegex(TypeError, 'must be integer', class_type, 'coord', 12, ((int_type('int', 4, True), 'x', None), )) self.assertRaisesRegex(TypeError, 'must be Type', class_type, 'coord', 12, ((None, 'x', 0), )) # Bit size. t = class_type('coord', 12, ( (int_type('int', 4, True), 'x', 0, 4), (int_type('int', 4, True), 'y', 32, 4), (int_type('int', 4, True), 'z', 64, 4), )) self.assertEqual(t.members, ( (int_type('int', 4, True), 'x', 0, 4), (int_type('int', 4, True), 'y', 32, 4), (int_type('int', 4, True), 'z', 64, 4), ))
class_type, enum_type, float_type, int_type, struct_type, typedef_type, union_type, ) DEFAULT_LANGUAGE = Language.C coord_type = class_type( "coord", 12, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 32), TypeMember(int_type("int", 4, True), "z", 64), ), ) point_type = struct_type( "point", 8, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 32), ), ) line_segment_type = struct_type( "line_segment", 16, (TypeMember(point_type, "a"), TypeMember(point_type, "b", 64)))
def test_class(self): t = class_type( "coord", 12, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 32), TypeMember(int_type("int", 4, True), "z", 64), ), ) self.assertEqual(t.kind, TypeKind.CLASS) self.assertIsNone(t.primitive) self.assertEqual(t.language, DEFAULT_LANGUAGE) self.assertEqual(t.tag, "coord") self.assertEqual(t.size, 12) self.assertEqual( t.members, ( TypeMember(int_type("int", 4, True), "x", 0, 0), TypeMember(int_type("int", 4, True), "y", 32, 0), TypeMember(int_type("int", 4, True), "z", 64, 0), ), ) self.assertTrue(t.is_complete()) self.assertEqual( t, class_type( "coord", 12, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 32), TypeMember(int_type("int", 4, True), "z", 64), ), ), ) # Different tag. self.assertNotEqual( t, class_type( "crd", 12, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 32), TypeMember(int_type("int", 4, True), "z", 64), ), ), ) # Different size. self.assertNotEqual( t, class_type( "coord", 16, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 32), TypeMember(int_type("int", 4, True), "z", 64), ), ), ) # One is anonymous. self.assertNotEqual( t, class_type( None, 12, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 32), TypeMember(int_type("int", 4, True), "z", 64), ), ), ) # Different members. self.assertNotEqual( t, class_type( "coord", 12, ( TypeMember(int_type("long", 8, True), "x", 0), TypeMember(int_type("long", 8, True), "y", 64), TypeMember(int_type("long", 8, True), "z", 128), ), ), ) # Different number of members. self.assertNotEqual( t, class_type( "coord", 12, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 32), ), ), ) # One member is anonymous. self.assertNotEqual( t, class_type( "coord", 8, ( TypeMember(int_type("int", 4, True), "x", 0, 0), TypeMember(int_type("int", 4, True), None, 32, 0), TypeMember(int_type("int", 4, True), "z", 64, 0), ), ), ) # One is incomplete. self.assertNotEqual(t, class_type("coord")) self.assertEqual( repr(t), "class_type(tag='coord', size=12, members=(TypeMember(type=int_type(name='int', size=4, is_signed=True), name='x', bit_offset=0), TypeMember(type=int_type(name='int', size=4, is_signed=True), name='y', bit_offset=32), TypeMember(type=int_type(name='int', size=4, is_signed=True), name='z', bit_offset=64)))", ) self.assertEqual(sizeof(t), 12) t = class_type( None, 12, ( TypeMember(int_type("int", 4, True), "x", 0), TypeMember(int_type("int", 4, True), "y", 32), TypeMember(int_type("int", 4, True), "z", 64), ), ) self.assertEqual(t.kind, TypeKind.CLASS) self.assertIsNone(t.primitive) self.assertIsNone(t.tag) self.assertEqual(t.size, 12) self.assertEqual( t.members, ( TypeMember(int_type("int", 4, True), "x", 0, 0), TypeMember(int_type("int", 4, True), "y", 32, 0), TypeMember(int_type("int", 4, True), "z", 64, 0), ), ) self.assertTrue(t.is_complete()) t = class_type("color", 0, ()) self.assertEqual(t.kind, TypeKind.CLASS) self.assertIsNone(t.primitive) self.assertEqual(t.tag, "color") self.assertEqual(t.size, 0) self.assertEqual(t.members, ()) self.assertTrue(t.is_complete()) self.assertEqual(repr(t), "class_type(tag='color', size=0, members=())") t = class_type("color") self.assertEqual(t.kind, TypeKind.CLASS) self.assertIsNone(t.primitive) self.assertEqual(t.tag, "color") self.assertIsNone(t.size) self.assertIsNone(t.members) self.assertFalse(t.is_complete()) self.assertEqual(repr(t), "class_type(tag='color', size=None, members=None)") t = class_type(None, None, None) self.assertEqual(t.kind, TypeKind.CLASS) self.assertIsNone(t.primitive) self.assertEqual(t.tag, None) self.assertIsNone(t.size) self.assertIsNone(t.members) self.assertFalse(t.is_complete()) self.assertEqual(repr(t), "class_type(tag=None, size=None, members=None)") self.assertRaises(TypeError, class_type, 4) self.assertRaisesRegex(ValueError, "must not have size", class_type, "coord", 12, None) self.assertRaisesRegex(ValueError, "must have size", class_type, "coord", None, ()) self.assertRaisesRegex(TypeError, "must be sequence or None", class_type, "coord", 12, 4) self.assertRaisesRegex(TypeError, "must be TypeMember", class_type, "coord", 12, (4, )) # Bit size. t = class_type( "coord", 12, ( TypeMember(int_type("int", 4, True), "x", 0, 4), TypeMember(int_type("int", 4, True), "y", 32, 4), TypeMember(int_type("int", 4, True), "z", 64, 4), ), ) self.assertEqual( t.members, ( TypeMember(int_type("int", 4, True), "x", 0, 4), TypeMember(int_type("int", 4, True), "y", 32, 4), TypeMember(int_type("int", 4, True), "z", 64, 4), ), )
Type, TypeKind, class_type, enum_type, float_type, int_type, struct_type, typedef_type, union_type, ) coord_type = class_type( "coord", 12, ( (int_type("int", 4, True), "x", 0), (int_type("int", 4, True), "y", 32), (int_type("int", 4, True), "z", 64), ), ) point_type = struct_type( "point", 8, ( (int_type("int", 4, True), "x", 0), (int_type("int", 4, True), "y", 32), ), ) line_segment_type = struct_type("line_segment", 16, ( (point_type, "a"), (point_type, "b", 64),
PlatformFlags, Program, Type, TypeKind, class_type, enum_type, float_type, int_type, struct_type, typedef_type, union_type, ) coord_type = class_type('coord', 12, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 32), (int_type('int', 4, True), 'z', 64), )) point_type = struct_type('point', 8, ( (int_type('int', 4, True), 'x', 0), (int_type('int', 4, True), 'y', 32), )) line_segment_type = struct_type('line_segment', 16, ( (point_type, 'a'), (point_type, 'b', 64), )) option_type = union_type('option', 4, ( (int_type('int', 4, True), 'i'), (float_type('float', 4), 'f'), )) color_type = enum_type('color', int_type('unsigned int', 4, False),