def test_fixed_size_array_no_sizer():
    class MyOneMarshal(Marshal):
        def write_to_stream(self, obj, stream, context=EMPTY_CONTEXT):
            stream.write("\x01")

        def create_from_stream(self, stream, context=EMPTY_CONTEXT, *args, **kwargs):
            return ord(stream.read(1))

    io = FixedSizeArrayMarshal(4, MyOneMarshal())
    assert io.write_to_string([1, 2, 3, 4]) == "\x01\x01\x01\x01"
    assert io.create_from_string("\x04\x03\x02\x01") == [4, 3, 2, 1]
Example #2
0
def test_fixed_size_array_no_sizer():
    class MyOneMarshal(Marshal):
        def write_to_stream(self, obj, stream, context=EMPTY_CONTEXT):
            stream.write(b"\x01")

        def create_from_stream(self,
                               stream,
                               context=EMPTY_CONTEXT,
                               *args,
                               **kwargs):
            return ord(stream.read(1))

    io = FixedSizeArrayMarshal(4, MyOneMarshal())
    assert io.write_to_string([1, 2, 3, 4]) == b"\x01\x01\x01\x01"
    assert io.create_from_string(b"\x04\x03\x02\x01") == [4, 3, 2, 1]
def test_fixed_size_array_with_sizer():
    io = FixedSizeArrayMarshal(4, UBInt8Marshal)
    assert io.sizeof([1, 2, 3, 4]) == 4
    assert io.min_max_sizeof() == MinMax(4, 4)
    assert io.is_fixed_size()
    assert io.write_to_string([ord("a"), ord("b"), ord("c"), ord("d")]) == "abcd"
    assert io.create_from_string("abcd") == [ord("a"), ord("b"), ord("c"), ord("d")]
Example #4
0
def test_fixed_size_array_with_sizer():
    io = FixedSizeArrayMarshal(4, UBInt8Marshal)
    assert io.sizeof([1, 2, 3, 4]) == 4
    assert io.min_max_sizeof() == MinMax(4, 4)
    assert io.is_fixed_size()
    assert io.write_to_string([ord('a'),
                               ord('b'),
                               ord('c'),
                               ord('d')]) == b"abcd"
    assert io.create_from_string(b"abcd") == [
        ord('a'), ord('b'), ord('c'), ord('d')
    ]