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:
4DBug committed 2026-10-31 08:18:01 -05:00
commit cdcb5b0b77
226 files changed
+8409

No files matched your search

+127
View File
@@ -0,0 +1,127 @@
--!strict
-- tests/T: the test helper every tests/**/*.luau uses, under both runtimes (luau --codegen -O2 and LuaJIT through the
-- build). A failed check throws, so the runner (tools/test.sh) sees a non-zero exit.
--
-- local T = require("@tests/T")
-- local t = T.suite("png")
-- t:check(cond, "label") -- truthy
-- t:eq(got, want, "label") -- ==, with both values in the message
-- t:fuzz("decode", function(b) return Png.decode(Bytes.reader(b)) end, samples) -- the no-throw pass
-- t:done() -- prints "png: 123 checks passed (luajit)"
local T = {}
T.runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
-- deterministic LCG (the same numbers on both runtimes): rand(n) -> 0 .. n - 1
function T.rng(seed: number): (n: number) -> number
local s = seed % 2147483648
return function(n: number): number
s = (s * 1103515245 + 12345) % 2147483648
return s % n
end
end
-- bytes({ 1, 2, 3 }) -> buffer
function T.bytes(t: { number }): buffer
local b = buffer.create(#t)
for i, v in t do
buffer.writeu8(b, i - 1, v)
end
return b
end
-- hex(buf, max?) -> "89 50 4e 47 ..."
function T.hex(b: buffer, max: number?): string
local n = math.min(buffer.len(b), max or 64)
local out = table.create(n)
for i = 0, n - 1 do
out[i + 1] = string.format("%02x", buffer.readu8(b, i))
end
return table.concat(out, " ") .. (if buffer.len(b) > n then " ..." else "")
end
-- clock() -> seconds (os.clock on both runtimes)
function T.clock(): number
return os.clock()
end
local Suite = {}
Suite.__index = Suite
export type Suite = typeof(setmetatable({} :: { name: string, passed: number, t0: number }, Suite))
function T.suite(name: string): Suite
return setmetatable({ name = name, passed = 0, t0 = os.clock() }, Suite)
end
function Suite.check(self: Suite, cond: any, label: string)
if not cond then
error(string.format("FAILED [%s, %s]: %s", self.name, T.runtime, label), 2)
end
self.passed += 1
end
function Suite.eq(self: Suite, got: any, want: any, label: string)
if got ~= want then
error(string.format("FAILED [%s, %s]: %s: got %s, want %s", self.name, T.runtime, label, tostring(got), tostring(want)), 2)
end
self.passed += 1
end
-- note(fmt, ...): an indented progress line
function Suite.note(_self: Suite, fmt: string, ...: any)
print(" " .. string.format(fmt, ...))
end
-- fuzz(label, decode, samples, opts?): the no-throw pass every format test runs. For each sample (buffer or string):
-- truncations at several cuts, opts.mutations (default 8) copies with 1..4 random bytes changed, plus
-- opts.garbage (default 200) random buffers. decode(buf) may return anything except throw. Returns the counts.
function Suite.fuzz(self: Suite, label: string, decode: (buffer) -> any, samples: { any }, opts: { mutations: number?, garbage: number?, seed: number?, keepHeader: number? }?): (number, number)
local o: { mutations: number?, garbage: number?, seed: number?, keepHeader: number? } = opts or {}
local rand = T.rng(o.seed or 1234)
local tries, threw = 0, 0
local function try(b: buffer, what: string)
tries += 1
local ok, err = pcall(decode, b)
if not ok then
threw += 1
print(string.format(" THREW on %s (%s): %s", what, label, tostring(err)))
end
end
local keep = o.keepHeader or 0
for _, s in samples do
local str = if type(s) == "string" then s else buffer.tostring(s)
local n = #str
for _, cut in { 1, 2, 4, 8, 16, 33, n // 3, n // 2, n - 13, n - 4, n - 1 } do
if cut > 0 and cut < n then
try(buffer.fromstring(string.sub(str, 1, cut)), "truncation")
end
end
if n > keep then
for _ = 1, o.mutations or 8 do
local b = buffer.fromstring(str)
for _ = 1, 1 + rand(4) do
buffer.writeu8(b, keep + rand(n - keep), rand(256))
end
try(b, "mutation")
end
end
end
for _ = 1, o.garbage or 200 do
local n = rand(300)
local b = buffer.create(n)
for k = 0, n - 1 do
buffer.writeu8(b, k, rand(256))
end
try(b, "garbage")
end
self:check(threw == 0, label .. ": bad input never throws (" .. threw .. " of " .. tries .. " threw)")
self:note("%s: %d bad inputs, none threw", label, tries)
return tries, threw
end
function Suite.done(self: Suite)
print(string.format("%s: %d checks passed (%s, %.2f s)", self.name, self.passed, T.runtime, os.clock() - self.t0))
end
return T
+132
View File
@@ -0,0 +1,132 @@
-- API tests (src/Formats/init.luau): load / save / saveString / convert / open / detect / formats / format, the
-- category facades, sniff-then-extension detection, errors as Err values (never a throw), and file paths on hosts with
-- a file system (LuaJIT: a temporary file; the luau CLI has none).
local T = require("@tests/T")
local F = require("@formats")
local Err = require("@formats/core/Err")
local t = T.suite("api")
local function image(w: number, h: number): any
local img = F.Types.Image.new(w, h)
for i = 0, w * h * 4 - 1 do
buffer.writeu8(img.pixels, i, (i * 37) % 256)
end
return img
end
-- host
t:eq(F.host.name, if T.runtime == "luajit" then "luajit" else "luau", "host detected")
-- formats()
do
local all = F.formats()
t:check(#all >= 1, "formats() lists the registry")
local png
for _, f in all do
if f.name == "png" then
png = f
end
end
t:check(png and png.category == "image" and png.caps.encode and table.find(png.extensions, "png"), "png in formats()")
t:eq(#F.image.formats(), #F.formats("image"), "facade formats()")
t:eq(#F.formats("nonexistent"), 0, "unknown category")
t:check(F.format("png") and F.format("png").name == "png", "format(name)")
t:check(F.format("apng") and F.format("apng").name == "png", "format(extension)")
local _, e = F.format("nope")
t:check(e and e.code == Err.UNKNOWN_FORMAT, "format(unknown)")
end
-- save -> load round trip through buffers and strings
do
local img = image(7, 5)
local buf, err = F.save(img, nil, { format = "png" })
t:check(buf and type(buf) == "buffer", "save to a buffer: " .. tostring(err))
local back = F.load(buf)
t:check(back and back.kind == "image" and back.width == 7 and back.height == 5, "load sniffs png")
t:eq(buffer.tostring(back.pixels), buffer.tostring(img.pixels), "pixels round trip")
t:eq(back.meta.source.format, "png", "meta.source.format")
-- save without a format: the source format
local again = F.save(back)
t:check(again and type(again) == "buffer", "save uses the source format")
local s = F.saveString(back)
t:check(type(s) == "string" and string.sub(s, 2, 4) == "PNG", "saveString")
local fromString = F.load(s)
t:check(fromString and fromString.width == 7, "load a string of bytes")
t:check(F.image.load(buf) ~= nil, "F.image.load")
t:eq(F.detect(buf), "png", "detect")
local conv = F.convert(s, "png", { level = 1 })
t:check(conv and F.load(conv).width == 7, "convert")
-- format options reach the encoder
local stored = F.save(img, nil, { format = "png", level = 0 }) :: buffer
t:check(buffer.len(stored) > buffer.len(buf :: buffer), "opts.level reaches the encoder")
end
-- detection: sniff beats the extension; the extension is the fallback; category restrictions
do
local png = F.save(image(2, 2), nil, { format = "png" }) :: buffer
t:eq(F.detect(png, { name = "photo.jpg" }), "png", "content beats a wrong extension")
local _, e = F.load("not an image at all")
t:check(e and e.code == Err.UNKNOWN_FORMAT, "unknown data: " .. tostring(e))
local _, e2 = F.load(png, { category = "audio" })
t:check(e2 and e2.code == Err.UNKNOWN_FORMAT, "png is not audio")
local _, e3 = F.audio.load(png)
t:check(e3 and e3.code == Err.UNKNOWN_FORMAT, "facade restricts the category")
-- forced format on bad data: the format's error, tagged with its name
local _, e4 = F.load("garbage!", { format = "png" })
t:check(e4 and e4.format == "png" and e4.code == Err.CORRUPT, "forced format error: " .. tostring(e4))
t:check(string.find(tostring(e4), "^png: ") ~= nil, "error message names the format")
local _, e5 = F.load("x", { format = "nope" })
t:check(e5 and e5.code == Err.UNKNOWN_FORMAT, "unknown forced format")
end
-- errors are values, never throws
do
local ok, v, e = pcall(F.load, 42 :: any)
t:check(ok and v == nil and e.code == Err.ARG, "load(42)")
local ok2, v2, e2 = pcall(F.save, image(1, 1))
t:check(ok2 and v2 == nil and e2.code == Err.ARG, "save without a format")
local ok3, _, e3 = pcall(F.save, { kind = "image" } :: any, nil, { format = "png" })
t:check(ok3 and e3 and e3.code == Err.ARG, "save of a broken image")
local ok4, _, e4 = pcall(F.save, image(1, 1), nil, { format = "nope" })
t:check(ok4 and e4.code == Err.UNKNOWN_FORMAT, "save to an unknown format")
local _, e5 = F.open(F.save(image(1, 1), nil, { format = "png" }))
t:check(e5 and e5.code == Err.UNSUPPORTED, "open() of a non-streaming format")
t:fuzz("F.load", function(b: buffer)
return F.load(b)
end, { F.saveString(image(9, 4), { format = "png" }) :: string }, { mutations = 20 })
end
-- files (hosts with a file system)
if F.host.hasFiles then
local path = os.tmpname() .. ".png"
local img = image(3, 3)
local ok, err = F.save(img, path)
t:check(ok == true, "save to a path (format from the extension): " .. tostring(err))
local back = F.load(path)
t:check(back and buffer.tostring(back.pixels) == buffer.tostring(img.pixels), "load from a path")
t:check(F.image.load(path) ~= nil, "facade load from a path")
local _, e = F.load("/nonexistent/dir/x.png", { path = true })
t:check(e and e.code == Err.IO, "missing file is Err.IO")
os.remove(path)
else
local _, e = F.save(image(1, 1), "out.png")
t:check(e and e.code == Err.IO, "no file system: save to a path is Err.IO")
local _, e2 = F.load("x.png", { path = true })
t:check(e2 and e2.code == Err.IO, "no file system: load of a path is Err.IO")
end
-- Types constructors
do
local T2 = F.Types
t:eq(buffer.len(T2.Image.new(3, 2, "rgb8").pixels), 18, "Image.new rgb8")
t:eq(buffer.len(T2.Image.new(3, 3, "yuv420p").pixels), 9 + 2 * 4, "Image.new yuv420p")
local a = T2.Audio.new(8000, 2, 100)
t:check(a.kind == "audio" and buffer.len(a.samples) == 800, "Audio.new")
local sc = T2.Scene.fromPrimitive({ attributes = {}, mode = "triangles" })
t:check(sc.kind == "scene" and #sc.nodes == 1 and sc.nodes[1].mesh == 1, "Scene.fromPrimitive")
t:eq(tostring(F.null), "null", "null sentinel")
t:check(F.data.null == F.null, "F.data.null")
end
t:done()
+172
View File
@@ -0,0 +1,172 @@
-- codecs/Deflate tests: every fixture case (Python zlib, levels 0-9, every strategy, small windows, flushes, gzip
-- headers, multi-member gzip) decodes; exact-size mode; checksum vectors; the compressor round-trips every corpus at
-- every level (and its zlib / gzip framing); truncated / corrupted / random input returns nil, Err and never throws.
-- Ported from deflate-luau tests/inflate.luau. Fixtures: tools/fixtures/deflate.py.
local T = require("@tests/T")
local Deflate = require("@formats/codecs/Deflate")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/deflate")
local t = T.suite("deflate")
local function decode(kind: string, b: buffer, ...: any): (buffer?, any)
if kind == "raw" then
return Deflate.inflate(b, ...)
elseif kind == "zlib" then
return Deflate.zlib(b, ...)
else
return Deflate.gunzip(b, ...)
end
end
-- checksum vectors
t:eq(Deflate.crc32("123456789"), 0xCBF43926, "crc32 check value")
t:eq(Deflate.crc32(""), 0, "crc32 empty")
t:eq(Deflate.adler32("Wikipedia"), 0x11E60398, "adler32 Wikipedia")
t:eq(Deflate.adler32(""), 1, "adler32 empty")
do
local s = string.rep("The quick brown fox jumps over the lazy dog. ", 400)
local b = buffer.fromstring(s)
local whole = Deflate.crc32(b)
local part = Deflate.crc32(b, 0, 1001)
t:eq(Deflate.crc32(b, 1001, nil, part), whole, "crc32 continuation")
local a = Deflate.adler32(b, 0, 7000)
t:eq(Deflate.adler32(b, 7000, nil, a), Deflate.adler32(b), "adler32 continuation")
t:eq(Deflate.crc32(b, 3, 17), Deflate.crc32(string.sub(s, 4, 20)), "crc32 range")
end
-- every case decodes to its corpus
local nbytes = 0
for _, c in Fx.cases do
local want = Fx.corpora[c.corpus]
local b = buffer.fromstring(c.data)
local out, err = decode(c.kind, b)
t:check(out ~= nil, c.name .. ": " .. tostring(err))
t:check(buffer.tostring(out :: buffer) == want, c.name .. ": output differs")
nbytes += #want
if c.kind == "raw" then
t:eq(err, #c.data, c.name .. ": consumed")
-- exact size, and a wrong exact size
local o2 = Deflate.inflate(b, 0, nil, #want)
t:check(o2 and buffer.tostring(o2) == want, c.name .. ": exact size")
if #want > 0 then
t:check(Deflate.inflate(b, 0, nil, #want - 1) == nil, c.name .. ": too small exact size fails")
end
t:check(Deflate.inflate(b, 0, nil, #want + 1) == nil, c.name .. ": too large exact size fails")
-- the same stream inside a bigger buffer, at an offset, with trailing bytes
local big = buffer.create(#c.data + 13)
buffer.fill(big, 0, 0xAB)
buffer.copy(big, 5, b)
local o3, e3 = Deflate.inflate(big, 5, #c.data + 8)
t:check(o3 and buffer.tostring(o3) == want and e3 == 5 + #c.data, c.name .. ": at an offset")
elseif c.kind == "zlib" then
t:eq(err, #c.data, c.name .. ": zlib consumed")
end
end
t:note("%d fixture streams, %.1f MB decoded", #Fx.cases, nbytes / 1e6)
-- strings are accepted
t:check(buffer.tostring((Deflate.zlib(Fx.cases[25].data))) == Fx.corpora[Fx.cases[25].corpus], "string input")
-- corrupted checksums
do
local z
for _, c in Fx.cases do
if c.kind == "zlib" and #Fx.corpora[c.corpus] > 100 then
z = c
break
end
end
local b = buffer.fromstring(z.data)
buffer.writeu8(b, buffer.len(b) - 1, bit32.bxor(buffer.readu8(b, buffer.len(b) - 1), 1))
local out, err = Deflate.zlib(b)
t:check(out == nil and Err.is(err) and err.code == Err.CHECKSUM, "zlib adler mismatch: " .. tostring(err))
for _, c in Fx.cases do
if c.kind == "gzip" and #Fx.corpora[c.corpus] > 100 then
local g = buffer.fromstring(c.data)
local n = buffer.len(g)
buffer.writeu8(g, n - 6, bit32.bxor(buffer.readu8(g, n - 6), 0x40))
local o, e = Deflate.gunzip(g)
t:check(o == nil and e and e.code == Err.CHECKSUM, c.name .. ": gzip crc mismatch: " .. tostring(e))
break
end
end
end
-- hand-made streams
do
local bytes = T.bytes
-- stored block "hi" then a fixed block with only end-of-block
local o = Deflate.inflate(bytes({ 0x00, 0x02, 0x00, 0xFD, 0xFF, 0x68, 0x69, 0x03, 0x00 }))
t:check(o and buffer.tostring(o) == "hi", "stored + fixed EOB")
t:check(Deflate.inflate(bytes({ 0x01, 0x02, 0x00, 0xFC, 0xFF, 0x68, 0x69 })) == nil, "stored NLEN mismatch")
t:check(Deflate.inflate(bytes({ 0x07 })) == nil, "block type 3")
local _, e0 = Deflate.inflate(bytes({}))
t:check(e0 and e0.code == Err.TRUNCATED, "empty input is TRUNCATED: " .. tostring(e0))
-- fixed block: a match before any output (distance too far back)
local _, e = Deflate.inflate(bytes({ 0x03, 0x02, 0x00 }))
t:check(e ~= nil and e.code == Err.CORRUPT, "distance too far back: " .. tostring(e))
t:check(Deflate.zlib(bytes({ 0x78, 0x9c })) == nil, "zlib truncated header")
t:check(Deflate.zlib(bytes({ 0x78, 0x9d, 0x03, 0, 0, 0, 0, 1 })) == nil, "zlib FCHECK")
t:check(Deflate.gunzip(bytes({ 0x1f, 0x8c, 8, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 })) == nil, "gzip magic")
local ok, r1, r2 = pcall(Deflate.inflate, 5 :: any)
t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG")
local ok2, _, r3 = pcall(Deflate.inflate, "abc", -1)
t:check(ok2 and r3.code == Err.ARG, "a bad offset returns Err.ARG")
end
-- the compressor: every corpus, every level, raw / zlib / gzip; ratio against Python zlib -6
do
local total, comp6, ref6 = 0, 0, 0
for name, data in Fx.corpora do
local b = buffer.fromstring(data)
for level = 0, 9 do
local z, err = Deflate.compress(b, { level = level })
t:check(z, name .. ": compress level " .. level .. ": " .. tostring(err))
local back, berr = Deflate.inflate(z :: buffer)
t:check(back and buffer.tostring(back) == data, string.format("%s: level %d round trip: %s", name, level, tostring(berr)))
if level == 6 then
comp6 += buffer.len(z :: buffer)
total += #data
end
end
local zl = Deflate.zlibCompress(data) :: buffer
local back = Deflate.zlib(zl)
t:check(back and buffer.tostring(back) == data, name .. ": zlibCompress round trip")
local gz = Deflate.gzip(data, { level = 9 }) :: buffer
local back2 = Deflate.gunzip(gz)
t:check(back2 and buffer.tostring(back2) == data, name .. ": gzip round trip")
end
for _, c in Fx.cases do
if c.name == c.corpus .. "/zlib6" then
ref6 += #c.data - 6
end
end
t:note("compress -6: %d -> %d bytes (%.1f%%); Python zlib -6: %d bytes", total, comp6, 100 * comp6 / total, ref6)
t:check(comp6 < ref6 * 1.25, "level 6 within 25% of zlib")
local _, e = Deflate.compress("x", { level = 11 })
t:check(e and e.code == Err.ARG, "bad level returns Err.ARG")
end
-- truncation, corruption and garbage never throw
do
local samples = {}
local kinds = {}
for i, c in Fx.cases do
if i % 9 == 0 and #c.data > 8 then
table.insert(samples, c.data)
kinds[c.data] = c.kind
end
end
t:fuzz("inflate", function(b: buffer)
return Deflate.inflate(b)
end, samples, { mutations = 6, garbage = 3000 })
t:fuzz("zlib", function(b: buffer)
return Deflate.zlib(b)
end, samples, { mutations = 3, garbage = 300 })
t:fuzz("gunzip", function(b: buffer)
return Deflate.gunzip(b)
end, samples, { mutations = 3, garbage = 300 })
end
t:done()
+275
View File
@@ -0,0 +1,275 @@
-- compat tests: the Luau semantics the library relies on, checked under both runtimes. Under luau these pin down what
-- Luau does; under LuaJIT they check compat/luajit.lua (bit32, buffer, table / math / string / utf8 additions, typeof)
-- and tools/build.py's lowering (generalized iteration, if-expression chains, continue, compound assignment, string
-- interpolation, floor division, number literals).
local T = require("@tests/T")
local t = T.suite("compat")
-- ------------------------------------------------------------------------------------------------ bit32
do
t:eq(bit32.band(0xFFFFFFFF, 0x0F0F0F0F), 0x0F0F0F0F, "band unsigned")
t:eq(bit32.band(-1), 0xFFFFFFFF, "band(-1)")
t:eq(bit32.band(), 0xFFFFFFFF, "band()")
t:eq(bit32.band(2 ^ 32 + 3, 7), 3, "band reduces mod 2^32")
t:eq(bit32.band(0xFF, 0xF0, 0x3C), 0x30, "band 3 args")
t:eq(bit32.band(0xFF, 0xF0, 0x3C, 0x20, 0x20), 0x20, "band 5 args")
t:eq(bit32.bor(0x80000000, 1), 0x80000001, "bor high bit")
t:eq(bit32.bor(), 0, "bor()")
t:eq(bit32.bor(1, 2, 4, 8, 16), 31, "bor 5 args")
t:eq(bit32.bxor(0xFFFFFFFF, 0x12345678), 0xEDCBA987, "bxor")
t:eq(bit32.bxor(1, 2, 4, 8, 16, 32, 64, 128, 256, 512), 1023, "bxor 10 args")
t:eq(bit32.bnot(0), 0xFFFFFFFF, "bnot")
t:eq(bit32.bnot(0xFFFFFFFF), 0, "bnot all")
t:eq(bit32.lshift(1, 31), 0x80000000, "lshift 31")
t:eq(bit32.lshift(1, 32), 0, "lshift 32")
t:eq(bit32.lshift(0xFFFFFFFF, 4), 0xFFFFFFF0, "lshift drops high bits")
t:eq(bit32.lshift(1, -1), 0, "lshift negative")
t:eq(bit32.lshift(8, -2), 2, "lshift negative = rshift")
t:eq(bit32.rshift(0x80000000, 31), 1, "rshift logical")
t:eq(bit32.rshift(0xFFFFFFFF, 0), 0xFFFFFFFF, "rshift 0 stays unsigned")
t:eq(bit32.rshift(2, -1), 4, "rshift negative = lshift")
t:eq(bit32.rshift(5, 40), 0, "rshift 40")
t:eq(bit32.arshift(0x80000000, 4), 0xF8000000, "arshift sign fill")
t:eq(bit32.arshift(0x80000000, 40), 0xFFFFFFFF, "arshift >= 32")
t:eq(bit32.lrotate(0x80000001, 1), 3, "lrotate")
t:eq(bit32.rrotate(3, 1), 0x80000001, "rrotate")
t:eq(bit32.extract(0xF0, 4, 4), 15, "extract")
t:eq(bit32.extract(0x80000000, 31), 1, "extract 1 bit")
t:eq(bit32.replace(0, 5, 4, 3), 80, "replace")
t:eq(bit32.countlz(1), 31, "countlz")
t:eq(bit32.countlz(0), 32, "countlz 0")
t:eq(bit32.countrz(8), 3, "countrz")
t:eq(bit32.countrz(0), 32, "countrz 0")
t:eq(bit32.byteswap(0x01020304), 0x04030201, "byteswap")
t:eq(bit32.byteswap(0x000000FF), 0xFF000000, "byteswap high")
t:check(bit32.btest(6, 2) and not bit32.btest(4, 2), "btest")
end
-- ------------------------------------------------------------------------------------------------ buffer
do
local b = buffer.create(16)
t:eq(buffer.len(b), 16, "len")
t:eq(buffer.readu32(b, 12), 0, "zero-filled")
buffer.writeu32(b, 0, 0xDEADBEEF)
t:eq(buffer.readu32(b, 0), 0xDEADBEEF, "u32 round trip")
t:eq(buffer.readu8(b, 0), 0xEF, "little-endian")
t:eq(buffer.readu16(b, 2), 0xDEAD, "u16")
t:eq(buffer.readi8(b, 3), -34, "i8 sign")
t:eq(buffer.readi16(b, 2), 0xDEAD - 65536, "i16 sign")
t:eq(buffer.readi32(b, 0), 0xDEADBEEF - 2 ^ 32, "i32 sign")
buffer.writeu8(b, 0, 300)
t:eq(buffer.readu8(b, 0), 44, "writeu8 wraps")
buffer.writeu8(b, 0, -1)
t:eq(buffer.readu8(b, 0), 255, "writeu8 -1")
buffer.writeu8(b, 0, 3.7)
t:eq(buffer.readu8(b, 0), 3, "writeu8 truncates")
buffer.writeu8(b, 0, 255 + 255)
t:eq(buffer.readu8(b, 0), 254, "writeu8 of a sum")
buffer.writeu32(b, 4, -1)
t:eq(buffer.readu32(b, 4), 0xFFFFFFFF, "writeu32 -1")
buffer.writeu32(b, 4, 2 ^ 32 + 5)
t:eq(buffer.readu32(b, 4), 5, "writeu32 wraps")
buffer.writei16(b, 8, 40000)
t:eq(buffer.readi16(b, 8), -25536, "writei16 wraps")
buffer.writeu16(b, 8, -2)
t:eq(buffer.readu16(b, 8), 65534, "writeu16 -2")
buffer.writei32(b, 8, -5)
t:eq(buffer.readi32(b, 8), -5, "i32 round trip")
buffer.writef32(b, 0, 1.5)
t:eq(buffer.readf32(b, 0), 1.5, "f32")
buffer.writef32(b, 0, 0.1)
t:check(math.abs(buffer.readf32(b, 0) - 0.1) < 1e-8 and buffer.readf32(b, 0) ~= 0.1, "f32 rounds")
buffer.writef64(b, 8, math.pi)
t:eq(buffer.readf64(b, 8), math.pi, "f64")
local nan = buffer.readf32(buffer.fromstring("\0\0\192\127"), 0)
t:check(nan ~= nan, "f32 NaN")
-- out of bounds throws (both runtimes), and nothing is written
t:check(not pcall(buffer.readu8, b, 16), "readu8 past the end throws")
t:check(not pcall(buffer.readu8, b, -1), "readu8 at -1 throws")
t:check(not pcall(buffer.readu32, b, 13), "readu32 overlapping the end throws")
t:check(not pcall(buffer.writeu16, b, 15, 0), "writeu16 overlapping the end throws")
t:check(not pcall(buffer.readstring, b, 10, 7), "readstring past the end throws")
t:check(not pcall(buffer.copy, b, 10, b, 0, 7), "copy past the end throws")
t:check(not pcall(buffer.fill, b, 10, 0, 7), "fill past the end throws")
-- strings, NULs, copy, fill
local s = buffer.fromstring("a\0b\255c")
t:eq(buffer.len(s), 5, "fromstring with NUL")
t:eq(buffer.tostring(s), "a\0b\255c", "tostring with NUL")
t:eq(buffer.readstring(s, 1, 3), "\0b\255", "readstring")
buffer.writestring(b, 0, "hello world")
t:eq(buffer.readstring(b, 0, 11), "hello world", "writestring")
buffer.writestring(b, 0, "HELLO", 2)
t:eq(buffer.readstring(b, 0, 5), "HEllo", "writestring count")
buffer.copy(b, 2, b, 0, 6) -- overlapping (memmove)
t:eq(buffer.readstring(b, 0, 8), "HEHEllo ", "copy overlapping")
local c = buffer.create(4)
buffer.copy(c, 1, s, 2)
t:eq(buffer.readstring(c, 1, 3), "b\255c", "copy default count")
buffer.fill(c, 0, 0x41)
t:eq(buffer.tostring(c), "AAAA", "fill to the end")
buffer.fill(c, 1, 0x142, 2)
t:eq(buffer.tostring(c), "ABBA", "fill value wraps")
t:eq(buffer.len(buffer.create(0)), 0, "empty buffer")
t:eq(buffer.tostring(buffer.create(0)), "", "empty tostring")
-- readbits / writebits
local bb = buffer.create(8)
buffer.writeu32(bb, 0, 0xDEADBEEF)
t:eq(buffer.readbits(bb, 4, 12), 3054, "readbits")
t:eq(buffer.readbits(bb, 0, 32), 0xDEADBEEF, "readbits 32")
t:eq(buffer.readbits(bb, 0, 0), 0, "readbits 0")
buffer.writebits(bb, 3, 7, 0x7F)
t:eq(buffer.readu32(bb, 0), 3735928831, "writebits")
buffer.writebits(bb, 30, 20, 0xABCDE)
t:eq(buffer.readbits(bb, 30, 20), 0xABCDE, "writebits across bytes")
t:check(not pcall(buffer.readbits, bb, 60, 5), "readbits past the end throws")
-- big buffers (LuaJIT: calloc'd memory)
local big = buffer.create(1000000)
buffer.writeu32(big, 999996, 0x01020304)
t:eq(buffer.readu32(big, 999996), 0x01020304, "big buffer")
t:eq(buffer.readu8(big, 500000), 0, "big buffer zero-filled")
end
-- ------------------------------------------------------------------------------------------------ typeof / type
do
local b = buffer.create(1)
t:eq(typeof(b), "buffer", "typeof buffer")
t:eq(type(b), "buffer", "type buffer")
t:eq(typeof({}), "table", "typeof table")
t:eq(type(1), "number", "type number")
t:eq(typeof(nil), "nil", "typeof nil")
t:eq(typeof("s"), "string", "typeof string")
end
-- ------------------------------------------------------------------------------------------------ table / math / string
do
local c = table.create(3, 7)
t:eq(#c, 3, "table.create size")
t:eq(c[3], 7, "table.create value")
t:eq(#table.create(5), 0, "table.create without value")
t:eq(table.find({ 5, 6, 7 }, 6), 2, "table.find")
t:eq(table.find({ 5, 6, 7 }, 8), nil, "table.find missing")
t:eq(table.find({ 5, 6, 5 }, 5, 2), 3, "table.find init")
local x = { 1, 2, a = 3 }
table.clear(x)
t:check(next(x) == nil, "table.clear")
local f = table.freeze({ 1 })
t:check(table.isfrozen(f) and not table.isfrozen({}), "freeze / isfrozen")
local cl = table.clone({ 1, 2, k = "v" })
t:check(cl[2] == 2 and cl.k == "v", "table.clone")
t:eq(table.unpack({ 1, 2, 3 }, 2, 3), 2, "table.unpack")
t:eq(select("#", table.unpack({ 1, 2, 3 })), 3, "table.unpack count")
local m = table.move({ 1, 2, 3 }, 1, 3, 2)
t:check(m[1] == 1 and m[2] == 1 and m[3] == 2 and m[4] == 3, "table.move overlapping")
t:eq(math.clamp(5, 0, 3), 3, "clamp high")
t:eq(math.clamp(-1, 0, 3), 0, "clamp low")
t:eq(math.sign(-3), -1, "sign")
t:eq(math.sign(0), 0, "sign 0")
t:eq(math.round(2.5), 3, "round half up")
t:eq(math.round(-2.5), -3, "round half away from zero")
t:eq(math.round(2.4), 2, "round down")
t:check(type(math.noise(0.5, 0.3, 0.1)) == "number", "noise is a number")
local parts = string.split("a,b,,c", ",")
t:check(#parts == 4 and parts[1] == "a" and parts[3] == "" and parts[4] == "c", "string.split")
t:eq(#string.split("", ","), 1, "split empty")
t:eq(#("x y z"):split(" "), 3, "split as a method")
t:eq(string.format("%5.1f|%d|%x|%s", 3.14159, 42, 255, "s"), " 3.1|42|ff|s", "string.format")
t:eq(math.floor(-3.5), -4, "floor")
end
-- ------------------------------------------------------------------------------------------------ utf8
do
t:eq(utf8.char(72, 228, 8364, 128512), "H\xC3\xA4\xE2\x82\xAC\xF0\x9F\x98\x80", "utf8.char")
t:eq(utf8.len("h\xC3\xA4ll\xE2\x82\xAC"), 5, "utf8.len")
local n, bad = utf8.len("a\xFFb")
t:check(n == nil and bad == 2, "utf8.len invalid")
t:eq(utf8.codepoint("\xC3\xA4"), 228, "utf8.codepoint")
t:eq(utf8.offset("h\xC3\xA4ll", 3), 4, "utf8.offset")
local cps = {}
for _, c in utf8.codes("a\xC3\xA4\xE2\x82\xAC") do
table.insert(cps, c)
end
t:check(#cps == 3 and cps[2] == 228 and cps[3] == 8364, "utf8.codes")
t:check(string.find("\xC3\xA4", utf8.charpattern) == 1, "utf8.charpattern")
end
-- ------------------------------------------------------------------------------------------------ syntax lowering
do
-- generalized iteration: arrays (in order), dictionaries, mixed, iterator functions, nested
local order = {}
for i, v in { 10, 20, 30 } do
table.insert(order, i * 100 + v)
end
t:check(order[1] == 110 and order[2] == 220 and order[3] == 330, "for over an array, in order")
local sum = 0
for k, v in { a = 1, b = 2, c = 3 } do
sum += v
t:check(type(k) == "string", "dict keys")
end
t:eq(sum, 6, "for over a dictionary")
local count = 0
for _ in { 1, 2, x = 3 } do
count += 1
end
t:eq(count, 3, "for over a mixed table visits every key")
local w = {}
for word in string.gmatch("a b c", "%a") do
table.insert(w, word)
end
t:eq(#w, 3, "for over an iterator function")
local pc = 0
for _, _ in pairs({ 1, 2 }) do
pc += 1
end
t:eq(pc, 2, "pairs untouched")
local empty = 0
for _ in {} do
empty += 1
end
t:eq(empty, 0, "for over an empty table")
-- if-expressions: chains of elseif (darklua 0.19 reverses them; build.py rewrites them)
local function grade(i: number): string
return if i <= 144 then "a" elseif i <= 256 then "b" elseif i <= 280 then "c" else "d"
end
t:check(grade(1) == "a" and grade(200) == "b" and grade(270) == "c" and grade(285) == "d", "if-expression elseif chain")
local function n3(i: number): number
return if i < 0 then -1 elseif i == 0 then 0 elseif i < 10 then 1 elseif i < 100 then 2 else 3
end
t:check(n3(-5) == -1 and n3(0) == 0 and n3(5) == 1 and n3(50) == 2 and n3(500) == 3, "numeric elseif chain")
local function falsy(c: boolean): any
return if c then nil else false
end
t:check(falsy(true) == nil and falsy(false) == false, "if-expression with falsy branches")
local function nested(a: boolean, b: boolean): number
return if a then (if b then 1 else 2) else if b then 3 else 4
end
t:check(nested(true, true) == 1 and nested(true, false) == 2 and nested(false, true) == 3 and nested(false, false) == 4, "nested if-expressions")
local v = 5
local arith = if v > 3 then v * 2 else v - 1
t:eq(arith, 10, "if-expression arithmetic")
-- continue, compound assignment, interpolation, floor division, literals
local odd = 0
for i = 1, 10 do
if i % 2 == 0 then
continue
end
odd += i
end
t:eq(odd, 25, "continue")
local s = "a"
s ..= "b"
local n = 7
n //= 2
n *= 3
n -= 1
n ^= 2
t:check(s == "ab" and n == 64, "compound assignment")
t:eq(`x={n} y={1 + 2}`, "x=64 y=3", "string interpolation")
t:eq(-7 // 2, -4, "floor division of a negative")
t:eq(7.5 // 2, 3, "floor division of a fraction")
t:eq(-7 % 3, 2, "modulo of a negative")
t:eq(0b1010 + 1_000 + 0xFF_FF, 10 + 1000 + 65535, "number literals")
t:eq(0x7FFFFFFF + 1, 2147483648, "no integer overflow")
end
t:done()
+178
View File
@@ -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()
+184
View File
@@ -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()
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 217 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 247 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 315 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 595 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 132 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 327 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 302 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 202 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 232 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 724 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 258 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 259 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 271 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 149 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 273 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 792 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 742 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 716 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 941 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 753 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 319 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 355 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 389 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 985 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 426 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 345 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 370 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 377 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 219 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 339 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 321 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 340 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 207 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 285 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 383 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 405 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 B

Loaded 100 of 226 files, more files were not shown because too many files have changed in this diff. Show more