Files
lucisv/crates/vlog_formatter/tests/verification.rs
T
lucioandClaude Fable 5 8effef9b6b Add safety-net: runtime equivalence + convergence verification
format_source now verifies every result before returning it, per
Verible's practice: the output must re-lex to the same token stream
as the input (comments included, whitespace ignored) and formatting
it a second time must be a fixed point. Any failure returns
FormatError::Verification and the caller keeps the original text —
"never corrupt code" and idempotency are now runtime guarantees.

Normalizations (documented in the feature README): multi-line
comments/directives/attributes compare line-trimmed since the
formatter re-indents them; @(*)/@* is a whitelisted canonicalization.

The net already catches three real pre-existing bugs (closing-label
drop, block-comment re-indent drift, sens-list comment drop), which
now refuse output with exit 2 instead of corrupting files. Fixes
queued as the next feature. Checks off TODO.md §1 items 1-3.

Dev: Opus agent; QA: Sonnet agent (17 adversarial pairs, all caught);
review in docs/features/safety-net/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-18 23:40:59 -07:00

200 lines
7.2 KiB
Rust

//! Tests for the runtime safety net: token-stream equivalence, convergence,
//! and the failsafe behavior of `format_source`.
use vlog_formatter::{
format_source, verify_convergence, verify_equivalence, Config, FormatError, VerificationError,
};
fn expect_divergence(input: &str, output: &str) -> VerificationError {
match verify_equivalence(input, output) {
Ok(()) => panic!("expected a divergence between\n{input}\n---\n{output}"),
Err(e) => e,
}
}
#[test]
fn identical_streams_are_equivalent() {
let src = "module m;\n assign x = a & b;\nendmodule\n";
verify_equivalence(src, src).expect("a text is equivalent to itself");
}
#[test]
fn whitespace_only_differences_are_equivalent() {
let input = "module m(input a,output b);assign b=~a;endmodule";
let output = "module m (input a, output b);\n assign b = ~a;\nendmodule\n";
verify_equivalence(input, output).expect("whitespace is not part of the token stream");
}
#[test]
fn comments_are_part_of_the_stream_but_may_be_reindented() {
let input = "module m;\n// note\nwire x;/* block */\nendmodule\n";
let output = "module m;\n // note\n wire x; /* block */\nendmodule\n";
verify_equivalence(input, output).expect("comment text survived");
}
#[test]
fn multiline_block_comment_may_be_reindented() {
let input = "module m;\n/* one\n two\n three */\nwire x;\nendmodule\n";
let output = "module m;\n /* one\n two\n three */\n wire x;\nendmodule\n";
verify_equivalence(input, output).expect("only per-line indentation changed");
}
#[test]
fn differing_token_is_rejected() {
let err = expect_divergence("assign x = a;\n", "assign x = b;\n");
match err {
VerificationError::Divergence { index, ref expected, ref got } => {
assert_eq!(index, 3);
assert_eq!(expected.as_ref().expect("expected item").text, "a");
assert_eq!(got.as_ref().expect("got item").text, "b");
}
other => panic!("wrong error: {other}"),
}
let rendered = err.to_string();
assert!(rendered.contains("index 3"), "{rendered}");
assert!(rendered.contains('a') && rendered.contains('b'), "{rendered}");
}
#[test]
fn dropped_comment_is_rejected() {
let input = "module m;\n // keep me\n wire x;\nendmodule\n";
let output = "module m;\n wire x;\nendmodule\n";
match expect_divergence(input, output) {
VerificationError::Divergence { expected, got, .. } => {
let expected = expected.expect("expected item");
assert_eq!(expected.text, "// keep me");
assert_eq!(got.expect("got item").text, "wire");
}
other => panic!("wrong error: {other}"),
}
}
#[test]
fn dropped_trailing_comment_is_rejected() {
let input = "module m;\n wire x; // trailing\nendmodule\n";
let output = "module m;\n wire x;\nendmodule\n";
assert!(matches!(expect_divergence(input, output), VerificationError::Divergence { .. }));
}
#[test]
fn comment_at_end_of_file_is_rejected_if_dropped() {
let input = "module m;\nendmodule\n// last word\n";
let output = "module m;\nendmodule\n";
assert!(matches!(expect_divergence(input, output), VerificationError::Divergence { .. }));
}
#[test]
fn truncated_output_is_rejected() {
let input = "module m;\n wire x;\nendmodule\n";
let output = "module m;\n wire x;\n";
match expect_divergence(input, output) {
VerificationError::Divergence { expected, got, .. } => {
assert_eq!(expected.expect("expected item").text, "endmodule");
assert!(got.is_none());
}
other => panic!("wrong error: {other}"),
}
}
#[test]
fn extra_token_in_output_is_rejected() {
let input = "module m;\nendmodule\n";
let output = "module m;\nendmodule\nmodule n;\nendmodule\n";
match expect_divergence(input, output) {
VerificationError::Divergence { expected, got, .. } => {
assert!(expected.is_none());
assert_eq!(got.expect("got item").text, "module");
}
other => panic!("wrong error: {other}"),
}
}
#[test]
fn same_text_different_kind_is_rejected() {
// `x` as an identifier vs. `"x"` as a string literal.
assert!(matches!(
expect_divergence("assign a = x;\n", "assign a = \"x\";\n"),
VerificationError::Divergence { .. }
));
}
#[test]
fn unlexable_output_is_reported() {
match verify_equivalence("module m;\nendmodule\n", "module m;\n\"unterminated\n") {
Err(VerificationError::Lex { side, .. }) => {
assert_eq!(side.to_string(), "output");
}
other => panic!("expected a lex error, got {other:?}"),
}
}
#[test]
fn implicit_sensitivity_spellings_are_equivalent() {
let input = "module m;\n always @* y = a;\nendmodule\n";
let output = "module m;\n always @(*) y = a;\nendmodule\n";
verify_equivalence(input, output).expect("`@*` and `@(*)` are the same construct");
verify_equivalence(output, input).expect("the canonicalization is symmetric");
}
#[test]
fn convergence_accepts_a_fixed_point() {
verify_convergence("module m;\nendmodule\n", "module m;\nendmodule\n").expect("fixed point");
}
#[test]
fn convergence_reports_the_first_differing_line() {
match verify_convergence(
"module m;\n wire x;\nendmodule\n",
"module m;\n wire x;\nendmodule\n",
) {
Err(VerificationError::NotConverged { line, first, second }) => {
assert_eq!(line, 2);
assert_eq!(first, " wire x;");
assert_eq!(second, " wire x;");
}
other => panic!("expected a convergence failure, got {other:?}"),
}
}
#[test]
fn convergence_reports_a_truncated_second_pass() {
match verify_convergence("module m;\nendmodule\n", "module m;\n") {
Err(VerificationError::NotConverged { line, second, .. }) => {
assert_eq!(line, 2);
assert_eq!(second, "<end of file>");
}
other => panic!("expected a convergence failure, got {other:?}"),
}
}
#[test]
fn format_source_runs_both_checks_and_succeeds_on_real_input() {
let src = "// header\n\
module m #(parameter W = 8) (input clk, output reg [W-1:0] q);\n\
always @(posedge clk) q <= q + 1; // count\n\
endmodule\n";
let out = format_source(src, &Config::default()).expect("formats cleanly");
verify_equivalence(src, &out).expect("equivalent");
verify_convergence(&out, &format_source(&out, &Config::default()).expect("second pass"))
.expect("converged");
}
#[test]
fn format_source_reports_source_errors_as_source_errors() {
match format_source("module m;\n", &Config::default()) {
Err(FormatError::Source(_)) => {}
other => panic!("expected a parse error, got {other:?}"),
}
}
#[test]
fn format_source_would_reject_a_non_equivalent_output() {
// The real formatter is expected never to produce such output, so the
// failsafe path is exercised through the checker with a crafted pair:
// this is exactly the call `format_source` makes before returning.
let src = "module m;\n wire x; // dropped by a hypothetical bug\nendmodule\n";
let bad_output = "module m;\n wire x;\nendmodule\n";
let err = FormatError::Verification(expect_divergence(src, bad_output));
assert!(err.to_string().starts_with("verification failed:"), "{err}");
}