def test_complete_struct(): BLong = new_primitive_type("long") BChar = new_primitive_type("char") BShort = new_primitive_type("short") BStruct = new_struct_type("foo") assert _getfields(BStruct) is None complete_struct_or_union(BStruct, [('a1', BLong, -1), ('a2', BChar, -1), ('a3', BShort, -1)]) d = _getfields(BStruct) assert len(d) == 3 assert d[0][0] == 'a1' assert d[0][1].type is BLong assert d[0][1].offset == 0 assert d[0][1].bitshift == -1 assert d[0][1].bitsize == -1 assert d[1][0] == 'a2' assert d[1][1].type is BChar assert d[1][1].offset == sizeof_type(BLong) assert d[1][1].bitshift == -1 assert d[1][1].bitsize == -1 assert d[2][0] == 'a3' assert d[2][1].type is BShort assert d[2][1].offset == sizeof_type(BLong) + sizeof_type(BShort) assert d[2][1].bitshift == -1 assert d[2][1].bitsize == -1 assert sizeof_type(BStruct) == 2 * sizeof_type(BLong) assert alignof(BStruct) == alignof(BLong)
def test_enum_type(): BEnum = new_enum_type("foo", (), ()) assert repr(BEnum) == "<ctype 'enum foo'>" assert _getfields(BEnum) == [] # BEnum = new_enum_type("foo", ('def', 'c', 'ab'), (0, 1, -20)) assert _getfields(BEnum) == [(-20, 'ab'), (0, 'def'), (1, 'c')]
def test_enum_type(): BEnum = new_enum_type("foo", (), ()) assert repr(BEnum) == "<ctype 'enum foo'>" assert _getfields(BEnum) == [] # BEnum = new_enum_type("foo", ('def', 'c', 'ab'), (0, 1, -20)) assert _getfields(BEnum) == [(-20, 'ab'), (0, 'def'), (1, 'c')]
def test_complete_struct(): BLong = new_primitive_type("long") BChar = new_primitive_type("char") BShort = new_primitive_type("short") BStruct = new_struct_type("foo") assert _getfields(BStruct) is None complete_struct_or_union(BStruct, [('a1', BLong, -1), ('a2', BChar, -1), ('a3', BShort, -1)]) d = _getfields(BStruct) assert len(d) == 3 assert d[0][0] == 'a1' assert d[0][1].type is BLong assert d[0][1].offset == 0 assert d[0][1].bitshift == -1 assert d[0][1].bitsize == -1 assert d[1][0] == 'a2' assert d[1][1].type is BChar assert d[1][1].offset == sizeof_type(BLong) assert d[1][1].bitshift == -1 assert d[1][1].bitsize == -1 assert d[2][0] == 'a3' assert d[2][1].type is BShort assert d[2][1].offset == sizeof_type(BLong) + sizeof_type(BShort) assert d[2][1].bitshift == -1 assert d[2][1].bitsize == -1 assert sizeof_type(BStruct) == 2 * sizeof_type(BLong) assert alignof(BStruct) == alignof(BLong)
def test_complete_union(): BLong = new_primitive_type("long") BChar = new_primitive_type("char") BUnion = new_union_type("foo") assert _getfields(BUnion) is None complete_struct_or_union(BUnion, [('a1', BLong, -1), ('a2', BChar, -1)]) d = _getfields(BUnion) assert len(d) == 2 assert d[0][0] == 'a1' assert d[0][1].type is BLong assert d[0][1].offset == 0 assert d[1][0] == 'a2' assert d[1][1].type is BChar assert d[1][1].offset == 0 assert sizeof_type(BUnion) == sizeof_type(BLong) assert alignof(BUnion) == alignof(BLong)
def test_complete_union(): BLong = new_primitive_type("long") BChar = new_primitive_type("char") BUnion = new_union_type("foo") assert _getfields(BUnion) is None complete_struct_or_union(BUnion, [('a1', BLong, -1), ('a2', BChar, -1)]) d = _getfields(BUnion) assert len(d) == 2 assert d[0][0] == 'a1' assert d[0][1].type is BLong assert d[0][1].offset == 0 assert d[1][0] == 'a2' assert d[1][1].type is BChar assert d[1][1].offset == 0 assert sizeof_type(BUnion) == sizeof_type(BLong) assert alignof(BUnion) == alignof(BLong)
def test_struct_with_bitfields(): BLong = new_primitive_type("long") BStruct = new_struct_type("foo") LONGBITS = 8 * sizeof_type(BLong) complete_struct_or_union(BStruct, [('a1', BLong, 1), ('a2', BLong, 2), ('a3', BLong, 3), ('a4', BLong, LONGBITS - 5)]) d = _getfields(BStruct) assert d[0][1].offset == d[1][1].offset == d[2][1].offset == 0 assert d[3][1].offset == sizeof_type(BLong) assert d[0][1].bitshift == 0 assert d[0][1].bitsize == 1 assert d[1][1].bitshift == 1 assert d[1][1].bitsize == 2 assert d[2][1].bitshift == 3 assert d[2][1].bitsize == 3 assert d[3][1].bitshift == 0 assert d[3][1].bitsize == LONGBITS - 5 assert sizeof_type(BStruct) == 2 * sizeof_type(BLong) assert alignof(BStruct) == alignof(BLong)
def test_struct_with_bitfields(): BLong = new_primitive_type("long") BStruct = new_struct_type("foo") LONGBITS = 8 * sizeof_type(BLong) complete_struct_or_union(BStruct, [('a1', BLong, 1), ('a2', BLong, 2), ('a3', BLong, 3), ('a4', BLong, LONGBITS - 5)]) d = _getfields(BStruct) assert d[0][1].offset == d[1][1].offset == d[2][1].offset == 0 assert d[3][1].offset == sizeof_type(BLong) assert d[0][1].bitshift == 0 assert d[0][1].bitsize == 1 assert d[1][1].bitshift == 1 assert d[1][1].bitsize == 2 assert d[2][1].bitshift == 3 assert d[2][1].bitsize == 3 assert d[3][1].bitshift == 0 assert d[3][1].bitsize == LONGBITS - 5 assert sizeof_type(BStruct) == 2 * sizeof_type(BLong) assert alignof(BStruct) == alignof(BLong)