ws-arch2-unix: codecs/compress/UnixCompress (.Z LZW decode + encode, bit-identical to ncompress 5.0: group padding on width changes / CLEAR, ratio-check clears) + archive/z (single-stream, .taz / .tZ -> .tar, writes ncompress's bytes), tests vs ncompress and gzip -d, bench lines
This commit is contained in:
1 parent
8b94584d0f
commit
6eadacce64
10 files changed
+690
-1
No files matched your search
@@ -0,0 +1,70 @@
|
||||
-- archive/z tests: .Z files from ncompress load as single-stream archives (entry names from the file name, .taz -> .tar),
|
||||
-- a .tar.Z unwraps to the tar through the api, writing gives ncompress's bytes (a buffer, a one-entry Archive) and a
|
||||
-- tar inside for several entries, conversions, errors, the fuzz pass. Fixtures: tools/fixtures/z.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Z = require("@formats/archive/z")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/z")
|
||||
|
||||
local t = T.suite("z")
|
||||
t:eq(Z.name, "z", "name")
|
||||
|
||||
local text = Fx.cases[3]
|
||||
do
|
||||
local a, err = Z.decode(Bytes.reader(buffer.fromstring(text.z)), { name = "notes.txt.Z" })
|
||||
t:check(a, "decode: " .. tostring(err))
|
||||
t:check(a.meta.stream, "single stream")
|
||||
t:eq(a:list()[1].name, "notes.txt", "entry named after the file")
|
||||
t:eq(a:list()[1].method, "lzw", "method")
|
||||
t:eq(buffer.tostring(a:read("notes.txt")), text.input, "data")
|
||||
t:eq(a.meta.source.maxBits, 16, "maxBits")
|
||||
t:eq(Z.decode(Bytes.reader(buffer.fromstring(text.z))):list()[1].name, "data", "no name: data")
|
||||
t:eq(F.detect(text.z), "z", "F.detect")
|
||||
t:eq(Z.decode(Bytes.reader(buffer.fromstring(text.z)), { name = "x.taz" }):list()[1].name, "x.tar", ".taz -> .tar")
|
||||
t:eq(Z.decode(Bytes.reader(buffer.fromstring(text.z)), { name = "x.tZ" }):list()[1].name, "x.tar", ".tZ -> .tar")
|
||||
end
|
||||
|
||||
-- the api: a .tar.Z is the tar inside
|
||||
do
|
||||
local a, err = F.load(Fx.tarZ, { name = "src.tar.Z" })
|
||||
t:check(a, "tar.Z: " .. tostring(err))
|
||||
t:eq(a.format, "tar", "tar.Z unwraps")
|
||||
t:eq(a.meta.compression, "z", "meta.compression")
|
||||
t:eq(#a:list(), #Fx.tarNames, "tar entries")
|
||||
local outer = F.load(Fx.tarZ, { name = "src.tar.Z", unwrap = false })
|
||||
t:eq(outer:list()[1].name, "src.tar", "unwrap = false")
|
||||
end
|
||||
|
||||
-- writing
|
||||
do
|
||||
local out = assert(F.save(text.input, nil, { format = "z" }))
|
||||
t:check(buffer.tostring(out) == text.z, "a string: ncompress's bytes")
|
||||
local one = Z.new(text.input, "t.txt")
|
||||
t:check(buffer.tostring(assert(one:write())) == text.z, "a one-entry archive: ncompress's bytes")
|
||||
local tar = F.format("tar").new()
|
||||
tar:add("a.txt", "alpha")
|
||||
tar:add("b.txt", "beta")
|
||||
local tz = assert(F.save(tar, nil, { format = "z" }))
|
||||
local back = F.load(tz)
|
||||
t:eq(back.format, "tar", "several entries: a tar inside")
|
||||
t:eq(buffer.tostring(back:read("b.txt")), "beta", "tar inside: data")
|
||||
local b10 = assert(Z.encode(Fx.cases[6].input, { maxBits = Fx.cases[6].bits }))
|
||||
t:check(buffer.tostring(b10) == Fx.cases[6].z, "maxBits option")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = Z.decode(Bytes.reader(buffer.fromstring("\31\157")))
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "short")
|
||||
local _, e2 = Z.encode({ kind = "image" })
|
||||
t:check(e2 and e2.code == Err.ARG, "encode a non-archive")
|
||||
t:eq(Z.sniff(Bytes.reader(buffer.fromstring("\31\139\8"))), 0, "gzip is not .Z")
|
||||
end
|
||||
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Z.decode(Bytes.reader(b), { maxBytes = 2 ^ 22 })
|
||||
end, { text.z, Fx.tarZ }, { mutations = 40, keepHeader = 3 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,75 @@
|
||||
-- codecs/compress/UnixCompress tests: ncompress 5.0 files (widths 10..16, block mode, CLEAR codes after the ratio
|
||||
-- check, empty / one-byte / random inputs) decode to their inputs; compress() gives ncompress's bytes for the same input
|
||||
-- and width; non-block-mode streams; errors; the fuzz pass. Fixtures: tools/fixtures/z.py.
|
||||
local T = require("@tests/T")
|
||||
local UnixCompress = require("@formats/codecs/compress/UnixCompress")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/z")
|
||||
|
||||
local t = T.suite("unixcompress")
|
||||
|
||||
for _, c in Fx.cases do
|
||||
local out, err = UnixCompress.decompress(c.z)
|
||||
t:check(out, c.name .. ": decompress: " .. tostring(err))
|
||||
t:eq(buffer.len(out), #c.input, c.name .. ": size")
|
||||
t:eq(Crc32.of(out), Crc32.of(buffer.fromstring(c.input)), c.name .. ": data")
|
||||
local z, zerr = UnixCompress.compress(c.input, nil, nil, { maxBits = c.bits })
|
||||
t:check(z, c.name .. ": compress: " .. tostring(zerr))
|
||||
t:eq(buffer.len(z), #c.z, c.name .. ": compressed size = ncompress")
|
||||
t:check(buffer.tostring(z) == c.z, c.name .. ": bytes identical to ncompress -b " .. c.bits)
|
||||
t:note("%-12s %7d -> %7d bytes (-b %d)", c.name, #c.input, #c.z, c.bits)
|
||||
end
|
||||
|
||||
-- 9-bit codes, LSB first, after a .Z header with the given flags byte
|
||||
local function pack(flags: number, codes: { number }): buffer
|
||||
local acc, nb, bytes = 0, 0, { 0x1F, 0x9D, flags }
|
||||
for _, c in codes do
|
||||
acc += c * 2 ^ nb
|
||||
nb += 9
|
||||
while nb >= 8 do
|
||||
table.insert(bytes, acc % 256)
|
||||
acc = acc // 256
|
||||
nb -= 8
|
||||
end
|
||||
end
|
||||
if nb > 0 then
|
||||
table.insert(bytes, acc)
|
||||
end
|
||||
return T.bytes(bytes)
|
||||
end
|
||||
|
||||
-- a non-block-mode stream (no CLEAR code; 256 is the first table entry): "ABABABA"
|
||||
do
|
||||
local out = UnixCompress.decompress(pack(0x10, { 65, 66, 256, 258 }))
|
||||
t:eq(out and buffer.tostring(out), "ABABABA", "non-block mode")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = UnixCompress.decompress("\31\157")
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "short header")
|
||||
local _, e2 = UnixCompress.decompress("\31\158\144")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad magic")
|
||||
local _, e3 = UnixCompress.decompress("\31\157\145")
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "17-bit codes")
|
||||
local _, e4 = UnixCompress.decompress(pack(0x90, { 65, 400 }))
|
||||
t:check(e4 and e4.code == Err.CORRUPT, "a code past the table: " .. tostring(e4))
|
||||
local _, e5 = UnixCompress.decompress(Fx.cases[5].z, nil, nil, { maxBytes = 1000 })
|
||||
t:check(e5 and e5.code == Err.LIMIT, "maxBytes")
|
||||
local _, e6 = UnixCompress.compress("x", nil, nil, { maxBits = 17 })
|
||||
t:check(e6 and e6.code == Err.ARG, "maxBits 17")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
if #c.z < 4000 and #c.z > 3 then
|
||||
table.insert(samples, c.z)
|
||||
end
|
||||
end
|
||||
table.insert(samples, string.sub(Fx.cases[#Fx.cases].z, 1, 3000))
|
||||
t:fuzz("decompress", function(b: buffer)
|
||||
return UnixCompress.decompress(b, nil, nil, { maxBytes = 2 ^ 22 })
|
||||
end, samples, { mutations = 40, keepHeader = 3 })
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user