120 lines
4.1 KiB
Luau
120 lines
4.1 KiB
Luau
-- codecs/compress/Lzw tests: GIF (LSB) and TIFF (MSB, early change) code streams from Pillow / libtiff decode to their data;
|
|
-- our encoder round-trips random and structured data at every GIF minimum code size and through table resets; the
|
|
-- output limit, truncated and corrupt streams; the fuzz no-throw pass. Fixtures: tools/fixtures/lzw.py.
|
|
local T = require("@tests/T")
|
|
local Lzw = require("@rosetta/codecs/compress/Lzw")
|
|
local Fx = require("@tests/gen/lzw")
|
|
|
|
local t = T.suite("lzw")
|
|
|
|
local function dec(codes: buffer, n: number, variant: string, min: number?): (string, number, string?)
|
|
local out = buffer.create(n)
|
|
local got, err = Lzw.decode(codes, 0, buffer.len(codes), out, 0, n, variant, min)
|
|
return buffer.tostring(out), got, err
|
|
end
|
|
|
|
-- reference streams
|
|
for _, c in Fx.gif do
|
|
local s, n, err = dec(buffer.fromstring(c.codes), #c.data, "gif", c.min)
|
|
t:check(err == nil, "gif " .. c.name .. ": " .. tostring(err))
|
|
t:eq(n, #c.data, "gif " .. c.name .. ": length")
|
|
t:check(s == c.data, "gif " .. c.name .. ": data")
|
|
end
|
|
for _, c in Fx.tiff do
|
|
local s, n, err = dec(buffer.fromstring(c.codes), #c.data, "tiff")
|
|
t:check(err == nil, "tiff " .. c.name .. ": " .. tostring(err))
|
|
t:eq(n, #c.data, "tiff " .. c.name .. ": length")
|
|
t:check(s == c.data, "tiff " .. c.name .. ": data")
|
|
end
|
|
t:note("%d GIF and %d TIFF reference streams decode", #Fx.gif, #Fx.tiff)
|
|
|
|
-- round trips
|
|
local rand = T.rng(3)
|
|
local function data(n: number, kind: number, max: number): buffer
|
|
local b = buffer.create(n)
|
|
for i = 0, n - 1 do
|
|
local v
|
|
if kind == 0 then
|
|
v = rand(max)
|
|
elseif kind == 1 then
|
|
v = (i // 7) % max
|
|
else
|
|
v = if rand(10) == 0 then rand(max) else (i % 13) % max
|
|
end
|
|
buffer.writeu8(b, i, v)
|
|
end
|
|
return b
|
|
end
|
|
local trips = 0
|
|
for _, n in { 0, 1, 2, 3, 100, 5000, 70000 } do
|
|
for kind = 0, 2 do
|
|
for min = 2, 8 do
|
|
local d = data(n, kind, 2 ^ min)
|
|
local codes = Lzw.encode(d, 0, n, "gif", min)
|
|
local s, got, err = dec(codes, n, "gif", min)
|
|
t:check(err == nil and got == n and s == buffer.tostring(d), string.format("gif round trip n=%d kind=%d min=%d: %s", n, kind, min, tostring(err)))
|
|
trips += 1
|
|
end
|
|
local d = data(n, kind, 256)
|
|
local codes = Lzw.encode(d, 0, n, "tiff")
|
|
local s, got, err = dec(codes, n, "tiff")
|
|
t:check(err == nil and got == n and s == buffer.tostring(d), string.format("tiff round trip n=%d kind=%d: %s", n, kind, tostring(err)))
|
|
trips += 1
|
|
end
|
|
end
|
|
t:note("%d round trips", trips)
|
|
|
|
-- compression actually happens; a range inside a bigger buffer
|
|
do
|
|
local d = buffer.create(10000)
|
|
local codes = Lzw.encode(d, 0, 10000, "tiff")
|
|
t:check(buffer.len(codes) < 400, "zeros compress: " .. buffer.len(codes))
|
|
local big = data(300, 1, 256)
|
|
local codes2 = Lzw.encode(big, 100, 100, "gif", 8)
|
|
local out = buffer.create(120)
|
|
local n = Lzw.decode(codes2, 0, buffer.len(codes2), out, 10, 100, "gif", 8)
|
|
t:eq(n, 100, "sub-range length")
|
|
t:check(buffer.readstring(out, 10, 100) == buffer.readstring(big, 100, 100), "sub-range data")
|
|
end
|
|
|
|
-- limits, truncation, corruption
|
|
do
|
|
local d = data(1000, 2, 16)
|
|
local codes = Lzw.encode(d, 0, 1000, "gif", 4)
|
|
local s, n, err = dec(codes, 500, "gif", 4)
|
|
t:check(n == 500 and err == nil and s == buffer.readstring(d, 0, 500), "output limit")
|
|
local cut = buffer.create(buffer.len(codes) // 2)
|
|
buffer.copy(cut, 0, codes, 0, buffer.len(cut))
|
|
local _, n2, err2 = dec(cut, 1000, "gif", 4)
|
|
t:check(err2 == "truncated" and n2 > 0 and n2 < 1000, "truncated: " .. tostring(err2))
|
|
local bad = buffer.fromstring("\255\255\255\255")
|
|
local _, _, err3 = dec(bad, 100, "gif", 2)
|
|
t:check(err3 == "corrupt", "corrupt: " .. tostring(err3))
|
|
local _, _, err4 = dec(codes, 100, "gif", 0)
|
|
t:check(err4 == "corrupt", "bad min code size")
|
|
end
|
|
|
|
-- fuzz
|
|
do
|
|
local samples = {}
|
|
for i, c in Fx.gif do
|
|
if i % 2 == 1 then
|
|
table.insert(samples, c.codes)
|
|
end
|
|
end
|
|
for i, c in Fx.tiff do
|
|
if i % 2 == 0 then
|
|
table.insert(samples, c.codes)
|
|
end
|
|
end
|
|
local out = buffer.create(100000)
|
|
t:fuzz("decode gif", function(b: buffer)
|
|
return Lzw.decode(b, 0, buffer.len(b), out, 0, 100000, "gif", 8)
|
|
end, samples)
|
|
t:fuzz("decode tiff", function(b: buffer)
|
|
return Lzw.decode(b, 0, buffer.len(b), out, 0, 100000, "tiff")
|
|
end, samples)
|
|
end
|
|
|
|
t:done()
|