73 lines
3.5 KiB
Python
73 lines
3.5 KiB
Python
# LZ4 frame fixtures (tools/fixtures.py): corpora compressed by the lz4 tool 1.10 (nixpkgs#lz4) with its frame options
|
|
# (default, linked blocks -BD, 64 KB blocks -B4 with and without -BD, block checksums -BX, --content-size,
|
|
# --no-frame-crc, -9, the legacy format -l), concatenated frames and a skippable frame; and our writer's frames
|
|
# (tools/fixtures/_lz4frame_encode.luau under the luau CLI) decompressed by `lz4 -d`, as verdicts.
|
|
# tests/gen/lz4frame.luau { cases = { { name, corpus, data } }, ours = { { name, ok } } }
|
|
# tests/gen/lz4frame_corpora.luau { [name] = bytes }
|
|
import os, shutil, struct, subprocess, tempfile
|
|
|
|
INPUTS = ["src/Rosetta/archive/lz4/init.luau", "src/Rosetta/codecs/compress/Lz4.luau", "src/Rosetta/codecs/compress/Xxh32.luau",
|
|
"tools/fixtures/_lz4frame_encode.luau"]
|
|
|
|
|
|
def lz4(args, data=None, path=None):
|
|
argv = ["nix", "shell", "nixpkgs#lz4", "-c", "lz4"] + args + ([path] if path else [])
|
|
return subprocess.run(argv, input=data, capture_output=True, check=True).stdout
|
|
|
|
|
|
def corpora(rng):
|
|
base = os.path.dirname(os.__file__)
|
|
text = bytearray()
|
|
for name in sorted(os.listdir(base)):
|
|
if name.endswith(".py"):
|
|
with open(os.path.join(base, name), "rb") as f:
|
|
text += f.read()
|
|
if len(text) >= 160000:
|
|
break
|
|
return {"empty": b"", "short": b"hello, lz4 frames\n", "text": bytes(text[:160000]), "random": rng.randbytes(20000),
|
|
"zeros": b"\0" * 300000}
|
|
|
|
|
|
def luau_cmd():
|
|
exe = shutil.which("luau")
|
|
return [exe] if exe else ["nix", "shell", "nixpkgs#luau", "-c", "luau"]
|
|
|
|
|
|
def generate(ctx):
|
|
cs = corpora(ctx.rng(43))
|
|
cases = []
|
|
tmp = tempfile.mkdtemp()
|
|
try:
|
|
for name, data in cs.items():
|
|
path = os.path.join(tmp, name)
|
|
with open(path, "wb") as f:
|
|
f.write(data)
|
|
variants = [("default", ["-c"])]
|
|
if name in ("text", "zeros", "short"):
|
|
variants += [("BD", ["-c", "-BD"]), ("B4", ["-c", "-B4"]), ("B4BD", ["-c", "-B4", "-BD"]), ("BX", ["-c", "-B4", "-BX"]),
|
|
("size", ["-c", "--content-size"]), ("nocrc", ["-c", "--no-frame-crc"]), ("hc9", ["-c", "-9", "-B4", "-BD"]),
|
|
("legacy", ["-c", "-l"])]
|
|
for vname, args in variants:
|
|
cases.append({"name": name + "/" + vname, "corpus": name, "data": lz4(args, path=path)})
|
|
finally:
|
|
shutil.rmtree(tmp)
|
|
# two frames, and a skippable frame in front
|
|
two = lz4(["-c"], data=cs["short"]) + lz4(["-c", "-B4"], data=cs["text"])
|
|
cases.append({"name": "concat", "corpus": "short+text", "data": two})
|
|
skip = struct.pack("<II", 0x184D2A53, 5) + b"skip!" + lz4(["-c"], data=cs["short"])
|
|
cases.append({"name": "skippable", "corpus": "short", "data": skip})
|
|
ctx.emit("lz4frame_corpora", cs)
|
|
enc = subprocess.run(luau_cmd() + [os.path.join(ctx.root, "tools", "fixtures", "_lz4frame_encode.luau")], capture_output=True,
|
|
check=True, cwd=ctx.root).stdout.decode()
|
|
ours = []
|
|
for line in enc.split("\n"):
|
|
if line.strip():
|
|
name, hexdata = line.split(" ", 1)
|
|
try:
|
|
got = lz4(["-d", "-c"], data=bytes.fromhex(hexdata.strip()))
|
|
ok = got == cs[name.split("/")[0]]
|
|
except subprocess.CalledProcessError:
|
|
ok = False
|
|
ours.append({"name": name, "ok": ok})
|
|
ctx.emit("lz4frame", {"cases": cases, "ours": sorted(ours, key=lambda o: o["name"])})
|