Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 226 additions & 0 deletions tests/test_volcanic_compare_arch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 FreePVR Contributors
"""
Clean-room tests for `volcanic_bitdiff.py --compare-arch / compare-arch`.

Covers DXT-48-1536 vs CXTP-48-1536 JSON capture-manifest pairing, framing-delta
summarization, text/markdown/JSON rendering, and graceful error handling.

Runs cleanly on any laptop without a phone or vendor binaries:
python3 -m unittest tests.test_volcanic_compare_arch -v
python3 -m unittest discover -s tests -v
"""

from __future__ import annotations

import json
import struct
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT / "tools" / "freeusc"))

import volcanic_bitdiff as vbd # noqa: E402

# Minimal clean-room framing vectors (valid Volcanic USC words, no vendor blobs).
USC_BASE = struct.pack("<3I", 0x0414A76B, 0x80408808, 0x8000031C) # FOP + CTRL.END
USC_OP_FLIPPED = struct.pack("<3I", 0x0414A76B ^ 0x00FC0000, 0x80408808, 0x8000031C) # opcode bits [23:18] flipped
USC_INT = struct.pack("<2I", 0x091023F0, 0x81854008) # INT
USC_LONG = USC_BASE + USC_INT # length + instruction delta vs USC_BASE
USC_DMA = struct.pack("<4I", 0x0005834E, 0x5808021E, 0x01000380, 0x83000004)


def _write_manifest(path: Path, device: str, probes: list) -> None:
path.write_text(json.dumps({"device_name": device, "probes": probes}))


