126 lines
4.7 KiB
Luau
126 lines
4.7 KiB
Luau
-- model/qb tests: Qubicle Binary files of every option against the fixture's reference reader / writer.
|
|
-- Fixtures: tools/fixtures/qb.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@rosetta")
|
|
local Qb = require("@rosetta/model/qb")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Types = require("@rosetta/Types")
|
|
local Fx = require("@tests/gen/qb")
|
|
|
|
local t = T.suite("qb")
|
|
|
|
t:eq(Qb.name, "qb", "name")
|
|
t:check(Qb.caps.decode and Qb.caps.encode, "caps")
|
|
|
|
local function dec(data: string | buffer): (Types.Scene?, any)
|
|
return Qb.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data))
|
|
end
|
|
|
|
local function check(scene: Types.Scene, want: any, label: string)
|
|
t:eq(#scene.nodes, #want.matrices, label .. ": matrices")
|
|
for i, m in want.matrices do
|
|
local node = scene.nodes[i]
|
|
local v = (scene.voxels :: { Types.Voxels })[node.voxels :: number]
|
|
t:eq(node.name, m.name, label .. " " .. i .. ": name")
|
|
t:eq(table.concat(v.size, ","), table.concat(m.size, ","), label .. " " .. i .. ": size")
|
|
t:eq(table.concat(node.translation :: { number }, ","), table.concat(m.pos, ","), label .. " " .. i .. ": position")
|
|
t:eq(Crc32.of(v.colors :: buffer), m.crc, label .. " " .. i .. ": colours")
|
|
local filled = 0
|
|
for k = 0, buffer.len(v.data) - 1 do
|
|
if buffer.readu8(v.data, k) ~= 0 then
|
|
filled += 1
|
|
end
|
|
end
|
|
t:eq(filled, m.filled, label .. " " .. i .. ": filled cells")
|
|
end
|
|
end
|
|
|
|
for _, c in Fx.cases do
|
|
local s, err = dec(c.data)
|
|
t:check(s ~= nil, c.name .. ": " .. tostring(err))
|
|
check(s :: Types.Scene, c, c.name)
|
|
local src = (s :: Types.Scene).meta.source
|
|
t:check(src.colorFormat == c.colorFormat and src.rightHanded == c.rightHanded and src.compressed == c.compressed and src.visibilityMask == c.visibilityMask, c.name .. ": settings")
|
|
t:check(buffer.tostring(Qb.encode(s) :: buffer) == c.data, c.name .. ": same bytes again")
|
|
-- the palette view: exact colours when there are up to 255
|
|
local v = ((s :: Types.Scene).voxels :: { Types.Voxels })[1]
|
|
local exact = true
|
|
for k = 0, buffer.len(v.data) - 1 do
|
|
local idx = buffer.readu8(v.data, k)
|
|
if idx > 0 then
|
|
local p, col = v.palette[idx], buffer.readu32(v.colors :: buffer, k * 4)
|
|
if p[1] + p[2] * 256 + p[3] * 65536 ~= col % 16777216 then
|
|
exact = false
|
|
end
|
|
end
|
|
end
|
|
t:eq(exact, c.name ~= "case5", c.name .. ": palette exact up to 255 colours")
|
|
t:eq(F.detect(c.data), "qb", c.name .. ": detect")
|
|
end
|
|
|
|
for name, want in Fx.ours do
|
|
local base = string.match(name, "^(case%d)_")
|
|
if base then
|
|
local c
|
|
for _, x in Fx.cases do
|
|
if x.name == base then
|
|
c = x
|
|
end
|
|
end
|
|
t:check(c ~= nil, name .. ": source")
|
|
-- the same cells whatever the settings (scene coordinates)
|
|
for i, m in want.matrices do
|
|
t:eq(m.crc, c.matrices[i].crc, name .. " " .. i .. ": colours read back")
|
|
t:eq(table.concat(m.pos, ","), table.concat(c.matrices[i].pos, ","), name .. " " .. i .. ": position read back")
|
|
end
|
|
if string.find(name, "_flip") then
|
|
t:check(want.rightHanded ~= c.rightHanded and want.compressed ~= c.compressed, name .. ": flipped settings")
|
|
end
|
|
end
|
|
end
|
|
do
|
|
local m = Fx.ours.palette_mask
|
|
t:check(m.visibilityMask and m.matrices[1].filled == 15, "palette grid with a mask")
|
|
local plain = Fx.ours.palette_plain
|
|
t:check(not plain.visibilityMask and plain.rightHanded and plain.compressed, "defaults")
|
|
-- our decode of the masked file: the centre cell (hidden on every side) has mask 1
|
|
local v = Types.Voxels.new(3, 3, 3)
|
|
v.palette[1] = { 200, 10, 10, 255 }
|
|
for i = 0, 26 do
|
|
buffer.writeu8(v.data, i, 1)
|
|
end
|
|
local s = assert(dec(assert(Qb.encode(v, { visibilityMask = true }))))
|
|
local colors = ((s.voxels :: { Types.Voxels })[1]).colors :: buffer
|
|
t:eq(buffer.readu8(colors, 13 * 4 + 3), 1, "hidden cell mask")
|
|
t:eq(buffer.readu8(colors, 0 * 4 + 3), 1 + 2 + 16 + 64, "corner cell mask (left, bottom, back)")
|
|
end
|
|
|
|
do
|
|
local s = assert(F.load(Fx.cases[1].data))
|
|
t:check(s.kind == "scene", "F.load")
|
|
t:check(F.save(s, nil, { format = "vox" }) ~= nil, "qb -> vox")
|
|
t:check(F.save(s, nil, { format = "schem" }) ~= nil, "qb -> schem")
|
|
end
|
|
|
|
do
|
|
local _, e1 = dec("\1\1\0\0")
|
|
t:check(e1 and e1.code == Err.TRUNCATED, "truncated header")
|
|
local _, e2 = dec("\2\1\0\0" .. string.rep("\0", 20))
|
|
t:check(e2 and e2.code == Err.CORRUPT, "bad version")
|
|
local _, e3 = dec(string.sub(Fx.cases[1].data, 1, 60))
|
|
t:check(e3 and e3.code == Err.TRUNCATED, "truncated matrix")
|
|
local _, e4 = Qb.encode({} :: any)
|
|
t:check(e4 and e4.code == Err.ARG, "encode nothing")
|
|
end
|
|
|
|
local samples = {}
|
|
for _, c in Fx.cases do
|
|
table.insert(samples, c.data)
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Qb.decode(Bytes.reader(b))
|
|
end, samples)
|
|
t:done()
|