formats-luau foundation: build (darklua + token passes) for Roblox and LuaJIT, compat prelude, core, Types, host adapters, API + registry, lint, tests on both runtimes; absorb deflate (codecs/Deflate + new compressor) and png (image/png + new encoder)
This commit is contained in:
commit
cdcb5b0b77
226 files changed
+8409
No files matched your search
@@ -0,0 +1,178 @@
|
||||
-- core/Bits and core/Huffman tests: LSB / MSB readers and writers round-trip random fields of 1..32 bits, align /
|
||||
-- peek / bytePos, overrun is a sticky error; canonical codes, length-limited code lengths, LSB and MSB decoding tables
|
||||
-- (with subtables) decode what the writers wrote; invalid code sets are rejected.
|
||||
local T = require("@tests/T")
|
||||
local Bits = require("@formats/core/Bits")
|
||||
local Huffman = require("@formats/core/Huffman")
|
||||
local Err = require("@formats/core/Err")
|
||||
|
||||
local t = T.suite("bits")
|
||||
local rand = T.rng(7)
|
||||
|
||||
-- round trips
|
||||
for _, kind in { "lsb", "msb" } do
|
||||
local w = if kind == "lsb" then Bits.lsbWriter() else Bits.msbWriter()
|
||||
local fields = {}
|
||||
for i = 1, 3000 do
|
||||
local n = 1 + rand(32)
|
||||
local v = if n == 32 then rand(65536) * 65536 + rand(65536) else rand(2 ^ n)
|
||||
fields[i] = { v, n }
|
||||
w:write(v, n)
|
||||
if i % 500 == 0 then
|
||||
w:align()
|
||||
fields[i][3] = true
|
||||
end
|
||||
end
|
||||
local buf = w:finish()
|
||||
local r = if kind == "lsb" then Bits.lsbReader(buf) else Bits.msbReader(buf)
|
||||
local ok = true
|
||||
for i, f in fields do
|
||||
if r:peek(math.min(f[2], 24)) ~= f[1] % 2 ^ math.min(f[2], 24) and kind == "lsb" then
|
||||
ok = false
|
||||
end
|
||||
local v = r:read(f[2])
|
||||
if v ~= f[1] then
|
||||
ok = false
|
||||
print(" mismatch", kind, i, v, f[1], f[2])
|
||||
break
|
||||
end
|
||||
if f[3] then
|
||||
r:align()
|
||||
end
|
||||
end
|
||||
t:check(ok, kind .. ": 3000 random fields round trip")
|
||||
t:check(r.err == nil, kind .. ": no error")
|
||||
r:read(16)
|
||||
t:check(r.err and r.err.code == Err.TRUNCATED, kind .. ": overrun is TRUNCATED")
|
||||
end
|
||||
|
||||
-- known bit orders
|
||||
do
|
||||
local lsb = Bits.lsbReader(T.bytes({ 0xB4 })) -- 1011 0100
|
||||
t:eq(lsb:read(3), 4, "lsb: first field is the low bits")
|
||||
t:eq(lsb:read(5), 0x16, "lsb: rest")
|
||||
local msb = Bits.msbReader(T.bytes({ 0xB4 }))
|
||||
t:eq(msb:read(3), 5, "msb: first field is the high bits")
|
||||
t:eq(msb:read(5), 0x14, "msb: rest")
|
||||
local w = Bits.msbWriter()
|
||||
w:write(5, 3)
|
||||
w:write(0x14, 5)
|
||||
w:write(1, 1)
|
||||
t:eq(T.hex(w:finish()), "b4 80", "msb writer pads with zeros")
|
||||
local r = Bits.lsbReader(T.bytes({ 0xFF, 0x12, 0x34 }))
|
||||
r:read(3)
|
||||
t:eq(r:bytePos(), 1, "bytePos after a partial byte")
|
||||
r:align()
|
||||
t:eq(r:read(8), 0x12, "align then read a byte")
|
||||
t:eq(r:bitsLeft(), 8, "bitsLeft")
|
||||
local e = Bits.msbReader(buffer.create(0))
|
||||
t:eq(e:peek(8), 0, "peek past the end is zeros")
|
||||
t:check(e.err == nil, "peek past the end is not an error")
|
||||
end
|
||||
|
||||
-- Huffman: canonical codes (RFC 1951 3.2.2 example)
|
||||
do
|
||||
local codes = Huffman.codes({ 3, 3, 3, 3, 3, 2, 4, 4 })
|
||||
local want = { 2, 3, 4, 5, 6, 0, 14, 15 }
|
||||
local ok = true
|
||||
for i = 1, 8 do
|
||||
ok = ok and codes[i] == want[i]
|
||||
end
|
||||
t:check(ok, "canonical codes (RFC 1951 example)")
|
||||
t:eq(Huffman.reverse(0x91, 8), 0x89, "reverse")
|
||||
t:eq(Huffman.reverse(1, 3), 4, "reverse 3 bits")
|
||||
end
|
||||
|
||||
-- lengths: limits, Kraft equality, single symbol
|
||||
local function kraft(lens: { number }): number
|
||||
local s = 0
|
||||
for _, l in lens do
|
||||
if l > 0 then
|
||||
s += 2 ^ -l
|
||||
end
|
||||
end
|
||||
return s
|
||||
end
|
||||
do
|
||||
local freqs = {}
|
||||
for i = 1, 40 do
|
||||
freqs[i] = math.floor(1.6 ^ i) -- Fibonacci-like: unlimited depth would be ~39
|
||||
end
|
||||
local lens = Huffman.lengths(freqs, 15)
|
||||
local maxl = 0
|
||||
for _, l in lens do
|
||||
maxl = math.max(maxl, l)
|
||||
end
|
||||
t:check(maxl <= 15, "length limit 15 (got " .. maxl .. ")")
|
||||
t:check(math.abs(kraft(lens) - 1) < 1e-12, "complete code")
|
||||
local one = Huffman.lengths({ 0, 5, 0 }, 15)
|
||||
t:check(one[1] == 0 and one[2] == 1 and one[3] == 0, "a single symbol gets length 1")
|
||||
local none = Huffman.lengths({ 0, 0 }, 15)
|
||||
t:check(none[1] == 0 and none[2] == 0, "no symbols")
|
||||
local u = Huffman.lengths({ 1, 1, 1, 1 }, 15)
|
||||
t:check(u[1] == 2 and u[4] == 2, "uniform")
|
||||
end
|
||||
|
||||
-- decoding tables: random code sets, encode with the writer, decode through LSB and MSB tables
|
||||
for trial = 1, 30 do
|
||||
local nsym = 2 + rand(300)
|
||||
local freqs = {}
|
||||
for i = 1, nsym do
|
||||
freqs[i] = if rand(4) == 0 then 0 else 1 + rand(1000) * rand(50)
|
||||
end
|
||||
freqs[1] += 1
|
||||
freqs[2] += 1
|
||||
local maxLen = if trial % 2 == 0 then 15 else 16
|
||||
local lens = Huffman.lengths(freqs, maxLen)
|
||||
local codes = Huffman.codes(lens)
|
||||
local lensBuf = buffer.create(nsym)
|
||||
for i = 1, nsym do
|
||||
buffer.writeu8(lensBuf, i - 1, lens[i])
|
||||
end
|
||||
local P = 4 + rand(6)
|
||||
local msgs = {}
|
||||
for i = 1, 500 do
|
||||
local s
|
||||
repeat
|
||||
s = rand(nsym)
|
||||
until lens[s + 1] > 0
|
||||
msgs[i] = s
|
||||
end
|
||||
for _, kind in { "lsb", "msb" } do
|
||||
local tab = buffer.create(Huffman.capacity(nsym, P, maxLen) * 4)
|
||||
local S = if kind == "lsb" then Huffman.buildLsb(lensBuf, 0, nsym, tab, P) else Huffman.buildMsb(lensBuf, 0, nsym, tab, P)
|
||||
t:check(S >= 0, kind .. ": table built")
|
||||
local w = if kind == "lsb" then Bits.lsbWriter() else Bits.msbWriter()
|
||||
for _, s in msgs do
|
||||
local l = lens[s + 1]
|
||||
w:write(if kind == "lsb" then Huffman.reverse(codes[s + 1], l) else codes[s + 1], l)
|
||||
end
|
||||
local buf = w:finish()
|
||||
local r = if kind == "lsb" then Bits.lsbReader(buf) else Bits.msbReader(buf)
|
||||
local ok = true
|
||||
for _, s in msgs do
|
||||
local got = if kind == "lsb" then Huffman.decodeLsb(r, tab, P, S) else Huffman.decodeMsb(r, tab, P, S)
|
||||
if got ~= s then
|
||||
ok = false
|
||||
break
|
||||
end
|
||||
end
|
||||
t:check(ok, string.format("%s trial %d: %d symbols, P=%d, decode", kind, trial, nsym, P))
|
||||
end
|
||||
end
|
||||
|
||||
-- invalid code sets
|
||||
do
|
||||
local over = T.bytes({ 1, 1, 1 }) -- three 1-bit codes
|
||||
t:eq(Huffman.buildLsb(over, 0, 3, buffer.create(1024 * 4), 8), -1, "over-subscribed")
|
||||
local tooLong = T.bytes({ 17, 1 })
|
||||
t:eq(Huffman.buildMsb(tooLong, 0, 2, buffer.create(1024 * 4), 8), -1, "length over 16")
|
||||
local incomplete = T.bytes({ 2, 0, 0 }) -- one 2-bit code: decoding the missing ones fails
|
||||
local tab = buffer.create(256 * 4)
|
||||
local S = Huffman.buildMsb(incomplete, 0, 3, tab, 4)
|
||||
t:check(S >= 0, "incomplete code set builds")
|
||||
local r = Bits.msbReader(T.bytes({ 0xC0 }))
|
||||
t:eq(Huffman.decodeMsb(r, tab, 4, S), -1, "missing code decodes to -1")
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,184 @@
|
||||
-- core/Bytes, core/Err, core/Crc32, core/Adler32, core/Input tests: every Reader / Writer method both endians, the
|
||||
-- sticky TRUNCATED error (never a throw), sub-readers without copies, Writer growth and patching.
|
||||
local T = require("@tests/T")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Adler32 = require("@formats/core/Adler32")
|
||||
local Input = require("@formats/core/Input")
|
||||
|
||||
local t = T.suite("bytes")
|
||||
|
||||
-- Writer -> Reader round trip of every type
|
||||
do
|
||||
local w = Bytes.writer(4) -- grows
|
||||
w:u8(0xAB)
|
||||
w:u16le(0x1234)
|
||||
w:u16be(0x1234)
|
||||
w:u24le(0x123456)
|
||||
w:u24be(0x123456)
|
||||
w:u32le(0xDEADBEEF)
|
||||
w:u32be(0xDEADBEEF)
|
||||
w:i8(-2)
|
||||
w:i16le(-300)
|
||||
w:i16be(-300)
|
||||
w:i32le(-70000)
|
||||
w:i32be(-70000)
|
||||
w:u64le(2 ^ 40 + 7)
|
||||
w:u64be(2 ^ 52 + 3)
|
||||
w:i64le(-5)
|
||||
w:f32le(1.5)
|
||||
w:f32be(-2.25)
|
||||
w:f64le(math.pi)
|
||||
w:f64be(-math.pi)
|
||||
w:u16le(0x3C00) -- f16 1.0
|
||||
w:u16be(0xC000) -- f16 -2.0
|
||||
w:string("abc")
|
||||
w:cstring("zero")
|
||||
w:string("fix\0\0\0")
|
||||
w:fill(7, 3)
|
||||
local mark = w:tell()
|
||||
w:u32be(0)
|
||||
w:setU32be(mark, 0xCAFEBABE)
|
||||
local buf = w:finish()
|
||||
t:eq(buffer.len(buf), w:tell(), "finish is exactly the written bytes")
|
||||
local r = Bytes.reader(buf)
|
||||
t:eq(r:u8(), 0xAB, "u8")
|
||||
t:eq(r:u16le(), 0x1234, "u16le")
|
||||
t:eq(r:u16be(), 0x1234, "u16be")
|
||||
t:eq(r:u24le(), 0x123456, "u24le")
|
||||
t:eq(r:u24be(), 0x123456, "u24be")
|
||||
t:eq(r:u32le(), 0xDEADBEEF, "u32le")
|
||||
t:eq(r:u32be(), 0xDEADBEEF, "u32be")
|
||||
t:eq(r:i8(), -2, "i8")
|
||||
t:eq(r:i16le(), -300, "i16le")
|
||||
t:eq(r:i16be(), -300, "i16be")
|
||||
t:eq(r:i32le(), -70000, "i32le")
|
||||
t:eq(r:i32be(), -70000, "i32be")
|
||||
t:eq(r:u64le(), 2 ^ 40 + 7, "u64le")
|
||||
t:eq(r:u64be(), 2 ^ 52 + 3, "u64be")
|
||||
t:eq(r:i64le(), -5, "i64le")
|
||||
t:eq(r:f32le(), 1.5, "f32le")
|
||||
t:eq(r:f32be(), -2.25, "f32be")
|
||||
t:eq(r:f64le(), math.pi, "f64le")
|
||||
t:eq(r:f64be(), -math.pi, "f64be")
|
||||
t:eq(r:f16le(), 1, "f16le")
|
||||
t:eq(r:f16be(), -2, "f16be")
|
||||
t:eq(r:string(3), "abc", "string")
|
||||
t:eq(r:cstring(), "zero", "cstring")
|
||||
t:eq(r:fixed(6), "fix", "fixed")
|
||||
t:eq(r:u8() + r:u8() + r:u8(), 21, "fill")
|
||||
t:eq(r:u32be(), 0xCAFEBABE, "patched field")
|
||||
t:check(r:eof() and r.err == nil, "consumed everything, no error")
|
||||
t:eq(Bytes.half(0x7C00), math.huge, "f16 infinity")
|
||||
t:check(Bytes.half(0x7E00) ~= Bytes.half(0x7E00), "f16 NaN")
|
||||
t:eq(Bytes.half(0x0001), 2 ^ -24, "f16 subnormal")
|
||||
end
|
||||
|
||||
-- sticky errors: reads past the end return 0 / "" and set err once, never throw
|
||||
do
|
||||
local r = Bytes.reader(buffer.fromstring("\1\2\3"))
|
||||
t:eq(r:u16le(), 0x0201, "read within")
|
||||
t:eq(r:u32le(), 0, "short read returns 0")
|
||||
t:check(r.err and r.err.code == Err.TRUNCATED, "TRUNCATED")
|
||||
t:eq(r.err.offset, 2, "error offset is where the read started")
|
||||
local first = r.err
|
||||
t:eq(r:u8(), 0, "reads after the error return 0")
|
||||
t:eq(r:string(5), "", "string after the error")
|
||||
t:check(r.err == first, "the first error sticks")
|
||||
t:check(r:eof(), "cursor at the end")
|
||||
local r2 = Bytes.reader(buffer.fromstring("abc"))
|
||||
t:check(not r2:seek(4) and r2.err ~= nil, "seek past the end fails")
|
||||
local r3 = Bytes.reader(buffer.fromstring("abc"))
|
||||
t:eq(r3:cstring(), "abc", "unterminated cstring returns what there is")
|
||||
t:check(r3.err and r3.err.code == Err.TRUNCATED, "unterminated cstring is TRUNCATED")
|
||||
local r4 = Bytes.reader(buffer.fromstring("abcd"))
|
||||
local nilv, e = r4:fail(Err.CORRUPT, "bad thing")
|
||||
t:check(nilv == nil and e.code == Err.CORRUPT and tostring(e) == "CORRUPT at 0x0: bad thing", "fail(): " .. tostring(e))
|
||||
t:check(not r4:need(5) and r4.err == e, "need keeps the first error")
|
||||
-- every method on an empty reader: no throw
|
||||
local empty = Bytes.reader(buffer.create(0))
|
||||
for _, m in { "u8", "i8", "u16le", "u16be", "i16le", "i16be", "u24le", "u24be", "u32le", "u32be", "i32le", "i32be", "u64le", "u64be", "i64le", "i64be", "f16le", "f16be", "f32le", "f32be", "f64le", "f64be", "cstring" } do
|
||||
local ok, v = pcall((Bytes.Reader :: any)[m], empty)
|
||||
t:check(ok and (v == 0 or v == ""), m .. " on an empty reader")
|
||||
end
|
||||
local ok = pcall(function()
|
||||
empty:string(-1)
|
||||
empty:bytes(10)
|
||||
empty:slice(10)
|
||||
empty:skip(3)
|
||||
empty:align(4)
|
||||
end)
|
||||
t:check(ok, "string / bytes / slice / skip / align on an empty reader")
|
||||
end
|
||||
|
||||
-- ranges, sub-readers, peek, match
|
||||
do
|
||||
local buf = buffer.fromstring("0123456789")
|
||||
local r = Bytes.reader(buf, 2, 5) -- "23456"
|
||||
t:eq(r:size(), 5, "size")
|
||||
t:eq(r:peek(3), "234", "peek")
|
||||
t:eq(r:peekU8(4), string.byte("6"), "peekU8")
|
||||
t:eq(r:peekU8(5), -1, "peekU8 past the end")
|
||||
t:check(r:match("23") and r:tell() == 2, "match consumes")
|
||||
t:check(not r:match("xx") and r:tell() == 2, "no match consumes nothing")
|
||||
local s = r:slice(2) -- "45"
|
||||
t:check(s.buf == buf and s:size() == 2 and r:tell() == 4, "slice: no copy, parent skips")
|
||||
t:eq(s:string(2), "45", "slice contents")
|
||||
t:eq(s:u8(), 0, "slice is bounded")
|
||||
local sub = r:sub(1, 3) -- "345"
|
||||
t:eq(sub:string(3), "345", "sub")
|
||||
t:eq(r:tell(), 4, "sub does not move the parent")
|
||||
local clamped = Bytes.reader(buf, 8, 100)
|
||||
t:eq(clamped:size(), 2, "reader length clamped")
|
||||
local neg = Bytes.reader(buf, -5)
|
||||
t:eq(neg:size(), 10, "negative offset clamped")
|
||||
local copy = Bytes.reader(buf, 1, 3):bytes(3)
|
||||
t:eq(buffer.tostring(copy), "123", "bytes copies")
|
||||
local a = Bytes.reader(buf)
|
||||
a:skip(3)
|
||||
t:check(a:align(4) and a:tell() == 4, "align")
|
||||
end
|
||||
|
||||
-- checksums
|
||||
do
|
||||
t:eq(Crc32.string("123456789"), 0xCBF43926, "crc32 check value")
|
||||
t:eq(Crc32.of(buffer.fromstring("xx123456789yy"), 2, 9), 0xCBF43926, "crc32 range")
|
||||
t:eq(Crc32.of(buffer.fromstring("ab"), 5, 9), 0, "crc32 clamped range")
|
||||
t:eq(Crc32.string("56789", Crc32.string("1234")), 0xCBF43926, "crc32 continuation")
|
||||
t:eq(Adler32.string("Wikipedia"), 0x11E60398, "adler32")
|
||||
t:eq(Adler32.string("pedia", Adler32.string("Wiki")), 0x11E60398, "adler32 continuation")
|
||||
end
|
||||
|
||||
-- Input (without a host)
|
||||
do
|
||||
local b = buffer.create(3)
|
||||
t:check(Input.resolve(b) == b, "a buffer passes through")
|
||||
local fromStr = Input.resolve("abc")
|
||||
t:eq(buffer.tostring(fromStr :: buffer), "abc", "a string is data without a host")
|
||||
local _, e = Input.resolve(42)
|
||||
t:check(e and e.code == Err.ARG, "a number is Err.ARG")
|
||||
t:eq(Input.extension("a/b.PNG"), "png", "extension")
|
||||
t:eq(Input.extension("noext"), nil, "no extension")
|
||||
t:check(Input.looksLikePath("a/b.png") and not Input.looksLikePath("a\0b") and not Input.looksLikePath("x\ny"), "looksLikePath")
|
||||
local fake = { hasFiles = true, exists = function(p: string): boolean
|
||||
return p == "img.png"
|
||||
end, readFile = function(_p: string): (buffer?, any)
|
||||
return buffer.fromstring("PNGDATA"), nil
|
||||
end }
|
||||
local fb, _, name = Input.resolve("img.png", nil, fake :: any)
|
||||
t:check(fb and buffer.tostring(fb) == "PNGDATA" and name == "img.png", "a path through the host")
|
||||
local db = Input.resolve("img.png", { data = true }, fake :: any)
|
||||
t:eq(buffer.tostring(db :: buffer), "img.png", "opts.data forces data")
|
||||
end
|
||||
|
||||
-- Err
|
||||
do
|
||||
local e = Err.new(Err.CORRUPT, "bad", 16)
|
||||
t:check(Err.is(e) and not Err.is({}) and not Err.is("x"), "Err.is")
|
||||
t:eq(tostring(Err.wrap(e, "png")), "png: CORRUPT at 0x10: bad", "tostring with format and offset")
|
||||
local w = Err.wrap("plain message", "gif")
|
||||
t:check(w.code == Err.CORRUPT and w.format == "gif", "wrap a string")
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user