Esempio n. 1
0
    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,
}""",
        )
Esempio n. 2
0
    def test_init(self):
        e = TypeEnumerator("a", 1)
        self.assertEqual(e.name, "a")
        self.assertEqual(e.value, 1)

        self.assertRaises(TypeError, TypeEnumerator, "a", None)
        self.assertRaises(TypeError, TypeEnumerator, None, 1)
Esempio n. 3
0
 def setUp(self):
     super().setUp()
     self.types = []
     self.objects = []
     self.prog = mock_program(types=self.types, objects=self.objects)
     self.coord_type = self.prog.class_type(
         "coord",
         12,
         (
             TypeMember(self.prog.int_type("int", 4, True), "x", 0),
             TypeMember(self.prog.int_type("int", 4, True), "y", 32),
             TypeMember(self.prog.int_type("int", 4, True), "z", 64),
         ),
     )
     self.point_type = self.prog.struct_type(
         "point",
         8,
         (
             TypeMember(self.prog.int_type("int", 4, True), "x", 0),
             TypeMember(self.prog.int_type("int", 4, True), "y", 32),
         ),
     )
     self.line_segment_type = self.prog.struct_type(
         "line_segment",
         16,
         (TypeMember(self.point_type,
                     "a"), TypeMember(self.point_type, "b", 64)),
     )
     self.option_type = self.prog.union_type(
         "option",
         4,
         (
             TypeMember(self.prog.int_type("int", 4, True), "i"),
             TypeMember(self.prog.float_type("float", 4), "f"),
         ),
     )
     self.color_type = self.prog.enum_type(
         "color",
         self.prog.int_type("unsigned int", 4, False),
         (
             TypeEnumerator("RED", 0),
             TypeEnumerator("GREEN", 1),
             TypeEnumerator("BLUE", 2),
         ),
     )
     self.pid_type = self.prog.typedef_type(
         "pid_t", self.prog.int_type("int", 4, True))
Esempio n. 4
0
    def test_enum_anonymous(self):
        self.assertPrettyPrint(
            self.prog.enum_type(
                None,
                self.prog.int_type("int", 4, True),
                (
                    TypeEnumerator("RED", 0),
                    TypeEnumerator("GREEN", -1),
                    TypeEnumerator("BLUE", -2),
                ),
            ),
            """\
enum {
	RED = 0,
	GREEN = -1,
	BLUE = -2,
}""",
        )
Esempio n. 5
0
    def test_decode_enum_type_flags(self):
        prog = Program(MOCK_PLATFORM)

        for bit_numbers, flags in (
            (True, self.FLAGS_BIT_NUMBERS),
            (False, self.FLAGS_VALUES),
        ):
            with self.subTest(bit_numbers=bit_numbers):
                type = prog.enum_type(
                    None,
                    prog.int_type("int", 4, True),
                    [TypeEnumerator(*flag) for flag in flags],
                )
                self.assertEqual(
                    decode_enum_type_flags(4, type, bit_numbers), "UNDERLINE"
                )
                self.assertEqual(
                    decode_enum_type_flags(27, type, bit_numbers), "BOLD|ITALIC|0x18"
                )
Esempio n. 6
0
    def test_enum(self):
        t = self.prog.enum_type(
            "color",
            self.prog.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.assertIdentical(t.type,
                             self.prog.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(
            repr(t),
            "prog.enum_type(tag='color', type=prog.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 = self.prog.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),
            "prog.enum_type(tag='color', type=None, enumerators=None)")

        # A type with no enumerators isn't valid in C, but we allow it.
        t = self.prog.enum_type("color",
                                self.prog.int_type("unsigned int", 4, False),
                                ())
        self.assertEqual(t.kind, TypeKind.ENUM)
        self.assertIsNone(t.primitive)
        self.assertEqual(t.tag, "color")
        self.assertIdentical(t.type,
                             self.prog.int_type("unsigned int", 4, False))
        self.assertEqual(t.enumerators, ())
        self.assertTrue(t.is_complete())

        self.assertEqual(
            repr(t),
            "prog.enum_type(tag='color', type=prog.int_type(name='unsigned int', size=4, is_signed=False), enumerators=())",
        )

        self.assertRaisesRegex(TypeError, "must be Type", self.prog.enum_type,
                               "color", 4, ())
        self.assertRaisesRegex(
            ValueError,
            "must be integer type",
            self.prog.enum_type,
            "color",
            self.prog.void_type(),
            (),
        )
        self.assertRaisesRegex(
            ValueError,
            "must be unqualified",
            self.prog.enum_type,
            "color",
            self.prog.int_type("unsigned int",
                               4,
                               True,
                               qualifiers=Qualifiers.CONST),
            (),
        )
        self.assertRaisesRegex(
            ValueError,
            "must not have compatible type",
            self.prog.enum_type,
            "color",
            self.prog.int_type("unsigned int", 4, False),
            None,
        )
        self.assertRaisesRegex(
            ValueError,
            "must have compatible type",
            self.prog.enum_type,
            "color",
            None,
            (),
        )
        self.assertRaisesRegex(
            TypeError,
            "must be sequence or None",
            self.prog.enum_type,
            "color",
            self.prog.int_type("unsigned int", 4, False),
            4,
        )
        self.assertRaisesRegex(
            TypeError,
            "must be TypeEnumerator",
            self.prog.enum_type,
            "color",
            self.prog.int_type("unsigned int", 4, False),
            (4, ),
        )
Esempio n. 7
0
 def test_cmp(self):
     self.assertEqual(TypeEnumerator("a", 1),
                      TypeEnumerator(name="a", value=1))
     self.assertNotEqual(TypeEnumerator("a", 1), TypeEnumerator("a", 2))
     self.assertNotEqual(TypeEnumerator("b", 1), TypeEnumerator("a", 1))
Esempio n. 8
0
 def test_sequence(self):
     e = TypeEnumerator("a", 1)
     name, value = e
     self.assertEqual(name, "a")
     self.assertEqual(value, 1)
     self.assertEqual(list(e), ["a", 1])
Esempio n. 9
0
 def test_repr(self):
     e = TypeEnumerator("a", 1)
     self.assertEqual(repr(e), "TypeEnumerator('a', 1)")
Esempio n. 10
0
)
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
Esempio n. 11
0
    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, ),
        )