Files
rosetta/tests/codecs/deflate.luau
T
4DBugandClaude Opus 5.5 434a304315 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically):
- codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder;
  codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample
  VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url;
  codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err
  Crc32 Adler32 Base64 Md5 Input Sniff.
- LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md).
- Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes.

C3, generic code out of format folders:
- containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first,
  trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64
  through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it.
- codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position,
  priming and end trimming) that aac, caf and m4a each carried (~70 lines each).
- codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of
  its own (the AND mask / alpha rule stay ICO's).
- codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model),
  Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is
  Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the
  fixtures' bind shapes).
- README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes:
  FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the
  shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks
  decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type
  baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 12:25:03 -05:00

173 lines
6.9 KiB
Luau

-- codecs/compress/Deflate tests: every fixture case (Python zlib, levels 0-9, every strategy, small windows, flushes, gzip
-- headers, multi-member gzip) decodes; exact-size mode; checksum vectors; the compressor round-trips every corpus at
-- every level (and its zlib / gzip framing); truncated / corrupted / random input returns nil, Err and never throws.
-- Ported from deflate-luau tests/inflate.luau. Fixtures: tools/fixtures/deflate.py.
local T = require("@tests/T")
local Deflate = require("@formats/codecs/compress/Deflate")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/deflate")
local t = T.suite("deflate")
local function decode(kind: string, b: buffer, ...: any): (buffer?, any)
if kind == "raw" then
return Deflate.inflate(b, ...)
elseif kind == "zlib" then
return Deflate.zlib(b, ...)
else
return Deflate.gunzip(b, ...)
end
end
-- checksum vectors
t:eq(Deflate.crc32("123456789"), 0xCBF43926, "crc32 check value")
t:eq(Deflate.crc32(""), 0, "crc32 empty")
t:eq(Deflate.adler32("Wikipedia"), 0x11E60398, "adler32 Wikipedia")
t:eq(Deflate.adler32(""), 1, "adler32 empty")
do
local s = string.rep("The quick brown fox jumps over the lazy dog. ", 400)
local b = buffer.fromstring(s)
local whole = Deflate.crc32(b)
local part = Deflate.crc32(b, 0, 1001)
t:eq(Deflate.crc32(b, 1001, nil, part), whole, "crc32 continuation")
local a = Deflate.adler32(b, 0, 7000)
t:eq(Deflate.adler32(b, 7000, nil, a), Deflate.adler32(b), "adler32 continuation")
t:eq(Deflate.crc32(b, 3, 17), Deflate.crc32(string.sub(s, 4, 20)), "crc32 range")
end
-- every case decodes to its corpus
local nbytes = 0
for _, c in Fx.cases do
local want = Fx.corpora[c.corpus]
local b = buffer.fromstring(c.data)
local out, err = decode(c.kind, b)
t:check(out ~= nil, c.name .. ": " .. tostring(err))
t:check(buffer.tostring(out :: buffer) == want, c.name .. ": output differs")
nbytes += #want
if c.kind == "raw" then
t:eq(err, #c.data, c.name .. ": consumed")
-- exact size, and a wrong exact size
local o2 = Deflate.inflate(b, 0, nil, #want)
t:check(o2 and buffer.tostring(o2) == want, c.name .. ": exact size")
if #want > 0 then
t:check(Deflate.inflate(b, 0, nil, #want - 1) == nil, c.name .. ": too small exact size fails")
end
t:check(Deflate.inflate(b, 0, nil, #want + 1) == nil, c.name .. ": too large exact size fails")
-- the same stream inside a bigger buffer, at an offset, with trailing bytes
local big = buffer.create(#c.data + 13)
buffer.fill(big, 0, 0xAB)
buffer.copy(big, 5, b)
local o3, e3 = Deflate.inflate(big, 5, #c.data + 8)
t:check(o3 and buffer.tostring(o3) == want and e3 == 5 + #c.data, c.name .. ": at an offset")
elseif c.kind == "zlib" then
t:eq(err, #c.data, c.name .. ": zlib consumed")
end
end
t:note("%d fixture streams, %.1f MB decoded", #Fx.cases, nbytes / 1e6)
-- strings are accepted
t:check(buffer.tostring((Deflate.zlib(Fx.cases[25].data))) == Fx.corpora[Fx.cases[25].corpus], "string input")
-- corrupted checksums
do
local z
for _, c in Fx.cases do
if c.kind == "zlib" and #Fx.corpora[c.corpus] > 100 then
z = c
break
end
end
local b = buffer.fromstring(z.data)
buffer.writeu8(b, buffer.len(b) - 1, bit32.bxor(buffer.readu8(b, buffer.len(b) - 1), 1))
local out, err = Deflate.zlib(b)
t:check(out == nil and Err.is(err) and err.code == Err.CHECKSUM, "zlib adler mismatch: " .. tostring(err))
for _, c in Fx.cases do
if c.kind == "gzip" and #Fx.corpora[c.corpus] > 100 then
local g = buffer.fromstring(c.data)
local n = buffer.len(g)
buffer.writeu8(g, n - 6, bit32.bxor(buffer.readu8(g, n - 6), 0x40))
local o, e = Deflate.gunzip(g)
t:check(o == nil and e and e.code == Err.CHECKSUM, c.name .. ": gzip crc mismatch: " .. tostring(e))
break
end
end
end
-- hand-made streams
do
local bytes = T.bytes
-- stored block "hi" then a fixed block with only end-of-block
local o = Deflate.inflate(bytes({ 0x00, 0x02, 0x00, 0xFD, 0xFF, 0x68, 0x69, 0x03, 0x00 }))
t:check(o and buffer.tostring(o) == "hi", "stored + fixed EOB")
t:check(Deflate.inflate(bytes({ 0x01, 0x02, 0x00, 0xFC, 0xFF, 0x68, 0x69 })) == nil, "stored NLEN mismatch")
t:check(Deflate.inflate(bytes({ 0x07 })) == nil, "block type 3")
local _, e0 = Deflate.inflate(bytes({}))
t:check(e0 and e0.code == Err.TRUNCATED, "empty input is TRUNCATED: " .. tostring(e0))
-- fixed block: a match before any output (distance too far back)
local _, e = Deflate.inflate(bytes({ 0x03, 0x02, 0x00 }))
t:check(e ~= nil and e.code == Err.CORRUPT, "distance too far back: " .. tostring(e))
t:check(Deflate.zlib(bytes({ 0x78, 0x9c })) == nil, "zlib truncated header")
t:check(Deflate.zlib(bytes({ 0x78, 0x9d, 0x03, 0, 0, 0, 0, 1 })) == nil, "zlib FCHECK")
t:check(Deflate.gunzip(bytes({ 0x1f, 0x8c, 8, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 })) == nil, "gzip magic")
local ok, r1, r2 = pcall(Deflate.inflate, 5 :: any)
t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG")
local ok2, _, r3 = pcall(Deflate.inflate, "abc", -1)
t:check(ok2 and r3.code == Err.ARG, "a bad offset returns Err.ARG")
end
-- the compressor: every corpus, every level, raw / zlib / gzip; ratio against Python zlib -6
do
local total, comp6, ref6 = 0, 0, 0
for name, data in Fx.corpora do
local b = buffer.fromstring(data)
for level = 0, 9 do
local z, err = Deflate.compress(b, { level = level })
t:check(z, name .. ": compress level " .. level .. ": " .. tostring(err))
local back, berr = Deflate.inflate(z :: buffer)
t:check(back and buffer.tostring(back) == data, string.format("%s: level %d round trip: %s", name, level, tostring(berr)))
if level == 6 then
comp6 += buffer.len(z :: buffer)
total += #data
end
end
local zl = Deflate.zlibCompress(data) :: buffer
local back = Deflate.zlib(zl)
t:check(back and buffer.tostring(back) == data, name .. ": zlibCompress round trip")
local gz = Deflate.gzip(data, { level = 9 }) :: buffer
local back2 = Deflate.gunzip(gz)
t:check(back2 and buffer.tostring(back2) == data, name .. ": gzip round trip")
end
for _, c in Fx.cases do
if c.name == c.corpus .. "/zlib6" then
ref6 += #c.data - 6
end
end
t:note("compress -6: %d -> %d bytes (%.1f%%); Python zlib -6: %d bytes", total, comp6, 100 * comp6 / total, ref6)
t:check(comp6 < ref6 * 1.25, "level 6 within 25% of zlib")
local _, e = Deflate.compress("x", { level = 11 })
t:check(e and e.code == Err.ARG, "bad level returns Err.ARG")
end
-- truncation, corruption and garbage never throw
do
local samples = {}
local kinds = {}
for i, c in Fx.cases do
if i % 9 == 0 and #c.data > 8 then
table.insert(samples, c.data)
kinds[c.data] = c.kind
end
end
t:fuzz("inflate", function(b: buffer)
return Deflate.inflate(b)
end, samples, { mutations = 6, garbage = 3000 })
t:fuzz("zlib", function(b: buffer)
return Deflate.zlib(b)
end, samples, { mutations = 3, garbage = 300 })
t:fuzz("gunzip", function(b: buffer)
return Deflate.gunzip(b)
end, samples, { mutations = 3, garbage = 300 })
end
t:done()