#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """snes-spc's smoke test: no game music, only an .spc file built here. python3 -I tests/smoke.py Part 1 needs nothing: ``build.sh --version`` names the tool and the pinned archive, and an unknown option is a usage error (2). Part 2 needs ``build/spc_dump`` (``build.sh``): it writes an .spc file whose SPC700 program is an endless loop with every DSP register zero, plays 1000 samples, and checks the sizes of the three outputs, that the sound is silence, that the final state is an .spc file of 66,048 bytes, and that a second run gives the same bytes. """ from __future__ import annotations import subprocess import sys import tempfile from pathlib import Path HERE = Path(__file__).resolve().parents[1] SPC_DUMP = HERE / "build" / "spc_dump" failures: list[str] = [] def check(ok: bool, what: str) -> None: print(("ok " if ok else "FAIL ") + what) if not ok: failures.append(what) def spc_file() -> bytes: """An .spc image (the published SPC file format): the signature, PC $0200, SP $EF, 64 KiB of RAM holding BRA $0200 at $0200, the 128 DSP registers all zero.""" data = bytearray(0x10200) data[0:33] = b"SNES-SPC700 Sound File Data v0.30" data[0x21:0x23] = bytes([26, 26]) data[0x23] = 27 # no ID666 tag data[0x24] = 30 data[0x25:0x27] = (0x0200).to_bytes(2, "little") # PC data[0x2B] = 0xEF # SP ram = 0x100 data[ram + 0x0200 : ram + 0x0202] = bytes([0x2F, 0xFE]) # BRA -2 return bytes(data) def main() -> int: out = subprocess.run(["sh", str(HERE / "build.sh"), "--version"], capture_output=True, text=True) check(out.returncode == 0 and out.stdout.startswith("snes-spc ") and "dbad8273" in out.stdout, "build.sh --version names the tool and the archive") # fmt: skip out = subprocess.run(["sh", str(HERE / "build.sh"), "--nonsense"], capture_output=True, text=True) check(out.returncode == 2, "an unknown option is a usage error (2)") if not SPC_DUMP.exists(): print("skip part 2: no build/spc_dump (run build.sh)") else: with tempfile.TemporaryDirectory(prefix="snes-spc-smoke-") as scratch: tmp = Path(scratch) (tmp / "loop.spc").write_bytes(spc_file()) runs = [] for n in (1, 2): raw, regs, final = (tmp / f"{n}.raw", tmp / f"{n}.regs", tmp / f"{n}.spc") done = subprocess.run([str(SPC_DUMP), str(tmp / "loop.spc"), "1000", str(raw), "--regs", str(regs), "--final", str(final)], capture_output=True) # fmt: skip check(done.returncode == 0, f"run {n}: spc_dump exits 0") runs.append(tuple(p.read_bytes() if p.exists() else b"" for p in (raw, regs, final))) raw, regs, final = runs[0] check(len(raw) == 4000 and not any(raw), "1000 samples, 4 bytes each, all silence") check(len(regs) == 1000 * 128, "128 DSP register bytes after every sample") # The library saves the state, not the signature (OBSERVED: the first 37 bytes zero). check(len(final) == 0x10200 and final[0x300:0x302] == bytes([0x2F, 0xFE]), "the final state: an .spc image of 66,048 bytes, the loop still in RAM") # fmt: skip check(runs[0] == runs[1], "a second run gives the same bytes") print(f"{len(failures)} failed" if failures else "smoke passed") return 1 if failures else 0 if __name__ == "__main__": raise SystemExit(main())