-- codecs/compress/Lz4 tests: every reference block (python-lz4 fast at accelerations 1 / 4 / 16, HC 4 / 9 / 12) -- decodes to its corpus, with and without the size; our encoder's blocks decode with the reference (fixture verdicts) -- and round trip here; linked blocks through decodeInto's history; error codes; truncated / mutated / random blocks -- never throw. Fixtures: tools/fixtures/lz4.py. local T = require("@tests/T") local Lz4 = require("@formats/codecs/compress/Lz4") local Err = require("@formats/core/Err") local Fx = require("@tests/gen/lz4") local C = require("@tests/gen/lz4_corpora") local t = T.suite("lz4") local nbytes = 0 for _, c in Fx.cases do local want = C[c.corpus] local out, err = Lz4.decompress(c.data, nil, nil, #want) t:check(out ~= nil and buffer.tostring(out) == want, c.name .. ": " .. tostring(err)) if #want > 0 then local grown = Lz4.decompress(c.data) t:check(grown ~= nil and buffer.tostring(grown) == want, c.name .. ": without the size") end nbytes += #want end t:note("%d reference blocks, %.1f MB decoded", #Fx.cases, nbytes / 1e6) for _, v in Fx.ours do t:check(v.ok, v.name .. ": the reference decodes our block") end for name, data in C do local b = Lz4.compress(data) :: buffer local back = Lz4.decompress(b, nil, nil, #data) t:check(back ~= nil and buffer.tostring(back) == data, name .. ": round trip") if name == "text" or name == "zeros" or name == "runs" then t:check(buffer.len(b) < #data / 2, name .. ": compresses") end t:check(buffer.len(b) <= Lz4.bound(#data), name .. ": within bound") end -- at an offset, and linked blocks: the second block refers into the first one's output do local text = C.text local a, b2 = string.sub(text, 1, 70000), string.sub(text, 70001, 140000) local pa = Lz4.compress(a) :: buffer -- a block of b2 that copies the first 40 bytes of b2's region from a (a match reaching back across blocks) local big = buffer.create(buffer.len(pa) + 20) buffer.copy(big, 9, pa) local out = Lz4.decompress(big, 9, buffer.len(pa), #a) t:check(out ~= nil and buffer.tostring(out) == a, "at an offset") local hand = buffer.create(3 + 1 + 2 + 1) -- token: 0 literals, match length 15 + ext; offset = 100 back into the previous block buffer.writeu8(hand, 0, 0x0F) buffer.writeu16(hand, 1, 100) buffer.writeu8(hand, 3, 20) -- match length 4 + 15 + 20 = 39 buffer.writeu8(hand, 4, 0x10) -- the last sequence: one literal buffer.writeu8(hand, 5, 33) local dst = buffer.create(#a + 40) buffer.writestring(dst, 0, a) local e, msg = Lz4.decodeInto(hand, 0, 6, dst, #a, #a + 40, 0) t:check(e == #a + 40, "linked block: " .. tostring(msg)) t:eq(buffer.readstring(dst, #a, 39), string.sub(a, #a - 99, #a - 61), "linked block: match from the history") local e2, msg2 = Lz4.decodeInto(hand, 0, 6, dst, #a, #a + 40) t:check(e2 == nil and string.find(msg2 or "", "offset") ~= nil, "without history the match is corrupt") t:check(b2 ~= nil, "second half") end -- errors do local _, e1 = Lz4.decompress("") t:check(e1 and e1.code == Err.TRUNCATED, "empty: " .. tostring(e1)) local _, e2 = Lz4.decompress("\x50ab") t:check(e2 and e2.code == Err.TRUNCATED, "literals past the end: " .. tostring(e2)) local _, e3 = Lz4.decompress("\x14abcd\x09\x00\x10z") t:check(e3 and e3.code == Err.CORRUPT, "offset before the start: " .. tostring(e3)) local _, e4 = Lz4.decompress("\x10a", nil, nil, 5) t:check(e4 and e4.code == Err.CORRUPT, "shorter than outSize: " .. tostring(e4)) local _, e5 = Lz4.decompress("\x50abcde", nil, nil, 2) t:check(e5 and e5.code == Err.LIMIT, "longer than outSize: " .. tostring(e5)) local _, e6 = Lz4.decompress(C.zeros and (Lz4.compress(C.zeros) :: buffer), nil, nil, nil, { maxBytes = 1000 }) t:check(e6 and e6.code == Err.LIMIT, "maxBytes: " .. tostring(e6)) local ok, r1, r2 = pcall(Lz4.decompress, 5 :: any) t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG") local _, e7 = Lz4.decompress("\x10a", nil, nil, -1) t:check(e7 and e7.code == Err.ARG, "bad outSize") end do local samples = {} for i, c in Fx.cases do if i % 4 == 0 and #c.data > 8 and #c.data < 30000 then table.insert(samples, c.data) end end t:fuzz("decompress", function(b: buffer) return Lz4.decompress(b, nil, nil, nil, { maxBytes = 1e6 }) end, samples, { mutations = 30, garbage = 2000 }) t:fuzz("decompress (sized)", function(b: buffer) return Lz4.decompress(b, nil, nil, 70000) end, samples, { mutations = 30, garbage = 500, seed = 5 }) end t:done()