def test_union(self): t = union_type( "foo", 4, ( (int_type("int", 4, True), "i"), (array_type(4, int_type("unsigned char", 1, False)), "a"), ), ) self.assertPrettyPrint( t, """\ union foo { int i; unsigned char a[4]; }""", ) t = union_type( "foo", 4, ( (int_type("int", 4, True), "i"), (array_type(4, int_type("unsigned char", 1, False)), "a"), ), Qualifiers.CONST, ) self.assertPrettyPrint( t, """\ const union foo { int i; unsigned char a[4]; }""", )
def test_union(self): t = union_type('foo', 4, ( (int_type('int', 4, True), 'i'), (array_type(4, int_type('unsigned char', 1, False)), 'a'), )) self.assertPrettyPrint( t, """\ union foo { int i; unsigned char a[4]; }""") t = union_type('foo', 4, ( (int_type('int', 4, True), 'i'), (array_type(4, int_type('unsigned char', 1, False)), 'a'), ), Qualifiers.CONST) self.assertPrettyPrint( t, """\ const union foo { int i; unsigned char a[4]; }""")
def test_union(self): t = union_type('option', 4, ( (int_type('int', 4, True), 'x'), (int_type('unsigned int', 4, False), 'y'), )) self.assertEqual(t.kind, TypeKind.UNION) self.assertIsNone(t.primitive) self.assertEqual(t.tag, 'option') self.assertEqual(t.size, 4) self.assertEqual(t.members, ( (int_type('int', 4, True), 'x', 0, 0), (int_type('unsigned int', 4, False), 'y', 0, 0), )) self.assertTrue(t.is_complete()) self.assertEqual( t, union_type('option', 4, ( (int_type('int', 4, True), 'x'), (int_type('unsigned int', 4, False), 'y'), ))) # Different tag. self.assertNotEqual( t, union_type('pt', 4, ( (int_type('int', 4, True), 'x'), (int_type('unsigned int', 4, False), 'y'), ))) # Different size. self.assertNotEqual( t, union_type('option', 8, ( (int_type('int', 4, True), 'x'), (int_type('unsigned int', 4, False), 'y'), ))) # One is anonymous. self.assertNotEqual( t, union_type(None, 4, ( (int_type('int', 4, True), 'x'), (int_type('unsigned int', 4, False), 'y'), ))) # Different members. self.assertNotEqual( t, union_type('option', 4, ( (int_type('long', 8, True), 'x'), (int_type('unsigned long', 8, False), 'y'), ))) # Different number of members. self.assertNotEqual( t, union_type('option', 4, ( (int_type('int', 4, True), 'x'), (int_type('unsigned int', 4, False), 'y'), (float_type('float', 4), 'z'), ))) # One member is anonymous. self.assertNotEqual( t, union_type('option', 4, ( (int_type('int', 4, True), 'x'), (int_type('unsigned int', 4, False), ), ))) # One is incomplete. self.assertNotEqual(t, union_type('option')) self.assertEqual( repr(t), "union_type(tag='option', size=4, members=((int_type(name='int', size=4, is_signed=True), 'x', 0, 0), (int_type(name='unsigned int', size=4, is_signed=False), 'y', 0, 0)))" ) t = union_type(None, 4, ( (int_type('int', 4, True), 'x'), (int_type('unsigned int', 4, False), 'y'), )) self.assertEqual(t.kind, TypeKind.UNION) self.assertIsNone(t.primitive) self.assertIsNone(t.tag) self.assertEqual(t.size, 4) self.assertEqual(t.members, ( (int_type('int', 4, True), 'x', 0, 0), (int_type('unsigned int', 4, False), 'y', 0, 0), )) self.assertTrue(t.is_complete()) t = union_type('color', 0, ()) self.assertEqual(t.kind, TypeKind.UNION) 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), "union_type(tag='color', size=0, members=())") t = union_type('color') self.assertEqual(t.kind, TypeKind.UNION) 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), "union_type(tag='color', size=None, members=None)") t = union_type(None, None, None) self.assertEqual(t.kind, TypeKind.UNION) 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), "union_type(tag=None, size=None, members=None)") self.assertRaises(TypeError, union_type, 4) self.assertRaisesRegex(ValueError, 'must not have size', union_type, 'option', 8, None) self.assertRaisesRegex(ValueError, 'must have size', union_type, 'option', None, ()) self.assertRaisesRegex(TypeError, 'must be sequence or None', union_type, 'option', 8, 4) self.assertRaisesRegex(TypeError, 'must be.*sequence', union_type, 'option', 8, (4, )) self.assertRaisesRegex(ValueError, 'must be.*sequence', union_type, 'option', 8, ((), )) self.assertRaisesRegex(TypeError, 'must be string or None', union_type, 'option', 8, ((int_type('int', 4, True), 4), )) self.assertRaisesRegex(TypeError, 'must be integer', union_type, 'option', 8, ((int_type('int', 4, True), 'x', None), )) self.assertRaisesRegex(TypeError, 'must be Type', union_type, 'option', 8, ((None, 'x'), )) # Bit size. t = union_type('option', 4, ( (int_type('int', 4, True), 'x', 0, 4), (int_type('unsigned int', 4, False), 'y', 0, 4), )) self.assertEqual(t.members, ( (int_type('int', 4, True), 'x', 0, 4), (int_type('unsigned int', 4, False), 'y', 0, 4), ))
) 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))) option_type = union_type( "option", 4, ( TypeMember(int_type("int", 4, True), "i"), TypeMember(float_type("float", 4), "f"), ), ) color_type = enum_type( "color", int_type("unsigned int", 4, False), (TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1), TypeEnumerator("BLUE", 2)), ) pid_type = typedef_type("pid_t", int_type("int", 4, True)) MOCK_32BIT_PLATFORM = Platform(Architecture.UNKNOWN, PlatformFlags.IS_LITTLE_ENDIAN) MOCK_PLATFORM = Platform( Architecture.UNKNOWN,
int_type, struct_type, typedef_type, union_type, ) 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), (('RED', 0), ('GREEN', 1), ('BLUE', 2))) pid_type = typedef_type('pid_t', int_type('int', 4, True)) MOCK_32BIT_PLATFORM = Platform(Architecture.UNKNOWN, PlatformFlags.IS_LITTLE_ENDIAN) MOCK_PLATFORM = Platform( Architecture.UNKNOWN, PlatformFlags.IS_64_BIT | PlatformFlags.IS_LITTLE_ENDIAN) class MockMemorySegment(NamedTuple): buf: bytes virt_addr: Optional[int] = None
), ) 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), (("RED", 0), ("GREEN", 1), ("BLUE", 2))) pid_type = typedef_type("pid_t", int_type("int", 4, True)) MOCK_32BIT_PLATFORM = Platform(Architecture.UNKNOWN, PlatformFlags.IS_LITTLE_ENDIAN) MOCK_PLATFORM = Platform( Architecture.UNKNOWN, PlatformFlags.IS_64_BIT | PlatformFlags.IS_LITTLE_ENDIAN) class MockMemorySegment(NamedTuple): buf: bytes virt_addr: Optional[int] = None
def test_union(self): t = union_type( "option", 4, ( TypeMember(int_type("int", 4, True), "x"), TypeMember(int_type("unsigned int", 4, False), "y"), ), ) self.assertEqual(t.kind, TypeKind.UNION) self.assertIsNone(t.primitive) self.assertEqual(t.language, DEFAULT_LANGUAGE) self.assertEqual(t.tag, "option") self.assertEqual(t.size, 4) self.assertEqual( t.members, ( TypeMember(int_type("int", 4, True), "x", 0, 0), TypeMember(int_type("unsigned int", 4, False), "y", 0, 0), ), ) self.assertTrue(t.is_complete()) self.assertEqual( t, union_type( "option", 4, ( TypeMember(int_type("int", 4, True), "x"), TypeMember(int_type("unsigned int", 4, False), "y"), ), ), ) # Different tag. self.assertNotEqual( t, union_type( "pt", 4, ( TypeMember(int_type("int", 4, True), "x"), TypeMember(int_type("unsigned int", 4, False), "y"), ), ), ) # Different size. self.assertNotEqual( t, union_type( "option", 8, ( TypeMember(int_type("int", 4, True), "x"), TypeMember(int_type("unsigned int", 4, False), "y"), ), ), ) # One is anonymous. self.assertNotEqual( t, union_type( None, 4, ( TypeMember(int_type("int", 4, True), "x"), TypeMember(int_type("unsigned int", 4, False), "y"), ), ), ) # Different members. self.assertNotEqual( t, union_type( "option", 4, ( TypeMember(int_type("long", 8, True), "x"), TypeMember(int_type("unsigned long", 8, False), "y"), ), ), ) # Different number of members. self.assertNotEqual( t, union_type( "option", 4, ( TypeMember(int_type("int", 4, True), "x"), TypeMember(int_type("unsigned int", 4, False), "y"), TypeMember(float_type("float", 4), "z"), ), ), ) # One member is anonymous. self.assertNotEqual( t, union_type( "option", 4, ( TypeMember(int_type("int", 4, True), "x"), TypeMember(int_type("unsigned int", 4, False), ), ), ), ) # One is incomplete. self.assertNotEqual(t, union_type("option")) self.assertEqual( repr(t), "union_type(tag='option', size=4, members=(TypeMember(type=int_type(name='int', size=4, is_signed=True), name='x', bit_offset=0), TypeMember(type=int_type(name='unsigned int', size=4, is_signed=False), name='y', bit_offset=0)))", ) self.assertEqual(sizeof(t), 4) t = union_type( None, 4, ( TypeMember(int_type("int", 4, True), "x"), TypeMember(int_type("unsigned int", 4, False), "y"), ), ) self.assertEqual(t.kind, TypeKind.UNION) self.assertIsNone(t.primitive) self.assertIsNone(t.tag) self.assertEqual(t.size, 4) self.assertEqual( t.members, ( TypeMember(int_type("int", 4, True), "x", 0, 0), TypeMember(int_type("unsigned int", 4, False), "y", 0, 0), ), ) self.assertTrue(t.is_complete()) t = union_type("color", 0, ()) self.assertEqual(t.kind, TypeKind.UNION) 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), "union_type(tag='color', size=0, members=())") t = union_type("color") self.assertEqual(t.kind, TypeKind.UNION) 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), "union_type(tag='color', size=None, members=None)") t = union_type(None, None, None) self.assertEqual(t.kind, TypeKind.UNION) 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), "union_type(tag=None, size=None, members=None)") self.assertRaises(TypeError, union_type, 4) self.assertRaisesRegex(ValueError, "must not have size", union_type, "option", 8, None) self.assertRaisesRegex(ValueError, "must have size", union_type, "option", None, ()) self.assertRaisesRegex(TypeError, "must be sequence or None", union_type, "option", 8, 4) self.assertRaisesRegex(TypeError, "must be TypeMember", union_type, "option", 8, (4, )) # Bit size. t = union_type( "option", 4, ( TypeMember(int_type("int", 4, True), "x", 0, 4), TypeMember(int_type("unsigned int", 4, False), "y", 0, 4), ), ) self.assertEqual( t.members, ( TypeMember(int_type("int", 4, True), "x", 0, 4), TypeMember(int_type("unsigned int", 4, False), "y", 0, 4), ), )