| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| //! Structured JSON and human-readable summary reports for `anneal-cpp-diff`. | |
| use std::fmt::Write as _; | |
| use serde::{Deserialize, Serialize}; | |
| use crate::{ | |
| corpus::{Category, Language}, | |
| verdict::{CaseExpectation, LegOutcome, Verdict}, | |
| }; | |
| /// Schema identifier for serialized `anneal-cpp-diff` JSON reports. | |
| pub const REPORT_SCHEMA: &str = "anneal-cpp-diff/v1"; | |
| /// Per-case differential result record. | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| pub struct CaseReport { | |
| pub id: String, | |
| pub category: Category, | |
| pub language: Language, | |
| pub expectation: CaseExpectation, | |
| pub verdict: Verdict, | |
| pub lean_outcome: LegOutcome, | |
| pub primary_compiler_outcome: LegOutcome, | |
| #[serde(skip_serializing_if = "Option::is_none")] | |
| pub secondary_compiler_outcome: Option<LegOutcome>, | |
| } | |
| /// Aggregated counts over one differential oracle run. | |
| #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| pub struct SuiteSummary { | |
| pub total: usize, | |
| pub passed: usize, | |
| pub failed: usize, | |
| pub defined_match: usize, | |
| pub defined_mismatch: usize, | |
| pub strict_ub_confirmed: usize, | |
| pub strict_ub_ubsan_confirmed: usize, | |
| pub strict_ub_lean_beyond_ubsan: usize, | |
| pub expected_unsupported: usize, | |
| pub unexpected_error: usize, | |
| } | |
| impl SuiteSummary { | |
| /// Tally counts from a slice of [`CaseReport`]s. | |
| pub fn from_cases(cases: &[CaseReport]) -> Self { | |
| let mut s = Self::default(); | |
| for c in cases { | |
| s.total += 1; | |
| if c.verdict.is_pass() { | |
| s.passed += 1; | |
| } else { | |
| s.failed += 1; | |
| } | |
| match &c.verdict { | |
| Verdict::DefinedMatch { .. } => s.defined_match += 1, | |
| Verdict::DefinedMismatch { .. } => s.defined_mismatch += 1, | |
| Verdict::StrictUbConfirmed { ubsan_confirmed, .. } => { | |
| s.strict_ub_confirmed += 1; | |
| if *ubsan_confirmed { | |
| s.strict_ub_ubsan_confirmed += 1; | |
| } else { | |
| s.strict_ub_lean_beyond_ubsan += 1; | |
| } | |
| } | |
| Verdict::ExpectedUnsupported { .. } => s.expected_unsupported += 1, | |
| Verdict::UnexpectedError { .. } => s.unexpected_error += 1, | |
| } | |
| } | |
| s | |
| } | |
| /// Returns `true` iff at least one case ran and every case passed. | |
| pub const fn all_passed(&self) -> bool { | |
| self.total > 0 && self.failed == 0 | |
| } | |
| } | |
| /// Complete differential oracle report suitable for JSON serialization or | |
| /// terminal presentation. | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| pub struct DiffReport { | |
| pub schema: String, | |
| pub primary_compiler: String, | |
| #[serde(skip_serializing_if = "Option::is_none")] | |
| pub secondary_compiler: Option<String>, | |
| pub summary: SuiteSummary, | |
| pub cases: Vec<CaseReport>, | |
| } | |
| impl DiffReport { | |
| /// Construct a [`DiffReport`] and compute its [`SuiteSummary`]. | |
| pub fn new( | |
| primary_compiler: impl Into<String>, | |
| secondary_compiler: Option<String>, | |
| cases: Vec<CaseReport>, | |
| ) -> Self { | |
| let summary = SuiteSummary::from_cases(&cases); | |
| Self { | |
| schema: REPORT_SCHEMA.to_owned(), | |
| primary_compiler: primary_compiler.into(), | |
| secondary_compiler, | |
| summary, | |
| cases, | |
| } | |
| } | |
| /// Serialize this report as pretty-printed JSON. | |
| pub fn to_json_pretty(&self) -> Result<String, serde_json::Error> { | |
| serde_json::to_string_pretty(self) | |
| } | |
| /// Render a concise human-readable summary plus any failing case details. | |
| pub fn format_human(&self) -> String { | |
| let mut out = String::new(); | |
| let s = &self.summary; | |
| let _ = writeln!( | |
| out, | |
| "anneal-cpp-diff ({}) — total={} passed={} failed={}", | |
| self.schema, s.total, s.passed, s.failed | |
| ); | |
| let _ = writeln!( | |
| out, | |
| " compilers: primary={}{}", | |
| self.primary_compiler, | |
| self.secondary_compiler | |
| .as_deref() | |
| .map_or_else(String::new, |sec| format!(", secondary={sec}")) | |
| ); | |
| let _ = writeln!( | |
| out, | |
| " verdicts: defined_match={} strict_ub_confirmed={} (ubsan={}, lean_stricter={}) expected_unsupported={} defined_mismatch={} unexpected_error={}", | |
| s.defined_match, | |
| s.strict_ub_confirmed, | |
| s.strict_ub_ubsan_confirmed, | |
| s.strict_ub_lean_beyond_ubsan, | |
| s.expected_unsupported, | |
| s.defined_mismatch, | |
| s.unexpected_error | |
| ); | |
| let failures: Vec<&CaseReport> = | |
| self.cases.iter().filter(|c| !c.verdict.is_pass()).collect(); | |
| if !failures.is_empty() { | |
| out.push_str("\nFAILURES:\n"); | |
| for c in failures { | |
| let _ = writeln!(out, " [{}] {}: {:?}", c.verdict.tag(), c.id, c.verdict); | |
| let _ = writeln!(out, " lean: {:?}", c.lean_outcome); | |
| let _ = writeln!(out, " primary: {:?}", c.primary_compiler_outcome); | |
| if let Some(sec) = &c.secondary_compiler_outcome { | |
| let _ = writeln!(out, " second: {sec:?}"); | |
| } | |
| } | |
| } | |
| out | |
| } | |
| } |