class TestCompareArchPairing(unittest.TestCase):
def test_pairs_by_name_and_reports_only_in(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "dxt.json", Path(td) / "cxtp.json"
_write_manifest(pa, "DXT-48-1536", [{"name": "a", "usc_hex": USC_BASE.hex()}, {"name": "only_a", "usc_hex": USC_BASE.hex()}])
_write_manifest(pb, "CXTP-48-1536", [{"name": "a", "usc_hex": USC_BASE.hex()}, {"name": "only_b", "usc_hex": USC_BASE.hex()}])
rep = vbd.compare_arch_manifests(pa, pb)
self.assertEqual(rep["pairing"]["matched"], ["a"])
self.assertEqual(rep["pairing"]["only_in_a"], ["only_a"])
self.assertEqual(rep["pairing"]["only_in_b"], ["only_b"])
self.assertEqual(rep["pairing"]["matched_count"], 1)

def test_identical_vs_differing_and_bit_range(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "dxt.json", Path(td) / "cxtp.json"
_write_manifest(pa, "DXT-48-1536", [{"name": "same", "usc_hex": USC_BASE.hex()}, {"name": "op", "usc_hex": USC_BASE.hex()}])
_write_manifest(pb, "CXTP-48-1536", [{"name": "same", "usc_hex": USC_BASE.hex()}, {"name": "op", "usc_hex": USC_OP_FLIPPED.hex()}])
rep = vbd.compare_arch_manifests(pa, pb)
self.assertEqual(rep["aggregate"]["identical_probes"], 1)
self.assertEqual(rep["aggregate"]["differing_probes"], 1)
by = {p["probe"]: p for p in rep["per_probe"]}
self.assertTrue(by["same"]["identical"])
self.assertFalse(by["op"]["identical"])
self.assertEqual(by["op"]["diff"]["diff_word_count"], 1)
self.assertEqual(by["op"]["diff"]["word_diffs"][0]["bit_ranges"], ["[23:18]"])

def test_length_and_instruction_deltas(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "dxt.json", Path(td) / "cxtp.json"
_write_manifest(pa, "DXT-48-1536", [{"name": "grow", "usc_hex": USC_BASE.hex()}])
_write_manifest(pb, "CXTP-48-1536", [{"name": "grow", "usc_hex": USC_LONG.hex()}])
rep = vbd.compare_arch_manifests(pa, pb)
p = rep["per_probe"][0]
self.assertTrue(p["mismatched_byte_length"])
self.assertGreater(p["len_delta_bytes"], 0)
self.assertGreater(p["instruction_delta"], 0)
self.assertEqual(rep["aggregate"]["probes_with_length_mismatch"], 1)
self.assertEqual(rep["aggregate"]["total_len_delta_bytes"], p["len_delta_bytes"])

def test_family_histogram_and_opcode_shifts(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "dxt.json", Path(td) / "cxtp.json"
_write_manifest(pa, "DXT-48-1536", [{"name": "p", "usc_hex": USC_BASE.hex()}])
_write_manifest(pb, "CXTP-48-1536", [{"name": "p", "usc_hex": USC_INT.hex()}])
rep = vbd.compare_arch_manifests(pa, pb)
p = rep["per_probe"][0]
self.assertIn("family_histogram_a", p)
self.assertIn("family_histogram_b", p)
self.assertIn("family_delta", p)
self.assertIn("opcode_cluster_shifts", rep["aggregate"])
self.assertIn("INT", rep["aggregate"]["opcode_cluster_shifts"])

def test_aggregate_union_mask_and_report_forms(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "dxt.json", Path(td) / "cxtp.json"
_write_manifest(pa, "DXT-48-1536", [{"name": "p", "usc_hex": USC_BASE.hex()}])
_write_manifest(pb, "CXTP-48-1536", [{"name": "p", "usc_hex": USC_OP_FLIPPED.hex()}])
rep = vbd.compare_arch_manifests(pa, pb)
self.assertEqual(rep["aggregate"]["union_xor_mask"], "0x00fc0000")
self.assertEqual(rep["aggregate"]["union_bit_ranges"], ["[23:18]"])
txt = vbd.compare_report_to_text(rep)
md = vbd.compare_report_to_markdown(rep)
js = json.dumps(rep)
for s in (txt, md, js):
self.assertIn("p", s)
self.assertIn("Volcanic CXTP vs DXT", txt)
self.assertIn("# Volcanic CXTP vs DXT", md)


class TestCompareArchManifestForms(unittest.TestCase):
def test_words_payload_forms(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "a.json", Path(td) / "b.json"
words_a = [0x0414A76B, 0x80408808, 0x8000031C]
# hex-string, words, words_hex all encode the same bytes
for idx, probes in enumerate([
[{"name": "p", "usc_hex": USC_BASE.hex()}],
[{"name": "p", "words": words_a}],
[{"name": "p", "words_hex": ["0x0414A76B", "0x80408808", "0x8000031C"]}],
]):
_write_manifest(pa, "DXT-48-1536", probes)
_write_manifest(pb, "CXTP-48-1536", [{"name": "p", "usc_hex": USC_BASE.hex()}])
rep = vbd.compare_arch_manifests(pa, pb)
self.assertTrue(rep["per_probe"][0]["identical"], f"form {idx}")

def test_file_based_manifest_strips_vulkan_header(self) -> None:
with tempfile.TemporaryDirectory() as td:
d = Path(td)
# raw USC file + full Vulkan cache blob side by side
(d / "p.usc").write_bytes(USC_BASE)
hdr = vbd.VK_CACHE_HEADER_V1.pack(32, 1, vbd.IMG_VENDOR_ID, 0x71022448, bytes(range(16))) + struct.pack("<I", 1)
(d / "p.pipeline_cache.bin").write_bytes(hdr + USC_BASE)
# A manifest referencing raw file payload vs file referencing cache blob payload
# need two manifests each with one probe pointing at the respective files.
_write_manifest(d / "a.json", "DXT-48-1536", [{"name": "p", "file": "p.usc"}])
_write_manifest(d / "b.json", "CXTP-48-1536", [{"name": "p", "file": "p.pipeline_cache.bin"}])
rep = vbd.compare_arch_manifests(d / "a.json", d / "b.json")
self.assertTrue(rep["per_probe"][0]["identical"])

def test_empty_probe_lists_graceful(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "a.json", Path(td) / "b.json"
_write_manifest(pa, "DXT-48-1536", [])
_write_manifest(pb, "CXTP-48-1536", [{"name": "p", "usc_hex": USC_BASE.hex()}])
rep = vbd.compare_arch_manifests(pa, pb)
self.assertEqual(rep["pairing"]["matched"], [])
self.assertEqual(rep["pairing"]["only_in_b"], ["p"])
self.assertEqual(rep["aggregate"]["identical_probes"], 0)
self.assertIn("no matched probes", vbd.compare_report_to_text(rep).lower())


class TestCompareArchErrors(unittest.TestCase):
def test_missing_manifest_raises(self) -> None:
with self.assertRaises(FileNotFoundError):
vbd.compare_arch_manifests(Path("/tmp/does-not-exist-dxt.json"), Path("/tmp/does-not-exist-cxtp.json"))

def test_malformed_json_raises(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "a.json", Path(td) / "b.json"
pa.write_text("{not json")
pb.write_text("{}")
with self.assertRaises(ValueError):
vbd.compare_arch_manifests(pa, pb)

def test_missing_probes_key_raises(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "a.json", Path(td) / "b.json"
pa.write_text(json.dumps({"no_list_here": 1}))
pb.write_text(json.dumps({"no_list_here": 2}))
with self.assertRaises(ValueError):
vbd.compare_arch_manifests(pa, pb)

def test_mismatched_lengths_do_not_raise(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "a.json", Path(td) / "b.json"
_write_manifest(pa, "DXT-48-1536", [{"name": "p", "usc_hex": USC_BASE.hex()}])
_write_manifest(pb, "CXTP-48-1536", [{"name": "p", "usc_hex": USC_DMA.hex()}])
rep = vbd.compare_arch_manifests(pa, pb)
self.assertEqual(len(rep["per_probe"]), 1)

def test_cli_missing_manifest_nonzero_exit(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa = Path(td) / "exists.json"
_write_manifest(pa, "DXT-48-1536", [{"name": "p", "usc_hex": USC_BASE.hex()}])
proc = subprocess.run(
[sys.executable, str(REPO_ROOT / "tools" / "freeusc" / "volcanic_bitdiff.py"),
"compare-arch", str(pa), str(Path(td) / "missing.json")],
capture_output=True, text=True, timeout=30,
)
self.assertNotEqual(proc.returncode, 0)
self.assertIn("error", proc.stderr.lower())

def test_cli_json_and_text_formats(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "a.json", Path(td) / "b.json"
_write_manifest(pa, "DXT-48-1536", [{"name": "p", "usc_hex": USC_BASE.hex()}])
_write_manifest(pb, "CXTP-48-1536", [{"name": "p", "usc_hex": USC_OP_FLIPPED.hex()}])
cli = [sys.executable, str(REPO_ROOT / "tools" / "freeusc" / "volcanic_bitdiff.py")]
for args, marker in [
(["compare-arch", str(pa), str(pb)], "union_xor_mask"),
(["compare-arch", str(pa), str(pb), "--format", "text"], "Volcanic CXTP"),
(["compare-arch", str(pa), str(pb), "--format", "markdown"], "# Volcanic CXTP"),
(["--compare-arch", str(pa), str(pb), "--format", "json"], "matched_count"),
(["--compare-arch", str(pa), str(pb), "--json"], "matched_count"),
]:
proc = subprocess.run(cli + args, capture_output=True, text=True, timeout=30)
self.assertEqual(proc.returncode, 0, proc.stderr)
self.assertIn(marker, proc.stdout)

def test_cli_empty_manifest_graceful_exit_zero(self) -> None:
with tempfile.TemporaryDirectory() as td:
pa, pb = Path(td) / "a.json", Path(td) / "b.json"
_write_manifest(pa, "DXT-48-1536", [])
_write_manifest(pb, "CXTP-48-1536", [])
proc = subprocess.run(
[sys.executable, str(REPO_ROOT / "tools" / "freeusc" / "volcanic_bitdiff.py"),
"compare-arch", str(pa), str(pb), "--format", "text"],
capture_output=True, text=True, timeout=30,
)
self.assertEqual(proc.returncode, 0, proc.stderr)
self.assertIn("Matched probes: 0", proc.stdout)


if __name__ == "__main__":
unittest.main()
Loading
Loading