-- tests/helpers.lua -- shared support for the hate-no-d test cases. -- -- Cases are plain Lua runs under ./hate-no-d (the embedded raylib + LuaJIT -- runtime). The module is cached here so a case can call helpers repeatedly -- without re-executing hate-no-d.lua. local ffi = require("ffi") local M = {} local ROOT = os.getenv("HATE_NO_D_ROOT") or "." local CACHE = {} --- Load (and cache) the generated binding. function M.load() if CACHE.M == nil then CACHE.M = dofile(ROOT .. "/hate-no-d.lua") -- Tests exercise thousands of loads; raylib's INFO logging would drown the -- assertions (and valgrind output). if CACHE.M.SetTraceLogLevel and CACHE.M.LOG_WARNING then CACHE.M.SetTraceLogLevel(CACHE.M.LOG_WARNING) end end return CACHE.M end --- The generated raylib module table (alias used by the spec's examples). function M.R() return M.load() end --- Run the Lua GC n times. function M.gc(n) for _ = 1, (n or 2) do collectgarbage("collect") end end --- M._ownership, with a clear error when the generator has not landed yet. function M.ow() local mod = M.load() local o = mod._ownership if type(o) ~= "table" then error("hate-no-d.lua has no M._ownership (regenerate the binding)", 2) end return o end function M.reset() M.ow().reset() end function M.snapshot() local o = M.ow() return { armed = o.armed, released = o.released, skipped = o.skipped, errors = o.errors, } end function M.delta(before, field) return M.ow()[field] - before[field] end function M.eq(a, b, msg) if a ~= b then error(string.format("%s: expected %s, got %s", msg or "assert", tostring(b), tostring(a)), 2) end end function M.ok(v, msg) if not v then error(msg or "assertion failed", 2) end end --- RSS in kB from /proc/self/status, or 0 when unavailable. function M.rss_kb() local f = io.open("/proc/self/status", "r") if f == nil then return 0 end local rss = 0 for line in f:lines() do local kb = line:match("^VmRSS:%s*(%d+)%s*kB") if kb then rss = tonumber(kb); break end end f:close() return rss end --- True when the runner told us there is no display. function M.headless() return os.getenv("HATE_NO_D_HEADLESS") == "1" end --- True when running under valgrind (RSS measurements are meaningless then). function M.under_valgrind() return os.getenv("HATE_NO_D_VALGRIND") == "1" end --- Ensure a window exists. Returns false (rather than failing) when the --- environment cannot provide one, so callers can print "skip ". function M.window(w, h) if M.headless() then return false end local R = M.load() if not R.C.IsWindowReady() then R.InitWindow(w or 64, h or 64, "hate-no-d test") end return R.C.IsWindowReady() and true or false end --- Ensure an audio device exists (same skip-friendly contract as window()). function M.audio() local R = M.load() if not R.IsAudioDeviceReady() then pcall(R.InitAudioDevice) end return R.IsAudioDeviceReady() and true or false end function M.skip(name, reason) if reason then print(string.format("skip %s (%s)", name, reason)) else print("skip " .. name) end end --- Accept either a Lua string (owned-string wrappers) or a cdata char* and --- return a Lua string. Used where lane A may still hand back a pointer. function M.tostr(x) if type(x) == "string" then return x end if x == nil then return nil end return ffi.string(x) end function M.out_int(v) local p = ffi.new("int[1]") if v ~= nil then p[0] = v end return p end function M.tmpfile(contents) local path = os.tmpname() local f = assert(io.open(path, "wb")) f:write(contents) f:close() return path end --- Build a PCM WAV in memory: mono, 44100 Hz, signed 16-bit, `samples` --- samples of silence. Returns (cdata uint8_t[44 + samples*2], size). function M.wav_bytes(samples) local data_size = samples * 2 local total = 44 + data_size local b = ffi.new("uint8_t[?]", total) local function put8(off, v) b[off] = v end local function put16(off, v) b[off] = v % 256 b[off + 1] = math.floor(v / 256) % 256 end local function put32(off, v) b[off] = v % 256 b[off + 1] = math.floor(v / 256) % 256 b[off + 2] = math.floor(v / 65536) % 256 b[off + 3] = math.floor(v / 16777216) % 256 end local function puts(off, s) for i = 1, #s do b[off + i - 1] = s:byte(i) end end puts(0, "RIFF") put32(4, 36 + data_size) puts(8, "WAVE") puts(12, "fmt ") put32(16, 16) -- fmt chunk size put16(20, 1) -- PCM put16(22, 1) -- mono put32(24, 44100) -- sample rate put32(28, 44100 * 2) -- byte rate put16(32, 2) -- block align put16(34, 16) -- bits per sample puts(36, "data") put32(40, data_size) -- samples stay zero (silence) return b, total end return M