Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-compress

Source of release 0.9.1

0.9.1 / tests / facade.luc

from compress import encode, decode, crc32, Inflater, Deflater, finished, need_output, yielded, decode_brotli, decode_gzip, encode_gzip, decode_zstd, encode_zstd

pub func main(arguments: list[str]) -> int!:
    let encoded = encode(b"Luce package source")
    let decoded = decode(encoded.bytes(), max_output = 19)
    assert(decoded.bytes() == b"Luce package source")
    assert(decoded.consumed() == encoded.bytes().length)
    let retained = decoded.bytes()
    decoded.close()
    encoded.close()
    assert(retained == b"Luce package source")
    assert(crc32(b"123456789") == 3421780262)
    let source = encode(b"small streaming result")
    let inflater = Inflater(max_output = 22)
    let first = inflater.feed(source.bytes(), capacity = 0, final_input = true)
    assert(first.status() == need_output and first.bytes().length == 0)
    let next = inflater.feed(source.bytes()[first.consumed()..], capacity = 64)
    assert(next.status() == finished and next.bytes() == b"small streaming result")
    assert(inflater.statistics().finished and inflater.statistics().output_bytes == 22)
    inflater.reset()
    assert(inflater.statistics().input_bytes == 0)
    inflater.close()
    assert(next.bytes() == b"small streaming result")
    let deflater = Deflater(max_input = 22)
    let blocked = deflater.feed(b"small streaming result", capacity = 0, final_input = true)
    assert(blocked.status() == need_output and blocked.consumed() == 0)
    let packed = deflater.feed(b"small streaming result", capacity = 64)
    assert(packed.status() == finished and packed.consumed() == 22)
    assert(deflater.statistics().finished and deflater.statistics().input_bytes == 22)
    deflater.reset()
    assert(deflater.statistics().output_bytes == 0)
    deflater.close()
    let roundtrip = decode(packed.bytes())
    assert(roundtrip.bytes() == b"small streaming result")
    let encoder = Deflater()
    let decoder = Inflater()
    let plain = b"small streaming result"
    var input_at = 0
    var output_at = 0
    var steps = 0
    while true:
        let chunk = encoder.feed(plain[input_at..], capacity = 64, final_input = true, work_limit = 1)
        assert(chunk.work_units() == 1)
        # The incremental encoder's stream (packed above), not the whole-buffer one.
        assert(chunk.bytes() == packed.bytes()[output_at..<output_at + chunk.bytes().length])
        input_at += chunk.consumed()
        output_at += chunk.bytes().length
        steps += 1
        assert(steps < 100)
        if chunk.status() == finished: break
        assert(chunk.status() == yielded)
    assert(input_at == plain.length and output_at == packed.bytes().length)
    assert(encoder.feed(b"", work_limit = 1).work_units() == 0)
    input_at = 0
    output_at = 0
    steps = 0
    while true:
        let chunk = decoder.feed(source.bytes()[input_at..], capacity = 64, final_input = true, work_limit = 1)
        assert(chunk.work_units() == 1)
        assert(chunk.bytes() == plain[output_at..<output_at + chunk.bytes().length])
        input_at += chunk.consumed()
        output_at += chunk.bytes().length
        steps += 1
        assert(steps < 100)
        if chunk.status() == finished: break
        assert(chunk.status() == yielded)
    assert(input_at == source.bytes().length and output_at == plain.length)
    decoder.close()
    encoder.close()
    # Brotli: WBITS 16, an uncompressed meta-block of "hello", then the empty last one.
    let hello = decode_brotli(b"\x40\x00\x10hello\x03", max_output = 5)
    assert(hello.bytes() == b"hello" and hello.consumed() == 9)
    hello.close()
    let zipped = encode_gzip(b"small streaming result", level = 9)
    let unzipped = decode_gzip(zipped.bytes())
    assert(unzipped.bytes() == b"small streaming result")
    unzipped.close()
    zipped.close()
    let squeezed = encode_zstd(b"small streaming result small streaming result", level = 3)
    let unsqueezed = decode_zstd(squeezed.bytes(), max_output = 45)
    assert(unsqueezed.bytes() == b"small streaming result small streaming result")
    unsqueezed.close()
    squeezed.close()
    print("PASS Luce codec API, binary ownership, consumed-byte count and one-operation cooperative yields")
    return 0