46 lines
2.4 KiB
Python
46 lines
2.4 KiB
Python
# .Z fixtures (tools/fixtures.py): Unix compress files written by ncompress 5.0 (nixpkgs#ncompress, `compress -c -f
|
|
# -b N`) from a few inputs: empty, one byte, short text, a 700 KB word corpus with incompressible stretches (the 16-bit
|
|
# table fills, the ratio check emits CLEAR codes), random bytes, and every width 10..16; a .tar.Z for the api's unwrap.
|
|
# gzip -d reads each file too (its output must be the input: a second decoder agreeing on what ncompress wrote).
|
|
# tests/gen/z.luau { cases = { { name, input, bits, z } }, tarZ = data, tarNames = { ... } }
|
|
import io, subprocess, tarfile
|
|
|
|
|
|
def compress(data, bits):
|
|
return subprocess.run(["nix", "shell", "nixpkgs#ncompress", "-c", "compress", "-c", "-f", "-b", str(bits)], input=data,
|
|
capture_output=True, check=True).stdout
|
|
|
|
|
|
def corpus(rng, size):
|
|
words = ["".join(rng.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(rng.randint(2, 9))) for _ in range(3000)]
|
|
out = bytearray()
|
|
while len(out) < size:
|
|
if rng.random() < 0.002:
|
|
out += rng.randbytes(rng.randint(500, 4000)) # incompressible stretches: the ratio drops, CLEAR follows
|
|
else:
|
|
out += (" ".join(rng.choice(words) for _ in range(12)) + ".\n").encode()
|
|
return bytes(out[:size])
|
|
|
|
|
|
def generate(ctx):
|
|
rng = ctx.rng(23)
|
|
big = corpus(rng, 700000)
|
|
inputs = [("empty", b"", 16), ("one", b"A", 16), ("text", b"TOBEORNOTTOBEORTOBEORNOT#" * 40, 16),
|
|
("random", rng.randbytes(30000), 16), ("corpus16", big, 16)]
|
|
mid = big[:120000]
|
|
for bits in range(10, 16): # (-b 9 output does not round-trip through ncompress or gzip: not a valid file)
|
|
inputs.append(("corpus-b%d" % bits, mid, bits))
|
|
cases = []
|
|
for name, data, bits in inputs:
|
|
z = compress(data, bits)
|
|
back = subprocess.run(["nix", "shell", "nixpkgs#gzip", "-c", "gzip", "-d", "-c"], input=z, capture_output=True, check=True).stdout
|
|
assert back == data, "gzip -d disagrees on " + name
|
|
cases.append({"name": name, "input": data, "bits": bits, "z": z})
|
|
b = io.BytesIO()
|
|
with tarfile.open(fileobj=b, mode="w", format=tarfile.PAX_FORMAT) as t:
|
|
for n, d in (("a.txt", b"alpha\n"), ("b/c.txt", b"gamma " * 100)):
|
|
ti = tarfile.TarInfo(n)
|
|
ti.size, ti.mtime = len(d), 1700000000
|
|
t.addfile(ti, io.BytesIO(d))
|
|
ctx.emit("z", {"cases": cases, "tarZ": compress(b.getvalue(), 16), "tarNames": ["a.txt", "b/c.txt"]})
|