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 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()
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 Atedgi(h.Struct): this = h.UInt8(1) aviv = h.FieldPlaceholder() def __init__(self, this=1, *args, **kwargs): self.this = this self.set_vec_field() super().__init__(*args, **kwargs) @h.from_bytes_hook(aviv) def set_vec_field(self): d = {len(x()): x for x in (h.UInt8, h.UInt16, h.UInt32, h.UInt64)} self.aviv = d[self.this.value]()
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)]()
class Isaac(h.Struct): arr = h.Array(field_type=h.UInt8(3), length=3, value=(1, 2), fill=True) x = h.UInt8(5)
class NadavLoYazam(h.Struct): nadav = h.UInt8() lo = h.UInt16() yazam = h.UInt32() bihlal = h.UInt64()
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 MyStructBody(h.Struct): b = h.UInt8(2) c = h.UInt8(3)
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
class MyStructFooter(h.Struct, footer=True): d = h.UInt8(4)
class FooterBody1(h.Struct): x = h.UInt8(0xFF)
class Card(h.Struct): value = h.UInt8() suit = h.UInt8()
class BadStruct(h.Struct): a = h.UInt8(validator=2)
class Becca(h.Struct): x = h.UInt8()
def test_struct_meta_len(): # noinspection PyTypeChecker assert len(Omri()) == len(Omri) def test_field_copy(): x = Omri() x.a = 3 y = Omri() assert x.a == 3 assert y.a == 256 @pytest.mark.parametrize('field', (h.UInt8, h.UInt8(), 3)) def test_redefining_fields(field): with pytest.raises(NameError): # noinspection PyUnusedLocal class Check(Omri): a = field def test_bytes_length(): assert len(bytes(Omri())) == 11 def test_non_default_args(): class Check(h.Struct): x = h.UInt8()
class Good(h.Struct): i32 = h.Int32(5, validator=lambda z: z > 4) i32_range = h.Int32(14, validator=range(0, 30, 2)) i32_set = h.Int32(2, validator={1, 2, 3}) i32_arr = h.Array(length=5, field_type=h.UInt8(5), validator=5)
class Tst(h.Struct): a = h.UInt8(validator=0)
class Shustin2(h.Struct): arr = h.Array(3, h.UInt8(8), validator=lambda x: x > 9, fill=True)
class Check(h.Struct): x = h.UInt8() def __init__(self, a): super().__init__(a) self.x = a
class Tomer(h.Struct): b = h.UInt8(5) c = h.Double(3)
class Omri(h.Struct): a = h.UInt16(256) b = h.UInt8(5) c = h.Double(3)
class Log(h.Struct): data = h.UInt8(2) time = Time()
class MyStructHeader(h.Struct): a = h.UInt8(1)