//! One conformance manifest against its reference trace. //! //! The comparison is the one in z80-rust-fast's `tests/rung1.rs`: every //! record, every field, in order, ignoring only the `mnemonic` and //! `operands` text an external producer omits. Nothing is loosened here. use serde_json::Value; use z80_rust::conformance::{manifest_from_json, trace_manifest}; /// The outcome of running one manifest and comparing it with its reference. #[derive(Clone, Debug, PartialEq, Eq)] pub struct CheckResult { /// Records this core produced. pub produced: usize, /// Records in the reference trace. pub expected: usize, /// Why the run stopped (`halted`, `max_steps`, `at_pc`, `cpm_exit`, `fault`). pub reason: String, /// T-states the run took. pub t_states: u64, /// The first record that differs, if any: index, ours, theirs. A count /// mismatch with all common records equal reports the first missing index. pub first_difference: Option, } #[derive(Clone, Debug, PartialEq, Eq)] pub struct Difference { pub index: usize, pub produced: Option, pub expected: Option, } impl CheckResult { pub fn identical(&self) -> bool { self.first_difference.is_none() && self.produced == self.expected } pub fn to_json(&self) -> String { let difference = match &self.first_difference { None => Value::Null, Some(d) => serde_json::json!({ "index": d.index, "produced": d.produced, "expected": d.expected, }), }; serde_json::json!({ "identical": self.identical(), "produced": self.produced, "expected": self.expected, "reason": self.reason, "t_states": self.t_states, "first_difference": difference, }) .to_string() } } /// Parse a reference trace (JSON Lines) the way `rung1.rs` does: blank lines /// skipped, `instruction.mnemonic` and `instruction.operands` removed. pub fn parse_reference(reference_jsonl: &str) -> Result, String> { let mut records = Vec::new(); for (line_number, line) in reference_jsonl.lines().enumerate() { if line.trim().is_empty() { continue; } let mut record: Value = serde_json::from_str(line) .map_err(|error| format!("reference line {}: {error}", line_number + 1))?; if let Some(instruction) = record["instruction"].as_object_mut() { instruction.remove("mnemonic"); instruction.remove("operands"); } records.push(record); } Ok(records) } /// Run `manifest_json` on the core and compare with `reference_jsonl`. /// /// Manifests whose memory segments are inline `data` need no file system; /// a `file` segment is an error on targets without one. pub fn check_manifest(manifest_json: &str, reference_jsonl: &str) -> Result { let value: Value = serde_json::from_str(manifest_json).map_err(|error| format!("manifest: {error}"))?; let manifest = manifest_from_json(&value, None)?; let expected = parse_reference(reference_jsonl)?; let mut produced: Vec = Vec::new(); let run = trace_manifest(&manifest, |record| { let value: Value = serde_json::from_str(&record.to_json()).expect("a StepRecord serializes to valid JSON"); produced.push(value); Ok(()) }) .map_err(|error| format!("run: {error}"))?; let first_difference = produced .iter() .zip(&expected) .position(|(mine, theirs)| mine != theirs) .or_else(|| { (produced.len() != expected.len()).then_some(produced.len().min(expected.len())) }) .map(|index| Difference { index, produced: produced.get(index).cloned(), expected: expected.get(index).cloned(), }); Ok(CheckResult { produced: produced.len(), expected: expected.len(), reason: run.reason.as_str().to_string(), t_states: run.t_states, first_difference, }) }