png: tests for maxSide / reduce (every PngSuite and generated image, APNG frames, a 2048x2048 16-bit fixture), yield; paeth for 16-bit RGB rows; jpeg: opts.maxSide (scale 1..8) and opts.yield reach the codec; Deflate.zlibStream tests

This commit is contained in:
4DBug committed 2026-11-02 23:01:59 -06:00
1 parent e3e6273799
commit 64a2553c0c
8 files changed
+321 -9

No files matched your search

+91
View File
@@ -161,6 +161,97 @@ do
end, samples, { mutations = 3, garbage = 300 })
end
-- zlibStream: split input ranges (junk between them), a small output window, a sink that takes fixed-size records
local function stream(b: buffer, pieces: number, window: number?, record: number): (string?, any)
local n = buffer.len(b)
local big = buffer.create(n + pieces * 7)
buffer.fill(big, 0, 0xCD)
local ranges = {}
local at, w = 0, 0
for i = 1, pieces do
local len = if i == pieces then n - at else (n - at) // (pieces - i + 1)
buffer.copy(big, w, b, at, len)
table.insert(ranges, { w, len })
at += len
w += len + 7
end
local parts = {}
local size, err = Deflate.zlibStream(big, ranges, function(out: buffer, from: number, to: number): number
table.insert(parts, buffer.readstring(out, from, to - from))
return (to - from) % record
end, window)
if not size then
return nil, err
end
return table.concat(parts), size
end
do
local cases, other = 0, nil
for _, c in Fx.cases do
if c.kind == "zlib" then
local want = Fx.corpora[c.corpus]
local b = buffer.fromstring(c.data)
other = other or b
for _, v in { { 1, nil, 1 }, { 3, 1, 777 }, { 9, 5000, 4096 } } do
local got, size = stream(b, math.min(v[1], buffer.len(b)), v[2], v[3])
t:check(got == want and size == #want, string.format("%s: zlibStream %d ranges, window %s: %s", c.name, v[1], tostring(v[2]), tostring(size)))
end
cases += 1
end
end
t:note("zlibStream: %d zlib streams match, split and through small windows", cases)
-- a sink that yields while another inflate runs in between
local src = buffer.create(300000)
for i = 0, 299999 do
buffer.writeu8(src, i, (i * 13 + (i // 1000) * 7 + (i // 7) % 11) % 256)
end
local z = assert(Deflate.zlibCompress(src, { level = 6 }))
local got = {}
local co = coroutine.wrap(function(): any
return Deflate.zlibStream(z, { { 0, buffer.len(z) } }, function(out: buffer, from: number, to: number): number
table.insert(got, buffer.readstring(out, from, to - from))
coroutine.yield()
return 0
end, 4096)
end)
local res = co()
local yields = 0
while res == nil do
yields += 1
local mid = Deflate.zlib(z)
t:check(mid and buffer.len(mid) == 300000, "inflate while a stream is suspended")
res = co()
end
t:check(res == 300000 and table.concat(got) == buffer.tostring(src), "zlibStream across yields (" .. yields .. ")")
local bad = buffer.fromstring(buffer.tostring(z))
buffer.writeu8(bad, buffer.len(bad) - 1, 0)
local _, e = Deflate.zlibStream(bad, { { 0, buffer.len(bad) } }, function()
return 0
end)
t:check(e and e.code == Err.CHECKSUM, "zlibStream Adler-32: " .. tostring(e))
local _, e2 = Deflate.zlibStream(z, { { 0, buffer.len(z) } }, function()
return -1
end)
t:check(e2 and e2.code == Err.CORRUPT, "zlibStream sink abort: " .. tostring(e2))
local _, e3 = Deflate.zlibStream(z, { { 0, buffer.len(z) + 1 } }, function()
return 0
end)
t:check(e3 and e3.code == Err.ARG, "zlibStream range out of bounds")
local samples = {}
for i, c in Fx.cases do
if c.kind == "zlib" and i % 5 == 0 then
table.insert(samples, c.data)
end
end
t:fuzz("zlibStream", function(b: buffer)
local half = buffer.len(b) // 2
return Deflate.zlibStream(b, { { 0, half }, { half, buffer.len(b) - half } }, function(_: buffer, from: number, to: number): number
return (to - from) % 5
end, 100)
end, samples, { mutations = 3, garbage = 100 })
end
-- peek: the first bytes of a gzip / zlib / raw stream without inflating the rest
do
local src = buffer.create(100000)