def test_enum_endianness(): assert Tal.u.type.endianness_format == h.Endianness.Default.value assert Yuval.u.type.endianness_format == h.Endianness.BigEndian.value assert bytes(h.Enum(h.UInt16, Lia, Lia.A)) == bytes(h.UInt16(1)) assert bytes(h.Enum(h.UInt16(endianness=h.BigEndian), Lia, Lia.A)) == bytes(h.UInt16(1, endianness=h.BigEndian)) assert Yuval().u.type.endianness_format == h.BigEndian.value assert bytes(Yuval(u=1))[-2:] == bytes(h.UInt16(1, endianness=h.BigEndian))
class Garzon(h.Struct): arr = h.Array(field_type=h.UInt8(3), length=3, value=(1, 2), fill=True) nested_vla_len = h.UInt16() nested_vla = h.Vector(field_type=h.Int32(), length=nested_vla_len, value=(9, 10, 100)) x = h.UInt8(5)
class Shine(h.Struct): arr = h.Array(field_type=h.UInt8(3), length=3, value=(1, 2), fill=True) vec_len = h.UInt8() vec = h.Vector(field_type=h.UInt8(), length=vec_len) y = h.UInt64(9) z_len = h.UInt8() z = h.Vector(field_type=Garzon(), length=z_len, value=[ Garzon(nested_vla=(11, 12, 13, 14, 15)), Garzon(arr=(9,)), Garzon()]) vec2_len = h.UInt8() vec2 = h.Vector(field_type=h.UInt32(), length=vec2_len, value=(53, 99, 123)) x = h.UInt16(104)
class Gilad(h.Struct): i8 = h.Int8(0) i16 = h.Int16(0) i32 = h.Int32(0) i64 = h.Int64(0) u8 = h.UInt8(0) u16 = h.UInt16(0) u32 = h.UInt32(0) u64 = h.UInt64(0) flt = h.Float(0) dub = h.Double(0)
class IDVector(h.Struct): vec_len = h.UInt16() id_len = h.UInt8() vec = h.Vector(vec_len, h.FieldPlaceholder) # Vector of IDs def __init__(self, id_type: Type[Field] = h.FieldPlaceholder, *args, **kwargs): self.vec.type = id_type() self.id_len = len(self.vec.type) super().__init__(*args, **kwargs) @h.from_bytes_hook(vec) def set_vec_field(self): d = {len(x()): x for x in (h.UInt8, h.UInt16, h.UInt32, h.UInt64)} self.vec.type = d[int(self.id_len)]()
def test_from_bytes_hooks(): class Ronen(h.Struct): x = h.UInt8(2) arr = h.Array(10, h.FieldPlaceholder, value=list(range(10))) @h.Struct.from_bytes_hook(arr) def foo(self): self.arr.type = h.UInt16() r = Ronen() r.arr.type = h.UInt16() r2 = Ronen.from_bytes(bytes(r)) assert r2.arr == list(range(10))
class Maor(h.Struct): vec_len = h.UInt16() vec = h.Vector(vec_len, Atedgi)
class Raif(h.Struct): a = h.Array(5, h.UInt16(1))
class Raif(h.Struct): a = h.Array(5, h.UInt16(), fill=True)
class NadavLoYazam(h.Struct): nadav = h.UInt8() lo = h.UInt16() yazam = h.UInt32() bihlal = h.UInt64()
class Shustin(h.Struct): length = h.UInt16() data = h.Vector(length=length, field_type=h.UInt8())
def test_add(): assert h.UInt8(2) + 3 == 5 assert 2 + h.UInt8(3) == 5 assert h.UInt8(2) + h.UInt16(3) == 5
def foo(self): self.arr.type = h.UInt16()
class Dror(h.Struct): a = h.UInt16 b = h.UInt16()
def test_comparisons(): assert h.UInt16(3) > h.UInt16(2) assert h.UInt16(3) == h.UInt16(3) assert h.UInt16(4) == h.UInt8(4)
class MessageBody1(h.Struct): x = h.UInt16(0x1D14)
class MissingOpcode(h.Struct): x = h.UInt16()
class Omri(h.Struct): a = h.UInt16(256) b = h.UInt8(5) c = h.Double(3)