Files

85 lines
3.0 KiB
Luau

-- audio/ape tests: tools/fixtures/ape.py's files (mac -c1000 .. -c5000, a FATE 3.99 sample) bit-exact against `mac -d`.
-- Fixtures: tools/fixtures/ape.py.
local T = require("@tests/T")
local Ape = require("@rosetta/audio/ape")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local F = require("@rosetta")
local Cases = require("@tests/gen/ape_cases")
local L = require("@tests/audio/_lossless")
local t = T.suite("ape")
local byName = {}
for _, c in Cases do
byName[c.name] = c
end
local function open(b: any, opts: any?): (any, any)
return L.open(Ape, b, opts)
end
t:eq(Ape.name, "ape", "name")
t:check(Ape.caps.decode and not Ape.caps.encode, "caps")
local seekChecks = L.cases(t, Ape, Cases)
t:note("%d fixtures bit-exact against mac -d, %d seeks", #Cases, seekChecks)
for _, name in { "s16_c2000", "tags", "f32", "ch6" } do
L.subrange(t, Ape, byName[name], "ape")
end
do
local d = open(byName.s16_c5000.data)
t:eq(d.level, "insane", "level")
t:eq(d.bitsPerSample, 16, "bits")
t:check(d.version >= 3990, "version")
t:eq(open(byName.s24_c4000.data).level, "extra high", "level 4000")
t:eq(open(byName.s16_c1000.data).level, "fast", "level 1000")
end
-- an ID3v2 tag in front of the descriptor (the seek table counts from the descriptor)
do
local fx = byName.s16_c3000
local id3 = "ID3\4\0\0\0\0\0\20" .. "TIT2\0\0\0\10\0\0\3hello id3"
local b = buffer.fromstring(id3 .. fx.data)
t:eq(Ape.sniff(Bytes.reader(b)), 1, "sniff ID3 + MAC")
local dec = open(b)
t:check(dec and (L.all(dec, fx)) == fx.crc, "ID3v2 in front")
end
do
local fx = byName.s16_c2000
local _ = fx
t:eq(F.detect(buffer.fromstring(fx.data)), "ape", "detect")
local a = F.audio.load(fx.data)
t:check(a and a.frames == fx.frames and a.meta.source.format == "ape", "F.audio.load")
end
-- errors: a damaged frame fails its CRC, bad headers
do
local fx = byName.s16_long -- two frames: the second is damaged
local b = buffer.fromstring(fx.data)
local at = #fx.data - 2000
buffer.writeu8(b, at, bit32.bxor(buffer.readu8(b, at), 0x40))
local dec = open(b)
local _, got = dec:read(fx.frames)
t:check(dec.error and (dec.error.code == Err.CHECKSUM or dec.error.code == Err.CORRUPT) and got == 73728, "damaged frame: " .. tostring(dec.error))
local _, e0 = open(buffer.fromstring(string.sub(fx.data, 1, 2000) .. string.rep("\0", 100)))
t:check(e0 ~= nil, "a broken first frame fails open: " .. tostring(e0))
local _, e1 = open("MAC ")
t:check(e1 and Err.is(e1), "too short: " .. tostring(e1))
local old = buffer.fromstring(fx.data)
buffer.writeu16(old, 4, 3800)
local _, e2 = open(old)
t:check(e2 and e2.code == Err.UNSUPPORTED, "old version: " .. tostring(e2))
t:eq(Ape.sniff(Bytes.reader(buffer.fromstring("RIFF"))), 0, "sniff RIFF")
end
local samples = {}
for _, name in { "s16_c2000", "s16_c4000", "mono", "s24", "s32", "f32", "ch6", "silence", "pseudo_stereo" } do
table.insert(samples, buffer.fromstring(byName[name].data))
end
t:fuzz("decode", function(b: buffer)
return Ape.decode(Bytes.reader(b))
end, samples, { mutations = 50 })
t:done()