def test_enum(self): t = enum_type( "color", int_type("unsigned int", 4, False), ( TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1), TypeEnumerator("BLUE", 2), ), ) self.assertPrettyPrint( t, """\ enum color { RED = 0, GREEN = 1, BLUE = 2, }""", ) t = enum_type( "color", int_type("unsigned int", 4, False), ( TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1), TypeEnumerator("BLUE", 2), ), Qualifiers.CONST, ) self.assertPrettyPrint( t, """\ const enum color { RED = 0, GREEN = 1, BLUE = 2, }""", ) t = enum_type( None, int_type("int", 4, True), ( TypeEnumerator("RED", 0), TypeEnumerator("GREEN", -1), TypeEnumerator("BLUE", -2), ), ) self.assertPrettyPrint( t, """\ enum { RED = 0, GREEN = -1, BLUE = -2, }""", )
def test_enumerator(self): sym = Symbol(color_type, is_enumerator=True) self.assertEqual(sym.type, color_type) self.assertIsNone(sym.value) self.assertIsNone(sym.value) self.assertTrue(sym.is_enumerator) self.assertIsNone(sym.byteorder) self.assertEqual(sym, Symbol(color_type, is_enumerator=True)) self.assertNotEqual(sym, Symbol(enum_type('color2'), is_enumerator=True)) self.assertNotEqual(sym, Symbol(color_type, value=1))
def test_enum(self): t = enum_type('color', int_type('unsigned int', 4, False), ( ('RED', 0), ('GREEN', 1), ('BLUE', 2), )) self.assertPrettyPrint( t, """\ enum color { RED = 0, GREEN = 1, BLUE = 2, }""") t = enum_type('color', int_type('unsigned int', 4, False), ( ('RED', 0), ('GREEN', 1), ('BLUE', 2), ), Qualifiers.CONST) self.assertPrettyPrint( t, """\ const enum color { RED = 0, GREEN = 1, BLUE = 2, }""") t = enum_type(None, int_type('int', 4, True), ( ('RED', 0), ('GREEN', -1), ('BLUE', -2), )) self.assertPrettyPrint( t, """\ enum { RED = 0, GREEN = -1, BLUE = -2, }""")
def test_enum(self): t = enum_type('color', int_type('unsigned int', 4, False), (('RED', 0), ('GREEN', 1), ('BLUE', 2))) self.assertEqual(t.kind, TypeKind.ENUM) self.assertIsNone(t.primitive) self.assertEqual(t.tag, 'color') self.assertEqual(t.type, int_type('unsigned int', 4, False)) self.assertEqual(t.enumerators, (('RED', 0), ('GREEN', 1), ('BLUE', 2))) self.assertTrue(t.is_complete()) self.assertEqual( t, enum_type('color', int_type('unsigned int', 4, False), (('RED', 0), ('GREEN', 1), ('BLUE', 2)))) # Different tag. self.assertNotEqual( t, enum_type('COLOR', int_type('unsigned int', 4, False), (('RED', 0), ('GREEN', 1), ('BLUE', 2)))) # One is anonymous. self.assertNotEqual( t, enum_type(None, int_type('unsigned int', 4, False), (('RED', 0), ('GREEN', 1), ('BLUE', 2)))) # Different compatible type. self.assertNotEqual( t, enum_type('color', int_type('int', 4, True), (('RED', 0), ('GREEN', 1), ('BLUE', 2)))) # Different enumerators. self.assertNotEqual( t, enum_type('color', int_type('unsigned int', 4, False), (('RED', 0), ('YELLOW', 1), ('BLUE', 2)))) # Different number of enumerators. self.assertNotEqual( t, enum_type('color', int_type('unsigned int', 4, False), (('RED', 0), ('GREEN', 1)))) # One is incomplete. self.assertNotEqual(t, enum_type('color')) self.assertEqual( repr(t), "enum_type(tag='color', type=int_type(name='unsigned int', size=4, is_signed=False), enumerators=(('RED', 0), ('GREEN', 1), ('BLUE', 2)))" ) t = enum_type('color', None, None) self.assertEqual(t.kind, TypeKind.ENUM) self.assertIsNone(t.primitive) self.assertEqual(t.tag, 'color') self.assertIsNone(t.type) self.assertIsNone(t.enumerators) self.assertFalse(t.is_complete()) self.assertEqual( repr(t), "enum_type(tag='color', type=None, enumerators=None)") # A type with no enumerators isn't valid in C, but we allow it. t = enum_type('color', int_type('unsigned int', 4, False), ()) self.assertEqual(t.kind, TypeKind.ENUM) self.assertIsNone(t.primitive) self.assertEqual(t.tag, 'color') self.assertEqual(t.type, int_type('unsigned int', 4, False)) self.assertEqual(t.enumerators, ()) self.assertTrue(t.is_complete()) self.assertEqual( repr(t), "enum_type(tag='color', type=int_type(name='unsigned int', size=4, is_signed=False), enumerators=())" ) self.assertRaisesRegex(TypeError, 'must be Type', enum_type, 'color', 4, ()) self.assertRaisesRegex(ValueError, 'must be integer type', enum_type, 'color', void_type(), ()) self.assertRaisesRegex( ValueError, 'must be unqualified', enum_type, 'color', int_type('unsigned int', 4, True, Qualifiers.CONST), ()) self.assertRaisesRegex(ValueError, 'must not have compatible type', enum_type, 'color', int_type('unsigned int', 4, False), None) self.assertRaisesRegex(ValueError, 'must have compatible type', enum_type, 'color', None, ()) self.assertRaisesRegex(TypeError, 'must be sequence or None', enum_type, 'color', int_type('unsigned int', 4, False), 4) self.assertRaisesRegex(TypeError, 'must be.*sequence', enum_type, 'color', int_type('unsigned int', 4, False), (4, )) self.assertRaisesRegex(ValueError, 'must be.*sequence', enum_type, 'color', int_type('unsigned int', 4, False), ((), )) self.assertRaisesRegex(TypeError, 'must be string', enum_type, 'color', int_type('unsigned int', 4, False), ((None, 0), )) self.assertRaisesRegex(TypeError, 'must be integer', enum_type, 'color', int_type('unsigned int', 4, False), (('RED', None), ))
), ) 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, PlatformFlags.IS_64_BIT | PlatformFlags.IS_LITTLE_ENDIAN) class MockMemorySegment(NamedTuple): buf: bytes virt_addr: Optional[int] = None phys_addr: Optional[int] = None
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 phys_addr: Optional[int] = None
"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 phys_addr: Optional[int] = None
def test_enum(self): t = enum_type( "color", int_type("unsigned int", 4, False), ( TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1), TypeEnumerator("BLUE", 2), ), ) self.assertEqual(t.kind, TypeKind.ENUM) self.assertIsNone(t.primitive) self.assertEqual(t.language, DEFAULT_LANGUAGE) self.assertEqual(t.tag, "color") self.assertEqual(t.type, int_type("unsigned int", 4, False)) self.assertEqual( t.enumerators, ( TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1), TypeEnumerator("BLUE", 2), ), ) self.assertTrue(t.is_complete()) self.assertEqual( t, enum_type( "color", int_type("unsigned int", 4, False), ( TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1), TypeEnumerator("BLUE", 2), ), ), ) # Different tag. self.assertNotEqual( t, enum_type( "COLOR", int_type("unsigned int", 4, False), ( TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1), TypeEnumerator("BLUE", 2), ), ), ) # One is anonymous. self.assertNotEqual( t, enum_type( None, int_type("unsigned int", 4, False), ( TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1), TypeEnumerator("BLUE", 2), ), ), ) # Different compatible type. self.assertNotEqual( t, enum_type( "color", int_type("int", 4, True), ( TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1), TypeEnumerator("BLUE", 2), ), ), ) # Different enumerators. self.assertNotEqual( t, enum_type( "color", int_type("unsigned int", 4, False), ( TypeEnumerator("RED", 0), TypeEnumerator("YELLOW", 1), TypeEnumerator("BLUE", 2), ), ), ) # Different number of enumerators. self.assertNotEqual( t, enum_type( "color", int_type("unsigned int", 4, False), (TypeEnumerator("RED", 0), TypeEnumerator("GREEN", 1)), ), ) # One is incomplete. self.assertNotEqual(t, enum_type("color")) self.assertEqual( repr(t), "enum_type(tag='color', type=int_type(name='unsigned int', size=4, is_signed=False), enumerators=(TypeEnumerator('RED', 0), TypeEnumerator('GREEN', 1), TypeEnumerator('BLUE', 2)))", ) self.assertEqual(sizeof(t), 4) t = enum_type("color", None, None) self.assertEqual(t.kind, TypeKind.ENUM) self.assertIsNone(t.primitive) self.assertEqual(t.tag, "color") self.assertIsNone(t.type) self.assertIsNone(t.enumerators) self.assertFalse(t.is_complete()) self.assertEqual( repr(t), "enum_type(tag='color', type=None, enumerators=None)") # A type with no enumerators isn't valid in C, but we allow it. t = enum_type("color", int_type("unsigned int", 4, False), ()) self.assertEqual(t.kind, TypeKind.ENUM) self.assertIsNone(t.primitive) self.assertEqual(t.tag, "color") self.assertEqual(t.type, int_type("unsigned int", 4, False)) self.assertEqual(t.enumerators, ()) self.assertTrue(t.is_complete()) self.assertEqual( repr(t), "enum_type(tag='color', type=int_type(name='unsigned int', size=4, is_signed=False), enumerators=())", ) self.assertRaisesRegex(TypeError, "must be Type", enum_type, "color", 4, ()) self.assertRaisesRegex(ValueError, "must be integer type", enum_type, "color", void_type(), ()) self.assertRaisesRegex( ValueError, "must be unqualified", enum_type, "color", int_type("unsigned int", 4, True, Qualifiers.CONST), (), ) self.assertRaisesRegex( ValueError, "must not have compatible type", enum_type, "color", int_type("unsigned int", 4, False), None, ) self.assertRaisesRegex(ValueError, "must have compatible type", enum_type, "color", None, ()) self.assertRaisesRegex( TypeError, "must be sequence or None", enum_type, "color", int_type("unsigned int", 4, False), 4, ) self.assertRaisesRegex( TypeError, "must be TypeEnumerator", enum_type, "color", int_type("unsigned int", 4, False), (4, ), )