def test_greedy_array_exceptions(): with pytest.raises(Exception) as e: class NotLast(prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("y", prophy.array(prophy.u32)), ("x", prophy.u32)] assert "unlimited field is not the last one" == str(e.value) with pytest.raises(Exception) as e: class GreedyComposite(prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("x", prophy.array(prophy.u32))] prophy.array(GreedyComposite) assert "array with unlimited field disallowed" == str(e.value)
def test_optional_array(): with pytest.raises(Exception) as e: prophy.optional(prophy.array(prophy.u8, size=3)) assert "optional array not implemented" == str(e.value) with pytest.raises(Exception) as e: class S(prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a_len", prophy.optional(prophy.u32)), ("a", prophy.array(prophy.u32, bound="a_len"))] assert "array S.a must not be bound to optional field" == str(e.value) with pytest.raises(Exception) as e: prophy.array(prophy.optional(prophy.u32)) assert "array of optional type not allowed" == str(e.value)
def test_array_static_attributes(): A = prophy.array(prophy.u16, size=3) assert A._SIZE == 6 assert A._DYNAMIC is False assert A._UNLIMITED is False assert A._OPTIONAL is False assert A._ALIGNMENT == 2 assert A._BOUND is None assert A._PARTIAL_ALIGNMENT is None
def test_array_bound_attributes(): A = prophy.array(prophy.u16, bound="a_len") assert A._SIZE == 0 assert A._DYNAMIC is True assert A._UNLIMITED is False assert A._OPTIONAL is False assert A._ALIGNMENT == 2 assert A._BOUND == "a_len" assert A._PARTIAL_ALIGNMENT is None
def test_array_greedy_attributes(): A = prophy.array(prophy.u16) assert A._SIZE == 0 assert A._DYNAMIC is True assert A._UNLIMITED is True assert A._OPTIONAL is False assert A._ALIGNMENT == 2 assert A._BOUND is None assert A._PARTIAL_ALIGNMENT is None
def test_array_static_attributes(): A = prophy.array(prophy.u16, size = 3) assert 6 == A._SIZE assert False == A._DYNAMIC assert False == A._UNLIMITED assert False == A._OPTIONAL assert 2 == A._ALIGNMENT assert None == A._BOUND assert A._PARTIAL_ALIGNMENT is None
def test_array_bound_attributes(): A = prophy.array(prophy.u16, bound = "a_len") assert 0 == A._SIZE assert True == A._DYNAMIC assert False == A._UNLIMITED assert False == A._OPTIONAL assert 2 == A._ALIGNMENT assert "a_len" == A._BOUND assert A._PARTIAL_ALIGNMENT is None
def test_array_greedy_attributes(): A = prophy.array(prophy.u16) assert 0 == A._SIZE assert True == A._DYNAMIC assert True == A._UNLIMITED assert False == A._OPTIONAL assert 2 == A._ALIGNMENT assert None == A._BOUND assert A._PARTIAL_ALIGNMENT is None
def test_fixed_scalar_array_exception(): class D(prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a_len", prophy.u8), ("a", prophy.array(prophy.u8, bound="a_len"))] class U(prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a", prophy.array(prophy.u8))] with pytest.raises(Exception, match="static/limited array of dynamic type not allowed"): prophy.array(D, size=2) with pytest.raises(Exception, match="static/limited array of dynamic type not allowed"): prophy.array(U, size=2) with pytest.raises(Exception, match="static/limited array of dynamic type not allowed"): prophy.array(D, bound="a_len", size=2) with pytest.raises(Exception, match="static/limited array of dynamic type not allowed"): prophy.array(U, bound="a_len", size=2)
class XS( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("len", prophy.u8), ("value", prophy.array(prophy.u8, bound="len", shift=2))]
class ComplicatedStruct( prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [("a", NestedStruct), ("b", prophy.u32), ("c", prophy.array(Struct, size=2)), ("d", prophy.array(Union, size=2))]
class X2( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("b_len", prophy.u8), ("a_len", prophy.u8), ("a", prophy.array(prophy.u8, bound="a_len")), ("b", prophy.array(prophy.u8, bound="b_len"))]
class GreedyScalarArray( prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [("x", prophy.array(prophy.u32))]
class Z( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("x", prophy.array(Y))]
class SArraySized( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a", prophy.array(S, size=3))]
def test_array_incorrect_attributes(): with pytest.raises(prophy.ProphyError) as err: prophy.array(prophy.u16, anything=3) assert str(err.value) == "unknown arguments to array field"
class Dynamic(prophy.struct): __metaclass__ = prophy.struct_generator _descriptor = [('a_len', prophy.u32), ('a', prophy.array(prophy.u8, bound='a_len'))]
class U(prophy.with_metaclass(prophy.union_generator, prophy.union)): _descriptor = [("a_len", prophy.u8, 0), ("a", prophy.array(prophy.u32, bound="a_len", size=3), 1)]
class B(prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [("num_of_x", prophy.u32), ("x", prophy.array(A, bound="num_of_x"))]
class C(prophy.struct_packed): __metaclass__ = prophy.struct_generator _descriptor = [('a_len', prophy.u32), ('a', prophy.array(B, bound='a_len'))]
class A(prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [("a_len", prophy.u16), ("a", prophy.array(prophy.u16, bound="a_len")), ("b", prophy.bytes())]
class A(prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [("a", prophy.array(prophy.u8, size=3)), ("b_len", prophy.u16), ("b", prophy.array(prophy.u32, bound="b_len"))]
class X(prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [("x_len", prophy.u8), ("x", prophy.array(prophy.u8, bound="x_len")), ("y", prophy.u32), ("z", prophy.u64)]
class BoundCompositeArray( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("len", prophy.u32), ("value", prophy.array(BoundScalarArray, bound="len"))]
class ComplexComposite( prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [("y_len", prophy.u32), ("x", Composite), ("y", prophy.array(Composite, bound="y_len"))]
class S2( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a", prophy.array(prophy.u32))]
class Array( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("value_len", prophy.u8), ("value", prophy.array(prophy.u8, size=1, shift=2))]
class StructWithU( prophy.with_metaclass(prophy.struct_generator, prophy.struct)): _descriptor = [("a_len", prophy.u8), ("a", prophy.array(UVarLen, bound="a_len"))]
class LengthFieldAfter( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a", prophy.array(prophy.i32, bound="after")), ("after", prophy.i32)]
def test_array_of_arrays_not_allowed(): A = prophy.array(prophy.r32) with pytest.raises(prophy.ProphyError) as err: prophy.array(A, size=3) assert str(err.value) == "array of arrays not allowed"
class LengthFieldNonexistent( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("a", prophy.array(prophy.i32, bound="nonexistent"))]
class GreedyComplexCompositeArray( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("x", prophy.array(ComplexComposite))]
class _( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("not_an_int", "not_an_int"), ("a", prophy.array(prophy.i32, bound="not_an_int"))]
class S(prophy.struct_packed): __metaclass__ = prophy.struct_generator _descriptor = [("a_len", prophy.optional(prophy.u32)), ("a", prophy.array(prophy.u32, bound="a_len"))]
class U(prophy.with_metaclass(prophy.union_generator, prophy.union)): _descriptor = [("a", prophy.array(prophy.u8, size=3), 0)]
class LengthFieldIsNotAnInteger( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("not_an_int", "not_an_int"), ("a", prophy.array(prophy.i32, bound="not_an_int"))]
class NotLast( prophy.with_metaclass(prophy.struct_generator, prophy.struct_packed)): _descriptor = [("y", prophy.array(prophy.u32)), ("x", prophy.u32)]