A sweep for duplicated function bodies, CRC tables and base64 across src/Formats: - core/Crc32.msb / MSB_TABLE: the MSB-first 0x04C11DB7 CRC-32 that containers/Ogg (Page + Mux), containers/MpegTs and codecs/Bzip2 each built (test vectors CRC-32/MPEG-2 and /BZIP2 in tests/core/bytes). - audio/flac/Encode uses audio/flac/Frame's CRC-8 / CRC-16 tables (Frame.crc8 added) instead of its own copy. - core/Bytes.toBuffer / Bytes.span: the (buffer | string, offset, length) argument checking codecs/Deflate, codecs/Lzma and codecs/Brotli each carried. - codecs/Text.utf8char: one copy instead of four (codecs/Json, codecs/Xml, data/yaml, data/toml). - Types.keyLess / Types.isArray (small additions to Types, backward compatible): data/msgpack, data/cbor and data/yaml/Emit had identical copies. - playlist/pls: dead copies of m3u's attributes / extinf removed. Left on purpose: per-codec bit readers / writers in hot loops (STYLE.md: inner loops keep their own bit buffers), same-folder helpers (Alac signExtend, Vorbis ilog), Zip's DOS-date civil() (a different epoch helper).
204 lines
7.9 KiB
Luau
204 lines
7.9 KiB
Luau
-- 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, Bytes.span, the
|
|
-- MSB-first CRC-32 (MPEG-2 / bzip2 check values).
|
|
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
|
|
|
|
-- Bytes.span: codec argument checking
|
|
do
|
|
local b, o, n = Bytes.span("x", "hello", 1, 10)
|
|
t:check(type(b) == "buffer" and o == 1 and n == 4, "span clamps the length")
|
|
local _, _, _, e = Bytes.span("x", 5)
|
|
t:check(e and e.code == Err.ARG, "span: not a buffer")
|
|
local _, _, _, e2 = Bytes.span("x", "abc", 4)
|
|
t:check(e2 and e2.code == Err.ARG, "span: offset past the end")
|
|
local _, _, _, e3 = Bytes.span("x", "abc", 0.5)
|
|
t:check(e3 and e3.code == Err.ARG, "span: fractional offset")
|
|
end
|
|
-- the MSB-first CRC-32: CRC-32/MPEG-2 and CRC-32/BZIP2 of "123456789"
|
|
do
|
|
local nine = buffer.fromstring("123456789")
|
|
t:eq(Crc32.msb(nine, 0, 9, 0xFFFFFFFF), 0x0376E6E7, "CRC-32/MPEG-2")
|
|
t:eq(bit32.bxor(Crc32.msb(nine, 0, 9, 0xFFFFFFFF), 0xFFFFFFFF), 0xFC891918, "CRC-32/BZIP2")
|
|
end
|
|
|
|
t:done()
|