Files
rosetta/tests/T.luau
T

128 lines
4.3 KiB
Luau

--!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