Something went wrong. Try again.
Brotli (RFC 7932) compression and decompression for Zig 0.17, with sans-I/O streaming cores and std.Io adapters.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394#!/usr/bin/env python3# SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us># SPDX-License-Identifier: MIT
"""Checks this library against the reference implementation.
zig build interop
Every input from tools/make_vectors.py, and a few more, is compressed bythis library at every quality and several window sizes and decompressed bythe reference decoder; and compressed by the reference encoder anddecompressed by this library. Both go through zbrotli, the library'scommand-line front end, so that the std.Io adapters are part of what ischecked.
It ends with how the sizes compare, which is not checked, only reported."""
import osimport subprocessimport sys
import brotli
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))import make_vectors as mv # noqa: E402
def run(zbrotli, args, data): p = subprocess.run([zbrotli] + args, input=data, capture_output=True) if p.returncode != 0: raise RuntimeError("zbrotli %s failed: %s" % (" ".join(args), p.stderr.decode())) return p.stdout
def main(zbrotli): text = mv.text(80 << 10, 1) inputs = { "empty": b"", "byte": b"x", "text": text, "long-text": mv.text(3 << 20, 9), "noise": mv.noise(200 << 10, 2), "samples": mv.samples(48 << 10, 3), "zeros": bytes(1 << 20), "mixed": mv.text(40 << 10, 4) + mv.noise(8 << 10, 5) + mv.samples(24 << 10, 6) + mv.text(24 << 10, 7), } failures = 0 sizes = {} for name, data in inputs.items(): for q in range(12): for w in (10, 16, 22, 24): if name == "long-text" and w != 22 and q not in (0, 6, 11): continue c = run(zbrotli, ["-q", str(q), "-w", str(w)], data) try: ok = brotli.decompress(c) == data except brotli.error as e: ok = False print("%s q%d w%d: reference decoder: %s" % (name, q, w, e)) if not ok: failures += 1 print("FAIL compress %s q%d w%d" % (name, q, w)) if w == 22: sizes[(name, q)] = len(c) for q in (0, 1, 5, 9, 11): c = brotli.compress(data, quality=q) if run(zbrotli, ["-d"], c) != data: failures += 1 print("FAIL decompress %s reference q%d" % (name, q)) sizes[(name, "ref", q)] = len(c) far = mv.spread_copies(8) for q in (0, 6): if brotli.decompress(run(zbrotli, ["-q", str(q), "-w", "24"], far)) != far: failures += 1 print("FAIL compress far-copies q%d" % q)
print("%-10s %10s %s" % ("input", "size", " ".join("q%-6d" % q for q in range(12)))) for name, data in inputs.items(): if not data: continue ours = " ".join("%-7d" % sizes[(name, q)] for q in range(12)) print("%-10s %10d %s" % (name, len(data), ours)) ref = " ".join("ref q%d: %d" % (q, sizes[(name, "ref", q)]) for q in (1, 5, 9, 11)) print("%-10s %10s %s" % ("", "", ref)) if failures: print("%d failures" % failures) sys.exit(1) print("all interoperable")
if __name__ == "__main__": main(sys.argv[1])