172 lines
5.3 KiB
Luau
172 lines
5.3 KiB
Luau
-- core/Bits and codecs/entropy/Huffman tests: bit readers / writers, canonical codes, length limits, decoding tables.
|
|
local T = require("@tests/T")
|
|
local Bits = require("@rosetta/core/Bits")
|
|
local Huffman = require("@rosetta/codecs/entropy/Huffman")
|
|
local Err = require("@rosetta/core/Err")
|
|
|
|
local t = T.suite("bits")
|
|
local rand = T.rng(7)
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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()
|